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
5eacecc96847fff60e33486ee5942517c8c5d6bc
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/RightCancellativeOp.lean
876b9f38e851520fe683c9c68abd0b69087146a3
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
9,088
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section RightCancellativeOp structure RightCancellativeOp (A : Type) : Type := (op : (A → (A → A))) (rinv : (A → (A → A))) (rightCancelOp : (∀ {x y : A} , (rinv (op y x) x) = y)) open RightCancellativeOp structure Sig (AS : Type) : Type := (opS : (AS → (AS → AS))) (rinvS : (AS → (AS → AS))) structure Product (A : Type) : Type := (opP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rinvP : ((Prod A A) → ((Prod A A) → (Prod A A)))) (rightCancelOpP : (∀ {xP yP : (Prod A A)} , (rinvP (opP yP xP) xP) = yP)) structure Hom {A1 : Type} {A2 : Type} (Ri1 : (RightCancellativeOp A1)) (Ri2 : (RightCancellativeOp A2)) : Type := (hom : (A1 → A2)) (pres_op : (∀ {x1 x2 : A1} , (hom ((op Ri1) x1 x2)) = ((op Ri2) (hom x1) (hom x2)))) (pres_rinv : (∀ {x1 x2 : A1} , (hom ((rinv Ri1) x1 x2)) = ((rinv Ri2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ri1 : (RightCancellativeOp A1)) (Ri2 : (RightCancellativeOp A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_op : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((op Ri1) x1 x2) ((op Ri2) y1 y2)))))) (interp_rinv : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((rinv Ri1) x1 x2) ((rinv Ri2) y1 y2)))))) inductive RightCancellativeOpTerm : Type | opL : (RightCancellativeOpTerm → (RightCancellativeOpTerm → RightCancellativeOpTerm)) | rinvL : (RightCancellativeOpTerm → (RightCancellativeOpTerm → RightCancellativeOpTerm)) open RightCancellativeOpTerm inductive ClRightCancellativeOpTerm (A : Type) : Type | sing : (A → ClRightCancellativeOpTerm) | opCl : (ClRightCancellativeOpTerm → (ClRightCancellativeOpTerm → ClRightCancellativeOpTerm)) | rinvCl : (ClRightCancellativeOpTerm → (ClRightCancellativeOpTerm → ClRightCancellativeOpTerm)) open ClRightCancellativeOpTerm inductive OpRightCancellativeOpTerm (n : ℕ) : Type | v : ((fin n) → OpRightCancellativeOpTerm) | opOL : (OpRightCancellativeOpTerm → (OpRightCancellativeOpTerm → OpRightCancellativeOpTerm)) | rinvOL : (OpRightCancellativeOpTerm → (OpRightCancellativeOpTerm → OpRightCancellativeOpTerm)) open OpRightCancellativeOpTerm inductive OpRightCancellativeOpTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpRightCancellativeOpTerm2) | sing2 : (A → OpRightCancellativeOpTerm2) | opOL2 : (OpRightCancellativeOpTerm2 → (OpRightCancellativeOpTerm2 → OpRightCancellativeOpTerm2)) | rinvOL2 : (OpRightCancellativeOpTerm2 → (OpRightCancellativeOpTerm2 → OpRightCancellativeOpTerm2)) open OpRightCancellativeOpTerm2 def simplifyCl {A : Type} : ((ClRightCancellativeOpTerm A) → (ClRightCancellativeOpTerm A)) | (opCl x1 x2) := (opCl (simplifyCl x1) (simplifyCl x2)) | (rinvCl x1 x2) := (rinvCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpRightCancellativeOpTerm n) → (OpRightCancellativeOpTerm n)) | (opOL x1 x2) := (opOL (simplifyOpB x1) (simplifyOpB x2)) | (rinvOL x1 x2) := (rinvOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpRightCancellativeOpTerm2 n A) → (OpRightCancellativeOpTerm2 n A)) | (opOL2 x1 x2) := (opOL2 (simplifyOp x1) (simplifyOp x2)) | (rinvOL2 x1 x2) := (rinvOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((RightCancellativeOp A) → (RightCancellativeOpTerm → A)) | Ri (opL x1 x2) := ((op Ri) (evalB Ri x1) (evalB Ri x2)) | Ri (rinvL x1 x2) := ((rinv Ri) (evalB Ri x1) (evalB Ri x2)) def evalCl {A : Type} : ((RightCancellativeOp A) → ((ClRightCancellativeOpTerm A) → A)) | Ri (sing x1) := x1 | Ri (opCl x1 x2) := ((op Ri) (evalCl Ri x1) (evalCl Ri x2)) | Ri (rinvCl x1 x2) := ((rinv Ri) (evalCl Ri x1) (evalCl Ri x2)) def evalOpB {A : Type} {n : ℕ} : ((RightCancellativeOp A) → ((vector A n) → ((OpRightCancellativeOpTerm n) → A))) | Ri vars (v x1) := (nth vars x1) | Ri vars (opOL x1 x2) := ((op Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) | Ri vars (rinvOL x1 x2) := ((rinv Ri) (evalOpB Ri vars x1) (evalOpB Ri vars x2)) def evalOp {A : Type} {n : ℕ} : ((RightCancellativeOp A) → ((vector A n) → ((OpRightCancellativeOpTerm2 n A) → A))) | Ri vars (v2 x1) := (nth vars x1) | Ri vars (sing2 x1) := x1 | Ri vars (opOL2 x1 x2) := ((op Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) | Ri vars (rinvOL2 x1 x2) := ((rinv Ri) (evalOp Ri vars x1) (evalOp Ri vars x2)) def inductionB {P : (RightCancellativeOpTerm → Type)} : ((∀ (x1 x2 : RightCancellativeOpTerm) , ((P x1) → ((P x2) → (P (opL x1 x2))))) → ((∀ (x1 x2 : RightCancellativeOpTerm) , ((P x1) → ((P x2) → (P (rinvL x1 x2))))) → (∀ (x : RightCancellativeOpTerm) , (P x)))) | popl prinvl (opL x1 x2) := (popl _ _ (inductionB popl prinvl x1) (inductionB popl prinvl x2)) | popl prinvl (rinvL x1 x2) := (prinvl _ _ (inductionB popl prinvl x1) (inductionB popl prinvl x2)) def inductionCl {A : Type} {P : ((ClRightCancellativeOpTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClRightCancellativeOpTerm A)) , ((P x1) → ((P x2) → (P (opCl x1 x2))))) → ((∀ (x1 x2 : (ClRightCancellativeOpTerm A)) , ((P x1) → ((P x2) → (P (rinvCl x1 x2))))) → (∀ (x : (ClRightCancellativeOpTerm A)) , (P x))))) | psing popcl prinvcl (sing x1) := (psing x1) | psing popcl prinvcl (opCl x1 x2) := (popcl _ _ (inductionCl psing popcl prinvcl x1) (inductionCl psing popcl prinvcl x2)) | psing popcl prinvcl (rinvCl x1 x2) := (prinvcl _ _ (inductionCl psing popcl prinvcl x1) (inductionCl psing popcl prinvcl x2)) def inductionOpB {n : ℕ} {P : ((OpRightCancellativeOpTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpRightCancellativeOpTerm n)) , ((P x1) → ((P x2) → (P (opOL x1 x2))))) → ((∀ (x1 x2 : (OpRightCancellativeOpTerm n)) , ((P x1) → ((P x2) → (P (rinvOL x1 x2))))) → (∀ (x : (OpRightCancellativeOpTerm n)) , (P x))))) | pv popol prinvol (v x1) := (pv x1) | pv popol prinvol (opOL x1 x2) := (popol _ _ (inductionOpB pv popol prinvol x1) (inductionOpB pv popol prinvol x2)) | pv popol prinvol (rinvOL x1 x2) := (prinvol _ _ (inductionOpB pv popol prinvol x1) (inductionOpB pv popol prinvol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpRightCancellativeOpTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpRightCancellativeOpTerm2 n A)) , ((P x1) → ((P x2) → (P (opOL2 x1 x2))))) → ((∀ (x1 x2 : (OpRightCancellativeOpTerm2 n A)) , ((P x1) → ((P x2) → (P (rinvOL2 x1 x2))))) → (∀ (x : (OpRightCancellativeOpTerm2 n A)) , (P x)))))) | pv2 psing2 popol2 prinvol2 (v2 x1) := (pv2 x1) | pv2 psing2 popol2 prinvol2 (sing2 x1) := (psing2 x1) | pv2 psing2 popol2 prinvol2 (opOL2 x1 x2) := (popol2 _ _ (inductionOp pv2 psing2 popol2 prinvol2 x1) (inductionOp pv2 psing2 popol2 prinvol2 x2)) | pv2 psing2 popol2 prinvol2 (rinvOL2 x1 x2) := (prinvol2 _ _ (inductionOp pv2 psing2 popol2 prinvol2 x1) (inductionOp pv2 psing2 popol2 prinvol2 x2)) def stageB : (RightCancellativeOpTerm → (Staged RightCancellativeOpTerm)) | (opL x1 x2) := (stage2 opL (codeLift2 opL) (stageB x1) (stageB x2)) | (rinvL x1 x2) := (stage2 rinvL (codeLift2 rinvL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClRightCancellativeOpTerm A) → (Staged (ClRightCancellativeOpTerm A))) | (sing x1) := (Now (sing x1)) | (opCl x1 x2) := (stage2 opCl (codeLift2 opCl) (stageCl x1) (stageCl x2)) | (rinvCl x1 x2) := (stage2 rinvCl (codeLift2 rinvCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpRightCancellativeOpTerm n) → (Staged (OpRightCancellativeOpTerm n))) | (v x1) := (const (code (v x1))) | (opOL x1 x2) := (stage2 opOL (codeLift2 opOL) (stageOpB x1) (stageOpB x2)) | (rinvOL x1 x2) := (stage2 rinvOL (codeLift2 rinvOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpRightCancellativeOpTerm2 n A) → (Staged (OpRightCancellativeOpTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (opOL2 x1 x2) := (stage2 opOL2 (codeLift2 opOL2) (stageOp x1) (stageOp x2)) | (rinvOL2 x1 x2) := (stage2 rinvOL2 (codeLift2 rinvOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (opT : ((Repr A) → ((Repr A) → (Repr A)))) (rinvT : ((Repr A) → ((Repr A) → (Repr A)))) end RightCancellativeOp
b96772bdad20ada385d115932145f7733df24d15
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/basic.lean
ead66d56a89d98543934a586a981cd8cc30a8a9b
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,673
lean
prelude constant A.{l1 l2} : Type.{l1} → Type.{l2} check A definition tst.{l} (A : Type) (B : Type) (C : Type.{l}) : Type := A → B → C check tst constant group.{l} : Type.{l+1} constant carrier.{l} : group.{l} → Type.{l} definition to_carrier (g : group) := carrier g check to_carrier.{1} section variable A : Type check A definition B := A → A end constant N : Type.{1} check B N constant f : B N check f constant a : N check f a section variable T1 : Type variable T2 : Type variable f : T1 → T2 → T2 definition double (a : T1) (b : T2) := f a (f a b) end check double check double.{1 2} definition Prop := Type.{0} constant eq : Π {A : Type}, A → A → Prop infix `=`:50 := eq check eq.{1} section universe variable l universe variable u variable {T1 : Type.{l}} variable {T2 : Type.{l}} variable {T3 : Type.{u}} variable f : T1 → T2 → T2 definition is_proj2 := ∀ x y, f x y = y definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z end check @is_proj2.{1} check @is_proj3.{1 2} namespace foo section variables {T1 T2 : Type} variable {T3 : Type} variable f : T1 → T2 → T2 definition is_proj2 := ∀ x y, f x y = y definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z end check @foo.is_proj2.{1} check @foo.is_proj3.{1 2} end foo namespace bla section variable {T1 : Type} variable {T2 : Type} variable {T3 : Type} variable f : T1 → T2 → T2 definition is_proj2 := ∀ x y, f x y = y definition is_proj3 (f : T1 → T2 → T3 → T3) := ∀ x y z, f x y z = z end check @bla.is_proj2.{1 2} check @bla.is_proj3.{1 2 3} end bla
c3ee661f8c14aa96490993049181c645c04d8381
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/class_number/number_field.lean
19deef684a86e0681e99d148e17f6019cefe2463
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
1,674
lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import number_theory.class_number.admissible_abs import number_theory.class_number.finite import number_theory.number_field /-! # Class numbers of number fields This file defines the class number of a number field as the (finite) cardinality of the class group of its ring of integers. It also proves some elementary results on the class number. ## Main definitions - `number_field.class_number`: the class number of a number field is the (finite) cardinality of the class group of its ring of integers -/ namespace number_field variables (K : Type*) [field K] [number_field K] namespace ring_of_integers noncomputable instance : fintype (class_group (ring_of_integers K) K) := class_group.fintype_of_admissible_of_finite ℚ _ absolute_value.abs_is_admissible end ring_of_integers /-- The class number of a number field is the (finite) cardinality of the class group. -/ noncomputable def class_number : ℕ := fintype.card (class_group (ring_of_integers K) K) variables {K} /-- The class number of a number field is `1` iff the ring of integers is a PID. -/ theorem class_number_eq_one_iff : class_number K = 1 ↔ is_principal_ideal_ring (ring_of_integers K) := card_class_group_eq_one_iff end number_field namespace rat open number_field theorem class_number_eq : number_field.class_number ℚ = 1 := class_number_eq_one_iff.mpr $ by convert is_principal_ideal_ring.of_surjective (rat.ring_of_integers_equiv.symm : ℤ →+* ring_of_integers ℚ) (rat.ring_of_integers_equiv.symm.surjective) end rat
73629aaca3bc177fbffcb53f521c9dc1e8be1f36
9c1ad797ec8a5eddb37d34806c543602d9a6bf70
/universal/uniqueness.lean
453b802b102ba1c81dcf9084b71001628d07db89
[]
no_license
timjb/lean-category-theory
816eefc3a0582c22c05f4ee1c57ed04e57c0982f
12916cce261d08bb8740bc85e0175b75fb2a60f4
refs/heads/master
1,611,078,926,765
1,492,080,000,000
1,492,080,000,000
88,348,246
0
0
null
1,492,262,499,000
1,492,262,498,000
null
UTF-8
Lean
false
false
612
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 .universal import .comparisons open tqft.categories open tqft.categories.universal namespace tqft.categories.universal -- lemma Equalizers_are_unique -- { C : Category } -- { X Y : C.Obj } -- ( f g : C.Hom X Y ) -- : unique_up_to_isomorphism (Equalizer f g) Equalizer.equalizer -- := sorry -- PROJECT prove this via the comma category formulation, using lemmas in comparisons.lean end tqft.categories.universal
5aa1b146cb230031205b6585a188596cdb7c0196
4727251e0cd73359b15b664c3170e5d754078599
/src/number_theory/primes_congruent_one.lean
213f45cc3b48305006a1a99a2714b523885c84e8
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,349
lean
/- Copyright (c) 2020 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import ring_theory.polynomial.cyclotomic.eval /-! # Primes congruent to one We prove that, for any positive `k : ℕ`, there are infinitely many primes `p` such that `p ≡ 1 [MOD k]`. -/ namespace nat open polynomial nat filter /-- For any positive `k : ℕ` there are infinitely many primes `p` such that `p ≡ 1 [MOD k]`. -/ lemma exists_prime_ge_modeq_one {k : ℕ} (n : ℕ) (hpos : 0 < k) : ∃ (p : ℕ), nat.prime p ∧ n ≤ p ∧ p ≡ 1 [MOD k] := begin let b := 3 * (k * n.factorial), have hgt : 1 < (eval ↑b (cyclotomic k ℤ)).nat_abs, { have hkey : ∀ l : ℕ, 2 < 3 * (l.succ * n.factorial) := λ l, lt_mul_of_lt_of_one_le (2 : ℕ).lt_succ_self (le_mul_of_le_of_le_one (nat.succ_pos _) n.factorial_pos), rcases k with _ | _ | k, { simpa using hpos, }, { simp only [one_mul, int.coe_nat_mul, int.coe_nat_succ, int.coe_nat_zero, zero_add, cyclotomic_one, eval_sub, eval_X, eval_one], convert int.nat_abs_lt_nat_abs_of_nonneg_of_lt int.one_nonneg _, rw lt_sub_iff_add_lt, specialize hkey 0, norm_cast, rwa one_mul at hkey, }, calc 1 ≤ _ : by { rw le_tsub_iff_left (one_le_two.trans (hkey _).le), exact (hkey _).le, } ... < _ : sub_one_lt_nat_abs_cyclotomic_eval (one_lt_succ_succ k) (one_lt_two.trans (hkey k.succ)).ne.symm, }, let p := min_fac (eval ↑b (cyclotomic k ℤ)).nat_abs, haveI hprime : fact p.prime := ⟨min_fac_prime (ne_of_lt hgt).symm⟩, have hroot : is_root (cyclotomic k (zmod p)) (cast_ring_hom (zmod p) b), { rw [is_root.def, ← map_cyclotomic_int k (zmod p), eval_map, coe_cast_ring_hom, ← int.cast_coe_nat, ← int.coe_cast_ring_hom, eval₂_hom, int.coe_cast_ring_hom, zmod.int_coe_zmod_eq_zero_iff_dvd _ _], apply int.dvd_nat_abs.1, exact_mod_cast min_fac_dvd (eval ↑b (cyclotomic k ℤ)).nat_abs }, refine ⟨p, hprime.1, _, _⟩, { by_contra habs, exact (prime.dvd_iff_not_coprime hprime.1).1 (dvd_factorial (min_fac_pos _) (le_of_not_ge habs)) (coprime_of_root_cyclotomic hpos hroot).symm.coprime_mul_left_right.coprime_mul_left_right }, { have hdiv := order_of_dvd_of_pow_eq_one (zmod.units_pow_card_sub_one_eq_one p (zmod.unit_of_coprime b (coprime_of_root_cyclotomic hpos hroot))), have : ¬p ∣ k := hprime.1.coprime_iff_not_dvd.1 (coprime_of_root_cyclotomic hpos hroot).symm.coprime_mul_left_right.coprime_mul_right_right, haveI := ne_zero.of_not_dvd (zmod p) this, have : k = order_of (b : zmod p) := (is_root_cyclotomic_iff.mp hroot).eq_order_of, rw [←order_of_units, zmod.coe_unit_of_coprime, ←this] at hdiv, exact ((modeq_iff_dvd' hprime.1.pos).2 hdiv).symm } end lemma frequently_at_top_modeq_one {k : ℕ} (hpos : 0 < k) : ∃ᶠ p in at_top, nat.prime p ∧ p ≡ 1 [MOD k] := begin refine frequently_at_top.2 (λ n, _), obtain ⟨p, hp⟩ := exists_prime_ge_modeq_one n hpos, exact ⟨p, ⟨hp.2.1, hp.1, hp.2.2⟩⟩ end lemma infinite_set_of_prime_modeq_one {k : ℕ} (hpos : 0 < k) : set.infinite {p : ℕ | nat.prime p ∧ p ≡ 1 [MOD k]} := frequently_at_top_iff_infinite.1 (frequently_at_top_modeq_one hpos) end nat
af940115a72c6a8f0f8a61e6f0805cd5baec12d2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/calculus/local_extr.lean
6694ec4d17df050aea31e66789a8edcfbc04a3e3
[ "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
20,749
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 analysis.calculus.deriv import data.polynomial.field_division import topology.algebra.order.extend_from import topology.algebra.polynomial import topology.local_extr /-! # Local extrema of smooth functions ## Main definitions In a real normed space `E` we define `pos_tangent_cone_at (s : set E) (x : E)`. This would be the same as `tangent_cone_at ℝ≥0 s x` if we had a theory of normed semifields. This set is used in the proof of Fermat's Theorem (see below), and can be used to formalize [Lagrange multipliers](https://en.wikipedia.org/wiki/Lagrange_multiplier) and/or [Karush–Kuhn–Tucker conditions](https://en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions). ## Main statements For each theorem name listed below, we also prove similar theorems for `min`, `extr` (if applicable)`, and `(f)deriv` instead of `has_fderiv`. * `is_local_max_on.has_fderiv_within_at_nonpos` : `f' y ≤ 0` whenever `a` is a local maximum of `f` on `s`, `f` has derivative `f'` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`. * `is_local_max_on.has_fderiv_within_at_eq_zero` : In the settings of the previous theorem, if both `y` and `-y` belong to the positive tangent cone, then `f' y = 0`. * `is_local_max.has_fderiv_at_eq_zero` : [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)), the derivative of a differentiable function at a local extremum point equals zero. * `exists_has_deriv_at_eq_zero` : [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem): given a function `f` continuous on `[a, b]` and differentiable on `(a, b)`, there exists `c ∈ (a, b)` such that `f' c = 0`. ## Implementation notes For each mathematical fact we prove several versions of its formalization: * for maxima and minima; * using `has_fderiv*`/`has_deriv*` or `fderiv*`/`deriv*`. For the `fderiv*`/`deriv*` versions we omit the differentiability condition whenever it is possible due to the fact that `fderiv` and `deriv` are defined to be zero for non-differentiable functions. ## References * [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)); * [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem); * [Tangent cone](https://en.wikipedia.org/wiki/Tangent_cone); ## Tags local extremum, Fermat's Theorem, Rolle's Theorem -/ universes u v open filter set open_locale topological_space classical polynomial section module variables {E : Type u} [normed_add_comm_group E] [normed_space ℝ E] {f : E → ℝ} {a : E} {f' : E →L[ℝ] ℝ} /-- "Positive" tangent cone to `s` at `x`; the only difference from `tangent_cone_at` is that we require `c n → ∞` instead of `‖c n‖ → ∞`. One can think about `pos_tangent_cone_at` as `tangent_cone_at nnreal` but we have no theory of normed semifields yet. -/ def pos_tangent_cone_at (s : set E) (x : E) : set E := {y : E | ∃(c : ℕ → ℝ) (d : ℕ → E), (∀ᶠ n in at_top, x + d n ∈ s) ∧ (tendsto c at_top at_top) ∧ (tendsto (λn, c n • d n) at_top (𝓝 y))} lemma pos_tangent_cone_at_mono : monotone (λ s, pos_tangent_cone_at s a) := begin rintros s t hst y ⟨c, d, hd, hc, hcd⟩, exact ⟨c, d, mem_of_superset hd $ λ h hn, hst hn, hc, hcd⟩ end lemma mem_pos_tangent_cone_at_of_segment_subset {s : set E} {x y : E} (h : segment ℝ x y ⊆ s) : y - x ∈ pos_tangent_cone_at s x := begin let c := λn:ℕ, (2:ℝ)^n, let d := λn:ℕ, (c n)⁻¹ • (y-x), refine ⟨c, d, filter.univ_mem' (λn, h _), tendsto_pow_at_top_at_top_of_one_lt one_lt_two, _⟩, show x + d n ∈ segment ℝ x y, { rw segment_eq_image', refine ⟨(c n)⁻¹, ⟨_, _⟩, rfl⟩, exacts [inv_nonneg.2 (pow_nonneg zero_le_two _), inv_le_one (one_le_pow_of_one_le one_le_two _)] }, show tendsto (λ n, c n • d n) at_top (𝓝 (y - x)), { convert tendsto_const_nhds, ext n, simp only [d, smul_smul], rw [mul_inv_cancel, one_smul], exact pow_ne_zero _ two_ne_zero } end lemma mem_pos_tangent_cone_at_of_segment_subset' {s : set E} {x y : E} (h : segment ℝ x (x + y) ⊆ s) : y ∈ pos_tangent_cone_at s x := by simpa only [add_sub_cancel'] using mem_pos_tangent_cone_at_of_segment_subset h lemma pos_tangent_cone_at_univ : pos_tangent_cone_at univ a = univ := eq_univ_of_forall $ λ x, mem_pos_tangent_cone_at_of_segment_subset' (subset_univ _) /-- If `f` has a local max on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_nonpos {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : f' y ≤ 0 := begin rcases hy with ⟨c, d, hd, hc, hcd⟩, have hc' : tendsto (λ n, ‖c n‖) at_top at_top, from tendsto_at_top_mono (λ n, le_abs_self _) hc, refine le_of_tendsto (hf.lim at_top hd hc' hcd) _, replace hd : tendsto (λ n, a + d n) at_top (𝓝[s] (a + 0)), from tendsto_inf.2 ⟨tendsto_const_nhds.add (tangent_cone_at.lim_zero _ hc' hcd), by rwa tendsto_principal⟩, rw [add_zero] at hd, replace h : ∀ᶠ n in at_top, f (a + d n) ≤ f a, from mem_map.1 (hd h), replace hc : ∀ᶠ n in at_top, 0 ≤ c n, from mem_map.1 (hc (mem_at_top (0:ℝ))), filter_upwards [h, hc], simp only [smul_eq_mul, mem_preimage, subset_def], assume n hnf hn, exact mul_nonpos_of_nonneg_of_nonpos hn (sub_nonpos.2 hnf) end /-- If `f` has a local max on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.fderiv_within_nonpos {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y ≤ 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonpos hf.has_fderiv_within_at hy else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_max_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := le_antisymm (h.has_fderiv_within_at_nonpos hf hy) $ by simpa using h.has_fderiv_within_at_nonpos hf hy' /-- If `f` has a local max on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_max_on.fderiv_within_eq_zero {s : set E} (h : is_local_max_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- If `f` has a local min on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.has_fderiv_within_at_nonneg {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : 0 ≤ f' y := by simpa using h.neg.has_fderiv_within_at_nonpos hf.neg hy /-- If `f` has a local min on `s` at `a` and `y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/ lemma is_local_min_on.fderiv_within_nonneg {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) : (0:ℝ) ≤ (fderiv_within ℝ f s a : E → ℝ) y := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_nonneg hf.has_fderiv_within_at hy else by { rw [fderiv_within_zero_of_not_differentiable_within_at hf], refl } /-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/ lemma is_local_min_on.has_fderiv_within_at_eq_zero {s : set E} (h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : f' y = 0 := by simpa using h.neg.has_fderiv_within_at_eq_zero hf.neg hy hy' /-- If `f` has a local min on `s` at `a` and both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y = 0`. -/ lemma is_local_min_on.fderiv_within_eq_zero {s : set E} (h : is_local_min_on f s a) {y} (hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : (fderiv_within ℝ f s a : E → ℝ) y = 0 := if hf : differentiable_within_at ℝ f s a then h.has_fderiv_within_at_eq_zero hf.has_fderiv_within_at hy hy' else by { rw fderiv_within_zero_of_not_differentiable_within_at hf, refl } /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_fderiv_at_eq_zero (h : is_local_min f a) (hf : has_fderiv_at f f' a) : f' = 0 := begin ext y, apply (h.on univ).has_fderiv_within_at_eq_zero hf.has_fderiv_within_at; rw pos_tangent_cone_at_univ; apply mem_univ end /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.fderiv_eq_zero (h : is_local_min f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_fderiv_at_eq_zero (h : is_local_max f a) (hf : has_fderiv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_fderiv_at_eq_zero hf.neg /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.fderiv_eq_zero (h : is_local_max f a) : fderiv ℝ f a = 0 := if hf : differentiable_at ℝ f a then h.has_fderiv_at_eq_zero hf.has_fderiv_at else fderiv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_fderiv_at_eq_zero (h : is_local_extr f a) : has_fderiv_at f f' a → f' = 0 := h.elim is_local_min.has_fderiv_at_eq_zero is_local_max.has_fderiv_at_eq_zero /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.fderiv_eq_zero (h : is_local_extr f a) : fderiv ℝ f a = 0 := h.elim is_local_min.fderiv_eq_zero is_local_max.fderiv_eq_zero end module section real variables {f : ℝ → ℝ} {f' : ℝ} {a b : ℝ} /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.has_deriv_at_eq_zero (h : is_local_min f a) (hf : has_deriv_at f f' a) : f' = 0 := by simpa using continuous_linear_map.ext_iff.1 (h.has_fderiv_at_eq_zero (has_deriv_at_iff_has_fderiv_at.1 hf)) 1 /-- **Fermat's Theorem**: the derivative of a function at a local minimum equals zero. -/ lemma is_local_min.deriv_eq_zero (h : is_local_min f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.has_deriv_at_eq_zero (h : is_local_max f a) (hf : has_deriv_at f f' a) : f' = 0 := neg_eq_zero.1 $ h.neg.has_deriv_at_eq_zero hf.neg /-- **Fermat's Theorem**: the derivative of a function at a local maximum equals zero. -/ lemma is_local_max.deriv_eq_zero (h : is_local_max f a) : deriv f a = 0 := if hf : differentiable_at ℝ f a then h.has_deriv_at_eq_zero hf.has_deriv_at else deriv_zero_of_not_differentiable_at hf /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.has_deriv_at_eq_zero (h : is_local_extr f a) : has_deriv_at f f' a → f' = 0 := h.elim is_local_min.has_deriv_at_eq_zero is_local_max.has_deriv_at_eq_zero /-- **Fermat's Theorem**: the derivative of a function at a local extremum equals zero. -/ lemma is_local_extr.deriv_eq_zero (h : is_local_extr f a) : deriv f a = 0 := h.elim is_local_min.deriv_eq_zero is_local_max.deriv_eq_zero end real section Rolle variables (f f' : ℝ → ℝ) {a b : ℝ} /-- A continuous function on a closed interval with `f a = f b` takes either its maximum or its minimum value at a point in the interior of the interval. -/ lemma exists_Ioo_extr_on_Icc (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, is_extr_on f (Icc a b) c := begin have ne : (Icc a b).nonempty, from nonempty_Icc.2 (le_of_lt hab), -- Consider absolute min and max points obtain ⟨c, cmem, cle⟩ : ∃ c ∈ Icc a b, ∀ x ∈ Icc a b, f c ≤ f x, from is_compact_Icc.exists_forall_le ne hfc, obtain ⟨C, Cmem, Cge⟩ : ∃ C ∈ Icc a b, ∀ x ∈ Icc a b, f x ≤ f C, from is_compact_Icc.exists_forall_ge ne hfc, by_cases hc : f c = f a, { by_cases hC : f C = f a, { have : ∀ x ∈ Icc a b, f x = f a, from λ x hx, le_antisymm (hC ▸ Cge x hx) (hc ▸ cle x hx), -- `f` is a constant, so we can take any point in `Ioo a b` rcases exists_between hab with ⟨c', hc'⟩, refine ⟨c', hc', or.inl _⟩, assume x hx, rw [mem_set_of_eq, this x hx, ← hC], exact Cge c' ⟨le_of_lt hc'.1, le_of_lt hc'.2⟩ }, { refine ⟨C, ⟨lt_of_le_of_ne Cmem.1 $ mt _ hC, lt_of_le_of_ne Cmem.2 $ mt _ hC⟩, or.inr Cge⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } }, { refine ⟨c, ⟨lt_of_le_of_ne cmem.1 $ mt _ hc, lt_of_le_of_ne cmem.2 $ mt _ hc⟩, or.inl cle⟩, exacts [λ h, by rw h, λ h, by rw [h, hfI]] } end /-- A continuous function on a closed interval with `f a = f b` has a local extremum at some point of the corresponding open interval. -/ lemma exists_local_extr_Ioo (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, is_local_extr f c := let ⟨c, cmem, hc⟩ := exists_Ioo_extr_on_Icc f hab hfc hfI in ⟨c, cmem, hc.is_local_extr $ Icc_mem_nhds cmem.1 cmem.2⟩ /-- **Rolle's Theorem** `has_deriv_at` version -/ lemma exists_has_deriv_at_eq_zero (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.has_deriv_at_eq_zero $ hff' c cmem⟩ /-- **Rolle's Theorem** `deriv` version -/ lemma exists_deriv_eq_zero (hab : a < b) (hfc : continuous_on f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, deriv f c = 0 := let ⟨c, cmem, hc⟩ := exists_local_extr_Ioo f hab hfc hfI in ⟨c, cmem, hc.deriv_eq_zero⟩ variables {f f'} {l : ℝ} /-- **Rolle's Theorem**, a version for a function on an open interval: if `f` has derivative `f'` on `(a, b)` and has the same limit `l` at `𝓝[>] a` and `𝓝[<] b`, then `f' c = 0` for some `c ∈ (a, b)`. -/ lemma exists_has_deriv_at_eq_zero' (hab : a < b) (hfa : tendsto f (𝓝[>] a) (𝓝 l)) (hfb : tendsto f (𝓝[<] b) (𝓝 l)) (hff' : ∀ x ∈ Ioo a b, has_deriv_at f (f' x) x) : ∃ c ∈ Ioo a b, f' c = 0 := begin have : continuous_on f (Ioo a b) := λ x hx, (hff' x hx).continuous_at.continuous_within_at, have hcont := continuous_on_Icc_extend_from_Ioo hab.ne this hfa hfb, obtain ⟨c, hc, hcextr⟩ : ∃ c ∈ Ioo a b, is_local_extr (extend_from (Ioo a b) f) c, { apply exists_local_extr_Ioo _ hab hcont, rw eq_lim_at_right_extend_from_Ioo hab hfb, exact eq_lim_at_left_extend_from_Ioo hab hfa }, use [c, hc], apply (hcextr.congr _).has_deriv_at_eq_zero (hff' c hc), rw eventually_eq_iff_exists_mem, exact ⟨Ioo a b, Ioo_mem_nhds hc.1 hc.2, extend_from_extends this⟩ end /-- **Rolle's Theorem**, a version for a function on an open interval: if `f` has the same limit `l` at `𝓝[>] a` and `𝓝[<] b`, then `deriv f c = 0` for some `c ∈ (a, b)`. This version does not require differentiability of `f` because we define `deriv f c = 0` whenever `f` is not differentiable at `c`. -/ lemma exists_deriv_eq_zero' (hab : a < b) (hfa : tendsto f (𝓝[>] a) (𝓝 l)) (hfb : tendsto f (𝓝[<] b) (𝓝 l)) : ∃ c ∈ Ioo a b, deriv f c = 0 := classical.by_cases (assume h : ∀ x ∈ Ioo a b, differentiable_at ℝ f x, show ∃ c ∈ Ioo a b, deriv f c = 0, from exists_has_deriv_at_eq_zero' hab hfa hfb (λ x hx, (h x hx).has_deriv_at)) (assume h : ¬∀ x ∈ Ioo a b, differentiable_at ℝ f x, have h : ∃ x, x ∈ Ioo a b ∧ ¬differentiable_at ℝ f x, by { push_neg at h, exact h }, let ⟨c, hc, hcdiff⟩ := h in ⟨c, hc, deriv_zero_of_not_differentiable_at hcdiff⟩) end Rolle namespace polynomial open_locale big_operators /-- The number of roots of a real polynomial `p` is at most the number of roots of its derivative that are not roots of `p` plus one. -/ lemma card_roots_to_finset_le_card_roots_derivative_diff_roots_succ (p : ℝ[X]) : p.roots.to_finset.card ≤ (p.derivative.roots.to_finset \ p.roots.to_finset).card + 1 := begin cases eq_or_ne p.derivative 0 with hp' hp', { rw [eq_C_of_derivative_eq_zero hp', roots_C, multiset.to_finset_zero, finset.card_empty], exact zero_le _ }, have hp : p ≠ 0, from ne_of_apply_ne derivative (by rwa [derivative_zero]), refine finset.card_le_diff_of_interleaved (λ x hx y hy hxy hxy', _), rw [multiset.mem_to_finset, mem_roots hp] at hx hy, obtain ⟨z, hz1, hz2⟩ := exists_deriv_eq_zero (λ x : ℝ, eval x p) hxy p.continuous_on (hx.trans hy.symm), refine ⟨z, _, hz1⟩, rwa [multiset.mem_to_finset, mem_roots hp', is_root, ← p.deriv] end /-- The number of roots of a real polynomial is at most the number of roots of its derivative plus one. -/ lemma card_roots_to_finset_le_derivative (p : ℝ[X]) : p.roots.to_finset.card ≤ p.derivative.roots.to_finset.card + 1 := p.card_roots_to_finset_le_card_roots_derivative_diff_roots_succ.trans $ add_le_add_right (finset.card_mono $ finset.sdiff_subset _ _) _ /-- The number of roots of a real polynomial (counted with multiplicities) is at most the number of roots of its derivative (counted with multiplicities) plus one. -/ lemma card_roots_le_derivative (p : ℝ[X]) : p.roots.card ≤ p.derivative.roots.card + 1 := calc p.roots.card = ∑ x in p.roots.to_finset, p.roots.count x : (multiset.to_finset_sum_count_eq _).symm ... = ∑ x in p.roots.to_finset, (p.roots.count x - 1 + 1) : eq.symm $ finset.sum_congr rfl $ λ x hx, tsub_add_cancel_of_le $ nat.succ_le_iff.2 $ multiset.count_pos.2 $ multiset.mem_to_finset.1 hx ... = ∑ x in p.roots.to_finset, (p.root_multiplicity x - 1) + p.roots.to_finset.card : by simp only [finset.sum_add_distrib, finset.card_eq_sum_ones, count_roots] ... ≤ ∑ x in p.roots.to_finset, p.derivative.root_multiplicity x + ((p.derivative.roots.to_finset \ p.roots.to_finset).card + 1) : add_le_add (finset.sum_le_sum $ λ x hx, root_multiplicity_sub_one_le_derivative_root_multiplicity _ _) p.card_roots_to_finset_le_card_roots_derivative_diff_roots_succ ... ≤ ∑ x in p.roots.to_finset, p.derivative.roots.count x + (∑ x in p.derivative.roots.to_finset \ p.roots.to_finset, p.derivative.roots.count x + 1) : begin simp only [← count_roots], refine add_le_add_left (add_le_add_right ((finset.card_eq_sum_ones _).trans_le _) _) _, refine finset.sum_le_sum (λ x hx, nat.succ_le_iff.2 $ _), rw [multiset.count_pos, ← multiset.mem_to_finset], exact (finset.mem_sdiff.1 hx).1 end ... = p.derivative.roots.card + 1 : begin rw [← add_assoc, ← finset.sum_union finset.disjoint_sdiff, finset.union_sdiff_self_eq_union, ← multiset.to_finset_sum_count_eq, ← finset.sum_subset (finset.subset_union_right _ _)], intros x hx₁ hx₂, simpa only [multiset.mem_to_finset, multiset.count_eq_zero] using hx₂ end /-- The number of real roots of a polynomial is at most the number of roots of its derivative plus one. -/ lemma card_root_set_le_derivative {F : Type*} [comm_ring F] [algebra F ℝ] (p : F[X]) : fintype.card (p.root_set ℝ) ≤ fintype.card (p.derivative.root_set ℝ) + 1 := by simpa only [root_set_def, finset.coe_sort_coe, fintype.card_coe, derivative_map] using card_roots_to_finset_le_derivative (p.map (algebra_map F ℝ)) end polynomial
08b29bfbeec9c38898aaa804db7d4123f16495d0
a51edd9a1700339fa6dc7dc428eb5dfa3994b8bc
/src/semantics.lean
4003790bf9614734b2b490f5970d93c87c1f6245
[]
no_license
avigad/formal_logic
83f5c0534b3e9e7da53eff01bb82289daad65555
59d7fe7cb7a7927fb72d89d4fd40965bcd769349
refs/heads/master
1,585,302,642,116
1,541,000,469,000
1,541,000,469,000
146,376,915
1
1
null
null
null
null
UTF-8
Lean
false
false
17,552
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The "standard" semantics for type theory: `type`s denote `Type`s, and `terms` denote values of the corresponding types. -/ import .syntax data.bool /- For now, we're only dealing with "pure" types, i.e. types without type variables and constructors only basic types and arrows. Also, the use of Type* is illusory. Because we interpret `nat`, `bool`, etc. as the corresponding elements of Type 1, Type* is forced to be Type 1. Maybe we can get around this with lifting. -/ namespace hol /- structure model := (domain : Type*) (const_val : term.const → domain) (app : domain → domain → domain) -/ namespace type /- evaluate the value of a type modulo a partial assignment, `bval`, to basic types -/ namespace basic def evalp (bval : ℕ → option Type*) : type.basic → option Type* | (user n) := bval n | prop := some Prop | nat := some _root_.nat | bool := some _root_.bool | int := some _root_.int | unit := some _root_.unit def ok (bval : ℕ → option Type*) : type.basic → _root_.bool | (user n) := option.is_some (bval n) | prop := tt | nat := tt | bool := tt | int := tt | unit := tt def eval (bval : ℕ → option Type*) : Π b : type.basic, b.ok bval = tt → Type* | (user n) h := option.get h | prop h := Prop | nat h := _root_.nat | bool h := _root_.bool | int h := _root_.int | unit h := _root_.unit end basic def lift_arrow : option Type* → option Type* → option Type* | (some T₁) (some T₂) := some (T₁ → T₂) | _ _ := none def evalp (bval : ℕ → option Type*) : type → option Type* | (Var n) := none | (Basic b) := basic.evalp bval b | (Arr t₁ t₂) := lift_arrow (evalp t₁) (evalp t₂) | (Constructor _) := none | (App _ _ ) := none def ok (bval : ℕ → option Type*) : type → bool | (Var n) := ff | (Basic b) := b.ok bval | (Arr t₁ t₂) := ok t₁ && ok t₂ | (Constructor _) := ff | (App _ _ ) := ff def eval (bval : ℕ → option Type*) : Π t : type, t.ok bval → Type* | (Var n) h := by { simp [type.ok] at h, contradiction } | (Basic b) h := b.eval bval h | (Arr t₁ t₂) h := eval t₁ (bool.band_elim_left h) → eval t₂ (bool.band_elim_right h) | (Constructor _) h := by { simp [type.ok] at h, contradiction } | (App _ _ ) h := by { simp [type.ok] at h, contradiction } /- some useful functions -/ theorem ok_domain (bval : ℕ → option Type*): Π t : type, t.ok bval = tt → t.domain.ok bval = tt := begin intro t, induction t; try {exact id}, simp [type.domain, type.ok], intro h, simp [h] end theorem ok_codomain (bval : ℕ → option Type*): Π t : type, t.ok bval → t.codomain.ok bval := begin intro t, induction t; try {exact id}, simp [type.codomain, type.ok] end def eval_ext (bval : ℕ → option Type*) (t₁ t₂: type) (h : t₁ = t₂) : ∀ h₁ h₂, t₁.eval bval h₁ = t₂.eval bval h₂ := by { rw h, intros, reflexivity } end type /- We can use these to interpret quantifiers with bool in place of Prop. section local attribute [instance] classical.prop_decidable noncomputable def classical_ball (T : Type*) (f : T → bool) : bool := if ∀ t : T, f t = bool.tt then bool.tt else bool.ff noncomputable def classical_bex (T : Type*) (f : T → bool) : bool := if ∃ t : T, f t = bool.tt then bool.tt else bool.ff end -/ namespace term namespace const inductive evaluates_to (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : const → Π T : Type*, T → Prop | eval_user (n : ℕ) (h : option.is_some (cval n)) (t : hol.type) (l : list hol.type) : evaluates_to ⟨kind.user n, t, l⟩ (option.get h).1 (option.get h).2 | eval_true : evaluates_to ⟨kind.true, mk_prop, []⟩ Prop _root_.true | eval_false : evaluates_to ⟨kind.false, mk_prop, []⟩ Prop _root_.false | eval_not : evaluates_to ⟨kind.not, mk_prop ⇒ mk_prop, []⟩ (Prop → Prop) (λ P, ¬ P) | eval_and : evaluates_to ⟨kind.and, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ (Prop → Prop → Prop) _root_.and | eval_or : evaluates_to ⟨kind.or, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ (Prop → Prop → Prop) _root_.or | eval_implies : evaluates_to ⟨kind.implies, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ (Prop → Prop → Prop) (λ P Q, P → Q) | eval_iff : evaluates_to ⟨kind.iff, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ (Prop → Prop → Prop) _root_.iff | eval_all (t : hol.type) (h : t.ok bval) : let T := type.eval bval t h in evaluates_to ⟨kind.all, (t ⇒ mk_prop) ⇒ mk_prop, [t]⟩ ((T → Prop) → Prop) (λ P, ∀ x : T, P x) | eval_ex (t : hol.type) (h : t.ok bval) : let T := type.eval bval t h in evaluates_to ⟨kind.all, (t ⇒ mk_prop) ⇒ mk_prop, [t]⟩ ((T → Prop) → Prop) (λ P, ∃ x : T, P x) | eval_add : evaluates_to ⟨kind.add, mk_nat ⇒ mk_nat ⇒ mk_nat, []⟩ (nat → nat → nat) nat.add | eval_mul : evaluates_to ⟨kind.mul, mk_nat ⇒ mk_nat ⇒ mk_nat, []⟩ (nat → nat → nat) nat.mul | eval_sub : evaluates_to ⟨kind.sub, mk_nat ⇒ mk_nat ⇒ mk_nat, []⟩ (nat → nat → nat) nat.sub | eval_bval (b : bool) : evaluates_to ⟨kind.bval b, mk_bool, []⟩ bool b | eval_nval (n : nat) : evaluates_to ⟨kind.nval n, mk_nat, []⟩ nat n inductive ok (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : const → Prop | ok_user (n : ℕ) (t : hol.type) (l : list hol.type) (h₀ : option.is_some (cval n)) (h₁ : t.ok bval) (h₂ : t.eval bval h₁ = (option.get h₀).fst ) : ok ⟨kind.user n, t, l⟩ | ok_true : ok ⟨kind.true, mk_prop, []⟩ | ok_false : ok ⟨kind.false, mk_prop, []⟩ | ok_not : ok ⟨kind.not, mk_prop ⇒ mk_prop, []⟩ | ok_and : ok ⟨kind.and, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ | ok_or : ok ⟨kind.or, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ | ok_implies : ok ⟨kind.implies, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ | ok_iff : ok ⟨kind.iff, mk_prop ⇒ mk_prop ⇒ mk_prop, []⟩ | ok_all (t : hol.type) (h : t.ok bval) : ok ⟨kind.all, (t ⇒ mk_prop) ⇒ mk_prop, [t]⟩ | ok_ex (t : hol.type) (h : t.ok bval) : ok ⟨kind.ex, (t ⇒ mk_prop) ⇒ mk_prop, [t]⟩ | ok_add : ok ⟨kind.add, mk_nat ⇒ mk_nat ⇒ mk_nat, []⟩ | ok_mul : ok ⟨kind.mul, mk_nat ⇒ mk_nat ⇒ mk_nat, []⟩ | ok_sub : ok ⟨kind.sub, mk_nat ⇒ mk_nat ⇒ mk_nat, []⟩ | ok_bval (b : bool) : ok ⟨kind.bval b, mk_bool, []⟩ | ok_nval (n : nat) : ok ⟨kind.nval n, mk_nat, []⟩ def type_ok (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : ∀ c : const, c.ok bval cval → c.type.ok bval | ⟨kind.user n, t, l⟩ h := by cases h; assumption | ⟨kind.true, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.false, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.not, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.and, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.or, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.implies, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.iff, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.all, t, k⟩ h := begin cases k with ty k', cases h, cases k'; cases h, simp [type.ok, mk_prop, type.basic.ok], assumption end | ⟨kind.ex, t, k⟩ h := begin cases k with ty k', cases h, cases k'; cases h, simp [type.ok, mk_prop, type.basic.ok], assumption end | ⟨kind.add, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.mul, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.sub, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.bval b, t, k⟩ h := by cases h; apply eq.refl | ⟨kind.nval n, t, k⟩ h := by cases h; apply eq.refl def eval (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : Π (c : const) (h : c.ok bval cval), (c.type).eval bval (c.type_ok bval cval h) | c@⟨kind.user n, ty, l⟩ h := have h' : option.is_some (cval n), by cases h; assumption, have h'' : c.type.eval bval (c.type_ok bval cval h) = (option.get h').fst, by {cases h, assumption}, cast h''.symm (option.get h').snd | c@⟨kind.true, _, _⟩ h := cast (by cases h; reflexivity) _root_.true | c@⟨kind.false, _, _⟩ h := cast (by cases h; reflexivity) _root_.false | c@⟨kind.not, _, _⟩ h := have h' : (c.type).eval bval (c.type_ok bval cval h) = (Prop → Prop), by cases h; reflexivity, cast h'.symm (λ p : Prop, ¬ p) | c@⟨kind.and, _, _⟩ h := have h' : (c.type).eval bval (c.type_ok bval cval h) = (Prop → (Prop → Prop)), by cases h; reflexivity, cast h'.symm _root_.and | c@⟨kind.or, _, _⟩ h := have h' : (c.type).eval bval (c.type_ok bval cval h) = (Prop → (Prop → Prop)), by cases h; reflexivity, cast h'.symm _root_.or | c@⟨kind.implies, _, _⟩ h := have h' : (c.type).eval bval (c.type_ok bval cval h) = (Prop → (Prop → Prop)), by cases h; reflexivity, cast h'.symm (λ p q, p → q) | c@⟨kind.iff, _, _⟩ h := have h' : (c.type).eval bval (c.type_ok bval cval h) = (Prop → (Prop → Prop)), by cases h; reflexivity, cast h'.symm _root_.iff | c@⟨kind.all, _, [ty]⟩ h := have h' : ty.ok bval, by cases h; assumption, let T := ty.eval bval h' in have h'' : (c.type).eval bval (c.type_ok bval cval h) = ((T → Prop) → Prop), by cases h; reflexivity, cast h''.symm (λ P, ∀ x : T, P x) | c@⟨kind.ex, _, [ty]⟩ h := have h' : ty.ok bval, by cases h; assumption, let T := ty.eval bval h' in have h'' : (c.type).eval bval (c.type_ok bval cval h) = ((T → Prop) → Prop), by cases h; reflexivity, cast h''.symm Exists | c@⟨kind.add, _, _⟩ h := cast (by cases h; reflexivity) nat.add | c@⟨kind.mul, _, _⟩ h := cast (by cases h; reflexivity) nat.mul | c@⟨kind.sub, _, _⟩ h := cast (by cases h; reflexivity) nat.sub | c@⟨kind.bval b, _, _⟩ h := cast (by cases h; reflexivity) b | c@⟨kind.nval n, _, _⟩ h := cast (by cases h; reflexivity) n | c@⟨kind.all, _, []⟩ h := false.elim (by cases h) | c@⟨kind.all, _, a :: b :: l⟩ h := false.elim (by cases h) | c@⟨kind.ex, _, []⟩ h := false.elim (by cases h) | c@⟨kind.ex, _, a :: b :: l⟩ h := false.elim (by cases h) /- TODO: prove that `evaluates_to` agrees with `eval`. -/ end const inductive evaluates_to (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : term → (list (Σ T : Type*, T)) → Π T : Type*, T → Prop | eval_var {n : ℕ} {T : Type*} (σ : list (Σ T : Type*, T)) {h : n < σ.length} : evaluates_to (Var n) σ (σ.nth_le n h).fst (σ.nth_le n h).snd | eval_const {c : const} {T : Type*} {a : T} {σ} (h : c.evaluates_to bval cval T a) : evaluates_to (Const c) σ T a | eval_app {t₁ t₂ : term} {σ} {T₁ T₂ : Type} {f : T₁ → T₂} {a : T₁} (h₁ : evaluates_to t₁ σ (T₁ → T₂) f) (h₂ : evaluates_to t₂ σ T₁ a) : evaluates_to (term.App t₁ t₂) σ T₂ (f a) | eval_abs {s : string} {ty : type} {t : term} {σ} {T₁ T₂ : Type} {f : T₁ → T₂} {a : T₁} (h₁ : ty.evalp bval = some T₁) (h₂ : ∀ a : T₁, evaluates_to t (⟨T₁, a⟩ :: σ) T₂ (f a)) : evaluates_to (term.Abs s ty t) σ (T₁ → T₂) f /- The reason it is so hard to write an evaluation function for a term is that there is so much data, and constraints. To evaluate a term `t`, we need `σ` : a specification of the types of the free deBruijn indices `bval` : an interpretation of basic types `cval` : an interpretation of constants `l` : an assignment of values to the indices and we need to know: `t` is well-typed with respect to `σ`, and `σ` assigns types to all free variables `bval` interprets all the relevant basic types `cval` interprets all the relevant constants, and assigns values of the right types `l` interprets all the relevant deBruin indices, and assigns values of the right types The next complicated predicate says that a term `t` is well-typed and we have the right data to interpret it. -/ inductive ok (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : Π (t : term) (σ : list type) (l : list (Σ T : Type*, T)), Prop | var_ok (n : nat) (σ : list type) (l : list (Σ T : Type*, T)) (h₀ : n < σ.length) (h₁ : n < l.length) (h₂ : (σ.nth_le n h₀).ok bval) (h₃ : (σ.nth_le n h₀).eval bval h₂ = (l.nth_le n h₁).fst) : ok (Var n) σ l | const_ok (c : const) (σ : list type) (l : list (Σ T : Type*, T)) (h : c.ok bval cval) : ok (Const c) σ l | app_ok (t₁ t₂ : term) (σ : list type) (l : list (Σ T : Type*, T)) (h₁ : ok t₁ σ l) (h₂ : ok t₂ σ l) (h₃ : (typeof t₁ σ).is_arrow) (h₄ : (typeof t₁ σ).domain = typeof t₂ σ): ok (App t₁ t₂) σ l | abs_ok (s : string) (ty : type) (t : term) (σ : list type) (l : list (Σ T : Type*, T)) (h₀ : ty.ok bval) (h₁ : (typeof t (ty :: σ)).ok bval) ( h₂ : let T := ty.eval bval h₀ in ∀ a : T, ok t (ty :: σ) (⟨T, a⟩ :: l)) : ok (Abs s ty t) σ l def type_ok (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : Π (t : term) (σ : list type) (l : list (Σ T : Type*, T)) (h : t.ok bval cval σ l), (t.typeof σ).ok bval | t@(Var n) σ l h := have h₀ : n < σ.length, by cases h; assumption, have h' : t.typeof σ = σ.nth_le n h₀, by simp [typeof, h₀], by { rw h', cases h, assumption } | (Const c) σ l h := have h₀ : c.ok bval cval, by cases h; assumption, c.type_ok bval cval h₀ | (App t₁ t₂) σ l h := have h₁ : t₁.ok bval cval σ l, by {cases h, assumption}, by { simp [typeof], apply type.ok_codomain, apply type_ok t₁ σ l, assumption} | (Abs s ty t) σ l h := by { simp [typeof, type.ok], cases h, split; assumption } def eval (bval : ℕ → option Type*) (cval : ℕ → option (Σ T : Type*, T)) : Π (t : term) (σ : list type) (l : list (Σ T : Type*, T)) (h : t.ok bval cval σ l), (t.typeof σ).eval bval (t.type_ok bval cval σ l h) | (Var n) σ l h := have h₀ : n < σ.length, by cases h; assumption, have h₁ : n < l.length, by cases h; assumption, have h₂ : (σ.nth_le n h₀).ok bval, by cases h; assumption, have h₃ : (σ.nth_le n h₀).eval bval h₂ = (l.nth_le n h₁).fst, by cases h; assumption, have h₄ : (typeof (Var n) σ).eval bval ((Var n).type_ok bval cval σ l h) = (l.nth_le n h₁).fst, by { rw ← h₃, apply type.eval_ext, simp [typeof, h₀] }, cast h₄.symm (l.nth_le n h₁).snd | (Const c) σ l h := have h : c.ok bval cval, by cases h; assumption, c.eval bval cval h | (App t₁ t₂) σ l h := have h₁ : t₁.ok bval cval σ l, by cases h; assumption, have h₂ : t₂.ok bval cval σ l, by cases h; assumption, have h₃ : (typeof t₁ σ).is_arrow, by cases h; assumption, have h₄ : (typeof t₁ σ).domain = typeof t₂ σ, by cases h; assumption, have h₅ : typeof t₁ σ = ((typeof t₁ σ).domain ⇒ (typeof t₁ σ).codomain), from type.eq_of_is_arrow _ h₃, have h₆ : (typeof t₁ σ).ok bval, from t₁.type_ok bval cval σ l h₁, have h₇ : ((typeof t₁ σ).domain ⇒ (typeof t₁ σ).codomain).ok bval, by rw ← h₅; exact h₆, have h₈ : (typeof t₁ σ).eval bval h₆ = ((typeof t₁ σ).domain ⇒ (typeof t₁ σ).codomain).eval bval h₇, from type.eval_ext bval _ _ h₅ _ _, have h₉ : (typeof t₁ σ).domain.ok bval, from type.ok_domain _ _ h₆, have h₁₀ : (typeof t₁ σ).codomain.ok bval, from type.ok_codomain _ _ h₆, have h₁₁ : (typeof t₁ σ).eval bval h₆ = ((typeof t₁ σ).domain.eval bval h₉ → (typeof t₁ σ).codomain.eval bval h₁₀), from h₈, have h₁₂ : (typeof t₂ σ).ok bval, from t₂.type_ok bval cval σ l h₂, have h₁₃ : (typeof t₂ σ).eval bval h₁₂ = (typeof t₁ σ).domain.eval bval h₉, from type.eval_ext bval _ _ h₄.symm _ _, let v₁ := eval t₁ σ l h₁, v₂ := eval t₂ σ l h₂ in (cast h₁₁ v₁) (cast h₁₃ v₂) | (Abs s ty t) σ l h := have h₀ : ty.ok bval, by cases h; assumption, have h₁ : (typeof t (ty :: σ)).ok bval, by cases h; assumption, let T := ty.eval bval h₀ in have h₂ : ∀ a : T, t.ok bval cval (ty :: σ) (⟨T, a⟩ :: l), by cases h; assumption, have h₃ : ∀ a : T, (typeof t (ty :: σ)).ok bval, from assume a, type_ok bval cval t _ (⟨T, a⟩ :: l) (h₂ a), let v := λ a : T, eval t (ty :: σ) (⟨T, a⟩ :: l) (h₂ a) in have h₄ : (Π (a : T), type.eval bval (typeof t (ty :: σ)) (h₃ a)) = type.eval bval (typeof (Abs s ty t) σ) ((Abs s ty t).type_ok bval cval σ l h), from rfl, cast h₄ v end term end hol
ffb7a711ee725b800b071d6b271244b4e2650879
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/fresh_names.lean
a95ce72db58ecfd3c2c7b97d6127fe3c23554f0c
[]
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
976
lean
/- Copyright (c) 2020 Jannis Limperg. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jannis Limperg -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.sum import Mathlib.meta.rb_map import Mathlib.tactic.dependencies import Mathlib.PostPort namespace Mathlib /-! # Tactics for giving hypotheses fresh names When introducing hypotheses, we often want to make sure that their names are fresh, i.e. not used by any other hypothesis in the context. This file provides tactics which allow you to give a list of possible names for each hypothesis, with the tactics picking the first name that is not yet used in the context. If all names are already used, the tactics use a name derived from the first name in the list. -/ namespace tactic -- This implementation is a bit of a hack, but probably fine in practice since -- we're unlikely to need more than two or three iterations of the loop.
68d41b74dbfda7d7663d9c01c7053860408d5903
e953c38599905267210b87fb5d82dcc3e52a4214
/hott/init/ua.hlean
bbed73018fe7ef96eb2987641b9c3f568c8deaad
[ "Apache-2.0" ]
permissive
c-cube/lean
563c1020bff98441c4f8ba60111fef6f6b46e31b
0fb52a9a139f720be418dafac35104468e293b66
refs/heads/master
1,610,753,294,113
1,440,451,356,000
1,440,499,588,000
41,748,334
0
0
null
1,441,122,656,000
1,441,122,656,000
null
UTF-8
Lean
false
false
2,707
hlean
/- Copyright (c) 2014 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jakob von Raumer Ported from Coq HoTT -/ prelude import .equiv open eq equiv is_equiv equiv.ops --Ensure that the types compared are in the same universe section universe variable l variables {A B : Type.{l}} definition is_equiv_cast_of_eq (H : A = B) : is_equiv (cast H) := (@is_equiv_tr Type (λX, X) A B H) definition equiv_of_eq (H : A = B) : A ≃ B := equiv.mk _ (is_equiv_cast_of_eq H) end axiom univalence (A B : Type) : is_equiv (@equiv_of_eq A B) attribute univalence [instance] -- This is the version of univalence axiom we will probably use most often definition ua [reducible] {A B : Type} : A ≃ B → A = B := equiv_of_eq⁻¹ definition eq_equiv_equiv (A B : Type) : (A = B) ≃ (A ≃ B) := equiv.mk equiv_of_eq _ definition equiv_of_eq_ua [reducible] {A B : Type} (f : A ≃ B) : equiv_of_eq (ua f) = f := right_inv equiv_of_eq f definition cast_ua_fn {A B : Type} (f : A ≃ B) : cast (ua f) = f := ap to_fun (equiv_of_eq_ua f) definition cast_ua {A B : Type} (f : A ≃ B) (a : A) : cast (ua f) a = f a := ap10 (cast_ua_fn f) a definition ua_equiv_of_eq [reducible] {A B : Type} (p : A = B) : ua (equiv_of_eq p) = p := left_inv equiv_of_eq p definition eq_of_equiv_lift {A B : Type} (f : A ≃ B) : A = lift B := ua (f ⬝e !equiv_lift) namespace equiv -- One consequence of UA is that we can transport along equivalencies of types -- We can use this for calculation evironments protected definition transport_of_equiv [subst] (P : Type → Type) {A B : Type} (H : A ≃ B) : P A → P B := eq.transport P (ua H) -- we can "recurse" on equivalences, by replacing them by (equiv_of_eq _) definition rec_on_ua [recursor] {A B : Type} {P : A ≃ B → Type} (f : A ≃ B) (H : Π(q : A = B), P (equiv_of_eq q)) : P f := right_inv equiv_of_eq f ▸ H (ua f) -- a variant where we immediately recurse on the equality in the new goal definition rec_on_ua_idp [recursor] {A : Type} {P : Π{B}, A ≃ B → Type} {B : Type} (f : A ≃ B) (H : P equiv.refl) : P f := rec_on_ua f (λq, eq.rec_on q H) -- a variant where (equiv_of_eq (ua f)) will be replaced by f in the new goal definition rec_on_ua' {A B : Type} {P : A ≃ B → A = B → Type} (f : A ≃ B) (H : Π(q : A = B), P (equiv_of_eq q) q) : P f (ua f) := right_inv equiv_of_eq f ▸ H (ua f) -- a variant where we do both definition rec_on_ua_idp' {A : Type} {P : Π{B}, A ≃ B → A = B → Type} {B : Type} (f : A ≃ B) (H : P equiv.refl idp) : P f (ua f) := rec_on_ua' f (λq, eq.rec_on q H) end equiv
6893500c31d05919d872002a84bf8f1b198bc0a2
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/type_context_auto.lean
3b18adb9b0f5a3faba2e7517ad9f18cd2ee04b83
[]
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
640
lean
import Mathlib.PrePort import Mathlib.Lean3Lib.init.control.monad import Mathlib.Lean3Lib.init.meta.local_context import Mathlib.Lean3Lib.init.meta.tactic import Mathlib.Lean3Lib.init.meta.fun_info namespace Mathlib namespace tactic.unsafe /-- A monad that exposes the functionality of the C++ class `type_context_old`. The idea is that the methods to `type_context` are more powerful but _unsafe_ in the sense that you can create terms that do not typecheck or that are infinitely descending. Under the hood, `type_context` is implemented as a reader monad, with a mutable `type_context` object. -/ namespace type_context end Mathlib
6ead8187002bc04ee5cd60d675decf4c3cbb1440
aa3f8992ef7806974bc1ffd468baa0c79f4d6643
/tests/lean/run/group3.lean
b48e0da129fd2b59ad01d4e6c208c17260d33df3
[ "Apache-2.0" ]
permissive
codyroux/lean
7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3
0cca265db19f7296531e339192e9b9bae4a31f8b
refs/heads/master
1,610,909,964,159
1,407,084,399,000
1,416,857,075,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,252
lean
-- Copyright (c) 2014 Jeremy Avigad. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Jeremy Avigad, Leonardo de Moura -- algebra.group -- ============= -- Various structures with 1, *, inv, including groups. import logic.eq logic.connectives import data.unit data.sigma data.prod import algebra.function algebra.binary open eq namespace algebra -- classes for notation -- -------------------- inductive has_mul [class] (A : Type) : Type := mk : (A → A → A) → has_mul A inductive has_one [class] (A : Type) : Type := mk : A → has_one A inductive has_inv [class] (A : Type) : Type := mk : (A → A) → has_inv A definition mul {A : Type} [s : has_mul A] (a b : A) : A := has_mul.rec (λf, f) s a b definition one {A : Type} [s : has_one A] : A := has_one.rec (λo, o) s definition inv {A : Type} [s : has_inv A] (a : A) : A := has_inv.rec (λi, i) s a infix `*` := mul postfix `⁻¹` := inv notation 1 := one -- semigroup -- --------- inductive semigroup [class] (A : Type) : Type := mk : Π mul: A → A → A, (∀a b c : A, (mul (mul a b) c = mul a (mul b c))) → semigroup A namespace semigroup section variables {A : Type} [s : semigroup A] variables a b c : A definition mul := semigroup.rec (λmul assoc, mul) s a b context infixl `*` := mul definition assoc : (a * b) * c = a * (b * c) := semigroup.rec (λmul assoc, assoc) s a b c end end end semigroup section variables {A : Type} [s : semigroup A] include s definition semigroup_has_mul [instance] : has_mul A := has_mul.mk semigroup.mul theorem mul_assoc (a b c : A) : a * b * c = a * (b * c) := !semigroup.assoc end -- comm_semigroup -- -------------- inductive comm_semigroup [class] (A : Type) : Type := mk : Π (mul: A → A → A) (infixl `*` := mul), (∀a b c, (a * b) * c = a * (b * c)) → (∀a b, a * b = b * a) → comm_semigroup A namespace comm_semigroup section variables {A : Type} [s : comm_semigroup A] variables a b c : A definition mul (a b : A) : A := comm_semigroup.rec (λmul assoc comm, mul) s a b definition assoc : mul (mul a b) c = mul a (mul b c) := comm_semigroup.rec (λmul assoc comm, assoc) s a b c definition comm : mul a b = mul b a := comm_semigroup.rec (λmul assoc comm, comm) s a b end end comm_semigroup section variables {A : Type} [s : comm_semigroup A] variables a b c : A include s definition comm_semigroup_semigroup [instance] : semigroup A := semigroup.mk comm_semigroup.mul comm_semigroup.assoc theorem mul_comm : a * b = b * a := !comm_semigroup.comm theorem mul_left_comm : a * (b * c) = b * (a * c) := binary.left_comm mul_comm mul_assoc a b c end -- monoid -- ------ inductive monoid [class] (A : Type) : Type := mk : Π (mul: A → A → A) (one : A) (infixl `*` := mul) (notation 1 := one), (∀a b c, (a * b) * c = a * (b * c)) → (∀a, a * 1 = a) → (∀a, 1 * a = a) → monoid A namespace monoid section variables {A : Type} [s : monoid A] variables a b c : A include s context definition mul := monoid.rec (λmul one assoc right_id left_id, mul) s a b definition one := monoid.rec (λmul one assoc right_id left_id, one) s infixl `*` := mul notation 1 := one definition assoc : (a * b) * c = a * (b * c) := monoid.rec (λmul one assoc right_id left_id, assoc) s a b c definition right_id : a * 1 = a := monoid.rec (λmul one assoc right_id left_id, right_id) s a definition left_id : 1 * a = a := monoid.rec (λmul one assoc right_id left_id, left_id) s a end end end monoid section variables {A : Type} [s : monoid A] variable a : A include s definition monoid_has_one [instance] : has_one A := has_one.mk (monoid.one) definition monoid_semigroup [instance] : semigroup A := semigroup.mk monoid.mul monoid.assoc theorem mul_right_id : a * 1 = a := !monoid.right_id theorem mul_left_id : 1 * a = a := !monoid.left_id end -- comm_monoid -- ----------- inductive comm_monoid [class] (A : Type) : Type := mk : Π (mul: A → A → A) (one : A) (infixl `*` := mul) (notation 1 := one), (∀a b c, (a * b) * c = a * (b * c)) → (∀a, a * 1 = a) → (∀a, 1 * a = a) → (∀a b, a * b = b * a) → comm_monoid A namespace comm_monoid section variables {A : Type} [s : comm_monoid A] variables a b c : A definition mul := comm_monoid.rec (λmul one assoc right_id left_id comm, mul) s a b definition one := comm_monoid.rec (λmul one assoc right_id left_id comm, one) s definition assoc : mul (mul a b) c = mul a (mul b c) := comm_monoid.rec (λmul one assoc right_id left_id comm, assoc) s a b c definition right_id : mul a one = a := comm_monoid.rec (λmul one assoc right_id left_id comm, right_id) s a definition left_id : mul one a = a := comm_monoid.rec (λmul one assoc right_id left_id comm, left_id) s a definition comm : mul a b = mul b a := comm_monoid.rec (λmul one assoc right_id left_id comm, comm) s a b end end comm_monoid section variables {A : Type} [s : comm_monoid A] include s definition comm_monoid_monoid [instance] : monoid A := monoid.mk comm_monoid.mul comm_monoid.one comm_monoid.assoc comm_monoid.right_id comm_monoid.left_id definition comm_monoid_comm_semigroup [instance] : comm_semigroup A := comm_semigroup.mk comm_monoid.mul comm_monoid.assoc comm_monoid.comm end -- bundled structures -- ------------------ inductive Semigroup [class] : Type := mk : Π carrier : Type, semigroup carrier → Semigroup section variable S : Semigroup definition Semigroup.carrier [coercion] : Type := Semigroup.rec (λc s, c) S definition Semigroup.struc [instance] : semigroup S := Semigroup.rec (λc s, s) S end inductive CommSemigroup [class] : Type := mk : Π carrier : Type, comm_semigroup carrier → CommSemigroup section variable S : CommSemigroup definition CommSemigroup.carrier [coercion] : Type := CommSemigroup.rec (λc s, c) S definition CommSemigroup.struc [instance] : comm_semigroup S := CommSemigroup.rec (λc s, s) S end inductive Monoid [class] : Type := mk : Π carrier : Type, monoid carrier → Monoid section variable S : Monoid definition Monoid.carrier [coercion] : Type := Monoid.rec (λc s, c) S definition Monoid.struc [instance] : monoid S := Monoid.rec (λc s, s) S end inductive CommMonoid : Type := mk : Π carrier : Type, comm_monoid carrier → CommMonoid section variable S : CommMonoid definition CommMonoid.carrier [coercion] : Type := CommMonoid.rec (λc s, c) S definition CommMonoid.struc [instance] : comm_monoid S := CommMonoid.rec (λc s, s) S end end algebra open algebra section examples theorem test1 {S : Semigroup} (a b c d : S) : a * (b * c) * d = a * b * (c * d) := calc a * (b * c) * d = a * b * c * d : {symm !mul_assoc} ... = a * b * (c * d) : !mul_assoc theorem test2 {M : CommSemigroup} (a b : M) : a * b = a * b := rfl theorem test3 {M : Monoid} (a b c d : M) : a * (b * c) * d = a * b * (c * d) := calc a * (b * c) * d = a * b * c * d : {symm !mul_assoc} ... = a * b * (c * d) : !mul_assoc -- for test4b to work, we need instances at the level of the bundled structures as well definition Monoid_Semigroup [instance] (M : Monoid) : Semigroup := Semigroup.mk (Monoid.carrier M) _ theorem test4 {M : Monoid} (a b c d : M) : a * (b * c) * d = a * b * (c * d) := test1 a b c d theorem test5 {M : Monoid} (a b c : M) : a * 1 * b * c = a * (b * c) := calc a * 1 * b * c = a * b * c : {!mul_right_id} ... = a * (b * c) : !mul_assoc theorem test5a {M : Monoid} (a b c : M) : a * 1 * b * c = a * (b * c) := calc a * 1 * b * c = a * b * c : {!mul_right_id} ... = a * (b * c) : !mul_assoc theorem test5b {A : Type} {M : monoid A} (a b c : A) : a * 1 * b * c = a * (b * c) := calc a * 1 * b * c = a * b * c : {!mul_right_id} ... = a * (b * c) : !mul_assoc theorem test6 {M : CommMonoid} (a b c : M) : a * 1 * b * c = a * (b * c) := calc a * 1 * b * c = a * b * c : {!mul_right_id} ... = a * (b * c) : !mul_assoc end examples
b81aeeac0d33e6f4991559d48448fac3c4d8cb37
649957717d58c43b5d8d200da34bf374293fe739
/src/category_theory/monoidal/category.lean
021c78a7decf252b8fcd916ada8e642e2a89e166
[ "Apache-2.0" ]
permissive
Vtec234/mathlib
b50c7b21edea438df7497e5ed6a45f61527f0370
fb1848bbbfce46152f58e219dc0712f3289d2b20
refs/heads/master
1,592,463,095,113
1,562,737,749,000
1,562,737,749,000
196,202,858
0
0
Apache-2.0
1,562,762,338,000
1,562,762,337,000
null
UTF-8
Lean
false
false
14,800
lean
-- Copyright (c) 2018 Michael Jendrusch. All rights reserved. -- Released under Apache 2.0 license as described in the file LICENSE. -- Authors: Michael Jendrusch, Scott Morrison import category_theory.monoidal.category_aux import category_theory.natural_isomorphism import tactic.basic import tactic.slice open category_theory universes v u open category_theory open category_theory.category open category_theory.iso namespace category_theory class monoidal_category (C : Type u) extends category.{v} C := -- curried tensor product of objects: (tensor_obj : C → C → C) (infixr ` ⊗ `:70 := tensor_obj) -- This notation is only temporary -- curried tensor product of morphisms: (tensor_hom : Π {X₁ Y₁ X₂ Y₂ : C}, hom X₁ Y₁ → hom X₂ Y₂ → hom (X₁ ⊗ X₂) (Y₁ ⊗ Y₂)) (infixr ` ⊗' `:69 := tensor_hom) -- This notation is only temporary -- tensor product laws: (tensor_id' : ∀ (X₁ X₂ : C), (𝟙 X₁) ⊗' (𝟙 X₂) = 𝟙 (X₁ ⊗ X₂) . obviously) (tensor_comp' : ∀ {X₁ Y₁ Z₁ X₂ Y₂ Z₂ : C} (f₁ : X₁ ⟶ Y₁) (f₂ : X₂ ⟶ Y₂) (g₁ : Y₁ ⟶ Z₁) (g₂ : Y₂ ⟶ Z₂), (f₁ ≫ g₁) ⊗' (f₂ ≫ g₂) = (f₁ ⊗' f₂) ≫ (g₁ ⊗' g₂) . obviously) -- tensor unit: (tensor_unit : C) -- associator: (associator : Π X Y Z : C, (X ⊗ Y) ⊗ Z ≅ X ⊗ (Y ⊗ Z)) (associator_naturality' : assoc_natural tensor_obj @tensor_hom associator . obviously) -- left unitor: (left_unitor : Π X : C, tensor_unit ⊗ X ≅ X) (left_unitor_naturality' : left_unitor_natural tensor_obj @tensor_hom tensor_unit left_unitor . obviously) -- right unitor: (right_unitor : Π X : C, X ⊗ tensor_unit ≅ X) (right_unitor_naturality' : right_unitor_natural tensor_obj @tensor_hom tensor_unit right_unitor . obviously) -- pentagon identity: (pentagon' : pentagon @tensor_hom associator . obviously) -- triangle identity: (triangle' : triangle @tensor_hom left_unitor right_unitor associator . obviously) restate_axiom monoidal_category.tensor_id' attribute [simp] monoidal_category.tensor_id restate_axiom monoidal_category.tensor_comp' attribute [simp] monoidal_category.tensor_comp restate_axiom monoidal_category.associator_naturality' restate_axiom monoidal_category.left_unitor_naturality' restate_axiom monoidal_category.right_unitor_naturality' restate_axiom monoidal_category.pentagon' restate_axiom monoidal_category.triangle' attribute [simp] monoidal_category.triangle open monoidal_category infixr ` ⊗ `:70 := tensor_obj infixr ` ⊗ `:70 := tensor_hom local notation `𝟙_` := tensor_unit local notation `α_` := associator local notation `λ_` := left_unitor local notation `ρ_` := right_unitor def tensor_iso {C : Type u} {X Y X' Y' : C} [monoidal_category.{v} C] (f : X ≅ Y) (g : X' ≅ Y') : X ⊗ X' ≅ Y ⊗ Y' := { hom := f.hom ⊗ g.hom, inv := f.inv ⊗ g.inv, hom_inv_id' := by rw [←tensor_comp, iso.hom_inv_id, iso.hom_inv_id, ←tensor_id], inv_hom_id' := by rw [←tensor_comp, iso.inv_hom_id, iso.inv_hom_id, ←tensor_id] } infixr ` ⊗ `:70 := tensor_iso namespace monoidal_category section variables {C : Type u} [𝒞 : monoidal_category.{v} C] include 𝒞 instance tensor_is_iso {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] : is_iso (f ⊗ g) := { ..(as_iso f ⊗ as_iso g) } @[simp] lemma inv_tensor {W X Y Z : C} (f : W ⟶ X) [is_iso f] (g : Y ⟶ Z) [is_iso g] : inv (f ⊗ g) = inv f ⊗ inv g := rfl variables {U V W X Y Z : C} -- When `rewrite_search` lands, add @[search] attributes to -- monoidal_category.tensor_id monoidal_category.tensor_comp monoidal_category.associator_naturality -- monoidal_category.left_unitor_naturality monoidal_category.right_unitor_naturality -- monoidal_category.pentagon monoidal_category.triangle -- tensor_comp_id tensor_id_comp comp_id_tensor_tensor_id -- triangle_assoc_comp_left triangle_assoc_comp_right triangle_assoc_comp_left_inv triangle_assoc_comp_right_inv -- left_unitor_tensor left_unitor_tensor_inv -- right_unitor_tensor right_unitor_tensor_inv -- pentagon_inv -- associator_inv_naturality -- left_unitor_inv_naturality -- right_unitor_inv_naturality @[simp] lemma comp_tensor_id (f : W ⟶ X) (g : X ⟶ Y) : (f ≫ g) ⊗ (𝟙 Z) = (f ⊗ (𝟙 Z)) ≫ (g ⊗ (𝟙 Z)) := by { rw ←tensor_comp, simp } @[simp] lemma id_tensor_comp (f : W ⟶ X) (g : X ⟶ Y) : (𝟙 Z) ⊗ (f ≫ g) = (𝟙 Z ⊗ f) ≫ (𝟙 Z ⊗ g) := by { rw ←tensor_comp, simp } @[simp] lemma id_tensor_comp_tensor_id (f : W ⟶ X) (g : Y ⟶ Z) : ((𝟙 Y) ⊗ f) ≫ (g ⊗ (𝟙 X)) = g ⊗ f := by { rw [←tensor_comp], simp } @[simp] lemma tensor_id_comp_id_tensor (f : W ⟶ X) (g : Y ⟶ Z) : (g ⊗ (𝟙 W)) ≫ ((𝟙 Z) ⊗ f) = g ⊗ f := by { rw [←tensor_comp], simp } lemma left_unitor_inv_naturality {X X' : C} (f : X ⟶ X') : f ≫ (λ_ X').inv = (λ_ X).inv ≫ (𝟙 _ ⊗ f) := begin apply (cancel_mono (λ_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [left_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma right_unitor_inv_naturality {X X' : C} (f : X ⟶ X') : f ≫ (ρ_ X').inv = (ρ_ X).inv ≫ (f ⊗ 𝟙 _) := begin apply (cancel_mono (ρ_ X').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [right_unitor_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end @[simp] lemma tensor_left_iff {X Y : C} (f g : X ⟶ Y) : ((𝟙 (𝟙_ C)) ⊗ f = (𝟙 (𝟙_ C)) ⊗ g) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (λ k, (λ_ _).inv ≫ k) h, dsimp at h', rw [←left_unitor_inv_naturality, ←left_unitor_inv_naturality] at h', exact (cancel_mono _).1 h', }, { intro h, subst h, } end @[simp] lemma tensor_right_iff {X Y : C} (f g : X ⟶ Y) : (f ⊗ (𝟙 (𝟙_ C)) = g ⊗ (𝟙 (𝟙_ C))) ↔ (f = g) := begin split, { intro h, have h' := congr_arg (λ k, (ρ_ _).inv ≫ k) h, dsimp at h', rw [←right_unitor_inv_naturality, ←right_unitor_inv_naturality] at h', exact (cancel_mono _).1 h' }, { intro h, subst h, } end -- We now prove: -- ((α_ (𝟙_ C) X Y).hom) ≫ -- ((λ_ (X ⊗ Y)).hom) -- = ((λ_ X).hom ⊗ (𝟙 Y)) -- (and the corresponding fact for right unitors) -- following the proof on nLab: -- Lemma 2.2 at https://ncatlab.org/nlab/revision/monoidal+category/115 lemma left_unitor_product_aux_perimeter (X Y : C) : ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫ (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom) = (((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y)) ≫ (α_ (𝟙_ C) X Y).hom := begin conv_lhs { congr, skip, rw [←category.assoc] }, rw [←category.assoc, monoidal_category.pentagon, associator_naturality, tensor_id, ←monoidal_category.triangle, ←category.assoc] end lemma left_unitor_product_aux_triangle (X Y : C) : ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y)) ≫ (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) = ((ρ_ (𝟙_ C)).hom ⊗ (𝟙 X)) ⊗ (𝟙 Y) := by rw [←comp_tensor_id, ←monoidal_category.triangle] lemma left_unitor_product_aux_square (X Y : C) : (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom ≫ ((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom ⊗ (𝟙 Y)) = (((𝟙 (𝟙_ C)) ⊗ (λ_ X).hom) ⊗ (𝟙 Y)) ≫ (α_ (𝟙_ C) X Y).hom := by rw associator_naturality lemma left_unitor_product_aux (X Y : C) : ((𝟙 (𝟙_ C)) ⊗ (α_ (𝟙_ C) X Y).hom) ≫ ((𝟙 (𝟙_ C)) ⊗ (λ_ (X ⊗ Y)).hom) = (𝟙 (𝟙_ C)) ⊗ ((λ_ X).hom ⊗ (𝟙 Y)) := begin rw ←(cancel_epi (α_ (𝟙_ C) ((𝟙_ C) ⊗ X) Y).hom), rw left_unitor_product_aux_square, rw ←(cancel_epi ((α_ (𝟙_ C) (𝟙_ C) X).hom ⊗ (𝟙 Y))), slice_rhs 1 2 { rw left_unitor_product_aux_triangle }, conv_lhs { rw [left_unitor_product_aux_perimeter] } end lemma right_unitor_product_aux_perimeter (X Y : C) : ((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫ ((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom) = ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (α_ X Y (𝟙_ C)).hom := begin transitivity (((α_ X Y _).hom ⊗ 𝟙 _) ≫ (α_ X _ _).hom ≫ (𝟙 X ⊗ (α_ Y _ _).hom)) ≫ (𝟙 X ⊗ 𝟙 Y ⊗ (λ_ _).hom), { conv_lhs { congr, skip, rw [←category.assoc] }, conv_rhs { rw [category.assoc] } }, { conv_lhs { congr, rw [monoidal_category.pentagon] }, conv_rhs { congr, rw [←monoidal_category.triangle] }, conv_rhs { rw [category.assoc] }, conv_rhs { congr, skip, congr, congr, rw [←tensor_id] }, conv_rhs { congr, skip, rw [associator_naturality] }, conv_rhs { rw [←category.assoc] } } end lemma right_unitor_product_aux_triangle (X Y : C) : ((𝟙 X) ⊗ (α_ Y (𝟙_ C) (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (𝟙 Y) ⊗ (λ_ (𝟙_ C)).hom) = (𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C)) := by rw [←id_tensor_comp, ←monoidal_category.triangle] lemma right_unitor_product_aux_square (X Y : C) : (α_ X (Y ⊗ (𝟙_ C)) (𝟙_ C)).hom ≫ ((𝟙 X) ⊗ (ρ_ Y).hom ⊗ (𝟙 (𝟙_ C))) = (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) ≫ (α_ X Y (𝟙_ C)).hom := by rw [associator_naturality] lemma right_unitor_product_aux (X Y : C) : ((α_ X Y (𝟙_ C)).hom ⊗ (𝟙 (𝟙_ C))) ≫ (((𝟙 X) ⊗ (ρ_ Y).hom) ⊗ (𝟙 (𝟙_ C))) = ((ρ_ (X ⊗ Y)).hom ⊗ (𝟙 (𝟙_ C))) := begin rw ←(cancel_mono (α_ X Y (𝟙_ C)).hom), slice_lhs 2 3 { rw ←right_unitor_product_aux_square }, rw [←right_unitor_product_aux_triangle, ←right_unitor_product_aux_perimeter], end -- See Proposition 2.2.4 of http://www-math.mit.edu/~etingof/egnobookfinal.pdf @[simp] lemma left_unitor_tensor (X Y : C) : ((α_ (𝟙_ C) X Y).hom) ≫ ((λ_ (X ⊗ Y)).hom) = ((λ_ X).hom ⊗ (𝟙 Y)) := by rw [←tensor_left_iff, id_tensor_comp, left_unitor_product_aux] @[simp] lemma left_unitor_tensor_inv (X Y : C) : ((λ_ (X ⊗ Y)).inv) ≫ ((α_ (𝟙_ C) X Y).inv) = ((λ_ X).inv ⊗ (𝟙 Y)) := eq_of_inv_eq_inv (by simp) @[simp] lemma right_unitor_tensor (X Y : C) : ((α_ X Y (𝟙_ C)).hom) ≫ ((𝟙 X) ⊗ (ρ_ Y).hom) = ((ρ_ (X ⊗ Y)).hom) := by rw [←tensor_right_iff, comp_tensor_id, right_unitor_product_aux] @[simp] lemma right_unitor_tensor_inv (X Y : C) : ((𝟙 X) ⊗ (ρ_ Y).inv) ≫ ((α_ X Y (𝟙_ C)).inv) = ((ρ_ (X ⊗ Y)).inv) := eq_of_inv_eq_inv (by simp) lemma associator_inv_naturality {X Y Z X' Y' Z' : C} (f : X ⟶ X') (g : Y ⟶ Y') (h : Z ⟶ Z') : (f ⊗ (g ⊗ h)) ≫ (α_ X' Y' Z').inv = (α_ X Y Z).inv ≫ ((f ⊗ g) ⊗ h) := begin apply (cancel_mono (α_ X' Y' Z').hom).1, simp only [assoc, comp_id, iso.inv_hom_id], rw [associator_naturality, ←category.assoc, iso.inv_hom_id, category.id_comp] end lemma pentagon_inv (W X Y Z : C) : ((𝟙 W) ⊗ (α_ X Y Z).inv) ≫ (α_ W (X ⊗ Y) Z).inv ≫ ((α_ W X Y).inv ⊗ (𝟙 Z)) = (α_ W X (Y ⊗ Z)).inv ≫ (α_ (W ⊗ X) Y Z).inv := begin apply category_theory.eq_of_inv_eq_inv, dsimp, rw [category.assoc, monoidal_category.pentagon] end @[simp] lemma triangle_assoc_comp_left (X Y : C) : (α_ X (𝟙_ C) Y).hom ≫ ((𝟙 X) ⊗ (λ_ Y).hom) = (ρ_ X).hom ⊗ 𝟙 Y := monoidal_category.triangle C X Y @[simp] lemma triangle_assoc_comp_right (X Y : C) : (α_ X (𝟙_ C) Y).inv ≫ ((ρ_ X).hom ⊗ 𝟙 Y) = ((𝟙 X) ⊗ (λ_ Y).hom) := by rw [←triangle_assoc_comp_left, ←category.assoc, iso.inv_hom_id, category.id_comp] @[simp] lemma triangle_assoc_comp_right_inv (X Y : C) : ((ρ_ X).inv ⊗ 𝟙 Y) ≫ (α_ X (𝟙_ C) Y).hom = ((𝟙 X) ⊗ (λ_ Y).inv) := begin apply (cancel_mono (𝟙 X ⊗ (λ_ Y).hom)).1, simp only [assoc, triangle_assoc_comp_left], rw [←comp_tensor_id, iso.inv_hom_id, ←id_tensor_comp, iso.inv_hom_id] end @[simp] lemma triangle_assoc_comp_left_inv (X Y : C) : ((𝟙 X) ⊗ (λ_ Y).inv) ≫ (α_ X (𝟙_ C) Y).inv = ((ρ_ X).inv ⊗ 𝟙 Y) := begin apply (cancel_mono ((ρ_ X).hom ⊗ 𝟙 Y)).1, simp only [triangle_assoc_comp_right, assoc], rw [←id_tensor_comp, iso.inv_hom_id, ←comp_tensor_id, iso.inv_hom_id] end end section -- In order to be able to describe the tensor product as a functor, we -- need to be up in at least `Type 0` for both objects and morphisms, -- so that we can construct products. variables (C : Type u) [𝒞 : monoidal_category.{v+1} C] include 𝒞 def tensor : (C × C) ⥤ C := { obj := λ X, tensor_obj X.1 X.2, map := λ {X Y : C × C} (f : X ⟶ Y), tensor_hom f.1 f.2 } def left_assoc_tensor : (C × C × C) ⥤ C := { obj := λ X, (X.1 ⊗ X.2.1) ⊗ X.2.2, map := λ {X Y : C × C × C} (f : X ⟶ Y), (f.1 ⊗ f.2.1) ⊗ f.2.2 } @[simp] lemma left_assoc_tensor_obj (X) : (left_assoc_tensor C).obj X = (X.1 ⊗ X.2.1) ⊗ X.2.2 := rfl @[simp] lemma left_assoc_tensor_map {X Y} (f : X ⟶ Y) : (left_assoc_tensor C).map f = (f.1 ⊗ f.2.1) ⊗ f.2.2 := rfl def right_assoc_tensor : (C × C × C) ⥤ C := { obj := λ X, X.1 ⊗ (X.2.1 ⊗ X.2.2), map := λ {X Y : C × C × C} (f : X ⟶ Y), f.1 ⊗ (f.2.1 ⊗ f.2.2) } @[simp] lemma right_assoc_tensor_obj (X) : (right_assoc_tensor C).obj X = X.1 ⊗ (X.2.1 ⊗ X.2.2) := rfl @[simp] lemma right_assoc_tensor_map {X Y} (f : X ⟶ Y) : (right_assoc_tensor C).map f = f.1 ⊗ (f.2.1 ⊗ f.2.2) := rfl def tensor_unit_left : C ⥤ C := { obj := λ X, 𝟙_ C ⊗ X, map := λ {X Y : C} (f : X ⟶ Y), (𝟙 (𝟙_ C)) ⊗ f } def tensor_unit_right : C ⥤ C := { obj := λ X, X ⊗ 𝟙_ C, map := λ {X Y : C} (f : X ⟶ Y), f ⊗ (𝟙 (𝟙_ C)) } -- We can express the associator and the unitors, given componentwise above, -- as natural isomorphisms. def associator_nat_iso : left_assoc_tensor C ≅ right_assoc_tensor C := nat_iso.of_components (by { intros, apply monoidal_category.associator }) (by { intros, apply monoidal_category.associator_naturality }) def left_unitor_nat_iso : tensor_unit_left C ≅ functor.id C := nat_iso.of_components (by { intros, apply monoidal_category.left_unitor }) (by { intros, apply monoidal_category.left_unitor_naturality }) def right_unitor_nat_iso : tensor_unit_right C ≅ functor.id C := nat_iso.of_components (by { intros, apply monoidal_category.right_unitor }) (by { intros, apply monoidal_category.right_unitor_naturality }) end end monoidal_category end category_theory
174057dc2bf6046efd0caa849200fdd1ad037d06
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/linear_algebra/affine_space/finite_dimensional.lean
cba33a297c0bbebdf4805520c22e747ed8d9bf9a
[ "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
15,470
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 linear_algebra.affine_space.independent import linear_algebra.finite_dimensional /-! # Finite-dimensional subspaces of affine spaces. This file provides a few results relating to finite-dimensional subspaces of affine spaces. ## Main definitions * `collinear` defines collinear sets of points as those that span a subspace of dimension at most 1. -/ noncomputable theory open_locale big_operators classical affine section affine_space' variables (k : Type*) {V : Type*} {P : Type*} [field k] [add_comm_group V] [module k V] [affine_space V P] variables {ι : Type*} include V open affine_subspace finite_dimensional module /-- The `vector_span` of a finite set is finite-dimensional. -/ lemma finite_dimensional_vector_span_of_finite {s : set P} (h : set.finite s) : finite_dimensional k (vector_span k s) := span_of_finite k $ h.vsub h /-- The `vector_span` of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_vector_span_of_fintype [fintype ι] (p : ι → P) : finite_dimensional k (vector_span k (set.range p)) := finite_dimensional_vector_span_of_finite k (set.finite_range _) /-- The `vector_span` of a subset of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_vector_span_image_of_fintype [fintype ι] (p : ι → P) (s : set ι) : finite_dimensional k (vector_span k (p '' s)) := finite_dimensional_vector_span_of_finite k ((set.finite.of_fintype _).image _) /-- The direction of the affine span of a finite set is finite-dimensional. -/ lemma finite_dimensional_direction_affine_span_of_finite {s : set P} (h : set.finite s) : finite_dimensional k (affine_span k s).direction := (direction_affine_span k s).symm ▸ finite_dimensional_vector_span_of_finite k h /-- The direction of the affine span of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_direction_affine_span_of_fintype [fintype ι] (p : ι → P) : finite_dimensional k (affine_span k (set.range p)).direction := finite_dimensional_direction_affine_span_of_finite k (set.finite_range _) /-- The direction of the affine span of a subset of a family indexed by a `fintype` is finite-dimensional. -/ instance finite_dimensional_direction_affine_span_image_of_fintype [fintype ι] (p : ι → P) (s : set ι) : finite_dimensional k (affine_span k (p '' s)).direction := finite_dimensional_direction_affine_span_of_finite k ((set.finite.of_fintype _).image _) variables {k} /-- The `vector_span` of a finite subset of an affinely independent family has dimension one less than its cardinality. -/ lemma finrank_vector_span_image_finset_of_affine_independent {p : ι → P} (hi : affine_independent k p) {s : finset ι} {n : ℕ} (hc : finset.card s = n + 1) : finrank k (vector_span k (s.image p : set P)) = n := begin have hi' := affine_independent_of_subset_affine_independent (affine_independent_set_of_affine_independent hi) (set.image_subset_range p ↑s), have hc' : (s.image p).card = n + 1, { rwa [s.card_image_of_injective (injective_of_affine_independent hi)] }, have hn : (s.image p).nonempty, { simp [hc', ←finset.card_pos] }, rcases hn with ⟨p₁, hp₁⟩, have hp₁' : p₁ ∈ p '' s := by simpa using hp₁, rw [affine_independent_set_iff_linear_independent_vsub k hp₁', ← finset.coe_singleton, ← finset.coe_image, ← finset.coe_sdiff, finset.sdiff_singleton_eq_erase, ← finset.coe_image] at hi', have hc : (finset.image (λ (p : P), p -ᵥ p₁) ((finset.image p s).erase p₁)).card = n, { rw [finset.card_image_of_injective _ (vsub_left_injective _), finset.card_erase_of_mem hp₁], exact nat.pred_eq_of_eq_succ hc' }, rwa [vector_span_eq_span_vsub_finset_right_ne k hp₁, finrank_span_finset_eq_card, hc] end /-- The `vector_span` of a finite affinely independent family has dimension one less than its cardinality. -/ lemma finrank_vector_span_of_affine_independent [fintype ι] {p : ι → P} (hi : affine_independent k p) {n : ℕ} (hc : fintype.card ι = n + 1) : finrank k (vector_span k (set.range p)) = n := begin rw ← finset.card_univ at hc, rw [← set.image_univ, ← finset.coe_univ, ← finset.coe_image], exact finrank_vector_span_image_finset_of_affine_independent hi hc end /-- If the `vector_span` of a finite subset of an affinely independent family lies in a submodule with dimension one less than its cardinality, it equals that submodule. -/ lemma vector_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one {p : ι → P} (hi : affine_independent k p) {s : finset ι} {sm : submodule k V} [finite_dimensional k sm] (hle : vector_span k (s.image p : set P) ≤ sm) (hc : finset.card s = finrank k sm + 1) : vector_span k (s.image p : set P) = sm := eq_of_le_of_finrank_eq hle $ finrank_vector_span_image_finset_of_affine_independent hi hc /-- If the `vector_span` of a finite affinely independent family lies in a submodule with dimension one less than its cardinality, it equals that submodule. -/ lemma vector_span_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one [fintype ι] {p : ι → P} (hi : affine_independent k p) {sm : submodule k V} [finite_dimensional k sm] (hle : vector_span k (set.range p) ≤ sm) (hc : fintype.card ι = finrank k sm + 1) : vector_span k (set.range p) = sm := eq_of_le_of_finrank_eq hle $ finrank_vector_span_of_affine_independent hi hc /-- If the `affine_span` of a finite subset of an affinely independent family lies in an affine subspace whose direction has dimension one less than its cardinality, it equals that subspace. -/ lemma affine_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one {p : ι → P} (hi : affine_independent k p) {s : finset ι} {sp : affine_subspace k P} [finite_dimensional k sp.direction] (hle : affine_span k (s.image p : set P) ≤ sp) (hc : finset.card s = finrank k sp.direction + 1) : affine_span k (s.image p : set P) = sp := begin have hn : (s.image p).nonempty, { rw [finset.nonempty.image_iff, ← finset.card_pos, hc], apply nat.succ_pos }, refine eq_of_direction_eq_of_nonempty_of_le _ ((affine_span_nonempty k _).2 hn) hle, have hd := direction_le hle, rw direction_affine_span at ⊢ hd, exact vector_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one hi hd hc end /-- If the `affine_span` of a finite affinely independent family lies in an affine subspace whose direction has dimension one less than its cardinality, it equals that subspace. -/ lemma affine_span_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one [fintype ι] {p : ι → P} (hi : affine_independent k p) {sp : affine_subspace k P} [finite_dimensional k sp.direction] (hle : affine_span k (set.range p) ≤ sp) (hc : fintype.card ι = finrank k sp.direction + 1) : affine_span k (set.range p) = sp := begin rw ←finset.card_univ at hc, rw [←set.image_univ, ←finset.coe_univ, ← finset.coe_image] at ⊢ hle, exact affine_span_image_finset_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one hi hle hc end /-- The `vector_span` of a finite affinely independent family whose cardinality is one more than that of the finite-dimensional space is `⊤`. -/ lemma vector_span_eq_top_of_affine_independent_of_card_eq_finrank_add_one [finite_dimensional k V] [fintype ι] {p : ι → P} (hi : affine_independent k p) (hc : fintype.card ι = finrank k V + 1) : vector_span k (set.range p) = ⊤ := eq_top_of_finrank_eq $ finrank_vector_span_of_affine_independent hi hc /-- The `affine_span` of a finite affinely independent family whose cardinality is one more than that of the finite-dimensional space is `⊤`. -/ lemma affine_span_eq_top_of_affine_independent_of_card_eq_finrank_add_one [finite_dimensional k V] [fintype ι] {p : ι → P} (hi : affine_independent k p) (hc : fintype.card ι = finrank k V + 1) : affine_span k (set.range p) = ⊤ := begin rw [←finrank_top, ←direction_top k V P] at hc, exact affine_span_eq_of_le_of_affine_independent_of_card_eq_finrank_add_one hi le_top hc end variables (k) /-- The `vector_span` of `n + 1` points in an indexed family has dimension at most `n`. -/ lemma finrank_vector_span_image_finset_le (p : ι → P) (s : finset ι) {n : ℕ} (hc : finset.card s = n + 1) : finrank k (vector_span k (s.image p : set P)) ≤ n := begin have hn : (s.image p).nonempty, { rw [finset.nonempty.image_iff, ← finset.card_pos, hc], apply nat.succ_pos }, rcases hn with ⟨p₁, hp₁⟩, rw [vector_span_eq_span_vsub_finset_right_ne k hp₁], refine le_trans (finrank_span_finset_le_card (((s.image p).erase p₁).image (λ p, p -ᵥ p₁))) _, rw [finset.card_image_of_injective _ (vsub_left_injective p₁), finset.card_erase_of_mem hp₁, nat.pred_le_iff, nat.succ_eq_add_one, ← hc], apply finset.card_image_le end /-- The `vector_span` of an indexed family of `n + 1` points has dimension at most `n`. -/ lemma finrank_vector_span_range_le [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 1) : finrank k (vector_span k (set.range p)) ≤ n := begin rw [←set.image_univ, ←finset.coe_univ, ← finset.coe_image], rw ←finset.card_univ at hc, exact finrank_vector_span_image_finset_le _ _ _ hc end /-- `n + 1` points are affinely independent if and only if their `vector_span` has dimension `n`. -/ lemma affine_independent_iff_finrank_vector_span_eq [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 1) : affine_independent k p ↔ finrank k (vector_span k (set.range p)) = n := begin have hn : nonempty ι, by simp [←fintype.card_pos_iff, hc], cases hn with i₁, rw [affine_independent_iff_linear_independent_vsub _ _ i₁, linear_independent_iff_card_eq_finrank_span, eq_comm, vector_span_range_eq_span_range_vsub_right_ne k p i₁], congr', rw ←finset.card_univ at hc, rw fintype.subtype_card, simp [finset.filter_ne', finset.card_erase_of_mem, hc] end /-- `n + 1` points are affinely independent if and only if their `vector_span` has dimension at least `n`. -/ lemma affine_independent_iff_le_finrank_vector_span [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 1) : affine_independent k p ↔ n ≤ finrank k (vector_span k (set.range p)) := begin rw affine_independent_iff_finrank_vector_span_eq k p hc, split, { rintro rfl, refl }, { exact λ hle, le_antisymm (finrank_vector_span_range_le k p hc) hle } end /-- `n + 2` points are affinely independent if and only if their `vector_span` does not have dimension at most `n`. -/ lemma affine_independent_iff_not_finrank_vector_span_le [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 2) : affine_independent k p ↔ ¬ finrank k (vector_span k (set.range p)) ≤ n := by rw [affine_independent_iff_le_finrank_vector_span k p hc, ←nat.lt_iff_add_one_le, lt_iff_not_ge] /-- `n + 2` points have a `vector_span` with dimension at most `n` if and only if they are not affinely independent. -/ lemma finrank_vector_span_le_iff_not_affine_independent [fintype ι] (p : ι → P) {n : ℕ} (hc : fintype.card ι = n + 2) : finrank k (vector_span k (set.range p)) ≤ n ↔ ¬ affine_independent k p := (not_iff_comm.1 (affine_independent_iff_not_finrank_vector_span_le k p hc).symm).symm /-- A set of points is collinear if their `vector_span` has dimension at most `1`. -/ def collinear (s : set P) : Prop := module.rank k (vector_span k s) ≤ 1 /-- The definition of `collinear`. -/ lemma collinear_iff_dim_le_one (s : set P) : collinear k s ↔ module.rank k (vector_span k s) ≤ 1 := iff.rfl /-- A set of points, whose `vector_span` is finite-dimensional, is collinear if and only if their `vector_span` has dimension at most `1`. -/ lemma collinear_iff_finrank_le_one (s : set P) [finite_dimensional k (vector_span k s)] : collinear k s ↔ finrank k (vector_span k s) ≤ 1 := begin have h := collinear_iff_dim_le_one k s, rw ←finrank_eq_dim at h, exact_mod_cast h end variables (P) /-- The empty set is collinear. -/ lemma collinear_empty : collinear k (∅ : set P) := begin rw [collinear_iff_dim_le_one, vector_span_empty], simp end variables {P} /-- A single point is collinear. -/ lemma collinear_singleton (p : P) : collinear k ({p} : set P) := begin rw [collinear_iff_dim_le_one, vector_span_singleton], simp end /-- Given a point `p₀` in a set of points, that set is collinear if and only if the points can all be expressed as multiples of the same vector, added to `p₀`. -/ lemma collinear_iff_of_mem {s : set P} {p₀ : P} (h : p₀ ∈ s) : collinear k s ↔ ∃ v : V, ∀ p ∈ s, ∃ r : k, p = r • v +ᵥ p₀ := begin simp_rw [collinear_iff_dim_le_one, dim_submodule_le_one_iff', submodule.le_span_singleton_iff], split, { rintro ⟨v₀, hv⟩, use v₀, intros p hp, obtain ⟨r, hr⟩ := hv (p -ᵥ p₀) (vsub_mem_vector_span k hp h), use r, rw eq_vadd_iff_vsub_eq, exact hr.symm }, { rintro ⟨v, hp₀v⟩, use v, intros w hw, have hs : vector_span k s ≤ k ∙ v, { rw [vector_span_eq_span_vsub_set_right k h, submodule.span_le, set.subset_def], intros x hx, rw [set_like.mem_coe, submodule.mem_span_singleton], rw set.mem_image at hx, rcases hx with ⟨p, hp, rfl⟩, rcases hp₀v p hp with ⟨r, rfl⟩, use r, simp }, have hw' := set_like.le_def.1 hs hw, rwa submodule.mem_span_singleton at hw' } end /-- A set of points is collinear if and only if they can all be expressed as multiples of the same vector, added to the same base point. -/ lemma collinear_iff_exists_forall_eq_smul_vadd (s : set P) : collinear k s ↔ ∃ (p₀ : P) (v : V), ∀ p ∈ s, ∃ r : k, p = r • v +ᵥ p₀ := begin rcases set.eq_empty_or_nonempty s with rfl | ⟨⟨p₁, hp₁⟩⟩, { simp [collinear_empty] }, { rw collinear_iff_of_mem k hp₁, split, { exact λ h, ⟨p₁, h⟩ }, { rintros ⟨p, v, hv⟩, use v, intros p₂ hp₂, rcases hv p₂ hp₂ with ⟨r, rfl⟩, rcases hv p₁ hp₁ with ⟨r₁, rfl⟩, use r - r₁, simp [vadd_vadd, ←add_smul] } } end /-- Two points are collinear. -/ lemma collinear_insert_singleton (p₁ p₂ : P) : collinear k ({p₁, p₂} : set P) := begin rw collinear_iff_exists_forall_eq_smul_vadd, use [p₁, p₂ -ᵥ p₁], intros p hp, rw [set.mem_insert_iff, set.mem_singleton_iff] at hp, cases hp, { use 0, simp [hp] }, { use 1, simp [hp] } end /-- Three points are affinely independent if and only if they are not collinear. -/ lemma affine_independent_iff_not_collinear (p : fin 3 → P) : affine_independent k p ↔ ¬ collinear k (set.range p) := by rw [collinear_iff_finrank_le_one, affine_independent_iff_not_finrank_vector_span_le k p (fintype.card_fin 3)] /-- Three points are collinear if and only if they are not affinely independent. -/ lemma collinear_iff_not_affine_independent (p : fin 3 → P) : collinear k (set.range p) ↔ ¬ affine_independent k p := by rw [collinear_iff_finrank_le_one, finrank_vector_span_le_iff_not_affine_independent k p (fintype.card_fin 3)] end affine_space'
b1d1486591c6f762871a7a933533bc1b91db5c0f
5756a081670ba9c1d1d3fca7bd47cb4e31beae66
/Mathport/Syntax/Transform/Declaration.lean
e4ab033c26e1aa28a6c8a5d4daeec8d059ac3b85
[ "Apache-2.0" ]
permissive
leanprover-community/mathport
2c9bdc8292168febf59799efdc5451dbf0450d4a
13051f68064f7638970d39a8fecaede68ffbf9e1
refs/heads/master
1,693,841,364,079
1,693,813,111,000
1,693,813,111,000
379,357,010
27
10
Apache-2.0
1,691,309,132,000
1,624,384,521,000
Lean
UTF-8
Lean
false
false
1,758
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import Mathport.Syntax.Transform.Basic mathport_rules | `($mods:declModifiers $attr:attrKind instance $[$prio:namedPrio]? $[$id:declId]? $sig:declSig := { $[$fieldName:ident := $fieldVal:term],* }) => `($mods:declModifiers $attr:attrKind instance $[$prio:namedPrio]? $[$id:declId]? $sig:declSig where $[$fieldName:ident := $fieldVal:term]*) | `($mods:declModifiers def $id:declId $sig:optDeclSig := { $[$fieldName:ident := $fieldVal:term],* }) => `($mods:declModifiers def $id:declId $sig:optDeclSig where $[$fieldName:ident := $fieldVal:term]*) open Lean Parser.Term Mathport.Transform in mathport_rules | `(letDecl| $id:ident $xs* := fun $ys* => $val:term) => do -- well this is surprisingly annoying let ofTerm | `($v:ident) => pure (v : TSyntax [`ident, ``hole]) | `(_) => `(hole| _) | _ => throwUnsupported let ys ← ys.mapM fun | `(funBinder| ⦃$vs* : $ty⦄) => `(letIdBinder| ⦃$vs* : $ty⦄) | `(funBinder| {$vs*}) => `(letIdBinder| {$vs*}) | `(funBinder| {$vs* : $ty}) => `(letIdBinder| {$vs* : $ty}) | `(funBinder| [$[$v :]? $ty]) => `(letIdBinder| [$[$v :]? $ty]) | `(funBinder| $x:ident) => `(letIdBinder| $x:ident) | `(funBinder| ($v1 $vs* : $(ty)?)) => do let v1' ← ofTerm v1 `(letIdBinder| ($v1' $(← vs.mapM ofTerm)* $[: $ty]?)) | `(funBinder| ($v1 $vs*)) => do let v1' ← ofTerm v1 `(letIdBinder| ($v1' $(← vs.mapM ofTerm)*)) | `(funBinder| _) => `(letIdBinder| _) | _ => throwUnsupported `(letDecl| $id:ident $(xs ++ ys)* := $val:term)
049fa87e6236c415cc884e8fd4059b33636fc8d7
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/data/dfinsupp/interval.lean
6f27107710034d57c9b1a81763b8fb4ad1775ac2
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
6,691
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import data.finset.locally_finite import data.finset.pointwise import data.fintype.big_operators import data.dfinsupp.order /-! # Finite intervals of finitely supported functions This file provides the `locally_finite_order` instance for `Π₀ i, α i` when `α` itself is locally finite and calculates the cardinality of its finite intervals. -/ open dfinsupp finset open_locale big_operators pointwise variables {ι : Type*} {α : ι → Type*} namespace finset variables [decidable_eq ι] [Π i, has_zero (α i)] {s : finset ι} {f : Π₀ i, α i} {t : Π i, finset (α i)} /-- Finitely supported product of finsets. -/ def dfinsupp (s : finset ι) (t : Π i, finset (α i)) : finset (Π₀ i, α i) := (s.pi t).map ⟨λ f, dfinsupp.mk s $ λ i, f i i.2, begin refine (mk_injective _).comp (λ f g h, _), ext i hi, convert congr_fun h ⟨i, hi⟩, end⟩ @[simp] lemma card_dfinsupp (s : finset ι) (t : Π i, finset (α i)) : (s.dfinsupp t).card = ∏ i in s, (t i).card := (card_map _).trans $ card_pi _ _ variables [Π i, decidable_eq (α i)] lemma mem_dfinsupp_iff : f ∈ s.dfinsupp t ↔ f.support ⊆ s ∧ ∀ i ∈ s, f i ∈ t i := begin refine mem_map.trans ⟨_, _⟩, { rintro ⟨f, hf, rfl⟩, refine ⟨support_mk_subset, λ i hi, _⟩, convert mem_pi.1 hf i hi, exact mk_of_mem hi }, { refine λ h, ⟨λ i _, f i, mem_pi.2 h.2, _⟩, ext i, dsimp, exact ite_eq_left_iff.2 (λ hi, (not_mem_support_iff.1 $ λ H, hi $ h.1 H).symm) } end /-- When `t` is supported on `s`, `f ∈ s.dfinsupp t` precisely means that `f` is pointwise in `t`. -/ @[simp] lemma mem_dfinsupp_iff_of_support_subset {t : Π₀ i, finset (α i)} (ht : t.support ⊆ s) : f ∈ s.dfinsupp t ↔ ∀ i, f i ∈ t i := begin refine mem_dfinsupp_iff.trans (forall_and_distrib.symm.trans $ forall_congr $ λ i, ⟨λ h, _, λ h, ⟨λ hi, ht $ mem_support_iff.2 $ λ H, mem_support_iff.1 hi _, λ _, h⟩⟩), { by_cases hi : i ∈ s, { exact h.2 hi }, { rw [not_mem_support_iff.1 (mt h.1 hi), not_mem_support_iff.1 (not_mem_mono ht hi)], exact zero_mem_zero } }, { rwa [H, mem_zero] at h } end end finset open finset namespace dfinsupp section bundled_singleton variables [Π i, has_zero (α i)] {f : Π₀ i, α i} {i : ι} {a : α i} /-- Pointwise `finset.singleton` bundled as a `dfinsupp`. -/ def singleton (f : Π₀ i, α i) : Π₀ i, finset (α i) := { to_fun := λ i, {f i}, support' := f.support'.map $ λ s, ⟨s, λ i, (s.prop i).imp id (congr_arg _) ⟩ } lemma mem_singleton_apply_iff : a ∈ f.singleton i ↔ a = f i := mem_singleton end bundled_singleton section bundled_Icc variables [Π i, has_zero (α i)] [Π i, partial_order (α i)] [Π i, locally_finite_order (α i)] {f g : Π₀ i, α i} {i : ι} {a : α i} /-- Pointwise `finset.Icc` bundled as a `dfinsupp`. -/ def range_Icc (f g : Π₀ i, α i) : Π₀ i, finset (α i) := { to_fun := λ i, Icc (f i) (g i), support' := f.support'.bind $ λ fs, g.support'.map $ λ gs, ⟨fs + gs, λ i, or_iff_not_imp_left.2 $ λ h, begin have hf : f i = 0 := (fs.prop i).resolve_left (multiset.not_mem_mono (multiset.le.subset $ multiset.le_add_right _ _) h), have hg : g i = 0 := (gs.prop i).resolve_left (multiset.not_mem_mono (multiset.le.subset $ multiset.le_add_left _ _) h), rw [hf, hg], exact Icc_self _, end⟩ } @[simp] lemma range_Icc_apply (f g : Π₀ i, α i) (i : ι) : f.range_Icc g i = Icc (f i) (g i) := rfl lemma mem_range_Icc_apply_iff : a ∈ f.range_Icc g i ↔ f i ≤ a ∧ a ≤ g i := mem_Icc lemma support_range_Icc_subset [decidable_eq ι] [Π i, decidable_eq (α i)] : (f.range_Icc g).support ⊆ f.support ∪ g.support := begin refine λ x hx, _, by_contra, refine not_mem_support_iff.2 _ hx, rw [range_Icc_apply, not_mem_support_iff.1 (not_mem_mono (subset_union_left _ _) h), not_mem_support_iff.1 (not_mem_mono (subset_union_right _ _) h)], exact Icc_self _, end end bundled_Icc section pi variables [Π i, has_zero (α i)] [decidable_eq ι] [Π i, decidable_eq (α i)] /-- Given a finitely supported function `f : Π₀ i, finset (α i)`, one can define the finset `f.pi` of all finitely supported functions whose value at `i` is in `f i` for all `i`. -/ def pi (f : Π₀ i, finset (α i)) : finset (Π₀ i, α i) := f.support.dfinsupp f @[simp] lemma mem_pi {f : Π₀ i, finset (α i)} {g : Π₀ i, α i} : g ∈ f.pi ↔ ∀ i, g i ∈ f i := mem_dfinsupp_iff_of_support_subset $ subset.refl _ @[simp] lemma card_pi (f : Π₀ i, finset (α i)) : f.pi.card = f.prod (λ i, (f i).card) := begin rw [pi, card_dfinsupp], exact finset.prod_congr rfl (λ i _, by simp only [pi.nat_apply, nat.cast_id]), end end pi section locally_finite variables [decidable_eq ι] [Π i, decidable_eq (α i)] variables [Π i, partial_order (α i)] [Π i, has_zero (α i)] [Π i, locally_finite_order (α i)] instance : locally_finite_order (Π₀ i, α i) := locally_finite_order.of_Icc (Π₀ i, α i) (λ f g, (f.support ∪ g.support).dfinsupp $ f.range_Icc g) (λ f g x, begin refine (mem_dfinsupp_iff_of_support_subset $ support_range_Icc_subset).trans _, simp_rw [mem_range_Icc_apply_iff, forall_and_distrib], refl, end) variables (f g : Π₀ i, α i) lemma Icc_eq : Icc f g = (f.support ∪ g.support).dfinsupp (f.range_Icc g) := rfl lemma card_Icc : (Icc f g).card = ∏ i in f.support ∪ g.support, (Icc (f i) (g i)).card := card_dfinsupp _ _ lemma card_Ico : (Ico f g).card = ∏ i in f.support ∪ g.support, (Icc (f i) (g i)).card - 1 := by rw [card_Ico_eq_card_Icc_sub_one, card_Icc] lemma card_Ioc : (Ioc f g).card = ∏ i in f.support ∪ g.support, (Icc (f i) (g i)).card - 1 := by rw [card_Ioc_eq_card_Icc_sub_one, card_Icc] lemma card_Ioo : (Ioo f g).card = ∏ i in f.support ∪ g.support, (Icc (f i) (g i)).card - 2 := by rw [card_Ioo_eq_card_Icc_sub_two, card_Icc] end locally_finite section canonically_ordered variables [decidable_eq ι] [Π i, decidable_eq (α i)] variables [Π i, canonically_ordered_add_monoid (α i)] [Π i, locally_finite_order (α i)] variables (f : Π₀ i, α i) lemma card_Iic : (Iic f).card = ∏ i in f.support, (Iic (f i)).card := by simp_rw [Iic_eq_Icc, card_Icc, dfinsupp.bot_eq_zero, support_zero, empty_union, zero_apply, bot_eq_zero] lemma card_Iio : (Iio f).card = ∏ i in f.support, (Iic (f i)).card - 1 := by rw [card_Iio_eq_card_Iic_sub_one, card_Iic] end canonically_ordered end dfinsupp
76516680925d1deb81e0a246c0f5ead16b34c9c5
406917967313cd8926a5a79666c51fad41d8670f
/lakefile.lean
4959364968085283d17179a3b47330786162ec2a
[ "MIT" ]
permissive
hargoniX/lean4-statvfs
a3c15f51e2714f9fd5a77725fc618831838b46ae
458370ad201d2c2820189a474fd8701ff7a940c5
refs/heads/main
1,693,550,798,436
1,634,894,543,000
1,634,894,543,000
417,835,011
1
0
null
null
null
null
UTF-8
Lean
false
false
549
lean
import Lake open System Lake DSL def cDir : FilePath := "c" def ffiSrc := cDir / "ffi.c" def buildDir := defaultBuildDir def ffiOTarget (pkgDir : FilePath) : FileTarget := oFileTarget (pkgDir / "build/c/ffi.o") (pkgDir / "c/ffi.c" : FilePath) #["-g"] "leanc" def cLibTarget (pkgDir : FilePath) : FileTarget := staticLibTarget (pkgDir / "build/c/ffi.a") #[ffiOTarget pkgDir] package statvfs (pkgDir) (args) { -- specify the lib as an additional target srcDir := "lib" moreLibTargets := #[cLibTarget pkgDir] moreLeancArgs := #["-g"] }
6aabc4fdcd8c26b73ff347c00a3df2425daaa16f
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/analysis/convex/specific_functions.lean
6dab62a6d733959b299ffe7ec6059af44b4d0e1a
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
4,232
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 analysis.calculus.mean_value data.nat.parity analysis.complex.exponential /-! # Collection of convex functions In this file we prove that the following functions are convex: * `convex_on_exp` : the exponential function is convex on $(-∞, +∞)$; * `convex_on_pow_of_even` : given an even natural number $n$, the function $f(x)=x^n$ is convex on $(-∞, +∞)$; * `convex_on_pow` : for a natural $n$, the function $f(x)=x^n$ is convex on $[0, +∞)$; * `convex_on_fpow` : for an integer $m$, the function $f(x)=x^m$ is convex on $(0, +∞)$. ## TODO * `convex_on_rpow : ∀ r : ℝ, (r ≤ 0 ∨ 1 ≤ r) → convex_on (Ioi 0) (λ x, x ^ r)` -/ open real set /-- `exp` is convex on the whole real line -/ lemma convex_on_exp : convex_on univ exp := convex_on_univ_of_deriv2_nonneg differentiable_exp (deriv_exp.symm ▸ differentiable_exp) (assume x, (iter_deriv_exp 2).symm ▸ le_of_lt (exp_pos x)) /-- `x^n`, `n : ℕ` is convex on the whole real line whenever `n` is even -/ lemma convex_on_pow_of_even {n : ℕ} (hn : n.even) : convex_on set.univ (λ x, x^n) := begin apply convex_on_univ_of_deriv2_nonneg differentiable_pow, { simp only [deriv_pow', differentiable.mul, differentiable_const, differentiable_pow] }, { intro x, rcases hn.sub (nat.even_bit0 1) with ⟨k, hk⟩, simp only [iter_deriv_pow, finset.prod_range_succ, finset.prod_range_zero, nat.sub_zero, mul_one, hk, pow_mul', pow_two], exact mul_nonneg (nat.cast_nonneg _) (mul_self_nonneg _) } end /-- `x^n`, `n : ℕ` is convex on `[0, +∞)` for all `n` -/ lemma convex_on_pow (n : ℕ) : convex_on (Ici 0) (λ x, x^n) := begin apply convex_on_of_deriv2_nonneg (convex_Ici _) (continuous_pow n).continuous_on; simp only [interior_Ici, differentiable_on_pow, deriv_pow', differentiable_on_const, differentiable_on.mul, iter_deriv_pow], intros x hx, exact mul_nonneg (nat.cast_nonneg _) (pow_nonneg (le_of_lt hx) _) end lemma finset.prod_nonneg_of_card_nonpos_even {α β : Type*} [decidable_eq α] [linear_ordered_comm_ring β] {f : α → β} [decidable_pred (λ x, f x ≤ 0)] {s : finset α} (h0 : (s.filter (λ x, f x ≤ 0)).card.even) : 0 ≤ s.prod f := calc 0 ≤ s.prod (λ x, (if f x ≤ 0 then (-1:β) else 1) * f x) : finset.prod_nonneg (λ x _, by { split_ifs with hx hx, by simp [hx], simp at hx ⊢, exact le_of_lt hx }) ... = _ : by rw [finset.prod_mul_distrib, finset.prod_ite, finset.prod_const_one, mul_one, finset.prod_const, neg_one_pow_eq_pow_mod_two, nat.even_iff.1 h0, pow_zero, one_mul] lemma int_prod_range_nonneg (m : ℤ) (n : ℕ) (hn : n.even) : 0 ≤ (finset.range n).prod (λ k, m - k) := begin cases (le_or_lt ↑n m) with hnm hmn, { exact finset.prod_nonneg (λ k hk, sub_nonneg.2 (le_trans (int.coe_nat_le.2 $ le_of_lt $ finset.mem_range.1 hk) hnm)) }, cases le_or_lt 0 m with hm hm, { lift m to ℕ using hm, exact le_of_eq (eq.symm $ finset.prod_eq_zero (finset.mem_range.2 $ int.coe_nat_lt.1 hmn) (sub_self _)) }, clear hmn, apply finset.prod_nonneg_of_card_nonpos_even, convert hn, convert finset.card_range n, ext k, simp only [finset.mem_filter, finset.mem_range], refine ⟨and.left, λ hk, ⟨hk, sub_nonpos.2 $ le_trans (le_of_lt hm) _⟩⟩, exact int.coe_nat_nonneg k end /-- `x^m`, `m : ℤ` is convex on `(0, +∞)` for all `m` -/ lemma convex_on_fpow (m : ℤ) : convex_on (Ioi 0) (λ x, x^m) := begin apply convex_on_of_deriv2_nonneg (convex_Ioi 0); try { rw [interior_Ioi] }, { exact (differentiable_on_fpow $ lt_irrefl _).continuous_on }, { exact differentiable_on_fpow (lt_irrefl _) }, { have : eq_on (deriv (λx:ℝ, x^m)) (λx, ↑m * x^(m-1)) (Ioi 0), from λ x hx, deriv_fpow (ne_of_gt hx), refine (differentiable_on_congr this).2 _, exact (differentiable_on_fpow (lt_irrefl _)).const_mul _ }, { intros x hx, simp only [iter_deriv_fpow (ne_of_gt hx)], refine mul_nonneg (int.cast_nonneg.2 _) (fpow_nonneg_of_nonneg (le_of_lt hx) _), exact int_prod_range_nonneg _ _ (nat.even_bit0 1) } end
536256b82fd6b498862a05a43ac2861e155b70dd
5749d8999a76f3a8fddceca1f6941981e33aaa96
/src/data/indicator_function.lean
01f2707557644595629480987e900ce5d9681c57
[ "Apache-2.0" ]
permissive
jdsalchow/mathlib
13ab43ef0d0515a17e550b16d09bd14b76125276
497e692b946d93906900bb33a51fd243e7649406
refs/heads/master
1,585,819,143,348
1,580,072,892,000
1,580,072,892,000
154,287,128
0
0
Apache-2.0
1,540,281,610,000
1,540,281,609,000
null
UTF-8
Lean
false
false
6,409
lean
/- Copyright (c) 2020 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import group_theory.group_action algebra.pi_instances data.set.disjointed /-! # Indicator function `indicator (s : set α) (f : α → β) (a : α)` is `f a` if `a ∈ s` and is `0` otherwise. ## Implementation note In mathematics, an indicator function or a characteristic function is a function used to indicate membership of an element in a set `s`, having the value `1` for all elements of `s` and the value `0` otherwise. But since it is usually used to restrict a function to a certain set `s`, we let the indicator function take the value `f x` for some function `f`, instead of `1`. If the usual indicator function is needed, just set `f` to be the constant function `λx, 1`. ## Tags indicator, characteristic -/ noncomputable theory open_locale classical namespace set universes u v variables {α : Type u} {β : Type v} section has_zero variables [has_zero β] {s t : set α} {f g : α → β} {a : α} /-- `indicator s f a` is `f a` if `a ∈ s`, `0` otherwise. -/ @[reducible] def indicator (s : set α) (f : α → β) : α → β := λ x, if x ∈ s then f x else 0 @[simp] lemma indicator_of_mem (h : a ∈ s) (f : α → β) : indicator s f a = f a := if_pos h @[simp] lemma indicator_of_not_mem (h : a ∉ s) (f : α → β) : indicator s f a = 0 := if_neg h lemma indicator_congr (h : ∀ a ∈ s, f a = g a) : indicator s f = indicator s g := funext $ λx, by { simp only [indicator], split_ifs, { exact h _ h_1 }, refl } @[simp] lemma indicator_univ (f : α → β) : indicator (univ : set α) f = f := funext $ λx, indicator_of_mem (mem_univ _) f @[simp] lemma indicator_empty (f : α → β) : indicator (∅ : set α) f = λa, 0 := funext $ λx, indicator_of_not_mem (not_mem_empty _) f variable (β) @[simp] lemma indicator_zero (s : set α) : indicator s (λx, (0:β)) = λx, (0:β) := funext $ λx, by { simp only [indicator], split_ifs, refl, refl } variable {β} lemma indicator_indicator (s t : set α) (f : α → β) : indicator s (indicator t f) = indicator (s ∩ t) f := funext $ λx, by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} } lemma indicator_preimage (s : set α) (f : α → β) (B : set β) : (indicator s f)⁻¹' B = s ∩ f ⁻¹' B ∪ (-s) ∩ (λa:α, (0:β)) ⁻¹' B := by { rw [indicator, if_preimage] } end has_zero section add_monoid variables [add_monoid β] {s t : set α} {f g : α → β} {a : α} lemma indicator_union_of_not_mem_inter (h : a ∉ s ∩ t) (f : α → β) : indicator (s ∪ t) f a = indicator s f a + indicator t f a := by { simp only [indicator], split_ifs, repeat {simp * at * {contextual := tt}} } lemma indicator_union_of_disjoint (h : disjoint s t) (f : α → β) : indicator (s ∪ t) f = λa, indicator s f a + indicator t f a := funext $ λa, indicator_union_of_not_mem_inter (by { convert not_mem_empty a, have := disjoint.eq_bot h, assumption }) _ lemma indicator_add (s : set α) (f g : α → β) : indicator s (λa, f a + g a) = λa, indicator s f a + indicator s g a := by { funext, simp only [indicator], split_ifs, { refl }, rw add_zero } variables (β) instance is_add_monoid_hom.indicator (s : set α) : is_add_monoid_hom (λf:α → β, indicator s f) := { map_add := λ _ _, indicator_add _ _ _, map_zero := indicator_zero _ _ } variables {β} {𝕜 : Type*} [monoid 𝕜] [distrib_mul_action 𝕜 β] lemma indicator_smul (s : set α) (r : 𝕜) (f : α → β) : indicator s (λ (x : α), r • f x) = λ (x : α), r • indicator s f x := by { simp only [indicator], funext, split_ifs, refl, exact (smul_zero r).symm } end add_monoid section add_group variables [add_group β] {s t : set α} {f g : α → β} {a : α} variables (β) instance is_add_group_hom.indicator (s : set α) : is_add_group_hom (λf:α → β, indicator s f) := { .. is_add_monoid_hom.indicator β s } variables {β} lemma indicator_neg (s : set α) (f : α → β) : indicator s (λa, - f a) = λa, - indicator s f a := show indicator s (- f) = - indicator s f, from is_add_group_hom.map_neg _ _ lemma indicator_sub (s : set α) (f g : α → β) : indicator s (λa, f a - g a) = λa, indicator s f a - indicator s g a := show indicator s (f - g) = indicator s f - indicator s g, from is_add_group_hom.map_sub _ _ _ lemma indicator_compl (s : set α) (f : α → β) : indicator (-s) f = λ a, f a - indicator s f a := begin funext, simp only [indicator], split_ifs with h₁ h₂, { rw sub_zero }, { rw sub_self }, { rw ← mem_compl_iff at h₂, contradiction } end lemma indicator_finset_sum {β} [add_comm_monoid β] {ι : Type*} (I : finset ι) (s : set α) (f : ι → α → β) : indicator s (I.sum f) = I.sum (λ i, indicator s (f i)) := begin convert (finset.sum_hom _ _).symm, split, exact indicator_zero _ _ end lemma indicator_finset_bUnion {β} [add_comm_monoid β] {ι} (I : finset ι) (s : ι → set α) {f : α → β} : (∀ (i ∈ I) (j ∈ I), i ≠ j → s i ∩ s j = ∅) → indicator (⋃ i ∈ I, s i) f = λ a, I.sum (λ i, indicator (s i) f a) := begin refine finset.induction_on I _ _, assume h, { funext, simp }, assume a I haI ih hI, funext, simp only [haI, finset.sum_insert, not_false_iff], rw [finset.bUnion_insert, indicator_union_of_not_mem_inter, ih _], { assume i hi j hj hij, exact hI i (finset.mem_insert_of_mem hi) j (finset.mem_insert_of_mem hj) hij }, simp only [not_exists, exists_prop, mem_Union, mem_inter_eq, not_and], assume hx a' ha', have := hI a (finset.mem_insert_self _ _) a' (finset.mem_insert_of_mem ha') _, { assume h, have h := mem_inter hx h, rw this at h, exact not_mem_empty _ h }, { assume h, rw h at haI, contradiction } end end add_group section order variables [has_zero β] [preorder β] {s t : set α} {f g : α → β} {a : α} lemma indicator_le_indicator (h : f a ≤ g a) : indicator s f a ≤ indicator s g a := by { simp only [indicator], split_ifs with ha, { exact h }, refl } lemma indicator_le_indicator_of_subset (h : s ⊆ t) (hf : ∀a, 0 ≤ f a) (a : α) : indicator s f a ≤ indicator t f a := begin simp only [indicator], split_ifs with h₁, { refl }, { have := h h₁, contradiction }, { exact hf a }, { refl } end end order end set
b895faf5242de01b2e5d5879b23d8ff606b2cfec
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/data/nat/default.lean
7a67bc80c26c4e6b5b271c4b876b140602e741ca
[]
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
386
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 -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.data.nat.basic import Mathlib.Lean3Lib.init.data.nat.div import Mathlib.Lean3Lib.init.data.nat.lemmas import Mathlib.Lean3Lib.init.data.nat.bitwise namespace Mathlib
63ca3868b9baf513df85b61c5308444fff32e826
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/renameBug.lean
97d62014e9bdc05a467d3e24a8630ca1bbfe6be8
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
134
lean
theorem ex : ∀ a b c : Nat, a = b → a = c → c = b := by intros rename _ = c => h1 apply Eq.symm assumption assumption
04649561292e6f6910ccf44a604a388c117159db
f4bff2062c030df03d65e8b69c88f79b63a359d8
/kb_solns/sets_level05.lean
4519453a2e11d3f3bfd6d3aec4bc93f1795b85bf
[ "Apache-2.0" ]
permissive
adastra7470/real-number-game
776606961f52db0eb824555ed2f8e16f92216ea3
f9dcb7d9255a79b57e62038228a23346c2dc301b
refs/heads/master
1,669,221,575,893
1,594,669,800,000
1,594,669,800,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
720
lean
import game.sets.sets_level04 -- hide namespace xena -- hide variable X : Type --hide /- # Chapter 1 : Sets ## Level 5 -/ /- You should now be able to prove the theorem below if you use `split` and `cases` together with `set.subset.antisymm`. -/ /- Lemma If $A$ and $B$ are sets of any type $X$, then $$ A \subseteq B \iff A \cap B = A.$$ -/ theorem subset_iff_intersection_eq (A : set X) (B : set X) : A ⊆ B ↔ A ∩ B = A := begin split, intro H, apply set.subset.antisymm, intros x hx, cases hx with hA hB, exact hA, intros x hx, split, exact hx, exact H hx, intro H, intros x hx, have G : x ∈ A ∩ B, rw H, exact hx, cases G with hA hB, exact hB, done end end xena -- hide
cc50d97dbc116e22031b7663f849780513f4cda0
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/inverse_auto.lean
80e473e6e338b5ccc5d15fc49cfb1adfc9d86d5e
[]
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
35,525
lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth. -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.times_cont_diff import Mathlib.topology.local_homeomorph import Mathlib.topology.metric_space.contracting import Mathlib.PostPort universes u_1 u_2 u_3 u_6 u_7 u_8 namespace Mathlib /-! # Inverse function theorem In this file we prove the inverse function theorem. It says that if a map `f : E → F` has an invertible strict derivative `f'` at `a`, then it is locally invertible, and the inverse function has derivative `f' ⁻¹`. We define `has_strict_deriv_at.to_local_homeomorph` that repacks a function `f` with a `hf : has_strict_fderiv_at f f' a`, `f' : E ≃L[𝕜] F`, into a `local_homeomorph`. The `to_fun` of this `local_homeomorph` is `defeq` to `f`, so one can apply theorems about `local_homeomorph` to `hf.to_local_homeomorph f`, and get statements about `f`. Then we define `has_strict_fderiv_at.local_inverse` to be the `inv_fun` of this `local_homeomorph`, and prove two versions of the inverse function theorem: * `has_strict_fderiv_at.to_local_inverse`: if `f` has an invertible derivative `f'` at `a` in the strict sense (`hf`), then `hf.local_inverse f f' a` has derivative `f'.symm` at `f a` in the strict sense; * `has_strict_fderiv_at.to_local_left_inverse`: if `f` has an invertible derivative `f'` at `a` in the strict sense and `g` is locally left inverse to `f` near `a`, then `g` has derivative `f'.symm` at `f a` in the strict sense. In the one-dimensional case we reformulate these theorems in terms of `has_strict_deriv_at` and `f'⁻¹`. We also reformulate the theorems in terms of `times_cont_diff`, to give that `C^k` (respectively, smooth) inputs give `C^k` (smooth) inverses. These versions require that continuous differentiability implies strict differentiability; this is false over a general field, true over `ℝ` or `ℂ` and implemented here assuming `is_R_or_C 𝕂`. Some related theorems, providing the derivative and higher regularity assuming that we already know the inverse function, are formulated in `fderiv.lean`, `deriv.lean`, and `times_cont_diff.lean`. ## Notations In the section about `approximates_linear_on` we introduce some `local notation` to make formulas shorter: * by `N` we denote `∥f'⁻¹∥`; * by `g` we denote the auxiliary contracting map `x ↦ x + f'.symm (y - f x)` used to prove that `{x | f x = y}` is nonempty. ## Tags derivative, strictly differentiable, continuously differentiable, smooth, inverse function -/ /-! ### Non-linear maps approximating close to affine maps In this section we study a map `f` such that `∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥` on an open set `s`, where `f' : E ≃L[𝕜] F` is a continuous linear equivalence and `c < ∥f'⁻¹∥`. Maps of this type behave like `f a + f' (x - a)` near each `a ∈ s`. If `E` is a complete space, we prove that the image `f '' s` is open, and `f` is a homeomorphism between `s` and `f '' s`. More precisely, we define `approximates_linear_on.to_local_homeomorph` to be a `local_homeomorph` with `to_fun = f`, `source = s`, and `target = f '' s`. Maps of this type naturally appear in the proof of the inverse function theorem (see next section), and `approximates_linear_on.to_local_homeomorph` will imply that the locally inverse function exists. We define this auxiliary notion to split the proof of the inverse function theorem into small lemmas. This approach makes it possible - to prove a lower estimate on the size of the domain of the inverse function; - to reuse parts of the proofs in the case if a function is not strictly differentiable. E.g., for a function `f : E × F → G` with estimates on `f x y₁ - f x y₂` but not on `f x₁ y - f x₂ y`. -/ /-- We say that `f` approximates a continuous linear map `f'` on `s` with constant `c`, if `∥f x - f y - f' (x - y)∥ ≤ c * ∥x - y∥` whenever `x, y ∈ s`. This predicate is defined to facilitate the splitting of the inverse function theorem into small lemmas. Some of these lemmas can be useful, e.g., to prove that the inverse function is defined on a specific set. -/ def approximates_linear_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : E → F) (f' : continuous_linear_map 𝕜 E F) (s : set E) (c : nnreal) := ∀ (x : E), x ∈ s → ∀ (y : E), y ∈ s → norm (f x - f y - coe_fn f' (x - y)) ≤ ↑c * norm (x - y) namespace approximates_linear_on /-! First we prove some properties of a function that `approximates_linear_on` a (not necessarily invertible) continuous linear map. -/ theorem mono_num {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} {c' : nnreal} (hc : c ≤ c') (hf : approximates_linear_on f f' s c) : approximates_linear_on f f' s c' := fun (x : E) (hx : x ∈ s) (y : E) (hy : y ∈ s) => le_trans (hf x hx y hy) (mul_le_mul_of_nonneg_right hc (norm_nonneg (x - y))) theorem mono_set {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {t : set E} {c : nnreal} (hst : s ⊆ t) (hf : approximates_linear_on f f' t c) : approximates_linear_on f f' s c := fun (x : E) (hx : x ∈ s) (y : E) (hy : y ∈ s) => hf x (hst hx) y (hst hy) theorem lipschitz_sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : lipschitz_with c fun (x : ↥s) => f ↑x - coe_fn f' ↑x := sorry protected theorem lipschitz {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : lipschitz_with (nnnorm f' + c) (set.restrict f s) := sorry protected theorem continuous {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : continuous (set.restrict f s) := lipschitz_with.continuous (approximates_linear_on.lipschitz hf) protected theorem continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f f' s c) : continuous_on f s := iff.mpr continuous_on_iff_continuous_restrict (approximates_linear_on.continuous hf) /-! From now on we assume that `f` approximates an invertible continuous linear map `f : E ≃L[𝕜] F`. We also assume that either `E = {0}`, or `c < ∥f'⁻¹∥⁻¹`. We use `N` as an abbreviation for `∥f'⁻¹∥`. -/ protected theorem antilipschitz {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : antilipschitz_with (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹ - c⁻¹) (set.restrict f s) := sorry protected theorem injective {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : function.injective (set.restrict f s) := antilipschitz_with.injective (approximates_linear_on.antilipschitz hf hc) protected theorem inj_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : set.inj_on f s := iff.mpr set.inj_on_iff_injective (approximates_linear_on.injective hf hc) /-- A map approximating a linear equivalence on a set defines a local equivalence on this set. Should not be used outside of this file, because it is superseded by `to_local_homeomorph` below. This is a first step towards the inverse function. -/ def to_local_equiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : local_equiv E F := set.inj_on.to_local_equiv f s (approximates_linear_on.inj_on hf hc) /-- The inverse function is continuous on `f '' s`. Use properties of `local_homeomorph` instead. -/ theorem inverse_continuous_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) : continuous_on (⇑(local_equiv.symm (to_local_equiv hf hc))) (f '' s) := sorry /-! Now we prove that `f '' s` is an open set. This follows from the fact that the restriction of `f` on `s` is an open map. More precisely, we show that the image of a closed ball $$\bar B(a, ε) ⊆ s$$ under `f` includes the closed ball $$\bar B\left(f(a), \frac{ε}{∥{f'}⁻¹∥⁻¹-c}\right)$$. In order to do this, we introduce an auxiliary map $$g_y(x) = x + {f'}⁻¹ (y - f x)$$. Provided that $$∥y - f a∥ ≤ \frac{ε}{∥{f'}⁻¹∥⁻¹-c}$$, we prove that $$g_y$$ contracts in $$\bar B(a, ε)$$ and `f` sends the fixed point of $$g_y$$ to `y`. -/ /-- Iterations of this map converge to `f⁻¹ y`. The formula is very similar to the one used in Newton's method, but we use the same `f'.symm` for all `y` instead of evaluating the derivative at each point along the orbit. -/ def inverse_approx_map {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] (f : E → F) (f' : continuous_linear_equiv 𝕜 E F) (y : F) (x : E) : E := x + coe_fn (continuous_linear_equiv.symm f') (y - f x) theorem inverse_approx_map_sub {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) (x : E) (x' : E) : inverse_approx_map f f' y x - inverse_approx_map f f' y x' = x - x' - coe_fn (continuous_linear_equiv.symm f') (f x - f x') := sorry theorem inverse_approx_map_dist_self {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) (x : E) : dist (inverse_approx_map f f' y x) x = dist (coe_fn (continuous_linear_equiv.symm f') (f x)) (coe_fn (continuous_linear_equiv.symm f') y) := sorry theorem inverse_approx_map_dist_self_le {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) (x : E) : dist (inverse_approx_map f f' y x) x ≤ ↑(nnnorm ↑(continuous_linear_equiv.symm f')) * dist (f x) y := sorry theorem inverse_approx_map_fixed_iff {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} (y : F) {x : E} : inverse_approx_map f f' y x = x ↔ f x = y := sorry theorem inverse_approx_map_contracts_on {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (y : F) (hf : approximates_linear_on f (↑f') s c) {x : E} {x' : E} (hx : x ∈ s) (hx' : x' ∈ s) : dist (inverse_approx_map f f' y x) (inverse_approx_map f f' y x') ≤ ↑(nnnorm ↑(continuous_linear_equiv.symm f')) * ↑c * dist x x' := sorry theorem inverse_approx_map_maps_to {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} {y : F} {ε : ℝ} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) {b : E} (hb : b ∈ s) (hε : metric.closed_ball b ε ⊆ s) (hy : y ∈ metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε)) : set.maps_to (inverse_approx_map f f' y) (metric.closed_ball b ε) (metric.closed_ball b ε) := sorry theorem surj_on_closed_ball {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} {ε : ℝ} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) {b : E} (ε0 : 0 ≤ ε) (hε : metric.closed_ball b ε ⊆ s) : set.surj_on f (metric.closed_ball b ε) (metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε)) := sorry /-- Given a function `f` that approximates a linear equivalence on an open set `s`, returns a local homeomorph with `to_fun = f` and `source = s`. -/ def to_local_homeomorph {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] (f : E → F) {f' : continuous_linear_equiv 𝕜 E F} (s : set E) {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : local_homeomorph E F := local_homeomorph.mk (to_local_equiv hf hc) hs sorry sorry (inverse_continuous_on hf hc) @[simp] theorem to_local_homeomorph_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : ⇑(to_local_homeomorph f s hf hc hs) = f := rfl @[simp] theorem to_local_homeomorph_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph f s hf hc hs)) = s := rfl @[simp] theorem to_local_homeomorph_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) : local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f s hf hc hs)) = f '' s := rfl theorem closed_ball_subset_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {s : set E} {c : nnreal} {ε : ℝ} (hf : approximates_linear_on f (↑f') s c) (hc : subsingleton E ∨ c < (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹)) (hs : is_open s) {b : E} (ε0 : 0 ≤ ε) (hε : metric.closed_ball b ε ⊆ s) : metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε) ⊆ local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f s hf hc hs)) := set.surj_on.mono hε (set.subset.refl (metric.closed_ball (f b) ((↑(nnnorm ↑(continuous_linear_equiv.symm f'))⁻¹ - ↑c) * ε))) (surj_on_closed_ball hf hc ε0 hε) end approximates_linear_on /-! ### Inverse function theorem Now we prove the inverse function theorem. Let `f : E → F` be a map defined on a complete vector space `E`. Assume that `f` has an invertible derivative `f' : E ≃L[𝕜] F` at `a : E` in the strict sense. Then `f` approximates `f'` in the sense of `approximates_linear_on` on an open neighborhood of `a`, and we can apply `approximates_linear_on.to_local_homeomorph` to construct the inverse function. -/ namespace has_strict_fderiv_at /-- If `f` has derivative `f'` at `a` in the strict sense and `c > 0`, then `f` approximates `f'` with constant `c` on some neighborhood of `a`. -/ theorem approximates_deriv_on_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_map 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f f' a) {c : nnreal} (hc : subsingleton E ∨ 0 < c) : ∃ (s : set E), ∃ (H : s ∈ nhds a), approximates_linear_on f f' s c := sorry theorem approximates_deriv_on_open_nhds {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : ∃ (s : set E), ∃ (hs : a ∈ s ∧ is_open s), approximates_linear_on f (↑f') s (nnnorm ↑(continuous_linear_equiv.symm f')⁻¹ / bit0 1) := sorry /-- Given a function with an invertible strict derivative at `a`, returns a `local_homeomorph` with `to_fun = f` and `a ∈ source`. This is a part of the inverse function theorem. The other part `has_strict_fderiv_at.to_local_inverse` states that the inverse function of this `local_homeomorph` has derivative `f'.symm`. -/ def to_local_homeomorph {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] (f : E → F) {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : local_homeomorph E F := approximates_linear_on.to_local_homeomorph f (classical.some (approximates_deriv_on_open_nhds hf)) sorry sorry sorry @[simp] theorem to_local_homeomorph_coe {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : ⇑(to_local_homeomorph f hf) = f := rfl theorem mem_to_local_homeomorph_source {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : a ∈ local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph f hf)) := and.left (Exists.fst (classical.some_spec (approximates_deriv_on_open_nhds hf))) theorem image_mem_to_local_homeomorph_target {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : f a ∈ local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f hf)) := local_homeomorph.map_source (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf) theorem map_nhds_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.map f (nhds a) = nhds (f a) := local_homeomorph.map_nhds_eq (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf) /-- Given a function `f` with an invertible derivative, returns a function that is locally inverse to `f`. -/ def local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] (f : E → F) (f' : continuous_linear_equiv 𝕜 E F) (a : E) (hf : has_strict_fderiv_at f (↑f') a) : F → E := ⇑(local_homeomorph.symm (to_local_homeomorph f hf)) theorem local_inverse_def {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : local_inverse f f' a hf = ⇑(local_homeomorph.symm (to_local_homeomorph f hf)) := rfl theorem eventually_left_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.eventually (fun (x : E) => local_inverse f f' a hf (f x) = x) (nhds a) := local_homeomorph.eventually_left_inverse (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf) @[simp] theorem local_inverse_apply_image {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : local_inverse f f' a hf (f a) = a := filter.eventually.self_of_nhds (eventually_left_inverse hf) theorem eventually_right_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.eventually (fun (y : F) => f (local_inverse f f' a hf y) = y) (nhds (f a)) := local_homeomorph.eventually_right_inverse' (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf) theorem local_inverse_continuous_at {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : continuous_at (local_inverse f f' a hf) (f a) := local_homeomorph.continuous_at_symm (to_local_homeomorph f hf) (image_mem_to_local_homeomorph_target hf) theorem local_inverse_tendsto {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : filter.tendsto (local_inverse f f' a hf) (nhds (f a)) (nhds a) := local_homeomorph.tendsto_symm (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf) theorem local_inverse_unique {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) {g : F → E} (hg : filter.eventually (fun (x : E) => g (f x) = x) (nhds a)) : filter.eventually (fun (y : F) => g y = local_inverse f f' a hf y) (nhds (f a)) := filter.eventually_eq_of_left_inv_of_right_inv hg (eventually_right_inverse hf) (local_homeomorph.tendsto_symm (to_local_homeomorph f hf) (mem_to_local_homeomorph_source hf)) /-- If `f` has an invertible derivative `f'` at `a` in the sense of strict differentiability `(hf)`, then the inverse function `hf.local_inverse f` has derivative `f'.symm` at `f a`. -/ theorem to_local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) : has_strict_fderiv_at (local_inverse f f' a hf) (↑(continuous_linear_equiv.symm f')) (f a) := sorry /-- If `f : E → F` has an invertible derivative `f'` at `a` in the sense of strict differentiability and `g (f x) = x` in a neighborhood of `a`, then `g` has derivative `f'.symm` at `f a`. For a version assuming `f (g y) = y` and continuity of `g` at `f a` but not `[complete_space E]` see `of_local_left_inverse`. -/ theorem to_local_left_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [cs : complete_space E] {f : E → F} {f' : continuous_linear_equiv 𝕜 E F} {a : E} (hf : has_strict_fderiv_at f (↑f') a) {g : F → E} (hg : filter.eventually (fun (x : E) => g (f x) = x) (nhds a)) : has_strict_fderiv_at g (↑(continuous_linear_equiv.symm f')) (f a) := congr_of_eventually_eq (to_local_inverse hf) (filter.eventually.mono (local_inverse_unique hf hg) fun (_x : F) => Eq.symm) end has_strict_fderiv_at /-- If a function has an invertible strict derivative at all points, then it is an open map. -/ theorem open_map_of_strict_fderiv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] {E : Type u_2} [normed_group E] [normed_space 𝕜 E] {F : Type u_3} [normed_group F] [normed_space 𝕜 F] [complete_space E] {f : E → F} {f' : E → continuous_linear_equiv 𝕜 E F} (hf : ∀ (x : E), has_strict_fderiv_at f (↑(f' x)) x) : is_open_map f := iff.mpr is_open_map_iff_nhds_le fun (x : E) => eq.ge (has_strict_fderiv_at.map_nhds_eq (hf x)) /-! ### Inverse function theorem, 1D case In this case we prove a version of the inverse function theorem for maps `f : 𝕜 → 𝕜`. We use `continuous_linear_equiv.units_equiv_aut` to translate `has_strict_deriv_at f f' a` and `f' ≠ 0` into `has_strict_fderiv_at f (_ : 𝕜 ≃L[𝕜] 𝕜) a`. -/ namespace has_strict_deriv_at /-- A function that is inverse to `f` near `a`. -/ def local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] (f : 𝕜 → 𝕜) (f' : 𝕜) (a : 𝕜) (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) : 𝕜 → 𝕜 := has_strict_fderiv_at.local_inverse f (coe_fn (continuous_linear_equiv.units_equiv_aut 𝕜) (units.mk0 f' hf')) a (has_strict_fderiv_at_equiv hf hf') theorem map_nhds_eq {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜} {a : 𝕜} (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) : filter.map f (nhds a) = nhds (f a) := has_strict_fderiv_at.map_nhds_eq (has_strict_fderiv_at_equiv hf hf') theorem to_local_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜} {a : 𝕜} (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) : has_strict_deriv_at (local_inverse f f' a hf hf') (f'⁻¹) (f a) := has_strict_fderiv_at.to_local_inverse (has_strict_fderiv_at_equiv hf hf') theorem to_local_left_inverse {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [cs : complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜} {a : 𝕜} (hf : has_strict_deriv_at f f' a) (hf' : f' ≠ 0) {g : 𝕜 → 𝕜} (hg : filter.eventually (fun (x : 𝕜) => g (f x) = x) (nhds a)) : has_strict_deriv_at g (f'⁻¹) (f a) := has_strict_fderiv_at.to_local_left_inverse (has_strict_fderiv_at_equiv hf hf') hg end has_strict_deriv_at /-- If a function has a non-zero strict derivative at all points, then it is an open map. -/ theorem open_map_of_strict_deriv {𝕜 : Type u_1} [nondiscrete_normed_field 𝕜] [complete_space 𝕜] {f : 𝕜 → 𝕜} {f' : 𝕜 → 𝕜} (hf : ∀ (x : 𝕜), has_strict_deriv_at f (f' x) x) (h0 : ∀ (x : 𝕜), f' x ≠ 0) : is_open_map f := iff.mpr is_open_map_iff_nhds_le fun (x : 𝕜) => eq.ge (has_strict_deriv_at.map_nhds_eq (hf x) (h0 x)) /-! ### Inverse function theorem, smooth case -/ namespace times_cont_diff_at /-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative at `a`, returns a `local_homeomorph` with `to_fun = f` and `a ∈ source`. -/ def to_local_homeomorph {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] (f : E' → F') {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : local_homeomorph E' F' := has_strict_fderiv_at.to_local_homeomorph f sorry @[simp] theorem to_local_homeomorph_coe {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : ⇑(to_local_homeomorph f hf hf' hn) = f := rfl theorem mem_to_local_homeomorph_source {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : a ∈ local_equiv.source (local_homeomorph.to_local_equiv (to_local_homeomorph f hf hf' hn)) := has_strict_fderiv_at.mem_to_local_homeomorph_source (has_strict_fderiv_at' hf hf' hn) theorem image_mem_to_local_homeomorph_target {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : f a ∈ local_equiv.target (local_homeomorph.to_local_equiv (to_local_homeomorph f hf hf' hn)) := has_strict_fderiv_at.image_mem_to_local_homeomorph_target (has_strict_fderiv_at' hf hf' hn) /-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative at `a`, returns a function that is locally inverse to `f`. -/ def local_inverse {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : F' → E' := has_strict_fderiv_at.local_inverse f f' a sorry theorem local_inverse_apply_image {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : local_inverse hf hf' hn (f a) = a := has_strict_fderiv_at.local_inverse_apply_image (has_strict_fderiv_at' hf hf' hn) /-- Given a `times_cont_diff` function over `𝕂` (which is `ℝ` or `ℂ`) with an invertible derivative at `a`, the inverse function (produced by `times_cont_diff.to_local_homeomorph`) is also `times_cont_diff`. -/ theorem to_local_inverse {𝕂 : Type u_6} [is_R_or_C 𝕂] {E' : Type u_7} [normed_group E'] [normed_space 𝕂 E'] {F' : Type u_8} [normed_group F'] [normed_space 𝕂 F'] [complete_space E'] {f : E' → F'} {f' : continuous_linear_equiv 𝕂 E' F'} {a : E'} {n : with_top ℕ} (hf : times_cont_diff_at 𝕂 n f a) (hf' : has_fderiv_at f (↑f') a) (hn : 1 ≤ n) : times_cont_diff_at 𝕂 n (local_inverse hf hf' hn) (f a) := sorry end Mathlib
c1c3bb0241605d8f90f2238cc3b66d47c67474c8
63abd62053d479eae5abf4951554e1064a4c45b4
/src/topology/algebra/group_with_zero.lean
f8226f8ee0fee6c46a15970058eeb4e12c97c08f
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
6,189
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import topology.algebra.monoid import algebra.group.pi /-! # Topological group with zero In this file we define `has_continuous_inv'` to be a mixin typeclass a type with `has_inv` and `has_zero` (e.g., a `group_with_zero`) such that `λ x, x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. Currently the only example of `has_continuous_inv'` in `mathlib` which is not a normed field is the type `nnnreal` (a.k.a. `ℝ≥0`) of nonnegative real numbers. Then we prove lemmas about continuity of `x ↦ x⁻¹` and `f / g` providing dot-style `*.inv'` and `*.div` operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. As a special case, we provide `*.div_const` operations that require only `group_with_zero` and `has_continuous_mul` instances. All lemmas about `(⁻¹)` use `inv'` in their names because lemmas without `'` are used for `topological_group`s. We also use `'` in the typeclass name `has_continuous_inv'` for the sake of consistency of notation. -/ open_locale topological_space open filter /-! ### A group with zero with continuous multiplication If `G₀` is a group with zero with continuous `(*)`, then `(/y)` is continuous for any `y`. In this section we prove lemmas that immediately follow from this fact providing `*.div_const` dot-style operations on `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. -/ variables {α G₀ : Type*} section div_const variables [group_with_zero G₀] [topological_space G₀] [has_continuous_mul G₀] {f : α → G₀} {s : set α} {l : filter α} lemma filter.tendsto.div_const {x y : G₀} (hf : tendsto f l (𝓝 x)) : tendsto (λa, f a / y) l (𝓝 (x / y)) := hf.mul tendsto_const_nhds variables [topological_space α] lemma continuous_at.div_const (hf : continuous f) {y : G₀} : continuous (λ x, f x / y) := hf.mul continuous_const lemma continuous_within_at.div_const {a} (hf : continuous_within_at f s a) {y : G₀} : continuous_within_at (λ x, f x / y) s a := hf.div_const lemma continuous_on.div_const (hf : continuous_on f s) {y : G₀} : continuous_on (λ x, f x / y) s := hf.mul continuous_on_const lemma continuous.div_const (hf : continuous f) {y : G₀} : continuous (λ x, f x / y) := hf.mul continuous_const end div_const /-- A type with `0` and `has_inv` such that `λ x, x⁻¹` is continuous at all nonzero points. Any normed (semi)field has this property. -/ class has_continuous_inv' (G₀ : Type*) [has_zero G₀] [has_inv G₀] [topological_space G₀] := (continuous_at_inv' : ∀ ⦃x : G₀⦄, x ≠ 0 → continuous_at has_inv.inv x) export has_continuous_inv' (continuous_at_inv') section inv' variables [has_zero G₀] [has_inv G₀] [topological_space G₀] [has_continuous_inv' G₀] {l : filter α} {f : α → G₀} {s : set α} {a : α} /-! ### Continuity of `λ x, x⁻¹` at a non-zero point We define `topological_group_with_zero` to be a `group_with_zero` such that the operation `x ↦ x⁻¹` is continuous at all nonzero points. In this section we prove dot-style `*.inv'` lemmas for `filter.tendsto`, `continuous_at`, `continuous_within_at`, `continuous_on`, and `continuous`. -/ lemma tendsto_inv' {x : G₀} (hx : x ≠ 0) : tendsto has_inv.inv (𝓝 x) (𝓝 x⁻¹) := continuous_at_inv' hx lemma continuous_on_inv' : continuous_on (has_inv.inv : G₀ → G₀) {0}ᶜ := λ x hx, (continuous_at_inv' hx).continuous_within_at /-- If a function converges to a nonzero value, its inverse converges to the inverse of this value. We use the name `tendsto.inv'` as `tendsto.inv` is already used in multiplicative topological groups. -/ lemma filter.tendsto.inv' {a : G₀} (hf : tendsto f l (𝓝 a)) (ha : a ≠ 0) : tendsto (λ x, (f x)⁻¹) l (𝓝 a⁻¹) := (tendsto_inv' ha).comp hf variables [topological_space α] lemma continuous_within_at.inv' (hf : continuous_within_at f s a) (ha : f a ≠ 0) : continuous_within_at (λ x, (f x)⁻¹) s a := hf.inv' ha lemma continuous_at.inv' (hf : continuous_at f a) (ha : f a ≠ 0) : continuous_at (λ x, (f x)⁻¹) a := hf.inv' ha lemma continuous.inv' (hf : continuous f) (h0 : ∀ x, f x ≠ 0) : continuous (λ x, (f x)⁻¹) := continuous_iff_continuous_at.2 $ λ x, (hf.tendsto x).inv' (h0 x) lemma continuous_on.inv' (hf : continuous_on f s) (h0 : ∀ x ∈ s, f x ≠ 0) : continuous_on (λ x, (f x)⁻¹) s := λ x hx, (hf x hx).inv' (h0 x hx) end inv' /-! ### Continuity of division If `G₀` is a `group_with_zero` with `x ↦ x⁻¹` continuous at all nonzero points and `(*)`, then division `(/)` is continuous at any point where the denominator is continuous. -/ section div variables [group_with_zero G₀] [topological_space G₀] [has_continuous_inv' G₀] [has_continuous_mul G₀] {f g : α → G₀} lemma filter.tendsto.div {l : filter α} {a b : G₀} (hf : tendsto f l (𝓝 a)) (hg : tendsto g l (𝓝 b)) (hy : b ≠ 0) : tendsto (f / g) l (𝓝 (a / b)) := hf.mul (hg.inv' hy) variables [topological_space α] {s : set α} {a : α} lemma continuous_within_at.div (hf : continuous_within_at f s a) (hg : continuous_within_at g s a) (h₀ : g a ≠ 0) : continuous_within_at (f / g) s a := hf.div hg h₀ lemma continuous_on.div (hf : continuous_on f s) (hg : continuous_on g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : continuous_on (f / g) s := λ x hx, (hf x hx).div (hg x hx) (h₀ x hx) /-- Continuity at a point of the result of dividing two functions continuous at that point, where the denominator is nonzero. -/ lemma continuous_at.div (hf : continuous_at f a) (hg : continuous_at g a) (h₀ : g a ≠ 0) : continuous_at (f / g) a := hf.div hg h₀ lemma continuous.div (hf : continuous f) (hg : continuous g) (h₀ : ∀ x, g x ≠ 0) : continuous (f / g) := hf.mul $ hg.inv' h₀ lemma continuous_on_div : continuous_on (λ p : G₀ × G₀, p.1 / p.2) {p | p.2 ≠ 0} := continuous_on_fst.div continuous_on_snd $ λ _, id end div
b3b2b3318d597863a42112229122d52ab0f38935
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/tests/lean/char_lits.lean
d2bccfcf1a40685567ece0a59cd415997cb87ff8
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
217
lean
import system.io open io #check #"a" #eval #"a" #eval #"\n" #eval #"\\" variable [io.interface] #eval put_str (list.cons #"\\" "aaa") #eval put_str [#"\n"] #eval put_str [#"\n"] #eval put_str (list.cons #"\'" "aaa")
1d95553d39f5fb00c172ae69eddd5825a2e0f615
4727251e0cd73359b15b664c3170e5d754078599
/src/measure_theory/function/jacobian.lean
9a85c2426f01f337e9a994cb1bbce11b667084af
[ "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
68,244
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 -/ import measure_theory.covering.besicovitch_vector_space import measure_theory.measure.haar_lebesgue import analysis.normed_space.pointwise import measure_theory.covering.differentiation import measure_theory.constructions.polish /-! # Change of variables in higher-dimensional integrals Let `μ` be a Lebesgue measure on a finite-dimensional real vector space `E`. Let `f : E → E` be a function which is injective and differentiable on a measurable set `s`, with derivative `f'`. Then we prove that `f '' s` is measurable, and its measure is given by the formula `μ (f '' s) = ∫⁻ x in s, |(f' x).det| ∂μ` (where `(f' x).det` is almost everywhere measurable, but not Borel-measurable in general). This formula is proved in `lintegral_abs_det_fderiv_eq_add_haar_image`. We deduce the change of variables formula for the Lebesgue and Bochner integrals, in `lintegral_image_eq_lintegral_abs_det_fderiv_mul` and `integral_image_eq_integral_abs_det_fderiv_smul` respectively. ## Main results * `add_haar_image_eq_zero_of_differentiable_on_of_add_haar_eq_zero`: if `f` is differentiable on a set `s` with zero measure, then `f '' s` also has zero measure. * `add_haar_image_eq_zero_of_det_fderiv_within_eq_zero`: if `f` is differentiable on a set `s`, and its derivative is never invertible, then `f '' s` has zero measure (a version of Sard's lemma). * `ae_measurable_fderiv_within`: if `f` is differentiable on a measurable set `s`, then `f'` is almost everywhere measurable on `s`. For the next statements, `s` is a measurable set and `f` is differentiable on `s` (with a derivative `f'`) and injective on `s`. * `measurable_image_of_fderiv_within`: the image `f '' s` is measurable. * `measurable_embedding_of_fderiv_within`: the function `s.restrict f` is a measurable embedding. * `lintegral_abs_det_fderiv_eq_add_haar_image`: the image measure is given by `μ (f '' s) = ∫⁻ x in s, |(f' x).det| ∂μ`. * `lintegral_image_eq_lintegral_abs_det_fderiv_mul`: for `g : E → ℝ≥0∞`, one has `∫⁻ x in f '' s, g x ∂μ = ∫⁻ x in s, ennreal.of_real (|(f' x).det|) * g (f x) ∂μ`. * `integral_image_eq_integral_abs_det_fderiv_smul`: for `g : E → F`, one has `∫ x in f '' s, g x ∂μ = ∫ x in s, |(f' x).det| • g (f x) ∂μ`. * `integrable_on_image_iff_integrable_on_abs_det_fderiv_smul`: for `g : E → F`, the function `g` is integrable on `f '' s` if and only if `|(f' x).det| • g (f x))` is integrable on `s`. ## Implementation Typical versions of these results in the literature have much stronger assumptions: `s` would typically be open, and the derivative `f' x` would depend continuously on `x` and be invertible everywhere, to have the local inverse theorem at our disposal. The proof strategy under our weaker assumptions is more involved. We follow [Fremlin, *Measure Theory* (volume 2)][fremlin_vol2]. The first remark is that, if `f` is sufficiently well approximated by a linear map `A` on a set `s`, then `f` expands the volume of `s` by at least `A.det - ε` and at most `A.det + ε`, where the closeness condition depends on `A` in a non-explicit way (see `add_haar_image_le_mul_of_det_lt` and `mul_le_add_haar_image_of_lt_det`). This fact holds for balls by a simple inclusion argument, and follows for general sets using the Besicovitch covering theorem to cover the set by balls with measures adding up essentially to `μ s`. When `f` is differentiable on `s`, one may partition `s` into countably many subsets `s ∩ t n` (where `t n` is measurable), on each of which `f` is well approximated by a linear map, so that the above results apply. See `exists_partition_approximates_linear_on_of_has_fderiv_within_at`, which follows from the pointwise differentiability (in a non-completely trivial way, as one should ensure a form of uniformity on the sets of the partition). Combining the above two results would give the conclusion, except for two difficulties: it is not obvious why `f '' s` and `f'` should be measurable, which prevents us from using countable additivity for the measure and the integral. It turns out that `f '' s` is indeed measurable, and that `f'` is almost everywhere measurable, which is enough to recover countable additivity. The measurability of `f '' s` follows from the deep Lusin-Souslin theorem ensuring that, in a Polish space, a continuous injective image of a measurable set is measurable. The key point to check the almost everywhere measurability of `f'` is that, if `f` is approximated up to `δ` by a linear map on a set `s`, then `f'` is within `δ` of `A` on a full measure subset of `s` (namely, its density points). With the above approximation argument, it follows that `f'` is the almost everywhere limit of a sequence of measurable functions (which are constant on the pieces of the good discretization), and is therefore almost everywhere measurable. ## Tags Change of variables in integrals ## References [Fremlin, *Measure Theory* (volume 2)][fremlin_vol2] -/ open measure_theory measure_theory.measure metric filter set finite_dimensional asymptotics topological_space open_locale nnreal ennreal topological_space pointwise variables {E F : Type*} [normed_group E] [normed_space ℝ E] [finite_dimensional ℝ E] [normed_group F] [normed_space ℝ F] {s : set E} {f : E → E} {f' : E → E →L[ℝ] E} /-! ### Decomposition lemmas We state lemmas ensuring that a differentiable function can be approximated, on countably many measurable pieces, by linear maps (with a prescribed precision depending on the linear map). -/ /-- Assume that a function `f` has a derivative at every point of a set `s`. Then one may cover `s` with countably many closed sets `t n` on which `f` is well approximated by linear maps `A n`. -/ lemma exists_closed_cover_approximates_linear_on_of_has_fderiv_within_at [second_countable_topology F] (f : E → F) (s : set E) (f' : E → E →L[ℝ] F) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (r : (E →L[ℝ] F) → ℝ≥0) (rpos : ∀ A, r A ≠ 0) : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] F)), (∀ n, is_closed (t n)) ∧ (s ⊆ ⋃ n, t n) ∧ (∀ n, approximates_linear_on f (A n) (s ∩ t n) (r (A n))) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = f' y) := begin /- Choose countably many linear maps `f' z`. For every such map, if `f` has a derivative at `x` close enough to `f' z`, then `f y - f x` is well approximated by `f' z (y - x)` for `y` close enough to `x`, say on a ball of radius `r` (or even `u n` for some `n`, where `u` is a fixed sequence tending to `0`). Let `M n z` be the points where this happens. Then this set is relatively closed inside `s`, and moreover in every closed ball of radius `u n / 3` inside it the map is well approximated by `f' z`. Using countably many closed balls to split `M n z` into small diameter subsets `K n z p`, one obtains the desired sets `t q` after reindexing. -/ -- exclude the trivial case where `s` is empty rcases eq_empty_or_nonempty s with rfl|hs, { refine ⟨λ n, ∅, λ n, 0, _, _, _, _⟩; simp }, -- we will use countably many linear maps. Select these from all the derivatives since the -- space of linear maps is second-countable obtain ⟨T, T_count, hT⟩ : ∃ T : set s, countable T ∧ (⋃ x ∈ T, ball (f' (x : E)) (r (f' x))) = ⋃ (x : s), ball (f' x) (r (f' x)) := topological_space.is_open_Union_countable _ (λ x, is_open_ball), -- fix a sequence `u` of positive reals tending to zero. obtain ⟨u, u_anti, u_pos, u_lim⟩ : ∃ (u : ℕ → ℝ), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ), -- `M n z` is the set of points `x` such that `f y - f x` is close to `f' z (y - x)` for `y` -- in the ball of radius `u n` around `x`. let M : ℕ → T → set E := λ n z, {x | x ∈ s ∧ ∀ y ∈ s ∩ ball x (u n), ∥f y - f x - f' z (y - x)∥ ≤ r (f' z) * ∥y - x∥}, -- As `f` is differentiable everywhere on `s`, the sets `M n z` cover `s` by design. have s_subset : ∀ x ∈ s, ∃ (n : ℕ) (z : T), x ∈ M n z, { assume x xs, obtain ⟨z, zT, hz⟩ : ∃ z ∈ T, f' x ∈ ball (f' (z : E)) (r (f' z)), { have : f' x ∈ ⋃ (z ∈ T), ball (f' (z : E)) (r (f' z)), { rw hT, refine mem_Union.2 ⟨⟨x, xs⟩, _⟩, simpa only [mem_ball, subtype.coe_mk, dist_self] using (rpos (f' x)).bot_lt }, rwa mem_Union₂ at this }, obtain ⟨ε, εpos, hε⟩ : ∃ (ε : ℝ), 0 < ε ∧ ∥f' x - f' z∥ + ε ≤ r (f' z), { refine ⟨r (f' z) - ∥f' x - f' z∥, _, le_of_eq (by abel)⟩, simpa only [sub_pos] using mem_ball_iff_norm.mp hz }, obtain ⟨δ, δpos, hδ⟩ : ∃ (δ : ℝ) (H : 0 < δ), ball x δ ∩ s ⊆ {y | ∥f y - f x - (f' x) (y - x)∥ ≤ ε * ∥y - x∥} := metric.mem_nhds_within_iff.1 (is_o.def (hf' x xs) εpos), obtain ⟨n, hn⟩ : ∃ n, u n < δ := ((tendsto_order.1 u_lim).2 _ δpos).exists, refine ⟨n, ⟨z, zT⟩, ⟨xs, _⟩⟩, assume y hy, calc ∥f y - f x - (f' z) (y - x)∥ = ∥(f y - f x - (f' x) (y - x)) + (f' x - f' z) (y - x)∥ : begin congr' 1, simp only [continuous_linear_map.coe_sub', map_sub, pi.sub_apply], abel, end ... ≤ ∥f y - f x - (f' x) (y - x)∥ + ∥(f' x - f' z) (y - x)∥ : norm_add_le _ _ ... ≤ ε * ∥y - x∥ + ∥f' x - f' z∥ * ∥y - x∥ : begin refine add_le_add (hδ _) (continuous_linear_map.le_op_norm _ _), rw inter_comm, exact inter_subset_inter_right _ (ball_subset_ball hn.le) hy, end ... ≤ r (f' z) * ∥y - x∥ : begin rw [← add_mul, add_comm], exact mul_le_mul_of_nonneg_right hε (norm_nonneg _), end }, -- the sets `M n z` are relatively closed in `s`, as all the conditions defining it are clearly -- closed have closure_M_subset : ∀ n z, s ∩ closure (M n z) ⊆ M n z, { rintros n z x ⟨xs, hx⟩, refine ⟨xs, λ y hy, _⟩, obtain ⟨a, aM, a_lim⟩ : ∃ (a : ℕ → E), (∀ k, a k ∈ M n z) ∧ tendsto a at_top (𝓝 x) := mem_closure_iff_seq_limit.1 hx, have L1 : tendsto (λ (k : ℕ), ∥f y - f (a k) - (f' z) (y - a k)∥) at_top (𝓝 ∥f y - f x - (f' z) (y - x)∥), { apply tendsto.norm, have L : tendsto (λ k, f (a k)) at_top (𝓝 (f x)), { apply (hf' x xs).continuous_within_at.tendsto.comp, apply tendsto_nhds_within_of_tendsto_nhds_of_eventually_within _ a_lim, exact eventually_of_forall (λ k, (aM k).1) }, apply tendsto.sub (tendsto_const_nhds.sub L), exact ((f' z).continuous.tendsto _).comp (tendsto_const_nhds.sub a_lim) }, have L2 : tendsto (λ (k : ℕ), (r (f' z) : ℝ) * ∥y - a k∥) at_top (𝓝 (r (f' z) * ∥y - x∥)) := (tendsto_const_nhds.sub a_lim).norm.const_mul _, have I : ∀ᶠ k in at_top, ∥f y - f (a k) - (f' z) (y - a k)∥ ≤ r (f' z) * ∥y - a k∥, { have L : tendsto (λ k, dist y (a k)) at_top (𝓝 (dist y x)) := tendsto_const_nhds.dist a_lim, filter_upwards [(tendsto_order.1 L).2 _ hy.2], assume k hk, exact (aM k).2 y ⟨hy.1, hk⟩ }, exact le_of_tendsto_of_tendsto L1 L2 I }, -- choose a dense sequence `d p` rcases topological_space.exists_dense_seq E with ⟨d, hd⟩, -- split `M n z` into subsets `K n z p` of small diameters by intersecting with the ball -- `closed_ball (d p) (u n / 3)`. let K : ℕ → T → ℕ → set E := λ n z p, closure (M n z) ∩ closed_ball (d p) (u n / 3), -- on the sets `K n z p`, the map `f` is well approximated by `f' z` by design. have K_approx : ∀ n (z : T) p, approximates_linear_on f (f' z) (s ∩ K n z p) (r (f' z)), { assume n z p x hx y hy, have yM : y ∈ M n z := closure_M_subset _ _ ⟨hy.1, hy.2.1⟩, refine yM.2 _ ⟨hx.1, _⟩, calc dist x y ≤ dist x (d p) + dist y (d p) : dist_triangle_right _ _ _ ... ≤ u n / 3 + u n / 3 : add_le_add hx.2.2 hy.2.2 ... < u n : by linarith [u_pos n] }, -- the sets `K n z p` are also closed, again by design. have K_closed : ∀ n (z : T) p, is_closed (K n z p) := λ n z p, is_closed_closure.inter is_closed_ball, -- reindex the sets `K n z p`, to let them only depend on an integer parameter `q`. obtain ⟨F, hF⟩ : ∃ F : ℕ → ℕ × T × ℕ, function.surjective F, { haveI : encodable T := T_count.to_encodable, haveI : nonempty T, { unfreezingI { rcases eq_empty_or_nonempty T with rfl|hT }, { rcases hs with ⟨x, xs⟩, rcases s_subset x xs with ⟨n, z, hnz⟩, exact false.elim z.2 }, { exact (nonempty_coe_sort _).2 hT } }, inhabit (ℕ × T × ℕ), exact ⟨_, encodable.surjective_decode_iget _⟩ }, -- these sets `t q = K n z p` will do refine ⟨λ q, K (F q).1 (F q).2.1 (F q).2.2, λ q, f' (F q).2.1, λ n, K_closed _ _ _, λ x xs, _, λ q, K_approx _ _ _, λ h's q, ⟨(F q).2.1, (F q).2.1.1.2, rfl⟩⟩, -- the only fact that needs further checking is that they cover `s`. -- we already know that any point `x ∈ s` belongs to a set `M n z`. obtain ⟨n, z, hnz⟩ : ∃ (n : ℕ) (z : T), x ∈ M n z := s_subset x xs, -- by density, it also belongs to a ball `closed_ball (d p) (u n / 3)`. obtain ⟨p, hp⟩ : ∃ (p : ℕ), x ∈ closed_ball (d p) (u n / 3), { have : set.nonempty (ball x (u n / 3)), { simp only [nonempty_ball], linarith [u_pos n] }, obtain ⟨p, hp⟩ : ∃ (p : ℕ), d p ∈ ball x (u n / 3) := hd.exists_mem_open is_open_ball this, exact ⟨p, (mem_ball'.1 hp).le⟩ }, -- choose `q` for which `t q = K n z p`. obtain ⟨q, hq⟩ : ∃ q, F q = (n, z, p) := hF _, -- then `x` belongs to `t q`. apply mem_Union.2 ⟨q, _⟩, simp only [hq, subset_closure hnz, hp, mem_inter_eq, and_self], end variables [measurable_space E] [borel_space E] (μ : measure E) [is_add_haar_measure μ] /-- Assume that a function `f` has a derivative at every point of a set `s`. Then one may partition `s` into countably many disjoint relatively measurable sets (i.e., intersections of `s` with measurable sets `t n`) on which `f` is well approximated by linear maps `A n`. -/ lemma exists_partition_approximates_linear_on_of_has_fderiv_within_at [second_countable_topology F] (f : E → F) (s : set E) (f' : E → E →L[ℝ] F) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (r : (E →L[ℝ] F) → ℝ≥0) (rpos : ∀ A, r A ≠ 0) : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] F)), pairwise (disjoint on t) ∧ (∀ n, measurable_set (t n)) ∧ (s ⊆ ⋃ n, t n) ∧ (∀ n, approximates_linear_on f (A n) (s ∩ t n) (r (A n))) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = f' y) := begin rcases exists_closed_cover_approximates_linear_on_of_has_fderiv_within_at f s f' hf' r rpos with ⟨t, A, t_closed, st, t_approx, ht⟩, refine ⟨disjointed t, A, disjoint_disjointed _, measurable_set.disjointed (λ n, (t_closed n).measurable_set), _, _, ht⟩, { rw Union_disjointed, exact st }, { assume n, exact (t_approx n).mono_set (inter_subset_inter_right _ (disjointed_subset _ _)) }, end namespace measure_theory /-! ### Local lemmas We check that a function which is well enough approximated by a linear map expands the volume essentially like this linear map, and that its derivative (if it exists) is almost everywhere close to the approximating linear map. -/ /-- Let `f` be a function which is sufficiently close (in the Lipschitz sense) to a given linear map `A`. Then it expands the volume of any set by at most `m` for any `m > det A`. -/ lemma add_haar_image_le_mul_of_det_lt (A : E →L[ℝ] E) {m : ℝ≥0} (hm : ennreal.of_real (|A.det|) < m) : ∀ᶠ δ in 𝓝[>] (0 : ℝ≥0), ∀ (s : set E) (f : E → E) (hf : approximates_linear_on f A s δ), μ (f '' s) ≤ m * μ s := begin apply nhds_within_le_nhds, let d := ennreal.of_real (|A.det|), -- construct a small neighborhood of `A '' (closed_ball 0 1)` with measure comparable to -- the determinant of `A`. obtain ⟨ε, hε, εpos⟩ : ∃ (ε : ℝ), μ (closed_ball 0 ε + A '' (closed_ball 0 1)) < m * μ (closed_ball 0 1) ∧ 0 < ε, { have HC : is_compact (A '' closed_ball 0 1) := (proper_space.is_compact_closed_ball _ _).image A.continuous, have L0 : tendsto (λ ε, μ (cthickening ε (A '' (closed_ball 0 1)))) (𝓝[>] 0) (𝓝 (μ (A '' (closed_ball 0 1)))), { apply tendsto.mono_left _ nhds_within_le_nhds, exact tendsto_measure_cthickening_of_is_compact HC }, have L1 : tendsto (λ ε, μ (closed_ball 0 ε + A '' (closed_ball 0 1))) (𝓝[>] 0) (𝓝 (μ (A '' (closed_ball 0 1)))), { apply L0.congr' _, filter_upwards [self_mem_nhds_within] with r hr, rw [←HC.add_closed_ball_zero (le_of_lt hr), add_comm] }, have L2 : tendsto (λ ε, μ (closed_ball 0 ε + A '' (closed_ball 0 1))) (𝓝[>] 0) (𝓝 (d * μ (closed_ball 0 1))), { convert L1, exact (add_haar_image_continuous_linear_map _ _ _).symm }, have I : d * μ (closed_ball 0 1) < m * μ (closed_ball 0 1) := (ennreal.mul_lt_mul_right ((measure_closed_ball_pos μ _ zero_lt_one).ne') measure_closed_ball_lt_top.ne).2 hm, have H : ∀ᶠ (b : ℝ) in 𝓝[>] 0, μ (closed_ball 0 b + A '' closed_ball 0 1) < m * μ (closed_ball 0 1) := (tendsto_order.1 L2).2 _ I, exact (H.and self_mem_nhds_within).exists }, have : Iio (⟨ε, εpos.le⟩ : ℝ≥0) ∈ 𝓝 (0 : ℝ≥0), { apply Iio_mem_nhds, exact εpos }, filter_upwards [this], -- fix a function `f` which is close enough to `A`. assume δ hδ s f hf, -- This function expands the volume of any ball by at most `m` have I : ∀ x r, x ∈ s → 0 ≤ r → μ (f '' (s ∩ closed_ball x r)) ≤ m * μ (closed_ball x r), { assume x r xs r0, have K : f '' (s ∩ closed_ball x r) ⊆ A '' (closed_ball 0 r) + closed_ball (f x) (ε * r), { rintros y ⟨z, ⟨zs, zr⟩, rfl⟩, apply set.mem_add.2 ⟨A (z - x), f z - f x - A (z - x) + f x, _, _, _⟩, { apply mem_image_of_mem, simpa only [dist_eq_norm, mem_closed_ball, mem_closed_ball_zero_iff] using zr }, { rw [mem_closed_ball_iff_norm, add_sub_cancel], calc ∥f z - f x - A (z - x)∥ ≤ δ * ∥z - x∥ : hf _ zs _ xs ... ≤ ε * r : mul_le_mul (le_of_lt hδ) (mem_closed_ball_iff_norm.1 zr) (norm_nonneg _) εpos.le }, { simp only [map_sub, pi.sub_apply], abel } }, have : A '' (closed_ball 0 r) + closed_ball (f x) (ε * r) = {f x} + r • (A '' (closed_ball 0 1) + closed_ball 0 ε), by rw [smul_add_set, ← add_assoc, add_comm ({f x}), add_assoc, smul_closed_ball _ _ εpos.le, smul_zero, singleton_add_closed_ball_zero, ← A.image_smul_set, smul_closed_ball _ _ zero_le_one, smul_zero, real.norm_eq_abs, abs_of_nonneg r0, mul_one, mul_comm], rw this at K, calc μ (f '' (s ∩ closed_ball x r)) ≤ μ ({f x} + r • (A '' (closed_ball 0 1) + closed_ball 0 ε)) : measure_mono K ... = ennreal.of_real (r ^ finrank ℝ E) * μ (A '' closed_ball 0 1 + closed_ball 0 ε) : by simp only [abs_of_nonneg r0, add_haar_smul, image_add_left, abs_pow, singleton_add, measure_preimage_add] ... ≤ ennreal.of_real (r ^ finrank ℝ E) * (m * μ (closed_ball 0 1)) : by { rw add_comm, exact ennreal.mul_le_mul le_rfl hε.le } ... = m * μ (closed_ball x r) : by { simp only [add_haar_closed_ball' _ _ r0], ring } }, -- covering `s` by closed balls with total measure very close to `μ s`, one deduces that the -- measure of `f '' s` is at most `m * (μ s + a)` for any positive `a`. have J : ∀ᶠ a in 𝓝[>] (0 : ℝ≥0∞), μ (f '' s) ≤ m * (μ s + a), { filter_upwards [self_mem_nhds_within] with a ha, change 0 < a at ha, obtain ⟨t, r, t_count, ts, rpos, st, μt⟩ : ∃ (t : set E) (r : E → ℝ), t.countable ∧ t ⊆ s ∧ (∀ (x : E), x ∈ t → 0 < r x) ∧ (s ⊆ ⋃ (x ∈ t), closed_ball x (r x)) ∧ ∑' (x : ↥t), μ (closed_ball ↑x (r ↑x)) ≤ μ s + a := besicovitch.exists_closed_ball_covering_tsum_measure_le μ ha.ne' (λ x, Ioi 0) s (λ x xs δ δpos, ⟨δ/2, by simp [half_pos δpos, half_lt_self δpos]⟩), haveI : encodable t := t_count.to_encodable, calc μ (f '' s) ≤ μ (⋃ (x : t), f '' (s ∩ closed_ball x (r x))) : begin rw bUnion_eq_Union at st, apply measure_mono, rw [← image_Union, ← inter_Union], exact image_subset _ (subset_inter (subset.refl _) st) end ... ≤ ∑' (x : t), μ (f '' (s ∩ closed_ball x (r x))) : measure_Union_le _ ... ≤ ∑' (x : t), m * μ (closed_ball x (r x)) : ennreal.tsum_le_tsum (λ x, I x (r x) (ts x.2) (rpos x x.2).le) ... ≤ m * (μ s + a) : by { rw ennreal.tsum_mul_left, exact ennreal.mul_le_mul le_rfl μt } }, -- taking the limit in `a`, one obtains the conclusion have L : tendsto (λ a, (m : ℝ≥0∞) * (μ s + a)) (𝓝[>] 0) (𝓝 (m * (μ s + 0))), { apply tendsto.mono_left _ nhds_within_le_nhds, apply ennreal.tendsto.const_mul (tendsto_const_nhds.add tendsto_id), simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff] }, rw add_zero at L, exact ge_of_tendsto L J, end /-- Let `f` be a function which is sufficiently close (in the Lipschitz sense) to a given linear map `A`. Then it expands the volume of any set by at least `m` for any `m < det A`. -/ lemma mul_le_add_haar_image_of_lt_det (A : E →L[ℝ] E) {m : ℝ≥0} (hm : (m : ℝ≥0∞) < ennreal.of_real (|A.det|)) : ∀ᶠ δ in 𝓝[>] (0 : ℝ≥0), ∀ (s : set E) (f : E → E) (hf : approximates_linear_on f A s δ), (m : ℝ≥0∞) * μ s ≤ μ (f '' s) := begin apply nhds_within_le_nhds, -- The assumption `hm` implies that `A` is invertible. If `f` is close enough to `A`, it is also -- invertible. One can then pass to the inverses, and deduce the estimate from -- `add_haar_image_le_mul_of_det_lt` applied to `f⁻¹` and `A⁻¹`. -- exclude first the trivial case where `m = 0`. rcases eq_or_lt_of_le (zero_le m) with rfl|mpos, { apply eventually_of_forall, simp only [forall_const, zero_mul, implies_true_iff, zero_le, ennreal.coe_zero] }, have hA : A.det ≠ 0, { assume h, simpa only [h, ennreal.not_lt_zero, ennreal.of_real_zero, abs_zero] using hm }, -- let `B` be the continuous linear equiv version of `A`. let B := A.to_continuous_linear_equiv_of_det_ne_zero hA, -- the determinant of `B.symm` is bounded by `m⁻¹` have I : ennreal.of_real (|(B.symm : E →L[ℝ] E).det|) < (m⁻¹ : ℝ≥0), { simp only [ennreal.of_real, abs_inv, real.to_nnreal_inv, continuous_linear_equiv.det_coe_symm, continuous_linear_map.coe_to_continuous_linear_equiv_of_det_ne_zero, ennreal.coe_lt_coe] at ⊢ hm, exact nnreal.inv_lt_inv mpos.ne' hm }, -- therefore, we may apply `add_haar_image_le_mul_of_det_lt` to `B.symm` and `m⁻¹`. obtain ⟨δ₀, δ₀pos, hδ₀⟩ : ∃ (δ : ℝ≥0), 0 < δ ∧ ∀ (t : set E) (g : E → E), approximates_linear_on g (B.symm : E →L[ℝ] E) t δ → μ (g '' t) ≤ ↑m⁻¹ * μ t, { have : ∀ᶠ (δ : ℝ≥0) in 𝓝[>] 0, ∀ (t : set E) (g : E → E), approximates_linear_on g (B.symm : E →L[ℝ] E) t δ → μ (g '' t) ≤ ↑m⁻¹ * μ t := add_haar_image_le_mul_of_det_lt μ B.symm I, rcases (this.and self_mem_nhds_within).exists with ⟨δ₀, h, h'⟩, exact ⟨δ₀, h', h⟩, }, -- record smallness conditions for `δ` that will be needed to apply `hδ₀` below. have L1 : ∀ᶠ δ in 𝓝 (0 : ℝ≥0), subsingleton E ∨ δ < ∥(B.symm : E →L[ℝ] E)∥₊⁻¹, { by_cases (subsingleton E), { simp only [h, true_or, eventually_const] }, simp only [h, false_or], apply Iio_mem_nhds, simpa only [h, false_or, nnreal.inv_pos] using B.subsingleton_or_nnnorm_symm_pos }, have L2 : ∀ᶠ δ in 𝓝 (0 : ℝ≥0), ∥(B.symm : E →L[ℝ] E)∥₊ * (∥(B.symm : E →L[ℝ] E)∥₊⁻¹ - δ)⁻¹ * δ < δ₀, { have : tendsto (λ δ, ∥(B.symm : E →L[ℝ] E)∥₊ * (∥(B.symm : E →L[ℝ] E)∥₊⁻¹ - δ)⁻¹ * δ) (𝓝 0) (𝓝 (∥(B.symm : E →L[ℝ] E)∥₊ * (∥(B.symm : E →L[ℝ] E)∥₊⁻¹ - 0)⁻¹ * 0)), { rcases eq_or_ne (∥(B.symm : E →L[ℝ] E)∥₊) 0 with H|H, { simpa only [H, zero_mul] using tendsto_const_nhds }, refine tendsto.mul (tendsto_const_nhds.mul _) tendsto_id, refine (tendsto.sub tendsto_const_nhds tendsto_id).inv₀ _, simpa only [tsub_zero, inv_eq_zero, ne.def] using H }, simp only [mul_zero] at this, exact (tendsto_order.1 this).2 δ₀ δ₀pos }, -- let `δ` be small enough, and `f` approximated by `B` up to `δ`. filter_upwards [L1, L2], assume δ h1δ h2δ s f hf, have hf' : approximates_linear_on f (B : E →L[ℝ] E) s δ, by { convert hf, exact A.coe_to_continuous_linear_equiv_of_det_ne_zero _ }, let F := hf'.to_local_equiv h1δ, -- the condition to be checked can be reformulated in terms of the inverse maps suffices H : μ ((F.symm) '' F.target) ≤ (m⁻¹ : ℝ≥0) * μ F.target, { change (m : ℝ≥0∞) * μ (F.source) ≤ μ (F.target), rwa [← F.symm_image_target_eq_source, mul_comm, ← ennreal.le_div_iff_mul_le, div_eq_mul_inv, mul_comm, ← ennreal.coe_inv (mpos.ne')], { apply or.inl, simpa only [ennreal.coe_eq_zero, ne.def] using mpos.ne'}, { simp only [ennreal.coe_ne_top, true_or, ne.def, not_false_iff] } }, -- as `f⁻¹` is well approximated by `B⁻¹`, the conclusion follows from `hδ₀` -- and our choice of `δ`. exact hδ₀ _ _ ((hf'.to_inv h1δ).mono_num h2δ.le), end /-- If a differentiable function `f` is approximated by a linear map `A` on a set `s`, up to `δ`, then at almost every `x` in `s` one has `∥f' x - A∥ ≤ δ`. -/ lemma _root_.approximates_linear_on.norm_fderiv_sub_le {A : E →L[ℝ] E} {δ : ℝ≥0} (hf : approximates_linear_on f A s δ) (hs : measurable_set s) (f' : E → E →L[ℝ] E) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) : ∀ᵐ x ∂(μ.restrict s), ∥f' x - A∥₊ ≤ δ := begin /- The conclusion will hold at the Lebesgue density points of `s` (which have full measure). At such a point `x`, for any `z` and any `ε > 0` one has for small `r` that `{x} + r • closed_ball z ε` intersects `s`. At a point `y` in the intersection, `f y - f x` is close both to `f' x (r z)` (by differentiability) and to `A (r z)` (by linear approximation), so these two quantities are close, i.e., `(f' x - A) z` is small. -/ filter_upwards [besicovitch.ae_tendsto_measure_inter_div μ s, ae_restrict_mem hs], -- start from a Lebesgue density point `x`, belonging to `s`. assume x hx xs, -- consider an arbitrary vector `z`. apply continuous_linear_map.op_norm_le_bound _ δ.2 (λ z, _), -- to show that `∥(f' x - A) z∥ ≤ δ ∥z∥`, it suffices to do it up to some error that vanishes -- asymptotically in terms of `ε > 0`. suffices H : ∀ ε, 0 < ε → ∥(f' x - A) z∥ ≤ (δ + ε) * (∥z∥ + ε) + ∥(f' x - A)∥ * ε, { have : tendsto (λ (ε : ℝ), ((δ : ℝ) + ε) * (∥z∥ + ε) + ∥(f' x - A)∥ * ε) (𝓝[>] 0) (𝓝 ((δ + 0) * (∥z∥ + 0) + ∥(f' x - A)∥ * 0)) := tendsto.mono_left (continuous.tendsto (by continuity) 0) nhds_within_le_nhds, simp only [add_zero, mul_zero] at this, apply le_of_tendsto_of_tendsto tendsto_const_nhds this, filter_upwards [self_mem_nhds_within], exact H }, -- fix a positive `ε`. assume ε εpos, -- for small enough `r`, the rescaled ball `r • closed_ball z ε` intersects `s`, as `x` is a -- density point have B₁ : ∀ᶠ r in 𝓝[>] (0 : ℝ), (s ∩ ({x} + r • closed_ball z ε)).nonempty := eventually_nonempty_inter_smul_of_density_one μ s x hx _ measurable_set_closed_ball (measure_closed_ball_pos μ z εpos).ne', obtain ⟨ρ, ρpos, hρ⟩ : ∃ ρ > 0, ball x ρ ∩ s ⊆ {y : E | ∥f y - f x - (f' x) (y - x)∥ ≤ ε * ∥y - x∥} := mem_nhds_within_iff.1 (is_o.def (hf' x xs) εpos), -- for small enough `r`, the rescaled ball `r • closed_ball z ε` is included in the set where -- `f y - f x` is well approximated by `f' x (y - x)`. have B₂ : ∀ᶠ r in 𝓝[>] (0 : ℝ), {x} + r • closed_ball z ε ⊆ ball x ρ := nhds_within_le_nhds (eventually_singleton_add_smul_subset bounded_closed_ball (ball_mem_nhds x ρpos)), -- fix a small positive `r` satisfying the above properties, as well as a corresponding `y`. obtain ⟨r, ⟨y, ⟨ys, hy⟩⟩, rρ, rpos⟩ : ∃ (r : ℝ), (s ∩ ({x} + r • closed_ball z ε)).nonempty ∧ {x} + r • closed_ball z ε ⊆ ball x ρ ∧ 0 < r := (B₁.and (B₂.and self_mem_nhds_within)).exists, -- write `y = x + r a` with `a ∈ closed_ball z ε`. obtain ⟨a, az, ya⟩ : ∃ a, a ∈ closed_ball z ε ∧ y = x + r • a, { simp only [mem_smul_set, image_add_left, mem_preimage, singleton_add] at hy, rcases hy with ⟨a, az, ha⟩, exact ⟨a, az, by simp only [ha, add_neg_cancel_left]⟩ }, have norm_a : ∥a∥ ≤ ∥z∥ + ε := calc ∥a∥ = ∥z + (a - z)∥ : by simp only [add_sub_cancel'_right] ... ≤ ∥z∥ + ∥a - z∥ : norm_add_le _ _ ... ≤ ∥z∥ + ε : add_le_add_left (mem_closed_ball_iff_norm.1 az) _, -- use the approximation properties to control `(f' x - A) a`, and then `(f' x - A) z` as `z` is -- close to `a`. have I : r * ∥(f' x - A) a∥ ≤ r * (δ + ε) * (∥z∥ + ε) := calc r * ∥(f' x - A) a∥ = ∥(f' x - A) (r • a)∥ : by simp only [continuous_linear_map.map_smul, norm_smul, real.norm_eq_abs, abs_of_nonneg rpos.le] ... = ∥(f y - f x - A (y - x)) - (f y - f x - (f' x) (y - x))∥ : begin congr' 1, simp only [ya, add_sub_cancel', sub_sub_sub_cancel_left, continuous_linear_map.coe_sub', eq_self_iff_true, sub_left_inj, pi.sub_apply, continuous_linear_map.map_smul, smul_sub], end ... ≤ ∥f y - f x - A (y - x)∥ + ∥f y - f x - (f' x) (y - x)∥ : norm_sub_le _ _ ... ≤ δ * ∥y - x∥ + ε * ∥y - x∥ : add_le_add (hf _ ys _ xs) (hρ ⟨rρ hy, ys⟩) ... = r * (δ + ε) * ∥a∥ : by { simp only [ya, add_sub_cancel', norm_smul, real.norm_eq_abs, abs_of_nonneg rpos.le], ring } ... ≤ r * (δ + ε) * (∥z∥ + ε) : mul_le_mul_of_nonneg_left norm_a (mul_nonneg rpos.le (add_nonneg δ.2 εpos.le)), show ∥(f' x - A) z∥ ≤ (δ + ε) * (∥z∥ + ε) + ∥(f' x - A)∥ * ε, from calc ∥(f' x - A) z∥ = ∥(f' x - A) a + (f' x - A) (z - a)∥ : begin congr' 1, simp only [continuous_linear_map.coe_sub', map_sub, pi.sub_apply], abel end ... ≤ ∥(f' x - A) a∥ + ∥(f' x - A) (z - a)∥ : norm_add_le _ _ ... ≤ (δ + ε) * (∥z∥ + ε) + ∥f' x - A∥ * ∥z - a∥ : begin apply add_le_add, { rw mul_assoc at I, exact (mul_le_mul_left rpos).1 I }, { apply continuous_linear_map.le_op_norm } end ... ≤ (δ + ε) * (∥z∥ + ε) + ∥f' x - A∥ * ε : add_le_add le_rfl (mul_le_mul_of_nonneg_left (mem_closed_ball_iff_norm'.1 az) (norm_nonneg _)), end /-! ### Measure zero of the image, over non-measurable sets If a set has measure `0`, then its image under a differentiable map has measure zero. This doesn't require the set to be measurable. In the same way, if `f` is differentiable on a set `s` with non-invertible derivative everywhere, then `f '' s` has measure `0`, again without measurability assumptions. -/ /-- A differentiable function maps sets of measure zero to sets of measure zero. -/ lemma add_haar_image_eq_zero_of_differentiable_on_of_add_haar_eq_zero (hf : differentiable_on ℝ f s) (hs : μ s = 0) : μ (f '' s) = 0 := begin refine le_antisymm _ (zero_le _), have : ∀ (A : E →L[ℝ] E), ∃ (δ : ℝ≥0), 0 < δ ∧ ∀ (t : set E) (hf : approximates_linear_on f A t δ), μ (f '' t) ≤ (real.to_nnreal (|A.det|) + 1 : ℝ≥0) * μ t, { assume A, let m : ℝ≥0 := real.to_nnreal ((|A.det|)) + 1, have I : ennreal.of_real (|A.det|) < m, by simp only [ennreal.of_real, m, lt_add_iff_pos_right, zero_lt_one, ennreal.coe_lt_coe], rcases ((add_haar_image_le_mul_of_det_lt μ A I).and self_mem_nhds_within).exists with ⟨δ, h, h'⟩, exact ⟨δ, h', λ t ht, h t f ht⟩ }, choose δ hδ using this, obtain ⟨t, A, t_disj, t_meas, t_cover, ht, -⟩ : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] E)), pairwise (disjoint on t) ∧ (∀ (n : ℕ), measurable_set (t n)) ∧ (s ⊆ ⋃ (n : ℕ), t n) ∧ (∀ (n : ℕ), approximates_linear_on f (A n) (s ∩ t n) (δ (A n))) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = fderiv_within ℝ f s y) := exists_partition_approximates_linear_on_of_has_fderiv_within_at f s (fderiv_within ℝ f s) (λ x xs, (hf x xs).has_fderiv_within_at) δ (λ A, (hδ A).1.ne'), calc μ (f '' s) ≤ μ (⋃ n, f '' (s ∩ t n)) : begin apply measure_mono, rw [← image_Union, ← inter_Union], exact image_subset f (subset_inter subset.rfl t_cover) end ... ≤ ∑' n, μ (f '' (s ∩ t n)) : measure_Union_le _ ... ≤ ∑' n, (real.to_nnreal (|(A n).det|) + 1 : ℝ≥0) * μ (s ∩ t n) : begin apply ennreal.tsum_le_tsum (λ n, _), apply (hδ (A n)).2, exact ht n, end ... ≤ ∑' n, (real.to_nnreal (|(A n).det|) + 1 : ℝ≥0) * 0 : begin refine ennreal.tsum_le_tsum (λ n, ennreal.mul_le_mul le_rfl _), exact le_trans (measure_mono (inter_subset_left _ _)) (le_of_eq hs), end ... = 0 : by simp only [tsum_zero, mul_zero] end /-- A version of Sard lemma in fixed dimension: given a differentiable function from `E` to `E` and a set where the differential is not invertible, then the image of this set has zero measure. Here, we give an auxiliary statement towards this result. -/ lemma add_haar_image_eq_zero_of_det_fderiv_within_eq_zero_aux (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (R : ℝ) (hs : s ⊆ closed_ball 0 R) (ε : ℝ≥0) (εpos : 0 < ε) (h'f' : ∀ x ∈ s, (f' x).det = 0) : μ (f '' s) ≤ ε * μ (closed_ball 0 R) := begin rcases eq_empty_or_nonempty s with rfl|h's, { simp only [measure_empty, zero_le, image_empty] }, have : ∀ (A : E →L[ℝ] E), ∃ (δ : ℝ≥0), 0 < δ ∧ ∀ (t : set E) (hf : approximates_linear_on f A t δ), μ (f '' t) ≤ (real.to_nnreal (|A.det|) + ε : ℝ≥0) * μ t, { assume A, let m : ℝ≥0 := real.to_nnreal (|A.det|) + ε, have I : ennreal.of_real (|A.det|) < m, by simp only [ennreal.of_real, m, lt_add_iff_pos_right, εpos, ennreal.coe_lt_coe], rcases ((add_haar_image_le_mul_of_det_lt μ A I).and self_mem_nhds_within).exists with ⟨δ, h, h'⟩, exact ⟨δ, h', λ t ht, h t f ht⟩ }, choose δ hδ using this, obtain ⟨t, A, t_disj, t_meas, t_cover, ht, Af'⟩ : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] E)), pairwise (disjoint on t) ∧ (∀ (n : ℕ), measurable_set (t n)) ∧ (s ⊆ ⋃ (n : ℕ), t n) ∧ (∀ (n : ℕ), approximates_linear_on f (A n) (s ∩ t n) (δ (A n))) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = f' y) := exists_partition_approximates_linear_on_of_has_fderiv_within_at f s f' hf' δ (λ A, (hδ A).1.ne'), calc μ (f '' s) ≤ μ (⋃ n, f '' (s ∩ t n)) : begin apply measure_mono, rw [← image_Union, ← inter_Union], exact image_subset f (subset_inter subset.rfl t_cover) end ... ≤ ∑' n, μ (f '' (s ∩ t n)) : measure_Union_le _ ... ≤ ∑' n, (real.to_nnreal (|(A n).det|) + ε : ℝ≥0) * μ (s ∩ t n) : begin apply ennreal.tsum_le_tsum (λ n, _), apply (hδ (A n)).2, exact ht n, end ... = ∑' n, ε * μ (s ∩ t n) : begin congr' with n, rcases Af' h's n with ⟨y, ys, hy⟩, simp only [hy, h'f' y ys, real.to_nnreal_zero, abs_zero, zero_add] end ... ≤ ε * ∑' n, μ (closed_ball 0 R ∩ t n) : begin rw ennreal.tsum_mul_left, refine ennreal.mul_le_mul le_rfl (ennreal.tsum_le_tsum (λ n, measure_mono _)), exact inter_subset_inter_left _ hs, end ... = ε * μ (⋃ n, closed_ball 0 R ∩ t n) : begin rw measure_Union, { exact pairwise_disjoint.mono t_disj (λ n, inter_subset_right _ _) }, { assume n, exact measurable_set_closed_ball.inter (t_meas n) } end ... ≤ ε * μ (closed_ball 0 R) : begin rw ← inter_Union, exact ennreal.mul_le_mul le_rfl (measure_mono (inter_subset_left _ _)), end end /-- A version of Sard lemma in fixed dimension: given a differentiable function from `E` to `E` and a set where the differential is not invertible, then the image of this set has zero measure. -/ lemma add_haar_image_eq_zero_of_det_fderiv_within_eq_zero (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (h'f' : ∀ x ∈ s, (f' x).det = 0) : μ (f '' s) = 0 := begin suffices H : ∀ R, μ (f '' (s ∩ closed_ball 0 R)) = 0, { apply le_antisymm _ (zero_le _), rw ← Union_inter_closed_ball_nat s 0, calc μ (f '' ⋃ (n : ℕ), s ∩ closed_ball 0 n) ≤ ∑' (n : ℕ), μ (f '' (s ∩ closed_ball 0 n)) : by { rw image_Union, exact measure_Union_le _ } ... ≤ 0 : by simp only [H, tsum_zero, nonpos_iff_eq_zero] }, assume R, have A : ∀ (ε : ℝ≥0) (εpos : 0 < ε), μ (f '' (s ∩ closed_ball 0 R)) ≤ ε * μ (closed_ball 0 R) := λ ε εpos, add_haar_image_eq_zero_of_det_fderiv_within_eq_zero_aux μ (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _)) R (inter_subset_right _ _) ε εpos (λ x hx, h'f' x hx.1), have B : tendsto (λ (ε : ℝ≥0), (ε : ℝ≥0∞) * μ (closed_ball 0 R)) (𝓝[>] 0) (𝓝 0), { have : tendsto (λ (ε : ℝ≥0), (ε : ℝ≥0∞) * μ (closed_ball 0 R)) (𝓝 0) (𝓝 (((0 : ℝ≥0) : ℝ≥0∞) * μ (closed_ball 0 R))) := ennreal.tendsto.mul_const (ennreal.tendsto_coe.2 tendsto_id) (or.inr ((measure_closed_ball_lt_top).ne)), simp only [zero_mul, ennreal.coe_zero] at this, exact tendsto.mono_left this nhds_within_le_nhds }, apply le_antisymm _ (zero_le _), apply ge_of_tendsto B, filter_upwards [self_mem_nhds_within], exact A, end /-! ### Weak measurability statements We show that the derivative of a function on a set is almost everywhere measurable, and that the image `f '' s` is measurable if `f` is injective on `s`. The latter statement follows from the Lusin-Souslin theorem. -/ /-- The derivative of a function on a measurable set is almost everywhere measurable on this set with respect to Lebesgue measure. Note that, in general, it is not genuinely measurable there, as `f'` is not unique (but only on a set of measure `0`, as the argument shows). -/ lemma ae_measurable_fderiv_within (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) : ae_measurable f' (μ.restrict s) := begin /- It suffices to show that `f'` can be uniformly approximated by a measurable function. Fix `ε > 0`. Thanks to `exists_partition_approximates_linear_on_of_has_fderiv_within_at`, one can find a countable measurable partition of `s` into sets `s ∩ t n` on which `f` is well approximated by linear maps `A n`. On almost all of `s ∩ t n`, it follows from `approximates_linear_on.norm_fderiv_sub_le` that `f'` is uniformly approximated by `A n`, which gives the conclusion. -/ -- fix a precision `ε` refine ae_measurable_of_unif_approx (λ ε εpos, _), let δ : ℝ≥0 := ⟨ε, le_of_lt εpos⟩, have δpos : 0 < δ := εpos, -- partition `s` into sets `s ∩ t n` on which `f` is approximated by linear maps `A n`. obtain ⟨t, A, t_disj, t_meas, t_cover, ht, Af'⟩ : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] E)), pairwise (disjoint on t) ∧ (∀ (n : ℕ), measurable_set (t n)) ∧ (s ⊆ ⋃ (n : ℕ), t n) ∧ (∀ (n : ℕ), approximates_linear_on f (A n) (s ∩ t n) δ) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = f' y) := exists_partition_approximates_linear_on_of_has_fderiv_within_at f s f' hf' (λ A, δ) (λ A, δpos.ne'), -- define a measurable function `g` which coincides with `A n` on `t n`. obtain ⟨g, g_meas, hg⟩ : ∃ g : E → (E →L[ℝ] E), measurable g ∧ ∀ (n : ℕ) (x : E), x ∈ t n → g x = A n := exists_measurable_piecewise_nat t t_meas t_disj (λ n x, A n) (λ n, measurable_const), refine ⟨g, g_meas.ae_measurable, _⟩, -- reduce to checking that `f'` and `g` are close on almost all of `s ∩ t n`, for all `n`. suffices H : ∀ᵐ (x : E) ∂(sum (λ n, μ.restrict (s ∩ t n))), dist (g x) (f' x) ≤ ε, { have : μ.restrict s ≤ sum (λ n, μ.restrict (s ∩ t n)), { have : s = ⋃ n, s ∩ t n, { rw ← inter_Union, exact subset.antisymm (subset_inter subset.rfl t_cover) (inter_subset_left _ _) }, conv_lhs { rw this }, exact restrict_Union_le }, exact ae_mono this H }, -- fix such an `n`. refine ae_sum_iff.2 (λ n, _), -- on almost all `s ∩ t n`, `f' x` is close to `A n` thanks to -- `approximates_linear_on.norm_fderiv_sub_le`. have E₁ : ∀ᵐ (x : E) ∂μ.restrict (s ∩ t n), ∥f' x - A n∥₊ ≤ δ := (ht n).norm_fderiv_sub_le μ (hs.inter (t_meas n)) f' (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _)), -- moreover, `g x` is equal to `A n` there. have E₂ : ∀ᵐ (x : E) ∂μ.restrict (s ∩ t n), g x = A n, { suffices H : ∀ᵐ (x : E) ∂μ.restrict (t n), g x = A n, from ae_mono (restrict_mono (inter_subset_right _ _) le_rfl) H, filter_upwards [ae_restrict_mem (t_meas n)], exact hg n }, -- putting these two properties together gives the conclusion. filter_upwards [E₁, E₂] with x hx1 hx2, rw ← nndist_eq_nnnorm at hx1, rw [hx2, dist_comm], exact hx1, end lemma ae_measurable_of_real_abs_det_fderiv_within (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) : ae_measurable (λ x, ennreal.of_real (|(f' x).det|)) (μ.restrict s) := begin apply ennreal.measurable_of_real.comp_ae_measurable, refine continuous_abs.measurable.comp_ae_measurable _, refine continuous_linear_map.continuous_det.measurable.comp_ae_measurable _, exact ae_measurable_fderiv_within μ hs hf' end lemma ae_measurable_to_nnreal_abs_det_fderiv_within (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) : ae_measurable (λ x, |(f' x).det|.to_nnreal) (μ.restrict s) := begin apply measurable_real_to_nnreal.comp_ae_measurable, refine continuous_abs.measurable.comp_ae_measurable _, refine continuous_linear_map.continuous_det.measurable.comp_ae_measurable _, exact ae_measurable_fderiv_within μ hs hf' end /-- If a function is differentiable and injective on a measurable set, then the image is measurable.-/ lemma measurable_image_of_fderiv_within (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) : measurable_set (f '' s) := begin have : differentiable_on ℝ f s := λ x hx, (hf' x hx).differentiable_within_at, exact hs.image_of_continuous_on_inj_on (differentiable_on.continuous_on this) hf, end /-- If a function is differentiable and injective on a measurable set `s`, then its restriction to `s` is a measurable embedding. -/ lemma measurable_embedding_of_fderiv_within (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) : measurable_embedding (s.restrict f) := begin have : differentiable_on ℝ f s := λ x hx, (hf' x hx).differentiable_within_at, exact this.continuous_on.measurable_embedding hs hf end /-! ### Proving the estimate for the measure of the image We show the formula `∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ = μ (f '' s)`, in `lintegral_abs_det_fderiv_eq_add_haar_image`. For this, we show both inequalities in both directions, first up to controlled errors and then letting these errors tend to `0`. -/ lemma add_haar_image_le_lintegral_abs_det_fderiv_aux1 (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) {ε : ℝ≥0} (εpos : 0 < ε) : μ (f '' s) ≤ ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ + 2 * ε * μ s := begin /- To bound `μ (f '' s)`, we cover `s` by sets where `f` is well-approximated by linear maps `A n` (and where `f'` is almost everywhere close to `A n`), and then use that `f` expands the measure of such a set by at most `(A n).det + ε`. -/ have : ∀ (A : E →L[ℝ] E), ∃ (δ : ℝ≥0), 0 < δ ∧ (∀ (B : E →L[ℝ] E), ∥B - A∥ ≤ δ → |B.det - A.det| ≤ ε) ∧ ∀ (t : set E) (g : E → E) (hf : approximates_linear_on g A t δ), μ (g '' t) ≤ (ennreal.of_real (|A.det|) + ε) * μ t, { assume A, let m : ℝ≥0 := real.to_nnreal (|A.det|) + ε, have I : ennreal.of_real (|A.det|) < m, by simp only [ennreal.of_real, m, lt_add_iff_pos_right, εpos, ennreal.coe_lt_coe], rcases ((add_haar_image_le_mul_of_det_lt μ A I).and self_mem_nhds_within).exists with ⟨δ, h, δpos⟩, obtain ⟨δ', δ'pos, hδ'⟩ : ∃ (δ' : ℝ) (H : 0 < δ'), ∀ B, dist B A < δ' → dist B.det A.det < ↑ε := continuous_at_iff.1 continuous_linear_map.continuous_det.continuous_at ε εpos, let δ'' : ℝ≥0 := ⟨δ' / 2, (half_pos δ'pos).le⟩, refine ⟨min δ δ'', lt_min δpos (half_pos δ'pos), _, _⟩, { assume B hB, rw ← real.dist_eq, apply (hδ' B _).le, rw dist_eq_norm, calc ∥B - A∥ ≤ (min δ δ'' : ℝ≥0) : hB ... ≤ δ'' : by simp only [le_refl, nnreal.coe_min, min_le_iff, or_true] ... < δ' : half_lt_self δ'pos }, { assume t g htg, exact h t g (htg.mono_num (min_le_left _ _)) } }, choose δ hδ using this, obtain ⟨t, A, t_disj, t_meas, t_cover, ht, -⟩ : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] E)), pairwise (disjoint on t) ∧ (∀ (n : ℕ), measurable_set (t n)) ∧ (s ⊆ ⋃ (n : ℕ), t n) ∧ (∀ (n : ℕ), approximates_linear_on f (A n) (s ∩ t n) (δ (A n))) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = f' y) := exists_partition_approximates_linear_on_of_has_fderiv_within_at f s f' hf' δ (λ A, (hδ A).1.ne'), calc μ (f '' s) ≤ μ (⋃ n, f '' (s ∩ t n)) : begin apply measure_mono, rw [← image_Union, ← inter_Union], exact image_subset f (subset_inter subset.rfl t_cover) end ... ≤ ∑' n, μ (f '' (s ∩ t n)) : measure_Union_le _ ... ≤ ∑' n, (ennreal.of_real (|(A n).det|) + ε) * μ (s ∩ t n) : begin apply ennreal.tsum_le_tsum (λ n, _), apply (hδ (A n)).2.2, exact ht n, end ... = ∑' n, ∫⁻ x in s ∩ t n, ennreal.of_real (|(A n).det|) + ε ∂μ : by simp only [lintegral_const, measurable_set.univ, measure.restrict_apply, univ_inter] ... ≤ ∑' n, ∫⁻ x in s ∩ t n, ennreal.of_real (|(f' x).det|) + 2 * ε ∂μ : begin apply ennreal.tsum_le_tsum (λ n, _), apply lintegral_mono_ae, filter_upwards [(ht n).norm_fderiv_sub_le μ (hs.inter (t_meas n)) f' (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _))], assume x hx, have I : |(A n).det| ≤ |(f' x).det| + ε := calc |(A n).det| = |(f' x).det - ((f' x).det - (A n).det)| : by { congr' 1, abel } ... ≤ |(f' x).det| + |(f' x).det - (A n).det| : abs_sub _ _ ... ≤ |(f' x).det| + ε : add_le_add le_rfl ((hδ (A n)).2.1 _ hx), calc ennreal.of_real (|(A n).det|) + ε ≤ ennreal.of_real (|(f' x).det| + ε) + ε : add_le_add (ennreal.of_real_le_of_real I) le_rfl ... = ennreal.of_real (|(f' x).det|) + 2 * ε : by simp only [ennreal.of_real_add, abs_nonneg, two_mul, add_assoc, nnreal.zero_le_coe, ennreal.of_real_coe_nnreal], end ... = ∫⁻ x in ⋃ n, s ∩ t n, ennreal.of_real (|(f' x).det|) + 2 * ε ∂μ : begin have M : ∀ (n : ℕ), measurable_set (s ∩ t n) := λ n, hs.inter (t_meas n), rw lintegral_Union M, exact pairwise_disjoint.mono t_disj (λ n, inter_subset_right _ _), end ... = ∫⁻ x in s, ennreal.of_real (|(f' x).det|) + 2 * ε ∂μ : begin have : s = ⋃ n, s ∩ t n, { rw ← inter_Union, exact subset.antisymm (subset_inter subset.rfl t_cover) (inter_subset_left _ _) }, rw ← this, end ... = ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ + 2 * ε * μ s : begin rw lintegral_add' (ae_measurable_of_real_abs_det_fderiv_within μ hs hf') ae_measurable_const, simp only [lintegral_const, measurable_set.univ, measure.restrict_apply, univ_inter], end end lemma add_haar_image_le_lintegral_abs_det_fderiv_aux2 (hs : measurable_set s) (h's : μ s ≠ ∞) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) : μ (f '' s) ≤ ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ := begin /- We just need to let the error tend to `0` in the previous lemma. -/ have : tendsto (λ (ε : ℝ≥0), ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ + 2 * ε * μ s) (𝓝[>] 0) (𝓝 (∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ + 2 * (0 : ℝ≥0) * μ s)), { apply tendsto.mono_left _ nhds_within_le_nhds, refine tendsto_const_nhds.add _, refine ennreal.tendsto.mul_const _ (or.inr h's), exact ennreal.tendsto.const_mul (ennreal.tendsto_coe.2 tendsto_id) (or.inr ennreal.coe_ne_top) }, simp only [add_zero, zero_mul, mul_zero, ennreal.coe_zero] at this, apply ge_of_tendsto this, filter_upwards [self_mem_nhds_within], rintros ε (εpos : 0 < ε), exact add_haar_image_le_lintegral_abs_det_fderiv_aux1 μ hs hf' εpos, end lemma add_haar_image_le_lintegral_abs_det_fderiv (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) : μ (f '' s) ≤ ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ := begin /- We already know the result for finite-measure sets. We cover `s` by finite-measure sets using `spanning_sets μ`, and apply the previous result to each of these parts. -/ let u := λ n, disjointed (spanning_sets μ) n, have u_meas : ∀ n, measurable_set (u n), { assume n, apply measurable_set.disjointed (λ i, _), exact measurable_spanning_sets μ i }, have A : s = ⋃ n, s ∩ u n, by rw [← inter_Union, Union_disjointed, Union_spanning_sets, inter_univ], calc μ (f '' s) ≤ ∑' n, μ (f '' (s ∩ u n)) : begin conv_lhs { rw [A, image_Union] }, exact measure_Union_le _, end ... ≤ ∑' n, ∫⁻ x in s ∩ u n, ennreal.of_real (|(f' x).det|) ∂μ : begin apply ennreal.tsum_le_tsum (λ n, _), apply add_haar_image_le_lintegral_abs_det_fderiv_aux2 μ (hs.inter (u_meas n)) _ (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _)), have : μ (u n) < ∞ := lt_of_le_of_lt (measure_mono (disjointed_subset _ _)) (measure_spanning_sets_lt_top μ n), exact ne_of_lt (lt_of_le_of_lt (measure_mono (inter_subset_right _ _)) this), end ... = ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ : begin conv_rhs { rw A }, rw lintegral_Union, { assume n, exact hs.inter (u_meas n) }, { exact pairwise_disjoint.mono (disjoint_disjointed _) (λ n, inter_subset_right _ _) } end end lemma lintegral_abs_det_fderiv_le_add_haar_image_aux1 (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) {ε : ℝ≥0} (εpos : 0 < ε) : ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ ≤ μ (f '' s) + 2 * ε * μ s := begin /- To bound `∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ`, we cover `s` by sets where `f` is well-approximated by linear maps `A n` (and where `f'` is almost everywhere close to `A n`), and then use that `f` expands the measure of such a set by at least `(A n).det - ε`. -/ have : ∀ (A : E →L[ℝ] E), ∃ (δ : ℝ≥0), 0 < δ ∧ (∀ (B : E →L[ℝ] E), ∥B - A∥ ≤ δ → |B.det - A.det| ≤ ε) ∧ ∀ (t : set E) (g : E → E) (hf : approximates_linear_on g A t δ), ennreal.of_real (|A.det|) * μ t ≤ μ (g '' t) + ε * μ t, { assume A, obtain ⟨δ', δ'pos, hδ'⟩ : ∃ (δ' : ℝ) (H : 0 < δ'), ∀ B, dist B A < δ' → dist B.det A.det < ↑ε := continuous_at_iff.1 continuous_linear_map.continuous_det.continuous_at ε εpos, let δ'' : ℝ≥0 := ⟨δ' / 2, (half_pos δ'pos).le⟩, have I'' : ∀ (B : E →L[ℝ] E), ∥B - A∥ ≤ ↑δ'' → |B.det - A.det| ≤ ↑ε, { assume B hB, rw ← real.dist_eq, apply (hδ' B _).le, rw dist_eq_norm, exact hB.trans_lt (half_lt_self δ'pos) }, rcases eq_or_ne A.det 0 with hA|hA, { refine ⟨δ'', half_pos δ'pos, I'', _⟩, simp only [hA, forall_const, zero_mul, ennreal.of_real_zero, implies_true_iff, zero_le, abs_zero] }, let m : ℝ≥0 := real.to_nnreal (|A.det|) - ε, have I : (m : ℝ≥0∞) < ennreal.of_real (|A.det|), { simp only [ennreal.of_real, with_top.coe_sub], apply ennreal.sub_lt_self ennreal.coe_ne_top, { simpa only [abs_nonpos_iff, real.to_nnreal_eq_zero, ennreal.coe_eq_zero, ne.def] using hA }, { simp only [εpos.ne', ennreal.coe_eq_zero, ne.def, not_false_iff] } }, rcases ((mul_le_add_haar_image_of_lt_det μ A I).and self_mem_nhds_within).exists with ⟨δ, h, δpos⟩, refine ⟨min δ δ'', lt_min δpos (half_pos δ'pos), _, _⟩, { assume B hB, apply I'' _ (hB.trans _), simp only [le_refl, nnreal.coe_min, min_le_iff, or_true] }, { assume t g htg, rcases eq_or_ne (μ t) ∞ with ht|ht, { simp only [ht, εpos.ne', with_top.mul_top, ennreal.coe_eq_zero, le_top, ne.def, not_false_iff, ennreal.add_top] }, have := h t g (htg.mono_num (min_le_left _ _)), rwa [with_top.coe_sub, ennreal.sub_mul, tsub_le_iff_right] at this, simp only [ht, implies_true_iff, ne.def, not_false_iff] } }, choose δ hδ using this, obtain ⟨t, A, t_disj, t_meas, t_cover, ht, -⟩ : ∃ (t : ℕ → set E) (A : ℕ → (E →L[ℝ] E)), pairwise (disjoint on t) ∧ (∀ (n : ℕ), measurable_set (t n)) ∧ (s ⊆ ⋃ (n : ℕ), t n) ∧ (∀ (n : ℕ), approximates_linear_on f (A n) (s ∩ t n) (δ (A n))) ∧ (s.nonempty → ∀ n, ∃ y ∈ s, A n = f' y) := exists_partition_approximates_linear_on_of_has_fderiv_within_at f s f' hf' δ (λ A, (hδ A).1.ne'), have s_eq : s = ⋃ n, s ∩ t n, { rw ← inter_Union, exact subset.antisymm (subset_inter subset.rfl t_cover) (inter_subset_left _ _) }, calc ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ = ∑' n, ∫⁻ x in s ∩ t n, ennreal.of_real (|(f' x).det|) ∂μ : begin conv_lhs { rw s_eq }, rw lintegral_Union, { exact λ n, hs.inter (t_meas n) }, { exact pairwise_disjoint.mono t_disj (λ n, inter_subset_right _ _) } end ... ≤ ∑' n, ∫⁻ x in s ∩ t n, ennreal.of_real (|(A n).det|) + ε ∂μ : begin apply ennreal.tsum_le_tsum (λ n, _), apply lintegral_mono_ae, filter_upwards [(ht n).norm_fderiv_sub_le μ (hs.inter (t_meas n)) f' (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _))], assume x hx, have I : |(f' x).det| ≤ |(A n).det| + ε := calc |(f' x).det| = |(A n).det + ((f' x).det - (A n).det)| : by { congr' 1, abel } ... ≤ |(A n).det| + |(f' x).det - (A n).det| : abs_add _ _ ... ≤ |(A n).det| + ε : add_le_add le_rfl ((hδ (A n)).2.1 _ hx), calc ennreal.of_real (|(f' x).det|) ≤ ennreal.of_real (|(A n).det| + ε) : ennreal.of_real_le_of_real I ... = ennreal.of_real (|(A n).det|) + ε : by simp only [ennreal.of_real_add, abs_nonneg, nnreal.zero_le_coe, ennreal.of_real_coe_nnreal] end ... = ∑' n, (ennreal.of_real (|(A n).det|) * μ (s ∩ t n) + ε * μ (s ∩ t n)) : by simp only [measurable_const, lintegral_const, lintegral_add, measurable_set.univ, eq_self_iff_true, measure.restrict_apply, univ_inter] ... ≤ ∑' n, ((μ (f '' (s ∩ t n)) + ε * μ (s ∩ t n)) + ε * μ (s ∩ t n)) : begin refine ennreal.tsum_le_tsum (λ n, add_le_add _ le_rfl), exact (hδ (A n)).2.2 _ _ (ht n), end ... = μ (f '' s) + 2 * ε * μ s : begin conv_rhs { rw s_eq }, rw [image_Union, measure_Union], rotate, { assume i j hij, apply (disjoint.image _ hf (inter_subset_left _ _) (inter_subset_left _ _)), exact disjoint.mono (inter_subset_right _ _) (inter_subset_right _ _) (t_disj i j hij) }, { assume i, exact measurable_image_of_fderiv_within (hs.inter (t_meas i)) (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _)) (hf.mono (inter_subset_left _ _)) }, rw measure_Union, rotate, { exact pairwise_disjoint.mono t_disj (λ i, inter_subset_right _ _) }, { exact λ i, hs.inter (t_meas i) }, rw [← ennreal.tsum_mul_left, ← ennreal.tsum_add], congr' 1, ext1 i, rw [mul_assoc, two_mul, add_assoc], end end lemma lintegral_abs_det_fderiv_le_add_haar_image_aux2 (hs : measurable_set s) (h's : μ s ≠ ∞) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) : ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ ≤ μ (f '' s) := begin /- We just need to let the error tend to `0` in the previous lemma. -/ have : tendsto (λ (ε : ℝ≥0), μ (f '' s) + 2 * ε * μ s) (𝓝[>] 0) (𝓝 (μ (f '' s) + 2 * (0 : ℝ≥0) * μ s)), { apply tendsto.mono_left _ nhds_within_le_nhds, refine tendsto_const_nhds.add _, refine ennreal.tendsto.mul_const _ (or.inr h's), exact ennreal.tendsto.const_mul (ennreal.tendsto_coe.2 tendsto_id) (or.inr ennreal.coe_ne_top) }, simp only [add_zero, zero_mul, mul_zero, ennreal.coe_zero] at this, apply ge_of_tendsto this, filter_upwards [self_mem_nhds_within], rintros ε (εpos : 0 < ε), exact lintegral_abs_det_fderiv_le_add_haar_image_aux1 μ hs hf' hf εpos end lemma lintegral_abs_det_fderiv_le_add_haar_image (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) : ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ ≤ μ (f '' s) := begin /- We already know the result for finite-measure sets. We cover `s` by finite-measure sets using `spanning_sets μ`, and apply the previous result to each of these parts. -/ let u := λ n, disjointed (spanning_sets μ) n, have u_meas : ∀ n, measurable_set (u n), { assume n, apply measurable_set.disjointed (λ i, _), exact measurable_spanning_sets μ i }, have A : s = ⋃ n, s ∩ u n, by rw [← inter_Union, Union_disjointed, Union_spanning_sets, inter_univ], calc ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ = ∑' n, ∫⁻ x in s ∩ u n, ennreal.of_real (|(f' x).det|) ∂μ : begin conv_lhs { rw A }, rw lintegral_Union, { assume n, exact hs.inter (u_meas n) }, { exact pairwise_disjoint.mono (disjoint_disjointed _) (λ n, inter_subset_right _ _) } end ... ≤ ∑' n, μ (f '' (s ∩ u n)) : begin apply ennreal.tsum_le_tsum (λ n, _), apply lintegral_abs_det_fderiv_le_add_haar_image_aux2 μ (hs.inter (u_meas n)) _ (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _)) (hf.mono (inter_subset_left _ _)), have : μ (u n) < ∞ := lt_of_le_of_lt (measure_mono (disjointed_subset _ _)) (measure_spanning_sets_lt_top μ n), exact ne_of_lt (lt_of_le_of_lt (measure_mono (inter_subset_right _ _)) this), end ... = μ (f '' s) : begin conv_rhs { rw [A, image_Union] }, rw measure_Union, { assume i j hij, apply disjoint.image _ hf (inter_subset_left _ _) (inter_subset_left _ _), exact disjoint.mono (inter_subset_right _ _) (inter_subset_right _ _) (disjoint_disjointed _ i j hij) }, { assume i, exact measurable_image_of_fderiv_within (hs.inter (u_meas i)) (λ x hx, (hf' x hx.1).mono (inter_subset_left _ _)) (hf.mono (inter_subset_left _ _)) }, end end /-- Change of variable formula for differentiable functions, set version: if a function `f` is injective and differentiable on a measurable set `s`, then the measure of `f '' s` is given by the integral of `|(f' x).det|` on `s`. Note that the measurability of `f '' s` is given by `measurable_image_of_fderiv_within`. -/ theorem lintegral_abs_det_fderiv_eq_add_haar_image (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) : ∫⁻ x in s, ennreal.of_real (|(f' x).det|) ∂μ = μ (f '' s) := le_antisymm (lintegral_abs_det_fderiv_le_add_haar_image μ hs hf' hf) (add_haar_image_le_lintegral_abs_det_fderiv μ hs hf') /-- Change of variable formula for differentiable functions, set version: if a function `f` is injective and differentiable on a measurable set `s`, then the pushforward of the measure with density `|(f' x).det|` on `s` is the Lebesgue measure on the image set. This version requires that `f` is measurable, as otherwise `measure.map f` is zero per our definitions. For a version without measurability assumption but dealing with the restricted function `s.restrict f`, see `restrict_map_with_density_abs_det_fderiv_eq_add_haar`. -/ theorem map_with_density_abs_det_fderiv_eq_add_haar (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) (h'f : measurable f) : measure.map f ((μ.restrict s).with_density (λ x, ennreal.of_real (|(f' x).det|))) = μ.restrict (f '' s) := begin apply measure.ext (λ t ht, _), rw [map_apply h'f ht, with_density_apply _ (h'f ht), measure.restrict_apply ht, restrict_restrict (h'f ht), lintegral_abs_det_fderiv_eq_add_haar_image μ ((h'f ht).inter hs) (λ x hx, (hf' x hx.2).mono (inter_subset_right _ _)) (hf.mono (inter_subset_right _ _)), image_preimage_inter] end /-- Change of variable formula for differentiable functions, set version: if a function `f` is injective and differentiable on a measurable set `s`, then the pushforward of the measure with density `|(f' x).det|` on `s` is the Lebesgue measure on the image set. This version is expressed in terms of the restricted function `s.restrict f`. For a version for the original function, but with a measurability assumption, see `map_with_density_abs_det_fderiv_eq_add_haar`. -/ theorem restrict_map_with_density_abs_det_fderiv_eq_add_haar (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) : measure.map (s.restrict f) (comap coe (μ.with_density (λ x, ennreal.of_real (|(f' x).det|)))) = μ.restrict (f '' s) := begin obtain ⟨u, u_meas, uf⟩ : ∃ u, measurable u ∧ eq_on u f s, { classical, refine ⟨piecewise s f 0, _, piecewise_eq_on _ _ _⟩, refine continuous_on.measurable_piecewise _ continuous_zero.continuous_on hs, have : differentiable_on ℝ f s := λ x hx, (hf' x hx).differentiable_within_at, exact this.continuous_on }, have u' : ∀ x ∈ s, has_fderiv_within_at u (f' x) s x := λ x hx, (hf' x hx).congr (λ y hy, uf hy) (uf hx), set F : s → E := u ∘ coe with hF, have A : measure.map F (comap coe (μ.with_density (λ x, ennreal.of_real (|(f' x).det|)))) = μ.restrict (u '' s), { rw [hF, ← measure.map_map u_meas measurable_subtype_coe, map_comap_subtype_coe hs, restrict_with_density hs], exact map_with_density_abs_det_fderiv_eq_add_haar μ hs u' (hf.congr uf.symm) u_meas }, rw uf.image_eq at A, have : F = s.restrict f, { ext x, exact uf x.2 }, rwa this at A, end /-! ### Change of variable formulas in integrals -/ /- Change of variable formula for differentiable functions: if a function `f` is injective and differentiable on a measurable set `s`, then the Lebesgue integral of a function `g : E → ℝ≥0∞` on `f '' s` coincides with the integral of `|(f' x).det| * g ∘ f` on `s`. Note that the measurability of `f '' s` is given by `measurable_image_of_fderiv_within`. -/ theorem lintegral_image_eq_lintegral_abs_det_fderiv_mul (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) (g : E → ℝ≥0∞) : ∫⁻ x in f '' s, g x ∂μ = ∫⁻ x in s, ennreal.of_real (|(f' x).det|) * g (f x) ∂μ := begin rw [← restrict_map_with_density_abs_det_fderiv_eq_add_haar μ hs hf' hf, (measurable_embedding_of_fderiv_within hs hf' hf).lintegral_map], have : ∀ (x : s), g (s.restrict f x) = (g ∘ f) x := λ x, rfl, simp only [this], rw [← (measurable_embedding.subtype_coe hs).lintegral_map, map_comap_subtype_coe hs, set_lintegral_with_density_eq_set_lintegral_mul_non_measurable₀ _ _ _ hs], { refl }, { simp only [eventually_true, ennreal.of_real_lt_top] }, { exact ae_measurable_of_real_abs_det_fderiv_within μ hs hf' } end /-- Integrability in the change of variable formula for differentiable functions: if a function `f` is injective and differentiable on a measurable set `s`, then a function `g : E → F` is integrable on `f '' s` if and only if `|(f' x).det| • g ∘ f` is integrable on `s`. -/ theorem integrable_on_image_iff_integrable_on_abs_det_fderiv_smul (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) (g : E → F) : integrable_on g (f '' s) μ ↔ integrable_on (λ x, |(f' x).det| • g (f x)) s μ := begin rw [integrable_on, ← restrict_map_with_density_abs_det_fderiv_eq_add_haar μ hs hf' hf, (measurable_embedding_of_fderiv_within hs hf' hf).integrable_map_iff], change (integrable ((g ∘ f) ∘ (coe : s → E)) _) ↔ _, rw [← (measurable_embedding.subtype_coe hs).integrable_map_iff, map_comap_subtype_coe hs], simp only [ennreal.of_real], rw [restrict_with_density hs, integrable_with_density_iff_integrable_coe_smul₀, integrable_on], { congr' 2 with x, rw real.coe_to_nnreal, exact abs_nonneg _ }, { exact ae_measurable_to_nnreal_abs_det_fderiv_within μ hs hf' } end /-- Change of variable formula for differentiable functions: if a function `f` is injective and differentiable on a measurable set `s`, then the Bochner integral of a function `g : E → F` on `f '' s` coincides with the integral of `|(f' x).det| • g ∘ f` on `s`. -/ theorem integral_image_eq_integral_abs_det_fderiv_smul [complete_space F] (hs : measurable_set s) (hf' : ∀ x ∈ s, has_fderiv_within_at f (f' x) s x) (hf : inj_on f s) (g : E → F) : ∫ x in f '' s, g x ∂μ = ∫ x in s, |(f' x).det| • g (f x) ∂μ := begin rw [← restrict_map_with_density_abs_det_fderiv_eq_add_haar μ hs hf' hf, (measurable_embedding_of_fderiv_within hs hf' hf).integral_map], have : ∀ (x : s), g (s.restrict f x) = (g ∘ f) x := λ x, rfl, simp only [this, ennreal.of_real], rw [← (measurable_embedding.subtype_coe hs).integral_map, map_comap_subtype_coe hs, set_integral_with_density_eq_set_integral_smul₀ (ae_measurable_to_nnreal_abs_det_fderiv_within μ hs hf') _ hs], congr' with x, conv_rhs { rw ← real.coe_to_nnreal _ (abs_nonneg (f' x).det) }, refl end end measure_theory
84d8a8a217de4a29331f77b7d6aa9ae66657295c
865bfafdd5c31c148e891f22dba45be7ae8fe9c5
/pointed.hlean
645960c4bcf888aa9485accf6048d2b82bb20abf
[ "Apache-2.0" ]
permissive
fpvandoorn/sequential_colimits
f0eaae8e87469c41e74bbac1ba32d1772ed0e7fa
e39c3bd2efc6dfb251d334cec8c76b6aa3947c31
refs/heads/master
1,590,752,488,347
1,511,385,696,000
1,511,385,754,000
43,088,713
0
0
null
1,443,123,449,000
1,443,123,449,000
null
UTF-8
Lean
false
false
10,985
hlean
/- pointed sequential colimits -/ -- authors: Floris van Doorn, Egbert Rijke, Stefano Piceghello import .seq_colim types.fin homotopy.chain_complex types.pointed2 open seq_colim pointed algebra eq is_trunc nat is_equiv equiv sigma sigma.ops chain_complex fiber namespace seq_colim definition pseq_diagram [reducible] (A : ℕ → Type*) : Type := Πn, A n →* A (succ n) definition pseq_colim [constructor] {X : ℕ → Type*} (f : pseq_diagram X) : Type* := pointed.MK (seq_colim f) (@sι _ _ 0 pt) definition inclusion_pt {X : ℕ → Type*} (f : pseq_diagram X) (n : ℕ) : inclusion f (Point (X n)) = Point (pseq_colim f) := begin induction n with n p, reflexivity, exact (ap (sι f) (respect_pt _))⁻¹ᵖ ⬝ (!glue ⬝ p) end definition pinclusion [constructor] {X : ℕ → Type*} (f : pseq_diagram X) (n : ℕ) : X n →* pseq_colim f := pmap.mk (inclusion f) (inclusion_pt f n) definition pshift_equiv [constructor] {A : ℕ → Type*} (f : Πn, A n →* A (succ n)) : pseq_colim f ≃* pseq_colim (λn, f (n+1)) := begin fapply pequiv_of_equiv, { apply shift_equiv }, { exact ap (ι _) (respect_pt (f 0)) } end definition pshift_equiv_pinclusion {A : ℕ → Type*} (f : Πn, A n →* A (succ n)) (n : ℕ) : psquare (pinclusion f n) (pinclusion (λn, f (n+1)) n) (f n) (pshift_equiv f) := phomotopy.mk homotopy.rfl begin refine !idp_con ⬝ _, esimp, induction n with n IH, { esimp[inclusion_pt], esimp[shift_diag], exact !idp_con⁻¹ }, { esimp[inclusion_pt], refine !con_inv_cancel_left ⬝ _, rewrite ap_con, rewrite ap_con, refine _ ⬝ whisker_right _ !con.assoc, refine _ ⬝ (con.assoc (_ ⬝ _) _ _)⁻¹, xrewrite [-IH], esimp[shift_up], rewrite [elim_glue, ap_inv, -ap_compose'], esimp, rewrite [-+con.assoc], apply whisker_right, rewrite con.assoc, apply !eq_inv_con_of_con_eq, symmetry, exact eq_of_square !natural_square } end definition pseq_colim_functor [constructor] {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n →* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~ f' n ∘* g n) : pseq_colim f →* pseq_colim f' := pmap.mk (seq_colim_functor g p) (ap (ι _) (respect_pt (g _))) definition pseq_colim_pequiv' [constructor] {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n ≃* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~ f' n ∘* g n) : pseq_colim @f ≃* pseq_colim @f' := pequiv_of_equiv (seq_colim_equiv g p) (ap (ι _) (respect_pt (g _))) definition pseq_colim_pequiv [constructor] {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n ≃* A' n) (p : Πn, g (n+1) ∘* f n ~* f' n ∘* g n) : pseq_colim @f ≃* pseq_colim @f' := pseq_colim_pequiv' g (λn, @p n) -- definition seq_colim_equiv_constant [constructor] {A : ℕ → Type*} {f f' : pseq_diagram A} -- (p : Π⦃n⦄ (a : A n), f n a = f' n a) : seq_colim f ≃ seq_colim f' := -- seq_colim_equiv (λn, erfl) p definition pseq_colim_equiv_constant' [constructor] {A : ℕ → Type*} {f f' : pseq_diagram A} (p : Π⦃n⦄, f n ~ f' n) : pseq_colim @f ≃* pseq_colim @f' := pseq_colim_pequiv' (λn, pequiv.rfl) p definition pseq_colim_equiv_constant [constructor] {A : ℕ → Type*} {f f' : pseq_diagram A} (p : Πn, f n ~* f' n) : pseq_colim @f ≃* pseq_colim @f' := pseq_colim_pequiv (λn, pequiv.rfl) (λn, !pid_pcompose ⬝* p n ⬝* !pcompose_pid⁻¹*) definition pseq_colim_pequiv_pinclusion {A A' : ℕ → Type*} {f : Πn, A n →* A (n+1)} {f' : Πn, A' n →* A' (n+1)} (g : Πn, A n ≃* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~* f' n ∘* g n) (n : ℕ) : psquare (pinclusion f n) (pinclusion f' n) (g n) (pseq_colim_pequiv g p) := phomotopy.mk homotopy.rfl begin esimp, refine !idp_con ⬝ _, induction n with n IH, { esimp[inclusion_pt], exact !idp_con⁻¹ }, { esimp[inclusion_pt], rewrite [+ap_con, -+ap_inv, +con.assoc, +seq_colim_functor_glue], xrewrite[-IH], rewrite[-+ap_compose', -+con.assoc], apply whisker_right, esimp, rewrite[(eq_con_inv_of_con_eq (to_homotopy_pt (@p _)))], rewrite[ap_con], esimp, rewrite[-+con.assoc, ap_con, -ap_compose', +ap_inv], rewrite[-+con.assoc], refine _ ⬝ whisker_right _ (whisker_right _ (whisker_right _ (whisker_right _ !con.left_inv⁻¹))), rewrite[idp_con, +con.assoc], apply whisker_left, rewrite[ap_con, -ap_compose', con_inv, +con.assoc], apply whisker_left, refine eq_inv_con_of_con_eq _, symmetry, exact eq_of_square !natural_square } end definition seq_colim_equiv_constant_pinclusion {A : ℕ → Type*} {f f' : pseq_diagram A} (p : Πn, f n ~* f' n) (n : ℕ) : pseq_colim_equiv_constant p ∘* pinclusion f n ~* pinclusion f' n := begin transitivity pinclusion f' n ∘* !pid, refine phomotopy_of_psquare !pseq_colim_pequiv_pinclusion, exact !pcompose_pid end definition pseq_colim.elim' [constructor] {A : ℕ → Type*} {B : Type*} {f : pseq_diagram A} (g : Πn, A n →* B) (p : Πn, g (n+1) ∘* f n ~ g n) : pseq_colim f →* B := begin fapply pmap.mk, { intro x, induction x with n a n a, { exact g n a }, { exact p n a }}, { esimp, apply respect_pt } end definition pseq_colim.elim [constructor] {A : ℕ → Type*} {B : Type*} {f : pseq_diagram A} (g : Πn, A n →* B) (p : Πn, g (n+1) ∘* f n ~* g n) : pseq_colim @f →* B := pseq_colim.elim' g p definition pseq_colim.elim_pinclusion {A : ℕ → Type*} {B : Type*} {f : pseq_diagram A} (g : Πn, A n →* B) (p : Πn, g (n+1) ∘* f n ~* g n) (n : ℕ) : pseq_colim.elim g p ∘* pinclusion f n ~* g n := begin refine phomotopy.mk phomotopy.rfl _, refine !idp_con ⬝ _, esimp, induction n with n IH, { esimp, esimp[inclusion_pt], exact !idp_con⁻¹ }, { esimp, esimp[inclusion_pt], rewrite ap_con, rewrite ap_con, rewrite elim_glue, rewrite [-ap_inv], rewrite [-ap_compose'], esimp, rewrite [(eq_con_inv_of_con_eq (!to_homotopy_pt))], rewrite [IH], rewrite [con_inv], rewrite [-+con.assoc], refine _ ⬝ whisker_right _ !con.assoc⁻¹, rewrite [con.left_inv], esimp, refine _ ⬝ !con.assoc⁻¹, rewrite [con.left_inv], esimp, rewrite [ap_inv], rewrite [-con.assoc], refine !idp_con⁻¹ ⬝ whisker_right _ !con.left_inv⁻¹, } end definition prep0 [constructor] {A : ℕ → Type*} (f : pseq_diagram A) (k : ℕ) : A 0 →* A k := pmap.mk (rep0 (λn x, f n x) k) begin induction k with k p, reflexivity, exact ap (@f k) p ⬝ !respect_pt end definition respect_pt_prep0_succ {A : ℕ → Type*} (f : pseq_diagram A) (k : ℕ) : respect_pt (prep0 f (succ k)) = ap (@f k) (respect_pt (prep0 f k)) ⬝ respect_pt (f k) := by reflexivity theorem prep0_succ_lemma {A : ℕ → Type*} (f : pseq_diagram A) (n : ℕ) (p : rep0 (λn x, f n x) n pt = rep0 (λn x, f n x) n pt) (q : prep0 f n (Point (A 0)) = Point (A n)) : loop_equiv_eq_closed (ap (@f n) q ⬝ respect_pt (@f n)) (ap (@f n) p) = Ω→(@f n) (loop_equiv_eq_closed q p) := by rewrite [▸*, con_inv, ↑ap1_gen, +ap_con, ap_inv, +con.assoc] variables {A : ℕ → Type} (f : seq_diagram A) definition succ_add_tr_rep {n : ℕ} (k : ℕ) (x : A n) : transport A (succ_add n k) (rep f k (f x)) = rep f (succ k) x := begin induction k with k p, reflexivity, exact tr_ap A succ (succ_add n k) _ ⬝ (fn_tr_eq_tr_fn (succ_add n k) f _)⁻¹ ⬝ ap (@f _) p, end definition succ_add_tr_rep_succ {n : ℕ} (k : ℕ) (x : A n) : succ_add_tr_rep f (succ k) x = tr_ap A succ (succ_add n k) _ ⬝ (fn_tr_eq_tr_fn (succ_add n k) f _)⁻¹ ⬝ ap (@f _) (succ_add_tr_rep f k x) := by reflexivity definition code_glue_equiv [constructor] {n : ℕ} (k : ℕ) (x y : A n) : rep f k (f x) = rep f k (f y) ≃ rep f (succ k) x = rep f (succ k) y := begin refine eq_equiv_fn_eq_of_equiv (equiv_ap A (succ_add n k)) _ _ ⬝e _, apply eq_equiv_eq_closed, exact succ_add_tr_rep f k x, exact succ_add_tr_rep f k y end theorem code_glue_equiv_ap {n : ℕ} {k : ℕ} {x y : A n} (p : rep f k (f x) = rep f k (f y)) : code_glue_equiv f (succ k) x y (ap (@f _) p) = ap (@f _) (code_glue_equiv f k x y p) := begin rewrite [▸*, +ap_con, ap_inv, +succ_add_tr_rep_succ, con_inv, inv_con_inv_right, +con.assoc], apply whisker_left, rewrite [- +con.assoc], apply whisker_right, rewrite [- +ap_compose'], note s := (eq_top_of_square (natural_square_tr (λx, fn_tr_eq_tr_fn (succ_add n k) f x ⬝ (tr_ap A succ (succ_add n k) (f x))⁻¹) p))⁻¹ᵖ, rewrite [inv_con_inv_right at s, -con.assoc at s], exact s end definition pseq_colim_loop {X : ℕ → Type*} (f : Πn, X n →* X (n+1)) : Ω (pseq_colim f) ≃* pseq_colim (λn, Ω→(f n)) := begin fapply pequiv_of_equiv, { refine !seq_colim_eq_equiv0 ⬝e _, fapply seq_colim_equiv, { intro n, exact loop_equiv_eq_closed (respect_pt (prep0 f n)) }, { intro n p, apply prep0_succ_lemma }}, { reflexivity } end definition pseq_colim_loop_pinclusion {X : ℕ → Type*} (f : Πn, X n →* X (n+1)) (n : ℕ) : pseq_colim_loop f ∘* Ω→ (pinclusion f n) ~* pinclusion (λn, Ω→(f n)) n := sorry definition pseq_diagram_pfiber {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n →* A' n) (p : Πn, g (succ n) ∘* f n ~* f' n ∘* g n) : pseq_diagram (λk, pfiber (g k)) := λk, pfiber_functor (f k) (f' k) (p k) definition pfiber_pseq_colim_functor {A A' : ℕ → Type*} {f : pseq_diagram A} {f' : pseq_diagram A'} (g : Πn, A n →* A' n) (p : Π⦃n⦄, g (n+1) ∘* f n ~* f' n ∘* g n) : pfiber (pseq_colim_functor g p) ≃* pseq_colim (pseq_diagram_pfiber g p) := sorry -- open succ_str -- definition pseq_colim_succ_str_change_index' {N : succ_str} {B : N → Type*} (n : N) (m : ℕ) -- (h : Πn, B n →* B (S n)) : -- pseq_colim (λk, h (n +' (m + succ k))) ≃* pseq_colim (λk, h (S n +' (m + k))) := -- sorry -- definition pseq_colim_succ_str_change_index {N : succ_str} {B : ℕ → N → Type*} (n : N) -- (h : Π(k : ℕ) n, B k n →* B k (S n)) : -- pseq_colim (λk, h k (n +' succ k)) ≃* pseq_colim (λk, h k (S n +' k)) := -- sorry -- definition pseq_colim_index_eq_general {N : succ_str} (B : N → Type*) (f g : ℕ → N) (p : f ~ g) -- (pf : Πn, S (f n) = f (n+1)) (pg : Πn, S (g n) = g (n+1)) (h : Πn, B n →* B (S n)) : -- @pseq_colim (λn, B (f n)) (λn, ptransport B (pf n) ∘* h (f n)) ≃* -- @pseq_colim (λn, B (g n)) (λn, ptransport B (pg n) ∘* h (g n)) := -- sorry end seq_colim
c60a639e7bea85143b43c4b6dbe9c94204991e56
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/order/liminf_limsup.lean
e4356cb7f67b21fb346f1813e7cd987bc1d3ce74
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
17,275
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel, Johannes Hölzl -/ import order.filter.partial import order.filter.at_top_bot /-! # liminfs and limsups of functions and filters Defines the Liminf/Limsup of a function taking values in a conditionally complete lattice, with respect to an arbitrary filter. We define `f.Limsup` (`f.Liminf`) where `f` is a filter taking values in a conditionally complete lattice. `f.Limsup` is the smallest element `a` such that, eventually, `u ≤ a` (and vice versa for `f.Liminf`). To work with the Limsup along a function `u` use `(f.map u).Limsup`. Usually, one defines the Limsup as `Inf (Sup s)` where the Inf is taken over all sets in the filter. For instance, in ℕ along a function `u`, this is `Inf_n (Sup_{k ≥ n} u k)` (and the latter quantity decreases with `n`, so this is in fact a limit.). There is however a difficulty: it is well possible that `u` is not bounded on the whole space, only eventually (think of `Limsup (λx, 1/x)` on ℝ. Then there is no guarantee that the quantity above really decreases (the value of the `Sup` beforehand is not really well defined, as one can not use ∞), so that the Inf could be anything. So one can not use this `Inf Sup ...` definition in conditionally complete lattices, and one has to use a less tractable definition. In conditionally complete lattices, the definition is only useful for filters which are eventually bounded above (otherwise, the Limsup would morally be +∞, which does not belong to the space) and which are frequently bounded below (otherwise, the Limsup would morally be -∞, which is not in the space either). We start with definitions of these concepts for arbitrary filters, before turning to the definitions of Limsup and Liminf. In complete lattices, however, it coincides with the `Inf Sup` definition. -/ open filter set open_locale filter variables {α : Type*} {β : Type*} namespace filter section relation /-- `f.is_bounded (≺)`: the filter `f` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. `r` will be usually instantiated with `≤` or `≥`. -/ def is_bounded (r : α → α → Prop) (f : filter α) := ∃ b, ∀ᶠ x in f, r x b /-- `f.is_bounded_under (≺) u`: the image of the filter `f` under `u` is eventually bounded w.r.t. the relation `≺`, i.e. eventually, it is bounded by some uniform bound. -/ def is_bounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (f.map u).is_bounded r variables {r : α → α → Prop} {f g : filter α} /-- `f` is eventually bounded if and only if, there exists an admissible set on which it is bounded. -/ lemma is_bounded_iff : f.is_bounded r ↔ (∃s∈f.sets, ∃b, s ⊆ {x | r x b}) := iff.intro (assume ⟨b, hb⟩, ⟨{a | r a b}, hb, b, subset.refl _⟩) (assume ⟨s, hs, b, hb⟩, ⟨b, mem_sets_of_superset hs hb⟩) /-- A bounded function `u` is in particular eventually bounded. -/ lemma is_bounded_under_of {f : filter β} {u : β → α} : (∃b, ∀x, r (u x) b) → f.is_bounded_under r u | ⟨b, hb⟩ := ⟨b, show ∀ᶠ x in f, r (u x) b, from eventually_of_forall hb⟩ lemma is_bounded_bot : is_bounded r ⊥ ↔ nonempty α := by simp [is_bounded, exists_true_iff_nonempty] lemma is_bounded_top : is_bounded r ⊤ ↔ (∃t, ∀x, r x t) := by simp [is_bounded, eq_univ_iff_forall] lemma is_bounded_principal (s : set α) : is_bounded r (𝓟 s) ↔ (∃t, ∀x∈s, r x t) := by simp [is_bounded, subset_def] lemma is_bounded_sup [is_trans α r] (hr : ∀b₁ b₂, ∃b, r b₁ b ∧ r b₂ b) : is_bounded r f → is_bounded r g → is_bounded r (f ⊔ g) | ⟨b₁, h₁⟩ ⟨b₂, h₂⟩ := let ⟨b, rb₁b, rb₂b⟩ := hr b₁ b₂ in ⟨b, eventually_sup.mpr ⟨h₁.mono (λ x h, trans h rb₁b), h₂.mono (λ x h, trans h rb₂b)⟩⟩ lemma is_bounded.mono (h : f ≤ g) : is_bounded r g → is_bounded r f | ⟨b, hb⟩ := ⟨b, h hb⟩ lemma is_bounded_under.mono {f g : filter β} {u : β → α} (h : f ≤ g) : g.is_bounded_under r u → f.is_bounded_under r u := λ hg, hg.mono (map_mono h) lemma is_bounded.is_bounded_under {q : β → β → Prop} {u : α → β} (hf : ∀a₀ a₁, r a₀ a₁ → q (u a₀) (u a₁)) : f.is_bounded r → f.is_bounded_under q u | ⟨b, h⟩ := ⟨u b, show ∀ᶠ x in f, q (u x) (u b), from h.mono (λ x, hf x b)⟩ /-- `is_cobounded (≺) f` states that the filter `f` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. There is a subtlety in this definition: we want `f.is_cobounded` to hold for any `f` in the case of complete lattices. This will be relevant to deduce theorems on complete lattices from their versions on conditionally complete lattices with additional assumptions. We have to be careful in the edge case of the trivial filter containing the empty set: the other natural definition `¬ ∀ a, ∀ᶠ n in f, a ≤ n` would not work as well in this case. -/ def is_cobounded (r : α → α → Prop) (f : filter α) := ∃b, ∀a, (∀ᶠ x in f, r x a) → r b a /-- `is_cobounded_under (≺) f u` states that the image of the filter `f` under the map `u` does not tend to infinity w.r.t. `≺`. This is also called frequently bounded. Will be usually instantiated with `≤` or `≥`. -/ def is_cobounded_under (r : α → α → Prop) (f : filter β) (u : β → α) := (f.map u).is_cobounded r /-- To check that a filter is frequently bounded, it suffices to have a witness which bounds `f` at some point for every admissible set. This is only an implication, as the other direction is wrong for the trivial filter.-/ lemma is_cobounded.mk [is_trans α r] (a : α) (h : ∀s∈f, ∃x∈s, r a x) : f.is_cobounded r := ⟨a, assume y s, let ⟨x, h₁, h₂⟩ := h _ s in trans h₂ h₁⟩ /-- A filter which is eventually bounded is in particular frequently bounded (in the opposite direction). At least if the filter is not trivial. -/ lemma is_bounded.is_cobounded_flip [is_trans α r] [ne_bot f] : f.is_bounded r → f.is_cobounded (flip r) | ⟨a, ha⟩ := ⟨a, assume b hb, let ⟨x, rxa, rbx⟩ := (ha.and hb).exists in show r b a, from trans rbx rxa⟩ lemma is_cobounded_bot : is_cobounded r ⊥ ↔ (∃b, ∀x, r b x) := by simp [is_cobounded] lemma is_cobounded_top : is_cobounded r ⊤ ↔ nonempty α := by simp [is_cobounded, eq_univ_iff_forall, exists_true_iff_nonempty] {contextual := tt} lemma is_cobounded_principal (s : set α) : (𝓟 s).is_cobounded r ↔ (∃b, ∀a, (∀x∈s, r x a) → r b a) := by simp [is_cobounded, subset_def] lemma is_cobounded.mono (h : f ≤ g) : f.is_cobounded r → g.is_cobounded r | ⟨b, hb⟩ := ⟨b, assume a ha, hb a (h ha)⟩ end relation lemma is_cobounded_le_of_bot [order_bot α] {f : filter α} : f.is_cobounded (≤) := ⟨⊥, assume a h, bot_le⟩ lemma is_cobounded_ge_of_top [order_top α] {f : filter α} : f.is_cobounded (≥) := ⟨⊤, assume a h, le_top⟩ lemma is_bounded_le_of_top [order_top α] {f : filter α} : f.is_bounded (≤) := ⟨⊤, eventually_of_forall $ λ _, le_top⟩ lemma is_bounded_ge_of_bot [order_bot α] {f : filter α} : f.is_bounded (≥) := ⟨⊥, eventually_of_forall $ λ _, bot_le⟩ lemma is_bounded_under_sup [semilattice_sup α] {f : filter β} {u v : β → α} : f.is_bounded_under (≤) u → f.is_bounded_under (≤) v → f.is_bounded_under (≤) (λa, u a ⊔ v a) | ⟨bu, (hu : ∀ᶠ x in f, u x ≤ bu)⟩ ⟨bv, (hv : ∀ᶠ x in f, v x ≤ bv)⟩ := ⟨bu ⊔ bv, show ∀ᶠ x in f, u x ⊔ v x ≤ bu ⊔ bv, by filter_upwards [hu, hv] assume x, sup_le_sup⟩ lemma is_bounded_under_inf [semilattice_inf α] {f : filter β} {u v : β → α} : f.is_bounded_under (≥) u → f.is_bounded_under (≥) v → f.is_bounded_under (≥) (λa, u a ⊓ v a) | ⟨bu, (hu : ∀ᶠ x in f, u x ≥ bu)⟩ ⟨bv, (hv : ∀ᶠ x in f, v x ≥ bv)⟩ := ⟨bu ⊓ bv, show ∀ᶠ x in f, u x ⊓ v x ≥ bu ⊓ bv, by filter_upwards [hu, hv] assume x, inf_le_inf⟩ /-- Filters are automatically bounded or cobounded in complete lattices. To use the same statements in complete and conditionally complete lattices but let automation fill automatically the boundedness proofs in complete lattices, we use the tactic `is_bounded_default` in the statements, in the form `(hf : f.is_bounded (≥) . is_bounded_default)`. -/ meta def is_bounded_default : tactic unit := tactic.applyc ``is_cobounded_le_of_bot <|> tactic.applyc ``is_cobounded_ge_of_top <|> tactic.applyc ``is_bounded_le_of_top <|> tactic.applyc ``is_bounded_ge_of_bot section conditionally_complete_lattice variables [conditionally_complete_lattice α] /-- The `Limsup` of a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `x ≤ a`. -/ def Limsup (f : filter α) : α := Inf { a | ∀ᶠ n in f, n ≤ a } /-- The `Liminf` of a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `x ≥ a`. -/ def Liminf (f : filter α) : α := Sup { a | ∀ᶠ n in f, a ≤ n } /-- The `limsup` of a function `u` along a filter `f` is the infimum of the `a` such that, eventually for `f`, holds `u x ≤ a`. -/ def limsup (f : filter β) (u : β → α) : α := (f.map u).Limsup /-- The `liminf` of a function `u` along a filter `f` is the supremum of the `a` such that, eventually for `f`, holds `u x ≥ a`. -/ def liminf (f : filter β) (u : β → α) : α := (f.map u).Liminf section variables {f : filter β} {u : β → α} theorem limsup_eq : f.limsup u = Inf { a | ∀ᶠ n in f, u n ≤ a } := rfl theorem liminf_eq : f.liminf u = Sup { a | ∀ᶠ n in f, a ≤ u n } := rfl end theorem Limsup_le_of_le {f : filter α} {a} (hf : f.is_cobounded (≤) . is_bounded_default) (h : ∀ᶠ n in f, n ≤ a) : f.Limsup ≤ a := cInf_le hf h theorem le_Liminf_of_le {f : filter α} {a} (hf : f.is_cobounded (≥) . is_bounded_default) (h : ∀ᶠ n in f, a ≤ n) : a ≤ f.Liminf := le_cSup hf h theorem le_Limsup_of_le {f : filter α} {a} (hf : f.is_bounded (≤) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, n ≤ b) → a ≤ b) : a ≤ f.Limsup := le_cInf hf h theorem Liminf_le_of_le {f : filter α} {a} (hf : f.is_bounded (≥) . is_bounded_default) (h : ∀ b, (∀ᶠ n in f, b ≤ n) → b ≤ a) : f.Liminf ≤ a := cSup_le hf h theorem Liminf_le_Limsup {f : filter α} [ne_bot f] (h₁ : f.is_bounded (≤) . is_bounded_default) (h₂ : f.is_bounded (≥) . is_bounded_default) : f.Liminf ≤ f.Limsup := Liminf_le_of_le h₂ $ assume a₀ ha₀, le_Limsup_of_le h₁ $ assume a₁ ha₁, show a₀ ≤ a₁, from let ⟨b, hb₀, hb₁⟩ := (ha₀.and ha₁).exists in le_trans hb₀ hb₁ lemma Liminf_le_Liminf {f g : filter α} (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) (h : ∀ a, (∀ᶠ n in f, a ≤ n) → ∀ᶠ n in g, a ≤ n) : f.Liminf ≤ g.Liminf := cSup_le_cSup hg hf h lemma Limsup_le_Limsup {f g : filter α} (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) (h : ∀ a, (∀ᶠ n in g, n ≤ a) → ∀ᶠ n in f, n ≤ a) : f.Limsup ≤ g.Limsup := cInf_le_cInf hf hg h lemma Limsup_le_Limsup_of_le {f g : filter α} (h : f ≤ g) (hf : f.is_cobounded (≤) . is_bounded_default) (hg : g.is_bounded (≤) . is_bounded_default) : f.Limsup ≤ g.Limsup := Limsup_le_Limsup hf hg (assume a ha, h ha) lemma Liminf_le_Liminf_of_le {f g : filter α} (h : g ≤ f) (hf : f.is_bounded (≥) . is_bounded_default) (hg : g.is_cobounded (≥) . is_bounded_default) : f.Liminf ≤ g.Liminf := Liminf_le_Liminf hf hg (assume a ha, h ha) lemma limsup_le_limsup {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : u ≤ᶠ[f] v) (hu : f.is_cobounded_under (≤) u . is_bounded_default) (hv : f.is_bounded_under (≤) v . is_bounded_default) : f.limsup u ≤ f.limsup v := Limsup_le_Limsup hu hv $ assume b, h.trans lemma liminf_le_liminf {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a ≤ v a) (hu : f.is_bounded_under (≥) u . is_bounded_default) (hv : f.is_cobounded_under (≥) v . is_bounded_default) : f.liminf u ≤ f.liminf v := @limsup_le_limsup (order_dual β) α _ _ _ _ h hv hu theorem Limsup_principal {s : set α} (h : bdd_above s) (hs : s.nonempty) : (𝓟 s).Limsup = Sup s := by simp [Limsup]; exact cInf_upper_bounds_eq_cSup h hs theorem Liminf_principal {s : set α} (h : bdd_below s) (hs : s.nonempty) : (𝓟 s).Liminf = Inf s := @Limsup_principal (order_dual α) _ s h hs lemma limsup_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : limsup f u = limsup f v := begin rw limsup_eq, congr' with b, exact eventually_congr (h.mono $ λ x hx, by simp [hx]) end lemma liminf_congr {α : Type*} [conditionally_complete_lattice β] {f : filter α} {u v : α → β} (h : ∀ᶠ a in f, u a = v a) : liminf f u = liminf f v := @limsup_congr (order_dual β) _ _ _ _ _ h lemma limsup_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : limsup f (λ x, b) = b := begin rw limsup_eq, apply le_antisymm, { exact cInf_le ⟨b, λ a, eventually_const.1⟩ (eventually_le.refl _ _) }, { exact le_cInf ⟨b, eventually_le.refl _ _⟩ (λ a, eventually_const.1) } end lemma liminf_const {α : Type*} [conditionally_complete_lattice β] {f : filter α} [ne_bot f] (b : β) : liminf f (λ x, b) = b := @limsup_const (order_dual β) α _ f _ b end conditionally_complete_lattice section complete_lattice variables [complete_lattice α] @[simp] theorem Limsup_bot : (⊥ : filter α).Limsup = ⊥ := bot_unique $ Inf_le $ by simp @[simp] theorem Liminf_bot : (⊥ : filter α).Liminf = ⊤ := top_unique $ le_Sup $ by simp @[simp] theorem Limsup_top : (⊤ : filter α).Limsup = ⊤ := top_unique $ le_Inf $ by simp [eq_univ_iff_forall]; exact assume b hb, (top_unique $ hb _) @[simp] theorem Liminf_top : (⊤ : filter α).Liminf = ⊥ := bot_unique $ Sup_le $ by simp [eq_univ_iff_forall]; exact assume b hb, (bot_unique $ hb _) lemma liminf_le_limsup {f : filter β} [ne_bot f] {u : β → α} : liminf f u ≤ limsup f u := Liminf_le_Limsup is_bounded_le_of_top is_bounded_ge_of_bot theorem has_basis.Limsup_eq_infi_Sup {ι} {p : ι → Prop} {s} {f : filter α} (h : f.has_basis p s) : f.Limsup = ⨅ i (hi : p i), Sup (s i) := le_antisymm (le_binfi $ λ i hi, Inf_le $ h.eventually_iff.2 ⟨i, hi, λ x, le_Sup⟩) (le_Inf $ assume a ha, let ⟨i, hi, ha⟩ := h.eventually_iff.1 ha in infi_le_of_le _ $ infi_le_of_le hi $ Sup_le ha) theorem Limsup_eq_infi_Sup {f : filter α} : f.Limsup = ⨅ s ∈ f, Sup s := f.basis_sets.Limsup_eq_infi_Sup /-- In a complete lattice, the limsup of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem limsup_eq_infi_supr {f : filter β} {u : β → α} : f.limsup u = ⨅ s ∈ f, ⨆ a ∈ s, u a := (f.basis_sets.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, id] lemma limsup_eq_infi_supr_of_nat {u : ℕ → α} : limsup at_top u = ⨅n:ℕ, ⨆i≥n, u i := (at_top_basis.map u).Limsup_eq_infi_Sup.trans $ by simp only [Sup_image, infi_const]; refl lemma limsup_eq_infi_supr_of_nat' {u : ℕ → α} : limsup at_top u = ⨅n:ℕ, ⨆i, u (i + n) := by simp only [limsup_eq_infi_supr_of_nat, supr_ge_eq_supr_nat_add] theorem Liminf_eq_supr_Inf {f : filter α} : f.Liminf = ⨆ s ∈ f, Inf s := @Limsup_eq_infi_Sup (order_dual α) _ _ /-- In a complete lattice, the liminf of a function is the infimum over sets `s` in the filter of the supremum of the function over `s` -/ theorem liminf_eq_supr_infi {f : filter β} {u : β → α} : f.liminf u = ⨆ s ∈ f, ⨅ a ∈ s, u a := @limsup_eq_infi_supr (order_dual α) β _ _ _ lemma liminf_eq_supr_infi_of_nat {u : ℕ → α} : liminf at_top u = ⨆n:ℕ, ⨅i≥n, u i := @limsup_eq_infi_supr_of_nat (order_dual α) _ u lemma liminf_eq_supr_infi_of_nat' {u : ℕ → α} : liminf at_top u = ⨆n:ℕ, ⨅i, u (i + n) := @limsup_eq_infi_supr_of_nat' (order_dual α) _ _ end complete_lattice section conditionally_complete_linear_order lemma eventually_lt_of_lt_liminf {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : b < liminf f u) (hu : f.is_bounded_under (≥) u . is_bounded_default) : ∀ᶠ a in f, b < u a := begin obtain ⟨c, hc, hbc⟩ : ∃ (c : β) (hc : c ∈ {c : β | ∀ᶠ (n : α) in f, c ≤ u n}), b < c := exists_lt_of_lt_cSup hu h, exact hc.mono (λ x hx, lt_of_lt_of_le hbc hx) end lemma eventually_lt_of_limsup_lt {f : filter α} [conditionally_complete_linear_order β] {u : α → β} {b : β} (h : limsup f u < b) (hu : f.is_bounded_under (≤) u . is_bounded_default) : ∀ᶠ a in f, u a < b := @eventually_lt_of_lt_liminf _ (order_dual β) _ _ _ _ h hu end conditionally_complete_linear_order end filter
c92f308b1e01bf329d8171ecf1df92dd5b2e9874
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/data/ordmap/ordset.lean
81c13cc09814f31a138454c26cb11ec0a0fc115c
[ "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
69,868
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.ordmap.ordnode import algebra.order.ring import data.nat.dist import tactic.linarith /-! # Verification of the `ordnode α` datatype This file proves the correctness of the operations in `data.ordmap.ordnode`. The public facing version is the type `ordset α`, which is a wrapper around `ordnode α` which includes the correctness invariant of the type, and it exposes parallel operations like `insert` as functions on `ordset` that do the same thing but bundle the correctness proofs. The advantage is that it is possible to, for example, prove that the result of `find` on `insert` will actually find the element, while `ordnode` cannot guarantee this if the input tree did not satisfy the type invariants. ## Main definitions * `ordset α`: A well formed set of values of type `α` ## Implementation notes The majority of this file is actually in the `ordnode` namespace, because we first have to prove the correctness of all the operations (and defining what correctness means here is actually somewhat subtle). So all the actual `ordset` operations are at the very end, once we have all the theorems. An `ordnode α` is an inductive type which describes a tree which stores the `size` at internal nodes. The correctness invariant of an `ordnode α` is: * `ordnode.sized t`: All internal `size` fields must match the actual measured size of the tree. (This is not hard to satisfy.) * `ordnode.balanced t`: Unless the tree has the form `()` or `((a) b)` or `(a (b))` (that is, nil or a single singleton subtree), the two subtrees must satisfy `size l ≤ δ * size r` and `size r ≤ δ * size l`, where `δ := 3` is a global parameter of the data structure (and this property must hold recursively at subtrees). This is why we say this is a "size balanced tree" data structure. * `ordnode.bounded lo hi t`: The members of the tree must be in strictly increasing order, meaning that if `a` is in the left subtree and `b` is the root, then `a ≤ b` and `¬ (b ≤ a)`. We enforce this using `ordnode.bounded` which includes also a global upper and lower bound. Because the `ordnode` file was ported from Haskell, the correctness invariants of some of the functions have not been spelled out, and some theorems like `ordnode.valid'.balance_l_aux` show very intricate assumptions on the sizes, which may need to be revised if it turns out some operations violate these assumptions, because there is a decent amount of slop in the actual data structure invariants, so the theorem will go through with multiple choices of assumption. **Note:** This file is incomplete, in the sense that the intent is to have verified versions and lemmas about all the definitions in `ordnode.lean`, but at the moment only a few operations are verified (the hard part should be out of the way, but still). Contributors are encouraged to pick this up and finish the job, if it appeals to you. ## Tags ordered map, ordered set, data structure, verified programming -/ variable {α : Type*} namespace ordnode /-! ### delta and ratio -/ theorem not_le_delta {s} (H : 1 ≤ s) : ¬ s ≤ delta * 0 := not_le_of_gt H theorem delta_lt_false {a b : ℕ} (h₁ : delta * a < b) (h₂ : delta * b < a) : false := not_le_of_lt (lt_trans ((mul_lt_mul_left dec_trivial).2 h₁) h₂) $ by simpa [mul_assoc] using nat.mul_le_mul_right a (dec_trivial : 1 ≤ delta * delta) /-! ### `singleton` -/ /-! ### `size` and `empty` -/ /-- O(n). Computes the actual number of elements in the set, ignoring the cached `size` field. -/ def real_size : ordnode α → ℕ | nil := 0 | (node _ l _ r) := real_size l + real_size r + 1 /-! ### `sized` -/ /-- The `sized` property asserts that all the `size` fields in nodes match the actual size of the respective subtrees. -/ def sized : ordnode α → Prop | nil := true | (node s l _ r) := s = size l + size r + 1 ∧ sized l ∧ sized r theorem sized.node' {l x r} (hl : @sized α l) (hr : sized r) : sized (node' l x r) := ⟨rfl, hl, hr⟩ theorem sized.eq_node' {s l x r} (h : @sized α (node s l x r)) : node s l x r = node' l x r := by rw h.1; refl theorem sized.size_eq {s l x r} (H : sized (@node α s l x r)) : size (@node α s l x r) = size l + size r + 1 := H.1 @[elab_as_eliminator] theorem sized.induction {t} (hl : @sized α t) {C : ordnode α → Prop} (H0 : C nil) (H1 : ∀ l x r, C l → C r → C (node' l x r)) : C t := begin induction t, {exact H0}, rw hl.eq_node', exact H1 _ _ _ (t_ih_l hl.2.1) (t_ih_r hl.2.2) end theorem size_eq_real_size : ∀ {t : ordnode α}, sized t → size t = real_size t | nil _ := rfl | (node s l x r) ⟨h₁, h₂, h₃⟩ := by rw [size, h₁, size_eq_real_size h₂, size_eq_real_size h₃]; refl @[simp] theorem sized.size_eq_zero {t : ordnode α} (ht : sized t) : size t = 0 ↔ t = nil := by cases t; [simp, simp [ht.1]] theorem sized.pos {s l x r} (h : sized (@node α s l x r)) : 0 < s := by rw h.1; apply nat.le_add_left /-! `dual` -/ theorem dual_dual : ∀ (t : ordnode α), dual (dual t) = t | nil := rfl | (node s l x r) := by rw [dual, dual, dual_dual, dual_dual] @[simp] theorem size_dual (t : ordnode α) : size (dual t) = size t := by cases t; refl /-! `balanced` -/ /-- The `balanced_sz l r` asserts that a hypothetical tree with children of sizes `l` and `r` is balanced: either `l ≤ δ * r` and `r ≤ δ * r`, or the tree is trivial with a singleton on one side and nothing on the other. -/ def balanced_sz (l r : ℕ) : Prop := l + r ≤ 1 ∨ (l ≤ delta * r ∧ r ≤ delta * l) instance balanced_sz.dec : decidable_rel balanced_sz := λ l r, or.decidable /-- The `balanced t` asserts that the tree `t` satisfies the balance invariants (at every level). -/ def balanced : ordnode α → Prop | nil := true | (node _ l _ r) := balanced_sz (size l) (size r) ∧ balanced l ∧ balanced r instance balanced.dec : decidable_pred (@balanced α) | t := by induction t; unfold balanced; resetI; apply_instance theorem balanced_sz.symm {l r : ℕ} : balanced_sz l r → balanced_sz r l := or.imp (by rw add_comm; exact id) and.symm theorem balanced_sz_zero {l : ℕ} : balanced_sz l 0 ↔ l ≤ 1 := by simp [balanced_sz] { contextual := tt } theorem balanced_sz_up {l r₁ r₂ : ℕ} (h₁ : r₁ ≤ r₂) (h₂ : l + r₂ ≤ 1 ∨ r₂ ≤ delta * l) (H : balanced_sz l r₁) : balanced_sz l r₂ := begin refine or_iff_not_imp_left.2 (λ h, _), refine ⟨_, h₂.resolve_left h⟩, cases H, { cases r₂, { cases h (le_trans (nat.add_le_add_left (nat.zero_le _) _) H) }, { exact le_trans (le_trans (nat.le_add_right _ _) H) (nat.le_add_left 1 _) } }, { exact le_trans H.1 (nat.mul_le_mul_left _ h₁) } end theorem balanced_sz_down {l r₁ r₂ : ℕ} (h₁ : r₁ ≤ r₂) (h₂ : l + r₂ ≤ 1 ∨ l ≤ delta * r₁) (H : balanced_sz l r₂) : balanced_sz l r₁ := have l + r₂ ≤ 1 → balanced_sz l r₁, from λ H, or.inl (le_trans (nat.add_le_add_left h₁ _) H), or.cases_on H this (λ H, or.cases_on h₂ this (λ h₂, or.inr ⟨h₂, le_trans h₁ H.2⟩)) theorem balanced.dual : ∀ {t : ordnode α}, balanced t → balanced (dual t) | nil h := ⟨⟩ | (node s l x r) ⟨b, bl, br⟩ := ⟨by rw [size_dual, size_dual]; exact b.symm, br.dual, bl.dual⟩ /-! ### `rotate` and `balance` -/ /-- Build a tree from three nodes, left associated (ignores the invariants). -/ def node3_l (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : ordnode α := node' (node' l x m) y r /-- Build a tree from three nodes, right associated (ignores the invariants). -/ def node3_r (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : ordnode α := node' l x (node' m y r) /-- Build a tree from three nodes, with `a () b -> (a ()) b` and `a (b c) d -> ((a b) (c d))`. -/ def node4_l : ordnode α → α → ordnode α → α → ordnode α → ordnode α | l x (node _ ml y mr) z r := node' (node' l x ml) y (node' mr z r) | l x nil z r := node3_l l x nil z r -- should not happen /-- Build a tree from three nodes, with `a () b -> a (() b)` and `a (b c) d -> ((a b) (c d))`. -/ def node4_r : ordnode α → α → ordnode α → α → ordnode α → ordnode α | l x (node _ ml y mr) z r := node' (node' l x ml) y (node' mr z r) | l x nil z r := node3_r l x nil z r -- should not happen /-- Concatenate two nodes, performing a left rotation `x (y z) -> ((x y) z)` if balance is upset. -/ def rotate_l : ordnode α → α → ordnode α → ordnode α | l x (node _ m y r) := if size m < ratio * size r then node3_l l x m y r else node4_l l x m y r | l x nil := node' l x nil -- should not happen /-- Concatenate two nodes, performing a right rotation `(x y) z -> (x (y z))` if balance is upset. -/ def rotate_r : ordnode α → α → ordnode α → ordnode α | (node _ l x m) y r := if size m < ratio * size l then node3_r l x m y r else node4_r l x m y r | nil y r := node' nil y r -- should not happen /-- A left balance operation. This will rebalance a concatenation, assuming the original nodes are not too far from balanced. -/ def balance_l' (l : ordnode α) (x : α) (r : ordnode α) : ordnode α := if size l + size r ≤ 1 then node' l x r else if size l > delta * size r then rotate_r l x r else node' l x r /-- A right balance operation. This will rebalance a concatenation, assuming the original nodes are not too far from balanced. -/ def balance_r' (l : ordnode α) (x : α) (r : ordnode α) : ordnode α := if size l + size r ≤ 1 then node' l x r else if size r > delta * size l then rotate_l l x r else node' l x r /-- The full balance operation. This is the same as `balance`, but with less manual inlining. It is somewhat easier to work with this version in proofs. -/ def balance' (l : ordnode α) (x : α) (r : ordnode α) : ordnode α := if size l + size r ≤ 1 then node' l x r else if size r > delta * size l then rotate_l l x r else if size l > delta * size r then rotate_r l x r else node' l x r theorem dual_node' (l : ordnode α) (x : α) (r : ordnode α) : dual (node' l x r) = node' (dual r) x (dual l) := by simp [node', add_comm] theorem dual_node3_l (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node3_l l x m y r) = node3_r (dual r) y (dual m) x (dual l) := by simp [node3_l, node3_r, dual_node'] theorem dual_node3_r (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node3_r l x m y r) = node3_l (dual r) y (dual m) x (dual l) := by simp [node3_l, node3_r, dual_node'] theorem dual_node4_l (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node4_l l x m y r) = node4_r (dual r) y (dual m) x (dual l) := by cases m; simp [node4_l, node4_r, dual_node3_l, dual_node'] theorem dual_node4_r (l : ordnode α) (x : α) (m : ordnode α) (y : α) (r : ordnode α) : dual (node4_r l x m y r) = node4_l (dual r) y (dual m) x (dual l) := by cases m; simp [node4_l, node4_r, dual_node3_r, dual_node'] theorem dual_rotate_l (l : ordnode α) (x : α) (r : ordnode α) : dual (rotate_l l x r) = rotate_r (dual r) x (dual l) := by cases r; simp [rotate_l, rotate_r, dual_node']; split_ifs; simp [dual_node3_l, dual_node4_l] theorem dual_rotate_r (l : ordnode α) (x : α) (r : ordnode α) : dual (rotate_r l x r) = rotate_l (dual r) x (dual l) := by rw [← dual_dual (rotate_l _ _ _), dual_rotate_l, dual_dual, dual_dual] theorem dual_balance' (l : ordnode α) (x : α) (r : ordnode α) : dual (balance' l x r) = balance' (dual r) x (dual l) := begin simp [balance', add_comm], split_ifs; simp [dual_node', dual_rotate_l, dual_rotate_r], cases delta_lt_false h_1 h_2 end theorem dual_balance_l (l : ordnode α) (x : α) (r : ordnode α) : dual (balance_l l x r) = balance_r (dual r) x (dual l) := begin unfold balance_l balance_r, cases r with rs rl rx rr, { cases l with ls ll lx lr, {refl}, cases ll with lls lll llx llr; cases lr with lrs lrl lrx lrr; dsimp only [dual]; try {refl}, split_ifs; repeat {simp [h, add_comm]} }, { cases l with ls ll lx lr, {refl}, dsimp only [dual], split_ifs, swap, {simp [add_comm]}, cases ll with lls lll llx llr; cases lr with lrs lrl lrx lrr; try {refl}, dsimp only [dual], split_ifs; simp [h, add_comm] }, end theorem dual_balance_r (l : ordnode α) (x : α) (r : ordnode α) : dual (balance_r l x r) = balance_l (dual r) x (dual l) := by rw [← dual_dual (balance_l _ _ _), dual_balance_l, dual_dual, dual_dual] theorem sized.node3_l {l x m y r} (hl : @sized α l) (hm : sized m) (hr : sized r) : sized (node3_l l x m y r) := (hl.node' hm).node' hr theorem sized.node3_r {l x m y r} (hl : @sized α l) (hm : sized m) (hr : sized r) : sized (node3_r l x m y r) := hl.node' (hm.node' hr) theorem sized.node4_l {l x m y r} (hl : @sized α l) (hm : sized m) (hr : sized r) : sized (node4_l l x m y r) := by cases m; [exact (hl.node' hm).node' hr, exact (hl.node' hm.2.1).node' (hm.2.2.node' hr)] theorem node3_l_size {l x m y r} : size (@node3_l α l x m y r) = size l + size m + size r + 2 := by dsimp [node3_l, node', size]; rw add_right_comm _ 1 theorem node3_r_size {l x m y r} : size (@node3_r α l x m y r) = size l + size m + size r + 2 := by dsimp [node3_r, node', size]; rw [← add_assoc, ← add_assoc] theorem node4_l_size {l x m y r} (hm : sized m) : size (@node4_l α l x m y r) = size l + size m + size r + 2 := by cases m; simp [node4_l, node3_l, node', add_comm, add_left_comm]; [skip, simp [size, hm.1]]; rw [← add_assoc, ← bit0]; simp [add_comm, add_left_comm] theorem sized.dual : ∀ {t : ordnode α} (h : sized t), sized (dual t) | nil h := ⟨⟩ | (node s l x r) ⟨rfl, sl, sr⟩ := ⟨by simp [size_dual, add_comm], sized.dual sr, sized.dual sl⟩ theorem sized.dual_iff {t : ordnode α} : sized (dual t) ↔ sized t := ⟨λ h, by rw ← dual_dual t; exact h.dual, sized.dual⟩ theorem sized.rotate_l {l x r} (hl : @sized α l) (hr : sized r) : sized (rotate_l l x r) := begin cases r, {exact hl.node' hr}, rw rotate_l, split_ifs, { exact hl.node3_l hr.2.1 hr.2.2 }, { exact hl.node4_l hr.2.1 hr.2.2 } end theorem sized.rotate_r {l x r} (hl : @sized α l) (hr : sized r) : sized (rotate_r l x r) := sized.dual_iff.1 $ by rw dual_rotate_r; exact hr.dual.rotate_l hl.dual theorem sized.rotate_l_size {l x r} (hm : sized r) : size (@rotate_l α l x r) = size l + size r + 1 := begin cases r; simp [rotate_l], simp [size, hm.1, add_comm, add_left_comm], rw [← add_assoc, ← bit0], simp, split_ifs; simp [node3_l_size, node4_l_size hm.2.1, add_comm, add_left_comm] end theorem sized.rotate_r_size {l x r} (hl : sized l) : size (@rotate_r α l x r) = size l + size r + 1 := by rw [← size_dual, dual_rotate_r, hl.dual.rotate_l_size, size_dual, size_dual, add_comm (size l)] theorem sized.balance' {l x r} (hl : @sized α l) (hr : sized r) : sized (balance' l x r) := begin unfold balance', split_ifs, { exact hl.node' hr }, { exact hl.rotate_l hr }, { exact hl.rotate_r hr }, { exact hl.node' hr } end theorem size_balance' {l x r} (hl : @sized α l) (hr : sized r) : size (@balance' α l x r) = size l + size r + 1 := begin unfold balance', split_ifs, { refl }, { exact hr.rotate_l_size }, { exact hl.rotate_r_size }, { refl } end /-! ## `all`, `any`, `emem`, `amem` -/ theorem all.imp {P Q : α → Prop} (H : ∀ a, P a → Q a) : ∀ {t}, all P t → all Q t | nil h := ⟨⟩ | (node _ l x r) ⟨h₁, h₂, h₃⟩ := ⟨h₁.imp, H _ h₂, h₃.imp⟩ theorem any.imp {P Q : α → Prop} (H : ∀ a, P a → Q a) : ∀ {t}, any P t → any Q t | nil := id | (node _ l x r) := or.imp any.imp $ or.imp (H _) any.imp theorem all_singleton {P : α → Prop} {x : α} : all P (singleton x) ↔ P x := ⟨λ h, h.2.1, λ h, ⟨⟨⟩, h, ⟨⟩⟩⟩ theorem any_singleton {P : α → Prop} {x : α} : any P (singleton x) ↔ P x := ⟨by rintro (⟨⟨⟩⟩ | h | ⟨⟨⟩⟩); exact h, λ h, or.inr (or.inl h)⟩ theorem all_dual {P : α → Prop} : ∀ {t : ordnode α}, all P (dual t) ↔ all P t | nil := iff.rfl | (node s l x r) := ⟨λ ⟨hr, hx, hl⟩, ⟨all_dual.1 hl, hx, all_dual.1 hr⟩, λ ⟨hl, hx, hr⟩, ⟨all_dual.2 hr, hx, all_dual.2 hl⟩⟩ theorem all_iff_forall {P : α → Prop} : ∀ {t}, all P t ↔ ∀ x, emem x t → P x | nil := (iff_true_intro $ by rintro _ ⟨⟩).symm | (node _ l x r) := by simp [all, emem, all_iff_forall, any, or_imp_distrib, forall_and_distrib] theorem any_iff_exists {P : α → Prop} : ∀ {t}, any P t ↔ ∃ x, emem x t ∧ P x | nil := ⟨by rintro ⟨⟩, by rintro ⟨_, ⟨⟩, _⟩⟩ | (node _ l x r) := by simp [any, emem, any_iff_exists, or_and_distrib_right, exists_or_distrib] theorem emem_iff_all {x : α} {t} : emem x t ↔ ∀ P, all P t → P x := ⟨λ h P al, all_iff_forall.1 al _ h, λ H, H _ $ all_iff_forall.2 $ λ _, id⟩ theorem all_node' {P l x r} : @all α P (node' l x r) ↔ all P l ∧ P x ∧ all P r := iff.rfl theorem all_node3_l {P l x m y r} : @all α P (node3_l l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := by simp [node3_l, all_node', and_assoc] theorem all_node3_r {P l x m y r} : @all α P (node3_r l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := iff.rfl theorem all_node4_l {P l x m y r} : @all α P (node4_l l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := by cases m; simp [node4_l, all_node', all, all_node3_l, and_assoc] theorem all_node4_r {P l x m y r} : @all α P (node4_r l x m y r) ↔ all P l ∧ P x ∧ all P m ∧ P y ∧ all P r := by cases m; simp [node4_r, all_node', all, all_node3_r, and_assoc] theorem all_rotate_l {P l x r} : @all α P (rotate_l l x r) ↔ all P l ∧ P x ∧ all P r := by cases r; simp [rotate_l, all_node']; split_ifs; simp [all_node3_l, all_node4_l, all] theorem all_rotate_r {P l x r} : @all α P (rotate_r l x r) ↔ all P l ∧ P x ∧ all P r := by rw [← all_dual, dual_rotate_r, all_rotate_l]; simp [all_dual, and_comm, and.left_comm] theorem all_balance' {P l x r} : @all α P (balance' l x r) ↔ all P l ∧ P x ∧ all P r := by rw balance'; split_ifs; simp [all_node', all_rotate_l, all_rotate_r] /-! ### `to_list` -/ theorem foldr_cons_eq_to_list : ∀ (t : ordnode α) (r : list α), t.foldr list.cons r = to_list t ++ r | nil r := rfl | (node _ l x r) r' := by rw [foldr, foldr_cons_eq_to_list, foldr_cons_eq_to_list, ← list.cons_append, ← list.append_assoc, ← foldr_cons_eq_to_list]; refl @[simp] theorem to_list_nil : to_list (@nil α) = [] := rfl @[simp] theorem to_list_node (s l x r) : to_list (@node α s l x r) = to_list l ++ x :: to_list r := by rw [to_list, foldr, foldr_cons_eq_to_list]; refl theorem emem_iff_mem_to_list {x : α} {t} : emem x t ↔ x ∈ to_list t := by unfold emem; induction t; simp [any, *, or_assoc] theorem length_to_list' : ∀ t : ordnode α, (to_list t).length = t.real_size | nil := rfl | (node _ l _ r) := by rw [to_list_node, list.length_append, list.length_cons, length_to_list', length_to_list']; refl theorem length_to_list {t : ordnode α} (h : sized t) : (to_list t).length = t.size := by rw [length_to_list', size_eq_real_size h] theorem equiv_iff {t₁ t₂ : ordnode α} (h₁ : sized t₁) (h₂ : sized t₂) : equiv t₁ t₂ ↔ to_list t₁ = to_list t₂ := and_iff_right_of_imp $ λ h, by rw [← length_to_list h₁, h, length_to_list h₂] /-! ### `mem` -/ theorem pos_size_of_mem [has_le α] [@decidable_rel α (≤)] {x : α} {t : ordnode α} (h : sized t) (h_mem : x ∈ t) : 0 < size t := by { cases t, { contradiction }, { simp [h.1] } } /-! ### `(find/erase/split)_(min/max)` -/ theorem find_min'_dual : ∀ t (x : α), find_min' (dual t) x = find_max' x t | nil x := rfl | (node _ l x r) _ := find_min'_dual r x theorem find_max'_dual (t) (x : α) : find_max' x (dual t) = find_min' t x := by rw [← find_min'_dual, dual_dual] theorem find_min_dual : ∀ t : ordnode α, find_min (dual t) = find_max t | nil := rfl | (node _ l x r) := congr_arg some $ find_min'_dual _ _ theorem find_max_dual (t : ordnode α) : find_max (dual t) = find_min t := by rw [← find_min_dual, dual_dual] theorem dual_erase_min : ∀ t : ordnode α, dual (erase_min t) = erase_max (dual t) | nil := rfl | (node _ nil x r) := rfl | (node _ l@(node _ _ _ _) x r) := by rw [erase_min, dual_balance_r, dual_erase_min, dual, dual, dual, erase_max] theorem dual_erase_max (t : ordnode α) : dual (erase_max t) = erase_min (dual t) := by rw [← dual_dual (erase_min _), dual_erase_min, dual_dual] theorem split_min_eq : ∀ s l (x : α) r, split_min' l x r = (find_min' l x, erase_min (node s l x r)) | _ nil x r := rfl | _ (node ls ll lx lr) x r := by rw [split_min', split_min_eq, split_min', find_min', erase_min] theorem split_max_eq : ∀ s l (x : α) r, split_max' l x r = (erase_max (node s l x r), find_max' x r) | _ l x nil := rfl | _ l x (node ls ll lx lr) := by rw [split_max', split_max_eq, split_max', find_max', erase_max] @[elab_as_eliminator] theorem find_min'_all {P : α → Prop} : ∀ t (x : α), all P t → P x → P (find_min' t x) | nil x h hx := hx | (node _ ll lx lr) x ⟨h₁, h₂, h₃⟩ hx := find_min'_all _ _ h₁ h₂ @[elab_as_eliminator] theorem find_max'_all {P : α → Prop} : ∀ (x : α) t, P x → all P t → P (find_max' x t) | x nil hx h := hx | x (node _ ll lx lr) hx ⟨h₁, h₂, h₃⟩ := find_max'_all _ _ h₂ h₃ /-! ### `glue` -/ /-! ### `merge` -/ @[simp] theorem merge_nil_left (t : ordnode α) : merge t nil = t := by cases t; refl @[simp] theorem merge_nil_right (t : ordnode α) : merge nil t = t := rfl @[simp] theorem merge_node {ls ll lx lr rs rl rx rr} : merge (@node α ls ll lx lr) (node rs rl rx rr) = if delta * ls < rs then balance_l (merge (node ls ll lx lr) rl) rx rr else if delta * rs < ls then balance_r ll lx (merge lr (node rs rl rx rr)) else glue (node ls ll lx lr) (node rs rl rx rr) := rfl /-! ### `insert` -/ theorem dual_insert [preorder α] [is_total α (≤)] [@decidable_rel α (≤)] (x : α) : ∀ t : ordnode α, dual (ordnode.insert x t) = @ordnode.insert (order_dual α) _ _ x (dual t) | nil := rfl | (node _ l y r) := begin rw [ordnode.insert, dual, ordnode.insert, order_dual.cmp_le_flip, ← cmp_le_swap x y], cases cmp_le x y; simp [ordering.swap, ordnode.insert, dual_balance_l, dual_balance_r, dual_insert] end /-! ### `balance` properties -/ theorem balance_eq_balance' {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) : @balance α l x r = balance' l x r := begin cases l with ls ll lx lr, { cases r with rs rl rx rr, { refl }, { rw sr.eq_node' at hr ⊢, cases rl with rls rll rlx rlr; cases rr with rrs rrl rrx rrr; dsimp [balance, balance'], { refl }, { have : size rrl = 0 ∧ size rrr = 0, { have := balanced_sz_zero.1 hr.1.symm, rwa [size, sr.2.2.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sr.2.2.2.1.size_eq_zero.1 this.1, cases sr.2.2.2.2.size_eq_zero.1 this.2, have : rrs = 1 := sr.2.2.1, subst rrs, rw [if_neg, if_pos, rotate_l, if_pos], {refl}, all_goals {exact dec_trivial} }, { have : size rll = 0 ∧ size rlr = 0, { have := balanced_sz_zero.1 hr.1, rwa [size, sr.2.1.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sr.2.1.2.1.size_eq_zero.1 this.1, cases sr.2.1.2.2.size_eq_zero.1 this.2, have : rls = 1 := sr.2.1.1, subst rls, rw [if_neg, if_pos, rotate_l, if_neg], {refl}, all_goals {exact dec_trivial} }, { symmetry, rw [zero_add, if_neg, if_pos, rotate_l], { split_ifs, { simp [node3_l, node', add_comm, add_left_comm] }, { simp [node4_l, node', sr.2.1.1, add_comm, add_left_comm] } }, { exact dec_trivial }, { exact not_le_of_gt (nat.succ_lt_succ (add_pos sr.2.1.pos sr.2.2.pos)) } } } }, { cases r with rs rl rx rr, { rw sl.eq_node' at hl ⊢, cases ll with lls lll llx llr; cases lr with lrs lrl lrx lrr; dsimp [balance, balance'], { refl }, { have : size lrl = 0 ∧ size lrr = 0, { have := balanced_sz_zero.1 hl.1.symm, rwa [size, sl.2.2.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sl.2.2.2.1.size_eq_zero.1 this.1, cases sl.2.2.2.2.size_eq_zero.1 this.2, have : lrs = 1 := sl.2.2.1, subst lrs, rw [if_neg, if_neg, if_pos, rotate_r, if_neg], {refl}, all_goals {exact dec_trivial} }, { have : size lll = 0 ∧ size llr = 0, { have := balanced_sz_zero.1 hl.1, rwa [size, sl.2.1.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sl.2.1.2.1.size_eq_zero.1 this.1, cases sl.2.1.2.2.size_eq_zero.1 this.2, have : lls = 1 := sl.2.1.1, subst lls, rw [if_neg, if_neg, if_pos, rotate_r, if_pos], {refl}, all_goals {exact dec_trivial} }, { symmetry, rw [if_neg, if_neg, if_pos, rotate_r], { split_ifs, { simp [node3_r, node', add_comm, add_left_comm] }, { simp [node4_r, node', sl.2.2.1, add_comm, add_left_comm] } }, { exact dec_trivial }, { exact dec_trivial }, { exact not_le_of_gt (nat.succ_lt_succ (add_pos sl.2.1.pos sl.2.2.pos)) } } }, { simp [balance, balance'], symmetry, rw [if_neg], { split_ifs, { have rd : delta ≤ size rl + size rr, { have := lt_of_le_of_lt (nat.mul_le_mul_left _ sl.pos) h, rwa [sr.1, nat.lt_succ_iff] at this }, cases rl with rls rll rlx rlr, { rw [size, zero_add] at rd, exact absurd (le_trans rd (balanced_sz_zero.1 hr.1.symm)) dec_trivial }, cases rr with rrs rrl rrx rrr, { exact absurd (le_trans rd (balanced_sz_zero.1 hr.1)) dec_trivial }, dsimp [rotate_l], split_ifs, { simp [node3_l, node', sr.1, add_comm, add_left_comm] }, { simp [node4_l, node', sr.1, sr.2.1.1, add_comm, add_left_comm] } }, { have ld : delta ≤ size ll + size lr, { have := lt_of_le_of_lt (nat.mul_le_mul_left _ sr.pos) h_1, rwa [sl.1, nat.lt_succ_iff] at this }, cases ll with lls lll llx llr, { rw [size, zero_add] at ld, exact absurd (le_trans ld (balanced_sz_zero.1 hl.1.symm)) dec_trivial }, cases lr with lrs lrl lrx lrr, { exact absurd (le_trans ld (balanced_sz_zero.1 hl.1)) dec_trivial }, dsimp [rotate_r], split_ifs, { simp [node3_r, node', sl.1, add_comm, add_left_comm] }, { simp [node4_r, node', sl.1, sl.2.2.1, add_comm, add_left_comm] } }, { simp [node'] } }, { exact not_le_of_gt (add_le_add sl.pos sr.pos : 2 ≤ ls + rs) } } } end theorem balance_l_eq_balance {l x r} (sl : sized l) (sr : sized r) (H1 : size l = 0 → size r ≤ 1) (H2 : 1 ≤ size l → 1 ≤ size r → size r ≤ delta * size l) : @balance_l α l x r = balance l x r := begin cases r with rs rl rx rr, { refl }, { cases l with ls ll lx lr, { have : size rl = 0 ∧ size rr = 0, { have := H1 rfl, rwa [size, sr.1, nat.succ_le_succ_iff, nat.le_zero_iff, add_eq_zero_iff] at this }, cases sr.2.1.size_eq_zero.1 this.1, cases sr.2.2.size_eq_zero.1 this.2, rw sr.eq_node', refl }, { replace H2 : ¬ rs > delta * ls := not_lt_of_le (H2 sl.pos sr.pos), simp [balance_l, balance, H2]; split_ifs; simp [add_comm] } } end /-- `raised n m` means `m` is either equal or one up from `n`. -/ def raised (n m : ℕ) : Prop := m = n ∨ m = n + 1 theorem raised_iff {n m} : raised n m ↔ n ≤ m ∧ m ≤ n + 1 := begin split, rintro (rfl | rfl), { exact ⟨le_refl _, nat.le_succ _⟩ }, { exact ⟨nat.le_succ _, le_refl _⟩ }, { rintro ⟨h₁, h₂⟩, rcases eq_or_lt_of_le h₁ with rfl | h₁, { exact or.inl rfl }, { exact or.inr (le_antisymm h₂ h₁) } } end theorem raised.dist_le {n m} (H : raised n m) : nat.dist n m ≤ 1 := by cases raised_iff.1 H with H1 H2; rwa [nat.dist_eq_sub_of_le H1, tsub_le_iff_left] theorem raised.dist_le' {n m} (H : raised n m) : nat.dist m n ≤ 1 := by rw nat.dist_comm; exact H.dist_le theorem raised.add_left (k) {n m} (H : raised n m) : raised (k + n) (k + m) := begin rcases H with rfl | rfl, { exact or.inl rfl }, { exact or.inr rfl } end theorem raised.add_right (k) {n m} (H : raised n m) : raised (n + k) (m + k) := by rw [add_comm, add_comm m]; exact H.add_left _ theorem raised.right {l x₁ x₂ r₁ r₂} (H : raised (size r₁) (size r₂)) : raised (size (@node' α l x₁ r₁)) (size (@node' α l x₂ r₂)) := begin dsimp [node', size], generalize_hyp : size r₂ = m at H ⊢, rcases H with rfl | rfl, { exact or.inl rfl }, { exact or.inr rfl } end theorem balance_l_eq_balance' {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : @balance_l α l x r = balance' l x r := begin rw [← balance_eq_balance' hl hr sl sr, balance_l_eq_balance sl sr], { intro l0, rw l0 at H, rcases H with ⟨_, ⟨⟨⟩⟩|⟨⟨⟩⟩, H⟩ | ⟨r', e, H⟩, { exact balanced_sz_zero.1 H.symm }, exact le_trans (raised_iff.1 e).1 (balanced_sz_zero.1 H.symm) }, { intros l1 r1, rcases H with ⟨l', e, H | ⟨H₁, H₂⟩⟩ | ⟨r', e, H | ⟨H₁, H₂⟩⟩, { exact le_trans (le_trans (nat.le_add_left _ _) H) (mul_pos dec_trivial l1 : (0:ℕ)<_) }, { exact le_trans H₂ (nat.mul_le_mul_left _ (raised_iff.1 e).1) }, { cases raised_iff.1 e, unfold delta, linarith }, { exact le_trans (raised_iff.1 e).1 H₂ } } end theorem balance_sz_dual {l r} (H : (∃ l', raised (@size α l) l' ∧ balanced_sz l' (@size α r)) ∨ ∃ r', raised r' (size r) ∧ balanced_sz (size l) r') : (∃ l', raised l' (size (dual r)) ∧ balanced_sz l' (size (dual l))) ∨ ∃ r', raised (size (dual l)) r' ∧ balanced_sz (size (dual r)) r' := begin rw [size_dual, size_dual], exact H.symm.imp (Exists.imp $ λ _, and.imp_right balanced_sz.symm) (Exists.imp $ λ _, and.imp_right balanced_sz.symm) end theorem size_balance_l {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : size (@balance_l α l x r) = size l + size r + 1 := by rw [balance_l_eq_balance' hl hr sl sr H, size_balance' sl sr] theorem all_balance_l {P l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : all P (@balance_l α l x r) ↔ all P l ∧ P x ∧ all P r := by rw [balance_l_eq_balance' hl hr sl sr H, all_balance'] theorem balance_r_eq_balance' {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : @balance_r α l x r = balance' l x r := by rw [← dual_dual (balance_r l x r), dual_balance_r, balance_l_eq_balance' hr.dual hl.dual sr.dual sl.dual (balance_sz_dual H), ← dual_balance', dual_dual] theorem size_balance_r {l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : size (@balance_r α l x r) = size l + size r + 1 := by rw [balance_r_eq_balance' hl hr sl sr H, size_balance' sl sr] theorem all_balance_r {P l x r} (hl : balanced l) (hr : balanced r) (sl : sized l) (sr : sized r) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : all P (@balance_r α l x r) ↔ all P l ∧ P x ∧ all P r := by rw [balance_r_eq_balance' hl hr sl sr H, all_balance'] /-! ### `bounded` -/ section variable [preorder α] /-- `bounded t lo hi` says that every element `x ∈ t` is in the range `lo < x < hi`, and also this property holds recursively in subtrees, making the full tree a BST. The bounds can be set to `lo = ⊥` and `hi = ⊤` if we care only about the internal ordering constraints. -/ def bounded : ordnode α → with_bot α → with_top α → Prop | nil (some a) (some b) := a < b | nil _ _ := true | (node _ l x r) o₁ o₂ := bounded l o₁ ↑x ∧ bounded r ↑x o₂ theorem bounded.dual : ∀ {t : ordnode α} {o₁ o₂} (h : bounded t o₁ o₂), @bounded (order_dual α) _ (dual t) o₂ o₁ | nil o₁ o₂ h := by cases o₁; cases o₂; try {trivial}; exact h | (node s l x r) _ _ ⟨ol, or⟩ := ⟨or.dual, ol.dual⟩ theorem bounded.dual_iff {t : ordnode α} {o₁ o₂} : bounded t o₁ o₂ ↔ @bounded (order_dual α) _ (dual t) o₂ o₁ := ⟨bounded.dual, λ h, by have := bounded.dual h; rwa [dual_dual, order_dual.preorder.dual_dual] at this⟩ theorem bounded.weak_left : ∀ {t : ordnode α} {o₁ o₂}, bounded t o₁ o₂ → bounded t ⊥ o₂ | nil o₁ o₂ h := by cases o₂; try {trivial}; exact h | (node s l x r) _ _ ⟨ol, or⟩ := ⟨ol.weak_left, or⟩ theorem bounded.weak_right : ∀ {t : ordnode α} {o₁ o₂}, bounded t o₁ o₂ → bounded t o₁ ⊤ | nil o₁ o₂ h := by cases o₁; try {trivial}; exact h | (node s l x r) _ _ ⟨ol, or⟩ := ⟨ol, or.weak_right⟩ theorem bounded.weak {t : ordnode α} {o₁ o₂} (h : bounded t o₁ o₂) : bounded t ⊥ ⊤ := h.weak_left.weak_right theorem bounded.mono_left {x y : α} (xy : x ≤ y) : ∀ {t : ordnode α} {o}, bounded t ↑y o → bounded t ↑x o | nil none h := ⟨⟩ | nil (some z) h := lt_of_le_of_lt xy h | (node s l z r) o ⟨ol, or⟩ := ⟨ol.mono_left, or⟩ theorem bounded.mono_right {x y : α} (xy : x ≤ y) : ∀ {t : ordnode α} {o}, bounded t o ↑x → bounded t o ↑y | nil none h := ⟨⟩ | nil (some z) h := lt_of_lt_of_le h xy | (node s l z r) o ⟨ol, or⟩ := ⟨ol, or.mono_right⟩ theorem bounded.to_lt : ∀ {t : ordnode α} {x y : α}, bounded t x y → x < y | nil x y h := h | (node _ l y r) x z ⟨h₁, h₂⟩ := lt_trans h₁.to_lt h₂.to_lt theorem bounded.to_nil {t : ordnode α} : ∀ {o₁ o₂}, bounded t o₁ o₂ → bounded nil o₁ o₂ | none _ h := ⟨⟩ | (some _) none h := ⟨⟩ | (some x) (some y) h := h.to_lt theorem bounded.trans_left {t₁ t₂ : ordnode α} {x : α} : ∀ {o₁ o₂}, bounded t₁ o₁ ↑x → bounded t₂ ↑x o₂ → bounded t₂ o₁ o₂ | none o₂ h₁ h₂ := h₂.weak_left | (some y) o₂ h₁ h₂ := h₂.mono_left (le_of_lt h₁.to_lt) theorem bounded.trans_right {t₁ t₂ : ordnode α} {x : α} : ∀ {o₁ o₂}, bounded t₁ o₁ ↑x → bounded t₂ ↑x o₂ → bounded t₁ o₁ o₂ | o₁ none h₁ h₂ := h₁.weak_right | o₁ (some y) h₁ h₂ := h₁.mono_right (le_of_lt h₂.to_lt) theorem bounded.mem_lt : ∀ {t o} {x : α}, bounded t o ↑x → all (< x) t | nil o x _ := ⟨⟩ | (node _ l y r) o x ⟨h₁, h₂⟩ := ⟨h₁.mem_lt.imp (λ z h, lt_trans h h₂.to_lt), h₂.to_lt, h₂.mem_lt⟩ theorem bounded.mem_gt : ∀ {t o} {x : α}, bounded t ↑x o → all (> x) t | nil o x _ := ⟨⟩ | (node _ l y r) o x ⟨h₁, h₂⟩ := ⟨h₁.mem_gt, h₁.to_lt, h₂.mem_gt.imp (λ z, lt_trans h₁.to_lt)⟩ theorem bounded.of_lt : ∀ {t o₁ o₂} {x : α}, bounded t o₁ o₂ → bounded nil o₁ ↑x → all (< x) t → bounded t o₁ ↑x | nil o₁ o₂ x _ hn _ := hn | (node _ l y r) o₁ o₂ x ⟨h₁, h₂⟩ hn ⟨al₁, al₂, al₃⟩ := ⟨h₁, h₂.of_lt al₂ al₃⟩ theorem bounded.of_gt : ∀ {t o₁ o₂} {x : α}, bounded t o₁ o₂ → bounded nil ↑x o₂ → all (> x) t → bounded t ↑x o₂ | nil o₁ o₂ x _ hn _ := hn | (node _ l y r) o₁ o₂ x ⟨h₁, h₂⟩ hn ⟨al₁, al₂, al₃⟩ := ⟨h₁.of_gt al₂ al₁, h₂⟩ theorem bounded.to_sep {t₁ t₂ o₁ o₂} {x : α} (h₁ : bounded t₁ o₁ ↑x) (h₂ : bounded t₂ ↑x o₂) : t₁.all (λ y, t₂.all (λ z : α, y < z)) := h₁.mem_lt.imp $ λ y yx, h₂.mem_gt.imp $ λ z xz, lt_trans yx xz end /-! ### `valid` -/ section variable [preorder α] /-- The validity predicate for an `ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. This version of `valid` also puts all elements in the tree in the interval `(lo, hi)`. -/ structure valid' (lo : with_bot α) (t : ordnode α) (hi : with_top α) : Prop := (ord : t.bounded lo hi) (sz : t.sized) (bal : t.balanced) /-- The validity predicate for an `ordnode` subtree. This asserts that the `size` fields are correct, the tree is balanced, and the elements of the tree are organized according to the ordering. -/ def valid (t : ordnode α) : Prop := valid' ⊥ t ⊤ theorem valid'.mono_left {x y : α} (xy : x ≤ y) {t : ordnode α} {o} (h : valid' ↑y t o) : valid' ↑x t o := ⟨h.1.mono_left xy, h.2, h.3⟩ theorem valid'.mono_right {x y : α} (xy : x ≤ y) {t : ordnode α} {o} (h : valid' o t ↑x) : valid' o t ↑y := ⟨h.1.mono_right xy, h.2, h.3⟩ theorem valid'.trans_left {t₁ t₂ : ordnode α} {x : α} {o₁ o₂} (h : bounded t₁ o₁ ↑x) (H : valid' ↑x t₂ o₂) : valid' o₁ t₂ o₂ := ⟨h.trans_left H.1, H.2, H.3⟩ theorem valid'.trans_right {t₁ t₂ : ordnode α} {x : α} {o₁ o₂} (H : valid' o₁ t₁ ↑x) (h : bounded t₂ ↑x o₂) : valid' o₁ t₁ o₂ := ⟨H.1.trans_right h, H.2, H.3⟩ theorem valid'.of_lt {t : ordnode α} {x : α} {o₁ o₂} (H : valid' o₁ t o₂) (h₁ : bounded nil o₁ ↑x) (h₂ : all (< x) t) : valid' o₁ t ↑x := ⟨H.1.of_lt h₁ h₂, H.2, H.3⟩ theorem valid'.of_gt {t : ordnode α} {x : α} {o₁ o₂} (H : valid' o₁ t o₂) (h₁ : bounded nil ↑x o₂) (h₂ : all (> x) t) : valid' ↑x t o₂ := ⟨H.1.of_gt h₁ h₂, H.2, H.3⟩ theorem valid'.valid {t o₁ o₂} (h : @valid' α _ o₁ t o₂) : valid t := ⟨h.1.weak, h.2, h.3⟩ theorem valid'_nil {o₁ o₂} (h : bounded nil o₁ o₂) : valid' o₁ (@nil α) o₂ := ⟨h, ⟨⟩, ⟨⟩⟩ theorem valid_nil : valid (@nil α) := valid'_nil ⟨⟩ theorem valid'.node {s l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : balanced_sz (size l) (size r)) (hs : s = size l + size r + 1) : valid' o₁ (@node α s l x r) o₂ := ⟨⟨hl.1, hr.1⟩, ⟨hs, hl.2, hr.2⟩, ⟨H, hl.3, hr.3⟩⟩ theorem valid'.dual : ∀ {t : ordnode α} {o₁ o₂} (h : valid' o₁ t o₂), @valid' (order_dual α) _ o₂ (dual t) o₁ | nil o₁ o₂ h := valid'_nil h.1.dual | (node s l x r) o₁ o₂ ⟨⟨ol, or⟩, ⟨rfl, sl, sr⟩, ⟨b, bl, br⟩⟩ := let ⟨ol', sl', bl'⟩ := valid'.dual ⟨ol, sl, bl⟩, ⟨or', sr', br'⟩ := valid'.dual ⟨or, sr, br⟩ in ⟨⟨or', ol'⟩, ⟨by simp [size_dual, add_comm], sr', sl'⟩, ⟨by rw [size_dual, size_dual]; exact b.symm, br', bl'⟩⟩ theorem valid'.dual_iff {t : ordnode α} {o₁ o₂} : valid' o₁ t o₂ ↔ @valid' (order_dual α) _ o₂ (dual t) o₁ := ⟨valid'.dual, λ h, by have := valid'.dual h; rwa [dual_dual, order_dual.preorder.dual_dual] at this⟩ theorem valid.dual {t : ordnode α} : valid t → @valid (order_dual α) _ (dual t) := valid'.dual theorem valid.dual_iff {t : ordnode α} : valid t ↔ @valid (order_dual α) _ (dual t) := valid'.dual_iff theorem valid'.left {s l x r o₁ o₂} (H : valid' o₁ (@node α s l x r) o₂) : valid' o₁ l x := ⟨H.1.1, H.2.2.1, H.3.2.1⟩ theorem valid'.right {s l x r o₁ o₂} (H : valid' o₁ (@node α s l x r) o₂) : valid' ↑x r o₂ := ⟨H.1.2, H.2.2.2, H.3.2.2⟩ theorem valid.left {s l x r} (H : valid (@node α s l x r)) : valid l := H.left.valid theorem valid.right {s l x r} (H : valid (@node α s l x r)) : valid r := H.right.valid theorem valid.size_eq {s l x r} (H : valid (@node α s l x r)) : size (@node α s l x r) = size l + size r + 1 := H.2.1 theorem valid'.node' {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : balanced_sz (size l) (size r)) : valid' o₁ (@node' α l x r) o₂ := hl.node hr H rfl theorem valid'_singleton {x : α} {o₁ o₂} (h₁ : bounded nil o₁ ↑x) (h₂ : bounded nil ↑x o₂) : valid' o₁ (singleton x : ordnode α) o₂ := (valid'_nil h₁).node (valid'_nil h₂) (or.inl zero_le_one) rfl theorem valid_singleton {x : α} : valid (singleton x : ordnode α) := valid'_singleton ⟨⟩ ⟨⟩ theorem valid'.node3_l {l x m y r o₁ o₂} (hl : valid' o₁ l ↑x) (hm : valid' ↑x m ↑y) (hr : valid' ↑y r o₂) (H1 : balanced_sz (size l) (size m)) (H2 : balanced_sz (size l + size m + 1) (size r)) : valid' o₁ (@node3_l α l x m y r) o₂ := (hl.node' hm H1).node' hr H2 theorem valid'.node3_r {l x m y r o₁ o₂} (hl : valid' o₁ l ↑x) (hm : valid' ↑x m ↑y) (hr : valid' ↑y r o₂) (H1 : balanced_sz (size l) (size m + size r + 1)) (H2 : balanced_sz (size m) (size r)) : valid' o₁ (@node3_r α l x m y r) o₂ := hl.node' (hm.node' hr H2) H1 theorem valid'.node4_l_lemma₁ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : b < 3 * a + 1 := by linarith theorem valid'.node4_l_lemma₂ {b c d : ℕ} (mr₂ : b + c + 1 ≤ 3 * d) : c ≤ 3 * d := by linarith theorem valid'.node4_l_lemma₃ {b c d : ℕ} (mr₁ : 2 * d ≤ b + c + 1) (mm₁ : b ≤ 3 * c) : d ≤ 3 * c := by linarith theorem valid'.node4_l_lemma₄ {a b c d : ℕ} (lr₁ : 3 * a ≤ b + c + 1 + d) (mr₂ : b + c + 1 ≤ 3 * d) (mm₁ : b ≤ 3 * c) : a + b + 1 ≤ 3 * (c + d + 1) := by linarith theorem valid'.node4_l_lemma₅ {a b c d : ℕ} (lr₂ : 3 * (b + c + 1 + d) ≤ 16 * a + 9) (mr₁ : 2 * d ≤ b + c + 1) (mm₂ : c ≤ 3 * b) : c + d + 1 ≤ 3 * (a + b + 1) := by linarith theorem valid'.node4_l {l x m y r o₁ o₂} (hl : valid' o₁ l ↑x) (hm : valid' ↑x m ↑y) (hr : valid' ↑y r o₂) (Hm : 0 < size m) (H : (size l = 0 ∧ size m = 1 ∧ size r ≤ 1) ∨ (0 < size l ∧ ratio * size r ≤ size m ∧ delta * size l ≤ size m + size r ∧ 3 * (size m + size r) ≤ 16 * size l + 9 ∧ size m ≤ delta * size r)) : valid' o₁ (@node4_l α l x m y r) o₂ := begin cases m with s ml z mr, {cases Hm}, suffices : balanced_sz (size l) (size ml) ∧ balanced_sz (size mr) (size r) ∧ balanced_sz (size l + size ml + 1) (size mr + size r + 1), from (valid'.node' (hl.node' hm.left this.1) (hm.right.node' hr this.2.1) this.2.2), rcases H with ⟨l0, m1, r0⟩ | ⟨l0, mr₁, lr₁, lr₂, mr₂⟩, { rw [hm.2.size_eq, nat.succ_inj', add_eq_zero_iff] at m1, rw [l0, m1.1, m1.2], rcases size r with _|_|_; exact dec_trivial }, { cases nat.eq_zero_or_pos (size r) with r0 r0, { rw r0 at mr₂, cases not_le_of_lt Hm mr₂ }, rw [hm.2.size_eq] at lr₁ lr₂ mr₁ mr₂, by_cases mm : size ml + size mr ≤ 1, { have r1 := le_antisymm ((mul_le_mul_left dec_trivial).1 (le_trans mr₁ (nat.succ_le_succ mm) : _ ≤ ratio * 1)) r0, rw [r1, add_assoc] at lr₁, have l1 := le_antisymm ((mul_le_mul_left dec_trivial).1 (le_trans lr₁ (add_le_add_right mm 2) : _ ≤ delta * 1)) l0, rw [l1, r1], cases size ml; cases size mr, { exact dec_trivial }, { rw zero_add at mm, rcases mm with _|⟨_,⟨⟩⟩, exact dec_trivial }, { rcases mm with _|⟨_,⟨⟩⟩, exact dec_trivial }, { rw nat.succ_add at mm, rcases mm with _|⟨_,⟨⟩⟩ } }, rcases hm.3.1.resolve_left mm with ⟨mm₁, mm₂⟩, cases nat.eq_zero_or_pos (size ml) with ml0 ml0, { rw [ml0, mul_zero, nat.le_zero_iff] at mm₂, rw [ml0, mm₂] at mm, cases mm dec_trivial }, cases nat.eq_zero_or_pos (size mr) with mr0 mr0, { rw [mr0, mul_zero, nat.le_zero_iff] at mm₁, rw [mr0, mm₁] at mm, cases mm dec_trivial }, have : 2 * size l ≤ size ml + size mr + 1, { have := nat.mul_le_mul_left _ lr₁, rw [mul_left_comm, mul_add] at this, have := le_trans this (add_le_add_left mr₁ _), rw [← nat.succ_mul] at this, exact (mul_le_mul_left dec_trivial).1 this }, refine ⟨or.inr ⟨_, _⟩, or.inr ⟨_, _⟩, or.inr ⟨_, _⟩⟩, { refine (mul_le_mul_left dec_trivial).1 (le_trans this _), rw [two_mul, nat.succ_le_iff], refine add_lt_add_of_lt_of_le _ mm₂, simpa using (mul_lt_mul_right ml0).2 (dec_trivial:1<3) }, { exact nat.le_of_lt_succ (valid'.node4_l_lemma₁ lr₂ mr₂ mm₁) }, { exact valid'.node4_l_lemma₂ mr₂ }, { exact valid'.node4_l_lemma₃ mr₁ mm₁ }, { exact valid'.node4_l_lemma₄ lr₁ mr₂ mm₁ }, { exact valid'.node4_l_lemma₅ lr₂ mr₁ mm₂ } } end theorem valid'.rotate_l_lemma₁ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (hb₂ : c ≤ 3 * b) : a ≤ 3 * b := by linarith theorem valid'.rotate_l_lemma₂ {a b c : ℕ} (H3 : 2 * (b + c) ≤ 9 * a + 3) (h : b < 2 * c) : b < 3 * a + 1 := by linarith theorem valid'.rotate_l_lemma₃ {a b c : ℕ} (H2 : 3 * a ≤ b + c) (h : b < 2 * c) : a + b < 3 * c := by linarith theorem valid'.rotate_l_lemma₄ {a b : ℕ} (H3 : 2 * b ≤ 9 * a + 3) : 3 * b ≤ 16 * a + 9 := by linarith theorem valid'.rotate_l {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H1 : ¬ size l + size r ≤ 1) (H2 : delta * size l < size r) (H3 : 2 * size r ≤ 9 * size l + 5 ∨ size r ≤ 3) : valid' o₁ (@rotate_l α l x r) o₂ := begin cases r with rs rl rx rr, {cases H2}, rw [hr.2.size_eq, nat.lt_succ_iff] at H2, rw [hr.2.size_eq] at H3, replace H3 : 2 * (size rl + size rr) ≤ 9 * size l + 3 ∨ size rl + size rr ≤ 2 := H3.imp (@nat.le_of_add_le_add_right 2 _ _) nat.le_of_succ_le_succ, have H3_0 : size l = 0 → size rl + size rr ≤ 2, { intro l0, rw l0 at H3, exact (or_iff_right_of_imp $ by exact λ h, (mul_le_mul_left dec_trivial).1 (le_trans h dec_trivial)).1 H3 }, have H3p : size l > 0 → 2 * (size rl + size rr) ≤ 9 * size l + 3 := λ l0 : 1 ≤ size l, (or_iff_left_of_imp $ by intro; linarith).1 H3, have ablem : ∀ {a b : ℕ}, 1 ≤ a → a + b ≤ 2 → b ≤ 1, {intros, linarith}, have hlp : size l > 0 → ¬ size rl + size rr ≤ 1 := λ l0 hb, absurd (le_trans (le_trans (nat.mul_le_mul_left _ l0) H2) hb) dec_trivial, rw rotate_l, split_ifs, { have rr0 : size rr > 0 := (mul_lt_mul_left dec_trivial).1 (lt_of_le_of_lt (nat.zero_le _) h : ratio * 0 < _), suffices : balanced_sz (size l) (size rl) ∧ balanced_sz (size l + size rl + 1) (size rr), { exact hl.node3_l hr.left hr.right this.1 this.2 }, cases nat.eq_zero_or_pos (size l) with l0 l0, { rw l0, replace H3 := H3_0 l0, have := hr.3.1, cases nat.eq_zero_or_pos (size rl) with rl0 rl0, { rw rl0 at this ⊢, rw le_antisymm (balanced_sz_zero.1 this.symm) rr0, exact dec_trivial }, have rr1 : size rr = 1 := le_antisymm (ablem rl0 H3) rr0, rw add_comm at H3, rw [rr1, show size rl = 1, from le_antisymm (ablem rr0 H3) rl0], exact dec_trivial }, replace H3 := H3p l0, rcases hr.3.1.resolve_left (hlp l0) with ⟨hb₁, hb₂⟩, cases nat.eq_zero_or_pos (size rl) with rl0 rl0, { rw rl0 at hb₂, cases not_le_of_gt rr0 hb₂ }, cases eq_or_lt_of_le (show 1 ≤ size rr, from rr0) with rr1 rr1, { rw [← rr1] at h H2 ⊢, have : size rl = 1 := le_antisymm (nat.lt_succ_iff.1 h) rl0, rw this at H2, exact absurd (le_trans (nat.mul_le_mul_left _ l0) H2) dec_trivial }, refine ⟨or.inr ⟨_, _⟩, or.inr ⟨_, _⟩⟩, { exact valid'.rotate_l_lemma₁ H2 hb₂ }, { exact nat.le_of_lt_succ (valid'.rotate_l_lemma₂ H3 h) }, { exact valid'.rotate_l_lemma₃ H2 h }, { exact le_trans hb₂ (nat.mul_le_mul_left _ $ le_trans (nat.le_add_left _ _) (nat.le_add_right _ _)) } }, { cases nat.eq_zero_or_pos (size rl) with rl0 rl0, { rw [rl0, not_lt, nat.le_zero_iff, nat.mul_eq_zero] at h, replace h := h.resolve_left dec_trivial, rw [rl0, h, nat.le_zero_iff, nat.mul_eq_zero] at H2, rw [hr.2.size_eq, rl0, h, H2.resolve_left dec_trivial] at H1, cases H1 dec_trivial }, refine hl.node4_l hr.left hr.right rl0 _, cases nat.eq_zero_or_pos (size l) with l0 l0, { replace H3 := H3_0 l0, cases nat.eq_zero_or_pos (size rr) with rr0 rr0, { have := hr.3.1, rw rr0 at this, exact or.inl ⟨l0, le_antisymm (balanced_sz_zero.1 this) rl0, rr0.symm ▸ zero_le_one⟩ }, exact or.inl ⟨l0, le_antisymm (ablem rr0 $ by rwa add_comm) rl0, ablem rl0 H3⟩ }, exact or.inr ⟨l0, not_lt.1 h, H2, valid'.rotate_l_lemma₄ (H3p l0), (hr.3.1.resolve_left (hlp l0)).1⟩ } end theorem valid'.rotate_r {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H1 : ¬ size l + size r ≤ 1) (H2 : delta * size r < size l) (H3 : 2 * size l ≤ 9 * size r + 5 ∨ size l ≤ 3) : valid' o₁ (@rotate_r α l x r) o₂ := begin refine valid'.dual_iff.2 _, rw dual_rotate_r, refine hr.dual.rotate_l hl.dual _ _ _, { rwa [size_dual, size_dual, add_comm] }, { rwa [size_dual, size_dual] }, { rwa [size_dual, size_dual] } end theorem valid'.balance'_aux {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H₁ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) (H₂ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : valid' o₁ (@balance' α l x r) o₂ := begin rw balance', split_ifs, { exact hl.node' hr (or.inl h) }, { exact hl.rotate_l hr h h_1 H₁ }, { exact hl.rotate_r hr h h_2 H₂ }, { exact hl.node' hr (or.inr ⟨not_lt.1 h_2, not_lt.1 h_1⟩) } end theorem valid'.balance'_lemma {α l l' r r'} (H1 : balanced_sz l' r') (H2 : nat.dist (@size α l) l' ≤ 1 ∧ size r = r' ∨ nat.dist (size r) r' ≤ 1 ∧ size l = l') : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3 := begin suffices : @size α r ≤ 3 * (size l + 1), { cases nat.eq_zero_or_pos (size l) with l0 l0, { apply or.inr, rwa l0 at this }, change 1 ≤ _ at l0, apply or.inl, linarith }, rcases H2 with ⟨hl, rfl⟩ | ⟨hr, rfl⟩; rcases H1 with h | ⟨h₁, h₂⟩, { exact le_trans (nat.le_add_left _ _) (le_trans h (nat.le_add_left _ _)) }, { exact le_trans h₂ (nat.mul_le_mul_left _ $ le_trans (nat.dist_tri_right _ _) (nat.add_le_add_left hl _)) }, { exact le_trans (nat.dist_tri_left' _ _) (le_trans (add_le_add hr (le_trans (nat.le_add_left _ _) h)) dec_trivial) }, { rw nat.mul_succ, exact le_trans (nat.dist_tri_right' _ _) (add_le_add h₂ (le_trans hr dec_trivial)) }, end theorem valid'.balance' {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : ∃ l' r', balanced_sz l' r' ∧ (nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ nat.dist (size r) r' ≤ 1 ∧ size l = l')) : valid' o₁ (@balance' α l x r) o₂ := let ⟨l', r', H1, H2⟩ := H in valid'.balance'_aux hl hr (valid'.balance'_lemma H1 H2) (valid'.balance'_lemma H1.symm H2.symm) theorem valid'.balance {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : ∃ l' r', balanced_sz l' r' ∧ (nat.dist (size l) l' ≤ 1 ∧ size r = r' ∨ nat.dist (size r) r' ≤ 1 ∧ size l = l')) : valid' o₁ (@balance α l x r) o₂ := by rw balance_eq_balance' hl.3 hr.3 hl.2 hr.2; exact hl.balance' hr H theorem valid'.balance_l_aux {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H₁ : size l = 0 → size r ≤ 1) (H₂ : 1 ≤ size l → 1 ≤ size r → size r ≤ delta * size l) (H₃ : 2 * @size α l ≤ 9 * size r + 5 ∨ size l ≤ 3) : valid' o₁ (@balance_l α l x r) o₂ := begin rw [balance_l_eq_balance hl.2 hr.2 H₁ H₂, balance_eq_balance' hl.3 hr.3 hl.2 hr.2], refine hl.balance'_aux hr (or.inl _) H₃, cases nat.eq_zero_or_pos (size r) with r0 r0, { rw r0, exact nat.zero_le _ }, cases nat.eq_zero_or_pos (size l) with l0 l0, { rw l0, exact le_trans (nat.mul_le_mul_left _ (H₁ l0)) dec_trivial }, replace H₂ : _ ≤ 3 * _ := H₂ l0 r0, linarith end theorem valid'.balance_l {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : (∃ l', raised l' (size l) ∧ balanced_sz l' (size r)) ∨ (∃ r', raised (size r) r' ∧ balanced_sz (size l) r')) : valid' o₁ (@balance_l α l x r) o₂ := begin rw balance_l_eq_balance' hl.3 hr.3 hl.2 hr.2 H, refine hl.balance' hr _, rcases H with ⟨l', e, H⟩ | ⟨r', e, H⟩, { exact ⟨_, _, H, or.inl ⟨e.dist_le', rfl⟩⟩ }, { exact ⟨_, _, H, or.inr ⟨e.dist_le, rfl⟩⟩ }, end theorem valid'.balance_r_aux {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H₁ : size r = 0 → size l ≤ 1) (H₂ : 1 ≤ size r → 1 ≤ size l → size l ≤ delta * size r) (H₃ : 2 * @size α r ≤ 9 * size l + 5 ∨ size r ≤ 3) : valid' o₁ (@balance_r α l x r) o₂ := begin rw [valid'.dual_iff, dual_balance_r], have := hr.dual.balance_l_aux hl.dual, rw [size_dual, size_dual] at this, exact this H₁ H₂ H₃ end theorem valid'.balance_r {l x r o₁ o₂} (hl : valid' o₁ l ↑x) (hr : valid' ↑x r o₂) (H : (∃ l', raised (size l) l' ∧ balanced_sz l' (size r)) ∨ (∃ r', raised r' (size r) ∧ balanced_sz (size l) r')) : valid' o₁ (@balance_r α l x r) o₂ := by rw [valid'.dual_iff, dual_balance_r]; exact hr.dual.balance_l hl.dual (balance_sz_dual H) theorem valid'.erase_max_aux {s l x r o₁ o₂} (H : valid' o₁ (node s l x r) o₂) : valid' o₁ (@erase_max α (node' l x r)) ↑(find_max' x r) ∧ size (node' l x r) = size (erase_max (node' l x r)) + 1 := begin have := H.2.eq_node', rw this at H, clear this, induction r with rs rl rx rr IHrl IHrr generalizing l x o₁, { exact ⟨H.left, rfl⟩ }, have := H.2.2.2.eq_node', rw this at H ⊢, rcases IHrr H.right with ⟨h, e⟩, refine ⟨valid'.balance_l H.left h (or.inr ⟨_, or.inr e, H.3.1⟩), _⟩, rw [erase_max, size_balance_l H.3.2.1 h.3 H.2.2.1 h.2 (or.inr ⟨_, or.inr e, H.3.1⟩)], rw [size, e], refl end theorem valid'.erase_min_aux {s l x r o₁ o₂} (H : valid' o₁ (node s l x r) o₂) : valid' ↑(find_min' l x) (@erase_min α (node' l x r)) o₂ ∧ size (node' l x r) = size (erase_min (node' l x r)) + 1 := by have := H.dual.erase_max_aux; rwa [← dual_node', size_dual, ← dual_erase_min, size_dual, ← valid'.dual_iff, find_max'_dual] at this theorem erase_min.valid : ∀ {t} (h : @valid α _ t), valid (erase_min t) | nil _ := valid_nil | (node _ l x r) h := by rw h.2.eq_node'; exact h.erase_min_aux.1.valid theorem erase_max.valid {t} (h : @valid α _ t) : valid (erase_max t) := by rw [valid.dual_iff, dual_erase_max]; exact erase_min.valid h.dual theorem valid'.glue_aux {l r o₁ o₂} (hl : valid' o₁ l o₂) (hr : valid' o₁ r o₂) (sep : l.all (λ x, r.all (λ y, x < y))) (bal : balanced_sz (size l) (size r)) : valid' o₁ (@glue α l r) o₂ ∧ size (glue l r) = size l + size r := begin cases l with ls ll lx lr, {exact ⟨hr, (zero_add _).symm⟩ }, cases r with rs rl rx rr, {exact ⟨hl, rfl⟩ }, dsimp [glue], split_ifs, { rw [split_max_eq, glue], cases valid'.erase_max_aux hl with v e, suffices H, refine ⟨valid'.balance_r v (hr.of_gt _ _) H, _⟩, { refine find_max'_all lx lr hl.1.2.to_nil (sep.2.2.imp _), exact λ x h, hr.1.2.to_nil.mono_left (le_of_lt h.2.1) }, { exact @find_max'_all _ (λ a, all (> a) (node rs rl rx rr)) lx lr sep.2.1 sep.2.2 }, { rw [size_balance_r v.3 hr.3 v.2 hr.2 H, add_right_comm, ← e, hl.2.1], refl }, { refine or.inl ⟨_, or.inr e, _⟩, rwa hl.2.eq_node' at bal } }, { rw [split_min_eq, glue], cases valid'.erase_min_aux hr with v e, suffices H, refine ⟨valid'.balance_l (hl.of_lt _ _) v H, _⟩, { refine @find_min'_all _ (λ a, bounded nil o₁ ↑a) rl rx (sep.2.1.1.imp _) hr.1.1.to_nil, exact λ y h, hl.1.1.to_nil.mono_right (le_of_lt h) }, { exact @find_min'_all _ (λ a, all (< a) (node ls ll lx lr)) rl rx (all_iff_forall.2 $ λ x hx, sep.imp $ λ y hy, all_iff_forall.1 hy.1 _ hx) (sep.imp $ λ y hy, hy.2.1) }, { rw [size_balance_l hl.3 v.3 hl.2 v.2 H, add_assoc, ← e, hr.2.1], refl }, { refine or.inr ⟨_, or.inr e, _⟩, rwa hr.2.eq_node' at bal } }, end theorem valid'.glue {l x r o₁ o₂} (hl : valid' o₁ l ↑(x:α)) (hr : valid' ↑x r o₂) : balanced_sz (size l) (size r) → valid' o₁ (@glue α l r) o₂ ∧ size (@glue α l r) = size l + size r := valid'.glue_aux (hl.trans_right hr.1) (hr.trans_left hl.1) (hl.1.to_sep hr.1) theorem valid'.merge_lemma {a b c : ℕ} (h₁ : 3 * a < b + c + 1) (h₂ : b ≤ 3 * c) : 2 * (a + b) ≤ 9 * c + 5 := by linarith theorem valid'.merge_aux₁ {o₁ o₂ ls ll lx lr rs rl rx rr t} (hl : valid' o₁ (@node α ls ll lx lr) o₂) (hr : valid' o₁ (node rs rl rx rr) o₂) (h : delta * ls < rs) (v : valid' o₁ t ↑rx) (e : size t = ls + size rl) : valid' o₁ (balance_l t rx rr) o₂ ∧ size (balance_l t rx rr) = ls + rs := begin rw hl.2.1 at e, rw [hl.2.1, hr.2.1, delta] at h, rcases hr.3.1 with H|⟨hr₁, hr₂⟩, {linarith}, suffices H₂, suffices H₁, refine ⟨valid'.balance_l_aux v hr.right H₁ H₂ _, _⟩, { rw e, exact or.inl (valid'.merge_lemma h hr₁) }, { rw [balance_l_eq_balance v.2 hr.2.2.2 H₁ H₂, balance_eq_balance' v.3 hr.3.2.2 v.2 hr.2.2.2, size_balance' v.2 hr.2.2.2, e, hl.2.1, hr.2.1], simp [add_comm, add_left_comm] }, { rw [e, add_right_comm], rintro ⟨⟩ }, { intros _ h₁, rw e, unfold delta at hr₂ ⊢, linarith } end theorem valid'.merge_aux {l r o₁ o₂} (hl : valid' o₁ l o₂) (hr : valid' o₁ r o₂) (sep : l.all (λ x, r.all (λ y, x < y))) : valid' o₁ (@merge α l r) o₂ ∧ size (merge l r) = size l + size r := begin induction l with ls ll lx lr IHll IHlr generalizing o₁ o₂ r, { exact ⟨hr, (zero_add _).symm⟩ }, induction r with rs rl rx rr IHrl IHrr generalizing o₁ o₂, { exact ⟨hl, rfl⟩ }, rw [merge_node], split_ifs, { cases IHrl (sep.imp $ λ x h, h.1) (hl.of_lt hr.1.1.to_nil $ sep.imp $ λ x h, h.2.1) hr.left with v e, exact valid'.merge_aux₁ hl hr h v e }, { cases IHlr hl.right (hr.of_gt hl.1.2.to_nil sep.2.1) sep.2.2 with v e, have := valid'.merge_aux₁ hr.dual hl.dual h_1 v.dual, rw [size_dual, add_comm, size_dual, ← dual_balance_r, ← valid'.dual_iff, size_dual, add_comm rs] at this, exact this e }, { refine valid'.glue_aux hl hr sep (or.inr ⟨not_lt.1 h_1, not_lt.1 h⟩) } end theorem valid.merge {l r} (hl : valid l) (hr : valid r) (sep : l.all (λ x, r.all (λ y, x < y))) : valid (@merge α l r) := (valid'.merge_aux hl hr sep).1 theorem insert_with.valid_aux [is_total α (≤)] [@decidable_rel α (≤)] (f : α → α) (x : α) (hf : ∀ y, x ≤ y ∧ y ≤ x → x ≤ f y ∧ f y ≤ x) : ∀ {t o₁ o₂}, valid' o₁ t o₂ → bounded nil o₁ ↑x → bounded nil ↑x o₂ → valid' o₁ (insert_with f x t) o₂ ∧ raised (size t) (size (insert_with f x t)) | nil o₁ o₂ _ bl br := ⟨valid'_singleton bl br, or.inr rfl⟩ | (node sz l y r) o₁ o₂ h bl br := begin rw [insert_with, cmp_le], split_ifs; rw [insert_with], { rcases h with ⟨⟨lx, xr⟩, hs, hb⟩, rcases hf _ ⟨h_1, h_2⟩ with ⟨xf, fx⟩, refine ⟨⟨⟨lx.mono_right (le_trans h_2 xf), xr.mono_left (le_trans fx h_1)⟩, hs, hb⟩, or.inl rfl⟩ }, { rcases insert_with.valid_aux h.left bl (lt_of_le_not_le h_1 h_2) with ⟨vl, e⟩, suffices H, { refine ⟨vl.balance_l h.right H, _⟩, rw [size_balance_l vl.3 h.3.2.2 vl.2 h.2.2.2 H, h.2.size_eq], refine (e.add_right _).add_right _ }, { exact or.inl ⟨_, e, h.3.1⟩ } }, { have : y < x := lt_of_le_not_le ((total_of (≤) _ _).resolve_left h_1) h_1, rcases insert_with.valid_aux h.right this br with ⟨vr, e⟩, suffices H, { refine ⟨h.left.balance_r vr H, _⟩, rw [size_balance_r h.3.2.1 vr.3 h.2.2.1 vr.2 H, h.2.size_eq], refine (e.add_left _).add_right _ }, { exact or.inr ⟨_, e, h.3.1⟩ } }, end theorem insert_with.valid [is_total α (≤)] [@decidable_rel α (≤)] (f : α → α) (x : α) (hf : ∀ y, x ≤ y ∧ y ≤ x → x ≤ f y ∧ f y ≤ x) {t} (h : valid t) : valid (insert_with f x t) := (insert_with.valid_aux _ _ hf h ⟨⟩ ⟨⟩).1 theorem insert_eq_insert_with [@decidable_rel α (≤)] (x : α) : ∀ t, ordnode.insert x t = insert_with (λ _, x) x t | nil := rfl | (node _ l y r) := by unfold ordnode.insert insert_with; cases cmp_le x y; unfold ordnode.insert insert_with; simp [insert_eq_insert_with] theorem insert.valid [is_total α (≤)] [@decidable_rel α (≤)] (x : α) {t} (h : valid t) : valid (ordnode.insert x t) := by rw insert_eq_insert_with; exact insert_with.valid _ _ (λ _ _, ⟨le_refl _, le_refl _⟩) h theorem insert'_eq_insert_with [@decidable_rel α (≤)] (x : α) : ∀ t, insert' x t = insert_with id x t | nil := rfl | (node _ l y r) := by unfold insert' insert_with; cases cmp_le x y; unfold insert' insert_with; simp [insert'_eq_insert_with] theorem insert'.valid [is_total α (≤)] [@decidable_rel α (≤)] (x : α) {t} (h : valid t) : valid (insert' x t) := by rw insert'_eq_insert_with; exact insert_with.valid _ _ (λ _, id) h theorem valid'.map_aux {β} [preorder β] {f : α → β} (f_strict_mono : strict_mono f) {t a₁ a₂} (h : valid' a₁ t a₂) : valid' (option.map f a₁) (map f t) (option.map f a₂) ∧ (map f t).size = t.size := begin induction t generalizing a₁ a₂, { simp [map], apply valid'_nil, cases a₁, { trivial }, cases a₂, { trivial }, simp [bounded], exact f_strict_mono h.ord }, { have t_ih_l' := t_ih_l h.left, have t_ih_r' := t_ih_r h.right, clear t_ih_l t_ih_r, cases t_ih_l' with t_l_valid t_l_size, cases t_ih_r' with t_r_valid t_r_size, simp [map], split, { exact and.intro t_l_valid.ord t_r_valid.ord }, { repeat { split }, { rw [t_l_size, t_r_size], exact h.sz.1 }, { exact t_l_valid.sz }, { exact t_r_valid.sz } }, { repeat { split }, { rw [t_l_size, t_r_size], exact h.bal.1 }, { exact t_l_valid.bal }, { exact t_r_valid.bal } } }, end theorem map.valid {β} [preorder β] {f : α → β} (f_strict_mono : strict_mono f) {t} (h : valid t) : valid (map f t) := (valid'.map_aux f_strict_mono h).1 theorem valid'.erase_aux [@decidable_rel α (≤)] (x : α) {t a₁ a₂} (h : valid' a₁ t a₂) : valid' a₁ (erase x t) a₂ ∧ raised (erase x t).size t.size := begin induction t generalizing a₁ a₂, { simp [erase, raised], exact h }, { simp [erase], have t_ih_l' := t_ih_l h.left, have t_ih_r' := t_ih_r h.right, clear t_ih_l t_ih_r, cases t_ih_l' with t_l_valid t_l_size, cases t_ih_r' with t_r_valid t_r_size, cases (cmp_le x t_x); simp [erase._match_1]; rw h.sz.1, { suffices h_balanceable, split, { exact valid'.balance_r t_l_valid h.right h_balanceable }, { rw size_balance_r t_l_valid.bal h.right.bal t_l_valid.sz h.right.sz h_balanceable, repeat { apply raised.add_right }, exact t_l_size }, { left, existsi t_l.size, exact (and.intro t_l_size h.bal.1) } }, { have h_glue := valid'.glue h.left h.right h.bal.1, cases h_glue with h_glue_valid h_glue_sized, split, { exact h_glue_valid }, { right, rw h_glue_sized } }, { suffices h_balanceable, split, { exact valid'.balance_l h.left t_r_valid h_balanceable }, { rw size_balance_l h.left.bal t_r_valid.bal h.left.sz t_r_valid.sz h_balanceable, apply raised.add_right, apply raised.add_left, exact t_r_size }, { right, existsi t_r.size, exact (and.intro t_r_size h.bal.1) } } }, end theorem erase.valid [@decidable_rel α (≤)] (x : α) {t} (h : valid t) : valid (erase x t) := (valid'.erase_aux x h).1 theorem size_erase_of_mem [@decidable_rel α (≤)] {x : α} {t a₁ a₂} (h : valid' a₁ t a₂) (h_mem : x ∈ t) : size (erase x t) = size t - 1 := begin induction t generalizing a₁ a₂ h h_mem, { contradiction }, { have t_ih_l' := t_ih_l h.left, have t_ih_r' := t_ih_r h.right, clear t_ih_l t_ih_r, unfold has_mem.mem mem at h_mem, unfold erase, cases (cmp_le x t_x); simp [mem._match_1] at h_mem; simp [erase._match_1], { have t_ih_l := t_ih_l' h_mem, clear t_ih_l' t_ih_r', have t_l_h := valid'.erase_aux x h.left, cases t_l_h with t_l_valid t_l_size, rw size_balance_r t_l_valid.bal h.right.bal t_l_valid.sz h.right.sz (or.inl (exists.intro t_l.size (and.intro t_l_size h.bal.1))), rw [t_ih_l, h.sz.1], have h_pos_t_l_size := pos_size_of_mem h.left.sz h_mem, cases t_l.size with t_l_size, { cases h_pos_t_l_size }, simp [nat.succ_add] }, { rw [(valid'.glue h.left h.right h.bal.1).2, h.sz.1], refl }, { have t_ih_r := t_ih_r' h_mem, clear t_ih_l' t_ih_r', have t_r_h := valid'.erase_aux x h.right, cases t_r_h with t_r_valid t_r_size, rw size_balance_l h.left.bal t_r_valid.bal h.left.sz t_r_valid.sz (or.inr (exists.intro t_r.size (and.intro t_r_size h.bal.1))), rw [t_ih_r, h.sz.1], have h_pos_t_r_size := pos_size_of_mem h.right.sz h_mem, cases t_r.size with t_r_size, { cases h_pos_t_r_size }, simp [nat.succ_add, nat.add_succ] } }, end end end ordnode /-- An `ordset α` is a finite set of values, represented as a tree. The operations on this type maintain that the tree is balanced and correctly stores subtree sizes at each level. The correctness property of the tree is baked into the type, so all operations on this type are correct by construction. -/ def ordset (α : Type*) [preorder α] := {t : ordnode α // t.valid} namespace ordset open ordnode variable [preorder α] /-- O(1). The empty set. -/ def nil : ordset α := ⟨nil, ⟨⟩, ⟨⟩, ⟨⟩⟩ /-- O(1). Get the size of the set. -/ def size (s : ordset α) : ℕ := s.1.size /-- O(1). Construct a singleton set containing value `a`. -/ protected def singleton (a : α) : ordset α := ⟨singleton a, valid_singleton⟩ instance : has_emptyc (ordset α) := ⟨nil⟩ instance : inhabited (ordset α) := ⟨nil⟩ instance : has_singleton α (ordset α) := ⟨ordset.singleton⟩ /-- O(1). Is the set empty? -/ def empty (s : ordset α) : Prop := s = ∅ theorem empty_iff {s : ordset α} : s = ∅ ↔ s.1.empty := ⟨λ h, by cases h; exact rfl, λ h, by cases s; cases s_val; [exact rfl, cases h]⟩ instance : decidable_pred (@empty α _) := λ s, decidable_of_iff' _ empty_iff /-- O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, this replaces it. -/ protected def insert [is_total α (≤)] [@decidable_rel α (≤)] (x : α) (s : ordset α) : ordset α := ⟨ordnode.insert x s.1, insert.valid _ s.2⟩ instance [is_total α (≤)] [@decidable_rel α (≤)] : has_insert α (ordset α) := ⟨ordset.insert⟩ /-- O(log n). Insert an element into the set, preserving balance and the BST property. If an equivalent element is already in the set, the set is returned as is. -/ def insert' [is_total α (≤)] [@decidable_rel α (≤)] (x : α) (s : ordset α) : ordset α := ⟨insert' x s.1, insert'.valid _ s.2⟩ section variables [@decidable_rel α (≤)] /-- O(log n). Does the set contain the element `x`? That is, is there an element that is equivalent to `x` in the order? -/ def mem (x : α) (s : ordset α) : bool := x ∈ s.val /-- O(log n). Retrieve an element in the set that is equivalent to `x` in the order, if it exists. -/ def find (x : α) (s : ordset α) : option α := ordnode.find x s.val instance : has_mem α (ordset α) := ⟨λ x s, mem x s⟩ instance mem.decidable (x : α) (s : ordset α) : decidable (x ∈ s) := bool.decidable_eq _ _ theorem pos_size_of_mem {x : α} {t : ordset α} (h_mem : x ∈ t) : 0 < size t := begin simp [has_mem.mem, mem] at h_mem, apply ordnode.pos_size_of_mem t.property.sz h_mem, end end /-- O(log n). Remove an element from the set equivalent to `x`. Does nothing if there is no such element. -/ def erase [@decidable_rel α (≤)] (x : α) (s : ordset α) : ordset α := ⟨ordnode.erase x s.val, ordnode.erase.valid x s.property⟩ /-- O(n). Map a function across a tree, without changing the structure. -/ def map {β} [preorder β] (f : α → β) (f_strict_mono : strict_mono f) (s : ordset α) : ordset β := ⟨ordnode.map f s.val, ordnode.map.valid f_strict_mono s.property⟩ end ordset
166b87580e3a4c237f11bb41f75331d737dc1b2f
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/category_theory/sites/sieves.lean
fb8f4b64053a5d92a4d88557308931bd853c6248
[ "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,138
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, E. W. Ayers -/ import category_theory.over import category_theory.limits.shapes.finite_limits import category_theory.yoneda import order.complete_lattice import data.set.lattice /-! # Theory of sieves - For an object `X` of a category `C`, a `sieve X` is a set of morphisms to `X` which is closed under left-composition. - The complete lattice structure on sieves is given, as well as the Galois insertion given by downward-closing. - A `sieve X` (functorially) induces a presheaf on `C` together with a monomorphism to the yoneda embedding of `X`. ## Tags sieve, pullback -/ universes v₁ v₂ v₃ u₁ u₂ u₃ namespace category_theory open category limits variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] (F : C ⥤ D) variables {X Y Z : C} (f : Y ⟶ X) /-- A set of arrows all with codomain `X`. -/ @[derive complete_lattice] def presieve (X : C) := Π ⦃Y⦄, set (Y ⟶ X) namespace presieve instance : inhabited (presieve X) := ⟨⊤⟩ /-- Given a set of arrows `S` all with codomain `X`, and a set of arrows with codomain `Y` for each `f : Y ⟶ X` in `S`, produce a set of arrows with codomain `X`: `{ g ≫ f | (f : Y ⟶ X) ∈ S, (g : Z ⟶ Y) ∈ R f }`. -/ def bind (S : presieve X) (R : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, S f → presieve Y) : presieve X := λ Z h, ∃ (Y : C) (g : Z ⟶ Y) (f : Y ⟶ X) (H : S f), R H g ∧ g ≫ f = h @[simp] lemma bind_comp {S : presieve X} {R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → presieve Y} {g : Z ⟶ Y} (h₁ : S f) (h₂ : R h₁ g) : bind S R (g ≫ f) := ⟨_, _, _, h₁, h₂, rfl⟩ /-- The singleton presieve. -/ -- Note we can't make this into `has_singleton` because of the out-param. inductive singleton : presieve X | mk : singleton f @[simp] lemma singleton_eq_iff_domain (f g : Y ⟶ X) : singleton f g ↔ f = g := begin split, { rintro ⟨a, rfl⟩, refl }, { rintro rfl, apply singleton.mk, } end lemma singleton_self : singleton f f := singleton.mk /-- Pullback a set of arrows with given codomain along a fixed map, by taking the pullback in the category. This is not the same as the arrow set of `sieve.pullback`, but there is a relation between them in `pullback_arrows_comm`. -/ inductive pullback_arrows [has_pullbacks C] (R : presieve X) : presieve Y | mk (Z : C) (h : Z ⟶ X) : R h → pullback_arrows (pullback.snd : pullback h f ⟶ Y) lemma pullback_singleton [has_pullbacks C] (g : Z ⟶ X) : pullback_arrows f (singleton g) = singleton (pullback.snd : pullback g f ⟶ _) := begin ext W h, split, { rintro ⟨W, _, _, _⟩, exact singleton.mk }, { rintro ⟨_⟩, exact pullback_arrows.mk Z g singleton.mk } end /-- Construct the presieve given by the family of arrows indexed by `ι`. -/ inductive of_arrows {ι : Type*} (Y : ι → C) (f : Π i, Y i ⟶ X) : presieve X | mk (i : ι) : of_arrows (f i) lemma of_arrows_punit : of_arrows _ (λ _ : punit, f) = singleton f := begin ext Y g, split, { rintro ⟨_⟩, apply singleton.mk }, { rintro ⟨_⟩, exact of_arrows.mk punit.star }, end lemma of_arrows_pullback [has_pullbacks C] {ι : Type*} (Z : ι → C) (g : Π (i : ι), Z i ⟶ X) : of_arrows (λ i, pullback (g i) f) (λ i, pullback.snd) = pullback_arrows f (of_arrows Z g) := begin ext T h, split, { rintro ⟨hk⟩, exact pullback_arrows.mk _ _ (of_arrows.mk hk) }, { rintro ⟨W, k, hk₁⟩, cases hk₁ with i hi, apply of_arrows.mk }, end lemma of_arrows_bind {ι : Type*} (Z : ι → C) (g : Π (i : ι), Z i ⟶ X) (j : Π ⦃Y⦄ (f : Y ⟶ X), of_arrows Z g f → Type*) (W : Π ⦃Y⦄ (f : Y ⟶ X) H, j f H → C) (k : Π ⦃Y⦄ (f : Y ⟶ X) H i, W f H i ⟶ Y) : (of_arrows Z g).bind (λ Y f H, of_arrows (W f H) (k f H)) = of_arrows (λ (i : Σ i, j _ (of_arrows.mk i)), W (g i.1) _ i.2) (λ ij, k (g ij.1) _ ij.2 ≫ g ij.1) := begin ext Y f, split, { rintro ⟨_, _, _, ⟨i⟩, ⟨i'⟩, rfl⟩, exact of_arrows.mk (sigma.mk _ _) }, { rintro ⟨i⟩, exact bind_comp _ (of_arrows.mk _) (of_arrows.mk _) } end /-- Given a presieve on `F(X)`, we can define a presieve on `X` by taking the preimage via `F`. -/ def functor_pullback (R : presieve (F.obj X)) : presieve X := λ _ f, R (F.map f) @[simp] lemma functor_pullback_mem (R : presieve (F.obj X)) {Y} (f : Y ⟶ X) : R.functor_pullback F f ↔ R (F.map f) := iff.rfl @[simp] lemma functor_pullback_id (R : presieve X) : R.functor_pullback (𝟭 _) = R := rfl section functor_pushforward variables {E : Type u₃} [category.{v₃} E] (G : D ⥤ E) /-- Given a presieve on `X`, we can define a presieve on `F(X)` (which is actually a sieve) by taking the sieve generated by the image via `F`. -/ def functor_pushforward (S : presieve X) : presieve (F.obj X) := λ Y f, ∃ (Z : C) (g : Z ⟶ X) (h : Y ⟶ F.obj Z), S g ∧ f = h ≫ F.map g /-- An auxillary definition in order to fix the choice of the preimages between various definitions. -/ @[nolint has_inhabited_instance] structure functor_pushforward_structure (S : presieve X) {Y} (f : Y ⟶ F.obj X) := (preobj : C) (premap : preobj ⟶ X) (lift : Y ⟶ F.obj preobj) (cover : S premap) (fac : f = lift ≫ F.map premap) /-- The fixed choice of a preimage. -/ noncomputable def get_functor_pushforward_structure {F : C ⥤ D} {S : presieve X} {Y : D} {f : Y ⟶ F.obj X} (h : S.functor_pushforward F f) : functor_pushforward_structure F S f := by { choose Z f' g h₁ h using h, exact ⟨Z, f', g, h₁, h⟩ } lemma functor_pushforward_comp (R : presieve X) : R.functor_pushforward (F ⋙ G) = (R.functor_pushforward F).functor_pushforward G := begin ext x f, split, { rintro ⟨X, f₁, g₁, h₁, rfl⟩, exact ⟨F.obj X, F.map f₁, g₁, ⟨X, f₁, 𝟙 _, h₁, by simp⟩, rfl⟩ }, { rintro ⟨X, f₁, g₁, ⟨X', f₂, g₂, h₁, rfl⟩, rfl⟩, use ⟨X', f₂, g₁ ≫ G.map g₂, h₁, by simp⟩ } end lemma image_mem_functor_pushforward (R : presieve X) {f : Y ⟶ X} (h : R f) : R.functor_pushforward F (F.map f) := ⟨Y, f, 𝟙 _, h, by simp⟩ end functor_pushforward end presieve /-- For an object `X` of a category `C`, a `sieve X` is a set of morphisms to `X` which is closed under left-composition. -/ structure sieve {C : Type u₁} [category.{v₁} C] (X : C) := (arrows : presieve X) (downward_closed' : ∀ {Y Z f} (hf : arrows f) (g : Z ⟶ Y), arrows (g ≫ f)) namespace sieve instance : has_coe_to_fun (sieve X) (λ _, presieve X) := ⟨sieve.arrows⟩ initialize_simps_projections sieve (arrows → apply) variables {S R : sieve X} @[simp, priority 100] lemma downward_closed (S : sieve X) {f : Y ⟶ X} (hf : S f) (g : Z ⟶ Y) : S (g ≫ f) := S.downward_closed' hf g lemma arrows_ext : Π {R S : sieve X}, R.arrows = S.arrows → R = S | ⟨Ra, _⟩ ⟨Sa, _⟩ rfl := rfl @[ext] protected lemma ext {R S : sieve X} (h : ∀ ⦃Y⦄ (f : Y ⟶ X), R f ↔ S f) : R = S := arrows_ext $ funext $ λ x, funext $ λ f, propext $ h f protected lemma ext_iff {R S : sieve X} : R = S ↔ (∀ ⦃Y⦄ (f : Y ⟶ X), R f ↔ S f) := ⟨λ h Y f, h ▸ iff.rfl, sieve.ext⟩ open lattice /-- The supremum of a collection of sieves: the union of them all. -/ protected def Sup (𝒮 : set (sieve X)) : (sieve X) := { arrows := λ Y, {f | ∃ S ∈ 𝒮, sieve.arrows S f}, downward_closed' := λ Y Z f, by { rintro ⟨S, hS, hf⟩ g, exact ⟨S, hS, S.downward_closed hf _⟩ } } /-- The infimum of a collection of sieves: the intersection of them all. -/ protected def Inf (𝒮 : set (sieve X)) : (sieve X) := { arrows := λ Y, {f | ∀ S ∈ 𝒮, sieve.arrows S f}, downward_closed' := λ Y Z f hf g S H, S.downward_closed (hf S H) g } /-- The union of two sieves is a sieve. -/ protected def union (S R : sieve X) : sieve X := { arrows := λ Y f, S f ∨ R f, downward_closed' := by { rintros Y Z f (h | h) g; simp [h] } } /-- The intersection of two sieves is a sieve. -/ protected def inter (S R : sieve X) : sieve X := { arrows := λ Y f, S f ∧ R f, downward_closed' := by { rintros Y Z f ⟨h₁, h₂⟩ g, simp [h₁, h₂] } } /-- Sieves on an object `X` form a complete lattice. We generate this directly rather than using the galois insertion for nicer definitional properties. -/ instance : complete_lattice (sieve X) := { le := λ S R, ∀ ⦃Y⦄ (f : Y ⟶ X), S f → R f, le_refl := λ S f q, id, le_trans := λ S₁ S₂ S₃ S₁₂ S₂₃ Y f h, S₂₃ _ (S₁₂ _ h), le_antisymm := λ S R p q, sieve.ext (λ Y f, ⟨p _, q _⟩), top := { arrows := λ _, set.univ, downward_closed' := λ Y Z f g h, ⟨⟩ }, bot := { arrows := λ _, ∅, downward_closed' := λ _ _ _ p _, false.elim p }, sup := sieve.union, inf := sieve.inter, Sup := sieve.Sup, Inf := sieve.Inf, le_Sup := λ 𝒮 S hS Y f hf, ⟨S, hS, hf⟩, Sup_le := λ ℰ S hS Y f, by { rintro ⟨R, hR, hf⟩, apply hS R hR _ hf }, Inf_le := λ _ _ hS _ _ h, h _ hS, le_Inf := λ _ _ hS _ _ hf _ hR, hS _ hR _ hf, le_sup_left := λ _ _ _ _, or.inl, le_sup_right := λ _ _ _ _, or.inr, sup_le := λ _ _ _ a b _ _ hf, hf.elim (a _) (b _), inf_le_left := λ _ _ _ _, and.left, inf_le_right := λ _ _ _ _, and.right, le_inf := λ _ _ _ p q _ _ z, ⟨p _ z, q _ z⟩, le_top := λ _ _ _ _, trivial, bot_le := λ _ _ _, false.elim } /-- The maximal sieve always exists. -/ instance sieve_inhabited : inhabited (sieve X) := ⟨⊤⟩ @[simp] lemma Inf_apply {Ss : set (sieve X)} {Y} (f : Y ⟶ X) : Inf Ss f ↔ ∀ (S : sieve X) (H : S ∈ Ss), S f := iff.rfl @[simp] lemma Sup_apply {Ss : set (sieve X)} {Y} (f : Y ⟶ X) : Sup Ss f ↔ ∃ (S : sieve X) (H : S ∈ Ss), S f := iff.rfl @[simp] lemma inter_apply {R S : sieve X} {Y} (f : Y ⟶ X) : (R ⊓ S) f ↔ R f ∧ S f := iff.rfl @[simp] lemma union_apply {R S : sieve X} {Y} (f : Y ⟶ X) : (R ⊔ S) f ↔ R f ∨ S f := iff.rfl @[simp] lemma top_apply (f : Y ⟶ X) : (⊤ : sieve X) f := trivial /-- Generate the smallest sieve containing the given set of arrows. -/ @[simps] def generate (R : presieve X) : sieve X := { arrows := λ Z f, ∃ Y (h : Z ⟶ Y) (g : Y ⟶ X), R g ∧ h ≫ g = f, downward_closed' := begin rintro Y Z _ ⟨W, g, f, hf, rfl⟩ h, exact ⟨_, h ≫ g, _, hf, by simp⟩, end } /-- Given a presieve on `X`, and a sieve on each domain of an arrow in the presieve, we can bind to produce a sieve on `X`. -/ @[simps] def bind (S : presieve X) (R : Π ⦃Y⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) : sieve X := { arrows := S.bind (λ Y f h, R h), downward_closed' := begin rintro Y Z f ⟨W, f, h, hh, hf, rfl⟩ g, exact ⟨_, g ≫ f, _, hh, by simp [hf]⟩, end } open order lattice lemma sets_iff_generate (R : presieve X) (S : sieve X) : generate R ≤ S ↔ R ≤ S := ⟨λ H Y g hg, H _ ⟨_, 𝟙 _, _, hg, category.id_comp _⟩, λ ss Y f, begin rintro ⟨Z, f, g, hg, rfl⟩, exact S.downward_closed (ss Z hg) f, end⟩ /-- Show that there is a galois insertion (generate, set_over). -/ def gi_generate : galois_insertion (generate : presieve X → sieve X) arrows := { gc := sets_iff_generate, choice := λ 𝒢 _, generate 𝒢, choice_eq := λ _ _, rfl, le_l_u := λ S Y f hf, ⟨_, 𝟙 _, _, hf, category.id_comp _⟩ } lemma le_generate (R : presieve X) : R ≤ generate R := gi_generate.gc.le_u_l R @[simp] lemma generate_sieve (S : sieve X) : generate S = S := gi_generate.l_u_eq S /-- If the identity arrow is in a sieve, the sieve is maximal. -/ lemma id_mem_iff_eq_top : S (𝟙 X) ↔ S = ⊤ := ⟨λ h, top_unique $ λ Y f _, by simpa using downward_closed _ h f, λ h, h.symm ▸ trivial⟩ /-- If an arrow set contains a split epi, it generates the maximal sieve. -/ lemma generate_of_contains_split_epi {R : presieve X} (f : Y ⟶ X) [split_epi f] (hf : R f) : generate R = ⊤ := begin rw ← id_mem_iff_eq_top, exact ⟨_, section_ f, f, hf, by simp⟩, end @[simp] lemma generate_of_singleton_split_epi (f : Y ⟶ X) [split_epi f] : generate (presieve.singleton f) = ⊤ := generate_of_contains_split_epi f (presieve.singleton_self _) @[simp] lemma generate_top : generate (⊤ : presieve X) = ⊤ := generate_of_contains_split_epi (𝟙 _) ⟨⟩ /-- Given a morphism `h : Y ⟶ X`, send a sieve S on X to a sieve on Y as the inverse image of S with `_ ≫ h`. That is, `sieve.pullback S h := (≫ h) '⁻¹ S`. -/ @[simps] def pullback (h : Y ⟶ X) (S : sieve X) : sieve Y := { arrows := λ Y sl, S (sl ≫ h), downward_closed' := λ Z W f g h, by simp [g] } @[simp] lemma pullback_id : S.pullback (𝟙 _) = S := by simp [sieve.ext_iff] @[simp] lemma pullback_top {f : Y ⟶ X} : (⊤ : sieve X).pullback f = ⊤ := top_unique (λ _ g, id) lemma pullback_comp {f : Y ⟶ X} {g : Z ⟶ Y} (S : sieve X) : S.pullback (g ≫ f) = (S.pullback f).pullback g := by simp [sieve.ext_iff] @[simp] lemma pullback_inter {f : Y ⟶ X} (S R : sieve X) : (S ⊓ R).pullback f = S.pullback f ⊓ R.pullback f := by simp [sieve.ext_iff] lemma pullback_eq_top_iff_mem (f : Y ⟶ X) : S f ↔ S.pullback f = ⊤ := by rw [← id_mem_iff_eq_top, pullback_apply, category.id_comp] lemma pullback_eq_top_of_mem (S : sieve X) {f : Y ⟶ X} : S f → S.pullback f = ⊤ := (pullback_eq_top_iff_mem f).1 /-- Push a sieve `R` on `Y` forward along an arrow `f : Y ⟶ X`: `gf : Z ⟶ X` is in the sieve if `gf` factors through some `g : Z ⟶ Y` which is in `R`. -/ @[simps] def pushforward (f : Y ⟶ X) (R : sieve Y) : sieve X := { arrows := λ Z gf, ∃ g, g ≫ f = gf ∧ R g, downward_closed' := λ Z₁ Z₂ g ⟨j, k, z⟩ h, ⟨h ≫ j, by simp [k], by simp [z]⟩ } lemma pushforward_apply_comp {R : sieve Y} {Z : C} {g : Z ⟶ Y} (hg : R g) (f : Y ⟶ X) : R.pushforward f (g ≫ f) := ⟨g, rfl, hg⟩ lemma pushforward_comp {f : Y ⟶ X} {g : Z ⟶ Y} (R : sieve Z) : R.pushforward (g ≫ f) = (R.pushforward g).pushforward f := sieve.ext (λ W h, ⟨λ ⟨f₁, hq, hf₁⟩, ⟨f₁ ≫ g, by simpa, f₁, rfl, hf₁⟩, λ ⟨y, hy, z, hR, hz⟩, ⟨z, by rwa reassoc_of hR, hz⟩⟩) lemma galois_connection (f : Y ⟶ X) : galois_connection (sieve.pushforward f) (sieve.pullback f) := λ S R, ⟨λ hR Z g hg, hR _ ⟨g, rfl, hg⟩, λ hS Z g ⟨h, hg, hh⟩, hg ▸ hS h hh⟩ lemma pullback_monotone (f : Y ⟶ X) : monotone (sieve.pullback f) := (galois_connection f).monotone_u lemma pushforward_monotone (f : Y ⟶ X) : monotone (sieve.pushforward f) := (galois_connection f).monotone_l lemma le_pushforward_pullback (f : Y ⟶ X) (R : sieve Y) : R ≤ (R.pushforward f).pullback f := (galois_connection f).le_u_l _ lemma pullback_pushforward_le (f : Y ⟶ X) (R : sieve X) : (R.pullback f).pushforward f ≤ R := (galois_connection f).l_u_le _ lemma pushforward_union {f : Y ⟶ X} (S R : sieve Y) : (S ⊔ R).pushforward f = S.pushforward f ⊔ R.pushforward f := (galois_connection f).l_sup lemma pushforward_le_bind_of_mem (S : presieve X) (R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) (f : Y ⟶ X) (h : S f) : (R h).pushforward f ≤ bind S R := begin rintro Z _ ⟨g, rfl, hg⟩, exact ⟨_, g, f, h, hg, rfl⟩, end lemma le_pullback_bind (S : presieve X) (R : Π ⦃Y : C⦄ ⦃f : Y ⟶ X⦄, S f → sieve Y) (f : Y ⟶ X) (h : S f) : R h ≤ (bind S R).pullback f := begin rw ← galois_connection f, apply pushforward_le_bind_of_mem, end /-- If `f` is a monomorphism, the pushforward-pullback adjunction on sieves is coreflective. -/ def galois_coinsertion_of_mono (f : Y ⟶ X) [mono f] : galois_coinsertion (sieve.pushforward f) (sieve.pullback f) := begin apply (galois_connection f).to_galois_coinsertion, rintros S Z g ⟨g₁, hf, hg₁⟩, rw cancel_mono f at hf, rwa ← hf, end /-- If `f` is a split epi, the pushforward-pullback adjunction on sieves is reflective. -/ def galois_insertion_of_split_epi (f : Y ⟶ X) [split_epi f] : galois_insertion (sieve.pushforward f) (sieve.pullback f) := begin apply (galois_connection f).to_galois_insertion, intros S Z g hg, refine ⟨g ≫ section_ f, by simpa⟩, end lemma pullback_arrows_comm [has_pullbacks C] {X Y : C} (f : Y ⟶ X) (R : presieve X) : sieve.generate (R.pullback_arrows f) = (sieve.generate R).pullback f := begin ext Z g, split, { rintro ⟨_, h, k, hk, rfl⟩, cases hk with W g hg, change (sieve.generate R).pullback f (h ≫ pullback.snd), rw [sieve.pullback_apply, assoc, ← pullback.condition, ← assoc], exact sieve.downward_closed _ (sieve.le_generate R W hg) (h ≫ pullback.fst)}, { rintro ⟨W, h, k, hk, comm⟩, exact ⟨_, _, _, presieve.pullback_arrows.mk _ _ hk, pullback.lift_snd _ _ comm⟩ }, end section functor variables {E : Type u₃} [category.{v₃} E] (G : D ⥤ E) /-- If `R` is a sieve, then the `category_theory.presieve.functor_pullback` of `R` is actually a sieve. -/ @[simps] def functor_pullback (R : sieve (F.obj X)) : sieve X := { arrows := presieve.functor_pullback F R, downward_closed' := λ _ _ f hf g, begin unfold presieve.functor_pullback, rw F.map_comp, exact R.downward_closed hf (F.map g), end } @[simp] lemma functor_pullback_arrows (R : sieve (F.obj X)) : (R.functor_pullback F).arrows = R.arrows.functor_pullback F := rfl @[simp] lemma functor_pullback_id (R : sieve X) : R.functor_pullback (𝟭 _) = R := by { ext, refl } lemma functor_pullback_comp (R : sieve ((F ⋙ G).obj X)) : R.functor_pullback (F ⋙ G) = (R.functor_pullback G).functor_pullback F := by { ext, refl } lemma functor_pushforward_extend_eq {R : presieve X} : (generate R).arrows.functor_pushforward F = R.functor_pushforward F := begin ext Y f, split, { rintro ⟨X', g, f', ⟨X'', g', f'', h₁, rfl⟩, rfl⟩, exact ⟨X'', f'', f' ≫ F.map g', h₁, by simp⟩ }, { rintro ⟨X', g, f', h₁, h₂⟩, exact ⟨X', g, f', le_generate R _ h₁, h₂⟩ } end /-- The sieve generated by the image of `R` under `F`. -/ @[simps] def functor_pushforward (R : sieve X) : sieve (F.obj X) := { arrows := R.arrows.functor_pushforward F, downward_closed' := λ Y Z f h g, by { obtain ⟨X, α, β, hα, rfl⟩ := h, exact ⟨X, α, g ≫ β, hα, by simp⟩ } } @[simp] lemma functor_pushforward_id (R : sieve X) : R.functor_pushforward (𝟭 _) = R := begin ext X f, split, { intro hf, obtain ⟨X, g, h, hg, rfl⟩ := hf, exact R.downward_closed hg h, }, { intro hf, exact ⟨X, f, 𝟙 _, hf, by simp⟩ } end lemma functor_pushforward_comp (R : sieve X) : R.functor_pushforward (F ⋙ G) = (R.functor_pushforward F).functor_pushforward G := by { ext, simpa [R.arrows.functor_pushforward_comp F G] } lemma functor_galois_connection (X : C) : _root_.galois_connection (sieve.functor_pushforward F : sieve X → sieve (F.obj X)) (sieve.functor_pullback F) := begin intros R S, split, { intros hle X f hf, apply hle, refine ⟨X, f, 𝟙 _, hf, _⟩, rw category.id_comp, }, { rintros hle Y f ⟨X, g, h, hg, rfl⟩, apply sieve.downward_closed S, exact hle g hg, } end lemma functor_pullback_monotone (X : C) : monotone (sieve.functor_pullback F : sieve (F.obj X) → sieve X) := (functor_galois_connection F X).monotone_u lemma functor_pushforward_monotone (X : C) : monotone (sieve.functor_pushforward F : sieve X → sieve (F.obj X)) := (functor_galois_connection F X).monotone_l lemma le_functor_pushforward_pullback (R : sieve X) : R ≤ (R.functor_pushforward F).functor_pullback F := (functor_galois_connection F X).le_u_l _ lemma functor_pullback_pushforward_le (R : sieve (F.obj X)) : (R.functor_pullback F).functor_pushforward F ≤ R := (functor_galois_connection F X).l_u_le _ lemma functor_pushforward_union (S R : sieve X) : (S ⊔ R).functor_pushforward F = S.functor_pushforward F ⊔ R.functor_pushforward F := (functor_galois_connection F X).l_sup lemma functor_pullback_union (S R : sieve (F.obj X)) : (S ⊔ R).functor_pullback F = S.functor_pullback F ⊔ R.functor_pullback F := rfl lemma functor_pullback_inter (S R : sieve (F.obj X)) : (S ⊓ R).functor_pullback F = S.functor_pullback F ⊓ R.functor_pullback F := rfl lemma functor_pushforward_bot (F : C ⥤ D) (X : C) : (⊥ : sieve X).functor_pushforward F = ⊥ := (functor_galois_connection F X).l_bot @[simp] lemma functor_pullback_bot (F : C ⥤ D) (X : C) : (⊥ : sieve (F.obj X)).functor_pullback F = ⊥ := rfl @[simp] lemma functor_pullback_top (F : C ⥤ D) (X : C) : (⊤ : sieve (F.obj X)).functor_pullback F = ⊤ := rfl lemma image_mem_functor_pushforward (R : sieve X) {V} {f : V ⟶ X} (h : R f) : R.functor_pushforward F (F.map f) := ⟨V, f, 𝟙 _, h, by simp⟩ /-- When `F` is essentially surjective and full, the galois connection is a galois insertion. -/ def ess_surj_full_functor_galois_insertion [ess_surj F] [full F] (X : C) : galois_insertion (sieve.functor_pushforward F : sieve X → sieve (F.obj X)) (sieve.functor_pullback F) := begin apply (functor_galois_connection F X).to_galois_insertion, intros S Y f hf, refine ⟨_, F.preimage ((F.obj_obj_preimage_iso Y).hom ≫ f), (F.obj_obj_preimage_iso Y).inv, _⟩, simpa using S.downward_closed hf _, end /-- When `F` is fully faithful, the galois connection is a galois coinsertion. -/ def fully_faithful_functor_galois_coinsertion [full F] [faithful F] (X : C) : galois_coinsertion (sieve.functor_pushforward F : sieve X → sieve (F.obj X)) (sieve.functor_pullback F) := begin apply (functor_galois_connection F X).to_galois_coinsertion, rintros S Y f ⟨Z, g, h, h₁, h₂⟩, rw [←F.image_preimage h, ←F.map_comp] at h₂, rw F.map_injective h₂, exact S.downward_closed h₁ _, end end functor /-- A sieve induces a presheaf. -/ @[simps] def functor (S : sieve X) : Cᵒᵖ ⥤ Type v₁ := { obj := λ Y, {g : Y.unop ⟶ X // S g}, map := λ Y Z f g, ⟨f.unop ≫ g.1, downward_closed _ g.2 _⟩ } /-- If a sieve S is contained in a sieve T, then we have a morphism of presheaves on their induced presheaves. -/ @[simps] def nat_trans_of_le {S T : sieve X} (h : S ≤ T) : S.functor ⟶ T.functor := { app := λ Y f, ⟨f.1, h _ f.2⟩ }. /-- The natural inclusion from the functor induced by a sieve to the yoneda embedding. -/ @[simps] def functor_inclusion (S : sieve X) : S.functor ⟶ yoneda.obj X := { app := λ Y f, f.1 }. lemma nat_trans_of_le_comm {S T : sieve X} (h : S ≤ T) : nat_trans_of_le h ≫ functor_inclusion _ = functor_inclusion _ := rfl /-- The presheaf induced by a sieve is a subobject of the yoneda embedding. -/ instance functor_inclusion_is_mono : mono S.functor_inclusion := ⟨λ Z f g h, by { ext Y y, apply congr_fun (nat_trans.congr_app h Y) y }⟩ /-- A natural transformation to a representable functor induces a sieve. This is the left inverse of `functor_inclusion`, shown in `sieve_of_functor_inclusion`. -/ -- TODO: Show that when `f` is mono, this is right inverse to `functor_inclusion` up to isomorphism. @[simps] def sieve_of_subfunctor {R} (f : R ⟶ yoneda.obj X) : sieve X := { arrows := λ Y g, ∃ t, f.app (opposite.op Y) t = g, downward_closed' := λ Y Z _, begin rintro ⟨t, rfl⟩ g, refine ⟨R.map g.op t, _⟩, rw functor_to_types.naturality _ _ f, simp, end } lemma sieve_of_subfunctor_functor_inclusion : sieve_of_subfunctor S.functor_inclusion = S := begin ext, simp only [functor_inclusion_app, sieve_of_subfunctor_apply, subtype.val_eq_coe], split, { rintro ⟨⟨f, hf⟩, rfl⟩, exact hf }, { intro hf, exact ⟨⟨_, hf⟩, rfl⟩ } end instance functor_inclusion_top_is_iso : is_iso ((⊤ : sieve X).functor_inclusion) := ⟨⟨{ app := λ Y a, ⟨a, ⟨⟩⟩ }, by tidy⟩⟩ end sieve end category_theory
316051c2f26f6a72433fbe54aa68832a97755fe6
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/topology/uniform_space/cauchy.lean
6f3ac6413e588447e71e7bd107c637d2f245eb6a
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
28,871
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 topology.uniform_space.basic import topology.bases import data.set.intervals /-! # Theory of Cauchy filters in uniform spaces. Complete uniform spaces. Totally bounded subsets. -/ universes u v open filter topological_space set classical uniform_space open_locale classical uniformity topological_space filter variables {α : Type u} {β : Type v} [uniform_space α] /-- A filter `f` is Cauchy if for every entourage `r`, there exists an `s ∈ f` such that `s × s ⊆ r`. This is a generalization of Cauchy sequences, because if `a : ℕ → α` then the filter of sets containing cofinitely many of the `a n` is Cauchy iff `a` is a Cauchy sequence. -/ def cauchy (f : filter α) := ne_bot f ∧ f ×ᶠ f ≤ (𝓤 α) /-- A set `s` is called *complete*, if any Cauchy filter `f` such that `s ∈ f` has a limit in `s` (formally, it satisfies `f ≤ 𝓝 x` for some `x ∈ s`). -/ def is_complete (s : set α) := ∀f, cauchy f → f ≤ 𝓟 s → ∃x∈s, f ≤ 𝓝 x lemma filter.has_basis.cauchy_iff {p : β → Prop} {s : β → set (α × α)} (h : (𝓤 α).has_basis p s) {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ i, p i → ∃ t ∈ f, ∀ x y ∈ t, (x, y) ∈ s i)) := and_congr iff.rfl $ (f.basis_sets.prod_self.le_basis_iff h).trans $ by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id] lemma cauchy_iff' {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, ∀ x y ∈ t, (x, y) ∈ s)) := (𝓤 α).basis_sets.cauchy_iff lemma cauchy_iff {f : filter α} : cauchy f ↔ (ne_bot f ∧ (∀ s ∈ 𝓤 α, ∃t∈f, (set.prod t t) ⊆ s)) := (𝓤 α).basis_sets.cauchy_iff.trans $ by simp only [subset_def, prod.forall, mem_prod_eq, and_imp, id] lemma cauchy_map_iff {l : filter β} {f : β → α} : cauchy (l.map f) ↔ (ne_bot l ∧ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α)) := by rw [cauchy, map_ne_bot_iff, prod_map_map_eq, tendsto] lemma cauchy_map_iff' {l : filter β} [hl : ne_bot l] {f : β → α} : cauchy (l.map f) ↔ tendsto (λp:β×β, (f p.1, f p.2)) (l ×ᶠ l) (𝓤 α) := cauchy_map_iff.trans $ and_iff_right hl lemma cauchy.mono {f g : filter α} [hg : ne_bot g] (h_c : cauchy f) (h_le : g ≤ f) : cauchy g := ⟨hg, le_trans (filter.prod_mono h_le h_le) h_c.right⟩ lemma cauchy.mono' {f g : filter α} (h_c : cauchy f) (hg : ne_bot g) (h_le : g ≤ f) : cauchy g := h_c.mono h_le lemma cauchy_nhds {a : α} : cauchy (𝓝 a) := ⟨nhds_ne_bot, calc 𝓝 a ×ᶠ 𝓝 a = (𝓤 α).lift (λs:set (α×α), (𝓤 α).lift' (λt:set(α×α), set.prod {y : α | (y, a) ∈ s} {y : α | (a, y) ∈ t})) : nhds_nhds_eq_uniformity_uniformity_prod ... ≤ (𝓤 α).lift' (λs:set (α×α), comp_rel s s) : le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le_of_le hs $ principal_mono.mpr $ assume ⟨x, y⟩ ⟨(hx : (x, a) ∈ s), (hy : (a, y) ∈ s)⟩, ⟨a, hx, hy⟩ ... ≤ 𝓤 α : comp_le_uniformity⟩ lemma cauchy_pure {a : α} : cauchy (pure a) := cauchy_nhds.mono (pure_le_nhds a) lemma filter.tendsto.cauchy_map {l : filter β} [ne_bot l] {f : β → α} {a : α} (h : tendsto f l (𝓝 a)) : cauchy (map f l) := cauchy_nhds.mono h /-- The common part of the proofs of `le_nhds_of_cauchy_adhp` and `sequentially_complete.le_nhds_of_seq_tendsto_nhds`: if for any entourage `s` one can choose a set `t ∈ f` of diameter `s` such that it contains a point `y` with `(x, y) ∈ s`, then `f` converges to `x`. -/ lemma le_nhds_of_cauchy_adhp_aux {f : filter α} {x : α} (adhs : ∀ s ∈ 𝓤 α, ∃ t ∈ f, (set.prod t t ⊆ s) ∧ ∃ y, (x, y) ∈ s ∧ y ∈ t) : f ≤ 𝓝 x := begin -- Consider a neighborhood `s` of `x` assume s hs, -- Take an entourage twice smaller than `s` rcases comp_mem_uniformity_sets (mem_nhds_uniformity_iff_right.1 hs) with ⟨U, U_mem, hU⟩, -- Take a set `t ∈ f`, `t × t ⊆ U`, and a point `y ∈ t` such that `(x, y) ∈ U` rcases adhs U U_mem with ⟨t, t_mem, ht, y, hxy, hy⟩, apply mem_of_superset t_mem, -- Given a point `z ∈ t`, we have `(x, y) ∈ U` and `(y, z) ∈ t × t ⊆ U`, hence `z ∈ s` exact (λ z hz, hU (prod_mk_mem_comp_rel hxy (ht $ mk_mem_prod hy hz)) rfl) end /-- If `x` is an adherent (cluster) point for a Cauchy filter `f`, then it is a limit point for `f`. -/ lemma le_nhds_of_cauchy_adhp {f : filter α} {x : α} (hf : cauchy f) (adhs : cluster_pt x f) : f ≤ 𝓝 x := le_nhds_of_cauchy_adhp_aux begin assume s hs, obtain ⟨t, t_mem, ht⟩ : ∃ t ∈ f, set.prod t t ⊆ s, from (cauchy_iff.1 hf).2 s hs, use [t, t_mem, ht], exact (forall_mem_nonempty_iff_ne_bot.2 adhs _ (inter_mem_inf (mem_nhds_left x hs) t_mem )) end lemma le_nhds_iff_adhp_of_cauchy {f : filter α} {x : α} (hf : cauchy f) : f ≤ 𝓝 x ↔ cluster_pt x f := ⟨assume h, cluster_pt.of_le_nhds' h hf.1, le_nhds_of_cauchy_adhp hf⟩ lemma cauchy.map [uniform_space β] {f : filter α} {m : α → β} (hf : cauchy f) (hm : uniform_continuous m) : cauchy (map m f) := ⟨hf.1.map _, calc map m f ×ᶠ map m f = map (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_map_map_eq ... ≤ map (λp:α×α, (m p.1, m p.2)) (𝓤 α) : map_mono hf.right ... ≤ 𝓤 β : hm⟩ lemma cauchy.comap [uniform_space β] {f : filter β} {m : α → β} (hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) [ne_bot (comap m f)] : cauchy (comap m f) := ⟨‹_›, calc comap m f ×ᶠ comap m f = comap (λp:α×α, (m p.1, m p.2)) (f ×ᶠ f) : filter.prod_comap_comap_eq ... ≤ comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) : comap_mono hf.right ... ≤ 𝓤 α : hm⟩ lemma cauchy.comap' [uniform_space β] {f : filter β} {m : α → β} (hf : cauchy f) (hm : comap (λp:α×α, (m p.1, m p.2)) (𝓤 β) ≤ 𝓤 α) (hb : ne_bot (comap m f)) : cauchy (comap m f) := hf.comap hm /-- Cauchy sequences. Usually defined on ℕ, but often it is also useful to say that a function defined on ℝ is Cauchy at +∞ to deduce convergence. Therefore, we define it in a type class that is general enough to cover both ℕ and ℝ, which are the main motivating examples. -/ def cauchy_seq [semilattice_sup β] (u : β → α) := cauchy (at_top.map u) lemma cauchy_seq.mem_entourage {ι : Type*} [nonempty ι] [linear_order ι] {u : ι → α} (h : cauchy_seq u) {V : set (α × α)} (hV : V ∈ 𝓤 α) : ∃ k₀, ∀ i j, k₀ ≤ i → k₀ ≤ j → (u i, u j) ∈ V := begin have := h.right hV, obtain ⟨⟨i₀, j₀⟩, H⟩ : ∃ a, ∀ b : ι × ι, b ≥ a → prod.map u u b ∈ V, by rwa [prod_map_at_top_eq, mem_map, mem_at_top_sets] at this, refine ⟨max i₀ j₀, _⟩, intros i j hi hj, exact H (i, j) ⟨le_of_max_le_left hi, le_of_max_le_right hj⟩, end lemma filter.tendsto.cauchy_seq [semilattice_sup β] [nonempty β] {f : β → α} {x} (hx : tendsto f at_top (𝓝 x)) : cauchy_seq f := hx.cauchy_map lemma cauchy_seq_const (x : α) : cauchy_seq (λ n : ℕ, x) := tendsto_const_nhds.cauchy_seq lemma cauchy_seq_iff_tendsto [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ tendsto (prod.map u u) at_top (𝓤 α) := cauchy_map_iff'.trans $ by simp only [prod_at_top_at_top_eq, prod.map_def] lemma cauchy_seq.subseq_subseq_mem {V : ℕ → set (α × α)} (hV : ∀ n, V n ∈ 𝓤 α) {u : ℕ → α} (hu : cauchy_seq u) {f g : ℕ → ℕ} (hf : tendsto f at_top at_top) (hg : tendsto g at_top at_top) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, ((u ∘ f ∘ φ) n, (u ∘ g ∘ φ) n) ∈ V n := begin rw cauchy_seq_iff_tendsto at hu, exact ((hu.comp $ hf.prod_at_top hg).comp tendsto_at_top_diagonal).subseq_mem hV, end lemma cauchy_seq_iff' {u : ℕ → α} : cauchy_seq u ↔ ∀ V ∈ 𝓤 α, ∀ᶠ k in at_top, k ∈ (prod.map u u) ⁻¹' V := by simpa only [cauchy_seq_iff_tendsto] lemma cauchy_seq_iff {u : ℕ → α} : cauchy_seq u ↔ ∀ V ∈ 𝓤 α, ∃ N, ∀ k ≥ N, ∀ l ≥ N, (u k, u l) ∈ V := by simp [cauchy_seq_iff', filter.eventually_at_top_prod_self', prod_map] lemma cauchy_seq.subseq_mem {V : ℕ → set (α × α)} (hV : ∀ n, V n ∈ 𝓤 α) {u : ℕ → α} (hu : cauchy_seq u) : ∃ φ : ℕ → ℕ, strict_mono φ ∧ ∀ n, (u $ φ (n + 1), u $ φ n) ∈ V n := begin have : ∀ n, ∃ N, ∀ k ≥ N, ∀ l ≥ k, (u l, u k) ∈ V n, { intro n, rw [cauchy_seq_iff] at hu, rcases hu _ (hV n) with ⟨N, H⟩, exact ⟨N, λ k hk l hl, H _ (le_trans hk hl) _ hk ⟩ }, obtain ⟨φ : ℕ → ℕ, φ_extr : strict_mono φ, hφ : ∀ n, ∀ l ≥ φ n, (u l, u $ φ n) ∈ V n⟩ := extraction_forall_of_eventually' this, exact ⟨φ, φ_extr, λ n, hφ _ _ (φ_extr $ lt_add_one n).le⟩, end /-- If a Cauchy sequence has a convergent subsequence, then it converges. -/ lemma tendsto_nhds_of_cauchy_seq_of_subseq [semilattice_sup β] {u : β → α} (hu : cauchy_seq u) {ι : Type*} {f : ι → β} {p : filter ι} [ne_bot p] (hf : tendsto f p at_top) {a : α} (ha : tendsto (u ∘ f) p (𝓝 a)) : tendsto u at_top (𝓝 a) := le_nhds_of_cauchy_adhp hu (map_cluster_pt_of_comp hf ha) @[nolint ge_or_gt] -- see Note [nolint_ge] lemma filter.has_basis.cauchy_seq_iff {γ} [nonempty β] [semilattice_sup β] {u : β → α} {p : γ → Prop} {s : γ → set (α × α)} (h : (𝓤 α).has_basis p s) : cauchy_seq u ↔ ∀ i, p i → ∃N, ∀m n≥N, (u m, u n) ∈ s i := begin rw [cauchy_seq_iff_tendsto, ← prod_at_top_at_top_eq], refine (at_top_basis.prod_self.tendsto_iff h).trans _, simp only [exists_prop, true_and, maps_to, preimage, subset_def, prod.forall, mem_prod_eq, mem_set_of_eq, mem_Ici, and_imp, prod.map] end lemma filter.has_basis.cauchy_seq_iff' {γ} [nonempty β] [semilattice_sup β] {u : β → α} {p : γ → Prop} {s : γ → set (α × α)} (H : (𝓤 α).has_basis p s) : cauchy_seq u ↔ ∀ i, p i → ∃N, ∀n≥N, (u n, u N) ∈ s i := begin refine H.cauchy_seq_iff.trans ⟨λ h i hi, _, λ h i hi, _⟩, { exact (h i hi).imp (λ N hN n hn, hN n N hn (le_refl N)) }, { rcases comp_symm_of_uniformity (H.mem_of_mem hi) with ⟨t, ht, ht', hts⟩, rcases H.mem_iff.1 ht with ⟨j, hj, hjt⟩, refine (h j hj).imp (λ N hN m n hm hn, hts ⟨u N, hjt _, ht' $ hjt _⟩), { exact hN m hm }, { exact hN n hn } } end lemma cauchy_seq_of_controlled [semilattice_sup β] [nonempty β] (U : β → set (α × α)) (hU : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) {f : β → α} (hf : ∀ {N m n : β}, N ≤ m → N ≤ n → (f m, f n) ∈ U N) : cauchy_seq f := cauchy_seq_iff_tendsto.2 begin assume s hs, rw [mem_map, mem_at_top_sets], cases hU s hs with N hN, refine ⟨(N, N), λ mn hmn, _⟩, cases mn with m n, exact hN (hf hmn.1 hmn.2) end /-- A complete space is defined here using uniformities. A uniform space is complete if every Cauchy filter converges. -/ class complete_space (α : Type u) [uniform_space α] : Prop := (complete : ∀{f:filter α}, cauchy f → ∃x, f ≤ 𝓝 x) lemma complete_univ {α : Type u} [uniform_space α] [complete_space α] : is_complete (univ : set α) := begin assume f hf _, rcases complete_space.complete hf with ⟨x, hx⟩, exact ⟨x, mem_univ x, hx⟩ end lemma cauchy_prod [uniform_space β] {f : filter α} {g : filter β} : cauchy f → cauchy g → cauchy (f ×ᶠ g) | ⟨f_proper, hf⟩ ⟨g_proper, hg⟩ := ⟨filter.prod_ne_bot.2 ⟨f_proper, g_proper⟩, let p_α := λp:(α×β)×(α×β), (p.1.1, p.2.1), p_β := λp:(α×β)×(α×β), (p.1.2, p.2.2) in suffices (f.prod f).comap p_α ⊓ (g.prod g).comap p_β ≤ (𝓤 α).comap p_α ⊓ (𝓤 β).comap p_β, by simpa [uniformity_prod, filter.prod, filter.comap_inf, filter.comap_comap, (∘), inf_assoc, inf_comm, inf_left_comm], inf_le_inf (filter.comap_mono hf) (filter.comap_mono hg)⟩ instance complete_space.prod [uniform_space β] [complete_space α] [complete_space β] : complete_space (α × β) := { complete := λ f hf, let ⟨x1, hx1⟩ := complete_space.complete $ hf.map uniform_continuous_fst in let ⟨x2, hx2⟩ := complete_space.complete $ hf.map uniform_continuous_snd in ⟨(x1, x2), by rw [nhds_prod_eq, filter.prod_def]; from filter.le_lift (λ s hs, filter.le_lift' $ λ t ht, have H1 : prod.fst ⁻¹' s ∈ f.sets := hx1 hs, have H2 : prod.snd ⁻¹' t ∈ f.sets := hx2 ht, filter.inter_mem H1 H2)⟩ } /--If `univ` is complete, the space is a complete space -/ lemma complete_space_of_is_complete_univ (h : is_complete (univ : set α)) : complete_space α := ⟨λ f hf, let ⟨x, _, hx⟩ := h f hf ((@principal_univ α).symm ▸ le_top) in ⟨x, hx⟩⟩ lemma complete_space_iff_is_complete_univ : complete_space α ↔ is_complete (univ : set α) := ⟨@complete_univ α _, complete_space_of_is_complete_univ⟩ lemma cauchy_iff_exists_le_nhds [complete_space α] {l : filter α} [ne_bot l] : cauchy l ↔ (∃x, l ≤ 𝓝 x) := ⟨complete_space.complete, assume ⟨x, hx⟩, cauchy_nhds.mono hx⟩ lemma cauchy_map_iff_exists_tendsto [complete_space α] {l : filter β} {f : β → α} [ne_bot l] : cauchy (l.map f) ↔ (∃x, tendsto f l (𝓝 x)) := cauchy_iff_exists_le_nhds /-- A Cauchy sequence in a complete space converges -/ theorem cauchy_seq_tendsto_of_complete [semilattice_sup β] [complete_space α] {u : β → α} (H : cauchy_seq u) : ∃x, tendsto u at_top (𝓝 x) := complete_space.complete H /-- If `K` is a complete subset, then any cauchy sequence in `K` converges to a point in `K` -/ lemma cauchy_seq_tendsto_of_is_complete [semilattice_sup β] {K : set α} (h₁ : is_complete K) {u : β → α} (h₂ : ∀ n, u n ∈ K) (h₃ : cauchy_seq u) : ∃ v ∈ K, tendsto u at_top (𝓝 v) := h₁ _ h₃ $ le_principal_iff.2 $ mem_map_iff_exists_image.2 ⟨univ, univ_mem, by { simp only [image_univ], rintros _ ⟨n, rfl⟩, exact h₂ n }⟩ theorem cauchy.le_nhds_Lim [complete_space α] [nonempty α] {f : filter α} (hf : cauchy f) : f ≤ 𝓝 (Lim f) := le_nhds_Lim (complete_space.complete hf) theorem cauchy_seq.tendsto_lim [semilattice_sup β] [complete_space α] [nonempty α] {u : β → α} (h : cauchy_seq u) : tendsto u at_top (𝓝 $ lim at_top u) := h.le_nhds_Lim lemma is_closed.is_complete [complete_space α] {s : set α} (h : is_closed s) : is_complete s := λ f cf fs, let ⟨x, hx⟩ := complete_space.complete cf in ⟨x, is_closed_iff_cluster_pt.mp h x (cf.left.mono (le_inf hx fs)), hx⟩ /-- A set `s` is totally bounded if for every entourage `d` there is a finite set of points `t` such that every element of `s` is `d`-near to some element of `t`. -/ def totally_bounded (s : set α) : Prop := ∀d ∈ 𝓤 α, ∃t : set α, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) theorem totally_bounded_iff_subset {s : set α} : totally_bounded s ↔ ∀d ∈ 𝓤 α, ∃t ⊆ s, finite t ∧ s ⊆ (⋃y∈t, {x | (x,y) ∈ d}) := ⟨λ H d hd, begin rcases comp_symm_of_uniformity hd with ⟨r, hr, rs, rd⟩, rcases H r hr with ⟨k, fk, ks⟩, let u := {y ∈ k | ∃ x, x ∈ s ∧ (x, y) ∈ r}, let f : u → α := λ x, classical.some x.2.2, have : ∀ x : u, f x ∈ s ∧ (f x, x.1) ∈ r := λ x, classical.some_spec x.2.2, refine ⟨range f, _, _, _⟩, { exact range_subset_iff.2 (λ x, (this x).1) }, { have : finite u := fk.subset (λ x h, h.1), exact ⟨@set.fintype_range _ _ _ _ this.fintype⟩ }, { intros x xs, have := ks xs, simp at this, rcases this with ⟨y, hy, xy⟩, let z : coe_sort u := ⟨y, hy, x, xs, xy⟩, exact mem_bUnion_iff.2 ⟨_, ⟨z, rfl⟩, rd $ mem_comp_rel.2 ⟨_, xy, rs (this z).2⟩⟩ } end, λ H d hd, let ⟨t, _, ht⟩ := H d hd in ⟨t, ht⟩⟩ lemma totally_bounded_of_forall_symm {s : set α} (h : ∀ V ∈ 𝓤 α, symmetric_rel V → ∃ t : set α, finite t ∧ s ⊆ ⋃ y ∈ t, ball y V) : totally_bounded s := begin intros V V_in, rcases h _ (symmetrize_mem_uniformity V_in) (symmetric_symmetrize_rel V) with ⟨t, tfin, h⟩, refine ⟨t, tfin, subset.trans h _⟩, mono, intros x x_in z z_in, exact z_in.right end lemma totally_bounded_subset {s₁ s₂ : set α} (hs : s₁ ⊆ s₂) (h : totally_bounded s₂) : totally_bounded s₁ := assume d hd, let ⟨t, ht₁, ht₂⟩ := h d hd in ⟨t, ht₁, subset.trans hs ht₂⟩ lemma totally_bounded_empty : totally_bounded (∅ : set α) := λ d hd, ⟨∅, finite_empty, empty_subset _⟩ /-- The closure of a totally bounded set is totally bounded. -/ lemma totally_bounded.closure {s : set α} (h : totally_bounded s) : totally_bounded (closure s) := assume t ht, let ⟨t', ht', hct', htt'⟩ := mem_uniformity_is_closed ht, ⟨c, hcf, hc⟩ := h t' ht' in ⟨c, hcf, calc closure s ⊆ closure (⋃ (y : α) (H : y ∈ c), {x : α | (x, y) ∈ t'}) : closure_mono hc ... = _ : is_closed.closure_eq $ is_closed_bUnion hcf $ assume i hi, continuous_iff_is_closed.mp (continuous_id.prod_mk continuous_const) _ hct' ... ⊆ _ : bUnion_subset $ assume i hi, subset.trans (assume x, @htt' (x, i)) (subset_bUnion_of_mem hi)⟩ /-- The image of a totally bounded set under a unifromly continuous map is totally bounded. -/ lemma totally_bounded.image [uniform_space β] {f : α → β} {s : set α} (hs : totally_bounded s) (hf : uniform_continuous f) : totally_bounded (f '' s) := assume t ht, have {p:α×α | (f p.1, f p.2) ∈ t} ∈ 𝓤 α, from hf ht, let ⟨c, hfc, hct⟩ := hs _ this in ⟨f '' c, hfc.image f, begin simp [image_subset_iff], simp [subset_def] at hct, intros x hx, simp, exact hct x hx end⟩ lemma ultrafilter.cauchy_of_totally_bounded {s : set α} (f : ultrafilter α) (hs : totally_bounded s) (h : ↑f ≤ 𝓟 s) : cauchy (f : filter α) := ⟨f.ne_bot', assume t ht, let ⟨t', ht'₁, ht'_symm, ht'_t⟩ := comp_symm_of_uniformity ht in let ⟨i, hi, hs_union⟩ := hs t' ht'₁ in have (⋃y∈i, {x | (x,y) ∈ t'}) ∈ f, from mem_of_superset (le_principal_iff.mp h) hs_union, have ∃y∈i, {x | (x,y) ∈ t'} ∈ f, from (ultrafilter.finite_bUnion_mem_iff hi).1 this, let ⟨y, hy, hif⟩ := this in have set.prod {x | (x,y) ∈ t'} {x | (x,y) ∈ t'} ⊆ comp_rel t' t', from assume ⟨x₁, x₂⟩ ⟨(h₁ : (x₁, y) ∈ t'), (h₂ : (x₂, y) ∈ t')⟩, ⟨y, h₁, ht'_symm h₂⟩, mem_of_superset (prod_mem_prod hif hif) (subset.trans this ht'_t)⟩ lemma totally_bounded_iff_filter {s : set α} : totally_bounded s ↔ (∀f, ne_bot f → f ≤ 𝓟 s → ∃c ≤ f, cauchy c) := begin split, { introsI H f hf hfs, exact ⟨ultrafilter.of f, ultrafilter.of_le f, (ultrafilter.of f).cauchy_of_totally_bounded H ((ultrafilter.of_le f).trans hfs)⟩ }, { intros H d hd, contrapose! H with hd_cover, set f := ⨅ t : finset α, 𝓟 (s \ ⋃ y ∈ t, {x | (x, y) ∈ d}), have : ne_bot f, { refine infi_ne_bot_of_directed' (directed_of_sup _) _, { intros t₁ t₂ h, exact principal_mono.2 (diff_subset_diff_right $ bUnion_subset_bUnion_left h) }, { intro t, simpa [nonempty_diff] using hd_cover t t.finite_to_set } }, have : f ≤ 𝓟 s, from infi_le_of_le ∅ (by simp), refine ⟨f, ‹_›, ‹_›, λ c hcf hc, _⟩, rcases mem_prod_same_iff.1 (hc.2 hd) with ⟨m, hm, hmd⟩, have : m ∩ s ∈ c, from inter_mem hm (le_principal_iff.mp (hcf.trans ‹_›)), rcases hc.1.nonempty_of_mem this with ⟨y, hym, hys⟩, set ys := ⋃ y' ∈ ({y} : finset α), {x | (x, y') ∈ d}, have : m ⊆ ys, by simpa [ys] using λ x hx, hmd (mk_mem_prod hx hym), have : c ≤ 𝓟 (s \ ys) := hcf.trans (infi_le_of_le {y} le_rfl), refine hc.1.ne (empty_mem_iff_bot.mp _), filter_upwards [le_principal_iff.1 this, hm], refine λ x hx hxm, hx.2 _, simpa [ys] using hmd (mk_mem_prod hxm hym) } end lemma totally_bounded_iff_ultrafilter {s : set α} : totally_bounded s ↔ (∀f : ultrafilter α, ↑f ≤ 𝓟 s → cauchy (f : filter α)) := begin refine ⟨λ hs f, f.cauchy_of_totally_bounded hs, λ H, totally_bounded_iff_filter.2 _⟩, introsI f hf hfs, exact ⟨ultrafilter.of f, ultrafilter.of_le f, H _ ((ultrafilter.of_le f).trans hfs)⟩ end lemma compact_iff_totally_bounded_complete {s : set α} : is_compact s ↔ totally_bounded s ∧ is_complete s := ⟨λ hs, ⟨totally_bounded_iff_ultrafilter.2 (λ f hf, let ⟨x, xs, fx⟩ := is_compact_iff_ultrafilter_le_nhds.1 hs f hf in cauchy_nhds.mono fx), λ f fc fs, let ⟨a, as, fa⟩ := @hs f fc.1 fs in ⟨a, as, le_nhds_of_cauchy_adhp fc fa⟩⟩, λ ⟨ht, hc⟩, is_compact_iff_ultrafilter_le_nhds.2 (λf hf, hc _ (totally_bounded_iff_ultrafilter.1 ht f hf) hf)⟩ lemma is_compact.totally_bounded {s : set α} (h : is_compact s) : totally_bounded s := (compact_iff_totally_bounded_complete.1 h).1 lemma is_compact.is_complete {s : set α} (h : is_compact s) : is_complete s := (compact_iff_totally_bounded_complete.1 h).2 @[priority 100] -- see Note [lower instance priority] instance complete_of_compact {α : Type u} [uniform_space α] [compact_space α] : complete_space α := ⟨λf hf, by simpa using (compact_iff_totally_bounded_complete.1 compact_univ).2 f hf⟩ lemma compact_of_totally_bounded_is_closed [complete_space α] {s : set α} (ht : totally_bounded s) (hc : is_closed s) : is_compact s := (@compact_iff_totally_bounded_complete α _ s).2 ⟨ht, hc.is_complete⟩ /-! ### Sequentially complete space In this section we prove that a uniform space is complete provided that it is sequentially complete (i.e., any Cauchy sequence converges) and its uniformity filter admits a countable generating set. In particular, this applies to (e)metric spaces, see the files `topology/metric_space/emetric_space` and `topology/metric_space/basic`. More precisely, we assume that there is a sequence of entourages `U_n` such that any other entourage includes one of `U_n`. Then any Cauchy filter `f` generates a decreasing sequence of sets `s_n ∈ f` such that `s_n × s_n ⊆ U_n`. Choose a sequence `x_n∈s_n`. It is easy to show that this is a Cauchy sequence. If this sequence converges to some `a`, then `f ≤ 𝓝 a`. -/ namespace sequentially_complete variables {f : filter α} (hf : cauchy f) {U : ℕ → set (α × α)} (U_mem : ∀ n, U n ∈ 𝓤 α) (U_le : ∀ s ∈ 𝓤 α, ∃ n, U n ⊆ s) open set finset noncomputable theory /-- An auxiliary sequence of sets approximating a Cauchy filter. -/ def set_seq_aux (n : ℕ) : {s : set α // ∃ (_ : s ∈ f), s.prod s ⊆ U n } := indefinite_description _ $ (cauchy_iff.1 hf).2 (U n) (U_mem n) /-- Given a Cauchy filter `f` and a sequence `U` of entourages, `set_seq` provides a sequence of monotonically decreasing sets `s n ∈ f` such that `(s n).prod (s n) ⊆ U`. -/ def set_seq (n : ℕ) : set α := ⋂ m ∈ Iic n, (set_seq_aux hf U_mem m).val lemma set_seq_mem (n : ℕ) : set_seq hf U_mem n ∈ f := (bInter_mem (finite_le_nat n)).2 (λ m _, (set_seq_aux hf U_mem m).2.fst) lemma set_seq_mono ⦃m n : ℕ⦄ (h : m ≤ n) : set_seq hf U_mem n ⊆ set_seq hf U_mem m := bInter_subset_bInter_left (λ k hk, le_trans hk h) lemma set_seq_sub_aux (n : ℕ) : set_seq hf U_mem n ⊆ set_seq_aux hf U_mem n := bInter_subset_of_mem right_mem_Iic lemma set_seq_prod_subset {N m n} (hm : N ≤ m) (hn : N ≤ n) : (set_seq hf U_mem m).prod (set_seq hf U_mem n) ⊆ U N := begin assume p hp, refine (set_seq_aux hf U_mem N).2.snd ⟨_, _⟩; apply set_seq_sub_aux, exact set_seq_mono hf U_mem hm hp.1, exact set_seq_mono hf U_mem hn hp.2 end /-- A sequence of points such that `seq n ∈ set_seq n`. Here `set_seq` is a monotonically decreasing sequence of sets `set_seq n ∈ f` with diameters controlled by a given sequence of entourages. -/ def seq (n : ℕ) : α := some $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n) lemma seq_mem (n : ℕ) : seq hf U_mem n ∈ set_seq hf U_mem n := some_spec $ hf.1.nonempty_of_mem (set_seq_mem hf U_mem n) lemma seq_pair_mem ⦃N m n : ℕ⦄ (hm : N ≤ m) (hn : N ≤ n) : (seq hf U_mem m, seq hf U_mem n) ∈ U N := set_seq_prod_subset hf U_mem hm hn ⟨seq_mem hf U_mem m, seq_mem hf U_mem n⟩ include U_le theorem seq_is_cauchy_seq : cauchy_seq $ seq hf U_mem := cauchy_seq_of_controlled U U_le $ seq_pair_mem hf U_mem /-- If the sequence `sequentially_complete.seq` converges to `a`, then `f ≤ 𝓝 a`. -/ theorem le_nhds_of_seq_tendsto_nhds ⦃a : α⦄ (ha : tendsto (seq hf U_mem) at_top (𝓝 a)) : f ≤ 𝓝 a := le_nhds_of_cauchy_adhp_aux begin assume s hs, rcases U_le s hs with ⟨m, hm⟩, rcases tendsto_at_top'.1 ha _ (mem_nhds_left a (U_mem m)) with ⟨n, hn⟩, refine ⟨set_seq hf U_mem (max m n), set_seq_mem hf U_mem _, _, seq hf U_mem (max m n), _, seq_mem hf U_mem _⟩, { have := le_max_left m n, exact set.subset.trans (set_seq_prod_subset hf U_mem this this) hm }, { exact hm (hn _ $ le_max_right m n) } end end sequentially_complete namespace uniform_space open sequentially_complete variables (H : is_countably_generated (𝓤 α)) include H /-- A uniform space is complete provided that (a) its uniformity filter has a countable basis; (b) any sequence satisfying a "controlled" version of the Cauchy condition converges. -/ theorem complete_of_convergent_controlled_sequences (U : ℕ → set (α × α)) (U_mem : ∀ n, U n ∈ 𝓤 α) (HU : ∀ u : ℕ → α, (∀ N m n, N ≤ m → N ≤ n → (u m, u n) ∈ U N) → ∃ a, tendsto u at_top (𝓝 a)) : complete_space α := begin rcases H.exists_antimono_seq' with ⟨U', U'_mono, hU'⟩, have Hmem : ∀ n, U n ∩ U' n ∈ 𝓤 α, from λ n, inter_mem (U_mem n) (hU'.2 ⟨n, subset.refl _⟩), refine ⟨λ f hf, (HU (seq hf Hmem) (λ N m n hm hn, _)).imp $ le_nhds_of_seq_tendsto_nhds _ _ (λ s hs, _)⟩, { rcases (hU'.1 hs) with ⟨N, hN⟩, exact ⟨N, subset.trans (inter_subset_right _ _) hN⟩ }, { exact inter_subset_left _ _ (seq_pair_mem hf Hmem hm hn) } end /-- A sequentially complete uniform space with a countable basis of the uniformity filter is complete. -/ theorem complete_of_cauchy_seq_tendsto (H' : ∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) : complete_space α := let ⟨U', U'_mono, hU'⟩ := H.exists_antimono_seq' in complete_of_convergent_controlled_sequences H U' (λ n, hU'.2 ⟨n, subset.refl _⟩) (λ u hu, H' u $ cauchy_seq_of_controlled U' (λ s hs, hU'.1 hs) hu) protected lemma first_countable_topology : first_countable_topology α := ⟨λ a, by { rw nhds_eq_comap_uniformity, exact H.comap (prod.mk a) }⟩ /-- A separable uniform space with countably generated uniformity filter is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational "radii" from a countable open symmetric antimono basis of `𝓤 α`. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable [separable_space α] : second_countable_topology α := begin rcases exists_countable_dense α with ⟨s, hsc, hsd⟩, obtain ⟨t : ℕ → set (α × α), hto : ∀ (i : ℕ), t i ∈ (𝓤 α).sets ∧ is_open (t i) ∧ symmetric_rel (t i), h_basis : (𝓤 α).has_antimono_basis (λ _, true) t⟩ := H.exists_antimono_subbasis uniformity_has_basis_open_symmetric, refine ⟨⟨⋃ (x ∈ s), range (λ k, ball x (t k)), hsc.bUnion (λ x hx, countable_range _), _⟩⟩, refine (is_topological_basis_of_open_of_nhds _ _).eq_generate_from, { simp only [mem_bUnion_iff, mem_range], rintros _ ⟨x, hxs, k, rfl⟩, exact is_open_ball x (hto k).2.1 }, { intros x V hxV hVo, simp only [mem_bUnion_iff, mem_range, exists_prop], rcases uniform_space.mem_nhds_iff.1 (is_open.mem_nhds hVo hxV) with ⟨U, hU, hUV⟩, rcases comp_symm_of_uniformity hU with ⟨U', hU', hsymm, hUU'⟩, rcases h_basis.to_has_basis.mem_iff.1 hU' with ⟨k, -, hk⟩, rcases hsd.inter_open_nonempty (ball x $ t k) (uniform_space.is_open_ball x (hto k).2.1) ⟨x, uniform_space.mem_ball_self _ (hto k).1⟩ with ⟨y, hxy, hys⟩, refine ⟨_, ⟨y, hys, k, rfl⟩, (hto k).2.2.subset hxy, λ z hz, _⟩, exact hUV (ball_subset_of_comp_subset (hk hxy) hUU' (hk hz)) } end end uniform_space
caf3f07a2f2353e2a865ba1dff458f51eab9b707
d9d511f37a523cd7659d6f573f990e2a0af93c6f
/src/data/polynomial/degree/definitions.lean
986ed56a25991aead2dff4849ddf56f526db4b31
[ "Apache-2.0" ]
permissive
hikari0108/mathlib
b7ea2b7350497ab1a0b87a09d093ecc025a50dfa
a9e7d333b0cfd45f13a20f7b96b7d52e19fa2901
refs/heads/master
1,690,483,608,260
1,631,541,580,000
1,631,541,580,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
40,267
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker -/ import data.nat.with_bot import data.polynomial.induction import data.polynomial.monomial /-! # Theory of univariate polynomials The definitions include `degree`, `monic`, `leading_coeff` Results include - `degree_mul` : The degree of the product is the sum of degrees - `leading_coeff_add_of_degree_eq` and `leading_coeff_add_of_degree_lt` : The leading_coefficient of a sum is determined by the leading coefficients and degrees -/ noncomputable theory open finsupp finset open_locale big_operators classical namespace polynomial universes u v variables {R : Type u} {S : Type v} {a b : R} {n m : ℕ} section semiring variables [semiring R] {p q r : polynomial R} /-- `degree p` is the degree of the polynomial `p`, i.e. the largest `X`-exponent in `p`. `degree p = some n` when `p ≠ 0` and `n` is the highest power of `X` that appears in `p`, otherwise `degree 0 = ⊥`. -/ def degree (p : polynomial R) : with_bot ℕ := p.support.sup some lemma degree_lt_wf : well_founded (λp q : polynomial R, degree p < degree q) := inv_image.wf degree (with_bot.well_founded_lt nat.lt_wf) instance : has_well_founded (polynomial R) := ⟨_, degree_lt_wf⟩ /-- `nat_degree p` forces `degree p` to ℕ, by defining nat_degree 0 = 0. -/ def nat_degree (p : polynomial R) : ℕ := (degree p).get_or_else 0 /-- `leading_coeff p` gives the coefficient of the highest power of `X` in `p`-/ def leading_coeff (p : polynomial R) : R := coeff p (nat_degree p) /-- a polynomial is `monic` if its leading coefficient is 1 -/ def monic (p : polynomial R) := leading_coeff p = (1 : R) @[nontriviality] lemma monic_of_subsingleton [subsingleton R] (p : polynomial R) : monic p := subsingleton.elim _ _ lemma monic.def : monic p ↔ leading_coeff p = 1 := iff.rfl instance monic.decidable [decidable_eq R] : decidable (monic p) := by unfold monic; apply_instance @[simp] lemma monic.leading_coeff {p : polynomial R} (hp : p.monic) : leading_coeff p = 1 := hp lemma monic.coeff_nat_degree {p : polynomial R} (hp : p.monic) : p.coeff p.nat_degree = 1 := hp @[simp] lemma degree_zero : degree (0 : polynomial R) = ⊥ := rfl @[simp] lemma nat_degree_zero : nat_degree (0 : polynomial R) = 0 := rfl @[simp] lemma coeff_nat_degree : coeff p (nat_degree p) = leading_coeff p := rfl lemma degree_eq_bot : degree p = ⊥ ↔ p = 0 := ⟨λ h, by rw [degree, ← max_eq_sup_with_bot] at h; exact support_eq_empty.1 (max_eq_none.1 h), λ h, h.symm ▸ rfl⟩ @[nontriviality] lemma degree_of_subsingleton [subsingleton R] : degree p = ⊥ := by rw [subsingleton.elim p 0, degree_zero] @[nontriviality] lemma nat_degree_of_subsingleton [subsingleton R] : nat_degree p = 0 := by rw [subsingleton.elim p 0, nat_degree_zero] lemma degree_eq_nat_degree (hp : p ≠ 0) : degree p = (nat_degree p : with_bot ℕ) := let ⟨n, hn⟩ := not_forall.1 (mt option.eq_none_iff_forall_not_mem.2 (mt degree_eq_bot.1 hp)) in have hn : degree p = some n := not_not.1 hn, by rw [nat_degree, hn]; refl lemma degree_eq_iff_nat_degree_eq {p : polynomial R} {n : ℕ} (hp : p ≠ 0) : p.degree = n ↔ p.nat_degree = n := by rw [degree_eq_nat_degree hp, with_bot.coe_eq_coe] lemma degree_eq_iff_nat_degree_eq_of_pos {p : polynomial R} {n : ℕ} (hn : 0 < n) : p.degree = n ↔ p.nat_degree = n := begin split, { intro H, rwa ← degree_eq_iff_nat_degree_eq, rintro rfl, rw degree_zero at H, exact option.no_confusion H }, { intro H, rwa degree_eq_iff_nat_degree_eq, rintro rfl, rw nat_degree_zero at H, rw H at hn, exact lt_irrefl _ hn } end lemma nat_degree_eq_of_degree_eq_some {p : polynomial R} {n : ℕ} (h : degree p = n) : nat_degree p = n := have hp0 : p ≠ 0, from λ hp0, by rw hp0 at h; exact option.no_confusion h, option.some_inj.1 $ show (nat_degree p : with_bot ℕ) = n, by rwa [← degree_eq_nat_degree hp0] @[simp] lemma degree_le_nat_degree : degree p ≤ nat_degree p := with_bot.gi_get_or_else_bot.gc.le_u_l _ lemma nat_degree_eq_of_degree_eq [semiring S] {q : polynomial S} (h : degree p = degree q) : nat_degree p = nat_degree q := by unfold nat_degree; rw h lemma le_degree_of_ne_zero (h : coeff p n ≠ 0) : (n : with_bot ℕ) ≤ degree p := show @has_le.le (with_bot ℕ) _ (some n : with_bot ℕ) (p.support.sup some : with_bot ℕ), from finset.le_sup (mem_support_iff.2 h) lemma le_nat_degree_of_ne_zero (h : coeff p n ≠ 0) : n ≤ nat_degree p := begin rw [← with_bot.coe_le_coe, ← degree_eq_nat_degree], exact le_degree_of_ne_zero h, { assume h, subst h, exact h rfl } end lemma le_nat_degree_of_mem_supp (a : ℕ) : a ∈ p.support → a ≤ nat_degree p:= le_nat_degree_of_ne_zero ∘ mem_support_iff.mp lemma supp_subset_range (h : nat_degree p < m) : p.support ⊆ finset.range m := λ n hn, mem_range.2 $ (le_nat_degree_of_mem_supp _ hn).trans_lt h lemma supp_subset_range_nat_degree_succ : p.support ⊆ finset.range (nat_degree p + 1) := supp_subset_range (nat.lt_succ_self _) lemma degree_le_degree (h : coeff q (nat_degree p) ≠ 0) : degree p ≤ degree q := begin by_cases hp : p = 0, { rw hp, exact bot_le }, { rw degree_eq_nat_degree hp, exact le_degree_of_ne_zero h } end lemma degree_ne_of_nat_degree_ne {n : ℕ} : p.nat_degree ≠ n → degree p ≠ n := mt $ λ h, by rw [nat_degree, h, option.get_or_else_coe] theorem nat_degree_le_iff_degree_le {n : ℕ} : nat_degree p ≤ n ↔ degree p ≤ n := with_bot.get_or_else_bot_le_iff alias polynomial.nat_degree_le_iff_degree_le ↔ . . lemma nat_degree_le_nat_degree [semiring S] {q : polynomial S} (hpq : p.degree ≤ q.degree) : p.nat_degree ≤ q.nat_degree := with_bot.gi_get_or_else_bot.gc.monotone_l hpq @[simp] lemma degree_C (ha : a ≠ 0) : degree (C a) = (0 : with_bot ℕ) := by { rw [degree, ← monomial_zero_left, support_monomial 0 _ ha, sup_singleton], refl } lemma degree_C_le : degree (C a) ≤ 0 := by by_cases h : a = 0; [rw [h, C_0], rw [degree_C h]]; [exact bot_le, exact le_refl _] lemma degree_one_le : degree (1 : polynomial R) ≤ (0 : with_bot ℕ) := by rw [← C_1]; exact degree_C_le @[simp] lemma nat_degree_C (a : R) : nat_degree (C a) = 0 := begin by_cases ha : a = 0, { have : C a = 0, { rw [ha, C_0] }, rw [nat_degree, degree_eq_bot.2 this], refl }, { rw [nat_degree, degree_C ha], refl } end @[simp] lemma nat_degree_one : nat_degree (1 : polynomial R) = 0 := nat_degree_C 1 @[simp] lemma nat_degree_nat_cast (n : ℕ) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_nat_cast, nat_degree_C] @[simp] lemma degree_monomial (n : ℕ) (ha : a ≠ 0) : degree (monomial n a) = n := by rw [degree, support_monomial _ _ ha]; refl @[simp] lemma degree_C_mul_X_pow (n : ℕ) (ha : a ≠ 0) : degree (C a * X ^ n) = n := by rw [← monomial_eq_C_mul_X, degree_monomial n ha] lemma degree_monomial_le (n : ℕ) (a : R) : degree (monomial n a) ≤ n := if h : a = 0 then by rw [h, (monomial n).map_zero]; exact bot_le else le_of_eq (degree_monomial n h) lemma degree_C_mul_X_pow_le (n : ℕ) (a : R) : degree (C a * X ^ n) ≤ n := by { rw C_mul_X_pow_eq_monomial, apply degree_monomial_le } lemma degree_C_mul_X_le (a : R) : degree (C a * X) ≤ 1 := by simpa only [pow_one] using degree_C_mul_X_pow_le 1 a @[simp] lemma nat_degree_C_mul_X_pow (n : ℕ) (a : R) (ha : a ≠ 0) : nat_degree (C a * X ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_C_mul_X_pow n ha) @[simp] lemma nat_degree_C_mul_X (a : R) (ha : a ≠ 0) : nat_degree (C a * X) = 1 := by simpa only [pow_one] using nat_degree_C_mul_X_pow 1 a ha @[simp] lemma nat_degree_monomial (i : ℕ) (r : R) (hr : r ≠ 0) : nat_degree (monomial i r) = i := by rw [← C_mul_X_pow_eq_monomial, nat_degree_C_mul_X_pow i r hr] lemma coeff_eq_zero_of_degree_lt (h : degree p < n) : coeff p n = 0 := not_not.1 (mt le_degree_of_ne_zero (not_le_of_gt h)) lemma coeff_eq_zero_of_nat_degree_lt {p : polynomial R} {n : ℕ} (h : p.nat_degree < n) : p.coeff n = 0 := begin apply coeff_eq_zero_of_degree_lt, by_cases hp : p = 0, { subst hp, exact with_bot.bot_lt_coe n }, { rwa [degree_eq_nat_degree hp, with_bot.coe_lt_coe] } end @[simp] lemma coeff_nat_degree_succ_eq_zero {p : polynomial R} : p.coeff (p.nat_degree + 1) = 0 := coeff_eq_zero_of_nat_degree_lt (lt_add_one _) -- We need the explicit `decidable` argument here because an exotic one shows up in a moment! lemma ite_le_nat_degree_coeff (p : polynomial R) (n : ℕ) (I : decidable (n < 1 + nat_degree p)) : @ite _ (n < 1 + nat_degree p) I (coeff p n) 0 = coeff p n := begin split_ifs, { refl }, { exact (coeff_eq_zero_of_nat_degree_lt (not_le.1 (λ w, h (nat.lt_one_add_iff.2 w)))).symm, } end lemma as_sum_support (p : polynomial R) : p = ∑ i in p.support, monomial i (p.coeff i) := (sum_monomial_eq p).symm lemma as_sum_support_C_mul_X_pow (p : polynomial R) : p = ∑ i in p.support, C (p.coeff i) * X^i := trans p.as_sum_support $ by simp only [C_mul_X_pow_eq_monomial] /-- We can reexpress a sum over `p.support` as a sum over `range n`, for any `n` satisfying `p.nat_degree < n`. -/ lemma sum_over_range' [add_comm_monoid S] (p : polynomial R) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) (n : ℕ) (w : p.nat_degree < n) : p.sum f = ∑ (a : ℕ) in range n, f a (coeff p a) := begin rcases p, have := supp_subset_range w, simp only [polynomial.sum, support, coeff, nat_degree, degree] at ⊢ this, exact finsupp.sum_of_support_subset _ this _ (λ n hn, h n) end /-- We can reexpress a sum over `p.support` as a sum over `range (p.nat_degree + 1)`. -/ lemma sum_over_range [add_comm_monoid S] (p : polynomial R) {f : ℕ → R → S} (h : ∀ n, f n 0 = 0) : p.sum f = ∑ (a : ℕ) in range (p.nat_degree + 1), f a (coeff p a) := sum_over_range' p h (p.nat_degree + 1) (lt_add_one _) lemma as_sum_range' (p : polynomial R) (n : ℕ) (w : p.nat_degree < n) : p = ∑ i in range n, monomial i (coeff p i) := p.sum_monomial_eq.symm.trans $ p.sum_over_range' monomial_zero_right _ w lemma as_sum_range (p : polynomial R) : p = ∑ i in range (p.nat_degree + 1), monomial i (coeff p i) := p.sum_monomial_eq.symm.trans $ p.sum_over_range $ monomial_zero_right lemma as_sum_range_C_mul_X_pow (p : polynomial R) : p = ∑ i in range (p.nat_degree + 1), C (coeff p i) * X ^ i := p.as_sum_range.trans $ by simp only [C_mul_X_pow_eq_monomial] lemma coeff_ne_zero_of_eq_degree (hn : degree p = n) : coeff p n ≠ 0 := λ h, mem_support_iff.mp (mem_of_max hn) h lemma eq_X_add_C_of_degree_le_one (h : degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := ext (λ n, nat.cases_on n (by simp) (λ n, nat.cases_on n (by simp [coeff_C]) (λ m, have degree p < m.succ.succ, from lt_of_le_of_lt h dec_trivial, by simp [coeff_eq_zero_of_degree_lt this, coeff_C, nat.succ_ne_zero, coeff_X, nat.succ_inj', @eq_comm ℕ 0]))) lemma eq_X_add_C_of_degree_eq_one (h : degree p = 1) : p = C (p.leading_coeff) * X + C (p.coeff 0) := (eq_X_add_C_of_degree_le_one (show degree p ≤ 1, from h ▸ le_refl _)).trans (by simp [leading_coeff, nat_degree_eq_of_degree_eq_some h]) lemma eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≤ 1) : p = C (p.coeff 1) * X + C (p.coeff 0) := eq_X_add_C_of_degree_le_one $ degree_le_of_nat_degree_le h lemma exists_eq_X_add_C_of_nat_degree_le_one (h : nat_degree p ≤ 1) : ∃ a b, p = C a * X + C b := ⟨p.coeff 1, p.coeff 0, eq_X_add_C_of_nat_degree_le_one h⟩ theorem degree_X_pow_le (n : ℕ) : degree (X^n : polynomial R) ≤ n := by simpa only [C_1, one_mul] using degree_C_mul_X_pow_le n (1:R) theorem degree_X_le : degree (X : polynomial R) ≤ 1 := degree_monomial_le _ _ lemma nat_degree_X_le : (X : polynomial R).nat_degree ≤ 1 := nat_degree_le_of_degree_le degree_X_le lemma support_C_mul_X_pow (c : R) (n : ℕ) : (C c * X ^ n).support ⊆ singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_monomial' _ _ end lemma mem_support_C_mul_X_pow {n a : ℕ} {c : R} (h : a ∈ (C c * X ^ n).support) : a = n := mem_singleton.1 $ support_C_mul_X_pow _ _ h lemma card_support_C_mul_X_pow_le_one {c : R} {n : ℕ} : (C c * X ^ n).support.card ≤ 1 := begin rw ← card_singleton n, apply card_le_of_subset (support_C_mul_X_pow c n), end lemma card_supp_le_succ_nat_degree (p : polynomial R) : p.support.card ≤ p.nat_degree + 1 := begin rw ← finset.card_range (p.nat_degree + 1), exact finset.card_le_of_subset supp_subset_range_nat_degree_succ, end lemma le_degree_of_mem_supp (a : ℕ) : a ∈ p.support → ↑a ≤ degree p := le_degree_of_ne_zero ∘ mem_support_iff.mp lemma nonempty_support_iff : p.support.nonempty ↔ p ≠ 0 := by rw [ne.def, nonempty_iff_ne_empty, ne.def, ← support_eq_empty] lemma support_C_mul_X_pow_nonzero {c : R} {n : ℕ} (h : c ≠ 0) : (C c * X ^ n).support = singleton n := begin rw [C_mul_X_pow_eq_monomial], exact support_monomial _ _ h end end semiring section nonzero_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_one : degree (1 : polynomial R) = (0 : with_bot ℕ) := degree_C (show (1 : R) ≠ 0, from zero_ne_one.symm) @[simp] lemma degree_X : degree (X : polynomial R) = 1 := degree_monomial _ one_ne_zero @[simp] lemma nat_degree_X : (X : polynomial R).nat_degree = 1 := nat_degree_eq_of_degree_eq_some degree_X end nonzero_semiring section ring variables [ring R] lemma coeff_mul_X_sub_C {p : polynomial R} {r : R} {a : ℕ} : coeff (p * (X - C r)) (a + 1) = coeff p a - coeff p (a + 1) * r := by simp [mul_sub] lemma C_eq_int_cast (n : ℤ) : C (n : R) = n := (C : R →+* _).map_int_cast n @[simp] lemma degree_neg (p : polynomial R) : degree (-p) = degree p := by unfold degree; rw support_neg @[simp] lemma nat_degree_neg (p : polynomial R) : nat_degree (-p) = nat_degree p := by simp [nat_degree] @[simp] lemma nat_degree_int_cast (n : ℤ) : nat_degree (n : polynomial R) = 0 := by simp only [←C_eq_int_cast, nat_degree_C] end ring section semiring variables [semiring R] /-- The second-highest coefficient, or 0 for constants -/ def next_coeff (p : polynomial R) : R := if p.nat_degree = 0 then 0 else p.coeff (p.nat_degree - 1) @[simp] lemma next_coeff_C_eq_zero (c : R) : next_coeff (C c) = 0 := by { rw next_coeff, simp } lemma next_coeff_of_pos_nat_degree (p : polynomial R) (hp : 0 < p.nat_degree) : next_coeff p = p.coeff (p.nat_degree - 1) := by { rw [next_coeff, if_neg], contrapose! hp, simpa } end semiring section semiring variables [semiring R] {p q : polynomial R} {ι : Type*} lemma coeff_nat_degree_eq_zero_of_degree_lt (h : degree p < degree q) : coeff p (nat_degree q) = 0 := coeff_eq_zero_of_degree_lt (lt_of_lt_of_le h degree_le_nat_degree) lemma ne_zero_of_degree_gt {n : with_bot ℕ} (h : n < degree p) : p ≠ 0 := mt degree_eq_bot.2 (ne.symm (ne_of_lt (lt_of_le_of_lt bot_le h))) lemma ne_zero_of_degree_ge_degree (hpq : p.degree ≤ q.degree) (hp : p ≠ 0) : q ≠ 0 := polynomial.ne_zero_of_degree_gt (lt_of_lt_of_le (bot_lt_iff_ne_bot.mpr (by rwa [ne.def, polynomial.degree_eq_bot])) hpq : q.degree > ⊥) lemma ne_zero_of_nat_degree_gt {n : ℕ} (h : n < nat_degree p) : p ≠ 0 := λ H, by simpa [H, nat.not_lt_zero] using h lemma degree_lt_degree (h : nat_degree p < nat_degree q) : degree p < degree q := begin by_cases hp : p = 0, { simp [hp], rw bot_lt_iff_ne_bot, intro hq, simpa [hp, degree_eq_bot.mp hq, lt_irrefl] using h }, { rw [degree_eq_nat_degree hp, degree_eq_nat_degree $ ne_zero_of_nat_degree_gt h], exact_mod_cast h } end lemma nat_degree_lt_nat_degree_iff (hp : p ≠ 0) : nat_degree p < nat_degree q ↔ degree p < degree q := ⟨degree_lt_degree, begin intro h, have hq : q ≠ 0 := ne_zero_of_degree_gt h, rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq] at h, exact_mod_cast h end⟩ lemma eq_C_of_degree_le_zero (h : degree p ≤ 0) : p = C (coeff p 0) := begin ext (_|n), { simp }, rw [coeff_C, if_neg (nat.succ_ne_zero _), coeff_eq_zero_of_degree_lt], exact h.trans_lt (with_bot.some_lt_some.2 n.succ_pos), end lemma eq_C_of_degree_eq_zero (h : degree p = 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero (h ▸ le_refl _) lemma degree_le_zero_iff : degree p ≤ 0 ↔ p = C (coeff p 0) := ⟨eq_C_of_degree_le_zero, λ h, h.symm ▸ degree_C_le⟩ lemma degree_add_le (p q : polynomial R) : degree (p + q) ≤ max (degree p) (degree q) := calc degree (p + q) = ((p + q).support).sup some : rfl ... ≤ (p.support ∪ q.support).sup some : by convert sup_mono support_add ... = p.support.sup some ⊔ q.support.sup some : by convert sup_union ... = _ : with_bot.sup_eq_max _ _ lemma nat_degree_add_le (p q : polynomial R) : nat_degree (p + q) ≤ max (nat_degree p) (nat_degree q) := begin cases le_max_iff.1 (degree_add_le p q); simp [nat_degree_le_nat_degree h] end @[simp] lemma leading_coeff_zero : leading_coeff (0 : polynomial R) = 0 := rfl @[simp] lemma leading_coeff_eq_zero : leading_coeff p = 0 ↔ p = 0 := ⟨λ h, by_contradiction $ λ hp, mt mem_support_iff.1 (not_not.2 h) (mem_of_max (degree_eq_nat_degree hp)), λ h, h.symm ▸ leading_coeff_zero⟩ lemma leading_coeff_ne_zero : leading_coeff p ≠ 0 ↔ p ≠ 0 := by rw [ne.def, leading_coeff_eq_zero] lemma leading_coeff_eq_zero_iff_deg_eq_bot : leading_coeff p = 0 ↔ degree p = ⊥ := by rw [leading_coeff_eq_zero, degree_eq_bot] lemma nat_degree_mem_support_of_nonzero (H : p ≠ 0) : p.nat_degree ∈ p.support := by { rw mem_support_iff, exact (not_congr leading_coeff_eq_zero).mpr H } lemma nat_degree_eq_support_max' (h : p ≠ 0) : p.nat_degree = p.support.max' (nonempty_support_iff.mpr h) := (le_max' _ _ $ nat_degree_mem_support_of_nonzero h).antisymm $ max'_le _ _ _ le_nat_degree_of_mem_supp lemma nat_degree_C_mul_X_pow_le (a : R) (n : ℕ) : nat_degree (C a * X ^ n) ≤ n := nat_degree_le_iff_degree_le.2 $ degree_C_mul_X_pow_le _ _ lemma degree_add_eq_left_of_degree_lt (h : degree q < degree p) : degree (p + q) = degree p := le_antisymm (max_eq_left_of_lt h ▸ degree_add_le _ _) $ degree_le_degree $ begin rw [coeff_add, coeff_nat_degree_eq_zero_of_degree_lt h, add_zero], exact mt leading_coeff_eq_zero.1 (ne_zero_of_degree_gt h) end lemma degree_add_eq_right_of_degree_lt (h : degree p < degree q) : degree (p + q) = degree q := by rw [add_comm, degree_add_eq_left_of_degree_lt h] lemma degree_add_C (hp : 0 < degree p) : degree (p + C a) = degree p := add_comm (C a) p ▸ degree_add_eq_right_of_degree_lt $ lt_of_le_of_lt degree_C_le hp lemma degree_add_eq_of_leading_coeff_add_ne_zero (h : leading_coeff p + leading_coeff q ≠ 0) : degree (p + q) = max p.degree q.degree := le_antisymm (degree_add_le _ _) $ match lt_trichotomy (degree p) (degree q) with | or.inl hlt := by rw [degree_add_eq_right_of_degree_lt hlt, max_eq_right_of_lt hlt]; exact le_refl _ | or.inr (or.inl heq) := le_of_not_gt $ assume hlt : max (degree p) (degree q) > degree (p + q), h $ show leading_coeff p + leading_coeff q = 0, begin rw [heq, max_self] at hlt, rw [leading_coeff, leading_coeff, nat_degree_eq_of_degree_eq heq, ← coeff_add], exact coeff_nat_degree_eq_zero_of_degree_lt hlt end | or.inr (or.inr hlt) := by rw [degree_add_eq_left_of_degree_lt hlt, max_eq_left_of_lt hlt]; exact le_refl _ end lemma degree_erase_le (p : polynomial R) (n : ℕ) : degree (p.erase n) ≤ degree p := by { rcases p, simp only [erase, degree, coeff, support], convert sup_mono (erase_subset _ _) } lemma degree_erase_lt (hp : p ≠ 0) : degree (p.erase (nat_degree p)) < degree p := begin apply lt_of_le_of_ne (degree_erase_le _ _), rw [degree_eq_nat_degree hp, degree, support_erase], exact λ h, not_mem_erase _ _ (mem_of_max h), end lemma degree_sum_le (s : finset ι) (f : ι → polynomial R) : degree (∑ i in s, f i) ≤ s.sup (λ b, degree (f b)) := finset.induction_on s (by simp only [sum_empty, sup_empty, degree_zero, le_refl]) $ assume a s has ih, calc degree (∑ i in insert a s, f i) ≤ max (degree (f a)) (degree (∑ i in s, f i)) : by rw sum_insert has; exact degree_add_le _ _ ... ≤ _ : by rw [sup_insert, with_bot.sup_eq_max]; exact max_le_max (le_refl _) ih lemma degree_mul_le (p q : polynomial R) : degree (p * q) ≤ degree p + degree q := calc degree (p * q) ≤ (p.support).sup (λi, degree (sum q (λj a, C (coeff p i * a) * X ^ (i + j)))) : begin simp only [monomial_eq_C_mul_X.symm], convert degree_sum_le _ _, exact mul_eq_sum_sum end ... ≤ p.support.sup (λi, q.support.sup (λj, degree (C (coeff p i * coeff q j) * X ^ (i + j)))) : finset.sup_mono_fun (assume i hi, degree_sum_le _ _) ... ≤ degree p + degree q : begin refine finset.sup_le (λ a ha, finset.sup_le (λ b hb, le_trans (degree_C_mul_X_pow_le _ _) _)), rw [with_bot.coe_add], rw mem_support_iff at ha hb, exact add_le_add (le_degree_of_ne_zero ha) (le_degree_of_ne_zero hb) end lemma degree_pow_le (p : polynomial R) : ∀ (n : ℕ), degree (p ^ n) ≤ n • (degree p) | 0 := by rw [pow_zero, zero_nsmul]; exact degree_one_le | (n+1) := calc degree (p ^ (n + 1)) ≤ degree p + degree (p ^ n) : by rw pow_succ; exact degree_mul_le _ _ ... ≤ _ : by rw succ_nsmul; exact add_le_add (le_refl _) (degree_pow_le _) @[simp] lemma leading_coeff_monomial (a : R) (n : ℕ) : leading_coeff (monomial n a) = a := begin by_cases ha : a = 0, { simp only [ha, (monomial n).map_zero, leading_coeff_zero] }, { rw [leading_coeff, nat_degree_monomial _ _ ha, coeff_monomial], simp } end lemma leading_coeff_C_mul_X_pow (a : R) (n : ℕ) : leading_coeff (C a * X ^ n) = a := by rw [C_mul_X_pow_eq_monomial, leading_coeff_monomial] @[simp] lemma leading_coeff_C (a : R) : leading_coeff (C a) = a := leading_coeff_monomial a 0 @[simp] lemma leading_coeff_X_pow (n : ℕ) : leading_coeff ((X : polynomial R) ^ n) = 1 := by simpa only [C_1, one_mul] using leading_coeff_C_mul_X_pow (1 : R) n @[simp] lemma leading_coeff_X : leading_coeff (X : polynomial R) = 1 := by simpa only [pow_one] using @leading_coeff_X_pow R _ 1 @[simp] lemma monic_X_pow (n : ℕ) : monic (X ^ n : polynomial R) := leading_coeff_X_pow n @[simp] lemma monic_X : monic (X : polynomial R) := leading_coeff_X @[simp] lemma leading_coeff_one : leading_coeff (1 : polynomial R) = 1 := leading_coeff_C 1 @[simp] lemma monic_one : monic (1 : polynomial R) := leading_coeff_C _ lemma monic.ne_zero {R : Type*} [semiring R] [nontrivial R] {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { rintro rfl, simpa [monic] using hp } lemma monic.ne_zero_of_ne (h : (0:R) ≠ 1) {p : polynomial R} (hp : p.monic) : p ≠ 0 := by { nontriviality R, exact hp.ne_zero } lemma monic.ne_zero_of_polynomial_ne {r} (hp : monic p) (hne : q ≠ r) : p ≠ 0 := by { haveI := nontrivial.of_polynomial_ne hne, exact hp.ne_zero } lemma leading_coeff_add_of_degree_lt (h : degree p < degree q) : leading_coeff (p + q) = leading_coeff q := have coeff p (nat_degree q) = 0, from coeff_nat_degree_eq_zero_of_degree_lt h, by simp only [leading_coeff, nat_degree_eq_of_degree_eq (degree_add_eq_right_of_degree_lt h), this, coeff_add, zero_add] lemma leading_coeff_add_of_degree_eq (h : degree p = degree q) (hlc : leading_coeff p + leading_coeff q ≠ 0) : leading_coeff (p + q) = leading_coeff p + leading_coeff q := have nat_degree (p + q) = nat_degree p, by apply nat_degree_eq_of_degree_eq; rw [degree_add_eq_of_leading_coeff_add_ne_zero hlc, h, max_self], by simp only [leading_coeff, this, nat_degree_eq_of_degree_eq h, coeff_add] @[simp] lemma coeff_mul_degree_add_degree (p q : polynomial R) : coeff (p * q) (nat_degree p + nat_degree q) = leading_coeff p * leading_coeff q := calc coeff (p * q) (nat_degree p + nat_degree q) = ∑ x in nat.antidiagonal (nat_degree p + nat_degree q), coeff p x.1 * coeff q x.2 : coeff_mul _ _ _ ... = coeff p (nat_degree p) * coeff q (nat_degree q) : begin refine finset.sum_eq_single (nat_degree p, nat_degree q) _ _, { rintro ⟨i,j⟩ h₁ h₂, rw nat.mem_antidiagonal at h₁, by_cases H : nat_degree p < i, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 H)), zero_mul] }, { rw not_lt_iff_eq_or_lt at H, cases H, { subst H, rw add_left_cancel_iff at h₁, dsimp at h₁, subst h₁, exfalso, exact h₂ rfl }, { suffices : nat_degree q < j, { rw [coeff_eq_zero_of_degree_lt (lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 this)), mul_zero] }, { by_contra H', rw not_lt at H', exact ne_of_lt (nat.lt_of_lt_of_le (nat.add_lt_add_right H j) (nat.add_le_add_left H' _)) h₁ } } } }, { intro H, exfalso, apply H, rw nat.mem_antidiagonal } end lemma degree_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : degree (p * q) = degree p + degree q := have hp : p ≠ 0 := by refine mt _ h; exact λ hp, by rw [hp, leading_coeff_zero, zero_mul], have hq : q ≠ 0 := by refine mt _ h; exact λ hq, by rw [hq, leading_coeff_zero, mul_zero], le_antisymm (degree_mul_le _ _) begin rw [degree_eq_nat_degree hp, degree_eq_nat_degree hq], refine le_degree_of_ne_zero _, rwa coeff_mul_degree_add_degree end lemma monic.degree_mul (hq : monic q) : degree (p * q) = degree p + degree q := if hp : p = 0 then by simp [hp] else degree_mul' $ by rwa [hq.leading_coeff, mul_one, ne.def, leading_coeff_eq_zero] lemma nat_degree_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : nat_degree (p * q) = nat_degree p + nat_degree q := have hp : p ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, zero_mul]), have hq : q ≠ 0 := mt leading_coeff_eq_zero.2 (λ h₁, h $ by rw [h₁, mul_zero]), nat_degree_eq_of_degree_eq_some $ by rw [degree_mul' h, with_bot.coe_add, degree_eq_nat_degree hp, degree_eq_nat_degree hq] lemma leading_coeff_mul' (h : leading_coeff p * leading_coeff q ≠ 0) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin unfold leading_coeff, rw [nat_degree_mul' h, coeff_mul_degree_add_degree], refl end lemma monomial_nat_degree_leading_coeff_eq_self (h : p.support.card ≤ 1) : monomial p.nat_degree p.leading_coeff = p := begin rcases card_support_le_one_iff_monomial.1 h with ⟨n, a, rfl⟩, by_cases ha : a = 0; simp [ha] end lemma C_mul_X_pow_eq_self (h : p.support.card ≤ 1) : C p.leading_coeff * X^p.nat_degree = p := by rw [C_mul_X_pow_eq_monomial, monomial_nat_degree_leading_coeff_eq_self h] lemma leading_coeff_pow' : leading_coeff p ^ n ≠ 0 → leading_coeff (p ^ n) = leading_coeff p ^ n := nat.rec_on n (by simp) $ λ n ih h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← ih h₁] at h, by rw [pow_succ, pow_succ, leading_coeff_mul' h₂, ih h₁] lemma degree_pow' : ∀ {n : ℕ}, leading_coeff p ^ n ≠ 0 → degree (p ^ n) = n • (degree p) | 0 := λ h, by rw [pow_zero, ← C_1] at *; rw [degree_C h, zero_nsmul] | (n+1) := λ h, have h₁ : leading_coeff p ^ n ≠ 0 := λ h₁, h $ by rw [pow_succ, h₁, mul_zero], have h₂ : leading_coeff p * leading_coeff (p ^ n) ≠ 0 := by rwa [pow_succ, ← leading_coeff_pow' h₁] at h, by rw [pow_succ, degree_mul' h₂, succ_nsmul, degree_pow' h₁] lemma nat_degree_pow' {n : ℕ} (h : leading_coeff p ^ n ≠ 0) : nat_degree (p ^ n) = n * nat_degree p := if hp0 : p = 0 then if hn0 : n = 0 then by simp * else by rw [hp0, zero_pow (nat.pos_of_ne_zero hn0)]; simp else have hpn : p ^ n ≠ 0, from λ hpn0, have h1 : _ := h, by rw [← leading_coeff_pow' h1, hpn0, leading_coeff_zero] at h; exact h rfl, option.some_inj.1 $ show (nat_degree (p ^ n) : with_bot ℕ) = (n * nat_degree p : ℕ), by rw [← degree_eq_nat_degree hpn, degree_pow' h, degree_eq_nat_degree hp0, ← with_bot.coe_nsmul]; simp theorem leading_coeff_mul_monic {p q : polynomial R} (hq : monic q) : leading_coeff (p * q) = leading_coeff p := decidable.by_cases (λ H : leading_coeff p = 0, by rw [H, leading_coeff_eq_zero.1 H, zero_mul, leading_coeff_zero]) (λ H : leading_coeff p ≠ 0, by rw [leading_coeff_mul', hq.leading_coeff, mul_one]; rwa [hq.leading_coeff, mul_one]) @[simp] theorem leading_coeff_mul_X_pow {p : polynomial R} {n : ℕ} : leading_coeff (p * X ^ n) = leading_coeff p := leading_coeff_mul_monic (monic_X_pow n) @[simp] theorem leading_coeff_mul_X {p : polynomial R} : leading_coeff (p * X) = leading_coeff p := leading_coeff_mul_monic monic_X lemma nat_degree_mul_le {p q : polynomial R} : nat_degree (p * q) ≤ nat_degree p + nat_degree q := begin apply nat_degree_le_of_degree_le, apply le_trans (degree_mul_le p q), rw with_bot.coe_add, refine add_le_add _ _; apply degree_le_nat_degree, end lemma subsingleton_of_monic_zero (h : monic (0 : polynomial R)) : (∀ p q : polynomial R, p = q) ∧ (∀ a b : R, a = b) := by rw [monic.def, leading_coeff_zero] at h; exact ⟨λ p q, by rw [← mul_one p, ← mul_one q, ← C_1, ← h, C_0, mul_zero, mul_zero], λ a b, by rw [← mul_one a, ← mul_one b, ← h, mul_zero, mul_zero]⟩ lemma zero_le_degree_iff {p : polynomial R} : 0 ≤ degree p ↔ p ≠ 0 := by rw [ne.def, ← degree_eq_bot]; cases degree p; exact dec_trivial lemma degree_nonneg_iff_ne_zero : 0 ≤ degree p ↔ p ≠ 0 := ⟨λ h0p hp0, absurd h0p (by rw [hp0, degree_zero]; exact dec_trivial), λ hp0, le_of_not_gt (λ h, by simp [gt, degree_eq_bot, *] at *)⟩ lemma nat_degree_eq_zero_iff_degree_le_zero : p.nat_degree = 0 ↔ p.degree ≤ 0 := by rw [← nonpos_iff_eq_zero, nat_degree_le_iff_degree_le, with_bot.coe_zero] theorem degree_le_iff_coeff_zero (f : polynomial R) (n : with_bot ℕ) : degree f ≤ n ↔ ∀ m : ℕ, n < m → coeff f m = 0 := ⟨λ (H : finset.sup (f.support) some ≤ n) m (Hm : n < (m : with_bot ℕ)), decidable.of_not_not $ λ H4, have H1 : m ∉ f.support, from λ H2, not_lt_of_ge ((finset.sup_le_iff.1 H) m H2 : ((m : with_bot ℕ) ≤ n)) Hm, H1 $ mem_support_iff.2 H4, λ H, finset.sup_le $ λ b Hb, decidable.of_not_not $ λ Hn, mem_support_iff.1 Hb $ H b $ lt_of_not_ge Hn⟩ theorem degree_lt_iff_coeff_zero (f : polynomial R) (n : ℕ) : degree f < n ↔ ∀ m : ℕ, n ≤ m → coeff f m = 0 := begin refine ⟨λ hf m hm, coeff_eq_zero_of_degree_lt (lt_of_lt_of_le hf (with_bot.coe_le_coe.2 hm)), _⟩, simp only [degree, finset.sup_lt_iff (with_bot.bot_lt_coe n), mem_support_iff, with_bot.some_eq_coe, with_bot.coe_lt_coe, ← @not_le ℕ], exact λ h m, mt (h m), end lemma degree_smul_le (a : R) (p : polynomial R) : degree (a • p) ≤ degree p := begin apply (degree_le_iff_coeff_zero _ _).2 (λ m hm, _), rw degree_lt_iff_coeff_zero at hm, simp [hm m (le_refl _)], end lemma nat_degree_smul_le (a : R) (p : polynomial R) : nat_degree (a • p) ≤ nat_degree p := nat_degree_le_nat_degree (degree_smul_le a p) lemma degree_lt_degree_mul_X (hp : p ≠ 0) : p.degree < (p * X).degree := by haveI := nontrivial.of_polynomial_ne hp; exact have leading_coeff p * leading_coeff X ≠ 0, by simpa, by erw [degree_mul' this, degree_eq_nat_degree hp, degree_X, ← with_bot.coe_one, ← with_bot.coe_add, with_bot.coe_lt_coe]; exact nat.lt_succ_self _ lemma nat_degree_pos_iff_degree_pos : 0 < nat_degree p ↔ 0 < degree p := lt_iff_lt_of_le_iff_le nat_degree_le_iff_degree_le lemma eq_C_of_nat_degree_le_zero (h : nat_degree p ≤ 0) : p = C (coeff p 0) := eq_C_of_degree_le_zero $ degree_le_of_nat_degree_le h lemma eq_C_of_nat_degree_eq_zero (h : nat_degree p = 0) : p = C (coeff p 0) := eq_C_of_nat_degree_le_zero h.le lemma ne_zero_of_coe_le_degree (hdeg : ↑n ≤ p.degree) : p ≠ 0 := by rw ← degree_nonneg_iff_ne_zero; exact trans (by exact_mod_cast n.zero_le) hdeg lemma le_nat_degree_of_coe_le_degree (hdeg : ↑n ≤ p.degree) : n ≤ p.nat_degree := with_bot.coe_le_coe.mp ((degree_eq_nat_degree $ ne_zero_of_coe_le_degree hdeg) ▸ hdeg) end semiring section nontrivial_semiring variables [semiring R] [nontrivial R] {p q : polynomial R} @[simp] lemma degree_X_pow (n : ℕ) : degree ((X : polynomial R) ^ n) = n := by rw [X_pow_eq_monomial, degree_monomial _ (@one_ne_zero R _ _)] @[simp] lemma nat_degree_X_pow (n : ℕ) : nat_degree ((X : polynomial R) ^ n) = n := nat_degree_eq_of_degree_eq_some (degree_X_pow n) theorem not_is_unit_X : ¬ is_unit (X : polynomial R) := λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by { change g * monomial 1 1 = 1 at hgf, rw [← coeff_one_zero, ← hgf], simp } @[simp] lemma degree_mul_X : degree (p * X) = degree p + 1 := by simp [monic_X.degree_mul] @[simp] lemma degree_mul_X_pow : degree (p * X ^ n) = degree p + n := by simp [(monic_X_pow n).degree_mul] end nontrivial_semiring section ring variables [ring R] {p q : polynomial R} lemma degree_sub_le (p q : polynomial R) : degree (p - q) ≤ max (degree p) (degree q) := by simpa only [sub_eq_add_neg, degree_neg q] using degree_add_le p (-q) lemma degree_sub_lt (hd : degree p = degree q) (hp0 : p ≠ 0) (hlc : leading_coeff p = leading_coeff q) : degree (p - q) < degree p := have hp : monomial (nat_degree p) (leading_coeff p) + p.erase (nat_degree p) = p := monomial_add_erase _ _, have hq : monomial (nat_degree q) (leading_coeff q) + q.erase (nat_degree q) = q := monomial_add_erase _ _, have hd' : nat_degree p = nat_degree q := by unfold nat_degree; rw hd, have hq0 : q ≠ 0 := mt degree_eq_bot.2 (hd ▸ mt degree_eq_bot.1 hp0), calc degree (p - q) = degree (erase (nat_degree q) p + -erase (nat_degree q) q) : by conv { to_lhs, rw [← hp, ← hq, hlc, hd', add_sub_add_left_eq_sub, sub_eq_add_neg] } ... ≤ max (degree (erase (nat_degree q) p)) (degree (erase (nat_degree q) q)) : degree_neg (erase (nat_degree q) q) ▸ degree_add_le _ _ ... < degree p : max_lt_iff.2 ⟨hd' ▸ degree_erase_lt hp0, hd.symm ▸ degree_erase_lt hq0⟩ lemma nat_degree_X_sub_C_le {r : R} : (X - C r).nat_degree ≤ 1 := nat_degree_le_iff_degree_le.2 $ le_trans (degree_sub_le _ _) $ max_le degree_X_le $ le_trans degree_C_le $ with_bot.coe_le_coe.2 zero_le_one lemma degree_sum_fin_lt {n : ℕ} (f : fin n → R) : degree (∑ i : fin n, C (f i) * X ^ (i : ℕ)) < n := begin haveI : is_commutative (with_bot ℕ) max := ⟨max_comm⟩, haveI : is_associative (with_bot ℕ) max := ⟨max_assoc⟩, calc (∑ i, C (f i) * X ^ (i : ℕ)).degree ≤ finset.univ.fold (⊔) ⊥ (λ i, (C (f i) * X ^ (i : ℕ)).degree) : degree_sum_le _ _ ... = finset.univ.fold max ⊥ (λ i, (C (f i) * X ^ (i : ℕ)).degree) : (@finset.fold_hom _ _ _ (⊔) _ _ _ ⊥ finset.univ _ _ _ id (with_bot.sup_eq_max)).symm ... < n : (finset.fold_max_lt (n : with_bot ℕ)).mpr ⟨with_bot.bot_lt_some _, _⟩, rintros ⟨i, hi⟩ -, calc (C (f ⟨i, hi⟩) * X ^ i).degree ≤ (C _).degree + (X ^ i).degree : degree_mul_le _ _ ... ≤ 0 + i : add_le_add degree_C_le (degree_X_pow_le i) ... = i : zero_add _ ... < n : with_bot.some_lt_some.mpr hi, end lemma degree_sub_eq_left_of_degree_lt (h : degree q < degree p) : degree (p - q) = degree p := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_left_of_degree_lt h] } lemma degree_sub_eq_right_of_degree_lt (h : degree p < degree q) : degree (p - q) = degree q := by { rw ← degree_neg q at h, rw [sub_eq_add_neg, degree_add_eq_right_of_degree_lt h, degree_neg] } end ring section nonzero_ring variables [nontrivial R] [ring R] @[simp] lemma degree_X_sub_C (a : R) : degree (X - C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≤ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_sub_eq_left_of_degree_lt this, degree_X] @[simp] lemma degree_X_add_C (a : R) : degree (X + C a) = 1 := have degree (C a) < degree (X : polynomial R), from calc degree (C a) ≤ 0 : degree_C_le ... < 1 : with_bot.some_lt_some.mpr zero_lt_one ... = degree X : degree_X.symm, by rw [degree_add_eq_left_of_degree_lt this, degree_X] @[simp] lemma nat_degree_X_sub_C (x : R) : (X - C x).nat_degree = 1 := nat_degree_eq_of_degree_eq_some $ degree_X_sub_C x @[simp] lemma next_coeff_X_sub_C (c : R) : next_coeff (X - C c) = - c := by simp [next_coeff_of_pos_nat_degree] lemma degree_X_pow_sub_C {n : ℕ} (hn : 0 < n) (a : R) : degree ((X : polynomial R) ^ n - C a) = n := have degree (C a) < degree ((X : polynomial R) ^ n), from calc degree (C a) ≤ 0 : degree_C_le ... < degree ((X : polynomial R) ^ n) : by rwa [degree_X_pow]; exact with_bot.coe_lt_coe.2 hn, by rw [degree_sub_eq_left_of_degree_lt this, degree_X_pow] lemma X_pow_sub_C_ne_zero {n : ℕ} (hn : 0 < n) (a : R) : (X : polynomial R) ^ n - C a ≠ 0 := mt degree_eq_bot.2 (show degree ((X : polynomial R) ^ n - C a) ≠ ⊥, by rw degree_X_pow_sub_C hn a; exact dec_trivial) theorem X_sub_C_ne_zero (r : R) : X - C r ≠ 0 := pow_one (X : polynomial R) ▸ X_pow_sub_C_ne_zero zero_lt_one r theorem zero_nmem_multiset_map_X_sub_C {α : Type*} (m : multiset α) (f : α → R) : (0 : polynomial R) ∉ m.map (λ a, X - C (f a)) := λ mem, let ⟨a, _, ha⟩ := multiset.mem_map.mp mem in X_sub_C_ne_zero _ ha lemma nat_degree_X_pow_sub_C {n : ℕ} {r : R} : (X ^ n - C r).nat_degree = n := begin by_cases hn : n = 0, { rw [hn, pow_zero, ←C_1, ←ring_hom.map_sub, nat_degree_C] }, { exact nat_degree_eq_of_degree_eq_some (degree_X_pow_sub_C (pos_iff_ne_zero.mpr hn) r) }, end @[simp] lemma leading_coeff_X_pow_sub_C {n : ℕ} (hn : 0 < n) {r : R} : (X ^ n - C r).leading_coeff = 1 := by rw [leading_coeff, nat_degree_X_pow_sub_C, coeff_sub, coeff_X_pow_self, coeff_C, if_neg (pos_iff_ne_zero.mp hn), sub_zero] @[simp] lemma leading_coeff_X_pow_sub_one {n : ℕ} (hn : 0 < n) : (X ^ n - 1 : polynomial R).leading_coeff = 1 := leading_coeff_X_pow_sub_C hn end nonzero_ring section no_zero_divisors variables [semiring R] [no_zero_divisors R] {p q : polynomial R} @[simp] lemma degree_mul : degree (p * q) = degree p + degree q := if hp0 : p = 0 then by simp only [hp0, degree_zero, zero_mul, with_bot.bot_add] else if hq0 : q = 0 then by simp only [hq0, degree_zero, mul_zero, with_bot.add_bot] else degree_mul' $ mul_ne_zero (mt leading_coeff_eq_zero.1 hp0) (mt leading_coeff_eq_zero.1 hq0) @[simp] lemma degree_pow [nontrivial R] (p : polynomial R) (n : ℕ) : degree (p ^ n) = n • (degree p) := by induction n; [simp only [pow_zero, degree_one, zero_nsmul], simp only [*, pow_succ, succ_nsmul, degree_mul]] @[simp] lemma leading_coeff_mul (p q : polynomial R) : leading_coeff (p * q) = leading_coeff p * leading_coeff q := begin by_cases hp : p = 0, { simp only [hp, zero_mul, leading_coeff_zero] }, { by_cases hq : q = 0, { simp only [hq, mul_zero, leading_coeff_zero] }, { rw [leading_coeff_mul'], exact mul_ne_zero (mt leading_coeff_eq_zero.1 hp) (mt leading_coeff_eq_zero.1 hq) } } end @[simp] lemma leading_coeff_X_add_C [nontrivial R] (a b : R) (ha : a ≠ 0): leading_coeff (C a * X + C b) = a := begin rw [add_comm, leading_coeff_add_of_degree_lt], { simp }, { simpa [degree_C ha] using lt_of_le_of_lt degree_C_le (with_bot.coe_lt_coe.2 zero_lt_one)} end /-- `polynomial.leading_coeff` bundled as a `monoid_hom` when `R` has `no_zero_divisors`, and thus `leading_coeff` is multiplicative -/ def leading_coeff_hom : polynomial R →* R := { to_fun := leading_coeff, map_one' := by simp, map_mul' := leading_coeff_mul } @[simp] lemma leading_coeff_hom_apply (p : polynomial R) : leading_coeff_hom p = leading_coeff p := rfl @[simp] lemma leading_coeff_pow (p : polynomial R) (n : ℕ) : leading_coeff (p ^ n) = leading_coeff p ^ n := (leading_coeff_hom : polynomial R →* R).map_pow p n end no_zero_divisors end polynomial
9d50e173e505ed23a77ffbc64359f28bbb5dd002
7cef822f3b952965621309e88eadf618da0c8ae9
/src/linear_algebra/dimension.lean
0027fb6dcfef76c59f92c9be1cf1f979b60f41f7
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
15,965
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Johannes Hölzl, Sander Dahmen Dimension of modules and vector spaces. -/ import linear_algebra.basis import set_theory.ordinal noncomputable theory universes u u' u'' v v' w w' variables {K : Type u} {V V₂ V₃ V₄ : Type v} variables {ι : Type w} {ι' : Type w'} {η : Type u''} {φ : η → Type u'} -- TODO: relax these universe constraints open_locale classical section vector_space variables [discrete_field K] [add_comm_group V] [vector_space K V] include K open submodule lattice function set variables (K V) def vector_space.dim : cardinal := cardinal.min (nonempty_subtype.2 (@exists_is_basis K V _ _ _)) (λ b, cardinal.mk b.1) variables {K V} open vector_space section set_option class.instance_max_depth 50 theorem is_basis.le_span (zero_ne_one : (0 : K) ≠ 1) {v : ι → V} {J : set V} (hv : is_basis K v) (hJ : span K J = ⊤) : cardinal.mk (range v) ≤ cardinal.mk J := begin cases le_or_lt cardinal.omega (cardinal.mk J) with oJ oJ, { have := cardinal.mk_range_eq_of_inj (linear_independent.injective zero_ne_one hv.1), let S : J → set ι := λ j, (is_basis.repr hv j).support.to_set, let S' : J → set V := λ j, v '' S j, have hs : range v ⊆ ⋃ j, S' j, { intros b hb, rcases mem_range.1 hb with ⟨i, hi⟩, have : span K J ≤ comap hv.repr (finsupp.supported K K (⋃ j, S j)) := span_le.2 (λ j hj x hx, ⟨_, ⟨⟨j, hj⟩, rfl⟩, hx⟩), rw hJ at this, replace : hv.repr (v i) ∈ (finsupp.supported K K (⋃ j, S j)) := this trivial, rw [hv.repr_eq_single, finsupp.mem_supported, finsupp.support_single_ne_zero zero_ne_one.symm] at this, rw ← hi, apply mem_Union.2, rcases mem_Union.1 (this (mem_singleton _)) with ⟨j, hj⟩, use j, rw mem_image, use i, exact ⟨hj, rfl⟩ }, refine le_of_not_lt (λ IJ, _), suffices : cardinal.mk (⋃ j, S' j) < cardinal.mk (range v), { exact not_le_of_lt this ⟨set.embedding_of_subset hs⟩ }, refine lt_of_le_of_lt (le_trans cardinal.mk_Union_le_sum_mk (cardinal.sum_le_sum _ (λ _, cardinal.omega) _)) _, { exact λ j, le_of_lt (cardinal.lt_omega_iff_finite.2 $ finite_image _ (finset.finite_to_set _)) }, { rwa [cardinal.sum_const, cardinal.mul_eq_max oJ (le_refl _), max_eq_left oJ] } }, { rcases exists_finite_card_le_of_finite_of_linear_independent_of_span (cardinal.lt_omega_iff_finite.1 oJ) hv.1.to_subtype_range _ with ⟨fI, hi⟩, { rwa [← cardinal.nat_cast_le, cardinal.finset_card, finset.coe_to_finset, cardinal.finset_card, finset.coe_to_finset] at hi, }, { rw hJ, apply set.subset_univ } }, end end /-- dimension theorem -/ theorem mk_eq_mk_of_basis {v : ι → V} {v' : ι' → V} (hv : is_basis K v) (hv' : is_basis K v') : cardinal.lift.{w w'} (cardinal.mk ι) = cardinal.lift.{w' w} (cardinal.mk ι') := begin rw ←cardinal.lift_inj.{(max w w') v}, rw [cardinal.lift_lift, cardinal.lift_lift], apply le_antisymm, { convert cardinal.lift_le.{v (max w w')}.2 (hv.le_span zero_ne_one hv'.2), { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_inj (hv.injective zero_ne_one)).symm, }, { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_inj (hv'.injective zero_ne_one)).symm, }, }, { convert cardinal.lift_le.{v (max w w')}.2 (hv'.le_span zero_ne_one hv.2), { rw cardinal.lift_max.{w' v w}, apply (cardinal.mk_range_eq_of_inj (hv'.injective zero_ne_one)).symm, }, { rw cardinal.lift_max.{w v w'}, apply (cardinal.mk_range_eq_of_inj (hv.injective zero_ne_one)).symm, }, } end theorem is_basis.mk_range_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.mk (range v) = dim K V := begin have := show ∃ v', dim K V = _, from cardinal.min_eq _ _, rcases this with ⟨v', e⟩, rw e, apply cardinal.lift_inj.1, rw cardinal.mk_range_eq_of_inj (h.injective zero_ne_one), convert @mk_eq_mk_of_basis _ _ _ _ _ _ _ _ _ h v'.property end theorem is_basis.mk_eq_dim {v : ι → V} (h : is_basis K v) : cardinal.lift.{w v} (cardinal.mk ι) = cardinal.lift.{v w} (dim K V) := by rw [←h.mk_range_eq_dim, cardinal.mk_range_eq_of_inj (h.injective zero_ne_one)] variables [add_comm_group V₂] [vector_space K V₂] theorem linear_equiv.dim_eq (f : V ≃ₗ[K] V₂) : dim K V = dim K V₂ := by letI := classical.dec_eq V; letI := classical.dec_eq V₂; exact let ⟨b, hb⟩ := exists_is_basis K V in cardinal.lift_inj.1 $ hb.mk_eq_dim.symm.trans (f.is_basis hb).mk_eq_dim @[simp] lemma dim_bot : dim K (⊥ : submodule K V) = 0 := by letI := classical.dec_eq V; rw [← cardinal.lift_inj, ← (@is_basis_empty_bot pempty K V _ _ _ not_nonempty_pempty).mk_eq_dim, cardinal.mk_pempty] @[simp] lemma dim_top : dim K (⊤ : submodule K V) = dim K V := linear_equiv.dim_eq (linear_equiv.of_top _ rfl) lemma dim_of_field (K : Type*) [discrete_field K] : dim K K = 1 := by rw [←cardinal.lift_inj, ← (@is_basis_singleton_one punit K _ _).mk_eq_dim, cardinal.mk_punit] lemma dim_span {v : ι → V} (hv : linear_independent K v) : dim K ↥(span K (range v)) = cardinal.mk (range v) := by rw [←cardinal.lift_inj, ← (is_basis_span hv).mk_eq_dim, cardinal.mk_range_eq_of_inj (@linear_independent.injective ι K V v _ _ _ zero_ne_one hv)] lemma dim_span_set {s : set V} (hs : linear_independent K (λ x, x : s → V)) : dim K ↥(span K s) = cardinal.mk s := by rw [← @set_of_mem_eq _ s, ← subtype.val_range]; exact dim_span hs lemma dim_span_le (s : set V) : dim K (span K s) ≤ cardinal.mk s := begin classical, rcases exists_linear_independent linear_independent_empty (set.empty_subset s) with ⟨b, hb, _, hsb, hlib⟩, have hsab : span K s = span K b, from span_eq_of_le _ hsb (span_le.2 (λ x hx, subset_span (hb hx))), convert cardinal.mk_le_mk_of_subset hb, rw [hsab, dim_span_set hlib] end lemma dim_span_of_finset (s : finset V) : dim K (span K (↑s : set V)) < cardinal.omega := calc dim K (span K (↑s : set V)) ≤ cardinal.mk (↑s : set V) : dim_span_le ↑s ... = s.card : by rw ←cardinal.finset_card ... < cardinal.omega : cardinal.nat_lt_omega _ theorem dim_prod : dim K (V × V₂) = dim K V + dim K V₂ := begin letI := classical.dec_eq V, letI := classical.dec_eq V₂, rcases exists_is_basis K V with ⟨b, hb⟩, rcases exists_is_basis K V₂ with ⟨c, hc⟩, rw [← cardinal.lift_inj, ← @is_basis.mk_eq_dim K (V × V₂) _ _ _ _ _ (is_basis_inl_union_inr hb hc), cardinal.lift_add, cardinal.lift_mk, ← hb.mk_eq_dim, ← hc.mk_eq_dim, cardinal.lift_mk, cardinal.lift_mk, cardinal.add_def (ulift b) (ulift c)], exact cardinal.lift_inj.1 (cardinal.lift_mk_eq.2 ⟨equiv.ulift.trans (equiv.sum_congr (@equiv.ulift b) (@equiv.ulift c)).symm ⟩), end theorem dim_quotient_add_dim (p : submodule K V) : dim K p.quotient + dim K p = dim K V := by classical; exact let ⟨f⟩ := quotient_prod_linear_equiv p in dim_prod.symm.trans f.dim_eq theorem dim_quotient_le (p : submodule K V) : dim K p.quotient ≤ dim K V := by { rw ← dim_quotient_add_dim p, exact cardinal.le_add_right _ _ } /-- rank-nullity theorem -/ theorem dim_range_add_dim_ker (f : V →ₗ[K] V₂) : dim K f.range + dim K f.ker = dim K V := begin haveI := λ (p : submodule K V), classical.dec_eq p.quotient, rw [← f.quot_ker_equiv_range.dim_eq, dim_quotient_add_dim] end lemma dim_range_le (f : V →ₗ[K] V₂) : dim K f.range ≤ dim K V := by rw ← dim_range_add_dim_ker f; exact le_add_right (le_refl _) lemma dim_map_le (f : V →ₗ V₂) (p : submodule K V) : dim K (p.map f) ≤ dim K p := begin have h := dim_range_le (f.comp (submodule.subtype p)), rwa [linear_map.range_comp, range_subtype] at h, end lemma dim_range_of_surjective (f : V →ₗ[K] V₂) (h : surjective f) : dim K f.range = dim K V₂ := begin refine linear_equiv.dim_eq (linear_equiv.of_bijective (submodule.subtype _) _ _), exact linear_map.ker_eq_bot.2 subtype.val_injective, rwa [range_subtype, linear_map.range_eq_top] end lemma dim_eq_surjective (f : V →ₗ[K] V₂) (h : surjective f) : dim K V = dim K V₂ + dim K f.ker := by rw [← dim_range_add_dim_ker f, ← dim_range_of_surjective f h] lemma dim_le_surjective (f : V →ₗ[K] V₂) (h : surjective f) : dim K V₂ ≤ dim K V := by rw [dim_eq_surjective f h]; refine le_add_right (le_refl _) lemma dim_eq_injective (f : V →ₗ[K] V₂) (h : injective f) : dim K V = dim K f.range := by rw [← dim_range_add_dim_ker f, linear_map.ker_eq_bot.2 h]; simp [dim_bot] lemma dim_submodule_le (s : submodule K V) : dim K s ≤ dim K V := by { rw ← dim_quotient_add_dim s, exact cardinal.le_add_left _ _ } lemma dim_le_injective (f : V →ₗ[K] V₂) (h : injective f) : dim K V ≤ dim K V₂ := by { rw [dim_eq_injective f h], exact dim_submodule_le _ } lemma dim_le_of_submodule (s t : submodule K V) (h : s ≤ t) : dim K s ≤ dim K t := dim_le_injective (of_le h) $ assume ⟨x, hx⟩ ⟨y, hy⟩ eq, subtype.eq $ show x = y, from subtype.ext.1 eq section variables [add_comm_group V₃] [vector_space K V₃] variables [add_comm_group V₄] [vector_space K V₄] set_option class.instance_max_depth 70 open linear_map /-- This is mostly an auxiliary lemma for `dim_sup_add_dim_inf_eq` -/ lemma dim_add_dim_split (db : V₃ →ₗ[K] V) (eb : V₄ →ₗ[K] V) (cd : V₂ →ₗ[K] V₃) (ce : V₂ →ₗ[K] V₄) (hde : ⊤ ≤ db.range ⊔ eb.range) (hgd : ker cd = ⊥) (eq : db.comp cd = eb.comp ce) (eq₂ : ∀d e, db d = eb e → (∃c, cd c = d ∧ ce c = e)) : dim K V + dim K V₂ = dim K V₃ + dim K V₄ := have hf : surjective (copair db eb), begin refine (range_eq_top.1 $ top_unique $ _), rwa [← map_top, ← prod_top, map_copair_prod] end, begin conv {to_rhs, rw [← dim_prod, dim_eq_surjective _ hf] }, congr' 1, apply linear_equiv.dim_eq, fapply linear_equiv.of_bijective, { refine cod_restrict _ (pair cd (- ce)) _, { assume c, simp [add_eq_zero_iff_eq_neg], exact linear_map.ext_iff.1 eq c } }, { rw [ker_cod_restrict, ker_pair, hgd, bot_inf_eq] }, { rw [eq_top_iff, range_cod_restrict, ← map_le_iff_le_comap, map_top, range_subtype], rintros ⟨d, e⟩, have h := eq₂ d (-e), simp [add_eq_zero_iff_eq_neg] at ⊢ h, assume hde, rcases h hde with ⟨c, h₁, h₂⟩, refine ⟨c, h₁, _⟩, rw [h₂, _root_.neg_neg] } end lemma dim_sup_add_dim_inf_eq (s t : submodule K V) : dim K (s ⊔ t : submodule K V) + dim K (s ⊓ t : submodule K V) = dim K s + dim K t := dim_add_dim_split (of_le le_sup_left) (of_le le_sup_right) (of_le inf_le_left) (of_le inf_le_right) begin rw [← map_le_map_iff (ker_subtype $ s ⊔ t), map_sup, map_top, ← linear_map.range_comp, ← linear_map.range_comp, subtype_comp_of_le, subtype_comp_of_le, range_subtype, range_subtype, range_subtype], exact le_refl _ end (ker_of_le _ _ _) begin ext ⟨x, hx⟩, refl end begin rintros ⟨b₁, hb₁⟩ ⟨b₂, hb₂⟩ eq, have : b₁ = b₂ := congr_arg subtype.val eq, subst this, exact ⟨⟨b₁, hb₁, hb₂⟩, rfl, rfl⟩ end lemma dim_add_le_dim_add_dim (s t : submodule K V) : dim K (s ⊔ t : submodule K V) ≤ dim K s + dim K t := by rw [← dim_sup_add_dim_inf_eq]; exact le_add_right (le_refl _) end section fintype variable [fintype η] variables [∀i, add_comm_group (φ i)] [∀i, vector_space K (φ i)] open linear_map lemma dim_pi : vector_space.dim K (Πi, φ i) = cardinal.sum (λi, vector_space.dim K (φ i)) := begin choose b hb using assume i, exists_is_basis K (φ i), have : is_basis K (λ (ji : Σ j, b j), std_basis K (λ j, φ j) ji.fst ji.snd.val), by apply pi.is_basis_std_basis _ hb, rw [←cardinal.lift_inj, ← this.mk_eq_dim], simp [λ i, (hb i).mk_range_eq_dim.symm, cardinal.sum_mk] end lemma dim_fun {V η : Type u} [fintype η] [add_comm_group V] [vector_space K V] : vector_space.dim K (η → V) = fintype.card η * vector_space.dim K V := by rw [dim_pi, cardinal.sum_const, cardinal.fintype_card] lemma dim_fun_eq_lift_mul : vector_space.dim K (η → V) = (fintype.card η : cardinal.{max u'' v}) * cardinal.lift.{v u''} (vector_space.dim K V) := by rw [dim_pi, cardinal.sum_const_eq_lift_mul, cardinal.fintype_card, cardinal.lift_nat_cast] lemma dim_fun' : vector_space.dim K (η → K) = fintype.card η := by rw [dim_fun_eq_lift_mul, dim_of_field K, cardinal.lift_one, mul_one, cardinal.nat_cast_inj] lemma dim_fin_fun (n : ℕ) : dim K (fin n → K) = n := by simp [dim_fun'] end fintype lemma exists_mem_ne_zero_of_ne_bot {s : submodule K V} (h : s ≠ ⊥) : ∃ b : V, b ∈ s ∧ b ≠ 0 := begin classical, by_contradiction hex, have : ∀x∈s, (x:V) = 0, { simpa only [not_exists, not_and, not_not, ne.def] using hex }, exact (h $ bot_unique $ assume s hs, (submodule.mem_bot K).2 $ this s hs) end lemma exists_mem_ne_zero_of_dim_pos {s : submodule K V} (h : vector_space.dim K s > 0) : ∃ b : V, b ∈ s ∧ b ≠ 0 := exists_mem_ne_zero_of_ne_bot $ assume eq, by rw [(>), eq, dim_bot] at h; exact lt_irrefl _ h lemma exists_is_basis_fintype (h : dim K V < cardinal.omega) : ∃ s : (set V), (is_basis K (subtype.val : s → V)) ∧ nonempty (fintype s) := begin cases exists_is_basis K V with s hs, rw [←cardinal.lift_lt, ← is_basis.mk_eq_dim hs, cardinal.lift_lt, cardinal.lt_omega_iff_fintype] at h, exact ⟨s, hs, h⟩ end section rank def rank (f : V →ₗ[K] V₂) : cardinal := dim K f.range lemma rank_le_domain (f : V →ₗ[K] V₂) : rank f ≤ dim K V := by rw [← dim_range_add_dim_ker f]; exact le_add_right (le_refl _) lemma rank_le_range (f : V →ₗ[K] V₂) : rank f ≤ dim K V₂ := dim_submodule_le _ lemma rank_add_le (f g : V →ₗ[K] V₂) : rank (f + g) ≤ rank f + rank g := calc rank (f + g) ≤ dim K (f.range ⊔ g.range : submodule K V₂) : begin refine dim_le_of_submodule _ _ _, exact (linear_map.range_le_iff_comap.2 $ eq_top_iff'.2 $ assume x, show f x + g x ∈ (f.range ⊔ g.range : submodule K V₂), from mem_sup.2 ⟨_, mem_image_of_mem _ (mem_univ _), _, mem_image_of_mem _ (mem_univ _), rfl⟩) end ... ≤ rank f + rank g : dim_add_le_dim_add_dim _ _ @[simp] lemma rank_zero : rank (0 : V →ₗ[K] V₂) = 0 := by rw [rank, linear_map.range_zero, dim_bot] lemma rank_finset_sum_le {η} (s : finset η) (f : η → V →ₗ[K] V₂) : rank (s.sum f) ≤ s.sum (λ d, rank (f d)) := @finset.sum_hom_rel _ _ _ _ _ (λa b, rank a ≤ b) f (λ d, rank (f d)) s (le_of_eq rank_zero) (λ i g c h, le_trans (rank_add_le _ _) (add_le_add_left' h)) variables [add_comm_group V₃] [vector_space K V₃] lemma rank_comp_le1 (g : V →ₗ[K] V₂) (f : V₂ →ₗ[K] V₃) : rank (f.comp g) ≤ rank f := begin refine dim_le_of_submodule _ _ _, rw [linear_map.range_comp], exact image_subset _ (subset_univ _) end lemma rank_comp_le2 (g : V →ₗ[K] V₂) (f : V₂ →ₗ V₃) : rank (f.comp g) ≤ rank g := by rw [rank, rank, linear_map.range_comp]; exact dim_map_le _ _ end rank end vector_space section unconstrained_universes variables {E : Type v'} variables [discrete_field K] [add_comm_group V] [vector_space K V] [add_comm_group E] [vector_space K E] open vector_space /-- Version of linear_equiv.dim_eq without universe constraints. -/ theorem linear_equiv.dim_eq_lift (f : V ≃ₗ[K] E) : cardinal.lift.{v v'} (dim K V) = cardinal.lift.{v' v} (dim K E) := begin cases exists_is_basis K V with b hb, rw [← cardinal.lift_inj.1 hb.mk_eq_dim, ← (f.is_basis hb).mk_eq_dim, cardinal.lift_mk], end end unconstrained_universes
ec0f585cbfc9a713dd3a47274832b26c1e92ea0a
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/topology/metric_space/emetric_space.lean
62f5b5ea734006ed4085fa17cea3c9ff5285821c
[ "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
38,303
lean
/- Copyright (c) 2015, 2017 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis, Johannes Hölzl, Mario Carneiro, Sébastien Gouëzel -/ import data.real.ennreal import data.finset.intervals import topology.uniform_space.uniform_embedding import topology.uniform_space.pi import topology.uniform_space.uniform_convergence /-! # Extended metric spaces This file is devoted to the definition and study of `emetric_spaces`, i.e., metric spaces in which the distance is allowed to take the value ∞. This extended distance is called `edist`, and takes values in `ℝ≥0∞`. Many definitions and theorems expected on emetric spaces are already introduced on uniform spaces and topological spaces. For example: open and closed sets, compactness, completeness, continuity and uniform continuity. The class `emetric_space` therefore extends `uniform_space` (and `topological_space`). -/ open set filter classical noncomputable theory open_locale uniformity topological_space big_operators filter nnreal ennreal universes u v w variables {α : Type u} {β : Type v} {γ : Type w} /-- Characterizing uniformities associated to a (generalized) distance function `D` in terms of the elements of the uniformity. -/ theorem uniformity_dist_of_mem_uniformity [linear_order β] {U : filter (α × α)} (z : β) (D : α → α → β) (H : ∀ s, s ∈ U ↔ ∃ε>z, ∀{a b:α}, D a b < ε → (a, b) ∈ s) : U = ⨅ ε>z, 𝓟 {p:α×α | D p.1 p.2 < ε} := le_antisymm (le_infi $ λ ε, le_infi $ λ ε0, le_principal_iff.2 $ (H _).2 ⟨ε, ε0, λ a b, id⟩) (λ r ur, let ⟨ε, ε0, h⟩ := (H _).1 ur in mem_infi_sets ε $ mem_infi_sets ε0 $ mem_principal_sets.2 $ λ ⟨a, b⟩, h) class has_edist (α : Type*) := (edist : α → α → ℝ≥0∞) export has_edist (edist) /-- Creating a uniform space from an extended distance. -/ def uniform_space_of_edist (edist : α → α → ℝ≥0∞) (edist_self : ∀ x : α, edist x x = 0) (edist_comm : ∀ x y : α, edist x y = edist y x) (edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) : uniform_space α := uniform_space.of_core { uniformity := (⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε}), refl := le_infi $ assume ε, le_infi $ by simp [set.subset_def, id_rel, edist_self, (>)] {contextual := tt}, comp := le_infi $ assume ε, le_infi $ assume h, have (2 : ℝ≥0∞) = (2 : ℕ) := by simp, have A : 0 < ε / 2 := ennreal.div_pos_iff.2 ⟨ne_of_gt h, by { convert ennreal.nat_ne_top 2 }⟩, lift'_le (mem_infi_sets (ε / 2) $ mem_infi_sets A (subset.refl _)) $ have ∀ (a b c : α), edist a c < ε / 2 → edist c b < ε / 2 → edist a b < ε, from assume a b c hac hcb, calc edist a b ≤ edist a c + edist c b : edist_triangle _ _ _ ... < ε / 2 + ε / 2 : ennreal.add_lt_add hac hcb ... = ε : by rw [ennreal.add_halves], by simpa [comp_rel], symm := tendsto_infi.2 $ assume ε, tendsto_infi.2 $ assume h, tendsto_infi' ε $ tendsto_infi' h $ tendsto_principal_principal.2 $ by simp [edist_comm] } -- the uniform structure is embedded in the emetric space structure -- to avoid instance diamond issues. See Note [forgetful inheritance]. /-- Extended metric spaces, with an extended distance `edist` possibly taking the value ∞ Each emetric space induces a canonical `uniform_space` and hence a canonical `topological_space`. This is enforced in the type class definition, by extending the `uniform_space` structure. When instantiating an `emetric_space` structure, the uniformity fields are not necessary, they will be filled in by default. There is a default value for the uniformity, that can be substituted in cases of interest, for instance when instantiating an `emetric_space` structure on a product. Continuity of `edist` is proved in `topology.instances.ennreal` -/ class emetric_space (α : Type u) extends has_edist α : Type u := (edist_self : ∀ x : α, edist x x = 0) (eq_of_edist_eq_zero : ∀ {x y : α}, edist x y = 0 → x = y) (edist_comm : ∀ x y : α, edist x y = edist y x) (edist_triangle : ∀ x y z : α, edist x z ≤ edist x y + edist y z) (to_uniform_space : uniform_space α := uniform_space_of_edist edist edist_self edist_comm edist_triangle) (uniformity_edist : 𝓤 α = ⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε} . control_laws_tac) /- emetric spaces are less common than metric spaces. Therefore, we work in a dedicated namespace, while notions associated to metric spaces are mostly in the root namespace. -/ variables [emetric_space α] @[priority 100] -- see Note [lower instance priority] instance emetric_space.to_uniform_space' : uniform_space α := emetric_space.to_uniform_space export emetric_space (edist_self eq_of_edist_eq_zero edist_comm edist_triangle) attribute [simp] edist_self /-- Characterize the equality of points by the vanishing of their extended distance -/ @[simp] theorem edist_eq_zero {x y : α} : edist x y = 0 ↔ x = y := iff.intro eq_of_edist_eq_zero (assume : x = y, this ▸ edist_self _) @[simp] theorem zero_eq_edist {x y : α} : 0 = edist x y ↔ x = y := iff.intro (assume h, eq_of_edist_eq_zero (h.symm)) (assume : x = y, this ▸ (edist_self _).symm) theorem edist_le_zero {x y : α} : (edist x y ≤ 0) ↔ x = y := nonpos_iff_eq_zero.trans edist_eq_zero /-- Triangle inequality for the extended distance -/ theorem edist_triangle_left (x y z : α) : edist x y ≤ edist z x + edist z y := by rw edist_comm z; apply edist_triangle theorem edist_triangle_right (x y z : α) : edist x y ≤ edist x z + edist y z := by rw edist_comm y; apply edist_triangle lemma edist_triangle4 (x y z t : α) : edist x t ≤ edist x y + edist y z + edist z t := calc edist x t ≤ edist x z + edist z t : edist_triangle x z t ... ≤ (edist x y + edist y z) + edist z t : add_le_add_right (edist_triangle x y z) _ /-- The triangle (polygon) inequality for sequences of points; `finset.Ico` version. -/ lemma edist_le_Ico_sum_edist (f : ℕ → α) {m n} (h : m ≤ n) : edist (f m) (f n) ≤ ∑ i in finset.Ico m n, edist (f i) (f (i + 1)) := begin revert n, refine nat.le_induction _ _, { simp only [finset.sum_empty, finset.Ico.self_eq_empty, edist_self], -- TODO: Why doesn't Lean close this goal automatically? `apply le_refl` fails too. exact le_refl (0:ℝ≥0∞) }, { assume n hn hrec, calc edist (f m) (f (n+1)) ≤ edist (f m) (f n) + edist (f n) (f (n+1)) : edist_triangle _ _ _ ... ≤ ∑ i in finset.Ico m n, _ + _ : add_le_add hrec (le_refl _) ... = ∑ i in finset.Ico m (n+1), _ : by rw [finset.Ico.succ_top hn, finset.sum_insert, add_comm]; simp } end /-- The triangle (polygon) inequality for sequences of points; `finset.range` version. -/ lemma edist_le_range_sum_edist (f : ℕ → α) (n : ℕ) : edist (f 0) (f n) ≤ ∑ i in finset.range n, edist (f i) (f (i + 1)) := finset.Ico.zero_bot n ▸ edist_le_Ico_sum_edist f (nat.zero_le n) /-- A version of `edist_le_Ico_sum_edist` with each intermediate distance replaced with an upper estimate. -/ lemma edist_le_Ico_sum_of_edist_le {f : ℕ → α} {m n} (hmn : m ≤ n) {d : ℕ → ℝ≥0∞} (hd : ∀ {k}, m ≤ k → k < n → edist (f k) (f (k + 1)) ≤ d k) : edist (f m) (f n) ≤ ∑ i in finset.Ico m n, d i := le_trans (edist_le_Ico_sum_edist f hmn) $ finset.sum_le_sum $ λ k hk, hd (finset.Ico.mem.1 hk).1 (finset.Ico.mem.1 hk).2 /-- A version of `edist_le_range_sum_edist` with each intermediate distance replaced with an upper estimate. -/ lemma edist_le_range_sum_of_edist_le {f : ℕ → α} (n : ℕ) {d : ℕ → ℝ≥0∞} (hd : ∀ {k}, k < n → edist (f k) (f (k + 1)) ≤ d k) : edist (f 0) (f n) ≤ ∑ i in finset.range n, d i := finset.Ico.zero_bot n ▸ edist_le_Ico_sum_of_edist_le (zero_le n) (λ _ _, hd) /-- Two points coincide if their distance is `< ε` for all positive ε -/ theorem eq_of_forall_edist_le {x y : α} (h : ∀ε > 0, edist x y ≤ ε) : x = y := eq_of_edist_eq_zero (eq_of_le_of_forall_le_of_dense bot_le h) /-- Reformulation of the uniform structure in terms of the extended distance -/ theorem uniformity_edist : 𝓤 α = ⨅ ε>0, 𝓟 {p:α×α | edist p.1 p.2 < ε} := emetric_space.uniformity_edist theorem uniformity_basis_edist : (𝓤 α).has_basis (λ ε : ℝ≥0∞, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) := (@uniformity_edist α _).symm ▸ has_basis_binfi_principal (λ r hr p hp, ⟨min r p, lt_min hr hp, λ x hx, lt_of_lt_of_le hx (min_le_left _ _), λ x hx, lt_of_lt_of_le hx (min_le_right _ _)⟩) ⟨1, ennreal.zero_lt_one⟩ /-- Characterization of the elements of the uniformity in terms of the extended distance -/ theorem mem_uniformity_edist {s : set (α×α)} : s ∈ 𝓤 α ↔ (∃ε>0, ∀{a b:α}, edist a b < ε → (a, b) ∈ s) := uniformity_basis_edist.mem_uniformity_iff /-- Given `f : β → ℝ≥0∞`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`. For specific bases see `uniformity_basis_edist`, `uniformity_basis_edist'`, `uniformity_basis_edist_nnreal`, and `uniformity_basis_edist_inv_nat`. -/ protected theorem emetric.mk_uniformity_basis {β : Type*} {p : β → Prop} {f : β → ℝ≥0∞} (hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) : (𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 < f x}) := begin refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩, split, { rintros ⟨ε, ε₀, hε⟩, rcases hf ε ε₀ with ⟨i, hi, H⟩, exact ⟨i, hi, λ x hx, hε $ lt_of_lt_of_le hx H⟩ }, { exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, H⟩ } end /-- Given `f : β → ℝ≥0∞`, if `f` sends `{i | p i}` to a set of positive numbers accumulating to zero, then closed `f i`-neighborhoods of the diagonal form a basis of `𝓤 α`. For specific bases see `uniformity_basis_edist_le` and `uniformity_basis_edist_le'`. -/ protected theorem emetric.mk_uniformity_basis_le {β : Type*} {p : β → Prop} {f : β → ℝ≥0∞} (hf₀ : ∀ x, p x → 0 < f x) (hf : ∀ ε, 0 < ε → ∃ x (hx : p x), f x ≤ ε) : (𝓤 α).has_basis p (λ x, {p:α×α | edist p.1 p.2 ≤ f x}) := begin refine ⟨λ s, uniformity_basis_edist.mem_iff.trans _⟩, split, { rintros ⟨ε, ε₀, hε⟩, rcases exists_between ε₀ with ⟨ε', hε'⟩, rcases hf ε' hε'.1 with ⟨i, hi, H⟩, exact ⟨i, hi, λ x hx, hε $ lt_of_le_of_lt (le_trans hx H) hε'.2⟩ }, { exact λ ⟨i, hi, H⟩, ⟨f i, hf₀ i hi, λ x hx, H (le_of_lt hx)⟩ } end theorem uniformity_basis_edist_le : (𝓤 α).has_basis (λ ε : ℝ≥0∞, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) := emetric.mk_uniformity_basis_le (λ _, id) (λ ε ε₀, ⟨ε, ε₀, le_refl ε⟩) theorem uniformity_basis_edist' (ε' : ℝ≥0∞) (hε' : 0 < ε') : (𝓤 α).has_basis (λ ε : ℝ≥0∞, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 < ε}) := emetric.mk_uniformity_basis (λ _, and.left) (λ ε ε₀, let ⟨δ, hδ⟩ := exists_between hε' in ⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩) theorem uniformity_basis_edist_le' (ε' : ℝ≥0∞) (hε' : 0 < ε') : (𝓤 α).has_basis (λ ε : ℝ≥0∞, ε ∈ Ioo 0 ε') (λ ε, {p:α×α | edist p.1 p.2 ≤ ε}) := emetric.mk_uniformity_basis_le (λ _, and.left) (λ ε ε₀, let ⟨δ, hδ⟩ := exists_between hε' in ⟨min ε δ, ⟨lt_min ε₀ hδ.1, lt_of_le_of_lt (min_le_right _ _) hδ.2⟩, min_le_left _ _⟩) theorem uniformity_basis_edist_nnreal : (𝓤 α).has_basis (λ ε : ℝ≥0, 0 < ε) (λ ε, {p:α×α | edist p.1 p.2 < ε}) := emetric.mk_uniformity_basis (λ _, ennreal.coe_pos.2) (λ ε ε₀, let ⟨δ, hδ⟩ := ennreal.lt_iff_exists_nnreal_btwn.1 ε₀ in ⟨δ, ennreal.coe_pos.1 hδ.1, le_of_lt hδ.2⟩) theorem uniformity_basis_edist_inv_nat : (𝓤 α).has_basis (λ _, true) (λ n:ℕ, {p:α×α | edist p.1 p.2 < (↑n)⁻¹}) := emetric.mk_uniformity_basis (λ n _, ennreal.inv_pos.2 $ ennreal.nat_ne_top n) (λ ε ε₀, let ⟨n, hn⟩ := ennreal.exists_inv_nat_lt (ne_of_gt ε₀) in ⟨n, trivial, le_of_lt hn⟩) /-- Fixed size neighborhoods of the diagonal belong to the uniform structure -/ theorem edist_mem_uniformity {ε:ℝ≥0∞} (ε0 : 0 < ε) : {p:α×α | edist p.1 p.2 < ε} ∈ 𝓤 α := mem_uniformity_edist.2 ⟨ε, ε0, λ a b, id⟩ namespace emetric theorem uniformity_has_countable_basis : is_countably_generated (𝓤 α) := is_countably_generated_of_seq ⟨_, uniformity_basis_edist_inv_nat.eq_infi⟩ /-- ε-δ characterization of uniform continuity on a set for emetric spaces -/ theorem uniform_continuous_on_iff [emetric_space β] {f : α → β} {s : set α} : uniform_continuous_on f s ↔ ∀ ε > 0, ∃ δ > 0, ∀{a b}, a ∈ s → b ∈ s → edist a b < δ → edist (f a) (f b) < ε := uniformity_basis_edist.uniform_continuous_on_iff uniformity_basis_edist /-- ε-δ characterization of uniform continuity on emetric spaces -/ theorem uniform_continuous_iff [emetric_space β] {f : α → β} : uniform_continuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀{a b:α}, edist a b < δ → edist (f a) (f b) < ε := uniformity_basis_edist.uniform_continuous_iff uniformity_basis_edist /-- ε-δ characterization of uniform embeddings on emetric spaces -/ theorem uniform_embedding_iff [emetric_space β] {f : α → β} : uniform_embedding f ↔ function.injective f ∧ uniform_continuous f ∧ ∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ := uniform_embedding_def'.trans $ and_congr iff.rfl $ and_congr iff.rfl ⟨λ H δ δ0, let ⟨t, tu, ht⟩ := H _ (edist_mem_uniformity δ0), ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 tu in ⟨ε, ε0, λ a b h, ht _ _ (hε h)⟩, λ H s su, let ⟨δ, δ0, hδ⟩ := mem_uniformity_edist.1 su, ⟨ε, ε0, hε⟩ := H _ δ0 in ⟨_, edist_mem_uniformity ε0, λ a b h, hδ (hε h)⟩⟩ /-- A map between emetric spaces is a uniform embedding if and only if the edistance between `f x` and `f y` is controlled in terms of the distance between `x` and `y` and conversely. -/ theorem uniform_embedding_iff' [emetric_space β] {f : α → β} : uniform_embedding f ↔ (∀ ε > 0, ∃ δ > 0, ∀ {a b : α}, edist a b < δ → edist (f a) (f b) < ε) ∧ (∀ δ > 0, ∃ ε > 0, ∀ {a b : α}, edist (f a) (f b) < ε → edist a b < δ) := begin split, { assume h, exact ⟨uniform_continuous_iff.1 (uniform_embedding_iff.1 h).2.1, (uniform_embedding_iff.1 h).2.2⟩ }, { rintros ⟨h₁, h₂⟩, refine uniform_embedding_iff.2 ⟨_, uniform_continuous_iff.2 h₁, h₂⟩, assume x y hxy, have : edist x y ≤ 0, { refine le_of_forall_lt' (λδ δpos, _), rcases h₂ δ δpos with ⟨ε, εpos, hε⟩, have : edist (f x) (f y) < ε, by simpa [hxy], exact hε this }, simpa using this } end /-- ε-δ characterization of Cauchy sequences on emetric spaces -/ protected lemma cauchy_iff {f : filter α} : cauchy f ↔ f ≠ ⊥ ∧ ∀ ε > 0, ∃ t ∈ f, ∀ x y ∈ t, edist x y < ε := by rw ← ne_bot_iff; exact uniformity_basis_edist.cauchy_iff /-- A very useful criterion to show that a space is complete is to show that all sequences which satisfy a bound of the form `edist (u n) (u m) < B N` for all `n m ≥ N` are converging. This is often applied for `B N = 2^{-N}`, i.e., with a very fast convergence to `0`, which makes it possible to use arguments of converging series, while this is impossible to do in general for arbitrary Cauchy sequences. -/ theorem complete_of_convergent_controlled_sequences (B : ℕ → ℝ≥0∞) (hB : ∀n, 0 < B n) (H : ∀u : ℕ → α, (∀N n m : ℕ, N ≤ n → N ≤ m → edist (u n) (u m) < B N) → ∃x, tendsto u at_top (𝓝 x)) : complete_space α := uniform_space.complete_of_convergent_controlled_sequences uniformity_has_countable_basis (λ n, {p:α×α | edist p.1 p.2 < B n}) (λ n, edist_mem_uniformity $ hB n) H /-- A sequentially complete emetric space is complete. -/ theorem complete_of_cauchy_seq_tendsto : (∀ u : ℕ → α, cauchy_seq u → ∃a, tendsto u at_top (𝓝 a)) → complete_space α := uniform_space.complete_of_cauchy_seq_tendsto uniformity_has_countable_basis /-- Expressing locally uniform convergence on a set using `edist`. -/ lemma tendsto_locally_uniformly_on_iff {ι : Type*} [topological_space β] {F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} : tendsto_locally_uniformly_on F f p s ↔ ∀ ε > 0, ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, ∀ᶠ n in p, ∀ y ∈ t, edist (f y) (F n y) < ε := begin refine ⟨λ H ε hε, H _ (edist_mem_uniformity hε), λ H u hu x hx, _⟩, rcases mem_uniformity_edist.1 hu with ⟨ε, εpos, hε⟩, rcases H ε εpos x hx with ⟨t, ht, Ht⟩, exact ⟨t, ht, Ht.mono (λ n hs x hx, hε (hs x hx))⟩ end /-- Expressing uniform convergence on a set using `edist`. -/ lemma tendsto_uniformly_on_iff {ι : Type*} {F : ι → β → α} {f : β → α} {p : filter ι} {s : set β} : tendsto_uniformly_on F f p s ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x ∈ s, edist (f x) (F n x) < ε := begin refine ⟨λ H ε hε, H _ (edist_mem_uniformity hε), λ H u hu, _⟩, rcases mem_uniformity_edist.1 hu with ⟨ε, εpos, hε⟩, exact (H ε εpos).mono (λ n hs x hx, hε (hs x hx)) end /-- Expressing locally uniform convergence using `edist`. -/ lemma tendsto_locally_uniformly_iff {ι : Type*} [topological_space β] {F : ι → β → α} {f : β → α} {p : filter ι} : tendsto_locally_uniformly F f p ↔ ∀ ε > 0, ∀ (x : β), ∃ t ∈ 𝓝 x, ∀ᶠ n in p, ∀ y ∈ t, edist (f y) (F n y) < ε := by simp only [← tendsto_locally_uniformly_on_univ, tendsto_locally_uniformly_on_iff, mem_univ, forall_const, exists_prop, nhds_within_univ] /-- Expressing uniform convergence using `edist`. -/ lemma tendsto_uniformly_iff {ι : Type*} {F : ι → β → α} {f : β → α} {p : filter ι} : tendsto_uniformly F f p ↔ ∀ ε > 0, ∀ᶠ n in p, ∀ x, edist (f x) (F n x) < ε := by simp only [← tendsto_uniformly_on_univ, tendsto_uniformly_on_iff, mem_univ, forall_const] end emetric open emetric /-- An emetric space is separated -/ @[priority 100] -- see Note [lower instance priority] instance to_separated : separated_space α := separated_def.2 $ λ x y h, eq_of_forall_edist_le $ λ ε ε0, le_of_lt (h _ (edist_mem_uniformity ε0)) /-- Auxiliary function to replace the uniformity on an emetric space with a uniformity which is equal to the original one, but maybe not defeq. This is useful if one wants to construct an emetric space with a specified uniformity. See Note [forgetful inheritance] explaining why having definitionally the right uniformity is often important. -/ def emetric_space.replace_uniformity {α} [U : uniform_space α] (m : emetric_space α) (H : @uniformity _ U = @uniformity _ emetric_space.to_uniform_space) : emetric_space α := { edist := @edist _ m.to_has_edist, edist_self := edist_self, eq_of_edist_eq_zero := @eq_of_edist_eq_zero _ _, edist_comm := edist_comm, edist_triangle := edist_triangle, to_uniform_space := U, uniformity_edist := H.trans (@emetric_space.uniformity_edist α _) } /-- The extended metric induced by an injective function taking values in an emetric space. -/ def emetric_space.induced {α β} (f : α → β) (hf : function.injective f) (m : emetric_space β) : emetric_space α := { edist := λ x y, edist (f x) (f y), edist_self := λ x, edist_self _, eq_of_edist_eq_zero := λ x y h, hf (edist_eq_zero.1 h), edist_comm := λ x y, edist_comm _ _, edist_triangle := λ x y z, edist_triangle _ _ _, to_uniform_space := uniform_space.comap f m.to_uniform_space, uniformity_edist := begin apply @uniformity_dist_of_mem_uniformity _ _ _ _ _ (λ x y, edist (f x) (f y)), refine λ s, mem_comap_sets.trans _, split; intro H, { rcases H with ⟨r, ru, rs⟩, rcases mem_uniformity_edist.1 ru with ⟨ε, ε0, hε⟩, refine ⟨ε, ε0, λ a b h, rs (hε _)⟩, exact h }, { rcases H with ⟨ε, ε0, hε⟩, exact ⟨_, edist_mem_uniformity ε0, λ ⟨a, b⟩, hε⟩ } end } /-- Emetric space instance on subsets of emetric spaces -/ instance {α : Type*} {p : α → Prop} [t : emetric_space α] : emetric_space (subtype p) := t.induced coe (λ x y, subtype.ext_iff_val.2) /-- The extended distance on a subset of an emetric space is the restriction of the original distance, by definition -/ theorem subtype.edist_eq {p : α → Prop} (x y : subtype p) : edist x y = edist (x : α) y := rfl /-- The product of two emetric spaces, with the max distance, is an extended metric spaces. We make sure that the uniform structure thus constructed is the one corresponding to the product of uniform spaces, to avoid diamond problems. -/ instance prod.emetric_space_max [emetric_space β] : emetric_space (α × β) := { edist := λ x y, max (edist x.1 y.1) (edist x.2 y.2), edist_self := λ x, by simp, eq_of_edist_eq_zero := λ x y h, begin cases max_le_iff.1 (le_of_eq h) with h₁ h₂, have A : x.fst = y.fst := edist_le_zero.1 h₁, have B : x.snd = y.snd := edist_le_zero.1 h₂, exact prod.ext_iff.2 ⟨A, B⟩ end, edist_comm := λ x y, by simp [edist_comm], edist_triangle := λ x y z, max_le (le_trans (edist_triangle _ _ _) (add_le_add (le_max_left _ _) (le_max_left _ _))) (le_trans (edist_triangle _ _ _) (add_le_add (le_max_right _ _) (le_max_right _ _))), uniformity_edist := begin refine uniformity_prod.trans _, simp [emetric_space.uniformity_edist, comap_infi], rw ← infi_inf_eq, congr, funext, rw ← infi_inf_eq, congr, funext, simp [inf_principal, ext_iff, max_lt_iff] end, to_uniform_space := prod.uniform_space } lemma prod.edist_eq [emetric_space β] (x y : α × β) : edist x y = max (edist x.1 y.1) (edist x.2 y.2) := rfl section pi open finset variables {π : β → Type*} [fintype β] /-- The product of a finite number of emetric spaces, with the max distance, is still an emetric space. This construction would also work for infinite products, but it would not give rise to the product topology. Hence, we only formalize it in the good situation of finitely many spaces. -/ instance emetric_space_pi [∀b, emetric_space (π b)] : emetric_space (Πb, π b) := { edist := λ f g, finset.sup univ (λb, edist (f b) (g b)), edist_self := assume f, bot_unique $ finset.sup_le $ by simp, edist_comm := assume f g, by unfold edist; congr; funext a; exact edist_comm _ _, edist_triangle := assume f g h, begin simp only [finset.sup_le_iff], assume b hb, exact le_trans (edist_triangle _ (g b) _) (add_le_add (le_sup hb) (le_sup hb)) end, eq_of_edist_eq_zero := assume f g eq0, begin have eq1 : sup univ (λ (b : β), edist (f b) (g b)) ≤ 0 := le_of_eq eq0, simp only [finset.sup_le_iff] at eq1, exact (funext $ assume b, edist_le_zero.1 $ eq1 b $ mem_univ b), end, to_uniform_space := Pi.uniform_space _, uniformity_edist := begin simp only [Pi.uniformity, emetric_space.uniformity_edist, comap_infi, gt_iff_lt, preimage_set_of_eq, comap_principal], rw infi_comm, congr, funext ε, rw infi_comm, congr, funext εpos, change 0 < ε at εpos, simp [set.ext_iff, εpos] end } lemma edist_pi_def [Π b, emetric_space (π b)] (f g : Π b, π b) : edist f g = finset.sup univ (λb, edist (f b) (g b)) := rfl @[simp] lemma edist_pi_const [nonempty β] (a b : α) : edist (λ x : β, a) (λ _, b) = edist a b := finset.sup_const univ_nonempty (edist a b) end pi namespace emetric variables {x y z : α} {ε ε₁ ε₂ : ℝ≥0∞} {s : set α} /-- `emetric.ball x ε` is the set of all points `y` with `edist y x < ε` -/ def ball (x : α) (ε : ℝ≥0∞) : set α := {y | edist y x < ε} @[simp] theorem mem_ball : y ∈ ball x ε ↔ edist y x < ε := iff.rfl theorem mem_ball' : y ∈ ball x ε ↔ edist x y < ε := by rw edist_comm; refl /-- `emetric.closed_ball x ε` is the set of all points `y` with `edist y x ≤ ε` -/ def closed_ball (x : α) (ε : ℝ≥0∞) := {y | edist y x ≤ ε} @[simp] theorem mem_closed_ball : y ∈ closed_ball x ε ↔ edist y x ≤ ε := iff.rfl theorem ball_subset_closed_ball : ball x ε ⊆ closed_ball x ε := assume y, by simp; intros h; apply le_of_lt h theorem pos_of_mem_ball (hy : y ∈ ball x ε) : 0 < ε := lt_of_le_of_lt (zero_le _) hy theorem mem_ball_self (h : 0 < ε) : x ∈ ball x ε := show edist x x < ε, by rw edist_self; assumption theorem mem_closed_ball_self : x ∈ closed_ball x ε := show edist x x ≤ ε, by rw edist_self; exact bot_le theorem mem_ball_comm : x ∈ ball y ε ↔ y ∈ ball x ε := by simp [edist_comm] theorem ball_subset_ball (h : ε₁ ≤ ε₂) : ball x ε₁ ⊆ ball x ε₂ := λ y (yx : _ < ε₁), lt_of_lt_of_le yx h theorem closed_ball_subset_closed_ball (h : ε₁ ≤ ε₂) : closed_ball x ε₁ ⊆ closed_ball x ε₂ := λ y (yx : _ ≤ ε₁), le_trans yx h theorem ball_disjoint (h : ε₁ + ε₂ ≤ edist x y) : ball x ε₁ ∩ ball y ε₂ = ∅ := eq_empty_iff_forall_not_mem.2 $ λ z ⟨h₁, h₂⟩, not_lt_of_le (edist_triangle_left x y z) (lt_of_lt_of_le (ennreal.add_lt_add h₁ h₂) h) theorem ball_subset (h : edist x y + ε₁ ≤ ε₂) (h' : edist x y < ⊤) : ball x ε₁ ⊆ ball y ε₂ := λ z zx, calc edist z y ≤ edist z x + edist x y : edist_triangle _ _ _ ... = edist x y + edist z x : add_comm _ _ ... < edist x y + ε₁ : (ennreal.add_lt_add_iff_left h').2 zx ... ≤ ε₂ : h theorem exists_ball_subset_ball (h : y ∈ ball x ε) : ∃ ε' > 0, ball y ε' ⊆ ball x ε := begin have : 0 < ε - edist y x := by simpa using h, refine ⟨ε - edist y x, this, ball_subset _ _⟩, { rw ennreal.add_sub_cancel_of_le (le_of_lt h), apply le_refl _}, { have : edist y x ≠ ⊤ := ne_top_of_lt h, apply lt_top_iff_ne_top.2 this } end theorem ball_eq_empty_iff : ball x ε = ∅ ↔ ε = 0 := eq_empty_iff_forall_not_mem.trans ⟨λh, le_bot_iff.1 (le_of_not_gt (λ ε0, h _ (mem_ball_self ε0))), λε0 y h, not_lt_of_le (le_of_eq ε0) (pos_of_mem_ball h)⟩ /-- Relation “two points are at a finite edistance” is an equivalence relation. -/ def edist_lt_top_setoid : setoid α := { r := λ x y, edist x y < ⊤, iseqv := ⟨λ x, by { rw edist_self, exact ennreal.coe_lt_top }, λ x y h, by rwa edist_comm, λ x y z hxy hyz, lt_of_le_of_lt (edist_triangle x y z) (ennreal.add_lt_top.2 ⟨hxy, hyz⟩)⟩ } @[simp] lemma ball_zero : ball x 0 = ∅ := by rw [emetric.ball_eq_empty_iff] theorem nhds_basis_eball : (𝓝 x).has_basis (λ ε:ℝ≥0∞, 0 < ε) (ball x) := nhds_basis_uniformity uniformity_basis_edist theorem nhds_basis_closed_eball : (𝓝 x).has_basis (λ ε:ℝ≥0∞, 0 < ε) (closed_ball x) := nhds_basis_uniformity uniformity_basis_edist_le theorem nhds_eq : 𝓝 x = (⨅ε>0, 𝓟 (ball x ε)) := nhds_basis_eball.eq_binfi theorem mem_nhds_iff : s ∈ 𝓝 x ↔ ∃ε>0, ball x ε ⊆ s := nhds_basis_eball.mem_iff theorem is_open_iff : is_open s ↔ ∀x∈s, ∃ε>0, ball x ε ⊆ s := by simp [is_open_iff_nhds, mem_nhds_iff] theorem is_open_ball : is_open (ball x ε) := is_open_iff.2 $ λ y, exists_ball_subset_ball theorem is_closed_ball_top : is_closed (ball x ⊤) := is_open_iff.2 $ λ y hy, ⟨⊤, ennreal.coe_lt_top, subset_compl_iff_disjoint.2 $ ball_disjoint $ by { rw ennreal.top_add, exact le_of_not_lt hy }⟩ theorem ball_mem_nhds (x : α) {ε : ℝ≥0∞} (ε0 : 0 < ε) : ball x ε ∈ 𝓝 x := mem_nhds_sets is_open_ball (mem_ball_self ε0) theorem ball_prod_same [emetric_space β] (x : α) (y : β) (r : ℝ≥0∞) : (ball x r).prod (ball y r) = ball (x, y) r := ext $ λ z, max_lt_iff.symm theorem closed_ball_prod_same [emetric_space β] (x : α) (y : β) (r : ℝ≥0∞) : (closed_ball x r).prod (closed_ball y r) = closed_ball (x, y) r := ext $ λ z, max_le_iff.symm /-- ε-characterization of the closure in emetric spaces -/ theorem mem_closure_iff : x ∈ closure s ↔ ∀ε>0, ∃y ∈ s, edist x y < ε := (mem_closure_iff_nhds_basis nhds_basis_eball).trans $ by simp only [mem_ball, edist_comm x] theorem tendsto_nhds {f : filter β} {u : β → α} {a : α} : tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, edist (u x) a < ε := nhds_basis_eball.tendsto_right_iff theorem tendsto_at_top [nonempty β] [semilattice_sup β] {u : β → α} {a : α} : tendsto u at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, edist (u n) a < ε := (at_top_basis.tendsto_iff nhds_basis_eball).trans $ by simp only [exists_prop, true_and, mem_Ici, mem_ball] /-- In an emetric space, Cauchy sequences are characterized by the fact that, eventually, the edistance between its elements is arbitrarily small -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem cauchy_seq_iff [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ε>0, ∃N, ∀m n≥N, edist (u m) (u n) < ε := uniformity_basis_edist.cauchy_seq_iff /-- A variation around the emetric characterization of Cauchy sequences -/ theorem cauchy_seq_iff' [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ε>(0 : ℝ≥0∞), ∃N, ∀n≥N, edist (u n) (u N) < ε := uniformity_basis_edist.cauchy_seq_iff' /-- A variation of the emetric characterization of Cauchy sequences that deals with `ℝ≥0` upper bounds. -/ theorem cauchy_seq_iff_nnreal [nonempty β] [semilattice_sup β] {u : β → α} : cauchy_seq u ↔ ∀ ε : ℝ≥0, 0 < ε → ∃ N, ∀ n, N ≤ n → edist (u n) (u N) < ε := uniformity_basis_edist_nnreal.cauchy_seq_iff' theorem totally_bounded_iff {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t : set α, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, H _ (edist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ theorem totally_bounded_iff' {s : set α} : totally_bounded s ↔ ∀ ε > 0, ∃t⊆s, finite t ∧ s ⊆ ⋃y∈t, ball y ε := ⟨λ H ε ε0, (totally_bounded_iff_subset.1 H) _ (edist_mem_uniformity ε0), λ H r ru, let ⟨ε, ε0, hε⟩ := mem_uniformity_edist.1 ru, ⟨t, _, ft, h⟩ := H ε ε0 in ⟨t, ft, subset.trans h $ Union_subset_Union $ λ y, Union_subset_Union $ λ yt z, hε⟩⟩ section compact /-- A compact set in an emetric space is separable, i.e., it is the closure of a countable set -/ lemma countable_closure_of_compact {α : Type u} [emetric_space α] {s : set α} (hs : is_compact s) : ∃ t ⊆ s, (countable t ∧ s = closure t) := begin have A : ∀ (e:ℝ≥0∞), e > 0 → ∃ t ⊆ s, (finite t ∧ s ⊆ (⋃x∈t, ball x e)) := totally_bounded_iff'.1 (compact_iff_totally_bounded_complete.1 hs).1, -- assume e, finite_cover_balls_of_compact hs, have B : ∀ (e:ℝ≥0∞), ∃ t ⊆ s, finite t ∧ (e > 0 → s ⊆ (⋃x∈t, ball x e)), { intro e, cases le_or_gt e 0 with h, { exact ⟨∅, by finish⟩ }, { rcases A e h with ⟨s, ⟨finite_s, closure_s⟩⟩, existsi s, finish }}, /-The desired countable set is obtained by taking for each `n` the centers of a finite cover by balls of radius `1/n`, and then the union over `n`. -/ choose T T_in_s finite_T using B, let t := ⋃n:ℕ, T n⁻¹, have T₁ : t ⊆ s := begin apply Union_subset, assume n, apply T_in_s end, have T₂ : countable t := by finish [countable_Union, finite.countable], have T₃ : s ⊆ closure t, { intros x x_in_s, apply mem_closure_iff.2, intros ε εpos, rcases ennreal.exists_inv_nat_lt (bot_lt_iff_ne_bot.1 εpos) with ⟨n, hn⟩, have inv_n_pos : (0 : ℝ≥0∞) < (n : ℕ)⁻¹ := by simp [ennreal.bot_lt_iff_ne_bot], have C : x ∈ (⋃y∈ T (n : ℕ)⁻¹, ball y (n : ℕ)⁻¹) := mem_of_mem_of_subset x_in_s ((finite_T (n : ℕ)⁻¹).2 inv_n_pos), rcases mem_Union.1 C with ⟨y, _, ⟨y_in_T, rfl⟩, Dxy⟩, simp at Dxy, -- Dxy : edist x y < 1 / ↑n have : y ∈ t := mem_of_mem_of_subset y_in_T (by apply subset_Union (λ (n:ℕ), T (n : ℕ)⁻¹)), have : edist x y < ε := lt_trans Dxy hn, exact ⟨y, ‹y ∈ t›, ‹edist x y < ε›⟩ }, have T₄ : closure t ⊆ s := calc closure t ⊆ closure s : closure_mono T₁ ... = s : hs.is_closed.closure_eq, exact ⟨t, ⟨T₁, T₂, subset.antisymm T₃ T₄⟩⟩ end end compact section first_countable @[priority 100] -- see Note [lower instance priority] instance (α : Type u) [emetric_space α] : topological_space.first_countable_topology α := uniform_space.first_countable_topology uniformity_has_countable_basis end first_countable section second_countable open topological_space /-- A separable emetric space is second countable: one obtains a countable basis by taking the balls centered at points in a dense subset, and with rational radii. We do not register this as an instance, as there is already an instance going in the other direction from second countable spaces to separable spaces, and we want to avoid loops. -/ lemma second_countable_of_separable (α : Type u) [emetric_space α] [separable_space α] : second_countable_topology α := uniform_space.second_countable_of_separable uniformity_has_countable_basis end second_countable section diam /-- The diameter of a set in an emetric space, named `emetric.diam` -/ def diam (s : set α) := ⨆ (x ∈ s) (y ∈ s), edist x y lemma diam_le_iff_forall_edist_le {d : ℝ≥0∞} : diam s ≤ d ↔ ∀ (x ∈ s) (y ∈ s), edist x y ≤ d := by simp only [diam, supr_le_iff] /-- If two points belong to some set, their edistance is bounded by the diameter of the set -/ lemma edist_le_diam_of_mem (hx : x ∈ s) (hy : y ∈ s) : edist x y ≤ diam s := diam_le_iff_forall_edist_le.1 (le_refl _) x hx y hy /-- If the distance between any two points in a set is bounded by some constant, this constant bounds the diameter. -/ lemma diam_le_of_forall_edist_le {d : ℝ≥0∞} (h : ∀ (x ∈ s) (y ∈ s), edist x y ≤ d) : diam s ≤ d := diam_le_iff_forall_edist_le.2 h /-- The diameter of a subsingleton vanishes. -/ lemma diam_subsingleton (hs : s.subsingleton) : diam s = 0 := nonpos_iff_eq_zero.1 $ diam_le_of_forall_edist_le $ λ x hx y hy, (hs hx hy).symm ▸ edist_self y ▸ le_refl _ /-- The diameter of the empty set vanishes -/ @[simp] lemma diam_empty : diam (∅ : set α) = 0 := diam_subsingleton subsingleton_empty /-- The diameter of a singleton vanishes -/ @[simp] lemma diam_singleton : diam ({x} : set α) = 0 := diam_subsingleton subsingleton_singleton lemma diam_eq_zero_iff : diam s = 0 ↔ s.subsingleton := ⟨λ h x hx y hy, edist_le_zero.1 $ h ▸ edist_le_diam_of_mem hx hy, diam_subsingleton⟩ lemma diam_pos_iff : 0 < diam s ↔ ∃ (x ∈ s) (y ∈ s), x ≠ y := begin have := not_congr (@diam_eq_zero_iff _ _ s), dunfold set.subsingleton at this, push_neg at this, simpa only [pos_iff_ne_zero, exists_prop] using this end lemma diam_insert : diam (insert x s) = max (⨆ y ∈ s, edist x y) (diam s) := eq_of_forall_ge_iff $ λ d, by simp only [diam_le_iff_forall_edist_le, ball_insert_iff, edist_self, edist_comm x, max_le_iff, supr_le_iff, zero_le, true_and, forall_and_distrib, and_self, ← and_assoc] lemma diam_pair : diam ({x, y} : set α) = edist x y := by simp only [supr_singleton, diam_insert, diam_singleton, ennreal.max_zero_right] lemma diam_triple : diam ({x, y, z} : set α) = max (max (edist x y) (edist x z)) (edist y z) := by simp only [diam_insert, supr_insert, supr_singleton, diam_singleton, ennreal.max_zero_right, ennreal.sup_eq_max] /-- The diameter is monotonous with respect to inclusion -/ lemma diam_mono {s t : set α} (h : s ⊆ t) : diam s ≤ diam t := diam_le_of_forall_edist_le $ λ x hx y hy, edist_le_diam_of_mem (h hx) (h hy) /-- The diameter of a union is controlled by the diameter of the sets, and the edistance between two points in the sets. -/ lemma diam_union {t : set α} (xs : x ∈ s) (yt : y ∈ t) : diam (s ∪ t) ≤ diam s + edist x y + diam t := begin have A : ∀a ∈ s, ∀b ∈ t, edist a b ≤ diam s + edist x y + diam t := λa ha b hb, calc edist a b ≤ edist a x + edist x y + edist y b : edist_triangle4 _ _ _ _ ... ≤ diam s + edist x y + diam t : add_le_add (add_le_add (edist_le_diam_of_mem ha xs) (le_refl _)) (edist_le_diam_of_mem yt hb), refine diam_le_of_forall_edist_le (λa ha b hb, _), cases (mem_union _ _ _).1 ha with h'a h'a; cases (mem_union _ _ _).1 hb with h'b h'b, { calc edist a b ≤ diam s : edist_le_diam_of_mem h'a h'b ... ≤ diam s + (edist x y + diam t) : le_add_right (le_refl _) ... = diam s + edist x y + diam t : by simp only [add_comm, eq_self_iff_true, add_left_comm] }, { exact A a h'a b h'b }, { have Z := A b h'b a h'a, rwa [edist_comm] at Z }, { calc edist a b ≤ diam t : edist_le_diam_of_mem h'a h'b ... ≤ (diam s + edist x y) + diam t : le_add_left (le_refl _) } end lemma diam_union' {t : set α} (h : (s ∩ t).nonempty) : diam (s ∪ t) ≤ diam s + diam t := let ⟨x, ⟨xs, xt⟩⟩ := h in by simpa using diam_union xs xt lemma diam_closed_ball {r : ℝ≥0∞} : diam (closed_ball x r) ≤ 2 * r := diam_le_of_forall_edist_le $ λa ha b hb, calc edist a b ≤ edist a x + edist b x : edist_triangle_right _ _ _ ... ≤ r + r : add_le_add ha hb ... = 2 * r : by simp [mul_two, mul_comm] lemma diam_ball {r : ℝ≥0∞} : diam (ball x r) ≤ 2 * r := le_trans (diam_mono ball_subset_closed_ball) diam_closed_ball end diam end emetric --namespace
f2364cb8e623ce1c2219042e6fcc792092a15921
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/measure_theory/covering/differentiation.lean
03e84a2f68a584a41bef7cf4ecbc75c868ad2c4c
[ "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
42,177
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.covering.vitali_family import measure_theory.measure.regular import measure_theory.function.ae_measurable_order import measure_theory.integral.lebesgue import measure_theory.decomposition.radon_nikodym /-! # Differentiation of measures On a metric space with a measure `μ`, consider a Vitali family (i.e., for each `x` one has a family of sets shrinking to `x`, with a good behavior with respect to covering theorems). Consider also another measure `ρ`. Then, for almost every `x`, the ratio `ρ a / μ a` converges when `a` shrinks to `x` along the Vitali family, towards the Radon-Nikodym derivative of `ρ` with respect to `μ`. This is the main theorem on differentiation of measures. This theorem is proved in this file, under the name `vitali_family.ae_tendsto_rn_deriv`. Note that, almost surely, `μ a` is eventually positive and finite (see `vitali_family.ae_eventually_measure_pos` and `vitali_family.eventually_measure_lt_top`), so the ratio really makes sense. For concrete applications, one needs concrete instances of Vitali families, as provided for instance by `besicovitch.vitali_family` (for balls) or by `vitali.vitali_family` (for doubling measures). ## Sketch of proof Let `v` be a Vitali family for `μ`. Assume for simplicity that `ρ` is absolutely continuous with respect to `μ`, as the case of a singular measure is easier. It is easy to see that a set `s` on which `liminf ρ a / μ a < q` satisfies `ρ s ≤ q * μ s`, by using a disjoint subcovering provided by the definition of Vitali families. Similarly for the limsup. It follows that a set on which `ρ a / μ a` oscillates has measure `0`, and therefore that `ρ a / μ a` converges almost surely (`vitali_family.ae_tendsto_div`). Moreover, on a set where the limit is close to a constant `c`, one gets `ρ s ∼ c μ s`, using again a covering lemma as above. It follows that `ρ` is equal to `μ.with_density (v.lim_ratio ρ x)`, where `v.lim_ratio ρ x` is the limit of `ρ a / μ a` at `x` (which is well defined almost everywhere). By uniqueness of the Radon-Nikodym derivative, one gets `v.lim_ratio ρ x = ρ.rn_deriv μ x` almost everywhere, completing the proof. There is a difficulty in this sketch: this argument works well when `v.lim_ratio ρ` is measurable, but there is no guarantee that this is the case, especially if one doesn't make further assumptions on the Vitali family. We use an indirect argument to show that `v.lim_ratio ρ` is always almost everywhere measurable, again based on the disjoint subcovering argument (see `vitali_family.exists_measurable_supersets_lim_ratio`), and then proceed as sketched above but replacing `v.lim_ratio ρ` by a measurable version called `v.lim_ratio_meas ρ`. ## References * [Herbert Federer, Geometric Measure Theory, Chapter 2.9][Federer1996] -/ open measure_theory metric set filter topological_space measure_theory.measure open_locale filter ennreal measure_theory nnreal topological_space local attribute [instance] emetric.second_countable_of_sigma_compact variables {α : Type*} [metric_space α] {m0 : measurable_space α} {μ : measure α} (v : vitali_family μ) include v namespace vitali_family /-- The limit along a Vitali family of `ρ a / μ a` where it makes sense, and garbage otherwise. Do *not* use this definition: it is only a temporary device to show that this ratio tends almost everywhere to the Radon-Nikodym derivative. -/ noncomputable def lim_ratio (ρ : measure α) (x : α) : ℝ≥0∞ := lim (v.filter_at x) (λ a, ρ a / μ a) /-- For almost every point `x`, sufficiently small sets in a Vitali family around `x` have positive measure. (This is a nontrivial result, following from the covering property of Vitali families). -/ theorem ae_eventually_measure_pos [second_countable_topology α] : ∀ᵐ x ∂μ, ∀ᶠ a in v.filter_at x, 0 < μ a := begin set s := {x | ¬ (∀ᶠ a in v.filter_at x, 0 < μ a)} with hs, simp only [not_lt, not_eventually, nonpos_iff_eq_zero] at hs, change μ s = 0, let f : α → set (set α) := λ x, {a | μ a = 0}, have h : v.fine_subfamily_on f s, { assume x hx ε εpos, rw hs at hx, simp only [frequently_filter_at_iff, exists_prop, gt_iff_lt, mem_set_of_eq] at hx, rcases hx ε εpos with ⟨a, a_sets, ax, μa⟩, exact ⟨a, ⟨a_sets, μa⟩, ax⟩ }, refine le_antisymm _ bot_le, calc μ s ≤ ∑' (x : h.index), μ (h.covering x) : h.measure_le_tsum ... = ∑' (x : h.index), 0 : by { congr, ext1 x, exact h.covering_mem x.2 } ... = 0 : by simp only [tsum_zero, add_zero] end /-- For every point `x`, sufficiently small sets in a Vitali family around `x` have finite measure. (This is a trivial result, following from the fact that the measure is locally finite). -/ theorem eventually_measure_lt_top [is_locally_finite_measure μ] (x : α) : ∀ᶠ a in v.filter_at x, μ a < ∞ := begin obtain ⟨ε, εpos, με⟩ : ∃ (ε : ℝ) (hi : 0 < ε), μ (closed_ball x ε) < ∞ := (μ.finite_at_nhds x).exists_mem_basis nhds_basis_closed_ball, exact v.eventually_filter_at_iff.2 ⟨ε, εpos, λ a ha haε, (measure_mono haε).trans_lt με⟩, end /-- If two measures `ρ` and `ν` have, at every point of a set `s`, arbitrarily small sets in a Vitali family satisfying `ρ a ≤ ν a`, then `ρ s ≤ ν s` if `ρ ≪ μ`.-/ theorem measure_le_of_frequently_le [sigma_compact_space α] [borel_space α] {ρ : measure α} (ν : measure α) [is_locally_finite_measure ν] (hρ : ρ ≪ μ) (s : set α) (hs : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, ρ a ≤ ν a) : ρ s ≤ ν s := begin -- this follows from a covering argument using the sets satisfying `ρ a ≤ ν a`. apply ennreal.le_of_forall_pos_le_add (λ ε εpos hc, _), obtain ⟨U, sU, U_open, νU⟩ : ∃ (U : set α) (H : s ⊆ U), is_open U ∧ ν U ≤ ν s + ε := exists_is_open_le_add s ν (ennreal.coe_pos.2 εpos).ne', let f : α → set (set α) := λ x, {a | ρ a ≤ ν a ∧ a ⊆ U}, have h : v.fine_subfamily_on f s, { apply v.fine_subfamily_on_of_frequently f s (λ x hx, _), have := (hs x hx).and_eventually ((v.eventually_filter_at_mem_sets x).and (v.eventually_filter_at_subset_of_nhds (U_open.mem_nhds (sU hx)))), apply frequently.mono this, rintros a ⟨ρa, av, aU⟩, exact ⟨ρa, aU⟩ }, haveI : encodable h.index := h.index_countable.to_encodable, calc ρ s ≤ ∑' (x : h.index), ρ (h.covering x) : h.measure_le_tsum_of_absolutely_continuous hρ ... ≤ ∑' (x : h.index), ν (h.covering x) : ennreal.tsum_le_tsum (λ x, (h.covering_mem x.2).1) ... = ν (⋃ (x : h.index), h.covering x) : by rw [measure_Union h.covering_disjoint_subtype (λ i, h.measurable_set_u i.2)] ... ≤ ν U : measure_mono (Union_subset (λ i, (h.covering_mem i.2).2)) ... ≤ ν s + ε : νU end section variables [sigma_compact_space α] [borel_space α] [is_locally_finite_measure μ] {ρ : measure α} [is_locally_finite_measure ρ] /-- If a measure `ρ` is singular with respect to `μ`, then for `μ` almost every `x`, the ratio `ρ a / μ a` tends to zero when `a` shrinks to `x` along the Vitali family. This makes sense as `μ a` is eventually positive by `ae_eventually_measure_pos`. -/ lemma ae_eventually_measure_zero_of_singular (hρ : ρ ⊥ₘ μ) : ∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 0) := begin have A : ∀ ε > (0 : ℝ≥0), ∀ᵐ x ∂μ, ∀ᶠ a in v.filter_at x, ρ a < ε * μ a, { assume ε εpos, set s := {x | ¬(∀ᶠ a in v.filter_at x, ρ a < ε * μ a) } with hs, change μ s = 0, obtain ⟨o, o_meas, ρo, μo⟩ : ∃ (o : set α), measurable_set o ∧ ρ o = 0 ∧ μ oᶜ = 0 := hρ, apply le_antisymm _ bot_le, calc μ s ≤ μ ((s ∩ o) ∪ oᶜ) : begin conv_lhs { rw ← inter_union_compl s o }, exact measure_mono (union_subset_union_right _ (inter_subset_right _ _)) end ... ≤ μ (s ∩ o) + μ (oᶜ) : measure_union_le _ _ ... = μ (s ∩ o) : by rw [μo, add_zero] ... = ε⁻¹ * (ε • μ) (s ∩ o) : begin simp only [coe_nnreal_smul_apply, ← mul_assoc, mul_comm _ (ε : ℝ≥0∞)], rw [ennreal.mul_inv_cancel (ennreal.coe_pos.2 εpos).ne' ennreal.coe_ne_top, one_mul], end ... ≤ ε⁻¹ * ρ (s ∩ o) : begin apply ennreal.mul_le_mul le_rfl, refine v.measure_le_of_frequently_le ρ ((measure.absolutely_continuous.refl μ).smul ε) _ _, assume x hx, rw hs at hx, simp only [mem_inter_eq, not_lt, not_eventually, mem_set_of_eq] at hx, exact hx.1 end ... ≤ ε⁻¹ * ρ o : ennreal.mul_le_mul le_rfl (measure_mono (inter_subset_right _ _)) ... = 0 : by rw [ρo, mul_zero] }, obtain ⟨u, u_anti, u_pos, u_lim⟩ : ∃ (u : ℕ → ℝ≥0), strict_anti u ∧ (∀ (n : ℕ), 0 < u n) ∧ tendsto u at_top (𝓝 0) := exists_seq_strict_anti_tendsto (0 : ℝ≥0), have B : ∀ᵐ x ∂μ, ∀ n, ∀ᶠ a in v.filter_at x, ρ a < u n * μ a := ae_all_iff.2 (λ n, A (u n) (u_pos n)), filter_upwards [B, v.ae_eventually_measure_pos], assume x hx h'x, refine tendsto_order.2 ⟨λ z hz, (ennreal.not_lt_zero hz).elim, λ z hz, _⟩, obtain ⟨w, w_pos, w_lt⟩ : ∃ (w : ℝ≥0), (0 : ℝ≥0∞) < w ∧ (w : ℝ≥0∞) < z := ennreal.lt_iff_exists_nnreal_btwn.1 hz, obtain ⟨n, hn⟩ : ∃ n, u n < w := ((tendsto_order.1 u_lim).2 w (ennreal.coe_pos.1 w_pos)).exists, filter_upwards [hx n, h'x, v.eventually_measure_lt_top x], assume a ha μa_pos μa_lt_top, rw ennreal.div_lt_iff (or.inl μa_pos.ne') (or.inl μa_lt_top.ne), exact ha.trans_le (ennreal.mul_le_mul ((ennreal.coe_le_coe.2 hn.le).trans w_lt.le) le_rfl) end section absolutely_continuous variable (hρ : ρ ≪ μ) include hρ /-- A set of points `s` satisfying both `ρ a ≤ c * μ a` and `ρ a ≥ d * μ a` at arbitrarily small sets in a Vitali family has measure `0` if `c < d`. Indeed, the first inequality should imply that `ρ s ≤ c * μ s`, and the second one that `ρ s ≥ d * μ s`, a contradiction if `0 < μ s`. -/ theorem null_of_frequently_le_of_frequently_ge {c d : ℝ≥0} (hcd : c < d) (s : set α) (hc : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, ρ a ≤ c * μ a) (hd : ∀ x ∈ s, ∃ᶠ a in v.filter_at x, (d : ℝ≥0∞) * μ a ≤ ρ a) : μ s = 0 := begin apply null_of_locally_null s (λ x hx, _), obtain ⟨o, xo, o_open, μo⟩ : ∃ o : set α, x ∈ o ∧ is_open o ∧ μ o < ∞ := measure.exists_is_open_measure_lt_top μ x, refine ⟨s ∩ o, inter_mem_nhds_within _ (o_open.mem_nhds xo), _⟩, let s' := s ∩ o, by_contra, apply lt_irrefl (ρ s'), calc ρ s' ≤ c * μ s' : v.measure_le_of_frequently_le (c • μ) hρ s' (λ x hx, hc x hx.1) ... < d * μ s' : begin apply (ennreal.mul_lt_mul_right h _).2 (ennreal.coe_lt_coe.2 hcd), exact (lt_of_le_of_lt (measure_mono (inter_subset_right _ _)) μo).ne, end ... ≤ ρ s' : v.measure_le_of_frequently_le ρ ((measure.absolutely_continuous.refl μ).smul d) s' (λ x hx, hd x hx.1) end /-- If `ρ` is absolutely continuous with respect to `μ`, then for almost every `x`, the ratio `ρ a / μ a` converges as `a` shrinks to `x` along a Vitali family for `μ`. -/ theorem ae_tendsto_div : ∀ᵐ x ∂μ, ∃ c, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 c) := begin obtain ⟨w, w_count, w_dense, w_zero, w_top⟩ : ∃ w : set ℝ≥0∞, countable w ∧ dense w ∧ 0 ∉ w ∧ ∞ ∉ w := ennreal.exists_countable_dense_no_zero_top, have I : ∀ x ∈ w, x ≠ ∞ := λ x xs hx, w_top (hx ▸ xs), have A : ∀ (c ∈ w) (d ∈ w), (c < d) → ∀ᵐ x ∂μ, ¬((∃ᶠ a in v.filter_at x, ρ a / μ a < c) ∧ (∃ᶠ a in v.filter_at x, d < ρ a / μ a)), { assume c hc d hd hcd, lift c to ℝ≥0 using I c hc, lift d to ℝ≥0 using I d hd, apply v.null_of_frequently_le_of_frequently_ge hρ (ennreal.coe_lt_coe.1 hcd), { simp only [and_imp, exists_prop, not_frequently, not_and, not_lt, not_le, not_eventually, mem_set_of_eq, mem_compl_eq, not_forall], assume x h1x h2x, apply h1x.mono (λ a ha, _), refine (ennreal.div_le_iff_le_mul _ (or.inr (bot_le.trans_lt ha).ne')).1 ha.le, simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff] }, { simp only [and_imp, exists_prop, not_frequently, not_and, not_lt, not_le, not_eventually, mem_set_of_eq, mem_compl_eq, not_forall], assume x h1x h2x, apply h2x.mono (λ a ha, _), exact ennreal.mul_le_of_le_div ha.le } }, have B : ∀ᵐ x ∂μ, ∀ (c ∈ w) (d ∈ w), (c < d) → ¬((∃ᶠ a in v.filter_at x, ρ a / μ a < c) ∧ (∃ᶠ a in v.filter_at x, d < ρ a / μ a)), by simpa only [ae_ball_iff w_count, ae_imp_iff], filter_upwards [B], assume x hx, exact tendsto_of_no_upcrossings w_dense hx, end lemma ae_tendsto_lim_ratio : ∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio ρ x)) := begin filter_upwards [v.ae_tendsto_div hρ], assume x hx, exact tendsto_nhds_lim hx, end /-- Given two thresholds `p < q`, the sets `{x | v.lim_ratio ρ x < p}` and `{x | q < v.lim_ratio ρ x}` are obviously disjoint. The key to proving that `v.lim_ratio ρ` is almost everywhere measurable is to show that these sets have measurable supersets which are also disjoint, up to zero measure. This is the content of this lemma. -/ lemma exists_measurable_supersets_lim_ratio {p q : ℝ≥0} (hpq : p < q) : ∃ a b, measurable_set a ∧ measurable_set b ∧ {x | v.lim_ratio ρ x < p} ⊆ a ∧ {x | (q : ℝ≥0∞) < v.lim_ratio ρ x} ⊆ b ∧ μ (a ∩ b) = 0 := begin /- Here is a rough sketch, assuming that the measure is finite and the limit is well defined everywhere. Let `u := {x | v.lim_ratio ρ x < p}` and `w := {x | q < v.lim_ratio ρ x}`. They have measurable supersets `u'` and `w'` of the same measure. We will show that these satisfy the conclusion of the theorem, i.e., `μ (u' ∩ w') = 0`. For this, note that `ρ (u' ∩ w') = ρ (u ∩ w')` (as `w'` is measurable, see `measure_to_measurable_add_inter_left`). The latter set is included in the set where the limit of the ratios is `< p`, and therefore its measure is `≤ p * μ (u ∩ w')`. Using the same trick in the other direction gives that this is `p * μ (u' ∩ w')`. We have shown that `ρ (u' ∩ w') ≤ p * μ (u' ∩ w')`. Arguing in the same way but using the `w` part gives `q * μ (u' ∩ w') ≤ ρ (u' ∩ w')`. If `μ (u' ∩ w')` were nonzero, this would be a contradiction as `p < q`. For the rigorous proof, we need to work on a part of the space where the measure is finite (provided by `spanning_sets (ρ + μ)`) and to restrict to the set where the limit is well defined (called `s` below, of full measure). Otherwise, the argument goes through. -/ let s := {x | ∃ c, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 c)}, let o : ℕ → set α := spanning_sets (ρ + μ), let u := λ n, s ∩ {x | v.lim_ratio ρ x < p} ∩ o n, let w := λ n, s ∩ {x | (q : ℝ≥0∞) < v.lim_ratio ρ x} ∩ o n, -- the supersets are obtained by restricting to the set `s` where the limit is well defined, to -- a finite measure part `o n`, taking a measurable superset here, and then taking the union over -- `n`. refine ⟨to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (u n)), to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (w n)), _, _, _, _, _⟩, -- check that these sets are measurable supersets as required { exact (measurable_set_to_measurable _ _).union (measurable_set.Union (λ n, (measurable_set_to_measurable _ _))) }, { exact (measurable_set_to_measurable _ _).union (measurable_set.Union (λ n, (measurable_set_to_measurable _ _))) }, { assume x hx, by_cases h : x ∈ s, { refine or.inr (mem_Union.2 ⟨spanning_sets_index (ρ + μ) x, _⟩), exact subset_to_measurable _ _ ⟨⟨h, hx⟩, mem_spanning_sets_index _ _⟩ }, { exact or.inl (subset_to_measurable μ sᶜ h) } }, { assume x hx, by_cases h : x ∈ s, { refine or.inr (mem_Union.2 ⟨spanning_sets_index (ρ + μ) x, _⟩), exact subset_to_measurable _ _ ⟨⟨h, hx⟩, mem_spanning_sets_index _ _⟩ }, { exact or.inl (subset_to_measurable μ sᶜ h) } }, -- it remains to check the nontrivial part that these sets have zero measure intersection. -- it suffices to do it for fixed `m` and `n`, as one is taking countable unions. suffices H : ∀ (m n : ℕ), μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) = 0, { have A : (to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (u n))) ∩ (to_measurable μ sᶜ ∪ (⋃ n, to_measurable (ρ + μ) (w n))) ⊆ to_measurable μ sᶜ ∪ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))), { simp only [inter_distrib_left, inter_distrib_right, true_and, subset_union_left, union_subset_iff, inter_self], refine ⟨_, _, _⟩, { exact (inter_subset_left _ _).trans (subset_union_left _ _) }, { exact (inter_subset_right _ _).trans (subset_union_left _ _) }, { simp_rw [Union_inter, inter_Union], exact subset_union_right _ _ } }, refine le_antisymm ((measure_mono A).trans _) bot_le, calc μ (to_measurable μ sᶜ ∪ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n)))) ≤ μ (to_measurable μ sᶜ) + μ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))) : measure_union_le _ _ ... = μ (⋃ m n, (to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))) : by { have : μ sᶜ = 0 := v.ae_tendsto_div hρ, rw [measure_to_measurable, this, zero_add] } ... ≤ ∑' m n, μ ((to_measurable (ρ + μ) (u m)) ∩ (to_measurable (ρ + μ) (w n))) : (measure_Union_le _).trans (ennreal.tsum_le_tsum (λ m, measure_Union_le _)) ... = 0 : by simp only [H, tsum_zero] }, -- now starts the nontrivial part of the argument. We fix `m` and `n`, and show that the -- measurable supersets of `u m` and `w n` have zero measure intersection by using the lemmas -- `measure_to_measurable_add_inter_left` (to reduce to `u m` or `w n` instead of the measurable -- superset) and `measure_le_of_frequently_le` to compare their measures for `ρ` and `μ`. assume m n, have I : (ρ + μ) (u m) ≠ ∞, { apply (lt_of_le_of_lt (measure_mono _) (measure_spanning_sets_lt_top (ρ + μ) m)).ne, exact inter_subset_right _ _ }, have J : (ρ + μ) (w n) ≠ ∞, { apply (lt_of_le_of_lt (measure_mono _) (measure_spanning_sets_lt_top (ρ + μ) n)).ne, exact inter_subset_right _ _ }, have A : ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) ≤ p * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) := calc ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) = ρ (u m ∩ to_measurable (ρ + μ) (w n)) : measure_to_measurable_add_inter_left (measurable_set_to_measurable _ _) I ... ≤ (p • μ) (u m ∩ to_measurable (ρ + μ) (w n)) : begin refine v.measure_le_of_frequently_le _ hρ _ (λ x hx, _), have L : tendsto (λ (a : set α), ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio ρ x)) := tendsto_nhds_lim hx.1.1.1, have I : ∀ᶠ (b : set α) in v.filter_at x, ρ b / μ b < p := (tendsto_order.1 L).2 _ hx.1.1.2, apply I.frequently.mono (λ a ha, _), rw [coe_nnreal_smul_apply], refine (ennreal.div_le_iff_le_mul _ (or.inr (bot_le.trans_lt ha).ne')).1 ha.le, simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff] end ... = p * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : by simp only [coe_nnreal_smul_apply, (measure_to_measurable_add_inter_right (measurable_set_to_measurable _ _) I)], have B : (q : ℝ≥0∞) * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) ≤ ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) := calc (q : ℝ≥0∞) * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) = (q : ℝ≥0∞) * μ (to_measurable (ρ + μ) (u m) ∩ w n) : begin conv_rhs { rw inter_comm }, rw [inter_comm, measure_to_measurable_add_inter_right (measurable_set_to_measurable _ _) J] end ... ≤ ρ (to_measurable (ρ + μ) (u m) ∩ w n) : begin rw [← coe_nnreal_smul_apply], refine v.measure_le_of_frequently_le _ (absolutely_continuous.rfl.coe_nnreal_smul _) _ _, assume x hx, have L : tendsto (λ (a : set α), ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio ρ x)) := tendsto_nhds_lim hx.2.1.1, have I : ∀ᶠ (b : set α) in v.filter_at x, (q : ℝ≥0∞) < ρ b / μ b := (tendsto_order.1 L).1 _ hx.2.1.2, apply I.frequently.mono (λ a ha, _), rw [coe_nnreal_smul_apply], exact ennreal.mul_le_of_le_div ha.le end ... = ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : begin conv_rhs { rw inter_comm }, rw inter_comm, exact (measure_to_measurable_add_inter_left (measurable_set_to_measurable _ _) J).symm, end, by_contra, apply lt_irrefl (ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n))), calc ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) ≤ p * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : A ... < q * μ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : begin apply (ennreal.mul_lt_mul_right h _).2 (ennreal.coe_lt_coe.2 hpq), suffices H : (ρ + μ) (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) ≠ ∞, { simp only [not_or_distrib, ennreal.add_eq_top, pi.add_apply, ne.def, coe_add] at H, exact H.2 }, apply (lt_of_le_of_lt (measure_mono (inter_subset_left _ _)) _).ne, rw measure_to_measurable, apply lt_of_le_of_lt (measure_mono _) (measure_spanning_sets_lt_top (ρ + μ) m), exact inter_subset_right _ _ end ... ≤ ρ (to_measurable (ρ + μ) (u m) ∩ to_measurable (ρ + μ) (w n)) : B end theorem ae_measurable_lim_ratio : ae_measurable (v.lim_ratio ρ) μ := begin apply ennreal.ae_measurable_of_exist_almost_disjoint_supersets _ _ (λ p q hpq, _), exact v.exists_measurable_supersets_lim_ratio hρ hpq, end /-- A measurable version of `v.lim_ratio ρ`. Do *not* use this definition: it is only a temporary device to show that `v.lim_ratio` is almost everywhere equal to the Radon-Nikodym derivative. -/ noncomputable def lim_ratio_meas : α → ℝ≥0∞ := (v.ae_measurable_lim_ratio hρ).mk _ lemma lim_ratio_meas_measurable : measurable (v.lim_ratio_meas hρ) := ae_measurable.measurable_mk _ lemma ae_tendsto_lim_ratio_meas : ∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio_meas hρ x)) := begin filter_upwards [v.ae_tendsto_lim_ratio hρ, ae_measurable.ae_eq_mk (v.ae_measurable_lim_ratio hρ)], assume x hx h'x, rwa h'x at hx, end /-- If, for all `x` in a set `s`, one has frequently `ρ a / μ a < p`, then `ρ s ≤ p * μ s`, as proved in `measure_le_of_frequently_le`. Since `ρ a / μ a` tends almost everywhere to `v.lim_ratio_meas hρ x`, the same property holds for sets `s` on which `v.lim_ratio_meas hρ < p`. -/ lemma measure_le_mul_of_subset_lim_ratio_meas_lt {p : ℝ≥0} {s : set α} (h : s ⊆ {x | v.lim_ratio_meas hρ x < p}) : ρ s ≤ p * μ s := begin let t := {x : α | tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio_meas hρ x))}, have A : μ tᶜ = 0 := v.ae_tendsto_lim_ratio_meas hρ, suffices H : ρ (s ∩ t) ≤ (p • μ) (s ∩ t), from calc ρ s = ρ ((s ∩ t) ∪ (s ∩ tᶜ)) : by rw inter_union_compl ... ≤ ρ (s ∩ t) + ρ (s ∩ tᶜ) : measure_union_le _ _ ... ≤ p * μ (s ∩ t) + 0 : add_le_add H ((measure_mono (inter_subset_right _ _)).trans (hρ A).le) ... ≤ p * μ s : by { rw add_zero, exact ennreal.mul_le_mul le_rfl (measure_mono (inter_subset_left _ _)) }, refine v.measure_le_of_frequently_le _ hρ _ (λ x hx, _), have I : ∀ᶠ (b : set α) in v.filter_at x, ρ b / μ b < p := (tendsto_order.1 hx.2).2 _ (h hx.1), apply I.frequently.mono (λ a ha, _), rw [coe_nnreal_smul_apply], refine (ennreal.div_le_iff_le_mul _ (or.inr (bot_le.trans_lt ha).ne')).1 ha.le, simp only [ennreal.coe_ne_top, ne.def, or_true, not_false_iff] end /-- If, for all `x` in a set `s`, one has frequently `q < ρ a / μ a`, then `q * μ s ≤ ρ s`, as proved in `measure_le_of_frequently_le`. Since `ρ a / μ a` tends almost everywhere to `v.lim_ratio_meas hρ x`, the same property holds for sets `s` on which `q < v.lim_ratio_meas hρ`. -/ lemma mul_measure_le_of_subset_lt_lim_ratio_meas {q : ℝ≥0} {s : set α} (h : s ⊆ {x | (q : ℝ≥0∞) < v.lim_ratio_meas hρ x}) : (q : ℝ≥0∞) * μ s ≤ ρ s := begin let t := {x : α | tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (v.lim_ratio_meas hρ x))}, have A : μ tᶜ = 0 := v.ae_tendsto_lim_ratio_meas hρ, suffices H : (q • μ) (s ∩ t) ≤ ρ (s ∩ t), from calc (q • μ) s = (q • μ) ((s ∩ t) ∪ (s ∩ tᶜ)) : by rw inter_union_compl ... ≤ (q • μ) (s ∩ t) + (q • μ) (s ∩ tᶜ) : measure_union_le _ _ ... ≤ ρ (s ∩ t) + q * μ tᶜ : begin apply add_le_add H, rw [coe_nnreal_smul_apply], exact ennreal.mul_le_mul le_rfl (measure_mono (inter_subset_right _ _)), end ... ≤ ρ s : by { rw [A, mul_zero, add_zero], exact measure_mono (inter_subset_left _ _) }, refine v.measure_le_of_frequently_le _ (absolutely_continuous.rfl.coe_nnreal_smul _) _ _, assume x hx, have I : ∀ᶠ a in v.filter_at x, (q : ℝ≥0∞) < ρ a / μ a := (tendsto_order.1 hx.2).1 _ (h hx.1), apply I.frequently.mono (λ a ha, _), rw [coe_nnreal_smul_apply], exact ennreal.mul_le_of_le_div ha.le end /-- The points with `v.lim_ratio_meas hρ x = ∞` have measure `0` for `μ`. -/ lemma measure_lim_ratio_meas_top : μ {x | v.lim_ratio_meas hρ x = ∞} = 0 := begin refine null_of_locally_null _ (λ x hx, _), obtain ⟨o, xo, o_open, μo⟩ : ∃ o : set α, x ∈ o ∧ is_open o ∧ ρ o < ∞ := measure.exists_is_open_measure_lt_top ρ x, let s := {x : α | v.lim_ratio_meas hρ x = ∞} ∩ o, refine ⟨s, inter_mem_nhds_within _ (o_open.mem_nhds xo), le_antisymm _ bot_le⟩, have ρs : ρ s ≠ ∞ := ((measure_mono (inter_subset_right _ _)).trans_lt μo).ne, have A : ∀ (q : ℝ≥0), 1 ≤ q → μ s ≤ q⁻¹ * ρ s, { assume q hq, rw [mul_comm, ← div_eq_mul_inv, ennreal.le_div_iff_mul_le _ (or.inr ρs), mul_comm], { apply v.mul_measure_le_of_subset_lt_lim_ratio_meas hρ, assume y hy, have : v.lim_ratio_meas hρ y = ∞ := hy.1, simp only [this, ennreal.coe_lt_top, mem_set_of_eq], }, { simp only [(zero_lt_one.trans_le hq).ne', true_or, ennreal.coe_eq_zero, ne.def, not_false_iff] } }, have B : tendsto (λ (q : ℝ≥0), (q : ℝ≥0∞)⁻¹ * ρ s) at_top (𝓝 (∞⁻¹ * ρ s)), { apply ennreal.tendsto.mul_const _ (or.inr ρs), exact ennreal.tendsto_inv_iff.2 (ennreal.tendsto_coe_nhds_top.2 tendsto_id) }, simp only [zero_mul, ennreal.inv_top] at B, apply ge_of_tendsto B, exact eventually_at_top.2 ⟨1, A⟩, end /-- The points with `v.lim_ratio_meas hρ x = 0` have measure `0` for `ρ`. -/ lemma measure_lim_ratio_meas_zero : ρ {x | v.lim_ratio_meas hρ x = 0} = 0 := begin refine null_of_locally_null _ (λ x hx, _), obtain ⟨o, xo, o_open, μo⟩ : ∃ o : set α, x ∈ o ∧ is_open o ∧ μ o < ∞ := measure.exists_is_open_measure_lt_top μ x, let s := {x : α | v.lim_ratio_meas hρ x = 0} ∩ o, refine ⟨s, inter_mem_nhds_within _ (o_open.mem_nhds xo), le_antisymm _ bot_le⟩, have μs : μ s ≠ ∞ := ((measure_mono (inter_subset_right _ _)).trans_lt μo).ne, have A : ∀ (q : ℝ≥0), 0 < q → ρ s ≤ q * μ s, { assume q hq, apply v.measure_le_mul_of_subset_lim_ratio_meas_lt hρ, assume y hy, have : v.lim_ratio_meas hρ y = 0 := hy.1, simp only [this, mem_set_of_eq, hq, ennreal.coe_pos], }, have B : tendsto (λ (q : ℝ≥0), (q : ℝ≥0∞) * μ s) (𝓝[>] (0 : ℝ≥0)) (𝓝 ((0 : ℝ≥0) * μ s)), { apply ennreal.tendsto.mul_const _ (or.inr μs), rw ennreal.tendsto_coe, exact nhds_within_le_nhds }, simp only [zero_mul, ennreal.coe_zero] at B, apply ge_of_tendsto B, filter_upwards [self_mem_nhds_within] using A, end /-- As an intermediate step to show that `μ.with_density (v.lim_ratio_meas hρ) = ρ`, we show here that `μ.with_density (v.lim_ratio_meas hρ) ≤ t^2 ρ` for any `t > 1`. -/ lemma with_density_le_mul {s : set α} (hs : measurable_set s) {t : ℝ≥0} (ht : 1 < t) : μ.with_density (v.lim_ratio_meas hρ) s ≤ t^2 * ρ s := begin /- We cut `s` into the sets where `v.lim_ratio_meas hρ = 0`, where `v.lim_ratio_meas hρ = ∞`, and where `v.lim_ratio_meas hρ ∈ [t^n, t^(n+1))` for `n : ℤ`. The first and second have measure `0`. For the latter, since `v.lim_ratio_meas hρ` fluctuates by at most `t` on this slice, we can use `measure_le_mul_of_subset_lim_ratio_meas_lt` and `mul_measure_le_of_subset_lt_lim_ratio_meas` to show that the two measures are comparable up to `t` (in fact `t^2` for technical reasons of strict inequalities). -/ have t_ne_zero' : t ≠ 0 := (zero_lt_one.trans ht).ne', have t_ne_zero : (t : ℝ≥0∞) ≠ 0, by simpa only [ennreal.coe_eq_zero, ne.def] using t_ne_zero', let ν := μ.with_density (v.lim_ratio_meas hρ), let f := v.lim_ratio_meas hρ, have f_meas : measurable f := v.lim_ratio_meas_measurable hρ, have A : ν (s ∩ f ⁻¹' ({0})) ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {0}), { apply le_trans _ (zero_le _), have M : measurable_set (s ∩ f ⁻¹' ({0})) := hs.inter (f_meas (measurable_set_singleton _)), simp only [ν, f, nonpos_iff_eq_zero, M, with_density_apply, lintegral_eq_zero_iff f_meas], apply (ae_restrict_iff' M).2, exact eventually_of_forall (λ x hx, hx.2) }, have B : ν (s ∩ f ⁻¹' ({∞})) ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {∞}), { apply le_trans (le_of_eq _) (zero_le _), apply with_density_absolutely_continuous μ _, rw ← nonpos_iff_eq_zero, exact (measure_mono (inter_subset_right _ _)).trans (v.measure_lim_ratio_meas_top hρ).le }, have C : ∀ (n : ℤ), ν (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))), { assume n, let I := Ico ((t : ℝ≥0∞)^n) (t^(n+1)), have M : measurable_set (s ∩ f ⁻¹' I) := hs.inter (f_meas measurable_set_Ico), simp only [f, M, with_density_apply, coe_nnreal_smul_apply], calc ∫⁻ x in s ∩ f⁻¹' I, f x ∂μ ≤ ∫⁻ x in s ∩ f⁻¹' I, t^(n+1) ∂μ : lintegral_mono_ae ((ae_restrict_iff' M).2 (eventually_of_forall (λ x hx, hx.2.2.le))) ... = t^(n+1) * μ (s ∩ f⁻¹' I) : by simp only [lintegral_const, measurable_set.univ, measure.restrict_apply, univ_inter] ... = t^(2 : ℤ) * (t^(n-1) * μ (s ∩ f⁻¹' I)) : begin rw [← mul_assoc, ← ennreal.zpow_add t_ne_zero ennreal.coe_ne_top], congr' 2, abel, end ... ≤ t^2 * ρ (s ∩ f ⁻¹' I) : begin apply ennreal.mul_le_mul le_rfl _, rw ← ennreal.coe_zpow (zero_lt_one.trans ht).ne', apply v.mul_measure_le_of_subset_lt_lim_ratio_meas hρ, assume x hx, apply lt_of_lt_of_le _ hx.2.1, rw [← ennreal.coe_zpow (zero_lt_one.trans ht).ne', ennreal.coe_lt_coe, sub_eq_add_neg, zpow_add₀ t_ne_zero'], conv_rhs { rw ← mul_one (t^ n) }, refine mul_lt_mul' le_rfl _ (zero_le _) (nnreal.zpow_pos t_ne_zero' _), rw zpow_neg_one, exact nnreal.inv_lt_one ht, end }, calc ν s = ν (s ∩ f⁻¹' {0}) + ν (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), ν (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) : measure_eq_measure_preimage_add_measure_tsum_Ico_zpow ν f_meas hs ht ... ≤ ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {0}) + ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), ((t : ℝ≥0∞)^2 • ρ) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) : add_le_add (add_le_add A B) (ennreal.tsum_le_tsum C) ... = ((t : ℝ≥0∞)^2 • ρ) s : (measure_eq_measure_preimage_add_measure_tsum_Ico_zpow ((t : ℝ≥0∞)^2 • ρ) f_meas hs ht).symm end /-- As an intermediate step to show that `μ.with_density (v.lim_ratio_meas hρ) = ρ`, we show here that `ρ ≤ t μ.with_density (v.lim_ratio_meas hρ)` for any `t > 1`. -/ lemma le_mul_with_density {s : set α} (hs : measurable_set s) {t : ℝ≥0} (ht : 1 < t) : ρ s ≤ t * μ.with_density (v.lim_ratio_meas hρ) s := begin /- We cut `s` into the sets where `v.lim_ratio_meas hρ = 0`, where `v.lim_ratio_meas hρ = ∞`, and where `v.lim_ratio_meas hρ ∈ [t^n, t^(n+1))` for `n : ℤ`. The first and second have measure `0`. For the latter, since `v.lim_ratio_meas hρ` fluctuates by at most `t` on this slice, we can use `measure_le_mul_of_subset_lim_ratio_meas_lt` and `mul_measure_le_of_subset_lt_lim_ratio_meas` to show that the two measures are comparable up to `t`. -/ have t_ne_zero' : t ≠ 0 := (zero_lt_one.trans ht).ne', have t_ne_zero : (t : ℝ≥0∞) ≠ 0, by simpa only [ennreal.coe_eq_zero, ne.def] using t_ne_zero', let ν := μ.with_density (v.lim_ratio_meas hρ), let f := v.lim_ratio_meas hρ, have f_meas : measurable f := v.lim_ratio_meas_measurable hρ, have A : ρ (s ∩ f ⁻¹' ({0})) ≤ (t • ν) (s ∩ f⁻¹' {0}), { refine le_trans (measure_mono (inter_subset_right _ _)) (le_trans (le_of_eq _) (zero_le _)), exact v.measure_lim_ratio_meas_zero hρ }, have B : ρ (s ∩ f ⁻¹' ({∞})) ≤ (t • ν) (s ∩ f⁻¹' {∞}), { apply le_trans (le_of_eq _) (zero_le _), apply hρ, rw ← nonpos_iff_eq_zero, exact (measure_mono (inter_subset_right _ _)).trans (v.measure_lim_ratio_meas_top hρ).le }, have C : ∀ (n : ℤ), ρ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) ≤ (t • ν) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))), { assume n, let I := Ico ((t : ℝ≥0∞)^n) (t^(n+1)), have M : measurable_set (s ∩ f ⁻¹' I) := hs.inter (f_meas measurable_set_Ico), simp only [f, M, with_density_apply, coe_nnreal_smul_apply], calc ρ (s ∩ f ⁻¹' I) ≤ t^ (n+1) * μ (s ∩ f ⁻¹' I) : begin rw ← ennreal.coe_zpow t_ne_zero', apply v.measure_le_mul_of_subset_lim_ratio_meas_lt hρ, assume x hx, apply hx.2.2.trans_le (le_of_eq _), rw ennreal.coe_zpow t_ne_zero', end ... = ∫⁻ x in s ∩ f⁻¹' I, t^(n+1) ∂μ : by simp only [lintegral_const, measurable_set.univ, measure.restrict_apply, univ_inter] ... ≤ ∫⁻ x in s ∩ f⁻¹' I, t * f x ∂μ : begin apply lintegral_mono_ae ((ae_restrict_iff' M).2 (eventually_of_forall (λ x hx, _))), rw [add_comm, ennreal.zpow_add t_ne_zero ennreal.coe_ne_top, zpow_one], exact ennreal.mul_le_mul le_rfl hx.2.1, end ... = t * ∫⁻ x in s ∩ f⁻¹' I, f x ∂μ : lintegral_const_mul _ f_meas }, calc ρ s = ρ (s ∩ f⁻¹' {0}) + ρ (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), ρ (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) : measure_eq_measure_preimage_add_measure_tsum_Ico_zpow ρ f_meas hs ht ... ≤ (t • ν) (s ∩ f⁻¹' {0}) + (t • ν) (s ∩ f⁻¹' {∞}) + ∑' (n : ℤ), (t • ν) (s ∩ f⁻¹' (Ico (t^n) (t^(n+1)))) : add_le_add (add_le_add A B) (ennreal.tsum_le_tsum C) ... = (t • ν) s : (measure_eq_measure_preimage_add_measure_tsum_Ico_zpow (t • ν) f_meas hs ht).symm end theorem with_density_lim_ratio_meas_eq : μ.with_density (v.lim_ratio_meas hρ) = ρ := begin ext1 s hs, refine le_antisymm _ _, { have : tendsto (λ (t : ℝ≥0), (t^2 * ρ s : ℝ≥0∞)) (𝓝[>] 1) (𝓝 ((1 : ℝ≥0)^2 * ρ s)), { refine ennreal.tendsto.mul _ _ tendsto_const_nhds _, { exact ennreal.tendsto.pow (ennreal.tendsto_coe.2 nhds_within_le_nhds) }, { simp only [one_pow, ennreal.coe_one, true_or, ne.def, not_false_iff, one_ne_zero] }, { simp only [one_pow, ennreal.coe_one, ne.def, or_true, ennreal.one_ne_top, not_false_iff] } }, simp only [one_pow, one_mul, ennreal.coe_one] at this, refine ge_of_tendsto this _, filter_upwards [self_mem_nhds_within] with _ ht, exact v.with_density_le_mul hρ hs ht, }, { have : tendsto (λ (t : ℝ≥0), (t : ℝ≥0∞) * μ.with_density (v.lim_ratio_meas hρ) s) (𝓝[>] 1) (𝓝 ((1 : ℝ≥0) * μ.with_density (v.lim_ratio_meas hρ) s)), { refine ennreal.tendsto.mul_const (ennreal.tendsto_coe.2 nhds_within_le_nhds) _, simp only [ennreal.coe_one, true_or, ne.def, not_false_iff, one_ne_zero], }, simp only [one_mul, ennreal.coe_one] at this, refine ge_of_tendsto this _, filter_upwards [self_mem_nhds_within] with _ ht, exact v.le_mul_with_density hρ hs ht } end /-- Weak version of the main theorem on differentiation of measures: given a Vitali family `v` for a locally finite measure `μ`, and another locally finite measure `ρ`, then for `μ`-almost every `x` the ratio `ρ a / μ a` converges, when `a` shrinks to `x` along the Vitali family, towards the Radon-Nikodym derivative of `ρ` with respect to `μ`. This version assumes that `ρ` is absolutely continuous with respect to `μ`. The general version without this superfluous assumption is `vitali_family.ae_tendsto_rn_deriv`. -/ theorem ae_tendsto_rn_deriv_of_absolutely_continuous : ∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (ρ.rn_deriv μ x)) := begin have A : (μ.with_density (v.lim_ratio_meas hρ)).rn_deriv μ =ᵐ[μ] v.lim_ratio_meas hρ := rn_deriv_with_density μ (v.lim_ratio_meas_measurable hρ), rw v.with_density_lim_ratio_meas_eq hρ at A, filter_upwards [v.ae_tendsto_lim_ratio_meas hρ, A] with _ _ h'x, rwa h'x, end end absolutely_continuous variable (ρ) /-- Main theorem on differentiation of measures: given a Vitali family `v` for a locally finite measure `μ`, and another locally finite measure `ρ`, then for `μ`-almost every `x` the ratio `ρ a / μ a` converges, when `a` shrinks to `x` along the Vitali family, towards the Radon-Nikodym derivative of `ρ` with respect to `μ`. -/ theorem ae_tendsto_rn_deriv : ∀ᵐ x ∂μ, tendsto (λ a, ρ a / μ a) (v.filter_at x) (𝓝 (ρ.rn_deriv μ x)) := begin let t := μ.with_density (ρ.rn_deriv μ), have eq_add : ρ = ρ.singular_part μ + t := have_lebesgue_decomposition_add _ _, have A : ∀ᵐ x ∂μ, tendsto (λ a, ρ.singular_part μ a / μ a) (v.filter_at x) (𝓝 0) := v.ae_eventually_measure_zero_of_singular (mutually_singular_singular_part ρ μ), have B : ∀ᵐ x ∂μ, t.rn_deriv μ x = ρ.rn_deriv μ x := rn_deriv_with_density μ (measurable_rn_deriv ρ μ), have C : ∀ᵐ x ∂μ, tendsto (λ a, t a / μ a) (v.filter_at x) (𝓝 (t.rn_deriv μ x)) := v.ae_tendsto_rn_deriv_of_absolutely_continuous (with_density_absolutely_continuous _ _), filter_upwards [A, B, C] with _ Ax Bx Cx, convert Ax.add Cx, { ext1 a, conv_lhs { rw [eq_add] }, simp only [pi.add_apply, coe_add, ennreal.add_div] }, { simp only [Bx, zero_add] } end /-- Given a measurable set `s`, then `μ (s ∩ a) / μ a` converges when `a` shrinks to a typical point `x` along a Vitali family. The limit is `1` for `x ∈ s` and `0` for `x ∉ s`. This shows that almost every point of `s` is a Lebesgue density point for `s`. A version for non-measurable sets holds, but it only gives the first conclusion, see `ae_tendsto_measure_inter_div`. -/ lemma ae_tendsto_measure_inter_div_of_measurable_set {s : set α} (hs : measurable_set s) : ∀ᵐ x ∂μ, tendsto (λ a, μ (s ∩ a) / μ a) (v.filter_at x) (𝓝 (s.indicator 1 x)) := begin haveI : is_locally_finite_measure (μ.restrict s) := is_locally_finite_measure_of_le restrict_le_self, filter_upwards [ae_tendsto_rn_deriv v (μ.restrict s), rn_deriv_restrict μ hs], assume x hx h'x, simpa only [h'x, restrict_apply' hs, inter_comm] using hx, end /-- Given an arbitrary set `s`, then `μ (s ∩ a) / μ a` converges to `1` when `a` shrinks to a typical point of `s` along a Vitali family. This shows that almost every point of `s` is a Lebesgue density point for `s`. A stronger version for measurable sets is given in `ae_tendsto_measure_inter_div_of_measurable_set`. -/ lemma ae_tendsto_measure_inter_div (s : set α) : ∀ᵐ x ∂(μ.restrict s), tendsto (λ a, μ (s ∩ a) / μ a) (v.filter_at x) (𝓝 1) := begin let t := to_measurable μ s, have A : ∀ᵐ x ∂(μ.restrict s), tendsto (λ a, μ (t ∩ a) / μ a) (v.filter_at x) (𝓝 (t.indicator 1 x)), { apply ae_mono restrict_le_self, apply ae_tendsto_measure_inter_div_of_measurable_set, exact measurable_set_to_measurable _ _ }, have B : ∀ᵐ x ∂(μ.restrict s), t.indicator 1 x = (1 : ℝ≥0∞), { refine ae_restrict_of_ae_restrict_of_subset (subset_to_measurable μ s) _, filter_upwards [ae_restrict_mem (measurable_set_to_measurable μ s)] with _ hx, simp only [hx, pi.one_apply, indicator_of_mem] }, filter_upwards [A, B] with x hx h'x, rw [h'x] at hx, apply hx.congr' _, filter_upwards [v.eventually_filter_at_measurable_set x] with _ ha, congr' 1, exact measure_to_measurable_inter_of_sigma_finite ha _, end end end vitali_family
2cc2c3c8978106a331f7ce0bfbd93c404f350194
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/analysis/special_functions/complex/log.lean
66f89c176c6ce33e729d36c2986b5f48fda8af96
[ "Apache-2.0" ]
permissive
ayush1801/mathlib
78949b9f789f488148142221606bf15c02b960d2
ce164e28f262acbb3de6281b3b03660a9f744e3c
refs/heads/master
1,692,886,907,941
1,635,270,866,000
1,635,270,866,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,571
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Abhimanyu Pallavi Sudhir, Jean Lo, Calle Sönne, Benjamin Davidson -/ import analysis.special_functions.complex.arg import analysis.special_functions.log /-! # The complex `log` function Basic properties, relationship with `exp`, and differentiability. -/ noncomputable theory namespace complex open set filter open_locale real topological_space /-- Inverse of the `exp` function. Returns values such that `(log x).im > - π` and `(log x).im ≤ π`. `log 0 = 0`-/ @[pp_nodot] noncomputable def log (x : ℂ) : ℂ := x.abs.log + arg x * I lemma log_re (x : ℂ) : x.log.re = x.abs.log := by simp [log] lemma log_im (x : ℂ) : x.log.im = x.arg := by simp [log] lemma neg_pi_lt_log_im (x : ℂ) : -π < (log x).im := by simp only [log_im, neg_pi_lt_arg] lemma log_im_le_pi (x : ℂ) : (log x).im ≤ π := by simp only [log_im, arg_le_pi] lemma exp_log {x : ℂ} (hx : x ≠ 0) : exp (log x) = x := by rw [log, exp_add_mul_I, ← of_real_sin, sin_arg, ← of_real_cos, cos_arg hx, ← of_real_exp, real.exp_log (abs_pos.2 hx), mul_add, of_real_div, of_real_div, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), ← mul_assoc, mul_div_cancel' _ (of_real_ne_zero.2 (mt abs_eq_zero.1 hx)), re_add_im] @[simp] lemma range_exp : range exp = {0}ᶜ := set.ext $ λ x, ⟨by { rintro ⟨x, rfl⟩, exact exp_ne_zero x }, λ hx, ⟨log x, exp_log hx⟩⟩ lemma exp_inj_of_neg_pi_lt_of_le_pi {x y : ℂ} (hx₁ : -π < x.im) (hx₂ : x.im ≤ π) (hy₁ : - π < y.im) (hy₂ : y.im ≤ π) (hxy : exp x = exp y) : x = y := by rw [exp_eq_exp_re_mul_sin_add_cos, exp_eq_exp_re_mul_sin_add_cos y] at hxy; exact complex.ext (real.exp_injective $ by simpa [abs_mul, abs_cos_add_sin_mul_I] using congr_arg complex.abs hxy) (by simpa [(of_real_exp _).symm, - of_real_exp, arg_real_mul _ (real.exp_pos _), arg_cos_add_sin_mul_I hx₁ hx₂, arg_cos_add_sin_mul_I hy₁ hy₂] using congr_arg arg hxy) lemma log_exp {x : ℂ} (hx₁ : -π < x.im) (hx₂: x.im ≤ π) : log (exp x) = x := exp_inj_of_neg_pi_lt_of_le_pi (by rw log_im; exact neg_pi_lt_arg _) (by rw log_im; exact arg_le_pi _) hx₁ hx₂ (by rw [exp_log (exp_ne_zero _)]) lemma of_real_log {x : ℝ} (hx : 0 ≤ x) : (x.log : ℂ) = log x := complex.ext (by rw [log_re, of_real_re, abs_of_nonneg hx]) (by rw [of_real_im, log_im, arg_of_real_of_nonneg hx]) lemma log_of_real_re (x : ℝ) : (log (x : ℂ)).re = real.log x := by simp [log_re] @[simp] lemma log_zero : log 0 = 0 := by simp [log] @[simp] lemma log_one : log 1 = 0 := by simp [log] lemma log_neg_one : log (-1) = π * I := by simp [log] lemma log_I : log I = π / 2 * I := by simp [log] lemma log_neg_I : log (-I) = -(π / 2) * I := by simp [log] lemma two_pi_I_ne_zero : (2 * π * I : ℂ) ≠ 0 := by norm_num [real.pi_ne_zero, I_ne_zero] lemma exp_eq_one_iff {x : ℂ} : exp x = 1 ↔ ∃ n : ℤ, x = n * ((2 * π) * I) := have real.exp (x.re) * real.cos (x.im) = 1 → real.cos x.im ≠ -1, from λ h₁ h₂, begin rw [h₂, mul_neg_eq_neg_mul_symm, mul_one, neg_eq_iff_neg_eq] at h₁, have := real.exp_pos x.re, rw ← h₁ at this, exact absurd this (by norm_num) end, calc exp x = 1 ↔ (exp x).re = 1 ∧ (exp x).im = 0 : by simp [complex.ext_iff] ... ↔ real.cos x.im = 1 ∧ real.sin x.im = 0 ∧ x.re = 0 : begin rw exp_eq_exp_re_mul_sin_add_cos, simp [complex.ext_iff, cos_of_real_re, sin_of_real_re, exp_of_real_re, real.exp_ne_zero], split; finish [real.sin_eq_zero_iff_cos_eq] end ... ↔ (∃ n : ℤ, ↑n * (2 * π) = x.im) ∧ (∃ n : ℤ, ↑n * π = x.im) ∧ x.re = 0 : by rw [real.sin_eq_zero_iff, real.cos_eq_one_iff] ... ↔ ∃ n : ℤ, x = n * ((2 * π) * I) : ⟨λ ⟨⟨n, hn⟩, ⟨m, hm⟩, h⟩, ⟨n, by simp [complex.ext_iff, hn.symm, h]⟩, λ ⟨n, hn⟩, ⟨⟨n, by simp [hn]⟩, ⟨2 * n, by simp [hn, mul_comm, mul_assoc, mul_left_comm]⟩, by simp [hn]⟩⟩ lemma exp_eq_exp_iff_exp_sub_eq_one {x y : ℂ} : exp x = exp y ↔ exp (x - y) = 1 := by rw [exp_sub, div_eq_one_iff_eq (exp_ne_zero _)] lemma exp_eq_exp_iff_exists_int {x y : ℂ} : exp x = exp y ↔ ∃ n : ℤ, x = y + n * ((2 * π) * I) := by simp only [exp_eq_exp_iff_exp_sub_eq_one, exp_eq_one_iff, sub_eq_iff_eq_add'] @[simp] lemma countable_preimage_exp {s : set ℂ} : countable (exp ⁻¹' s) ↔ countable s := begin refine ⟨λ hs, _, λ hs, _⟩, { refine ((hs.image exp).insert 0).mono _, rw [image_preimage_eq_inter_range, range_exp, ← diff_eq, ← union_singleton, diff_union_self], exact subset_union_left _ _ }, { rw ← bUnion_preimage_singleton, refine hs.bUnion (λ z hz, _), rcases em (∃ w, exp w = z) with ⟨w, rfl⟩|hne, { simp only [preimage, mem_singleton_iff, exp_eq_exp_iff_exists_int, set_of_exists], exact countable_Union (λ m, countable_singleton _) }, { push_neg at hne, simp [preimage, hne] } } end alias countable_preimage_exp ↔ _ set.countable.preimage_cexp /-- `complex.exp` as a `local_homeomorph` with `source = {z | -π < im z < π}` and `target = {z | 0 < re z} ∪ {z | im z ≠ 0}`. This definition is used to prove that `complex.log` is complex differentiable at all points but the negative real semi-axis. -/ def exp_local_homeomorph : local_homeomorph ℂ ℂ := local_homeomorph.of_continuous_open { to_fun := exp, inv_fun := log, source := {z : ℂ | z.im ∈ Ioo (- π) π}, target := {z : ℂ | 0 < z.re} ∪ {z : ℂ | z.im ≠ 0}, map_source' := begin rintro ⟨x, y⟩ ⟨h₁ : -π < y, h₂ : y < π⟩, refine (not_or_of_imp $ λ hz, _).symm, obtain rfl : y = 0, { rw exp_im at hz, simpa [(real.exp_pos _).ne', real.sin_eq_zero_iff_of_lt_of_lt h₁ h₂] using hz }, rw [mem_set_of_eq, ← of_real_def, exp_of_real_re], exact real.exp_pos x end, map_target' := λ z h, suffices 0 ≤ z.re ∨ z.im ≠ 0, by simpa [log_im, neg_pi_lt_arg, (arg_le_pi _).lt_iff_ne, arg_eq_pi_iff, not_and_distrib], h.imp (λ h, le_of_lt h) id, left_inv' := λ x hx, log_exp hx.1 (le_of_lt hx.2), right_inv' := λ x hx, exp_log $ by { rintro rfl, simpa [lt_irrefl] using hx } } continuous_exp.continuous_on is_open_map_exp (is_open_Ioo.preimage continuous_im) lemma has_strict_deriv_at_log {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) : has_strict_deriv_at log x⁻¹ x := have h0 : x ≠ 0, by { rintro rfl, simpa [lt_irrefl] using h }, exp_local_homeomorph.has_strict_deriv_at_symm h h0 $ by simpa [exp_log h0] using has_strict_deriv_at_exp (log x) lemma has_strict_fderiv_at_log_real {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) : has_strict_fderiv_at log (x⁻¹ • (1 : ℂ →L[ℝ] ℂ)) x := (has_strict_deriv_at_log h).complex_to_real_fderiv lemma times_cont_diff_at_log {x : ℂ} (h : 0 < x.re ∨ x.im ≠ 0) {n : with_top ℕ} : times_cont_diff_at ℂ n log x := exp_local_homeomorph.times_cont_diff_at_symm_deriv (exp_ne_zero $ log x) h (has_deriv_at_exp _) times_cont_diff_exp.times_cont_diff_at lemma tendsto_log_nhds_within_im_neg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : ℂ | z.im < 0}] z) (𝓝 $ real.log (abs z) - π * I) := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add (((continuous_of_real.tendsto _).comp $ tendsto_arg_nhds_within_im_neg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { simp [sub_eq_add_neg] }, { lift z to ℝ using him, simpa using hre.ne } end lemma continuous_within_at_log_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : continuous_within_at log {z : ℂ | 0 ≤ z.im} z := begin have := (continuous_of_real.continuous_at.comp_continuous_within_at (continuous_abs.continuous_within_at.log _)).tendsto.add ((continuous_of_real.continuous_at.comp_continuous_within_at $ continuous_within_at_arg_of_re_neg_of_im_zero hre him).mul tendsto_const_nhds), convert this, { lift z to ℝ using him, simpa using hre.ne } end lemma tendsto_log_nhds_within_im_nonneg_of_re_neg_of_im_zero {z : ℂ} (hre : z.re < 0) (him : z.im = 0) : tendsto log (𝓝[{z : ℂ | 0 ≤ z.im}] z) (𝓝 $ real.log (abs z) + π * I) := by simpa only [log, arg_eq_pi_iff.2 ⟨hre, him⟩] using (continuous_within_at_log_of_re_neg_of_im_zero hre him).tendsto end complex section log_deriv open complex filter open_locale topological_space variables {α : Type*} lemma filter.tendsto.clog {l : filter α} {f : α → ℂ} {x : ℂ} (h : tendsto f l (𝓝 x)) (hx : 0 < x.re ∨ x.im ≠ 0) : tendsto (λ t, log (f t)) l (𝓝 $ log x) := (has_strict_deriv_at_log hx).continuous_at.tendsto.comp h variables [topological_space α] lemma continuous_at.clog {f : α → ℂ} {x : α} (h₁ : continuous_at f x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_at (λ t, log (f t)) x := h₁.clog h₂ lemma continuous_within_at.clog {f : α → ℂ} {s : set α} {x : α} (h₁ : continuous_within_at f s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_within_at (λ t, log (f t)) s x := h₁.clog h₂ lemma continuous_on.clog {f : α → ℂ} {s : set α} (h₁ : continuous_on f s) (h₂ : ∀ x ∈ s, 0 < (f x).re ∨ (f x).im ≠ 0) : continuous_on (λ t, log (f t)) s := λ x hx, (h₁ x hx).clog (h₂ x hx) lemma continuous.clog {f : α → ℂ} (h₁ : continuous f) (h₂ : ∀ x, 0 < (f x).re ∨ (f x).im ≠ 0) : continuous (λ t, log (f t)) := continuous_iff_continuous_at.2 $ λ x, h₁.continuous_at.clog (h₂ x) variables {E : Type*} [normed_group E] [normed_space ℂ E] lemma has_strict_fderiv_at.clog {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E} (h₁ : has_strict_fderiv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_strict_fderiv_at (λ t, log (f t)) ((f x)⁻¹ • f') x := (has_strict_deriv_at_log h₂).comp_has_strict_fderiv_at x h₁ lemma has_strict_deriv_at.clog {f : ℂ → ℂ} {f' x : ℂ} (h₁ : has_strict_deriv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_strict_deriv_at (λ t, log (f t)) (f' / f x) x := by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log h₂).comp x h₁ } lemma has_strict_deriv_at.clog_real {f : ℝ → ℂ} {x : ℝ} {f' : ℂ} (h₁ : has_strict_deriv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_strict_deriv_at (λ t, log (f t)) (f' / f x) x := by simpa only [div_eq_inv_mul] using (has_strict_fderiv_at_log_real h₂).comp_has_strict_deriv_at x h₁ lemma has_fderiv_at.clog {f : E → ℂ} {f' : E →L[ℂ] ℂ} {x : E} (h₁ : has_fderiv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_fderiv_at (λ t, log (f t)) ((f x)⁻¹ • f') x := (has_strict_deriv_at_log h₂).has_deriv_at.comp_has_fderiv_at x h₁ lemma has_deriv_at.clog {f : ℂ → ℂ} {f' x : ℂ} (h₁ : has_deriv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_deriv_at (λ t, log (f t)) (f' / f x) x := by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log h₂).has_deriv_at.comp x h₁ } lemma has_deriv_at.clog_real {f : ℝ → ℂ} {x : ℝ} {f' : ℂ} (h₁ : has_deriv_at f f' x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_deriv_at (λ t, log (f t)) (f' / f x) x := by simpa only [div_eq_inv_mul] using (has_strict_fderiv_at_log_real h₂).has_fderiv_at.comp_has_deriv_at x h₁ lemma differentiable_at.clog {f : E → ℂ} {x : E} (h₁ : differentiable_at ℂ f x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : differentiable_at ℂ (λ t, log (f t)) x := (h₁.has_fderiv_at.clog h₂).differentiable_at lemma has_fderiv_within_at.clog {f : E → ℂ} {f' : E →L[ℂ] ℂ} {s : set E} {x : E} (h₁ : has_fderiv_within_at f f' s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_fderiv_within_at (λ t, log (f t)) ((f x)⁻¹ • f') s x := (has_strict_deriv_at_log h₂).has_deriv_at.comp_has_fderiv_within_at x h₁ lemma has_deriv_within_at.clog {f : ℂ → ℂ} {f' x : ℂ} {s : set ℂ} (h₁ : has_deriv_within_at f f' s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_deriv_within_at (λ t, log (f t)) (f' / f x) s x := by { rw div_eq_inv_mul, exact (has_strict_deriv_at_log h₂).has_deriv_at.comp_has_deriv_within_at x h₁ } lemma has_deriv_within_at.clog_real {f : ℝ → ℂ} {s : set ℝ} {x : ℝ} {f' : ℂ} (h₁ : has_deriv_within_at f f' s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : has_deriv_within_at (λ t, log (f t)) (f' / f x) s x := by simpa only [div_eq_inv_mul] using (has_strict_fderiv_at_log_real h₂).has_fderiv_at.comp_has_deriv_within_at x h₁ lemma differentiable_within_at.clog {f : E → ℂ} {s : set E} {x : E} (h₁ : differentiable_within_at ℂ f s x) (h₂ : 0 < (f x).re ∨ (f x).im ≠ 0) : differentiable_within_at ℂ (λ t, log (f t)) s x := (h₁.has_fderiv_within_at.clog h₂).differentiable_within_at lemma differentiable_on.clog {f : E → ℂ} {s : set E} (h₁ : differentiable_on ℂ f s) (h₂ : ∀ x ∈ s, 0 < (f x).re ∨ (f x).im ≠ 0) : differentiable_on ℂ (λ t, log (f t)) s := λ x hx, (h₁ x hx).clog (h₂ x hx) lemma differentiable.clog {f : E → ℂ} (h₁ : differentiable ℂ f) (h₂ : ∀ x, 0 < (f x).re ∨ (f x).im ≠ 0) : differentiable ℂ (λ t, log (f t)) := λ x, (h₁ x).clog (h₂ x) end log_deriv
4504d3f2401762e2b076eaf0d606a0585ab13432
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/archive/imo/imo1981_q3.lean
2e6a97b1f70649597a2f08b612672f7732c67f7d
[ "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,804
lean
/- Copyright (c) 2020 Kevin Lacker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Lacker -/ import data.nat.fib import tactic.linarith /-! # IMO 1981 Q3 Determine the maximum value of `m ^ 2 + n ^ 2`, where `m` and `n` are integers in `{1, 2, ..., 1981}` and `(n ^ 2 - m * n - m ^ 2) ^ 2 = 1`. The trick to this problem is that `m` and `n` have to be consecutive Fibonacci numbers, because you can reduce any solution to a smaller one using the Fibonacci recurrence. -/ /- First, define the problem in terms of finding the maximum of a set. We first generalize the problem to `{1, 2, ..., N}` and specialize to `N = 1981` at the very end. -/ open int nat set section variable (N : ℕ) -- N = 1981 @[mk_iff] structure problem_predicate (m n : ℤ) : Prop := (m_range : m ∈ Ioc 0 (N : ℤ)) (n_range : n ∈ Ioc 0 (N : ℤ)) (eq_one : (n ^ 2 - m * n - m ^ 2) ^ 2 = 1) def specified_set : set ℤ := {k : ℤ | ∃ m : ℤ, ∃ n : ℤ, k = m ^ 2 + n ^ 2 ∧ problem_predicate N m n} /- We want to reduce every solution to a smaller solution. Specifically, we show that when `(m, n)` is a solution, `(n - m, m)` is also a solution, except for the base case of `(1, 1)`. -/ namespace problem_predicate variable {N} lemma m_le_n {m n : ℤ} (h1 : problem_predicate N m n) : m ≤ n := begin by_contradiction h2, have h3 : 1 = (n * (n - m) - m ^ 2) ^ 2, { calc 1 = (n ^ 2 - m * n - m ^ 2) ^ 2 : h1.eq_one.symm ... = (n * (n - m) - m ^ 2) ^ 2 : by ring }, have h4 : n * (n - m) - m ^ 2 < -1, by nlinarith [h1.n_range.left], have h5 : 1 < (n * (n - m) - m ^ 2) ^ 2, by nlinarith, exact h5.ne h3 end lemma eq_imp_1 {n : ℤ} (h1 : problem_predicate N n n) : n = 1 := begin have : n * (n * (n * n)) = 1, { calc _ = (n ^ 2 - n * n - n ^ 2) ^ 2 : by simp [sq, mul_assoc] ... = 1 : h1.eq_one }, exact eq_one_of_mul_eq_one_right h1.m_range.left.le this, end lemma reduction {m n : ℤ} (h1 : problem_predicate N m n) (h2 : 1 < n) : problem_predicate N (n - m) m := begin obtain (rfl : m = n) | (h3 : m < n) := h1.m_le_n.eq_or_lt, { have h4 : m = 1, from h1.eq_imp_1, exact absurd h4.symm h2.ne }, refine_struct { n_range := h1.m_range, .. }, -- m_range: { have h5 : 0 < n - m, from sub_pos.mpr h3, have h6 : n - m < N, { calc _ < n : sub_lt_self n h1.m_range.left ... ≤ N : h1.n_range.right }, exact ⟨h5, h6.le⟩ }, -- eq_one: { calc _ = (n ^ 2 - m * n - m ^ 2) ^ 2 : by ring ... = 1 : h1.eq_one }, end end problem_predicate /- It will be convenient to have the lemmas above in their natural number form. Most of these can be proved with the `norm_cast` family of tactics. -/ def nat_predicate (m n : ℕ) : Prop := problem_predicate N ↑m ↑n namespace nat_predicate variable {N} lemma m_le_n {m n : ℕ} (h1 : nat_predicate N m n) : m ≤ n := by exact_mod_cast h1.m_le_n lemma eq_imp_1 {n : ℕ} (h1 : nat_predicate N n n) : n = 1 := by exact_mod_cast h1.eq_imp_1 lemma reduction {m n : ℕ} (h1 : nat_predicate N m n) (h2 : 1 < n) : nat_predicate N (n - m) m := have m ≤ n, from h1.m_le_n, by exact_mod_cast h1.reduction (by exact_mod_cast h2) lemma n_pos {m n : ℕ} (h1 : nat_predicate N m n) : 0 < n := by exact_mod_cast h1.n_range.left lemma m_pos {m n : ℕ} (h1 : nat_predicate N m n) : 0 < m := by exact_mod_cast h1.m_range.left lemma n_le_N {m n : ℕ} (h1 : nat_predicate N m n) : n ≤ N := by exact_mod_cast h1.n_range.right /- Now we can use induction to show that solutions must be Fibonacci numbers. -/ lemma imp_fib {n : ℕ} : ∀ m : ℕ, nat_predicate N m n → ∃ k : ℕ, m = fib k ∧ n = fib (k + 1) := begin apply nat.strong_induction_on n _, intros n h1 m h2, have h3 : m ≤ n, from h2.m_le_n, obtain (rfl : 1 = n) | (h4 : 1 < n) := (succ_le_iff.mpr h2.n_pos).eq_or_lt, { use 1, have h5 : 1 ≤ m, from succ_le_iff.mpr h2.m_pos, simpa [fib_one, fib_two] using (h3.antisymm h5 : m = 1) }, { obtain (rfl : m = n) | (h6 : m < n) := h3.eq_or_lt, { exact absurd h2.eq_imp_1 (ne_of_gt h4) }, { have h7 : nat_predicate N (n - m) m, from h2.reduction h4, obtain ⟨k : ℕ, hnm : n - m = fib k, rfl : m = fib (k+1)⟩ := h1 m h6 (n - m) h7, use [k + 1, rfl], rw [fib_succ_succ, ← hnm, nat.sub_add_cancel h3] } } end end nat_predicate /- Next, we prove that if `N < fib K + fib (K+1)`, then the largest `m` and `n` satisfying `nat_predicate m n N` are `fib K` and `fib (K+1)`, respectively. -/ variables {K : ℕ} (HK : N < fib K + fib (K+1)) {N} include HK lemma m_n_bounds {m n : ℕ} (h1 : nat_predicate N m n) : m ≤ fib K ∧ n ≤ fib (K+1) := begin obtain ⟨k : ℕ, hm : m = fib k, hn : n = fib (k+1)⟩ := h1.imp_fib m, by_cases h2 : k < K + 1, { have h3 : k ≤ K, from lt_succ_iff.mp h2, split, { calc m = fib k : hm ... ≤ fib K : fib_mono h3, }, { have h6 : k + 1 ≤ K + 1, from succ_le_succ h3, calc n = fib (k+1) : hn ... ≤ fib (K+1) : fib_mono h6 } }, { have h7 : N < n, { have h8 : K + 2 ≤ k + 1, from succ_le_succ (not_lt.mp h2), calc N < fib (K+2) : HK ... ≤ fib (k+1) : fib_mono h8 ... = n : hn.symm, }, have h9 : n ≤ N, from h1.n_le_N, exact absurd h7 h9.not_lt } end /- We spell out the consequences of this result for `specified_set N` here. -/ variables {M : ℕ} (HM : M = (fib K) ^ 2 + (fib (K+1)) ^ 2) include HM lemma k_bound {m n : ℤ} (h1 : problem_predicate N m n) : m ^ 2 + n ^ 2 ≤ M := begin have h2 : 0 ≤ m, from h1.m_range.left.le, have h3 : 0 ≤ n, from h1.n_range.left.le, rw [← nat_abs_of_nonneg h2, ← nat_abs_of_nonneg h3] at h1, clear h2 h3, obtain ⟨h4 : m.nat_abs ≤ fib K, h5 : n.nat_abs ≤ fib (K+1)⟩ := m_n_bounds HK h1, have h6 : m ^ 2 ≤ (fib K) ^ 2, from nat_abs_le_iff_sq_le.mp h4, have h7 : n ^ 2 ≤ (fib (K+1)) ^ 2, from nat_abs_le_iff_sq_le.mp h5, linarith end lemma solution_bound : ∀ {k : ℤ}, k ∈ specified_set N → k ≤ M | _ ⟨_, _, rfl, h⟩ := k_bound HK HM h theorem solution_greatest (H : problem_predicate N (fib K) (fib (K + 1))) : is_greatest (specified_set N) M := ⟨⟨fib K, fib (K+1), by simp [HM], H⟩, λ k h, solution_bound HK HM h⟩ end /- Now we just have to demonstrate that 987 and 1597 are in fact the largest Fibonacci numbers in this range, and thus provide the maximum of `specified_set`. -/ theorem imo1981_q3 : is_greatest (specified_set 1981) 3524578 := begin have := λ h, @solution_greatest 1981 16 h 3524578, simp only [show fib (16:ℕ) = 987 ∧ fib (16+1:ℕ) = 1597, by norm_num [fib_succ_succ]] at this, apply_mod_cast this; norm_num [problem_predicate_iff], end
1fa8de80d5621398f788e142e6c83c696b443d02
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebraic_topology/dold_kan/equivalence.lean
dc025a7938b2b5dfbf0e3602bf28d2bcf13e5d5c
[ "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
8,887
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebraic_topology.dold_kan.equivalence_pseudoabelian import algebraic_topology.dold_kan.normalized /-! # The Dold-Kan correspondence The Dold-Kan correspondence states that for any abelian category `A`, there is an equivalence between the category of simplicial objects in `A` and the category of chain complexes in `A` (with degrees indexed by `ℕ` and the homological convention that the degree is decreased by the differentials). In this file, we finish the construction of this equivalence by providing `category_theory.abelian.dold_kan.equivalence` which is of type `simplicial_object A ≌ chain_complex A ℕ` for any abelian category `A`. The functor `simplicial_object A ⥤ chain_complex A ℕ` of this equivalence is definitionally equal to `normalized_Moore_complex A`. ## Overall strategy of the proof of the correspondence Before starting the implementation of the proof in Lean, the author noticed that the Dold-Kan equivalence not only applies to abelian categories, but should also hold generally for any pseudoabelian category `C` (i.e. a category with instances `[preadditive C]` `[has_finite_coproducts C]` and `[is_idempotent_complete C]`): this is `category_theory.idempotents.dold_kan.equivalence`. When the alternating face map complex `K[X]` of a simplicial object `X` in an abelian is studied, it is shown that it decomposes as a direct sum of the normalized subcomplex and of the degenerate subcomplex. The crucial observation is that in this decomposition, the projection on the normalized subcomplex can be defined in each degree using simplicial operators. Then, the definition of this projection `P_infty : K[X] ⟶ K[X]` can be carried out for any `X : simplicial_object C` when `C` is a preadditive category. The construction of the endomorphism `P_infty` is done in the files `homotopies.lean`, `faces.lean`, `projections.lean` and `p_infty.lean`. Eventually, as we would also like to show that the inclusion of the normalized Moore complex is a homotopy equivalence (cf. file `homotopy_equivalence.lean`), this projection `P_infty` needs to be homotopic to the identity. In our construction, we get this for free because `P_infty` is obtained by altering the identity endomorphism by null homotopic maps. More details about this aspect of the proof are in the file `homotopies.lean`. When the alternating face map complex `K[X]` is equipped with the idempotent endomorphism `P_infty`, it becomes an object in `karoubi (chain_complex C ℕ)` which is the idempotent completion of the category `chain_complex C ℕ`. In `functor_n.lean`, we obtain this functor `N₁ : simplicial_object C ⥤ karoubi (chain_complex C ℕ)`, which is formally extended as `N₂ : karoubi (simplicial_object C) ⥤ karoubi (chain_complex C ℕ)`. (Here, some functors have an index which is the number of occurrences of `karoubi` at the source or the target.) In `functor_gamma.lean`, assuming that the category `C` is additive, we define the functor in the other direction `Γ₂ : karoubi (chain_complex C ℕ) ⥤ karoubi (simplicial_object C)` as the formal extension of a functor `Γ₀ : chain_complex C ℕ ⥤ simplicial_object C` which is defined similarly as in *Simplicial Homotopy Theory* by Goerss-Jardine. In `degeneracies.lean`, we show that `P_infty` vanishes on the image of degeneracy operators, which is one of the key properties that makes it possible to contruct the isomorphism `N₂Γ₂ : Γ₂ ⋙ N₂ ≅ 𝟭 (karoubi (chain_complex C ℕ))`. The rest of the proof follows the strategy in the [original paper by Dold][dold1958]. We show that the functor `N₂` reflects isomorphisms in `n_reflects_iso.lean`: this relies on a decomposition of the identity of `X _[n]` using `P_infty.f n` and degeneracies obtained in `decomposition.lean`. Then, in `n_comp_gamma.lean`, we construct a natural transformation `Γ₂N₂.trans : N₂ ⋙ Γ₂ ⟶ 𝟭 (karoubi (simplicial_object C))`. It is shown that it is an isomorphism using the fact that `N₂` reflects isomorphisms, and because we can show that the composition `N₂ ⟶ N₂ ⋙ Γ₂ ⋙ N₂ ⟶ N₂` is the identity (see `identity_N₂`). The fact that `N₂` is defined as a formal direct factor makes the proof easier because we only have to compare endomorphisms of an alternating face map complex `K[X]` and we do not have to worry with inclusions of kernel subobjects. In `equivalence_additive.lean`, we obtain the equivalence `equivalence : karoubi (simplicial_object C) ≌ karoubi (chain_complex C ℕ)`. It is in the namespace `category_theory.preadditive.dold_kan`. The functors in this equivalence are named `N` and `Γ`: by definition, they are `N₂` and `Γ₂`. In `equivalence_pseudoabelian.lean`, assuming `C` is idempotent complete, we obtain `equivalence : simplicial_object C ≌ chain_complex C ℕ` in the namespace `category_theory.idempotents.dold_kan`. This could be roughly obtained by composing the previous equivalence with the equivalences `simplicial_object C ≌ karoubi (simplicial_object C)` and `karoubi (chain_complex C ℕ) ≌ chain_complex C ℕ`. Instead, we polish this construction in `compatibility.lean` by ensuring good definitional properties of the equivalence (e.g. the inverse functor is definitionallly equal to `Γ₀' : chain_complex C ℕ ⥤ simplicial_object C`) and showing compatibilities for the unit and counit isomorphisms. In this file `equivalence.lean`, assuming the category `A` is abelian, we obtain `equivalence : simplicial_object A ≌ chain_complex A ℕ` in the namespace `category_theory.abelian.dold_kan`. This is obtained by replacing the functor `category_theory.idempotents.dold_kan.N` of the equivalence in the pseudoabelian case with the isomorphic functor `normalized_Moore_complex A` thanks to the isomorphism obtained in `normalized.lean`. TODO: Show functoriality properties of the three equivalences above. More precisely, for example in the case of abelian categories `A` and `B`, if `F : A ⥤ B` is an additive functor, we can show that the functors `N` for `A` and `B` are compatible with the functors `simplicial_object A ⥤ simplicial_object B` and `chain_complex A ℕ ⥤ chain_complex B ℕ` induced by `F`. (Note that this does not require that `F` is an exact functor!) TODO: Introduce the degenerate subcomplex `D[X]` which is generated by degenerate simplices, show that the projector `P_infty` corresponds to a decomposition `K[X] ≅ N[X] ⊞ D[X]`. TODO: dualise all of this as `cosimplicial_object A ⥤ cochain_complex A ℕ`. (It is unclear what is the best way to do this. The exact design may be decided when it is needed.) ## References * [Albrecht Dold, Homology of Symmetric Products and Other Functors of Complexes][dold1958] * [Paul G. Goerss, John F. Jardine, Simplicial Homotopy Theory][goerss-jardine-2009] -/ noncomputable theory open category_theory open category_theory.category open category_theory.idempotents variables {A : Type*} [category A] [abelian A] namespace category_theory namespace abelian namespace dold_kan open algebraic_topology.dold_kan /-- The functor `N` for the equivalence is `normalized_Moore_complex A` -/ def N : simplicial_object A ⥤ chain_complex A ℕ := algebraic_topology.normalized_Moore_complex A /-- The functor `Γ` for the equivalence is the same as in the pseudoabelian case. -/ def Γ : chain_complex A ℕ ⥤ simplicial_object A := idempotents.dold_kan.Γ /-- The comparison isomorphism between `normalized_Moore_complex A` and the functor `idempotents.dold_kan.N` from the pseudoabelian case -/ @[simps] def comparison_N : (N : simplicial_object A ⥤ _) ≅ idempotents.dold_kan.N := calc N ≅ N ⋙ 𝟭 _ : functor.left_unitor N ... ≅ N ⋙ ((to_karoubi_equivalence _).functor ⋙ (to_karoubi_equivalence _).inverse) : iso_whisker_left _ (to_karoubi_equivalence _).unit_iso ... ≅ (N ⋙ (to_karoubi_equivalence _).functor) ⋙ (to_karoubi_equivalence _).inverse : iso.refl _ ... ≅ N₁ ⋙ (to_karoubi_equivalence _).inverse : iso_whisker_right (N₁_iso_normalized_Moore_complex_comp_to_karoubi A).symm _ ... ≅ idempotents.dold_kan.N : by refl /-- The Dold-Kan equivalence for abelian categories -/ @[simps functor] def equivalence : simplicial_object A ≌ chain_complex A ℕ := begin let F : simplicial_object A ⥤ _ := idempotents.dold_kan.N, let hF : is_equivalence F := is_equivalence.of_equivalence idempotents.dold_kan.equivalence, letI : is_equivalence (N : simplicial_object A ⥤ _ ) := is_equivalence.of_iso comparison_N.symm hF, exact N.as_equivalence, end lemma equivalence_inverse : (equivalence : simplicial_object A ≌ _).inverse = Γ := rfl end dold_kan end abelian end category_theory
4469014184274a19a09de460485db87448a054c1
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/derive_fintype.lean
3e53867d4f59789b739af369b9a7c92151a82a1b
[]
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
9,242
lean
/- Copyright (c) 2020 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.logic.basic import Mathlib.data.fintype.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Derive handler for `fintype` instances This file introduces a derive handler to automatically generate `fintype` instances for structures and inductives. ## Implementation notes To construct a fintype instance, we need 3 things: 1. A list `l` of elements 2. A proof that `l` has no duplicates 3. A proof that every element in the type is in `l` Now fintype is defined as a finset which enumerates all elements, so steps (1) and (2) are bundled together. It is possible to use finset operations that remove duplicates to avoid the need to prove (2), but this adds unnecessary functions to the constructed term, which makes it more expensive to compute the list, and it also adds a dependence on decidable equality for the type, which we want to avoid. Because we will rely on fintype instances for constructor arguments, we can't actually build a list directly, so (1) and (2) are necessarily somewhat intertwined. The inductive types we will be proving instances for look something like this: ``` @[derive fintype] inductive foo | zero : foo | one : bool → foo | two : ∀ x : fin 3, bar x → foo ``` The list of elements that we generate is ``` {foo.zero} ∪ (finset.univ : bool).map (λ b, finset.one b) ∪ (finset.univ : Σ' x : fin 3, bar x).map (λ ⟨x, y⟩, finset.two x y) ``` except that instead of `∪`, that is `finset.union`, we use `finset.disj_union` which doesn't require any deduplication, but does require a proof that the two parts of the union are disjoint. We use `finset.cons` to append singletons like `foo.zero`. The proofs of disjointness would be somewhat expensive since there are quadratically many of them, so instead we use a "discriminant" function. Essentially, we define ``` def foo.enum : foo → ℕ | foo.zero := 0 | (foo.one _) := 1 | (foo.two _ _) := 2 ``` and now the existence of this function implies that foo.zero is not foo.two and so on because they map to different natural numbers. We can prove that sets of natural numbers are mutually disjoint more easily because they have a linear order: `0 < 1 < 2` so `0 ≠ 2`. To package this argument up, we define `finset_above foo foo.enum n` to be a finset `s` together with a proof that all elements `a ∈ s` have `n ≤ enum a`. Now we only have to prove that `enum foo.zero = 0`, `enum (foo.one _) = 1`, etc. (linearly many proofs, all `rfl`) in order to prove that all variants are mutually distinct. We mirror the `finset.cons` and `finset.disj_union` functions into `finset_above.cons` and `finset_above.union`, and this forms the main part of the finset construction. This only handles distinguishing variants of a finset. Now we must enumerate the elements of a variant, for example `{foo.one ff, foo.one tt}`, while at the same time proving that all these elements have discriminant `1` in this case. To do that, we use the `finset_in` type, which is a finset satisfying a property `P`, here `λ a, foo.enum a = 1`. We could use `finset.bind` many times to construct the finset but it turns out to be somewhat complicated to get good side goals for a naturally nodup version of `finset.bind` in the same way as we did with `finset.cons` and `finset.union`. Instead, we tuple up all arguments into one type, leveraging the `fintype` instance on `psigma`, and then define a map from this type to the inductive type that untuples them and applies the constructor. The injectivity property of the constructor ensures that this function is injective, so we can use `finset.map` to apply it. This is the content of the constructor `finset_in.mk`. That completes the proofs of (1) and (2). To prove (3), we perform one case analysis over the inductive type, proving theorems like ``` foo.one a ∈ {foo.zero} ∪ (finset.univ : bool).map (λ b, finset.one b) ∪ (finset.univ : Σ' x : fin 3, bar x).map (λ ⟨x, y⟩, finset.two x y) ``` by seeking to the relevant disjunct and then supplying the constructor arguments. This part of the proof is quadratic, but quite simple. (We could do it in `O(n log n)` if we used a balanced tree for the unions.) The tactics perform the following parts of this proof scheme: * `mk_sigma` constructs the type `Γ` in `finset_in.mk` * `mk_sigma_elim` constructs the function `f` in `finset_in.mk` * `mk_sigma_elim_inj` proves that `f` is injective * `mk_sigma_elim_eq` proves that `∀ a, enum (f a) = k` * `mk_finset` constructs the finset `S = {foo.zero} ∪ ...` by recursion on the variants * `mk_finset_total` constructs the proof `|- foo.zero ∈ S; |- foo.one a ∈ S; |- foo.two a b ∈ S` by recursion on the subgoals coming out of the initial `cases` * `mk_fintype_instance` puts it all together to produce a proof of `fintype foo`. The construction of `foo.enum` is also done in this function. -/ namespace derive_fintype /-- A step in the construction of `finset.univ` for a finite inductive type. We will set `enum` to the discriminant of the inductive type, so a `finset_above` represents a finset that enumerates all elements in a tail of the constructor list. -/ def finset_above (α : Type u_1) (enum : α → ℕ) (n : ℕ) := Subtype fun (s : finset α) => ∀ (x : α), x ∈ s → n ≤ enum x /-- Construct a fintype instance from a completed `finset_above`. -/ def mk_fintype {α : Type u_1} (enum : α → ℕ) (s : finset_above α enum 0) (H : ∀ (x : α), x ∈ subtype.val s) : fintype α := fintype.mk (subtype.val s) H /-- This is the case for a simple variant (no arguments) in an inductive type. -/ def finset_above.cons {α : Type u_1} {enum : α → ℕ} (n : ℕ) (a : α) (h : enum a = n) (s : finset_above α enum (n + 1)) : finset_above α enum n := { val := finset.cons a (subtype.val s) sorry, property := sorry } theorem finset_above.mem_cons_self {α : Type u_1} {enum : α → ℕ} {n : ℕ} {a : α} {h : enum a = n} {s : finset_above α enum (n + 1)} : a ∈ subtype.val (finset_above.cons n a h s) := multiset.mem_cons_self a (finset.val (subtype.val s)) theorem finset_above.mem_cons_of_mem {α : Type u_1} {enum : α → ℕ} {n : ℕ} {a : α} {h : enum a = n} {s : finset_above α enum (n + 1)} {b : α} : b ∈ subtype.val s → b ∈ subtype.val (finset_above.cons n a h s) := multiset.mem_cons_of_mem /-- The base case is when we run out of variants; we just put an empty finset at the end. -/ def finset_above.nil {α : Type u_1} {enum : α → ℕ} (n : ℕ) : finset_above α enum n := { val := ∅, property := sorry } protected instance finset_above.inhabited (α : Type u_1) (enum : α → ℕ) (n : ℕ) : Inhabited (finset_above α enum n) := { default := finset_above.nil n } /-- This is a finset covering a nontrivial variant (with one or more constructor arguments). The property `P` here is `λ a, enum a = n` where `n` is the discriminant for the current variant. -/ def finset_in {α : Type u_1} (P : α → Prop) := Subtype fun (s : finset α) => ∀ (x : α), x ∈ s → P x /-- To construct the finset, we use an injective map from the type `Γ`, which will be the sigma over all constructor arguments. We use sigma instances and existing fintype instances to prove that `Γ` is a fintype, and construct the function `f` that maps `⟨a, b, c, ...⟩` to `C_n a b c ...` where `C_n` is the nth constructor, and `mem` asserts `enum (C_n a b c ...) = n`. -/ def finset_in.mk {α : Type u_1} {P : α → Prop} (Γ : Type u_2) [fintype Γ] (f : Γ → α) (inj : function.injective f) (mem : ∀ (x : Γ), P (f x)) : finset_in P := { val := finset.map (function.embedding.mk f inj) finset.univ, property := sorry } theorem finset_in.mem_mk {α : Type u_1} {P : α → Prop} {Γ : Type u_2} {s : fintype Γ} {f : Γ → α} {inj : function.injective f} {mem : ∀ (x : Γ), P (f x)} {a : α} (b : Γ) (H : f b = a) : a ∈ subtype.val (finset_in.mk Γ f inj mem) := iff.mpr finset.mem_map (Exists.intro b (Exists.intro (finset.mem_univ b) H)) /-- For nontrivial variants, we split the constructor list into a `finset_in` component for the current constructor and a `finset_above` for the rest. -/ def finset_above.union {α : Type u_1} {enum : α → ℕ} (n : ℕ) (s : finset_in fun (a : α) => enum a = n) (t : finset_above α enum (n + 1)) : finset_above α enum n := { val := finset.disj_union (subtype.val s) (subtype.val t) sorry, property := sorry } theorem finset_above.mem_union_left {α : Type u_1} {enum : α → ℕ} {n : ℕ} {s : finset_in fun (a : α) => enum a = n} {t : finset_above α enum (n + 1)} {a : α} (H : a ∈ subtype.val s) : a ∈ subtype.val (finset_above.union n s t) := iff.mpr multiset.mem_add (Or.inl H) theorem finset_above.mem_union_right {α : Type u_1} {enum : α → ℕ} {n : ℕ} {s : finset_in fun (a : α) => enum a = n} {t : finset_above α enum (n + 1)} {a : α} (H : a ∈ subtype.val t) : a ∈ subtype.val (finset_above.union n s t) := iff.mpr multiset.mem_add (Or.inr H)
264d687b9887cf90f074ef079210248f6790b037
592ee40978ac7604005a4e0d35bbc4b467389241
/Library/generated/mathscheme-lean/AdditiveMagma.lean
4f50e6b304e258fdabc540cafdf7f934a2554be7
[]
no_license
ysharoda/Deriving-Definitions
3e149e6641fae440badd35ac110a0bd705a49ad2
dfecb27572022de3d4aa702cae8db19957523a59
refs/heads/master
1,679,127,857,700
1,615,939,007,000
1,615,939,007,000
229,785,731
4
0
null
null
null
null
UTF-8
Lean
false
false
5,935
lean
import init.data.nat.basic import init.data.fin.basic import data.vector import .Prelude open Staged open nat open fin open vector section AdditiveMagma structure AdditiveMagma (A : Type) : Type := (plus : (A → (A → A))) open AdditiveMagma structure Sig (AS : Type) : Type := (plusS : (AS → (AS → AS))) structure Product (A : Type) : Type := (plusP : ((Prod A A) → ((Prod A A) → (Prod A A)))) structure Hom {A1 : Type} {A2 : Type} (Ad1 : (AdditiveMagma A1)) (Ad2 : (AdditiveMagma A2)) : Type := (hom : (A1 → A2)) (pres_plus : (∀ {x1 x2 : A1} , (hom ((plus Ad1) x1 x2)) = ((plus Ad2) (hom x1) (hom x2)))) structure RelInterp {A1 : Type} {A2 : Type} (Ad1 : (AdditiveMagma A1)) (Ad2 : (AdditiveMagma A2)) : Type 1 := (interp : (A1 → (A2 → Type))) (interp_plus : (∀ {x1 x2 : A1} {y1 y2 : A2} , ((interp x1 y1) → ((interp x2 y2) → (interp ((plus Ad1) x1 x2) ((plus Ad2) y1 y2)))))) inductive AdditiveMagmaTerm : Type | plusL : (AdditiveMagmaTerm → (AdditiveMagmaTerm → AdditiveMagmaTerm)) open AdditiveMagmaTerm inductive ClAdditiveMagmaTerm (A : Type) : Type | sing : (A → ClAdditiveMagmaTerm) | plusCl : (ClAdditiveMagmaTerm → (ClAdditiveMagmaTerm → ClAdditiveMagmaTerm)) open ClAdditiveMagmaTerm inductive OpAdditiveMagmaTerm (n : ℕ) : Type | v : ((fin n) → OpAdditiveMagmaTerm) | plusOL : (OpAdditiveMagmaTerm → (OpAdditiveMagmaTerm → OpAdditiveMagmaTerm)) open OpAdditiveMagmaTerm inductive OpAdditiveMagmaTerm2 (n : ℕ) (A : Type) : Type | v2 : ((fin n) → OpAdditiveMagmaTerm2) | sing2 : (A → OpAdditiveMagmaTerm2) | plusOL2 : (OpAdditiveMagmaTerm2 → (OpAdditiveMagmaTerm2 → OpAdditiveMagmaTerm2)) open OpAdditiveMagmaTerm2 def simplifyCl {A : Type} : ((ClAdditiveMagmaTerm A) → (ClAdditiveMagmaTerm A)) | (plusCl x1 x2) := (plusCl (simplifyCl x1) (simplifyCl x2)) | (sing x1) := (sing x1) def simplifyOpB {n : ℕ} : ((OpAdditiveMagmaTerm n) → (OpAdditiveMagmaTerm n)) | (plusOL x1 x2) := (plusOL (simplifyOpB x1) (simplifyOpB x2)) | (v x1) := (v x1) def simplifyOp {n : ℕ} {A : Type} : ((OpAdditiveMagmaTerm2 n A) → (OpAdditiveMagmaTerm2 n A)) | (plusOL2 x1 x2) := (plusOL2 (simplifyOp x1) (simplifyOp x2)) | (v2 x1) := (v2 x1) | (sing2 x1) := (sing2 x1) def evalB {A : Type} : ((AdditiveMagma A) → (AdditiveMagmaTerm → A)) | Ad (plusL x1 x2) := ((plus Ad) (evalB Ad x1) (evalB Ad x2)) def evalCl {A : Type} : ((AdditiveMagma A) → ((ClAdditiveMagmaTerm A) → A)) | Ad (sing x1) := x1 | Ad (plusCl x1 x2) := ((plus Ad) (evalCl Ad x1) (evalCl Ad x2)) def evalOpB {A : Type} {n : ℕ} : ((AdditiveMagma A) → ((vector A n) → ((OpAdditiveMagmaTerm n) → A))) | Ad vars (v x1) := (nth vars x1) | Ad vars (plusOL x1 x2) := ((plus Ad) (evalOpB Ad vars x1) (evalOpB Ad vars x2)) def evalOp {A : Type} {n : ℕ} : ((AdditiveMagma A) → ((vector A n) → ((OpAdditiveMagmaTerm2 n A) → A))) | Ad vars (v2 x1) := (nth vars x1) | Ad vars (sing2 x1) := x1 | Ad vars (plusOL2 x1 x2) := ((plus Ad) (evalOp Ad vars x1) (evalOp Ad vars x2)) def inductionB {P : (AdditiveMagmaTerm → Type)} : ((∀ (x1 x2 : AdditiveMagmaTerm) , ((P x1) → ((P x2) → (P (plusL x1 x2))))) → (∀ (x : AdditiveMagmaTerm) , (P x))) | pplusl (plusL x1 x2) := (pplusl _ _ (inductionB pplusl x1) (inductionB pplusl x2)) def inductionCl {A : Type} {P : ((ClAdditiveMagmaTerm A) → Type)} : ((∀ (x1 : A) , (P (sing x1))) → ((∀ (x1 x2 : (ClAdditiveMagmaTerm A)) , ((P x1) → ((P x2) → (P (plusCl x1 x2))))) → (∀ (x : (ClAdditiveMagmaTerm A)) , (P x)))) | psing ppluscl (sing x1) := (psing x1) | psing ppluscl (plusCl x1 x2) := (ppluscl _ _ (inductionCl psing ppluscl x1) (inductionCl psing ppluscl x2)) def inductionOpB {n : ℕ} {P : ((OpAdditiveMagmaTerm n) → Type)} : ((∀ (fin : (fin n)) , (P (v fin))) → ((∀ (x1 x2 : (OpAdditiveMagmaTerm n)) , ((P x1) → ((P x2) → (P (plusOL x1 x2))))) → (∀ (x : (OpAdditiveMagmaTerm n)) , (P x)))) | pv pplusol (v x1) := (pv x1) | pv pplusol (plusOL x1 x2) := (pplusol _ _ (inductionOpB pv pplusol x1) (inductionOpB pv pplusol x2)) def inductionOp {n : ℕ} {A : Type} {P : ((OpAdditiveMagmaTerm2 n A) → Type)} : ((∀ (fin : (fin n)) , (P (v2 fin))) → ((∀ (x1 : A) , (P (sing2 x1))) → ((∀ (x1 x2 : (OpAdditiveMagmaTerm2 n A)) , ((P x1) → ((P x2) → (P (plusOL2 x1 x2))))) → (∀ (x : (OpAdditiveMagmaTerm2 n A)) , (P x))))) | pv2 psing2 pplusol2 (v2 x1) := (pv2 x1) | pv2 psing2 pplusol2 (sing2 x1) := (psing2 x1) | pv2 psing2 pplusol2 (plusOL2 x1 x2) := (pplusol2 _ _ (inductionOp pv2 psing2 pplusol2 x1) (inductionOp pv2 psing2 pplusol2 x2)) def stageB : (AdditiveMagmaTerm → (Staged AdditiveMagmaTerm)) | (plusL x1 x2) := (stage2 plusL (codeLift2 plusL) (stageB x1) (stageB x2)) def stageCl {A : Type} : ((ClAdditiveMagmaTerm A) → (Staged (ClAdditiveMagmaTerm A))) | (sing x1) := (Now (sing x1)) | (plusCl x1 x2) := (stage2 plusCl (codeLift2 plusCl) (stageCl x1) (stageCl x2)) def stageOpB {n : ℕ} : ((OpAdditiveMagmaTerm n) → (Staged (OpAdditiveMagmaTerm n))) | (v x1) := (const (code (v x1))) | (plusOL x1 x2) := (stage2 plusOL (codeLift2 plusOL) (stageOpB x1) (stageOpB x2)) def stageOp {n : ℕ} {A : Type} : ((OpAdditiveMagmaTerm2 n A) → (Staged (OpAdditiveMagmaTerm2 n A))) | (sing2 x1) := (Now (sing2 x1)) | (v2 x1) := (const (code (v2 x1))) | (plusOL2 x1 x2) := (stage2 plusOL2 (codeLift2 plusOL2) (stageOp x1) (stageOp x2)) structure StagedRepr (A : Type) (Repr : (Type → Type)) : Type := (plusT : ((Repr A) → ((Repr A) → (Repr A)))) end AdditiveMagma
e1731dee59059292ed04f7dac79cde2f693acbed
137c667471a40116a7afd7261f030b30180468c2
/src/measure_theory/vitali_caratheodory.lean
579779468d342a75b0e0ce84ad7e9f895cdfc98b
[ "Apache-2.0" ]
permissive
bragadeesh153/mathlib
46bf814cfb1eecb34b5d1549b9117dc60f657792
b577bb2cd1f96eb47031878256856020b76f73cd
refs/heads/master
1,687,435,188,334
1,626,384,207,000
1,626,384,207,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
29,706
lean
/- Copyright (c) 2021 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import measure_theory.regular import topology.semicontinuous import measure_theory.bochner_integration import topology.instances.ereal /-! # Vitali-Carathéodory theorem Vitali-Carathéodory theorem asserts the following. Consider an integrable function `f : α → ℝ` on a space with a regular measure. Then there exists a function `g : α → ereal` such that `f x < g x` everywhere, `g` is lower semicontinuous, and the integral of `g` is arbitrarily close to that of `f`. This theorem is proved in this file, as `exists_lt_lower_semicontinuous_integral_lt`. Symmetrically, there exists `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. It follows from the previous statement applied to `-f`. It is formalized under the name `exists_upper_semicontinuous_lt_integral_gt`. The most classical version of Vitali-Carathéodory theorem only ensures a large inequality `f x ≤ g x`. For applications to the fundamental theorem of calculus, though, the strict inequality `f x < g x` is important. Therefore, we prove the stronger version with strict inequalities in this file. There is a price to pay: we require that the measure is `σ`-finite, which is not necessary for the classical Vitali-Carathéodory theorem. Since this is satisfied in all applications, this is not a real problem. ## Sketch of proof Decomposing `f` as the difference of its positive and negative parts, it suffices to show that a positive function can be bounded from above by a lower semicontinuous function, and from below by an upper semicontinuous function, with integrals close to that of `f`. For the bound from above, write `f` as a series `∑' n, cₙ * indicator (sₙ)` of simple functions. Then, approximate `sₙ` by a larger open set `uₙ` with measure very close to that of `sₙ` (this is possible by regularity of the measure), and set `g = ∑' n, cₙ * indicator (uₙ)`. It is lower semicontinuous as a series of lower semicontinuous functions, and its integral is arbitrarily close to that of `f`. For the bound from below, use finitely many terms in the series, and approximate `sₙ` from inside by a closed set `Fₙ`. Then `∑ n < N, cₙ * indicator (Fₙ)` is bounded from above by `f`, it is upper semicontinuous as a finite sum of upper semicontinuous functions, and its integral is arbitrarily close to that of `f`. The main pain point in the implementation is that one needs to jump between the spaces `ℝ`, `ℝ≥0`, `ℝ≥0∞` and `ereal` (and be careful that addition is not well behaved on `ereal`), and between `lintegral` and `integral`. We first show the bound from above for simple functions and the nonnegative integral (this is the main nontrivial mathematical point), then deduce it for general nonnegative functions, first for the nonnegative integral and then for the Bochner integral. Then we follow the same steps for the lower bound. Finally, we glue them together to obtain the main statement `exists_lt_lower_semicontinuous_integral_lt`. ## References [Rudin, *Real and Complex Analysis* (Theorem 2.24)][rudin2006real] -/ open_locale ennreal nnreal open measure_theory measure_theory.measure variables {α : Type*} [topological_space α] [measurable_space α] [borel_space α] (μ : measure α) [weakly_regular μ] namespace measure_theory local infixr ` →ₛ `:25 := simple_func /-! ### Lower semicontinuous upper bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_le_lower_semicontinuous_lintegral_ge : ∀ (f : α →ₛ ℝ≥0) {ε : ℝ≥0∞} (εpos : 0 < ε), ∃ g : α → ℝ≥0, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin refine simple_func.induction _ _, { assume c s hs ε εpos, let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases h : ∫⁻ x, f x ∂μ = ⊤, { refine ⟨λ x, c, λ x, _, lower_semicontinuous_const, by simp only [ennreal.top_add, le_top, h]⟩, simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_self _ _ _ }, by_cases hc : c = 0, { refine ⟨λ x, 0, _, lower_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [lintegral_const, zero_mul, zero_le, ennreal.coe_zero] } }, have : μ s < μ s + ε / c, { have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨εpos.ne', ennreal.coe_ne_top⟩, simpa using (ennreal.add_lt_add_iff_left _).2 this, simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, or_false, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and, restrict_apply] using h }, obtain ⟨u, u_open, su, μu⟩ : ∃ u, is_open u ∧ s ⊆ u ∧ μ u < μ s + ε / c := hs.exists_is_open_lt_of_lt _ this, refine ⟨set.indicator u (λ x, c), λ x, _, u_open.lower_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset su (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ u ≤ c * μ s + ε, by simpa only [hs, u_open.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ u ≤ c * (μ s + ε / c) : ennreal.mul_le_mul (le_refl _) μu.le ... = c * μ s + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { assume f₁ f₂ H h₁ h₂ ε εpos, rcases h₁ (ennreal.half_pos εpos) with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.half_pos εpos) with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, lintegral_add g₁cont.measurable.coe_nnreal_ennreal g₂cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, conv_lhs { rw ← ennreal.add_halves ε }, abel } end open simple_func (eapprox_diff tsum_eapprox_diff) /-- Given a measurable function `f` with values in `ℝ≥0`, there exists a lower semicontinuous function `g ≥ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_le_lower_semicontinuous_lintegral_ge (f : α → ℝ≥0∞) (hf : measurable f) {ε : ℝ≥0∞} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, f x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases ennreal.exists_pos_sum_of_encodable' εpos ℕ with ⟨δ, δpos, hδ⟩, have : ∀ n, ∃ g : α → ℝ≥0, (∀ x, simple_func.eapprox_diff f n x ≤ g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, simple_func.eapprox_diff f n x ∂μ + δ n) := λ n, simple_func.exists_le_lower_semicontinuous_lintegral_ge μ (simple_func.eapprox_diff f n) (δpos n), choose g f_le_g gcont hg using this, refine ⟨λ x, (∑' n, g n x), λ x, _, _, _⟩, { rw ← tsum_eapprox_diff f hf, exact ennreal.tsum_le_tsum (λ n, ennreal.coe_le_coe.2 (f_le_g n x)) }, { apply lower_semicontinuous_tsum (λ n, _), exact ennreal.continuous_coe.comp_lower_semicontinuous (gcont n) (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { calc ∫⁻ x, ∑' (n : ℕ), g n x ∂μ = ∑' n, ∫⁻ x, g n x ∂μ : by rw lintegral_tsum (λ n, (gcont n).measurable.coe_nnreal_ennreal) ... ≤ ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ + δ n) : ennreal.tsum_le_tsum hg ... = ∑' n, (∫⁻ x, eapprox_diff f n x ∂μ) + ∑' n, δ n : ennreal.tsum_add ... ≤ ∫⁻ (x : α), f x ∂μ + ε : begin refine add_le_add _ hδ.le, rw [← lintegral_tsum], { simp_rw [tsum_eapprox_diff f hf, le_refl] }, { assume n, exact (simple_func.measurable _).coe_nnreal_ennreal } end } end /-- Given a measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge [sigma_finite μ] (f : α → ℝ≥0) (fmeas : measurable f) {ε : ℝ≥0} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases exists_integrable_pos_of_sigma_finite μ (nnreal.half_pos εpos) with ⟨w, wpos, wmeas, wint⟩, let f' := λ x, ((f x + w x : ℝ≥0) : ℝ≥0∞), rcases exists_le_lower_semicontinuous_lintegral_ge μ f' (fmeas.add wmeas).coe_nnreal_ennreal (ennreal.coe_pos.2 (nnreal.half_pos εpos)) with ⟨g, le_g, gcont, gint⟩, refine ⟨g, λ x, _, gcont, _⟩, { calc (f x : ℝ≥0∞) < f' x : by simpa [← ennreal.coe_lt_coe] using add_lt_add_left (wpos x) (f x) ... ≤ g x : le_g x }, { calc ∫⁻ (x : α), g x ∂μ ≤ ∫⁻ (x : α), f x + w x ∂μ + (ε / 2 : ℝ≥0) : gint ... = ∫⁻ (x : α), f x ∂ μ + ∫⁻ (x : α), w x ∂ μ + (ε / 2 : ℝ≥0) : by rw lintegral_add fmeas.coe_nnreal_ennreal wmeas.coe_nnreal_ennreal ... ≤ ∫⁻ (x : α), f x ∂ μ + (ε / 2 : ℝ≥0) + (ε / 2 : ℝ≥0) : add_le_add_right (add_le_add_left wint.le _) _ ... = ∫⁻ (x : α), f x ∂μ + ε : by rw [add_assoc, ← ennreal.coe_add, nnreal.add_halves] }, end /-- Given an almost everywhere measurable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable [sigma_finite μ] (f : α → ℝ≥0) (fmeas : ae_measurable f μ) {ε : ℝ≥0} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∫⁻ x, g x ∂μ ≤ ∫⁻ x, f x ∂μ + ε) := begin rcases exists_lt_lower_semicontinuous_lintegral_ge μ (fmeas.mk f) fmeas.measurable_mk (nnreal.half_pos εpos) with ⟨g0, f_lt_g0, g0_cont, g0_int⟩, rcases exists_measurable_superset_of_null fmeas.ae_eq_mk with ⟨s, hs, smeas, μs⟩, rcases exists_le_lower_semicontinuous_lintegral_ge μ (s.indicator (λ x, ∞)) (measurable_const.indicator smeas) (ennreal.half_pos (ennreal.coe_pos.2 εpos)) with ⟨g1, le_g1, g1_cont, g1_int⟩, refine ⟨λ x, g0 x + g1 x, λ x, _, g0_cont.add g1_cont, _⟩, { by_cases h : x ∈ s, { have := le_g1 x, simp only [h, set.indicator_of_mem, top_le_iff] at this, simp [this] }, { have : f x = fmeas.mk f x, by { rw set.compl_subset_comm at hs, exact hs h }, rw this, exact (f_lt_g0 x).trans_le le_self_add } }, { calc ∫⁻ x, g0 x + g1 x ∂μ = ∫⁻ x, g0 x ∂μ + ∫⁻ x, g1 x ∂μ : lintegral_add g0_cont.measurable g1_cont.measurable ... ≤ (∫⁻ x, f x ∂μ + ε / 2) + (0 + ε / 2) : begin refine add_le_add _ _, { convert g0_int using 2, { exact lintegral_congr_ae (fmeas.ae_eq_mk.fun_comp _) }, { simp only [ennreal.coe_div, ennreal.coe_one, ennreal.coe_bit0, ne.def, not_false_iff, bit0_eq_zero, one_ne_zero], } }, { convert g1_int, simp only [smeas, μs, lintegral_const, set.univ_inter, measurable_set.univ, lintegral_indicator, mul_zero, restrict_apply] } end ... = ∫⁻ x, f x ∂μ + ε : by simp only [add_assoc, ennreal.add_halves, zero_add] } end variable {μ} /-- Given an integrable function `f` with values in `ℝ≥0` in a sigma-finite space, there exists a lower semicontinuous function `g > f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_lt_lower_semicontinuous_integral_gt_nnreal [sigma_finite μ] (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0∞, (∀ x, (f x : ℝ≥0∞) < g x) ∧ lower_semicontinuous g ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (integrable (λ x, (g x).to_real) μ) ∧ (∫ x, (g x).to_real ∂μ < ∫ x, f x ∂μ + ε) := begin have fmeas : ae_measurable f μ, by { convert fint.ae_measurable.real_to_nnreal, ext1 x, simp only [real.to_nnreal_coe] }, let δ : ℝ≥0 := ⟨ε/2, (half_pos εpos).le⟩, have δpos : 0 < δ := half_pos εpos, have int_f_lt_top : ∫⁻ (a : α), (f a) ∂μ < ∞ := has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral, rcases exists_lt_lower_semicontinuous_lintegral_ge_of_ae_measurable μ f fmeas δpos with ⟨g, f_lt_g, gcont, gint⟩, have gint_lt : ∫⁻ (x : α), g x ∂μ < ∞ := gint.trans_lt (by simpa using int_f_lt_top), have g_lt_top : ∀ᵐ (x : α) ∂μ, g x < ∞ := ae_lt_top gcont.measurable gint_lt, have Ig : ∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ = ∫⁻ (a : α), g a ∂μ, { apply lintegral_congr_ae, filter_upwards [g_lt_top], assume x hx, simp only [hx.ne, ennreal.of_real_to_real, ne.def, not_false_iff] }, refine ⟨g, f_lt_g, gcont, g_lt_top, _, _⟩, { refine ⟨gcont.measurable.ennreal_to_real.ae_measurable, _⟩, simp [has_finite_integral_iff_norm, real.norm_eq_abs, abs_of_nonneg], convert gint_lt using 1 }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { calc ennreal.to_real (∫⁻ (a : α), ennreal.of_real (g a).to_real ∂μ) = ennreal.to_real (∫⁻ (a : α), g a ∂μ) : by congr' 1 ... ≤ ennreal.to_real (∫⁻ (a : α), f a ∂μ + δ) : begin apply ennreal.to_real_mono _ gint, simpa using int_f_lt_top.ne, end ... = ennreal.to_real (∫⁻ (a : α), f a ∂μ) + δ : by rw [ennreal.to_real_add int_f_lt_top.ne ennreal.coe_ne_top, ennreal.coe_to_real] ... < ennreal.to_real (∫⁻ (a : α), f a ∂μ) + ε : add_lt_add_left (by simp [δ, half_lt_self εpos]) _ ... = (∫⁻ (a : α), ennreal.of_real ↑(f a) ∂μ).to_real + ε : by simp }, { apply filter.eventually_of_forall (λ x, _), simp }, { exact fmeas.coe_nnreal_real, }, { apply filter.eventually_of_forall (λ x, _), simp }, { apply gcont.measurable.ennreal_to_real.ae_measurable } } end /-! ### Upper semicontinuous lower bound for nonnegative functions -/ /-- Given a simple function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma simple_func.exists_upper_semicontinuous_le_lintegral_le : ∀ (f : α →ₛ ℝ≥0) (int_f : ∫⁻ x, f x ∂μ < ∞) {ε : ℝ≥0∞} (εpos : 0 < ε), ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin refine simple_func.induction _ _, { assume c s hs int_f ε εpos, let f := simple_func.piecewise s hs (simple_func.const α c) (simple_func.const α 0), by_cases hc : c = 0, { refine ⟨λ x, 0, _, upper_semicontinuous_const, _⟩, { simp only [hc, set.indicator_zero', pi.zero_apply, simple_func.const_zero, implies_true_iff, eq_self_iff_true, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, le_zero_iff] }, { simp only [hc, set.indicator_zero', lintegral_const, zero_mul, pi.zero_apply, simple_func.const_zero, zero_add, zero_le', simple_func.coe_zero, set.piecewise_eq_indicator, ennreal.coe_zero, simple_func.coe_piecewise, εpos.le] } }, have μs_lt_top : μ s < ∞, by simpa only [hs, hc, lt_top_iff_ne_top, true_and, simple_func.coe_const, or_false, lintegral_const, ennreal.coe_indicator, set.univ_inter, ennreal.coe_ne_top, restrict_apply measurable_set.univ, with_top.mul_eq_top_iff, simple_func.const_zero, function.const_apply, lintegral_indicator, ennreal.coe_eq_zero, ne.def, not_false_iff, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, false_and] using int_f, have : (0 : ℝ≥0∞) < ε / c := ennreal.div_pos_iff.2 ⟨εpos.ne', ennreal.coe_ne_top⟩, obtain ⟨F, F_closed, Fs, μF⟩ : ∃ F, is_closed F ∧ F ⊆ s ∧ μ s < μ F + ε / c := hs.exists_lt_is_closed_of_lt_top_of_pos μs_lt_top this, refine ⟨set.indicator F (λ x, c), λ x, _, F_closed.upper_semicontinuous_indicator (zero_le _), _⟩, { simp only [simple_func.coe_const, simple_func.const_zero, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise], exact set.indicator_le_indicator_of_subset Fs (λ x, zero_le _) _ }, { suffices : (c : ℝ≥0∞) * μ s ≤ c * μ F + ε, by simpa only [hs, F_closed.measurable_set, simple_func.coe_const, function.const_apply, lintegral_const, ennreal.coe_indicator, set.univ_inter, measurable_set.univ, simple_func.const_zero, lintegral_indicator, simple_func.coe_zero, set.piecewise_eq_indicator, simple_func.coe_piecewise, restrict_apply], calc (c : ℝ≥0∞) * μ s ≤ c * (μ F + ε / c) : ennreal.mul_le_mul (le_refl _) μF.le ... = c * μ F + ε : begin simp_rw [mul_add], rw ennreal.mul_div_cancel' _ ennreal.coe_ne_top, simpa using hc, end } }, { assume f₁ f₂ H h₁ h₂ f_int ε εpos, have A : ∫⁻ (x : α), f₁ x ∂μ + ∫⁻ (x : α), f₂ x ∂μ < ⊤, { rw ← lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, simpa only [simple_func.coe_add, ennreal.coe_add, pi.add_apply] using f_int }, rcases h₁ (ennreal.add_lt_top.1 A).1 (ennreal.half_pos εpos) with ⟨g₁, f₁_le_g₁, g₁cont, g₁int⟩, rcases h₂ (ennreal.add_lt_top.1 A).2 (ennreal.half_pos εpos) with ⟨g₂, f₂_le_g₂, g₂cont, g₂int⟩, refine ⟨λ x, g₁ x + g₂ x, λ x, add_le_add (f₁_le_g₁ x) (f₂_le_g₂ x), g₁cont.add g₂cont, _⟩, simp only [simple_func.coe_add, ennreal.coe_add, pi.add_apply], rw [lintegral_add f₁.measurable.coe_nnreal_ennreal f₂.measurable.coe_nnreal_ennreal, lintegral_add g₁cont.measurable.coe_nnreal_ennreal g₂cont.measurable.coe_nnreal_ennreal], convert add_le_add g₁int g₂int using 1, conv_lhs { rw ← ennreal.add_halves ε }, abel } end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `lintegral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_lintegral_le (f : α → ℝ≥0) (int_f : ∫⁻ x, f x ∂μ < ∞) {ε : ℝ≥0∞} (εpos : 0 < ε) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, g x ∂μ + ε) := begin obtain ⟨fs, fs_le_f, int_fs⟩ : ∃ (fs : α →ₛ ℝ≥0), (∀ x, fs x ≤ f x) ∧ (∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε/2) := begin have := ennreal.lt_add_right int_f (ennreal.half_pos εpos), conv_rhs at this { rw lintegral_eq_nnreal (λ x, (f x : ℝ≥0∞)) μ }, erw ennreal.bsupr_add at this; [skip, exact ⟨0, λ x, by simp⟩], simp only [lt_supr_iff] at this, rcases this with ⟨fs, fs_le_f, int_fs⟩, refine ⟨fs, λ x, by simpa only [ennreal.coe_le_coe] using fs_le_f x, _⟩, convert int_fs.le, rw ← simple_func.lintegral_eq_lintegral, refl end, have int_fs_lt_top : ∫⁻ x, fs x ∂μ < ∞, { apply lt_of_le_of_lt (lintegral_mono (λ x, _)) int_f, simpa only [ennreal.coe_le_coe] using fs_le_f x }, obtain ⟨g, g_le_fs, gcont, gint⟩ : ∃ g : α → ℝ≥0, (∀ x, g x ≤ fs x) ∧ upper_semicontinuous g ∧ (∫⁻ x, fs x ∂μ ≤ ∫⁻ x, g x ∂μ + ε/2) := fs.exists_upper_semicontinuous_le_lintegral_le int_fs_lt_top (ennreal.half_pos εpos), refine ⟨g, λ x, (g_le_fs x).trans (fs_le_f x), gcont, _⟩, calc ∫⁻ x, f x ∂μ ≤ ∫⁻ x, fs x ∂μ + ε / 2 : int_fs ... ≤ (∫⁻ x, g x ∂μ + ε / 2) + ε / 2 : add_le_add gint (le_refl _) ... = ∫⁻ x, g x ∂μ + ε : by rw [add_assoc, ennreal.add_halves] end /-- Given an integrable function `f` with values in `ℝ≥0`, there exists an upper semicontinuous function `g ≤ f` with integral arbitrarily close to that of `f`. Formulation in terms of `integral`. Auxiliary lemma for Vitali-Carathéodory theorem `exists_lt_lower_semicontinuous_integral_lt`. -/ lemma exists_upper_semicontinuous_le_integral_le (f : α → ℝ≥0) (fint : integrable (λ x, (f x : ℝ)) μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ℝ≥0, (∀ x, g x ≤ f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, (g x : ℝ)) μ) ∧ (∫ x, (f x : ℝ) ∂μ - ε ≤ ∫ x, g x ∂μ) := begin let δ : ℝ≥0 := ⟨ε, εpos.le⟩, have δpos : (0 : ℝ≥0∞) < δ := ennreal.coe_lt_coe.2 εpos, have If : ∫⁻ x, f x ∂ μ < ∞ := has_finite_integral_iff_of_nnreal.1 fint.has_finite_integral, rcases exists_upper_semicontinuous_le_lintegral_le f If δpos with ⟨g, gf, gcont, gint⟩, have Ig : ∫⁻ x, g x ∂ μ < ∞, { apply lt_of_le_of_lt (lintegral_mono (λ x, _)) If, simpa using gf x }, refine ⟨g, gf, gcont, _, _⟩, { refine integrable.mono fint gcont.measurable.coe_nnreal_real.ae_measurable _, exact filter.eventually_of_forall (λ x, by simp [gf x]) }, { rw [integral_eq_lintegral_of_nonneg_ae, integral_eq_lintegral_of_nonneg_ae], { rw sub_le_iff_le_add, convert ennreal.to_real_mono _ gint, { simp, }, { rw ennreal.to_real_add Ig.ne ennreal.coe_ne_top, simp }, { simpa using Ig.ne } }, { apply filter.eventually_of_forall, simp }, { exact gcont.measurable.coe_nnreal_real.ae_measurable }, { apply filter.eventually_of_forall, simp }, { exact fint.ae_measurable } } end /-! ### Vitali-Carathéodory theorem -/ /-- **Vitali-Carathéodory Theorem**: given an integrable real function `f`, there exists an integrable function `g > f` which is lower semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_lt_lower_semicontinuous_integral_lt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (f x : ereal) < g x) ∧ lower_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂ μ, g x < ⊤) ∧ (∫ x, ereal.to_real (g x) ∂μ < ∫ x, f x ∂μ + ε) := begin let δ : ℝ≥0 := ⟨ε/2, (half_pos εpos).le⟩, have δpos : 0 < δ := half_pos εpos, let fp : α → ℝ≥0 := λ x, real.to_nnreal (f x), have int_fp : integrable (λ x, (fp x : ℝ)) μ := hf.real_to_nnreal, rcases exists_lt_lower_semicontinuous_integral_gt_nnreal fp int_fp δpos with ⟨gp, fp_lt_gp, gpcont, gp_lt_top, gp_integrable, gpint⟩, let fm : α → ℝ≥0 := λ x, real.to_nnreal (-f x), have int_fm : integrable (λ x, (fm x : ℝ)) μ := hf.neg.real_to_nnreal, rcases exists_upper_semicontinuous_le_integral_le fm int_fm δpos with ⟨gm, gm_le_fm, gmcont, gm_integrable, gmint⟩, let g : α → ereal := λ x, (gp x : ereal) - (gm x), have ae_g : ∀ᵐ x ∂ μ, (g x).to_real = (gp x : ereal).to_real - (gm x : ereal).to_real, { filter_upwards [gp_lt_top], assume x hx, rw ereal.to_real_sub; simp [hx.ne] }, refine ⟨g, _, _, _, _, _⟩, show integrable (λ x, ereal.to_real (g x)) μ, { rw integrable_congr ae_g, convert gp_integrable.sub gm_integrable, ext x, simp }, show ∫ (x : α), (g x).to_real ∂μ < ∫ (x : α), f x ∂μ + ε, from calc ∫ (x : α), (g x).to_real ∂μ = ∫ (x : α), ereal.to_real (gp x) - ereal.to_real (gm x) ∂μ : integral_congr_ae ae_g ... = ∫ (x : α), ereal.to_real (gp x) ∂ μ - ∫ (x : α), gm x ∂μ : begin simp only [ereal.to_real_coe_ennreal, ennreal.coe_to_real, coe_coe], exact integral_sub gp_integrable gm_integrable, end ... < ∫ (x : α), ↑(fp x) ∂μ + ↑δ - ∫ (x : α), gm x ∂μ : begin apply sub_lt_sub_right, convert gpint, simp only [ereal.to_real_coe_ennreal], end ... ≤ ∫ (x : α), ↑(fp x) ∂μ + ↑δ - (∫ (x : α), fm x ∂μ - δ) : sub_le_sub_left gmint _ ... = ∫ (x : α), f x ∂μ + 2 * δ : by { simp_rw [integral_eq_integral_pos_part_sub_integral_neg_part hf, fp, fm], ring } ... = ∫ (x : α), f x ∂μ + ε : by { congr' 1, field_simp [δ, mul_comm] }, show ∀ᵐ (x : α) ∂μ, g x < ⊤, { filter_upwards [gp_lt_top], assume x hx, simp [g, ereal.sub_eq_add_neg, lt_top_iff_ne_top, lt_top_iff_ne_top.1 hx] }, show ∀ x, (f x : ereal) < g x, { assume x, rw ereal.coe_real_ereal_eq_coe_to_nnreal_sub_coe_to_nnreal (f x), refine ereal.sub_lt_sub_of_lt_of_le _ _ _ _, { simp only [ereal.coe_ennreal_lt_coe_ennreal_iff, coe_coe], exact (fp_lt_gp x) }, { simp only [ennreal.coe_le_coe, ereal.coe_ennreal_le_coe_ennreal_iff, coe_coe], exact (gm_le_fm x) }, { simp only [ereal.coe_ennreal_ne_bot, ne.def, not_false_iff, coe_coe] }, { simp only [ereal.coe_nnreal_ne_top, ne.def, not_false_iff, coe_coe] } }, show lower_semicontinuous g, { apply lower_semicontinuous.add', { exact continuous_coe_ennreal_ereal.comp_lower_semicontinuous gpcont (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy) }, { apply ereal.continuous_neg.comp_upper_semicontinuous_antimono _ (λ x y hxy, ereal.neg_le_neg_iff.2 hxy), dsimp, apply continuous_coe_ennreal_ereal.comp_upper_semicontinuous _ (λ x y hxy, ereal.coe_ennreal_le_coe_ennreal_iff.2 hxy), exact ennreal.continuous_coe.comp_upper_semicontinuous gmcont (λ x y hxy, ennreal.coe_le_coe.2 hxy) }, { assume x, exact ereal.continuous_at_add (by simp) (by simp) } } end /-- **Vitali-Carathéodory Theorem**: given an integrable real function `f`, there exists an integrable function `g < f` which is upper semicontinuous, with integral arbitrarily close to that of `f`. This function has to be `ereal`-valued in general. -/ lemma exists_upper_semicontinuous_lt_integral_gt [sigma_finite μ] (f : α → ℝ) (hf : integrable f μ) {ε : ℝ} (εpos : 0 < ε) : ∃ g : α → ereal, (∀ x, (g x : ereal) < f x) ∧ upper_semicontinuous g ∧ (integrable (λ x, ereal.to_real (g x)) μ) ∧ (∀ᵐ x ∂μ, ⊥ < g x) ∧ (∫ x, f x ∂μ < ∫ x, ereal.to_real (g x) ∂μ + ε) := begin rcases exists_lt_lower_semicontinuous_integral_lt (λ x, - f x) hf.neg εpos with ⟨g, g_lt_f, gcont, g_integrable, g_lt_top, gint⟩, refine ⟨λ x, - g x, _, _, _, _, _⟩, { exact λ x, ereal.neg_lt_iff_neg_lt.1 (by simpa only [ereal.coe_neg] using g_lt_f x) }, { exact ereal.continuous_neg.comp_lower_semicontinuous_antimono gcont (λ x y hxy, ereal.neg_le_neg_iff.2 hxy) }, { convert g_integrable.neg, ext x, simp }, { simpa [bot_lt_iff_ne_bot, lt_top_iff_ne_top] using g_lt_top }, { simp_rw [integral_neg, lt_neg_add_iff_add_lt] at gint, rw add_comm at gint, simpa [integral_neg] using gint } end end measure_theory
bd5c52de859afc7c35ca43ab57adb947f22644c4
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/LocalContext.lean
d3b708e8319a8a38642503d2d8c0bd08b842e7c4
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
17,439
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Std.Data.PersistentArray import Lean.Expr import Lean.Hygiene namespace Lean /-- A declaration for a LocalContext. This is used to register which free variables are in scope. Each declaration comes with - `index` the position of the decl in the local context - `fvarId` the unique id of the free variables - `userName` the pretty-printable name of the variable - `type` the type. A `cdecl` is a local variable, a `ldecl` is a let-bound free variable with a `value : Expr`. -/ inductive LocalDecl where | cdecl (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) | ldecl (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) (nonDep : Bool) deriving Inhabited @[export lean_mk_local_decl] def mkLocalDeclEx (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) : LocalDecl := .cdecl index fvarId userName type bi @[export lean_mk_let_decl] def mkLetDeclEx (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (val : Expr) : LocalDecl := .ldecl index fvarId userName type val false @[export lean_local_decl_binder_info] def LocalDecl.binderInfoEx : LocalDecl → BinderInfo | .cdecl _ _ _ _ bi => bi | _ => BinderInfo.default namespace LocalDecl def isLet : LocalDecl → Bool | cdecl .. => false | ldecl .. => true def index : LocalDecl → Nat | cdecl (index := i) .. => i | ldecl (index := i) .. => i def setIndex : LocalDecl → Nat → LocalDecl | cdecl _ id n t bi, idx => cdecl idx id n t bi | ldecl _ id n t v nd, idx => ldecl idx id n t v nd def fvarId : LocalDecl → FVarId | cdecl (fvarId := id) .. => id | ldecl (fvarId := id) .. => id def userName : LocalDecl → Name | cdecl (userName := n) .. => n | ldecl (userName := n) .. => n def type : LocalDecl → Expr | cdecl (type := t) .. => t | ldecl (type := t) .. => t def setType : LocalDecl → Expr → LocalDecl | cdecl idx id n _ bi, t => cdecl idx id n t bi | ldecl idx id n _ v nd, t => ldecl idx id n t v nd def binderInfo : LocalDecl → BinderInfo | cdecl (bi := bi) .. => bi | ldecl .. => BinderInfo.default def isAuxDecl (d : LocalDecl) : Bool := d.binderInfo.isAuxDecl def value? : LocalDecl → Option Expr | cdecl .. => none | ldecl (value := v) .. => some v def value : LocalDecl → Expr | cdecl .. => panic! "let declaration expected" | ldecl (value := v) .. => v def hasValue : LocalDecl → Bool | cdecl .. => false | ldecl .. => true def setValue : LocalDecl → Expr → LocalDecl | ldecl idx id n t _ nd, v => ldecl idx id n t v nd | d, _ => d def setUserName : LocalDecl → Name → LocalDecl | cdecl index id _ type bi, userName => cdecl index id userName type bi | ldecl index id _ type val nd, userName => ldecl index id userName type val nd def setBinderInfo : LocalDecl → BinderInfo → LocalDecl | cdecl index id n type _, bi => cdecl index id n type bi | ldecl .., _ => panic! "unexpected let declaration" def toExpr (decl : LocalDecl) : Expr := mkFVar decl.fvarId def hasExprMVar : LocalDecl → Bool | cdecl (type := t) .. => t.hasExprMVar | ldecl (type := t) (value := v) .. => t.hasExprMVar || v.hasExprMVar end LocalDecl open Std (PersistentHashMap PersistentArray PArray) /-- A LocalContext is an ordered set of local variable declarations. It is used to store the free variables (also known as local constants) that are in scope. When inspecting a goal or expected type in the infoview, the local context is all of the variables above the `⊢` symbol. -/ structure LocalContext where fvarIdToDecl : PersistentHashMap FVarId LocalDecl := {} decls : PersistentArray (Option LocalDecl) := {} deriving Inhabited namespace LocalContext @[export lean_mk_empty_local_ctx] def mkEmpty : Unit → LocalContext := fun _ => {} def empty : LocalContext := {} @[export lean_local_ctx_is_empty] def isEmpty (lctx : LocalContext) : Bool := lctx.fvarIdToDecl.isEmpty /-- Low level API for creating local declarations. It is used to implement actions in the monads `Elab` and `Tactic`. It should not be used directly since the argument `(fvarId : FVarId)` is assumed to be unique. You can create a unique fvarId with `mkFreshFVarId`. -/ @[export lean_local_ctx_mk_local_decl] def mkLocalDecl (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo := BinderInfo.default) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size let decl := LocalDecl.cdecl idx fvarId userName type bi { fvarIdToDecl := map.insert fvarId decl, decls := decls.push decl } /-- Low level API for let declarations. Do not use directly.-/ @[export lean_local_ctx_mk_let_decl] def mkLetDecl (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) (nonDep := false) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size let decl := LocalDecl.ldecl idx fvarId userName type value nonDep { fvarIdToDecl := map.insert fvarId decl, decls := decls.push decl } /-- Low level API for adding a local declaration. Do not use directly. -/ def addDecl (lctx : LocalContext) (newDecl : LocalDecl) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size let newDecl := newDecl.setIndex idx { fvarIdToDecl := map.insert newDecl.fvarId newDecl, decls := decls.push newDecl } @[export lean_local_ctx_find] def find? (lctx : LocalContext) (fvarId : FVarId) : Option LocalDecl := lctx.fvarIdToDecl.find? fvarId def findFVar? (lctx : LocalContext) (e : Expr) : Option LocalDecl := lctx.find? e.fvarId! def get! (lctx : LocalContext) (fvarId : FVarId) : LocalDecl := match lctx.find? fvarId with | some d => d | none => panic! "unknown free variable" /-- Gets the declaration for expression `e` in the local context. If `e` is not a free variable or not present then panics. -/ def getFVar! (lctx : LocalContext) (e : Expr) : LocalDecl := lctx.get! e.fvarId! def contains (lctx : LocalContext) (fvarId : FVarId) : Bool := lctx.fvarIdToDecl.contains fvarId /-- Returns true when the lctx contains the free variable `e`. Panics if `e` is not an fvar. -/ def containsFVar (lctx : LocalContext) (e : Expr) : Bool := lctx.contains e.fvarId! def getFVarIds (lctx : LocalContext) : Array FVarId := lctx.decls.foldl (init := #[]) fun r decl? => match decl? with | some decl => r.push decl.fvarId | none => r /-- Return all of the free variables in the given context. -/ def getFVars (lctx : LocalContext) : Array Expr := lctx.getFVarIds.map mkFVar private partial def popTailNoneAux (a : PArray (Option LocalDecl)) : PArray (Option LocalDecl) := if a.size == 0 then a else match a.get! (a.size - 1) with | none => popTailNoneAux a.pop | some _ => a @[export lean_local_ctx_erase] def erase (lctx : LocalContext) (fvarId : FVarId) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match map.find? fvarId with | none => lctx | some decl => { fvarIdToDecl := map.erase fvarId, decls := popTailNoneAux (decls.set decl.index none) } @[export lean_local_ctx_pop] def pop (lctx : LocalContext): LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => if decls.size == 0 then lctx else match decls.get! (decls.size - 1) with | none => lctx -- unreachable | some decl => { fvarIdToDecl := map.erase decl.fvarId, decls := popTailNoneAux decls.pop } @[export lean_local_ctx_find_from_user_name] def findFromUserName? (lctx : LocalContext) (userName : Name) : Option LocalDecl := lctx.decls.findSomeRev? fun decl => match decl with | none => none | some decl => if decl.userName == userName then some decl else none @[export lean_local_ctx_uses_user_name] def usesUserName (lctx : LocalContext) (userName : Name) : Bool := (lctx.findFromUserName? userName).isSome private partial def getUnusedNameAux (lctx : LocalContext) (suggestion : Name) (i : Nat) : Name × Nat := let curr := suggestion.appendIndexAfter i if lctx.usesUserName curr then getUnusedNameAux lctx suggestion (i + 1) else (curr, i + 1) @[export lean_local_ctx_get_unused_name] def getUnusedName (lctx : LocalContext) (suggestion : Name) : Name := let suggestion := suggestion.eraseMacroScopes if lctx.usesUserName suggestion then (getUnusedNameAux lctx suggestion 1).1 else suggestion @[export lean_local_ctx_last_decl] def lastDecl (lctx : LocalContext) : Option LocalDecl := lctx.decls.get! (lctx.decls.size - 1) def setUserName (lctx : LocalContext) (fvarId : FVarId) (userName : Name) : LocalContext := let decl := lctx.get! fvarId let decl := decl.setUserName userName { fvarIdToDecl := lctx.fvarIdToDecl.insert decl.fvarId decl, decls := lctx.decls.set decl.index decl } @[export lean_local_ctx_rename_user_name] def renameUserName (lctx : LocalContext) (fromName : Name) (toName : Name) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match lctx.findFromUserName? fromName with | none => lctx | some decl => let decl := decl.setUserName toName; { fvarIdToDecl := map.insert decl.fvarId decl, decls := decls.set decl.index decl } /-- Low-level function for updating the local context. Assumptions about `f`, the resulting nested expressions must be definitionally equal to their original values, the `index` nor `fvarId` are modified. -/ @[inline] def modifyLocalDecl (lctx : LocalContext) (fvarId : FVarId) (f : LocalDecl → LocalDecl) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match lctx.find? fvarId with | none => lctx | some decl => let decl := f decl { fvarIdToDecl := map.insert decl.fvarId decl decls := decls.set decl.index decl } def setBinderInfo (lctx : LocalContext) (fvarId : FVarId) (bi : BinderInfo) : LocalContext := modifyLocalDecl lctx fvarId fun decl => decl.setBinderInfo bi @[export lean_local_ctx_num_indices] def numIndices (lctx : LocalContext) : Nat := lctx.decls.size @[export lean_local_ctx_get] def getAt? (lctx : LocalContext) (i : Nat) : Option LocalDecl := lctx.decls.get! i @[specialize] def foldlM [Monad m] (lctx : LocalContext) (f : β → LocalDecl → m β) (init : β) (start : Nat := 0) : m β := lctx.decls.foldlM (init := init) (start := start) fun b decl => match decl with | none => pure b | some decl => f b decl @[specialize] def foldrM [Monad m] (lctx : LocalContext) (f : LocalDecl → β → m β) (init : β) : m β := lctx.decls.foldrM (init := init) fun decl b => match decl with | none => pure b | some decl => f decl b @[specialize] def forM [Monad m] (lctx : LocalContext) (f : LocalDecl → m PUnit) : m PUnit := lctx.decls.forM fun decl => match decl with | none => pure PUnit.unit | some decl => f decl @[specialize] def findDeclM? [Monad m] (lctx : LocalContext) (f : LocalDecl → m (Option β)) : m (Option β) := lctx.decls.findSomeM? fun decl => match decl with | none => pure none | some decl => f decl @[specialize] def findDeclRevM? [Monad m] (lctx : LocalContext) (f : LocalDecl → m (Option β)) : m (Option β) := lctx.decls.findSomeRevM? fun decl => match decl with | none => pure none | some decl => f decl instance : ForIn m LocalContext LocalDecl where forIn lctx init f := lctx.decls.forIn init fun d? b => match d? with | none => return ForInStep.yield b | some d => f d b @[inline] def foldl (lctx : LocalContext) (f : β → LocalDecl → β) (init : β) (start : Nat := 0) : β := Id.run <| lctx.foldlM f init start @[inline] def foldr (lctx : LocalContext) (f : LocalDecl → β → β) (init : β) : β := Id.run <| lctx.foldrM f init def size (lctx : LocalContext) : Nat := lctx.foldl (fun n _ => n+1) 0 @[inline] def findDecl? (lctx : LocalContext) (f : LocalDecl → Option β) : Option β := Id.run <| lctx.findDeclM? f @[inline] def findDeclRev? (lctx : LocalContext) (f : LocalDecl → Option β) : Option β := Id.run <| lctx.findDeclRevM? f partial def isSubPrefixOfAux (a₁ a₂ : PArray (Option LocalDecl)) (exceptFVars : Array Expr) (i j : Nat) : Bool := if i < a₁.size then match a₁[i]! with | none => isSubPrefixOfAux a₁ a₂ exceptFVars (i+1) j | some decl₁ => if exceptFVars.any fun fvar => fvar.fvarId! == decl₁.fvarId then isSubPrefixOfAux a₁ a₂ exceptFVars (i+1) j else if j < a₂.size then match a₂[j]! with | none => isSubPrefixOfAux a₁ a₂ exceptFVars i (j+1) | some decl₂ => if decl₁.fvarId == decl₂.fvarId then isSubPrefixOfAux a₁ a₂ exceptFVars (i+1) (j+1) else isSubPrefixOfAux a₁ a₂ exceptFVars i (j+1) else false else true /-- Given `lctx₁ - exceptFVars` of the form `(x_1 : A_1) ... (x_n : A_n)`, then return true iff there is a local context `B_1* (x_1 : A_1) ... B_n* (x_n : A_n)` which is a prefix of `lctx₂` where `B_i`'s are (possibly empty) sequences of local declarations. -/ def isSubPrefixOf (lctx₁ lctx₂ : LocalContext) (exceptFVars : Array Expr := #[]) : Bool := isSubPrefixOfAux lctx₁.decls lctx₂.decls exceptFVars 0 0 @[inline] def mkBinding (isLambda : Bool) (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := let b := b.abstract xs xs.size.foldRev (init := b) fun i b => let x := xs[i]! match lctx.findFVar? x with | some (.cdecl _ _ n ty bi) => let ty := ty.abstractRange i xs; if isLambda then Lean.mkLambda n bi ty b else Lean.mkForall n bi ty b | some (.ldecl _ _ n ty val nonDep) => if b.hasLooseBVar 0 then let ty := ty.abstractRange i xs let val := val.abstractRange i xs mkLet n ty val b nonDep else b.lowerLooseBVars 1 1 | none => panic! "unknown free variable" /-- Creates the expression `fun x₁ .. xₙ => b` for free variables `xs = #[x₁, .., xₙ]`, suitably abstracting `b` and the types for each of the `xᵢ`. -/ def mkLambda (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := mkBinding true lctx xs b /-- Creates the expression `(x₁:α₁) → .. → (xₙ:αₙ) → b` for free variables `xs = #[x₁, .., xₙ]`, suitably abstracting `b` and the types for each of the `xᵢ`, `αᵢ`. -/ def mkForall (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := mkBinding false lctx xs b @[inline] def anyM [Monad m] (lctx : LocalContext) (p : LocalDecl → m Bool) : m Bool := lctx.decls.anyM fun d => match d with | some decl => p decl | none => pure false @[inline] def allM [Monad m] (lctx : LocalContext) (p : LocalDecl → m Bool) : m Bool := lctx.decls.allM fun d => match d with | some decl => p decl | none => pure true /-- Return `true` if `lctx` contains a local declaration satisfying `p`. -/ @[inline] def any (lctx : LocalContext) (p : LocalDecl → Bool) : Bool := Id.run <| lctx.anyM p /-- Return `true` if all declarations in `lctx` satisfy `p`. -/ @[inline] def all (lctx : LocalContext) (p : LocalDecl → Bool) : Bool := Id.run <| lctx.allM p /-- If option `pp.sanitizeNames` is set to `true`, add tombstone to shadowed local declaration names and ones contains macroscopes. -/ def sanitizeNames (lctx : LocalContext) : StateM NameSanitizerState LocalContext := do let st ← get if !getSanitizeNames st.options then pure lctx else StateT.run' (s := ({} : NameSet)) <| lctx.decls.size.foldRevM (init := lctx) fun i lctx => do match lctx.decls[i]! with | none => pure lctx | some decl => if decl.userName.hasMacroScopes || (← get).contains decl.userName then do modify fun s => s.insert decl.userName let userNameNew ← liftM <| sanitizeName decl.userName pure <| lctx.setUserName decl.fvarId userNameNew else modify fun s => s.insert decl.userName pure lctx end LocalContext /-- Class used to denote that `m` has a local context. -/ class MonadLCtx (m : Type → Type) where getLCtx : m LocalContext export MonadLCtx (getLCtx) instance [MonadLift m n] [MonadLCtx m] : MonadLCtx n where getLCtx := liftM (getLCtx : m _) def LocalDecl.replaceFVarId (fvarId : FVarId) (e : Expr) (d : LocalDecl) : LocalDecl := if d.fvarId == fvarId then d else match d with | .cdecl idx id n type bi => .cdecl idx id n (type.replaceFVarId fvarId e) bi | .ldecl idx id n type val nonDep => .ldecl idx id n (type.replaceFVarId fvarId e) (val.replaceFVarId fvarId e) nonDep def LocalContext.replaceFVarId (fvarId : FVarId) (e : Expr) (lctx : LocalContext) : LocalContext := let lctx := lctx.erase fvarId { fvarIdToDecl := lctx.fvarIdToDecl.map (·.replaceFVarId fvarId e) decls := lctx.decls.map fun localDecl? => localDecl?.map (·.replaceFVarId fvarId e) } end Lean
bc6a202b48a4fc29daae3bb26801d3318dee18e6
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/topology/instances/real.lean
e0e5b32864433c94623d2935090304eba1a55e15
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
18,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 topology.metric_space.basic topology.algebra.uniform_group topology.algebra.ring tactic.linarith noncomputable theory open classical set lattice filter topological_space metric 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_comap theorem uniform_embedding_of_rat : uniform_embedding (coe : ℚ → ℝ) := uniform_embedding_comap rat.cast_injective theorem dense_embedding_of_rat : dense_embedding (coe : ℚ → ℝ) := uniform_embedding_of_rat.dense_embedding $ λ x, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε,ε0, hε⟩ := mem_nhds_iff.1 ht in let ⟨q, h⟩ := exists_rat_near x ε0 in ne_empty_iff_exists_mem.2 ⟨_, hε (mem_ball'.2 h), q, rfl⟩ theorem embedding_of_rat : embedding (coe : ℚ → ℝ) := dense_embedding_of_rat.embedding theorem continuous_of_rat : continuous (coe : ℚ → ℝ) := uniform_continuous_of_rat.continuous theorem real.uniform_continuous_add : uniform_continuous (λp : ℝ × ℝ, p.1 + p.2) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_add_continuous_lemma abs ε0 in ⟨δ, δ0, λ a b h, let ⟨h₁, h₂⟩ := max_lt_iff.1 h in Hδ h₁ h₂⟩ -- TODO(Mario): Find a way to use rat_add_continuous_lemma theorem rat.uniform_continuous_add : uniform_continuous (λp : ℚ × ℚ, p.1 + p.2) := uniform_embedding_of_rat.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 ℝ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [real.dist_eq] using h⟩ theorem rat.uniform_continuous_neg : uniform_continuous (@has_neg.neg ℚ _) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨_, ε0, λ a b h, by rw dist_comm at h; simpa [rat.dist_eq] using h⟩ instance : uniform_add_group ℝ := uniform_add_group.mk' real.uniform_continuous_add real.uniform_continuous_neg instance : uniform_add_group ℚ := uniform_add_group.mk' rat.uniform_continuous_add rat.uniform_continuous_neg instance : topological_add_group ℝ := by apply_instance instance : topological_add_group ℚ := by apply_instance instance : orderable_topology ℚ := induced_orderable_topology _ (λ x y, rat.cast_lt) (@exists_rat_btwn _ _ _) lemma real.is_topological_basis_Ioo_rat : @is_topological_basis ℝ _ (⋃(a b : ℚ) (h : a < b), {Ioo a b}) := is_topological_basis_of_open_of_nhds (by simp [is_open_Ioo] {contextual:=tt}) (assume a v hav hv, let ⟨l, u, hl, hu, h⟩ := (mem_nhds_unbounded (no_top _) (no_bot _)).mp (mem_nhds_sets hv hav), ⟨q, hlq, hqa⟩ := exists_rat_btwn hl, ⟨p, hap, hpu⟩ := exists_rat_btwn hu in ⟨Ioo q p, by simp; exact ⟨q, p, rat.cast_lt.1 $ lt_trans hqa hap, rfl⟩, ⟨hqa, hap⟩, assume a' ⟨hqa', ha'p⟩, h _ (lt_trans hlq hqa') (lt_trans ha'p hpu)⟩) instance : second_countable_topology ℝ := ⟨⟨(⋃(a b : ℚ) (h : a < b), {Ioo a b}), by simp [countable_Union, countable_Union_Prop], real.is_topological_basis_Ioo_rat.2.2⟩⟩ /- TODO(Mario): Prove that these are uniform isomorphisms instead of uniform embeddings lemma uniform_embedding_add_rat {r : ℚ} : uniform_embedding (λp:ℚ, p + r) := _ lemma uniform_embedding_mul_rat {q : ℚ} (hq : q ≠ 0) : uniform_embedding ((*) q) := _ -/ lemma real.uniform_continuous_inv (s : set ℝ) {r : ℝ} (r0 : 0 < r) (H : ∀ x ∈ s, r ≤ abs x) : uniform_continuous (λp:s, p.1⁻¹) := metric.uniform_continuous_iff.2 $ λ ε ε0, let ⟨δ, δ0, Hδ⟩ := rat_inv_continuous_lemma abs ε0 r0 in ⟨δ, δ0, λ a b h, Hδ (H _ a.2) (H _ b.2) h⟩ lemma real.uniform_continuous_abs : uniform_continuous (abs : ℝ → ℝ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b, lt_of_le_of_lt (abs_abs_sub_abs_le_abs_sub _ _)⟩ lemma real.continuous_abs : continuous (abs : ℝ → ℝ) := real.uniform_continuous_abs.continuous lemma rat.uniform_continuous_abs : uniform_continuous (abs : ℚ → ℚ) := metric.uniform_continuous_iff.2 $ λ ε ε0, ⟨ε, ε0, λ a b h, lt_of_le_of_lt (by simpa [rat.dist_eq] using abs_abs_sub_abs_le_abs_sub _ _) h⟩ lemma rat.continuous_abs : continuous (abs : ℚ → ℚ) := rat.uniform_continuous_abs.continuous lemma real.tendsto_inv {r : ℝ} (r0 : r ≠ 0) : tendsto (λq, q⁻¹) (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_continuous_at.mpr $ assume ⟨r, hr⟩, (continuous_iff_continuous_at.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) := metric.uniform_continuous_iff.2 $ λ ε ε0, begin cases no_top (abs x) with y xy, have y0 := lt_of_le_of_lt (abs_nonneg _) xy, refine ⟨_, div_pos ε0 y0, λ a b h, _⟩, rw [real.dist_eq, ← mul_sub, abs_mul, ← mul_div_cancel' ε (ne_of_gt y0)], exact mul_lt_mul' (le_of_lt xy) h (abs_nonneg _) y0 end lemma real.uniform_continuous_mul (s : set (ℝ × ℝ)) {r₁ r₂ : ℝ} (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) := metric.uniform_continuous_iff.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₂⟩ protected lemma real.continuous_mul : continuous (λp : ℝ × ℝ, p.1 * p.2) := continuous_iff_continuous_at.2 $ λ ⟨a₁, a₂⟩, tendsto_of_uniform_continuous_subtype (real.uniform_continuous_mul ({x | abs x < abs a₁ + 1}.prod {x | abs x < abs a₂ + 1}) (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' (abs a₁ + 1)) (real.continuous_abs _ $ is_open_gt' (abs a₂ + 1))) ⟨lt_add_one (abs a₁), lt_add_one (abs a₂)⟩) instance : topological_ring ℝ := { continuous_mul := real.continuous_mul, ..real.topological_add_group } instance : topological_semiring ℝ := by apply_instance 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) := metric.totally_bounded_iff.2 $ λ ε ε0, begin rcases exists_nat_gt ((b - a) / ε) with ⟨n, ba⟩, rw [div_lt_iff' ε0, sub_lt_iff_lt_add'] at ba, let s := (λ i:ℕ, a + ε * i) '' {i:ℕ | i < n}, refine ⟨s, finite_image _ ⟨set.fintype_lt_nat _⟩, λ x h, _⟩, rcases h with ⟨ax, xb⟩, let i : ℕ := ⌊(x - a) / ε⌋.to_nat, have : (i : ℤ) = ⌊(x - a) / ε⌋ := int.to_nat_of_nonneg (floor_nonneg.2 $ le_of_lt (div_pos (sub_pos.2 ax) ε0)), simp, refine ⟨_, ⟨i, _, rfl⟩, _⟩, { rw [← int.coe_nat_lt, this], refine int.cast_lt.1 (lt_of_le_of_lt (floor_le _) _), rw [int.cast_coe_nat, div_lt_iff' ε0, sub_lt_iff_lt_add'], exact lt_trans xb ba }, { rw [real.dist_eq, ← int.cast_coe_nat, this, abs_of_nonneg, ← sub_sub, sub_lt_iff_lt_add'], { have := lt_floor_add_one ((x - a) / ε), rwa [div_lt_iff' ε0, mul_add, mul_one] at this }, { have := floor_le ((x - a) / ε), rwa [ge, sub_nonneg, ← le_sub_iff_add_le', ← le_div_iff' ε0] } } end lemma real.totally_bounded_ball (x ε : ℝ) : totally_bounded (ball x ε) := by rw real.ball_eq_Ioo; apply real.totally_bounded_Ioo lemma real.totally_bounded_Ico (a b : ℝ) : totally_bounded (Ico a b) := let ⟨c, ac⟩ := no_bot a in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨lt_of_lt_of_le ac h₁, h₂⟩) (real.totally_bounded_Ioo c b) lemma real.totally_bounded_Icc (a b : ℝ) : totally_bounded (Icc a b) := let ⟨c, bc⟩ := no_top b in totally_bounded_subset (by exact λ x ⟨h₁, h₂⟩, ⟨h₁, lt_of_le_of_lt h₂ bc⟩) (real.totally_bounded_Ico a c) lemma rat.totally_bounded_Icc (a b : ℚ) : totally_bounded (Icc a b) := begin have := totally_bounded_preimage uniform_embedding_of_rat (real.totally_bounded_Icc a b), rwa (set.ext (λ q, _) : Icc _ _ = _), simp end -- TODO(Mario): Generalize to first-countable uniform spaces? instance : complete_space ℝ := ⟨λ f cf, begin let g : ℕ → {ε:ℝ//ε>0} := λ n, ⟨n.to_pnat'⁻¹, inv_pos (nat.cast_pos.2 n.to_pnat'.pos)⟩, choose S hS hS_dist using show ∀n:ℕ, ∃t ∈ f.sets, ∀ x y ∈ t, dist x y < g n, from assume n, let ⟨t, tf, h⟩ := (metric.cauchy_iff.1 cf).2 (g n).1 (g n).2 in ⟨t, tf, h⟩, let F : ℕ → set ℝ := λn, ⋂i≤n, S i, have hF : ∀n, F n ∈ f.sets := assume n, Inter_mem_sets (finite_le_nat n) (λ i _, hS i), have hF_dist : ∀n, ∀ x y ∈ F n, dist x y < g n := assume n x y hx hy, have F n ⊆ S n := bInter_subset_of_mem (le_refl n), (hS_dist n) _ _ (this hx) (this hy), choose G hG using assume n:ℕ, inhabited_of_mem_sets cf.1 (hF n), have hg : ∀ ε > 0, ∃ n, ∀ j ≥ n, (g j : ℝ) < ε, { intros ε ε0, cases exists_nat_gt ε⁻¹ with n hn, refine ⟨n, λ j nj, _⟩, have hj := lt_of_lt_of_le hn (nat.cast_le.2 nj), have j0 := lt_trans (inv_pos ε0) hj, have jε := (inv_lt j0 ε0).2 hj, rwa ← pnat.to_pnat'_coe (nat.cast_pos.1 j0) at jε }, let c : cau_seq ℝ abs, { refine ⟨λ n, G n, λ ε ε0, _⟩, cases hg _ ε0 with n hn, refine ⟨n, λ j jn, _⟩, have : F j ⊆ F n := bInter_subset_bInter_left (λ i h, @le_trans _ _ i n j h jn), exact lt_trans (hF_dist n _ _ (this (hG j)) (hG n)) (hn _ $ le_refl _) }, refine ⟨cau_seq.lim c, λ s h, _⟩, rcases metric.mem_nhds_iff.1 h with ⟨ε, ε0, hε⟩, cases exists_forall_ge_and (hg _ $ half_pos ε0) (cau_seq.equiv_lim c _ $ half_pos ε0) with n hn, cases hn _ (le_refl _) with h₁ h₂, refine sets_of_superset _ (hF n) (subset.trans _ $ subset.trans (ball_half_subset (G n) h₂) hε), exact λ x h, lt_trans ((hF_dist n) x (G n) h (hG n)) h₁ end⟩ lemma tendsto_coe_nat_real_at_top_iff {f : α → ℕ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, nat.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨n, le_of_lt hn⟩ lemma tendsto_coe_nat_real_at_top_at_top : tendsto (coe : ℕ → ℝ) at_top at_top := tendsto_coe_nat_real_at_top_iff.2 tendsto_id lemma tendsto_coe_int_real_at_top_iff {f : α → ℤ} {l : filter α} : tendsto (λ n, (f n : ℝ)) l at_top ↔ tendsto f l at_top := tendsto_at_top_embedding (assume a₁ a₂, int.cast_le) $ assume r, let ⟨n, hn⟩ := exists_nat_gt r in ⟨(n:ℤ), le_of_lt $ by rwa [int.cast_coe_nat]⟩ lemma tendsto_coe_int_real_at_top_at_top : tendsto (coe : ℤ → ℝ) at_top at_top := tendsto_coe_int_real_at_top_iff.2 tendsto_id section lemma closure_of_rat_image_lt {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q < x}) = {r | ↑q ≤ r} := subset.antisymm ((closure_subset_iff_subset_of_is_closed (is_closed_ge' _)).2 (image_subset_iff.2 $ λ p h, le_of_lt $ (@rat.cast_lt ℝ _ _ _).2 h)) $ λ x hx, mem_closure_iff_nhds.2 $ λ t ht, let ⟨ε, ε0, hε⟩ := metric.mem_nhds_iff.1 ht in let ⟨p, h₁, h₂⟩ := exists_rat_btwn ((lt_add_iff_pos_right x).2 ε0) in ne_empty_iff_exists_mem.2 ⟨_, hε (show abs _ < _, by rwa [abs_of_nonneg (le_of_lt $ sub_pos.2 h₁), sub_lt_iff_lt_add']), p, rat.cast_lt.1 (@lt_of_le_of_lt ℝ _ _ _ _ hx h₁), rfl⟩ /- TODO(Mario): Put these back only if needed later lemma closure_of_rat_image_le_eq {q : ℚ} : closure ((coe:ℚ → ℝ) '' {x | q ≤ x}) = {r | ↑q ≤ r} := _ lemma closure_of_rat_image_le_le_eq {a b : ℚ} (hab : a ≤ b) : closure (of_rat '' {q:ℚ | a ≤ q ∧ q ≤ b}) = {r:ℝ | of_rat a ≤ r ∧ r ≤ of_rat b} := _-/ lemma compact_Icc {a b : ℝ} : compact (Icc a b) := compact_of_totally_bounded_is_closed (real.totally_bounded_Icc a b) (is_closed_inter (is_closed_ge' a) (is_closed_le' b)) instance : proper_space ℝ := { compact_ball := λx r, by rw closed_ball_Icc; apply compact_Icc } open real lemma real.intermediate_value {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x))) (ha : f a ≤ t) (hb : t ≤ f b) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let x := real.Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} in have hx₁ : ∃ y, ∀ g ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}, g ≤ y := ⟨b, λ _ h, h.2.2⟩, have hx₂ : ∃ y, y ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} := ⟨a, ha, le_refl _, hab⟩, have hax : a ≤ x, from le_Sup _ hx₁ ⟨ha, le_refl _, hab⟩, have hxb : x ≤ b, from (Sup_le _ hx₂ hx₁).2 (λ _ h, h.2.2), ⟨x, hax, hxb, eq_of_forall_dist_le $ λ ε ε0, let ⟨δ, hδ0, hδ⟩ := metric.tendsto_nhds_nhds.1 (hf _ hax hxb) ε ε0 in (le_total t (f x)).elim (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonneg (sub_nonneg.2 h)] at hfε, refine mt (Sup_le {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} hx₂ hx₁).2 (not_le_of_gt (sub_lt_self x (half_pos hδ0))) (λ g hg, le_of_not_gt (λ hgδ, not_lt_of_ge hg.1 (lt_trans (lt_sub.1 hfε) (sub_lt_of_sub_lt (lt_of_le_of_lt (le_abs_self _) _))))), rw abs_sub, exact hδ (abs_sub_lt_iff.2 ⟨lt_of_le_of_lt (sub_nonpos.2 (le_Sup _ hx₁ hg)) hδ0, by simp only [x] at *; linarith⟩) end) (λ h, le_of_not_gt $ λ hfε, begin rw [dist_eq, abs_of_nonpos (sub_nonpos.2 h)] at hfε, exact mt (le_Sup {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b}) (λ h : ∀ k, k ∈ {x | f x ≤ t ∧ a ≤ x ∧ x ≤ b} → k ≤ x, not_le_of_gt ((lt_add_iff_pos_left x).2 (half_pos hδ0)) (h _ ⟨le_trans (le_sub_iff_add_le.2 (le_trans (le_abs_self _) (le_of_lt (hδ $ by rw [dist_eq, add_sub_cancel, abs_of_nonneg (le_of_lt (half_pos hδ0))]; exact half_lt_self hδ0)))) (by linarith), le_trans hax (le_of_lt ((lt_add_iff_pos_left _).2 (half_pos hδ0))), le_of_not_gt (λ hδy, not_lt_of_ge hb (lt_of_le_of_lt (show f b ≤ f b - f x - ε + t, by linarith) (add_lt_of_neg_of_le (sub_neg_of_lt (lt_of_le_of_lt (le_abs_self _) (@hδ b (abs_sub_lt_iff.2 ⟨by simp only [x] at *; linarith, by linarith⟩)))) (le_refl _))))⟩)) hx₁ end)⟩ lemma real.intermediate_value' {f : ℝ → ℝ} {a b t : ℝ} (hf : ∀ x, a ≤ x → x ≤ b → tendsto f (nhds x) (nhds (f x))) (ha : t ≤ f a) (hb : f b ≤ t) (hab : a ≤ b) : ∃ x : ℝ, a ≤ x ∧ x ≤ b ∧ f x = t := let ⟨x, hx₁, hx₂, hx₃⟩ := @real.intermediate_value (λ x, - f x) a b (-t) (λ x hax hxb, tendsto_neg (hf x hax hxb)) (neg_le_neg ha) (neg_le_neg hb) hab in ⟨x, hx₁, hx₂, neg_inj hx₃⟩ lemma real.bounded_iff_bdd_below_bdd_above {s : set ℝ} : bounded s ↔ bdd_below s ∧ bdd_above s := ⟨begin assume bdd, rcases (bounded_iff_subset_ball 0).1 bdd with ⟨r, hr⟩, -- hr : s ⊆ closed_ball 0 r rw closed_ball_Icc at hr, -- hr : s ⊆ Icc (0 - r) (0 + r) exact ⟨⟨-r, λy hy, by simpa using (hr hy).1⟩, ⟨r, λy hy, by simpa using (hr hy).2⟩⟩ end, begin rintros ⟨⟨m, hm⟩, ⟨M, hM⟩⟩, have I : s ⊆ Icc m M := λx hx, ⟨hm x hx, hM x hx⟩, have : Icc m M = closed_ball ((m+M)/2) ((M-m)/2) := by rw closed_ball_Icc; congr; ring, rw this at I, exact bounded.subset I bounded_closed_ball end⟩ end
8de07e232ab0e9637a553849fb2cc38d86a50e8f
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/analysis/topology.lean
ed908dbf875852520bd6ae53e6cfd5e7c59b6680
[ "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
8,273
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Computational realization of topological spaces (experimental). -/ import topology.bases import data.analysis.filter 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*} namespace ctop section variables (F : ctop α σ) instance : has_coe_to_fun (ctop α σ) := ⟨_, 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 protected def ctop.to_realizer (F : ctop α σ) : @ctop.realizer _ F.to_topsp := @ctop.realizer.mk _ F.to_topsp σ F rfl 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 α] 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⟩⟩ 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 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] theorem nhds_σ (m : α → β) (F : realizer α) (a : α) : (F.nhds a).σ = {s : F.σ // a ∈ F.F s} := rfl @[simp] theorem nhds_F (m : α → β) (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 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⟩ def compact.realizer [topological_space α] (R : realizer α) (s : set α) := ∀ {f : filter α} (F : f.realizer) (x : F.σ), f ≠ ⊥ → F.F x ⊆ s → {a // a∈s ∧ 𝓝 a ⊓ f ≠ ⊥}
3738d4b8a97e57a6bd6c28dec6989362ac754601
57fdc8de88f5ea3bfde4325e6ecd13f93a274ab5
/data/nat/choose.lean
7c043a2a7cecf86dae5114010234778481ba431e
[ "Apache-2.0" ]
permissive
louisanu/mathlib
11f56f2d40dc792bc05ee2f78ea37d73e98ecbfe
2bd5e2159d20a8f20d04fc4d382e65eea775ed39
refs/heads/master
1,617,706,993,439
1,523,163,654,000
1,523,163,654,000
124,519,997
0
0
Apache-2.0
1,520,588,283,000
1,520,588,283,000
null
UTF-8
Lean
false
false
3,716
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes Mostly based on Jeremy Avigad's choose file in lean 2 -/ import data.nat.basic open nat def choose : ℕ → ℕ → ℕ | _ 0 := 1 | 0 (k + 1) := 0 | (n + 1) (k + 1) := choose n k + choose n (succ k) @[simp] lemma choose_zero_right (n : ℕ) : choose n 0 = 1 := by cases n; refl @[simp] lemma choose_zero_succ (k : ℕ) : choose 0 (succ k) = 0 := rfl lemma choose_succ_succ (n k : ℕ) : choose (succ n) (succ k) = choose n k + choose n (succ k) := rfl lemma choose_eq_zero_of_lt : ∀ {n k}, n < k → choose n k = 0 | _ 0 hk := absurd hk dec_trivial | 0 (k + 1) hk := choose_zero_succ _ | (n + 1) (k + 1) hk := have hnk : n < k, from lt_of_succ_lt_succ hk, have hnk1 : n < k + 1, from lt_of_succ_lt hk, by rw [choose_succ_succ, choose_eq_zero_of_lt hnk, choose_eq_zero_of_lt hnk1] @[simp] lemma choose_self (n : ℕ) : choose n n = 1 := by induction n; simp [*, choose, choose_eq_zero_of_lt (lt_succ_self _)] @[simp] lemma choose_succ_self (n : ℕ) : choose n (succ n) = 0 := choose_eq_zero_of_lt (lt_succ_self _) @[simp] lemma choose_one_right (n : ℕ) : choose n 1 = n := by induction n; simp [*, choose] lemma choose_pos : ∀ {n k}, k ≤ n → 0 < choose n k | 0 _ hk := by rw [eq_zero_of_le_zero hk]; exact dec_trivial | (n + 1) 0 hk := by simp; exact dec_trivial | (n + 1) (k + 1) hk := by rw choose_succ_succ; exact add_pos_of_pos_of_nonneg (choose_pos (le_of_succ_le_succ hk)) (zero_le _) lemma succ_mul_choose_eq : ∀ n k, succ n * choose n k = choose (succ n) (succ k) * succ k | 0 0 := dec_trivial | 0 (k + 1) := by simp [choose] | (n + 1) 0 := by simp | (n + 1) (k + 1) := by rw [choose_succ_succ (succ n) (succ k), add_mul, ←succ_mul_choose_eq, mul_succ, ←succ_mul_choose_eq, add_right_comm, ←mul_add, ←choose_succ_succ, ←succ_mul] lemma choose_mul_fact_mul_fact : ∀ {n k}, k ≤ n → choose n k * fact k * fact (n - k) = fact n | 0 _ hk := by simp [eq_zero_of_le_zero hk] | (n + 1) 0 hk := by simp | (n + 1) (succ k) hk := begin cases lt_or_eq_of_le hk with hk₁ hk₁, { have h : choose n k * fact (succ k) * fact (n - k) = succ k * fact n := by rw ← choose_mul_fact_mul_fact (le_of_succ_le_succ hk); simp [fact_succ, mul_comm, mul_left_comm], have h₁ : fact (n - k) = (n - k) * fact (n - succ k) := by rw [← succ_sub_succ, succ_sub (le_of_lt_succ hk₁), fact_succ], have h₂ : choose n (succ k) * fact (succ k) * ((n - k) * fact (n - succ k)) = (n - k) * fact n := by rw ← choose_mul_fact_mul_fact (le_of_lt_succ hk₁); simp [fact_succ, mul_comm, mul_left_comm, mul_assoc], have h₃ : k * fact n ≤ n * fact n := mul_le_mul_right _ (le_of_succ_le_succ hk), rw [choose_succ_succ, add_mul, add_mul, succ_sub_succ, h, h₁, h₂, ← add_one, add_mul, nat.mul_sub_right_distrib, fact_succ, ← nat.add_sub_assoc h₃, add_assoc, ← add_mul, nat.add_sub_cancel_left, add_comm] }, { simp [hk₁, mul_comm, choose, nat.sub_self] } end theorem choose_eq_fact_div_fact {n k : ℕ} (hk : k ≤ n) : choose n k = fact n / (fact k * fact (n - k)) := begin have : fact n = choose n k * (fact k * fact (n - k)) := by rw ← mul_assoc; exact (choose_mul_fact_mul_fact hk).symm, exact (nat.div_eq_of_eq_mul_left (mul_pos (fact_pos _) (fact_pos _)) this).symm end theorem fact_mul_fact_dvd_fact {n k : ℕ} (hk : k ≤ n) : fact k * fact (n - k) ∣ fact n := by rw [←choose_mul_fact_mul_fact hk, mul_assoc]; exact dvd_mul_left _ _
2ef9d7b6ce1134ce6b9fe43e51e312761872dfdf
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/topology/category/Profinite/default.lean
dffbcfcaa7973978ff2b90fdae1041e79cb62f5c
[ "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
9,703
lean
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Calle Sönne -/ import topology.category.CompHaus import topology.connected import topology.subset_properties import category_theory.adjunction.reflective import category_theory.monad.limits import category_theory.Fintype /-! # The category of Profinite Types We construct the category of profinite topological spaces, often called profinite sets -- perhaps they could be called profinite types in Lean. The type of profinite topological spaces is called `Profinite`. It has a category instance and is a fully faithful subcategory of `Top`. The fully faithful functor is called `Profinite_to_Top`. ## Implementation notes A profinite type is defined to be a topological space which is compact, Hausdorff and totally disconnected. ## TODO 0. Link to category of projective limits of finite discrete sets. 1. finite coproducts 2. Clausen/Scholze topology on the category `Profinite`. ## Tags profinite -/ universe variable u open category_theory /-- The type of profinite topological spaces. -/ structure Profinite := (to_Top : Top) [is_compact : compact_space to_Top] [is_t2 : t2_space to_Top] [is_totally_disconnected : totally_disconnected_space to_Top] namespace Profinite /-- Construct a term of `Profinite` from a type endowed with the structure of a compact, Hausdorff and totally disconnected topological space. -/ def of (X : Type*) [topological_space X] [compact_space X] [t2_space X] [totally_disconnected_space X] : Profinite := ⟨⟨X⟩⟩ instance : inhabited Profinite := ⟨Profinite.of pempty⟩ instance category : category Profinite := induced_category.category to_Top instance concrete_category : concrete_category Profinite := induced_category.concrete_category _ instance has_forget₂ : has_forget₂ Profinite Top := induced_category.has_forget₂ _ instance : has_coe_to_sort Profinite := ⟨Type*, λ X, X.to_Top⟩ instance {X : Profinite} : compact_space X := X.is_compact instance {X : Profinite} : t2_space X := X.is_t2 instance {X : Profinite} : totally_disconnected_space X := X.is_totally_disconnected @[simp] lemma coe_to_Top {X : Profinite} : (X.to_Top : Type*) = X := rfl @[simp] lemma coe_id (X : Profinite) : (𝟙 X : X → X) = id := rfl @[simp] lemma coe_comp {X Y Z : Profinite} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g : X → Z) = g ∘ f := rfl end Profinite /-- The fully faithful embedding of `Profinite` in `Top`. -/ @[simps, derive [full, faithful]] def Profinite_to_Top : Profinite ⥤ Top := forget₂ _ _ /-- The fully faithful embedding of `Profinite` in `CompHaus`. -/ @[simps] def Profinite.to_CompHaus : Profinite ⥤ CompHaus := { obj := λ X, { to_Top := X.to_Top }, map := λ _ _ f, f } instance : full Profinite.to_CompHaus := { preimage := λ _ _ f, f } instance : faithful Profinite.to_CompHaus := {} @[simp] lemma Profinite.to_CompHaus_to_Top : Profinite.to_CompHaus ⋙ CompHaus_to_Top = Profinite_to_Top := rfl section Profinite local attribute [instance] connected_component_setoid /-- (Implementation) The object part of the connected_components functor from compact Hausdorff spaces to Profinite spaces, given by quotienting a space by its connected components. See: https://stacks.math.columbia.edu/tag/0900 -/ -- Without explicit universe annotations here, Lean introduces two universe variables and -- unhelpfully defines a function `CompHaus.{max u₁ u₂} → Profinite.{max u₁ u₂}`. def CompHaus.to_Profinite_obj (X : CompHaus.{u}) : Profinite.{u} := { to_Top := { α := connected_components X.to_Top.α }, is_compact := quotient.compact_space, is_t2 := connected_components.t2, is_totally_disconnected := connected_components.totally_disconnected_space } /-- (Implementation) The bijection of homsets to establish the reflective adjunction of Profinite spaces in compact Hausdorff spaces. -/ def Profinite.to_CompHaus_equivalence (X : CompHaus.{u}) (Y : Profinite.{u}) : (CompHaus.to_Profinite_obj X ⟶ Y) ≃ (X ⟶ Profinite.to_CompHaus.obj Y) := { to_fun := λ f, { to_fun := f.1 ∘ quotient.mk, continuous_to_fun := continuous.comp f.2 (continuous_quotient_mk) }, inv_fun := λ g, { to_fun := continuous.connected_components_lift g.2, continuous_to_fun := continuous.connected_components_lift_continuous g.2}, left_inv := λ f, continuous_map.ext $ λ x, quotient.induction_on x $ λ a, rfl, right_inv := λ f, continuous_map.ext $ λ x, rfl } /-- The connected_components functor from compact Hausdorff spaces to profinite spaces, left adjoint to the inclusion functor. -/ def CompHaus.to_Profinite : CompHaus ⥤ Profinite := adjunction.left_adjoint_of_equiv Profinite.to_CompHaus_equivalence (λ _ _ _ _ _, rfl) lemma CompHaus.to_Profinite_obj' (X : CompHaus) : ↥(CompHaus.to_Profinite.obj X) = connected_components X.to_Top.α := rfl /-- Finite types are given the discrete topology. -/ def Fintype.discrete_topology (A : Fintype) : topological_space A := ⊥ section discrete_topology local attribute [instance] Fintype.discrete_topology /-- The natural functor from `Fintype` to `Profinite`, endowing a finite type with the discrete topology. -/ @[simps] def Fintype.to_Profinite : Fintype ⥤ Profinite := { obj := λ A, Profinite.of A, map := λ _ _ f, ⟨f⟩ } end discrete_topology end Profinite namespace Profinite /-- An explicit limit cone for a functor `F : J ⥤ Profinite`, defined in terms of `Top.limit_cone`. -/ def limit_cone {J : Type u} [small_category J] (F : J ⥤ Profinite.{u}) : limits.cone F := { X := { to_Top := CompHaus_to_Top.obj (CompHaus.limit_cone (F ⋙ Profinite.to_CompHaus)).X, is_compact := by { dsimp [CompHaus_to_Top], apply_instance }, is_t2 := by { dsimp [CompHaus_to_Top], apply_instance }, is_totally_disconnected := by { dsimp [CompHaus_to_Top, CompHaus.limit_cone, Profinite.to_CompHaus, Top.limit_cone], apply_instance } }, π := { app := λ j, (CompHaus.limit_cone (F ⋙ Profinite.to_CompHaus)).π.app j } } /-- The limit cone `Profinite.limit_cone F` is indeed a limit cone. -/ def limit_cone_is_limit {J : Type u} [small_category J] (F : J ⥤ Profinite.{u}) : limits.is_limit (limit_cone F) := { lift := λ S, (CompHaus.limit_cone_is_limit (F ⋙ Profinite.to_CompHaus)).lift (Profinite.to_CompHaus.map_cone S), uniq' := λ S m h, (CompHaus.limit_cone_is_limit _).uniq (Profinite.to_CompHaus.map_cone S) _ h } /-- The adjunction between CompHaus.to_Profinite and Profinite.to_CompHaus -/ def to_Profinite_adj_to_CompHaus : CompHaus.to_Profinite ⊣ Profinite.to_CompHaus := adjunction.adjunction_of_equiv_left _ _ /-- The category of profinite sets is reflective in the category of compact hausdroff spaces -/ instance to_CompHaus.reflective : reflective Profinite.to_CompHaus := { to_is_right_adjoint := ⟨CompHaus.to_Profinite, Profinite.to_Profinite_adj_to_CompHaus⟩ } noncomputable instance to_CompHaus.creates_limits : creates_limits Profinite.to_CompHaus := monadic_creates_limits _ noncomputable instance to_Top.reflective : reflective (Profinite_to_Top : Profinite ⥤ Top) := reflective.comp Profinite.to_CompHaus CompHaus_to_Top noncomputable instance to_Top.creates_limits : creates_limits Profinite_to_Top := monadic_creates_limits _ instance has_limits : limits.has_limits Profinite := has_limits_of_has_limits_creates_limits Profinite_to_Top instance has_colimits : limits.has_colimits Profinite := has_colimits_of_reflective to_CompHaus noncomputable instance forget_preserves_limits : limits.preserves_limits (forget Profinite) := by apply limits.comp_preserves_limits Profinite_to_Top (forget _) variables {X Y : Profinite.{u}} (f : X ⟶ Y) /-- Any morphism of profinite spaces is a closed map. -/ lemma is_closed_map : is_closed_map f := show is_closed_map (Profinite.to_CompHaus.map f), from CompHaus.is_closed_map _ /-- Any continuous bijection of profinite spaces induces an isomorphism. -/ lemma is_iso_of_bijective (bij : function.bijective f) : is_iso f := begin haveI := CompHaus.is_iso_of_bijective (Profinite.to_CompHaus.map f) bij, exact is_iso_of_fully_faithful Profinite.to_CompHaus _ end /-- Any continuous bijection of profinite spaces induces an isomorphism. -/ noncomputable def iso_of_bijective (bij : function.bijective f) : X ≅ Y := by letI := Profinite.is_iso_of_bijective f bij; exact as_iso f instance forget_reflects_isomorphisms : reflects_isomorphisms (forget Profinite) := ⟨by introsI A B f hf; exact Profinite.is_iso_of_bijective _ ((is_iso_iff_bijective ⇑f).mp hf)⟩ /-- Construct an isomorphism from a homeomorphism. -/ @[simps hom inv] def iso_of_homeo (f : X ≃ₜ Y) : X ≅ Y := { hom := ⟨f, f.continuous⟩, inv := ⟨f.symm, f.symm.continuous⟩, hom_inv_id' := by { ext x, exact f.symm_apply_apply x }, inv_hom_id' := by { ext x, exact f.apply_symm_apply x } } /-- Construct a homeomorphism from an isomorphism. -/ @[simps] def homeo_of_iso (f : X ≅ Y) : X ≃ₜ Y := { to_fun := f.hom, inv_fun := f.inv, left_inv := λ x, by { change (f.hom ≫ f.inv) x = x, rw [iso.hom_inv_id, coe_id, id.def] }, right_inv := λ x, by { change (f.inv ≫ f.hom) x = x, rw [iso.inv_hom_id, coe_id, id.def] }, continuous_to_fun := f.hom.continuous, continuous_inv_fun := f.inv.continuous } /-- The equivalence between isomorphisms in `Profinite` and homeomorphisms of topological spaces. -/ @[simps] def iso_equiv_homeo : (X ≅ Y) ≃ (X ≃ₜ Y) := { to_fun := homeo_of_iso, inv_fun := iso_of_homeo, left_inv := λ f, by { ext, refl }, right_inv := λ f, by { ext, refl } } end Profinite
82f9230d2882568d48e6468d72513a3013994f6e
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/abelian/homology.lean
ef09c0ffb79eedd29f16ec599ab9145a304b8754
[ "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
9,701
lean
/- Copyright (c) 2022 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz -/ import category_theory.abelian.exact import category_theory.abelian.pseudoelements /-! The object `homology f g w`, where `w : f ≫ g = 0`, can be identified with either a cokernel or a kernel. The isomorphism with a cokernel is `homology_iso_cokernel_lift`, which was obtained elsewhere. In the case of an abelian category, this file shows the isomorphism with a kernel as well. We use these isomorphisms to obtain the analogous api for `homology`: - `homology.ι` is the map from `homology f g w` into the cokernel of `f`. - `homology.π'` is the map from `kernel g` to `homology f g w`. - `homology.desc'` constructs a morphism from `homology f g w`, when it is viewed as a cokernel. - `homology.lift` constructs a morphism to `homology f g w`, when it is viewed as a kernel. - Various small lemmas are proved as well, mimicking the API for (co)kernels. With these definitions and lemmas, the isomorphisms between homology and a (co)kernel need not be used directly. -/ open category_theory.limits open category_theory noncomputable theory universes v u variables {A : Type u} [category.{v} A] [abelian A] variables {X Y Z : A} (f : X ⟶ Y) (g : Y ⟶ Z) (w : f ≫ g = 0) namespace category_theory.abelian /-- The cokernel of `kernel.lift g f w`. This is isomorphic to `homology f g w`. See `homology_iso_cokernel_lift`. -/ abbreviation homology_c : A := cokernel (kernel.lift g f w) /-- The kernel of `cokernel.desc f g w`. This is isomorphic to `homology f g w`. See `homology_iso_kernel_desc`. -/ abbreviation homology_k : A := kernel (cokernel.desc f g w) /-- The canonical map from `homology_c` to `homology_k`. This is an isomorphism, and it is used in obtaining the API for `homology f g w` in the bottom of this file. -/ abbreviation homology_c_to_k : homology_c f g w ⟶ homology_k f g w := cokernel.desc _ (kernel.lift _ (kernel.ι _ ≫ cokernel.π _) (by simp)) begin apply limits.equalizer.hom_ext, simp, end local attribute [instance] pseudoelement.hom_to_fun pseudoelement.has_zero instance : mono (homology_c_to_k f g w) := begin apply pseudoelement.mono_of_zero_of_map_zero, intros a ha, obtain ⟨a,rfl⟩ := pseudoelement.pseudo_surjective_of_epi (cokernel.π (kernel.lift g f w)) a, apply_fun (kernel.ι (cokernel.desc f g w)) at ha, simp only [←pseudoelement.comp_apply, cokernel.π_desc, kernel.lift_ι, pseudoelement.apply_zero] at ha, simp only [pseudoelement.comp_apply] at ha, obtain ⟨b,hb⟩ : ∃ b, f b = _ := (pseudoelement.pseudo_exact_of_exact (exact_cokernel f)).2 _ ha, suffices : ∃ c, kernel.lift g f w c = a, { obtain ⟨c,rfl⟩ := this, simp [← pseudoelement.comp_apply] }, use b, apply_fun kernel.ι g, swap, { apply pseudoelement.pseudo_injective_of_mono }, simpa [← pseudoelement.comp_apply] end instance : epi (homology_c_to_k f g w) := begin apply pseudoelement.epi_of_pseudo_surjective, intros a, let b := kernel.ι (cokernel.desc f g w) a, obtain ⟨c,hc⟩ : ∃ c, cokernel.π f c = b, apply pseudoelement.pseudo_surjective_of_epi (cokernel.π f), have : g c = 0, { dsimp [b] at hc, rw [(show g = cokernel.π f ≫ cokernel.desc f g w, by simp), pseudoelement.comp_apply, hc], simp [← pseudoelement.comp_apply] }, obtain ⟨d,hd⟩ : ∃ d, kernel.ι g d = c, { apply (pseudoelement.pseudo_exact_of_exact exact_kernel_ι).2 _ this }, use cokernel.π (kernel.lift g f w) d, apply_fun kernel.ι (cokernel.desc f g w), swap, { apply pseudoelement.pseudo_injective_of_mono }, simp only [←pseudoelement.comp_apply, cokernel.π_desc, kernel.lift_ι], simp only [pseudoelement.comp_apply, hd, hc], end instance (w : f ≫ g = 0) : is_iso (homology_c_to_k f g w) := is_iso_of_mono_of_epi _ end category_theory.abelian /-- The homology associated to `f` and `g` is isomorphic to a kernel. -/ def homology_iso_kernel_desc : homology f g w ≅ kernel (cokernel.desc f g w) := homology_iso_cokernel_lift _ _ _ ≪≫ as_iso (category_theory.abelian.homology_c_to_k _ _ _) namespace homology -- `homology.π` is taken /-- The canonical map from the kernel of `g` to the homology of `f` and `g`. -/ def π' : kernel g ⟶ homology f g w := cokernel.π _ ≫ (homology_iso_cokernel_lift _ _ _).inv /-- The canonical map from the homology of `f` and `g` to the cokernel of `f`. -/ def ι : homology f g w ⟶ cokernel f := (homology_iso_kernel_desc _ _ _).hom ≫ kernel.ι _ /-- Obtain a morphism from the homology, given a morphism from the kernel. -/ def desc' {W : A} (e : kernel g ⟶ W) (he : kernel.lift g f w ≫ e = 0) : homology f g w ⟶ W := (homology_iso_cokernel_lift _ _ _).hom ≫ cokernel.desc _ e he /-- Obtain a moprhism to the homology, given a morphism to the kernel. -/ def lift {W : A} (e : W ⟶ cokernel f) (he : e ≫ cokernel.desc f g w = 0) : W ⟶ homology f g w := kernel.lift _ e he ≫ (homology_iso_kernel_desc _ _ _).inv @[simp, reassoc] lemma π'_desc' {W : A} (e : kernel g ⟶ W) (he : kernel.lift g f w ≫ e = 0) : π' f g w ≫ desc' f g w e he = e := by { dsimp [π', desc'], simp } @[simp, reassoc] lemma lift_ι {W : A} (e : W ⟶ cokernel f) (he : e ≫ cokernel.desc f g w = 0) : lift f g w e he ≫ ι _ _ _ = e := by { dsimp [ι, lift], simp } @[simp, reassoc] lemma condition_π' : kernel.lift g f w ≫ π' f g w = 0 := by { dsimp [π'], simp } @[simp, reassoc] lemma condition_ι : ι f g w ≫ cokernel.desc f g w = 0 := by { dsimp [ι], simp } @[ext] lemma hom_from_ext {W : A} (a b : homology f g w ⟶ W) (h : π' f g w ≫ a = π' f g w ≫ b) : a = b := begin dsimp [π'] at h, apply_fun (λ e, (homology_iso_cokernel_lift f g w).inv ≫ e), swap, { intros i j hh, apply_fun (λ e, (homology_iso_cokernel_lift f g w).hom ≫ e) at hh, simpa using hh }, simp only [category.assoc] at h, exact coequalizer.hom_ext h, end @[ext] lemma hom_to_ext {W : A} (a b : W ⟶ homology f g w) (h : a ≫ ι f g w = b ≫ ι f g w) : a = b := begin dsimp [ι] at h, apply_fun (λ e, e ≫ (homology_iso_kernel_desc f g w).hom), swap, { intros i j hh, apply_fun (λ e, e ≫ (homology_iso_kernel_desc f g w).inv) at hh, simpa using hh }, simp only [← category.assoc] at h, exact equalizer.hom_ext h, end @[simp, reassoc] lemma π'_ι : π' f g w ≫ ι f g w = kernel.ι _ ≫ cokernel.π _ := by { dsimp [π', ι, homology_iso_kernel_desc], simp } @[simp, reassoc] lemma π'_eq_π : (kernel_subobject_iso _).hom ≫ π' f g w = π _ _ _ := begin dsimp [π', homology_iso_cokernel_lift], simp only [← category.assoc], rw iso.comp_inv_eq, dsimp [π, homology_iso_cokernel_image_to_kernel'], simp, end section variables {X' Y' Z' : A} (f' : X' ⟶ Y') (g' : Y' ⟶ Z') (w' : f' ≫ g' = 0) @[simp, reassoc] lemma π'_map (α β h) : π' _ _ _ ≫ map w w' α β h = kernel.map _ _ α.right β.right (by simp [h,β.w.symm]) ≫ π' _ _ _ := begin apply_fun (λ e, (kernel_subobject_iso _).hom ≫ e), swap, { intros i j hh, apply_fun (λ e, (kernel_subobject_iso _).inv ≫ e) at hh, simpa using hh }, dsimp [map], simp only [π'_eq_π_assoc], dsimp [π], simp only [cokernel.π_desc], rw [← iso.inv_comp_eq, ← category.assoc], have : (limits.kernel_subobject_iso g).inv ≫ limits.kernel_subobject_map β = kernel.map _ _ β.left β.right β.w.symm ≫ (kernel_subobject_iso _).inv, { rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv], ext, dsimp, simp }, rw this, simp only [category.assoc], dsimp [π', homology_iso_cokernel_lift], simp only [cokernel_iso_of_eq_inv_comp_desc, cokernel.π_desc_assoc], congr' 1, { congr, exact h.symm }, { rw [iso.inv_comp_eq, ← category.assoc, iso.eq_comp_inv], dsimp [homology_iso_cokernel_image_to_kernel'], simp } end lemma map_eq_desc'_lift_left (α β h) : map w w' α β h = homology.desc' _ _ _ (homology.lift _ _ _ (kernel.ι _ ≫ β.left ≫ cokernel.π _) (by simp)) (by { ext, simp only [←h, category.assoc, zero_comp, lift_ι, kernel.lift_ι_assoc], erw ← reassoc_of α.w, simp } ) := begin apply homology.hom_from_ext, simp only [π'_map, π'_desc'], dsimp [π', lift], rw iso.eq_comp_inv, dsimp [homology_iso_kernel_desc], ext, simp [h], end lemma map_eq_lift_desc'_left (α β h) : map w w' α β h = homology.lift _ _ _ (homology.desc' _ _ _ (kernel.ι _ ≫ β.left ≫ cokernel.π _) (by { simp only [kernel.lift_ι_assoc, ← h], erw ← reassoc_of α.w, simp })) (by { ext, simp }) := by { rw map_eq_desc'_lift_left, ext, simp } lemma map_eq_desc'_lift_right (α β h) : map w w' α β h = homology.desc' _ _ _ (homology.lift _ _ _ (kernel.ι _ ≫ α.right ≫ cokernel.π _) (by simp [h])) (by { ext, simp only [category.assoc, zero_comp, lift_ι, kernel.lift_ι_assoc], erw ← reassoc_of α.w, simp } ) := by { rw map_eq_desc'_lift_left, ext, simp [h] } lemma map_eq_lift_desc'_right (α β h) : map w w' α β h = homology.lift _ _ _ (homology.desc' _ _ _ (kernel.ι _ ≫ α.right ≫ cokernel.π _) (by { simp only [kernel.lift_ι_assoc], erw ← reassoc_of α.w, simp })) (by { ext, simp [h] }) := by { rw map_eq_desc'_lift_right, ext, simp } @[simp, reassoc] lemma map_ι (α β h) : map w w' α β h ≫ ι f' g' w' = ι f g w ≫ cokernel.map f f' α.left β.left (by simp [h, β.w.symm]) := begin rw [map_eq_lift_desc'_left, lift_ι], ext, simp only [← category.assoc], rw [π'_ι, π'_desc', category.assoc, category.assoc, cokernel.π_desc], end end end homology
0aa70f23b298ef72ee5d1e8776ec9da8489f756f
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/listtostring.lean
4e13fdb545ea9d3df73a42938b2563106600da4e
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
78
lean
#eval toString (List.range 100000) #eval toString (List.range 100000).toArray
74d71c79e3e739d3afa12741386ecabd51c1171f
491068d2ad28831e7dade8d6dff871c3e49d9431
/tests/lean/extra/show_goal.lean
47b83ab3d1a68d8bf13ddecfb88d8b2591127b5b
[ "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
561
lean
import data.nat open nat example (a b c d : nat) : a + b = 0 → b = 0 → c + 1 + a = 1 → d = c - 1 → d = 0 := begin intro h₁ h₂, have aeq0 : a = 0, begin rewrite h₂ at h₁, exact h₁ end, intro h₃ h₄, have deq0 : d = 0, begin have ceq : c = 0, begin rewrite aeq0 at h₃, rewrite add_zero at h₃, rewrite add_succ at h₃, rewrite add_zero at h₃, injection h₃, assumption end, rewrite ceq at h₄, repeat (esimp [sub, pred] at h₄), assumption end, exact deq0 end
c421526cce33336b4150ff4a8f7b10542f18ac20
9dc8cecdf3c4634764a18254e94d43da07142918
/src/field_theory/abel_ruffini.lean
2ecab32a062ae96b854c89c3b6b41766aa726a61
[ "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
17,340
lean
/- Copyright (c) 2020 Thomas Browning and Patrick Lutz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning, Patrick Lutz -/ import group_theory.solvable import field_theory.polynomial_galois_group import ring_theory.roots_of_unity /-! # The Abel-Ruffini Theorem This file proves one direction of the Abel-Ruffini theorem, namely that if an element is solvable by radicals, then its minimal polynomial has solvable Galois group. ## Main definitions * `solvable_by_rad F E` : the intermediate field of solvable-by-radicals elements ## Main results * the Abel-Ruffini Theorem `solvable_by_rad.is_solvable'` : An irreducible polynomial with a root that is solvable by radicals has a solvable Galois group. -/ noncomputable theory open_locale classical polynomial open polynomial intermediate_field section abel_ruffini variables {F : Type*} [field F] {E : Type*} [field E] [algebra F E] lemma gal_zero_is_solvable : is_solvable (0 : F[X]).gal := by apply_instance lemma gal_one_is_solvable : is_solvable (1 : F[X]).gal := by apply_instance lemma gal_C_is_solvable (x : F) : is_solvable (C x).gal := by apply_instance lemma gal_X_is_solvable : is_solvable (X : F[X]).gal := by apply_instance lemma gal_X_sub_C_is_solvable (x : F) : is_solvable (X - C x).gal := by apply_instance lemma gal_X_pow_is_solvable (n : ℕ) : is_solvable (X ^ n : F[X]).gal := by apply_instance lemma gal_mul_is_solvable {p q : F[X]} (hp : is_solvable p.gal) (hq : is_solvable q.gal) : is_solvable (p * q).gal := solvable_of_solvable_injective (gal.restrict_prod_injective p q) lemma gal_prod_is_solvable {s : multiset F[X]} (hs : ∀ p ∈ s, is_solvable (gal p)) : is_solvable s.prod.gal := begin apply multiset.induction_on' s, { exact gal_one_is_solvable }, { intros p t hps hts ht, rw [multiset.insert_eq_cons, multiset.prod_cons], exact gal_mul_is_solvable (hs p hps) ht }, end lemma gal_is_solvable_of_splits {p q : F[X]} (hpq : fact (p.splits (algebra_map F q.splitting_field))) (hq : is_solvable q.gal) : is_solvable p.gal := begin haveI : is_solvable (q.splitting_field ≃ₐ[F] q.splitting_field) := hq, exact solvable_of_surjective (alg_equiv.restrict_normal_hom_surjective q.splitting_field), end lemma gal_is_solvable_tower (p q : F[X]) (hpq : p.splits (algebra_map F q.splitting_field)) (hp : is_solvable p.gal) (hq : is_solvable (q.map (algebra_map F p.splitting_field)).gal) : is_solvable q.gal := begin let K := p.splitting_field, let L := q.splitting_field, haveI : fact (p.splits (algebra_map F L)) := ⟨hpq⟩, let ϕ : (L ≃ₐ[K] L) ≃* (q.map (algebra_map F K)).gal := (is_splitting_field.alg_equiv L (q.map (algebra_map F K))).aut_congr, have ϕ_inj : function.injective ϕ.to_monoid_hom := ϕ.injective, haveI : is_solvable (K ≃ₐ[F] K) := hp, haveI : is_solvable (L ≃ₐ[K] L) := solvable_of_solvable_injective ϕ_inj, exact is_solvable_of_is_scalar_tower F p.splitting_field q.splitting_field, end section gal_X_pow_sub_C lemma gal_X_pow_sub_one_is_solvable (n : ℕ) : is_solvable (X ^ n - 1 : F[X]).gal := begin by_cases hn : n = 0, { rw [hn, pow_zero, sub_self], exact gal_zero_is_solvable }, have hn' : 0 < n := pos_iff_ne_zero.mpr hn, have hn'' : (X ^ n - 1 : F[X]) ≠ 0 := λ h, one_ne_zero ((leading_coeff_X_pow_sub_one hn').symm.trans (congr_arg leading_coeff h)), apply is_solvable_of_comm, intros σ τ, ext a ha, rw [mem_root_set hn'', alg_hom.map_sub, aeval_X_pow, aeval_one, sub_eq_zero] at ha, have key : ∀ σ : (X ^ n - 1 : F[X]).gal, ∃ m : ℕ, σ a = a ^ m, { intro σ, obtain ⟨m, hm⟩ := map_root_of_unity_eq_pow_self σ.to_alg_hom ⟨is_unit.unit (is_unit_of_pow_eq_one a n ha hn'), by { ext, rwa [units.coe_pow, is_unit.unit_spec, subtype.coe_mk n hn'] }⟩, use m, convert hm }, obtain ⟨c, hc⟩ := key σ, obtain ⟨d, hd⟩ := key τ, rw [σ.mul_apply, τ.mul_apply, hc, τ.map_pow, hd, σ.map_pow, hc, ←pow_mul, pow_mul'], end lemma gal_X_pow_sub_C_is_solvable_aux (n : ℕ) (a : F) (h : (X ^ n - 1 : F[X]).splits (ring_hom.id F)) : is_solvable (X ^ n - C a).gal := begin by_cases ha : a = 0, { rw [ha, C_0, sub_zero], exact gal_X_pow_is_solvable n }, have ha' : algebra_map F (X ^ n - C a).splitting_field a ≠ 0 := mt ((injective_iff_map_eq_zero _).mp (ring_hom.injective _) a) ha, by_cases hn : n = 0, { rw [hn, pow_zero, ←C_1, ←C_sub], exact gal_C_is_solvable (1 - a) }, have hn' : 0 < n := pos_iff_ne_zero.mpr hn, have hn'' : X ^ n - C a ≠ 0 := λ h, one_ne_zero ((leading_coeff_X_pow_sub_C hn').symm.trans (congr_arg leading_coeff h)), have hn''' : (X ^ n - 1 : F[X]) ≠ 0 := λ h, one_ne_zero ((leading_coeff_X_pow_sub_one hn').symm.trans (congr_arg leading_coeff h)), have mem_range : ∀ {c}, c ^ n = 1 → ∃ d, algebra_map F (X ^ n - C a).splitting_field d = c := λ c hc, ring_hom.mem_range.mp (minpoly.mem_range_of_degree_eq_one F c (h.def.resolve_left hn''' (minpoly.irreducible ((splitting_field.normal (X ^ n - C a)).is_integral c)) (minpoly.dvd F c (by rwa [map_id, alg_hom.map_sub, sub_eq_zero, aeval_X_pow, aeval_one])))), apply is_solvable_of_comm, intros σ τ, ext b hb, rw [mem_root_set hn'', alg_hom.map_sub, aeval_X_pow, aeval_C, sub_eq_zero] at hb, have hb' : b ≠ 0, { intro hb', rw [hb', zero_pow hn'] at hb, exact ha' hb.symm }, have key : ∀ σ : (X ^ n - C a).gal, ∃ c, σ b = b * algebra_map F _ c, { intro σ, have key : (σ b / b) ^ n = 1 := by rw [div_pow, ←σ.map_pow, hb, σ.commutes, div_self ha'], obtain ⟨c, hc⟩ := mem_range key, use c, rw [hc, mul_div_cancel' (σ b) hb'] }, obtain ⟨c, hc⟩ := key σ, obtain ⟨d, hd⟩ := key τ, rw [σ.mul_apply, τ.mul_apply, hc, τ.map_mul, τ.commutes, hd, σ.map_mul, σ.commutes, hc], rw [mul_assoc, mul_assoc, mul_right_inj' hb', mul_comm], end lemma splits_X_pow_sub_one_of_X_pow_sub_C {F : Type*} [field F] {E : Type*} [field E] (i : F →+* E) (n : ℕ) {a : F} (ha : a ≠ 0) (h : (X ^ n - C a).splits i) : (X ^ n - 1).splits i := begin have ha' : i a ≠ 0 := mt ((injective_iff_map_eq_zero i).mp (i.injective) a) ha, by_cases hn : n = 0, { rw [hn, pow_zero, sub_self], exact splits_zero i }, have hn' : 0 < n := pos_iff_ne_zero.mpr hn, have hn'' : (X ^ n - C a).degree ≠ 0 := ne_of_eq_of_ne (degree_X_pow_sub_C hn' a) (mt with_bot.coe_eq_coe.mp hn), obtain ⟨b, hb⟩ := exists_root_of_splits i h hn'', rw [eval₂_sub, eval₂_X_pow, eval₂_C, sub_eq_zero] at hb, have hb' : b ≠ 0, { intro hb', rw [hb', zero_pow hn'] at hb, exact ha' hb.symm }, let s := ((X ^ n - C a).map i).roots, have hs : _ = _ * (s.map _).prod := eq_prod_roots_of_splits h, rw [leading_coeff_X_pow_sub_C hn', ring_hom.map_one, C_1, one_mul] at hs, have hs' : s.card = n := (nat_degree_eq_card_roots h).symm.trans nat_degree_X_pow_sub_C, apply @splits_of_exists_multiset F E _ _ i (X ^ n - 1) (s.map (λ c : E, c / b)), rw [leading_coeff_X_pow_sub_one hn', ring_hom.map_one, C_1, one_mul, multiset.map_map], have C_mul_C : (C (i a⁻¹)) * (C (i a)) = 1, { rw [←C_mul, ←i.map_mul, inv_mul_cancel ha, i.map_one, C_1] }, have key1 : (X ^ n - 1).map i = C (i a⁻¹) * ((X ^ n - C a).map i).comp (C b * X), { rw [polynomial.map_sub, polynomial.map_sub, polynomial.map_pow, map_X, map_C, polynomial.map_one, sub_comp, pow_comp, X_comp, C_comp, mul_pow, ←C_pow, hb, mul_sub, ←mul_assoc, C_mul_C, one_mul] }, have key2 : (λ q : E[X], q.comp (C b * X)) ∘ (λ c : E, X - C c) = (λ c : E, C b * (X - C (c / b))), { ext1 c, change (X - C c).comp (C b * X) = C b * (X - C (c / b)), rw [sub_comp, X_comp, C_comp, mul_sub, ←C_mul, mul_div_cancel' c hb'] }, rw [key1, hs, multiset_prod_comp, multiset.map_map, key2, multiset.prod_map_mul, multiset.map_const, multiset.prod_repeat, hs', ←C_pow, hb, ←mul_assoc, C_mul_C, one_mul], all_goals { exact field.to_nontrivial F }, end lemma gal_X_pow_sub_C_is_solvable (n : ℕ) (x : F) : is_solvable (X ^ n - C x).gal := begin by_cases hx : x = 0, { rw [hx, C_0, sub_zero], exact gal_X_pow_is_solvable n }, apply gal_is_solvable_tower (X ^ n - 1) (X ^ n - C x), { exact splits_X_pow_sub_one_of_X_pow_sub_C _ n hx (splitting_field.splits _) }, { exact gal_X_pow_sub_one_is_solvable n }, { rw [polynomial.map_sub, polynomial.map_pow, map_X, map_C], apply gal_X_pow_sub_C_is_solvable_aux, have key := splitting_field.splits (X ^ n - 1 : F[X]), rwa [←splits_id_iff_splits, polynomial.map_sub, polynomial.map_pow, map_X, polynomial.map_one] at key } end end gal_X_pow_sub_C variables (F) /-- Inductive definition of solvable by radicals -/ inductive is_solvable_by_rad : E → Prop | base (a : F) : is_solvable_by_rad (algebra_map F E a) | add (a b : E) : is_solvable_by_rad a → is_solvable_by_rad b → is_solvable_by_rad (a + b) | neg (α : E) : is_solvable_by_rad α → is_solvable_by_rad (-α) | mul (α β : E) : is_solvable_by_rad α → is_solvable_by_rad β → is_solvable_by_rad (α * β) | inv (α : E) : is_solvable_by_rad α → is_solvable_by_rad α⁻¹ | rad (α : E) (n : ℕ) (hn : n ≠ 0) : is_solvable_by_rad (α^n) → is_solvable_by_rad α variables (E) /-- The intermediate field of solvable-by-radicals elements -/ def solvable_by_rad : intermediate_field F E := { carrier := is_solvable_by_rad F, zero_mem' := by { convert is_solvable_by_rad.base (0 : F), rw ring_hom.map_zero }, add_mem' := is_solvable_by_rad.add, neg_mem' := is_solvable_by_rad.neg, one_mem' := by { convert is_solvable_by_rad.base (1 : F), rw ring_hom.map_one }, mul_mem' := is_solvable_by_rad.mul, inv_mem' := is_solvable_by_rad.inv, algebra_map_mem' := is_solvable_by_rad.base } namespace solvable_by_rad variables {F} {E} {α : E} lemma induction (P : solvable_by_rad F E → Prop) (base : ∀ α : F, P (algebra_map F (solvable_by_rad F E) α)) (add : ∀ α β : solvable_by_rad F E, P α → P β → P (α + β)) (neg : ∀ α : solvable_by_rad F E, P α → P (-α)) (mul : ∀ α β : solvable_by_rad F E, P α → P β → P (α * β)) (inv : ∀ α : solvable_by_rad F E, P α → P α⁻¹) (rad : ∀ α : solvable_by_rad F E, ∀ n : ℕ, n ≠ 0 → P (α^n) → P α) (α : solvable_by_rad F E) : P α := begin revert α, suffices : ∀ (α : E), is_solvable_by_rad F α → (∃ β : solvable_by_rad F E, ↑β = α ∧ P β), { intro α, obtain ⟨α₀, hα₀, Pα⟩ := this α (subtype.mem α), convert Pα, exact subtype.ext hα₀.symm }, apply is_solvable_by_rad.rec, { exact λ α, ⟨algebra_map F (solvable_by_rad F E) α, rfl, base α⟩ }, { intros α β hα hβ Pα Pβ, obtain ⟨⟨α₀, hα₀, Pα⟩, β₀, hβ₀, Pβ⟩ := ⟨Pα, Pβ⟩, exact ⟨α₀ + β₀, by {rw [←hα₀, ←hβ₀], refl }, add α₀ β₀ Pα Pβ⟩ }, { intros α hα Pα, obtain ⟨α₀, hα₀, Pα⟩ := Pα, exact ⟨-α₀, by {rw ←hα₀, refl }, neg α₀ Pα⟩ }, { intros α β hα hβ Pα Pβ, obtain ⟨⟨α₀, hα₀, Pα⟩, β₀, hβ₀, Pβ⟩ := ⟨Pα, Pβ⟩, exact ⟨α₀ * β₀, by {rw [←hα₀, ←hβ₀], refl }, mul α₀ β₀ Pα Pβ⟩ }, { intros α hα Pα, obtain ⟨α₀, hα₀, Pα⟩ := Pα, exact ⟨α₀⁻¹, by {rw ←hα₀, refl }, inv α₀ Pα⟩ }, { intros α n hn hα Pα, obtain ⟨α₀, hα₀, Pα⟩ := Pα, refine ⟨⟨α, is_solvable_by_rad.rad α n hn hα⟩, rfl, rad _ n hn _⟩, convert Pα, exact subtype.ext (eq.trans ((solvable_by_rad F E).coe_pow _ n) hα₀.symm) } end theorem is_integral (α : solvable_by_rad F E) : is_integral F α := begin revert α, apply solvable_by_rad.induction, { exact λ _, is_integral_algebra_map }, { exact λ _ _, is_integral_add }, { exact λ _, is_integral_neg }, { exact λ _ _, is_integral_mul }, { exact λ α hα, subalgebra.inv_mem_of_algebraic (integral_closure F (solvable_by_rad F E)) (show is_algebraic F ↑(⟨α, hα⟩ : integral_closure F (solvable_by_rad F E)), by exact is_algebraic_iff_is_integral.mpr hα) }, { intros α n hn hα, obtain ⟨p, h1, h2⟩ := is_algebraic_iff_is_integral.mpr hα, refine is_algebraic_iff_is_integral.mp ⟨p.comp (X ^ n), ⟨λ h, h1 (leading_coeff_eq_zero.mp _), by rw [aeval_comp, aeval_X_pow, h2]⟩⟩, rwa [←leading_coeff_eq_zero, leading_coeff_comp, leading_coeff_X_pow, one_pow, mul_one] at h, rwa nat_degree_X_pow } end /-- The statement to be proved inductively -/ def P (α : solvable_by_rad F E) : Prop := is_solvable (minpoly F α).gal /-- An auxiliary induction lemma, which is generalized by `solvable_by_rad.is_solvable`. -/ lemma induction3 {α : solvable_by_rad F E} {n : ℕ} (hn : n ≠ 0) (hα : P (α ^ n)) : P α := begin let p := minpoly F (α ^ n), have hp : p.comp (X ^ n) ≠ 0, { intro h, cases (comp_eq_zero_iff.mp h) with h' h', { exact minpoly.ne_zero (is_integral (α ^ n)) h' }, { exact hn (by rw [←nat_degree_C _, ←h'.2, nat_degree_X_pow]) } }, apply gal_is_solvable_of_splits, { exact ⟨splits_of_splits_of_dvd _ hp (splitting_field.splits (p.comp (X ^ n))) (minpoly.dvd F α (by rw [aeval_comp, aeval_X_pow, minpoly.aeval]))⟩ }, { refine gal_is_solvable_tower p (p.comp (X ^ n)) _ hα _, { exact gal.splits_in_splitting_field_of_comp _ _ (by rwa [nat_degree_X_pow]) }, { obtain ⟨s, hs⟩ := (splits_iff_exists_multiset _).1 (splitting_field.splits p), rw [map_comp, polynomial.map_pow, map_X, hs, mul_comp, C_comp], apply gal_mul_is_solvable (gal_C_is_solvable _), rw multiset_prod_comp, apply gal_prod_is_solvable, intros q hq, rw multiset.mem_map at hq, obtain ⟨q, hq, rfl⟩ := hq, rw multiset.mem_map at hq, obtain ⟨q, hq, rfl⟩ := hq, rw [sub_comp, X_comp, C_comp], exact gal_X_pow_sub_C_is_solvable n q } }, end /-- An auxiliary induction lemma, which is generalized by `solvable_by_rad.is_solvable`. -/ lemma induction2 {α β γ : solvable_by_rad F E} (hγ : γ ∈ F⟮α, β⟯) (hα : P α) (hβ : P β) : P γ := begin let p := (minpoly F α), let q := (minpoly F β), have hpq := polynomial.splits_of_splits_mul _ (mul_ne_zero (minpoly.ne_zero (is_integral α)) (minpoly.ne_zero (is_integral β))) (splitting_field.splits (p * q)), let f : F⟮α, β⟯ →ₐ[F] (p * q).splitting_field := classical.choice (alg_hom_mk_adjoin_splits begin intros x hx, cases hx, rw hx, exact ⟨is_integral α, hpq.1⟩, cases hx, exact ⟨is_integral β, hpq.2⟩, end), have key : minpoly F γ = minpoly F (f ⟨γ, hγ⟩) := minpoly.eq_of_irreducible_of_monic (minpoly.irreducible (is_integral γ)) begin suffices : aeval (⟨γ, hγ⟩ : F ⟮α, β⟯) (minpoly F γ) = 0, { rw [aeval_alg_hom_apply, this, alg_hom.map_zero] }, apply (algebra_map F⟮α, β⟯ (solvable_by_rad F E)).injective, rw [ring_hom.map_zero, is_scalar_tower.algebra_map_aeval], exact minpoly.aeval F γ, end (minpoly.monic (is_integral γ)), rw [P, key], exact gal_is_solvable_of_splits ⟨normal.splits (splitting_field.normal _) _⟩ (gal_mul_is_solvable hα hβ), end /-- An auxiliary induction lemma, which is generalized by `solvable_by_rad.is_solvable`. -/ lemma induction1 {α β : solvable_by_rad F E} (hβ : β ∈ F⟮α⟯) (hα : P α) : P β := induction2 (adjoin.mono F _ _ (ge_of_eq (set.pair_eq_singleton α)) hβ) hα hα theorem is_solvable (α : solvable_by_rad F E) : is_solvable (minpoly F α).gal := begin revert α, apply solvable_by_rad.induction, { exact λ α, by { rw minpoly.eq_X_sub_C, exact gal_X_sub_C_is_solvable α } }, { exact λ α β, induction2 (add_mem (subset_adjoin F _ (set.mem_insert α _)) (subset_adjoin F _ (set.mem_insert_of_mem α (set.mem_singleton β)))) }, { exact λ α, induction1 (neg_mem (mem_adjoin_simple_self F α)) }, { exact λ α β, induction2 (mul_mem (subset_adjoin F _ (set.mem_insert α _)) (subset_adjoin F _ (set.mem_insert_of_mem α (set.mem_singleton β)))) }, { exact λ α, induction1 (inv_mem (mem_adjoin_simple_self F α)) }, { exact λ α n, induction3 }, end /-- **Abel-Ruffini Theorem** (one direction): An irreducible polynomial with an `is_solvable_by_rad` root has solvable Galois group -/ lemma is_solvable' {α : E} {q : F[X]} (q_irred : irreducible q) (q_aeval : aeval α q = 0) (hα : is_solvable_by_rad F α) : _root_.is_solvable q.gal := begin haveI : _root_.is_solvable (q * C q.leading_coeff⁻¹).gal, { rw [minpoly.eq_of_irreducible q_irred q_aeval, ←show minpoly F (⟨α, hα⟩ : solvable_by_rad F E) = minpoly F α, from minpoly.eq_of_algebra_map_eq (ring_hom.injective _) (is_integral ⟨α, hα⟩) rfl], exact is_solvable ⟨α, hα⟩ }, refine solvable_of_surjective (gal.restrict_dvd_surjective ⟨C q.leading_coeff⁻¹, rfl⟩ _), rw [mul_ne_zero_iff, ne, ne, C_eq_zero, inv_eq_zero], exact ⟨q_irred.ne_zero, leading_coeff_ne_zero.mpr q_irred.ne_zero⟩, end end solvable_by_rad end abel_ruffini
29b7f3e5e0977e09176132e808468826064597c3
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/set/intervals/with_bot_top.lean
872f690fdba60490e77ec38e272be554d0342186
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,301
lean
/- Copyright (c) 2022 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import data.set.intervals.basic /-! # Intervals in `with_top α` and `with_bot α` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we prove various lemmas about `set.image`s and `set.preimage`s of intervals under `coe : α → with_top α` and `coe : α → with_bot α`. -/ open set variables {α : Type*} /-! ### `with_top` -/ namespace with_top @[simp] lemma preimage_coe_top : (coe : α → with_top α) ⁻¹' {⊤} = (∅ : set α) := eq_empty_of_subset_empty $ λ a, coe_ne_top variables [partial_order α] {a b : α} lemma range_coe : range (coe : α → with_top α) = Iio ⊤ := begin ext x, rw [mem_Iio, lt_top_iff_ne_top, mem_range, ← none_eq_top, option.ne_none_iff_exists], refl, end @[simp] lemma preimage_coe_Ioi : (coe : α → with_top α) ⁻¹' (Ioi a) = Ioi a := ext $ λ x, coe_lt_coe @[simp] lemma preimage_coe_Ici : (coe : α → with_top α) ⁻¹' (Ici a) = Ici a := ext $ λ x, coe_le_coe @[simp] lemma preimage_coe_Iio : (coe : α → with_top α) ⁻¹' (Iio a) = Iio a := ext $ λ x, coe_lt_coe @[simp] lemma preimage_coe_Iic : (coe : α → with_top α) ⁻¹' (Iic a) = Iic a := ext $ λ x, coe_le_coe @[simp] lemma preimage_coe_Icc : (coe : α → with_top α) ⁻¹' (Icc a b) = Icc a b := by simp [← Ici_inter_Iic] @[simp] lemma preimage_coe_Ico : (coe : α → with_top α) ⁻¹' (Ico a b) = Ico a b := by simp [← Ici_inter_Iio] @[simp] lemma preimage_coe_Ioc : (coe : α → with_top α) ⁻¹' (Ioc a b) = Ioc a b := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_coe_Ioo : (coe : α → with_top α) ⁻¹' (Ioo a b) = Ioo a b := by simp [← Ioi_inter_Iio] @[simp] lemma preimage_coe_Iio_top : (coe : α → with_top α) ⁻¹' (Iio ⊤) = univ := by rw [← range_coe, preimage_range] @[simp] lemma preimage_coe_Ico_top : (coe : α → with_top α) ⁻¹' (Ico a ⊤) = Ici a := by simp [← Ici_inter_Iio] @[simp] lemma preimage_coe_Ioo_top : (coe : α → with_top α) ⁻¹' (Ioo a ⊤) = Ioi a := by simp [← Ioi_inter_Iio] lemma image_coe_Ioi : (coe : α → with_top α) '' (Ioi a) = Ioo a ⊤ := by rw [← preimage_coe_Ioi, image_preimage_eq_inter_range, range_coe, Ioi_inter_Iio] lemma image_coe_Ici : (coe : α → with_top α) '' (Ici a) = Ico a ⊤ := by rw [← preimage_coe_Ici, image_preimage_eq_inter_range, range_coe, Ici_inter_Iio] lemma image_coe_Iio : (coe : α → with_top α) '' (Iio a) = Iio a := by rw [← preimage_coe_Iio, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (Iio_subset_Iio le_top)] lemma image_coe_Iic : (coe : α → with_top α) '' (Iic a) = Iic a := by rw [← preimage_coe_Iic, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (Iic_subset_Iio.2 $ coe_lt_top a)] lemma image_coe_Icc : (coe : α → with_top α) '' (Icc a b) = Icc a b := by rw [← preimage_coe_Icc, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Icc_subset_Iic_self $ Iic_subset_Iio.2 $ coe_lt_top b)] lemma image_coe_Ico : (coe : α → with_top α) '' (Ico a b) = Ico a b := by rw [← preimage_coe_Ico, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Ico_subset_Iio_self $ Iio_subset_Iio le_top)] lemma image_coe_Ioc : (coe : α → with_top α) '' (Ioc a b) = Ioc a b := by rw [← preimage_coe_Ioc, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Ioc_subset_Iic_self $ Iic_subset_Iio.2 $ coe_lt_top b)] lemma image_coe_Ioo : (coe : α → with_top α) '' (Ioo a b) = Ioo a b := by rw [← preimage_coe_Ioo, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Ioo_subset_Iio_self $ Iio_subset_Iio le_top)] end with_top /-! ### `with_bot` -/ namespace with_bot @[simp] lemma preimage_coe_bot : (coe : α → with_bot α) ⁻¹' {⊥} = (∅ : set α) := @with_top.preimage_coe_top αᵒᵈ variables [partial_order α] {a b : α} lemma range_coe : range (coe : α → with_bot α) = Ioi ⊥ := @with_top.range_coe αᵒᵈ _ @[simp] lemma preimage_coe_Ioi : (coe : α → with_bot α) ⁻¹' (Ioi a) = Ioi a := ext $ λ x, coe_lt_coe @[simp] lemma preimage_coe_Ici : (coe : α → with_bot α) ⁻¹' (Ici a) = Ici a := ext $ λ x, coe_le_coe @[simp] lemma preimage_coe_Iio : (coe : α → with_bot α) ⁻¹' (Iio a) = Iio a := ext $ λ x, coe_lt_coe @[simp] lemma preimage_coe_Iic : (coe : α → with_bot α) ⁻¹' (Iic a) = Iic a := ext $ λ x, coe_le_coe @[simp] lemma preimage_coe_Icc : (coe : α → with_bot α) ⁻¹' (Icc a b) = Icc a b := by simp [← Ici_inter_Iic] @[simp] lemma preimage_coe_Ico : (coe : α → with_bot α) ⁻¹' (Ico a b) = Ico a b := by simp [← Ici_inter_Iio] @[simp] lemma preimage_coe_Ioc : (coe : α → with_bot α) ⁻¹' (Ioc a b) = Ioc a b := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_coe_Ioo : (coe : α → with_bot α) ⁻¹' (Ioo a b) = Ioo a b := by simp [← Ioi_inter_Iio] @[simp] lemma preimage_coe_Ioi_bot : (coe : α → with_bot α) ⁻¹' (Ioi ⊥) = univ := by rw [← range_coe, preimage_range] @[simp] lemma preimage_coe_Ioc_bot : (coe : α → with_bot α) ⁻¹' (Ioc ⊥ a) = Iic a := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_coe_Ioo_bot : (coe : α → with_bot α) ⁻¹' (Ioo ⊥ a) = Iio a := by simp [← Ioi_inter_Iio] lemma image_coe_Iio : (coe : α → with_bot α) '' (Iio a) = Ioo ⊥ a := by rw [← preimage_coe_Iio, image_preimage_eq_inter_range, range_coe, inter_comm, Ioi_inter_Iio] lemma image_coe_Iic : (coe : α → with_bot α) '' (Iic a) = Ioc ⊥ a := by rw [← preimage_coe_Iic, image_preimage_eq_inter_range, range_coe, inter_comm, Ioi_inter_Iic] lemma image_coe_Ioi : (coe : α → with_bot α) '' (Ioi a) = Ioi a := by rw [← preimage_coe_Ioi, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (Ioi_subset_Ioi bot_le)] lemma image_coe_Ici : (coe : α → with_bot α) '' (Ici a) = Ici a := by rw [← preimage_coe_Ici, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (Ici_subset_Ioi.2 $ bot_lt_coe a)] lemma image_coe_Icc : (coe : α → with_bot α) '' (Icc a b) = Icc a b := by rw [← preimage_coe_Icc, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Icc_subset_Ici_self $ Ici_subset_Ioi.2 $ bot_lt_coe a)] lemma image_coe_Ioc : (coe : α → with_bot α) '' (Ioc a b) = Ioc a b := by rw [← preimage_coe_Ioc, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Ioc_subset_Ioi_self $ Ioi_subset_Ioi bot_le)] lemma image_coe_Ico : (coe : α → with_bot α) '' (Ico a b) = Ico a b := by rw [← preimage_coe_Ico, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Ico_subset_Ici_self $ Ici_subset_Ioi.2 $ bot_lt_coe a)] lemma image_coe_Ioo : (coe : α → with_bot α) '' (Ioo a b) = Ioo a b := by rw [← preimage_coe_Ioo, image_preimage_eq_inter_range, range_coe, inter_eq_self_of_subset_left (subset.trans Ioo_subset_Ioi_self $ Ioi_subset_Ioi bot_le)] end with_bot
0f6d7d26992ead72c750c1f78d463e6c88d31ed5
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/test/norm_cast_int.lean
cde611b00b8c2f065aff4478362b656a5f1f02d0
[ "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
583
lean
import tactic.norm_cast import data.int.cast set_option pp.numerals false set_option pp.notation false -- set_option trace.simplify.rewrite true run_cmd norm_cast.numeral_to_coe `(0 : ℤ) run_cmd norm_cast.numeral_to_coe `(1 : ℤ) run_cmd norm_cast.numeral_to_coe `(2 : ℤ) run_cmd norm_cast.numeral_to_coe `(3 : ℤ) run_cmd norm_cast.coe_to_numeral `((0 : ℕ) : ℤ) run_cmd norm_cast.coe_to_numeral `((1 : ℕ) : ℤ) run_cmd norm_cast.coe_to_numeral `((2 : ℕ) : ℤ) run_cmd norm_cast.coe_to_numeral `((3 : ℕ) : ℤ) example : ((42 : ℕ) : ℤ) = 42 := by norm_cast
cc49e6e34aa8a040cb67f04e5198a010e45207e6
82e44445c70db0f03e30d7be725775f122d72f3e
/src/algebra/ordered_monoid_lemmas.lean
2e4814f0349d1b2f4ba92c9ea288299f6dff3b38
[ "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
24,189
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, Johannes Hölzl, Damiano Testa -/ import algebra.covariant_and_contravariant import order.basic /-! # Ordered monoids This file develops the basics of ordered monoids. ## Implementation details Unfortunately, the number of `'` appended to lemmas in this file may differ between the multiplicative and the additive version of a lemma. The reason is that we did not want to change existing names in the library. ## Remark No monoid is actually present in this file: all assumptions have been generalized to `has_mul` or `mul_one_class`. -/ -- TODO: If possible, uniformize lemma names, taking special care of `'`, -- after the `ordered`-refactor is done. variables {α : Type*} section has_mul variables [has_mul α] section has_le variables [has_le α] @[simp, to_additive] lemma mul_le_mul_iff_left [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b c : α} : a * b ≤ a * c ↔ b ≤ c := rel_iff_cov α α (*) (≤) a @[simp, to_additive] lemma mul_le_mul_iff_right [covariant_class α α (function.swap (*)) (≤)] [contravariant_class α α (function.swap (*)) (≤)] (a : α) {b c : α} : b * a ≤ c * a ↔ b ≤ c := rel_iff_cov α α (function.swap (*)) (≤) a /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive add_le_add_left] lemma mul_le_mul_left' [covariant_class α α (*) (≤)] {b c : α} (bc : b ≤ c) (a : α) : a * b ≤ a * c := covariant_class.elim _ bc @[to_additive le_of_add_le_add_left] lemma le_of_mul_le_mul_left' [contravariant_class α α (*) (≤)] {a b c : α} (bc : a * b ≤ a * c) : b ≤ c := contravariant_class.elim _ bc /- The prime on this lemma is present only on the multiplicative version. The unprimed version is taken by the analogous lemma for semiring, with an extra non-negativity assumption. -/ @[to_additive add_le_add_right] lemma mul_le_mul_right' [covariant_class α α (function.swap (*)) (≤)] {b c : α} (bc : b ≤ c) (a : α) : b * a ≤ c * a := covariant_class.elim a bc @[to_additive le_of_add_le_add_right] lemma le_of_mul_le_mul_right' [contravariant_class α α (function.swap (*)) (≤)] {a b c : α} (bc : b * a ≤ c * a) : b ≤ c := contravariant_class.elim a bc end has_le section has_lt variables [has_lt α] @[simp, to_additive] lemma mul_lt_mul_iff_left [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] (a : α) {b c : α} : a * b < a * c ↔ b < c := rel_iff_cov α α (*) (<) a @[simp, to_additive] lemma mul_lt_mul_iff_right [covariant_class α α (function.swap (*)) (<)] [contravariant_class α α (function.swap (*)) (<)] (a : α) {b c : α} : b * a < c * a ↔ b < c := rel_iff_cov α α (function.swap (*)) (<) a @[to_additive add_lt_add_left] lemma mul_lt_mul_left' [covariant_class α α (*) (<)] {b c : α} (bc : b < c) (a : α) : a * b < a * c := covariant_class.elim _ bc @[to_additive lt_of_add_lt_add_left] lemma lt_of_mul_lt_mul_left' [contravariant_class α α (*) (<)] {a b c : α} (bc : a * b < a * c) : b < c := contravariant_class.elim _ bc @[to_additive add_lt_add_right] lemma mul_lt_mul_right' [covariant_class α α (function.swap (*)) (<)] {b c : α} (bc : b < c) (a : α) : b * a < c * a := covariant_class.elim a bc @[to_additive lt_of_add_lt_add_right] lemma lt_of_mul_lt_mul_right' [contravariant_class α α (function.swap (*)) (<)] {a b c : α} (bc : b * a < c * a) : b < c := contravariant_class.elim a bc end has_lt end has_mul -- using one section mul_one_class variables [mul_one_class α] section has_le variables [has_le α] @[simp, to_additive le_add_iff_nonneg_right] lemma le_mul_iff_one_le_right' [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b : α} : a ≤ a * b ↔ 1 ≤ b := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[simp, to_additive add_le_iff_nonpos_right] lemma mul_le_iff_le_one_right' [covariant_class α α (*) (≤)] [contravariant_class α α (*) (≤)] (a : α) {b : α} : a * b ≤ a ↔ b ≤ 1 := iff.trans (by rw [mul_one]) (mul_le_mul_iff_left a) @[simp, to_additive le_add_iff_nonneg_left] lemma le_mul_iff_one_le_left' [covariant_class α α (function.swap (*)) (≤)] [contravariant_class α α (function.swap (*)) (≤)] (a : α) {b : α} : a ≤ b * a ↔ 1 ≤ b := iff.trans (by rw one_mul) (mul_le_mul_iff_right a) @[simp, to_additive add_le_iff_nonpos_left] lemma mul_le_iff_le_one_left' [covariant_class α α (function.swap (*)) (≤)] [contravariant_class α α (function.swap (*)) (≤)] {a b : α} : a * b ≤ b ↔ a ≤ 1 := iff.trans (by rw one_mul) (mul_le_mul_iff_right b) end has_le section has_lt variable [has_lt α] @[to_additive lt_add_of_pos_right] lemma lt_mul_of_one_lt_right' [covariant_class α α (*) (<)] (a : α) {b : α} (h : 1 < b) : a < a * b := calc a = a * 1 : (mul_one _).symm ... < a * b : mul_lt_mul_left' h a @[simp, to_additive lt_add_iff_pos_right] lemma lt_mul_iff_one_lt_right' [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] (a : α) {b : α} : a < a * b ↔ 1 < b := iff.trans (by rw mul_one) (mul_lt_mul_iff_left a) @[simp, to_additive add_lt_iff_neg_left] lemma mul_lt_iff_lt_one_left' [covariant_class α α (*) (<)] [contravariant_class α α (*) (<)] {a b : α} : a * b < a ↔ b < 1 := iff.trans (by rw mul_one) (mul_lt_mul_iff_left a) @[simp, to_additive lt_add_iff_pos_left] lemma lt_mul_iff_one_lt_left' [covariant_class α α (function.swap (*)) (<)] [contravariant_class α α (function.swap (*)) (<)] (a : α) {b : α} : a < b * a ↔ 1 < b := iff.trans (by rw one_mul) (mul_lt_mul_iff_right a) @[simp, to_additive add_lt_iff_neg_right] lemma mul_lt_iff_lt_one_right' [covariant_class α α (function.swap (*)) (<)] [contravariant_class α α (function.swap (*)) (<)] {a : α} (b : α) : a * b < b ↔ a < 1 := iff.trans (by rw one_mul) (mul_lt_mul_iff_right b) end has_lt section preorder variable [preorder α] @[to_additive] lemma mul_le_of_le_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (hbc : b ≤ c) (ha : a ≤ 1) : b * a ≤ c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b ... = b : mul_one b ... ≤ c : hbc alias mul_le_of_le_of_le_one ← mul_le_one' attribute [to_additive add_nonpos] mul_le_one' @[to_additive] lemma lt_mul_of_lt_of_one_le [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : 1 ≤ a) : b < c * a := calc b < c : hbc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c @[to_additive] lemma mul_lt_of_lt_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (hbc : b < c) (ha : a ≤ 1) : b * a < c := calc b * a ≤ b * 1 : mul_le_mul_left' ha b ... = b : mul_one b ... < c : hbc @[to_additive] lemma lt_mul_of_le_of_one_lt [covariant_class α α (*) (<)] {a b c : α} (hbc : b ≤ c) (ha : 1 < a) : b < c * a := calc b ≤ c : hbc ... = c * 1 : (mul_one c).symm ... < c * a : mul_lt_mul_left' ha c @[to_additive] lemma mul_lt_of_le_one_of_lt [covariant_class α α (function.swap (*)) (≤)] {a b c : α} (ha : a ≤ 1) (hb : b < c) : a * b < c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b ... = b : one_mul b ... < c : hb @[to_additive] lemma mul_le_of_le_one_of_le [covariant_class α α (function.swap (*)) (≤)] {a b c : α} (ha : a ≤ 1) (hbc : b ≤ c) : a * b ≤ c := calc a * b ≤ 1 * b : mul_le_mul_right' ha b ... = b : one_mul b ... ≤ c : hbc @[to_additive] lemma le_mul_of_one_le_of_le [covariant_class α α (function.swap (*)) (≤)] {a b c: α} (ha : 1 ≤ a) (hbc : b ≤ c) : b ≤ a * c := calc b ≤ c : hbc ... = 1 * c : (one_mul c).symm ... ≤ a * c : mul_le_mul_right' ha c /-- Assume monotonicity on the `left`. The lemma assuming `right` is `right.mul_lt_one`. -/ @[to_additive] lemma left.mul_lt_one [covariant_class α α (*) (<)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := calc a * b < a * 1 : mul_lt_mul_left' hb a ... = a : mul_one a ... < 1 : ha /-- Assume monotonicity on the `right`. The lemma assuming `left` is `left.mul_lt_one`. -/ @[to_additive] lemma right.mul_lt_one [covariant_class α α (function.swap (*)) (<)] {a b : α} (ha : a < 1) (hb : b < 1) : a * b < 1 := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... < 1 : hb @[to_additive] lemma mul_lt_of_le_of_lt_one [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] {a b c: α} (hbc : b ≤ c) (ha : a < 1) : b * a < c := calc b * a ≤ c * a : mul_le_mul_right' hbc a ... < c * 1 : mul_lt_mul_left' ha c ... = c : mul_one c @[to_additive] lemma mul_lt_of_lt_one_of_le [covariant_class α α (function.swap (*)) (<)] {a b c : α} (ha : a < 1) (hbc : b ≤ c) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... ≤ c : hbc @[to_additive] lemma lt_mul_of_one_lt_of_le [covariant_class α α (function.swap (*)) (<)] {a b c : α} (ha : 1 < a) (hbc : b ≤ c) : b < a * c := calc b ≤ c : hbc ... = 1 * c : (one_mul c).symm ... < a * c : mul_lt_mul_right' ha c /-- Assumes left covariance. -/ @[to_additive] lemma le_mul_of_le_of_le_one [covariant_class α α (*) (≤)] {a b c : α} (ha : c ≤ a) (hb : 1 ≤ b) : c ≤ a * b := calc c ≤ a : ha ... = a * 1 : (mul_one a).symm ... ≤ a * b : mul_le_mul_left' hb a /- This lemma is present to mimick the name of an existing one. -/ @[to_additive add_nonneg] lemma one_le_mul [covariant_class α α (*) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := le_mul_of_le_of_le_one ha hb /-- Assumes left covariance. -/ @[to_additive] lemma lt_mul_of_lt_of_one_lt [covariant_class α α (*) (<)] {a b c : α} (ha : c < a) (hb : 1 < b) : c < a * b := calc c < a : ha ... = a * 1 : (mul_one _).symm ... < a * b : mul_lt_mul_left' hb a /-- Assumes left covariance. -/ @[to_additive] lemma left.mul_lt_one_of_lt_of_lt_one [covariant_class α α (*) (<)] {a b c : α} (ha : a < c) (hb : b < 1) : a * b < c := calc a * b < a * 1 : mul_lt_mul_left' hb a ... = a : mul_one a ... < c : ha /-- Assumes right covariance. -/ @[to_additive] lemma right.mul_lt_one_of_lt_of_lt_one [covariant_class α α (function.swap (*)) (<)] {a b c : α} (ha : a < 1) (hb : b < c) : a * b < c := calc a * b < 1 * b : mul_lt_mul_right' ha b ... = b : one_mul b ... < c : hb /-- Assumes right covariance. -/ @[to_additive right.add_nonneg] lemma right.one_le_mul [covariant_class α α (function.swap (*)) (≤)] {a b : α} (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := calc 1 ≤ b : hb ... = 1 * b : (one_mul b).symm ... ≤ a * b : mul_le_mul_right' ha b /-- Assumes right covariance. -/ @[to_additive right.add_pos] lemma right.one_lt_mul [covariant_class α α (function.swap (*)) (<)] {b : α} (hb : 1 < b) {a: α} (ha : 1 < a) : 1 < a * b := calc 1 < b : hb ... = 1 * b : (one_mul _).symm ... < a * b : mul_lt_mul_right' ha b end preorder @[to_additive le_add_of_nonneg_right] lemma le_mul_of_one_le_right' [has_le α] [covariant_class α α (*) (≤)] {a b : α} (h : 1 ≤ b) : a ≤ a * b := calc a = a * 1 : (mul_one _).symm ... ≤ a * b : mul_le_mul_left' h a @[to_additive add_le_of_nonpos_right] lemma mul_le_of_le_one_right' [has_le α] [covariant_class α α (*) (≤)] {a b : α} (h : b ≤ 1) : a * b ≤ a := calc a * b ≤ a * 1 : mul_le_mul_left' h a ... = a : mul_one a end mul_one_class /- This is not instance, since we want to have an instance from `left_cancel_semigroup`s to the appropriate `covariant_class`. -/ /-- A semigroup with a partial order and satisfying `left_cancel_semigroup` (i.e. `a * c < b * c → a < b`) is a `left_cancel semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `left_cancel_add_semigroup` (i.e. `c + a < c + b → a < b`) is a `left_cancel add_semigroup`."] def contravariant.to_left_cancel_semigroup [semigroup α] [partial_order α] [contravariant_class α α ((*) : α → α → α) (≤)] : left_cancel_semigroup α := { mul_left_cancel := λ a b c bc, (le_of_mul_le_mul_left' bc.le).antisymm (le_of_mul_le_mul_left' bc.ge), ..‹semigroup α› } /- This is not instance, since we want to have an instance from `right_cancel_semigroup`s to the appropriate `covariant_class`. -/ /-- A semigroup with a partial order and satisfying `right_cancel_semigroup` (i.e. `a * c < b * c → a < b`) is a `right_cancel semigroup`. -/ @[to_additive "An additive semigroup with a partial order and satisfying `right_cancel_add_semigroup` (`a + c < b + c → a < b`) is a `right_cancel add_semigroup`."] def contravariant.to_right_cancel_semigroup [semigroup α] [partial_order α] [contravariant_class α α (function.swap (*) : α → α → α) (≤)] : right_cancel_semigroup α := { mul_right_cancel := λ a b c bc, le_antisymm (le_of_mul_le_mul_right' bc.le) (le_of_mul_le_mul_right' bc.ge) ..‹semigroup α› } variables {a b c d : α} section left variables [preorder α] section has_mul variables [has_mul α] @[to_additive] lemma mul_lt_mul_of_lt_of_lt [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (<)] (h₁ : a < b) (h₂ : c < d) : a * c < b * d := calc a * c < a * d : mul_lt_mul_left' h₂ a ... < b * d : mul_lt_mul_right' h₁ d section contravariant_mul_lt_left_le_right variables [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma mul_lt_mul_of_le_of_lt (h₁ : a ≤ b) (h₂ : c < d) : a * c < b * d := (mul_le_mul_right' h₁ _).trans_lt (mul_lt_mul_left' h₂ b) @[to_additive add_lt_add] lemma mul_lt_mul''' (h₁ : a < b) (h₂ : c < d) : a * c < b * d := mul_lt_mul_of_le_of_lt h₁.le h₂ end contravariant_mul_lt_left_le_right variable [covariant_class α α (*) (≤)] @[to_additive] lemma mul_lt_of_mul_lt_left (h : a * b < c) (hle : d ≤ b) : a * d < c := (mul_le_mul_left' hle a).trans_lt h @[to_additive] lemma mul_le_of_mul_le_left (h : a * b ≤ c) (hle : d ≤ b) : a * d ≤ c := @act_rel_of_rel_of_act_rel _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ a _ _ _ hle h @[to_additive] lemma lt_mul_of_lt_mul_left (h : a < b * c) (hle : c ≤ d) : a < b * d := h.trans_le (mul_le_mul_left' hle b) @[to_additive] lemma le_mul_of_le_mul_left (h : a ≤ b * c) (hle : c ≤ d) : a ≤ b * d := @rel_act_of_rel_of_rel_act _ _ _ (≤) _ ⟨λ _ _ _, le_trans⟩ b _ _ _ hle h @[to_additive] lemma mul_lt_mul_of_lt_of_le [covariant_class α α (function.swap (*)) (<)] (h₁ : a < b) (h₂ : c ≤ d) : a * c < b * d := (mul_le_mul_left' h₂ _).trans_lt (mul_lt_mul_right' h₁ d) end has_mul /-! Here we start using properties of one, on the left. -/ section mul_one_class variables [mul_one_class α] [covariant_class α α (*) (≤)] @[to_additive] lemma lt_of_mul_lt_of_one_le_left (h : a * b < c) (hle : 1 ≤ b) : a < c := (le_mul_of_one_le_right' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_left (h : a * b ≤ c) (hle : 1 ≤ b) : a ≤ c := (le_mul_of_one_le_right' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_left (h : a < b * c) (hle : c ≤ 1) : a < b := h.trans_le (mul_le_of_le_one_right' hle) @[to_additive] lemma le_of_le_mul_of_le_one_left (h : a ≤ b * c) (hle : c ≤ 1) : a ≤ b := h.trans (mul_le_of_le_one_right' hle) @[to_additive] theorem mul_lt_of_lt_of_lt_one (bc : b < c) (a1 : a < 1) : b * a < c := calc b * a ≤ b * 1 : mul_le_mul_left' a1.le _ ... = b : mul_one b ... < c : bc end mul_one_class end left section right section preorder variables [preorder α] section has_mul variables [has_mul α] variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma mul_lt_of_mul_lt_right (h : a * b < c) (hle : d ≤ a) : d * b < c := (mul_le_mul_right' hle b).trans_lt h @[to_additive] lemma mul_le_of_mul_le_right (h : a * b ≤ c) (hle : d ≤ a) : d * b ≤ c := (mul_le_mul_right' hle b).trans h @[to_additive] lemma lt_mul_of_lt_mul_right (h : a < b * c) (hle : b ≤ d) : a < d * c := h.trans_le (mul_le_mul_right' hle c) @[to_additive] lemma le_mul_of_le_mul_right (h : a ≤ b * c) (hle : b ≤ d) : a ≤ d * c := h.trans (mul_le_mul_right' hle c) variable [covariant_class α α (*) (≤)] @[to_additive add_le_add] lemma mul_le_mul' (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := (mul_le_mul_left' h₂ _).trans (mul_le_mul_right' h₁ d) @[to_additive] lemma mul_le_mul_three {e f : α} (h₁ : a ≤ d) (h₂ : b ≤ e) (h₃ : c ≤ f) : a * b * c ≤ d * e * f := mul_le_mul' (mul_le_mul' h₁ h₂) h₃ end has_mul /-! Here we start using properties of one, on the right. -/ section mul_one_class variables [mul_one_class α] section le_right variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive le_add_of_nonneg_left] lemma le_mul_of_one_le_left' (h : 1 ≤ b) : a ≤ b * a := calc a = 1 * a : (one_mul a).symm ... ≤ b * a : mul_le_mul_right' h a @[to_additive add_le_of_nonpos_left] lemma mul_le_of_le_one_left' (h : b ≤ 1) : b * a ≤ a := calc b * a ≤ 1 * a : mul_le_mul_right' h a ... = a : one_mul a @[to_additive] lemma lt_of_mul_lt_of_one_le_right (h : a * b < c) (hle : 1 ≤ a) : b < c := (le_mul_of_one_le_left' hle).trans_lt h @[to_additive] lemma le_of_mul_le_of_one_le_right (h : a * b ≤ c) (hle : 1 ≤ a) : b ≤ c := (le_mul_of_one_le_left' hle).trans h @[to_additive] lemma lt_of_lt_mul_of_le_one_right (h : a < b * c) (hle : b ≤ 1) : a < c := h.trans_le (mul_le_of_le_one_left' hle) @[to_additive] lemma le_of_le_mul_of_le_one_right (h : a ≤ b * c) (hle : b ≤ 1) : a ≤ c := h.trans (mul_le_of_le_one_left' hle) theorem mul_lt_of_lt_one_of_lt (a1 : a < 1) (bc : b < c) : a * b < c := calc a * b ≤ 1 * b : mul_le_mul_right' a1.le _ ... = b : one_mul b ... < c : bc end le_right section lt_right @[to_additive lt_add_of_pos_left] lemma lt_mul_of_one_lt_left' [covariant_class α α (function.swap (*)) (<)] (a : α) {b : α} (h : 1 < b) : a < b * a := calc a = 1 * a : (one_mul _).symm ... < b * a : mul_lt_mul_right' h a end lt_right end mul_one_class end preorder end right section preorder variables [preorder α] section mul_one_class variables [mul_one_class α] section covariant_left variable [covariant_class α α (*) (≤)] @[to_additive add_pos_of_pos_of_nonneg] lemma one_lt_mul_of_lt_of_le' (ha : 1 < a) (hb : 1 ≤ b) : 1 < a * b := lt_of_lt_of_le ha $ le_mul_of_one_le_right' hb @[to_additive add_pos] lemma one_lt_mul' (ha : 1 < a) (hb : 1 < b) : 1 < a * b := one_lt_mul_of_lt_of_le' ha hb.le @[to_additive] lemma lt_mul_of_lt_of_one_le' (hbc : b < c) (ha : 1 ≤ a) : b < c * a := hbc.trans_le $ le_mul_of_one_le_right' ha @[to_additive] lemma lt_mul_of_lt_of_one_lt' (hbc : b < c) (ha : 1 < a) : b < c * a := lt_mul_of_lt_of_one_le' hbc ha.le @[to_additive] lemma le_mul_of_le_of_one_le (hbc : b ≤ c) (ha : 1 ≤ a) : b ≤ c * a := calc b ≤ c : hbc ... = c * 1 : (mul_one c).symm ... ≤ c * a : mul_le_mul_left' ha c @[to_additive add_nonneg] lemma one_le_mul_right (ha : 1 ≤ a) (hb : 1 ≤ b) : 1 ≤ a * b := calc 1 ≤ a : ha ... = a * 1 : (mul_one a).symm ... ≤ a * b : mul_le_mul_left' hb a end covariant_left section covariant_right variable [covariant_class α α (function.swap (*)) (≤)] @[to_additive add_pos_of_nonneg_of_pos] lemma one_lt_mul_of_le_of_lt' (ha : 1 ≤ a) (hb : 1 < b) : 1 < a * b := lt_of_lt_of_le hb $ le_mul_of_one_le_left' ha @[to_additive] lemma lt_mul_of_one_le_of_lt (ha : 1 ≤ a) (hbc : b < c) : b < a * c := hbc.trans_le $ le_mul_of_one_le_left' ha @[to_additive] lemma lt_mul_of_one_lt_of_lt (ha : 1 < a) (hbc : b < c) : b < a * c := lt_mul_of_one_le_of_lt ha.le hbc end covariant_right end mul_one_class end preorder section partial_order /-! Properties assuming `partial_order`. -/ variables [mul_one_class α] [partial_order α] [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] @[to_additive] lemma mul_eq_one_iff' (ha : 1 ≤ a) (hb : 1 ≤ b) : a * b = 1 ↔ a = 1 ∧ b = 1 := iff.intro (assume hab : a * b = 1, have a ≤ 1, from hab ▸ le_mul_of_le_of_one_le le_rfl hb, have a = 1, from le_antisymm this ha, have b ≤ 1, from hab ▸ le_mul_of_one_le_of_le ha le_rfl, have b = 1, from le_antisymm this hb, and.intro ‹a = 1› ‹b = 1›) (assume ⟨ha', hb'⟩, by rw [ha', hb', mul_one]) end partial_order section mono variables [has_mul α] {β : Type*} {f g : β → α} section has_le variables [preorder α] [preorder β] @[to_additive monotone.const_add] lemma monotone.const_mul' [covariant_class α α (*) (≤)] (hf : monotone f) (a : α) : monotone (λ x, a * f x) := λ x y h, mul_le_mul_left' (hf h) a @[to_additive monotone.add_const] lemma monotone.mul_const' [covariant_class α α (function.swap (*)) (≤)] (hf : monotone f) (a : α) : monotone (λ x, f x * a) := λ x y h, mul_le_mul_right' (hf h) a end has_le variables [preorder α] [preorder β] /-- The product of two monotone functions is monotone. -/ @[to_additive monotone.add "The sum of two monotone functions is monotone."] lemma monotone.mul' [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (≤)] (hf : monotone f) (hg : monotone g) : monotone (λ x, f x * g x) := λ x y h, mul_le_mul' (hf h) (hg h) end mono section strict_mono variables [has_mul α] {β : Type*} {f g : β → α} section left variables [has_lt α] [covariant_class α α (*) (<)] [has_lt β] @[to_additive strict_mono.const_add] lemma strict_mono.const_mul' (hf : strict_mono f) (c : α) : strict_mono (λ x, c * f x) := λ a b ab, mul_lt_mul_left' (hf ab) c end left section right variables [has_lt α] [covariant_class α α (function.swap (*)) (<)] [has_lt β] @[to_additive strict_mono.add_const] lemma strict_mono.mul_const' (hf : strict_mono f) (c : α) : strict_mono (λ x, f x * c) := λ a b ab, mul_lt_mul_right' (hf ab) c end right /-- The product of two strictly monotone functions is strictly monotone. -/ @[to_additive strict_mono.add "The sum of two strictly monotone functions is strictly monotone."] lemma strict_mono.mul' [has_lt β] [preorder α] [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (<)] (hf : strict_mono f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ a b ab, mul_lt_mul_of_lt_of_lt (hf ab) (hg ab) variables [preorder α] /-- The product of a monotone function and a strictly monotone function is strictly monotone. -/ @[to_additive monotone.add_strict_mono "The sum of a monotone function and a strictly monotone function is strictly monotone."] lemma monotone.mul_strict_mono' [covariant_class α α (*) (<)] [covariant_class α α (function.swap (*)) (≤)] {β : Type*} [preorder β] {f g : β → α} (hf : monotone f) (hg : strict_mono g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_le_of_lt (hf h.le) (hg h) variables [covariant_class α α (*) (≤)] [covariant_class α α (function.swap (*)) (<)] [preorder β] /-- The product of a strictly monotone function and a monotone function is strictly monotone. -/ @[to_additive strict_mono.add_monotone "The sum of a strictly monotone function and a monotone function is strictly monotone."] lemma strict_mono.mul_monotone' (hf : strict_mono f) (hg : monotone g) : strict_mono (λ x, f x * g x) := λ x y h, mul_lt_mul_of_lt_of_le (hf h) (hg h.le) end strict_mono
7e22313d8a7565168fd086f338632d01d0327c0e
9bf90df35bb15a2f76571e35c48192142a328c40
/src/ch2.lean
0dafc4aad418951d7b0c6fbcc97f09231e181291
[]
no_license
ehaskell1/set_theory
ed0726520e84990d5f3180bafa0a3674ed31fb5e
e6c829c4dd953d98c9cba08f9f79784cd91794fb
refs/heads/master
1,693,282,405,362
1,636,928,916,000
1,636,928,916,000
428,055,746
0
0
null
null
null
null
UTF-8
Lean
false
false
11,432
lean
import ch1 namespace Set theorem russels_paradox : ¬ ∃ S : Set, ∀ x, x ∈ S ↔ x ∉ x := begin rintro ⟨S, hs⟩, specialize hs S, exact (iff_not_self _).mp hs, end -- axiom of specification is enabled by has_sep and mem_sep theorem univ_not_set : ¬ ∃ S : Set, ∀ x, x ∈ S := begin rintro ⟨S, hs⟩, apply russels_paradox, let B := {x ∈ S | x ∉ x}, refine ⟨B, _⟩, intro x, rw mem_sep, exact and_iff_right (hs x), end theorem univ_not_set' (A : Set) : ∃ x, x ∉ A := begin let B := {x ∈ A | x ∉ x}, refine ⟨B, _⟩, intro he, suffices h : B ∈ B ↔ B ∈ A ∧ B ∉ B, rw @and_iff_right _ (B ∉ B) he at h, exact (iff_not_self (B ∈ B)).mp h, exact mem_sep, end -- set union enabled by Union and mem_Union theorem union_empty_eq_empty : (∅ : Set).Union = ∅ := begin apply ext, intro z, rw (iff_false _).mpr (mem_empty z), rw iff_false, rw mem_Union, rintro ⟨y, H, hi⟩, exact mem_empty y H, end def Inter (A : Set) : Set := {x ∈ A.Union | ∀ z ∈ A, x ∈ z} def inhab (A : Set) : Prop := ∃ x, x ∈ A theorem ne_empty_of_inhabited (A : Set) (h : A.inhab) : A ≠ ∅ := begin cases h with x h, intro he, rw he at h, exact mem_empty x h, end theorem inhabited_of_ne_empty {A : Set} (hA : A ≠ ∅) : A.inhab := begin apply classical.by_contradiction, intro hni, apply hA, rw eq_empty, intros z hz, apply hni, exact ⟨_, hz⟩, end lemma vacuous {p : Set → Prop} : ∀ x : Set, x ∈ (∅ : Set) → p x := begin intros x hx, exfalso, exact mem_empty _ hx, end theorem exists_unique_of_exists {p : Set → Prop} (h : ∃ x : Set, ∀ z, z ∈ x ↔ p z) : ∃! x : Set, ∀ z, z ∈ x ↔ p z := begin cases h with x h, refine ⟨x, h, (λ y H, _)⟩, refine ext (λ z, _), simp only [h z, H z], end theorem exists_unique_Inter {A : Set} (h : A.inhab) : ∃! B : Set, ∀ x, x ∈ B ↔ ∀ z ∈ A, x ∈ z := begin apply exists_unique_of_exists, cases h with c hin, refine ⟨{x ∈ c | ∀ z ∈ A, x ∈ z}, (λ z, _)⟩, simp only [mem_sep], split, rintro ⟨hz, ha⟩, exact ha, intro h, exact ⟨h c hin, h⟩, end theorem not_exists_Inter_empty : ¬ ∃ B : Set, ∀ x, x ∈ B ↔ ∀ z ∈ (∅ : Set), x ∈ z := begin rintro ⟨B, h⟩, apply univ_not_set, refine ⟨B, _⟩, intro x, specialize h x, rw h, intros a he, exfalso, exact mem_empty a he, end theorem no_abs_complement {A : Set} [decidable_pred (λ x, x ∈ A)] : ¬ ∃ B : Set, ∀ x, x ∈ B ↔ x ∉ A := begin rintro ⟨B, h⟩, apply univ_not_set, refine ⟨A ∪ B, _⟩, intro x, specialize h x, simp only [mem_union, h], exact decidable.em _, end theorem p10 {a B : Set} (h : a ∈ B) : a.powerset ∈ B.Union.powerset.powerset := begin rw mem_powerset, intros x h₂, rw mem_powerset at h₂, rw mem_powerset, intros z h₃, rw mem_Union, exact ⟨a, h, h₂ h₃⟩, end @[simp] lemma mem_Inter {A x : Set} : x ∈ A.Inter ↔ A.inhab ∧ ∀ z ∈ A, x ∈ z := begin simp [Inter, mem_sep, inhab], intro ha, split, { rintro ⟨z, hz, hx⟩, exact ⟨_, hz⟩, }, { rintro ⟨x, hx⟩, exact ⟨_, hx, ha _ hx⟩, }, end lemma subset_self {A : Set} : A ⊆ A := (λ x hx, hx) lemma eq_iff_subset_and_subset {A B : Set} : A = B ↔ A ⊆ B ∧ B ⊆ A := begin split, intro he, rw he, exact ⟨subset_self, subset_self⟩, rintro ⟨ha, hb⟩, apply ext, intro x, rw iff_iff_implies_and_implies, rw subset_def at *, exact ⟨@ha x, @hb x⟩, end lemma diff_ne_empty_of_ne {B A : Set} (hBA : B ⊆ A) (hne : B ≠ A) : (A \ B) ≠ ∅ := begin intro he, apply hne, rw eq_iff_subset_and_subset, refine ⟨hBA, _⟩, intros x hx, apply classical.by_contradiction, intro hxB, apply mem_empty x, rw ←he, rw mem_diff, exact ⟨hx, hxB⟩, end lemma subset_trans {A B C : Set} (hA : A ⊆ B) (hB : B ⊆ C) : A ⊆ C := (λ x hx, hB (hA hx)) lemma subset_Union {X Y : Set} (h : X ∈ Y) : X ⊆ Y.Union := begin simp only [subset_def, mem_Union, exists_prop], exact (λ z hz, ⟨_, h, hz⟩), end lemma subset_Inter {X Y : Set} (h : X ∈ Y) : Y.Inter ⊆ X := begin simp only [subset_def, mem_Inter], exact (λ z ⟨hi, ha⟩, ha _ h), end lemma mem_powerset_self {X : Set} : X ∈ X.powerset := begin rw mem_powerset, exact subset_self, end @[simp] lemma sep_subset {a : Set} {p : Set → Prop} : {x ∈ a | p x} ⊆ a := (λ x hx, (mem_sep.mp hx).left) theorem p21 {A B : Set} : (A ∪ B).Union = A.Union ∪ B.Union := begin apply ext, simp only [exists_prop, mem_union, mem_Union], intro x, split, { rintro ⟨X, (hX|hX), hx⟩, { left, exact ⟨_, hX, hx⟩, }, { right, exact ⟨_, hX, hx⟩, }, }, { rintro (⟨X, hX, hx⟩|⟨X, hX, hx⟩), { exact ⟨_, or.inl hX, hx⟩, }, { exact ⟨_, or.inr hX, hx⟩, }, }, end lemma union_comm {x y : Set} : x ∪ y = y ∪ x := begin apply ext, simp only [mem_union, or_comm, forall_const, iff_self], end lemma union_empty {x : Set} : x ∪ ∅ = x := begin apply ext, simp only [mem_union, mem_empty, forall_const, iff_self, or_false], end lemma inter_comm {x y : Set} : x ∩ y = y ∩ x := begin apply ext, simp only [mem_inter, and_comm, iff_self, implies_true_iff], end lemma inter_union {A B C : Set} : A ∩ (B ∪ C) = A ∩ B ∪ A ∩ C := begin apply ext, simp only [and_or_distrib_left, forall_const, mem_inter, iff_self, mem_union], end lemma union_inter {A B C : Set} : (A ∪ B) ∩ C = A ∩ C ∪ B ∩ C := begin apply ext, simp only [mem_inter, mem_union, or_and_distrib_right, forall_const, iff_self], end lemma union_assoc {A B C : Set} : A ∪ (B ∪ C) = (A ∪ B) ∪ C := begin apply ext, simp only [mem_union, or_assoc, forall_const, iff_self], end lemma inter_empty {A : Set} : A ∩ ∅ = ∅ := begin simp only [eq_empty, mem_inter, mem_empty, forall_const, not_false_iff, and_false], end lemma subset_union_left {A B : Set} : A ⊆ A ∪ B := subset_union_of_subset subset_self lemma subset_union_right {A B : Set} : B ⊆ A ∪ B := begin rw union_comm, exact subset_union_left, end lemma subset_union_of_subset_left {A B C : Set} (hAB : A ⊆ B) : A ⊆ B ∪ C := begin intros x hx, rw mem_union, exact or.inl (hAB hx), end lemma subset_union_of_subset_right {A B C : Set} (hAB : A ⊆ C) : A ⊆ B ∪ C := begin intros x hx, rw mem_union, exact or.inr (hAB hx), end lemma union_subset_of_subset_of_subset {A B C : Set} (hA : A ⊆ C) (hB : B ⊆ C) : (A ∪ B) ⊆ C := begin intro x, rw mem_union, rintro (hx|hx), { exact hA hx, }, { exact hB hx, }, end lemma union_eq_union_diff {A B : Set} : A ∪ B = A ∪ (B \ A) := begin apply ext, simp only [mem_union, mem_diff, or_and_distrib_left, classical.em, forall_const, and_true, iff_self], end lemma union_eq_left_of_subset {A B : Set} (hBA : B ⊆ A) : A ∪ B = A := begin rw eq_iff_subset_and_subset, exact ⟨union_subset_of_subset_of_subset subset_self hBA, subset_union_left⟩, end lemma union_diff_eq_self_of_subset {A B : Set} (hAB : A ⊆ B) : A ∪ (B \ A) = B := begin rw [←union_eq_union_diff, eq_iff_subset_and_subset], exact ⟨union_subset_of_subset_of_subset hAB subset_self, subset_union_right⟩, end lemma diff_singleton_union_eq {A x : Set} (xA : x ∈ A) : (A \ {x}) ∪ {x} = A := begin rw union_comm, apply union_diff_eq_self_of_subset, intros z zx, rw mem_singleton at zx, subst zx, exact xA, end lemma subset_diff {A B : Set} : A \ B ⊆ A := begin intros x hx, rw mem_diff at hx, exact hx.left, end lemma self_inter_diff_empty {A B : Set} : A ∩ (B \ A) = ∅ := begin simp [eq_empty, mem_inter, mem_diff], intros x hA hB, exact hA, end lemma subset_Union_of_mem {X K : Set} (hXK : X ∈ K) : X ⊆ K.Union := begin intros z hz, rw mem_Union, exact ⟨_, hXK, hz⟩, end lemma Union_powerset_subset_self {X : Set} : X.powerset.Union ⊆ X := begin intros z hz, simp only [mem_Union, exists_prop, mem_powerset] at hz, rcases hz with ⟨Y, hYX, hzy⟩, exact hYX hzy, end lemma Union_subset_of_subset_powerset {X Y : Set} (h : X ⊆ Y.powerset) : X.Union ⊆ Y := begin intros x hx, rw mem_Union at hx, rcases hx with ⟨Z, hZX, hxZ⟩, specialize h hZX, rw mem_powerset at h, exact h hxZ, end lemma diff_diff_eq_self_of_subset {A B : Set} (hBA : B ⊆ A) : A \ (A \ B) = B := begin apply ext, intro x, simp only [mem_diff], split, rintro ⟨hA, h⟩, apply classical.by_contradiction, intro hB, exact h ⟨hA, hB⟩, intro hB, refine ⟨hBA hB, _⟩, rintro ⟨hA, hB'⟩, exact hB' hB, end lemma empty_subset {A : Set} : ∅ ⊆ A := λ x : Set, assume hx : x ∈ ∅, show x ∈ A, from false.elim (mem_empty x hx) lemma prod_subset_of_subset_of_subset {A B : Set} (hAB : A ⊆ B) {C D : Set} (hCD : C ⊆ D) : A.prod C ⊆ B.prod D := begin intros z hz, simp only [mem_prod, exists_prop] at *, rcases hz with ⟨a, ha, c, hc, he⟩, exact ⟨_, hAB ha, _, hCD hc, he⟩, end lemma inter_eq_of_subset {A B : Set} (hAB : A ⊆ B) : B ∩ A = A := begin apply ext, intro x, rw mem_inter, split, rintro ⟨hB, hA⟩, exact hA, intro hA, exact ⟨hAB hA, hA⟩, end lemma inter_subset_right {A B : Set} : A ∩ B ⊆ B := begin intros x xAB, rw mem_inter at xAB, exact xAB.right, end lemma inter_subset_left {A B : Set} : A ∩ B ⊆ A := begin intros x xAB, rw mem_inter at xAB, exact xAB.left, end lemma diff_sub_diff_of_sub {A B : Set} (AB : A ⊆ B) {C : Set} : C \ B ⊆ C \ A := begin intros x xCB, rw mem_diff at *, exact ⟨xCB.left, assume xA, xCB.right (AB xA)⟩, end lemma mem_sep' {X : Set} {p : Set → Prop} {x : Set} (xX : x ∈ X) : x ∈ {x ∈ X | p x} ↔ p x := by simp only [mem_sep, xX, true_and] lemma inter_eq_of_sub {A B : Set} (AB : A ⊆ B) : A ∩ B = A := begin apply ext, intro x, rw mem_inter, split, rintro ⟨xA, -⟩, exact xA, intro xA, exact ⟨xA, AB xA⟩, end lemma singleton_ne_empty {x : Set} : ({x} : Set) ≠ ∅ := begin apply ne_empty_of_inhabited, use x, rw mem_singleton, end lemma sub_inter_of_sub {x a b : Set} (xa : x ⊆ a) (xb : x ⊆ b) : x ⊆ a ∩ b := begin intros z zx, rw mem_inter, exact ⟨xa zx, xb zx⟩, end lemma Union_sub_of_sub {A B : Set} (AB : A ⊆ B) : A.Union ⊆ B.Union := begin simp only [subset_def, mem_Union, exists_prop], rintros x ⟨y, yA, xy⟩, exact ⟨_, AB yA, xy⟩, end lemma Union_sub {A X : Set} (h : ∀ {x : Set}, x ∈ X → x ⊆ A) : X.Union ⊆ A := begin intro z, rw mem_Union, rintro ⟨x, xX, zx⟩, exact h xX zx, end lemma Union_diff_empty_eq {A : Set} : A.Union = (A \ {∅}).Union := begin apply ext, simp only [mem_Union, exists_prop, mem_diff, mem_singleton], intro x, split, rintro ⟨y, yA, xy⟩, refine ⟨_, ⟨yA, _⟩, xy⟩, apply ne_empty_of_inhabited, rw inhab, exact ⟨_, xy⟩, rintro ⟨y, ⟨yA, -⟩, xy⟩, finish, end lemma union_eq_Union {A B : Set} : A ∪ B = Union {A, B} := begin apply ext, simp only [mem_union, mem_Union, exists_prop, mem_insert, mem_singleton], intro x, split, finish, rintro ⟨z, (hz|hz), xz⟩; finish, end lemma Union_empty : Union ∅ = ∅ := begin rw eq_empty, intros z h, rw mem_Union at h, rcases h with ⟨x, h, -⟩, exact mem_empty _ h, end lemma inhab_of_Union_inhab {S : Set} (inh : S.Union.inhab) : S.inhab := begin obtain ⟨x, hx⟩ := inh, rw mem_Union at hx, rcases hx with ⟨y, yS, xy⟩, exact ⟨_, yS⟩, end end Set
eb38384a66270eeac1813be42c7acd9d5bc2ff2c
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/dynamics/circle/rotation_number/translation_number.lean
3e464fa42239230a88e095e06a9c57d6f13451c5
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
28,239
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import analysis.specific_limits import order.iterate import algebra.iterate_hom /-! # Translation number of a monotone real map that commutes with `x ↦ x + 1` Let `f : ℝ → ℝ` be a monotone map such that `f (x + 1) = f x + 1` for all `x`. Then the limit $$ \tau(f)=\lim_{n\to\infty}{f^n(x)-x}{n} $$ exists and does not depend on `x`. This number is called the *translation number* of `f`. Different authors use different notation for this number: `τ`, `ρ`, `rot`, etc In this file we define a structure `circle_deg1_lift` for bundled maps with these properties, define translation number of `f : circle_deg1_lift`, prove some estimates relating `f^n(x)-x` to `τ(f)`. In case of a continuous map `f` we also prove that `f` admits a point `x` such that `f^n(x)=x+m` if and only if `τ(f)=m/n`. Maps of this type naturally appear as lifts of orientation preserving circle homeomorphisms. More precisely, let `f` be an orientation preserving homeomorphism of the circle $S^1=ℝ/ℤ$, and consider a real number `a` such that `⟦a⟧ = f 0`, where `⟦⟧` means the natural projection `ℝ → ℝ/ℤ`. Then there exists a unique continuous function `F : ℝ → ℝ` such that `F 0 = a` and `⟦F x⟧ = f ⟦x⟧` for all `x` (this fact is not formalized yet). This function is strictly monotone, continuous, and satisfies `F (x + 1) = F x + 1`. The number `⟦τ F⟧ : ℝ / ℤ` is called the *rotation number* of `f`. It does not depend on the choice of `a`. We chose to define translation number for a wider class of maps `f : ℝ → ℝ` for two reasons: * non-strictly monotone circle self-maps with discontinuities naturally appear as Poincaré maps for some flows on the two-torus (e.g., one can take a constant flow and glue in a few Cherry cells); * definition and some basic properties still work for this class. ## Notation We use a local notation `τ` for the translation number of `f : circle_deg1_lift`. ## Tags circle homeomorphism, rotation number -/ open filter set open_locale topological_space classical /-! ### Definition and monoid structure -/ /-- A lift of a monotone degree one map `S¹ → S¹`. -/ structure circle_deg1_lift : Type := (to_fun : ℝ → ℝ) (monotone' : monotone to_fun) (map_add_one' : ∀ x, to_fun (x + 1) = to_fun x + 1) namespace circle_deg1_lift instance : has_coe_to_fun circle_deg1_lift := ⟨λ _, ℝ → ℝ, circle_deg1_lift.to_fun⟩ @[simp] lemma coe_mk (f h₁ h₂) : ⇑(mk f h₁ h₂) = f := rfl variables (f g : circle_deg1_lift) protected lemma monotone : monotone f := f.monotone' @[mono] lemma mono {x y} (h : x ≤ y) : f x ≤ f y := f.monotone h @[simp] lemma map_add_one : ∀ x, f (x + 1) = f x + 1 := f.map_add_one' @[simp] lemma map_one_add (x : ℝ) : f (1 + x) = 1 + f x := by rw [add_comm, map_add_one, add_comm] theorem coe_inj : ∀ ⦃f g : circle_deg1_lift ⦄, (f : ℝ → ℝ) = g → f = g := assume ⟨f, fm, fd⟩ ⟨g, gm, gd⟩ h, by congr; exact h @[ext] theorem ext ⦃f g : circle_deg1_lift ⦄ (h : ∀ x, f x = g x) : f = g := coe_inj $ funext h theorem ext_iff {f g : circle_deg1_lift} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ instance : monoid circle_deg1_lift := { mul := λ f g, { to_fun := f ∘ g, monotone' := f.monotone.comp g.monotone, map_add_one' := λ x, by simp [map_add_one] }, one := ⟨id, monotone_id, λ _, rfl⟩, mul_one := λ f, coe_inj $ function.comp.right_id f, one_mul := λ f, coe_inj $ function.comp.left_id f, mul_assoc := λ f₁ f₂ f₃, coe_inj rfl } instance : inhabited circle_deg1_lift := ⟨1⟩ @[simp] lemma coe_mul : ⇑(f * g) = f ∘ g := rfl lemma mul_apply (x) : (f * g) x = f (g x) := rfl @[simp] lemma coe_one : ⇑(1 : circle_deg1_lift) = id := rfl instance units_has_coe_to_fun : has_coe_to_fun (units circle_deg1_lift) := ⟨λ _, ℝ → ℝ, λ f, ⇑(f : circle_deg1_lift)⟩ @[simp, norm_cast] lemma units_coe (f : units circle_deg1_lift) : ⇑(f : circle_deg1_lift) = f := rfl lemma coe_pow : ∀ n : ℕ, ⇑(f^n) = (f^[n]) | 0 := rfl | (n+1) := by {ext x, simp [coe_pow n, pow_succ'] } lemma semiconj_by_iff_semiconj {f g₁ g₂ : circle_deg1_lift} : semiconj_by f g₁ g₂ ↔ function.semiconj f g₁ g₂ := ext_iff lemma commute_iff_commute {f g : circle_deg1_lift} : commute f g ↔ function.commute f g := ext_iff /-! ### Translate by a constant -/ /-- The map `y ↦ x + y` as a `circle_deg1_lift`. More precisely, we define a homomorphism from `multiplicative ℝ` to `units circle_deg1_lift`, so the translation by `x` is `translation (multiplicative.of_add x)`. -/ def translate : multiplicative ℝ →* units circle_deg1_lift := by refine (units.map _).comp to_units.to_monoid_hom; exact { to_fun := λ x, ⟨λ y, x.to_add + y, λ y₁ y₂ h, add_le_add_left h _, λ y, (add_assoc _ _ _).symm⟩, map_one' := ext $ zero_add, map_mul' := λ x y, ext $ add_assoc _ _ } @[simp] lemma translate_apply (x y : ℝ) : translate (multiplicative.of_add x) y = x + y := rfl @[simp] lemma translate_inv_apply (x y : ℝ) : (translate $ multiplicative.of_add x)⁻¹ y = -x + y := rfl @[simp] lemma translate_gpow (x : ℝ) (n : ℤ) : (translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ n * x) := by simp only [← gsmul_eq_mul, of_add_gsmul, monoid_hom.map_gpow] @[simp] lemma translate_pow (x : ℝ) (n : ℕ) : (translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ n * x) := translate_gpow x n @[simp] lemma translate_iterate (x : ℝ) (n : ℕ) : (translate (multiplicative.of_add x))^[n] = translate (multiplicative.of_add $ n * x) := by rw [← units_coe, ← coe_pow, ← units.coe_pow, translate_pow, units_coe] /-! ### Commutativity with integer translations In this section we prove that `f` commutes with translations by an integer number. First we formulate these statements (for a natural or an integer number, addition on the left or on the right, addition or subtraction) using `function.commute`, then reformulate as `simp` lemmas `map_int_add` etc. -/ lemma commute_nat_add (n : ℕ) : function.commute f ((+) n) := by simpa only [nsmul_one, add_left_iterate] using function.commute.iterate_right f.map_one_add n lemma commute_add_nat (n : ℕ) : function.commute f (λ x, x + n) := by simp only [add_comm _ (n:ℝ), f.commute_nat_add n] lemma commute_sub_nat (n : ℕ) : function.commute f (λ x, x - n) := (f.commute_add_nat n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv lemma commute_add_int : ∀ n : ℤ, function.commute f (λ x, x + n) | (n:ℕ) := f.commute_add_nat n | -[1+n] := f.commute_sub_nat (n + 1) lemma commute_int_add (n : ℤ) : function.commute f ((+) n) := by simpa only [add_comm _ (n:ℝ)] using f.commute_add_int n lemma commute_sub_int (n : ℤ) : function.commute f (λ x, x - n) := (f.commute_add_int n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv @[simp] lemma map_int_add (m : ℤ) (x : ℝ) : f (m + x) = m + f x := f.commute_int_add m x @[simp] lemma map_add_int (x : ℝ) (m : ℤ) : f (x + m) = f x + m := f.commute_add_int m x @[simp] lemma map_sub_int (x : ℝ) (n : ℤ) : f (x - n) = f x - n := f.commute_sub_int n x @[simp] lemma map_add_nat (x : ℝ) (n : ℕ) : f (x + n) = f x + n := f.map_add_int x n @[simp] lemma map_nat_add (n : ℕ) (x : ℝ) : f (n + x) = n + f x := f.map_int_add n x @[simp] lemma map_sub_nat (x : ℝ) (n : ℕ) : f (x - n) = f x - n := f.map_sub_int x n lemma map_int_of_map_zero (n : ℤ) : f n = f 0 + n := by rw [← f.map_add_int, zero_add] @[simp] lemma map_fract_sub_fract_eq (x : ℝ) : f (fract x) - fract x = f x - x := by conv_rhs { rw [← fract_add_floor x, f.map_add_int, add_sub_comm, sub_self, add_zero] } /-! ### Pointwise order on circle maps -/ /-- Monotone circle maps form a lattice with respect to the pointwise order -/ noncomputable instance : lattice circle_deg1_lift := { sup := λ f g, { to_fun := λ x, max (f x) (g x), monotone' := λ x y h, max_le_max (f.mono h) (g.mono h), -- TODO: generalize to `monotone.max` map_add_one' := λ x, by simp [max_add_add_right] }, le := λ f g, ∀ x, f x ≤ g x, le_refl := λ f x, le_refl (f x), le_trans := λ f₁ f₂ f₃ h₁₂ h₂₃ x, le_trans (h₁₂ x) (h₂₃ x), le_antisymm := λ f₁ f₂ h₁₂ h₂₁, ext $ λ x, le_antisymm (h₁₂ x) (h₂₁ x), le_sup_left := λ f g x, le_max_left (f x) (g x), le_sup_right := λ f g x, le_max_right (f x) (g x), sup_le := λ f₁ f₂ f₃ h₁ h₂ x, max_le (h₁ x) (h₂ x), inf := λ f g, { to_fun := λ x, min (f x) (g x), monotone' := λ x y h, min_le_min (f.mono h) (g.mono h), map_add_one' := λ x, by simp [min_add_add_right] }, inf_le_left := λ f g x, min_le_left (f x) (g x), inf_le_right := λ f g x, min_le_right (f x) (g x), le_inf := λ f₁ f₂ f₃ h₂ h₃ x, le_min (h₂ x) (h₃ x) } @[simp] lemma sup_apply (x : ℝ) : (f ⊔ g) x = max (f x) (g x) := rfl @[simp] lemma inf_apply (x : ℝ) : (f ⊓ g) x = min (f x) (g x) := rfl lemma iterate_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^[n]) := λ f g h, f.monotone.iterate_le_of_le h _ lemma iterate_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^[n] ≤ (g^[n]) := iterate_monotone n h lemma pow_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^n ≤ g^n := λ x, by simp only [coe_pow, iterate_mono h n x] lemma pow_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^n) := λ f g h, pow_mono h n /-! ### Estimates on `(f * g) 0` We prove the estimates `f 0 + ⌊g 0⌋ ≤ f (g 0) ≤ f 0 + ⌈g 0⌉` and some corollaries with added/removed floors and ceils. We also prove that for two semiconjugate maps `g₁`, `g₂`, the distance between `g₁ 0` and `g₂ 0` is less than two. -/ lemma map_le_of_map_zero (x : ℝ) : f x ≤ f 0 + ⌈x⌉ := calc f x ≤ f ⌈x⌉ : f.monotone $ le_ceil _ ... = f 0 + ⌈x⌉ : f.map_int_of_map_zero _ lemma map_map_zero_le : f (g 0) ≤ f 0 + ⌈g 0⌉ := f.map_le_of_map_zero (g 0) lemma floor_map_map_zero_le : ⌊f (g 0)⌋ ≤ ⌊f 0⌋ + ⌈g 0⌉ := calc ⌊f (g 0)⌋ ≤ ⌊f 0 + ⌈g 0⌉⌋ : floor_mono $ f.map_map_zero_le g ... = ⌊f 0⌋ + ⌈g 0⌉ : floor_add_int _ _ lemma ceil_map_map_zero_le : ⌈f (g 0)⌉ ≤ ⌈f 0⌉ + ⌈g 0⌉ := calc ⌈f (g 0)⌉ ≤ ⌈f 0 + ⌈g 0⌉⌉ : ceil_mono $ f.map_map_zero_le g ... = ⌈f 0⌉ + ⌈g 0⌉ : ceil_add_int _ _ lemma map_map_zero_lt : f (g 0) < f 0 + g 0 + 1 := calc f (g 0) ≤ f 0 + ⌈g 0⌉ : f.map_map_zero_le g ... < f 0 + (g 0 + 1) : add_lt_add_left (ceil_lt_add_one _) _ ... = f 0 + g 0 + 1 : (add_assoc _ _ _).symm lemma le_map_of_map_zero (x : ℝ) : f 0 + ⌊x⌋ ≤ f x := calc f 0 + ⌊x⌋ = f ⌊x⌋ : (f.map_int_of_map_zero _).symm ... ≤ f x : f.monotone $ floor_le _ lemma le_map_map_zero : f 0 + ⌊g 0⌋ ≤ f (g 0) := f.le_map_of_map_zero (g 0) lemma le_floor_map_map_zero : ⌊f 0⌋ + ⌊g 0⌋ ≤ ⌊f (g 0)⌋ := calc ⌊f 0⌋ + ⌊g 0⌋ = ⌊f 0 + ⌊g 0⌋⌋ : (floor_add_int _ _).symm ... ≤ ⌊f (g 0)⌋ : floor_mono $ f.le_map_map_zero g lemma le_ceil_map_map_zero : ⌈f 0⌉ + ⌊g 0⌋ ≤ ⌈(f * g) 0⌉ := calc ⌈f 0⌉ + ⌊g 0⌋ = ⌈f 0 + ⌊g 0⌋⌉ : (ceil_add_int _ _).symm ... ≤ ⌈f (g 0)⌉ : ceil_mono $ f.le_map_map_zero g lemma lt_map_map_zero : f 0 + g 0 - 1 < f (g 0) := calc f 0 + g 0 - 1 = f 0 + (g 0 - 1) : add_assoc _ _ _ ... < f 0 + ⌊g 0⌋ : add_lt_add_left (sub_one_lt_floor _) _ ... ≤ f (g 0) : f.le_map_map_zero g lemma dist_map_map_zero_lt : dist (f 0 + g 0) (f (g 0)) < 1 := begin rw [dist_comm, real.dist_eq, abs_lt, lt_sub_iff_add_lt', sub_lt_iff_lt_add'], exact ⟨f.lt_map_map_zero g, f.map_map_zero_lt g⟩ end lemma dist_map_zero_lt_of_semiconj {f g₁ g₂ : circle_deg1_lift} (h : function.semiconj f g₁ g₂) : dist (g₁ 0) (g₂ 0) < 2 := calc dist (g₁ 0) (g₂ 0) ≤ dist (g₁ 0) (f (g₁ 0) - f 0) + dist _ (g₂ 0) : dist_triangle _ _ _ ... = dist (f 0 + g₁ 0) (f (g₁ 0)) + dist (g₂ 0 + f 0) (g₂ (f 0)) : by simp only [h.eq, real.dist_eq, sub_sub, add_comm (f 0), sub_sub_assoc_swap, abs_sub (g₂ (f 0))] ... < 2 : add_lt_add (f.dist_map_map_zero_lt g₁) (g₂.dist_map_map_zero_lt f) lemma dist_map_zero_lt_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (h : semiconj_by f g₁ g₂) : dist (g₁ 0) (g₂ 0) < 2 := dist_map_zero_lt_of_semiconj $ semiconj_by_iff_semiconj.1 h /-! ### Estimates on `(f^n) x` If we know that `f x` is `≤`/`<`/`≥`/`>`/`=` to `x + m`, then we have a similar estimate on `f^[n] x` and `x + n * m`. For `≤`, `≥`, and `=` we formulate both `of` (implication) and `iff` versions because implications work for `n = 0`. For `<` and `>` we formulate only `iff` versions. -/ lemma iterate_le_of_map_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) (n : ℕ) : f^[n] x ≤ x + n * m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_le_of_map_le f.monotone (monotone_id.add_const m) h n lemma le_iterate_of_add_int_le_map {x : ℝ} {m : ℤ} (h : x + m ≤ f x) (n : ℕ) : x + n * m ≤ (f^[n]) x := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).symm.iterate_le_of_map_le (monotone_id.add_const m) f.monotone h n lemma iterate_eq_of_map_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) (n : ℕ) : f^[n] x = x + n * m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_eq_of_map_eq n h lemma iterate_pos_le_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x ≤ x + n * m ↔ f x ≤ x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_le_iff_map_le f.monotone (strict_mono_id.add_const m) hn lemma iterate_pos_lt_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x < x + n * m ↔ f x < x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_lt_iff_map_lt f.monotone (strict_mono_id.add_const m) hn lemma iterate_pos_eq_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x = x + n * m ↔ f x = x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_eq_iff_map_eq f.monotone (strict_mono_id.add_const m) hn lemma le_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + n * m ≤ (f^[n]) x ↔ x + m ≤ f x := by simpa only [not_lt] using not_congr (f.iterate_pos_lt_iff hn) lemma lt_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + n * m < (f^[n]) x ↔ x + m < f x := by simpa only [not_le] using not_congr (f.iterate_pos_le_iff hn) lemma mul_floor_map_zero_le_floor_iterate_zero (n : ℕ) : ↑n * ⌊f 0⌋ ≤ ⌊(f^[n] 0)⌋ := begin rw [le_floor, int.cast_mul, int.cast_coe_nat, ← zero_add ((n : ℝ) * _)], apply le_iterate_of_add_int_le_map, simp [floor_le] end /-! ### Definition of translation number -/ noncomputable theory /-- An auxiliary sequence used to define the translation number. -/ def transnum_aux_seq (n : ℕ) : ℝ := (f^(2^n)) 0 / 2^n /-- The translation number of a `circle_deg1_lift`, $τ(f)=\lim_{n→∞}\frac{f^n(x)-x}{n}$. We use an auxiliary sequence `\frac{f^{2^n}(0)}{2^n}` to define `τ(f)` because some proofs are simpler this way. -/ def translation_number : ℝ := lim at_top f.transnum_aux_seq -- TODO: choose two different symbols for `circle_deg1_lift.translation_number` and the future -- `circle_mono_homeo.rotation_number`, then make them `localized notation`s local notation `τ` := translation_number lemma transnum_aux_seq_def : f.transnum_aux_seq = λ n : ℕ, (f^(2^n)) 0 / 2^n := rfl lemma translation_number_eq_of_tendsto_aux {τ' : ℝ} (h : tendsto f.transnum_aux_seq at_top (𝓝 τ')) : τ f = τ' := h.lim_eq lemma translation_number_eq_of_tendsto₀ {τ' : ℝ} (h : tendsto (λ n:ℕ, f^[n] 0 / n) at_top (𝓝 τ')) : τ f = τ' := f.translation_number_eq_of_tendsto_aux $ by simpa [(∘), transnum_aux_seq_def, coe_pow] using h.comp (nat.tendsto_pow_at_top_at_top_of_one_lt one_lt_two) lemma translation_number_eq_of_tendsto₀' {τ' : ℝ} (h : tendsto (λ n:ℕ, f^[n + 1] 0 / (n + 1)) at_top (𝓝 τ')) : τ f = τ' := f.translation_number_eq_of_tendsto₀ $ (tendsto_add_at_top_iff_nat 1).1 h lemma transnum_aux_seq_zero : f.transnum_aux_seq 0 = f 0 := by simp [transnum_aux_seq] lemma transnum_aux_seq_dist_lt (n : ℕ) : dist (f.transnum_aux_seq n) (f.transnum_aux_seq (n+1)) < (1 / 2) / (2^n) := begin have : 0 < (2^(n+1):ℝ) := pow_pos zero_lt_two _, rw [div_div_eq_div_mul, ← pow_succ, ← abs_of_pos this], replace := abs_pos_iff.2 (ne_of_gt this), convert (div_lt_div_right this).2 ((f^(2^n)).dist_map_map_zero_lt (f^(2^n))), simp_rw [transnum_aux_seq, real.dist_eq], rw [← abs_div, sub_div, pow_succ, ← two_mul, mul_div_mul_left _ _ (@two_ne_zero ℝ _), nat.pow_succ, pow_mul, pow_two, mul_apply] end lemma tendsto_translation_number_aux : tendsto f.transnum_aux_seq at_top (𝓝 $ τ f) := (cauchy_seq_of_le_geometric_two 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n)).tendsto_lim lemma dist_map_zero_translation_number_le : dist (f 0) (τ f) ≤ 1 := f.transnum_aux_seq_zero ▸ dist_le_of_le_geometric_two_of_tendsto₀ 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n) f.tendsto_translation_number_aux lemma tendsto_translation_number_of_dist_bounded_aux (x : ℕ → ℝ) (C : ℝ) (H : ∀ n : ℕ, dist ((f^n) 0) (x n) ≤ C) : tendsto (λ n : ℕ, x (2^n) / (2^n)) at_top (𝓝 $ τ f) := begin refine f.tendsto_translation_number_aux.congr_dist (squeeze_zero (λ _, dist_nonneg) _ _), { exact λ n, C / 2^n }, { intro n, have : 0 < (2^n:ℝ) := pow_pos zero_lt_two _, convert (div_le_div_right this).2 (H (2^n)), rw [transnum_aux_seq, real.dist_eq, ← sub_div, abs_div, abs_of_pos this, real.dist_eq] }, { exact mul_zero C ▸ tendsto_const_nhds.mul (tendsto_inv_at_top_zero.comp $ tendsto_pow_at_top_at_top_of_one_lt one_lt_two) } end lemma translation_number_eq_of_dist_bounded {f g : circle_deg1_lift} (C : ℝ) (H : ∀ n : ℕ, dist ((f^n) 0) ((g^n) 0) ≤ C) : τ f = τ g := eq.symm $ g.translation_number_eq_of_tendsto_aux $ f.tendsto_translation_number_of_dist_bounded_aux _ C H @[simp] lemma translation_number_map_id : τ 1 = 0 := translation_number_eq_of_tendsto₀ _ $ by simp [tendsto_const_nhds] lemma translation_number_eq_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (H : semiconj_by f g₁ g₂) : τ g₁ = τ g₂ := translation_number_eq_of_dist_bounded 2 $ λ n, le_of_lt $ dist_map_zero_lt_of_semiconj_by $ H.pow_right n lemma translation_number_eq_of_semiconj {f g₁ g₂ : circle_deg1_lift} (H : function.semiconj f g₁ g₂) : τ g₁ = τ g₂ := translation_number_eq_of_semiconj_by $ semiconj_by_iff_semiconj.2 H lemma translation_number_mul_of_commute {f g : circle_deg1_lift} (h : commute f g) : τ (f * g) = τ f + τ g := begin have : tendsto (λ n : ℕ, ((λ k, (f^k) 0 + (g^k) 0) (2^n)) / (2^n)) at_top (𝓝 $ τ f + τ g) := ((f.tendsto_translation_number_aux.add g.tendsto_translation_number_aux).congr $ λ n, (add_div ((f^(2^n)) 0) ((g^(2^n)) 0) ((2:ℝ)^n)).symm), refine tendsto_nhds_unique ((f * g).tendsto_translation_number_of_dist_bounded_aux _ 1 (λ n, _)) this, rw [h.mul_pow, dist_comm], exact le_of_lt ((f^n).dist_map_map_zero_lt (g^n)) end @[simp] lemma translation_number_pow : ∀ n : ℕ, τ (f^n) = n * τ f | 0 := by simp | (n+1) := by rw [pow_succ', translation_number_mul_of_commute (commute.pow_self f n), translation_number_pow n, nat.cast_add_one, add_mul, one_mul] @[simp] lemma translation_number_conj_eq (f : units circle_deg1_lift) (g : circle_deg1_lift) : τ (↑f * g * ↑(f⁻¹)) = τ g := (translation_number_eq_of_semiconj_by (f.mk_semiconj_by g)).symm @[simp] lemma translation_number_conj_eq' (f : units circle_deg1_lift) (g : circle_deg1_lift) : τ (↑(f⁻¹) * g * f) = τ g := translation_number_conj_eq f⁻¹ g lemma dist_pow_map_zero_mul_translation_number_le (n:ℕ) : dist ((f^n) 0) (n * f.translation_number) ≤ 1 := f.translation_number_pow n ▸ (f^n).dist_map_zero_translation_number_le lemma tendsto_translation_number₀' : tendsto (λ n:ℕ, (f^(n+1)) 0 / (n+1)) at_top (𝓝 $ τ f) := begin refine (tendsto_iff_dist_tendsto_zero.2 $ squeeze_zero (λ _, dist_nonneg) (λ n, _) ((tendsto_const_div_at_top_nhds_0_nat 1).comp (tendsto_add_at_top_nat 1))), dsimp, have : (0:ℝ) < n + 1 := n.cast_add_one_pos, rw [real.dist_eq, div_sub' _ _ _ (ne_of_gt this), abs_div, ← real.dist_eq, abs_of_pos this, div_le_div_right this, ← nat.cast_add_one], apply dist_pow_map_zero_mul_translation_number_le end lemma tendsto_translation_number₀ : tendsto (λ n:ℕ, ((f^n) 0) / n) at_top (𝓝 $ τ f) := (tendsto_add_at_top_iff_nat 1).1 f.tendsto_translation_number₀' /-- For any `x : ℝ` the sequence $\frac{f^n(x)-x}{n}$ tends to the translation number of `f`. In particular, this limit does not depend on `x`. -/ lemma tendsto_translation_number (x : ℝ) : tendsto (λ n:ℕ, ((f^n) x - x) / n) at_top (𝓝 $ τ f) := begin rw [← translation_number_conj_eq' (translate $ multiplicative.of_add x)], convert tendsto_translation_number₀ _, ext n, simp [sub_eq_neg_add, units.conj_pow'] end lemma tendsto_translation_number' (x : ℝ) : tendsto (λ n:ℕ, ((f^(n+1)) x - x) / (n+1)) at_top (𝓝 $ τ f) := (tendsto_add_at_top_iff_nat 1).2 (f.tendsto_translation_number x) lemma translation_number_mono : monotone τ := λ f g h, le_of_tendsto_of_tendsto' f.tendsto_translation_number₀ g.tendsto_translation_number₀ $ λ n, div_le_div_of_le_of_nonneg (pow_mono h n 0) n.cast_nonneg lemma translation_number_translate (x : ℝ) : τ (translate $ multiplicative.of_add x) = x := translation_number_eq_of_tendsto₀' _ $ by simp [nat.cast_add_one_ne_zero, mul_div_cancel_left, tendsto_const_nhds] lemma translation_number_le_of_le_add {z : ℝ} (hz : ∀ x, f x ≤ x + z) : τ f ≤ z := translation_number_translate z ▸ translation_number_mono (λ x, trans_rel_left _ (hz x) (add_comm _ _)) lemma le_translation_number_of_add_le {z : ℝ} (hz : ∀ x, x + z ≤ f x) : z ≤ τ f := translation_number_translate z ▸ translation_number_mono (λ x, trans_rel_right _ (add_comm _ _) (hz x)) lemma translation_number_le_of_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) : τ f ≤ m := le_of_tendsto' (f.tendsto_translation_number' x) $ λ n, (div_le_iff' (n.cast_add_one_pos : (0 : ℝ) < _)).mpr $ sub_le_iff_le_add'.2 $ (coe_pow f (n + 1)).symm ▸ f.iterate_le_of_map_le_add_int h (n + 1) lemma translation_number_le_of_le_add_nat {x : ℝ} {m : ℕ} (h : f x ≤ x + m) : τ f ≤ m := @translation_number_le_of_le_add_int f x m h lemma le_translation_number_of_add_int_le {x : ℝ} {m : ℤ} (h : x + m ≤ f x) : ↑m ≤ τ f := ge_of_tendsto' (f.tendsto_translation_number' x) $ λ n, (le_div_iff (n.cast_add_one_pos : (0 : ℝ) < _)).mpr $ le_sub_iff_add_le'.2 $ by simp only [coe_pow, mul_comm (m:ℝ), ← nat.cast_add_one, f.le_iterate_of_add_int_le_map h] lemma le_translation_number_of_add_nat_le {x : ℝ} {m : ℕ} (h : x + m ≤ f x) : ↑m ≤ τ f := @le_translation_number_of_add_int_le f x m h /-- If `f x - x` is an integer number `m` for some point `x`, then `τ f = m`. On the circle this means that a map with a fixed point has rotation number zero. -/ lemma translation_number_of_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) : τ f = m := le_antisymm (translation_number_le_of_le_add_int f $ le_of_eq h) (le_translation_number_of_add_int_le f $ le_of_eq h.symm) lemma floor_sub_le_translation_number (x : ℝ) : ↑⌊f x - x⌋ ≤ τ f := le_translation_number_of_add_int_le f $ le_sub_iff_add_le'.1 (floor_le $ f x - x) lemma translation_number_le_ceil_sub (x : ℝ) : τ f ≤ ⌈f x - x⌉ := translation_number_le_of_le_add_int f $ sub_le_iff_le_add'.1 (le_ceil $ f x - x) lemma map_lt_of_translation_number_lt_int {n : ℤ} (h : τ f < n) (x : ℝ) : f x < x + n := not_le.1 $ mt f.le_translation_number_of_add_int_le $ not_le.2 h lemma map_lt_of_translation_number_lt_nat {n : ℕ} (h : τ f < n) (x : ℝ) : f x < x + n := @map_lt_of_translation_number_lt_int f n h x lemma lt_map_of_int_lt_translation_number {n : ℤ} (h : ↑n < τ f) (x : ℝ) : x + n < f x := not_le.1 $ mt f.translation_number_le_of_le_add_int $ not_le.2 h lemma lt_map_of_nat_lt_translation_number {n : ℕ} (h : ↑n < τ f) (x : ℝ) : x + n < f x := @lt_map_of_int_lt_translation_number f n h x /-- If `f^n x - x`, `n > 0`, is an integer number `m` for some point `x`, then `τ f = m / n`. On the circle this means that a map with a periodic orbit has a rational rotation number. -/ lemma translation_number_of_map_pow_eq_add_int {x : ℝ} {n : ℕ} {m : ℤ} (h : (f^n) x = x + m) (hn : 0 < n) : τ f = m / n := begin have := (f^n).translation_number_of_eq_add_int h, rwa [translation_number_pow, mul_comm, ← eq_div_iff] at this, exact nat.cast_ne_zero.2 (ne_of_gt hn) end /-- If a predicate depends only on `f x - x` and holds for all `0 ≤ x ≤ 1`, then it holds for all `x`. -/ lemma forall_map_sub_of_Icc (P : ℝ → Prop) (h : ∀ x ∈ Icc (0:ℝ) 1, P (f x - x)) (x : ℝ) : P (f x - x) := f.map_fract_sub_fract_eq x ▸ h _ ⟨fract_nonneg _, le_of_lt (fract_lt_one _)⟩ lemma translation_number_lt_of_forall_lt_add (hf : continuous f) {z : ℝ} (hz : ∀ x, f x < x + z) : τ f < z := begin obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f y - y ≤ f x - x, from compact_Icc.exists_forall_ge (nonempty_Icc.2 zero_le_one) (hf.sub continuous_id).continuous_on, refine lt_of_le_of_lt _ (sub_lt_iff_lt_add'.2 $ hz x), apply translation_number_le_of_le_add, simp only [← sub_le_iff_le_add'], exact f.forall_map_sub_of_Icc (λ a, a ≤ f x - x) hx end lemma lt_translation_number_of_forall_add_lt (hf : continuous f) {z : ℝ} (hz : ∀ x, x + z < f x) : z < τ f := begin obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f x - x ≤ f y - y, from compact_Icc.exists_forall_le (nonempty_Icc.2 zero_le_one) (hf.sub continuous_id).continuous_on, refine lt_of_lt_of_le (lt_sub_iff_add_lt'.2 $ hz x) _, apply le_translation_number_of_add_le, simp only [← le_sub_iff_add_le'], exact f.forall_map_sub_of_Icc _ hx end /-- If `f` is a continuous monotone map `ℝ → ℝ`, `f (x + 1) = f x + 1`, then there exists `x` such that `f x = x + τ f`. -/ lemma exists_eq_add_translation_number (hf : continuous f) : ∃ x, f x = x + τ f := begin obtain ⟨a, ha⟩ : ∃ x, f x ≤ x + f.translation_number, { by_contradiction H, push_neg at H, exact lt_irrefl _ (f.lt_translation_number_of_forall_add_lt hf H) }, obtain ⟨b, hb⟩ : ∃ x, x + τ f ≤ f x, { by_contradiction H, push_neg at H, exact lt_irrefl _ (f.translation_number_lt_of_forall_lt_add hf H) }, exact intermediate_value_univ₂ hf (continuous_id.add continuous_const) ha hb end lemma translation_number_eq_int_iff (hf : continuous f) {m : ℤ} : τ f = m ↔ ∃ x, f x = x + m := begin refine ⟨λ h, h ▸ f.exists_eq_add_translation_number hf, _⟩, rintros ⟨x, hx⟩, exact f.translation_number_of_eq_add_int hx end lemma continuous_pow (hf : continuous f) (n : ℕ) : continuous ⇑(f^n : circle_deg1_lift) := by { rw coe_pow, exact hf.iterate n } lemma translation_number_eq_rat_iff (hf : continuous f) {m : ℤ} {n : ℕ} (hn : 0 < n) : τ f = m / n ↔ ∃ x, (f^n) x = x + m := begin rw [eq_div_iff, mul_comm, ← translation_number_pow]; [skip, exact ne_of_gt (nat.cast_pos.2 hn)], exact (f^n).translation_number_eq_int_iff (f.continuous_pow hf n) end end circle_deg1_lift
b7d9115d4eb42eed961bb9f488a23345f7ac5c79
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/1433.lean
2d49585860a6325735e43625c85fd145349905b7
[ "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
282
lean
protected def num_denum := ℤ × {d:ℤ // d > 0} protected def rel : num_denum → num_denum → Prop | ⟨n₁, ⟨d₁, _⟩⟩ ⟨n₂, ⟨d₂, _⟩⟩ := n₁ = n₂ example : ∀(a : num_denum), rel a a := λ⟨n₁, ⟨d₁, h₁⟩⟩, show n₁ = n₁, begin [smt] end
c0b29ce36a17e31317d9b4706af61e5abc277274
d744d97b07fc1e61b0ebea192b2d624125898128
/src/list.lean
d3fad4cd4558df3379c065c6386ae43303a3f617
[ "MIT" ]
permissive
parkersullivan/leanstuff
019c757848dd8f5db185e6973969141aee6fef7c
3da132c44d365ecd933de64fa04ba66b9e620683
refs/heads/master
1,599,036,040,845
1,573,316,625,000
1,573,316,625,000
219,181,711
0
0
null
null
null
null
UTF-8
Lean
false
false
1,904
lean
import .option namespace my_private open my_option inductive pol_list (α : Type) : Type | nil {} : pol_list | cons : α → pol_list → pol_list def f := @pol_list.cons ℕ 7 def g := @pol_list.cons ℕ 11 def pol_map {α β : Type} : (α → β) → pol_list α → pol_list β | f pol_list.nil := pol_list.nil | f (pol_list.cons h t) := (pol_list.cons (f h) (pol_map f t)) def is_even : ℕ → bool | n := n % 2 = 0 def pol_append {α : Type} : pol_list α → pol_list α → pol_list α | pol_list.nil l2 := l2 | (pol_list.cons h t) l2 := pol_list.cons h (pol_append t l2) /-list reverse function, filter: takes a function alpha to bool and a list of bools and a sub list of only the elemtns that return true-/ def reverse_list {α : Type} : pol_list α → pol_list α | pol_list.nil := pol_list.nil | (pol_list.cons h t) := pol_append (reverse_list t) (pol_list.cons h pol_list.nil) def filter {α : Type} : (α → bool) → pol_list α → pol_list α | f pol_list.nil := pol_list.nil | f (pol_list.cons h t) := if (f h) then pol_list.cons h (filter f t) else (filter f t) def fold {α β : Type} : (α → β → β) → β → pol_list α → β | f i pol_list.nil := i | f i (pol_list.cons h t) := (f h (fold f i t)) def fold_is_even : ℕ → bool → bool | h r := band (is_even h) r def head (α : Type) : pol_list α → pol_option α | pol_list.nil := pol_option.None α | (pol_list.cons h t) := pol_option.Some h def tail (α : Type) : pol_list α → pol_option (pol_list α) | pol_list.nil := pol_option.None (pol_list α) | (pol_list.cons h t) := pol_option.Some t def list_in {α : Type} [decidable_eq α]: pol_list α → α → bool | pol_list.nil x := ff | (pol_list.cons h t) x := if (h = x) then tt else if (list_in t x) then tt else ff end my_private
513c45981e532c2bd9563f626c5e5432537eac4e
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/run/inliner_loop.lean
30e8e27219a38bf8fd4fbe2f83ba86307446993f
[ "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
152
lean
new_frontend unsafe inductive t | mk : (t → t) → t unsafe def loop' : t → t | t.mk f => f (t.mk f) unsafe def loop : t := loop' (t.mk loop')
99f1b98e1ef60ebbe60c8e6d3c21e716a41b1ca1
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/types/lift.hlean
26236eeee539c47e50ceda0075e1ac6defd16456
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,119
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn Theorems about lift -/ import ..function open eq equiv equiv.ops is_equiv is_trunc pointed namespace lift universe variables u v variables {A : Type.{u}} (z z' : lift.{u v} A) protected definition eta : up (down z) = z := by induction z; reflexivity protected definition code [unfold 2 3] : lift A → lift A → Type | code (up a) (up a') := a = a' protected definition decode [unfold 2 3] : Π(z z' : lift A), lift.code z z' → z = z' | decode (up a) (up a') := λc, ap up c variables {z z'} protected definition encode [unfold 3 4 5] (p : z = z') : lift.code z z' := by induction p; induction z; esimp variables (z z') definition lift_eq_equiv : (z = z') ≃ lift.code z z' := equiv.MK lift.encode !lift.decode abstract begin intro c, induction z with a, induction z' with a', esimp at *, induction c, reflexivity end end abstract begin intro p, induction p, induction z, reflexivity end end section variables {a a' : A} definition eq_of_up_eq_up [unfold 4] (p : up a = up a') : a = a' := lift.encode p definition lift_transport {P : A → Type} (p : a = a') (z : lift (P a)) : p ▸ z = up (p ▸ down z) := by induction p; induction z; reflexivity end variables {A' : Type} (f : A → A') (g : lift A → lift A') definition lift_functor [unfold 4] : lift A → lift A' | lift_functor (up a) := up (f a) definition is_equiv_lift_functor [constructor] [Hf : is_equiv f] : is_equiv (lift_functor f) := adjointify (lift_functor f) (lift_functor f⁻¹) abstract begin intro z', induction z' with a', esimp, exact ap up !right_inv end end abstract begin intro z, induction z with a, esimp, exact ap up !left_inv end end definition lift_equiv_lift_of_is_equiv [constructor] [Hf : is_equiv f] : lift A ≃ lift A' := equiv.mk _ (is_equiv_lift_functor f) definition lift_equiv_lift [constructor] (f : A ≃ A') : lift A ≃ lift A' := equiv.mk _ (is_equiv_lift_functor f) definition lift_equiv_lift_refl (A : Type) : lift_equiv_lift (erfl : A ≃ A) = erfl := by apply equiv_eq'; intro z; induction z with a; reflexivity definition lift_inv_functor [unfold_full] (a : A) : A' := down (g (up a)) definition is_equiv_lift_inv_functor [constructor] [Hf : is_equiv g] : is_equiv (lift_inv_functor g) := adjointify (lift_inv_functor g) (lift_inv_functor g⁻¹) abstract begin intro z', rewrite [▸*,lift.eta,right_inv g], end end abstract begin intro z', rewrite [▸*,lift.eta,left_inv g], end end definition equiv_of_lift_equiv_lift [constructor] (g : lift A ≃ lift A') : A ≃ A' := equiv.mk _ (is_equiv_lift_inv_functor g) definition lift_functor_left_inv : lift_inv_functor (lift_functor f) = f := eq_of_homotopy (λa, idp) definition lift_functor_right_inv : lift_functor (lift_inv_functor g) = g := begin apply eq_of_homotopy, intro z, induction z with a, esimp, apply lift.eta end variables (A A') definition is_equiv_lift_functor_fn [constructor] : is_equiv (lift_functor : (A → A') → (lift A → lift A')) := adjointify lift_functor lift_inv_functor lift_functor_right_inv lift_functor_left_inv definition lift_imp_lift_equiv [constructor] : (lift A → lift A') ≃ (A → A') := (equiv.mk _ (is_equiv_lift_functor_fn A A'))⁻¹ᵉ -- can we deduce this from lift_imp_lift_equiv? definition lift_equiv_lift_equiv [constructor] : (lift A ≃ lift A') ≃ (A ≃ A') := equiv.MK equiv_of_lift_equiv_lift lift_equiv_lift abstract begin intro f, apply equiv_eq, reflexivity end end abstract begin intro g, apply equiv_eq, esimp, apply eq_of_homotopy, intro z, induction z with a, esimp, apply lift.eta end end definition lift_eq_lift_equiv.{u1 u2} (A A' : Type.{u1}) : (lift.{u1 u2} A = lift.{u1 u2} A') ≃ (A = A') := !eq_equiv_equiv ⬝e !lift_equiv_lift_equiv ⬝e !eq_equiv_equiv⁻¹ᵉ definition is_embedding_lift [instance] : is_embedding lift := begin intro A A', fapply is_equiv.homotopy_closed, exact to_inv !lift_eq_lift_equiv, exact _, { intro p, induction p, esimp [lift_eq_lift_equiv,equiv.trans,equiv.symm,eq_equiv_equiv], rewrite [equiv_of_eq_refl,lift_equiv_lift_refl], apply ua_refl} end definition plift [constructor] (A : pType.{u}) : pType.{max u v} := pType.mk (lift A) (up pt) definition plift_functor [constructor] {A B : Type*} (f : A →* B) : plift A →* plift B := pmap.mk (lift_functor f) (ap up (respect_pt f)) -- is_trunc_lift is defined in init.trunc end lift
cb232878343643f2150b6c5b66c4141cf5762202
3dc4623269159d02a444fe898d33e8c7e7e9461b
/.github/workflows/project_1_a_decrire/Nouveau dossier/import tactic.ring.lean
d223e8606b76ca32e1805235561fcb5ce5adb458
[]
no_license
Or7ando/lean
cc003e6c41048eae7c34aa6bada51c9e9add9e66
d41169cf4e416a0d42092fb6bdc14131cee9dd15
refs/heads/master
1,650,600,589,722
1,587,262,906,000
1,587,262,906,000
255,387,160
0
0
null
null
null
null
UTF-8
Lean
false
false
1,356
lean
import tactic.ring import tactic.ring_exp import algebra.category.CommRing.basic open CommRing --- Goal : understand the notion of structure with a little exemple ! namespace co_maxi structure comax {R : Type}(a b: R) [comm_ring R] := (u : R)(v : R) (certificat : a * u + b * v = (1 : R)) #print comax open is_ring_hom --- to have acces map_mul map_one def comp (A B: Type)(φ : A → B)[comm_ring A][comm_ring B][is_ring_hom φ] (a b : A) : (comax a b) → comax (φ a) (φ b) := λ ⟨u_ab,v_ab,certificat_ab⟩, --- {} or ⟨ ⟩ λ OBJET and then OBJET.u etc begin have certificat : (φ a) * (φ u_ab) + (φ b) * (φ v_ab) = 1, -- It's trivial ring identity, but i have to control ! rw [ ← map_mul φ, ← map_mul φ , ← map_add φ,certificat_ab], apply map_one, exact {u := φ u_ab,v := φ v_ab,certificat := certificat}, --- the constructor of structure : i thinck better than ⟨ ⟩ end --- How to make comp beautiful fonctorial ? --- goal execute the proof with an example. --- For the moment we deal with zmod ring ! end co_maxi namespace Exemple --- This is a closed universe #print Ring --- but we have access to the primitive structure #print co_maxi.comax -- we have to co_maxi. first to acces comax ! end Exemple open co_maxi
71ea91d2f947a0e5447a02edb35b0ae89dc1ef01
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/real/nnreal.lean
7af447cae0bee0793b4b21294fa136bd5e3220de
[ "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
34,849
lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import algebra.big_operators.ring import data.real.pointwise import algebra.indicator_function import algebra.algebra.basic import algebra.order.module import algebra.order.nonneg /-! # Nonnegative real numbers In this file we define `nnreal` (notation: `ℝ≥0`) to be the type of non-negative real numbers, a.k.a. the interval `[0, ∞)`. We also define the following operations and structures on `ℝ≥0`: * the order on `ℝ≥0` is the restriction of the order on `ℝ`; these relations define a conditionally complete linear order with a bottom element, `conditionally_complete_linear_order_bot`; * `a + b` and `a * b` are the restrictions of addition and multiplication of real numbers to `ℝ≥0`; these operations together with `0 = ⟨0, _⟩` and `1 = ⟨1, _⟩` turn `ℝ≥0` into a conditionally complete linear ordered archimedean commutative semifield; we have no typeclass for this in `mathlib` yet, so we define the following instances instead: - `linear_ordered_semiring ℝ≥0`; - `ordered_comm_semiring ℝ≥0`; - `canonically_ordered_comm_semiring ℝ≥0`; - `linear_ordered_comm_group_with_zero ℝ≥0`; - `canonically_linear_ordered_add_monoid ℝ≥0`; - `archimedean ℝ≥0`; - `conditionally_complete_linear_order_bot ℝ≥0`. These instances are derived from corresponding instances about the type `{x : α // 0 ≤ x}` in an appropriate ordered field/ring/group/monoid `α`. See `algebra/order/nonneg`. * `real.to_nnreal x` is defined as `⟨max x 0, _⟩`, i.e. `↑(real.to_nnreal x) = x` when `0 ≤ x` and `↑(real.to_nnreal x) = 0` otherwise. We also define an instance `can_lift ℝ ℝ≥0`. This instance can be used by the `lift` tactic to replace `x : ℝ` and `hx : 0 ≤ x` in the proof context with `x : ℝ≥0` while replacing all occurences of `x` with `↑x`. This tactic also works for a function `f : α → ℝ` with a hypothesis `hf : ∀ x, 0 ≤ f x`. ## Notations This file defines `ℝ≥0` as a localized notation for `nnreal`. -/ open_locale classical big_operators /-- Nonnegative real numbers. -/ @[derive [ ordered_semiring, comm_monoid_with_zero, -- to ensure these instance are computable floor_semiring, comm_semiring, semiring, semilattice_inf, densely_ordered, order_bot, canonically_linear_ordered_semifield, linear_ordered_comm_group_with_zero, archimedean, linear_ordered_semiring, ordered_comm_semiring, canonically_ordered_comm_semiring, has_sub, has_ordered_sub, has_div, inhabited]] def nnreal := {r : ℝ // 0 ≤ r} localized "notation (name := nnreal) `ℝ≥0` := nnreal" in nnreal namespace nnreal instance : has_coe ℝ≥0 ℝ := ⟨subtype.val⟩ /- Simp lemma to put back `n.val` into the normal form given by the coercion. -/ @[simp] lemma val_eq_coe (n : ℝ≥0) : n.val = n := rfl instance : can_lift ℝ ℝ≥0 := subtype.can_lift _ protected lemma eq {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) → n = m := subtype.eq protected lemma eq_iff {n m : ℝ≥0} : (n : ℝ) = (m : ℝ) ↔ n = m := iff.intro nnreal.eq (congr_arg coe) lemma ne_iff {x y : ℝ≥0} : (x : ℝ) ≠ (y : ℝ) ↔ x ≠ y := not_iff_not_of_iff $ nnreal.eq_iff protected lemma «forall» {p : ℝ≥0 → Prop} : (∀ x : ℝ≥0, p x) ↔ ∀ (x : ℝ) (hx : 0 ≤ x), p ⟨x, hx⟩ := subtype.forall protected lemma «exists» {p : ℝ≥0 → Prop} : (∃ x : ℝ≥0, p x) ↔ ∃ (x : ℝ) (hx : 0 ≤ x), p ⟨x, hx⟩ := subtype.exists /-- Reinterpret a real number `r` as a non-negative real number. Returns `0` if `r < 0`. -/ noncomputable def _root_.real.to_nnreal (r : ℝ) : ℝ≥0 := ⟨max r 0, le_max_right _ _⟩ lemma _root_.real.coe_to_nnreal (r : ℝ) (hr : 0 ≤ r) : (real.to_nnreal r : ℝ) = r := max_eq_left hr lemma _root_.real.le_coe_to_nnreal (r : ℝ) : r ≤ real.to_nnreal r := le_max_left r 0 lemma coe_nonneg (r : ℝ≥0) : (0 : ℝ) ≤ r := r.2 @[norm_cast] theorem coe_mk (a : ℝ) (ha) : ((⟨a, ha⟩ : ℝ≥0) : ℝ) = a := rfl example : has_zero ℝ≥0 := by apply_instance example : has_one ℝ≥0 := by apply_instance example : has_add ℝ≥0 := by apply_instance noncomputable example : has_sub ℝ≥0 := by apply_instance example : has_mul ℝ≥0 := by apply_instance noncomputable example : has_inv ℝ≥0 := by apply_instance noncomputable example : has_div ℝ≥0 := by apply_instance example : has_le ℝ≥0 := by apply_instance example : has_bot ℝ≥0 := by apply_instance example : inhabited ℝ≥0 := by apply_instance example : nontrivial ℝ≥0 := by apply_instance protected lemma coe_injective : function.injective (coe : ℝ≥0 → ℝ) := subtype.coe_injective @[simp, norm_cast] protected lemma coe_eq {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) = r₂ ↔ r₁ = r₂ := nnreal.coe_injective.eq_iff protected lemma coe_zero : ((0 : ℝ≥0) : ℝ) = 0 := rfl protected lemma coe_one : ((1 : ℝ≥0) : ℝ) = 1 := rfl protected lemma coe_add (r₁ r₂ : ℝ≥0) : ((r₁ + r₂ : ℝ≥0) : ℝ) = r₁ + r₂ := rfl protected lemma coe_mul (r₁ r₂ : ℝ≥0) : ((r₁ * r₂ : ℝ≥0) : ℝ) = r₁ * r₂ := rfl protected lemma coe_inv (r : ℝ≥0) : ((r⁻¹ : ℝ≥0) : ℝ) = r⁻¹ := rfl protected lemma coe_div (r₁ r₂ : ℝ≥0) : ((r₁ / r₂ : ℝ≥0) : ℝ) = r₁ / r₂ := rfl @[simp, norm_cast] protected lemma coe_bit0 (r : ℝ≥0) : ((bit0 r : ℝ≥0) : ℝ) = bit0 r := rfl @[simp, norm_cast] protected lemma coe_bit1 (r : ℝ≥0) : ((bit1 r : ℝ≥0) : ℝ) = bit1 r := rfl protected lemma coe_two : ((2 : ℝ≥0) : ℝ) = 2 := rfl @[simp, norm_cast] protected lemma coe_sub {r₁ r₂ : ℝ≥0} (h : r₂ ≤ r₁) : ((r₁ - r₂ : ℝ≥0) : ℝ) = r₁ - r₂ := max_eq_left $ le_sub.2 $ by simp [show (r₂ : ℝ) ≤ r₁, from h] @[simp, norm_cast] protected lemma coe_eq_zero (r : ℝ≥0) : ↑r = (0 : ℝ) ↔ r = 0 := by rw [← nnreal.coe_zero, nnreal.coe_eq] @[simp, norm_cast] protected lemma coe_eq_one (r : ℝ≥0) : ↑r = (1 : ℝ) ↔ r = 1 := by rw [← nnreal.coe_one, nnreal.coe_eq] lemma coe_ne_zero {r : ℝ≥0} : (r : ℝ) ≠ 0 ↔ r ≠ 0 := by norm_cast example : comm_semiring ℝ≥0 := by apply_instance /-- Coercion `ℝ≥0 → ℝ` as a `ring_hom`. -/ def to_real_hom : ℝ≥0 →+* ℝ := ⟨coe, nnreal.coe_one, nnreal.coe_mul, nnreal.coe_zero, nnreal.coe_add⟩ @[simp] lemma coe_to_real_hom : ⇑to_real_hom = coe := rfl section actions /-- A `mul_action` over `ℝ` restricts to a `mul_action` over `ℝ≥0`. -/ instance {M : Type*} [mul_action ℝ M] : mul_action ℝ≥0 M := mul_action.comp_hom M to_real_hom.to_monoid_hom lemma smul_def {M : Type*} [mul_action ℝ M] (c : ℝ≥0) (x : M) : c • x = (c : ℝ) • x := rfl instance {M N : Type*} [mul_action ℝ M] [mul_action ℝ N] [has_smul M N] [is_scalar_tower ℝ M N] : is_scalar_tower ℝ≥0 M N := { smul_assoc := λ r, (smul_assoc (r : ℝ) : _)} instance smul_comm_class_left {M N : Type*} [mul_action ℝ N] [has_smul M N] [smul_comm_class ℝ M N] : smul_comm_class ℝ≥0 M N := { smul_comm := λ r, (smul_comm (r : ℝ) : _)} instance smul_comm_class_right {M N : Type*} [mul_action ℝ N] [has_smul M N] [smul_comm_class M ℝ N] : smul_comm_class M ℝ≥0 N := { smul_comm := λ m r, (smul_comm m (r : ℝ) : _)} /-- A `distrib_mul_action` over `ℝ` restricts to a `distrib_mul_action` over `ℝ≥0`. -/ instance {M : Type*} [add_monoid M] [distrib_mul_action ℝ M] : distrib_mul_action ℝ≥0 M := distrib_mul_action.comp_hom M to_real_hom.to_monoid_hom /-- A `module` over `ℝ` restricts to a `module` over `ℝ≥0`. -/ instance {M : Type*} [add_comm_monoid M] [module ℝ M] : module ℝ≥0 M := module.comp_hom M to_real_hom /-- An `algebra` over `ℝ` restricts to an `algebra` over `ℝ≥0`. -/ instance {A : Type*} [semiring A] [algebra ℝ A] : algebra ℝ≥0 A := { smul := (•), commutes' := λ r x, by simp [algebra.commutes], smul_def' := λ r x, by simp [←algebra.smul_def (r : ℝ) x, smul_def], to_ring_hom := ((algebra_map ℝ A).comp (to_real_hom : ℝ≥0 →+* ℝ)) } -- verify that the above produces instances we might care about example : algebra ℝ≥0 ℝ := by apply_instance example : distrib_mul_action ℝ≥0ˣ ℝ := by apply_instance end actions example : monoid_with_zero ℝ≥0 := by apply_instance example : comm_monoid_with_zero ℝ≥0 := by apply_instance noncomputable example : comm_group_with_zero ℝ≥0 := by apply_instance @[simp, norm_cast] lemma coe_indicator {α} (s : set α) (f : α → ℝ≥0) (a : α) : ((s.indicator f a : ℝ≥0) : ℝ) = s.indicator (λ x, f x) a := (to_real_hom : ℝ≥0 →+ ℝ).map_indicator _ _ _ @[simp, norm_cast] lemma coe_pow (r : ℝ≥0) (n : ℕ) : ((r^n : ℝ≥0) : ℝ) = r^n := to_real_hom.map_pow r n @[simp, norm_cast] lemma coe_zpow (r : ℝ≥0) (n : ℤ) : ((r^n : ℝ≥0) : ℝ) = r^n := by cases n; simp @[norm_cast] lemma coe_list_sum (l : list ℝ≥0) : ((l.sum : ℝ≥0) : ℝ) = (l.map coe).sum := to_real_hom.map_list_sum l @[norm_cast] lemma coe_list_prod (l : list ℝ≥0) : ((l.prod : ℝ≥0) : ℝ) = (l.map coe).prod := to_real_hom.map_list_prod l @[norm_cast] lemma coe_multiset_sum (s : multiset ℝ≥0) : ((s.sum : ℝ≥0) : ℝ) = (s.map coe).sum := to_real_hom.map_multiset_sum s @[norm_cast] lemma coe_multiset_prod (s : multiset ℝ≥0) : ((s.prod : ℝ≥0) : ℝ) = (s.map coe).prod := to_real_hom.map_multiset_prod s @[norm_cast] lemma coe_sum {α} {s : finset α} {f : α → ℝ≥0} : ↑(∑ a in s, f a) = ∑ a in s, (f a : ℝ) := to_real_hom.map_sum _ _ lemma _root_.real.to_nnreal_sum_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : real.to_nnreal (∑ a in s, f a) = ∑ a in s, real.to_nnreal (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_sum, real.coe_to_nnreal _ (finset.sum_nonneg hf)], exact finset.sum_congr rfl (λ x hxs, by rw real.coe_to_nnreal _ (hf x hxs)), end @[norm_cast] lemma coe_prod {α} {s : finset α} {f : α → ℝ≥0} : ↑(∏ a in s, f a) = ∏ a in s, (f a : ℝ) := to_real_hom.map_prod _ _ lemma _root_.real.to_nnreal_prod_of_nonneg {α} {s : finset α} {f : α → ℝ} (hf : ∀ a, a ∈ s → 0 ≤ f a) : real.to_nnreal (∏ a in s, f a) = ∏ a in s, real.to_nnreal (f a) := begin rw [←nnreal.coe_eq, nnreal.coe_prod, real.coe_to_nnreal _ (finset.prod_nonneg hf)], exact finset.prod_congr rfl (λ x hxs, by rw real.coe_to_nnreal _ (hf x hxs)), end lemma nsmul_coe (r : ℝ≥0) (n : ℕ) : ↑(n • r) = n • (r:ℝ) := by norm_cast @[simp, norm_cast] protected lemma coe_nat_cast (n : ℕ) : (↑(↑n : ℝ≥0) : ℝ) = n := map_nat_cast to_real_hom n noncomputable example : linear_order ℝ≥0 := by apply_instance @[simp, norm_cast] protected lemma coe_le_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) ≤ r₂ ↔ r₁ ≤ r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_lt_coe {r₁ r₂ : ℝ≥0} : (r₁ : ℝ) < r₂ ↔ r₁ < r₂ := iff.rfl @[simp, norm_cast] protected lemma coe_pos {r : ℝ≥0} : (0 : ℝ) < r ↔ 0 < r := iff.rfl protected lemma coe_mono : monotone (coe : ℝ≥0 → ℝ) := λ _ _, nnreal.coe_le_coe.2 protected lemma _root_.real.to_nnreal_mono : monotone real.to_nnreal := λ x y h, max_le_max h (le_refl 0) @[simp] lemma _root_.real.to_nnreal_coe {r : ℝ≥0} : real.to_nnreal r = r := nnreal.eq $ max_eq_left r.2 @[simp] lemma mk_coe_nat (n : ℕ) : @eq ℝ≥0 (⟨(n : ℝ), n.cast_nonneg⟩ : ℝ≥0) n := nnreal.eq (nnreal.coe_nat_cast n).symm @[simp] lemma to_nnreal_coe_nat (n : ℕ) : real.to_nnreal n = n := nnreal.eq $ by simp [real.coe_to_nnreal] /-- `real.to_nnreal` and `coe : ℝ≥0 → ℝ` form a Galois insertion. -/ noncomputable def gi : galois_insertion real.to_nnreal coe := galois_insertion.monotone_intro nnreal.coe_mono real.to_nnreal_mono real.le_coe_to_nnreal (λ _, real.to_nnreal_coe) -- note that anything involving the (decidability of the) linear order, including `⊔`/`⊓` (min, max) -- will be noncomputable, everything else should not be. example : order_bot ℝ≥0 := by apply_instance example : partial_order ℝ≥0 := by apply_instance noncomputable example : canonically_linear_ordered_add_monoid ℝ≥0 := by apply_instance noncomputable example : linear_ordered_add_comm_monoid ℝ≥0 := by apply_instance noncomputable example : distrib_lattice ℝ≥0 := by apply_instance noncomputable example : semilattice_inf ℝ≥0 := by apply_instance noncomputable example : semilattice_sup ℝ≥0 := by apply_instance noncomputable example : linear_ordered_semiring ℝ≥0 := by apply_instance example : ordered_comm_semiring ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_monoid ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_monoid_with_zero ℝ≥0 := by apply_instance noncomputable example : linear_ordered_comm_group_with_zero ℝ≥0 := by apply_instance example : canonically_ordered_comm_semiring ℝ≥0 := by apply_instance example : densely_ordered ℝ≥0 := by apply_instance example : no_max_order ℝ≥0 := by apply_instance /-- If `a` is a nonnegative real number, then the closed interval `[0, a]` in `ℝ` is order isomorphic to the interval `set.Iic a`. -/ @[simps apply_coe_coe] def order_iso_Icc_zero_coe (a : ℝ≥0) : set.Icc (0 : ℝ) a ≃o set.Iic a := { to_equiv := equiv.set.sep (set.Ici 0) (λ x, x ≤ a), map_rel_iff' := λ x y, iff.rfl } @[simp] lemma order_iso_Icc_zero_coe_symm_apply_coe (a : ℝ≥0) (b : set.Iic a) : ((order_iso_Icc_zero_coe a).symm b : ℝ) = b := rfl -- note we need the `@` to make the `has_mem.mem` have a sensible type lemma coe_image {s : set ℝ≥0} : coe '' s = {x : ℝ | ∃ h : 0 ≤ x, @has_mem.mem (ℝ≥0) _ _ ⟨x, h⟩ s} := subtype.coe_image lemma bdd_above_coe {s : set ℝ≥0} : bdd_above ((coe : ℝ≥0 → ℝ) '' s) ↔ bdd_above s := iff.intro (assume ⟨b, hb⟩, ⟨real.to_nnreal b, assume ⟨y, hy⟩ hys, show y ≤ max b 0, from le_max_of_le_left $ hb $ set.mem_image_of_mem _ hys⟩) (assume ⟨b, hb⟩, ⟨b, assume y ⟨x, hx, eq⟩, eq ▸ hb hx⟩) lemma bdd_below_coe (s : set ℝ≥0) : bdd_below ((coe : ℝ≥0 → ℝ) '' s) := ⟨0, assume r ⟨q, _, eq⟩, eq ▸ q.2⟩ noncomputable instance : conditionally_complete_linear_order_bot ℝ≥0 := nonneg.conditionally_complete_linear_order_bot real.Sup_empty.le @[norm_cast] lemma coe_Sup (s : set ℝ≥0) : (↑(Sup s) : ℝ) = Sup ((coe : ℝ≥0 → ℝ) '' s) := eq.symm $ @subset_Sup_of_within ℝ (set.Ici 0) _ ⟨(0 : ℝ≥0)⟩ s $ real.Sup_nonneg _ $ λ y ⟨x, _, hy⟩, hy ▸ x.2 @[norm_cast] lemma coe_supr {ι : Sort*} (s : ι → ℝ≥0) : (↑(⨆ i, s i) : ℝ) = ⨆ i, (s i) := by rw [supr, supr, coe_Sup, set.range_comp] @[norm_cast] lemma coe_Inf (s : set ℝ≥0) : (↑(Inf s) : ℝ) = Inf ((coe : ℝ≥0 → ℝ) '' s) := eq.symm $ @subset_Inf_of_within ℝ (set.Ici 0) _ ⟨(0 : ℝ≥0)⟩ s $ real.Inf_nonneg _ $ λ y ⟨x, _, hy⟩, hy ▸ x.2 @[simp] lemma Inf_empty : Inf (∅ : set ℝ≥0) = 0 := by rw [← nnreal.coe_eq_zero, coe_Inf, set.image_empty, real.Inf_empty] @[norm_cast] lemma coe_infi {ι : Sort*} (s : ι → ℝ≥0) : (↑(⨅ i, s i) : ℝ) = ⨅ i, (s i) := by rw [infi, infi, coe_Inf, set.range_comp] lemma le_infi_add_infi {ι ι' : Sort*} [nonempty ι] [nonempty ι'] {f : ι → ℝ≥0} {g : ι' → ℝ≥0} {a : ℝ≥0} (h : ∀ i j, a ≤ f i + g j) : a ≤ (⨅ i, f i) + ⨅ j, g j := begin rw [← nnreal.coe_le_coe, nnreal.coe_add, coe_infi, coe_infi], exact le_cinfi_add_cinfi h end example : archimedean ℝ≥0 := by apply_instance -- TODO: why are these three instances necessary? why aren't they inferred? instance covariant_add : covariant_class ℝ≥0 ℝ≥0 (+) (≤) := ordered_add_comm_monoid.to_covariant_class_left ℝ≥0 instance contravariant_add : contravariant_class ℝ≥0 ℝ≥0 (+) (<) := ordered_cancel_add_comm_monoid.to_contravariant_class_left ℝ≥0 instance covariant_mul : covariant_class ℝ≥0 ℝ≥0 (*) (≤) := ordered_comm_monoid.to_covariant_class_left ℝ≥0 -- Why isn't `nnreal.contravariant_add` inferred? lemma le_of_forall_pos_le_add {a b : ℝ≥0} (h : ∀ε, 0 < ε → a ≤ b + ε) : a ≤ b := @le_of_forall_pos_le_add _ _ _ _ _ _ nnreal.contravariant_add _ _ h lemma lt_iff_exists_rat_btwn (a b : ℝ≥0) : a < b ↔ (∃q:ℚ, 0 ≤ q ∧ a < real.to_nnreal q ∧ real.to_nnreal q < b) := iff.intro (assume (h : (↑a:ℝ) < (↑b:ℝ)), let ⟨q, haq, hqb⟩ := exists_rat_btwn h in have 0 ≤ (q : ℝ), from le_trans a.2 $ le_of_lt haq, ⟨q, rat.cast_nonneg.1 this, by simp [real.coe_to_nnreal _ this, nnreal.coe_lt_coe.symm, haq, hqb]⟩) (assume ⟨q, _, haq, hqb⟩, lt_trans haq hqb) lemma bot_eq_zero : (⊥ : ℝ≥0) = 0 := rfl lemma mul_sup (a b c : ℝ≥0) : a * (b ⊔ c) = (a * b) ⊔ (a * c) := mul_max_of_nonneg _ _ $ zero_le a lemma sup_mul (a b c : ℝ≥0) : (a ⊔ b) * c = (a * c) ⊔ (b * c) := max_mul_of_nonneg _ _ $ zero_le c lemma mul_finset_sup {α} (r : ℝ≥0) (s : finset α) (f : α → ℝ≥0) : r * s.sup f = s.sup (λ a, r * f a) := (finset.comp_sup_eq_sup_comp _ (nnreal.mul_sup r) (mul_zero r)) lemma finset_sup_mul {α} (s : finset α) (f : α → ℝ≥0) (r : ℝ≥0) : s.sup f * r = s.sup (λ a, f a * r) := (finset.comp_sup_eq_sup_comp (* r) (λ x y, nnreal.sup_mul x y r) (zero_mul r)) lemma finset_sup_div {α} {f : α → ℝ≥0} {s : finset α} (r : ℝ≥0) : s.sup f / r = s.sup (λ a, f a / r) := by simp only [div_eq_inv_mul, mul_finset_sup] @[simp, norm_cast] lemma coe_max (x y : ℝ≥0) : ((max x y : ℝ≥0) : ℝ) = max (x : ℝ) (y : ℝ) := nnreal.coe_mono.map_max @[simp, norm_cast] lemma coe_min (x y : ℝ≥0) : ((min x y : ℝ≥0) : ℝ) = min (x : ℝ) (y : ℝ) := nnreal.coe_mono.map_min @[simp] lemma zero_le_coe {q : ℝ≥0} : 0 ≤ (q : ℝ) := q.2 end nnreal namespace real section to_nnreal @[simp] lemma to_nnreal_zero : real.to_nnreal 0 = 0 := by simp [real.to_nnreal]; refl @[simp] lemma to_nnreal_one : real.to_nnreal 1 = 1 := by simp [real.to_nnreal, max_eq_left (zero_le_one : (0 :ℝ) ≤ 1)]; refl @[simp] lemma to_nnreal_pos {r : ℝ} : 0 < real.to_nnreal r ↔ 0 < r := by simp [real.to_nnreal, nnreal.coe_lt_coe.symm, lt_irrefl] @[simp] lemma to_nnreal_eq_zero {r : ℝ} : real.to_nnreal r = 0 ↔ r ≤ 0 := by simpa [-to_nnreal_pos] using (not_iff_not.2 (@to_nnreal_pos r)) lemma to_nnreal_of_nonpos {r : ℝ} : r ≤ 0 → real.to_nnreal r = 0 := to_nnreal_eq_zero.2 @[simp] lemma coe_to_nnreal' (r : ℝ) : (real.to_nnreal r : ℝ) = max r 0 := rfl @[simp] lemma to_nnreal_le_to_nnreal_iff {r p : ℝ} (hp : 0 ≤ p) : real.to_nnreal r ≤ real.to_nnreal p ↔ r ≤ p := by simp [nnreal.coe_le_coe.symm, real.to_nnreal, hp] @[simp] lemma to_nnreal_lt_to_nnreal_iff' {r p : ℝ} : real.to_nnreal r < real.to_nnreal p ↔ r < p ∧ 0 < p := nnreal.coe_lt_coe.symm.trans max_lt_max_left_iff lemma to_nnreal_lt_to_nnreal_iff {r p : ℝ} (h : 0 < p) : real.to_nnreal r < real.to_nnreal p ↔ r < p := to_nnreal_lt_to_nnreal_iff'.trans (and_iff_left h) lemma to_nnreal_lt_to_nnreal_iff_of_nonneg {r p : ℝ} (hr : 0 ≤ r) : real.to_nnreal r < real.to_nnreal p ↔ r < p := to_nnreal_lt_to_nnreal_iff'.trans ⟨and.left, λ h, ⟨h, lt_of_le_of_lt hr h⟩⟩ @[simp] lemma to_nnreal_add {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal (r + p) = real.to_nnreal r + real.to_nnreal p := nnreal.eq $ by simp [real.to_nnreal, hr, hp, add_nonneg] lemma to_nnreal_add_to_nnreal {r p : ℝ} (hr : 0 ≤ r) (hp : 0 ≤ p) : real.to_nnreal r + real.to_nnreal p = real.to_nnreal (r + p) := (real.to_nnreal_add hr hp).symm lemma to_nnreal_le_to_nnreal {r p : ℝ} (h : r ≤ p) : real.to_nnreal r ≤ real.to_nnreal p := real.to_nnreal_mono h lemma to_nnreal_add_le {r p : ℝ} : real.to_nnreal (r + p) ≤ real.to_nnreal r + real.to_nnreal p := nnreal.coe_le_coe.1 $ max_le (add_le_add (le_max_left _ _) (le_max_left _ _)) nnreal.zero_le_coe lemma to_nnreal_le_iff_le_coe {r : ℝ} {p : ℝ≥0} : real.to_nnreal r ≤ p ↔ r ≤ ↑p := nnreal.gi.gc r p lemma le_to_nnreal_iff_coe_le {r : ℝ≥0} {p : ℝ} (hp : 0 ≤ p) : r ≤ real.to_nnreal p ↔ ↑r ≤ p := by rw [← nnreal.coe_le_coe, real.coe_to_nnreal p hp] lemma le_to_nnreal_iff_coe_le' {r : ℝ≥0} {p : ℝ} (hr : 0 < r) : r ≤ real.to_nnreal p ↔ ↑r ≤ p := (le_or_lt 0 p).elim le_to_nnreal_iff_coe_le $ λ hp, by simp only [(hp.trans_le r.coe_nonneg).not_le, to_nnreal_eq_zero.2 hp.le, hr.not_le] lemma to_nnreal_lt_iff_lt_coe {r : ℝ} {p : ℝ≥0} (ha : 0 ≤ r) : real.to_nnreal r < p ↔ r < ↑p := by rw [← nnreal.coe_lt_coe, real.coe_to_nnreal r ha] lemma lt_to_nnreal_iff_coe_lt {r : ℝ≥0} {p : ℝ} : r < real.to_nnreal p ↔ ↑r < p := begin cases le_total 0 p, { rw [← nnreal.coe_lt_coe, real.coe_to_nnreal p h] }, { rw [to_nnreal_eq_zero.2 h], split, { intro, have := not_lt_of_le (zero_le r), contradiction }, { intro rp, have : ¬(p ≤ 0) := not_le_of_lt (lt_of_le_of_lt (nnreal.coe_nonneg _) rp), contradiction } } end @[simp] lemma to_nnreal_bit0 (r : ℝ) : real.to_nnreal (bit0 r) = bit0 (real.to_nnreal r) := begin cases le_total r 0 with hr hr, { rw [to_nnreal_of_nonpos hr, to_nnreal_of_nonpos, bit0_zero], exact add_nonpos hr hr }, { exact to_nnreal_add hr hr } end @[simp] lemma to_nnreal_bit1 {r : ℝ} (hr : 0 ≤ r) : real.to_nnreal (bit1 r) = bit1 (real.to_nnreal r) := (real.to_nnreal_add (by simp [hr]) zero_le_one).trans (by simp [bit1]) end to_nnreal end real open real namespace nnreal section mul lemma mul_eq_mul_left {a b c : ℝ≥0} (h : a ≠ 0) : (a * b = a * c ↔ b = c) := by rw [mul_eq_mul_left_iff, or_iff_left h] lemma _root_.real.to_nnreal_mul {p q : ℝ} (hp : 0 ≤ p) : real.to_nnreal (p * q) = real.to_nnreal p * real.to_nnreal q := begin cases le_total 0 q with hq hq, { apply nnreal.eq, simp [real.to_nnreal, hp, hq, max_eq_left, mul_nonneg] }, { have hpq := mul_nonpos_of_nonneg_of_nonpos hp hq, rw [to_nnreal_eq_zero.2 hq, to_nnreal_eq_zero.2 hpq, mul_zero] } end end mul section pow lemma pow_antitone_exp {a : ℝ≥0} (m n : ℕ) (mn : m ≤ n) (a1 : a ≤ 1) : a ^ n ≤ a ^ m := pow_le_pow_of_le_one (zero_le a) a1 mn lemma exists_pow_lt_of_lt_one {a b : ℝ≥0} (ha : 0 < a) (hb : b < 1) : ∃ n : ℕ, b ^ n < a := by simpa only [← coe_pow, nnreal.coe_lt_coe] using exists_pow_lt_of_lt_one (nnreal.coe_pos.2 ha) (nnreal.coe_lt_coe.2 hb) lemma exists_mem_Ico_zpow {x : ℝ≥0} {y : ℝ≥0} (hx : x ≠ 0) (hy : 1 < y) : ∃ n : ℤ, x ∈ set.Ico (y ^ n) (y ^ (n + 1)) := begin obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, (y : ℝ) ^ n ≤ x ∧ (x : ℝ) < y ^ (n + 1) := exists_mem_Ico_zpow (bot_lt_iff_ne_bot.mpr hx) hy, rw ← nnreal.coe_zpow at hn h'n, exact ⟨n, hn, h'n⟩, end lemma exists_mem_Ioc_zpow {x : ℝ≥0} {y : ℝ≥0} (hx : x ≠ 0) (hy : 1 < y) : ∃ n : ℤ, x ∈ set.Ioc (y ^ n) (y ^ (n + 1)) := begin obtain ⟨n, hn, h'n⟩ : ∃ n : ℤ, (y : ℝ) ^ n < x ∧ (x : ℝ) ≤ y ^ (n + 1) := exists_mem_Ioc_zpow (bot_lt_iff_ne_bot.mpr hx) hy, rw ← nnreal.coe_zpow at hn h'n, exact ⟨n, hn, h'n⟩, end end pow section sub /-! ### Lemmas about subtraction In this section we provide a few lemmas about subtraction that do not fit well into any other typeclass. For lemmas about subtraction and addition see lemmas about `has_ordered_sub` in the file `algebra.order.sub`. See also `mul_tsub` and `tsub_mul`. -/ lemma sub_def {r p : ℝ≥0} : r - p = real.to_nnreal (r - p) := rfl lemma coe_sub_def {r p : ℝ≥0} : ↑(r - p) = max (r - p : ℝ) 0 := rfl noncomputable example : has_ordered_sub ℝ≥0 := by apply_instance lemma sub_div (a b c : ℝ≥0) : (a - b) / c = a / c - b / c := tsub_div _ _ _ end sub section inv lemma sum_div {ι} (s : finset ι) (f : ι → ℝ≥0) (b : ℝ≥0) : (∑ i in s, f i) / b = ∑ i in s, (f i / b) := finset.sum_div @[simp] lemma inv_pos {r : ℝ≥0} : 0 < r⁻¹ ↔ 0 < r := inv_pos lemma div_pos {r p : ℝ≥0} (hr : 0 < r) (hp : 0 < p) : 0 < r / p := div_pos hr hp lemma div_self_le (r : ℝ≥0) : r / r ≤ 1 := div_self_le_one r @[simp] lemma inv_le {r p : ℝ≥0} (h : r ≠ 0) : r⁻¹ ≤ p ↔ 1 ≤ r * p := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h] lemma inv_le_of_le_mul {r p : ℝ≥0} (h : 1 ≤ r * p) : r⁻¹ ≤ p := by by_cases r = 0; simp [*, inv_le] @[simp] lemma le_inv_iff_mul_le {r p : ℝ≥0} (h : p ≠ 0) : (r ≤ p⁻¹ ↔ r * p ≤ 1) := by rw [← mul_le_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] @[simp] lemma lt_inv_iff_mul_lt {r p : ℝ≥0} (h : p ≠ 0) : (r < p⁻¹ ↔ r * p < 1) := by rw [← mul_lt_mul_left (pos_iff_ne_zero.2 h), mul_inv_cancel h, mul_comm] lemma mul_le_iff_le_inv {a b r : ℝ≥0} (hr : r ≠ 0) : r * a ≤ b ↔ a ≤ r⁻¹ * b := have 0 < r, from lt_of_le_of_ne (zero_le r) hr.symm, by rw [← mul_le_mul_left (inv_pos.mpr this), ← mul_assoc, inv_mul_cancel hr, one_mul] lemma le_div_iff_mul_le {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := le_div_iff₀ hr lemma div_le_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ b * r := div_le_iff₀ hr lemma div_le_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a / r ≤ b ↔ a ≤ r * b := @div_le_iff' ℝ _ a r b $ pos_iff_ne_zero.2 hr lemma div_le_of_le_mul {a b c : ℝ≥0} (h : a ≤ b * c) : a / c ≤ b := if h0 : c = 0 then by simp [h0] else (div_le_iff h0).2 h lemma div_le_of_le_mul' {a b c : ℝ≥0} (h : a ≤ b * c) : a / b ≤ c := div_le_of_le_mul $ mul_comm b c ▸ h lemma le_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ a * r ≤ b := @le_div_iff ℝ _ a b r $ pos_iff_ne_zero.2 hr lemma le_div_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a ≤ b / r ↔ r * a ≤ b := @le_div_iff' ℝ _ a b r $ pos_iff_ne_zero.2 hr lemma div_lt_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a / r < b ↔ a < b * r := lt_iff_lt_of_le_iff_le (le_div_iff hr) lemma div_lt_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a / r < b ↔ a < r * b := lt_iff_lt_of_le_iff_le (le_div_iff' hr) lemma lt_div_iff {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ a * r < b := lt_iff_lt_of_le_iff_le (div_le_iff hr) lemma lt_div_iff' {a b r : ℝ≥0} (hr : r ≠ 0) : a < b / r ↔ r * a < b := lt_iff_lt_of_le_iff_le (div_le_iff' hr) lemma mul_lt_of_lt_div {a b r : ℝ≥0} (h : a < b / r) : a * r < b := begin refine (lt_div_iff $ λ hr, false.elim _).1 h, subst r, simpa using h end lemma div_le_div_left_of_le {a b c : ℝ≥0} (b0 : 0 < b) (c0 : 0 < c) (cb : c ≤ b) : a / b ≤ a / c := begin by_cases a0 : a = 0, { rw [a0, zero_div, zero_div] }, { cases a with a ha, replace a0 : 0 < a := lt_of_le_of_ne ha (ne_of_lt (zero_lt_iff.mpr a0)), exact (div_le_div_left a0 b0 c0).mpr cb } end lemma div_le_div_left {a b c : ℝ≥0} (a0 : 0 < a) (b0 : 0 < b) (c0 : 0 < c) : a / b ≤ a / c ↔ c ≤ b := div_le_div_left a0 b0 c0 lemma le_of_forall_lt_one_mul_le {x y : ℝ≥0} (h : ∀a<1, a * x ≤ y) : x ≤ y := le_of_forall_ge_of_dense $ assume a ha, have hx : x ≠ 0 := pos_iff_ne_zero.1 (lt_of_le_of_lt (zero_le _) ha), have hx' : x⁻¹ ≠ 0, by rwa [(≠), inv_eq_zero], have a * x⁻¹ < 1, by rwa [← lt_inv_iff_mul_lt hx', inv_inv], have (a * x⁻¹) * x ≤ y, from h _ this, by rwa [mul_assoc, inv_mul_cancel hx, mul_one] at this lemma div_add_div_same (a b c : ℝ≥0) : a / c + b / c = (a + b) / c := div_add_div_same _ _ _ lemma half_pos {a : ℝ≥0} (h : 0 < a) : 0 < a / 2 := half_pos h lemma add_halves (a : ℝ≥0) : a / 2 + a / 2 = a := add_halves _ lemma half_le_self (a : ℝ≥0) : a / 2 ≤ a := half_le_self bot_le lemma half_lt_self {a : ℝ≥0} (h : a ≠ 0) : a / 2 < a := half_lt_self h.bot_lt lemma two_inv_lt_one : (2⁻¹:ℝ≥0) < 1 := two_inv_lt_one lemma div_lt_one_of_lt {a b : ℝ≥0} (h : a < b) : a / b < 1 := begin rwa [div_lt_iff, one_mul], exact ne_of_gt (lt_of_le_of_lt (zero_le _) h) end @[field_simps] lemma div_add_div (a : ℝ≥0) {b : ℝ≥0} (c : ℝ≥0) {d : ℝ≥0} (hb : b ≠ 0) (hd : d ≠ 0) : a / b + c / d = (a * d + b * c) / (b * d) := div_add_div _ _ hb hd @[field_simps] lemma add_div' (a b c : ℝ≥0) (hc : c ≠ 0) : b + a / c = (b * c + a) / c := add_div' _ _ _ hc @[field_simps] lemma div_add' (a b c : ℝ≥0) (hc : c ≠ 0) : a / c + b = (a + b * c) / c := div_add' _ _ _ hc lemma _root_.real.to_nnreal_inv {x : ℝ} : real.to_nnreal x⁻¹ = (real.to_nnreal x)⁻¹ := begin by_cases hx : 0 ≤ x, { nth_rewrite 0 ← real.coe_to_nnreal x hx, rw [←nnreal.coe_inv, real.to_nnreal_coe], }, { have hx' := le_of_not_ge hx, rw [to_nnreal_eq_zero.mpr hx', inv_zero, to_nnreal_eq_zero.mpr (inv_nonpos.mpr hx')], }, end lemma _root_.real.to_nnreal_div {x y : ℝ} (hx : 0 ≤ x) : real.to_nnreal (x / y) = real.to_nnreal x / real.to_nnreal y := by rw [div_eq_mul_inv, div_eq_mul_inv, ← real.to_nnreal_inv, ← real.to_nnreal_mul hx] lemma _root_.real.to_nnreal_div' {x y : ℝ} (hy : 0 ≤ y) : real.to_nnreal (x / y) = real.to_nnreal x / real.to_nnreal y := by rw [div_eq_inv_mul, div_eq_inv_mul, real.to_nnreal_mul (inv_nonneg.2 hy), real.to_nnreal_inv] lemma inv_lt_one_iff {x : ℝ≥0} (hx : x ≠ 0) : x⁻¹ < 1 ↔ 1 < x := by rwa [← one_div, div_lt_iff hx, one_mul] lemma inv_lt_one {x : ℝ≥0} (hx : 1 < x) : x⁻¹ < 1 := inv_lt_one hx lemma zpow_pos {x : ℝ≥0} (hx : x ≠ 0) (n : ℤ) : 0 < x ^ n := begin cases n, { simp [pow_pos hx.bot_lt _] }, { simp [pow_pos hx.bot_lt _] } end lemma inv_lt_inv_iff {x y : ℝ≥0} (hx : x ≠ 0) (hy : y ≠ 0) : y⁻¹ < x⁻¹ ↔ x < y := inv_lt_inv₀ hy hx lemma inv_lt_inv {x y : ℝ≥0} (hx : x ≠ 0) (h : x < y) : y⁻¹ < x⁻¹ := (inv_lt_inv_iff hx ((bot_le.trans_lt h).ne')).2 h end inv @[simp] lemma abs_eq (x : ℝ≥0) : |(x : ℝ)| = x := abs_of_nonneg x.property section csupr open set variables {ι : Sort*} {f : ι → ℝ≥0} lemma le_to_nnreal_of_coe_le {x : ℝ≥0} {y : ℝ} (h : ↑x ≤ y) : x ≤ y.to_nnreal := (le_to_nnreal_iff_coe_le $ x.2.trans h).2 h lemma Sup_of_not_bdd_above {s : set ℝ≥0} (hs : ¬bdd_above s) : has_Sup.Sup s = 0 := begin rw [← bdd_above_coe] at hs, rw [← nnreal.coe_eq, coe_Sup], exact Sup_of_not_bdd_above hs, end lemma supr_of_not_bdd_above (hf : ¬ bdd_above (range f)) : (⨆ i, f i) = 0 := Sup_of_not_bdd_above hf lemma infi_empty [is_empty ι] (f : ι → ℝ≥0) : (⨅ i, f i) = 0 := by { rw [← nnreal.coe_eq, coe_infi], exact real.cinfi_empty _, } @[simp] lemma infi_const_zero {α : Sort*} : (⨅ i : α, (0 : ℝ≥0)) = 0 := by { rw [← nnreal.coe_eq, coe_infi], exact real.cinfi_const_zero, } lemma infi_mul (f : ι → ℝ≥0) (a : ℝ≥0) : infi f * a = ⨅ i, f i * a := begin rw [← nnreal.coe_eq, nnreal.coe_mul, coe_infi, coe_infi], exact real.infi_mul_of_nonneg (nnreal.coe_nonneg _) _, end lemma mul_infi (f : ι → ℝ≥0) (a : ℝ≥0) : a * infi f = ⨅ i, a * f i := by simpa only [mul_comm] using infi_mul f a lemma mul_supr (f : ι → ℝ≥0) (a : ℝ≥0) : a * (⨆ i, f i) = ⨆ i, a * f i := begin rw [← nnreal.coe_eq, nnreal.coe_mul, nnreal.coe_supr, nnreal.coe_supr], exact real.mul_supr_of_nonneg (nnreal.coe_nonneg _) _, end lemma supr_mul (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) * a = ⨆ i, f i * a := by { rw [mul_comm, mul_supr], simp_rw [mul_comm] } lemma supr_div (f : ι → ℝ≥0) (a : ℝ≥0) : (⨆ i, f i) / a = ⨆ i, f i / a := by simp only [div_eq_mul_inv, supr_mul] variable [nonempty ι] lemma le_mul_infi {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, a ≤ g * h j) : a ≤ g * infi h := by { rw [mul_infi], exact le_cinfi H } lemma mul_supr_le {a : ℝ≥0} {g : ℝ≥0} {h : ι → ℝ≥0} (H : ∀ j, g * h j ≤ a) : g * supr h ≤ a := by { rw [mul_supr], exact csupr_le H } lemma le_infi_mul {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, a ≤ g i * h) : a ≤ infi g * h := by { rw infi_mul, exact le_cinfi H } lemma supr_mul_le {a : ℝ≥0} {g : ι → ℝ≥0} {h : ℝ≥0} (H : ∀ i, g i * h ≤ a) : supr g * h ≤ a := by { rw supr_mul, exact csupr_le H } lemma le_infi_mul_infi {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, a ≤ g i * h j) : a ≤ infi g * infi h := le_infi_mul $ λ i, le_mul_infi $ H i lemma supr_mul_supr_le {a : ℝ≥0} {g h : ι → ℝ≥0} (H : ∀ i j, g i * h j ≤ a) : supr g * supr h ≤ a := supr_mul_le $ λ i, mul_supr_le $ H _ end csupr end nnreal namespace set namespace ord_connected variables {s : set ℝ} {t : set ℝ≥0} lemma preimage_coe_nnreal_real (h : s.ord_connected) : (coe ⁻¹' s : set ℝ≥0).ord_connected := h.preimage_mono nnreal.coe_mono lemma image_coe_nnreal_real (h : t.ord_connected) : (coe '' t : set ℝ).ord_connected := ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, ⟨⟨z, x.2.trans hz.1⟩, h.out hx hy hz, rfl⟩⟩ lemma image_real_to_nnreal (h : s.ord_connected) : (real.to_nnreal '' s).ord_connected := begin refine ⟨ball_image_iff.2 $ λ x hx, ball_image_iff.2 $ λ y hy z hz, _⟩, cases le_total y 0 with hy₀ hy₀, { rw [mem_Icc, real.to_nnreal_of_nonpos hy₀, nonpos_iff_eq_zero] at hz, exact ⟨y, hy, (to_nnreal_of_nonpos hy₀).trans hz.2.symm⟩ }, { lift y to ℝ≥0 using hy₀, rw [to_nnreal_coe] at hz, exact ⟨z, h.out hx hy ⟨to_nnreal_le_iff_le_coe.1 hz.1, hz.2⟩, to_nnreal_coe⟩ } end lemma preimage_real_to_nnreal (h : t.ord_connected) : (real.to_nnreal ⁻¹' t).ord_connected := h.preimage_mono real.to_nnreal_mono end ord_connected end set namespace real /-- The absolute value on `ℝ` as a map to `ℝ≥0`. -/ @[pp_nodot] noncomputable def nnabs : ℝ →*₀ ℝ≥0 := { to_fun := λ x, ⟨|x|, abs_nonneg x⟩, map_zero' := by { ext, simp }, map_one' := by { ext, simp }, map_mul' := λ x y, by { ext, simp [abs_mul] } } @[norm_cast, simp] lemma coe_nnabs (x : ℝ) : (nnabs x : ℝ) = |x| := rfl @[simp] lemma nnabs_of_nonneg {x : ℝ} (h : 0 ≤ x) : nnabs x = to_nnreal x := by { ext, simp [coe_to_nnreal x h, abs_of_nonneg h] } lemma coe_to_nnreal_le (x : ℝ) : (to_nnreal x : ℝ) ≤ |x| := max_le (le_abs_self _) (abs_nonneg _) lemma cast_nat_abs_eq_nnabs_cast (n : ℤ) : (n.nat_abs : ℝ≥0) = nnabs n := by { ext, rw [nnreal.coe_nat_cast, int.cast_nat_abs, real.coe_nnabs] } end real
763574c9477302116ba8c5ff46a82c5eb7a9015b
19cc34575500ee2e3d4586c15544632aa07a8e66
/src/number_theory/sum_four_squares.lean
48994897e3bc3907cc58d75d1fc903fcaba4b0ab
[ "Apache-2.0" ]
permissive
LibertasSpZ/mathlib
b9fcd46625eb940611adb5e719a4b554138dade6
33f7870a49d7cc06d2f3036e22543e6ec5046e68
refs/heads/master
1,672,066,539,347
1,602,429,158,000
1,602,429,158,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,873
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes ## Lagrange's four square theorem The main result in this file is `sum_four_squares`, a proof that every natural number is the sum of four square numbers. # Implementation Notes The proof used is close to Lagrange's original proof. -/ import algebra.group_power.identities import data.zmod.basic import field_theory.finite.basic import data.int.parity import data.fintype.card open finset polynomial finite_field equiv open_locale big_operators namespace int lemma sum_two_squares_of_two_mul_sum_two_squares {m x y : ℤ} (h : 2 * m = x^2 + y^2) : m = ((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2 := have even (x^2 + y^2), by simp [h.symm, even_mul], have hxaddy : even (x + y), by simpa [pow_two] with parity_simps, have hxsuby : even (x - y), by simpa [pow_two] with parity_simps, have (x^2 + y^2) % 2 = 0, by simp [h.symm], (mul_right_inj' (show (2*2 : ℤ) ≠ 0, from dec_trivial)).1 $ calc 2 * 2 * m = (x - y)^2 + (x + y)^2 : by rw [mul_assoc, h]; ring ... = (2 * ((x - y) / 2))^2 + (2 * ((x + y) / 2))^2 : by rw [int.mul_div_cancel' hxsuby, int.mul_div_cancel' hxaddy] ... = 2 * 2 * (((x - y) / 2) ^ 2 + ((x + y) / 2) ^ 2) : by simp [mul_add, pow_succ, mul_comm, mul_assoc, mul_left_comm] lemma exists_sum_two_squares_add_one_eq_k (p : ℕ) [hp : fact p.prime] : ∃ (a b : ℤ) (k : ℕ), a^2 + b^2 + 1 = k * p ∧ k < p := hp.eq_two_or_odd.elim (λ hp2, hp2.symm ▸ ⟨1, 0, 1, rfl, dec_trivial⟩) $ λ hp1, let ⟨a, b, hab⟩ := zmod.sum_two_squares p (-1) in have hab' : (p : ℤ) ∣ a.val_min_abs ^ 2 + b.val_min_abs ^ 2 + 1, from (char_p.int_cast_eq_zero_iff (zmod p) p _).1 $ by simpa [eq_neg_iff_add_eq_zero] using hab, let ⟨k, hk⟩ := hab' in have hk0 : 0 ≤ k, from nonneg_of_mul_nonneg_left (by rw ← hk; exact (add_nonneg (add_nonneg (pow_two_nonneg _) (pow_two_nonneg _)) zero_le_one)) (int.coe_nat_pos.2 hp.pos), ⟨a.val_min_abs, b.val_min_abs, k.nat_abs, by rw [hk, int.nat_abs_of_nonneg hk0, mul_comm], lt_of_mul_lt_mul_left (calc p * k.nat_abs = a.val_min_abs.nat_abs ^ 2 + b.val_min_abs.nat_abs ^ 2 + 1 : by rw [← int.coe_nat_inj', int.coe_nat_add, int.coe_nat_add, int.coe_nat_pow, int.coe_nat_pow, int.nat_abs_pow_two, int.nat_abs_pow_two, int.coe_nat_one, hk, int.coe_nat_mul, int.nat_abs_of_nonneg hk0] ... ≤ (p / 2) ^ 2 + (p / 2)^2 + 1 : add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (le_refl _) ... < (p / 2) ^ 2 + (p / 2)^ 2 + (p % 2)^2 + ((2 * (p / 2)^2 + (4 * (p / 2) * (p % 2)))) : by rw [hp1, one_pow, mul_one]; exact (lt_add_iff_pos_right _).2 (add_pos_of_nonneg_of_pos (nat.zero_le _) (mul_pos dec_trivial (nat.div_pos hp.two_le dec_trivial))) ... = p * p : by { conv_rhs { rw [← nat.mod_add_div p 2] }, ring }) (show 0 ≤ p, from nat.zero_le _)⟩ end int namespace nat open int open_locale classical private lemma sum_four_squares_of_two_mul_sum_four_squares {m a b c d : ℤ} (h : a^2 + b^2 + c^2 + d^2 = 2 * m) : ∃ w x y z : ℤ, w^2 + x^2 + y^2 + z^2 = m := have ∀ f : fin 4 → zmod 2, (f 0)^2 + (f 1)^2 + (f 2)^2 + (f 3)^2 = 0 → ∃ i : (fin 4), (f i)^2 + f (swap i 0 1)^2 = 0 ∧ f (swap i 0 2)^2 + f (swap i 0 3)^2 = 0, from dec_trivial, let f : fin 4 → ℤ := vector.nth (a::b::c::d::vector.nil) in let ⟨i, hσ⟩ := this (coe ∘ f) (by rw [← @zero_mul (zmod 2) _ m, ← show ((2 : ℤ) : zmod 2) = 0, from rfl, ← int.cast_mul, ← h]; simp only [int.cast_add, int.cast_pow]; refl) in let σ := swap i 0 in have h01 : 2 ∣ f (σ 0) ^ 2 + f (σ 1) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa [σ] using hσ.1, have h23 : 2 ∣ f (σ 2) ^ 2 + f (σ 3) ^ 2, from (char_p.int_cast_eq_zero_iff (zmod 2) 2 _).1 $ by simpa using hσ.2, let ⟨x, hx⟩ := h01 in let ⟨y, hy⟩ := h23 in ⟨(f (σ 0) - f (σ 1)) / 2, (f (σ 0) + f (σ 1)) / 2, (f (σ 2) - f (σ 3)) / 2, (f (σ 2) + f (σ 3)) / 2, begin rw [← int.sum_two_squares_of_two_mul_sum_two_squares hx.symm, add_assoc, ← int.sum_two_squares_of_two_mul_sum_two_squares hy.symm, ← mul_right_inj' (show (2 : ℤ) ≠ 0, from dec_trivial), ← h, mul_add, ← hx, ← hy], have : ∑ x, f (σ x)^2 = ∑ x, f x^2, { conv_rhs { rw ← σ.sum_comp } }, have fin4univ : (univ : finset (fin 4)).1 = 0::1::2::3::0, from dec_trivial, simpa [finset.sum_eq_multiset_sum, fin4univ, multiset.sum_cons, f, add_assoc] end⟩ private lemma prime_sum_four_squares (p : ℕ) [hp : _root_.fact p.prime] : ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = p := have hm : ∃ m < p, 0 < m ∧ ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = m * p, from let ⟨a, b, k, hk⟩ := exists_sum_two_squares_add_one_eq_k p in ⟨k, hk.2, nat.pos_of_ne_zero $ (λ hk0, by { rw [hk0, int.coe_nat_zero, zero_mul] at hk, exact ne_of_gt (show a^2 + b^2 + 1 > 0, from add_pos_of_nonneg_of_pos (add_nonneg (pow_two_nonneg _) (pow_two_nonneg _)) zero_lt_one) hk.1 }), a, b, 1, 0, by simpa [pow_two] using hk.1⟩, let m := nat.find hm in let ⟨a, b, c, d, (habcd : a^2 + b^2 + c^2 + d^2 = m * p)⟩ := (nat.find_spec hm).snd.2 in by haveI hm0 : _root_.fact (0 < m) := (nat.find_spec hm).snd.1; exact have hmp : m < p, from (nat.find_spec hm).fst, m.mod_two_eq_zero_or_one.elim (λ hm2 : m % 2 = 0, let ⟨k, hk⟩ := (nat.dvd_iff_mod_eq_zero _ _).2 hm2 in have hk0 : 0 < k, from nat.pos_of_ne_zero $ λ _, by { simp [*, lt_irrefl] at * }, have hkm : k < m, { rw [hk, two_mul], exact (lt_add_iff_pos_left _).2 hk0 }, false.elim $ nat.find_min hm hkm ⟨lt_trans hkm hmp, hk0, sum_four_squares_of_two_mul_sum_four_squares (show a^2 + b^2 + c^2 + d^2 = 2 * (k * p), by { rw [habcd, hk, int.coe_nat_mul, mul_assoc], simp })⟩) (λ hm2 : m % 2 = 1, if hm1 : m = 1 then ⟨a, b, c, d, by simp only [hm1, habcd, int.coe_nat_one, one_mul]⟩ else let w := (a : zmod m).val_min_abs, x := (b : zmod m).val_min_abs, y := (c : zmod m).val_min_abs, z := (d : zmod m).val_min_abs in have hnat_abs : w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ), by simp [pow_two], have hwxyzlt : w^2 + x^2 + y^2 + z^2 < m^2, from calc w^2 + x^2 + y^2 + z^2 = (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs ^2 + z.nat_abs ^ 2 : ℕ) : hnat_abs ... ≤ ((m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 + (m / 2) ^ 2 : ℕ) : int.coe_nat_le.2 $ add_le_add (add_le_add (add_le_add (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _)) (nat.pow_le_pow_of_le_left (zmod.nat_abs_val_min_abs_le _) _) ... = 4 * (m / 2 : ℕ) ^ 2 : by simp [pow_two, bit0, bit1, mul_add, add_mul, add_assoc] ... < 4 * (m / 2 : ℕ) ^ 2 + ((4 * (m / 2) : ℕ) * (m % 2 : ℕ) + (m % 2 : ℕ)^2) : (lt_add_iff_pos_right _).2 (by { rw [hm2, int.coe_nat_one, one_pow, mul_one], exact add_pos_of_nonneg_of_pos (int.coe_nat_nonneg _) zero_lt_one }) ... = m ^ 2 : by { conv_rhs {rw [← nat.mod_add_div m 2]}, simp [-nat.mod_add_div, mul_add, add_mul, bit0, bit1, mul_comm, mul_assoc, mul_left_comm, pow_add, add_comm, add_left_comm] }, have hwxyzabcd : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = ((a^2 + b^2 + c^2 + d^2 : ℤ) : zmod m), by simp [w, x, y, z, pow_two], have hwxyz0 : ((w^2 + x^2 + y^2 + z^2 : ℤ) : zmod m) = 0, by rw [hwxyzabcd, habcd, int.cast_mul, cast_coe_nat, zmod.cast_self, zero_mul], let ⟨n, hn⟩ := ((char_p.int_cast_eq_zero_iff _ m _).1 hwxyz0) in have hn0 : 0 < n.nat_abs, from int.nat_abs_pos_of_ne_zero (λ hn0, have hwxyz0 : (w.nat_abs^2 + x.nat_abs^2 + y.nat_abs^2 + z.nat_abs^2 : ℕ) = 0, by { rw [← int.coe_nat_eq_zero, ← hnat_abs], rwa [hn0, mul_zero] at hn }, have habcd0 : (m : ℤ) ∣ a ∧ (m : ℤ) ∣ b ∧ (m : ℤ) ∣ c ∧ (m : ℤ) ∣ d, by simpa [@add_eq_zero_iff_eq_zero_of_nonneg ℤ _ _ _ (pow_two_nonneg _) (pow_two_nonneg _), pow_two, w, x, y, z, (char_p.int_cast_eq_zero_iff _ m _), and.assoc] using hwxyz0, let ⟨ma, hma⟩ := habcd0.1, ⟨mb, hmb⟩ := habcd0.2.1, ⟨mc, hmc⟩ := habcd0.2.2.1, ⟨md, hmd⟩ := habcd0.2.2.2 in have hmdvdp : m ∣ p, from int.coe_nat_dvd.1 ⟨ma^2 + mb^2 + mc^2 + md^2, (mul_right_inj' (show (m : ℤ) ≠ 0, from int.coe_nat_ne_zero_iff_pos.2 hm0)).1 $ by { rw [← habcd, hma, hmb, hmc, hmd], ring }⟩, (hp.2 _ hmdvdp).elim hm1 (λ hmeqp, by simpa [lt_irrefl, hmeqp] using hmp)), have hawbxcydz : ((m : ℕ) : ℤ) ∣ a * w + b * x + c * y + d * z, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { rw [← hwxyz0], simp, ring }, have haxbwczdy : ((m : ℕ) : ℤ) ∣ a * x - b * w - c * z + d * y, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, have haybzcwdx : ((m : ℕ) : ℤ) ∣ a * y + b * z - c * w - d * x, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, have hazbycxdw : ((m : ℕ) : ℤ) ∣ a * z - b * y + c * x - d * w, from (char_p.int_cast_eq_zero_iff (zmod m) m _).1 $ by { simp [sub_eq_add_neg], ring }, let ⟨s, hs⟩ := hawbxcydz, ⟨t, ht⟩ := haxbwczdy, ⟨u, hu⟩ := haybzcwdx, ⟨v, hv⟩ := hazbycxdw in have hn_nonneg : 0 ≤ n, from nonneg_of_mul_nonneg_left (by { erw [← hn], repeat {try {refine add_nonneg _ _}, try {exact pow_two_nonneg _}} }) (int.coe_nat_pos.2 hm0), have hnm : n.nat_abs < m, from int.coe_nat_lt.1 (lt_of_mul_lt_mul_left (by { rw [int.nat_abs_of_nonneg hn_nonneg, ← hn, ← pow_two], exact hwxyzlt }) (int.coe_nat_nonneg m)), have hstuv : s^2 + t^2 + u^2 + v^2 = n.nat_abs * p, from (mul_right_inj' (show (m^2 : ℤ) ≠ 0, from pow_ne_zero 2 (int.coe_nat_ne_zero_iff_pos.2 hm0))).1 $ calc (m : ℤ)^2 * (s^2 + t^2 + u^2 + v^2) = ((m : ℕ) * s)^2 + ((m : ℕ) * t)^2 + ((m : ℕ) * u)^2 + ((m : ℕ) * v)^2 : by { simp [mul_pow], ring } ... = (w^2 + x^2 + y^2 + z^2) * (a^2 + b^2 + c^2 + d^2) : by { simp only [hs.symm, ht.symm, hu.symm, hv.symm], ring } ... = _ : by { rw [hn, habcd, int.nat_abs_of_nonneg hn_nonneg], dsimp [m], ring }, false.elim $ nat.find_min hm hnm ⟨lt_trans hnm hmp, hn0, s, t, u, v, hstuv⟩) lemma sum_four_squares : ∀ n : ℕ, ∃ a b c d : ℕ, a^2 + b^2 + c^2 + d^2 = n | 0 := ⟨0, 0, 0, 0, rfl⟩ | 1 := ⟨1, 0, 0, 0, rfl⟩ | n@(k+2) := have hm : _root_.fact (min_fac (k+2)).prime := min_fac_prime dec_trivial, have n / min_fac n < n := factors_lemma, let ⟨a, b, c, d, h₁⟩ := show ∃ a b c d : ℤ, a^2 + b^2 + c^2 + d^2 = min_fac n, by exactI prime_sum_four_squares (min_fac (k+2)) in let ⟨w, x, y, z, h₂⟩ := sum_four_squares (n / min_fac n) in ⟨(a * w - b * x - c * y - d * z).nat_abs, (a * x + b * w + c * z - d * y).nat_abs, (a * y - b * z + c * w + d * x).nat_abs, (a * z + b * y - c * x + d * w).nat_abs, begin rw [← int.coe_nat_inj', ← nat.mul_div_cancel' (min_fac_dvd (k+2)), int.coe_nat_mul, ← h₁, ← h₂], simp [sum_four_sq_mul_sum_four_sq], end⟩ end nat
4ec46a7aed57f45d167b412d76ec0caf453e1834
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/zmod/quadratic_reciprocity.lean
aecd3253b14a1e2821b9d9454a48fd4181ee947a
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
23,109
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 data.zmod.basic 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 zmodp namespace zmodp variables {p q : ℕ} (hp : nat.prime p) (hq : nat.prime q) @[simp] lemma card_units_zmodp : fintype.card (units (zmodp p hp)) = p - 1 := by rw [card_units, card_zmodp] theorem fermat_little {p : ℕ} (hp : nat.prime p) {a : zmodp p hp} (ha : a ≠ 0) : a ^ (p - 1) = 1 := by rw [← units.mk0_val ha, ← @units.coe_one (zmodp p hp), ← units.coe_pow, ← units.ext_iff, ← card_units_zmodp hp, pow_card_eq_one] lemma euler_criterion_units {x : units (zmodp p hp)} : (∃ y : units (zmodp p hp), y ^ 2 = x) ↔ x ^ (p / 2) = 1 := hp.eq_two_or_odd.elim (λ h, by resetI; subst h; revert x; exact dec_trivial) (λ hp1, let ⟨g, hg⟩ := is_cyclic.exists_generator (units (zmodp p hp)) in let ⟨n, hn⟩ := show x ∈ powers g, from (powers_eq_gpowers g).symm ▸ hg x in ⟨λ ⟨y, hy⟩, by rw [← hy, ← pow_mul, two_mul_odd_div_two hp1, ← card_units_zmodp hp, pow_card_eq_one], λ hx, have 2 * (p / 2) ∣ n * (p / 2), by rw [two_mul_odd_div_two hp1, ← card_units_zmodp hp, ← order_of_eq_card_of_forall_mem_gpowers hg]; exact order_of_dvd_of_pow_eq_one (by rwa [pow_mul, hn]), let ⟨m, hm⟩ := dvd_of_mul_dvd_mul_right (nat.div_pos hp.two_le dec_trivial) this in ⟨g ^ m, by rwa [← pow_mul, mul_comm, ← hm]⟩⟩) lemma euler_criterion {a : zmodp p hp} (ha : a ≠ 0) : (∃ y : zmodp p hp, y ^ 2 = a) ↔ a ^ (p / 2) = 1 := ⟨λ ⟨y, hy⟩, have hy0 : y ≠ 0, from λ h, by simp [h, _root_.zero_pow (succ_pos 1)] at hy; cc, by simpa using (units.ext_iff.1 $ (euler_criterion_units hp).1 ⟨units.mk0 _ hy0, show _ = units.mk0 _ ha, by rw [units.ext_iff]; simpa⟩), λ h, let ⟨y, hy⟩ := (euler_criterion_units hp).2 (show units.mk0 _ ha ^ (p / 2) = 1, by simpa [units.ext_iff]) in ⟨y, by simpa [units.ext_iff] using hy⟩⟩ lemma exists_pow_two_eq_neg_one_iff_mod_four_ne_three : (∃ y : zmodp p hp, y ^ 2 = -1) ↔ p % 4 ≠ 3 := have (-1 : zmodp p hp) ≠ 0, from mt neg_eq_zero.1 one_ne_zero, hp.eq_two_or_odd.elim (λ hp, by resetI; subst hp; exact dec_trivial) (λ hp1, (mod_two_eq_zero_or_one (p / 2)).elim (λ hp2, begin rw [euler_criterion hp this, neg_one_pow_eq_pow_mod_two, hp2, _root_.pow_zero, eq_self_iff_true, true_iff], assume h, rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl, h] at hp2, exact absurd hp2 dec_trivial, end) (λ hp2, begin rw [euler_criterion hp this, neg_one_pow_eq_pow_mod_two, hp2, _root_.pow_one, iff_false_intro (zmodp.ne_neg_self hp hp1 one_ne_zero).symm, false_iff, not_not], rw [← nat.mod_mul_right_div_self, show 2 * 2 = 4, from rfl] at hp2, rw [← nat.mod_mul_left_mod _ 2, show 2 * 2 = 4, from rfl] at hp1, have hp4 : p % 4 < 4, from nat.mod_lt _ dec_trivial, revert hp1 hp2, revert hp4, generalize : p % 4 = k, revert k, exact dec_trivial end)) lemma pow_div_two_eq_neg_one_or_one {a : zmodp p hp} (ha : a ≠ 0) : a ^ (p / 2) = 1 ∨ a ^ (p / 2) = -1 := hp.eq_two_or_odd.elim (λ h, by revert a ha; resetI; subst h; exact dec_trivial) (λ hp1, by rw [← mul_self_eq_one_iff, ← _root_.pow_add, ← two_mul, two_mul_odd_div_two hp1]; exact fermat_little hp ha) @[simp] lemma wilsons_lemma {p : ℕ} (hp : nat.prime p) : (fact (p - 1) : zmodp p hp) = -1 := begin rw [← finset.prod_Ico_id_eq_fact, ← @units.coe_one (zmodp p hp), ← units.coe_neg, ← @prod_univ_units_id_eq_neg_one (zmodp p hp), ← prod_hom (coe : units (zmodp p hp) → zmodp p hp), ← prod_hom (coe : ℕ → zmodp p hp)], exact eq.symm (prod_bij (λ a _, (a : zmodp p hp).1) (λ a ha, Ico.mem.2 ⟨nat.pos_of_ne_zero (λ h, units.coe_ne_zero a (fin.eq_of_veq h)), by rw [← succ_sub hp.pos, succ_sub_one]; exact (a : zmodp p hp).2⟩) (λ a _, by simp) (λ _ _ _ _, units.ext_iff.2 ∘ fin.eq_of_veq) (λ b hb, have b ≠ 0 ∧ b < p, by rwa [Ico.mem, nat.succ_le_iff, ← succ_sub hp.pos, succ_sub_one, nat.pos_iff_ne_zero] at hb, ⟨units.mk0 _ (show (b : zmodp p hp) ≠ 0, from fin.ne_of_vne $ by rw [zmod.val_cast_nat, ← @nat.cast_zero (zmodp p hp), zmod.val_cast_nat]; simp [mod_eq_of_lt this.2, this.1]), mem_univ _, by simp [val_cast_of_lt hp this.2]⟩)) end @[simp] lemma prod_Ico_one_prime {p : ℕ} (hp : nat.prime p) : (Ico 1 p).prod (λ x, (x : zmodp p hp)) = -1 := by conv in (Ico 1 p) { rw [← succ_sub_one p, succ_sub hp.pos] }; rw [prod_hom (coe : ℕ → zmodp p hp), finset.prod_Ico_id_eq_fact, wilsons_lemma] end zmodp /-- 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 : p.prime) (a : zmodp p hp) (hpa : 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) := have he : ∀ {x}, x ∈ Ico 1 (p / 2).succ → x ≠ 0 ∧ x ≤ p / 2, by simp [nat.lt_succ_iff, nat.succ_le_iff, nat.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 hsurj : ∀ b : ℕ , b ∈ Ico 1 (p / 2).succ → ∃ x ∈ Ico 1 (p / 2).succ, b = (a * x : zmodp p hp).val_min_abs.nat_abs, from λ b hb, ⟨(b / a : zmodp p hp).val_min_abs.nat_abs, Ico.mem.2 ⟨nat.pos_of_ne_zero $ by simp [div_eq_mul_inv, hpa, zmodp.eq_zero_iff_dvd_nat hp b, hpe hb], nat.lt_succ_of_le $ zmodp.nat_abs_val_min_abs_le _⟩, begin rw [zmodp.cast_nat_abs_val_min_abs], split_ifs, { erw [mul_div_cancel' _ hpa, zmodp.val_min_abs, zmod.val_min_abs, zmodp.val_cast_of_lt hp (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' _ hpa, zmod.nat_abs_val_min_abs_neg, zmod.val_min_abs, zmodp.val_cast_of_lt hp (hep hb), if_pos (le_of_lt_succ (Ico.mem.1 hb).2), int.nat_abs_of_nat] }, end⟩, have hmem : ∀ x : ℕ, x ∈ Ico 1 (p / 2).succ → (a * x : zmodp p hp).val_min_abs.nat_abs ∈ Ico 1 (p / 2).succ, from λ x hx, by simp [hpa, zmodp.eq_zero_iff_dvd_nat hp x, hpe hx, lt_succ_iff, succ_le_iff, nat.pos_iff_ne_zero, zmodp.nat_abs_val_min_abs_le _], multiset.map_eq_map_of_bij_of_nodup _ _ (finset.nodup _) (finset.nodup _) (λ x _, (a * x : zmodp p hp).val_min_abs.nat_abs) hmem (λ _ _, rfl) (inj_on_of_surj_on_of_card_le _ hmem hsurj (le_refl _)) hsurj private lemma gauss_lemma_aux₁ {p : ℕ} (hp : p.prime) (hp2 : p % 2 = 1) {a : ℕ} (hpa : (a : zmodp p hp) ≠ 0) : (a^(p / 2) * (p / 2).fact : zmodp p hp) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmodp p hp).val ≤ p / 2)).card * (p / 2).fact := calc (a ^ (p / 2) * (p / 2).fact : zmodp p hp) = (Ico 1 (p / 2).succ).prod (λ x, a * x) : by rw [prod_mul_distrib, ← prod_nat_cast, ← prod_nat_cast, prod_Ico_id_eq_fact, prod_const, Ico.card, succ_sub_one]; simp ... = (Ico 1 (p / 2).succ).prod (λ x, (a * x : zmodp p hp).val) : by simp ... = (Ico 1 (p / 2).succ).prod (λ x, (if (a * x : zmodp p hp).val ≤ p / 2 then 1 else -1) * (a * x : zmodp p hp).val_min_abs.nat_abs) : prod_congr rfl $ λ _ _, begin simp only [zmodp.cast_nat_abs_val_min_abs], split_ifs; simp end ... = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmodp p hp).val ≤ p / 2)).card * (Ico 1 (p / 2).succ).prod (λ x, (a * x : zmodp p hp).val_min_abs.nat_abs) : have (Ico 1 (p / 2).succ).prod (λ x, if (a * x : zmodp p hp).val ≤ p / 2 then (1 : zmodp p hp) else -1) = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, ¬(a * x : zmodp p hp).val ≤ p / 2)).prod (λ _, -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 : zmodp p hp).val ≤ p / 2)).card * (p / 2).fact : by rw [← prod_nat_cast, finset.prod_eq_multiset_prod, Ico_map_val_min_abs_nat_abs_eq_Ico_map_id hp a hpa, ← finset.prod_eq_multiset_prod, prod_Ico_id_eq_fact] private lemma gauss_lemma_aux₂ {p : ℕ} (hp : p.prime) (hp2 : p % 2 = 1) {a : ℕ} (hpa : (a : zmodp p hp) ≠ 0) : (a^(p / 2) : zmodp p hp) = (-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmodp p hp).val)).card := (domain.mul_right_inj (show ((p / 2).fact : zmodp p hp) ≠ 0, by rw [ne.def, zmodp.eq_zero_iff_dvd_nat, hp.dvd_fact, not_le]; exact nat.div_lt_self hp.pos dec_trivial)).1 $ by simpa using gauss_lemma_aux₁ _ hp2 hpa private lemma eisenstein_lemma_aux₁ {p : ℕ} (hp : p.prime) (hp2 : p % 2 = 1) {a : ℕ} (hap : (a : zmodp p hp) ≠ 0) : (((Ico 1 (p / 2).succ).sum (λ x, a * x) : ℕ) : zmod 2) = ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmodp p hp).val))).card + (Ico 1 (p / 2).succ).sum (λ x, x) + ((Ico 1 (p / 2).succ).sum (λ x, (a * x) / p) : ℕ) := have hp2 : (p : zmod 2) = (1 : ℕ), from zmod.eq_iff_modeq_nat.2 hp2, calc (((Ico 1 (p / 2).succ).sum (λ x, a * x) : ℕ) : zmod 2) = (((Ico 1 (p / 2).succ).sum (λ x, (a * x) % p + p * ((a * x) / p)) : ℕ) : zmod 2) : by simp only [mod_add_div] ... = ((Ico 1 (p / 2).succ).sum (λ x, ((a * x : ℕ) : zmodp p hp).val) : ℕ) + ((Ico 1 (p / 2).succ).sum (λ x, (a * x) / p) : ℕ) : by simp only [zmodp.val_cast_nat]; simp [sum_add_distrib, mul_sum.symm, nat.cast_add, nat.cast_mul, sum_nat_cast, hp2] ... = _ : congr_arg2 (+) (calc (((Ico 1 (p / 2).succ).sum (λ x, ((a * x : ℕ) : zmodp p hp).val) : ℕ) : zmod 2) = (Ico 1 (p / 2).succ).sum (λ x, ((((a * x : zmodp p hp).val_min_abs + (if (a * x : zmodp p hp).val ≤ p / 2 then 0 else p)) : ℤ) : zmod 2)) : by simp only [(zmodp.val_eq_ite_val_min_abs _).symm]; simp [sum_nat_cast] ... = ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmodp p hp).val)).card + (((Ico 1 (p / 2).succ).sum (λ x, (a * x : zmodp p hp).val_min_abs.nat_abs)) : ℕ) : by simp [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 hp _ hap, ← finset.sum_eq_multiset_sum]; simp [sum_nat_cast]) rfl private lemma eisenstein_lemma_aux₂ {p : ℕ} (hp : p.prime) (hp2 : p % 2 = 1) {a : ℕ} (ha2 : a % 2 = 1) (hap : (a : zmodp p hp) ≠ 0) : ((Ico 1 (p / 2).succ).filter ((λ x : ℕ, p / 2 < (a * x : zmodp p hp).val))).card ≡ (Ico 1 (p / 2).succ).sum (λ x, (x * a) / p) [MOD 2] := have ha2 : (a : zmod 2) = (1 : ℕ), from zmod.eq_iff_modeq_nat.2 ha2, (@zmod.eq_iff_modeq_nat 2 _ _).1 $ sub_eq_zero.1 $ by simpa [finset.mul_sum.symm, mul_comm, ha2, sum_nat_cast, add_neg_eq_iff_eq_add.symm, zmod.neg_eq_self_mod_two] using eq.symm (eisenstein_lemma_aux₁ hp hp2 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.2 $ λ 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 integers point 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 : ℕ} : (Ico 1 (p / 2).succ).sum (λ a, (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] else calc (Ico 1 (p / 2).succ).sum (λ a, (a * q) / p) = (Ico 1 (p / 2).succ).sum (λ a, ((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 {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp {contextual := tt}) (λ ⟨b₁, b₂⟩ h, ⟨⟨b₁, b₂⟩, by revert h; simp {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 : p.prime) (hq0 : (q : zmodp p hp) ≠ 0) : (Ico 1 (p / 2).succ).sum (λ a, (a * q) / p) + (Ico 1 (q / 2).succ).sum (λ a, (a * p) / q) = (p / 2) * (q / 2) := 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 {contextual := tt}) (λ ⟨_, _⟩ ⟨_, _⟩, by simp {contextual := tt}) (λ ⟨x₁, x₂⟩ h, ⟨⟨x₂, x₁⟩, by revert h; simp {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)), from 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 [*, nat.lt_succ_iff] at *; tauto) (nat.div_lt_self hp.pos dec_trivial), begin have : (x.1 : zmodp p hp) = 0, { simpa [hq0] using congr_arg (coe : ℕ → zmodp p hp) (le_antisymm hpq hqp) }, rw [fin.eq_iff_veq, zmodp.val_cast_of_lt hp hxp, zmodp.zero_val] at this, simp * at * end, 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.2 $ λ x, by have := le_total (x.2 * p) (x.1 * q); simp; tauto, by rw [sum_Ico_eq_card_lt, sum_Ico_eq_card_lt, hswap, ← card_disjoint_union hdisj, hunion, card_product]; simp variables {p q : ℕ} (hp : nat.prime p) (hq : nat.prime q) namespace zmodp def legendre_sym (a p : ℕ) (hp : nat.prime p) : ℤ := if (a : zmodp p hp) = 0 then 0 else if ∃ b : zmodp p hp, b ^ 2 = a then 1 else -1 lemma legendre_sym_eq_pow (a p : ℕ) (hp : nat.prime p) : (legendre_sym a p hp : zmodp p hp) = (a ^ (p / 2)) := if ha : (a : zmodp p hp) = 0 then by simp [*, legendre_sym, _root_.zero_pow (nat.div_pos hp.two_le (succ_pos 1))] else (nat.prime.eq_two_or_odd hp).elim (λ hp2, begin resetI; subst hp2, suffices : ∀ a : zmodp 2 nat.prime_two, (((ite (a = 0) 0 (ite (∃ (b : zmodp 2 hp), b ^ 2 = a) 1 (-1))) : ℤ) : zmodp 2 nat.prime_two) = a ^ (2 / 2), { exact this a }, exact dec_trivial, end) (λ hp1, have _ := euler_criterion hp ha, have (-1 : zmodp p hp) ≠ 1, from (ne_neg_self hp hp1 zero_ne_one.symm).symm, by cases zmodp.pow_div_two_eq_neg_one_or_one hp ha; simp [legendre_sym, *] at *) lemma legendre_sym_eq_one_or_neg_one (a : ℕ) (hp : nat.prime p) (ha : (a : zmodp p hp) ≠ 0) : legendre_sym a p hp = -1 ∨ legendre_sym a p hp = 1 := by unfold legendre_sym; split_ifs; simp * at * /-- 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 : p % 2 = 1) (ha0 : (a : zmodp p hp) ≠ 0) : legendre_sym a p hp = (-1) ^ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmodp p hp).val)).card := have (legendre_sym a p hp : zmodp p hp) = (((-1)^((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmodp p hp).val)).card : ℤ) : zmodp p hp), by rw [legendre_sym_eq_pow, gauss_lemma_aux₂ hp hp1 ha0]; simp, begin cases legendre_sym_eq_one_or_neg_one a hp ha0; cases @neg_one_pow_eq_or ℤ _ ((Ico 1 (p / 2).succ).filter (λ x : ℕ, p / 2 < (a * x : zmodp p hp).val)).card; simp [*, zmodp.ne_neg_self hp hp1 one_ne_zero, (zmodp.ne_neg_self hp hp1 one_ne_zero).symm] at * end lemma legendre_sym_eq_one_iff {a : ℕ} (ha0 : (a : zmodp p hp) ≠ 0) : legendre_sym a p hp = 1 ↔ (∃ b : zmodp p hp, b ^ 2 = a) := by rw [legendre_sym]; split_ifs; finish lemma eisenstein_lemma (hp1 : p % 2 = 1) {a : ℕ} (ha1 : a % 2 = 1) (ha0 : (a : zmodp p hp) ≠ 0) : legendre_sym a p hp = (-1)^(Ico 1 (p / 2).succ).sum (λ x, (x * a) / p) := by rw [neg_one_pow_eq_pow_mod_two, gauss_lemma hp hp1 ha0, neg_one_pow_eq_pow_mod_two, show _ = _, from eisenstein_lemma_aux₂ hp hp1 ha1 ha0] theorem quadratic_reciprocity (hp1 : p % 2 = 1) (hq1 : q % 2 = 1) (hpq : p ≠ q) : legendre_sym p q hq * legendre_sym q p hp = (-1) ^ ((p / 2) * (q / 2)) := have hpq0 : (p : zmodp q hq) ≠ 0, from zmodp.prime_ne_zero _ hp hpq.symm, have hqp0 : (q : zmodp p hp) ≠ 0, from zmodp.prime_ne_zero _ hq hpq, by rw [eisenstein_lemma _ hq1 hp1 hpq0, eisenstein_lemma _ hp1 hq1 hqp0, ← _root_.pow_add, sum_mul_div_add_sum_mul_div_eq_mul _ hpq0, mul_comm] lemma legendre_sym_two (hp1 : p % 2 = 1) : legendre_sym 2 p hp = (-1) ^ (p / 4 + p / 2) := have hp2 : p ≠ 2, from mt (congr_arg (% 2)) (by simp [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 : zmodp p hp).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 [← mod_add_div p 2, add_comm, hp1]}; exact lt_succ_self _, by rw [← nat.cast_two, ← nat.cast_mul, zmodp.val_cast_of_lt _ h2xp], have hdisj : disjoint ((Ico 1 (p / 2).succ).filter (λ x, p / 2 < ((2 : ℕ) * x : zmodp p hp).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 : zmodp p hp).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 _ hp1 (prime_ne_zero hp prime_two hp2), neg_one_pow_eq_pow_mod_two, @neg_one_pow_eq_pow_mod_two _ _ (p / 4 + p / 2)], refine congr_arg2 _ rfl ((@zmod.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, zmod.neg_eq_self_mod_two, ← nat.cast_add, ← card_disjoint_union hdisj, hunion, hcard] end lemma exists_pow_two_eq_two_iff (hp1 : p % 2 = 1) : (∃ a : zmodp p hp, a ^ 2 = 2) ↔ p % 8 = 1 ∨ p % 8 = 7 := have hp2 : ((2 : ℕ) : zmodp p hp) ≠ 0, from zmodp.prime_ne_zero hp prime_two (λ h, by simp * at *), 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 : zmodp p hp) = (2 : ℕ), by simp, ← legendre_sym_eq_one_iff hp hp2, legendre_sym_two hp hp1, 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), revert this hp1, erw [hpm4, hpm2], generalize hm : p % 8 = m, clear hm, revert m, exact dec_trivial end lemma exists_pow_two_eq_prime_iff_of_mod_four_eq_one (hp1 : p % 4 = 1) (hq1 : q % 2 = 1) : (∃ a : zmodp p hp, a ^ 2 = q) ↔ ∃ b : zmodp q hq, b ^ 2 = p := if hpq : p = q then by resetI; subst 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 have := quadratic_reciprocity hp hq (odd_of_mod_four_eq_one hp1) hq1 hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg (zmodp.prime_ne_zero hp hq hpq), if_neg (zmodp.prime_ne_zero hq hp (ne.symm hpq))] at this, 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 : zmodp p hp, a ^ 2 = q) ↔ ¬∃ b : zmodp q hq, 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 have := quadratic_reciprocity hp hq (odd_of_mod_four_eq_three hp3) (odd_of_mod_four_eq_three hq3) hpq, rw [neg_one_pow_eq_pow_mod_two, h1, legendre_sym, legendre_sym, if_neg (zmodp.prime_ne_zero hp hq hpq), if_neg (zmodp.prime_ne_zero hq hp hpq.symm)] at this, split_ifs at this; simp *; contradiction end end zmodp
b910a6303e20602b279db295a92b23052a9a5c8a
82e44445c70db0f03e30d7be725775f122d72f3e
/src/data/finset/powerset.lean
4cd886a35273c3917a938c670bb20daa12114d9f
[ "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
10,325
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.finset.lattice /-! # The powerset of a finset -/ namespace finset open multiset variables {α : Type*} /-! ### powerset -/ section powerset /-- When `s` is a finset, `s.powerset` is the finset of all subsets of `s` (seen as finsets). -/ def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] lemma powerset_empty : finset.powerset (∅ : finset α) = {∅} := rfl @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ /-- **Number of Subsets of a Set** -/ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) lemma not_mem_of_mem_powerset_of_not_mem {s t : finset α} {a : α} (ht : t ∈ s.powerset) (h : a ∉ s) : a ∉ t := by { apply mt _ h, apply mem_powerset.1 ht } lemma powerset_insert [decidable_eq α] (s : finset α) (a : α) : powerset (insert a s) = s.powerset ∪ s.powerset.image (insert a) := begin ext t, simp only [exists_prop, mem_powerset, mem_image, mem_union, subset_insert_iff], by_cases h : a ∈ t, { split, { exact λH, or.inr ⟨_, H, insert_erase h⟩ }, { intros H, cases H, { exact subset.trans (erase_subset a t) H }, { rcases H with ⟨u, hu⟩, rw ← hu.2, exact subset.trans (erase_insert_subset a u) hu.1 } } }, { have : ¬ ∃ (u : finset α), u ⊆ s ∧ insert a u = t, by simp [ne.symm (ne_insert_of_not_mem _ _ h)], simp [finset.erase_eq_of_not_mem h, this] } end /-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for any subset. -/ instance decidable_exists_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop} [Π t (h : t ⊆ s), decidable (p t h)] : decidable (∃ t (h : t ⊆ s), p t h) := decidable_of_iff (∃ t (hs : t ∈ s.powerset), p t (mem_powerset.1 hs)) ⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_powerset.2 hs, hp⟩)⟩ /-- For predicate `p` decidable on subsets, it is decidable whether `p` holds for every subset. -/ instance decidable_forall_of_decidable_subsets {s : finset α} {p : Π t ⊆ s, Prop} [Π t (h : t ⊆ s), decidable (p t h)] : decidable (∀ t (h : t ⊆ s), p t h) := decidable_of_iff (∀ t (h : t ∈ s.powerset), p t (mem_powerset.1 h)) ⟨(λ h t hs, h t (mem_powerset.2 hs)), (λ h _ _, h _ _)⟩ /-- A version of `finset.decidable_exists_of_decidable_subsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_exists_of_decidable_subsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∃ t (h : t ⊆ s), p t) := @finset.decidable_exists_of_decidable_subsets _ _ _ hu /-- A version of `finset.decidable_forall_of_decidable_subsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_forall_of_decidable_subsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊆ s), decidable (p t)) : decidable (∀ t (h : t ⊆ s), p t) := @finset.decidable_forall_of_decidable_subsets _ _ _ hu end powerset section ssubsets variables [decidable_eq α] /-- For `s` a finset, `s.ssubsets` is the finset comprising strict subsets of `s`. -/ def ssubsets (s : finset α) : finset (finset α) := erase (powerset s) s @[simp] lemma mem_ssubsets {s t : finset α} : t ∈ s.ssubsets ↔ t ⊂ s := by rw [ssubsets, mem_erase, mem_powerset, ssubset_iff_subset_ne, and.comm] lemma empty_mem_ssubsets {s : finset α} (h : s.nonempty) : ∅ ∈ s.ssubsets := by { rw [mem_ssubsets, ssubset_iff_subset_ne], exact ⟨empty_subset s, h.ne_empty.symm⟩, } /-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for any ssubset. -/ instance decidable_exists_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop} [Π t (h : t ⊂ s), decidable (p t h)] : decidable (∃ t h, p t h) := decidable_of_iff (∃ t (hs : t ∈ s.ssubsets), p t (mem_ssubsets.1 hs)) ⟨(λ ⟨t, _, hp⟩, ⟨t, _, hp⟩), (λ ⟨t, hs, hp⟩, ⟨t, mem_ssubsets.2 hs, hp⟩)⟩ /-- For predicate `p` decidable on ssubsets, it is decidable whether `p` holds for every ssubset. -/ instance decidable_forall_of_decidable_ssubsets {s : finset α} {p : Π t ⊂ s, Prop} [Π t (h : t ⊂ s), decidable (p t h)] : decidable (∀ t h, p t h) := decidable_of_iff (∀ t (h : t ∈ s.ssubsets), p t (mem_ssubsets.1 h)) ⟨(λ h t hs, h t (mem_ssubsets.2 hs)), (λ h _ _, h _ _)⟩ /-- A version of `finset.decidable_exists_of_decidable_ssubsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_exists_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∃ t (h : t ⊂ s), p t) := @finset.decidable_exists_of_decidable_ssubsets _ _ _ _ hu /-- A version of `finset.decidable_forall_of_decidable_ssubsets` with a non-dependent `p`. Typeclass inference cannot find `hu` here, so this is not an instance. -/ def decidable_forall_of_decidable_ssubsets' {s : finset α} {p : finset α → Prop} (hu : Π t (h : t ⊂ s), decidable (p t)) : decidable (∀ t (h : t ⊂ s), p t) := @finset.decidable_forall_of_decidable_ssubsets _ _ _ _ hu end ssubsets section powerset_len /-- Given an integer `n` and a finset `s`, then `powerset_len n s` is the finset of subsets of `s` of cardinality `n`. -/ def powerset_len (n : ℕ) (s : finset α) : finset (finset α) := ⟨(s.1.powerset_len n).pmap finset.mk (λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset_len s.2)⟩ /-- **Formula for the Number of Combinations** -/ theorem mem_powerset_len {n} {s t : finset α} : s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n := by cases s; simp [powerset_len, val_le_iff.symm]; refl @[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) : powerset_len n s ⊆ powerset_len n t := λ u h', mem_powerset_len.2 $ and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h') /-- **Formula for the Number of Combinations** -/ @[simp] theorem card_powerset_len (n : ℕ) (s : finset α) : card (powerset_len n s) = nat.choose (card s) n := (card_pmap _ _ _).trans (card_powerset_len n s.1) @[simp] lemma powerset_len_zero (s : finset α) : finset.powerset_len 0 s = {∅} := begin ext, rw [mem_powerset_len, mem_singleton, card_eq_zero], refine ⟨λ h, h.2, λ h, by { rw h, exact ⟨empty_subset s, rfl⟩ }⟩, end @[simp] theorem powerset_len_empty (n : ℕ) {s : finset α} (h : s.card < n) : powerset_len n s = ∅ := finset.card_eq_zero.mp (by rw [card_powerset_len, nat.choose_eq_zero_of_lt h]) theorem powerset_len_eq_filter {n} {s : finset α} : powerset_len n s = (powerset s).filter (λ x, x.card = n) := by { ext, simp [mem_powerset_len] } lemma powerset_len_succ_insert [decidable_eq α] {x : α} {s : finset α} (h : x ∉ s) (n : ℕ) : powerset_len n.succ (insert x s) = powerset_len n.succ s ∪ (powerset_len n s).image (insert x) := begin rw [powerset_len_eq_filter, powerset_insert, filter_union, ←powerset_len_eq_filter], congr, rw [powerset_len_eq_filter, image_filter], congr' 1, ext t, simp only [mem_powerset, mem_filter, function.comp_app, and.congr_right_iff], intro ht, have : x ∉ t := λ H, h (ht H), simp [card_insert_of_not_mem this, nat.succ_inj'] end lemma powerset_len_nonempty {n : ℕ} {s : finset α} (h : n < s.card) : (powerset_len n s).nonempty := begin classical, induction s using finset.induction_on with x s hx IH generalizing n, { simpa using h }, { cases n, { simp }, { rw [card_insert_of_not_mem hx, nat.succ_lt_succ_iff] at h, rw powerset_len_succ_insert hx, refine nonempty.mono _ ((IH h).image (insert x)), convert (subset_union_right _ _) } } end @[simp] lemma powerset_len_self (s : finset α) : powerset_len s.card s = {s} := begin ext, rw [mem_powerset_len, mem_singleton], split, { exact λ ⟨hs, hc⟩, eq_of_subset_of_card_le hs hc.ge }, { rintro rfl, simp } end lemma powerset_card_bUnion [decidable_eq (finset α)] (s : finset α) : finset.powerset s = (range (s.card + 1)).bUnion (λ i, powerset_len i s) := begin refine ext (λ a, ⟨λ ha, _, λ ha, _ ⟩), { rw mem_bUnion, exact ⟨a.card, mem_range.mpr (nat.lt_succ_of_le (card_le_of_subset (mem_powerset.mp ha))), mem_powerset_len.mpr ⟨mem_powerset.mp ha, rfl⟩⟩ }, { rcases mem_bUnion.mp ha with ⟨i, hi, ha⟩, exact mem_powerset.mpr (mem_powerset_len.mp ha).1, } end lemma powerset_len_sup [decidable_eq α] (u : finset α) (n : ℕ) (hn : n < u.card) : (powerset_len n.succ u).sup id = u := begin apply le_antisymm, { simp_rw [sup_le_iff, mem_powerset_len], rintros x ⟨h, -⟩, exact h }, { rw [sup_eq_bUnion, le_iff_subset, subset_iff], cases (nat.succ_le_of_lt hn).eq_or_lt with h' h', { simp [h'] }, { intros x hx, simp only [mem_bUnion, exists_prop, id.def], obtain ⟨t, ht⟩ : ∃ t, t ∈ powerset_len n (u.erase x) := powerset_len_nonempty _, { refine ⟨insert x t, _, mem_insert_self _ _⟩, rw [←insert_erase hx, powerset_len_succ_insert (not_mem_erase _ _)], exact mem_union_right _ (mem_image_of_mem _ ht) }, { rwa [card_erase_of_mem hx, nat.lt_pred_iff] } } } end end powerset_len end finset
bc51961757dbf84d40bb2a0a938532661fb44e84
97f752b44fd85ec3f635078a2dd125ddae7a82b6
/hott/algebra/category/iso.hlean
7c52c9f522d284b7679dbe2b0b33e74b14a12f3d
[ "Apache-2.0" ]
permissive
tectronics/lean
ab977ba6be0fcd46047ddbb3c8e16e7c26710701
f38af35e0616f89c6e9d7e3eb1d48e47ee666efe
refs/heads/master
1,532,358,526,384
1,456,276,623,000
1,456,276,623,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,789
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Floris van Doorn, Jakob von Raumer -/ import .precategory 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 abbreviation retraction_of [unfold 6] := @split_mono.retraction_of abbreviation retraction_comp [unfold 6] := @split_mono.retraction_comp abbreviation section_of [unfold 6] := @split_epi.section_of abbreviation comp_section [unfold 6] := @split_epi.comp_section abbreviation inverse [unfold 6] := @is_iso.inverse abbreviation left_inverse [unfold 6] := @is_iso.left_inverse abbreviation right_inverse [unfold 6] := @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 [constructor] [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 [constructor] [instance] [priority 300] [reducible] (f : a ⟶ b) [H : is_iso f] : split_epi f := split_epi.mk !right_inverse definition is_iso_id [constructor] [instance] [priority 500] (a : ob) : is_iso (ID a) := is_iso.mk _ !id_id !id_id definition is_iso_inverse [constructor] [instance] [priority 200] (f : a ⟶ b) {H : is_iso f} : is_iso f⁻¹ := is_iso.mk _ !right_inverse !left_inverse theorem 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] theorem retraction_eq [H : split_mono f] (H2 : f ∘ h = id) : retraction_of f = h := left_inverse_eq_right_inverse !retraction_comp H2 theorem section_eq [H : split_epi f] (H2 : h ∘ f = id) : section_of f = h := (left_inverse_eq_right_inverse H2 !comp_section)⁻¹ theorem inverse_eq_right [H : is_iso f] (H2 : f ∘ h = id) : f⁻¹ = h := left_inverse_eq_right_inverse !left_inverse H2 theorem inverse_eq_left [H : is_iso f] (H2 : h ∘ f = id) : f⁻¹ = h := (left_inverse_eq_right_inverse H2 !right_inverse)⁻¹ theorem 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 [constructor] (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) theorem inverse_unique (H H' : is_iso f) : @inverse _ _ _ _ f H = @inverse _ _ _ _ f H' := inverse_eq_left !left_inverse theorem inverse_involutive (f : a ⟶ b) [H : is_iso f] [H : is_iso (f⁻¹)] : (f⁻¹)⁻¹ = f := inverse_eq_right !left_inverse theorem inverse_eq_inverse {f g : a ⟶ b} [H : is_iso f] [H : is_iso g] (p : f = g) : f⁻¹ = g⁻¹ := by cases p;apply inverse_unique theorem retraction_id (a : ob) : retraction_of (ID a) = id := retraction_eq !id_id theorem section_id (a : ob) : section_of (ID a) = id := section_eq !id_id theorem id_inverse (a : ob) [H : is_iso (ID a)] : (ID a)⁻¹ = id := inverse_eq_left !id_id definition split_mono_comp [constructor] [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 [constructor] [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 [constructor] [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 theorem is_prop_is_iso [instance] (f : hom a b) : is_prop (is_iso f) := begin apply is_prop.mk, intro 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_prop.elim, apply is_prop.elim, end end iso open iso /- isomorphic objects -/ structure iso {ob : Type} [C : precategory ob] (a b : ob) := (to_hom : hom a b) (struct : is_iso to_hom) infix ` ≅ `:50 := iso notation c ` ≅[`:50 C:0 `] `:0 c':50 := @iso C _ c c' attribute iso.struct [instance] [priority 2000] namespace iso variables {ob : Type} [C : precategory ob] variables {a b c : ob} {g : b ⟶ c} {f : a ⟶ b} {h : b ⟶ a} include C attribute to_hom [coercion] protected definition MK [constructor] (f : a ⟶ b) (g : b ⟶ a) (H1 : g ∘ f = id) (H2 : f ∘ g = id) := @(mk f) (is_iso.mk _ H1 H2) variable {C} definition to_inv [reducible] [unfold 5] (f : a ≅ b) : b ⟶ a := (to_hom f)⁻¹ definition to_left_inverse [unfold 5] (f : a ≅ b) : (to_hom f)⁻¹ ∘ (to_hom f) = id := left_inverse (to_hom f) definition to_right_inverse [unfold 5] (f : a ≅ b) : (to_hom f) ∘ (to_hom f)⁻¹ = id := right_inverse (to_hom f) variable [C] protected definition refl [constructor] (a : ob) : a ≅ a := mk (ID a) _ protected definition symm [constructor] ⦃a b : ob⦄ (H : a ≅ b) : b ≅ a := mk (to_hom H)⁻¹ _ protected definition trans [constructor] ⦃a b c : ob⦄ (H1 : a ≅ b) (H2 : b ≅ c) : a ≅ c := mk (to_hom H2 ∘ to_hom H1) _ infixl ` ⬝i `:75 := iso.trans postfix [parsing_only] `⁻¹ⁱ`:(max + 1) := iso.symm definition change_hom [constructor] (H : a ≅ b) (f : a ⟶ b) (p : to_hom H = f) : a ≅ b := iso.MK f (to_inv H) (p ▸ to_left_inverse H) (p ▸ to_right_inverse H) definition change_inv [constructor] (H : a ≅ b) (g : b ⟶ a) (p : to_inv H = g) : a ≅ b := iso.MK (to_hom H) g (p ▸ to_left_inverse H) (p ▸ to_right_inverse H) definition iso_mk_eq {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_prop.elim variable {C} definition iso_eq {f f' : a ≅ b} (p : to_hom f = to_hom f') : f = f' := by (cases f; cases f'; apply (iso_mk_eq p)) variable [C] -- The structure for isomorphism can be characterized up to equivalence by a sigma type. protected 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 -- The type of isomorphisms between two objects is a set definition is_set_iso [instance] : is_set (a ≅ b) := begin apply is_trunc_is_equiv_closed, apply equiv.to_is_equiv (!iso.sigma_char), end definition iso_of_eq [unfold 5] (p : a = b) : a ≅ b := eq.rec_on p (iso.refl a) definition hom_of_eq [reducible] [unfold 5] (p : a = b) : a ⟶ b := iso.to_hom (iso_of_eq p) definition inv_of_eq [reducible] [unfold 5] (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 theorem hom_of_eq_inv (p : a = b) : hom_of_eq p⁻¹ = inv_of_eq p := eq.rec_on p idp theorem inv_of_eq_inv (p : a = b) : inv_of_eq p⁻¹ = hom_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 !id_id⁻¹)) 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) := by induction p; exact !id_leftright⁻¹ definition transport_hom_of_eq_right (p : x = y) (f : hom c (F x)) : p ▸ f = hom_of_eq (ap F p) ∘ f := by induction p; exact !id_left⁻¹ definition transport_hom_of_eq_left (p : x = y) (f : hom (F x) c) : p ▸ f = f ∘ inv_of_eq (ap F p) := by induction p; exact !id_right⁻¹ 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) : {right_inv 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 f) ... = 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 attribute iso.refl [refl] attribute iso.symm [symm] attribute iso.trans [trans] 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 theorem comp.right_inverse : q ∘ q⁻¹ = id := !right_inverse theorem comp.left_inverse : q⁻¹ ∘ q = id := !left_inverse theorem inverse_comp_cancel_left : q⁻¹ ∘ (q ∘ p) = p := by rewrite [assoc, left_inverse, id_left] theorem comp_inverse_cancel_left : q ∘ (q⁻¹ ∘ g) = g := by rewrite [assoc, right_inverse, id_left] theorem comp_inverse_cancel_right : (r ∘ q) ∘ q⁻¹ = r := by rewrite [-assoc, right_inverse, id_right] theorem inverse_comp_cancel_right : (f ∘ q⁻¹) ∘ q = f := by rewrite [-assoc, left_inverse, id_right] theorem 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]) theorem inverse_comp_inverse_left [H' : is_iso g] : (q⁻¹ ∘ g)⁻¹ = g⁻¹ ∘ q := inverse_involutive q ▸ comp_inverse q⁻¹ g theorem inverse_comp_inverse_right [H' : is_iso f] : (q ∘ f⁻¹)⁻¹ = f ∘ q⁻¹ := inverse_involutive f ▸ comp_inverse q f⁻¹ theorem 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} {r' : c ⟶ d} {i : b ⟶ c} {f : b ⟶ a} {r : c ⟶ d} {q : b ⟶ c} {p : a ⟶ b} {g : d ⟶ c} {h : c ⟶ b} {p' : a ⟶ b} {x : b ⟶ d} {z : a ⟶ c} {y : d ⟶ b} {w : c ⟶ a} variable [Hq : is_iso q] include Hq theorem comp_eq_of_eq_inverse_comp (H : y = q⁻¹ ∘ g) : q ∘ y = g := H⁻¹ ▸ comp_inverse_cancel_left q g theorem comp_eq_of_eq_comp_inverse (H : w = f ∘ q⁻¹) : w ∘ q = f := H⁻¹ ▸ inverse_comp_cancel_right f q theorem eq_comp_of_inverse_comp_eq (H : q⁻¹ ∘ g = y) : g = q ∘ y := (comp_eq_of_eq_inverse_comp H⁻¹)⁻¹ theorem eq_comp_of_comp_inverse_eq (H : f ∘ q⁻¹ = w) : f = w ∘ q := (comp_eq_of_eq_comp_inverse H⁻¹)⁻¹ variable {Hq} theorem inverse_comp_eq_of_eq_comp (H : z = q ∘ p) : q⁻¹ ∘ z = p := H⁻¹ ▸ inverse_comp_cancel_left q p theorem comp_inverse_eq_of_eq_comp (H : x = r ∘ q) : x ∘ q⁻¹ = r := H⁻¹ ▸ comp_inverse_cancel_right r q theorem eq_inverse_comp_of_comp_eq (H : q ∘ p = z) : p = q⁻¹ ∘ z := (inverse_comp_eq_of_eq_comp H⁻¹)⁻¹ theorem eq_comp_inverse_of_comp_eq (H : r ∘ q = x) : r = x ∘ q⁻¹ := (comp_inverse_eq_of_eq_comp H⁻¹)⁻¹ theorem eq_inverse_of_comp_eq_id' (H : h ∘ q = id) : h = q⁻¹ := (inverse_eq_left H)⁻¹ theorem eq_inverse_of_comp_eq_id (H : q ∘ h = id) : h = q⁻¹ := (inverse_eq_right H)⁻¹ theorem inverse_eq_of_id_eq_comp (H : id = h ∘ q) : q⁻¹ = h := (eq_inverse_of_comp_eq_id' H⁻¹)⁻¹ theorem inverse_eq_of_id_eq_comp' (H : id = q ∘ h) : q⁻¹ = h := (eq_inverse_of_comp_eq_id H⁻¹)⁻¹ variable [Hq] theorem eq_of_comp_inverse_eq_id (H : i ∘ q⁻¹ = id) : i = q := eq_inverse_of_comp_eq_id' H ⬝ inverse_involutive q theorem eq_of_inverse_comp_eq_id (H : q⁻¹ ∘ i = id) : i = q := eq_inverse_of_comp_eq_id H ⬝ inverse_involutive q theorem eq_of_id_eq_comp_inverse (H : id = i ∘ q⁻¹) : q = i := (eq_of_comp_inverse_eq_id H⁻¹)⁻¹ theorem eq_of_id_eq_inverse_comp (H : id = q⁻¹ ∘ i) : q = i := (eq_of_inverse_comp_eq_id H⁻¹)⁻¹ variables (q) theorem comp.cancel_left (H : q ∘ p = q ∘ p') : p = p' := by rewrite [-inverse_comp_cancel_left q, H, inverse_comp_cancel_left q] theorem comp.cancel_right (H : r ∘ q = r' ∘ q) : r = r' := by rewrite [-comp_inverse_cancel_right _ q, H, comp_inverse_cancel_right _ q] end end iso
f7fdc13642f154b7798abafa74d6e7e824c052a5
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/data/nat/fib.lean
6213b9500fd8074326fb3925d85e20ebece3a390
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,564
lean
/- Copyright (c) 2019 Kevin Kappelmann. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Kappelmann -/ import data.stream.basic import tactic import data.nat.gcd /-! # The Fibonacci Sequence ## Summary Definition of the Fibonacci sequence `F₀ = 0, F₁ = 1, Fₙ₊₂ = Fₙ + Fₙ₊₁`. ## Main Definitions - `fib` returns the stream of Fibonacci numbers. ## Main Statements - `fib_succ_succ` : shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.`. - `fib_gcd` : `fib n` is a strong divisibility sequence. ## Implementation Notes For efficiency purposes, the sequence is defined using `stream.iterate`. ## Tags fib, fibonacci -/ namespace nat /-- Auxiliary function used in the definition of `fib_aux_stream`. -/ private def fib_aux_step : (ℕ × ℕ) → (ℕ × ℕ) := λ p, ⟨p.snd, p.fst + p.snd⟩ /-- Auxiliary stream creating Fibonacci pairs `⟨Fₙ, Fₙ₊₁⟩`. -/ private def fib_aux_stream : stream (ℕ × ℕ) := stream.iterate fib_aux_step ⟨0, 1⟩ /-- Implementation of the fibonacci sequence satisfying `fib 0 = 0, fib 1 = 1, fib (n + 2) = fib n + fib (n + 1)`. *Note:* We use a stream iterator for better performance when compared to the naive recursive implementation. -/ @[pp_nodot] def fib (n : ℕ) : ℕ := (fib_aux_stream n).fst @[simp] lemma fib_zero : fib 0 = 0 := rfl @[simp] lemma fib_one : fib 1 = 1 := rfl @[simp] lemma fib_two : fib 2 = 1 := rfl private lemma fib_aux_stream_succ {n : ℕ} : fib_aux_stream (n + 1) = fib_aux_step (fib_aux_stream n) := begin change (stream.nth (n + 1) $ stream.iterate fib_aux_step ⟨0, 1⟩) = fib_aux_step (stream.nth n $ stream.iterate fib_aux_step ⟨0, 1⟩), rw [stream.nth_succ_iterate, stream.map_iterate, stream.nth_map] end /-- Shows that `fib` indeed satisfies the Fibonacci recurrence `Fₙ₊₂ = Fₙ + Fₙ₊₁.` -/ lemma fib_succ_succ {n : ℕ} : fib (n + 2) = fib n + fib (n + 1) := by simp only [fib, fib_aux_stream_succ, fib_aux_step] lemma fib_pos {n : ℕ} (n_pos : 0 < n) : 0 < fib n := begin induction n with n IH, case nat.zero { norm_num at n_pos }, case nat.succ { cases n, case nat.zero { simp [fib_succ_succ, zero_lt_one] }, case nat.succ { have : 0 ≤ fib n, by simp, exact (lt_add_of_nonneg_of_lt this $ IH n.succ_pos) }} end lemma fib_le_fib_succ {n : ℕ} : fib n ≤ fib (n + 1) := by { cases n; simp [fib_succ_succ] } @[mono] lemma fib_mono : monotone fib := monotone_of_monotone_nat $ λ _, fib_le_fib_succ lemma le_fib_self {n : ℕ} (five_le_n : 5 ≤ n) : n ≤ fib n := begin induction five_le_n with n five_le_n IH, { have : 5 = fib 5, by refl, -- 5 ≤ fib 5 exact le_of_eq this }, { -- n + 1 ≤ fib (n + 1) for 5 ≤ n cases n with n', -- rewrite n = succ n' to use fib.succ_succ { have : 5 = 0, from nat.le_zero_iff.elim_left five_le_n, contradiction }, rw fib_succ_succ, suffices : 1 + (n' + 1) ≤ fib n' + fib (n' + 1), by rwa [nat.succ_eq_add_one, add_comm], have : n' ≠ 0, by { intro h, have : 5 ≤ 1, by rwa h at five_le_n, norm_num at this }, have : 1 ≤ fib n', from nat.succ_le_of_lt (fib_pos $ pos_iff_ne_zero.mpr this), mono } end /-- Subsequent Fibonacci numbers are coprime, see https://proofwiki.org/wiki/Consecutive_Fibonacci_Numbers_are_Coprime -/ lemma fib_coprime_fib_succ (n : ℕ) : nat.coprime (fib n) (fib (n + 1)) := begin unfold coprime, induction n with n ih, { simp }, { convert ih using 1, rw [fib_succ_succ, succ_eq_add_one, gcd_rec, add_mod_right, gcd_comm (fib n), gcd_rec (fib (n + 1))], } end /-- See https://proofwiki.org/wiki/Fibonacci_Number_in_terms_of_Smaller_Fibonacci_Numbers -/ lemma fib_add (m n : ℕ) : fib m * fib n + fib (m + 1) * fib (n + 1) = fib (m + n + 1) := begin induction n with n ih generalizing m, { simp }, { intros, specialize ih (m + 1), rw [add_assoc m 1 n, add_comm 1 n] at ih, simp only [fib_succ_succ, ← ih], ring, } end lemma gcd_fib_add_self (m n : ℕ) : gcd (fib m) (fib (n + m)) = gcd (fib m) (fib n) := begin cases eq_zero_or_pos n, { rw h, simp }, replace h := nat.succ_pred_eq_of_pos h, rw [← h, succ_eq_add_one], calc gcd (fib m) (fib (n.pred + 1 + m)) = gcd (fib m) (fib (n.pred) * (fib m) + fib (n.pred + 1) * fib (m + 1)) : by { rw fib_add n.pred _, ring } ... = gcd (fib m) (fib (n.pred + 1) * fib (m + 1)) : by rw [add_comm, gcd_add_mul_self (fib m) _ (fib (n.pred))] ... = gcd (fib m) (fib (n.pred + 1)) : coprime.gcd_mul_right_cancel_right (fib (n.pred + 1)) (coprime.symm (fib_coprime_fib_succ m)) end lemma gcd_fib_add_mul_self (m n : ℕ) : ∀ k, gcd (fib m) (fib (n + k * m)) = gcd (fib m) (fib n) | 0 := by simp | (k+1) := by rw [← gcd_fib_add_mul_self k, add_mul, ← add_assoc, one_mul, gcd_fib_add_self _ _] /-- `fib n` is a strong divisibility sequence, see https://proofwiki.org/wiki/GCD_of_Fibonacci_Numbers -/ lemma fib_gcd (m n : ℕ) : fib (gcd m n) = gcd (fib m) (fib n) := begin wlog h : m ≤ n using [n m, m n], exact le_total m n, { apply gcd.induction m n, { simp }, intros m n mpos h, rw ← gcd_rec m n at h, conv_rhs { rw ← mod_add_div n m }, rwa [mul_comm, gcd_fib_add_mul_self m (n % m) (n / m), gcd_comm (fib m) _] }, rwa [gcd_comm, gcd_comm (fib m)] end lemma fib_dvd (m n : ℕ) (h : m ∣ n) : fib m ∣ fib n := by rwa [gcd_eq_left_iff_dvd, ← fib_gcd, gcd_eq_left_iff_dvd.mp] end nat
eb8bf5ec253700351426e42b9fc17c354acd18e9
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/set_theory/cofinality.lean
2d4398224e8ef5e08cedf9c29429b96050316c7d
[ "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
23,544
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Floris van Doorn -/ import set_theory.cardinal_ordinal /-! # Cofinality This file contains the definition of cofinality of a ordinal number and regular cardinals ## Main Definitions * `ordinal.cof o` is the cofinality of the ordinal `o`. If `o` is the order type of the relation `<` on `α`, then `o.cof` is the smallest cardinality of a subset `s` of α that is *cofinal* in `α`, i.e. `∀ x : α, ∃ y ∈ s, ¬ y < x`. * `cardinal.is_limit c` means that `c` is a (weak) limit cardinal: `c ≠ 0 ∧ ∀ x < c, succ x < c`. * `cardinal.is_strong_limit c` means that `c` is a strong limit cardinal: `c ≠ 0 ∧ ∀ x < c, 2 ^ x < c`. * `cardinal.is_regular c` means that `c` is a regular cardinal: `omega ≤ c ∧ c.ord.cof = c`. * `cardinal.is_inaccessible c` means that `c` is strongly inaccessible: `omega < c ∧ is_regular c ∧ is_strong_limit c`. ## Main Statements * `ordinal.infinite_pigeonhole_card`: the infinite pigeonhole principle * `cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for `c ≥ cardinal.omega` * `cardinal.univ_inaccessible`: The type of ordinals in `Type u` form an inaccessible cardinal (in `Type v` with `v > u`). This shows (externally) that in `Type u` there are at least `u` inaccessible cardinals. ## Implementation Notes * The cofinality is defined for ordinals. If `c` is a cardinal number, its cofinality is `c.ord.cof`. ## Tags cofinality, regular cardinals, limits cardinals, inaccessible cardinals, infinite pigeonhole principle -/ noncomputable theory open function cardinal set open_locale classical universes u v w variables {α : Type*} {r : α → α → Prop} namespace order /-- Cofinality of a reflexive order `≼`. This is the smallest cardinality of a subset `S : set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/ def cof (r : α → α → Prop) [is_refl α r] : cardinal := @cardinal.min {S : set α // ∀ a, ∃ b ∈ S, r a b} ⟨⟨set.univ, λ a, ⟨a, ⟨⟩, refl _⟩⟩⟩ (λ S, mk S) lemma cof_le (r : α → α → Prop) [is_refl α r] {S : set α} (h : ∀a, ∃(b ∈ S), r a b) : order.cof r ≤ mk S := le_trans (cardinal.min_le _ ⟨S, h⟩) (le_refl _) lemma le_cof {r : α → α → Prop} [is_refl α r] (c : cardinal) : c ≤ order.cof r ↔ ∀ {S : set α} (h : ∀a, ∃(b ∈ S), r a b) , c ≤ mk S := by { rw [order.cof, cardinal.le_min], exact ⟨λ H S h, H ⟨S, h⟩, λ H ⟨S, h⟩, H h ⟩ } end order theorem rel_iso.cof.aux {α : Type u} {β : Type v} {r s} [is_refl α r] [is_refl β s] (f : r ≃r s) : cardinal.lift.{u (max u v)} (order.cof r) ≤ cardinal.lift.{v (max u v)} (order.cof s) := begin rw [order.cof, order.cof, lift_min, lift_min, cardinal.le_min], intro S, cases S with S H, simp [(∘)], refine le_trans (min_le _ _) _, { exact ⟨f ⁻¹' S, λ a, let ⟨b, bS, h⟩ := H (f a) in ⟨f.symm b, by simp [bS, ← f.map_rel_iff, h, -coe_fn_coe_base, -coe_fn_coe_trans, principal_seg.coe_coe_fn', initial_seg.coe_coe_fn]⟩⟩ }, { exact lift_mk_le.{u v (max u v)}.2 ⟨⟨λ ⟨x, h⟩, ⟨f x, h⟩, λ ⟨x, h₁⟩ ⟨y, h₂⟩ h₃, by congr; injection h₃ with h'; exact f.to_equiv.injective h'⟩⟩ } end theorem rel_iso.cof {α : Type u} {β : Type v} {r s} [is_refl α r] [is_refl β s] (f : r ≃r s) : cardinal.lift.{u (max u v)} (order.cof r) = cardinal.lift.{v (max u v)} (order.cof s) := le_antisymm (rel_iso.cof.aux f) (rel_iso.cof.aux f.symm) def strict_order.cof (r : α → α → Prop) [h : is_irrefl α r] : cardinal := @order.cof α (λ x y, ¬ r y x) ⟨h.1⟩ namespace ordinal /-- Cofinality of an ordinal. This is the smallest cardinal of a subset `S` of the ordinal which is unbounded, in the sense `∀ a, ∃ b ∈ S, ¬(b > a)`. It is defined for all ordinals, but `cof 0 = 0` and `cof (succ o) = 1`, so it is only really interesting on limit ordinals (when it is an infinite cardinal). -/ def cof (o : ordinal.{u}) : cardinal.{u} := quot.lift_on o (λ ⟨α, r, _⟩, by exactI strict_order.cof r) begin rintros ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨⟨f, hf⟩⟩, rw ← cardinal.lift_inj, apply rel_iso.cof ⟨f, _⟩, simp [hf] end lemma cof_type (r : α → α → Prop) [is_well_order α r] : (type r).cof = strict_order.cof r := rfl theorem le_cof_type [is_well_order α r] {c} : c ≤ cof (type r) ↔ ∀ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) → c ≤ mk S := by dsimp [cof, strict_order.cof, order.cof, type, quotient.mk, quot.lift_on]; rw [cardinal.le_min, subtype.forall]; refl theorem cof_type_le [is_well_order α r] (S : set α) (h : ∀ a, ∃ b ∈ S, ¬ r b a) : cof (type r) ≤ mk S := le_cof_type.1 (le_refl _) S h theorem lt_cof_type [is_well_order α r] (S : set α) (hl : mk S < cof (type r)) : ∃ a, ∀ b ∈ S, r b a := not_forall_not.1 $ λ h, not_le_of_lt hl $ cof_type_le S (λ a, not_ball.1 (h a)) theorem cof_eq (r : α → α → Prop) [is_well_order α r] : ∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ mk S = cof (type r) := begin have : ∃ i, cof (type r) = _, { dsimp [cof, order.cof, type, quotient.mk, quot.lift_on], apply cardinal.min_eq }, exact let ⟨⟨S, hl⟩, e⟩ := this in ⟨S, hl, e.symm⟩, end theorem ord_cof_eq (r : α → α → Prop) [is_well_order α r] : ∃ S : set α, (∀ a, ∃ b ∈ S, ¬ r b a) ∧ type (subrel r S) = (cof (type r)).ord := let ⟨S, hS, e⟩ := cof_eq r, ⟨s, _, e'⟩ := cardinal.ord_eq S, T : set α := {a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a} in begin resetI, suffices, { refine ⟨T, this, le_antisymm _ (cardinal.ord_le.2 $ cof_type_le T this)⟩, rw [← e, e'], refine type_le'.2 ⟨rel_embedding.of_monotone (λ a, ⟨a, let ⟨aS, _⟩ := a.2 in aS⟩) (λ a b h, _)⟩, rcases a with ⟨a, aS, ha⟩, rcases b with ⟨b, bS, hb⟩, change s ⟨a, _⟩ ⟨b, _⟩, refine ((trichotomous_of s _ _).resolve_left (λ hn, _)).resolve_left _, { exact asymm h (ha _ hn) }, { intro e, injection e with e, subst b, exact irrefl _ h } }, { intro a, have : {b : S | ¬ r b a}.nonempty := let ⟨b, bS, ba⟩ := hS a in ⟨⟨b, bS⟩, ba⟩, let b := (is_well_order.wf).min _ this, have ba : ¬r b a := (is_well_order.wf).min_mem _ this, refine ⟨b, ⟨b.2, λ c, not_imp_not.1 $ λ h, _⟩, ba⟩, rw [show ∀b:S, (⟨b, b.2⟩:S) = b, by intro b; cases b; refl], exact (is_well_order.wf).not_lt_min _ this (is_order_connected.neg_trans h ba) } end theorem lift_cof (o) : (cof o).lift = cof o.lift := induction_on o $ begin introsI α r _, cases lift_type r with _ e, rw e, apply le_antisymm, { unfreezingI { refine le_cof_type.2 (λ S H, _) }, have : (mk (ulift.up ⁻¹' S)).lift ≤ mk S := ⟨⟨λ ⟨⟨x, h⟩⟩, ⟨⟨x⟩, h⟩, λ ⟨⟨x, h₁⟩⟩ ⟨⟨y, h₂⟩⟩ e, by simp at e; congr; injection e⟩⟩, refine le_trans (cardinal.lift_le.2 $ cof_type_le _ _) this, exact λ a, let ⟨⟨b⟩, bs, br⟩ := H ⟨a⟩ in ⟨b, bs, br⟩ }, { rcases cof_eq r with ⟨S, H, e'⟩, have : mk (ulift.down ⁻¹' S) ≤ (mk S).lift := ⟨⟨λ ⟨⟨x⟩, h⟩, ⟨⟨x, h⟩⟩, λ ⟨⟨x⟩, h₁⟩ ⟨⟨y⟩, h₂⟩ e, by simp at e; congr; injections⟩⟩, rw e' at this, unfreezingI { refine le_trans (cof_type_le _ _) this }, exact λ ⟨a⟩, let ⟨b, bs, br⟩ := H a in ⟨⟨b⟩, bs, br⟩ } end theorem cof_le_card (o) : cof o ≤ card o := induction_on o $ λ α r _, begin resetI, have : mk (@set.univ α) = card (type r) := quotient.sound ⟨equiv.set.univ _⟩, rw ← this, exact cof_type_le set.univ (λ a, ⟨a, ⟨⟩, irrefl a⟩) end theorem cof_ord_le (c : cardinal) : cof c.ord ≤ c := by simpa using cof_le_card c.ord @[simp] theorem cof_zero : cof 0 = 0 := le_antisymm (by simpa using cof_le_card 0) (cardinal.zero_le _) @[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 := ⟨induction_on o $ λ α r _ z, by exactI let ⟨S, hl, e⟩ := cof_eq r in type_eq_zero_iff_empty.2 $ λ ⟨a⟩, let ⟨b, h, _⟩ := hl a in ne_zero_iff_nonempty.2 (by exact ⟨⟨_, h⟩⟩) (e.trans z), λ e, by simp [e]⟩ @[simp] theorem cof_succ (o) : cof (succ o) = 1 := begin apply le_antisymm, { refine induction_on o (λ α r _, _), change cof (type _) ≤ _, rw [← (_ : mk _ = 1)], apply cof_type_le, { refine λ a, ⟨sum.inr punit.star, set.mem_singleton _, _⟩, rcases a with a|⟨⟨⟨⟩⟩⟩; simp [empty_relation] }, { rw [cardinal.fintype_card, set.card_singleton], simp } }, { rw [← cardinal.succ_zero, cardinal.succ_le], simpa [lt_iff_le_and_ne, cardinal.zero_le] using λ h, succ_ne_zero o (cof_eq_zero.1 (eq.symm h)) } end @[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a := ⟨induction_on o $ λ α r _ z, begin resetI, rcases cof_eq r with ⟨S, hl, e⟩, rw z at e, cases ne_zero_iff_nonempty.1 (by rw e; exact one_ne_zero) with a, refine ⟨typein r a, eq.symm $ quotient.sound ⟨rel_iso.of_surjective (rel_embedding.of_monotone _ (λ x y, _)) (λ x, _)⟩⟩, { apply sum.rec; [exact subtype.val, exact λ _, a] }, { rcases x with x|⟨⟨⟨⟩⟩⟩; rcases y with y|⟨⟨⟨⟩⟩⟩; simp [subrel, order.preimage, empty_relation], exact x.2 }, { suffices : r x a ∨ ∃ (b : punit), ↑a = x, {simpa}, rcases trichotomous_of r x a with h|h|h, { exact or.inl h }, { exact or.inr ⟨punit.star, h.symm⟩ }, { rcases hl x with ⟨a', aS, hn⟩, rw (_ : ↑a = a') at h, {exact absurd h hn}, refine congr_arg subtype.val (_ : a = ⟨a', aS⟩), haveI := le_one_iff_subsingleton.1 (le_of_eq e), apply subsingleton.elim } } end, λ ⟨a, e⟩, by simp [e]⟩ @[simp] theorem cof_add (a b : ordinal) : b ≠ 0 → cof (a + b) = cof b := induction_on a $ λ α r _, induction_on b $ λ β s _ b0, begin resetI, change cof (type _) = _, refine eq_of_forall_le_iff (λ c, _), rw [le_cof_type, le_cof_type], split; intros H S hS, { refine le_trans (H {a | sum.rec_on a (∅:set α) S} (λ a, _)) ⟨⟨_, _⟩⟩, { cases a with a b, { cases type_ne_zero_iff_nonempty.1 b0 with b, rcases hS b with ⟨b', bs, _⟩, exact ⟨sum.inr b', bs, by simp⟩ }, { rcases hS b with ⟨b', bs, h⟩, exact ⟨sum.inr b', bs, by simp [h]⟩ } }, { exact λ a, match a with ⟨sum.inr b, h⟩ := ⟨b, h⟩ end }, { exact λ a b, match a, b with ⟨sum.inr a, h₁⟩, ⟨sum.inr b, h₂⟩, h := by congr; injection h end } }, { refine le_trans (H (sum.inr ⁻¹' S) (λ a, _)) ⟨⟨_, _⟩⟩, { rcases hS (sum.inr a) with ⟨a'|b', bs, h⟩; simp at h, { cases h }, { exact ⟨b', bs, h⟩ } }, { exact λ ⟨a, h⟩, ⟨_, h⟩ }, { exact λ ⟨a, h₁⟩ ⟨b, h₂⟩ h, by injection h with h; congr; injection h } } end @[simp] theorem cof_cof (o : ordinal) : cof (cof o).ord= cof o := le_antisymm (le_trans (cof_le_card _) (by simp)) $ induction_on o $ λ α r _, by exactI let ⟨S, hS, e₁⟩ := ord_cof_eq r, ⟨T, hT, e₂⟩ := cof_eq (subrel r S) in begin rw e₁ at e₂, rw ← e₂, refine le_trans (cof_type_le {a | ∃ h, (subtype.mk a h : S) ∈ T} (λ a, _)) ⟨⟨_, _⟩⟩, { rcases hS a with ⟨b, bS, br⟩, rcases hT ⟨b, bS⟩ with ⟨⟨c, cS⟩, cT, cs⟩, exact ⟨c, ⟨cS, cT⟩, is_order_connected.neg_trans cs br⟩ }, { exact λ ⟨a, h⟩, ⟨⟨a, h.fst⟩, h.snd⟩ }, { exact λ ⟨a, ha⟩ ⟨b, hb⟩ h, by injection h with h; congr; injection h }, end theorem omega_le_cof {o} : cardinal.omega ≤ cof o ↔ is_limit o := begin rcases zero_or_succ_or_limit o with rfl|⟨o,rfl⟩|l, { simp [not_zero_is_limit, cardinal.omega_ne_zero] }, { simp [not_succ_is_limit, cardinal.one_lt_omega] }, { simp [l], refine le_of_not_lt (λ h, _), cases cardinal.lt_omega.1 h with n e, have := cof_cof o, rw [e, ord_nat] at this, cases n, { simp at e, simpa [e, not_zero_is_limit] using l }, { rw [← nat_cast_succ, cof_succ] at this, rw [← this, cof_eq_one_iff_is_succ] at e, rcases e with ⟨a, rfl⟩, exact not_succ_is_limit _ l } } end @[simp] theorem cof_omega : cof omega = cardinal.omega := le_antisymm (by rw ← card_omega; apply cof_le_card) (omega_le_cof.2 omega_is_limit) theorem cof_eq' (r : α → α → Prop) [is_well_order α r] (h : is_limit (type r)) : ∃ S : set α, (∀ a, ∃ b ∈ S, r a b) ∧ mk S = cof (type r) := let ⟨S, H, e⟩ := cof_eq r in ⟨S, λ a, let a' := enum r _ (h.2 _ (typein_lt_type r a)) in let ⟨b, h, ab⟩ := H a' in ⟨b, h, (is_order_connected.conn a b a' $ (typein_lt_typein r).1 (by rw typein_enum; apply ordinal.lt_succ_self)).resolve_right ab⟩, e⟩ theorem cof_sup_le_lift {ι} (f : ι → ordinal) (H : ∀ i, f i < sup f) : cof (sup f) ≤ (mk ι).lift := begin generalize e : sup f = o, refine ordinal.induction_on o _ e, introsI α r _ e', rw e' at H, refine le_trans (cof_type_le (set.range (λ i, enum r _ (H i))) _) ⟨embedding.of_surjective _ _⟩, { intro a, by_contra h, apply not_le_of_lt (typein_lt_type r a), rw [← e', sup_le], intro i, have h : ∀ (x : ι), r (enum r (f x) _) a, { simpa using h }, simpa only [typein_enum] using le_of_lt ((typein_lt_typein r).2 (h i)) }, { exact λ i, ⟨_, set.mem_range_self i.1⟩ }, { intro a, rcases a with ⟨_, i, rfl⟩, exact ⟨⟨i⟩, by simp⟩ } end theorem cof_sup_le {ι} (f : ι → ordinal) (H : ∀ i, f i < sup.{u u} f) : cof (sup.{u u} f) ≤ mk ι := by simpa using cof_sup_le_lift.{u u} f H theorem cof_bsup_le_lift {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup o f) → cof (bsup o f) ≤ o.card.lift := induction_on o $ λ α r _ f H, by rw bsup_type; refine cof_sup_le_lift _ _; rw ← bsup_type; intro a; apply H theorem cof_bsup_le {o : ordinal} : ∀ (f : Π a < o, ordinal), (∀ i h, f i h < bsup.{u u} o f) → cof (bsup.{u u} o f) ≤ o.card := induction_on o $ λ α r _ f H, by simpa using cof_bsup_le_lift.{u u} f H @[simp] theorem cof_univ : cof univ.{u v} = cardinal.univ := le_antisymm (cof_le_card _) begin refine le_of_forall_lt (λ c h, _), rcases lt_univ'.1 h with ⟨c, rfl⟩, rcases @cof_eq ordinal.{u} (<) _ with ⟨S, H, Se⟩, rw [univ, ← lift_cof, ← cardinal.lift_lift, cardinal.lift_lt, ← Se], refine lt_of_not_ge (λ h, _), cases cardinal.lift_down h with a e, refine quotient.induction_on a (λ α e, _) e, cases quotient.exact e with f, have f := equiv.ulift.symm.trans f, let g := λ a, (f a).1, let o := succ (sup.{u u} g), rcases H o with ⟨b, h, l⟩, refine l (lt_succ.2 _), rw ← show g (f.symm ⟨b, h⟩) = b, by dsimp [g]; simp, apply le_sup end theorem sup_lt_ord {ι} (f : ι → ordinal) {c : ordinal} (H1 : cardinal.mk ι < c.cof) (H2 : ∀ i, f i < c) : sup.{u u} f < c := begin apply lt_of_le_of_ne, { rw [sup_le], exact λ i, le_of_lt (H2 i) }, rintro h, apply not_le_of_lt H1, simpa [sup_ord, H2, h] using cof_sup_le.{u} f end theorem sup_lt {ι} (f : ι → cardinal) {c : cardinal} (H1 : cardinal.mk ι < c.ord.cof) (H2 : ∀ i, f i < c) : cardinal.sup.{u u} f < c := by { rw [←ord_lt_ord, ←sup_ord], apply sup_lt_ord _ H1, intro i, rw ord_lt_ord, apply H2 } /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : is_well_order α r] {s : set (set α)} (h₁ : unbounded r $ ⋃₀ s) (h₂ : mk s < strict_order.cof r) : ∃(x ∈ s), unbounded r x := begin by_contra h, simp only [not_exists, exists_prop, not_and, not_unbounded_iff] at h, apply not_le_of_lt h₂, let f : s → α := λ x : s, wo.wf.sup x (h x.1 x.2), let t : set α := range f, have : mk t ≤ mk s, exact mk_range_le, refine le_trans _ this, have : unbounded r t, { intro x, rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩, refine ⟨f ⟨c, hc⟩, mem_range_self _, _⟩, intro hxz, apply hxy, refine trans (wo.wf.lt_sup _ hy) hxz }, exact cardinal.min_le _ (subtype.mk t this) end /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_Union {α β : Type u} (r : α → α → Prop) [wo : is_well_order α r] (s : β → set α) (h₁ : unbounded r $ ⋃x, s x) (h₂ : mk β < strict_order.cof r) : ∃x : β, unbounded r (s x) := begin rw [← sUnion_range] at h₁, have : mk ↥(range (λ (i : β), s i)) < strict_order.cof r := lt_of_le_of_lt mk_range_le h₂, rcases unbounded_of_unbounded_sUnion r h₁ this with ⟨_, ⟨x, rfl⟩, u⟩, exact ⟨x, u⟩ end /-- The infinite pigeonhole principle -/ theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : cardinal.omega ≤ mk β) (h₂ : mk α < (mk β).ord.cof) : ∃a : α, mk (f ⁻¹' {a}) = mk β := begin have : ¬∀a, mk (f ⁻¹' {a}) < mk β, { intro h, apply not_lt_of_ge (ge_of_eq $ mk_univ), rw [←@preimage_univ _ _ f, ←Union_of_singleton, preimage_Union], apply lt_of_le_of_lt mk_Union_le_sum_mk, apply lt_of_le_of_lt (sum_le_sup _), apply mul_lt_of_lt h₁ (lt_of_lt_of_le h₂ $ cof_ord_le _), exact sup_lt _ h₂ h }, rw [not_forall] at this, cases this with x h, use x, apply le_antisymm _ (le_of_not_gt h), rw [le_mk_iff_exists_set], exact ⟨_, rfl⟩ end /-- pigeonhole principle for a cardinality below the cardinality of the domain -/ theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : cardinal) (hθ : θ ≤ mk β) (h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) : ∃a : α, θ ≤ mk (f ⁻¹' {a}) := begin rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩, cases infinite_pigeonhole (f ∘ subtype.val : s → α) h₁ h₂ with a ha, use a, rw [←ha, @preimage_comp _ _ _ subtype.val f], apply mk_preimage_of_injective _ _ subtype.val_injective end theorem infinite_pigeonhole_set {β α : Type u} {s : set β} (f : s → α) (θ : cardinal) (hθ : θ ≤ mk s) (h₁ : cardinal.omega ≤ θ) (h₂ : mk α < θ.ord.cof) : ∃(a : α) (t : set β) (h : t ⊆ s), θ ≤ mk t ∧ ∀{{x}} (hx : x ∈ t), f ⟨x, h hx⟩ = a := begin cases infinite_pigeonhole_card f θ hθ h₁ h₂ with a ha, refine ⟨a, {x | ∃(h : x ∈ s), f ⟨x, h⟩ = a}, _, _, _⟩, { rintro x ⟨hx, hx'⟩, exact hx }, { refine le_trans ha _, apply ge_of_eq, apply quotient.sound, constructor, refine equiv.trans _ (equiv.subtype_subtype_equiv_subtype_exists _ _).symm, simp only [set_coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_set_of_eq] }, rintro x ⟨hx, hx'⟩, exact hx' end end ordinal namespace cardinal open ordinal local infixr ^ := @pow cardinal.{u} cardinal cardinal.has_pow /-- A cardinal is a limit if it is not zero or a successor cardinal. Note that `ω` is a limit cardinal by this definition. -/ def is_limit (c : cardinal) : Prop := c ≠ 0 ∧ ∀ x < c, succ x < c /-- A cardinal is a strong limit if it is not zero and it is closed under powersets. Note that `ω` is a strong limit by this definition. -/ def is_strong_limit (c : cardinal) : Prop := c ≠ 0 ∧ ∀ x < c, 2 ^ x < c theorem is_strong_limit.is_limit {c} (H : is_strong_limit c) : is_limit c := ⟨H.1, λ x h, lt_of_le_of_lt (succ_le.2 $ cantor _) (H.2 _ h)⟩ /-- A cardinal is regular if it is infinite and it equals its own cofinality. -/ def is_regular (c : cardinal) : Prop := omega ≤ c ∧ c.ord.cof = c theorem cof_is_regular {o : ordinal} (h : o.is_limit) : is_regular o.cof := ⟨omega_le_cof.2 h, cof_cof _⟩ theorem omega_is_regular : is_regular omega := ⟨le_refl _, by simp⟩ theorem succ_is_regular {c : cardinal.{u}} (h : omega ≤ c) : is_regular (succ c) := ⟨le_trans h (le_of_lt $ lt_succ_self _), begin refine le_antisymm (cof_ord_le _) (succ_le.2 _), cases quotient.exists_rep (succ c) with α αe, simp at αe, rcases ord_eq α with ⟨r, wo, re⟩, resetI, have := ord_is_limit (le_trans h $ le_of_lt $ lt_succ_self _), rw [← αe, re] at this ⊢, rcases cof_eq' r this with ⟨S, H, Se⟩, rw [← Se], apply lt_imp_lt_of_le_imp_le (λ (h : mk S ≤ c), canonically_ordered_semiring.mul_le_mul_right' h c), rw [mul_eq_self h, ← succ_le, ← αe, ← sum_const], refine le_trans _ (sum_le_sum (λ x:S, card (typein r x)) _ _), { simp [typein, sum_mk (λ x:S, {a//r a x})], refine ⟨embedding.of_surjective _ _⟩, { exact λ x, x.2.1 }, { exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } }, { intro i, rw [← lt_succ, ← lt_ord, ← αe, re], apply typein_lt_type } end⟩ theorem sup_lt_ord_of_is_regular {ι} (f : ι → ordinal) {c} (hc : is_regular c) (H1 : cardinal.mk ι < c) (H2 : ∀ i, f i < c.ord) : ordinal.sup.{u u} f < c.ord := by { apply sup_lt_ord _ _ H2, rw [hc.2], exact H1 } theorem sup_lt_of_is_regular {ι} (f : ι → cardinal) {c} (hc : is_regular c) (H1 : cardinal.mk ι < c) (H2 : ∀ i, f i < c) : sup.{u u} f < c := by { apply sup_lt _ _ H2, rwa [hc.2] } theorem sum_lt_of_is_regular {ι} (f : ι → cardinal) {c} (hc : is_regular c) (H1 : cardinal.mk ι < c) (H2 : ∀ i, f i < c) : sum.{u u} f < c := lt_of_le_of_lt (sum_le_sup _) $ mul_lt_of_lt hc.1 H1 $ sup_lt_of_is_regular f hc H1 H2 /-- A cardinal is inaccessible if it is an uncountable regular strong limit cardinal. -/ def is_inaccessible (c : cardinal) := omega < c ∧ is_regular c ∧ is_strong_limit c theorem is_inaccessible.mk {c} (h₁ : omega < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, 2 ^ x < c) : is_inaccessible c := ⟨h₁, ⟨le_of_lt h₁, le_antisymm (cof_ord_le _) h₂⟩, ne_of_gt (lt_trans omega_pos h₁), h₃⟩ /- Lean's foundations prove the existence of ω many inaccessible cardinals -/ theorem univ_inaccessible : is_inaccessible (univ.{u v}) := is_inaccessible.mk (by simpa using lift_lt_univ' omega) (by simp) (λ c h, begin rcases lt_univ'.1 h with ⟨c, rfl⟩, rw ← lift_two_power.{u (max (u+1) v)}, apply lift_lt_univ' end) theorem lt_power_cof {c : cardinal.{u}} : omega ≤ c → c < c ^ cof c.ord := quotient.induction_on c $ λ α h, begin rcases ord_eq α with ⟨r, wo, re⟩, resetI, have := ord_is_limit h, rw [mk_def, re] at this ⊢, rcases cof_eq' r this with ⟨S, H, Se⟩, have := sum_lt_prod (λ a:S, mk {x // r x a}) (λ _, mk α) (λ i, _), { simp [Se.symm] at this ⊢, refine lt_of_le_of_lt _ this, refine ⟨embedding.of_surjective _ _⟩, { exact λ x, x.2.1 }, { exact λ a, let ⟨b, h, ab⟩ := H a in ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ } }, { have := typein_lt_type r i, rwa [← re, lt_ord] at this } end theorem lt_cof_power {a b : cardinal} (ha : omega ≤ a) (b1 : 1 < b) : a < cof (b ^ a).ord := begin have b0 : b ≠ 0 := ne_of_gt (lt_trans zero_lt_one b1), apply lt_imp_lt_of_le_imp_le (power_le_power_left $ power_ne_zero a b0), rw [← power_mul, mul_eq_self ha], exact lt_power_cof (le_trans ha $ le_of_lt $ cantor' _ b1), end end cardinal
0d5fa75d577a7f14b53cc4e5df76e1a4da8f0ee4
87a08a8e9b222ec02f3327dca4ae24590c1b3de9
/src/topology/order.lean
8532721c75ab9de7afbdd6aa97f8a2fe0df89b52
[ "Apache-2.0" ]
permissive
naussicaa/mathlib
86d05223517a39e80920549a8052f9cf0e0b77b8
1ef2c2df20cf45c21675d855436228c7ae02d47a
refs/heads/master
1,592,104,950,080
1,562,073,069,000
1,562,073,069,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
41,654
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 Order on topological structures. Lattice structure, Galois connection, and applications. -/ import topology.basic open set filter lattice classical local attribute [instance] prop_decidable universes u v w namespace topological_space variables {α : Type u} /-- The least topology containing a collection of basic sets. -/ inductive generate_open (g : set (set α)) : set α → Prop | basic : ∀s∈g, generate_open s | univ : generate_open univ | inter : ∀s t, generate_open s → generate_open t → generate_open (s ∩ t) | sUnion : ∀k, (∀s∈k, generate_open s) → generate_open (⋃₀ k) /-- The smallest topological space containing the collection `g` of basic sets -/ def generate_from (g : set (set α)) : topological_space α := { is_open := generate_open g, is_open_univ := generate_open.univ g, is_open_inter := generate_open.inter, is_open_sUnion := generate_open.sUnion } lemma nhds_generate_from {g : set (set α)} {a : α} : @nhds α (generate_from g) a = (⨅s∈{s | a ∈ s ∧ s ∈ g}, principal s) := by rw nhds_def; exact le_antisymm (infi_le_infi $ assume s, infi_le_infi_const $ assume ⟨as, sg⟩, ⟨as, generate_open.basic _ sg⟩) (le_infi $ assume s, le_infi $ assume ⟨as, hs⟩, begin revert as, clear_, induction hs, case generate_open.basic : s hs { exact assume as, infi_le_of_le s $ infi_le _ ⟨as, hs⟩ }, case generate_open.univ { rw [principal_univ], exact assume _, le_top }, case generate_open.inter : s t hs' ht' hs ht { exact assume ⟨has, hat⟩, calc _ ≤ principal s ⊓ principal t : le_inf (hs has) (ht hat) ... = _ : inf_principal }, case generate_open.sUnion : k hk' hk { exact λ ⟨t, htk, hat⟩, calc _ ≤ principal t : hk t htk hat ... ≤ _ : le_principal_iff.2 $ subset_sUnion_of_mem htk } end) lemma tendsto_nhds_generate_from {β : Type*} {m : α → β} {f : filter α} {g : set (set β)} {b : β} (h : ∀s∈g, b ∈ s → m ⁻¹' s ∈ f) : tendsto m f (@nhds β (generate_from g) b) := by rw [nhds_generate_from]; exact (tendsto_infi.2 $ assume s, tendsto_infi.2 $ assume ⟨hbs, hsg⟩, tendsto_principal.2 $ h s hsg hbs) protected def mk_of_nhds (n : α → filter α) : topological_space α := { is_open := λs, ∀a∈s, s ∈ n a, is_open_univ := assume x h, univ_mem_sets, is_open_inter := assume s t hs ht x ⟨hxs, hxt⟩, inter_mem_sets (hs x hxs) (ht x hxt), is_open_sUnion := assume s hs a ⟨x, hx, hxa⟩, mem_sets_of_superset (hs x hx _ hxa) (set.subset_sUnion_of_mem hx) } lemma nhds_mk_of_nhds (n : α → filter α) (a : α) (h₀ : pure ≤ n) (h₁ : ∀{a s}, s ∈ n a → ∃ t ∈ n a, t ⊆ s ∧ ∀a' ∈ t, s ∈ n a') : @nhds α (topological_space.mk_of_nhds n) a = n a := begin letI := topological_space.mk_of_nhds n, refine le_antisymm (assume s hs, _) (assume s hs, _), { have h₀ : {b | s ∈ n b} ⊆ s := assume b hb, mem_pure_sets.1 $ h₀ b hb, have h₁ : {b | s ∈ n b} ∈ nhds a, { refine mem_nhds_sets (assume b (hb : s ∈ n b), _) hs, rcases h₁ hb with ⟨t, ht, hts, h⟩, exact mem_sets_of_superset ht h }, exact mem_sets_of_superset h₁ h₀ }, { rcases (@mem_nhds_sets_iff α (topological_space.mk_of_nhds n) _ _).1 hs with ⟨t, hts, ht, hat⟩, exact (n a).sets_of_superset (ht _ hat) hts }, end end topological_space section lattice variables {α : Type u} {β : Type v} /- The following partial order will be convenient in order to get a complete lattice instance via the Galois insertion method, but the partial order that will be used elsewhere is the dual one, defined at the end of this section. -/ def tmp_order : partial_order (topological_space α) := { le := λt s, t.is_open ≤ s.is_open, le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₁ h₂, le_refl := assume t, le_refl t.is_open, le_trans := assume a b c h₁ h₂, @le_trans _ _ a.is_open b.is_open c.is_open h₁ h₂ } local attribute [instance] tmp_order private lemma generate_from_le_iff_subset_is_open {g : set (set α)} {t : topological_space α} : topological_space.generate_from g ≤ t ↔ g ⊆ {s | t.is_open s} := iff.intro (assume ht s hs, ht _ $ topological_space.generate_open.basic s hs) (assume hg s hs, hs.rec_on (assume v hv, hg hv) t.is_open_univ (assume u v _ _, t.is_open_inter u v) (assume k _, t.is_open_sUnion k)) protected def mk_of_closure (s : set (set α)) (hs : {u | (topological_space.generate_from s).is_open u} = s) : topological_space α := { is_open := λu, u ∈ s, is_open_univ := hs ▸ topological_space.generate_open.univ _, is_open_inter := hs ▸ topological_space.generate_open.inter, is_open_sUnion := hs ▸ topological_space.generate_open.sUnion } lemma mk_of_closure_sets {s : set (set α)} {hs : {u | (topological_space.generate_from s).is_open u} = s} : mk_of_closure s hs = topological_space.generate_from s := topological_space_eq hs.symm def gi_generate_from (α : Type*) : galois_insertion topological_space.generate_from (λt:topological_space α, {s | t.is_open s}) := { gc := assume g t, generate_from_le_iff_subset_is_open, le_l_u := assume ts s hs, topological_space.generate_open.basic s hs, choice := λg hg, mk_of_closure g (subset.antisymm hg $ generate_from_le_iff_subset_is_open.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } lemma generate_from_mono {α} {g₁ g₂ : set (set α)} (h : g₁ ⊆ g₂) : topological_space.generate_from g₁ ≤ topological_space.generate_from g₂ := (gi_generate_from _).gc.monotone_l h def tmp_complete_lattice {α : Type u} : complete_lattice (topological_space α) := (gi_generate_from α).lift_complete_lattice instance : partial_order (topological_space α) := { le := λ t s, s.is_open ≤ t.is_open, le_antisymm := assume t s h₁ h₂, topological_space_eq $ le_antisymm h₂ h₁, le_refl := assume t, le_refl t.is_open, le_trans := assume a b c h₁ h₂, le_trans h₂ h₁ } lemma le_generate_from_iff_subset_is_open {g : set (set α)} {t : topological_space α} : t ≤ topological_space.generate_from g ↔ g ⊆ {s | t.is_open s} := generate_from_le_iff_subset_is_open instance : complete_lattice (topological_space α) := @order_dual.lattice.complete_lattice _ tmp_complete_lattice class discrete_topology (α : Type*) [t : topological_space α] : Prop := (eq_bot : t = ⊥) @[simp] lemma is_open_discrete [topological_space α] [discrete_topology α] (s : set α) : is_open s := (discrete_topology.eq_bot α).symm ▸ trivial lemma continuous_of_discrete_topology [topological_space α] [discrete_topology α] [topological_space β] {f : α → β} : continuous f := λs hs, is_open_discrete _ lemma nhds_bot (α : Type*) : (@nhds α ⊥) = pure := begin ext a s, rw [mem_nhds_sets_iff, mem_pure_iff], split, { exact assume ⟨t, ht, _, hta⟩, ht hta }, { exact assume h, ⟨{a}, set.singleton_subset_iff.2 h, trivial, set.mem_singleton a⟩ } end lemma nhds_discrete (α : Type*) [topological_space α] [discrete_topology α] : (@nhds α _) = pure := (discrete_topology.eq_bot α).symm ▸ nhds_bot α lemma le_of_nhds_le_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x ≤ @nhds α t₂ x) : t₁ ≤ t₂ := assume s, show @is_open α t₂ s → @is_open α t₁ s, by { simp only [is_open_iff_nhds, le_principal_iff], exact assume hs a ha, h _ $ hs _ ha } lemma eq_of_nhds_eq_nhds {t₁ t₂ : topological_space α} (h : ∀x, @nhds α t₁ x = @nhds α t₂ x) : t₁ = t₂ := le_antisymm (le_of_nhds_le_nhds $ assume x, le_of_eq $ h x) (le_of_nhds_le_nhds $ assume x, le_of_eq $ (h x).symm) lemma eq_bot_of_singletons_open {t : topological_space α} (h : ∀ x, t.is_open {x}) : t = ⊥ := bot_unique $ le_of_nhds_le_nhds $ assume x, have nhds x ≤ pure x, from nhds_le_of_le (mem_singleton _) (h x) (by simp), le_trans this (@pure_le_nhds _ ⊥ x) end lattice section galois_connection variables {α : Type*} {β : Type*} {γ : Type*} /-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of sets that are preimages of some open set in `β`. This is the coarsest topology that makes `f` continuous. -/ def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) : topological_space α := { is_open := λs, ∃s', t.is_open s' ∧ f ⁻¹' s' = s, is_open_univ := ⟨univ, t.is_open_univ, preimage_univ⟩, is_open_inter := by rintro s₁ s₂ ⟨s'₁, hs₁, rfl⟩ ⟨s'₂, hs₂, rfl⟩; exact ⟨s'₁ ∩ s'₂, t.is_open_inter _ _ hs₁ hs₂, preimage_inter⟩, is_open_sUnion := assume s h, begin simp only [classical.skolem] at h, cases h with f hf, apply exists.intro (⋃(x : set α) (h : x ∈ s), f x h), simp only [sUnion_eq_bUnion, preimage_Union, (λx h, (hf x h).right)], refine ⟨_, rfl⟩, exact (@is_open_Union β _ t _ $ assume i, show is_open (⋃h, f i h), from @is_open_Union β _ t _ $ assume h, (hf i h).left) end } lemma is_open_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_open α (t.induced f) s ↔ (∃t, is_open t ∧ f ⁻¹' t = s) := iff.refl _ lemma is_closed_induced_iff [t : topological_space β] {s : set α} {f : α → β} : @is_closed α (t.induced f) s ↔ (∃t, is_closed t ∧ s = f ⁻¹' t) := ⟨assume ⟨t, ht, heq⟩, ⟨-t, is_closed_compl_iff.2 ht, by simp only [preimage_compl, heq, lattice.neg_neg]⟩, assume ⟨t, ht, heq⟩, ⟨-t, ht, by simp only [preimage_compl, heq.symm]⟩⟩ /-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that makes `f` continuous. -/ def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) : topological_space β := { is_open := λs, t.is_open (f ⁻¹' s), is_open_univ := by rw preimage_univ; exact t.is_open_univ, is_open_inter := assume s₁ s₂ h₁ h₂, by rw preimage_inter; exact t.is_open_inter _ _ h₁ h₂, is_open_sUnion := assume s h, by rw [preimage_sUnion]; exact (@is_open_Union _ _ t _ $ assume i, show is_open (⋃ (H : i ∈ s), f ⁻¹' i), from @is_open_Union _ _ t _ $ assume hi, h i hi) } lemma is_open_coinduced {t : topological_space α} {s : set β} {f : α → β} : @is_open β (topological_space.coinduced f t) s ↔ is_open (f ⁻¹' s) := iff.refl _ variables {t t₁ t₂ : topological_space α} {t' : topological_space β} {f : α → β} {g : β → α} lemma coinduced_le_iff_le_induced {f : α → β } {tα : topological_space α} {tβ : topological_space β} : tα.coinduced f ≤ tβ ↔ tα ≤ tβ.induced f := iff.intro (assume h s ⟨t, ht, hst⟩, hst ▸ h _ ht) (assume h s hs, show tα.is_open (f ⁻¹' s), from h _ ⟨s, hs, rfl⟩) lemma gc_coinduced_induced (f : α → β) : galois_connection (topological_space.coinduced f) (topological_space.induced f) := assume f g, coinduced_le_iff_le_induced lemma induced_mono (h : t₁ ≤ t₂) : t₁.induced g ≤ t₂.induced g := (gc_coinduced_induced g).monotone_u h lemma coinduced_mono (h : t₁ ≤ t₂) : t₁.coinduced f ≤ t₂.coinduced f := (gc_coinduced_induced f).monotone_l h @[simp] lemma induced_top : (⊤ : topological_space α).induced g = ⊤ := (gc_coinduced_induced g).u_top @[simp] lemma induced_inf : (t₁ ⊓ t₂).induced g = t₁.induced g ⊓ t₂.induced g := (gc_coinduced_induced g).u_inf @[simp] lemma induced_infi {ι : Sort w} {t : ι → topological_space α} : (⨅i, t i).induced g = (⨅i, (t i).induced g) := (gc_coinduced_induced g).u_infi @[simp] lemma coinduced_bot : (⊥ : topological_space α).coinduced f = ⊥ := (gc_coinduced_induced f).l_bot @[simp] lemma coinduced_sup : (t₁ ⊔ t₂).coinduced f = t₁.coinduced f ⊔ t₂.coinduced f := (gc_coinduced_induced f).l_sup @[simp] lemma coinduced_supr {ι : Sort w} {t : ι → topological_space α} : (⨆i, t i).coinduced f = (⨆i, (t i).coinduced f) := (gc_coinduced_induced f).l_supr lemma induced_id [t : topological_space α] : t.induced id = t := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', hs, h⟩, h ▸ 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₁ ▸ h₂ ▸ ⟨s, hs, rfl⟩, assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩ 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 end galois_connection /- constructions using the complete lattice structure -/ section constructions open topological_space variables {α : Type u} {β : Type v} instance inhabited_topological_space {α : Type u} : inhabited (topological_space α) := ⟨⊤⟩ instance : topological_space empty := ⊥ instance : discrete_topology empty := ⟨rfl⟩ instance : topological_space unit := ⊥ instance : discrete_topology unit := ⟨rfl⟩ instance : topological_space bool := ⊥ instance : discrete_topology bool := ⟨rfl⟩ instance : topological_space ℕ := ⊥ instance : discrete_topology ℕ := ⟨rfl⟩ instance : topological_space ℤ := ⊥ instance : discrete_topology ℤ := ⟨rfl⟩ instance sierpinski_space : topological_space Prop := generate_from {{true}} instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := induced subtype.val t instance {r : α → α → Prop} [t : topological_space α] : topological_space (quot r) := coinduced (quot.mk r) t instance {s : setoid α} [t : topological_space α] : topological_space (quotient s) := coinduced quotient.mk t instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := induced prod.fst t₁ ⊓ induced prod.snd t₂ instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := coinduced sum.inl t₁ ⊔ coinduced sum.inr t₂ instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) := ⨆a, coinduced (sigma.mk a) (t₂ a) instance Pi.topological_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) := ⨅a, induced (λf, f a) (t₂ a) instance [topological_space α] : topological_space (list α) := topological_space.mk_of_nhds (traverse nhds) lemma nhds_list [topological_space α] (as : list α) : nhds as = traverse nhds as := begin refine nhds_mk_of_nhds _ _ _ _, { assume l, induction l, case list.nil { exact le_refl _ }, case list.cons : a l ih { suffices : list.cons <$> pure a <*> pure l ≤ list.cons <$> nhds a <*> traverse nhds l, { simpa only [-filter.pure_def] with functor_norm using this }, exact filter.seq_mono (filter.map_mono $ pure_le_nhds a) ih } }, { assume l s hs, rcases (mem_traverse_sets_iff _ _).1 hs with ⟨u, hu, hus⟩, clear as hs, have : ∃v:list (set α), l.forall₂ (λa s, is_open s ∧ a ∈ s) v ∧ sequence v ⊆ s, { induction hu generalizing s, case list.forall₂.nil : hs this { existsi [], simpa only [list.forall₂_nil_left_iff, exists_eq_left] }, case list.forall₂.cons : a s as ss ht h ih t hts { rcases mem_nhds_sets_iff.1 ht with ⟨u, hut, hu⟩, rcases ih (subset.refl _) with ⟨v, hv, hvss⟩, exact ⟨u::v, list.forall₂.cons hu hv, subset.trans (set.seq_mono (set.image_subset _ hut) hvss) hts⟩ } }, rcases this with ⟨v, hv, hvs⟩, refine ⟨sequence v, mem_traverse_sets _ _ _, hvs, _⟩, { exact hv.imp (assume a s ⟨hs, ha⟩, mem_nhds_sets hs ha) }, { assume u hu, have hu := (list.mem_traverse _ _).1 hu, have : list.forall₂ (λa s, is_open s ∧ a ∈ s) u v, { refine list.forall₂.flip _, replace hv := hv.flip, simp only [list.forall₂_and_left, flip] at ⊢ hv, exact ⟨hv.1, hu.flip⟩ }, refine mem_sets_of_superset _ hvs, exact mem_traverse_sets _ _ (this.imp $ assume a s ⟨hs, ha⟩, mem_nhds_sets hs ha) } } end lemma nhds_nil [topological_space α] : nhds ([] : list α) = pure [] := by rw [nhds_list, list.traverse_nil _]; apply_instance lemma nhds_cons [topological_space α] (a : α) (l : list α) : nhds (a :: l) = list.cons <$> nhds a <*> nhds l := by rw [nhds_list, list.traverse_cons _, ← nhds_list]; apply_instance lemma quotient_dense_of_dense [setoid α] [topological_space α] {s : set α} (H : ∀ x, x ∈ closure s) : closure (quotient.mk '' s) = univ := eq_univ_of_forall $ λ x, begin rw mem_closure_iff, intros U U_op x_in_U, let V := quotient.mk ⁻¹' U, cases quotient.exists_rep x with y y_x, have y_in_V : y ∈ V, by simp only [mem_preimage_eq, y_x, x_in_U], have V_op : is_open V := U_op, have : V ∩ s ≠ ∅ := mem_closure_iff.1 (H y) V V_op y_in_V, rcases exists_mem_of_ne_empty this with ⟨w, w_in_V, w_in_range⟩, exact ne_empty_of_mem ⟨w_in_V, mem_image_of_mem quotient.mk w_in_range⟩ end lemma le_generate_from {t : topological_space α} { g : set (set α) } (h : ∀s∈g, is_open s) : t ≤ generate_from g := le_generate_from_iff_subset_is_open.2 h lemma induced_generate_from_eq {α β} {b : set (set β)} {f : α → β} : (generate_from b).induced f = topological_space.generate_from (preimage f '' b) := le_antisymm (le_generate_from $ ball_image_iff.2 $ assume s hs, ⟨s, generate_open.basic _ hs, rfl⟩) (coinduced_le_iff_le_induced.1 $ le_generate_from $ assume s hs, generate_open.basic _ $ mem_image_of_mem _ hs) protected def topological_space.nhds_adjoint (a : α) (f : filter α) : topological_space α := { is_open := λs, a ∈ s → s ∈ f, is_open_univ := assume s, univ_mem_sets, is_open_inter := assume s t hs ht ⟨has, hat⟩, inter_mem_sets (hs has) (ht hat), is_open_sUnion := assume k hk ⟨u, hu, hau⟩, mem_sets_of_superset (hk u hu hau) (subset_sUnion_of_mem hu) } lemma gc_nhds (a : α) : galois_connection (topological_space.nhds_adjoint a) (λt, @nhds α t a):= assume f t, by { rw le_nhds_iff, exact ⟨λ H s hs has, H _ has hs, λ H s has hs, H _ hs has⟩ } lemma nhds_mono {t₁ t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : @nhds α t₁ a ≤ @nhds α t₂ a := (gc_nhds a).monotone_u h lemma nhds_infi {ι : Sort*} {t : ι → topological_space α} {a : α} : @nhds α (infi t) a = (⨅i, @nhds α (t i) a) := (gc_nhds a).u_infi lemma nhds_Inf {s : set (topological_space α)} {a : α} : @nhds α (Inf s) a = (⨅t∈s, @nhds α t a) := (gc_nhds a).u_Inf lemma nhds_inf {t₁ t₂ : topological_space α} {a : α} : @nhds α (t₁ ⊓ t₂) a = @nhds α t₁ a ⊓ @nhds α t₂ a := (gc_nhds a).u_inf lemma nhds_top {a : α} : @nhds α ⊤ a = ⊤ := (gc_nhds a).u_top instance {p : α → Prop} [topological_space α] [discrete_topology α] : discrete_topology (subtype p) := ⟨bot_unique $ assume s hs, ⟨subtype.val '' s, is_open_discrete _, (set.preimage_image_eq _ subtype.val_injective)⟩⟩ instance sum.discrete_topology [topological_space α] [topological_space β] [hα : discrete_topology α] [hβ : discrete_topology β] : discrete_topology (α ⊕ β) := ⟨by unfold sum.topological_space; simp [hα.eq_bot, hβ.eq_bot]⟩ instance sigma.discrete_topology {β : α → Type v} [Πa, topological_space (β a)] [h : Πa, discrete_topology (β a)] : discrete_topology (sigma β) := ⟨by { unfold sigma.topological_space, simp [λ a, (h a).eq_bot] }⟩ local notation `cont` := @continuous _ _ local notation `tspace` := topological_space open topological_space variables {γ : Type*} {f : α → β} {ι : Sort*} lemma continuous_iff_coinduced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ coinduced f t₁ ≤ t₂ := iff.rfl lemma continuous_iff_le_induced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ t₁ ≤ induced f t₂ := iff.trans continuous_iff_coinduced_le (gc_coinduced_induced f _ _) theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (f ⁻¹' s)) : cont t (generate_from b) f := continuous_iff_coinduced_le.2 $ le_generate_from h lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f := assume s h, ⟨_, h, rfl⟩ lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ} (h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g := assume s ⟨t, ht, s_eq⟩, s_eq ▸ 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_sup_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_sup_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₂ f → cont t₁ (t₂ ⊔ t₃) f := continuous_le_rng le_sup_left lemma continuous_sup_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} : cont t₁ t₃ f → cont t₁ (t₂ ⊔ t₃) f := continuous_le_rng le_sup_right lemma continuous_Sup_dom {t₁ : set (tspace α)} {t₂ : tspace β} (h : ∀t∈t₁, cont t t₂ f) : cont (Sup t₁) t₂ f := continuous_iff_le_induced.2 $ Sup_le $ assume t ht, continuous_iff_le_induced.1 $ h t ht lemma continuous_Sup_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Sup t₂) f := continuous_iff_coinduced_le.2 $ le_Sup_of_le h₁ $ continuous_iff_coinduced_le.1 hf lemma continuous_supr_dom {t₁ : ι → tspace α} {t₂ : tspace β} (h : ∀i, cont (t₁ i) t₂ f) : cont (supr t₁) t₂ f := continuous_Sup_dom $ assume t ⟨i, (t_eq : t₁ i = t)⟩, t_eq ▸ h i lemma continuous_supr_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι} (h : cont t₁ (t₂ i) f) : cont t₁ (supr t₂) f := continuous_Sup_rng ⟨i, rfl⟩ h lemma continuous_inf_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊓ t₃) f := continuous_iff_coinduced_le.2 $ le_inf (continuous_iff_coinduced_le.1 h₁) (continuous_iff_coinduced_le.1 h₂) lemma continuous_inf_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₁ t₃ f → cont (t₁ ⊓ t₂) t₃ f := continuous_le_dom inf_le_left lemma continuous_inf_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} : cont t₂ t₃ f → cont (t₁ ⊓ t₂) t₃ f := continuous_le_dom inf_le_right lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} {t : tspace α} (h₁ : t ∈ t₁) : cont t t₂ f → cont (Inf t₁) t₂ f := continuous_le_dom $ Inf_le h₁ lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)} (h : ∀t∈t₂, cont t₁ t f) : cont t₁ (Inf t₂) f := continuous_iff_coinduced_le.2 $ le_Inf $ assume b hb, continuous_iff_coinduced_le.1 $ h b hb lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} {i : ι} : cont (t₁ i) t₂ f → cont (infi t₁) t₂ f := continuous_le_dom $ infi_le _ _ lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β} (h : ∀i, cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f := continuous_iff_coinduced_le.2 $ le_infi $ assume i, continuous_iff_coinduced_le.1 $ h i lemma continuous_bot {t : tspace β} : cont ⊥ t f := continuous_iff_le_induced.2 $ bot_le lemma continuous_top {t : tspace α} : cont t ⊤ f := continuous_iff_coinduced_le.2 $ le_top /- nhds in the induced topology -/ theorem mem_nhds_induced [T : topological_space α] (f : β → α) (a : β) (s : set β) : s ∈ @nhds β (topological_space.induced f T) a ↔ ∃ u ∈ nhds (f a), f ⁻¹' u ⊆ s := begin simp only [nhds_sets, is_open_induced_iff, exists_prop, set.mem_set_of_eq], split, { rintros ⟨u, usub, ⟨v, openv, ueq⟩, au⟩, exact ⟨v, ⟨v, set.subset.refl v, openv, by rwa ←ueq at au⟩, by rw ueq; exact usub⟩ }, rintros ⟨u, ⟨v, vsubu, openv, amem⟩, finvsub⟩, exact ⟨f ⁻¹' v, set.subset.trans (set.preimage_mono vsubu) finvsub, ⟨⟨v, openv, rfl⟩, amem⟩⟩ end theorem nhds_induced [T : topological_space α] (f : β → α) (a : β) : @nhds β (topological_space.induced f T) a = comap f (nhds (f a)) := filter_eq $ by ext s; rw mem_nhds_induced; rw mem_comap_sets theorem map_nhds_induced_of_surjective [T : topological_space α] {f : β → α} (hf : function.surjective f) (a : β) (s : set α) : map f (@nhds β (topological_space.induced f T) a) = nhds (f a) := by rw [nhds_induced, map_comap_of_surjective hf] section topα variable [topological_space α] /- The nhds filter and the subspace topology. -/ theorem mem_nhds_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : t ∈ nhds a ↔ ∃ u ∈ nhds a.val, (@subtype.val α s) ⁻¹' u ⊆ t := by rw mem_nhds_induced theorem nhds_subtype (s : set α) (a : {x // x ∈ s}) : nhds a = comap subtype.val (nhds a.val) := by rw nhds_induced theorem principal_subtype (s : set α) (t : set {x // x ∈ s}) : principal t = comap subtype.val (principal (subtype.val '' t)) := by rw comap_principal; rw set.preimage_image_eq; apply subtype.val_injective /- nhds_within and subtypes -/ theorem mem_nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t u : set {x // x ∈ s}) : t ∈ nhds_within a u ↔ t ∈ comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' u)) := by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within] theorem nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : nhds_within a t = comap (@subtype.val _ s) (nhds_within a.val (subtype.val '' t)) := filter_eq $ by ext u; rw mem_nhds_within_subtype theorem nhds_within_eq_map_subtype_val {s : set α} {a : α} (h : a ∈ s) : nhds_within a s = map subtype.val (nhds ⟨a, h⟩) := have h₀ : s ∈ nhds_within a s, by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] }, have h₁ : ∀ y ∈ s, ∃ x, @subtype.val _ s x = y, from λ y h, ⟨⟨y, h⟩, rfl⟩, begin rw [←nhds_within_univ, nhds_within_subtype, subtype.val_image_univ], exact (map_comap_of_surjective' h₀ h₁).symm, end theorem tendsto_nhds_within_iff_subtype {s : set α} {a : α} (h : a ∈ s) (f : α → β) (l : filter β) : tendsto f (nhds_within a s) l ↔ tendsto (function.restrict f s) (nhds ⟨a, h⟩) l := by rw [tendsto, tendsto, function.restrict, nhds_within_eq_map_subtype_val h, ←(@filter.map_map _ _ _ _ subtype.val)] variables [tspace β] [tspace γ] theorem continuous_within_at_univ (f : α → β) (x : α) : continuous_within_at f set.univ x ↔ continuous_at f x := by rw [continuous_at, continuous_within_at, nhds_within_univ] theorem continuous_within_at_iff_continuous_at_restrict (f : α → β) {x : α} {s : set α} (h : x ∈ s) : continuous_within_at f s x ↔ continuous_at (function.restrict f s) ⟨x, h⟩ := tendsto_nhds_within_iff_subtype h f _ theorem continuous_within_at.tendsto_nhds_within_image {f : α → β} {x : α} {s : set α} (h : continuous_within_at f s x) : tendsto f (nhds_within x s) (nhds_within (f x) (f '' s)) := tendsto_inf.2 ⟨h, tendsto_principal.2 $ mem_inf_sets_of_right $ mem_principal_sets.2 $ λ x, mem_image_of_mem _⟩ theorem continuous_on_iff {f : α → β} {s : set α} : continuous_on f s ↔ ∀ x ∈ s, ∀ t : set β, is_open t → f x ∈ t → ∃ u, is_open u ∧ x ∈ u ∧ u ∩ s ⊆ f ⁻¹' t := by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within] theorem continuous_on_iff_continuous_restrict {f : α → β} {s : set α} : continuous_on f s ↔ continuous (function.restrict f s) := begin rw [continuous_on, continuous_iff_continuous_at], split, { rintros h ⟨x, xs⟩, exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) }, intros h x xs, exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩) end theorem continuous_on_iff' {f : α → β} {s : set α} : continuous_on f s ↔ ∀ t : set β, is_open t → ∃ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s := have ∀ t, is_open (function.restrict f s ⁻¹' t) ↔ ∃ (u : set α), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_open_induced_iff, function.restrict_eq, set.preimage_comp], simp only [subtype.preimage_val_eq_preimage_val_iff], split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ } end, by rw [continuous_on_iff_continuous_restrict, continuous]; simp only [this] theorem continuous_on_iff_is_closed {f : α → β} {s : set α} : continuous_on f s ↔ ∀ t : set β, is_closed t → ∃ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s := have ∀ t, is_closed (function.restrict f s ⁻¹' t) ↔ ∃ (u : set α), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s, begin intro t, rw [is_closed_induced_iff, function.restrict_eq, set.preimage_comp], simp only [subtype.preimage_val_eq_preimage_val_iff] end, by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this] theorem nhds_within_le_comap {x : α} {s : set α} {f : α → β} (ctsf : continuous_within_at f s x) : nhds_within x s ≤ comap f (nhds_within (f x) (f '' s)) := map_le_iff_le_comap.1 ctsf.tendsto_nhds_within_image theorem continuous_within_at_iff_ptendsto_res (f : α → β) {x : α} {s : set α} (xs : x ∈ s) : continuous_within_at f s x ↔ ptendsto (pfun.res f s) (nhds x) (nhds (f x)) := tendsto_iff_ptendsto _ _ _ _ def continuous_iff_continuous_on_univ {f : α → β} : continuous f ↔ continuous_on f univ := by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at, nhds_within_univ] lemma continuous_within_at.mono {f : α → β} {s t : set α} {x : α} (h : continuous_within_at f t x) (hs : s ⊆ t) : continuous_within_at f s x := tendsto_le_left (nhds_within_mono x hs) h lemma continuous_within_at_inter' {f : α → β} {s t : set α} {x : α} (h : t ∈ nhds_within x s) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict'' s h] lemma continuous_within_at_inter {f : α → β} {s t : set α} {x : α} (h : t ∈ nhds x) : continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x := by simp [continuous_within_at, nhds_within_restrict' s h] lemma continuous_on.congr_mono {f g : α → β} {s s₁ : set α} (h : continuous_on f s) (h' : ∀x ∈ s₁, g x = f x) (h₁ : s₁ ⊆ s) : continuous_on g s₁ := begin assume x hx, unfold continuous_within_at, have A := (h x (h₁ hx)).mono h₁, unfold continuous_within_at at A, rw ← h' x hx at A, have : {x : α | g x = f x} ∈ nhds_within x s₁ := mem_inf_sets_of_right h', apply tendsto.congr' _ A, convert this, ext, finish end lemma continuous_at.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous_at f x) : continuous_within_at f s x := continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _) lemma continuous_within_at.continuous_at {f : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) (hs : s ∈ nhds x) : continuous_at f x := begin have : s = univ ∩ s, by rw univ_inter, rwa [this, continuous_within_at_inter hs, continuous_within_at_univ] at h end lemma continuous_within_at.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α} (hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : f '' s ⊆ t) : continuous_within_at (g ∘ f) s x := begin have : tendsto f (principal s) (principal t), by { rw tendsto_principal_principal, exact λx hx, h (mem_image_of_mem _ hx) }, have : tendsto f (nhds_within x s) (principal t) := tendsto_le_left lattice.inf_le_right this, have : tendsto f (nhds_within x s) (nhds_within (f x) t) := tendsto_inf.2 ⟨hf, this⟩, exact tendsto.comp hg this end lemma continuous_at.comp {g : β → γ} {f : α → β} {x : α} (hg : continuous_at g (f x)) (hf : continuous_at f x) : continuous_at (g ∘ f) x := begin rw ← continuous_within_at_univ at *, exact continuous_within_at.comp hg hf (subset_univ _) end lemma continuous_on.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} (hg : continuous_on g t) (hf : continuous_on f s) (h : f '' s ⊆ t) : continuous_on (g ∘ f) s := λx hx, continuous_within_at.comp (hg _ (h (mem_image_of_mem _ hx))) (hf x hx) h lemma continuous_on.mono {f : α → β} {s t : set α} (hf : continuous_on f s) (h : t ⊆ s) : continuous_on f t := λx hx, tendsto_le_left (nhds_within_mono _ h) (hf x (h hx)) lemma continuous.continuous_on {f : α → β} {s : set α} (h : continuous f) : continuous_on f s := begin rw continuous_iff_continuous_on_univ at h, exact h.mono (subset_univ _) end lemma continuous.comp_continuous_on {g : β → γ} {f : α → β} {s : set α} (hg : continuous g) (hf : continuous_on f s) : continuous_on (g ∘ f) s := hg.continuous_on.comp hf (subset_univ _) lemma continuous.continuous_at {f : α → β} {x : α} (h : continuous f) : continuous_at f x := begin have := continuous_iff_continuous_on_univ.1 h x (mem_univ _), rwa continuous_within_at_univ at this, end lemma continuous_at.preimage_mem_nhds {f : α → β} {x : α} {t : set β} (h : continuous_at f x) (ht : t ∈ nhds (f x)) : f ⁻¹' t ∈ nhds x := h ht lemma continuous_within_at.preimage_mem_nhds_within {f : α → β} {x : α} {s : set α} {t : set β} (h : continuous_within_at f s x) (ht : t ∈ nhds (f x)) : f ⁻¹' t ∈ nhds_within x s := h ht lemma continuous_within_at.congr_of_mem_nhds_within {f f₁ : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) (h₁ : {y | f₁ y = f y} ∈ nhds_within x s) (hx : f₁ x = f x) : continuous_within_at f₁ s x := by rwa [continuous_within_at, filter.tendsto, hx, filter.map_cong h₁] lemma continuous_on_const {s : set α} {c : β} : continuous_on (λx, c) s := continuous_const.continuous_on lemma continuous_on_open_iff {f : α → β} {s : set α} (hs : is_open s) : continuous_on f s ↔ (∀t, _root_.is_open t → is_open (s ∩ f⁻¹' t)) := begin rw continuous_on_iff', split, { assume h t ht, rcases h t ht with ⟨u, u_open, hu⟩, rw [inter_comm, hu], apply is_open_inter u_open hs }, { assume h t ht, refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩, rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] } end lemma continuous_on.preimage_open_of_open {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) := (continuous_on_open_iff hs).1 hf t ht lemma continuous_on.preimage_closed_of_closed {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) := begin rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩, rw [inter_comm, hu.2], apply is_closed_inter hu.1 hs end lemma continuous_on.preimage_interior_subset_interior_preimage {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) ⊆ s ∩ interior (f⁻¹' t) := calc s ∩ f ⁻¹' (interior t) = interior (s ∩ f ⁻¹' (interior t)) : (interior_eq_of_open (hf.preimage_open_of_open hs is_open_interior)).symm ... ⊆ interior (s ∩ f ⁻¹' t) : interior_mono (inter_subset_inter (subset.refl _) (preimage_mono interior_subset)) ... = s ∩ interior (f ⁻¹' t) : by rw [interior_inter, interior_eq_of_open hs] lemma continuous_on_of_locally_continuous_on {f : α → β} {s : set α} (h : ∀x∈s, ∃t, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s := begin assume x xs, rcases h x xs with ⟨t, open_t, xt, ct⟩, have := ct x ⟨xs, xt⟩, rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this end lemma continuous_on_open_of_generate_from {β : Type*} {s : set α} {T : set (set β)} {f : α → β} (hs : is_open s) (h : ∀t ∈ T, is_open (s ∩ f⁻¹' t)) : @continuous_on α β _ (topological_space.generate_from T) f s := begin rw continuous_on_open_iff, assume t ht, induction ht with u hu u v Tu Tv hu hv U hU hU', { exact h u hu }, { simp only [preimage_univ, inter_univ], exact hs }, { have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v), by { ext x, simp, split, finish, finish }, rw this, exact is_open_inter hu hv }, { rw [preimage_sUnion, inter_bUnion], exact is_open_bUnion hU' }, { exact hs } end end topα end constructions section induced open topological_space variables {α : Type*} {β : Type*} variables [t : topological_space β] {f : α → β} theorem is_open_induced_eq {s : set α} : @_root_.is_open _ (induced f t) s ↔ s ∈ preimage f '' {s | is_open s} := iff.refl _ theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (f ⁻¹' s) := ⟨s, h, rfl⟩ lemma nhds_induced_eq_comap {a : α} : @nhds α (induced f t) a = comap f (nhds (f a)) := calc @nhds α (induced f t) a = (⨅ s (x : s ∈ preimage f '' set_of is_open ∧ a ∈ s), principal s) : by simp [nhds, is_open_induced_eq, -mem_image, and_comm] ... = (⨅ s (x : is_open s ∧ f a ∈ s), principal (f ⁻¹' s)) : by simp only [infi_and, infi_image]; refl ... = _ : by simp [nhds, comap_infi, and_comm] lemma map_nhds_induced_eq {a : α} (h : range f ∈ nhds (f a)) : map f (@nhds α (induced f t) a) = nhds (f a) := by rw [nhds_induced_eq_comap, filter.map_comap h] lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} (hf : ∀x y, f x = f y → x = y) : a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) := have comap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ comap_bot, assume h, forall_sets_neq_empty_iff_neq_bot.mp $ assume s₁ ⟨s₂, hs₂, (hs : f ⁻¹' s₂ ⊆ s₁)⟩, have f '' s ∈ nhds (f a) ⊓ principal (f '' s), from mem_inf_sets_of_right $ by simp [subset.refl], have s₂ ∩ f '' s ∈ nhds (f a) ⊓ principal (f '' s), from inter_mem_sets hs₂ this, let ⟨b, hb₁, ⟨a, ha, ha₂⟩⟩ := inhabited_of_mem_sets h this in ne_empty_of_mem $ hs $ by rwa [←ha₂] at hb₁⟩, calc a ∈ @closure α (topological_space.induced f t) s ↔ (@nhds α (topological_space.induced f t) a) ⊓ principal s ≠ ⊥ : by rw [closure_eq_nhds]; refl ... ↔ comap f (nhds (f a)) ⊓ principal (f ⁻¹' (f '' s)) ≠ ⊥ : by rw [nhds_induced_eq_comap, preimage_image_eq _ hf] ... ↔ comap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [comap_inf, ←comap_principal] ... ↔ _ : by rwa [closure_eq_nhds] end induced section sierpinski variables {α : Type*} [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 infi variables {α : Type u} {ι : Type v} {t : ι → topological_space α} lemma is_open_supr_iff {s : set α} : @is_open _ (⨆ i, t i) s ↔ ∀ i, @is_open _ (t i) s := begin -- s defines a map from α to Prop, which is continuous iff s is open. suffices : @continuous _ _ (⨆ i, t i) _ s ↔ ∀ i, @continuous _ _ (t i) _ s, { simpa only [continuous_Prop] using this }, simp only [continuous_iff_le_induced, supr_le_iff] end lemma is_closed_infi_iff {s : set α} : @is_closed _ (⨆ i, t i) s ↔ ∀ i, @is_closed _ (t i) s := is_open_supr_iff end infi
b63fbd16ad450543fab0f2676f328f5850950fbf
1fd908b06e3f9c1252cb2285ada1102623a67f72
/tests/induction.lean
4cf20d671e5395daa6414bb7359f67ca34b9444c
[ "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
5,960
lean
import ..init open expr tactic pexpr hott universes u v noncomputable theory axiom sorry' : Π{α : Sort _}, α /- no warnings are generated when using this axiom -/ @[induction] def foo2 {X Y : Type} {P : X × Y → Type} (x : X × Y) (z : Πa b, P ⟨a, b⟩) : P x := sorry' @[induction] def foo3 {P : ℕ × ℕ → Sort _} (x : ℕ × ℕ) (z : ℕ → P (0,0)) : P x := sorry' @[induction] def foo4 {X Y : Type} {P : Type} (x : X × Y) (z : X → Y → P) : P := sorry' @[induction] def foo5 {X : Type} {P : Type} (x : ℕ × X) (z : X → X → P) : P := sorry' @[induction] def foo6 {P : hott.trunc -2 nat → Type} (x : hott.trunc -2 nat) (z : Π(n : ℕ), P (hott.trunc.tr n)) : P x := sorry' @[induction] def foo7 {P : hott.trunc -2 nat → Type _} (x : hott.trunc -2 nat) (z : Πx, P x → P x) : (ℕ → P (hott.trunc.tr 0)) → P x := sorry' @[induction] def foo8 {P : hott.trunc -2 nat → Type _} (x : hott.trunc -2 nat) : P x := sorry' @[induction] def foo9 (n : ℕ₋₂) (A : Type _) {P : hott.trunc n A → Type _} (x : hott.trunc n A) : (Πx, P x) → P x := sorry' @[induction] def foo10 (n : ℕ₋₂) (A : Type _) {P : Type _} (x : hott.trunc n A) : (ℕ → P) → P := sorry' attribute [induction] trunc.rec attribute [induction] hott.quotient.rec attribute [induction, priority 2000] hott.trunc.rec attribute [induction] hott.eq.idp_rec_on attribute [induction] hott.eq.idp_rec_on attribute [induction] hott.eq.rec attribute [induction] prod.elim def foo11 {X : Type} {P : Type} (x : X) (z : X → X → P) : P := sorry' def foo12 {X : Type} {P : Type} (x : ℕ × X) (z : x = x → P) : P := sorry' def foo13 {X : Type} {P : Type} (x : ℕ) (z : P) : P := sorry' run_cmd success_if_fail $ get_induction_info `foo11 run_cmd success_if_fail $ get_induction_info `foo12 run_cmd success_if_fail $ get_induction_info `foo13 def indfoo {X Y : Type} (x : hott.trunc -2 nat) : hott.eq x x := begin hinduction x using foo7 with x' p n, all_goals { exact sorry' } end def indfoo2 (x : ℕ) (y : ℕ) : x = x := begin hinduction_only x with n IH, all_goals { exact sorry' } end def indfoo3 {X Y : Type} {P : bool × bool → Type} (x : bool × bool) (z : Πx, P x) : P x := begin hinduction_only x, exact sorry' end def indfoo4 {X Y : Type} {P : Type} (x : bool × bool) (z : P) : P := begin success_if_fail { hinduction x using foo5 }, hinduction x, exact sorry' end def indfoo5 {X Y : Type} {P : hott.trunc -2 nat → Type} (x : hott.trunc -2 nat) (z : Πx, P x) : P x := begin hinduction_only x, exact sorry' end def indfoo6 {X Y : Type} (x : hott.trunc -2 nat) : hott.eq x x := begin hinduction x using trunc.rec with x' p n, reflexivity end example {X Y : Type} (x : hott.trunc -2 nat) : hott.eq x x := begin hinduction x using foo8 with x' p n end example {X Y : Type} (x : hott.trunc -2 nat) : hott.eq x x := begin hinduction x using foo9 with x' p n, all_goals { exact sorry' } end example {X Y : Type} (x : hott.trunc -2 nat) : hott.eq x x := begin success_if_fail { hinduction x using foo10 with x' p n }, exact sorry' end example (x : ℕ) (y : ℕ) : x = x := begin hinduction_only x using nat.rec with n IH generalizing y, all_goals { exact sorry' } end example (x : ℕ) (y : ℕ) : x = x := begin revert y, hinduction_only x using nat.rec with n IH, all_goals { intro }, all_goals { exact sorry' } end example (x : ℕ) (p : x = 3) : x = x := begin hinduction_only x using nat.rec with n IH, all_goals { exact sorry' } end example (x : ℕ) : let y := x in Π(p : y = y), x = y := begin intros _ _, hinduction_only x using nat.rec with n IH generalizing y, all_goals { exact sorry' } end hott_theory @[induction] def eqrec1 {A : Type u} {a : A} {C : Π (a' : A), a = a' → Sort v} (H : C a (refl a)) {a' : A} (n : a = a') : C a' n := sorry' @[induction] def eqrec2 {A : Type u} {a : A} {C : a = a → Sort v} (H : C (refl a)) (n : a = a) : C n := sorry' @[induction] def eqrec3 {A : Type u} {C : Π (a' : A), a' = a' → Sort v} (H : Πa, C a (refl a)) {a : A} (n : a = a) : C a n := sorry' @[induction] def eqrec4 {A : Type u} {a : A} {C : A → Sort v} (H : C a) {a' : A} (n : a = a') : C a' := sorry' @[induction] def eqrec5 {A : Type u} {a : A} {C : Sort v} (H : C) {a' : A} (n : a = a') : C := sorry' attribute [induction] pathover.rec idp_rec_on -- #print eqrec1._ind_info -- #print eqrec2._ind_info -- #print eqrec3._ind_info -- #print eqrec4._ind_info -- #print eqrec5._ind_info open hott.trunc hott.is_trunc @[hott] def trunc_sigma_equiv {n : ℕ₋₂} {A : Type _} {P : A → Type _} : trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) := begin fapply equiv.MK; intro x, { hinduction x with p, exact tr ⟨p.1, tr p.2⟩ }, { hinduction x with p, induction p with a p, hinduction p with p, exact tr ⟨a, p⟩ }, all_goals { exact sorry' } end @[hott] def trunc_sigma_equiv2 {n : ℕ₋₂} {A : Type _} {P : A → Type _} : trunc n (Σ x, P x) ≃ trunc n (Σ x, trunc n (P x)) := begin fapply equiv.MK; intro x, { hinduction x with p, exact tr ⟨p.1, tr p.2⟩ }, { hinduction x with p, have x := p.2, hinduction x with q, exact tr ⟨p.1, q⟩ }, all_goals { exact sorry' } end def eqrecfail1 {A : Type u} {a : A} {C : Π (a' : A), a = a' → Sort v} (H : C a (refl a)) {a' : A} (n : a = a') : C (id a') n := sorry' def eqrecfail2 {A : Type u} {a : A} {C : Π (a' : A), a = a' → Sort v} (H : C a (refl a)) {a' : A} (n : a = a') : C a' (n ⬝ idp) := sorry' def eqrecfail3 {A : Type u} {a : A} {C : Π (a' : A), a = a' → Sort v} (H : C a (refl a)) {a' : A} (n : a = id a') : C a' n := sorry' run_cmd success_if_fail $ get_induction_info `eqrecfail1 run_cmd success_if_fail $ get_induction_info `eqrecfail2 run_cmd success_if_fail $ get_induction_info `eqrecfail3 --run_cmd success_if_fail $ get_induction_info `eqrecfail4 -- #print eqrec1._ind_info -- #print idp_rec_on._ind_info -- #print idp_rec_on
3409633ce19ca8a9f7d4b4369f36cf73a7531cbb
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/limits/shapes/finite_products.lean
b55b0a09c6bbf4383a0c9c560e85a43c3da26df1
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
2,512
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.finite_limits import category_theory.limits.shapes.binary_products import category_theory.limits.shapes.terminal universes v u open category_theory namespace category_theory.limits variables (C : Type u) [category.{v} C] /-- A category has finite products if there is a chosen limit for every diagram with shape `discrete J`, where we have `[decidable_eq J]` and `[fintype J]`. -/ -- We can't simply make this an abbreviation, as we do with other `has_Xs` limits typeclasses, -- because of https://github.com/leanprover-community/lean/issues/429 def has_finite_products : Prop := Π (J : Type v) [decidable_eq J] [fintype J], has_limits_of_shape (discrete J) C attribute [class] has_finite_products instance has_limits_of_shape_discrete (J : Type v) [fintype J] [has_finite_products C] : has_limits_of_shape (discrete J) C := by { classical, exact ‹has_finite_products C› J } /-- If `C` has finite limits then it has finite products. -/ lemma has_finite_products_of_has_finite_limits [has_finite_limits C] : has_finite_products C := λ J 𝒥₁ 𝒥₂, by { resetI, apply_instance } /-- If a category has all products then in particular it has finite products. -/ lemma has_finite_products_of_has_products [has_products C] : has_finite_products C := by { dsimp [has_finite_products], apply_instance } /-- A category has finite coproducts if there is a chosen colimit for every diagram with shape `discrete J`, where we have `[decidable_eq J]` and `[fintype J]`. -/ def has_finite_coproducts : Prop := Π (J : Type v) [decidable_eq J] [fintype J], has_colimits_of_shape (discrete J) C attribute [class] has_finite_coproducts instance has_colimits_of_shape_discrete (J : Type v) [fintype J] [has_finite_coproducts C] : has_colimits_of_shape (discrete J) C := by { classical, exact ‹has_finite_coproducts C› J } /-- If `C` has finite colimits then it has finite coproducts. -/ lemma has_finite_coproducts_of_has_finite_colimits [has_finite_colimits C] : has_finite_coproducts C := λ J 𝒥₁ 𝒥₂, by { resetI, apply_instance } /-- If a category has all coproducts then in particular it has finite coproducts. -/ lemma has_finite_coproducts_of_has_coproducts [has_coproducts C] : has_finite_coproducts C := by { dsimp [has_finite_coproducts], apply_instance } end category_theory.limits
d6b086b7fbd567544372d1c8183756c0b0c31f24
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/ParserCompiler/Attribute.lean
0fa91659db1981d07f9865ce692ff14ce7c85d9c
[ "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,845
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Attributes import Lean.Compiler.InitAttr import Lean.ToExpr namespace Lean namespace ParserCompiler structure CombinatorAttribute := (attr : AttributeImpl) (ext : SimplePersistentEnvExtension (Name × Name) (NameMap Name)) -- TODO(Sebastian): We'll probably want priority support here at some point def registerCombinatorAttribute (name : Name) (descr : String) : IO CombinatorAttribute := do ext : SimplePersistentEnvExtension (Name × Name) (NameMap Name) ← registerSimplePersistentEnvExtension { name := name, addImportedFn := mkStateFromImportedEntries (fun s p => s.insert p.1 p.2) {}, addEntryFn := fun (s : NameMap Name) (p : Name × Name) => s.insert p.1 p.2, }; let attrImpl : AttributeImpl := { name := name, descr := descr, add := fun decl args _ => do env ← getEnv; match attrParamSyntaxToIdentifier args with | some parserDeclName => do _ ← getConstInfo parserDeclName; setEnv $ ext.addEntry env (parserDeclName, decl) | none => throwError $ "invalid [" ++ name ++ "] argument, expected identifier" }; registerBuiltinAttribute attrImpl; pure { attr := attrImpl, ext := ext } namespace CombinatorAttribute instance : Inhabited CombinatorAttribute := ⟨{attr := arbitrary _, ext := arbitrary _}⟩ def getDeclFor (attr : CombinatorAttribute) (env : Environment) (parserDecl : Name) : Option Name := (attr.ext.getState env).find? parserDecl def setDeclFor (attr : CombinatorAttribute) (env : Environment) (parserDecl : Name) (decl : Name) : Environment := attr.ext.addEntry env (parserDecl, decl) end CombinatorAttribute end ParserCompiler end Lean
ba9abbbeb82c5d3a2f1e45e48c0da833028073fa
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Elab/PreDefinition/Structural/BRecOn.lean
dbd98408d8d80c2c863b049e55935558d932716b
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
cipher1024/lean4
6e1f98bb58e7a92b28f5364eb38a14c8d0aae393
69114d3b50806264ef35b57394391c3e738a9822
refs/heads/master
1,642,227,983,603
1,642,011,696,000
1,642,011,696,000
228,607,691
0
0
Apache-2.0
1,576,584,269,000
1,576,584,268,000
null
UTF-8
Lean
false
false
10,391
lean
/- Copyright (c) 2021 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Match.Match import Lean.Elab.RecAppSyntax import Lean.Elab.PreDefinition.Structural.Basic namespace Lean.Elab.Structural open Meta private def throwToBelowFailed : MetaM α := throwError "toBelow failed" /- See toBelow -/ private partial def toBelowAux (C : Expr) : Expr → Expr → Expr → MetaM Expr | belowDict, arg, F => do let belowDict ← whnf belowDict trace[Elab.definition.structural] "belowDict: {belowDict}, arg: {arg}" match belowDict with | Expr.app (Expr.app (Expr.const `PProd _ _) d1 _) d2 _ => (do toBelowAux C d1 arg (← mkAppM `PProd.fst #[F])) <|> (do toBelowAux C d2 arg (← mkAppM `PProd.snd #[F])) | Expr.app (Expr.app (Expr.const `And _ _) d1 _) d2 _ => (do toBelowAux C d1 arg (← mkAppM `And.left #[F])) <|> (do toBelowAux C d2 arg (← mkAppM `And.right #[F])) | _ => forallTelescopeReducing belowDict fun xs belowDict => do let argArgs := arg.getAppArgs unless argArgs.size >= xs.size do throwToBelowFailed let n := argArgs.size let argTailArgs := argArgs.extract (n - xs.size) n let belowDict := belowDict.replaceFVars xs argTailArgs match belowDict with | Expr.app belowDictFun belowDictArg _ => unless belowDictFun.getAppFn == C do throwToBelowFailed unless ← isDefEq belowDictArg arg do throwToBelowFailed pure (mkAppN F argTailArgs) | _ => throwToBelowFailed /- See toBelow -/ private def withBelowDict (below : Expr) (numIndParams : Nat) (k : Expr → Expr → MetaM α) : MetaM α := do let belowType ← inferType below trace[Elab.definition.structural] "belowType: {belowType}" belowType.withApp fun f args => do let motivePos := numIndParams + 1 unless motivePos < args.size do throwError "unexpected 'below' type{indentExpr belowType}" let pre := mkAppN f (args.extract 0 numIndParams) let preType ← inferType pre forallBoundedTelescope preType (some 1) fun x _ => do let motiveType ← inferType x[0] withLocalDeclD (← mkFreshUserName `C) motiveType fun C => let belowDict := mkApp pre C let belowDict := mkAppN belowDict (args.extract (numIndParams + 1) args.size) k C belowDict /- `below` is a free variable with type of the form `I.below indParams motive indices major`, where `I` is the name of an inductive datatype. For example, when trying to show that the following function terminates using structural recursion ```lean def addAdjacent : List Nat → List Nat | [] => [] | [a] => [a] | a::b::as => (a+b) :: addAdjacent as ``` when we are visiting `addAdjacent as` at `replaceRecApps`, `below` has type `@List.below Nat (fun (x : List Nat) => List Nat) (a::b::as)` The motive `fun (x : List Nat) => List Nat` depends on the actual function we are trying to compute. So, we first replace it with a fresh variable `C` at `withBelowDict`. Recall that `brecOn` implements course-of-values recursion, and `below` can be viewed as a dictionary of the "previous values". We search this dictionary using the auxiliary function `toBelowAux`. The dictionary is built using the `PProd` (`And` for inductive predicates). We keep searching it until we find `C recArg`, where `C` is the auxiliary fresh variable created at `withBelowDict`. -/ private partial def toBelow (below : Expr) (numIndParams : Nat) (recArg : Expr) : MetaM Expr := do withBelowDict below numIndParams fun C belowDict => toBelowAux C belowDict recArg below private partial def replaceRecApps (recFnName : Name) (recArgInfo : RecArgInfo) (below : Expr) (e : Expr) : M Expr := let rec loop (below : Expr) (e : Expr) : M Expr := do match e with | Expr.lam n d b c => withLocalDecl n c.binderInfo (← loop below d) fun x => do mkLambdaFVars #[x] (← loop below (b.instantiate1 x)) | Expr.forallE n d b c => withLocalDecl n c.binderInfo (← loop below d) fun x => do mkForallFVars #[x] (← loop below (b.instantiate1 x)) | Expr.letE n type val body _ => withLetDecl n (← loop below type) (← loop below val) fun x => do mkLetFVars #[x] (← loop below (body.instantiate1 x)) (usedLetOnly := false) | Expr.mdata d b _ => if let some stx := getRecAppSyntax? e then loop below b else return mkMData d (← loop below b) | Expr.proj n i e _ => return mkProj n i (← loop below e) | Expr.app _ _ _ => let processApp (e : Expr) : M Expr := e.withApp fun f args => do if f.isConstOf recFnName then let numFixed := recArgInfo.fixedParams.size let recArgPos := recArgInfo.fixedParams.size + recArgInfo.pos if recArgPos >= args.size then throwError "insufficient number of parameters at recursive application {indentExpr e}" let recArg := args[recArgPos] -- For reflexive type, we may have nested recursive applications in recArg let recArg ← loop below recArg let f ← try toBelow below recArgInfo.indParams.size recArg catch _ => throwError "failed to eliminate recursive application{indentExpr e}" -- Recall that the fixed parameters are not in the scope of the `brecOn`. So, we skip them. let argsNonFixed := args.extract numFixed args.size -- The function `f` does not explicitly take `recArg` and its indices as arguments. So, we skip them too. let mut fArgs := #[] for i in [:argsNonFixed.size] do if recArgInfo.pos != i && !recArgInfo.indicesPos.contains i then let arg := argsNonFixed[i] let arg ← replaceRecApps recFnName recArgInfo below arg fArgs := fArgs.push arg return mkAppN f fArgs else return mkAppN (← loop below f) (← args.mapM (loop below)) let matcherApp? ← matchMatcherApp? e match matcherApp? with | some matcherApp => if !recArgHasLooseBVarsAt recFnName recArgInfo.recArgPos e then processApp e else /- Here is an example we currently not handle ``` def g (xs : List Nat) : Nat := match xs with | [] => 0 | y::ys => match ys with | [] => 1 | _::_::zs => g zs + 1 | zs => g ys + 2 ``` We are matching on `ys`, but still using `ys` in the third alternative. If we push the `below` argument over the dependent match it will be able to eliminate recursive call using `zs`. To make it work, users have to write the third alternative as `| zs => g zs + 2` If this is too annoying in practice, we may replace `ys` with the matching term, but this may generate weird error messages, when it doesn't work. -/ trace[Elab.definition.structural] "below before matcherApp.addArg: {below} : {← inferType below}" let matcherApp ← mapError (matcherApp.addArg below) (fun msg => "failed to add `below` argument to 'matcher' application" ++ indentD msg) let altsNew ← (Array.zip matcherApp.alts matcherApp.altNumParams).mapM fun (alt, numParams) => lambdaTelescope alt fun xs altBody => do trace[Elab.definition.structural] "altNumParams: {numParams}, xs: {xs}" unless xs.size >= numParams do throwError "unexpected matcher application alternative{indentExpr alt}\nat application{indentExpr e}" let belowForAlt := xs[numParams - 1] mkLambdaFVars xs (← loop belowForAlt altBody) pure { matcherApp with alts := altsNew }.toExpr | none => processApp e | e => ensureNoRecFn recFnName e loop below e def mkBRecOn (recFnName : Name) (recArgInfo : RecArgInfo) (value : Expr) : M Expr := do trace[Elab.definition.structural] "mkBRecOn: {value}" let type := (← inferType value).headBeta let major := recArgInfo.ys[recArgInfo.pos] let otherArgs := recArgInfo.ys.filter fun y => y != major && !recArgInfo.indIndices.contains y trace[Elab.definition.structural] "fixedParams: {recArgInfo.fixedParams}, otherArgs: {otherArgs}" let motive ← mkForallFVars otherArgs type let mut brecOnUniv ← getLevel motive trace[Elab.definition.structural] "brecOn univ: {brecOnUniv}" let useBInductionOn := recArgInfo.reflexive && brecOnUniv == levelZero if recArgInfo.reflexive && brecOnUniv != levelZero then brecOnUniv ← decLevel brecOnUniv let motive ← mkLambdaFVars (recArgInfo.indIndices.push major) motive trace[Elab.definition.structural] "brecOn motive: {motive}" let brecOn := if useBInductionOn then Lean.mkConst (mkBInductionOnName recArgInfo.indName) recArgInfo.indLevels else Lean.mkConst (mkBRecOnName recArgInfo.indName) (brecOnUniv :: recArgInfo.indLevels) let brecOn := mkAppN brecOn recArgInfo.indParams let brecOn := mkApp brecOn motive let brecOn := mkAppN brecOn recArgInfo.indIndices let brecOn := mkApp brecOn major check brecOn let brecOnType ← inferType brecOn trace[Elab.definition.structural] "brecOn {brecOn}" trace[Elab.definition.structural] "brecOnType {brecOnType}" forallBoundedTelescope brecOnType (some 1) fun F _ => do let F := F[0] let FType ← inferType F trace[Elab.definition.structural] "FType: {FType}" let FType ← instantiateForall FType recArgInfo.indIndices let FType ← instantiateForall FType #[major] forallBoundedTelescope FType (some 1) fun below _ => do -- TODO: `below` user name is `f`, and it will make a global `f` to be pretty printed as `_root_.f` in error messages. -- We should add an option to `forallBoundedTelescope` to ensure fresh names are used. let below := below[0] let valueNew ← replaceRecApps recFnName recArgInfo below value let Farg ← mkLambdaFVars (recArgInfo.indIndices ++ #[major, below] ++ otherArgs) valueNew let brecOn := mkApp brecOn Farg return mkAppN brecOn otherArgs end Lean.Elab.Structural
a799213a63efb9af5345b6860046783088ca75c7
38bf3fd2bb651ab70511408fcf70e2029e2ba310
/src/algebra/big_operators.lean
09a73eaded091647b608ba1674ac45dd8fee7142
[ "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
30,851
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 Some big operators for lists and finite sets. -/ import tactic.tauto data.list.basic data.finset data.nat.enat import algebra.group algebra.ordered_group algebra.group_power universes u v w variables {α : Type u} {β : Type v} {γ : Type w} theorem directed.finset_le {r : α → α → Prop} [is_trans α r] {ι} (hι : nonempty ι) {f : ι → α} (D : directed r f) (s : finset ι) : ∃ z, ∀ i ∈ s, r (f i) (f z) := show ∃ z, ∀ i ∈ s.1, r (f i) (f z), from multiset.induction_on s.1 (let ⟨z⟩ := hι in ⟨z, λ _, false.elim⟩) $ λ i s ⟨j, H⟩, let ⟨k, h₁, h₂⟩ := D i j in ⟨k, λ a h, or.cases_on (multiset.mem_cons.1 h) (λ h, h.symm ▸ h₁) (λ h, trans (H _ h) h₂)⟩ theorem finset.exists_le {α : Type u} [nonempty α] [directed_order α] (s : finset α) : ∃ M, ∀ i ∈ s, i ≤ M := directed.finset_le (by apply_instance) directed_order.directed s namespace finset variables {s s₁ s₂ : finset α} {a : α} {f g : α → β} /-- `prod s f` is the product of `f x` as `x` ranges over the elements of the finite set `s`. -/ @[to_additive] protected def prod [comm_monoid β] (s : finset α) (f : α → β) : β := (s.1.map f).prod @[to_additive] theorem prod_eq_fold [comm_monoid β] (s : finset α) (f : α → β) : s.prod f = s.fold (*) 1 f := rfl section comm_monoid variables [comm_monoid β] @[simp, to_additive] lemma prod_empty {α : Type u} {f : α → β} : (∅:finset α).prod f = 1 := rfl @[simp, to_additive] lemma prod_insert [decidable_eq α] : a ∉ s → (insert a s).prod f = f a * s.prod f := fold_insert @[simp, to_additive] lemma prod_singleton : (singleton a).prod f = f a := eq.trans fold_singleton $ mul_one _ @[simp] lemma prod_const_one : s.prod (λx, (1 : β)) = 1 := by simp only [finset.prod, multiset.map_const, multiset.prod_repeat, one_pow] @[simp] lemma sum_const_zero {β} {s : finset α} [add_comm_monoid β] : s.sum (λx, (0 : β)) = 0 := @prod_const_one _ (multiplicative β) _ _ attribute [to_additive] prod_const_one @[simp, to_additive] lemma prod_image [decidable_eq α] {s : finset γ} {g : γ → α} : (∀x∈s, ∀y∈s, g x = g y → x = y) → (s.image g).prod f = s.prod (λx, f (g x)) := fold_image @[simp, to_additive] lemma prod_map (s : finset α) (e : α ↪ γ) (f : γ → β) : (s.map e).prod f = s.prod (λa, f (e a)) := by rw [finset.prod, finset.map_val, multiset.map_map]; refl @[congr, to_additive] lemma prod_congr (h : s₁ = s₂) : (∀x∈s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr attribute [congr] finset.sum_congr @[to_additive] lemma prod_union_inter [decidable_eq α] : (s₁ ∪ s₂).prod f * (s₁ ∩ s₂).prod f = s₁.prod f * s₂.prod f := fold_union_inter @[to_additive] lemma prod_union [decidable_eq α] (h : disjoint s₁ s₂) : (s₁ ∪ s₂).prod f = s₁.prod f * s₂.prod f := by rw [←prod_union_inter, (disjoint_iff_inter_eq_empty.mp h)]; exact (mul_one _).symm @[to_additive] lemma prod_sdiff [decidable_eq α] (h : s₁ ⊆ s₂) : (s₂ \ s₁).prod f * s₁.prod f = s₂.prod f := by rw [←prod_union sdiff_disjoint, sdiff_union_of_subset h] @[to_additive] lemma prod_bind [decidable_eq α] {s : finset γ} {t : γ → finset α} : (∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y)) → (s.bind t).prod f = s.prod (λx, (t x).prod f) := by haveI := classical.dec_eq γ; exact finset.induction_on s (λ _, by simp only [bind_empty, prod_empty]) (assume x s hxs ih hd, have hd' : ∀x∈s, ∀y∈s, x ≠ y → disjoint (t x) (t y), from assume _ hx _ hy, hd _ (mem_insert_of_mem hx) _ (mem_insert_of_mem hy), have ∀y∈s, x ≠ y, from assume _ hy h, by rw [←h] at hy; contradiction, have ∀y∈s, disjoint (t x) (t y), from assume _ hy, hd _ (mem_insert_self _ _) _ (mem_insert_of_mem hy) (this _ hy), have disjoint (t x) (finset.bind s t), from (disjoint_bind_right _ _ _).mpr this, by simp only [bind_insert, prod_insert hxs, prod_union this, ih hd']) @[to_additive] lemma prod_product {s : finset γ} {t : finset α} {f : γ×α → β} : (s.product t).prod f = s.prod (λx, t.prod $ λy, f (x, y)) := begin haveI := classical.dec_eq α, haveI := classical.dec_eq γ, rw [product_eq_bind, prod_bind], { congr, funext, exact prod_image (λ _ _ _ _ H, (prod.mk.inj H).2) }, simp only [disjoint_iff_ne, mem_image], rintros _ _ _ _ h ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ ⟨_, _⟩ ⟨_, _, ⟨_, _⟩⟩ _, apply h, cc end @[to_additive] lemma prod_sigma {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} {f : sigma σ → β} : (s.sigma t).prod f = s.prod (λa, (t a).prod $ λs, f ⟨a, s⟩) := by haveI := classical.dec_eq α; haveI := (λ a, classical.dec_eq (σ a)); exact calc (s.sigma t).prod f = (s.bind (λa, (t a).image (λs, sigma.mk a s))).prod f : by rw sigma_eq_bind ... = s.prod (λa, ((t a).image (λs, sigma.mk a s)).prod f) : prod_bind $ assume a₁ ha a₂ ha₂ h, by simp only [disjoint_iff_ne, mem_image]; rintro ⟨_, _⟩ ⟨_, _, _⟩ ⟨_, _⟩ ⟨_, _, _⟩ ⟨_, _⟩; apply h; cc ... = (s.prod $ λa, (t a).prod $ λs, f ⟨a, s⟩) : prod_congr rfl $ λ _ _, prod_image $ λ _ _ _ _ _, by cc @[to_additive] lemma prod_image' [decidable_eq α] {s : finset γ} {g : γ → α} (h : γ → β) (eq : ∀c∈s, f (g c) = (s.filter (λc', g c' = g c)).prod h) : (s.image g).prod f = s.prod h := begin letI := classical.dec_eq γ, rw [← image_bind_filter_eq s g] {occs := occurrences.pos [2]}, rw [finset.prod_bind], { refine finset.prod_congr rfl (assume a ha, _), rcases finset.mem_image.1 ha with ⟨b, hb, rfl⟩, exact eq b hb }, assume a₀ _ a₁ _ ne, refine (disjoint_iff_ne.2 _), assume c₀ h₀ c₁ h₁, rcases mem_filter.1 h₀ with ⟨h₀, rfl⟩, rcases mem_filter.1 h₁ with ⟨h₁, rfl⟩, exact mt (congr_arg g) ne end @[to_additive] lemma prod_mul_distrib : s.prod (λx, f x * g x) = s.prod f * s.prod g := eq.trans (by rw one_mul; refl) fold_op_distrib @[to_additive] lemma prod_comm [decidable_eq γ] {s : finset γ} {t : finset α} {f : γ → α → β} : s.prod (λx, t.prod $ f x) = t.prod (λy, s.prod $ λx, f x y) := finset.induction_on s (by simp only [prod_empty, prod_const_one]) $ λ _ _ H ih, by simp only [prod_insert H, prod_mul_distrib, ih] lemma prod_hom [comm_monoid γ] (g : β → γ) [is_monoid_hom g] : s.prod (λx, g (f x)) = g (s.prod f) := eq.trans (by rw is_monoid_hom.map_one g; refl) (fold_hom $ is_monoid_hom.map_mul g) @[to_additive] lemma prod_hom_rel [comm_monoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : finset α} (h₁ : r 1 1) (h₂ : ∀a b c, r b c → r (f a * b) (g a * c)) : r (s.prod f) (s.prod g) := begin letI := classical.dec_eq α, refine finset.induction_on s h₁ (assume a s has ih, _), rw [prod_insert has, prod_insert has], exact h₂ a (s.prod f) (s.prod g) ih, end @[to_additive] lemma prod_subset (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → f x = 1) : s₁.prod f = s₂.prod f := by haveI := classical.dec_eq α; exact have (s₂ \ s₁).prod f = (s₂ \ s₁).prod (λx, 1), from prod_congr rfl $ by simpa only [mem_sdiff, and_imp], by rw [←prod_sdiff h]; simp only [this, prod_const_one, one_mul] @[to_additive] lemma prod_filter (p : α → Prop) [decidable_pred p] (f : α → β) : (s.filter p).prod f = s.prod (λa, if p a then f a else 1) := calc (s.filter p).prod f = (s.filter p).prod (λa, if p a then f a else 1) : prod_congr rfl (assume a h, by rw [if_pos (mem_filter.1 h).2]) ... = s.prod (λa, if p a then f a else 1) : begin refine prod_subset (filter_subset s) (assume x hs h, _), rw [mem_filter, not_and] at h, exact if_neg (h hs) end @[to_additive] lemma prod_eq_single {s : finset α} {f : α → β} (a : α) (h₀ : ∀b∈s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : s.prod f = f a := by haveI := classical.dec_eq α; from classical.by_cases (assume : a ∈ s, calc s.prod f = ({a} : finset α).prod f : begin refine (prod_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { simpa only [mem_singleton] } end ... = f a : prod_singleton) (assume : a ∉ s, (prod_congr rfl $ λ b hb, h₀ b hb $ by rintro rfl; cc).trans $ prod_const_one.trans (h₁ this).symm) @[simp, to_additive] lemma prod_ite [decidable_eq α] (s : finset α) (a : α) (b : β) : s.prod (λ x, (ite (a = x) b 1)) = ite (a ∈ s) b 1 := begin rw ←finset.prod_filter, split_ifs; simp only [filter_eq, if_true, if_false, h, prod_empty, prod_singleton, insert_empty_eq_singleton], end @[to_additive] lemma prod_attach {f : α → β} : s.attach.prod (λx, f x.val) = s.prod f := by haveI := classical.dec_eq α; exact calc s.attach.prod (λx, f x.val) = ((s.attach).image subtype.val).prod f : by rw [prod_image]; exact assume x _ y _, subtype.eq ... = _ : by rw [attach_image_val] @[to_additive] lemma prod_bij {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, γ) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = s.attach.prod (λx, f x.val) : prod_attach.symm ... = s.attach.prod (λx, g (i x.1 x.2)) : prod_congr rfl $ assume x hx, h _ _ ... = (s.attach.image $ λx:{x // x ∈ s}, i x.1 x.2).prod g : (prod_image $ assume (a₁:{x // x ∈ s}) _ a₂ _ eq, subtype.eq $ i_inj a₁.1 a₂.1 a₁.2 a₂.2 eq).symm ... = t.prod g : prod_subset (by simp only [subset_iff, mem_image, mem_attach]; rintro _ ⟨⟨_, _⟩, _, rfl⟩; solve_by_elim) (assume b hb hb1, false.elim $ hb1 $ by rcases i_surj b hb with ⟨a, ha, rfl⟩; exact mem_image.2 ⟨⟨_, _⟩, mem_attach _ _, rfl⟩) @[to_additive] lemma prod_bij_ne_one {s : finset α} {t : finset γ} {f : α → β} {g : γ → β} (i : Πa∈s, f a ≠ 1 → γ) (hi₁ : ∀a h₁ h₂, i a h₁ h₂ ∈ t) (hi₂ : ∀a₁ a₂ h₁₁ h₁₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (hi₃ : ∀b∈t, g b ≠ 1 → ∃a h₁ h₂, b = i a h₁ h₂) (h : ∀a h₁ h₂, f a = g (i a h₁ h₂)) : s.prod f = t.prod g := by haveI := classical.prop_decidable; exact calc s.prod f = (s.filter $ λx, f x ≠ 1).prod f : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro).symm ... = (t.filter $ λx, g x ≠ 1).prod g : prod_bij (assume a ha, i a (mem_filter.mp ha).1 (mem_filter.mp ha).2) (assume a ha, (mem_filter.mp ha).elim $ λh₁ h₂, mem_filter.mpr ⟨hi₁ a h₁ h₂, λ hg, h₂ (hg ▸ h a h₁ h₂)⟩) (assume a ha, (mem_filter.mp ha).elim $ h a) (assume a₁ a₂ ha₁ ha₂, (mem_filter.mp ha₁).elim $ λha₁₁ ha₁₂, (mem_filter.mp ha₂).elim $ λha₂₁ ha₂₂, hi₂ a₁ a₂ _ _ _ _) (assume b hb, (mem_filter.mp hb).elim $ λh₁ h₂, let ⟨a, ha₁, ha₂, eq⟩ := hi₃ b h₁ h₂ in ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩) ... = t.prod g : (prod_subset (filter_subset _) $ by simp only [not_imp_comm, mem_filter]; exact λ _, and.intro) @[to_additive] lemma exists_ne_one_of_prod_ne_one : s.prod f ≠ 1 → ∃a∈s, f a ≠ 1 := by haveI := classical.dec_eq α; exact finset.induction_on s (λ H, (H rfl).elim) (assume a s has ih h, classical.by_cases (assume ha : f a = 1, let ⟨a, ha, hfa⟩ := ih (by rwa [prod_insert has, ha, one_mul] at h) in ⟨a, mem_insert_of_mem ha, hfa⟩) (assume hna : f a ≠ 1, ⟨a, mem_insert_self _ _, hna⟩)) @[to_additive] lemma prod_range_succ (f : ℕ → β) (n : ℕ) : (range (nat.succ n)).prod f = f n * (range n).prod f := by rw [range_succ, prod_insert not_mem_range_self] lemma prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).prod f = (range n).prod (f ∘ nat.succ) * f 0 | 0 := (prod_range_succ _ _).trans $ mul_comm _ _ | (n + 1) := by rw [prod_range_succ (λ m, f (nat.succ m)), mul_assoc, ← prod_range_succ']; exact prod_range_succ _ _ @[to_additive finset.sum_range_zero] lemma prod_range_zero (f : ℕ → β) : (range 0).prod f = 1 := by rw [range_zero, prod_empty] lemma prod_range_one (f : ℕ → β) : (range 1).prod f = f 0 := by { rw [range_one], apply @prod_singleton ℕ β 0 f } lemma sum_range_one {δ : Type*} [add_comm_monoid δ] (f : ℕ → δ) : (range 1).sum f = f 0 := by { rw [range_one], apply @sum_singleton ℕ δ 0 f } attribute [to_additive finset.sum_range_one] prod_range_one @[simp] lemma prod_const (b : β) : s.prod (λ a, b) = b ^ s.card := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by rw [prod_insert has, card_insert_of_not_mem has, pow_succ, ih]) lemma prod_pow (s : finset α) (n : ℕ) (f : α → β) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [_root_.mul_pow] {contextual := tt}) lemma prod_nat_pow (s : finset α) (n : ℕ) (f : α → ℕ) : s.prod (λ x, f x ^ n) = s.prod f ^ n := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (by simp [nat.mul_pow] {contextual := tt}) @[to_additive] lemma prod_involution {s : finset α} {f : α → β} : ∀ (g : Π a ∈ s, α) (h₁ : ∀ a ha, f a * f (g a ha) = 1) (h₂ : ∀ a ha, f a ≠ 1 → g a ha ≠ a) (h₃ : ∀ a ha, g a ha ∈ s) (h₄ : ∀ a ha, g (g a ha) (h₃ a ha) = a), s.prod f = 1 := by haveI := classical.dec_eq α; haveI := classical.dec_eq β; exact finset.strong_induction_on s (λ s ih g h₁ h₂ h₃ h₄, if hs : s = ∅ then hs.symm ▸ rfl else let ⟨x, hx⟩ := exists_mem_of_ne_empty hs in have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s, from λ y hy, (mem_of_mem_erase (mem_of_mem_erase hy)), have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y, from λ x hx y hy h, by rw [← h₄ x hx, ← h₄ y hy]; simp [h], have ih': (erase (erase s x) (g x hx)).prod f = (1 : β) := ih ((s.erase x).erase (g x hx)) ⟨subset.trans (erase_subset _ _) (erase_subset _ _), λ h, not_mem_erase (g x hx) (s.erase x) (h (h₃ x hx))⟩ (λ y hy, g y (hmem y hy)) (λ y hy, h₁ y (hmem y hy)) (λ y hy, h₂ y (hmem y hy)) (λ y hy, mem_erase.2 ⟨λ (h : g y _ = g x hx), by simpa [g_inj h] using hy, mem_erase.2 ⟨λ (h : g y _ = x), have y = g x hx, from h₄ y (hmem y hy) ▸ by simp [h], by simpa [this] using hy, h₃ y (hmem y hy)⟩⟩) (λ y hy, h₄ y (hmem y hy)), if hx1 : f x = 1 then ih' ▸ eq.symm (prod_subset hmem (λ y hy hy₁, have y = x ∨ y = g x hx, by simp [hy] at hy₁; tauto, this.elim (λ h, h.symm ▸ hx1) (λ h, h₁ x hx ▸ h ▸ hx1.symm ▸ (one_mul _).symm))) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨h₂ x hx hx1, h₃ x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h₁ x hx]) @[to_additive] lemma prod_eq_one {f : α → β} {s : finset α} (h : ∀x∈s, f x = 1) : s.prod f = 1 := calc s.prod f = s.prod (λx, 1) : finset.prod_congr rfl h ... = 1 : finset.prod_const_one end comm_monoid attribute [to_additive] prod_hom lemma sum_smul [add_comm_monoid β] (s : finset α) (n : ℕ) (f : α → β) : s.sum (λ x, add_monoid.smul n (f x)) = add_monoid.smul n (s.sum f) := @prod_pow _ (multiplicative β) _ _ _ _ attribute [to_additive sum_smul] prod_pow @[simp] lemma sum_const [add_comm_monoid β] (b : β) : s.sum (λ a, b) = add_monoid.smul s.card b := @prod_const _ (multiplicative β) _ _ _ attribute [to_additive] prod_const lemma sum_range_succ' [add_comm_monoid β] (f : ℕ → β) : ∀ n : ℕ, (range (nat.succ n)).sum f = (range n).sum (f ∘ nat.succ) + f 0 := @prod_range_succ' (multiplicative β) _ _ attribute [to_additive] prod_range_succ' lemma sum_nat_cast [add_comm_monoid β] [has_one β] (s : finset α) (f : α → ℕ) : ↑(s.sum f) = s.sum (λa, f a : α → β) := (sum_hom _).symm lemma prod_nat_cast [comm_semiring β] (s : finset α) (f : α → ℕ) : ↑(s.prod f) = s.prod (λa, f a : α → β) := (prod_hom _).symm protected lemma sum_nat_coe_enat [decidable_eq α] (s : finset α) (f : α → ℕ) : s.sum (λ x, (f x : enat)) = (s.sum f : ℕ) := begin induction s using finset.induction with a s has ih h, { simp }, { simp [has, ih] } end lemma le_sum_of_subadditive [add_comm_monoid α] [ordered_comm_monoid β] (f : α → β) (h_zero : f 0 = 0) (h_add : ∀x y, f (x + y) ≤ f x + f y) (s : finset γ) (g : γ → α) : f (s.sum g) ≤ s.sum (λc, f (g c)) := begin refine le_trans (multiset.le_sum_of_subadditive f h_zero h_add _) _, rw [multiset.map_map], refl end lemma abs_sum_le_sum_abs [discrete_linear_ordered_field α] {f : β → α} {s : finset β} : abs (s.sum f) ≤ s.sum (λa, abs (f a)) := le_sum_of_subadditive _ abs_zero abs_add s f section comm_group variables [comm_group β] @[simp, to_additive] lemma prod_inv_distrib : s.prod (λx, (f x)⁻¹) = (s.prod f)⁻¹ := prod_hom has_inv.inv end comm_group @[simp] theorem card_sigma {σ : α → Type*} (s : finset α) (t : Π a, finset (σ a)) : card (s.sigma t) = s.sum (λ a, card (t a)) := multiset.card_sigma _ _ lemma card_bind [decidable_eq β] {s : finset α} {t : α → finset β} (h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → disjoint (t x) (t y)) : (s.bind t).card = s.sum (λ u, card (t u)) := calc (s.bind t).card = (s.bind t).sum (λ _, 1) : by simp ... = s.sum (λ a, (t a).sum (λ _, 1)) : finset.sum_bind h ... = s.sum (λ u, card (t u)) : by simp lemma card_bind_le [decidable_eq β] {s : finset α} {t : α → finset β} : (s.bind t).card ≤ s.sum (λ a, (t a).card) := by haveI := classical.dec_eq α; exact finset.induction_on s (by simp) (λ a s has ih, calc ((insert a s).bind t).card ≤ (t a).card + (s.bind t).card : by rw bind_insert; exact finset.card_union_le _ _ ... ≤ (insert a s).sum (λ a, card (t a)) : by rw sum_insert has; exact add_le_add_left ih _) lemma gsmul_sum [add_comm_group β] {f : α → β} {s : finset α} (z : ℤ) : gsmul z (s.sum f) = s.sum (λa, gsmul z (f a)) := (finset.sum_hom (gsmul z)).symm end finset namespace finset variables {s s₁ s₂ : finset α} {f g : α → β} {b : β} {a : α} @[simp] lemma sum_sub_distrib [add_comm_group β] : s.sum (λx, f x - g x) = s.sum f - s.sum g := sum_add_distrib.trans $ congr_arg _ sum_neg_distrib section semiring variables [semiring β] lemma sum_mul : s.sum f * b = s.sum (λx, f x * b) := (sum_hom (λx, x * b)).symm lemma mul_sum : b * s.sum f = s.sum (λx, b * f x) := (sum_hom (λx, b * x)).symm @[simp] lemma sum_mul_boole [decidable_eq α] (s : finset α) (f : α → β) (a : α) : s.sum (λ x, (f x * ite (a = x) 1 0)) = ite (a ∈ s) (f a) 0 := begin convert sum_ite s a (f a), funext, split_ifs with h; simp [h], end @[simp] lemma sum_boole_mul [decidable_eq α] (s : finset α) (f : α → β) (a : α) : s.sum (λ x, (ite (a = x) 1 0) * f x) = ite (a ∈ s) (f a) 0 := begin convert sum_ite s a (f a), funext, split_ifs with h; simp [h], end end semiring section comm_semiring variables [decidable_eq α] [comm_semiring β] lemma prod_eq_zero (ha : a ∈ s) (h : f a = 0) : s.prod f = 0 := calc s.prod f = (insert a (erase s a)).prod f : by rw insert_erase ha ... = 0 : by rw [prod_insert (not_mem_erase _ _), h, zero_mul] lemma prod_sum {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa, finset (δ a)} {f : Πa, δ a → β} : s.prod (λa, (t a).sum (λb, f a b)) = (s.pi t).sum (λp, s.attach.prod (λx, f x.1 (p x.1 x.2))) := begin induction s using finset.induction with a s ha ih, { rw [pi_empty, sum_singleton], refl }, { have h₁ : ∀x ∈ t a, ∀y ∈ t a, ∀h : x ≠ y, disjoint (image (pi.cons s a x) (pi s t)) (image (pi.cons s a y) (pi s t)), { assume x hx y hy h, simp only [disjoint_iff_ne, mem_image], rintros _ ⟨p₂, hp, eq₂⟩ _ ⟨p₃, hp₃, eq₃⟩ eq, have : pi.cons s a x p₂ a (mem_insert_self _ _) = pi.cons s a y p₃ a (mem_insert_self _ _), { rw [eq₂, eq₃, eq] }, rw [pi.cons_same, pi.cons_same] at this, exact h this }, rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_bind h₁], refine sum_congr rfl (λ b _, _), have h₂ : ∀p₁∈pi s t, ∀p₂∈pi s t, pi.cons s a b p₁ = pi.cons s a b p₂ → p₁ = p₂, from assume p₁ h₁ p₂ h₂ eq, injective_pi_cons ha eq, rw [sum_image h₂, mul_sum], refine sum_congr rfl (λ g _, _), rw [attach_insert, prod_insert, prod_image], { simp only [pi.cons_same], congr', ext ⟨v, hv⟩, congr', exact (pi.cons_ne (by rintro rfl; exact ha hv)).symm }, { exact λ _ _ _ _, subtype.eq ∘ subtype.mk.inj }, { simp only [mem_image], rintro ⟨⟨_, hm⟩, _, rfl⟩, exact ha hm } } end end comm_semiring section integral_domain /- add integral_semi_domain to support nat and ennreal -/ variables [decidable_eq α] [integral_domain β] lemma prod_eq_zero_iff : s.prod f = 0 ↔ (∃a∈s, f a = 0) := finset.induction_on s ⟨not.elim one_ne_zero, λ ⟨_, H, _⟩, H.elim⟩ $ λ a s ha ih, by rw [prod_insert ha, mul_eq_zero_iff_eq_zero_or_eq_zero, bex_def, exists_mem_insert, ih, ← bex_def] end integral_domain section ordered_comm_monoid variables [decidable_eq α] [ordered_comm_monoid β] lemma sum_le_sum : (∀x∈s, f x ≤ g x) → s.sum f ≤ s.sum g := finset.induction_on s (λ _, le_refl _) $ assume a s ha ih h, have f a + s.sum f ≤ g a + s.sum g, from add_le_add' (h _ (mem_insert_self _ _)) (ih $ assume x hx, h _ $ mem_insert_of_mem hx), by simpa only [sum_insert ha] lemma sum_nonneg (h : ∀x∈s, 0 ≤ f x) : 0 ≤ s.sum f := le_trans (by rw [sum_const_zero]) (sum_le_sum h) lemma sum_nonpos (h : ∀x∈s, f x ≤ 0) : s.sum f ≤ 0 := le_trans (sum_le_sum h) (by rw [sum_const_zero]) lemma sum_le_sum_of_subset_of_nonneg (h : s₁ ⊆ s₂) (hf : ∀x∈s₂, x ∉ s₁ → 0 ≤ f x) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f ≤ (s₂ \ s₁).sum f + s₁.sum f : le_add_of_nonneg_left' $ sum_nonneg $ by simpa only [mem_sdiff, and_imp] ... = (s₂ \ s₁ ∪ s₁).sum f : (sum_union sdiff_disjoint).symm ... = s₂.sum f : by rw [sdiff_union_of_subset h] lemma sum_eq_zero_iff_of_nonneg : (∀x∈s, 0 ≤ f x) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := finset.induction_on s (λ _, ⟨λ _ _, false.elim, λ _, rfl⟩) $ λ a s ha ih H, have ∀ x ∈ s, 0 ≤ f x, from λ _, H _ ∘ mem_insert_of_mem, by rw [sum_insert ha, add_eq_zero_iff' (H _ $ mem_insert_self _ _) (sum_nonneg this), forall_mem_insert, ih this] lemma sum_eq_zero_iff_of_nonpos : (∀x∈s, f x ≤ 0) → (s.sum f = 0 ↔ ∀x∈s, f x = 0) := @sum_eq_zero_iff_of_nonneg _ (order_dual β) _ _ _ _ lemma single_le_sum (hf : ∀x∈s, 0 ≤ f x) {a} (h : a ∈ s) : f a ≤ s.sum f := have (singleton a).sum f ≤ s.sum f, from sum_le_sum_of_subset_of_nonneg (λ x e, (mem_singleton.1 e).symm ▸ h) (λ x h _, hf x h), by rwa sum_singleton at this end ordered_comm_monoid section canonically_ordered_monoid variables [decidable_eq α] [canonically_ordered_monoid β] lemma sum_le_sum_of_subset (h : s₁ ⊆ s₂) : s₁.sum f ≤ s₂.sum f := sum_le_sum_of_subset_of_nonneg h $ assume x h₁ h₂, zero_le _ lemma sum_le_sum_of_ne_zero [@decidable_rel β (≤)] (h : ∀x∈s₁, f x ≠ 0 → x ∈ s₂) : s₁.sum f ≤ s₂.sum f := calc s₁.sum f = (s₁.filter (λx, f x = 0)).sum f + (s₁.filter (λx, f x ≠ 0)).sum f : by rw [←sum_union, filter_union_filter_neg_eq]; exact disjoint_filter (assume _ h n_h, n_h h) ... ≤ s₂.sum f : add_le_of_nonpos_of_le' (sum_nonpos $ by simp only [mem_filter, and_imp]; exact λ _ _, le_of_eq) (sum_le_sum_of_subset $ by simpa only [subset_iff, mem_filter, and_imp]) end canonically_ordered_monoid section linear_ordered_comm_ring variables [decidable_eq α] [linear_ordered_comm_ring β] /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_nonneg {s : finset α} {f : α → β} (h0 : ∀(x ∈ s), 0 ≤ f x) : 0 ≤ s.prod f := begin induction s using finset.induction with a s has ih h, { simp [zero_le_one] }, { simp [has], apply mul_nonneg, apply h0 a (mem_insert_self a s), exact ih (λ x H, h0 x (mem_insert_of_mem H)) } end /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_pos {s : finset α} {f : α → β} (h0 : ∀(x ∈ s), 0 < f x) : 0 < s.prod f := begin induction s using finset.induction with a s has ih h, { simp [zero_lt_one] }, { simp [has], apply mul_pos, apply h0 a (mem_insert_self a s), exact ih (λ x H, h0 x (mem_insert_of_mem H)) } end /- this is also true for a ordered commutative multiplicative monoid -/ lemma prod_le_prod {s : finset α} {f g : α → β} (h0 : ∀(x ∈ s), 0 ≤ f x) (h1 : ∀(x ∈ s), f x ≤ g x) : s.prod f ≤ s.prod g := begin induction s using finset.induction with a s has ih h, { simp }, { simp [has], apply mul_le_mul, exact h1 a (mem_insert_self a s), apply ih (λ x H, h0 _ _) (λ x H, h1 _ _); exact (mem_insert_of_mem H), apply prod_nonneg (λ x H, h0 x (mem_insert_of_mem H)), apply le_trans (h0 a (mem_insert_self a s)) (h1 a (mem_insert_self a s)) } end end linear_ordered_comm_ring @[simp] lemma card_pi [decidable_eq α] {δ : α → Type*} (s : finset α) (t : Π a, finset (δ a)) : (s.pi t).card = s.prod (λ a, card (t a)) := multiset.card_pi _ _ @[simp] lemma prod_range_id_eq_fact (n : ℕ) : ((range n.succ).erase 0).prod (λ x, x) = nat.fact n := calc ((range n.succ).erase 0).prod (λ x, x) = (range n).prod nat.succ : eq.symm (prod_bij (λ x _, nat.succ x) (λ a h₁, mem_erase.2 ⟨nat.succ_ne_zero _, mem_range.2 $ nat.succ_lt_succ $ by simpa using h₁⟩) (by simp) (λ _ _ _ _, nat.succ_inj) (λ b h, have b.pred.succ = b, from nat.succ_pred_eq_of_pos $ by simp [nat.pos_iff_ne_zero] at *; tauto, ⟨nat.pred b, mem_range.2 $ nat.lt_of_succ_lt_succ (by simp [*] at *), this.symm⟩)) ... = nat.fact n : by induction n; simp [*, range_succ] end finset namespace finset section gauss_sum /-- Gauss' summation formula -/ lemma sum_range_id_mul_two : ∀(n : ℕ), (finset.range n).sum (λi, i) * 2 = n * (n - 1) | 0 := rfl | 1 := rfl | ((n + 1) + 1) := begin rw [sum_range_succ, add_mul, sum_range_id_mul_two (n + 1), mul_comm, two_mul, nat.add_sub_cancel, nat.add_sub_cancel, mul_comm _ n], simp only [add_mul, one_mul, add_comm, add_assoc, add_left_comm] end /-- Gauss' summation formula -/ lemma sum_range_id (n : ℕ) : (finset.range n).sum (λi, i) = (n * (n - 1)) / 2 := by rw [← sum_range_id_mul_two n, nat.mul_div_cancel]; exact dec_trivial end gauss_sum lemma card_eq_sum_ones (s : finset α) : s.card = s.sum (λ _, 1) := by simp end finset section group open list variables [group α] [group β] @[to_additive] theorem is_group_hom.map_prod (f : α → β) [is_group_hom f] (l : list α) : f (prod l) = prod (map f l) := by induction l; simp only [*, is_mul_hom.map_mul f, is_group_hom.map_one f, prod_nil, prod_cons, map] theorem is_group_anti_hom.map_prod (f : α → β) [is_group_anti_hom f] (l : list α) : f (prod l) = prod (map f (reverse l)) := by induction l with hd tl ih; [exact is_group_anti_hom.map_one f, simp only [prod_cons, is_group_anti_hom.map_mul f, ih, reverse_cons, map_append, prod_append, map_singleton, prod_cons, prod_nil, mul_one]] theorem inv_prod : ∀ l : list α, (prod l)⁻¹ = prod (map (λ x, x⁻¹) (reverse l)) := λ l, @is_group_anti_hom.map_prod _ _ _ _ _ inv_is_group_anti_hom l -- TODO there is probably a cleaner proof of this end group section comm_group variables [comm_group α] [comm_group β] (f : α → β) [is_group_hom f] @[to_additive] lemma is_group_hom.map_multiset_prod (m : multiset α) : f m.prod = (m.map f).prod := quotient.induction_on m $ assume l, by simp [is_group_hom.map_prod f l] @[to_additive] lemma is_group_hom.map_finset_prod (g : γ → α) (s : finset γ) : f (s.prod g) = s.prod (f ∘ g) := show f (s.val.map g).prod = (s.val.map (f ∘ g)).prod, by rw [is_group_hom.map_multiset_prod f]; simp end comm_group @[to_additive is_add_group_hom_finset_sum] lemma is_group_hom_finset_prod {α β γ} [group α] [comm_group β] (s : finset γ) (f : γ → α → β) [∀c, is_group_hom (f c)] : is_group_hom (λa, s.prod (λc, f c a)) := { map_mul := assume a b, by simp only [λc, is_mul_hom.map_mul (f c), finset.prod_mul_distrib] } attribute [instance] is_group_hom_finset_prod is_add_group_hom_finset_sum namespace multiset variables [decidable_eq α] @[simp] lemma to_finset_sum_count_eq (s : multiset α) : s.to_finset.sum (λa, s.count a) = s.card := multiset.induction_on s rfl (assume a s ih, calc (to_finset (a :: s)).sum (λx, count x (a :: s)) = (to_finset (a :: s)).sum (λx, (if x = a then 1 else 0) + count x s) : finset.sum_congr rfl $ λ _ _, by split_ifs; [simp only [h, count_cons_self, nat.one_add], simp only [count_cons_of_ne h, zero_add]] ... = card (a :: s) : begin by_cases a ∈ s.to_finset, { have : (to_finset s).sum (λx, ite (x = a) 1 0) = (finset.singleton a).sum (λx, ite (x = a) 1 0), { apply (finset.sum_subset _ _).symm, { intros _ H, rwa mem_singleton.1 H }, { exact λ _ _ H, if_neg (mt finset.mem_singleton.2 H) } }, rw [to_finset_cons, finset.insert_eq_of_mem h, finset.sum_add_distrib, ih, this, finset.sum_singleton, if_pos rfl, add_comm, card_cons] }, { have ha : a ∉ s, by rwa mem_to_finset at h, have : (to_finset s).sum (λx, ite (x = a) 1 0) = (to_finset s).sum (λx, 0), from finset.sum_congr rfl (λ x hx, if_neg $ by rintro rfl; cc), rw [to_finset_cons, finset.sum_insert h, if_pos rfl, finset.sum_add_distrib, this, finset.sum_const_zero, ih, count_eq_zero_of_not_mem ha, zero_add, add_comm, card_cons] } end) end multiset
f0613c0360a3b35b9568598ef394a9359ebce42f
ac2987d8c7832fb4a87edb6bee26141facbb6fa0
/Mathlib/Data/Nat/Basic.lean
64af73f96b8096096e94c700fd4d859bb062f884
[ "Apache-2.0" ]
permissive
AurelienSaue/mathlib4
52204b9bd9d207c922fe0cf3397166728bb6c2e2
84271fe0875bafdaa88ac41f1b5a7c18151bd0d5
refs/heads/master
1,689,156,096,545
1,629,378,840,000
1,629,378,840,000
389,648,603
0
0
Apache-2.0
1,627,307,284,000
1,627,307,284,000
null
UTF-8
Lean
false
false
28,154
lean
import Mathlib.Tactic.Basic import Mathlib.Logic.Basic notation "ℕ" => Nat namespace Nat lemma succ_ne_self : ∀ n : ℕ, succ n ≠ n | 0, h => absurd h (Nat.succ_ne_zero 0) | n+1, h => succ_ne_self n (Nat.noConfusion h id) lemma eq_zero_of_add_eq_zero_right : ∀ {n m : ℕ}, n + m = 0 → n = 0 | 0, m => by simp [Nat.zero_add] | n+1, m => fun h => by exfalso rw [add_one, succ_add] at h apply succ_ne_zero _ h lemma eq_zero_of_add_eq_zero_left {n m : ℕ} (h : n + m = 0) : m = 0 := @eq_zero_of_add_eq_zero_right m n (Nat.add_comm n m ▸ h) @[simp] lemma pred_zero : pred 0 = 0 := rfl @[simp] lemma pred_succ (n : ℕ) : pred (succ n) = n := rfl protected lemma pos_of_ne_zero {n : ℕ} : n ≠ 0 → 0 < n := Or.resolve_left (eq_zero_or_pos n) -- TODO: in mathlib, this is done for ordered monoids protected lemma pos_iff_ne_zero {n : ℕ} : 0 < n ↔ n ≠ 0 := by refine ⟨?_, Nat.pos_of_ne_zero⟩ cases n with | zero => intro h; contradiction | succ n => intro _; apply succ_ne_zero protected lemma not_lt_of_le {n m : ℕ} (h₁ : m ≤ n) : ¬ n < m | h₂ => Nat.not_le_of_gt h₂ h₁ protected lemma lt_of_not_le {a b : ℕ} : ¬ a ≤ b → b < a := (Nat.lt_or_ge b a).resolve_right protected lemma le_or_le (a b : ℕ) : a ≤ b ∨ b ≤ a := (Nat.lt_or_ge _ _).imp_left Nat.le_of_lt protected lemma le_of_not_le {a b : ℕ} : ¬ a ≤ b → b ≤ a := (Nat.le_or_le _ _).resolve_left @[simp] protected lemma not_lt {n m : ℕ} : ¬ n < m ↔ m ≤ n := ⟨Nat.lt_of_not_le, Nat.not_lt_of_le⟩ @[simp] protected lemma not_le {n m : ℕ} : ¬ n ≤ m ↔ m < n := ⟨Nat.lt_of_not_le, Nat.not_lt_of_le⟩ protected lemma lt_or_eq_of_le {n m : ℕ} (h : n ≤ m) : n < m ∨ n = m := (Nat.lt_or_ge _ _).imp_right (Nat.le_antisymm h) protected lemma lt_iff_le_not_le {m n : ℕ} : m < n ↔ m ≤ n ∧ ¬ n ≤ m := ⟨fun h => ⟨Nat.le_of_lt h, Nat.not_le_of_gt h⟩, fun h => Nat.gt_of_not_le h.2⟩ lemma pred_lt_pred : ∀ {n m : ℕ}, n ≠ 0 → n < m → pred n < pred m | 0, _, h, _ => (h rfl).elim | n+1, m+1, _, h => lt_of_succ_lt_succ h protected lemma add_left_cancel_iff {n m k : ℕ} : n + m = n + k ↔ m = k := ⟨Nat.add_left_cancel, fun | rfl => rfl⟩ protected lemma le_of_add_le_add_left {k n m : ℕ} (h : k + n ≤ k + m) : n ≤ m := by let ⟨w, hw⟩ := le.dest h rw [Nat.add_assoc, Nat.add_left_cancel_iff] at hw exact Nat.le.intro hw protected lemma le_of_add_le_add_right {k n m : ℕ} : n + k ≤ m + k → n ≤ m := by rw [Nat.add_comm _ k, Nat.add_comm _ k] apply Nat.le_of_add_le_add_left protected lemma add_le_add_iff_le_right (k n m : ℕ) : n + k ≤ m + k ↔ n ≤ m := ⟨Nat.le_of_add_le_add_right, fun h => Nat.add_le_add_right h _⟩ protected lemma lt_of_add_lt_add_left {k n m : ℕ} (h : k + n < k + m) : n < m := Nat.lt_of_le_of_ne (Nat.le_of_add_le_add_left (Nat.le_of_lt h)) fun heq => Nat.lt_irrefl (k + m) $ by rwa [heq] at h protected lemma lt_add_of_pos_right {n k : ℕ} (h : 0 < k) : n < n + k := Nat.add_lt_add_left h n protected lemma lt_add_of_pos_left {n k : ℕ} (h : 0 < k) : n < k + n := by rw [Nat.add_comm]; exact Nat.lt_add_of_pos_right h protected lemma le_of_mul_le_mul_left {a b c : ℕ} (h : c * a ≤ c * b) (hc : 0 < c) : a ≤ b := Nat.not_lt.1 fun h1 => Nat.not_le.2 (Nat.mul_lt_mul_of_pos_left h1 hc) h lemma eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : 0 < n) (H : n * m = n * k) : m = k := Nat.le_antisymm (Nat.le_of_mul_le_mul_left (Nat.le_of_eq H) Hn) (Nat.le_of_mul_le_mul_left (Nat.le_of_eq H.symm) Hn) lemma eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : 0 < m) (H : n * m = k * m) : n = k := by rw [Nat.mul_comm n m, Nat.mul_comm k m] at H exact Nat.eq_of_mul_eq_mul_left Hm H protected lemma add_self_ne_one : ∀ (n : ℕ), n + n ≠ 1 | n+1, h => have h1 : succ (succ (n + n)) = 1 := succ_add n n ▸ h Nat.noConfusion h1 fun. /- sub properties -/ @[simp] protected lemma zero_sub : ∀ a : ℕ, 0 - a = 0 | 0 => rfl | (a+1) => congr_arg pred (Nat.zero_sub a) lemma sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le (sub_le a b) protected lemma sub_le_sub_right {n m : ℕ} (h : n ≤ m) : ∀ k, n - k ≤ m - k | 0 => h | z+1 => pred_le_pred (Nat.sub_le_sub_right h z) protected lemma add_sub_add_right : ∀ (n k m : ℕ), (n + k) - (m + k) = n - m | n, 0, m => by rw [Nat.add_zero, Nat.add_zero] | n, k+1, m => by rw [add_succ, add_succ, succ_sub_succ, Nat.add_sub_add_right n k m] protected lemma add_sub_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m := by rw [Nat.add_comm k n, Nat.add_comm k m, Nat.add_sub_add_right] protected lemma add_sub_cancel (n m : ℕ) : n + m - m = n := suffices n + m - (0 + m) = n by rwa [Nat.zero_add] at this by rw [Nat.add_sub_add_right, Nat.sub_zero] protected lemma add_sub_cancel_left (n m : ℕ) : n + m - n = m := show n + m - (n + 0) = m by rw [Nat.add_sub_add_left, Nat.sub_zero] protected lemma sub_sub : ∀ (n m k : ℕ), n - m - k = n - (m + k) | n, m, 0 => by rw [Nat.add_zero, Nat.sub_zero] | n, m, (succ k) => by rw [add_succ, sub_succ, sub_succ, Nat.sub_sub n m k] lemma sub_self_add (n m : ℕ) : n - (n + m) = 0 := show (n + 0) - (n + m) = 0 by rw [Nat.add_sub_add_left, Nat.zero_sub] lemma sub_lt_self {a b : ℕ} (h₀ : 0 < a) (h₁ : a ≤ b) : b - a < b := by apply sub_lt _ h₀ apply Nat.lt_of_lt_of_le h₀ h₁ lemma sub_one (n : ℕ) : n - 1 = pred n := rfl lemma succ_sub_one (n : ℕ) : succ n - 1 = n := rfl lemma succ_pred_eq_of_pos : ∀ {n : ℕ}, 0 < n → succ (pred n) = n | n+1, _ => rfl lemma succ_sub (h : n ≤ m) : succ m - n = succ (m - n) := let ⟨k, hk⟩ := Nat.le.dest h by rw [← hk, Nat.add_sub_cancel_left, ← add_succ, Nat.add_sub_cancel_left] lemma sub_eq_zero_of_le (h : n ≤ m) : n - m = 0 := let ⟨k, hk⟩ := Nat.le.dest h; by rw [← hk, sub_self_add] protected lemma le_of_sub_eq_zero : {n m : ℕ} → n - m = 0 → n ≤ m | n, 0, H => by rw [Nat.sub_zero] at H; simp [H] | 0, m+1, H => zero_le _ | n+1, m+1, H => Nat.add_le_add_right (Nat.le_of_sub_eq_zero $ by simp [Nat.add_sub_add_right] at H; exact H) _ protected lemma sub_eq_zero_iff_le : n - m = 0 ↔ n ≤ m := ⟨Nat.le_of_sub_eq_zero, Nat.sub_eq_zero_of_le⟩ lemma add_sub_of_le {n m : ℕ} (h : n ≤ m) : n + (m - n) = m := let ⟨k, hk⟩ := Nat.le.dest h; by rw [← hk, Nat.add_sub_cancel_left] protected lemma sub_add_cancel : {a b : ℕ} → a ≤ b → b - a + a = b | 0, b, _ => rfl | a+1, b+1, h => congrArg Nat.succ $ show (b + 1) - (a + 1) + a = b by rw [Nat.add_comm a, ← Nat.sub_sub] exact Nat.sub_add_cancel h protected lemma add_sub_cancel' {n m : ℕ} (h : m ≤ n) : m + (n - m) = n := by rw [Nat.add_comm, Nat.sub_add_cancel h] protected lemma add_sub_assoc {m k : ℕ} (h : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := let ⟨l, hl⟩ := Nat.le.dest h by rw [← hl, Nat.add_sub_cancel_left, Nat.add_comm k, ← Nat.add_assoc, Nat.add_sub_cancel] protected lemma sub_eq_iff_eq_add {a b c : ℕ} (ab : b ≤ a) : a - b = c ↔ a = c + b := ⟨fun c_eq => by rw [c_eq.symm, Nat.sub_add_cancel ab], fun a_eq => by rw [a_eq, Nat.add_sub_cancel]⟩ protected lemma lt_of_sub_eq_succ (H : m - n = succ l) : n < m := Nat.not_le.1 fun H' => by simp [Nat.sub_eq_zero_of_le H'] at H protected lemma lt_of_add_lt_add_right {a b c : ℕ} (h : a + b < c + b) : a < c := Nat.lt_of_not_le fun h' => Nat.not_le_of_gt h (Nat.add_le_add_right h' _) protected lemma sub_pos_of_lt (h : m < n) : 0 < n - m := by apply Nat.lt_of_add_lt_add_right (b := m) rw [Nat.zero_add, Nat.sub_add_cancel (Nat.le_of_lt h)]; exact h protected lemma sub_lt_sub_left : ∀ {k m n : ℕ} (H : k < m) (h : k < n), m - n < m - k | 0, m+1, n+1, _, _ => by rw [Nat.add_sub_add_right]; exact lt_succ_of_le (Nat.sub_le _ _) | k+1, m+1, n+1, h1, h2 => by rw [Nat.add_sub_add_right, Nat.add_sub_add_right] exact Nat.sub_lt_sub_left h1 h2 protected lemma sub_lt_left_of_lt_add {n k m : ℕ} (H : n ≤ k) (h : k < n + m) : k - n < m := by have := Nat.sub_le_sub_right (succ_le_of_lt h) n rwa [Nat.add_sub_cancel_left, Nat.succ_sub H] at this protected lemma add_le_of_le_sub_left {n k m : ℕ} (H : m ≤ k) (h : n ≤ k - m) : m + n ≤ k := Nat.not_lt.1 fun h' => Nat.not_le.2 h (Nat.sub_lt_left_of_lt_add H h') lemma le_of_le_of_sub_le_sub_right {n m k : ℕ} (h₀ : k ≤ m) (h₁ : n - k ≤ m - k) : n ≤ m := Nat.not_lt.1 fun h' => by have := Nat.add_le_of_le_sub_left h₀ h₁ rw [Nat.add_sub_cancel'] at this; exact Nat.not_le.2 h' this exact Nat.le_trans h₀ (Nat.le_of_lt h') protected lemma sub_le_sub_right_iff {n m k : ℕ} (h : k ≤ m) : n - k ≤ m - k ↔ n ≤ m := ⟨le_of_le_of_sub_le_sub_right h, fun h => Nat.sub_le_sub_right h k⟩ lemma le_sub_iff_add_le {x y k : ℕ} (h : k ≤ y) : x ≤ y - k ↔ x + k ≤ y := by rw [← Nat.add_sub_cancel x k, Nat.sub_le_sub_right_iff h, Nat.add_sub_cancel] protected lemma min_comm (a b : ℕ) : Nat.min a b = Nat.min b a := by simp [Nat.min] by_cases h₁ : a ≤ b <;> by_cases h₂ : b ≤ a <;> simp [h₁, h₂] - (exact Nat.le_antisymm h₁ h₂) - cases not_or_intro h₁ h₂ (Nat.le_or_le _ _) protected lemma min_le_left (a b : ℕ) : Nat.min a b ≤ a := by simp [Nat.min]; by_cases a ≤ b <;> simp [h] - (exact Nat.le_refl _) - exact Nat.le_of_not_le h protected lemma min_eq_left (h : a ≤ b) : Nat.min a b = a := by simp [Nat.min, h] protected lemma min_eq_right (h : b ≤ a) : Nat.min a b = b := by rw [Nat.min_comm a b]; exact Nat.min_eq_left h protected lemma zero_min (a : ℕ) : Nat.min 0 a = 0 := Nat.min_eq_left (zero_le a) protected lemma min_zero (a : ℕ) : Nat.min a 0 = 0 := Nat.min_eq_right (zero_le a) lemma sub_eq_sub_min (n m : ℕ) : n - m = n - Nat.min n m := if h : n ≥ m then by rw [Nat.min_eq_right h] else by rw [sub_eq_zero_of_le (Nat.le_of_not_le h), Nat.min_eq_left (Nat.le_of_not_le h), Nat.sub_self] @[simp] lemma sub_add_min_cancel (n m : ℕ) : n - m + Nat.min n m = n := by rw [sub_eq_sub_min, Nat.sub_add_cancel (Nat.min_le_left n m)] protected def lt_wf := @Nat.ltWf protected def strong_rec_on {p : ℕ → Sort u} (n : ℕ) (H : ∀ n, (∀ m, m < n → p m) → p n) : p n := Nat.lt_wf.fix' H n protected def case_strong_rec_on {p : ℕ → Sort u} (a : ℕ) (hz : p 0) (hi : ∀ n, (∀ m, m ≤ n → p m) → p (succ n)) : p a := Nat.strong_rec_on a fun | 0, _ => hz | n+1, ih => hi n ih lemma mod_add_div (m k : ℕ) : m % k + k * (m / k) = m := by induction m, k using mod.inductionOn with rw [div_eq, mod_eq] | base x y h => simp [h] | ind x y h IH => simp [h]; rw [Nat.mul_succ, ← Nat.add_assoc, IH, Nat.sub_add_cancel h.2] /- div -/ @[simp] protected lemma div_one (n : ℕ) : n / 1 = n := have this : n % 1 + 1 * (n / 1) = n := mod_add_div _ _ by rwa [mod_one, Nat.zero_add, Nat.one_mul] at this @[simp] protected lemma div_zero (n : ℕ) : n / 0 = 0 := by rw [div_eq]; simp [Nat.lt_irrefl] @[simp] protected lemma zero_div (b : ℕ) : 0 / b = 0 := (div_eq 0 b).trans $ if_neg $ And.rec Nat.not_le_of_gt lemma le_div_iff_mul_le (k0 : 0 < k) : x ≤ y / k ↔ x * k ≤ y := by induction y, k using mod.inductionOn generalizing x with (rw [div_eq]; simp [h]; cases x with simp [zero_le] | succ x => ?_) | base y k h => simp [eq_false (not_succ_le_zero x), succ_mul, Nat.add_comm] refine Nat.lt_of_lt_of_le ?_ (Nat.le_add_right _ _) exact Nat.lt_of_not_le fun h' => h ⟨k0, h'⟩ | ind y k h IH => rw [← add_one, Nat.add_le_add_iff_le_right, IH k0, succ_mul, le_sub_iff_add_le h.2] lemma div_lt_iff_lt_mul (Hk : 0 < k) : x / k < y ↔ x < y * k := by rw [← Nat.not_le, ← Nat.not_le]; apply not_congr; apply le_div_iff_mul_le Hk lemma mul_div_le (m n : ℕ) : n * (m / n) ≤ m := by match n, Nat.eq_zero_or_pos n with | _, Or.inl rfl => rw [Nat.zero_mul]; exact m.zero_le | n, Or.inr h => rw [Nat.mul_comm, ← Nat.le_div_iff_mul_le h]; exact Nat.le_refl _ protected lemma div_le_of_le_mul : ∀ {k : ℕ}, m ≤ k * n → m / k ≤ n | 0, h => by simp [Nat.div_zero]; apply zero_le | k+1, h => Nat.le_of_mul_le_mul_left (Nat.le_trans (mul_div_le _ _) h) (zero_lt_succ _) protected lemma div_le_self : ∀ (m n : ℕ), m / n ≤ m | m, 0 => by simp [Nat.div_zero]; apply zero_le | m, n+1 => Nat.div_le_of_le_mul $ by have := Nat.mul_le_mul_right m (succ_pos n) rwa [Nat.one_mul] at this lemma div_eq_sub_div (h₁ : 0 < b) (h₂ : b ≤ a) : a / b = (a - b) / b + 1 := by rw [div_eq a, if_pos]; split <;> assumption lemma div_eq_of_lt (h₀ : a < b) : a / b = 0 := by rw [div_eq a, if_neg] intro h₁ apply Nat.not_le_of_gt h₀ h₁.right /- successor and predecessor -/ lemma add_one_ne_zero (n : ℕ) : n + 1 ≠ 0 := succ_ne_zero _ lemma eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := by cases n <;> simp lemma exists_eq_succ_of_ne_zero (H : n ≠ 0) : ∃k : ℕ, n = succ k := ⟨_, (eq_zero_or_eq_succ_pred _).resolve_left H⟩ def discriminate (H1: n = 0 → α) (H2 : ∀m, n = succ m → α) : α := match e: n with | 0 => H1 e | succ m => H2 m e lemma one_succ_zero : 1 = succ 0 := rfl def two_step_induction {P : ℕ → Sort u} (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : (a : ℕ) → P a | 0 => H1 | 1 => H2 | n+2 => H3 _ (two_step_induction H1 H2 H3 _) (two_step_induction H1 H2 H3 _) def sub_induction {P : ℕ → ℕ → Sort u} (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : (n m : ℕ) → P n m | 0, m => H1 _ | n+1, 0 => H2 _ | n+1, m+1 => H3 _ _ (sub_induction H1 H2 H3 n m) /- addition -/ lemma succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := by simp [succ_add, add_succ] lemma one_add (n : ℕ) : 1 + n = succ n := by simp [Nat.add_comm] lemma eq_zero_of_add_eq_zero (H : n + m = 0) : n = 0 ∧ m = 0 := ⟨Nat.eq_zero_of_add_eq_zero_right H, Nat.eq_zero_of_add_eq_zero_left H⟩ lemma eq_zero_of_mul_eq_zero : ∀ {n m : ℕ}, n * m = 0 → n = 0 ∨ m = 0 | 0, m, _ => Or.inl rfl | n+1, m, h => by rw [succ_mul] at h; exact Or.inr (eq_zero_of_add_eq_zero_left h) /- properties of inequality -/ lemma le_succ_of_pred_le : pred n ≤ m → n ≤ succ m := match n with | 0 => fun _ => zero_le _ | a+1 => succ_le_succ lemma le_lt_antisymm {n m : ℕ} (h₁ : n ≤ m) (h₂ : m < n) : False := Nat.lt_irrefl n (Nat.lt_of_le_of_lt h₁ h₂) lemma lt_le_antisymm {n m : ℕ} (h₁ : n < m) (h₂ : m ≤ n) : False := le_lt_antisymm h₂ h₁ protected lemma lt_asymm {n m : ℕ} (h₁ : n < m) : ¬ m < n := le_lt_antisymm (Nat.le_of_lt h₁) protected def lt_ge_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : b ≤ a → C) : C := if h : a < b then h₁ h else h₂ (Nat.not_lt.1 h) protected def lt_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : a = b → C) (h₃ : b < a → C) : C := Nat.lt_ge_by_cases h₁ fun h₁ => Nat.lt_ge_by_cases h₃ fun h => h₂ (Nat.le_antisymm h h₁) protected lemma lt_trichotomy (a b : ℕ) : a < b ∨ a = b ∨ b < a := Nat.lt_by_cases Or.inl (Or.inr ∘ Or.inl) (Or.inr ∘ Or.inr) protected lemma eq_or_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ∨ b < a := (Nat.lt_trichotomy a b).resolve_left hnlt lemma lt_succ_of_lt (h : a < b) : a < succ b := le_succ_of_le h lemma one_pos : 0 < 1 := Nat.zero_lt_one /- subtraction -/ protected lemma sub_le_sub_left (k : ℕ) (h : n ≤ m) : k - m ≤ k - n := match m, le.dest h with | _, ⟨a, rfl⟩ => by rw [← Nat.sub_sub]; apply sub_le protected lemma sub.right_comm (m n k : ℕ) : m - n - k = m - k - n := by rw [Nat.sub_sub, Nat.sub_sub, Nat.add_comm] lemma succ_sub_sub_succ (n m k : ℕ) : succ n - m - succ k = n - m - k := by rw [sub.right_comm, succ_sub_succ, sub.right_comm] lemma mul_pred_left : ∀ (n m : ℕ), pred n * m = n * m - m | 0, m => by simp [Nat.zero_sub, pred_zero, Nat.zero_mul] | n+1, m => by rw [pred_succ, succ_mul, Nat.add_sub_cancel] lemma mul_pred_right (n m : ℕ) : n * pred m = n * m - n := by rw [Nat.mul_comm, mul_pred_left, Nat.mul_comm] protected lemma mul_sub_right_distrib (n) : ∀ (m k : ℕ), (n - m) * k = n * k - m * k | 0, k => by simp [Nat.sub_zero, Nat.zero_mul] | m+1, k => by rw [Nat.sub_succ, mul_pred_left, Nat.mul_sub_right_distrib, succ_mul, Nat.sub_sub] protected lemma mul_sub_left_distrib (n m k : ℕ) : n * (m - k) = n * m - n * k := by rw [Nat.mul_comm, Nat.mul_sub_right_distrib, Nat.mul_comm m n, Nat.mul_comm n k] protected lemma mul_self_sub_mul_self_eq (a b : Nat) : a * a - b * b = (a + b) * (a - b) := by rw [Nat.mul_sub_left_distrib, Nat.right_distrib, Nat.right_distrib, Nat.mul_comm b a, Nat.add_comm (a*a) (a*b), Nat.add_sub_add_left] lemma succ_mul_succ_eq (a b : Nat) : succ a * succ b = a*b + a + b + 1 := by rw [mul_succ, succ_mul, Nat.add_right_comm _ a]; rfl protected lemma sub_sub_self {n m : ℕ} (h : m ≤ n) : n - (n - m) = m := (Nat.sub_eq_iff_eq_add (Nat.sub_le _ _)).2 (add_sub_of_le h).symm protected lemma sub_add_comm {n m k : ℕ} (h : k ≤ n) : n + m - k = n - k + m := (Nat.sub_eq_iff_eq_add (Nat.le_trans h (Nat.le_add_right _ _))).2 (by rwa [Nat.add_right_comm, Nat.sub_add_cancel]) lemma sub_one_sub_lt (h : i < n) : n - 1 - i < n := by rw [Nat.sub_sub] apply Nat.sub_lt apply Nat.lt_of_lt_of_le (Nat.zero_lt_succ _) h rw [Nat.add_comm] apply Nat.zero_lt_succ lemma pred_inj : ∀ {a b : ℕ}, 0 < a → 0 < b → Nat.pred a = Nat.pred b → a = b | a+1, b+1, ha, hb, h => by rw [show a = b from h] | a+1, 0, ha, hb, h => absurd hb (Nat.lt_irrefl _) | 0, b+1, ha, hb, h => absurd ha (Nat.lt_irrefl _) | 0, 0, ha, hb, h => rfl /- find -/ section find variable (p : ℕ → Prop) private def lbp (m n : ℕ) : Prop := m = n + 1 ∧ ∀ k, k ≤ n → ¬p k variable [DecidablePred p] (H : ∃ n, p n) private def wf_lbp : WellFounded (lbp p) := by refine ⟨let ⟨n, pn⟩ := H; ?_⟩ suffices ∀ m k, n ≤ k + m → Acc (lbp p) k from fun a => this _ _ (Nat.le_add_left _ _) intro m induction m with refine fun k kn => ⟨_, fun | _, ⟨rfl, a⟩ => ?_⟩ | zero => exact absurd pn (a _ kn) | succ m IH => exact IH _ (by rw [Nat.add_right_comm]; exact kn) protected def find_x : {n // p n ∧ ∀ m, m < n → ¬p m} := (wf_lbp p H).fix' (C := fun k => (∀n, n < k → ¬p n) → {n // p n ∧ ∀ m, m < n → ¬p m}) (fun m IH al => if pm : p m then ⟨m, pm, al⟩ else have this : ∀ n, n ≤ m → ¬p n := fun n h => (Nat.lt_or_eq_of_le h).elim (al n) fun e => by rw [e]; exact pm IH _ ⟨rfl, this⟩ fun n h => this n $ Nat.le_of_succ_le_succ h) 0 fun n h => absurd h (Nat.not_lt_zero _) /-- If `p` is a (decidable) predicate on `ℕ` and `hp : ∃ (n : ℕ), p n` is a proof that there exists some natural number satisfying `p`, then `nat.find hp` is the smallest natural number satisfying `p`. Note that `nat.find` is protected, meaning that you can't just write `find`, even if the `nat` namespace is open. The API for `nat.find` is: * `nat.find_spec` is the proof that `nat.find hp` satisfies `p`. * `nat.find_min` is the proof that if `m < nat.find hp` then `m` does not satisfy `p`. * `nat.find_min'` is the proof that if `m` does satisfy `p` then `nat.find hp ≤ m`. -/ protected def find : ℕ := (Nat.find_x p H).1 protected lemma find_spec : p (Nat.find p H) := (Nat.find_x p H).2.1 protected lemma find_min : ∀ {m : ℕ}, m < Nat.find p H → ¬p m := @(Nat.find_x p H).2.2 protected lemma find_min' {m : ℕ} (h : p m) : Nat.find p H ≤ m := Nat.not_lt.1 fun l => Nat.find_min p H l h end find /- Up -/ /-- A well-ordered relation for "upwards" induction on the ℕural numbers up to some bound `ub`. -/ def Up (ub a i : ℕ) := i < a ∧ i < ub lemma Up.next {ub i} (h : i < ub) : Up ub (i+1) i := ⟨Nat.lt_succ_self _, h⟩ lemma Up.WF (ub) : WellFounded (Up ub) := Subrelation.wf (h₂ := measureWf (ub - .)) @fun a i ⟨ia, iu⟩ => Nat.sub_lt_sub_left iu ia /- mod -/ @[simp] lemma add_mod_right (x z : ℕ) : (x + z) % z = x % z := by rw [mod_eq_sub_mod (Nat.le_add_left _ _), Nat.add_sub_cancel] @[simp] lemma add_mod_left (x z : ℕ) : (x + z) % x = z % x := by rw [Nat.add_comm, add_mod_right] @[simp] lemma add_mul_mod_self_left (x y z : ℕ) : (x + y * z) % y = x % y := by induction z with | zero => rw [Nat.mul_zero, Nat.add_zero] | succ z ih => rw [mul_succ, ← Nat.add_assoc, add_mod_right, ih] @[simp] lemma add_mul_mod_self_right (x y z : ℕ) : (x + y * z) % z = x % z := by rw [Nat.mul_comm, add_mul_mod_self_left] @[simp] lemma mul_mod_right (m n : ℕ) : (m * n) % m = 0 := by rw [← Nat.zero_add (m*n), add_mul_mod_self_left, zero_mod] @[simp] lemma mul_mod_left (m n : ℕ) : (m * n) % n = 0 := by rw [Nat.mul_comm, mul_mod_right] lemma mul_mod_mul_left (z x y : ℕ) : (z * x) % (z * y) = z * (x % y) := if y0 : y = 0 then by rw [y0, Nat.mul_zero, mod_zero, mod_zero] else if z0 : z = 0 then by rw [z0, Nat.zero_mul, Nat.zero_mul, Nat.zero_mul, mod_zero] else by induction x using Nat.strong_rec_on with | _ n IH => have y0 : y > 0 := Nat.pos_of_ne_zero y0 have z0 : z > 0 := Nat.pos_of_ne_zero z0 cases Nat.lt_or_ge n y with | inl yn => rw [mod_eq_of_lt yn, mod_eq_of_lt (Nat.mul_lt_mul_of_pos_left yn z0)] | inr yn => rw [mod_eq_sub_mod yn, mod_eq_sub_mod (Nat.mul_le_mul_left z yn), ← Nat.mul_sub_left_distrib] exact IH _ (sub_lt (Nat.lt_of_lt_of_le y0 yn) y0) lemma mul_mod_mul_right (z x y : ℕ) : (x * z) % (y * z) = (x % y) * z := by rw [Nat.mul_comm x z, Nat.mul_comm y z, Nat.mul_comm (x % y) z]; apply mul_mod_mul_left lemma sub_mul_mod (x k n : ℕ) (h₁ : n*k ≤ x) : (x - n*k) % n = x % n := by induction k with | zero => rw [Nat.mul_zero, Nat.sub_zero] | succ k IH => have h₂ : n * k ≤ x := by rw [mul_succ] at h₁ apply Nat.le_trans _ h₁ apply le_add_right _ n have h₄ : x - n * k ≥ n := by apply Nat.le_of_add_le_add_right (k := n*k) rw [Nat.sub_add_cancel h₂] simp [mul_succ, Nat.add_comm] at h₁; simp [h₁] rw [mul_succ, ← Nat.sub_sub, ← mod_eq_sub_mod h₄, IH h₂] /- div -/ lemma sub_mul_div (x n p : ℕ) (h₁ : n*p ≤ x) : (x - n*p) / n = x / n - p := by cases eq_zero_or_pos n with | inl h₀ => rw [h₀, Nat.div_zero, Nat.div_zero, Nat.zero_sub] | inr h₀ => induction p with | zero => rw [Nat.mul_zero, Nat.sub_zero, Nat.sub_zero] | succ p IH => have h₂ : n*p ≤ x := by transitivity - (apply Nat.mul_le_mul_left; apply le_succ) - apply h₁ have h₃ : x - n * p ≥ n := by apply Nat.le_of_add_le_add_right rw [Nat.sub_add_cancel h₂, Nat.add_comm] rw [mul_succ] at h₁ apply h₁ rw [sub_succ, ← IH h₂] rw [div_eq_sub_div h₀ h₃] simp [add_one, pred_succ, mul_succ, Nat.sub_sub] lemma div_mul_le_self : ∀ (m n : ℕ), m / n * n ≤ m | m, 0 => by simp; apply zero_le | m, n+1 => (le_div_iff_mul_le (Nat.succ_pos _)).1 (Nat.le_refl _) @[simp] lemma add_div_right (x : ℕ) {z : ℕ} (H : 0 < z) : (x + z) / z = succ (x / z) := by rw [div_eq_sub_div H (Nat.le_add_left _ _), Nat.add_sub_cancel] @[simp] lemma add_div_left (x : ℕ) {z : ℕ} (H : 0 < z) : (z + x) / z = succ (x / z) := by rw [Nat.add_comm, add_div_right x H] @[simp] lemma mul_div_right (n : ℕ) {m : ℕ} (H : 0 < m) : m * n / m = n := by induction n <;> simp_all [mul_succ] @[simp] lemma mul_div_left (m : ℕ) {n : ℕ} (H : 0 < n) : m * n / n = m := by rw [Nat.mul_comm, mul_div_right _ H] protected lemma div_self (H : 0 < n) : n / n = 1 := let t := add_div_right 0 H; by rwa [Nat.zero_add, Nat.zero_div] at t lemma add_mul_div_left (x z : ℕ) {y : ℕ} (H : 0 < y) : (x + y * z) / y = x / y + z := by induction z with | zero => rw [Nat.mul_zero, Nat.add_zero, Nat.add_zero] | succ z ih => rw [mul_succ, ← Nat.add_assoc, add_div_right _ H, ih]; rfl lemma add_mul_div_right (x y : ℕ) {z : ℕ} (H : 0 < z) : (x + y * z) / z = x / z + y := by rw [Nat.mul_comm, add_mul_div_left _ _ H] protected lemma mul_div_cancel (m : ℕ) {n : ℕ} (H : 0 < n) : m * n / n = m := let t := add_mul_div_right 0 m H; by rwa [Nat.zero_add, Nat.zero_div, Nat.zero_add] at t protected lemma mul_div_cancel_left (m : ℕ) {n : ℕ} (H : 0 < n) : n * m / n = m := by rw [Nat.mul_comm, Nat.mul_div_cancel _ H] protected lemma div_eq_of_eq_mul_left (H1 : 0 < n) (H2 : m = k * n) : m / n = k := by rw [H2, Nat.mul_div_cancel _ H1] protected lemma div_eq_of_eq_mul_right (H1 : 0 < n) (H2 : m = n * k) : m / n = k := by rw [H2, Nat.mul_div_cancel_left _ H1] protected lemma div_eq_of_lt_le (lo : k * n ≤ m) (hi : m < succ k * n) : m / n = k := have npos : 0 < n := (eq_zero_or_pos _).resolve_left fun hn => by rw [hn, Nat.mul_zero] at hi lo; exact absurd lo (Nat.not_le_of_gt hi) Nat.le_antisymm (le_of_lt_succ ((Nat.div_lt_iff_lt_mul npos).2 hi)) ((Nat.le_div_iff_mul_le npos).2 lo) lemma mul_sub_div (x n p : ℕ) (h₁ : x < n*p) : (n * p - succ x) / n = p - succ (x / n) := by have npos : 0 < n := (eq_zero_or_pos _).resolve_left fun n0 => by rw [n0, Nat.zero_mul] at h₁; exact not_lt_zero _ h₁ (apply Nat.div_eq_of_lt_le) - rw [Nat.mul_sub_right_distrib, Nat.mul_comm] apply Nat.sub_le_sub_left (exact (div_lt_iff_lt_mul npos).1 (lt_succ_self _)) - change succ (pred (n * p - x)) ≤ (succ (pred (p - x / n))) * n rw [succ_pred_eq_of_pos (Nat.sub_pos_of_lt h₁), fun h => succ_pred_eq_of_pos (Nat.sub_pos_of_lt h)] -- TODO: why is the function needed? - rw [Nat.mul_sub_right_distrib, Nat.mul_comm] (apply Nat.sub_le_sub_left; apply div_mul_le_self) - apply (div_lt_iff_lt_mul npos).2; rwa [Nat.mul_comm] protected lemma div_div_eq_div_mul (m n k : ℕ) : m / n / k = m / (n * k) := by cases eq_zero_or_pos k with | inl k0 => rw [k0, Nat.mul_zero, Nat.div_zero, Nat.div_zero] | inr kpos => ?_ cases eq_zero_or_pos n with | inl n0 => rw [n0, Nat.zero_mul, Nat.div_zero, Nat.zero_div] | inr npos => ?_ (apply Nat.le_antisymm) - apply (le_div_iff_mul_le (Nat.mul_pos npos kpos)).2 rw [Nat.mul_comm n k, ← Nat.mul_assoc] apply (le_div_iff_mul_le npos).1 apply (le_div_iff_mul_le kpos).1 (apply Nat.le_refl) - apply (le_div_iff_mul_le kpos).2 apply (le_div_iff_mul_le npos).2 rw [Nat.mul_assoc, Nat.mul_comm n k] apply (le_div_iff_mul_le (Nat.mul_pos kpos npos)).1 apply Nat.le_refl protected lemma mul_div_mul {m : ℕ} (n k : ℕ) (H : 0 < m) : m * n / (m * k) = n / k := by rw [← Nat.div_div_eq_div_mul, Nat.mul_div_cancel_left _ H] protected lemma mul_le_mul_of_nonneg_left {a b c : ℕ} (h₁ : a ≤ b) : c * a ≤ c * b := by by_cases hba: b ≤ a; { simp [Nat.le_antisymm hba h₁]; apply Nat.le_refl } by_cases hc0 : c ≤ 0; { simp [Nat.le_antisymm hc0 (zero_le c), Nat.zero_mul] } exact Nat.le_of_lt (Nat.mul_lt_mul_of_pos_left (Nat.not_le.1 hba) (Nat.not_le.1 hc0)) protected lemma mul_le_mul_of_nonneg_right {a b c : ℕ} (h₁ : a ≤ b) : a * c ≤ b * c := by by_cases hba : b ≤ a; { simp [Nat.le_antisymm hba h₁]; apply Nat.le_refl } by_cases hc0 : c ≤ 0; { simp [Nat.le_antisymm hc0 (zero_le c), Nat.mul_zero] } exact Nat.le_of_lt (Nat.mul_lt_mul_of_pos_right (Nat.not_le.1 hba) (Nat.not_le.1 hc0)) protected lemma mul_lt_mul (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) : a * b < c * d := Nat.lt_of_lt_of_le (Nat.mul_lt_mul_of_pos_right hac pos_b) (Nat.mul_le_mul_of_nonneg_left hbd) protected lemma mul_lt_mul' (h1 : a ≤ c) (h2 : b < d) (h3 : 0 < c) : a * b < c * d := Nat.lt_of_le_of_lt (Nat.mul_le_mul_of_nonneg_right h1) (Nat.mul_lt_mul_of_pos_left h2 h3) lemma div_lt_self (h₁ : 0 < n) (h₂ : 1 < m) : n / m < n := by have m_pos : 0 < m := Nat.lt_trans (by rfl) h₂ suffices 1 * n < m * n by rw [Nat.one_mul, Nat.mul_comm] at this exact (Nat.div_lt_iff_lt_mul m_pos).2 this exact Nat.mul_lt_mul h₂ (Nat.le_refl _) h₁ end Nat
8f7ec79d8e5898c2506189c009339b2d557d8033
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/prod/tprod.lean
65718a6766ea0cf16a83a71110f1b8fcd35c2a4b
[ "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,006
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import data.list.nodup /-! # Finite products of types > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines the product of types over a list. For `l : list ι` and `α : ι → Type*` we define `list.tprod α l = l.foldr (λ i β, α i × β) punit`. This type should not be used if `Π i, α i` or `Π i ∈ l, α i` can be used instead (in the last expression, we could also replace the list `l` by a set or a finset). This type is used as an intermediary between binary products and finitary products. The application of this type is finitary product measures, but it could be used in any construction/theorem that is easier to define/prove on binary products than on finitary products. * Once we have the construction on binary products (like binary product measures in `measure_theory.prod`), we can easily define a finitary version on the type `tprod l α` by iterating. Properties can also be easily extended from the binary case to the finitary case by iterating. * Then we can use the equivalence `list.tprod.pi_equiv_tprod` below (or enhanced versions of it, like a `measurable_equiv` for product measures) to get the construction on `Π i : ι, α i`, at least when assuming `[fintype ι] [encodable ι]` (using `encodable.sorted_univ`). Using `local attribute [instance] fintype.to_encodable` we can get rid of the argument `[encodable ι]`. ## Main definitions * We have the equivalence `tprod.pi_equiv_tprod : (Π i, α i) ≃ tprod α l` if `l` contains every element of `ι` exactly once. * The product of sets is `set.tprod : (Π i, set (α i)) → set (tprod α l)`. -/ open list function variables {ι : Type*} {α : ι → Type*} {i j : ι} {l : list ι} {f : Π i, α i} namespace list variable (α) /-- The product of a family of types over a list. -/ def tprod (l : list ι) : Type* := l.foldr (λ i β, α i × β) punit variable {α} namespace tprod open list /-- Turning a function `f : Π i, α i` into an element of the iterated product `tprod α l`. -/ protected def mk : ∀ (l : list ι) (f : Π i, α i), tprod α l | [] := λ f, punit.star | (i :: is) := λ f, (f i, mk is f) instance [∀ i, inhabited (α i)] : inhabited (tprod α l) := ⟨tprod.mk l default⟩ @[simp] lemma fst_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).1 = f i := rfl @[simp] lemma snd_mk (i : ι) (l : list ι) (f : Π i, α i) : (tprod.mk (i::l) f).2 = tprod.mk l f := rfl variables [decidable_eq ι] /-- Given an element of the iterated product `l.prod α`, take a projection into direction `i`. If `i` appears multiple times in `l`, this chooses the first component in direction `i`. -/ protected def elim : ∀ {l : list ι} (v : tprod α l) {i : ι} (hi : i ∈ l), α i | (i :: is) v j hj := if hji : j = i then by { subst hji, exact v.1 } else elim v.2 (hj.resolve_left hji) @[simp] lemma elim_self (v : tprod α (i :: l)) : v.elim (l.mem_cons_self i) = v.1 := by simp [tprod.elim] @[simp] lemma elim_of_ne (hj : j ∈ i :: l) (hji : j ≠ i) (v : tprod α (i :: l)) : v.elim hj = tprod.elim v.2 (hj.resolve_left hji) := by simp [tprod.elim, hji] @[simp] lemma elim_of_mem (hl : (i :: l).nodup) (hj : j ∈ l) (v : tprod α (i :: l)) : v.elim (mem_cons_of_mem _ hj) = tprod.elim v.2 hj := by { apply elim_of_ne, rintro rfl, exact hl.not_mem hj } lemma elim_mk : ∀ (l : list ι) (f : Π i, α i) {i : ι} (hi : i ∈ l), (tprod.mk l f).elim hi = f i | (i :: is) f j hj := begin by_cases hji : j = i, { subst hji, simp }, { rw [elim_of_ne _ hji, snd_mk, elim_mk] } end @[ext] lemma ext : ∀ {l : list ι} (hl : l.nodup) {v w : tprod α l} (hvw : ∀ i (hi : i ∈ l), v.elim hi = w.elim hi), v = w | [] hl v w hvw := punit.ext | (i :: is) hl v w hvw := begin ext, rw [← elim_self v, hvw, elim_self], refine ext (nodup_cons.mp hl).2 (λ j hj, _), rw [← elim_of_mem hl, hvw, elim_of_mem hl] end /-- A version of `tprod.elim` when `l` contains all elements. In this case we get a function into `Π i, α i`. -/ @[simp] protected def elim' (h : ∀ i, i ∈ l) (v : tprod α l) (i : ι) : α i := v.elim (h i) lemma mk_elim (hnd : l.nodup) (h : ∀ i, i ∈ l) (v : tprod α l) : tprod.mk l (v.elim' h) = v := tprod.ext hnd (λ i hi, by simp [elim_mk]) /-- Pi-types are equivalent to iterated products. -/ def pi_equiv_tprod (hnd : l.nodup) (h : ∀ i, i ∈ l) : (Π i, α i) ≃ tprod α l := ⟨tprod.mk l, tprod.elim' h, λ f, funext $ λ i, elim_mk l f (h i), mk_elim hnd h⟩ end tprod end list namespace set open list /-- A product of sets in `tprod α l`. -/ @[simp] protected def tprod : ∀ (l : list ι) (t : Π i, set (α i)), set (tprod α l) | [] t := univ | (i :: is) t := t i ×ˢ tprod is t lemma mk_preimage_tprod : ∀ (l : list ι) (t : Π i, set (α i)), tprod.mk l ⁻¹' set.tprod l t = {i | i ∈ l}.pi t | [] t := by simp [set.tprod] | (i :: l) t := begin ext f, have : f ∈ tprod.mk l ⁻¹' set.tprod l t ↔ f ∈ {x | x ∈ l}.pi t, { rw [mk_preimage_tprod l t] }, change tprod.mk l f ∈ set.tprod l t ↔ ∀ (i : ι), i ∈ l → f i ∈ t i at this, /- `simp [set.tprod, tprod.mk, this]` can close this goal but is slow. -/ rw [set.tprod, tprod.mk, mem_preimage, mem_pi, prod_mk_mem_set_prod_eq], simp_rw [mem_set_of_eq, mem_cons_iff], rw [forall_eq_or_imp, and.congr_right_iff], exact λ _, this end lemma elim_preimage_pi [decidable_eq ι] {l : list ι} (hnd : l.nodup) (h : ∀ i, i ∈ l) (t : Π i, set (α i)) : tprod.elim' h ⁻¹' pi univ t = set.tprod l t := begin have : { i | i ∈ l } = univ, { ext i, simp [h] }, rw [← this, ← mk_preimage_tprod, preimage_preimage], convert preimage_id, simp [tprod.mk_elim hnd h, id_def] end end set
2be84790b53154d8f70fe13c4cb5828082c4f3d3
0845ae2ca02071debcfd4ac24be871236c01784f
/tests/compiler/str.lean
5abe86f52547f301c683a7f8641e7e2fa5e67bad
[ "Apache-2.0" ]
permissive
GaloisInc/lean4
74c267eb0e900bfaa23df8de86039483ecbd60b7
228ddd5fdcd98dd4e9c009f425284e86917938aa
refs/heads/master
1,643,131,356,301
1,562,715,572,000
1,562,715,572,000
192,390,898
0
0
null
1,560,792,750,000
1,560,792,749,000
null
UTF-8
Lean
false
false
936
lean
def showChars : Nat → String → String.Pos → IO Unit | 0 _ _ := pure () | (n+1) s idx := unless (s.atEnd idx) $ IO.println (">> " ++ toString (s.get idx)) *> showChars n s (s.next idx) def main : IO UInt32 := let s₁ := "hello α_world_β" in let b : String.Pos := 0 in let e := s₁.bsize in IO.println (s₁.extract b e) *> IO.println (s₁.extract (b+2) e) *> IO.println (s₁.extract (b+2) (e-1)) *> IO.println (s₁.extract (b+2) (e-2)) *> IO.println (s₁.extract (b+7) e) *> IO.println (s₁.extract (b+8) e) *> IO.println (toString e) *> IO.println (repr " aaa ".trim) *> showChars s₁.length s₁ 0 *> IO.println ("abc".isPrefixOf "abcd") *> IO.println ("abcd".isPrefixOf "abcd") *> IO.println ("".isPrefixOf "abcd") *> IO.println ("".isPrefixOf "") *> IO.println ("ab".isPrefixOf "cb") *> IO.println ("ab".isPrefixOf "a") *> IO.println ("αb".isPrefixOf "αbc") *> pure 0
3f56840f23745f3ef1c5cae060b092a05930ec1b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/calculus/mean_value.lean
a8bcfed1b4f32b311f7778738031fd2015c28e4c
[]
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
52,017
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, Yury Kudryashov -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.calculus.local_extr import Mathlib.analysis.convex.topology import Mathlib.data.complex.is_R_or_C import Mathlib.PostPort universes u_1 u_2 u_3 u_4 u_5 namespace Mathlib /-! # The mean value inequality and equalities In this file we prove the following facts: * `convex.norm_image_sub_le_of_norm_deriv_le` : if `f` is differentiable on a convex set `s` and the norm of its derivative is bounded by `C`, then `f` is Lipschitz continuous on `s` with constant `C`; also a variant in which what is bounded by `C` is the norm of the difference of the derivative from a fixed linear map. * `image_le_of*`, `image_norm_le_of_*` : several similar lemmas deducing `f x ≤ B x` or `∥f x∥ ≤ B x` from upper estimates on `f'` or `∥f'∥`, respectively. These lemmas differ by their assumptions: * `of_liminf_*` lemmas assume that limit inferior of some ratio is less than `B' x`; * `of_deriv_right_*`, `of_norm_deriv_right_*` lemmas assume that the right derivative or its norm is less than `B' x`; * `of_*_lt_*` lemmas assume a strict inequality whenever `f x = B x` or `∥f x∥ = B x`; * `of_*_le_*` lemmas assume a non-strict inequality everywhere on `[a, b)`; * name of a lemma ends with `'` if (1) it assumes that `B` is continuous on `[a, b]` and has a right derivative at every point of `[a, b)`, and (2) the lemma has a counterpart assuming that `B` is differentiable everywhere on `ℝ` * `norm_image_sub_le_*_segment` : if derivative of `f` on `[a, b]` is bounded above by a constant `C`, then `∥f x - f a∥ ≤ C * ∥x - a∥`; several versions deal with right derivative and derivative within `[a, b]` (`has_deriv_within_at` or `deriv_within`). * `convex.is_const_of_fderiv_within_eq_zero` : if a function has derivative `0` on a convex set `s`, then it is a constant on `s`. * `exists_ratio_has_deriv_at_eq_ratio_slope` and `exists_ratio_deriv_eq_ratio_slope` : Cauchy's Mean Value Theorem. * `exists_has_deriv_at_eq_slope` and `exists_deriv_eq_slope` : Lagrange's Mean Value Theorem. * `domain_mvt` : Lagrange's Mean Value Theorem, applied to a segment in a convex domain. * `convex.image_sub_lt_mul_sub_of_deriv_lt`, `convex.mul_sub_lt_image_sub_of_lt_deriv`, `convex.image_sub_le_mul_sub_of_deriv_le`, `convex.mul_sub_le_image_sub_of_le_deriv`, if `∀ x, C (</≤/>/≥) (f' x)`, then `C * (y - x) (</≤/>/≥) (f y - f x)` whenever `x < y`. * `convex.mono_of_deriv_nonneg`, `convex.antimono_of_deriv_nonpos`, `convex.strict_mono_of_deriv_pos`, `convex.strict_antimono_of_deriv_neg` : if the derivative of a function is non-negative/non-positive/positive/negative, then the function is monotone/monotonically decreasing/strictly monotone/strictly monotonically decreasing. * `convex_on_of_deriv_mono`, `convex_on_of_deriv2_nonneg` : if the derivative of a function is increasing or its second derivative is nonnegative, then the original function is convex. * `strict_fderiv_of_cont_diff` : a C^1 function over the reals is strictly differentiable. (This is a corollary of the mean value inequality.) -/ /-! ### One-dimensional fencing inequalities -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_liminf_slope_right_lt_deriv_boundary' {f : ℝ → ℝ} {f' : ℝ → ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → ∀ (r : ℝ), f' x < r → filter.frequently (fun (z : ℝ) => z - x⁻¹ * (f z - f x) < r) (nhds_within x (set.Ioi x))) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → f x = B x → f' x < B' x) {x : ℝ} : x ∈ set.Icc a b → f x ≤ B x := sorry /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_liminf_slope_right_lt_deriv_boundary {f : ℝ → ℝ} {f' : ℝ → ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → ∀ (r : ℝ), f' x < r → filter.frequently (fun (z : ℝ) => z - x⁻¹ * (f z - f x) < r) (nhds_within x (set.Ioi x))) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ (x : ℝ), has_deriv_at B (B' x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → f x = B x → f' x < B' x) {x : ℝ} : x ∈ set.Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' hf hf' ha (fun (x : ℝ) (hx : x ∈ set.Icc a b) => continuous_at.continuous_within_at (has_deriv_at.continuous_at (hB x))) (fun (x : ℝ) (hx : x ∈ set.Ico a b) => has_deriv_at.has_deriv_within_at (hB x)) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(f z - f x) / (z - x)` is bounded above by `B'`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_liminf_slope_right_le_deriv_boundary {f : ℝ → ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → ∀ (r : ℝ), B' x < r → filter.frequently (fun (z : ℝ) => z - x⁻¹ * (f z - f x) < r) (nhds_within x (set.Ioi x))) {x : ℝ} : x ∈ set.Icc a b → f x ≤ B x := sorry /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has right derivative `B'` at every point of `[a, b)`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_deriv_right_lt_deriv_boundary' {f : ℝ → ℝ} {f' : ℝ → ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → f x = B x → f' x < B' x) {x : ℝ} : x ∈ set.Icc a b → f x ≤ B x := sorry /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x < B' x` whenever `f x = B x`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_deriv_right_lt_deriv_boundary {f : ℝ → ℝ} {f' : ℝ → ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : ∀ (x : ℝ), has_deriv_at B (B' x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → f x = B x → f' x < B' x) {x : ℝ} : x ∈ set.Icc a b → f x ≤ B x := image_le_of_deriv_right_lt_deriv_boundary' hf hf' ha (fun (x : ℝ) (hx : x ∈ set.Icc a b) => continuous_at.continuous_within_at (has_deriv_at.continuous_at (hB x))) (fun (x : ℝ) (hx : x ∈ set.Ico a b) => has_deriv_at.has_deriv_within_at (hB x)) bound /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `f a ≤ B a`; * `B` has derivative `B'` everywhere on `ℝ`; * `f` has right derivative `f'` at every point of `[a, b)`; * we have `f' x ≤ B' x` on `[a, b)`. Then `f x ≤ B x` everywhere on `[a, b]`. -/ theorem image_le_of_deriv_right_le_deriv_boundary {f : ℝ → ℝ} {f' : ℝ → ℝ} {a : ℝ} {b : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : f a ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → f' x ≤ B' x) {x : ℝ} : x ∈ set.Icc a b → f x ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary hf ha hB hB' fun (x : ℝ) (hx : x ∈ set.Ico a b) (r : ℝ) (hr : B' x < r) => has_deriv_within_at.liminf_right_slope_le (hf' x hx) (lt_of_le_of_lt (bound x hx) hr) /-! ### Vector-valued functions `f : ℝ → E` -/ /-- General fencing theorem for continuous functions with an estimate on the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `B` has right derivative at every point of `[a, b)`; * for each `x ∈ [a, b)` the right-side limit inferior of `(∥f z∥ - ∥f x∥) / (z - x)` is bounded above by a function `f'`; * we have `f' x < B' x` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. -/ theorem image_norm_le_of_liminf_right_slope_norm_lt_deriv_boundary {a : ℝ} {b : ℝ} {E : Type u_1} [normed_group E] {f : ℝ → E} {f' : ℝ → ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → ∀ (r : ℝ), f' x < r → filter.frequently (fun (z : ℝ) => z - x⁻¹ * (norm (f z) - norm (f x)) < r) (nhds_within x (set.Ioi x))) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : norm (f a) ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f x) = B x → f' x < B' x) {x : ℝ} : x ∈ set.Icc a b → norm (f x) ≤ B x := image_le_of_liminf_slope_right_lt_deriv_boundary' (continuous.comp_continuous_on continuous_norm hf) hf' ha hB hB' bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_lt_deriv_boundary' {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : norm (f a) ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f x) = B x → norm (f' x) < B' x) {x : ℝ} : x ∈ set.Icc a b → norm (f x) ≤ B x := sorry /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * the norm of `f'` is strictly less than `B'` whenever `∥f x∥ = B x`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_lt_deriv_boundary {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : norm (f a) ≤ B a) (hB : ∀ (x : ℝ), has_deriv_at B (B' x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f x) = B x → norm (f' x) < B' x) {x : ℝ} : x ∈ set.Icc a b → norm (f x) ≤ B x := image_norm_le_of_norm_deriv_right_lt_deriv_boundary' hf hf' ha (fun (x : ℝ) (hx : x ∈ set.Icc a b) => continuous_at.continuous_within_at (has_deriv_at.continuous_at (hB x))) (fun (x : ℝ) (hx : x ∈ set.Ico a b) => has_deriv_at.has_deriv_within_at (hB x)) bound /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` and `B` have right derivatives `f'` and `B'` respectively at every point of `[a, b)`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_le_deriv_boundary' {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : norm (f a) ≤ B a) (hB : continuous_on B (set.Icc a b)) (hB' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at B (B' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f' x) ≤ B' x) {x : ℝ} : x ∈ set.Icc a b → norm (f x) ≤ B x := image_le_of_liminf_slope_right_le_deriv_boundary (continuous.comp_continuous_on continuous_norm hf) ha hB hB' fun (x : ℝ) (hx : x ∈ set.Ico a b) (r : ℝ) (hr : B' x < r) => has_deriv_within_at.liminf_right_slope_norm_le (hf' x hx) (lt_of_le_of_lt (bound x hx) hr) /-- General fencing theorem for continuous functions with an estimate on the norm of the derivative. Let `f` and `B` be continuous functions on `[a, b]` such that * `∥f a∥ ≤ B a`; * `f` has right derivative `f'` at every point of `[a, b)`; * `B` has derivative `B'` everywhere on `ℝ`; * we have `∥f' x∥ ≤ B x` everywhere on `[a, b)`. Then `∥f x∥ ≤ B x` everywhere on `[a, b]`. We use one-sided derivatives in the assumptions to make this theorem work for piecewise differentiable functions. -/ theorem image_norm_le_of_norm_deriv_right_le_deriv_boundary {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) {B : ℝ → ℝ} {B' : ℝ → ℝ} (ha : norm (f a) ≤ B a) (hB : ∀ (x : ℝ), has_deriv_at B (B' x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f' x) ≤ B' x) {x : ℝ} : x ∈ set.Icc a b → norm (f x) ≤ B x := image_norm_le_of_norm_deriv_right_le_deriv_boundary' hf hf' ha (fun (x : ℝ) (hx : x ∈ set.Icc a b) => continuous_at.continuous_within_at (has_deriv_at.continuous_at (hB x))) (fun (x : ℝ) (hx : x ∈ set.Ico a b) => has_deriv_at.has_deriv_within_at (hB x)) bound /-- A function on `[a, b]` with the norm of the right derivative bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`. -/ theorem norm_image_sub_le_of_norm_deriv_right_le_segment {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} {C : ℝ} (hf : continuous_on f (set.Icc a b)) (hf' : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f' x) ≤ C) (x : ℝ) (H : x ∈ set.Icc a b) : norm (f x - f a) ≤ C * (x - a) := sorry /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment' {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} {C : ℝ} (hf : ∀ (x : ℝ), x ∈ set.Icc a b → has_deriv_within_at f (f' x) (set.Icc a b) x) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (f' x) ≤ C) (x : ℝ) (H : x ∈ set.Icc a b) : norm (f x - f a) ≤ C * (x - a) := sorry /-- A function on `[a, b]` with the norm of the derivative within `[a, b]` bounded by `C` satisfies `∥f x - f a∥ ≤ C * (x - a)`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {C : ℝ} (hf : differentiable_on ℝ f (set.Icc a b)) (bound : ∀ (x : ℝ), x ∈ set.Ico a b → norm (deriv_within f (set.Icc a b) x) ≤ C) (x : ℝ) (H : x ∈ set.Icc a b) : norm (f x - f a) ≤ C * (x - a) := norm_image_sub_le_of_norm_deriv_le_segment' (fun (x : ℝ) (hx : x ∈ set.Icc a b) => differentiable_within_at.has_deriv_within_at (hf x hx)) bound /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `has_deriv_within_at` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01' {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {f' : ℝ → E} {C : ℝ} (hf : ∀ (x : ℝ), x ∈ set.Icc 0 1 → has_deriv_within_at f (f' x) (set.Icc 0 1) x) (bound : ∀ (x : ℝ), x ∈ set.Ico 0 1 → norm (f' x) ≤ C) : norm (f 1 - f 0) ≤ C := sorry /-- A function on `[0, 1]` with the norm of the derivative within `[0, 1]` bounded by `C` satisfies `∥f 1 - f 0∥ ≤ C`, `deriv_within` version. -/ theorem norm_image_sub_le_of_norm_deriv_le_segment_01 {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {C : ℝ} (hf : differentiable_on ℝ f (set.Icc 0 1)) (bound : ∀ (x : ℝ), x ∈ set.Ico 0 1 → norm (deriv_within f (set.Icc 0 1) x) ≤ C) : norm (f 1 - f 0) ≤ C := sorry theorem constant_of_has_deriv_right_zero {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} (hcont : continuous_on f (set.Icc a b)) (hderiv : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f 0 (set.Ici x) x) (x : ℝ) (H : x ∈ set.Icc a b) : f x = f a := sorry theorem constant_of_deriv_within_zero {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} (hdiff : differentiable_on ℝ f (set.Icc a b)) (hderiv : ∀ (x : ℝ), x ∈ set.Ico a b → deriv_within f (set.Icc a b) x = 0) (x : ℝ) (H : x ∈ set.Icc a b) : f x = f a := sorry /-- If two continuous functions on `[a, b]` have the same right derivative and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_has_deriv_right_eq {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {f' : ℝ → E} {g : ℝ → E} (derivf : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at f (f' x) (set.Ici x) x) (derivg : ∀ (x : ℝ), x ∈ set.Ico a b → has_deriv_within_at g (f' x) (set.Ici x) x) (fcont : continuous_on f (set.Icc a b)) (gcont : continuous_on g (set.Icc a b)) (hi : f a = g a) (y : ℝ) (H : y ∈ set.Icc a b) : f y = g y := sorry /-- If two differentiable functions on `[a, b]` have the same derivative within `[a, b]` everywhere on `[a, b)` and are equal at `a`, then they are equal everywhere on `[a, b]`. -/ theorem eq_of_deriv_within_eq {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : ℝ → E} {a : ℝ} {b : ℝ} {g : ℝ → E} (fdiff : differentiable_on ℝ f (set.Icc a b)) (gdiff : differentiable_on ℝ g (set.Icc a b)) (hderiv : set.eq_on (deriv_within f (set.Icc a b)) (deriv_within g (set.Icc a b)) (set.Ico a b)) (hi : f a = g a) (y : ℝ) (H : y ∈ set.Icc a b) : f y = g y := sorry /-! ### Vector-valued functions `f : E → F` -/ /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {x : E} {y : E} {f' : E → continuous_linear_map ℝ E F} (hf : ∀ (x : E), x ∈ s → has_fderiv_within_at f (f' x) s x) (bound : ∀ (x : E), x ∈ s → norm (f' x) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x) ≤ C * norm (y - x) := sorry /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_fderiv_within_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {f' : E → continuous_linear_map ℝ E F} (hf : ∀ (x : E), x ∈ s → has_fderiv_within_at f (f' x) s x) (bound : ∀ (x : E), x ∈ s → norm (f' x) ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := sorry /-- The mean value theorem on a convex set: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {x : E} {y : E} (hf : differentiable_on ℝ f s) (bound : ∀ (x : E), x ∈ s → norm (fderiv_within ℝ f s x) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x) ≤ C * norm (y - x) := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (fun (x : E) (hx : x ∈ s) => differentiable_within_at.has_fderiv_within_at (hf x hx)) bound hs xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_within_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} (hf : differentiable_on ℝ f s) (bound : ∀ (x : E), x ∈ s → norm (fderiv_within ℝ f s x) ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_fderiv_within_le (fun (x : E) (hx : x ∈ s) => differentiable_within_at.has_fderiv_within_at (hf x hx)) bound hs /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {x : E} {y : E} (hf : ∀ (x : E), x ∈ s → differentiable_at ℝ f x) (bound : ∀ (x : E), x ∈ s → norm (fderiv ℝ f x) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x) ≤ C * norm (y - x) := convex.norm_image_sub_le_of_norm_has_fderiv_within_le (fun (x : E) (hx : x ∈ s) => has_fderiv_at.has_fderiv_within_at (differentiable_at.has_fderiv_at (hf x hx))) bound hs xs ys /-- The mean value theorem on a convex set: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `fderiv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_fderiv_le {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} (hf : ∀ (x : E), x ∈ s → differentiable_at ℝ f x) (bound : ∀ (x : E), x ∈ s → norm (fderiv ℝ f x) ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_fderiv_within_le (fun (x : E) (hx : x ∈ s) => has_fderiv_at.has_fderiv_within_at (differentiable_at.has_fderiv_at (hf x hx))) bound hs /-- Variant of the mean value inequality on a convex set, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_fderiv_within_le' {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {x : E} {y : E} {f' : E → continuous_linear_map ℝ E F} {φ : continuous_linear_map ℝ E F} (hf : ∀ (x : E), x ∈ s → has_fderiv_within_at f (f' x) s x) (bound : ∀ (x : E), x ∈ s → norm (f' x - φ) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x - coe_fn φ (y - x)) ≤ C * norm (y - x) := sorry /-- Variant of the mean value inequality on a convex set. Version with `fderiv_within`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_within_le' {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {x : E} {y : E} {φ : continuous_linear_map ℝ E F} (hf : differentiable_on ℝ f s) (bound : ∀ (x : E), x ∈ s → norm (fderiv_within ℝ f s x - φ) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x - coe_fn φ (y - x)) ≤ C * norm (y - x) := convex.norm_image_sub_le_of_norm_has_fderiv_within_le' (fun (x : E) (hx : x ∈ s) => differentiable_within_at.has_fderiv_within_at (hf x hx)) bound hs xs ys /-- Variant of the mean value inequality on a convex set. Version with `fderiv`. -/ theorem convex.norm_image_sub_le_of_norm_fderiv_le' {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} {C : ℝ} {s : set E} {x : E} {y : E} {φ : continuous_linear_map ℝ E F} (hf : ∀ (x : E), x ∈ s → differentiable_at ℝ f x) (bound : ∀ (x : E), x ∈ s → norm (fderiv ℝ f x - φ) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x - coe_fn φ (y - x)) ≤ C * norm (y - x) := convex.norm_image_sub_le_of_norm_has_fderiv_within_le' (fun (x : E) (hx : x ∈ s) => has_fderiv_at.has_fderiv_within_at (differentiable_at.has_fderiv_at (hf x hx))) bound hs xs ys /-- If a function has zero Fréchet derivative at every point of a convex set, then it is a constant on this set. -/ theorem convex.is_const_of_fderiv_within_eq_zero {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {s : set E} (hs : convex s) {f : E → F} (hf : differentiable_on ℝ f s) (hf' : ∀ (x : E), x ∈ s → fderiv_within ℝ f s x = 0) {x : E} {y : E} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := sorry theorem is_const_of_fderiv_eq_zero {E : Type u_1} [normed_group E] [normed_space ℝ E] {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : E → F} (hf : differentiable ℝ f) (hf' : ∀ (x : E), fderiv ℝ f x = 0) (x : E) (y : E) : f x = f y := sorry /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `has_deriv_within`. -/ theorem convex.norm_image_sub_le_of_norm_has_deriv_within_le {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : ℝ → F} {f' : ℝ → F} {C : ℝ} {s : set ℝ} {x : ℝ} {y : ℝ} (hf : ∀ (x : ℝ), x ∈ s → has_deriv_within_at f (f' x) s x) (bound : ∀ (x : ℝ), x ∈ s → norm (f' x) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x) ≤ C * norm (y - x) := sorry /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `has_deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_has_deriv_within_le {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : ℝ → F} {f' : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : ∀ (x : ℝ), x ∈ s → has_deriv_within_at f (f' x) s x) (bound : ∀ (x : ℝ), x ∈ s → norm (f' x) ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := sorry /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function within this set is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv_within` -/ theorem convex.norm_image_sub_le_of_norm_deriv_within_le {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : ℝ → F} {C : ℝ} {s : set ℝ} {x : ℝ} {y : ℝ} (hf : differentiable_on ℝ f s) (bound : ∀ (x : ℝ), x ∈ s → norm (deriv_within f s x) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x) ≤ C * norm (y - x) := convex.norm_image_sub_le_of_norm_has_deriv_within_le (fun (x : ℝ) (hx : x ∈ s) => differentiable_within_at.has_deriv_within_at (hf x hx)) bound hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv_within` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_within_le {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : ℝ → F} {C : ℝ} {s : set ℝ} (hs : convex s) (hf : differentiable_on ℝ f s) (bound : ∀ (x : ℝ), x ∈ s → norm (deriv_within f s x) ≤ C) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_deriv_within_le hs (fun (x : ℝ) (hx : x ∈ s) => differentiable_within_at.has_deriv_within_at (hf x hx)) bound /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C`, then the function is `C`-Lipschitz. Version with `deriv`. -/ theorem convex.norm_image_sub_le_of_norm_deriv_le {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : ℝ → F} {C : ℝ} {s : set ℝ} {x : ℝ} {y : ℝ} (hf : ∀ (x : ℝ), x ∈ s → differentiable_at ℝ f x) (bound : ∀ (x : ℝ), x ∈ s → norm (deriv f x) ≤ C) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : norm (f y - f x) ≤ C * norm (y - x) := convex.norm_image_sub_le_of_norm_has_deriv_within_le (fun (x : ℝ) (hx : x ∈ s) => has_deriv_at.has_deriv_within_at (differentiable_at.has_deriv_at (hf x hx))) bound hs xs ys /-- The mean value theorem on a convex set in dimension 1: if the derivative of a function is bounded by `C` on `s`, then the function is `C`-Lipschitz on `s`. Version with `deriv` and `lipschitz_on_with`. -/ theorem convex.lipschitz_on_with_of_norm_deriv_le {F : Type u_2} [normed_group F] [normed_space ℝ F] {f : ℝ → F} {C : ℝ} {s : set ℝ} (hf : ∀ (x : ℝ), x ∈ s → differentiable_at ℝ f x) (bound : ∀ (x : ℝ), x ∈ s → norm (deriv f x) ≤ C) (hs : convex s) : lipschitz_on_with (nnreal.of_real C) f s := convex.lipschitz_on_with_of_norm_has_deriv_within_le hs (fun (x : ℝ) (hx : x ∈ s) => has_deriv_at.has_deriv_within_at (differentiable_at.has_deriv_at (hf x hx))) bound /-! ### Functions `[a, b] → ℝ`. -/ -- Declare all variables here to make sure they come in a correct order /-- Cauchy's Mean Value Theorem, `has_deriv_at` version. -/ theorem exists_ratio_has_deriv_at_eq_ratio_slope (f : ℝ → ℝ) (f' : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b) (hfc : continuous_on f (set.Icc a b)) (hff' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at f (f' x) x) (g : ℝ → ℝ) (g' : ℝ → ℝ) (hgc : continuous_on g (set.Icc a b)) (hgg' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at g (g' x) x) : ∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), (g b - g a) * f' c = (f b - f a) * g' c := sorry /-- Cauchy's Mean Value Theorem, extended `has_deriv_at` version. -/ theorem exists_ratio_has_deriv_at_eq_ratio_slope' (f : ℝ → ℝ) (f' : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b) (g : ℝ → ℝ) (g' : ℝ → ℝ) {lfa : ℝ} {lga : ℝ} {lfb : ℝ} {lgb : ℝ} (hff' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at f (f' x) x) (hgg' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at g (g' x) x) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds lfa)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds lga)) (hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds lfb)) (hgb : filter.tendsto g (nhds_within b (set.Iio b)) (nhds lgb)) : ∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), (lgb - lga) * f' c = (lfb - lfa) * g' c := sorry /-- Lagrange's Mean Value Theorem, `has_deriv_at` version -/ theorem exists_has_deriv_at_eq_slope (f : ℝ → ℝ) (f' : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b) (hfc : continuous_on f (set.Icc a b)) (hff' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at f (f' x) x) : ∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), f' c = (f b - f a) / (b - a) := sorry /-- Cauchy's Mean Value Theorem, `deriv` version. -/ theorem exists_ratio_deriv_eq_ratio_slope (f : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b) (hfc : continuous_on f (set.Icc a b)) (hfd : differentiable_on ℝ f (set.Ioo a b)) (g : ℝ → ℝ) (hgc : continuous_on g (set.Icc a b)) (hgd : differentiable_on ℝ g (set.Ioo a b)) : ∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), (g b - g a) * deriv f c = (f b - f a) * deriv g c := sorry /-- Cauchy's Mean Value Theorem, extended `deriv` version. -/ theorem exists_ratio_deriv_eq_ratio_slope' (f : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b) (g : ℝ → ℝ) {lfa : ℝ} {lga : ℝ} {lfb : ℝ} {lgb : ℝ} (hdf : differentiable_on ℝ f (set.Ioo a b)) (hdg : differentiable_on ℝ g (set.Ioo a b)) (hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds lfa)) (hga : filter.tendsto g (nhds_within a (set.Ioi a)) (nhds lga)) (hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds lfb)) (hgb : filter.tendsto g (nhds_within b (set.Iio b)) (nhds lgb)) : ∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), (lgb - lga) * deriv f c = (lfb - lfa) * deriv g c := sorry /-- Lagrange's Mean Value Theorem, `deriv` version. -/ theorem exists_deriv_eq_slope (f : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b) (hfc : continuous_on f (set.Icc a b)) (hfd : differentiable_on ℝ f (set.Ioo a b)) : ∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), deriv f c = (f b - f a) / (b - a) := sorry /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C < f'`, then `f` grows faster than `C * x` on `D`, i.e., `C * (y - x) < f y - f x` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.mul_sub_lt_image_sub_of_lt_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C : ℝ} (hf'_gt : ∀ (x : ℝ), x ∈ interior D → C < deriv f x) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x < y → C * (y - x) < f y - f x := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `C < f'`, then `f` grows faster than `C * x`, i.e., `C * (y - x) < f y - f x` whenever `x < y`. -/ theorem mul_sub_lt_image_sub_of_lt_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C : ℝ} (hf'_gt : ∀ (x : ℝ), C < deriv f x) {x : ℝ} {y : ℝ} (hxy : x < y) : C * (y - x) < f y - f x := convex.mul_sub_lt_image_sub_of_lt_deriv convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => hf'_gt x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `C ≤ f'`, then `f` grows at least as fast as `C * x` on `D`, i.e., `C * (y - x) ≤ f y - f x` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.mul_sub_le_image_sub_of_le_deriv {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C : ℝ} (hf'_ge : ∀ (x : ℝ), x ∈ interior D → C ≤ deriv f x) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x ≤ y → C * (y - x) ≤ f y - f x := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `C ≤ f'`, then `f` grows at least as fast as `C * x`, i.e., `C * (y - x) ≤ f y - f x` whenever `x ≤ y`. -/ theorem mul_sub_le_image_sub_of_le_deriv {f : ℝ → ℝ} (hf : differentiable ℝ f) {C : ℝ} (hf'_ge : ∀ (x : ℝ), C ≤ deriv f x) {x : ℝ} {y : ℝ} (hxy : x ≤ y) : C * (y - x) ≤ f y - f x := convex.mul_sub_le_image_sub_of_le_deriv convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => hf'_ge x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x, y ∈ D`, `x < y`. -/ theorem convex.image_sub_lt_mul_sub_of_deriv_lt {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C : ℝ} (lt_hf' : ∀ (x : ℝ), x ∈ interior D → deriv f x < C) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x < y → f y - f x < C * (y - x) := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' < C`, then `f` grows slower than `C * x` on `D`, i.e., `f y - f x < C * (y - x)` whenever `x < y`. -/ theorem image_sub_lt_mul_sub_of_deriv_lt {f : ℝ → ℝ} (hf : differentiable ℝ f) {C : ℝ} (lt_hf' : ∀ (x : ℝ), deriv f x < C) {x : ℝ} {y : ℝ} (hxy : x < y) : f y - f x < C * (y - x) := convex.image_sub_lt_mul_sub_of_deriv_lt convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => lt_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f' ≤ C`, then `f` grows at most as fast as `C * x` on `D`, i.e., `f y - f x ≤ C * (y - x)` whenever `x, y ∈ D`, `x ≤ y`. -/ theorem convex.image_sub_le_mul_sub_of_deriv_le {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) {C : ℝ} (le_hf' : ∀ (x : ℝ), x ∈ interior D → deriv f x ≤ C) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x ≤ y → f y - f x ≤ C * (y - x) := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `f' ≤ C`, then `f` grows at most as fast as `C * x`, i.e., `f y - f x ≤ C * (y - x)` whenever `x ≤ y`. -/ theorem image_sub_le_mul_sub_of_deriv_le {f : ℝ → ℝ} (hf : differentiable ℝ f) {C : ℝ} (le_hf' : ∀ (x : ℝ), deriv f x ≤ C) {x : ℝ} {y : ℝ} (hxy : x ≤ y) : f y - f x ≤ C * (y - x) := convex.image_sub_le_mul_sub_of_deriv_le convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => le_hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is positive, then `f` is a strictly monotonically increasing function on `D`. -/ theorem convex.strict_mono_of_deriv_pos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_pos : ∀ (x : ℝ), x ∈ interior D → 0 < deriv f x) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x < y → f x < f y := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is positive, then `f` is a strictly monotonically increasing function. -/ theorem strict_mono_of_deriv_pos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_pos : ∀ (x : ℝ), 0 < deriv f x) : strict_mono f := sorry /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonnegative, then `f` is a monotonically increasing function on `D`. -/ theorem convex.mono_of_deriv_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonneg : ∀ (x : ℝ), x ∈ interior D → 0 ≤ deriv f x) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x ≤ y → f x ≤ f y := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonnegative, then `f` is a monotonically increasing function. -/ theorem mono_of_deriv_nonneg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ (x : ℝ), 0 ≤ deriv f x) : monotone f := fun (x y : ℝ) (hxy : x ≤ y) => convex.mono_of_deriv_nonneg convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is negative, then `f` is a strictly monotonically decreasing function on `D`. -/ theorem convex.strict_antimono_of_deriv_neg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_neg : ∀ (x : ℝ), x ∈ interior D → deriv f x < 0) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x < y → f y < f x := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is negative, then `f` is a strictly monotonically decreasing function. -/ theorem strict_antimono_of_deriv_neg {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ (x : ℝ), deriv f x < 0) {x : ℝ} {y : ℝ} : x < y → f y < f x := fun (hxy : x < y) => convex.strict_antimono_of_deriv_neg convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => hf' x) x y trivial trivial hxy /-- Let `f` be a function continuous on a convex (or, equivalently, connected) subset `D` of the real line. If `f` is differentiable on the interior of `D` and `f'` is nonpositive, then `f` is a monotonically decreasing function on `D`. -/ theorem convex.antimono_of_deriv_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_nonpos : ∀ (x : ℝ), x ∈ interior D → deriv f x ≤ 0) (x : ℝ) (y : ℝ) (H : x ∈ D) : y ∈ D → x ≤ y → f y ≤ f x := sorry /-- Let `f : ℝ → ℝ` be a differentiable function. If `f'` is nonpositive, then `f` is a monotonically decreasing function. -/ theorem antimono_of_deriv_nonpos {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf' : ∀ (x : ℝ), deriv f x ≤ 0) {x : ℝ} {y : ℝ} : x ≤ y → f y ≤ f x := fun (hxy : x ≤ y) => convex.antimono_of_deriv_nonpos convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) (fun (x : ℝ) (_x : x ∈ interior set.univ) => hf' x) x y trivial trivial hxy /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is monotone on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv_mono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ (x y : ℝ), x ∈ interior D → y ∈ interior D → x ≤ y → deriv f x ≤ deriv f y) : convex_on D f := sorry /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is differentiable on its interior, and `f'` is antimonotone on the interior, then `f` is concave on `D`. -/ theorem concave_on_of_deriv_antimono {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'_mono : ∀ (x y : ℝ), x ∈ interior D → y ∈ interior D → x ≤ y → deriv f y ≤ deriv f x) : concave_on D f := sorry /-- If a function `f` is differentiable and `f'` is monotone on `ℝ` then `f` is convex. -/ theorem convex_on_univ_of_deriv_mono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_mono : monotone (deriv f)) : convex_on set.univ f := convex_on_of_deriv_mono convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) fun (x y : ℝ) (_x : x ∈ interior set.univ) (_x : y ∈ interior set.univ) (h : x ≤ y) => hf'_mono h /-- If a function `f` is differentiable and `f'` is antimonotone on `ℝ` then `f` is concave. -/ theorem concave_on_univ_of_deriv_antimono {f : ℝ → ℝ} (hf : differentiable ℝ f) (hf'_antimono : ∀ {a b : ℝ}, a ≤ b → deriv f b ≤ deriv f a) : concave_on set.univ f := concave_on_of_deriv_antimono convex_univ (continuous.continuous_on (differentiable.continuous hf)) (differentiable.differentiable_on hf) fun (x y : ℝ) (_x : x ∈ interior set.univ) (_x : y ∈ interior set.univ) (h : x ≤ y) => hf'_antimono h /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonnegative on the interior, then `f` is convex on `D`. -/ theorem convex_on_of_deriv2_nonneg {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonneg : ∀ (x : ℝ), x ∈ interior D → 0 ≤ nat.iterate deriv (bit0 1) f x) : convex_on D f := sorry /-- If a function `f` is continuous on a convex set `D ⊆ ℝ`, is twice differentiable on its interior, and `f''` is nonpositive on the interior, then `f` is concave on `D`. -/ theorem concave_on_of_deriv2_nonpos {D : set ℝ} (hD : convex D) {f : ℝ → ℝ} (hf : continuous_on f D) (hf' : differentiable_on ℝ f (interior D)) (hf'' : differentiable_on ℝ (deriv f) (interior D)) (hf''_nonpos : ∀ (x : ℝ), x ∈ interior D → nat.iterate deriv (bit0 1) f x ≤ 0) : concave_on D f := sorry /-- If a function `f` is twice differentiable on a open convex set `D ⊆ ℝ` and `f''` is nonnegative on `D`, then `f` is convex on `D`. -/ theorem convex_on_open_of_deriv2_nonneg {D : set ℝ} (hD : convex D) (hD₂ : is_open D) {f : ℝ → ℝ} (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D) (hf''_nonneg : ∀ (x : ℝ), x ∈ D → 0 ≤ nat.iterate deriv (bit0 1) f x) : convex_on D f := sorry /-- If a function `f` is twice differentiable on a open convex set `D ⊆ ℝ` and `f''` is nonpositive on `D`, then `f` is concave on `D`. -/ theorem concave_on_open_of_deriv2_nonpos {D : set ℝ} (hD : convex D) (hD₂ : is_open D) {f : ℝ → ℝ} (hf' : differentiable_on ℝ f D) (hf'' : differentiable_on ℝ (deriv f) D) (hf''_nonpos : ∀ (x : ℝ), x ∈ D → nat.iterate deriv (bit0 1) f x ≤ 0) : concave_on D f := sorry /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonnegative on `ℝ`, then `f` is convex on `ℝ`. -/ theorem convex_on_univ_of_deriv2_nonneg {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonneg : ∀ (x : ℝ), 0 ≤ nat.iterate deriv (bit0 1) f x) : convex_on set.univ f := convex_on_open_of_deriv2_nonneg convex_univ is_open_univ (differentiable.differentiable_on hf') (differentiable.differentiable_on hf'') fun (x : ℝ) (_x : x ∈ set.univ) => hf''_nonneg x /-- If a function `f` is twice differentiable on `ℝ`, and `f''` is nonpositive on `ℝ`, then `f` is concave on `ℝ`. -/ theorem concave_on_univ_of_deriv2_nonpos {f : ℝ → ℝ} (hf' : differentiable ℝ f) (hf'' : differentiable ℝ (deriv f)) (hf''_nonpos : ∀ (x : ℝ), nat.iterate deriv (bit0 1) f x ≤ 0) : concave_on set.univ f := concave_on_open_of_deriv2_nonpos convex_univ is_open_univ (differentiable.differentiable_on hf') (differentiable.differentiable_on hf'') fun (x : ℝ) (_x : x ∈ set.univ) => hf''_nonpos x /-! ### Functions `f : E → ℝ` -/ /-- Lagrange's Mean Value Theorem, applied to convex domains. -/ theorem domain_mvt {E : Type u_1} [normed_group E] [normed_space ℝ E] {f : E → ℝ} {s : set E} {x : E} {y : E} {f' : E → continuous_linear_map ℝ E ℝ} (hf : ∀ (x : E), x ∈ s → has_fderiv_within_at f (f' x) s x) (hs : convex s) (xs : x ∈ s) (ys : y ∈ s) : ∃ (z : E), ∃ (H : z ∈ segment x y), f y - f x = coe_fn (f' z) (y - x) := sorry -- parametrize segment -- derivative of pullback of f under parametrization -- apply 1-variable mean value theorem to pullback -- reinterpret on domain /-! ### Vector-valued functions `f : E → F`. Strict differentiability. A `C^1` function is strictly differentiable, when the field is `ℝ` or `ℂ`. This follows from the mean value inequality on balls, which is a particular case of the above results after restricting the scalars to `ℝ`. Note that it does not make sense to talk of a convex set over `ℂ`, but balls make sense and are enough. Many formulations of the mean value inequality could be generalized to balls over `ℝ` or `ℂ`. For now, we only include the ones that we need. -/ /-- Variant of the mean value inequality over `ℝ` or `ℂ`, on a ball, using a bound on the difference between the derivative and a fixed linear map, rather than a bound on the derivative itself. Version with `has_fderiv_within`. -/ theorem is_R_or_C.norm_image_sub_le_of_norm_has_fderiv_within_le' {𝕜 : Type u_3} [is_R_or_C 𝕜] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {H : Type u_5} [normed_group H] [normed_space 𝕜 H] {f : G → H} {C : ℝ} {x : G} {y : G} {z : G} {r : ℝ} {f' : G → continuous_linear_map 𝕜 G H} {φ : continuous_linear_map 𝕜 G H} (hf : ∀ (x : G), x ∈ metric.ball z r → has_fderiv_within_at f (f' x) (metric.ball z r) x) (bound : ∀ (x : G), x ∈ metric.ball z r → norm (f' x - φ) ≤ C) (xs : x ∈ metric.ball z r) (ys : y ∈ metric.ball z r) : norm (f y - f x - coe_fn φ (y - x)) ≤ C * norm (y - x) := sorry /-- Over the reals or the complexes, a continuously differentiable function is strictly differentiable. -/ theorem strict_fderiv_of_cont_diff {𝕜 : Type u_3} [is_R_or_C 𝕜] {G : Type u_4} [normed_group G] [normed_space 𝕜 G] {H : Type u_5} [normed_group H] [normed_space 𝕜 H] {f : G → H} {s : set G} {x : G} {f' : G → continuous_linear_map 𝕜 G H} (hf : ∀ (x : G), x ∈ s → has_fderiv_within_at f (f' x) s x) (hcont : continuous_on f' s) (hs : s ∈ nhds x) : has_strict_fderiv_at f (f' x) x := sorry
2aa7560e1590e03ffdc19eb1bf78342d2679aa69
367134ba5a65885e863bdc4507601606690974c1
/src/dynamics/circle/rotation_number/translation_number.lean
d037027e79c3be5e87372c3fd22e5f27036d730c
[ "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
39,613
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import analysis.specific_limits import order.iterate import order.semiconj_Sup import algebra.iterate_hom /-! # Translation number of a monotone real map that commutes with `x ↦ x + 1` Let `f : ℝ → ℝ` be a monotone map such that `f (x + 1) = f x + 1` for all `x`. Then the limit $$ \tau(f)=\lim_{n\to\infty}{f^n(x)-x}{n} $$ exists and does not depend on `x`. This number is called the *translation number* of `f`. Different authors use different notation for this number: `τ`, `ρ`, `rot`, etc In this file we define a structure `circle_deg1_lift` for bundled maps with these properties, define translation number of `f : circle_deg1_lift`, prove some estimates relating `f^n(x)-x` to `τ(f)`. In case of a continuous map `f` we also prove that `f` admits a point `x` such that `f^n(x)=x+m` if and only if `τ(f)=m/n`. Maps of this type naturally appear as lifts of orientation preserving circle homeomorphisms. More precisely, let `f` be an orientation preserving homeomorphism of the circle $S^1=ℝ/ℤ$, and consider a real number `a` such that `⟦a⟧ = f 0`, where `⟦⟧` means the natural projection `ℝ → ℝ/ℤ`. Then there exists a unique continuous function `F : ℝ → ℝ` such that `F 0 = a` and `⟦F x⟧ = f ⟦x⟧` for all `x` (this fact is not formalized yet). This function is strictly monotone, continuous, and satisfies `F (x + 1) = F x + 1`. The number `⟦τ F⟧ : ℝ / ℤ` is called the *rotation number* of `f`. It does not depend on the choice of `a`. ## Main definitions * `circle_deg1_lift`: a monotone map `f : ℝ → ℝ` such that `f (x + 1) = f x + 1` for all `x`; the type `circle_deg1_lift` is equipped with `lattice` and `monoid` structures; the multiplication is given by composition: `(f * g) x = f (g x)`. * `circle_deg1_lift.translation_number`: translation number of `f : circle_deg1_lift`. ## Main statements We prove the following properties of `circle_deg1_lift.translation_number`. * `circle_deg1_lift.translation_number_eq_of_dist_bounded`: if the distance between `(f^n) 0` and `(g^n) 0` is bounded from above uniformly in `n : ℕ`, then `f` and `g` have equal translation numbers. * `circle_deg1_lift.translation_number_eq_of_semiconj_by`: if two `circle_deg1_lift` maps `f`, `g` are semiconjugate by a `circle_deg1_lift` map, then `τ f = τ g`. * `circle_deg1_lift.translation_number_units_inv`: if `f` is an invertible `circle_deg1_lift` map (equivalently, `f` is a lift of an orientation-preserving circle homeomorphism), then the translation number of `f⁻¹` is the negative of the translation number of `f`. * `circle_deg1_lift.translation_number_mul_of_commute`: if `f` and `g` commute, then `τ (f * g) = τ f + τ g`. * `circle_deg1_lift.translation_number_eq_rat_iff`: the translation number of `f` is equal to a rational number `m / n` if and only if `(f^n) x = x + m` for some `x`. * `circle_deg1_lift.semiconj_of_bijective_of_translation_number_eq`: if `f` and `g` are two bijective `circle_deg1_lift` maps and their translation numbers are equal, then these maps are semiconjugate to each other. * `circle_deg1_lift.semiconj_of_group_action_of_forall_translation_number_eq`: let `f₁` and `f₂` be two actions of a group `G` on the circle by degree 1 maps (formally, `f₁` and `f₂` are two homomorphisms from `G →* circle_deg1_lift`). If the translation numbers of `f₁ g` and `f₂ g` are equal to each other for all `g : G`, then these two actions are semiconjugate by some `F : circle_deg1_lift`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. ## Notation We use a local notation `τ` for the translation number of `f : circle_deg1_lift`. ## Implementation notes We define the translation number of `f : circle_deg1_lift` to be the limit of the sequence `(f ^ (2 ^ n)) 0 / (2 ^ n)`, then prove that `((f ^ n) x - x) / n` tends to this number for any `x`. This way it is much easier to prove that the limit exists and basic properties of the limit. We define translation number for a wider class of maps `f : ℝ → ℝ` instead of lifts of orientation preserving circle homeomorphisms for two reasons: * non-strictly monotone circle self-maps with discontinuities naturally appear as Poincaré maps for some flows on the two-torus (e.g., one can take a constant flow and glue in a few Cherry cells); * definition and some basic properties still work for this class. ## References * [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes] ## TODO Here are some short-term goals. * Introduce a structure or a typeclass for lifts of circle homeomorphisms. We use `units circle_deg1_lift` for now, but it's better to have a dedicated type (or a typeclass?). * Prove that the `semiconj_by` relation on circle homeomorphisms is an equivalence relation. * Introduce `conditionally_complete_lattice` structure, use it in the proof of `circle_deg1_lift.semiconj_of_group_action_of_forall_translation_number_eq`. * Prove that the orbits of the irrational rotation are dense in the circle. Deduce that a homeomorphism with an irrational rotation is semiconjugate to the corresponding irrational translation by a continuous `circle_deg1_lift`. ## Tags circle homeomorphism, rotation number -/ open filter set function (hiding commute) open_locale topological_space classical /-! ### Definition and monoid structure -/ /-- A lift of a monotone degree one map `S¹ → S¹`. -/ structure circle_deg1_lift : Type := (to_fun : ℝ → ℝ) (monotone' : monotone to_fun) (map_add_one' : ∀ x, to_fun (x + 1) = to_fun x + 1) namespace circle_deg1_lift instance : has_coe_to_fun circle_deg1_lift := ⟨λ _, ℝ → ℝ, circle_deg1_lift.to_fun⟩ @[simp] lemma coe_mk (f h₁ h₂) : ⇑(mk f h₁ h₂) = f := rfl variables (f g : circle_deg1_lift) protected lemma monotone : monotone f := f.monotone' @[mono] lemma mono {x y} (h : x ≤ y) : f x ≤ f y := f.monotone h lemma strict_mono_iff_injective : strict_mono f ↔ injective f := f.monotone.strict_mono_iff_injective @[simp] lemma map_add_one : ∀ x, f (x + 1) = f x + 1 := f.map_add_one' @[simp] lemma map_one_add (x : ℝ) : f (1 + x) = 1 + f x := by rw [add_comm, map_add_one, add_comm] theorem coe_inj : ∀ ⦃f g : circle_deg1_lift ⦄, (f : ℝ → ℝ) = g → f = g := assume ⟨f, fm, fd⟩ ⟨g, gm, gd⟩ h, by congr; exact h @[ext] theorem ext ⦃f g : circle_deg1_lift ⦄ (h : ∀ x, f x = g x) : f = g := coe_inj $ funext h theorem ext_iff {f g : circle_deg1_lift} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ instance : monoid circle_deg1_lift := { mul := λ f g, { to_fun := f ∘ g, monotone' := f.monotone.comp g.monotone, map_add_one' := λ x, by simp [map_add_one] }, one := ⟨id, monotone_id, λ _, rfl⟩, mul_one := λ f, coe_inj $ function.comp.right_id f, one_mul := λ f, coe_inj $ function.comp.left_id f, mul_assoc := λ f₁ f₂ f₃, coe_inj rfl } instance : inhabited circle_deg1_lift := ⟨1⟩ @[simp] lemma coe_mul : ⇑(f * g) = f ∘ g := rfl lemma mul_apply (x) : (f * g) x = f (g x) := rfl @[simp] lemma coe_one : ⇑(1 : circle_deg1_lift) = id := rfl instance units_has_coe_to_fun : has_coe_to_fun (units circle_deg1_lift) := ⟨λ _, ℝ → ℝ, λ f, ⇑(f : circle_deg1_lift)⟩ @[simp, norm_cast] lemma units_coe (f : units circle_deg1_lift) : ⇑(f : circle_deg1_lift) = f := rfl @[simp] lemma units_inv_apply_apply (f : units circle_deg1_lift) (x : ℝ) : (f⁻¹ : units circle_deg1_lift) (f x) = x := by simp only [← units_coe, ← mul_apply, f.inv_mul, coe_one, id] @[simp] lemma units_apply_inv_apply (f : units circle_deg1_lift) (x : ℝ) : f ((f⁻¹ : units circle_deg1_lift) x) = x := by simp only [← units_coe, ← mul_apply, f.mul_inv, coe_one, id] /-- If a lift of a circle map is bijective, then it is an order automorphism of the line. -/ def to_order_iso : units circle_deg1_lift →* ℝ ≃o ℝ := { to_fun := λ f, { to_fun := f, inv_fun := ⇑(f⁻¹), left_inv := units_inv_apply_apply f, right_inv := units_apply_inv_apply f, map_rel_iff' := λ x y, ⟨λ h, by simpa using mono ↑(f⁻¹) h, mono f⟩ }, map_one' := rfl, map_mul' := λ f g, rfl } @[simp] lemma coe_to_order_iso (f : units circle_deg1_lift) : ⇑(to_order_iso f) = f := rfl @[simp] lemma coe_to_order_iso_symm (f : units circle_deg1_lift) : ⇑(to_order_iso f).symm = (f⁻¹ : units circle_deg1_lift) := rfl @[simp] lemma coe_to_order_iso_inv (f : units circle_deg1_lift) : ⇑(to_order_iso f)⁻¹ = (f⁻¹ : units circle_deg1_lift) := rfl lemma is_unit_iff_bijective {f : circle_deg1_lift} : is_unit f ↔ bijective f := ⟨λ ⟨u, h⟩, h ▸ (to_order_iso u).bijective, λ h, is_unit_unit { val := f, inv := { to_fun := (equiv.of_bijective f h).symm, monotone' := λ x y hxy, (f.strict_mono_iff_injective.2 h.1).le_iff_le.1 (by simp only [equiv.of_bijective_apply_symm_apply f h, hxy]), map_add_one' := λ x, h.1 $ by simp only [equiv.of_bijective_apply_symm_apply f, f.map_add_one] }, val_inv := ext $ equiv.of_bijective_apply_symm_apply f h, inv_val := ext $ equiv.of_bijective_symm_apply_apply f h }⟩ lemma coe_pow : ∀ n : ℕ, ⇑(f^n) = (f^[n]) | 0 := rfl | (n+1) := by {ext x, simp [coe_pow n, pow_succ'] } lemma semiconj_by_iff_semiconj {f g₁ g₂ : circle_deg1_lift} : semiconj_by f g₁ g₂ ↔ semiconj f g₁ g₂ := ext_iff lemma commute_iff_commute {f g : circle_deg1_lift} : commute f g ↔ function.commute f g := ext_iff /-! ### Translate by a constant -/ /-- The map `y ↦ x + y` as a `circle_deg1_lift`. More precisely, we define a homomorphism from `multiplicative ℝ` to `units circle_deg1_lift`, so the translation by `x` is `translation (multiplicative.of_add x)`. -/ def translate : multiplicative ℝ →* units circle_deg1_lift := by refine (units.map _).comp to_units.to_monoid_hom; exact { to_fun := λ x, ⟨λ y, x.to_add + y, λ y₁ y₂ h, add_le_add_left h _, λ y, (add_assoc _ _ _).symm⟩, map_one' := ext $ zero_add, map_mul' := λ x y, ext $ add_assoc _ _ } @[simp] lemma translate_apply (x y : ℝ) : translate (multiplicative.of_add x) y = x + y := rfl @[simp] lemma translate_inv_apply (x y : ℝ) : (translate $ multiplicative.of_add x)⁻¹ y = -x + y := rfl @[simp] lemma translate_gpow (x : ℝ) (n : ℤ) : (translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ ↑n * x) := by simp only [← gsmul_eq_mul, of_add_gsmul, monoid_hom.map_gpow] @[simp] lemma translate_pow (x : ℝ) (n : ℕ) : (translate (multiplicative.of_add x))^n = translate (multiplicative.of_add $ ↑n * x) := translate_gpow x n @[simp] lemma translate_iterate (x : ℝ) (n : ℕ) : (translate (multiplicative.of_add x))^[n] = translate (multiplicative.of_add $ ↑n * x) := by rw [← units_coe, ← coe_pow, ← units.coe_pow, translate_pow, units_coe] /-! ### Commutativity with integer translations In this section we prove that `f` commutes with translations by an integer number. First we formulate these statements (for a natural or an integer number, addition on the left or on the right, addition or subtraction) using `function.commute`, then reformulate as `simp` lemmas `map_int_add` etc. -/ lemma commute_nat_add (n : ℕ) : function.commute f ((+) n) := by simpa only [nsmul_one, add_left_iterate] using function.commute.iterate_right f.map_one_add n lemma commute_add_nat (n : ℕ) : function.commute f (λ x, x + n) := by simp only [add_comm _ (n:ℝ), f.commute_nat_add n] lemma commute_sub_nat (n : ℕ) : function.commute f (λ x, x - n) := by simpa only [sub_eq_add_neg] using (f.commute_add_nat n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv lemma commute_add_int : ∀ n : ℤ, function.commute f (λ x, x + n) | (n:ℕ) := f.commute_add_nat n | -[1+n] := by simpa only [sub_eq_add_neg] using f.commute_sub_nat (n + 1) lemma commute_int_add (n : ℤ) : function.commute f ((+) n) := by simpa only [add_comm _ (n:ℝ)] using f.commute_add_int n lemma commute_sub_int (n : ℤ) : function.commute f (λ x, x - n) := by simpa only [sub_eq_add_neg] using (f.commute_add_int n).inverses_right (equiv.add_right _).right_inv (equiv.add_right _).left_inv @[simp] lemma map_int_add (m : ℤ) (x : ℝ) : f (m + x) = m + f x := f.commute_int_add m x @[simp] lemma map_add_int (x : ℝ) (m : ℤ) : f (x + m) = f x + m := f.commute_add_int m x @[simp] lemma map_sub_int (x : ℝ) (n : ℤ) : f (x - n) = f x - n := f.commute_sub_int n x @[simp] lemma map_add_nat (x : ℝ) (n : ℕ) : f (x + n) = f x + n := f.map_add_int x n @[simp] lemma map_nat_add (n : ℕ) (x : ℝ) : f (n + x) = n + f x := f.map_int_add n x @[simp] lemma map_sub_nat (x : ℝ) (n : ℕ) : f (x - n) = f x - n := f.map_sub_int x n lemma map_int_of_map_zero (n : ℤ) : f n = f 0 + n := by rw [← f.map_add_int, zero_add] @[simp] lemma map_fract_sub_fract_eq (x : ℝ) : f (fract x) - fract x = f x - x := by conv_rhs { rw [← fract_add_floor x, f.map_add_int, add_sub_comm, sub_self, add_zero] } /-! ### Pointwise order on circle maps -/ /-- Monotone circle maps form a lattice with respect to the pointwise order -/ noncomputable instance : lattice circle_deg1_lift := { sup := λ f g, { to_fun := λ x, max (f x) (g x), monotone' := λ x y h, max_le_max (f.mono h) (g.mono h), -- TODO: generalize to `monotone.max` map_add_one' := λ x, by simp [max_add_add_right] }, le := λ f g, ∀ x, f x ≤ g x, le_refl := λ f x, le_refl (f x), le_trans := λ f₁ f₂ f₃ h₁₂ h₂₃ x, le_trans (h₁₂ x) (h₂₃ x), le_antisymm := λ f₁ f₂ h₁₂ h₂₁, ext $ λ x, le_antisymm (h₁₂ x) (h₂₁ x), le_sup_left := λ f g x, le_max_left (f x) (g x), le_sup_right := λ f g x, le_max_right (f x) (g x), sup_le := λ f₁ f₂ f₃ h₁ h₂ x, max_le (h₁ x) (h₂ x), inf := λ f g, { to_fun := λ x, min (f x) (g x), monotone' := λ x y h, min_le_min (f.mono h) (g.mono h), map_add_one' := λ x, by simp [min_add_add_right] }, inf_le_left := λ f g x, min_le_left (f x) (g x), inf_le_right := λ f g x, min_le_right (f x) (g x), le_inf := λ f₁ f₂ f₃ h₂ h₃ x, le_min (h₂ x) (h₃ x) } @[simp] lemma sup_apply (x : ℝ) : (f ⊔ g) x = max (f x) (g x) := rfl @[simp] lemma inf_apply (x : ℝ) : (f ⊓ g) x = min (f x) (g x) := rfl lemma iterate_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^[n]) := λ f g h, f.monotone.iterate_le_of_le h _ lemma iterate_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^[n] ≤ (g^[n]) := iterate_monotone n h lemma pow_mono {f g : circle_deg1_lift} (h : f ≤ g) (n : ℕ) : f^n ≤ g^n := λ x, by simp only [coe_pow, iterate_mono h n x] lemma pow_monotone (n : ℕ) : monotone (λ f : circle_deg1_lift, f^n) := λ f g h, pow_mono h n /-! ### Estimates on `(f * g) 0` We prove the estimates `f 0 + ⌊g 0⌋ ≤ f (g 0) ≤ f 0 + ⌈g 0⌉` and some corollaries with added/removed floors and ceils. We also prove that for two semiconjugate maps `g₁`, `g₂`, the distance between `g₁ 0` and `g₂ 0` is less than two. -/ lemma map_le_of_map_zero (x : ℝ) : f x ≤ f 0 + ⌈x⌉ := calc f x ≤ f ⌈x⌉ : f.monotone $ le_ceil _ ... = f 0 + ⌈x⌉ : f.map_int_of_map_zero _ lemma map_map_zero_le : f (g 0) ≤ f 0 + ⌈g 0⌉ := f.map_le_of_map_zero (g 0) lemma floor_map_map_zero_le : ⌊f (g 0)⌋ ≤ ⌊f 0⌋ + ⌈g 0⌉ := calc ⌊f (g 0)⌋ ≤ ⌊f 0 + ⌈g 0⌉⌋ : floor_mono $ f.map_map_zero_le g ... = ⌊f 0⌋ + ⌈g 0⌉ : floor_add_int _ _ lemma ceil_map_map_zero_le : ⌈f (g 0)⌉ ≤ ⌈f 0⌉ + ⌈g 0⌉ := calc ⌈f (g 0)⌉ ≤ ⌈f 0 + ⌈g 0⌉⌉ : ceil_mono $ f.map_map_zero_le g ... = ⌈f 0⌉ + ⌈g 0⌉ : ceil_add_int _ _ lemma map_map_zero_lt : f (g 0) < f 0 + g 0 + 1 := calc f (g 0) ≤ f 0 + ⌈g 0⌉ : f.map_map_zero_le g ... < f 0 + (g 0 + 1) : add_lt_add_left (ceil_lt_add_one _) _ ... = f 0 + g 0 + 1 : (add_assoc _ _ _).symm lemma le_map_of_map_zero (x : ℝ) : f 0 + ⌊x⌋ ≤ f x := calc f 0 + ⌊x⌋ = f ⌊x⌋ : (f.map_int_of_map_zero _).symm ... ≤ f x : f.monotone $ floor_le _ lemma le_map_map_zero : f 0 + ⌊g 0⌋ ≤ f (g 0) := f.le_map_of_map_zero (g 0) lemma le_floor_map_map_zero : ⌊f 0⌋ + ⌊g 0⌋ ≤ ⌊f (g 0)⌋ := calc ⌊f 0⌋ + ⌊g 0⌋ = ⌊f 0 + ⌊g 0⌋⌋ : (floor_add_int _ _).symm ... ≤ ⌊f (g 0)⌋ : floor_mono $ f.le_map_map_zero g lemma le_ceil_map_map_zero : ⌈f 0⌉ + ⌊g 0⌋ ≤ ⌈(f * g) 0⌉ := calc ⌈f 0⌉ + ⌊g 0⌋ = ⌈f 0 + ⌊g 0⌋⌉ : (ceil_add_int _ _).symm ... ≤ ⌈f (g 0)⌉ : ceil_mono $ f.le_map_map_zero g lemma lt_map_map_zero : f 0 + g 0 - 1 < f (g 0) := calc f 0 + g 0 - 1 = f 0 + (g 0 - 1) : add_sub_assoc _ _ _ ... < f 0 + ⌊g 0⌋ : add_lt_add_left (sub_one_lt_floor _) _ ... ≤ f (g 0) : f.le_map_map_zero g lemma dist_map_map_zero_lt : dist (f 0 + g 0) (f (g 0)) < 1 := begin rw [dist_comm, real.dist_eq, abs_lt, lt_sub_iff_add_lt', sub_lt_iff_lt_add', ← sub_eq_add_neg], exact ⟨f.lt_map_map_zero g, f.map_map_zero_lt g⟩ end lemma dist_map_zero_lt_of_semiconj {f g₁ g₂ : circle_deg1_lift} (h : function.semiconj f g₁ g₂) : dist (g₁ 0) (g₂ 0) < 2 := calc dist (g₁ 0) (g₂ 0) ≤ dist (g₁ 0) (f (g₁ 0) - f 0) + dist _ (g₂ 0) : dist_triangle _ _ _ ... = dist (f 0 + g₁ 0) (f (g₁ 0)) + dist (g₂ 0 + f 0) (g₂ (f 0)) : by simp only [h.eq, real.dist_eq, sub_sub, add_comm (f 0), sub_sub_assoc_swap, abs_sub (g₂ (f 0))] ... < 2 : add_lt_add (f.dist_map_map_zero_lt g₁) (g₂.dist_map_map_zero_lt f) lemma dist_map_zero_lt_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (h : semiconj_by f g₁ g₂) : dist (g₁ 0) (g₂ 0) < 2 := dist_map_zero_lt_of_semiconj $ semiconj_by_iff_semiconj.1 h /-! ### Limits at infinities and continuity -/ protected lemma tendsto_at_bot : tendsto f at_bot at_bot := tendsto_at_bot_mono f.map_le_of_map_zero $ tendsto_at_bot_add_const_left _ _ $ tendsto_at_bot_mono (λ x, (ceil_lt_add_one x).le) $ tendsto_at_bot_add_const_right _ _ tendsto_id protected lemma tendsto_at_top : tendsto f at_top at_top := tendsto_at_top_mono f.le_map_of_map_zero $ tendsto_at_top_add_const_left _ _ $ tendsto_at_top_mono (λ x, (sub_one_lt_floor x).le) $ by simpa [sub_eq_add_neg] using tendsto_at_top_add_const_right _ _ tendsto_id lemma continuous_iff_surjective : continuous f ↔ function.surjective f := ⟨λ h, h.surjective f.tendsto_at_top f.tendsto_at_bot, f.monotone.continuous_of_surjective⟩ /-! ### Estimates on `(f^n) x` If we know that `f x` is `≤`/`<`/`≥`/`>`/`=` to `x + m`, then we have a similar estimate on `f^[n] x` and `x + n * m`. For `≤`, `≥`, and `=` we formulate both `of` (implication) and `iff` versions because implications work for `n = 0`. For `<` and `>` we formulate only `iff` versions. -/ lemma iterate_le_of_map_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) (n : ℕ) : f^[n] x ≤ x + n * m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_le_of_map_le f.monotone (monotone_id.add_const m) h n lemma le_iterate_of_add_int_le_map {x : ℝ} {m : ℤ} (h : x + m ≤ f x) (n : ℕ) : x + n * m ≤ (f^[n]) x := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).symm.iterate_le_of_map_le (monotone_id.add_const m) f.monotone h n lemma iterate_eq_of_map_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) (n : ℕ) : f^[n] x = x + n * m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_eq_of_map_eq n h lemma iterate_pos_le_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x ≤ x + n * m ↔ f x ≤ x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_le_iff_map_le f.monotone (strict_mono_id.add_const m) hn lemma iterate_pos_lt_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x < x + n * m ↔ f x < x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_lt_iff_map_lt f.monotone (strict_mono_id.add_const m) hn lemma iterate_pos_eq_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : f^[n] x = x + n * m ↔ f x = x + m := by simpa only [nsmul_eq_mul, add_right_iterate] using (f.commute_add_int m).iterate_pos_eq_iff_map_eq f.monotone (strict_mono_id.add_const m) hn lemma le_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + n * m ≤ (f^[n]) x ↔ x + m ≤ f x := by simpa only [not_lt] using not_congr (f.iterate_pos_lt_iff hn) lemma lt_iterate_pos_iff {x : ℝ} {m : ℤ} {n : ℕ} (hn : 0 < n) : x + n * m < (f^[n]) x ↔ x + m < f x := by simpa only [not_le] using not_congr (f.iterate_pos_le_iff hn) lemma mul_floor_map_zero_le_floor_iterate_zero (n : ℕ) : ↑n * ⌊f 0⌋ ≤ ⌊(f^[n] 0)⌋ := begin rw [le_floor, int.cast_mul, int.cast_coe_nat, ← zero_add ((n : ℝ) * _)], apply le_iterate_of_add_int_le_map, simp [floor_le] end /-! ### Definition of translation number -/ noncomputable theory /-- An auxiliary sequence used to define the translation number. -/ def transnum_aux_seq (n : ℕ) : ℝ := (f^(2^n)) 0 / 2^n /-- The translation number of a `circle_deg1_lift`, $τ(f)=\lim_{n→∞}\frac{f^n(x)-x}{n}$. We use an auxiliary sequence `\frac{f^{2^n}(0)}{2^n}` to define `τ(f)` because some proofs are simpler this way. -/ def translation_number : ℝ := lim at_top f.transnum_aux_seq -- TODO: choose two different symbols for `circle_deg1_lift.translation_number` and the future -- `circle_mono_homeo.rotation_number`, then make them `localized notation`s local notation `τ` := translation_number lemma transnum_aux_seq_def : f.transnum_aux_seq = λ n : ℕ, (f^(2^n)) 0 / 2^n := rfl lemma translation_number_eq_of_tendsto_aux {τ' : ℝ} (h : tendsto f.transnum_aux_seq at_top (𝓝 τ')) : τ f = τ' := h.lim_eq lemma translation_number_eq_of_tendsto₀ {τ' : ℝ} (h : tendsto (λ n:ℕ, f^[n] 0 / n) at_top (𝓝 τ')) : τ f = τ' := f.translation_number_eq_of_tendsto_aux $ by simpa [(∘), transnum_aux_seq_def, coe_pow] using h.comp (nat.tendsto_pow_at_top_at_top_of_one_lt one_lt_two) lemma translation_number_eq_of_tendsto₀' {τ' : ℝ} (h : tendsto (λ n:ℕ, f^[n + 1] 0 / (n + 1)) at_top (𝓝 τ')) : τ f = τ' := f.translation_number_eq_of_tendsto₀ $ (tendsto_add_at_top_iff_nat 1).1 h lemma transnum_aux_seq_zero : f.transnum_aux_seq 0 = f 0 := by simp [transnum_aux_seq] lemma transnum_aux_seq_dist_lt (n : ℕ) : dist (f.transnum_aux_seq n) (f.transnum_aux_seq (n+1)) < (1 / 2) / (2^n) := begin have : 0 < (2^(n+1):ℝ) := pow_pos zero_lt_two _, rw [div_div_eq_div_mul, ← pow_succ, ← abs_of_pos this], replace := abs_pos.2 (ne_of_gt this), convert (div_lt_div_right this).2 ((f^(2^n)).dist_map_map_zero_lt (f^(2^n))), simp_rw [transnum_aux_seq, real.dist_eq], rw [← abs_div, sub_div, pow_succ', pow_succ, ← two_mul, mul_div_mul_left _ _ (@two_ne_zero ℝ _ _), pow_mul, pow_two, mul_apply] end lemma tendsto_translation_number_aux : tendsto f.transnum_aux_seq at_top (𝓝 $ τ f) := (cauchy_seq_of_le_geometric_two 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n)).tendsto_lim lemma dist_map_zero_translation_number_le : dist (f 0) (τ f) ≤ 1 := f.transnum_aux_seq_zero ▸ dist_le_of_le_geometric_two_of_tendsto₀ 1 (λ n, le_of_lt $ f.transnum_aux_seq_dist_lt n) f.tendsto_translation_number_aux lemma tendsto_translation_number_of_dist_bounded_aux (x : ℕ → ℝ) (C : ℝ) (H : ∀ n : ℕ, dist ((f^n) 0) (x n) ≤ C) : tendsto (λ n : ℕ, x (2^n) / (2^n)) at_top (𝓝 $ τ f) := begin refine f.tendsto_translation_number_aux.congr_dist (squeeze_zero (λ _, dist_nonneg) _ _), { exact λ n, C / 2^n }, { intro n, have : 0 < (2^n:ℝ) := pow_pos zero_lt_two _, convert (div_le_div_right this).2 (H (2^n)), rw [transnum_aux_seq, real.dist_eq, ← sub_div, abs_div, abs_of_pos this, real.dist_eq] }, { exact mul_zero C ▸ tendsto_const_nhds.mul (tendsto_inv_at_top_zero.comp $ tendsto_pow_at_top_at_top_of_one_lt one_lt_two) } end lemma translation_number_eq_of_dist_bounded {f g : circle_deg1_lift} (C : ℝ) (H : ∀ n : ℕ, dist ((f^n) 0) ((g^n) 0) ≤ C) : τ f = τ g := eq.symm $ g.translation_number_eq_of_tendsto_aux $ f.tendsto_translation_number_of_dist_bounded_aux _ C H @[simp] lemma translation_number_one : τ 1 = 0 := translation_number_eq_of_tendsto₀ _ $ by simp [tendsto_const_nhds] lemma translation_number_eq_of_semiconj_by {f g₁ g₂ : circle_deg1_lift} (H : semiconj_by f g₁ g₂) : τ g₁ = τ g₂ := translation_number_eq_of_dist_bounded 2 $ λ n, le_of_lt $ dist_map_zero_lt_of_semiconj_by $ H.pow_right n lemma translation_number_eq_of_semiconj {f g₁ g₂ : circle_deg1_lift} (H : function.semiconj f g₁ g₂) : τ g₁ = τ g₂ := translation_number_eq_of_semiconj_by $ semiconj_by_iff_semiconj.2 H lemma translation_number_mul_of_commute {f g : circle_deg1_lift} (h : commute f g) : τ (f * g) = τ f + τ g := begin have : tendsto (λ n : ℕ, ((λ k, (f^k) 0 + (g^k) 0) (2^n)) / (2^n)) at_top (𝓝 $ τ f + τ g) := ((f.tendsto_translation_number_aux.add g.tendsto_translation_number_aux).congr $ λ n, (add_div ((f^(2^n)) 0) ((g^(2^n)) 0) ((2:ℝ)^n)).symm), refine tendsto_nhds_unique ((f * g).tendsto_translation_number_of_dist_bounded_aux _ 1 (λ n, _)) this, rw [h.mul_pow, dist_comm], exact le_of_lt ((f^n).dist_map_map_zero_lt (g^n)) end @[simp] lemma translation_number_units_inv (f : units circle_deg1_lift) : τ ↑(f⁻¹) = -τ f := eq_neg_iff_add_eq_zero.2 $ by simp [← translation_number_mul_of_commute (commute.refl _).units_inv_left] @[simp] lemma translation_number_pow : ∀ n : ℕ, τ (f^n) = n * τ f | 0 := by simp | (n+1) := by rw [pow_succ', translation_number_mul_of_commute (commute.pow_self f n), translation_number_pow n, nat.cast_add_one, add_mul, one_mul] @[simp] lemma translation_number_gpow (f : units circle_deg1_lift) : ∀ n : ℤ, τ (f ^ n : units _) = n * τ f | (n : ℕ) := by simp [translation_number_pow f n] | -[1+n] := by { simp, ring } @[simp] lemma translation_number_conj_eq (f : units circle_deg1_lift) (g : circle_deg1_lift) : τ (↑f * g * ↑(f⁻¹)) = τ g := (translation_number_eq_of_semiconj_by (f.mk_semiconj_by g)).symm @[simp] lemma translation_number_conj_eq' (f : units circle_deg1_lift) (g : circle_deg1_lift) : τ (↑(f⁻¹) * g * f) = τ g := translation_number_conj_eq f⁻¹ g lemma dist_pow_map_zero_mul_translation_number_le (n:ℕ) : dist ((f^n) 0) (n * f.translation_number) ≤ 1 := f.translation_number_pow n ▸ (f^n).dist_map_zero_translation_number_le lemma tendsto_translation_number₀' : tendsto (λ n:ℕ, (f^(n+1)) 0 / (n+1)) at_top (𝓝 $ τ f) := begin refine (tendsto_iff_dist_tendsto_zero.2 $ squeeze_zero (λ _, dist_nonneg) (λ n, _) ((tendsto_const_div_at_top_nhds_0_nat 1).comp (tendsto_add_at_top_nat 1))), dsimp, have : (0:ℝ) < n + 1 := n.cast_add_one_pos, rw [real.dist_eq, div_sub' _ _ _ (ne_of_gt this), abs_div, ← real.dist_eq, abs_of_pos this, div_le_div_right this, ← nat.cast_add_one], apply dist_pow_map_zero_mul_translation_number_le end lemma tendsto_translation_number₀ : tendsto (λ n:ℕ, ((f^n) 0) / n) at_top (𝓝 $ τ f) := (tendsto_add_at_top_iff_nat 1).1 f.tendsto_translation_number₀' /-- For any `x : ℝ` the sequence $\frac{f^n(x)-x}{n}$ tends to the translation number of `f`. In particular, this limit does not depend on `x`. -/ lemma tendsto_translation_number (x : ℝ) : tendsto (λ n:ℕ, ((f^n) x - x) / n) at_top (𝓝 $ τ f) := begin rw [← translation_number_conj_eq' (translate $ multiplicative.of_add x)], convert tendsto_translation_number₀ _, ext n, simp [sub_eq_neg_add, units.conj_pow'] end lemma tendsto_translation_number' (x : ℝ) : tendsto (λ n:ℕ, ((f^(n+1)) x - x) / (n+1)) at_top (𝓝 $ τ f) := (tendsto_add_at_top_iff_nat 1).2 (f.tendsto_translation_number x) lemma translation_number_mono : monotone τ := λ f g h, le_of_tendsto_of_tendsto' f.tendsto_translation_number₀ g.tendsto_translation_number₀ $ λ n, div_le_div_of_le_of_nonneg (pow_mono h n 0) n.cast_nonneg lemma translation_number_translate (x : ℝ) : τ (translate $ multiplicative.of_add x) = x := translation_number_eq_of_tendsto₀' _ $ by simp [nat.cast_add_one_ne_zero, mul_div_cancel_left, tendsto_const_nhds] lemma translation_number_le_of_le_add {z : ℝ} (hz : ∀ x, f x ≤ x + z) : τ f ≤ z := translation_number_translate z ▸ translation_number_mono (λ x, trans_rel_left _ (hz x) (add_comm _ _)) lemma le_translation_number_of_add_le {z : ℝ} (hz : ∀ x, x + z ≤ f x) : z ≤ τ f := translation_number_translate z ▸ translation_number_mono (λ x, trans_rel_right _ (add_comm _ _) (hz x)) lemma translation_number_le_of_le_add_int {x : ℝ} {m : ℤ} (h : f x ≤ x + m) : τ f ≤ m := le_of_tendsto' (f.tendsto_translation_number' x) $ λ n, (div_le_iff' (n.cast_add_one_pos : (0 : ℝ) < _)).mpr $ sub_le_iff_le_add'.2 $ (coe_pow f (n + 1)).symm ▸ f.iterate_le_of_map_le_add_int h (n + 1) lemma translation_number_le_of_le_add_nat {x : ℝ} {m : ℕ} (h : f x ≤ x + m) : τ f ≤ m := @translation_number_le_of_le_add_int f x m h lemma le_translation_number_of_add_int_le {x : ℝ} {m : ℤ} (h : x + m ≤ f x) : ↑m ≤ τ f := ge_of_tendsto' (f.tendsto_translation_number' x) $ λ n, (le_div_iff (n.cast_add_one_pos : (0 : ℝ) < _)).mpr $ le_sub_iff_add_le'.2 $ by simp only [coe_pow, mul_comm (m:ℝ), ← nat.cast_add_one, f.le_iterate_of_add_int_le_map h] lemma le_translation_number_of_add_nat_le {x : ℝ} {m : ℕ} (h : x + m ≤ f x) : ↑m ≤ τ f := @le_translation_number_of_add_int_le f x m h /-- If `f x - x` is an integer number `m` for some point `x`, then `τ f = m`. On the circle this means that a map with a fixed point has rotation number zero. -/ lemma translation_number_of_eq_add_int {x : ℝ} {m : ℤ} (h : f x = x + m) : τ f = m := le_antisymm (translation_number_le_of_le_add_int f $ le_of_eq h) (le_translation_number_of_add_int_le f $ le_of_eq h.symm) lemma floor_sub_le_translation_number (x : ℝ) : ↑⌊f x - x⌋ ≤ τ f := le_translation_number_of_add_int_le f $ le_sub_iff_add_le'.1 (floor_le $ f x - x) lemma translation_number_le_ceil_sub (x : ℝ) : τ f ≤ ⌈f x - x⌉ := translation_number_le_of_le_add_int f $ sub_le_iff_le_add'.1 (le_ceil $ f x - x) lemma map_lt_of_translation_number_lt_int {n : ℤ} (h : τ f < n) (x : ℝ) : f x < x + n := not_le.1 $ mt f.le_translation_number_of_add_int_le $ not_le.2 h lemma map_lt_of_translation_number_lt_nat {n : ℕ} (h : τ f < n) (x : ℝ) : f x < x + n := @map_lt_of_translation_number_lt_int f n h x lemma map_lt_add_floor_translation_number_add_one (x : ℝ) : f x < x + ⌊τ f⌋ + 1 := begin rw [add_assoc], norm_cast, refine map_lt_of_translation_number_lt_int _ _ _, push_cast, exact lt_floor_add_one _ end lemma map_lt_add_translation_number_add_one (x : ℝ) : f x < x + τ f + 1 := calc f x < x + ⌊τ f⌋ + 1 : f.map_lt_add_floor_translation_number_add_one x ... ≤ x + τ f + 1 : by { mono*, exact floor_le (τ f) } lemma lt_map_of_int_lt_translation_number {n : ℤ} (h : ↑n < τ f) (x : ℝ) : x + n < f x := not_le.1 $ mt f.translation_number_le_of_le_add_int $ not_le.2 h lemma lt_map_of_nat_lt_translation_number {n : ℕ} (h : ↑n < τ f) (x : ℝ) : x + n < f x := @lt_map_of_int_lt_translation_number f n h x /-- If `f^n x - x`, `n > 0`, is an integer number `m` for some point `x`, then `τ f = m / n`. On the circle this means that a map with a periodic orbit has a rational rotation number. -/ lemma translation_number_of_map_pow_eq_add_int {x : ℝ} {n : ℕ} {m : ℤ} (h : (f^n) x = x + m) (hn : 0 < n) : τ f = m / n := begin have := (f^n).translation_number_of_eq_add_int h, rwa [translation_number_pow, mul_comm, ← eq_div_iff] at this, exact nat.cast_ne_zero.2 (ne_of_gt hn) end /-- If a predicate depends only on `f x - x` and holds for all `0 ≤ x ≤ 1`, then it holds for all `x`. -/ lemma forall_map_sub_of_Icc (P : ℝ → Prop) (h : ∀ x ∈ Icc (0:ℝ) 1, P (f x - x)) (x : ℝ) : P (f x - x) := f.map_fract_sub_fract_eq x ▸ h _ ⟨fract_nonneg _, le_of_lt (fract_lt_one _)⟩ lemma translation_number_lt_of_forall_lt_add (hf : continuous f) {z : ℝ} (hz : ∀ x, f x < x + z) : τ f < z := begin obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f y - y ≤ f x - x, from compact_Icc.exists_forall_ge (nonempty_Icc.2 zero_le_one) (hf.sub continuous_id).continuous_on, refine lt_of_le_of_lt _ (sub_lt_iff_lt_add'.2 $ hz x), apply translation_number_le_of_le_add, simp only [← sub_le_iff_le_add'], exact f.forall_map_sub_of_Icc (λ a, a ≤ f x - x) hx end lemma lt_translation_number_of_forall_add_lt (hf : continuous f) {z : ℝ} (hz : ∀ x, x + z < f x) : z < τ f := begin obtain ⟨x, xmem, hx⟩ : ∃ x ∈ Icc (0:ℝ) 1, ∀ y ∈ Icc (0:ℝ) 1, f x - x ≤ f y - y, from compact_Icc.exists_forall_le (nonempty_Icc.2 zero_le_one) (hf.sub continuous_id).continuous_on, refine lt_of_lt_of_le (lt_sub_iff_add_lt'.2 $ hz x) _, apply le_translation_number_of_add_le, simp only [← le_sub_iff_add_le'], exact f.forall_map_sub_of_Icc _ hx end /-- If `f` is a continuous monotone map `ℝ → ℝ`, `f (x + 1) = f x + 1`, then there exists `x` such that `f x = x + τ f`. -/ lemma exists_eq_add_translation_number (hf : continuous f) : ∃ x, f x = x + τ f := begin obtain ⟨a, ha⟩ : ∃ x, f x ≤ x + f.translation_number, { by_contradiction H, push_neg at H, exact lt_irrefl _ (f.lt_translation_number_of_forall_add_lt hf H) }, obtain ⟨b, hb⟩ : ∃ x, x + τ f ≤ f x, { by_contradiction H, push_neg at H, exact lt_irrefl _ (f.translation_number_lt_of_forall_lt_add hf H) }, exact intermediate_value_univ₂ hf (continuous_id.add continuous_const) ha hb end lemma translation_number_eq_int_iff (hf : continuous f) {m : ℤ} : τ f = m ↔ ∃ x, f x = x + m := begin refine ⟨λ h, h ▸ f.exists_eq_add_translation_number hf, _⟩, rintros ⟨x, hx⟩, exact f.translation_number_of_eq_add_int hx end lemma continuous_pow (hf : continuous f) (n : ℕ) : continuous ⇑(f^n : circle_deg1_lift) := by { rw coe_pow, exact hf.iterate n } lemma translation_number_eq_rat_iff (hf : continuous f) {m : ℤ} {n : ℕ} (hn : 0 < n) : τ f = m / n ↔ ∃ x, (f^n) x = x + m := begin rw [eq_div_iff, mul_comm, ← translation_number_pow]; [skip, exact ne_of_gt (nat.cast_pos.2 hn)], exact (f^n).translation_number_eq_int_iff (f.continuous_pow hf n) end /-- Consider two actions `f₁ f₂ : G →* circle_deg1_lift` of a group on the real line by lifts of orientation preserving circle homeomorphisms. Suppose that for each `g : G` the homeomorphisms `f₁ g` and `f₂ g` have equal rotation numbers. Then there exists `F : circle_deg1_lift` such that `F * f₁ g = f₂ g * F` for all `g : G`. This is a version of Proposition 5.4 from [Étienne Ghys, Groupes d'homeomorphismes du cercle et cohomologie bornee][ghys87:groupes]. -/ lemma semiconj_of_group_action_of_forall_translation_number_eq {G : Type*} [group G] (f₁ f₂ : G →* circle_deg1_lift) (h : ∀ g, τ (f₁ g) = τ (f₂ g)) : ∃ F : circle_deg1_lift, ∀ g, semiconj F (f₁ g) (f₂ g) := begin -- Equality of translation number guarantees that for each `x` -- the set `{f₂ g⁻¹ (f₁ g x) | g : G}` is bounded above. have : ∀ x, bdd_above (range $ λ g, f₂ g⁻¹ (f₁ g x)), { refine λ x, ⟨x + 2, _⟩, rintro _ ⟨g, rfl⟩, have : τ (f₂ g⁻¹) = -τ (f₂ g), by rw [← monoid_hom.coe_to_hom_units, monoid_hom.map_inv, translation_number_units_inv, monoid_hom.coe_to_hom_units], calc f₂ g⁻¹ (f₁ g x) ≤ f₂ g⁻¹ (x + τ (f₁ g) + 1) : mono _ (map_lt_add_translation_number_add_one _ _).le ... = f₂ g⁻¹ (x + τ (f₂ g)) + 1 : by rw [h, map_add_one] ... ≤ x + τ (f₂ g) + τ (f₂ g⁻¹) + 1 + 1 : by { mono, exact (map_lt_add_translation_number_add_one _ _).le } ... = x + 2 : by simp [this, bit0, add_assoc] }, -- We have a theorem about actions by `order_iso`, so we introduce auxiliary maps -- to `ℝ ≃o ℝ`. set F₁ := to_order_iso.comp f₁.to_hom_units, set F₂ := to_order_iso.comp f₂.to_hom_units, have hF₁ : ∀ g, ⇑(F₁ g) = f₁ g := λ _, rfl, have hF₂ : ∀ g, ⇑(F₂ g) = f₂ g := λ _, rfl, simp only [← hF₁, ← hF₂], -- Now we apply `cSup_div_semiconj` and go back to `f₁` and `f₂`. refine ⟨⟨_, λ x y hxy, _, λ x, _⟩, cSup_div_semiconj F₂ F₁ (λ x, _)⟩; simp only [hF₁, hF₂, ← monoid_hom.map_inv, coe_mk], { refine csupr_le_csupr (this y) (λ g, _), exact mono _ (mono _ hxy) }, { simp only [map_add_one], exact (map_csupr_of_continuous_at_of_monotone (continuous_at_id.add continuous_at_const) (monotone_id.add_const (1 : ℝ)) (this x)).symm }, { exact this x } end /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses arguments `f₁ f₂ : units circle_deg1_lift` to assume that `f₁` and `f₂` are homeomorphisms. -/ lemma units_semiconj_of_translation_number_eq {f₁ f₂ : units circle_deg1_lift} (h : τ f₁ = τ f₂) : ∃ F : circle_deg1_lift, semiconj F f₁ f₂ := begin have : ∀ n : multiplicative ℤ, τ ((units.coe_hom _).comp (gpowers_hom _ f₁) n) = τ ((units.coe_hom _).comp (gpowers_hom _ f₂) n), { intro n, simp [h] }, exact (semiconj_of_group_action_of_forall_translation_number_eq _ _ this).imp (λ F hF, hF (multiplicative.of_add 1)) end /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses assumptions `is_unit f₁` and `is_unit f₂` to assume that `f₁` and `f₂` are homeomorphisms. -/ lemma semiconj_of_is_unit_of_translation_number_eq {f₁ f₂ : circle_deg1_lift} (h₁ : is_unit f₁) (h₂ : is_unit f₂) (h : τ f₁ = τ f₂) : ∃ F : circle_deg1_lift, semiconj F f₁ f₂ := by { rcases ⟨h₁, h₂⟩ with ⟨⟨f₁, rfl⟩, ⟨f₂, rfl⟩⟩, exact units_semiconj_of_translation_number_eq h } /-- If two lifts of circle homeomorphisms have the same translation number, then they are semiconjugate by a `circle_deg1_lift`. This version uses assumptions `bijective f₁` and `bijective f₂` to assume that `f₁` and `f₂` are homeomorphisms. -/ lemma semiconj_of_bijective_of_translation_number_eq {f₁ f₂ : circle_deg1_lift} (h₁ : bijective f₁) (h₂ : bijective f₂) (h : τ f₁ = τ f₂) : ∃ F : circle_deg1_lift, semiconj F f₁ f₂ := semiconj_of_is_unit_of_translation_number_eq (is_unit_iff_bijective.2 h₁) (is_unit_iff_bijective.2 h₂) h end circle_deg1_lift
2aa7c1acb3770abb933316cfa92dc294dc61cca0
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/order/filter/default.lean
6d5a63a02bd84fdced0f6947f730234860a9a599
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
23
lean
import .basic .partial
8a05d2fb195c1d7a5195d6214bb0cfe98387542b
a721fe7446524f18ba361625fc01033d9c8b7a78
/src/principia/myset/mess.lean
c1c39af43227afe1b3e521c76360d309d1d96c26
[]
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
554
lean
import .basic import ..mynat.basic namespace hidden namespace myset universes u v w open myset open mynat variables {α : Type u} {β : Type v} {γ : Type w} def PPP: mynat → Type | 0 := mynat | (succ n) := PPP n → Prop def bigtype: Type := (Σ n: mynat, PPP n) structure bigtype2: Type := mk :: (n: mynat) (x: PPP n) def cartprod2 (I: Type u) (sets: I → Σ α: Type u, myset α): Type (u + 1) := {f: I → Σ α: Type u, α // ∀ i: I, ∃ hα: (f i).fst = (sets i).fst, (sets i).snd (hα.mp (f i).snd)} end myset end hidden
a7b72b39adaa54043f1cf5b673ef5861d6b29ecc
46125763b4dbf50619e8846a1371029346f4c3db
/src/data/pequiv.lean
138cb991040fa52406ede48742a1a97e8305d9d6
[ "Apache-2.0" ]
permissive
thjread/mathlib
a9d97612cedc2c3101060737233df15abcdb9eb1
7cffe2520a5518bba19227a107078d83fa725ddc
refs/heads/master
1,615,637,696,376
1,583,953,063,000
1,583,953,063,000
246,680,271
0
0
Apache-2.0
1,583,960,875,000
1,583,960,875,000
null
UTF-8
Lean
false
false
11,772
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 data.equiv.basic data.set.lattice tactic.tauto universes u v w x /-- A `pequiv` is a partial equivalence, a representation of a bijection between a subset of `α` and a subset of `β` -/ structure pequiv (α : Type u) (β : Type v) := (to_fun : α → option β) (inv_fun : β → option α) (inv : ∀ (a : α) (b : β), a ∈ inv_fun b ↔ b ∈ to_fun a) infixr ` ≃. `:25 := pequiv namespace pequiv variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} open function option instance : has_coe_to_fun (α ≃. β) := ⟨_, to_fun⟩ @[simp] lemma coe_mk_apply (f₁ : α → option β) (f₂ : β → option α) (h) (x : α) : (pequiv.mk f₁ f₂ h : α → option β) x = f₁ x := rfl @[ext] lemma ext : ∀ {f g : α ≃. β} (h : ∀ x, f x = g x), f = g | ⟨f₁, f₂, hf⟩ ⟨g₁, g₂, hg⟩ h := have h : f₁ = g₁, from funext h, have ∀ b, f₂ b = g₂ b, begin subst h, assume b, have hf := λ a, hf a b, have hg := λ a, hg a b, cases h : g₂ b with a, { simp only [h, option.not_mem_none, false_iff] at hg, simp only [hg, iff_false] at hf, rwa [option.eq_none_iff_forall_not_mem] }, { rw [← option.mem_def, hf, ← hg, h, option.mem_def] } end, by simp [*, funext_iff] lemma ext_iff {f g : α ≃. β} : f = g ↔ ∀ x, f x = g x := ⟨congr_fun ∘ congr_arg _, ext⟩ @[refl] protected def refl (α : Type*) : α ≃. α := { to_fun := some, inv_fun := some, inv := λ _ _, eq_comm } @[symm] protected def symm (f : α ≃. β) : β ≃. α := { to_fun := f.2, inv_fun := f.1, inv := λ _ _, (f.inv _ _).symm } lemma mem_iff_mem (f : α ≃. β) : ∀ {a : α} {b : β}, a ∈ f.symm b ↔ b ∈ f a := f.3 lemma eq_some_iff (f : α ≃. β) : ∀ {a : α} {b : β}, f.symm b = some a ↔ f a = some b := f.3 @[trans] protected def trans (f : α ≃. β) (g : β ≃. γ) : pequiv α γ := { to_fun := λ a, (f a).bind g, inv_fun := λ a, (g.symm a).bind f.symm, inv := λ a b, by simp [*, and.comm, eq_some_iff f, eq_some_iff g] at * } @[simp] lemma refl_apply (a : α) : pequiv.refl α a = some a := rfl @[simp] lemma symm_refl : (pequiv.refl α).symm = pequiv.refl α := rfl @[simp] lemma symm_refl_apply (a : α) : (pequiv.refl α).symm a = some a := rfl @[simp] lemma symm_symm (f : α ≃. β) : f.symm.symm = f := by cases f; refl @[simp] lemma symm_symm_apply (f : α ≃. β) (a : α) : f.symm.symm a = f a := by rw symm_symm lemma symm_injective : function.injective (@pequiv.symm α β) := injective_of_has_left_inverse ⟨_, symm_symm⟩ lemma trans_assoc (f : α ≃. β) (g : β ≃. γ) (h : γ ≃. δ) : (f.trans g).trans h = f.trans (g.trans h) := ext (λ _, option.bind_assoc _ _ _) lemma mem_trans (f : α ≃. β) (g : β ≃. γ) (a : α) (c : γ) : c ∈ f.trans g a ↔ ∃ b, b ∈ f a ∧ c ∈ g b := option.bind_eq_some' lemma trans_eq_some (f : α ≃. β) (g : β ≃. γ) (a : α) (c : γ) : f.trans g a = some c ↔ ∃ b, f a = some b ∧ g b = some c := option.bind_eq_some' lemma trans_eq_none (f : α ≃. β) (g : β ≃. γ) (a : α) : f.trans g a = none ↔ (∀ b c, b ∉ f a ∨ c ∉ g b) := by simp only [eq_none_iff_forall_not_mem, mem_trans]; push_neg; tauto @[simp] lemma refl_trans (f : α ≃. β) : (pequiv.refl α).trans f = f := by ext; dsimp [pequiv.trans]; refl @[simp] lemma trans_refl (f : α ≃. β) : f.trans (pequiv.refl β) = f := by ext; dsimp [pequiv.trans]; simp @[simp] lemma refl_trans_apply (f : α ≃. β) (a : α) : (pequiv.refl α).trans f a = f a := by rw refl_trans @[simp] lemma trans_refl_apply (f : α ≃. β) (a : α) : f.trans (pequiv.refl β) a = f a := by rw trans_refl protected lemma inj (f : α ≃. β) {a₁ a₂ : α} {b : β} (h₁ : b ∈ f a₁) (h₂ : b ∈ f a₂) : a₁ = a₂ := by rw ← mem_iff_mem at *; cases h : f.symm b; simp * at * lemma injective_of_forall_ne_is_some (f : α ≃. β) (a₂ : α) (h : ∀ (a₁ : α), a₁ ≠ a₂ → is_some (f a₁)) : injective f := injective_of_has_left_inverse ⟨λ b, option.rec_on b a₂ (λ b', option.rec_on (f.symm b') a₂ id), λ x, begin classical, cases hfx : f x, { have : x = a₂, from not_imp_comm.1 (h x) (hfx.symm ▸ by simp), simp [this] }, { simp only [hfx], rw [(eq_some_iff f).2 hfx], refl } end⟩ lemma injective_of_forall_is_some {f : α ≃. β} (h : ∀ (a : α), is_some (f a)) : injective f := (classical.em (nonempty α)).elim (λ hn, injective_of_forall_ne_is_some f (classical.choice hn) (λ a _, h a)) (λ hn x, (hn ⟨x⟩).elim) section of_set variables (s : set α) [decidable_pred s] def of_set (s : set α) [decidable_pred s] : α ≃. α := { to_fun := λ a, if a ∈ s then some a else none, inv_fun := λ a, if a ∈ s then some a else none, inv := λ a b, by split_ifs; finish [eq_comm] } lemma mem_of_set_self_iff {s : set α} [decidable_pred s] {a : α} : a ∈ of_set s a ↔ a ∈ s := by dsimp [of_set]; split_ifs; simp * lemma mem_of_set_iff {s : set α} [decidable_pred s] {a b : α} : a ∈ of_set s b ↔ a = b ∧ a ∈ s := by dsimp [of_set]; split_ifs; split; finish @[simp] lemma of_set_eq_some_iff {s : set α} {h : decidable_pred s} {a b : α} : of_set s b = some a ↔ a = b ∧ a ∈ s := mem_of_set_iff @[simp] lemma of_set_eq_some_self_iff {s : set α} {h : decidable_pred s} {a : α} : of_set s a = some a ↔ a ∈ s := mem_of_set_self_iff @[simp] lemma of_set_symm : (of_set s).symm = of_set s := rfl @[simp] lemma of_set_univ : of_set set.univ = pequiv.refl α := by ext; dsimp [of_set]; simp [eq_comm] @[simp] lemma of_set_eq_refl {s : set α} [decidable_pred s] : of_set s = pequiv.refl α ↔ s = set.univ := ⟨λ h, begin rw [set.eq_univ_iff_forall], intro, rw [← mem_of_set_self_iff, h], exact rfl end, λ h, by simp only [of_set_univ.symm, h]; congr⟩ end of_set lemma symm_trans_rev (f : α ≃. β) (g : β ≃. γ) : (f.trans g).symm = g.symm.trans f.symm := rfl lemma trans_symm (f : α ≃. β) : f.trans f.symm = of_set {a | (f a).is_some} := begin ext, dsimp [pequiv.trans], simp only [eq_some_iff f, option.is_some_iff_exists, option.mem_def, bind_eq_some', of_set_eq_some_iff], split, { rintros ⟨b, hb₁, hb₂⟩, exact ⟨pequiv.inj _ hb₂ hb₁, b, hb₂⟩ }, { simp {contextual := tt} } end lemma symm_trans (f : α ≃. β) : f.symm.trans f = of_set {b | (f.symm b).is_some} := symm_injective $ by simp [symm_trans_rev, trans_symm, -symm_symm] lemma trans_symm_eq_iff_forall_is_some {f : α ≃. β} : f.trans f.symm = pequiv.refl α ↔ ∀ a, is_some (f a) := by rw [trans_symm, of_set_eq_refl, set.eq_univ_iff_forall]; refl instance : lattice.has_bot (α ≃. β) := ⟨{ to_fun := λ _, none, inv_fun := λ _, none, inv := by simp }⟩ @[simp] lemma bot_apply (a : α) : (⊥ : α ≃. β) a = none := rfl @[simp] lemma symm_bot : (⊥ : α ≃. β).symm = ⊥ := rfl @[simp] lemma trans_bot (f : α ≃. β) : f.trans (⊥ : β ≃. γ) = ⊥ := by ext; dsimp [pequiv.trans]; simp @[simp] lemma bot_trans (f : β ≃. γ) : (⊥ : α ≃. β).trans f = ⊥ := by ext; dsimp [pequiv.trans]; simp lemma is_some_symm_get (f : α ≃. β) {a : α} (h : is_some (f a)) : is_some (f.symm (option.get h)) := is_some_iff_exists.2 ⟨a, by rw [f.eq_some_iff, some_get]⟩ section single variables [decidable_eq α] [decidable_eq β] [decidable_eq γ] def single (a : α) (b : β) : α ≃. β := { to_fun := λ x, if x = a then some b else none, inv_fun := λ x, if x = b then some a else none, inv := λ _ _, by simp; split_ifs; cc } lemma mem_single (a : α) (b : β) : b ∈ single a b a := if_pos rfl lemma mem_single_iff (a₁ a₂ : α) (b₁ b₂ : β) : b₁ ∈ single a₂ b₂ a₁ ↔ a₁ = a₂ ∧ b₁ = b₂ := by dsimp [single]; split_ifs; simp [*, eq_comm] @[simp] lemma symm_single (a : α) (b : β) : (single a b).symm = single b a := rfl @[simp] lemma single_apply (a : α) (b : β) : single a b a = some b := if_pos rfl @[simp] lemma symm_single_apply (a : α) (b : β) : (single a b).symm b = some a := by dsimp; simp lemma single_apply_of_ne {a₁ a₂ : α} (h : a₁ ≠ a₂) (b : β) : single a₁ b a₂ = none := if_neg h.symm lemma single_trans_of_mem (a : α) {b : β} {c : γ} {f : β ≃. γ} (h : c ∈ f b) : (single a b).trans f = single a c := begin ext, dsimp [single, pequiv.trans], split_ifs; simp * at * end lemma trans_single_of_mem {a : α} {b : β} (c : γ) {f : α ≃. β} (h : b ∈ f a) : f.trans (single b c) = single a c := symm_injective $ single_trans_of_mem _ ((mem_iff_mem f).2 h) @[simp] lemma single_trans_single (a : α) (b : β) (c : γ) : (single a b).trans (single b c) = single a c := single_trans_of_mem _ (mem_single _ _) @[simp] lemma single_subsingleton_eq_refl [subsingleton α] (a b : α) : single a b = pequiv.refl α := begin ext i j, dsimp [single], rw [if_pos (subsingleton.elim i a), subsingleton.elim i j, subsingleton.elim b j] end lemma trans_single_of_eq_none {b : β} (c : γ) {f : α ≃. β} (h : f.symm b = none) : f.trans (single b c) = ⊥ := begin ext, simp only [eq_none_iff_forall_not_mem, option.mem_def, f.eq_some_iff] at h, dsimp [pequiv.trans, single], simp, intros, split_ifs; simp * at * end lemma single_trans_of_eq_none (a : α) {b : β} {f : β ≃. γ} (h : f b = none) : (single a b).trans f = ⊥ := symm_injective $ trans_single_of_eq_none _ h lemma single_trans_single_of_ne {b₁ b₂ : β} (h : b₁ ≠ b₂) (a : α) (c : γ) : (single a b₁).trans (single b₂ c) = ⊥ := single_trans_of_eq_none _ (single_apply_of_ne h.symm _) end single section order open lattice instance : partial_order (α ≃. β) := { le := λ f g, ∀ (a : α) (b : β), b ∈ f a → b ∈ g a, le_refl := λ _ _ _, id, le_trans := λ f g h fg gh a b, (gh a b) ∘ (fg a b), le_antisymm := λ f g fg gf, ext begin assume a, cases h : g a with b, { exact eq_none_iff_forall_not_mem.2 (λ b hb, option.not_mem_none b $ h ▸ fg a b hb) }, { exact gf _ _ h } end } lemma le_def {f g : α ≃. β} : f ≤ g ↔ (∀ (a : α) (b : β), b ∈ f a → b ∈ g a) := iff.rfl instance : order_bot (α ≃. β) := { bot_le := λ _ _ _ h, (not_mem_none _ h).elim, ..pequiv.partial_order, ..pequiv.lattice.has_bot } instance [decidable_eq α] [decidable_eq β] : semilattice_inf_bot (α ≃. β) := { inf := λ f g, { to_fun := λ a, if f a = g a then f a else none, inv_fun := λ b, if f.symm b = g.symm b then f.symm b else none, inv := λ a b, begin have := @mem_iff_mem _ _ f a b, have := @mem_iff_mem _ _ g a b, split_ifs; finish end }, inf_le_left := λ _ _ _ _, by simp; split_ifs; cc, inf_le_right := λ _ _ _ _, by simp; split_ifs; cc, le_inf := λ f g h fg gh a b, begin have := fg a b, have := gh a b, simp [le_def], split_ifs; finish end, ..pequiv.lattice.order_bot } end order end pequiv namespace equiv variables {α : Type*} {β : Type*} {γ : Type*} def to_pequiv (f : α ≃ β) : α ≃. β := { to_fun := some ∘ f, inv_fun := some ∘ f.symm, inv := by simp [equiv.eq_symm_apply, eq_comm] } @[simp] lemma to_pequiv_refl : (equiv.refl α).to_pequiv = pequiv.refl α := rfl lemma to_pequiv_trans (f : α ≃ β) (g : β ≃ γ) : (f.trans g).to_pequiv = f.to_pequiv.trans g.to_pequiv := rfl lemma to_pequiv_symm (f : α ≃ β) : f.symm.to_pequiv = f.to_pequiv.symm := rfl lemma to_pequiv_apply (f : α ≃ β) (x : α) : f.to_pequiv x = some (f x) := rfl end equiv
79cd8c2e8924dee9861fb0b9461c096a72b501c1
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/algebra/group/basic.lean
cd43b0856a7856df475b5894873b04da6ef2cc21
[ "Apache-2.0" ]
permissive
troyjlee/mathlib
e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5
45e7eb8447555247246e3fe91c87066506c14875
refs/heads/master
1,689,248,035,046
1,629,470,528,000
1,629,470,528,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,636
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro -/ import algebra.group.defs import logic.function.basic /-! # Basic lemmas about semigroups, monoids, and groups This file lists various basic lemmas about semigroups, monoids, and groups. Most proofs are one-liners from the corresponding axioms. For the definitions of semigroups, monoids and groups, see `algebra/group/defs.lean`. -/ universe u section associative variables {α : Type u} (f : α → α → α) [is_associative α f] (x y : α) /-- Composing two associative operations of `f : α → α → α` on the left is equal to an associative operation on the left. -/ lemma comp_assoc_left : (f x) ∘ (f y) = (f (f x y)) := by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] } /-- Composing two associative operations of `f : α → α → α` on the right is equal to an associative operation on the right. -/ lemma comp_assoc_right : (λ z, f z x) ∘ (λ z, f z y) = (λ z, f z (f y x)) := by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] } end associative section semigroup variables {α : Type*} /-- Composing two multiplications on the left by `y` then `x` is equal to a multiplication on the left by `x * y`. -/ @[simp, to_additive "Composing two additions on the left by `y` then `x` is equal to a addition on the left by `x + y`."] lemma comp_mul_left [semigroup α] (x y : α) : ((*) x) ∘ ((*) y) = ((*) (x * y)) := comp_assoc_left _ _ _ /-- Composing two multiplications on the right by `y` and `x` is equal to a multiplication on the right by `y * x`. -/ @[simp, to_additive "Composing two additions on the right by `y` and `x` is equal to a addition on the right by `y + x`."] lemma comp_mul_right [semigroup α] (x y : α) : (* x) ∘ (* y) = (* (y * x)) := comp_assoc_right _ _ _ end semigroup section mul_one_class variables {M : Type u} [mul_one_class M] @[to_additive] lemma ite_mul_one {P : Prop} [decidable P] {a b : M} : ite P (a * b) 1 = ite P a 1 * ite P b 1 := by { by_cases h : P; simp [h], } @[to_additive] lemma eq_one_iff_eq_one_of_mul_eq_one {a b : M} (h : a * b = 1) : a = 1 ↔ b = 1 := by split; { rintro rfl, simpa using h } @[to_additive] lemma one_mul_eq_id : ((*) (1 : M)) = id := funext one_mul @[to_additive] lemma mul_one_eq_id : (* (1 : M)) = id := funext mul_one end mul_one_class section comm_semigroup variables {G : Type u} [comm_semigroup G] @[no_rsimp, to_additive] lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) := left_comm has_mul.mul mul_comm mul_assoc attribute [no_rsimp] add_left_comm @[to_additive] lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b := right_comm has_mul.mul mul_comm mul_assoc @[to_additive] theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) := by simp only [mul_left_comm, mul_assoc] end comm_semigroup local attribute [simp] mul_assoc sub_eq_add_neg section add_monoid variables {M : Type u} [add_monoid M] {a b c : M} @[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _ @[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 := by rw [bit1, bit0_zero, zero_add] end add_monoid section comm_monoid variables {M : Type u} [comm_monoid M] {x y z : M} @[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z := left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz end comm_monoid section left_cancel_monoid variables {M : Type u} [left_cancel_monoid M] {a b : M} @[simp, to_additive] lemma mul_right_eq_self : a * b = a ↔ b = 1 := calc a * b = a ↔ a * b = a * 1 : by rw mul_one ... ↔ b = 1 : mul_left_cancel_iff @[simp, to_additive] lemma self_eq_mul_right : a = a * b ↔ b = 1 := eq_comm.trans mul_right_eq_self end left_cancel_monoid section right_cancel_monoid variables {M : Type u} [right_cancel_monoid M] {a b : M} @[simp, to_additive] lemma mul_left_eq_self : a * b = b ↔ a = 1 := calc a * b = b ↔ a * b = 1 * b : by rw one_mul ... ↔ a = 1 : mul_right_cancel_iff @[simp, to_additive] lemma self_eq_mul_left : b = a * b ↔ a = 1 := eq_comm.trans mul_left_eq_self end right_cancel_monoid section div_inv_monoid variables {G : Type u} [div_inv_monoid G] @[to_additive] lemma inv_eq_one_div (x : G) : x⁻¹ = 1 / x := by rw [div_eq_mul_inv, one_mul] @[to_additive] lemma mul_one_div (x y : G) : x * (1 / y) = x / y := by rw [div_eq_mul_inv, one_mul, div_eq_mul_inv] lemma mul_div_assoc {a b c : G} : a * b / c = a * (b / c) := by rw [div_eq_mul_inv, div_eq_mul_inv, mul_assoc _ _ _] lemma mul_div_assoc' (a b c : G) : a * (b / c) = (a * b) / c := mul_div_assoc.symm @[simp, to_additive] lemma one_div (a : G) : 1 / a = a⁻¹ := (inv_eq_one_div a).symm end div_inv_monoid section group variables {G : Type u} [group G] {a b c : G} @[simp, to_additive] lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a := by simp [mul_assoc] @[simp, to_additive neg_zero] lemma one_inv : 1⁻¹ = (1 : G) := inv_eq_of_mul_eq_one (one_mul 1) @[to_additive] theorem left_inverse_inv (G) [group G] : function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) := inv_inv @[simp, to_additive] lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv @[simp, to_additive] lemma inv_surjective : function.surjective (has_inv.inv : G → G) := inv_involutive.surjective @[to_additive] lemma inv_injective : function.injective (has_inv.inv : G → G) := inv_involutive.injective @[simp, to_additive] theorem inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff @[simp, to_additive] lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b := by rw [← mul_assoc, mul_right_inv, one_mul] @[to_additive] theorem mul_left_surjective (a : G) : function.surjective ((*) a) := λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩ @[to_additive] theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) := λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩ @[simp, to_additive neg_add_rev] lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := inv_eq_of_mul_eq_one $ by simp @[to_additive] lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ := by simp [h] @[to_additive] lemma eq_inv_of_mul_eq_one (h : a * b = 1) : a = b⁻¹ := have a⁻¹ = b, from inv_eq_of_mul_eq_one h, by simp [this.symm] @[to_additive] lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ := by simp [h.symm] @[to_additive] lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c := by simp [h.symm] @[to_additive] lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c := by simp [h] @[to_additive] lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c := by simp [h] @[to_additive] lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c := by simp [h.symm] @[to_additive] lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c := by simp [h.symm, mul_inv_cancel_left] @[to_additive] lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c := by rw [h, mul_inv_cancel_left] @[to_additive] lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c := by simp [h] @[simp, to_additive] theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 := by rw [← @inv_inj _ _ a 1, one_inv] @[simp, to_additive] theorem one_eq_inv : 1 = a⁻¹ ↔ a = 1 := by rw [eq_comm, inv_eq_one] @[to_additive] theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 := not_congr inv_eq_one @[to_additive] theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ := ⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩ @[to_additive] theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a := eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm @[to_additive] theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ := ⟨eq_inv_of_mul_eq_one, λ h, by rw [h, mul_left_inv]⟩ @[to_additive] theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b := by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm] @[to_additive] theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 := mul_eq_one_iff_eq_inv.symm @[to_additive] theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 := mul_eq_one_iff_inv_eq.symm @[to_additive] theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive] theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive] theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive] theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ @[to_additive] theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inv] @[to_additive] theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b := by rw [mul_eq_one_iff_eq_inv, inv_inj] @[to_additive] lemma div_left_injective : function.injective (λ a, a / b) := by simpa only [div_eq_mul_inv] using λ a a' h, mul_left_injective (b⁻¹) h @[to_additive] lemma div_right_injective : function.injective (λ a, b / a) := by simpa only [div_eq_mul_inv] using λ a a' h, inv_injective (mul_right_injective b h) -- The unprimed version is used by `group_with_zero`. This is the preferred choice. -- See https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/.60div_one'.60 @[simp, to_additive sub_zero] lemma div_one' (a : G) : a / 1 = a := calc a / 1 = a * 1⁻¹ : div_eq_mul_inv a 1 ... = a * 1 : congr_arg _ one_inv ... = a : mul_one a end group section add_group variables {G : Type u} [add_group G] {a b c d : G} @[simp] lemma sub_self (a : G) : a - a = 0 := by rw [sub_eq_add_neg, add_right_neg a] @[simp] lemma sub_add_cancel (a b : G) : a - b + b = a := by rw [sub_eq_add_neg, neg_add_cancel_right a b] @[simp] lemma add_sub_cancel (a b : G) : a + b - b = a := by rw [sub_eq_add_neg, add_neg_cancel_right a b] lemma add_sub_assoc (a b c : G) : a + b - c = a + (b - c) := by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg] lemma eq_of_sub_eq_zero (h : a - b = 0) : a = b := calc a = a - b + b : (sub_add_cancel a b).symm ... = b : by rw [h, zero_add] lemma sub_ne_zero_of_ne (h : a ≠ b) : a - b ≠ 0 := mt eq_of_sub_eq_zero h @[simp] lemma sub_neg_eq_add (a b : G) : a - (-b) = a + b := by rw [sub_eq_add_neg, neg_neg] @[simp] lemma neg_sub (a b : G) : -(a - b) = b - a := neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, add_right_neg]) local attribute [simp] add_assoc lemma add_sub (a b c : G) : a + (b - c) = a + b - c := by simp lemma sub_add_eq_sub_sub_swap (a b c : G) : a - (b + c) = a - c - b := by simp @[simp] lemma add_sub_add_right_eq_sub (a b c : G) : (a + c) - (b + c) = a - b := by rw [sub_add_eq_sub_sub_swap]; simp lemma eq_sub_of_add_eq (h : a + c = b) : a = b - c := by simp [← h] lemma sub_eq_of_eq_add (h : a = c + b) : a - b = c := by simp [h] lemma eq_add_of_sub_eq (h : a - c = b) : a = b + c := by simp [← h] lemma add_eq_of_eq_sub (h : a = c - b) : a + b = c := by simp [h] @[simp] lemma sub_right_inj : a - b = a - c ↔ b = c := sub_right_injective.eq_iff @[simp] lemma sub_left_inj : b - a = c - a ↔ b = c := by { rw [sub_eq_add_neg, sub_eq_add_neg], exact add_left_inj _ } lemma sub_add_sub_cancel (a b c : G) : (a - b) + (b - c) = a - c := by rw [← add_sub_assoc, sub_add_cancel] lemma sub_sub_sub_cancel_right (a b c : G) : (a - c) - (b - c) = a - b := by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel] theorem sub_sub_assoc_swap : a - (b - c) = a + c - b := by simp theorem sub_eq_zero : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩ alias sub_eq_zero ↔ _ sub_eq_zero_of_eq theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b := not_congr sub_eq_zero @[simp] theorem sub_eq_self : a - b = a ↔ b = 0 := by rw [sub_eq_add_neg, add_right_eq_self, neg_eq_zero] theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b := by rw [sub_eq_add_neg, eq_add_neg_iff_add_eq] theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b := by rw [sub_eq_add_neg, add_neg_eq_iff_eq_add] theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d := by rw [← sub_eq_zero, H, sub_eq_zero] theorem left_inverse_sub_add_left (c : G) : function.left_inverse (λ x, x - c) (λ x, x + c) := assume x, add_sub_cancel x c theorem left_inverse_add_left_sub (c : G) : function.left_inverse (λ x, x + c) (λ x, x - c) := assume x, sub_add_cancel x c theorem left_inverse_add_right_neg_add (c : G) : function.left_inverse (λ x, c + x) (λ x, - c + x) := assume x, add_neg_cancel_left c x theorem left_inverse_neg_add_add_right (c : G) : function.left_inverse (λ x, - c + x) (λ x, c + x) := assume x, neg_add_cancel_left c x end add_group section comm_group variables {G : Type u} [comm_group G] @[to_additive neg_add] lemma mul_inv (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev, mul_comm] end comm_group section add_comm_group variables {G : Type u} [add_comm_group G] {a b c d : G} local attribute [simp] add_assoc add_comm add_left_comm sub_eq_add_neg lemma sub_add_eq_sub_sub (a b c : G) : a - (b + c) = a - b - c := by simp lemma neg_add_eq_sub (a b : G) : -a + b = b - a := by simp lemma sub_add_eq_add_sub (a b c : G) : a - b + c = a + c - b := by simp lemma sub_sub (a b c : G) : a - b - c = a - (b + c) := by simp lemma sub_add (a b c : G) : a - b + c = a - (b - c) := by simp @[simp] lemma add_sub_add_left_eq_sub (a b c : G) : (c + a) - (c + b) = a - b := by simp lemma eq_sub_of_add_eq' (h : c + a = b) : a = b - c := by simp [h.symm] lemma sub_eq_of_eq_add' (h : a = b + c) : a - b = c := begin simp [h], rw [add_left_comm], simp end lemma eq_add_of_sub_eq' (h : a - b = c) : a = b + c := by simp [h.symm] lemma add_eq_of_eq_sub' (h : b = c - a) : a + b = c := begin simp [h], rw [add_comm c, add_neg_cancel_left] end lemma sub_sub_self (a b : G) : a - (a - b) = b := begin simp, rw [add_comm b, add_neg_cancel_left] end lemma add_sub_comm (a b c d : G) : a + b - (c + d) = (a - c) + (b - d) := by simp lemma sub_eq_sub_add_sub (a b c : G) : a - b = c - b + (a - c) := begin simp, rw [add_left_comm c], simp end lemma neg_neg_sub_neg (a b : G) : - (-a - -b) = a - b := by simp @[simp] lemma sub_sub_cancel (a b : G) : a - (a - b) = b := sub_sub_self a b lemma sub_eq_neg_add (a b : G) : a - b = -b + a := by rw [sub_eq_add_neg, add_comm _ _] theorem neg_add' (a b : G) : -(a + b) = -a - b := by rw [sub_eq_add_neg, neg_add a b] @[simp] lemma neg_sub_neg (a b : G) : -a - -b = b - a := by simp [sub_eq_neg_add, add_comm] lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b := by rw [eq_sub_iff_add_eq, add_comm] lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c := by rw [sub_eq_iff_eq_add, add_comm] @[simp] lemma add_sub_cancel' (a b : G) : a + b - a = b := by rw [sub_eq_neg_add, neg_add_cancel_left] @[simp] lemma add_sub_cancel'_right (a b : G) : a + (b - a) = b := by rw [← add_sub_assoc, add_sub_cancel'] -- This lemma is in the `simp` set under the name `add_neg_cancel_comm_assoc`, -- defined in `algebra/group/commute` lemma add_add_neg_cancel'_right (a b : G) : a + (b + -a) = b := by rw [← sub_eq_add_neg, add_sub_cancel'_right a b] lemma sub_right_comm (a b c : G) : a - b - c = a - c - b := by { repeat { rw sub_eq_add_neg }, exact add_right_comm _ _ _ } @[simp] lemma add_add_sub_cancel (a b c : G) : (a + c) + (b - c) = a + b := by rw [add_assoc, add_sub_cancel'_right] @[simp] lemma sub_add_add_cancel (a b c : G) : (a - c) + (b + c) = a + b := by rw [add_left_comm, sub_add_cancel, add_comm] @[simp] lemma sub_add_sub_cancel' (a b c : G) : (a - b) + (c - a) = c - b := by rw add_comm; apply sub_add_sub_cancel @[simp] lemma add_sub_sub_cancel (a b c : G) : (a + b) - (a - c) = b + c := by rw [← sub_add, add_sub_cancel'] @[simp] lemma sub_sub_sub_cancel_left (a b c : G) : (c - a) - (c - b) = b - a := by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel] lemma sub_eq_sub_iff_add_eq_add : a - b = c - d ↔ a + d = c + b := begin rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'], simp only [add_comm, eq_comm] end lemma sub_eq_sub_iff_sub_eq_sub : a - b = c - d ↔ a - c = b - d := by rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, sub_eq_iff_eq_add', add_sub_assoc] end add_comm_group
58b0958e0b460cad6c7dd147d556f92878f00c78
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/special_functions/complex/circle.lean
51ca090e57ab15f3c28a38908b17d4fe5a723b59
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,966
lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import analysis.complex.circle import analysis.special_functions.complex.log /-! # Maps on the unit circle In this file we prove some basic lemmas about `exp_map_circle` and the restriction of `complex.arg` to the unit circle. These two maps define a local equivalence between `circle` and `ℝ`, see `circle.arg_local_equiv` and `circle.arg_equiv`, that sends the whole circle to `(-π, π]`. -/ open complex function set open_locale real namespace circle lemma injective_arg : injective (λ z : circle, arg z) := λ z w h, subtype.ext $ ext_abs_arg ((abs_coe_circle z).trans (abs_coe_circle w).symm) h @[simp] lemma arg_eq_arg {z w : circle} : arg z = arg w ↔ z = w := injective_arg.eq_iff end circle lemma arg_exp_map_circle {x : ℝ} (h₁ : -π < x) (h₂ : x ≤ π) : arg (exp_map_circle x) = x := by rw [exp_map_circle_apply, exp_mul_I, arg_cos_add_sin_mul_I ⟨h₁, h₂⟩] @[simp] lemma exp_map_circle_arg (z : circle) : exp_map_circle (arg z) = z := circle.injective_arg $ arg_exp_map_circle (neg_pi_lt_arg _) (arg_le_pi _) namespace circle /-- `complex.arg ∘ coe` and `exp_map_circle` define a local equivalence between `circle and `ℝ` with `source = set.univ` and `target = set.Ioc (-π) π`. -/ @[simps { fully_applied := ff }] noncomputable def arg_local_equiv : local_equiv circle ℝ := { to_fun := arg ∘ coe, inv_fun := exp_map_circle, source := univ, target := Ioc (-π) π, map_source' := λ z _, ⟨neg_pi_lt_arg _, arg_le_pi _⟩, map_target' := maps_to_univ _ _, left_inv' := λ z _, exp_map_circle_arg z, right_inv' := λ x hx, arg_exp_map_circle hx.1 hx.2 } /-- `complex.arg` and `exp_map_circle` define an equivalence between `circle and `(-π, π]`. -/ @[simps { fully_applied := ff }] noncomputable def arg_equiv : circle ≃ Ioc (-π) π := { to_fun := λ z, ⟨arg z, neg_pi_lt_arg _, arg_le_pi _⟩, inv_fun := exp_map_circle ∘ coe, left_inv := λ z, arg_local_equiv.left_inv trivial, right_inv := λ x, subtype.ext $ arg_local_equiv.right_inv x.2 } end circle lemma left_inverse_exp_map_circle_arg : left_inverse exp_map_circle (arg ∘ coe) := exp_map_circle_arg lemma inv_on_arg_exp_map_circle : inv_on (arg ∘ coe) exp_map_circle (Ioc (-π) π) univ := circle.arg_local_equiv.symm.inv_on lemma surj_on_exp_map_circle_neg_pi_pi : surj_on exp_map_circle (Ioc (-π) π) univ := circle.arg_local_equiv.symm.surj_on lemma exp_map_circle_eq_exp_map_circle {x y : ℝ} : exp_map_circle x = exp_map_circle y ↔ ∃ m : ℤ, x = y + m * (2 * π) := begin rw [subtype.ext_iff, exp_map_circle_apply, exp_map_circle_apply, exp_eq_exp_iff_exists_int], refine exists_congr (λ n, _), rw [← mul_assoc, ← add_mul, mul_left_inj' I_ne_zero, ← of_real_one, ← of_real_bit0, ← of_real_mul, ← of_real_int_cast, ← of_real_mul, ← of_real_add, of_real_inj] end lemma periodic_exp_map_circle : periodic exp_map_circle (2 * π) := λ z, exp_map_circle_eq_exp_map_circle.2 ⟨1, by rw [int.cast_one, one_mul]⟩ @[simp] lemma exp_map_circle_two_pi : exp_map_circle (2 * π) = 1 := periodic_exp_map_circle.eq.trans exp_map_circle_zero lemma exp_map_circle_sub_two_pi (x : ℝ) : exp_map_circle (x - 2 * π) = exp_map_circle x := periodic_exp_map_circle.sub_eq x lemma exp_map_circle_add_two_pi (x : ℝ) : exp_map_circle (x + 2 * π) = exp_map_circle x := periodic_exp_map_circle x /-- `exp_map_circle`, applied to a `real.angle`. -/ noncomputable def real.angle.exp_map_circle (θ : real.angle) : circle := periodic_exp_map_circle.lift θ @[simp] lemma real.angle.exp_map_circle_coe (x : ℝ) : real.angle.exp_map_circle x = exp_map_circle x := rfl @[simp] lemma real.angle.exp_map_circle_zero : real.angle.exp_map_circle 0 = 1 := by rw [←real.angle.coe_zero, real.angle.exp_map_circle_coe, exp_map_circle_zero] @[simp] lemma real.angle.exp_map_circle_neg (θ : real.angle) : real.angle.exp_map_circle (-θ) = (real.angle.exp_map_circle θ)⁻¹ := begin induction θ using real.angle.induction_on, simp_rw [←real.angle.coe_neg, real.angle.exp_map_circle_coe, exp_map_circle_neg] end @[simp] lemma real.angle.exp_map_circle_add (θ₁ θ₂ : real.angle) : real.angle.exp_map_circle (θ₁ + θ₂) = (real.angle.exp_map_circle θ₁) * (real.angle.exp_map_circle θ₂) := begin induction θ₁ using real.angle.induction_on, induction θ₂ using real.angle.induction_on, exact exp_map_circle_add θ₁ θ₂ end @[simp] lemma real.angle.arg_exp_map_circle (θ : real.angle) : (arg (real.angle.exp_map_circle θ) : real.angle) = θ := begin induction θ using real.angle.induction_on, rw [real.angle.exp_map_circle_coe, exp_map_circle_apply, exp_mul_I, ←of_real_cos, ←of_real_sin, ←real.angle.cos_coe, ←real.angle.sin_coe, arg_cos_add_sin_mul_I_coe_angle] end
ae1d05dc42d202ad5698fba2a46cb37319ff9f6b
316671bfe98dfae2abac483407a5ae2daca54b1b
/01_Equality/06_properties.lean
38d1d90f367b330ebcd635f608c8907bae183728
[]
no_license
chasedawson/cs-dm
9b38bd7b0bd411a9f7713f5d613b8a0b2fdcd28b
ac3cca3eadc56a6355c8e9a56a39ed7eb1db55cb
refs/heads/master
1,585,360,471,246
1,536,097,455,000
1,536,097,455,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,656
lean
/- HOMEWORK: Read, complete, submit. We've seen that equality is reflexive. That is, everything is equal to itself. It is also symmetric in the sense that if any value, a, is equal to some other value, b, i.e., if a = b, then b is also equal to a, i.e., b = a. What this means is we have an inference rule that both expresses the symmetric property of equality and allows us to compute a proof of b = a from any proof of a = b, no matter what a and b are (no matter what type, T, a and b have, and no matter what values they have of this type). -/ /- (T: Type) (a b: T) (ab: a = b) ------------------------------ (eq.symm) ba: b = a -/ -- Let's see it in action in five lines def a := 1 def b := 2 - 1 lemma ab : a = b := rfl #check ab -- a proof of a = b #check (eq.symm ab) -- a proof of b = a! /- This is a big success. We understand not only the property of being symmetric but that we can use symmetry to derive new proofs from proofs that we already have. In fact, eq.symm is a program that does just this derivation, as we see here! -/ /- Finally we come to the notion that equality is also transitive. That means that for any values, a, b, and c, if a = b, and if b = c, then it must be that consequently a = c as well. {T : Type}, { a b c: T }, ab: a = b, bc: b = c ------------ (eq.trans) ac: a = c That is, if given proofs of a = b and b = c, eq.symm constructs and returns a proof of a = c. Let's see it in action. We've already got variables a and b to work with. We need one more, c. -/ def c := 1 /- We've also already got a proof of a = b. It's easy to generate one of b = c. -/ lemma bc : b = c := rfl /- And now we can apply eq.trans to these two premise-proofs and it will construct and return a proof of the conclusion. The expression that applies eq.trans to these two proofs is (eq.trans ab bc). Now for the fun part! -/ #check eq.trans ab bc /- EXERCISE: First write a textual inference rule, let's call it eq_snart. It says that if T is any type; if a, b, and c are values of this type, T; and you are given proofs of a = b and c = b then you can derive a proof of a = c. -/ /- EXERCISE: Now "prove" that this rule is valid by implementing it as a program that, when given any argument values of the specified types, returns a proof of the specified type (of the conclusion). Hint: Use eq.trans to construct the proof of a = c. It's first argument will be ab, the proof that a = b. It's second argument has to be a proof of b = c for it to work to derive a proof of a = c; but all we've got is a proof of c = b (in the reverse order). How can we pass a second argument of type b = c to eq.trans, so that it can do its job, when we have at hand is a proof of c = b. Now a major hint: we already have a way to construct a proof of b = c from a proof of c = b. Just use it. Ignore the error message in the following incomplete code. The problem is simply that the definition is incomplete, due to the underscore placeholder. Replace the underscore with your answer. Leave parenthesis around your expression so that it gets evaluated as its own term. -/ def eq_snart { T : Type} { a b c: T } (ab: a = b) (cb: c = b) := eq.trans ab (_) /- EXERCISE: Use lean to implement a new rule that that, from a proof of c = b and a proof of b = a, derives a proof of a = c. Call the proof eq_snart' (why not, it sounds funny). -/ /- EXERCISE: Use eq_snart rather than eq.trans directly to prove a = c, given proofs of a = b and c = b. -/ lemma cb : c = b := rfl #check cb theorem aeqc : a = c := eq_snart _ _ /- In general, there are many ways to prove a given theorem. Each distinct proof is nevertheless an inhabitant of the type of the proposition that it proves, and each suffices as evidence to justify a truth judgment for the proposition. In many cases, the particular proof object that is used -/ /- SUMMARY: In this section (1) you first recalled that the equality relation is reflexive, symmetric, and transitive. (2) You saw that in Lean, these are not just abstract ideas; there are also inference rules that you can apply to to arguments of the right types to build proofs of new propositions. (3) You also saw that you can prove your own inference rules by writing programs that implement them! Such programs can use already accepted inference rules (such as eq.refl, eq.symm, eq.trans) in their implementations. Thereafter, the new rules are as good as the old, and can then also be used to construct proofs that might be needed. -/
7bed2c03e120764c83721bfa340825a155c2dd43
d9ed0fce1c218297bcba93e046cb4e79c83c3af8
/library/tools/super/misc_preprocessing.lean
6f03f68f8d1f6714e248a44eaa2f8b80c02987a9
[ "Apache-2.0" ]
permissive
leodemoura/lean_clone
005c63aa892a6492f2d4741ee3c2cb07a6be9d7f
cc077554b584d39bab55c360bc12a6fe7957afe6
refs/heads/master
1,610,506,475,484
1,482,348,354,000
1,482,348,543,000
77,091,586
0
0
null
null
null
null
UTF-8
Lean
false
false
1,012
lean
/- Copyright (c) 2016 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner -/ import .clause .prover_state open expr list monad namespace super meta def is_taut (c : clause) : tactic bool := do qf ← c^.open_constn c^.num_quants, return $ list.bor $ do l1 ← qf^.1^.get_lits, guard l1^.is_neg, l2 ← qf^.1^.get_lits, guard l2^.is_pos, [decidable.to_bool $ l1^.formula = l2^.formula] meta def tautology_removal_pre : prover unit := preprocessing_rule $ λnew, filter (λc, lift bnot $♯ is_taut c^.c) new meta def remove_duplicates : list derived_clause → list derived_clause | [] := [] | (c :: cs) := let (same_type, other_type) := partition (λc' : derived_clause, c'^.c^.type = c^.c^.type) cs in { c with sc := foldl score.min c^.sc (same_type^.for $ λc, c^.sc) } :: remove_duplicates other_type meta def remove_duplicates_pre : prover unit := preprocessing_rule $ λnew, return $ remove_duplicates new end super
0712cf87c2a9d6b6ec199370cda94923be296112
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/topology/subset_properties.lean
a3dd26c7e8779d427d82414b09f5e6080d20e14e
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
65,651
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import topology.bases import data.finset.order import data.set.accumulate /-! # Properties of subsets of topological spaces In this file we define various properties of subsets of a topological space, and some classes on topological spaces. ## Main definitions We define the following properties for sets in a topological space: * `is_compact`: each open cover has a finite subcover. This is defined in mathlib using filters. The main property of a compact set is `is_compact.elim_finite_subcover`. * `is_clopen`: a set that is both open and closed. * `is_irreducible`: a nonempty set that has contains no non-trivial pair of disjoint opens. See also the section below in the module doc. For each of these definitions (except for `is_clopen`), we also have a class stating that the whole space satisfies that property: `compact_space`, `irreducible_space` Furthermore, we have two more classes: * `locally_compact_space`: for every point `x`, every open neighborhood of `x` contains a compact neighborhood of `x`. The definition is formulated in terms of the neighborhood filter. * `sigma_compact_space`: a space that is the union of a countably many compact subspaces. ## On the definition of irreducible and connected sets/spaces In informal mathematics, irreducible spaces are assumed to be nonempty. We formalise the predicate without that assumption as `is_preirreducible`. In other words, the only difference is whether the empty space counts as irreducible. There are good reasons to consider the empty space to be “too simple to be simple” See also https://ncatlab.org/nlab/show/too+simple+to+be+simple, and in particular https://ncatlab.org/nlab/show/too+simple+to+be+simple#relationship_to_biased_definitions. -/ open set filter classical topological_space open_locale classical topological_space filter universes u v variables {α : Type u} {β : Type v} [topological_space α] {s t : set α} /- compact sets -/ section compact /-- A set `s` is compact if for every nontrivial filter `f` that contains `s`, there exists `a ∈ s` such that every set of `f` meets every neighborhood of `a`. -/ def is_compact (s : set α) := ∀ ⦃f⦄ [ne_bot f], f ≤ 𝓟 s → ∃a∈s, cluster_pt a f /-- The complement to a compact set belongs to a filter `f` if it belongs to each filter `𝓝 a ⊓ f`, `a ∈ s`. -/ lemma is_compact.compl_mem_sets (hs : is_compact s) {f : filter α} (hf : ∀ a ∈ s, sᶜ ∈ 𝓝 a ⊓ f) : sᶜ ∈ f := begin contrapose! hf, simp only [not_mem_iff_inf_principal_compl, compl_compl, inf_assoc, ← exists_prop] at hf ⊢, exact @hs _ hf inf_le_right end /-- The complement to a compact set belongs to a filter `f` if each `a ∈ s` has a neighborhood `t` within `s` such that `tᶜ` belongs to `f`. -/ lemma is_compact.compl_mem_sets_of_nhds_within (hs : is_compact s) {f : filter α} (hf : ∀ a ∈ s, ∃ t ∈ 𝓝[s] a, tᶜ ∈ f) : sᶜ ∈ f := begin refine hs.compl_mem_sets (λ a ha, _), rcases hf a ha with ⟨t, ht, hst⟩, replace ht := mem_inf_principal.1 ht, refine mem_inf_sets.2 ⟨_, ht, _, hst, _⟩, rintros x ⟨h₁, h₂⟩ hs, exact h₂ (h₁ hs) end /-- If `p : set α → Prop` is stable under restriction and union, and each point `x` of a compact set `s` has a neighborhood `t` within `s` such that `p t`, then `p s` holds. -/ @[elab_as_eliminator] lemma is_compact.induction_on {s : set α} (hs : is_compact s) {p : set α → Prop} (he : p ∅) (hmono : ∀ ⦃s t⦄, s ⊆ t → p t → p s) (hunion : ∀ ⦃s t⦄, p s → p t → p (s ∪ t)) (hnhds : ∀ x ∈ s, ∃ t ∈ 𝓝[s] x, p t) : p s := let f : filter α := { sets := {t | p tᶜ}, univ_sets := by simpa, sets_of_superset := λ t₁ t₂ ht₁ ht, hmono (compl_subset_compl.2 ht) ht₁, inter_sets := λ t₁ t₂ ht₁ ht₂, by simp [compl_inter, hunion ht₁ ht₂] } in have sᶜ ∈ f, from hs.compl_mem_sets_of_nhds_within (by simpa using hnhds), by simpa /-- The intersection of a compact set and a closed set is a compact set. -/ lemma is_compact.inter_right (hs : is_compact s) (ht : is_closed t) : is_compact (s ∩ t) := begin introsI f hnf hstf, obtain ⟨a, hsa, ha⟩ : ∃ a ∈ s, cluster_pt a f := hs (le_trans hstf (le_principal_iff.2 (inter_subset_left _ _))), have : a ∈ t := (ht.mem_of_nhds_within_ne_bot $ ha.mono $ le_trans hstf (le_principal_iff.2 (inter_subset_right _ _))), exact ⟨a, ⟨hsa, this⟩, ha⟩ end /-- The intersection of a closed set and a compact set is a compact set. -/ lemma is_compact.inter_left (ht : is_compact t) (hs : is_closed s) : is_compact (s ∩ t) := inter_comm t s ▸ ht.inter_right hs /-- The set difference of a compact set and an open set is a compact set. -/ lemma is_compact.diff (hs : is_compact s) (ht : is_open t) : is_compact (s \ t) := hs.inter_right (is_closed_compl_iff.mpr ht) /-- A closed subset of a compact set is a compact set. -/ lemma compact_of_is_closed_subset (hs : is_compact s) (ht : is_closed t) (h : t ⊆ s) : is_compact t := inter_eq_self_of_subset_right h ▸ hs.inter_right ht lemma is_compact.adherence_nhdset {f : filter α} (hs : is_compact s) (hf₂ : f ≤ 𝓟 s) (ht₁ : is_open t) (ht₂ : ∀a∈s, cluster_pt a f → a ∈ t) : t ∈ f := classical.by_cases mem_sets_of_eq_bot $ assume : f ⊓ 𝓟 tᶜ ≠ ⊥, let ⟨a, ha, (hfa : cluster_pt a $ f ⊓ 𝓟 tᶜ)⟩ := @@hs ⟨this⟩ $ inf_le_left_of_le hf₂ in have a ∈ t, from ht₂ a ha (hfa.of_inf_left), have tᶜ ∩ t ∈ 𝓝[tᶜ] a, from inter_mem_nhds_within _ (is_open.mem_nhds ht₁ this), have A : 𝓝[tᶜ] a = ⊥, from empty_in_sets_eq_bot.1 $ compl_inter_self t ▸ this, have 𝓝[tᶜ] a ≠ ⊥, from hfa.of_inf_right.ne, absurd A this lemma is_compact_iff_ultrafilter_le_nhds : is_compact s ↔ (∀f : ultrafilter α, ↑f ≤ 𝓟 s → ∃a∈s, ↑f ≤ 𝓝 a) := begin refine (forall_ne_bot_le_iff _).trans _, { rintro f g hle ⟨a, has, haf⟩, exact ⟨a, has, haf.mono hle⟩ }, { simp only [ultrafilter.cluster_pt_iff] } end alias is_compact_iff_ultrafilter_le_nhds ↔ is_compact.ultrafilter_le_nhds _ /-- For every open directed cover of a compact set, there exists a single element of the cover which itself includes the set. -/ lemma is_compact.elim_directed_cover {ι : Type v} [hι : nonempty ι] (hs : is_compact s) (U : ι → set α) (hUo : ∀i, is_open (U i)) (hsU : s ⊆ ⋃ i, U i) (hdU : directed (⊆) U) : ∃ i, s ⊆ U i := hι.elim $ λ i₀, is_compact.induction_on hs ⟨i₀, empty_subset _⟩ (λ s₁ s₂ hs ⟨i, hi⟩, ⟨i, subset.trans hs hi⟩) (λ s₁ s₂ ⟨i, hi⟩ ⟨j, hj⟩, let ⟨k, hki, hkj⟩ := hdU i j in ⟨k, union_subset (subset.trans hi hki) (subset.trans hj hkj)⟩) (λ x hx, let ⟨i, hi⟩ := mem_Union.1 (hsU hx) in ⟨U i, mem_nhds_within_of_mem_nhds (is_open.mem_nhds (hUo i) hi), i, subset.refl _⟩) /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover {ι : Type v} (hs : is_compact s) (U : ι → set α) (hUo : ∀i, is_open (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : finset ι, s ⊆ ⋃ i ∈ t, U i := hs.elim_directed_cover _ (λ t, is_open_bUnion $ λ i _, hUo i) (Union_eq_Union_finset U ▸ hsU) (directed_of_sup $ λ t₁ t₂ h, bUnion_subset_bUnion_left h) lemma is_compact.elim_nhds_subcover' (hs : is_compact s) (U : Π x ∈ s, set α) (hU : ∀ x ∈ s, U x ‹x ∈ s› ∈ 𝓝 x) : ∃ t : finset s, s ⊆ ⋃ x ∈ t, U (x : s) x.2 := (hs.elim_finite_subcover (λ x : s, interior (U x x.2)) (λ x, is_open_interior) (λ x hx, mem_Union.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 $ hU _ _⟩)).imp $ λ t ht, subset.trans ht $ bUnion_subset_bUnion_right $ λ _ _, interior_subset lemma is_compact.elim_nhds_subcover (hs : is_compact s) (U : α → set α) (hU : ∀ x ∈ s, U x ∈ 𝓝 x) : ∃ t : finset α, (∀ x ∈ t, x ∈ s) ∧ s ⊆ ⋃ x ∈ t, U x := let ⟨t, ht⟩ := hs.elim_nhds_subcover' (λ x _, U x) hU in ⟨t.image coe, λ x hx, let ⟨y, hyt, hyx⟩ := finset.mem_image.1 hx in hyx ▸ y.2, by rwa finset.set_bUnion_finset_image⟩ /-- For every family of closed sets whose intersection avoids a compact set, there exists a finite subfamily whose intersection avoids this compact set. -/ lemma is_compact.elim_finite_subfamily_closed {s : set α} {ι : Type v} (hs : is_compact s) (Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : s ∩ (⋂ i, Z i) = ∅) : ∃ t : finset ι, s ∩ (⋂ i ∈ t, Z i) = ∅ := let ⟨t, ht⟩ := hs.elim_finite_subcover (λ i, (Z i)ᶜ) (λ i, (hZc i).is_open_compl) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using ht⟩ /-- If `s` is a compact set in a topological space `α` and `f : ι → set α` is a locally finite family of sets, then `f i ∩ s` is nonempty only for a finitely many `i`. -/ lemma locally_finite.finite_nonempty_inter_compact {ι : Type*} {f : ι → set α} (hf : locally_finite f) {s : set α} (hs : is_compact s) : finite {i | (f i ∩ s).nonempty} := begin choose U hxU hUf using hf, rcases hs.elim_nhds_subcover U (λ x _, hxU x) with ⟨t, -, hsU⟩, refine (t.finite_to_set.bUnion (λ x _, hUf x)).subset _, rintro i ⟨x, hx⟩, rcases mem_bUnion_iff.1 (hsU hx.2) with ⟨c, hct, hcx⟩, exact mem_bUnion hct ⟨x, hx.1, hcx⟩ end /-- To show that a compact set intersects the intersection of a family of closed sets, it is sufficient to show that it intersects every finite subfamily. -/ lemma is_compact.inter_Inter_nonempty {s : set α} {ι : Type v} (hs : is_compact s) (Z : ι → set α) (hZc : ∀i, is_closed (Z i)) (hsZ : ∀ t : finset ι, (s ∩ ⋂ i ∈ t, Z i).nonempty) : (s ∩ ⋂ i, Z i).nonempty := begin simp only [← ne_empty_iff_nonempty] at hsZ ⊢, apply mt (hs.elim_finite_subfamily_closed Z hZc), push_neg, exact hsZ end /-- Cantor's intersection theorem: the intersection of a directed family of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_directed_nonempty_compact_closed {ι : Type v} [hι : nonempty ι] (Z : ι → set α) (hZd : directed (⊇) Z) (hZn : ∀ i, (Z i).nonempty) (hZc : ∀ i, is_compact (Z i)) (hZcl : ∀ i, is_closed (Z i)) : (⋂ i, Z i).nonempty := begin apply hι.elim, intro i₀, let Z' := λ i, Z i ∩ Z i₀, suffices : (⋂ i, Z' i).nonempty, { exact nonempty.mono (Inter_subset_Inter $ assume i, inter_subset_left (Z i) (Z i₀)) this }, rw ← ne_empty_iff_nonempty, intro H, obtain ⟨t, ht⟩ : ∃ (t : finset ι), ((Z i₀) ∩ ⋂ (i ∈ t), Z' i) = ∅, from (hZc i₀).elim_finite_subfamily_closed Z' (assume i, is_closed.inter (hZcl i) (hZcl i₀)) (by rw [H, inter_empty]), obtain ⟨i₁, hi₁⟩ : ∃ i₁ : ι, Z i₁ ⊆ Z i₀ ∧ ∀ i ∈ t, Z i₁ ⊆ Z' i, { rcases directed.finset_le hZd t with ⟨i, hi⟩, rcases hZd i i₀ with ⟨i₁, hi₁, hi₁₀⟩, use [i₁, hi₁₀], intros j hj, exact subset_inter (subset.trans hi₁ (hi j hj)) hi₁₀ }, suffices : ((Z i₀) ∩ ⋂ (i ∈ t), Z' i).nonempty, { rw ← ne_empty_iff_nonempty at this, contradiction }, refine nonempty.mono _ (hZn i₁), exact subset_inter hi₁.left (subset_bInter hi₁.right) end /-- Cantor's intersection theorem for sequences indexed by `ℕ`: the intersection of a decreasing sequence of nonempty compact closed sets is nonempty. -/ lemma is_compact.nonempty_Inter_of_sequence_nonempty_compact_closed (Z : ℕ → set α) (hZd : ∀ i, Z (i+1) ⊆ Z i) (hZn : ∀ i, (Z i).nonempty) (hZ0 : is_compact (Z 0)) (hZcl : ∀ i, is_closed (Z i)) : (⋂ i, Z i).nonempty := have Zmono : _, from @monotone_of_monotone_nat (order_dual _) _ Z hZd, have hZd : directed (⊇) Z, from directed_of_sup Zmono, have ∀ i, Z i ⊆ Z 0, from assume i, Zmono $ zero_le i, have hZc : ∀ i, is_compact (Z i), from assume i, compact_of_is_closed_subset hZ0 (hZcl i) (this i), is_compact.nonempty_Inter_of_directed_nonempty_compact_closed Z hZd hZn hZc hZcl /-- For every open cover of a compact set, there exists a finite subcover. -/ lemma is_compact.elim_finite_subcover_image {b : set β} {c : β → set α} (hs : is_compact s) (hc₁ : ∀i∈b, is_open (c i)) (hc₂ : s ⊆ ⋃i∈b, c i) : ∃b'⊆b, finite b' ∧ s ⊆ ⋃i∈b', c i := begin rcases hs.elim_finite_subcover (λ i, c i : b → set α) _ _ with ⟨d, hd⟩; [skip, simpa using hc₁, simpa using hc₂], refine ⟨↑(d.image coe), _, finset.finite_to_set _, _⟩; simp * end /-- A set `s` is compact if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem is_compact_of_finite_subfamily_closed (h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) : is_compact s := assume f hfn hfs, classical.by_contradiction $ assume : ¬ (∃x∈s, cluster_pt x f), have hf : ∀x∈s, 𝓝 x ⊓ f = ⊥, by simpa only [cluster_pt, not_exists, not_not, ne_bot_iff], have ¬ ∃x∈s, ∀t∈f.sets, x ∈ closure t, from assume ⟨x, hxs, hx⟩, have ∅ ∈ 𝓝 x ⊓ f, by rw [empty_in_sets_eq_bot, hf x hxs], let ⟨t₁, ht₁, t₂, ht₂, ht⟩ := by rw [mem_inf_sets] at this; exact this in have ∅ ∈ 𝓝[t₂] x, from (𝓝[t₂] x).sets_of_superset (inter_mem_inf_sets ht₁ (subset.refl t₂)) ht, have 𝓝[t₂] x = ⊥, by rwa [empty_in_sets_eq_bot] at this, by simp only [closure_eq_cluster_pts] at hx; exact (hx t₂ ht₂).ne this, let ⟨t, ht⟩ := h (λ i : f.sets, closure i.1) (λ i, is_closed_closure) (by simpa [eq_empty_iff_forall_not_mem, not_exists]) in have (⋂i∈t, subtype.val i) ∈ f, from t.Inter_mem_sets.2 $ assume i hi, i.2, have s ∩ (⋂i∈t, subtype.val i) ∈ f, from inter_mem_sets (le_principal_iff.1 hfs) this, have ∅ ∈ f, from mem_sets_of_superset this $ assume x ⟨hxs, hx⟩, let ⟨i, hit, hxi⟩ := (show ∃i ∈ t, x ∉ closure (subtype.val i), by { rw [eq_empty_iff_forall_not_mem] at ht, simpa [hxs, not_forall] using ht x }) in have x ∈ closure i.val, from subset_closure (mem_bInter_iff.mp hx i hit), show false, from hxi this, hfn.ne $ by rwa [empty_in_sets_eq_bot] at this /-- A set `s` is compact if for every open cover of `s`, there exists a finite subcover. -/ lemma is_compact_of_finite_subcover (h : Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) → s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) : is_compact s := is_compact_of_finite_subfamily_closed $ assume ι Z hZc hsZ, let ⟨t, ht⟩ := h (λ i, (Z i)ᶜ) (assume i, is_open_compl_iff.mpr $ hZc i) (by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using hsZ) in ⟨t, by simpa only [subset_def, not_forall, eq_empty_iff_forall_not_mem, mem_Union, exists_prop, mem_inter_eq, not_and, iff_self, mem_Inter, mem_compl_eq] using ht⟩ /-- A set `s` is compact if and only if for every open cover of `s`, there exists a finite subcover. -/ lemma is_compact_iff_finite_subcover : is_compact s ↔ (Π {ι : Type u} (U : ι → (set α)), (∀ i, is_open (U i)) → s ⊆ (⋃ i, U i) → (∃ (t : finset ι), s ⊆ (⋃ i ∈ t, U i))) := ⟨assume hs ι, hs.elim_finite_subcover, is_compact_of_finite_subcover⟩ /-- A set `s` is compact if and only if for every family of closed sets whose intersection avoids `s`, there exists a finite subfamily whose intersection avoids `s`. -/ theorem is_compact_iff_finite_subfamily_closed : is_compact s ↔ (Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → s ∩ (⋂ i, Z i) = ∅ → (∃ (t : finset ι), s ∩ (⋂ i ∈ t, Z i) = ∅)) := ⟨assume hs ι, hs.elim_finite_subfamily_closed, is_compact_of_finite_subfamily_closed⟩ @[simp] lemma is_compact_empty : is_compact (∅ : set α) := assume f hnf hsf, not.elim hnf.ne $ empty_in_sets_eq_bot.1 $ le_principal_iff.1 hsf @[simp] lemma is_compact_singleton {a : α} : is_compact ({a} : set α) := λ f hf hfa, ⟨a, rfl, cluster_pt.of_le_nhds' (hfa.trans $ by simpa only [principal_singleton] using pure_le_nhds a) hf⟩ lemma set.subsingleton.is_compact {s : set α} (hs : s.subsingleton) : is_compact s := subsingleton.induction_on hs is_compact_empty $ λ x, is_compact_singleton lemma set.finite.compact_bUnion {s : set β} {f : β → set α} (hs : finite s) (hf : ∀i ∈ s, is_compact (f i)) : is_compact (⋃i ∈ s, f i) := is_compact_of_finite_subcover $ assume ι U hUo hsU, have ∀i : subtype s, ∃t : finset ι, f i ⊆ (⋃ j ∈ t, U j), from assume ⟨i, hi⟩, (hf i hi).elim_finite_subcover _ hUo (calc f i ⊆ ⋃i ∈ s, f i : subset_bUnion_of_mem hi ... ⊆ ⋃j, U j : hsU), let ⟨finite_subcovers, h⟩ := axiom_of_choice this in by haveI : fintype (subtype s) := hs.fintype; exact let t := finset.bUnion finset.univ finite_subcovers in have (⋃i ∈ s, f i) ⊆ (⋃ i ∈ t, U i), from bUnion_subset $ assume i hi, calc f i ⊆ (⋃ j ∈ finite_subcovers ⟨i, hi⟩, U j) : (h ⟨i, hi⟩) ... ⊆ (⋃ j ∈ t, U j) : bUnion_subset_bUnion_left $ assume j hj, finset.mem_bUnion.mpr ⟨_, finset.mem_univ _, hj⟩, ⟨t, this⟩ lemma finset.compact_bUnion (s : finset β) {f : β → set α} (hf : ∀i ∈ s, is_compact (f i)) : is_compact (⋃i ∈ s, f i) := s.finite_to_set.compact_bUnion hf lemma compact_accumulate {K : ℕ → set α} (hK : ∀ n, is_compact (K n)) (n : ℕ) : is_compact (accumulate K n) := (finite_le_nat n).compact_bUnion $ λ k _, hK k lemma compact_Union {f : β → set α} [fintype β] (h : ∀i, is_compact (f i)) : is_compact (⋃i, f i) := by rw ← bUnion_univ; exact finite_univ.compact_bUnion (λ i _, h i) lemma set.finite.is_compact (hs : finite s) : is_compact s := bUnion_of_singleton s ▸ hs.compact_bUnion (λ _ _, is_compact_singleton) lemma finite_of_is_compact_of_discrete [discrete_topology α] (s : set α) (hs : is_compact s) : s.finite := begin have := hs.elim_finite_subcover (λ x : α, ({x} : set α)) (λ x, is_open_discrete _), simp only [set.subset_univ, forall_prop_of_true, set.Union_of_singleton] at this, rcases this with ⟨t, ht⟩, suffices : (⋃ (i : α) (H : i ∈ t), {i} : set α) = (t : set α), { rw this at ht, exact t.finite_to_set.subset ht }, ext x, simp only [exists_prop, set.mem_Union, set.mem_singleton_iff, exists_eq_right', finset.mem_coe] end lemma is_compact.union (hs : is_compact s) (ht : is_compact t) : is_compact (s ∪ t) := by rw union_eq_Union; exact compact_Union (λ b, by cases b; assumption) lemma is_compact.insert (hs : is_compact s) (a) : is_compact (insert a s) := is_compact_singleton.union hs /-- If `V : ι → set α` is a decreasing family of closed compact sets then any neighborhood of `⋂ i, V i` contains some `V i`. We assume each `V i` is compact *and* closed because `α` is not assumed to be Hausdorff. See `exists_subset_nhd_of_compact` for version assuming this. -/ lemma exists_subset_nhd_of_compact' {ι : Type*} [nonempty ι] {V : ι → set α} (hV : directed (⊇) V) (hV_cpct : ∀ i, is_compact (V i)) (hV_closed : ∀ i, is_closed (V i)) {U : set α} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := begin set Y := ⋂ i, V i, obtain ⟨W, hsubW, W_op, hWU⟩ : ∃ W, Y ⊆ W ∧ is_open W ∧ W ⊆ U, from exists_open_set_nhds hU, suffices : ∃ i, V i ⊆ W, { rcases this with ⟨i, hi⟩, refine ⟨i, set.subset.trans hi hWU⟩ }, by_contradiction H, push_neg at H, replace H : ∀ i, (V i ∩ Wᶜ).nonempty := λ i, set.inter_compl_nonempty_iff.mpr (H i), have : (⋂ i, V i ∩ Wᶜ).nonempty, { apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed _ _ H, { intro i, exact (hV_cpct i).inter_right W_op.is_closed_compl }, { intro i, apply (hV_closed i).inter W_op.is_closed_compl }, { intros i j, rcases hV i j with ⟨k, hki, hkj⟩, use k, split ; intro x ; simp only [and_imp, mem_inter_eq, mem_compl_eq] ; tauto } }, have : ¬ (⋂ (i : ι), V i) ⊆ W, by simpa [← Inter_inter, inter_compl_nonempty_iff], contradiction end /-- `filter.cocompact` is the filter generated by complements to compact sets. -/ def filter.cocompact (α : Type*) [topological_space α] : filter α := ⨅ (s : set α) (hs : is_compact s), 𝓟 (sᶜ) lemma filter.has_basis_cocompact : (filter.cocompact α).has_basis is_compact compl := has_basis_binfi_principal' (λ s hs t ht, ⟨s ∪ t, hs.union ht, compl_subset_compl.2 (subset_union_left s t), compl_subset_compl.2 (subset_union_right s t)⟩) ⟨∅, is_compact_empty⟩ lemma filter.mem_cocompact : s ∈ filter.cocompact α ↔ ∃ t, is_compact t ∧ tᶜ ⊆ s := filter.has_basis_cocompact.mem_iff.trans $ exists_congr $ λ t, exists_prop lemma filter.mem_cocompact' : s ∈ filter.cocompact α ↔ ∃ t, is_compact t ∧ sᶜ ⊆ t := filter.mem_cocompact.trans $ exists_congr $ λ t, and_congr_right $ λ ht, compl_subset_comm lemma is_compact.compl_mem_cocompact (hs : is_compact s) : sᶜ ∈ filter.cocompact α := filter.has_basis_cocompact.mem_of_mem hs section tube_lemma variables [topological_space β] /-- `nhds_contain_boxes s t` means that any open neighborhood of `s × t` in `α × β` includes a product of an open neighborhood of `s` by an open neighborhood of `t`. -/ 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) (hn.preimage continuous_swap) (by rwa [←image_subset_iff, image_swap_prod]) in ⟨v, u, vo, uo, tv, su, by rwa [←image_subset_iff, image_swap_prod] at p⟩ lemma nhds_contain_boxes.comm {s : set α} {t : set β} : nhds_contain_boxes s t ↔ nhds_contain_boxes t s := iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm lemma nhds_contain_boxes_of_singleton {x : α} {y : β} : nhds_contain_boxes ({x} : set α) ({y} : set β) := assume n hn hp, let ⟨u, v, uo, vo, xu, yv, hp'⟩ := is_open_prod_iff.mp hn x y (hp $ by simp) in ⟨u, v, uo, vo, by simpa, by simpa, hp'⟩ lemma nhds_contain_boxes_of_compact {s : set α} (hs : is_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, subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1), let ⟨s0, s0_cover⟩ := hs.elim_finite_subcover _ (λi, (h i).1) 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.finite_to_set (λ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›⟩ /-- If `s` and `t` are compact sets and `n` is an open neighborhood of `s × t`, then there exist open neighborhoods `u ⊇ s` and `v ⊇ t` such that `u × v ⊆ n`. -/ lemma generalized_tube_lemma {s : set α} (hs : is_compact s) {t : set β} (ht : is_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 /-- Type class for compact spaces. Separation is sometimes included in the definition, especially in the French literature, but we do not include it here. -/ class compact_space (α : Type*) [topological_space α] : Prop := (compact_univ : is_compact (univ : set α)) @[priority 10] -- see Note [lower instance priority] instance subsingleton.compact_space [subsingleton α] : compact_space α := ⟨subsingleton_univ.is_compact⟩ lemma compact_univ [h : compact_space α] : is_compact (univ : set α) := h.compact_univ lemma cluster_point_of_compact [compact_space α] (f : filter α) [ne_bot f] : ∃ x, cluster_pt x f := by simpa using compact_univ (show f ≤ 𝓟 univ, by simp) lemma compact_space.elim_nhds_subcover {α : Type*} [topological_space α] [compact_space α] (U : α → set α) (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : finset α, (⋃ x ∈ t, U x) = ⊤ := begin obtain ⟨t, -, s⟩ := is_compact.elim_nhds_subcover compact_univ U (λ x m, hU x), exact ⟨t, by { rw eq_top_iff, exact s }⟩, end theorem compact_space_of_finite_subfamily_closed {α : Type u} [topological_space α] (h : Π {ι : Type u} (Z : ι → (set α)), (∀ i, is_closed (Z i)) → (⋂ i, Z i) = ∅ → ∃ (t : finset ι), (⋂ i ∈ t, Z i) = ∅) : compact_space α := { compact_univ := begin apply is_compact_of_finite_subfamily_closed, intros ι Z, specialize h Z, simpa using h end } lemma is_closed.is_compact [compact_space α] {s : set α} (h : is_closed s) : is_compact s := compact_of_is_closed_subset compact_univ h (subset_univ _) /-- A compact discrete space is finite. -/ noncomputable def fintype_of_compact_of_discrete [compact_space α] [discrete_topology α] : fintype α := fintype_of_univ_finite $ finite_of_is_compact_of_discrete _ compact_univ lemma finite_cover_nhds_interior [compact_space α] {U : α → set α} (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : finset α, (⋃ x ∈ t, interior (U x)) = univ := let ⟨t, ht⟩ := compact_univ.elim_finite_subcover (λ x, interior (U x)) (λ x, is_open_interior) (λ x _, mem_Union.2 ⟨x, mem_interior_iff_mem_nhds.2 (hU x)⟩) in ⟨t, univ_subset_iff.1 ht⟩ lemma finite_cover_nhds [compact_space α] {U : α → set α} (hU : ∀ x, U x ∈ 𝓝 x) : ∃ t : finset α, (⋃ x ∈ t, U x) = univ := let ⟨t, ht⟩ := finite_cover_nhds_interior hU in ⟨t, univ_subset_iff.1 $ ht ▸ bUnion_subset_bUnion_right (λ x hx, interior_subset)⟩ /-- If `α` is a compact space, then a locally finite family of sets of `α` can have only finitely many nonempty elements. -/ lemma locally_finite.finite_nonempty_of_compact {ι : Type*} [compact_space α] {f : ι → set α} (hf : locally_finite f) : finite {i | (f i).nonempty} := by simpa only [inter_univ] using hf.finite_nonempty_inter_compact compact_univ /-- If `α` is a compact space, then a locally finite family of nonempty sets of `α` can have only finitely many elements, `set.finite` version. -/ lemma locally_finite.finite_of_compact {ι : Type*} [compact_space α] {f : ι → set α} (hf : locally_finite f) (hne : ∀ i, (f i).nonempty) : finite (univ : set ι) := by simpa only [hne] using hf.finite_nonempty_of_compact /-- If `α` is a compact space, then a locally finite family of nonempty sets of `α` can have only finitely many elements, `fintype` version. -/ noncomputable def locally_finite.fintype_of_compact {ι : Type*} [compact_space α] {f : ι → set α} (hf : locally_finite f) (hne : ∀ i, (f i).nonempty) : fintype ι := fintype_of_univ_finite (hf.finite_of_compact hne) variables [topological_space β] lemma is_compact.image_of_continuous_on {f : α → β} (hs : is_compact s) (hf : continuous_on f s) : is_compact (f '' s) := begin intros l lne ls, have : ne_bot (l.comap f ⊓ 𝓟 s) := comap_inf_principal_ne_bot_of_image_mem lne (le_principal_iff.1 ls), obtain ⟨a, has, ha⟩ : ∃ a ∈ s, cluster_pt a (l.comap f ⊓ 𝓟 s) := @@hs this inf_le_right, use [f a, mem_image_of_mem f has], have : tendsto f (𝓝 a ⊓ (comap f l ⊓ 𝓟 s)) (𝓝 (f a) ⊓ l), { convert (hf a has).inf (@tendsto_comap _ _ f l) using 1, rw nhds_within, ac_refl }, exact @@tendsto.ne_bot _ this ha, end lemma is_compact.image {f : α → β} (hs : is_compact s) (hf : continuous f) : is_compact (f '' s) := hs.image_of_continuous_on hf.continuous_on lemma is_compact_range [compact_space α] {f : α → β} (hf : continuous f) : is_compact (range f) := by rw ← image_univ; exact compact_univ.image hf /-- If X is is_compact then pr₂ : X × Y → Y is a closed map -/ theorem is_closed_proj_of_is_compact {X : Type*} [topological_space X] [compact_space X] {Y : Type*} [topological_space Y] : is_closed_map (prod.snd : X × Y → Y) := begin set πX := (prod.fst : X × Y → X), set πY := (prod.snd : X × Y → Y), assume C (hC : is_closed C), rw is_closed_iff_cluster_pt at hC ⊢, assume y (y_closure : cluster_pt y $ 𝓟 (πY '' C)), have : ne_bot (map πX (comap πY (𝓝 y) ⊓ 𝓟 C)), { suffices : ne_bot (map πY (comap πY (𝓝 y) ⊓ 𝓟 C)), by simpa only [map_ne_bot_iff], convert y_closure, calc map πY (comap πY (𝓝 y) ⊓ 𝓟 C) = 𝓝 y ⊓ map πY (𝓟 C) : filter.push_pull' _ _ _ ... = 𝓝 y ⊓ 𝓟 (πY '' C) : by rw map_principal }, resetI, obtain ⟨x, hx⟩ : ∃ x, cluster_pt x (map πX (comap πY (𝓝 y) ⊓ 𝓟 C)), from cluster_point_of_compact _, refine ⟨⟨x, y⟩, _, by simp [πY]⟩, apply hC, rw [cluster_pt, ← filter.map_ne_bot_iff πX], convert hx, calc map πX (𝓝 (x, y) ⊓ 𝓟 C) = map πX (comap πX (𝓝 x) ⊓ comap πY (𝓝 y) ⊓ 𝓟 C) : by rw [nhds_prod_eq, filter.prod] ... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C ⊓ comap πX (𝓝 x)) : by ac_refl ... = map πX (comap πY (𝓝 y) ⊓ 𝓟 C) ⊓ 𝓝 x : by rw filter.push_pull ... = 𝓝 x ⊓ map πX (comap πY (𝓝 y) ⊓ 𝓟 C) : by rw inf_comm end lemma exists_subset_nhd_of_compact_space [compact_space α] {ι : Type*} [nonempty ι] {V : ι → set α} (hV : directed (⊇) V) (hV_closed : ∀ i, is_closed (V i)) {U : set α} (hU : ∀ x ∈ ⋂ i, V i, U ∈ 𝓝 x) : ∃ i, V i ⊆ U := exists_subset_nhd_of_compact' hV (λ i, (hV_closed i).is_compact) hV_closed hU lemma embedding.is_compact_iff_is_compact_image {f : α → β} (hf : embedding f) : is_compact s ↔ is_compact (f '' s) := iff.intro (assume h, h.image hf.continuous) $ assume h, begin rw is_compact_iff_ultrafilter_le_nhds at ⊢ h, intros u us', have : ↑(u.map f) ≤ 𝓟 (f '' s), begin rw [ultrafilter.coe_map, map_le_iff_le_comap, comap_principal], convert us', exact preimage_image_eq _ hf.inj end, rcases h (u.map f) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩, refine ⟨a, ha, _⟩, rwa [hf.induced, nhds_induced, ←map_le_iff_le_comap] end /-- A closed embedding is proper, ie, inverse images of compact sets are contained in compacts. -/ lemma closed_embedding.tendsto_cocompact {f : α → β} (hf : closed_embedding f) : tendsto f (filter.cocompact α) (filter.cocompact β) := begin rw filter.has_basis_cocompact.tendsto_iff filter.has_basis_cocompact, intros K hK, refine ⟨f ⁻¹' (K ∩ (set.range f)), _, λ x hx, by simpa using hx⟩, apply hf.to_embedding.is_compact_iff_is_compact_image.mpr, rw set.image_preimage_eq_of_subset (set.inter_subset_right _ _), exact hK.inter_right hf.closed_range, end lemma compact_iff_compact_in_subtype {p : α → Prop} {s : set {a // p a}} : is_compact s ↔ is_compact ((coe : _ → α) '' s) := embedding_subtype_coe.is_compact_iff_is_compact_image lemma is_compact_iff_is_compact_univ {s : set α} : is_compact s ↔ is_compact (univ : set s) := by rw [compact_iff_compact_in_subtype, image_univ, subtype.range_coe]; refl lemma is_compact_iff_compact_space {s : set α} : is_compact s ↔ compact_space s := is_compact_iff_is_compact_univ.trans ⟨λ h, ⟨h⟩, @compact_space.compact_univ _ _⟩ lemma is_compact.prod {s : set α} {t : set β} (hs : is_compact s) (ht : is_compact t) : is_compact (set.prod s t) := begin rw is_compact_iff_ultrafilter_le_nhds at hs ht ⊢, intros f hfs, rw le_principal_iff at hfs, obtain ⟨a : α, sa : a ∈ s, ha : map prod.fst ↑f ≤ 𝓝 a⟩ := hs (f.map prod.fst) (le_principal_iff.2 $ mem_map.2 $ mem_sets_of_superset hfs (λ x, and.left)), obtain ⟨b : β, tb : b ∈ t, hb : map prod.snd ↑f ≤ 𝓝 b⟩ := ht (f.map prod.snd) (le_principal_iff.2 $ mem_map.2 $ mem_sets_of_superset hfs (λ x, and.right)), rw map_le_iff_le_comap at ha hb, refine ⟨⟨a, b⟩, ⟨sa, tb⟩, _⟩, rw nhds_prod_eq, exact le_inf ha hb end lemma inducing.is_compact_iff {f : α → β} (hf : inducing f) {s : set α} : is_compact (f '' s) ↔ is_compact s := begin split, { introsI hs F F_ne_bot F_le, obtain ⟨_, ⟨x, x_in : x ∈ s, rfl⟩, hx : cluster_pt (f x) (map f F)⟩ := hs (calc map f F ≤ map f (𝓟 s) : map_mono F_le ... = 𝓟 (f '' s) : map_principal), use [x, x_in], suffices : (map f (𝓝 x ⊓ F)).ne_bot, by simpa [filter.map_ne_bot_iff], rwa calc map f (𝓝 x ⊓ F) = map f ((comap f $ 𝓝 $ f x) ⊓ F) : by rw hf.nhds_eq_comap ... = 𝓝 (f x) ⊓ map f F : filter.push_pull' _ _ _ }, { intro hs, exact hs.image hf.continuous } end /-- Finite topological spaces are compact. -/ @[priority 100] instance fintype.compact_space [fintype α] : compact_space α := { compact_univ := finite_univ.is_compact } /-- The product of two compact spaces is compact. -/ instance [compact_space α] [compact_space β] : compact_space (α × β) := ⟨by { rw ← univ_prod_univ, exact compact_univ.prod compact_univ }⟩ /-- The disjoint union of two compact spaces is compact. -/ instance [compact_space α] [compact_space β] : compact_space (α ⊕ β) := ⟨begin rw ← range_inl_union_range_inr, exact (is_compact_range continuous_inl).union (is_compact_range continuous_inr) end⟩ /-- The coproduct of the cocompact filters on two topological spaces is the cocompact filter on their product. -/ lemma filter.coprod_cocompact {β : Type*} [topological_space β]: (filter.cocompact α).coprod (filter.cocompact β) = filter.cocompact (α × β) := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap_sets, filter.mem_cocompact], split, { rintro ⟨⟨A, ⟨t, ht, hAt⟩, hAS⟩, B, ⟨t', ht', hBt'⟩, hBS⟩, refine ⟨t.prod t', ht.prod ht', _⟩, refine subset.trans _ (union_subset hAS hBS), rw compl_subset_comm at ⊢ hAt hBt', refine subset.trans _ (set.prod_mono hAt hBt'), intros x, simp only [compl_union, mem_inter_eq, mem_prod, mem_preimage, mem_compl_eq], tauto }, { rintros ⟨t, ht, htS⟩, refine ⟨⟨(prod.fst '' t)ᶜ, _, _⟩, ⟨(prod.snd '' t)ᶜ, _, _⟩⟩, { exact ⟨prod.fst '' t, ht.image continuous_fst, subset.rfl⟩ }, { rw preimage_compl, rw compl_subset_comm at ⊢ htS, exact subset.trans htS (subset_preimage_image prod.fst _) }, { exact ⟨prod.snd '' t, ht.image continuous_snd, subset.rfl⟩ }, { rw preimage_compl, rw compl_subset_comm at ⊢ htS, exact subset.trans htS (subset_preimage_image prod.snd _) } } end section tychonoff variables {ι : Type*} {π : ι → Type*} [∀ i, topological_space (π i)] /-- Tychonoff's theorem -/ lemma is_compact_pi_infinite {s : Π i, set (π i)} : (∀ i, is_compact (s i)) → is_compact {x : Π i, π i | ∀ i, x i ∈ s i} := begin simp only [is_compact_iff_ultrafilter_le_nhds, nhds_pi, exists_prop, mem_set_of_eq, le_infi_iff, le_principal_iff], intros h f hfs, have : ∀i:ι, ∃a, a∈s i ∧ tendsto (λx:Πi:ι, π i, x i) f (𝓝 a), { refine λ i, h i (f.map _) (mem_map.2 _), exact mem_sets_of_superset hfs (λ x hx, hx i) }, choose a ha, exact ⟨a, assume i, (ha i).left, assume i, (ha i).right.le_comap⟩ end /-- A version of Tychonoff's theorem that uses `set.pi`. -/ lemma is_compact_univ_pi {s : Π i, set (π i)} (h : ∀ i, is_compact (s i)) : is_compact (pi univ s) := by { convert is_compact_pi_infinite h, simp only [pi, forall_prop_of_true, mem_univ] } instance pi.compact_space [∀ i, compact_space (π i)] : compact_space (Πi, π i) := ⟨by { rw [← pi_univ univ], exact is_compact_univ_pi (λ i, compact_univ) }⟩ /-- Product of compact sets is compact -/ lemma filter.Coprod_cocompact {δ : Type*} {κ : δ → Type*} [Π d, topological_space (κ d)] : filter.Coprod (λ d, filter.cocompact (κ d)) = filter.cocompact (Π d, κ d) := begin ext S, simp only [mem_coprod_iff, exists_prop, mem_comap_sets, filter.mem_cocompact], split, { intros h, rw filter.mem_Coprod_iff at h, choose t ht1 ht2 using h, choose t1 ht11 ht12 using λ d, filter.mem_cocompact.mp (ht1 d), refine ⟨set.pi set.univ t1, _, _⟩, { convert is_compact_pi_infinite ht11, ext, simp }, { refine subset.trans _ (set.Union_subset ht2), intros x, simp only [mem_Union, mem_univ_pi, exists_imp_distrib, mem_compl_eq, not_forall], intros d h, exact ⟨d, ht12 d h⟩ } }, { rintros ⟨t, h1, h2⟩, rw filter.mem_Coprod_iff, intros d, refine ⟨((λ (k : Π (d : δ), κ d), k d) '' t)ᶜ, _, _⟩, { rw filter.mem_cocompact, refine ⟨(λ (k : Π (d : δ), κ d), k d) '' t, _, set.subset.refl _⟩, exact is_compact.image h1 (continuous_pi_iff.mp (continuous_id) d) }, refine subset.trans _ h2, intros x hx, simp only [not_exists, mem_image, mem_preimage, mem_compl_eq] at hx, simpa using mt (hx x) }, end end tychonoff instance quot.compact_space {r : α → α → Prop} [compact_space α] : compact_space (quot r) := ⟨by { rw ← range_quot_mk, exact is_compact_range continuous_quot_mk }⟩ instance quotient.compact_space {s : setoid α} [compact_space α] : compact_space (quotient s) := quot.compact_space /-- There are various definitions of "locally compact space" in the literature, which agree for Hausdorff spaces but not in general. This one is the precise condition on X needed for the evaluation `map C(X, Y) × X → Y` to be continuous for all `Y` when `C(X, Y)` is given the compact-open topology. -/ class locally_compact_space (α : Type*) [topological_space α] : Prop := (local_compact_nhds : ∀ (x : α) (n ∈ 𝓝 x), ∃ s ∈ 𝓝 x, s ⊆ n ∧ is_compact s) lemma compact_basis_nhds [locally_compact_space α] (x : α) : (𝓝 x).has_basis (λ s, s ∈ 𝓝 x ∧ is_compact s) (λ s, s) := has_basis_self.2 $ by simpa only [and_comm] using locally_compact_space.local_compact_nhds x lemma locally_compact_space_of_has_basis {ι : α → Type*} {p : Π x, ι x → Prop} {s : Π x, ι x → set α} (h : ∀ x, (𝓝 x).has_basis (p x) (s x)) (hc : ∀ x i, p x i → is_compact (s x i)) : locally_compact_space α := ⟨λ x t ht, let ⟨i, hp, ht⟩ := (h x).mem_iff.1 ht in ⟨s x i, (h x).mem_of_mem hp, ht, hc x i hp⟩⟩ instance locally_compact_space.prod (α : Type*) (β : Type*) [topological_space α] [topological_space β] [locally_compact_space α] [locally_compact_space β] : locally_compact_space (α × β) := have _ := λ x : α × β, (compact_basis_nhds x.1).prod_nhds' (compact_basis_nhds x.2), locally_compact_space_of_has_basis this $ λ x s ⟨⟨_, h₁⟩, _, h₂⟩, h₁.prod h₂ /-- A reformulation of the definition of locally compact space: In a locally compact space, every open set containing `x` has a compact subset containing `x` in its interior. -/ lemma exists_compact_subset [locally_compact_space α] {x : α} {U : set α} (hU : is_open U) (hx : x ∈ U) : ∃ (K : set α), is_compact K ∧ x ∈ interior K ∧ K ⊆ U := begin rcases locally_compact_space.local_compact_nhds x U (hU.mem_nhds hx) with ⟨K, h1K, h2K, h3K⟩, exact ⟨K, h3K, mem_interior_iff_mem_nhds.2 h1K, h2K⟩, end /-- In a locally compact space every point has a compact neighborhood. -/ lemma exists_compact_mem_nhds [locally_compact_space α] (x : α) : ∃ K, is_compact K ∧ K ∈ 𝓝 x := let ⟨K, hKc, hx, H⟩ := exists_compact_subset is_open_univ (mem_univ x) in ⟨K, hKc, mem_interior_iff_mem_nhds.1 hx⟩ /-- In a locally compact space, every compact set is contained in the interior of a compact set. -/ lemma exists_compact_superset [locally_compact_space α] {K : set α} (hK : is_compact K) : ∃ K', is_compact K' ∧ K ⊆ interior K' := begin choose U hUc hxU using λ x : K, exists_compact_mem_nhds (x : α), have : K ⊆ ⋃ x, interior (U x), from λ x hx, mem_Union.2 ⟨⟨x, hx⟩, mem_interior_iff_mem_nhds.2 (hxU _)⟩, rcases hK.elim_finite_subcover _ _ this with ⟨t, ht⟩, { refine ⟨_, t.compact_bUnion (λ x _, hUc x), λ x hx, _⟩, rcases mem_bUnion_iff.1 (ht hx) with ⟨y, hyt, hy⟩, exact interior_mono (subset_bUnion_of_mem hyt) hy }, { exact λ _, is_open_interior } end lemma ultrafilter.le_nhds_Lim [compact_space α] (F : ultrafilter α) : ↑F ≤ 𝓝 (@Lim _ _ (F : filter α).nonempty_of_ne_bot F) := begin rcases compact_univ.ultrafilter_le_nhds F (by simp) with ⟨x, -, h⟩, exact le_nhds_Lim ⟨x,h⟩, end theorem is_closed.exists_minimal_nonempty_closed_subset [compact_space α] {S : set α} (hS : is_closed S) (hne : S.nonempty) : ∃ (V : set α), V ⊆ S ∧ V.nonempty ∧ is_closed V ∧ (∀ (V' : set α), V' ⊆ V → V'.nonempty → is_closed V' → V' = V) := begin let opens := {U : set α | Sᶜ ⊆ U ∧ is_open U ∧ Uᶜ.nonempty}, obtain ⟨U, ⟨Uc, Uo, Ucne⟩, h⟩ := zorn.zorn_subset opens (λ c hc hz, begin by_cases hcne : c.nonempty, { obtain ⟨U₀, hU₀⟩ := hcne, haveI : nonempty {U // U ∈ c} := ⟨⟨U₀, hU₀⟩⟩, obtain ⟨U₀compl, U₀opn, U₀ne⟩ := hc hU₀, use ⋃₀ c, refine ⟨⟨_, _, _⟩, λ U hU a ha, ⟨U, hU, ha⟩⟩, { exact λ a ha, ⟨U₀, hU₀, U₀compl ha⟩ }, { exact is_open_sUnion (λ _ h, (hc h).2.1) }, { convert_to (⋂(U : {U // U ∈ c}), U.1ᶜ).nonempty, { ext, simp only [not_exists, exists_prop, not_and, set.mem_Inter, subtype.forall, set.mem_set_of_eq, set.mem_compl_eq, subtype.val_eq_coe], refl, }, apply is_compact.nonempty_Inter_of_directed_nonempty_compact_closed, { rintros ⟨U, hU⟩ ⟨U', hU'⟩, obtain ⟨V, hVc, hVU, hVU'⟩ := zorn.chain.directed_on hz U hU U' hU', exact ⟨⟨V, hVc⟩, set.compl_subset_compl.mpr hVU, set.compl_subset_compl.mpr hVU'⟩, }, { exact λ U, (hc U.2).2.2, }, { exact λ U, (is_closed_compl_iff.mpr (hc U.2).2.1).is_compact, }, { exact λ U, (is_closed_compl_iff.mpr (hc U.2).2.1), } } }, { use Sᶜ, refine ⟨⟨set.subset.refl _, is_open_compl_iff.mpr hS, _⟩, λ U Uc, (hcne ⟨U, Uc⟩).elim⟩, rw compl_compl, exact hne, } end), refine ⟨Uᶜ, set.compl_subset_comm.mp Uc, Ucne, is_closed_compl_iff.mpr Uo, _⟩, intros V' V'sub V'ne V'cls, have : V'ᶜ = U, { refine h V'ᶜ ⟨_, is_open_compl_iff.mpr V'cls, _⟩ (set.subset_compl_comm.mp V'sub), exact set.subset.trans Uc (set.subset_compl_comm.mp V'sub), simp only [compl_compl, V'ne], }, rw [←this, compl_compl], end /-- A σ-compact space is a space that is the union of a countable collection of compact subspaces. Note that a locally compact separable T₂ space need not be σ-compact. The sequence can be extracted using `topological_space.compact_covering`. -/ class sigma_compact_space (α : Type*) [topological_space α] : Prop := (exists_compact_covering : ∃ K : ℕ → set α, (∀ n, is_compact (K n)) ∧ (⋃ n, K n) = univ) @[priority 200] -- see Note [lower instance priority] instance compact_space.sigma_compact [compact_space α] : sigma_compact_space α := ⟨⟨λ _, univ, λ _, compact_univ, Union_const _⟩⟩ lemma sigma_compact_space.of_countable (S : set (set α)) (Hc : countable S) (Hcomp : ∀ s ∈ S, is_compact s) (HU : ⋃₀ S = univ) : sigma_compact_space α := ⟨(exists_seq_cover_iff_countable ⟨_, is_compact_empty⟩).2 ⟨S, Hc, Hcomp, HU⟩⟩ @[priority 100] -- see Note [lower instance priority] instance sigma_compact_space_of_locally_compact_second_countable [locally_compact_space α] [second_countable_topology α] : sigma_compact_space α := begin choose K hKc hxK using λ x : α, exists_compact_mem_nhds x, rcases countable_cover_nhds hxK with ⟨s, hsc, hsU⟩, refine sigma_compact_space.of_countable _ (hsc.image K) (ball_image_iff.2 $ λ x _, hKc x) _, rwa sUnion_image end variables (α) [sigma_compact_space α] open sigma_compact_space /-- A choice of compact covering for a `σ`-compact space, chosen to be monotone. -/ def compact_covering : ℕ → set α := accumulate exists_compact_covering.some lemma is_compact_compact_covering (n : ℕ) : is_compact (compact_covering α n) := compact_accumulate (classical.some_spec sigma_compact_space.exists_compact_covering).1 n lemma Union_compact_covering : (⋃ n, compact_covering α n) = univ := begin rw [compact_covering, Union_accumulate], exact (classical.some_spec sigma_compact_space.exists_compact_covering).2 end @[mono] lemma compact_covering_subset ⦃m n : ℕ⦄ (h : m ≤ n) : compact_covering α m ⊆ compact_covering α n := monotone_accumulate h variable {α} /-- If `α` is a `σ`-compact space, then a locally finite family of nonempty sets of `α` can have only countably many elements, `set.countable` version. -/ lemma locally_finite.countable_of_sigma_compact {ι : Type*} {f : ι → set α} (hf : locally_finite f) (hne : ∀ i, (f i).nonempty) : countable (univ : set ι) := begin have := λ n, hf.finite_nonempty_inter_compact (is_compact_compact_covering α n), refine (countable_Union (λ n, (this n).countable)).mono (λ i hi, _), rcases hne i with ⟨x, hx⟩, rcases Union_eq_univ_iff.1 (Union_compact_covering α) x with ⟨n, hn⟩, exact mem_Union.2 ⟨n, x, hx, hn⟩ end /-- In a topological space with sigma compact topology, if `f` is a function that sends each point `x` to a neighborhood of `x`, then for some countable set `s`, the neighborhoods `f x`, `x ∈ s`, cover the whole space. -/ lemma countable_cover_nhds_of_sigma_compact {f : α → set α} (hf : ∀ x, f x ∈ 𝓝 x) : ∃ s : set α, countable s ∧ (⋃ x ∈ s, f x) = univ := begin choose t ht hsub using λ n, (is_compact_compact_covering α n).elim_nhds_subcover f (λ x _, hf x), refine ⟨⋃ n, (t n : set α), countable_Union $ λ n, (t n).countable_to_set, _⟩, simp only [eq_univ_iff_forall, mem_Union, exists_prop], intro x, rcases Union_eq_univ_iff.1 (Union_compact_covering α) x with ⟨n, hn⟩, rcases mem_bUnion_iff.1 (hsub n hn) with ⟨c, hct, hfx⟩, exact ⟨c, ⟨n, hct⟩, hfx⟩ end end compact /-- An [exhaustion by compact sets](https://en.wikipedia.org/wiki/Exhaustion_by_compact_sets) of a topological space is a sequence of compact sets `K n` such that `K n ⊆ interior (K (n + 1))` and `(⋃ n, K n) = univ`. If `X` is a locally compact sigma compact space, then `compact_exhaustion.choice X` provides a choice of an exhaustion by compact sets. This choice is also available as `(default : compact_exhaustion X)`. -/ structure compact_exhaustion (X : Type*) [topological_space X] := (to_fun : ℕ → set X) (is_compact' : ∀ n, is_compact (to_fun n)) (subset_interior_succ' : ∀ n, to_fun n ⊆ interior (to_fun (n + 1))) (Union_eq' : (⋃ n, to_fun n) = univ) namespace compact_exhaustion instance : has_coe_to_fun (compact_exhaustion α) := ⟨_, to_fun⟩ variables {α} (K : compact_exhaustion α) protected lemma is_compact (n : ℕ) : is_compact (K n) := K.is_compact' n lemma subset_interior_succ (n : ℕ) : K n ⊆ interior (K (n + 1)) := K.subset_interior_succ' n lemma subset_succ (n : ℕ) : K n ⊆ K (n + 1) := subset.trans (K.subset_interior_succ n) interior_subset @[mono] protected lemma subset ⦃m n : ℕ⦄ (h : m ≤ n) : K m ⊆ K n := show K m ≤ K n, from monotone_of_monotone_nat K.subset_succ h lemma subset_interior ⦃m n : ℕ⦄ (h : m < n) : K m ⊆ interior (K n) := subset.trans (K.subset_interior_succ m) $ interior_mono $ K.subset h lemma Union_eq : (⋃ n, K n) = univ := K.Union_eq' lemma exists_mem (x : α) : ∃ n, x ∈ K n := Union_eq_univ_iff.1 K.Union_eq x /-- The minimal `n` such that `x ∈ K n`. -/ protected noncomputable def find (x : α) : ℕ := nat.find (K.exists_mem x) lemma mem_find (x : α) : x ∈ K (K.find x) := nat.find_spec (K.exists_mem x) lemma mem_iff_find_le {x : α} {n : ℕ} : x ∈ K n ↔ K.find x ≤ n := ⟨λ h, nat.find_min' (K.exists_mem x) h, λ h, K.subset h $ K.mem_find x⟩ /-- Prepend the empty set to a compact exhaustion `K n`. -/ def shiftr : compact_exhaustion α := { to_fun := λ n, nat.cases_on n ∅ K, is_compact' := λ n, nat.cases_on n is_compact_empty K.is_compact, subset_interior_succ' := λ n, nat.cases_on n (empty_subset _) K.subset_interior_succ, Union_eq' := Union_eq_univ_iff.2 $ λ x, ⟨K.find x + 1, K.mem_find x⟩ } @[simp] lemma find_shiftr (x : α) : K.shiftr.find x = K.find x + 1 := nat.find_comp_succ _ _ (not_mem_empty _) lemma mem_diff_shiftr_find (x : α) : x ∈ K.shiftr (K.find x + 1) \ K.shiftr (K.find x) := ⟨K.mem_find _, mt K.shiftr.mem_iff_find_le.1 $ by simp only [find_shiftr, not_le, nat.lt_succ_self]⟩ /-- A choice of an [exhaustion by compact sets](https://en.wikipedia.org/wiki/Exhaustion_by_compact_sets) of a locally compact sigma compact space. -/ noncomputable def choice (X : Type*) [topological_space X] [locally_compact_space X] [sigma_compact_space X] : compact_exhaustion X := begin apply classical.choice, let K : ℕ → {s : set X // is_compact s} := λ n, nat.rec_on n ⟨∅, is_compact_empty⟩ (λ n s, ⟨(exists_compact_superset s.2).some ∪ compact_covering X n, (exists_compact_superset s.2).some_spec.1.union (is_compact_compact_covering _ _)⟩), refine ⟨⟨λ n, K n, λ n, (K n).2, λ n, _, _⟩⟩, { exact subset.trans (exists_compact_superset (K n).2).some_spec.2 (interior_mono $ subset_union_left _ _) }, { refine univ_subset_iff.1 (Union_compact_covering X ▸ _), exact Union_subset_Union2 (λ n, ⟨n + 1, subset_union_right _ _⟩) } end noncomputable instance [locally_compact_space α] [sigma_compact_space α] : inhabited (compact_exhaustion α) := ⟨compact_exhaustion.choice α⟩ end compact_exhaustion section clopen /-- A set is clopen if it is both open and closed. -/ def is_clopen (s : set α) : Prop := is_open s ∧ is_closed s theorem is_clopen.union {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∪ t) := ⟨is_open.union hs.1 ht.1, is_closed.union hs.2 ht.2⟩ theorem is_clopen.inter {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ t) := ⟨is_open.inter hs.1 ht.1, is_closed.inter hs.2 ht.2⟩ @[simp] theorem is_clopen_empty : is_clopen (∅ : set α) := ⟨is_open_empty, is_closed_empty⟩ @[simp] theorem is_clopen_univ : is_clopen (univ : set α) := ⟨is_open_univ, is_closed_univ⟩ theorem is_clopen.compl {s : set α} (hs : is_clopen s) : is_clopen sᶜ := ⟨hs.2.is_open_compl, is_closed_compl_iff.2 hs.1⟩ @[simp] theorem is_clopen_compl_iff {s : set α} : is_clopen sᶜ ↔ is_clopen s := ⟨λ h, compl_compl s ▸ is_clopen.compl h, is_clopen.compl⟩ theorem is_clopen.diff {s t : set α} (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s \ t) := hs.inter ht.compl lemma is_clopen_Union {β : Type*} [fintype β] {s : β → set α} (h : ∀ i, is_clopen (s i)) : is_clopen (⋃ i, s i) := ⟨is_open_Union (forall_and_distrib.1 h).1, is_closed_Union (forall_and_distrib.1 h).2⟩ lemma is_clopen_bUnion {β : Type*} {s : finset β} {f : β → set α} (h : ∀i ∈ s, is_clopen $ f i) : is_clopen (⋃ i ∈ s, f i) := begin refine ⟨is_open_bUnion (λ i hi, (h i hi).1), _⟩, show is_closed (⋃ (i : β) (H : i ∈ (s : set β)), f i), rw bUnion_eq_Union, exact is_closed_Union (λ ⟨i, hi⟩,(h i hi).2) end lemma is_clopen_Inter {β : Type*} [fintype β] {s : β → set α} (h : ∀ i, is_clopen (s i)) : is_clopen (⋂ i, s i) := ⟨(is_open_Inter (forall_and_distrib.1 h).1), (is_closed_Inter (forall_and_distrib.1 h).2)⟩ lemma is_clopen_bInter {β : Type*} {s : finset β} {f : β → set α} (h : ∀i∈s, is_clopen (f i)) : is_clopen (⋂i∈s, f i) := ⟨ is_open_bInter ⟨finset_coe.fintype s⟩ (λ i hi, (h i hi).1), by {show is_closed (⋂ (i : β) (H : i ∈ (↑s : set β)), f i), rw bInter_eq_Inter, apply is_closed_Inter, rintro ⟨i, hi⟩, exact (h i hi).2}⟩ lemma continuous_on.preimage_clopen_of_clopen {β: Type*} [topological_space β] {f : α → β} {s : set α} {t : set β} (hf : continuous_on f s) (hs : is_clopen s) (ht : is_clopen t) : is_clopen (s ∩ f⁻¹' t) := ⟨continuous_on.preimage_open_of_open hf hs.1 ht.1, continuous_on.preimage_closed_of_closed hf hs.2 ht.2⟩ /-- The intersection of a disjoint covering by two open sets of a clopen set will be clopen. -/ theorem is_clopen_inter_of_disjoint_cover_clopen {Z a b : set α} (h : is_clopen Z) (cover : Z ⊆ a ∪ b) (ha : is_open a) (hb : is_open b) (hab : a ∩ b = ∅) : is_clopen (Z ∩ a) := begin refine ⟨is_open.inter h.1 ha, _⟩, have : is_closed (Z ∩ bᶜ) := is_closed.inter h.2 (is_closed_compl_iff.2 hb), convert this using 1, apply subset.antisymm, { exact inter_subset_inter_right Z (subset_compl_iff_disjoint.2 hab) }, { rintros x ⟨hx₁, hx₂⟩, exact ⟨hx₁, by simpa [not_mem_of_mem_compl hx₂] using cover hx₁⟩ } end end clopen section preirreducible /-- A preirreducible set `s` is one where there is no non-trivial pair of disjoint opens on `s`. -/ def is_preirreducible (s : set α) : Prop := ∀ (u v : set α), is_open u → is_open v → (s ∩ u).nonempty → (s ∩ v).nonempty → (s ∩ (u ∩ v)).nonempty /-- An irreducible set `s` is one that is nonempty and where there is no non-trivial pair of disjoint opens on `s`. -/ def is_irreducible (s : set α) : Prop := s.nonempty ∧ is_preirreducible s lemma is_irreducible.nonempty {s : set α} (h : is_irreducible s) : s.nonempty := h.1 lemma is_irreducible.is_preirreducible {s : set α} (h : is_irreducible s) : is_preirreducible s := h.2 theorem is_preirreducible_empty : is_preirreducible (∅ : set α) := λ _ _ _ _ _ ⟨x, h1, h2⟩, h1.elim theorem is_irreducible_singleton {x} : is_irreducible ({x} : set α) := ⟨singleton_nonempty x, λ u v _ _ ⟨y, h1, h2⟩ ⟨z, h3, h4⟩, by rw mem_singleton_iff at h1 h3; substs y z; exact ⟨x, rfl, h2, h4⟩⟩ theorem is_preirreducible.closure {s : set α} (H : is_preirreducible s) : is_preirreducible (closure s) := λ u v hu hv ⟨y, hycs, hyu⟩ ⟨z, hzcs, hzv⟩, let ⟨p, hpu, hps⟩ := mem_closure_iff.1 hycs u hu hyu in let ⟨q, hqv, hqs⟩ := mem_closure_iff.1 hzcs v hv hzv in let ⟨r, hrs, hruv⟩ := H u v hu hv ⟨p, hps, hpu⟩ ⟨q, hqs, hqv⟩ in ⟨r, subset_closure hrs, hruv⟩ lemma is_irreducible.closure {s : set α} (h : is_irreducible s) : is_irreducible (closure s) := ⟨h.nonempty.closure, h.is_preirreducible.closure⟩ theorem exists_preirreducible (s : set α) (H : is_preirreducible s) : ∃ t : set α, is_preirreducible t ∧ s ⊆ t ∧ ∀ u, is_preirreducible u → t ⊆ u → u = t := let ⟨m, hm, hsm, hmm⟩ := zorn.zorn_subset_nonempty {t : set α | is_preirreducible t} (λ c hc hcc hcn, let ⟨t, htc⟩ := hcn in ⟨⋃₀ c, λ u v hu hv ⟨y, hy, hyu⟩ ⟨z, hz, hzv⟩, let ⟨p, hpc, hyp⟩ := mem_sUnion.1 hy, ⟨q, hqc, hzq⟩ := mem_sUnion.1 hz in or.cases_on (zorn.chain.total hcc hpc hqc) (assume hpq : p ⊆ q, let ⟨x, hxp, hxuv⟩ := hc hqc u v hu hv ⟨y, hpq hyp, hyu⟩ ⟨z, hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hqc, hxuv⟩) (assume hqp : q ⊆ p, let ⟨x, hxp, hxuv⟩ := hc hpc u v hu hv ⟨y, hyp, hyu⟩ ⟨z, hqp hzq, hzv⟩ in ⟨x, mem_sUnion_of_mem hxp hpc, hxuv⟩), λ x hxc, subset_sUnion_of_mem hxc⟩) s H in ⟨m, hm, hsm, λ u hu hmu, hmm _ hu hmu⟩ /-- A maximal irreducible set that contains a given point. -/ def irreducible_component (x : α) : set α := classical.some (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) lemma irreducible_component_property (x : α) : is_preirreducible (irreducible_component x) ∧ {x} ⊆ (irreducible_component x) ∧ ∀ u, is_preirreducible u → (irreducible_component x) ⊆ u → u = (irreducible_component x) := classical.some_spec (exists_preirreducible {x} is_irreducible_singleton.is_preirreducible) theorem mem_irreducible_component {x : α} : x ∈ irreducible_component x := singleton_subset_iff.1 (irreducible_component_property x).2.1 theorem is_irreducible_irreducible_component {x : α} : is_irreducible (irreducible_component x) := ⟨⟨x, mem_irreducible_component⟩, (irreducible_component_property x).1⟩ theorem eq_irreducible_component {x : α} : ∀ {s : set α}, is_preirreducible s → irreducible_component x ⊆ s → s = irreducible_component x := (irreducible_component_property x).2.2 theorem is_closed_irreducible_component {x : α} : is_closed (irreducible_component x) := closure_eq_iff_is_closed.1 $ eq_irreducible_component is_irreducible_irreducible_component.is_preirreducible.closure subset_closure /-- A preirreducible space is one where there is no non-trivial pair of disjoint opens. -/ class preirreducible_space (α : Type u) [topological_space α] : Prop := (is_preirreducible_univ [] : is_preirreducible (univ : set α)) /-- An irreducible space is one that is nonempty and where there is no non-trivial pair of disjoint opens. -/ class irreducible_space (α : Type u) [topological_space α] extends preirreducible_space α : Prop := (to_nonempty [] : nonempty α) -- see Note [lower instance priority] attribute [instance, priority 50] irreducible_space.to_nonempty theorem nonempty_preirreducible_inter [preirreducible_space α] {s t : set α} : is_open s → is_open t → s.nonempty → t.nonempty → (s ∩ t).nonempty := by simpa only [univ_inter, univ_subset_iff] using @preirreducible_space.is_preirreducible_univ α _ _ s t theorem is_preirreducible.image [topological_space β] {s : set α} (H : is_preirreducible s) (f : α → β) (hf : continuous_on f s) : is_preirreducible (f '' s) := begin rintros u v hu hv ⟨_, ⟨⟨x, hx, rfl⟩, hxu⟩⟩ ⟨_, ⟨⟨y, hy, rfl⟩, hyv⟩⟩, rw ← mem_preimage at hxu hyv, rcases continuous_on_iff'.1 hf u hu with ⟨u', hu', u'_eq⟩, rcases continuous_on_iff'.1 hf v hv with ⟨v', hv', v'_eq⟩, have := H u' v' hu' hv', rw [inter_comm s u', ← u'_eq] at this, rw [inter_comm s v', ← v'_eq] at this, rcases this ⟨x, hxu, hx⟩ ⟨y, hyv, hy⟩ with ⟨z, hzs, hzu', hzv'⟩, refine ⟨f z, mem_image_of_mem f hzs, _, _⟩, all_goals { rw ← mem_preimage, apply mem_of_mem_inter_left, show z ∈ _ ∩ s, simp [*] } end theorem is_irreducible.image [topological_space β] {s : set α} (H : is_irreducible s) (f : α → β) (hf : continuous_on f s) : is_irreducible (f '' s) := ⟨nonempty_image_iff.mpr H.nonempty, H.is_preirreducible.image f hf⟩ lemma subtype.preirreducible_space {s : set α} (h : is_preirreducible s) : preirreducible_space s := { is_preirreducible_univ := begin intros u v hu hv hsu hsv, rw is_open_induced_iff at hu hv, rcases hu with ⟨u, hu, rfl⟩, rcases hv with ⟨v, hv, rfl⟩, rcases hsu with ⟨⟨x, hxs⟩, hxs', hxu⟩, rcases hsv with ⟨⟨y, hys⟩, hys', hyv⟩, rcases h u v hu hv ⟨x, hxs, hxu⟩ ⟨y, hys, hyv⟩ with ⟨z, hzs, ⟨hzu, hzv⟩⟩, exact ⟨⟨z, hzs⟩, ⟨set.mem_univ _, ⟨hzu, hzv⟩⟩⟩ end } lemma subtype.irreducible_space {s : set α} (h : is_irreducible s) : irreducible_space s := { is_preirreducible_univ := (subtype.preirreducible_space h.is_preirreducible).is_preirreducible_univ, to_nonempty := h.nonempty.to_subtype } /-- A set `s` is irreducible if and only if for every finite collection of open sets all of whose members intersect `s`, `s` also intersects the intersection of the entire collection (i.e., there is an element of `s` contained in every member of the collection). -/ lemma is_irreducible_iff_sInter {s : set α} : is_irreducible s ↔ ∀ (U : finset (set α)) (hU : ∀ u ∈ U, is_open u) (H : ∀ u ∈ U, (s ∩ u).nonempty), (s ∩ ⋂₀ ↑U).nonempty := begin split; intro h, { intro U, apply finset.induction_on U, { intros, simpa using h.nonempty }, { intros u U hu IH hU H, rw [finset.coe_insert, sInter_insert], apply h.2, { solve_by_elim [finset.mem_insert_self] }, { apply is_open_sInter (finset.finite_to_set U), intros, solve_by_elim [finset.mem_insert_of_mem] }, { solve_by_elim [finset.mem_insert_self] }, { apply IH, all_goals { intros, solve_by_elim [finset.mem_insert_of_mem] } } } }, { split, { simpa using h ∅ _ _; intro u; simp }, intros u v hu hv hu' hv', simpa using h {u,v} _ _, all_goals { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption } } end /-- A set is preirreducible if and only if for every cover by two closed sets, it is contained in one of the two covering sets. -/ lemma is_preirreducible_iff_closed_union_closed {s : set α} : is_preirreducible s ↔ ∀ (z₁ z₂ : set α), is_closed z₁ → is_closed z₂ → s ⊆ z₁ ∪ z₂ → s ⊆ z₁ ∨ s ⊆ z₂ := begin split, all_goals { intros h t₁ t₂ ht₁ ht₂, specialize h t₁ᶜ t₂ᶜ, simp only [is_open_compl_iff, is_closed_compl_iff] at h, specialize h ht₁ ht₂ }, { contrapose!, simp only [not_subset], rintro ⟨⟨x, hx, hx'⟩, ⟨y, hy, hy'⟩⟩, rcases h ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩ with ⟨z, hz, hz'⟩, rw ← compl_union at hz', exact ⟨z, hz, hz'⟩ }, { rintro ⟨x, hx, hx'⟩ ⟨y, hy, hy'⟩, rw ← compl_inter at h, delta set.nonempty, rw imp_iff_not_or at h, contrapose! h, split, { intros z hz hz', exact h z ⟨hz, hz'⟩ }, { split; intro H; refine H _ ‹_›; assumption } } end /-- A set is irreducible if and only if for every cover by a finite collection of closed sets, it is contained in one of the members of the collection. -/ lemma is_irreducible_iff_sUnion_closed {s : set α} : is_irreducible s ↔ ∀ (Z : finset (set α)) (hZ : ∀ z ∈ Z, is_closed z) (H : s ⊆ ⋃₀ ↑Z), ∃ z ∈ Z, s ⊆ z := begin rw [is_irreducible, is_preirreducible_iff_closed_union_closed], split; intro h, { intro Z, apply finset.induction_on Z, { intros, rw [finset.coe_empty, sUnion_empty] at H, rcases h.1 with ⟨x, hx⟩, exfalso, tauto }, { intros z Z hz IH hZ H, cases h.2 z (⋃₀ ↑Z) _ _ _ with h' h', { exact ⟨z, finset.mem_insert_self _ _, h'⟩ }, { rcases IH _ h' with ⟨z', hz', hsz'⟩, { exact ⟨z', finset.mem_insert_of_mem hz', hsz'⟩ }, { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { solve_by_elim [finset.mem_insert_self] }, { rw sUnion_eq_bUnion, apply is_closed_bUnion (finset.finite_to_set Z), { intros, solve_by_elim [finset.mem_insert_of_mem] } }, { simpa using H } } }, { split, { by_contradiction hs, simpa using h ∅ _ _, { intro z, simp }, { simpa [set.nonempty] using hs } }, intros z₁ z₂ hz₁ hz₂ H, have := h {z₁, z₂} _ _, simp only [exists_prop, finset.mem_insert, finset.mem_singleton] at this, { rcases this with ⟨z, rfl|rfl, hz⟩; tauto }, { intro t, rw [finset.mem_insert, finset.mem_singleton], rintro (rfl|rfl); assumption }, { simpa using H } } end end preirreducible
7e0385603d3ce2cf0f068789c87900ea8ab77a06
7a097aab717143b13a4a4f8824fe9cfe670a7883
/src/smt2/except.lean
18480371e9f3b1472965affee64c79ef96e7d6d0
[ "Apache-2.0" ]
permissive
digama0/smt2_interface
6b3bbc12cb90bd4bc70b07ca86918c1e9ff4ed97
cf516ae4142f33adcf4b272758d63fee37ec9f7d
refs/heads/master
1,611,266,770,453
1,498,177,713,000
1,498,177,713,000
95,167,865
0
0
null
1,498,177,813,000
1,498,177,813,000
null
UTF-8
Lean
false
false
4,891
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jared Roesch -/ /- FIXME(@jroesch): remove this after the native compiler branch is merged. -/ universes u v inductive except (ε α : Type u) | error {} : ε → except | ok {} : α → except def except.return {ε α : Type u} (a : α) : except ε α := except.ok a def except.map {ε α β : Type u} (f : α → β) : except ε α → except ε β | (except.error err) := except.error err | (except.ok v) := except.ok $ f v def except.bind {ε α β : Type u} (ma : except ε α) (f : α → except ε β) : except ε β := match ma with | (except.error err) := except.error err | (except.ok v) := f v end lemma except.id_map {ε α : Type u} (x : except ε α) : except.map id x = x := begin intros, cases x; dsimp [except.map]; reflexivity, end lemma except.pure_bind {ε α β : Type u} (x : α) (f : α → except ε β) : except.bind (except.return x) f = f x := begin intros, dsimp [except.return, except.bind], reflexivity end lemma except.bind_pure_comp_eq_map {ε α β : Type u} (f : α → β) (x : except ε α) : (except.bind x) (except.return ∘ f) = except.map f x := begin intros, cases x; dsimp, unfold except.bind except.return except.map, unfold except.bind._match_1, simp, reflexivity, end instance except_monad (ε : Type u) : monad (except ε) := { pure := @except.return ε, map := @except.map ε, bind := @except.bind ε, id_map := @except.id_map ε, pure_bind := @except.pure_bind ε, bind_pure_comp_eq_map := @except.bind_pure_comp_eq_map ε, bind_assoc := begin intros, cases x; dsimp [except.bind]; reflexivity end } @[reducible] def except_t (m : Type u → Type v) (ε α : Type u) := m (except ε α) section except_t parameter (m : Type u → Type v) parameter [monad_m : monad m] parameter ε : Type u def except_t.return {α : Type u} (a : α) : except_t m ε α := @return m monad_m _ (except.ok a) -- WHy doesn't this work anymore, this seems super annoying ... -- Overall if we lost the ability for these types of projections working it seems like -- a massive lose compared to the old style. -- -- def except_t.map {α β : Type u} (f : α → β) (ma : except_t m ε α) : except_t m ε β := -- (@has_map.map m _ _ _) (except.map f) ma -- This is super annoying ... def except_t.map {α β : Type u} (f : α → β) (ma : except_t m ε α) : except_t m ε β := (@has_map.map m (monad_m.to_applicative.to_functor.to_has_map) _) _ (except.map f) ma def except_t.bind {α β : Type u} (ma : except_t m ε α) (f : α → except_t m ε β) : except_t m ε β := (@has_bind.bind m monad_m.to_has_bind _ _) ma $ λ res, match res with | (except.error err) := return $ except.error err | (except.ok v) := f v end lemma except_t.id_map {α : Type u} (x : except_t m ε α) : except_t.map id x = x := begin intros, unfold except_t at x, unfold except_t.map, have P : @except.map ε α α id = id, apply funext, intros, rewrite except.id_map, simp, rewrite P, rw functor.id_map, end lemma except_t.pure_bind {α β : Type u} (x : α) (f : α → except_t m ε β) : except_t.bind (except_t.return x) f = f x := begin unfold except_t.return except_t.bind, have pb := @monad.pure_bind, rewrite pb, unfold except_t.bind._match_1, reflexivity end lemma except_t.bind_assoc {α β γ : Type u} (x : except_t m ε α) (f : α → except_t m ε β) (g : β → except_t m ε γ): except_t.bind (except_t.bind x f) g = except_t.bind x (λ (x : α), except_t.bind (f x) g) := begin unfold except_t.bind, rewrite monad.bind_assoc, apply congr, simp, apply funext, intros, cases x_1; simp; unfold except_t.bind._match_1, unfold return pure, rewrite @monad.pure_bind, unfold except_t.bind._match_1 has_pure.pure, dsimp, reflexivity, reflexivity, end lemma except_t.bind_pure_comp_eq_map {α β : Type u} (f : α → β) (x : except_t m ε α) : (except_t.bind x) (except_t.return ∘ f) = except_t.map f x := begin intros, unfold except_t at x, unfold except_t.bind except_t.map, rewrite -monad.bind_pure_comp_eq_map, apply congr ; simp, apply funext, intros, cases x_1, unfold except_t.bind._match_1, unfold function.comp, simp [except.map], unfold return pure, unfold function.comp, unfold except_t.bind._match_1, simp [except.map], unfold except_t.return, reflexivity, end end except_t instance except_t.monad (m : Type u → Type v) [monad m] (ε : Type u) : monad (except_t m ε) := { pure := @except_t.return m _ ε , map := @except_t.map m _ ε, bind := @except_t.bind m _ ε, id_map := @except_t.id_map m _ ε, pure_bind := @except_t.pure_bind m _ ε, bind_assoc := @except_t.bind_assoc m _ ε, bind_pure_comp_eq_map := @except_t.bind_pure_comp_eq_map m _ ε }
daddcd4a9aaadb5f595350d462f6f4c5f525b5fb
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/order/omega_complete_partial_order.lean
bf44b4f4c0a47f6d0b9384673b8ae88d6ad7ca55
[ "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
27,636
lean
/- Copyright (c) 2020 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import control.monad.basic import data.part import order.hom.order import data.nat.order.basic import tactic.wlog /-! # Omega Complete Partial Orders An omega-complete partial order is a partial order with a supremum operation on increasing sequences indexed by natural numbers (which we call `ωSup`). In this sense, it is strictly weaker than join complete semi-lattices as only ω-sized totally ordered sets have a supremum. The concept of an omega-complete partial order (ωCPO) is useful for the formalization of the semantics of programming languages. Its notion of supremum helps define the meaning of recursive procedures. ## Main definitions * class `omega_complete_partial_order` * `ite`, `map`, `bind`, `seq` as continuous morphisms ## Instances of `omega_complete_partial_order` * `part` * every `complete_lattice` * pi-types * product types * `monotone_hom` * `continuous_hom` (with notation →𝒄) * an instance of `omega_complete_partial_order (α →𝒄 β)` * `continuous_hom.of_fun` * `continuous_hom.of_mono` * continuous functions: * `id` * `ite` * `const` * `part.bind` * `part.map` * `part.seq` ## References * [Chain-complete posets and directed sets with applications][markowsky1976] * [Recursive definitions of partial functions and their computations][cadiou1972] * [Semantics of Programming Languages: Structures and Techniques][gunter1992] -/ universes u v local attribute [-simp] part.bind_eq_bind part.map_eq_map open_locale classical namespace order_hom variables (α : Type*) (β : Type*) {γ : Type*} {φ : Type*} variables [preorder α] [preorder β] [preorder γ] [preorder φ] variables {β γ} variables {α} {α' : Type*} {β' : Type*} [preorder α'] [preorder β'] /-- `part.bind` as a monotone function -/ @[simps] def bind {β γ} (f : α →o part β) (g : α →o β → part γ) : α →o part γ := { to_fun := λ x, f x >>= g x, monotone' := begin intros x y h a, simp only [and_imp, exists_prop, part.bind_eq_bind, part.mem_bind_iff, exists_imp_distrib], intros b hb ha, refine ⟨b, f.monotone h _ hb, g.monotone h _ _ ha⟩, end } end order_hom namespace omega_complete_partial_order /-- A chain is a monotone sequence. See the definition on page 114 of [gunter1992]. -/ def chain (α : Type u) [preorder α] := ℕ →o α namespace chain variables {α : Type u} {β : Type v} {γ : Type*} variables [preorder α] [preorder β] [preorder γ] instance : has_coe_to_fun (chain α) (λ _, ℕ → α) := order_hom.has_coe_to_fun instance [inhabited α] : inhabited (chain α) := ⟨⟨default, λ _ _ _, le_rfl⟩⟩ instance : has_mem α (chain α) := ⟨λ a (c : ℕ →o α), ∃ i, a = c i⟩ variables (c c' : chain α) variables (f : α →o β) variables (g : β →o γ) instance : has_le (chain α) := { le := λ x y, ∀ i, ∃ j, x i ≤ y j } /-- `map` function for `chain` -/ @[simps {fully_applied := ff}] def map : chain β := f.comp c variables {f} lemma mem_map (x : α) : x ∈ c → f x ∈ chain.map c f := λ ⟨i,h⟩, ⟨i, h.symm ▸ rfl⟩ lemma exists_of_mem_map {b : β} : b ∈ c.map f → ∃ a, a ∈ c ∧ f a = b := λ ⟨i,h⟩, ⟨c i, ⟨i, rfl⟩, h.symm⟩ lemma mem_map_iff {b : β} : b ∈ c.map f ↔ ∃ a, a ∈ c ∧ f a = b := ⟨ exists_of_mem_map _, λ h, by { rcases h with ⟨w,h,h'⟩, subst b, apply mem_map c _ h, } ⟩ @[simp] lemma map_id : c.map order_hom.id = c := order_hom.comp_id _ lemma map_comp : (c.map f).map g = c.map (g.comp f) := rfl @[mono] lemma map_le_map {g : α →o β} (h : f ≤ g) : c.map f ≤ c.map g := λ i, by simp [mem_map_iff]; intros; existsi i; apply h /-- `chain.zip` pairs up the elements of two chains that have the same index -/ @[simps] def zip (c₀ : chain α) (c₁ : chain β) : chain (α × β) := order_hom.prod c₀ c₁ end chain end omega_complete_partial_order open omega_complete_partial_order section prio set_option extends_priority 50 /-- An omega-complete partial order is a partial order with a supremum operation on increasing sequences indexed by natural numbers (which we call `ωSup`). In this sense, it is strictly weaker than join complete semi-lattices as only ω-sized totally ordered sets have a supremum. See the definition on page 114 of [gunter1992]. -/ class omega_complete_partial_order (α : Type*) extends partial_order α := (ωSup : chain α → α) (le_ωSup : ∀(c:chain α), ∀ i, c i ≤ ωSup c) (ωSup_le : ∀(c:chain α) x, (∀ i, c i ≤ x) → ωSup c ≤ x) end prio namespace omega_complete_partial_order variables {α : Type u} {β : Type v} {γ : Type*} variables [omega_complete_partial_order α] /-- Transfer a `omega_complete_partial_order` on `β` to a `omega_complete_partial_order` on `α` using a strictly monotone function `f : β →o α`, a definition of ωSup and a proof that `f` is continuous with regard to the provided `ωSup` and the ωCPO on `α`. -/ @[reducible] protected def lift [partial_order β] (f : β →o α) (ωSup₀ : chain β → β) (h : ∀ x y, f x ≤ f y → x ≤ y) (h' : ∀ c, f (ωSup₀ c) = ωSup (c.map f)) : omega_complete_partial_order β := { ωSup := ωSup₀, ωSup_le := λ c x hx, h _ _ (by rw h'; apply ωSup_le; intro; apply f.monotone (hx i)), le_ωSup := λ c i, h _ _ (by rw h'; apply le_ωSup (c.map f)) } lemma le_ωSup_of_le {c : chain α} {x : α} (i : ℕ) (h : x ≤ c i) : x ≤ ωSup c := le_trans h (le_ωSup c _) lemma ωSup_total {c : chain α} {x : α} (h : ∀ i, c i ≤ x ∨ x ≤ c i) : ωSup c ≤ x ∨ x ≤ ωSup c := classical.by_cases (assume : ∀ i, c i ≤ x, or.inl (ωSup_le _ _ this)) (assume : ¬ ∀ i, c i ≤ x, have ∃ i, ¬ c i ≤ x, by simp only [not_forall] at this ⊢; assumption, let ⟨i, hx⟩ := this in have x ≤ c i, from (h i).resolve_left hx, or.inr $ le_ωSup_of_le _ this) @[mono] lemma ωSup_le_ωSup_of_le {c₀ c₁ : chain α} (h : c₀ ≤ c₁) : ωSup c₀ ≤ ωSup c₁ := ωSup_le _ _ $ λ i, Exists.rec_on (h i) $ λ j h, le_trans h (le_ωSup _ _) lemma ωSup_le_iff (c : chain α) (x : α) : ωSup c ≤ x ↔ (∀ i, c i ≤ x) := begin split; intros, { transitivity ωSup c, exact le_ωSup _ _, assumption }, exact ωSup_le _ _ ‹_›, end /-- A subset `p : α → Prop` of the type closed under `ωSup` induces an `omega_complete_partial_order` on the subtype `{a : α // p a}`. -/ def subtype {α : Type*} [omega_complete_partial_order α] (p : α → Prop) (hp : ∀ (c : chain α), (∀ i ∈ c, p i) → p (ωSup c)) : omega_complete_partial_order (subtype p) := omega_complete_partial_order.lift (order_hom.subtype.val p) (λ c, ⟨ωSup _, hp (c.map (order_hom.subtype.val p)) (λ i ⟨n, q⟩, q.symm ▸ (c n).2)⟩) (λ x y h, h) (λ c, rfl) section continuity open chain variables [omega_complete_partial_order β] variables [omega_complete_partial_order γ] /-- A monotone function `f : α →o β` is continuous if it distributes over ωSup. In order to distinguish it from the (more commonly used) continuity from topology (see topology/basic.lean), the present definition is often referred to as "Scott-continuity" (referring to Dana Scott). It corresponds to continuity in Scott topological spaces (not defined here). -/ def continuous (f : α →o β) : Prop := ∀ c : chain α, f (ωSup c) = ωSup (c.map f) /-- `continuous' f` asserts that `f` is both monotone and continuous. -/ def continuous' (f : α → β) : Prop := ∃ hf : monotone f, continuous ⟨f, hf⟩ lemma continuous'.to_monotone {f : α → β} (hf : continuous' f) : monotone f := hf.fst lemma continuous.of_bundled (f : α → β) (hf : monotone f) (hf' : continuous ⟨f, hf⟩) : continuous' f := ⟨hf, hf'⟩ lemma continuous.of_bundled' (f : α →o β) (hf' : continuous f) : continuous' f := ⟨f.mono, hf'⟩ lemma continuous'.to_bundled (f : α → β) (hf : continuous' f) : continuous ⟨f, hf.to_monotone⟩ := hf.snd @[simp, norm_cast] lemma continuous'_coe : ∀ {f : α →o β}, continuous' f ↔ continuous f | ⟨f, hf⟩ := ⟨λ ⟨hf', hc⟩, hc, λ hc, ⟨hf, hc⟩⟩ variables (f : α →o β) (g : β →o γ) lemma continuous_id : continuous (@order_hom.id α _) := by intro; rw c.map_id; refl lemma continuous_comp (hfc : continuous f) (hgc : continuous g) : continuous (g.comp f):= begin dsimp [continuous] at *, intro, rw [hfc,hgc,chain.map_comp] end lemma id_continuous' : continuous' (@id α) := continuous_id.of_bundled' _ lemma continuous_const (x : β) : continuous (order_hom.const α x) := λ c, eq_of_forall_ge_iff $ λ z, by simp [ωSup_le_iff] lemma const_continuous' (x: β) : continuous' (function.const α x) := continuous.of_bundled' (order_hom.const α x) (continuous_const x) end continuity end omega_complete_partial_order namespace part variables {α : Type u} {β : Type v} {γ : Type*} open omega_complete_partial_order lemma eq_of_chain {c : chain (part α)} {a b : α} (ha : some a ∈ c) (hb : some b ∈ c) : a = b := begin cases ha with i ha, replace ha := ha.symm, cases hb with j hb, replace hb := hb.symm, wlog h : i ≤ j := le_total i j using [a b i j, b a j i], rw [eq_some_iff] at ha hb, have := c.monotone h _ ha, apply mem_unique this hb end /-- The (noncomputable) `ωSup` definition for the `ω`-CPO structure on `part α`. -/ protected noncomputable def ωSup (c : chain (part α)) : part α := if h : ∃a, some a ∈ c then some (classical.some h) else none lemma ωSup_eq_some {c : chain (part α)} {a : α} (h : some a ∈ c) : part.ωSup c = some a := have ∃a, some a ∈ c, from ⟨a, h⟩, have a' : some (classical.some this) ∈ c, from classical.some_spec this, calc part.ωSup c = some (classical.some this) : dif_pos this ... = some a : congr_arg _ (eq_of_chain a' h) lemma ωSup_eq_none {c : chain (part α)} (h : ¬∃a, some a ∈ c) : part.ωSup c = none := dif_neg h lemma mem_chain_of_mem_ωSup {c : chain (part α)} {a : α} (h : a ∈ part.ωSup c) : some a ∈ c := begin simp [part.ωSup] at h, split_ifs at h, { have h' := classical.some_spec h_1, rw ← eq_some_iff at h, rw ← h, exact h' }, { rcases h with ⟨ ⟨ ⟩ ⟩ } end noncomputable instance omega_complete_partial_order : omega_complete_partial_order (part α) := { ωSup := part.ωSup, le_ωSup := λ c i, by { intros x hx, rw ← eq_some_iff at hx ⊢, rw [ωSup_eq_some, ← hx], rw ← hx, exact ⟨i,rfl⟩ }, ωSup_le := by { rintros c x hx a ha, replace ha := mem_chain_of_mem_ωSup ha, cases ha with i ha, apply hx i, rw ← ha, apply mem_some } } section inst lemma mem_ωSup (x : α) (c : chain (part α)) : x ∈ ωSup c ↔ some x ∈ c := begin simp [omega_complete_partial_order.ωSup,part.ωSup], split, { split_ifs, swap, rintro ⟨⟨⟩⟩, intro h', have hh := classical.some_spec h, simp at h', subst x, exact hh }, { intro h, have h' : ∃ (a : α), some a ∈ c := ⟨_,h⟩, rw dif_pos h', have hh := classical.some_spec h', rw eq_of_chain hh h, simp } end end inst end part namespace pi variables {α : Type*} {β : α → Type*} {γ : Type*} open omega_complete_partial_order omega_complete_partial_order.chain instance [∀a, omega_complete_partial_order (β a)] : omega_complete_partial_order (Πa, β a) := { ωSup := λc a, ωSup (c.map (pi.eval_order_hom a)), ωSup_le := assume c f hf a, ωSup_le _ _ $ by { rintro i, apply hf }, le_ωSup := assume c i x, le_ωSup_of_le _ $ le_rfl } namespace omega_complete_partial_order variables [∀ x, omega_complete_partial_order $ β x] variables [omega_complete_partial_order γ] lemma flip₁_continuous' (f : ∀ x : α, γ → β x) (a : α) (hf : continuous' (λ x y, f y x)) : continuous' (f a) := continuous.of_bundled _ (λ x y h, hf.to_monotone h a) (λ c, congr_fun (hf.to_bundled _ c) a) lemma flip₂_continuous' (f : γ → Π x, β x) (hf : ∀ x, continuous' (λ g, f g x)) : continuous' f := continuous.of_bundled _ (λ x y h a, (hf a).to_monotone h) (by intro c; ext a; apply (hf a).to_bundled _ c) end omega_complete_partial_order end pi namespace prod open omega_complete_partial_order variables {α : Type*} {β : Type*} {γ : Type*} variables [omega_complete_partial_order α] variables [omega_complete_partial_order β] variables [omega_complete_partial_order γ] /-- The supremum of a chain in the product `ω`-CPO. -/ @[simps] protected def ωSup (c : chain (α × β)) : α × β := (ωSup (c.map order_hom.fst), ωSup (c.map order_hom.snd)) @[simps ωSup_fst ωSup_snd] instance : omega_complete_partial_order (α × β) := { ωSup := prod.ωSup, ωSup_le := λ c ⟨x,x'⟩ h, ⟨ωSup_le _ _ $ λ i, (h i).1, ωSup_le _ _ $ λ i, (h i).2⟩, le_ωSup := λ c i, ⟨le_ωSup (c.map order_hom.fst) i, le_ωSup (c.map order_hom.snd) i⟩ } lemma ωSup_zip (c₀ : chain α) (c₁ : chain β) : ωSup (c₀.zip c₁) = (ωSup c₀, ωSup c₁) := begin apply eq_of_forall_ge_iff, rintro ⟨z₁,z₂⟩, simp [ωSup_le_iff, forall_and_distrib], end end prod open omega_complete_partial_order namespace complete_lattice variables (α : Type u) /-- Any complete lattice has an `ω`-CPO structure where the countable supremum is a special case of arbitrary suprema. -/ @[priority 100] -- see Note [lower instance priority] instance [complete_lattice α] : omega_complete_partial_order α := { ωSup := λc, ⨆ i, c i, ωSup_le := λ ⟨c, _⟩ s hs, by simp only [supr_le_iff, order_hom.coe_fun_mk] at ⊢ hs; intros i; apply hs i, le_ωSup := assume ⟨c, _⟩ i, by simp only [order_hom.coe_fun_mk]; apply le_supr_of_le i; refl } variables {α} {β : Type v} [omega_complete_partial_order α] [complete_lattice β] lemma Sup_continuous (s : set $ α →o β) (hs : ∀ f ∈ s, continuous f) : continuous (Sup s) := begin intro c, apply eq_of_forall_ge_iff, intro z, suffices : (∀ (f ∈ s) n, (f : _) (c n) ≤ z) ↔ (∀ n (f ∈ s), (f : _) (c n) ≤ z), by simpa [ωSup_le_iff, hs _ _ _] { contextual := tt }, exact ⟨λ H n f hf, H f hf n, λ H f hf n, H n f hf⟩ end lemma supr_continuous {ι : Sort*} {f : ι → α →o β} (h : ∀ i, continuous (f i)) : continuous (⨆ i, f i) := Sup_continuous _ $ set.forall_range_iff.2 h theorem Sup_continuous' (s : set (α → β)) (hc : ∀ f ∈ s, continuous' f) : continuous' (Sup s) := begin lift s to set (α →o β) using λ f hf, (hc f hf).to_monotone, simp only [set.ball_image_iff, continuous'_coe] at hc, rw [Sup_image], norm_cast, exact supr_continuous (λ f, supr_continuous (λ hf, hc f hf)), end lemma sup_continuous {f g : α →o β} (hf : continuous f) (hg : continuous g) : continuous (f ⊔ g) := begin rw ← Sup_pair, apply Sup_continuous, rintro f (rfl|rfl|_); assumption end lemma top_continuous : continuous (⊤ : α →o β) := begin intro c, apply eq_of_forall_ge_iff, intro z, simp only [ωSup_le_iff, forall_const, chain.map_coe, (∘), function.const, order_hom.has_top_top, order_hom.const_coe_coe], end lemma bot_continuous : continuous (⊥ : α →o β) := begin rw ← Sup_empty, exact Sup_continuous _ (λ f hf, hf.elim), end end complete_lattice namespace complete_lattice variables {α β : Type*} [omega_complete_partial_order α] [complete_linear_order β] lemma inf_continuous (f g : α →o β) (hf : continuous f) (hg : continuous g) : continuous (f ⊓ g) := begin refine λ c, eq_of_forall_ge_iff (λ z, _), simp only [inf_le_iff, hf c, hg c, ωSup_le_iff, ←forall_or_distrib_left, ←forall_or_distrib_right, function.comp_app, chain.map_coe, order_hom.has_inf_inf_coe], exact ⟨λ h _, h _ _, λ h i j, (h (max i j)).imp (le_trans $ f.mono $ c.mono $ le_max_left _ _) (le_trans $ g.mono $ c.mono $ le_max_right _ _)⟩, end lemma inf_continuous' {f g : α → β} (hf : continuous' f) (hg : continuous' g) : continuous' (f ⊓ g) := ⟨_, inf_continuous _ _ hf.snd hg.snd⟩ end complete_lattice namespace omega_complete_partial_order variables {α : Type u} {α' : Type*} {β : Type v} {β' : Type*} {γ : Type*} {φ : Type*} variables [omega_complete_partial_order α] [omega_complete_partial_order β] variables [omega_complete_partial_order γ] [omega_complete_partial_order φ] variables [omega_complete_partial_order α'] [omega_complete_partial_order β'] namespace order_hom /-- The `ωSup` operator for monotone functions. -/ @[simps] protected def ωSup (c : chain (α →o β)) : α →o β := { to_fun := λ a, ωSup (c.map (order_hom.apply a)), monotone' := λ x y h, ωSup_le_ωSup_of_le (chain.map_le_map _ $ λ a, a.monotone h) } @[simps ωSup_coe] instance omega_complete_partial_order : omega_complete_partial_order (α →o β) := omega_complete_partial_order.lift order_hom.coe_fn_hom order_hom.ωSup (λ x y h, h) (λ c, rfl) end order_hom section variables (α β) /-- A monotone function on `ω`-continuous partial orders is said to be continuous if for every chain `c : chain α`, `f (⊔ i, c i) = ⊔ i, f (c i)`. This is just the bundled version of `order_hom.continuous`. -/ structure continuous_hom extends order_hom α β := (cont : continuous (order_hom.mk to_fun monotone')) attribute [nolint doc_blame] continuous_hom.to_order_hom infixr ` →𝒄 `:25 := continuous_hom -- Input: \r\MIc instance : has_coe_to_fun (α →𝒄 β) (λ _, α → β) := ⟨λ f, f.to_order_hom.to_fun⟩ instance : has_coe (α →𝒄 β) (α →o β) := { coe := continuous_hom.to_order_hom } instance : partial_order (α →𝒄 β) := partial_order.lift (λ f, f.to_order_hom.to_fun) $ by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ h; congr; exact h /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def continuous_hom.simps.apply (h : α →𝒄 β) : α → β := h initialize_simps_projections continuous_hom (to_order_hom_to_fun → apply, -to_order_hom) end namespace continuous_hom theorem congr_fun {f g : α →𝒄 β} (h : f = g) (x : α) : f x = g x := congr_arg (λ h : α →𝒄 β, h x) h theorem congr_arg (f : α →𝒄 β) {x y : α} (h : x = y) : f x = f y := congr_arg (λ x : α, f x) h protected lemma monotone (f : α →𝒄 β) : monotone f := f.monotone' @[mono] lemma apply_mono {f g : α →𝒄 β} {x y : α} (h₁ : f ≤ g) (h₂ : x ≤ y) : f x ≤ g y := order_hom.apply_mono (show (f : α →o β) ≤ g, from h₁) h₂ lemma ite_continuous' {p : Prop} [hp : decidable p] (f g : α → β) (hf : continuous' f) (hg : continuous' g) : continuous' (λ x, if p then f x else g x) := by split_ifs; simp * lemma ωSup_bind {β γ : Type v} (c : chain α) (f : α →o part β) (g : α →o β → part γ) : ωSup (c.map (f.bind g)) = ωSup (c.map f) >>= ωSup (c.map g) := begin apply eq_of_forall_ge_iff, intro x, simp only [ωSup_le_iff, part.bind_le, chain.mem_map_iff, and_imp, order_hom.bind_coe, exists_imp_distrib], split; intro h''', { intros b hb, apply ωSup_le _ _ _, rintros i y hy, simp only [part.mem_ωSup] at hb, rcases hb with ⟨j,hb⟩, replace hb := hb.symm, simp only [part.eq_some_iff, chain.map_coe, function.comp_app, order_hom.apply_coe] at hy hb, replace hb : b ∈ f (c (max i j)) := f.mono (c.mono (le_max_right i j)) _ hb, replace hy : y ∈ g (c (max i j)) b := g.mono (c.mono (le_max_left i j)) _ _ hy, apply h''' (max i j), simp only [exists_prop, part.bind_eq_bind, part.mem_bind_iff, chain.map_coe, function.comp_app, order_hom.bind_coe], exact ⟨_,hb,hy⟩, }, { intros i, intros y hy, simp only [exists_prop, part.bind_eq_bind, part.mem_bind_iff, chain.map_coe, function.comp_app, order_hom.bind_coe] at hy, rcases hy with ⟨b,hb₀,hb₁⟩, apply h''' b _, { apply le_ωSup (c.map g) _ _ _ hb₁ }, { apply le_ωSup (c.map f) i _ hb₀ } }, end lemma bind_continuous' {β γ : Type v} (f : α → part β) (g : α → β → part γ) : continuous' f → continuous' g → continuous' (λ x, f x >>= g x) | ⟨hf,hf'⟩ ⟨hg,hg'⟩ := continuous.of_bundled' (order_hom.bind ⟨f,hf⟩ ⟨g,hg⟩) (by intro c; rw [ωSup_bind, ← hf', ← hg']; refl) lemma map_continuous' {β γ : Type v} (f : β → γ) (g : α → part β) (hg : continuous' g) : continuous' (λ x, f <$> g x) := by simp only [map_eq_bind_pure_comp]; apply bind_continuous' _ _ hg; apply const_continuous' lemma seq_continuous' {β γ : Type v} (f : α → part (β → γ)) (g : α → part β) (hf : continuous' f) (hg : continuous' g) : continuous' (λ x, f x <*> g x) := by simp only [seq_eq_bind_map]; apply bind_continuous' _ _ hf; apply pi.omega_complete_partial_order.flip₂_continuous'; intro; apply map_continuous' _ _ hg lemma continuous (F : α →𝒄 β) (C : chain α) : F (ωSup C) = ωSup (C.map F) := continuous_hom.cont _ _ /-- Construct a continuous function from a bare function, a continuous function, and a proof that they are equal. -/ @[simps, reducible] def of_fun (f : α → β) (g : α →𝒄 β) (h : f = g) : α →𝒄 β := by refine {to_order_hom := {to_fun := f, ..}, ..}; subst h; rcases g with ⟨⟨⟩⟩; assumption /-- Construct a continuous function from a monotone function with a proof of continuity. -/ @[simps, reducible] def of_mono (f : α →o β) (h : ∀ c : chain α, f (ωSup c) = ωSup (c.map f)) : α →𝒄 β := { to_fun := f, monotone' := f.monotone, cont := h } /-- The identity as a continuous function. -/ @[simps] def id : α →𝒄 α := of_mono order_hom.id continuous_id /-- The composition of continuous functions. -/ @[simps] def comp (f : β →𝒄 γ) (g : α →𝒄 β) : α →𝒄 γ := of_mono (order_hom.comp (↑f) (↑g)) (continuous_comp _ _ g.cont f.cont) @[ext] protected lemma ext (f g : α →𝒄 β) (h : ∀ x, f x = g x) : f = g := by cases f; cases g; congr; ext; apply h protected lemma coe_inj (f g : α →𝒄 β) (h : (f : α → β) = g) : f = g := continuous_hom.ext _ _ $ _root_.congr_fun h @[simp] lemma comp_id (f : β →𝒄 γ) : f.comp id = f := by ext; refl @[simp] lemma id_comp (f : β →𝒄 γ) : id.comp f = f := by ext; refl @[simp] lemma comp_assoc (f : γ →𝒄 φ) (g : β →𝒄 γ) (h : α →𝒄 β) : f.comp (g.comp h) = (f.comp g).comp h := by ext; refl @[simp] lemma coe_apply (a : α) (f : α →𝒄 β) : (f : α →o β) a = f a := rfl /-- `function.const` is a continuous function. -/ def const (x : β) : α →𝒄 β := of_mono (order_hom.const _ x) (continuous_const x) @[simp] theorem const_apply (f : β) (a : α) : const f a = f := rfl instance [inhabited β] : inhabited (α →𝒄 β) := ⟨ const default ⟩ /-- The map from continuous functions to monotone functions is itself a monotone function. -/ @[simps] def to_mono : (α →𝒄 β) →o (α →o β) := { to_fun := λ f, f, monotone' := λ x y h, h } /-- When proving that a chain of applications is below a bound `z`, it suffices to consider the functions and values being selected from the same index in the chains. This lemma is more specific than necessary, i.e. `c₀` only needs to be a chain of monotone functions, but it is only used with continuous functions. -/ @[simp] lemma forall_forall_merge (c₀ : chain (α →𝒄 β)) (c₁ : chain α) (z : β) : (∀ (i j : ℕ), (c₀ i) (c₁ j) ≤ z) ↔ ∀ (i : ℕ), (c₀ i) (c₁ i) ≤ z := begin split; introv h, { apply h }, { apply le_trans _ (h (max i j)), transitivity c₀ i (c₁ (max i j)), { apply (c₀ i).monotone, apply c₁.monotone, apply le_max_right }, { apply c₀.monotone, apply le_max_left } } end @[simp] lemma forall_forall_merge' (c₀ : chain (α →𝒄 β)) (c₁ : chain α) (z : β) : (∀ (j i : ℕ), (c₀ i) (c₁ j) ≤ z) ↔ ∀ (i : ℕ), (c₀ i) (c₁ i) ≤ z := by rw [forall_swap,forall_forall_merge] /-- The `ωSup` operator for continuous functions, which takes the pointwise countable supremum of the functions in the `ω`-chain. -/ @[simps] protected def ωSup (c : chain (α →𝒄 β)) : α →𝒄 β := continuous_hom.of_mono (ωSup $ c.map to_mono) begin intro c', apply eq_of_forall_ge_iff, intro z, simp only [ωSup_le_iff, (c _).continuous, chain.map_coe, order_hom.apply_coe, to_mono_coe, coe_apply, order_hom.omega_complete_partial_order_ωSup_coe, forall_forall_merge, forall_forall_merge', (∘), function.eval], end @[simps ωSup] instance : omega_complete_partial_order (α →𝒄 β) := omega_complete_partial_order.lift continuous_hom.to_mono continuous_hom.ωSup (λ x y h, h) (λ c, rfl) namespace prod /-- The application of continuous functions as a continuous function. -/ @[simps] def apply : (α →𝒄 β) × α →𝒄 β := { to_fun := λ f, f.1 f.2, monotone' := λ x y h, by {dsimp, transitivity y.fst x.snd; [apply h.1, apply y.1.monotone h.2]}, cont := begin intro c, apply le_antisymm, { apply ωSup_le, intros i, dsimp, rw (c _).fst.continuous, apply ωSup_le, intros j, apply le_ωSup_of_le (max i j), apply apply_mono, exact monotone_fst (order_hom.mono _ (le_max_left _ _)), exact monotone_snd (order_hom.mono _ (le_max_right _ _)), }, { apply ωSup_le, intros i, apply le_ωSup_of_le i, dsimp, apply order_hom.mono _, apply le_ωSup_of_le i, reflexivity, } end } end prod lemma ωSup_def (c : chain (α →𝒄 β)) (x : α) : ωSup c x = continuous_hom.ωSup c x := rfl lemma ωSup_apply_ωSup (c₀ : chain (α →𝒄 β)) (c₁ : chain α) : ωSup c₀ (ωSup c₁) = prod.apply (ωSup (c₀.zip c₁)) := by simp [prod.apply_apply, prod.ωSup_zip] /-- A family of continuous functions yields a continuous family of functions. -/ @[simps] def flip {α : Type*} (f : α → β →𝒄 γ) : β →𝒄 α → γ := { to_fun := λ x y, f y x, monotone' := λ x y h a, (f a).monotone h, cont := by intro; ext; change f x _ = _; rw [(f x).continuous ]; refl, } /-- `part.bind` as a continuous function. -/ @[simps { rhs_md := reducible }] noncomputable def bind {β γ : Type v} (f : α →𝒄 part β) (g : α →𝒄 β → part γ) : α →𝒄 part γ := of_mono (order_hom.bind (↑f) (↑g)) $ λ c, begin rw [order_hom.bind, ← order_hom.bind, ωSup_bind, ← f.continuous, ← g.continuous], refl end /-- `part.map` as a continuous function. -/ @[simps {rhs_md := reducible}] noncomputable def map {β γ : Type v} (f : β → γ) (g : α →𝒄 part β) : α →𝒄 part γ := of_fun (λ x, f <$> g x) (bind g (const (pure ∘ f))) $ by ext; simp only [map_eq_bind_pure_comp, bind_apply, order_hom.bind_coe, const_apply, order_hom.const_coe_coe, coe_apply] /-- `part.seq` as a continuous function. -/ @[simps {rhs_md := reducible}] noncomputable def seq {β γ : Type v} (f : α →𝒄 part (β → γ)) (g : α →𝒄 part β) : α →𝒄 part γ := of_fun (λ x, f x <*> g x) (bind f $ (flip $ _root_.flip map g)) (by ext; simp only [seq_eq_bind_map, flip, part.bind_eq_bind, map_apply, part.mem_bind_iff, bind_apply, order_hom.bind_coe, coe_apply, flip_apply]; refl) end continuous_hom end omega_complete_partial_order
7eeceb972d7501c8717d23a886b3ef9fe86c15f8
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/998.lean
89ba3044a858da11fd4130c78f9de7a972dcbfae
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
470
lean
import Lean set_option maxHeartbeats 100000 attribute [simp] Array.findIdx?.loop attribute [simp] Lean.expandExplicitBindersAux.loop attribute [simp] Lean.Elab.Term.resolveLocalName.loop -- Mathlib -- attribute [simp] BinaryHeap.heapifyDown -- attribute [simp] ByteSlice.forIn.loop -- attribute [simp] Lean.Export.exportName -- Fixed see 998Export.lean -- attribute [simp] Lean.Export.exportLevel -- Fixed see 998Export.lean -- attribute [simp] Array.heapSort.loop
28368e55a1677963b405a95008ea0a74660f8d3a
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/analysis/calculus/times_cont_diff.lean
7e23554f84607e6532327c4918aeca871a5b3e48
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
35,752
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 analysis.calculus.deriv /-! # Higher differentiabiliity A function is `C^1` on a domain if it is differentiable there, and its derivative is continuous. By induction, it is `C^n` if it is `C^{n-1}` and its (n-1)-th derivative is `C^1` there or, equivalently, if it is `C^1` and its derivative is `C^{n-1}`. Finally, it is `C^∞` if it is `C^n` for all n. We formalize these notions by defining iteratively the n-th derivative of a function at the (n-1)-th derivative of the derivative. It is called `iterated_fderiv k n f x` where `k` is the field, `n` is the number of iterations, `f` is the function and `x` is the point. We also define a version `iterated_fderiv_within` relative to a domain, as well as predicates `times_cont_diff k n f` and `times_cont_diff_on k n f s` saying that the function is `C^n`, respectively in the whole space or on the set `s`. We prove basic properties of these notions. ## Implementation notes The n-th derivative of a function belongs to the space E →L[k] (E →L[k] (E ... F)...))), where there are n iterations of `E →L[k]`. We define this space inductively, call it `iterated_continuous_linear_map k n E F`, and denote it by `E [×n]→L[k] F`. We can define it inductively either from the left (i.e., saying that the (n+1)-th space S_{n+1} is E →L[k] S_n) or from the right (i.e., saying that the (n+1)-th space associated to F, denoted by S_{n+1} (F), is equal to S_n (E →L[k] F)). For proofs, it turns out to be more convenient to use the latter approach (from the right), as it means to prove things at the (n+1)-th step we only need to understand well enough the derivative in E →L[k] F (contrary to the approach from the left, where one would need to know enough on the n-th derivative to deduce things on the (n+1)-th derivative). In other words, one could define the (n+1)-th derivative either as the derivative of the n-th derivative, or as the n-th derivative of the derivative. We use the latter definition. A difficulty is related to universes: the first and second spaces in the sequence, for n=0 and 1, are F and E →L[k] F. If E has universe u and F has universe v, then the first one lives in v and the second one in max v u. Since they should live in the same universe (as all the other spaces in the construction), it means that at the 0-th step we should not use F, but ulift it to universe max v u. But we should also ulift its vector space structure and its normed space structure. This can certainly be done, but I decided it was not worth it for now. Therefore, the definition is only made when E and F live in the same universe. Regarding the definition of `C^n` functions, there are two equivalent definitions: * require by induction that the function is differentiable, and that its derivative is C^{n-1} * or require that, for all m ≤ n, the m-th derivative is continuous, and for all m < n the m-th derivative is differentiable. The first definition is more efficient for many proofs by induction. The second definition is more satisfactory as it gives concrete information about the n-th derivative (contrary to the first point of view), and moreover it also makes sense for n = ∞. Therefore, we give (and use) both definitions, named respectively `times_cont_diff_rec` and `times_cont_diff` (as well as relativized versions on a set). We show that they are equivalent. The first one is mainly auxiliary: in applications, one should always use `times_cont_diff` (but the proofs below use heavily the equivalence to show that `times_cont_diff` is well behaved). ## Tags derivative, differentiability, higher derivative, C^n -/ noncomputable theory local attribute [instance, priority 0] classical.decidable_inhabited classical.prop_decidable universes u v w open set variables {k : Type*} [nondiscrete_normed_field k] {E : Type u} [normed_group E] [normed_space k E] {F : Type u} [normed_group F] [normed_space k F] {G : Type u} [normed_group G] [normed_space k G] {s s₁ u : set E} {f f₁ : E → F} {f' f₁' : E →L[k] F} {f₂ : E → G} {f₂' : E →L[k] G} {g : F → G} {x : E} {c : F} {L : filter E} {t : set F} {b : E × F → G} {sb : set (E × F)} {p : E × F} {n : ℕ} include k /-- The space `iterated_continuous_linear_map k n E F` is the space E →L[k] (E →L[k] (E ... F)...))), defined inductively over `n`. This is the space to which the `n`-th derivative of a function naturally belongs. It is only defined when `E` and `F` live in the same universe. -/ def iterated_continuous_linear_map (k : Type w) [nondiscrete_normed_field k] : Π (n : ℕ) (E : Type u) [gE : normed_group E] [@normed_space k E _ gE] (F : Type u) [gF : normed_group F] [@normed_space k F _ gF], Type u | 0 E _ _ F _ _ := F | (n+1) E _ _ F _ _ := by { resetI, exact iterated_continuous_linear_map n E (E →L[k] F) } notation E `[×`:25 n `]→L[`:25 k `] ` F := iterated_continuous_linear_map k n E F /-- Define by induction a normed group structure on the space of iterated continuous linear maps. To avoid `resetI` in the statement, use the @ version with all parameters. As the equation compiler chokes on this one, we use the `nat.rec_on` version. -/ def iterated_continuous_linear_map.normed_group_rec (k : Type w) [hk : nondiscrete_normed_field k] (n : ℕ) (E : Type u) [gE : normed_group E] [sE : normed_space k E] : ∀(F : Type u) [nF : normed_group F] [sF : @normed_space k F _ nF], normed_group (@iterated_continuous_linear_map k hk n E gE sE F nF sF) := nat.rec_on n (λF nF sF, nF) (λn aux_n F nF sF, by { resetI, apply aux_n }) /-- Define by induction a normed space structure on the space of iterated continuous linear maps. To avoid `resetI` in the statement, use the @ version with all parameters. As the equation compiler chokes on this one, we use the `nat.rec_on` version. -/ def iterated_continuous_linear_map.normed_space_rec (k : Type w) [hk : nondiscrete_normed_field k] (n : ℕ) (E : Type u) [gE : normed_group E] [sE : normed_space k E] : ∀(F : Type u) [nF : normed_group F] [sF : @normed_space k F _ nF], @normed_space k (@iterated_continuous_linear_map k hk n E gE sE F nF sF) _ (@iterated_continuous_linear_map.normed_group_rec k hk n E gE sE F nF sF) := nat.rec_on n (λF nF sF, sF) (λn aux_n F nF sF, by { resetI, apply aux_n }) /-- Explicit normed group structure on the space of iterated continuous linear maps. -/ instance iterated_continuous_linear_map.normed_group (n : ℕ) (k : Type w) [hk : nondiscrete_normed_field k] (E : Type u) [gE : normed_group E] [sE : normed_space k E] (F : Type u) [gF : normed_group F] [sF : normed_space k F] : normed_group (E [×n]→L[k] F) := iterated_continuous_linear_map.normed_group_rec k n E F /-- Explicit normed space structure on the space of iterated continuous linear maps. -/ instance iterated_continuous_linear_map.normed_space (n : ℕ) (k : Type w) [hk : nondiscrete_normed_field k] (E : Type u) [gE : normed_group E] [sE : normed_space k E] (F : Type u) [gF : normed_group F] [sF : normed_space k F] : normed_space k (E [×n]→L[k] F) := iterated_continuous_linear_map.normed_space_rec k n E F /-- The n-th derivative of a function, defined inductively by saying that the (n+1)-th derivative of f is the n-th derivative of the derivative of f. -/ def iterated_fderiv (k : Type w) [hk : nondiscrete_normed_field k] (n : ℕ) {E : Type u} [gE : normed_group E] [sE : normed_space k E] : ∀{F : Type u} [gF : normed_group F] [sF : @normed_space k F _ gF] (f : E → F), E → @iterated_continuous_linear_map k hk n E gE sE F gF sF := nat.rec_on n (λF gF sF f, f) (λn rec F gF sF f, by { resetI, exact rec (fderiv k f) }) @[simp] lemma iterated_fderiv_zero : iterated_fderiv k 0 f = f := rfl @[simp] lemma iterated_fderiv_succ : iterated_fderiv k (n+1) f = (iterated_fderiv k n (λx, fderiv k f x) : _) := rfl /-- The n-th derivative of a function along a set, defined inductively by saying that the (n+1)-th derivative of f is the n-th derivative of the derivative of f. -/ def iterated_fderiv_within (k : Type w) [hk :nondiscrete_normed_field k] (n : ℕ) {E : Type u} [gE : normed_group E] [sE : normed_space k E] : ∀{F : Type u} [gF : normed_group F] [sF : @normed_space k F _ gF] (f : E → F) (s : set E), E → @iterated_continuous_linear_map k hk n E gE sE F gF sF := nat.rec_on n (λF gF sF f s, f) (λn rec F gF sF f s, by { resetI, exact rec (fderiv_within k f s) s}) @[simp] lemma iterated_fderiv_within_zero : iterated_fderiv_within k 0 f s = f := rfl @[simp] lemma iterated_fderiv_within_succ : iterated_fderiv_within k (n+1) f s = (iterated_fderiv_within k n (λx, fderiv_within k f s x) s : _) := rfl theorem iterated_fderiv_within_univ {n : ℕ} : iterated_fderiv_within k n f univ = iterated_fderiv k n f := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F, { refl }, { simp [IH] } end /-- If two functions coincide on a set `s` of unique differentiability, then their iterated differentials within this set coincide. -/ lemma iterated_fderiv_within_congr (hs : unique_diff_on k s) (hL : ∀y∈s, f₁ y = f y) (hx : x ∈ s) : iterated_fderiv_within k n f₁ s x = iterated_fderiv_within k n f s x := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f x, { simp [hL x hx] }, { simp only [iterated_fderiv_within_succ], refine IH (λy hy, _) hx, apply fderiv_within_congr (hs y hy) hL (hL y hy) } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with an open set containing `x`. -/ lemma iterated_fderiv_within_inter_open (xu : x ∈ u) (hu : is_open u) (xs : x ∈ s) (hs : unique_diff_on k (s ∩ u)) : iterated_fderiv_within k n f (s ∩ u) x = iterated_fderiv_within k n f s x := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { simp }, { simp, rw ← IH, apply iterated_fderiv_within_congr hs (λy hy, _) ⟨xs, xu⟩, apply fderiv_within_inter (mem_nhds_sets hu hy.2), have := hs y hy, rwa unique_diff_within_at_inter (mem_nhds_sets hu hy.2) at this } end /-- The iterated differential within a set `s` at a point `x` is not modified if one intersects `s` with a neighborhood of `x`. -/ lemma iterated_fderiv_within_inter (hu : u ∈ nhds x) (xs : x ∈ s) (hs : unique_diff_on k s) : iterated_fderiv_within k n f (s ∩ u) x = iterated_fderiv_within k n f s x := begin rcases mem_nhds_sets_iff.1 hu with ⟨v, vu, v_open, xv⟩, have A : (s ∩ u) ∩ v = s ∩ v, { apply subset.antisymm (inter_subset_inter (inter_subset_left _ _) (subset.refl _)), exact λ y ⟨ys, yv⟩, ⟨⟨ys, vu yv⟩, yv⟩ }, have : iterated_fderiv_within k n f (s ∩ v) x = iterated_fderiv_within k n f s x := iterated_fderiv_within_inter_open xv v_open xs (unique_diff_on_inter hs v_open), rw ← this, have : iterated_fderiv_within k n f ((s ∩ u) ∩ v) x = iterated_fderiv_within k n f (s ∩ u) x, { refine iterated_fderiv_within_inter_open xv v_open ⟨xs, mem_of_nhds hu⟩ _, rw A, exact unique_diff_on_inter hs v_open }, rw A at this, rw ← this end /-- Auxiliary definition defining `C^n` functions by induction over `n`. In applications, use `times_cont_diff` instead. -/ def times_cont_diff_rec (k : Type w) [nondiscrete_normed_field k] : Π (n : ℕ) {E : Type u} [gE : normed_group E] [@normed_space k E _ gE] {F : Type u} [gF : normed_group F] [@normed_space k F _ gF] (f : E → F), Prop | 0 E _ _ F _ _ f := by { resetI, exact continuous f } | (n+1) E _ _ F _ _ f := by { resetI, exact differentiable k f ∧ times_cont_diff_rec n (fderiv k f) } @[simp] lemma times_cont_diff_rec_zero : times_cont_diff_rec k 0 f ↔ continuous f := by refl @[simp] lemma times_cont_diff_rec_succ : times_cont_diff_rec k n.succ f ↔ differentiable k f ∧ times_cont_diff_rec k n (λx, fderiv k f x) := by refl lemma times_cont_diff_rec.of_succ (h : times_cont_diff_rec k n.succ f) : times_cont_diff_rec k n f := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F, { exact h.1.continuous }, { rw times_cont_diff_rec_succ at h ⊢, exact ⟨h.1, IH h.2⟩ } end lemma times_cont_diff_rec.continuous (h : times_cont_diff_rec k n f) : continuous (iterated_fderiv k n f) := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { exact h }, { rw iterated_fderiv_succ, exact IH (times_cont_diff_rec_succ.1 h).2 } end lemma times_cont_diff_rec.differentiable (h : times_cont_diff_rec k (n+1) f) : differentiable k (iterated_fderiv k n f) := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { exact h.1 }, { rw iterated_fderiv_succ, apply IH h.2 } end /-- Auxiliary definition defining `C^n` functions on a set by induction over `n`. In applications, use `times_cont_diff_on` instead. -/ def times_cont_diff_on_rec (k : Type w) [nondiscrete_normed_field k] : Π (n : ℕ) {E : Type u} [gE : normed_group E] [@normed_space k E _ gE] {F : Type u} [gF : normed_group F] [@normed_space k F _ gF] (f : E → F) (s : set E), Prop | 0 E _ _ F _ _ f s := by { resetI, exact continuous_on f s } | (n+1) E _ _ F _ _ f s := by { resetI, exact differentiable_on k f s ∧ times_cont_diff_on_rec n (fderiv_within k f s) s} @[simp] lemma times_cont_diff_on_rec_zero : times_cont_diff_on_rec k 0 f s ↔ continuous_on f s := by refl @[simp] lemma times_cont_diff_on_rec_succ : times_cont_diff_on_rec k n.succ f s ↔ differentiable_on k f s ∧ times_cont_diff_on_rec k n (λx, fderiv_within k f s x) s := by refl lemma times_cont_diff_on_rec.of_succ (h : times_cont_diff_on_rec k n.succ f s) : times_cont_diff_on_rec k n f s := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F, { exact h.1.continuous_on }, { rw times_cont_diff_on_rec_succ at h ⊢, exact ⟨h.1, IH h.2⟩ } end lemma times_cont_diff_on_rec.continuous_on_iterated_fderiv_within (h : times_cont_diff_on_rec k n f s) : continuous_on (iterated_fderiv_within k n f s) s := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { exact h }, { rw iterated_fderiv_within_succ, exact IH (times_cont_diff_on_rec_succ.1 h).2 } end lemma times_cont_diff_on_rec.differentiable_on (h : times_cont_diff_on_rec k (n+1) f s) : differentiable_on k (iterated_fderiv_within k n f s) s := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { exact h.1 }, { rw iterated_fderiv_within_succ, apply IH h.2 } end lemma times_cont_diff_on_rec_univ : times_cont_diff_on_rec k n f univ ↔ times_cont_diff_rec k n f := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { rw [times_cont_diff_on_rec_zero, times_cont_diff_rec_zero, continuous_iff_continuous_on_univ] }, { rw [times_cont_diff_on_rec_succ, times_cont_diff_rec_succ, differentiable_on_univ, fderiv_within_univ, IH] } end /-- A function is `C^n` on a set, for `n : with_top ℕ`, if its derivatives of order at most `n` are all well defined and continuous. -/ def times_cont_diff_on (k : Type w) [nondiscrete_normed_field k] (n : with_top ℕ) {E F : Type u} [normed_group E] [normed_space k E] [normed_group F] [normed_space k F] (f : E → F) (s : set E) := (∀m:ℕ, (m : with_top ℕ) ≤ n → continuous_on (iterated_fderiv_within k m f s) s) ∧ (∀m:ℕ, (m : with_top ℕ) < n → differentiable_on k (iterated_fderiv_within k m f s) s) @[simp] lemma times_cont_diff_on_zero : times_cont_diff_on k 0 f s ↔ continuous_on f s := by simp [times_cont_diff_on] /-- The two definitions of `C^n` functions on domains, directly in terms of continuity of all derivatives, or by induction, are equivalent. -/ theorem times_cont_diff_on_iff_times_cont_diff_on_rec : times_cont_diff_on k n f s ↔ times_cont_diff_on_rec k n f s := begin tactic.unfreeze_local_instances, induction n with n IH generalizing F f, { rw [with_top.coe_zero, times_cont_diff_on_rec_zero, times_cont_diff_on_zero] }, { split, { assume H, rw times_cont_diff_on_rec_succ, refine ⟨H.2 0 (by simp only [with_top.zero_lt_coe, with_top.coe_zero, nat.succ_pos n]), _⟩, rw ← IH, split, { assume m hm, have : (m.succ : with_top nat) ≤ n.succ := with_top.coe_le_coe.2 (nat.succ_le_succ (with_top.coe_le_coe.1 hm)), exact H.1 _ this }, { assume m hm, have : (m.succ : with_top nat) < n.succ := with_top.coe_lt_coe.2 (nat.succ_le_succ (with_top.coe_lt_coe.1 hm)), exact H.2 _ this } }, { assume H, split, { assume m hm, simp only [with_top.coe_le_coe] at hm, cases nat.of_le_succ hm with h h, { have := H.of_succ, rw ← IH at this, exact this.1 _ (with_top.coe_le_coe.2 h) }, { rw h, simp at H, exact H.2.continuous_on_iterated_fderiv_within } }, { assume m hm, simp only [with_top.coe_lt_coe] at hm, cases nat.of_le_succ hm with h h, { have := H.of_succ, rw ← IH at this, exact this.2 _ (with_top.coe_lt_coe.2 h) }, { rw nat.succ_inj h, exact H.differentiable_on } } } }, end /- Next lemma is marked as a simp lemma as `C^(n+1)` functions appear mostly in inductions. -/ @[simp] lemma times_cont_diff_on_succ : times_cont_diff_on k n.succ f s ↔ differentiable_on k f s ∧ times_cont_diff_on k n (λx, fderiv_within k f s x) s := by simp [times_cont_diff_on_iff_times_cont_diff_on_rec] lemma times_cont_diff_on.of_le {m n : with_top ℕ} (h : times_cont_diff_on k n f s) (le : m ≤ n) : times_cont_diff_on k m f s := ⟨λp hp, h.1 p (le_trans hp le), λp hp, h.2 p (lt_of_lt_of_le hp le)⟩ lemma times_cont_diff_on.of_succ (h : times_cont_diff_on k n.succ f s) : times_cont_diff_on k n f s := h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)) lemma times_cont_diff_on.continuous_on {n : with_top ℕ} (h : times_cont_diff_on k n f s) : continuous_on f s := h.1 0 (by simp) lemma times_cont_diff_on.continuous_on_fderiv_within {n : with_top ℕ} (h : times_cont_diff_on k n f s) (hn : 1 ≤ n) : continuous_on (fderiv_within k f s) s := h.1 1 hn set_option class.instance_max_depth 50 /-- If a function is at least `C^1`, its bundled derivative (mapping `(x, v)` to `Df(x) v`) is continuous. -/ lemma times_cont_diff_on.continuous_on_fderiv_within_apply {n : with_top ℕ} (h : times_cont_diff_on k n f s) (hn : 1 ≤ n) : continuous_on (λp : E × E, (fderiv_within k f s p.1 : E → F) p.2) (set.prod s univ) := begin have A : continuous (λq : (E →L[k] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous_on (λp : E × E, (fderiv_within k f s p.1, p.2)) (set.prod s univ), { apply continuous_on.prod _ continuous_snd.continuous_on, refine continuous_on.comp (h.continuous_on_fderiv_within hn) continuous_fst.continuous_on (λx hx, _), simp at hx, rcases hx with ⟨y, hy⟩, exact hy }, exact A.comp_continuous_on B end lemma times_cont_diff_on_top : times_cont_diff_on k ⊤ f s ↔ (∀n:ℕ, times_cont_diff_on k n f s) := begin split, { assume h n, exact h.of_le lattice.le_top }, { assume h, split, { exact λm hm, (h m).1 m (le_refl _) }, { exact λ m hm, (h m.succ).2 m (with_top.coe_lt_coe.2 (lt_add_one m)) } } end lemma times_cont_diff_on_fderiv_within_nat {m n : ℕ} (hf : times_cont_diff_on k n f s) (h : m + 1 ≤ n) : times_cont_diff_on k m (λx, fderiv_within k f s x) s := begin have : times_cont_diff_on k m.succ f s := hf.of_le (with_top.coe_le_coe.2 h), exact (times_cont_diff_on_succ.1 this).2 end lemma times_cont_diff_on_fderiv_within {m n : with_top ℕ} (hf : times_cont_diff_on k n f s) (h : m + 1 ≤ n) : times_cont_diff_on k m (λx, fderiv_within k f s x) s := begin cases m, { change ⊤ + 1 ≤ n at h, have : n = ⊤, by simpa using h, rw this at hf, change times_cont_diff_on k ⊤ (λ (x : E), fderiv_within k f s x) s, rw times_cont_diff_on_top at ⊢ hf, exact λm, times_cont_diff_on_fderiv_within_nat (hf (m + 1)) (le_refl _) }, { have : times_cont_diff_on k (m + 1) f s := hf.of_le h, exact times_cont_diff_on_fderiv_within_nat this (le_refl _) } end lemma times_cont_diff_on.congr_mono {n : with_top ℕ} (H : times_cont_diff_on k n f s) (hs : unique_diff_on k s₁) (h : ∀x ∈ s₁, f₁ x = f x) (h₁ : s₁ ⊆ s) : times_cont_diff_on k n f₁ s₁ := begin tactic.unfreeze_local_instances, induction n using with_top.nat_induction with n IH Itop generalizing F, { rw times_cont_diff_on_zero at H ⊢, exact continuous_on.congr_mono H h h₁ }, { rw times_cont_diff_on_succ at H ⊢, refine ⟨differentiable_on.congr_mono H.1 h h₁, IH H.2 (λx hx, _)⟩, apply differentiable_within_at.fderiv_within_congr_mono (H.1 x (h₁ hx)) h (h x hx) (hs x hx) h₁ }, { rw times_cont_diff_on_top at H ⊢, assume n, exact Itop n (H n) h } end lemma times_cont_diff_on.congr {n : with_top ℕ} {s : set E} (H : times_cont_diff_on k n f s) (hs : unique_diff_on k s) (h : ∀x ∈ s, f₁ x = f x) : times_cont_diff_on k n f₁ s := times_cont_diff_on.congr_mono H hs h (subset.refl _) lemma times_cont_diff_on.congr_mono' {n m : with_top ℕ} {s : set E} (H : times_cont_diff_on k n f s) (hs : unique_diff_on k s₁) (h : ∀x ∈ s₁, f₁ x = f x) (h₁ : s₁ ⊆ s) (le : m ≤ n) : times_cont_diff_on k m f₁ s₁ := times_cont_diff_on.of_le (H.congr_mono hs h h₁) le lemma times_cont_diff_on.mono {n : with_top ℕ} {s t : set E} (h : times_cont_diff_on k n f t) (hst : s ⊆ t) (hs : unique_diff_on k s) : times_cont_diff_on k n f s := times_cont_diff_on.congr_mono h hs (λx hx, rfl) hst /-- Being `C^n` is a local property. -/ lemma times_cont_diff_on_of_locally_times_cont_diff_on {n : with_top ℕ} {s : set E} (hs : unique_diff_on k s) (h : ∀x∈s, ∃u, is_open u ∧ x ∈ u ∧ times_cont_diff_on k n f (s ∩ u)) : times_cont_diff_on k n f s := begin split, { assume m hm, apply continuous_on_of_locally_continuous_on (λx hx, _), rcases h x hx with ⟨u, u_open, xu, hu⟩, refine ⟨u, u_open, xu,_⟩, apply continuous_on.congr_mono (hu.1 m hm) (λy hy, _) (subset.refl _), symmetry, exact iterated_fderiv_within_inter_open hy.2 u_open hy.1 (unique_diff_on_inter hs u_open) }, { assume m hm, apply differentiable_on_of_locally_differentiable_on (λx hx, _), rcases h x hx with ⟨u, u_open, xu, hu⟩, refine ⟨u, u_open, xu,_⟩, apply differentiable_on.congr_mono (hu.2 m hm) (λy hy, _) (subset.refl _), symmetry, exact iterated_fderiv_within_inter_open hy.2 u_open hy.1 (unique_diff_on_inter hs u_open) } end /-- A function is `C^n`, for `n : with_top ℕ`, if its derivatives of order at most `n` are all well defined and continuous. -/ def times_cont_diff (k : Type w) [nondiscrete_normed_field k] (n : with_top ℕ) {E F : Type u} [normed_group E] [normed_space k E] [normed_group F] [normed_space k F] (f : E → F) := (∀m:ℕ, (m : with_top ℕ) ≤ n → continuous (iterated_fderiv k m f )) ∧ (∀m:ℕ, (m : with_top ℕ) < n → differentiable k (iterated_fderiv k m f)) lemma times_cont_diff_on_univ {n : with_top ℕ} : times_cont_diff_on k n f univ ↔ times_cont_diff k n f := by simp [times_cont_diff_on, times_cont_diff, iterated_fderiv_within_univ, continuous_iff_continuous_on_univ, differentiable_on_univ] @[simp] lemma times_cont_diff_zero : times_cont_diff k 0 f ↔ continuous f := by simp [times_cont_diff] theorem times_cont_diff_iff_times_cont_diff_rec : times_cont_diff k n f ↔ times_cont_diff_rec k n f := by simp [times_cont_diff_on_univ.symm, times_cont_diff_on_rec_univ.symm, times_cont_diff_on_iff_times_cont_diff_on_rec] @[simp] lemma times_cont_diff_succ : times_cont_diff k n.succ f ↔ differentiable k f ∧ times_cont_diff k n (λx, fderiv k f x) := by simp [times_cont_diff_iff_times_cont_diff_rec] lemma times_cont_diff.of_le {m n : with_top ℕ} (h : times_cont_diff k n f) (le : m ≤ n) : times_cont_diff k m f := ⟨λp hp, h.1 p (le_trans hp le), λp hp, h.2 p (lt_of_lt_of_le hp le)⟩ lemma times_cont_diff.of_succ (h : times_cont_diff k n.succ f) : times_cont_diff k n f := h.of_le (with_top.coe_le_coe.2 (nat.le_succ n)) lemma times_cont_diff.continuous {n : with_top ℕ} (h : times_cont_diff k n f) : continuous f := h.1 0 (by simp) lemma times_cont_diff.continuous_fderiv {n : with_top ℕ} (h : times_cont_diff k n f) (hn : 1 ≤ n) : continuous (fderiv k f) := h.1 1 hn lemma times_cont_diff.continuous_fderiv_apply {n : with_top ℕ} (h : times_cont_diff k n f) (hn : 1 ≤ n) : continuous (λp : E × E, (fderiv k f p.1 : E → F) p.2) := begin have A : continuous (λq : (E →L[k] F) × E, q.1 q.2) := is_bounded_bilinear_map_apply.continuous, have B : continuous (λp : E × E, (fderiv k f p.1, p.2)), { apply continuous.prod_mk _ continuous_snd, exact continuous.comp (h.continuous_fderiv hn) continuous_fst }, exact A.comp B end lemma times_cont_diff_top : times_cont_diff k ⊤ f ↔ (∀n:ℕ, times_cont_diff k n f) := by simp [times_cont_diff_on_univ.symm, times_cont_diff_on_rec_univ.symm, times_cont_diff_on_top] lemma times_cont_diff.times_cont_diff_on {n : with_top ℕ} {s : set E} (h : times_cont_diff k n f) (hs : unique_diff_on k s) : times_cont_diff_on k n f s := by { rw ← times_cont_diff_on_univ at h, apply times_cont_diff_on.mono h (subset_univ _) hs } /-- Constants are C^∞. -/ lemma times_cont_diff_const {n : with_top ℕ} {c : F} : times_cont_diff k n (λx : E, c) := begin tactic.unfreeze_local_instances, induction n using with_top.nat_induction with n IH Itop generalizing F, { rw times_cont_diff_zero, apply continuous_const }, { refine times_cont_diff_succ.2 ⟨differentiable_const _, _⟩, simp [fderiv_const], exact IH }, { rw times_cont_diff_top, assume n, apply Itop } end /-- Linear functions are C^∞. -/ lemma is_bounded_linear_map.times_cont_diff {n : with_top ℕ} (hf : is_bounded_linear_map k f) : times_cont_diff k n f := begin induction n using with_top.nat_induction with n IH Itop, { rw times_cont_diff_zero, exact hf.continuous }, { refine times_cont_diff_succ.2 ⟨hf.differentiable, _⟩, simp [hf.fderiv], exact times_cont_diff_const }, { rw times_cont_diff_top, apply Itop } end /-- The first projection in a product is C^∞. -/ lemma times_cont_diff_fst {n : with_top ℕ} : times_cont_diff k n (prod.fst : E × F → E) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.fst /-- The second projection in a product is C^∞. -/ lemma times_cont_diff_snd {n : with_top ℕ} : times_cont_diff k n (prod.snd : E × F → F) := is_bounded_linear_map.times_cont_diff is_bounded_linear_map.snd /-- The identity is C^∞. -/ lemma times_cont_diff_id {n : with_top ℕ} : times_cont_diff k n (id : E → E) := is_bounded_linear_map.id.times_cont_diff /-- Bilinear functions are C^∞. -/ lemma is_bounded_bilinear_map.times_cont_diff {n : with_top ℕ} (hb : is_bounded_bilinear_map k b) : times_cont_diff k n b := begin induction n using with_top.nat_induction with n IH Itop, { rw times_cont_diff_zero, exact hb.continuous }, { refine times_cont_diff_succ.2 ⟨hb.differentiable, _⟩, simp [hb.fderiv], exact hb.is_bounded_linear_map_deriv.times_cont_diff }, { rw times_cont_diff_top, apply Itop } end /-- Composition by bounded linear maps preserves `C^n` functions on domains. -/ lemma times_cont_diff_on.comp_is_bounded_linear {n : with_top ℕ} {s : set E} {f : E → F} {g : F → G} (hf : times_cont_diff_on k n f s) (hg : is_bounded_linear_map k g) (hs : unique_diff_on k s) : times_cont_diff_on k n (λx, g (f x)) s := begin tactic.unfreeze_local_instances, induction n using with_top.nat_induction with n IH Itop generalizing F G, { have : continuous_on g univ := hg.continuous.continuous_on, rw times_cont_diff_on_zero at hf ⊢, apply continuous_on.comp this hf (subset_univ _) }, { rw times_cont_diff_on_succ at hf ⊢, refine ⟨differentiable_on.comp hg.differentiable_on hf.1 (subset_univ _), _⟩, let Φ : (E →L[k] F) → (E →L[k] G) := λu, continuous_linear_map.comp (hg.to_continuous_linear_map) u, have : ∀x∈s, fderiv_within k (g ∘ f) s x = Φ (fderiv_within k f s x), { assume x hx, rw [fderiv_within.comp x _ (hf.1 x hx) (subset_univ _) (hs x hx), fderiv_within_univ, hg.fderiv], rw differentiable_within_at_univ, exact hg.differentiable_at }, apply times_cont_diff_on.congr_mono _ hs this (subset.refl _), simp only [times_cont_diff_on_succ] at hf, exact IH hf.2 hg.to_continuous_linear_map.is_bounded_linear_map_comp_left }, { rw times_cont_diff_on_top at hf ⊢, assume n, apply Itop n (hf n) hg } end /-- Composition by bounded linear maps preserves `C^n` functions. -/ lemma times_cont_diff.comp_is_bounded_linear {n : with_top ℕ} {f : E → F} {g : F → G} (hf : times_cont_diff k n f) (hg : is_bounded_linear_map k g) : times_cont_diff k n (λx, g (f x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp_is_bounded_linear (times_cont_diff_on_univ.2 hf) hg is_open_univ.unique_diff_on /-- The cartesian product of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.prod {n : with_top ℕ} {s : set E} {f : E → F} {g : E → G} (hf : times_cont_diff_on k n f s) (hg : times_cont_diff_on k n g s) (hs : unique_diff_on k s) : times_cont_diff_on k n (λx:E, (f x, g x)) s := begin tactic.unfreeze_local_instances, induction n using with_top.nat_induction with n IH Itop generalizing F G, { rw times_cont_diff_on_zero at hf hg ⊢, exact continuous_on.prod hf hg }, { rw times_cont_diff_on_succ at hf hg ⊢, refine ⟨differentiable_on.prod hf.1 hg.1, _⟩, let F₁ := λx : E, (fderiv_within k f s x, fderiv_within k g s x), let Φ : ((E →L[k] F) × (E →L[k] G)) → (E →L[k] (F × G)) := λp, continuous_linear_map.prod p.1 p.2, have : times_cont_diff_on k n (Φ ∘ F₁) s := times_cont_diff_on.comp_is_bounded_linear (IH hf.2 hg.2) is_bounded_linear_map_prod_iso hs, apply times_cont_diff_on.congr_mono this hs (λx hx, _) (subset.refl _), apply differentiable_at.fderiv_within_prod (hf.1 x hx) (hg.1 x hx) (hs x hx) }, { rw times_cont_diff_on_top at hf hg ⊢, assume n, apply Itop n (hf n) (hg n) } end /-- The cartesian product of `C^n` functions is `C^n`. -/ lemma times_cont_diff.prod {n : with_top ℕ} {f : E → F} {g : E → G} (hf : times_cont_diff k n f) (hg : times_cont_diff k n g) : times_cont_diff k n (λx:E, (f x, g x)) := times_cont_diff_on_univ.1 $ times_cont_diff_on.prod (times_cont_diff_on_univ.2 hf) (times_cont_diff_on_univ.2 hg) is_open_univ.unique_diff_on /-- The composition of `C^n` functions on domains is `C^n`. -/ lemma times_cont_diff_on.comp {n : with_top ℕ} {s : set E} {t : set F} {g : F → G} {f : E → F} (hg : times_cont_diff_on k n g t) (hf : times_cont_diff_on k n f s) (hs : unique_diff_on k s) (st : f '' s ⊆ t) : times_cont_diff_on k n (g ∘ f) s := begin tactic.unfreeze_local_instances, induction n using with_top.nat_induction with n IH Itop generalizing E F G, { rw times_cont_diff_on_zero at hf hg ⊢, exact continuous_on.comp hg hf st }, { rw times_cont_diff_on_succ at hf hg ⊢, /- We have to show that the derivative of g ∘ f is C^n, given that g and f are C^(n+1). By the chain rule, this derivative is Dg(f x) ⬝ Df(x). This is the composition of x ↦ (Dg (f x), Df (x)) with the product of bounded linear maps, which is bilinear and therefore C^∞. By the induction assumption, it suffices to show that x ↦ (Dg (f x), Df (x)) is C^n. It is even enough to show that each component is C^n. This follows from the assumptions on f and g, and the inductive assumption. -/ refine ⟨differentiable_on.comp hg.1 hf.1 st, _⟩, have : ∀x∈s, fderiv_within k (g ∘ f) s x = continuous_linear_map.comp (fderiv_within k g t (f x)) (fderiv_within k f s x), { assume x hx, apply fderiv_within.comp x _ (hf.1 x hx) st (hs x hx), exact hg.1 _ (st (mem_image_of_mem _ hx)) }, apply times_cont_diff_on.congr _ hs this, have A : times_cont_diff_on k n (λx, fderiv_within k g t (f x)) s := IH hg.2 (times_cont_diff_on_succ.2 hf).of_succ hs st, have B : times_cont_diff_on k n (λx, fderiv_within k f s x) s := hf.2, have C : times_cont_diff_on k n (λx:E, (fderiv_within k f s x, fderiv_within k g t (f x))) s := times_cont_diff_on.prod B A hs, have D : times_cont_diff_on k n (λ(p : (E →L[k] F) × (F →L[k] G)), p.2.comp p.1) univ := is_bounded_bilinear_map_comp.times_cont_diff.times_cont_diff_on is_open_univ.unique_diff_on, exact IH D C hs (subset_univ _) }, { rw times_cont_diff_on_top at hf hg ⊢, assume n, apply Itop n (hg n) (hf n) hs st } end /-- The composition of `C^n` functions is `C^n`. -/ lemma times_cont_diff.comp {n : with_top ℕ} {g : F → G} {f : E → F} (hg : times_cont_diff k n g) (hf : times_cont_diff k n f) : times_cont_diff k n (g ∘ f) := times_cont_diff_on_univ.1 $ times_cont_diff_on.comp (times_cont_diff_on_univ.2 hg) (times_cont_diff_on_univ.2 hf) is_open_univ.unique_diff_on (subset_univ _) /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff_on_fderiv_within_apply {m n : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff_on k n f s) (hs : unique_diff_on k s) (hmn : m + 1 ≤ n) : times_cont_diff_on k m (λp : E × E, (fderiv_within k f s p.1 : E →L[k] F) p.2) (set.prod s (univ : set E)) := begin have U : unique_diff_on k (set.prod s (univ : set E)) := hs.prod unique_diff_on_univ, have A : times_cont_diff_on k m (λp : (E →L[k] F) × E, p.1 p.2) univ, { rw times_cont_diff_on_univ, apply is_bounded_bilinear_map.times_cont_diff, exact is_bounded_bilinear_map_apply }, have B : times_cont_diff_on k m (λ (p : E × E), ((fderiv_within k f s p.fst), p.snd)) (set.prod s univ), { apply times_cont_diff_on.prod _ _ U, { have I : times_cont_diff_on k m (λ (x : E), fderiv_within k f s x) s := times_cont_diff_on_fderiv_within hf hmn, have J : times_cont_diff_on k m (λ (x : E × E), x.1) (set.prod s univ), { apply times_cont_diff.times_cont_diff_on _ U, apply is_bounded_linear_map.times_cont_diff, apply is_bounded_linear_map.fst }, exact times_cont_diff_on.comp I J U (fst_image_prod_subset _ _) }, { apply times_cont_diff.times_cont_diff_on _ U, apply is_bounded_linear_map.times_cont_diff, apply is_bounded_linear_map.snd } }, apply times_cont_diff_on.comp A B U (subset_univ _), end /-- The bundled derivative of a `C^{n+1}` function is `C^n`. -/ lemma times_cont_diff.times_cont_diff_fderiv_apply {n m : with_top ℕ} {s : set E} {f : E → F} (hf : times_cont_diff k n f) (hmn : m + 1 ≤ n) : times_cont_diff k m (λp : E × E, (fderiv k f p.1 : E →L[k] F) p.2) := begin rw ← times_cont_diff_on_univ at ⊢ hf, rw [← fderiv_within_univ, ← univ_prod_univ], exact times_cont_diff_on_fderiv_within_apply hf unique_diff_on_univ hmn end
5c3b0df5ce592c066fdf1d12b3bd45bfe591d4e9
4da0c8e61fcd6ec3f3be47ee14a038850c03d0c3
/src/default.lean
107244bf4fc802d1e5eccbdecb72dc1d3965c318
[ "Apache-2.0" ]
permissive
bbentzen/mpl
fcbea60204bc8fd64667e0f76a5cebf4b67fb6ca
bb5066ec51fa11a4b66f440c4f6c9a3d8fb2e0de
refs/heads/master
1,625,175,849,308
1,624,207,634,000
1,624,207,634,000
142,774,375
9
0
null
null
null
null
UTF-8
Lean
false
false
179
lean
/- Copyright (c) 2018 Bruno Bentzen. All rights reserved. Released under the Apache License 2.0 (see "License"); Author: Bruno Bentzen -/ import data.set .language .context
e19307b9b71ae95846dfafa1bf7f7101949bea70
f5f7e6fae601a5fe3cac7cc3ed353ed781d62419
/src/measure_theory/outer_measure.lean
100d379cecdd6f5a084c828a1085f60b712d7eda
[ "Apache-2.0" ]
permissive
EdAyers/mathlib
9ecfb2f14bd6caad748b64c9c131befbff0fb4e0
ca5d4c1f16f9c451cf7170b10105d0051db79e1b
refs/heads/master
1,626,189,395,845
1,555,284,396,000
1,555,284,396,000
144,004,030
0
0
Apache-2.0
1,533,727,664,000
1,533,727,663,000
null
UTF-8
Lean
false
false
19,624
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 Outer measures -- overapproximations of measures -/ import order.galois_connection algebra.big_operators algebra.module topology.instances.ennreal analysis.specific_limits measure_theory.measurable_space noncomputable theory open set lattice finset function filter encodable local attribute [instance] classical.prop_decidable namespace measure_theory structure outer_measure (α : Type*) := (measure_of : set α → ennreal) (empty : measure_of ∅ = 0) (mono : ∀{s₁ s₂}, s₁ ⊆ s₂ → measure_of s₁ ≤ measure_of s₂) (Union_nat : ∀(s:ℕ → set α), measure_of (⋃i, s i) ≤ (∑i, measure_of (s i))) namespace outer_measure instance {α} : has_coe_to_fun (outer_measure α) := ⟨_, λ m, m.measure_of⟩ section basic variables {α : Type*} {ms : set (outer_measure α)} {m : outer_measure α} @[simp] theorem empty' (m : outer_measure α) : m ∅ = 0 := m.empty theorem mono' (m : outer_measure α) {s₁ s₂} (h : s₁ ⊆ s₂) : m s₁ ≤ m s₂ := m.mono h theorem Union_aux (m : set α → ennreal) (m0 : m ∅ = 0) {β} [encodable β] (s : β → set α) : (∑ b, m (s b)) = ∑ i, m (⋃ b ∈ decode2 β i, s b) := begin have H : ∀ n, m (⋃ b ∈ decode2 β n, s b) ≠ 0 → (decode2 β n).is_some, { intros n h, cases decode2 β n with b, { exact (h (by simp [m0])).elim }, { exact rfl } }, refine tsum_eq_tsum_of_ne_zero_bij (λ n h, option.get (H n h)) _ _ _, { intros m n hm hn e, have := mem_decode2.1 (option.get_mem (H n hn)), rwa [← e, mem_decode2.1 (option.get_mem (H m hm))] at this }, { intros b h, refine ⟨encode b, _, _⟩, { convert h, simp [ext_iff, encodek2] }, { exact option.get_of_mem _ (encodek2 _) } }, { intros n h, transitivity, swap, rw [show decode2 β n = _, from option.get_mem (H n h)], congr, simp [ext_iff] } end protected theorem Union (m : outer_measure α) {β} [encodable β] (s : β → set α) : m (⋃i, s i) ≤ (∑i, m (s i)) := by rw [Union_decode2, Union_aux _ m.empty' s]; exact m.Union_nat _ lemma Union_null (m : outer_measure α) {β} [encodable β] {s : β → set α} (h : ∀ i, m (s i) = 0) : m (⋃i, s i) = 0 := by simpa [h] using m.Union s protected lemma union (m : outer_measure α) (s₁ s₂ : set α) : m (s₁ ∪ s₂) ≤ m s₁ + m s₂ := begin convert m.Union (λ b, cond b s₁ s₂), { simp [union_eq_Union] }, { rw tsum_fintype, change _ = _ + _, simp } end lemma union_null (m : outer_measure α) {s₁ s₂ : set α} (h₁ : m s₁ = 0) (h₂ : m s₂ = 0) : m (s₁ ∪ s₂) = 0 := by simpa [h₁, h₂] using m.union s₁ s₂ @[extensionality] lemma ext : ∀{μ₁ μ₂ : outer_measure α}, (∀s, μ₁ s = μ₂ s) → μ₁ = μ₂ | ⟨m₁, e₁, _, u₁⟩ ⟨m₂, e₂, _, u₂⟩ h := by congr; exact funext h instance : has_zero (outer_measure α) := ⟨{ measure_of := λ_, 0, empty := rfl, mono := assume _ _ _, le_refl 0, Union_nat := assume s, zero_le _ }⟩ @[simp] theorem zero_apply (s : set α) : (0 : outer_measure α) s = 0 := rfl instance : inhabited (outer_measure α) := ⟨0⟩ instance : has_add (outer_measure α) := ⟨λm₁ m₂, { measure_of := λs, m₁ s + m₂ s, empty := show m₁ ∅ + m₂ ∅ = 0, by simp [outer_measure.empty], mono := assume s₁ s₂ h, add_le_add' (m₁.mono h) (m₂.mono h), Union_nat := assume s, calc m₁ (⋃i, s i) + m₂ (⋃i, s i) ≤ (∑i, m₁ (s i)) + (∑i, m₂ (s i)) : add_le_add' (m₁.Union_nat s) (m₂.Union_nat s) ... = _ : ennreal.tsum_add.symm}⟩ @[simp] theorem add_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ + m₂) s = m₁ s + m₂ s := rfl instance : add_comm_monoid (outer_measure α) := { zero := 0, add := (+), add_comm := assume a b, ext $ assume s, add_comm _ _, add_assoc := assume a b c, ext $ assume s, add_assoc _ _ _, add_zero := assume a, ext $ assume s, add_zero _, zero_add := assume a, ext $ assume s, zero_add _ } instance : has_bot (outer_measure α) := ⟨0⟩ instance outer_measure.order_bot : order_bot (outer_measure α) := { le := λm₁ m₂, ∀s, m₁ s ≤ m₂ s, bot := 0, le_refl := assume a s, le_refl _, le_trans := assume a b c hab hbc s, le_trans (hab s) (hbc s), le_antisymm := assume a b hab hba, ext $ assume s, le_antisymm (hab s) (hba s), bot_le := assume a s, zero_le _ } section supremum instance : has_Sup (outer_measure α) := ⟨λms, { measure_of := λs, ⨆m:ms, m.val s, empty := le_zero_iff_eq.1 $ supr_le $ λ ⟨m, h⟩, le_of_eq m.empty, mono := assume s₁ s₂ hs, supr_le_supr $ assume ⟨m, hm⟩, m.mono hs, Union_nat := assume f, supr_le $ assume m, calc m.val (⋃i, f i) ≤ (∑ (i : ℕ), m.val (f i)) : m.val.Union_nat _ ... ≤ (∑i, ⨆m:ms, m.val (f i)) : ennreal.tsum_le_tsum $ assume i, le_supr (λm:ms, m.val (f i)) m }⟩ private lemma le_Sup (hm : m ∈ ms) : m ≤ Sup ms := λ s, le_supr (λm:ms, m.val s) ⟨m, hm⟩ private lemma Sup_le (hm : ∀m' ∈ ms, m' ≤ m) : Sup ms ≤ m := λ s, (supr_le $ assume ⟨m', h'⟩, (hm m' h') s) instance : has_Inf (outer_measure α) := ⟨λs, Sup {m | ∀m'∈s, m ≤ m'}⟩ private lemma Inf_le (hm : m ∈ ms) : Inf ms ≤ m := Sup_le $ assume m' h', h' _ hm private lemma le_Inf (hm : ∀m' ∈ ms, m ≤ m') : m ≤ Inf ms := le_Sup hm instance : complete_lattice (outer_measure α) := { top := Sup univ, le_top := assume a, le_Sup (mem_univ a), Sup := Sup, Sup_le := assume s m, Sup_le, le_Sup := assume s m, le_Sup, Inf := Inf, Inf_le := assume s m, Inf_le, le_Inf := assume s m, le_Inf, sup := λa b, Sup {a, b}, le_sup_left := assume a b, le_Sup $ by simp, le_sup_right := assume a b, le_Sup $ by simp, sup_le := assume a b c ha hb, Sup_le $ by simp [or_imp_distrib, ha, hb] {contextual:=tt}, inf := λa b, Inf {a, b}, inf_le_left := assume a b, Inf_le $ by simp, inf_le_right := assume a b, Inf_le $ by simp, le_inf := assume a b c ha hb, le_Inf $ by simp [or_imp_distrib, ha, hb] {contextual:=tt}, .. outer_measure.order_bot } @[simp] theorem Sup_apply (ms : set (outer_measure α)) (s : set α) : (Sup ms) s = ⨆ m : ms, m s := rfl @[simp] theorem supr_apply {ι} (f : ι → outer_measure α) (s : set α) : (⨆ i : ι, f i) s = ⨆ i, f i s := le_antisymm (supr_le $ λ ⟨_, i, rfl⟩, le_supr _ i) (supr_le $ λ i, le_supr (λ (m : {a : outer_measure α // ∃ i, f i = a}), m.1 s) ⟨f i, i, rfl⟩) @[simp] theorem sup_apply (m₁ m₂ : outer_measure α) (s : set α) : (m₁ ⊔ m₂) s = m₁ s ⊔ m₂ s := by have := supr_apply (λ b, cond b m₁ m₂) s; rwa [supr_bool_eq, supr_bool_eq] at this end supremum def map {β} (f : α → β) (m : outer_measure α) : outer_measure β := { measure_of := λs, m (f ⁻¹' s), empty := m.empty, mono := λ s t h, m.mono (preimage_mono h), Union_nat := λ s, by rw [preimage_Union]; exact m.Union_nat (λ i, f ⁻¹' s i) } @[simp] theorem map_apply {β} (f : α → β) (m : outer_measure α) (s : set β) : map f m s = m (f ⁻¹' s) := rfl @[simp] theorem map_id (m : outer_measure α) : map id m = m := ext $ λ s, rfl @[simp] theorem map_map {β γ} (f : α → β) (g : β → γ) (m : outer_measure α) : map g (map f m) = map (g ∘ f) m := ext $ λ s, rfl instance : functor outer_measure := {map := λ α β, map} instance : is_lawful_functor outer_measure := { id_map := λ α, map_id, comp_map := λ α β γ f g m, (map_map f g m).symm } /-- The dirac outer measure. -/ def dirac (a : α) : outer_measure α := { measure_of := λs, ⨆ h : a ∈ s, 1, empty := by simp, mono := λ s t h, supr_le_supr2 (λ h', ⟨h h', le_refl _⟩), Union_nat := λ s, supr_le $ λ h, let ⟨i, h⟩ := mem_Union.1 h in le_trans (by exact le_supr _ h) (ennreal.le_tsum i) } @[simp] theorem dirac_apply (a : α) (s : set α) : dirac a s = ⨆ h : a ∈ s, 1 := rfl def sum {ι} (f : ι → outer_measure α) : outer_measure α := { measure_of := λs, ∑ i, f i s, empty := by simp, mono := λ s t h, ennreal.tsum_le_tsum (λ i, (f i).mono' h), Union_nat := λ s, by rw ennreal.tsum_comm; exact ennreal.tsum_le_tsum (λ i, (f i).Union_nat _) } @[simp] theorem sum_apply {ι} (f : ι → outer_measure α) (s : set α) : sum f s = ∑ i, f i s := rfl instance : has_scalar ennreal (outer_measure α) := ⟨λ a m, { measure_of := λs, a * m s, empty := by simp, mono := λ s t h, canonically_ordered_semiring.mul_le_mul (le_refl _) (m.mono' h), Union_nat := λ s, by rw ennreal.mul_tsum; exact canonically_ordered_semiring.mul_le_mul (le_refl _) (m.Union_nat _) }⟩ @[simp] theorem smul_apply (a : ennreal) (m : outer_measure α) (s : set α) : (a • m) s = a * m s := rfl instance : semimodule ennreal (outer_measure α) := { smul_add := λ a m₁ m₂, ext $ λ s, mul_add _ _ _, add_smul := λ a b m, ext $ λ s, add_mul _ _ _, mul_smul := λ a b m, ext $ λ s, mul_assoc _ _ _, one_smul := λ m, ext $ λ s, one_mul _, zero_smul := λ m, ext $ λ s, zero_mul _, smul_zero := λ a, ext $ λ s, mul_zero _, ..outer_measure.has_scalar } theorem smul_dirac_apply (a : ennreal) (b : α) (s : set α) : (a • dirac b) s = ⨆ h : b ∈ s, a := by by_cases b ∈ s; simp [h] theorem top_apply {s : set α} (h : s ≠ ∅) : (⊤ : outer_measure α) s = ⊤ := let ⟨a, as⟩ := set.exists_mem_of_ne_empty h in top_unique $ le_supr_of_le ⟨(⊤ : ennreal) • dirac a, trivial⟩ $ by simp [smul_dirac_apply, as] end basic section of_function set_option eqn_compiler.zeta true /-- Given any function `m` assigning measures to sets satisying `m ∅ = 0`, there is a unique maximal outer measure `μ` satisfying `μ s ≤ m s` for all `s : set α`. -/ protected def of_function {α : Type*} (m : set α → ennreal) (m_empty : m ∅ = 0) : outer_measure α := let μ := λs, ⨅{f : ℕ → set α} (h : s ⊆ ⋃i, f i), ∑i, m (f i) in { measure_of := μ, empty := le_antisymm (infi_le_of_le (λ_, ∅) $ infi_le_of_le (empty_subset _) $ by simp [m_empty]) (zero_le _), mono := assume s₁ s₂ hs, infi_le_infi $ assume f, infi_le_infi2 $ assume hb, ⟨subset.trans hs hb, le_refl _⟩, Union_nat := assume s, ennreal.le_of_forall_epsilon_le $ begin assume ε hε (hb : (∑i, μ (s i)) < ⊤), rcases ennreal.exists_pos_sum_of_encodable (ennreal.coe_lt_coe.2 hε) ℕ with ⟨ε', hε', hl⟩, refine le_trans _ (add_le_add_left' (le_of_lt hl)), rw ← ennreal.tsum_add, choose f hf using show ∀i, ∃f:ℕ → set α, s i ⊆ (⋃i, f i) ∧ (∑i, m (f i)) < μ (s i) + ε' i, { intro, have : μ (s i) < μ (s i) + ε' i := ennreal.lt_add_right (lt_of_le_of_lt (by apply ennreal.le_tsum) hb) (by simpa using hε' i), simpa [μ, infi_lt_iff] }, refine le_trans _ (ennreal.tsum_le_tsum $ λ i, le_of_lt (hf i).2), rw [← ennreal.tsum_prod, ← tsum_equiv equiv.nat_prod_nat_equiv_nat.symm], swap, {apply_instance}, refine infi_le_of_le _ (infi_le _ _), exact Union_subset (λ i, subset.trans (hf i).1 $ Union_subset $ λ j, subset.trans (by simp) $ subset_Union _ $ equiv.nat_prod_nat_equiv_nat (i, j)), end } theorem of_function_le {α : Type*} (m : set α → ennreal) (m_empty s) : outer_measure.of_function m m_empty s ≤ m s := let f : ℕ → set α := λi, nat.rec_on i s (λn s, ∅) in infi_le_of_le f $ infi_le_of_le (subset_Union f 0) $ le_of_eq $ calc (∑i, m (f i)) = ({0} : finset ℕ).sum (λi, m (f i)) : tsum_eq_sum $ by intro i; cases i; simp [m_empty] ... = m s : by simp; refl theorem le_of_function {α : Type*} {m m_empty} {μ : outer_measure α} : μ ≤ outer_measure.of_function m m_empty ↔ ∀ s, μ s ≤ m s := ⟨λ H s, le_trans (H _) (of_function_le _ _ _), λ H s, le_infi $ λ f, le_infi $ λ hs, le_trans (μ.mono hs) $ le_trans (μ.Union f) $ ennreal.tsum_le_tsum $ λ i, H _⟩ end of_function section caratheodory_measurable universe u parameters {α : Type u} (m : outer_measure α) include m local attribute [simp] set.inter_comm set.inter_left_comm set.inter_assoc variables {s s₁ s₂ : set α} private def C (s : set α) := ∀t, m t = m (t ∩ s) + m (t \ s) private lemma C_iff_le {s : set α} : C s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := forall_congr $ λ t, le_antisymm_iff.trans $ and_iff_right $ by convert m.union _ _; rw inter_union_diff t s @[simp] private lemma C_empty : C ∅ := by simp [C, m.empty, diff_empty] private lemma C_compl : C s₁ → C (- s₁) := by simp [C, diff_eq] @[simp] private lemma C_compl_iff : C (- s) ↔ C s := ⟨λ h, by simpa using C_compl m h, C_compl⟩ private lemma C_union (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∪ s₂) := λ t, begin rw [h₁ t, h₂ (t ∩ s₁), h₂ (t \ s₁), h₁ (t ∩ (s₁ ∪ s₂)), inter_diff_assoc _ _ s₁, set.inter_assoc _ _ s₁, inter_eq_self_of_subset_right (set.subset_union_left _ _), union_diff_left, h₂ (t ∩ s₁)], simp [diff_eq] end private lemma measure_inter_union (h : s₁ ∩ s₂ ⊆ ∅) (h₁ : C s₁) {t : set α} : m (t ∩ (s₁ ∪ s₂)) = m (t ∩ s₁) + m (t ∩ s₂) := by rw [h₁, set.inter_assoc, union_inter_cancel_left, inter_diff_assoc, union_diff_cancel_left h] private lemma C_Union_lt {s : ℕ → set α} : ∀{n:ℕ}, (∀i<n, C (s i)) → C (⋃i<n, s i) | 0 h := by simp [nat.not_lt_zero] | (n + 1) h := by rw Union_lt_succ; exact C_union m (h n (le_refl (n + 1))) (C_Union_lt $ assume i hi, h i $ lt_of_lt_of_le hi $ nat.le_succ _) private lemma C_inter (h₁ : C s₁) (h₂ : C s₂) : C (s₁ ∩ s₂) := by rw [← C_compl_iff, compl_inter]; from C_union _ (C_compl _ h₁) (C_compl _ h₂) private lemma C_sum {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) {t : set α} : ∀ {n}, (finset.range n).sum (λi, m (t ∩ s i)) = m (t ∩ ⋃i<n, s i) | 0 := by simp [nat.not_lt_zero, m.empty] | (nat.succ n) := begin simp [Union_lt_succ, range_succ], rw [measure_inter_union m _ (h n), C_sum], intro a, simpa [range_succ] using λ h₁ i hi h₂, hd _ _ (ne_of_gt hi) ⟨h₁, h₂⟩ end private lemma C_Union_nat {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) : C (⋃i, s i) := C_iff_le.2 $ λ t, begin have hp : m (t ∩ ⋃i, s i) ≤ (⨆n, m (t ∩ ⋃i<n, s i)), { convert m.Union (λ i, t ∩ s i), { rw inter_Union_left }, { simp [ennreal.tsum_eq_supr_nat, C_sum m h hd] } }, refine le_trans (add_le_add_right' hp) _, rw ennreal.supr_add, refine supr_le (λ n, le_trans (add_le_add_left' _) (ge_of_eq (C_Union_lt m (λ i _, h i) _))), refine m.mono (diff_subset_diff_right _), exact bUnion_subset (λ i _, subset_Union _ i), end private lemma f_Union {s : ℕ → set α} (h : ∀i, C (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑i, m (s i) := begin refine le_antisymm (m.Union_nat s) _, rw ennreal.tsum_eq_supr_nat, refine supr_le (λ n, _), have := @C_sum _ m _ h hd univ n, simp at this, simp [this], exact m.mono (bUnion_subset (λ i _, subset_Union _ i)), end private def caratheodory_dynkin : measurable_space.dynkin_system α := { has := C, has_empty := C_empty, has_compl := assume s, C_compl, has_Union_nat := assume f hf hn, C_Union_nat hn hf } /-- Given an outer measure `μ`, the Caratheodory measurable space is defined such that `s` is measurable if `∀t, μ t = μ (t ∩ s) + μ (t \ s)`. -/ protected def caratheodory : measurable_space α := caratheodory_dynkin.to_measurable_space $ assume s₁ s₂, C_inter lemma is_caratheodory {s : set α} : caratheodory.is_measurable s ↔ ∀t, m t = m (t ∩ s) + m (t \ s) := iff.rfl lemma is_caratheodory_le {s : set α} : caratheodory.is_measurable s ↔ ∀t, m (t ∩ s) + m (t \ s) ≤ m t := C_iff_le protected lemma Union_eq_of_caratheodory {s : ℕ → set α} (h : ∀i, caratheodory.is_measurable (s i)) (hd : pairwise (disjoint on s)) : m (⋃i, s i) = ∑i, m (s i) := f_Union h hd end caratheodory_measurable variables {α : Type*} lemma caratheodory_is_measurable {m : set α → ennreal} {s : set α} {h₀ : m ∅ = 0} (hs : ∀t, m (t ∩ s) + m (t \ s) ≤ m t) : (outer_measure.of_function m h₀).caratheodory.is_measurable s := let o := (outer_measure.of_function m h₀) in (is_caratheodory_le o).2 $ λ t, le_infi $ λ f, le_infi $ λ hf, begin refine le_trans (add_le_add' (infi_le_of_le (λi, f i ∩ s) $ infi_le _ _) (infi_le_of_le (λi, f i \ s) $ infi_le _ _)) _, { rw ← inter_Union_right, exact inter_subset_inter_left _ hf }, { rw ← diff_Union_right, exact diff_subset_diff_left hf }, { rw ← ennreal.tsum_add, exact ennreal.tsum_le_tsum (λ i, hs _) } end @[simp] theorem zero_caratheodory : (0 : outer_measure α).caratheodory = ⊤ := top_unique $ λ s _ t, (add_zero _).symm theorem top_caratheodory : (⊤ : outer_measure α).caratheodory = ⊤ := top_unique $ assume s hs, (is_caratheodory_le _).2 $ assume t, by by_cases ht : t = ∅; simp [ht, top_apply] theorem le_add_caratheodory (m₁ m₂ : outer_measure α) : m₁.caratheodory ⊓ m₂.caratheodory ≤ (m₁ + m₂ : outer_measure α).caratheodory := λ s ⟨hs₁, hs₂⟩ t, by simp [hs₁ t, hs₂ t] theorem le_sum_caratheodory {ι} (m : ι → outer_measure α) : (⨅ i, (m i).caratheodory) ≤ (sum m).caratheodory := λ s h t, by simp [λ i, measurable_space.is_measurable_infi.1 h i t, ennreal.tsum_add] theorem le_smul_caratheodory (a : ennreal) (m : outer_measure α) : m.caratheodory ≤ (a • m).caratheodory := λ s h t, by simp [h t, mul_add] @[simp] theorem dirac_caratheodory (a : α) : (dirac a).caratheodory = ⊤ := top_unique $ λ s _ t, begin by_cases a ∈ t; simp [h], by_cases a ∈ s; simp [h] end section Inf_gen def Inf_gen (m : set (outer_measure α)) (s : set α) : ennreal := ⨆(h : s ≠ ∅), ⨅ (μ : outer_measure α) (h : μ ∈ m), μ s @[simp] lemma Inf_gen_empty (m : set (outer_measure α)) : Inf_gen m ∅ = 0 := by simp [Inf_gen] lemma Inf_gen_nonempty1 (m : set (outer_measure α)) (t : set α) (h : t ≠ ∅) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := by rw [Inf_gen, supr_pos h] lemma Inf_gen_nonempty2 (m : set (outer_measure α)) (μ) (h : μ ∈ m) (t) : Inf_gen m t = (⨅ (μ : outer_measure α) (h : μ ∈ m), μ t) := begin by_cases ht : t = ∅, { simp [ht], refine (bot_unique $ infi_le_of_le μ $ _).symm, refine infi_le_of_le h (le_refl ⊥) }, { exact Inf_gen_nonempty1 m t ht } end lemma Inf_eq_of_function_Inf_gen (m : set (outer_measure α)) : Inf m = outer_measure.of_function (Inf_gen m) (Inf_gen_empty m) := begin refine le_antisymm (assume t', le_of_function.2 (assume t, _) _) (lattice.le_Inf $ assume μ hμ t, le_trans (outer_measure.of_function_le _ _ _) _); by_cases ht : t = ∅; simp [ht, Inf_gen_nonempty1], { assume μ hμ, exact (show Inf m ≤ μ, from lattice.Inf_le hμ) t }, { exact infi_le_of_le μ (infi_le _ hμ) } end end Inf_gen end outer_measure end measure_theory
04289309362bc1c0075fe1d9dc184e60e82f3a43
efce24474b28579aba3272fdb77177dc2b11d7aa
/src/homotopy_theory/topological_spaces/colimits.lean
90b98606179038745af80b52f0d0b3284ffd6691
[ "Apache-2.0" ]
permissive
rwbarton/lean-homotopy-theory
cff499f24268d60e1c546e7c86c33f58c62888ed
39e1b4ea1ed1b0eca2f68bc64162dde6a6396dee
refs/heads/lean-3.4.2
1,622,711,883,224
1,598,550,958,000
1,598,550,958,000
136,023,667
12
6
Apache-2.0
1,573,187,573,000
1,528,116,262,000
Lean
UTF-8
Lean
false
false
4,718
lean
import category_theory.colimits import category_theory.colimit_lemmas import .category /- Construction of the basic finite colimits in Top. -/ open category_theory local notation f ` ∘ `:80 g:80 := g ≫ f universe u namespace homotopy_theory.topological_spaces namespace Top local notation `Top` := Top.{u} section initial_object protected def empty : Top := Top.mk_ob pempty protected def empty_induced (Z : Top) : Top.empty ⟶ Z := Top.mk_hom (λ x, pempty.cases_on (λ _, Z) x) (by continuity) protected def empty_is_initial_object : Is_initial_object.{u u+1} Top.empty := Is_initial_object.mk' Top.empty_induced (assume Z k k', by ext x; cases x) instance : has_initial_object.{u} Top := ⟨⟨Top.empty, Top.empty_is_initial_object⟩⟩ protected def is_initial_object_of_to_empty (A : Top) (h : A → pempty) : Is_initial_object.{u} A := have continuous h, begin intros u hu, have : u = ∅, from set.eq_empty_iff_forall_not_mem.mpr (pempty.rec _), rw [this, set.preimage_empty], exact is_open_empty end, have h' : A ⟶ Top.empty := Top.mk_hom h this, Is_initial_object.mk' (λ Z, Top.empty_induced Z ∘ h') (assume Z k k', begin ext x, cases h x end) end initial_object section coproduct section construction parameters (X₀ X₁ : Top) protected def coproduct_object : Top := Top.mk_ob (X₀ ⊕ X₁) protected def coproduct_map₀ : X₀ ⟶ coproduct_object := Top.mk_hom sum.inl protected def coproduct_map₁ : X₁ ⟶ coproduct_object := Top.mk_hom sum.inr variables ⦃Z : Top⦄ (h₀ : X₀ ⟶ Z) (h₁ : X₁ ⟶ Z) protected def coproduct_induced : coproduct_object ⟶ Z := Top.mk_hom (λ x, sum.cases_on x h₀ h₁) (by continuity!) protected def coproduct_induced_commutes₀ : coproduct_induced h₀ h₁ ∘ coproduct_map₀ = h₀ := by ext; refl protected def coproduct_induced_commutes₁ : coproduct_induced h₀ h₁ ∘ coproduct_map₁ = h₁ := by ext; refl protected def coproduct_uniqueness {k k' : coproduct_object ⟶ Z} (e₀ : k ∘ coproduct_map₀ = k' ∘ coproduct_map₀) (e₁ : k ∘ coproduct_map₁ = k' ∘ coproduct_map₁) : k = k' := begin ext x, cases x, { exact @@Top.hom_congr e₀ x }, { exact @@Top.hom_congr e₁ x } end protected def coproduct_is_coproduct : Is_coproduct coproduct_map₀ coproduct_map₁ := Is_coproduct.mk' coproduct_induced coproduct_induced_commutes₀ coproduct_induced_commutes₁ coproduct_uniqueness end construction instance Top.has_coproducts : has_coproducts.{u} Top := ⟨λ X₀ X₁, { ob := Top.coproduct_object X₀ X₁, map₀ := Top.coproduct_map₀ X₀ X₁, map₁ := Top.coproduct_map₁ X₀ X₁, is_coproduct := Top.coproduct_is_coproduct X₀ X₁ }⟩ end coproduct section coequalizer section construction parameters {X Y : Top} (f₀ f₁ : X ⟶ Y) protected def coeq_rel : Y → Y → Prop := λ y₀ y₁, ∃ x, y₀ = f₀ x ∧ y₁ = f₁ x protected def coequalizer_object : Top := Top.mk_ob (quot coeq_rel) protected def coequalizer_map : Y ⟶ Top.coequalizer_object f₀ f₁ := Top.mk_hom (quot.mk coeq_rel) protected lemma coequalizer_commutes : coequalizer_map ∘ f₀ = coequalizer_map ∘ f₁ := by ext x; exact quot.sound ⟨x, rfl, rfl⟩ variable ⦃Z : Top⦄ protected def coequalizer_induced {k : Y ⟶ Z} (e : k ∘ f₀ = k ∘ f₁) : coequalizer_object ⟶ Z := Top.mk_hom (@quot.lift _ coeq_rel _ k (assume y₀ y₁ ⟨x, h₀, h₁⟩, by rw [h₀, h₁]; exact Top.hom_congr e x)) (by continuity) -- TODO: How can we avoid duplicating the proof on the last line here? protected lemma coequalizer_induced_commutes {k : Y ⟶ Z} (e : k ∘ f₀ = k ∘ f₁) : coequalizer_induced e ∘ coequalizer_map = k := Top.hom_eq $ assume x, @quot.lift_beta _ coeq_rel _ k (assume y₀ y₁ ⟨x, h₀, h₁⟩, by rw [h₀, h₁]; exact Top.hom_congr e x) _ protected lemma coequalizer_uniqueness {k k' : coequalizer_object ⟶ Z} (e : k ∘ coequalizer_map = k' ∘ coequalizer_map) : k = k' := Top.hom_eq $ λ x, quot.ind (Top.hom_congr e) x protected def coequalizer_is_coequalizer : Is_coequalizer f₀ f₁ coequalizer_map := Is_coequalizer.mk' coequalizer_commutes coequalizer_induced coequalizer_induced_commutes coequalizer_uniqueness end construction instance Top.has_coequalizers : has_coequalizers.{u} Top := ⟨λ X Y f₀ f₁, { ob := Top.coequalizer_object f₀ f₁, map := Top.coequalizer_map f₀ f₁, is_coequalizer := Top.coequalizer_is_coequalizer _ _ }⟩ end coequalizer section pushout instance : has_pushouts.{u} Top := has_pushouts_of_has_coequalizers_and_coproducts end pushout end «Top» end homotopy_theory.topological_spaces
897d6ab73182c65d61e6cbc74a580bbce085eb7e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/equiv/fin_auto.lean
97edcd46418f18f7cf14d46ded9f0891db1c4147
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,078
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Kenny Lau -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.fin import Mathlib.data.equiv.basic import Mathlib.PostPort universes u namespace Mathlib /-! # Equivalences for `fin n` -/ /-- Equivalence between `fin 0` and `empty`. -/ def fin_zero_equiv : fin 0 ≃ empty := equiv.mk fin_zero_elim empty.elim sorry sorry /-- Equivalence between `fin 0` and `pempty`. -/ def fin_zero_equiv' : fin 0 ≃ pempty := equiv.equiv_pempty sorry /-- Equivalence between `fin 1` and `punit`. -/ def fin_one_equiv : fin 1 ≃ PUnit := equiv.mk (fun (_x : fin 1) => Unit.unit) (fun (_x : PUnit) => 0) sorry sorry /-- Equivalence between `fin 2` and `bool`. -/ def fin_two_equiv : fin (bit0 1) ≃ Bool := equiv.mk (fin.cases false fun (_x : fin 1) => tt) (fun (b : Bool) => cond b 1 0) sorry sorry /-- Equivalence between `fin n.succ` and `option (fin n)` -/ def fin_succ_equiv (n : ℕ) : fin (Nat.succ n) ≃ Option (fin n) := equiv.mk (fun (x : fin (Nat.succ n)) => fin.cases none some x) (fun (x : Option (fin n)) => option.rec_on x 0 fin.succ) sorry sorry @[simp] theorem fin_succ_equiv_zero {n : ℕ} : coe_fn (fin_succ_equiv n) 0 = none := rfl @[simp] theorem fin_succ_equiv_succ {n : ℕ} (m : fin n) : coe_fn (fin_succ_equiv n) (fin.succ m) = some m := sorry @[simp] theorem fin_succ_equiv_symm_none {n : ℕ} : coe_fn (equiv.symm (fin_succ_equiv n)) none = 0 := rfl @[simp] theorem fin_succ_equiv_symm_some {n : ℕ} (m : fin n) : coe_fn (equiv.symm (fin_succ_equiv n)) (some m) = fin.succ m := rfl /-- Equivalence between `fin m ⊕ fin n` and `fin (m + n)` -/ def sum_fin_sum_equiv {m : ℕ} {n : ℕ} : fin m ⊕ fin n ≃ fin (m + n) := equiv.mk (fun (x : fin m ⊕ fin n) => sum.rec_on x (fun (y : fin m) => { val := subtype.val y, property := sorry }) fun (y : fin n) => { val := m + subtype.val y, property := sorry }) (fun (x : fin (m + n)) => dite (subtype.val x < m) (fun (H : subtype.val x < m) => sum.inl { val := subtype.val x, property := H }) fun (H : ¬subtype.val x < m) => sum.inr { val := subtype.val x - m, property := sorry }) sorry sorry /-- Equivalence between `fin m × fin n` and `fin (m * n)` -/ def fin_prod_fin_equiv {m : ℕ} {n : ℕ} : fin m × fin n ≃ fin (m * n) := equiv.mk (fun (x : fin m × fin n) => { val := subtype.val (prod.snd x) + n * subtype.val (prod.fst x), property := sorry }) (fun (x : fin (m * n)) => (fun (H : 0 < n) => ({ val := subtype.val x / n, property := sorry }, { val := subtype.val x % n, property := sorry })) sorry) sorry sorry /-- `fin 0` is a subsingleton. -/ protected instance subsingleton_fin_zero : subsingleton (fin 0) := equiv.subsingleton fin_zero_equiv /-- `fin 1` is a subsingleton. -/ protected instance subsingleton_fin_one : subsingleton (fin 1) := equiv.subsingleton fin_one_equiv end Mathlib
79e0ea272e9115a79011c65ddd8d9dc8edbad66f
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/hott/types/unit.hlean
090f14c73450f0bcfa901a3b08c41bd0a7c78bcf
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
828
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Theorems about the unit type -/ open equiv option eq namespace unit protected definition eta : Π(u : unit), ⋆ = u | eta ⋆ := idp definition unit_equiv_option_empty : unit ≃ option empty := begin fapply equiv.MK, { intro u, exact none}, { intro e, exact star}, { intro e, cases e, reflexivity, contradiction}, { intro u, cases u, reflexivity}, end definition unit_imp_equiv (A : Type) : (unit → A) ≃ A := begin fapply equiv.MK, { intro f, exact f star}, { intro a u, exact a}, { intro a, reflexivity}, { intro f, apply eq_of_homotopy, intro u, cases u, reflexivity}, end end unit open unit is_trunc
9c9ce4ea37008123eba52ffe57aa17c520b1e6c7
4e3bf8e2b29061457a887ac8889e88fa5aa0e34c
/lean/love06_monads_exercise_sheet.lean
bc1fe4f1d95695f3f7df0a1b5d1f1c0a5ea518ad
[]
no_license
mukeshtiwari/logical_verification_2019
9f964c067a71f65eb8884743273fbeef99e6503d
16f62717f55ed5b7b87e03ae0134791a9bef9b9a
refs/heads/master
1,619,158,844,208
1,585,139,500,000
1,585,139,500,000
249,906,380
0
0
null
1,585,118,728,000
1,585,118,727,000
null
UTF-8
Lean
false
false
6,461
lean
/- LoVe Exercise 6: Monads -/ import .love06_monads_demo namespace LoVe /- Question 1: A State Monad with Failure -/ open LoVe.lawful_monad /- We introduce a richer notion of lawful monad that provides an `orelse` operator `<|>` satisfying some laws, given below. `emp` denotes failure. `x <|> y` tries `x` first, falling back on `y` on failure. -/ class lawful_monad_with_orelse (m : Type → Type) extends lawful_monad m, has_orelse m := (emp {} {α} : m α) (emp_orelse {α} (a : m α) : (emp <|> a) = a) (orelse_emp {α} (a : m α) : (a <|> emp) = a) (orelse_assoc {α} (a b c : m α) : ((a <|> b) <|> c) = (a <|> (b <|> c))) (emp_bind {α β} (f : α → m β) : (emp >>= f) = emp) (bind_emp {α β} (f : m α) : (f >>= (λa, emp : α → m β)) = emp) open LoVe.lawful_monad_with_orelse /- We register the laws as simplification rules: -/ attribute [simp] LoVe.lawful_monad_with_orelse.emp_orelse LoVe.lawful_monad_with_orelse.orelse_emp LoVe.lawful_monad_with_orelse.orelse_assoc LoVe.lawful_monad_with_orelse.emp_bind LoVe.lawful_monad_with_orelse.bind_emp /- 1.1. We set up the `option` type constructor to be a `lawful_monad_with_orelse`. Complete the proofs. -/ namespace option /- The following line declares some variables as default arguments to definitions that refer to them. -/ variables {α β γ : Type} def orelse {α} : option α → option α → option α | none b := b | (some a) _ := some a instance lawful_monad_with_orelse_option : lawful_monad_with_orelse option := { emp := λα, none, orelse := @option.orelse, emp_orelse := sorry, orelse_emp := assume α a, match a with | some a := by refl | none := by refl end, orelse_assoc := sorry, emp_bind := assume α β f, by refl, bind_emp := sorry, .. LoVe.option.lawful_monad_option } @[simp] lemma some_bind (a : α) (g : α → option β) : (some a >>= g) = g a := sorry end option /- Let us enable some convenient pattern matching syntax, by instantiating Lean's `monad_fail` type class. (Do not worry if you do not understand what we are referring to.) -/ instance {m : Type → Type} [lawful_monad_with_orelse m] : monad_fail m := ⟨λα msg, emp⟩ /- Now we can write definitions such as the following: -/ def first_of_three {m : Type → Type} [lawful_monad_with_orelse m] (c : m (list ℕ)) : m ℕ := do [n, _, _] ← c, -- look m, this list has three elements! pure n #eval first_of_three (some [1]) #eval first_of_three (some [1, 2, 3]) #eval first_of_three (some [1, 2, 3, 4]) /- Using `lawful_monad_with_orelse` and the `monad_fail` syntax, we can give a consise definition for the `sum_2_5_7` function seen in the lecture. -/ def sum_2_5_7₇ {m : Type → Type} [lawful_monad_with_orelse m] (c : m (list ℕ)) : m ℕ := do (_ :: n2 :: _ :: _ :: n5 :: _ :: n7 :: _) ← c, pure (n2 + n5 + n7) /- 1.2. Now we are ready to define `fstate σ`: a monad with an internal state of type `σ` that can fail (unlike `state σ`). We start with defining `fstate σ α`, where `σ` is the type of the internal state, and `α` is the type of the value stored in the monad. We use `option` to model failure. This means we can also use the monadic behvior of `option` when defining the monadic opertions on `fstate`. **Hint:** Remember that `fstate σ α` is an alias for a function type, so you can use pattern matching and `λs, …` to define values of type `fstate σ α`. **Hint:** `fstate` is very similar to `state` in the lecture demonstration. You can look there for inspiration. -/ def fstate (σ : Type) (α : Type) := σ → option (α × σ) /- 1.3. Define the `get` and `set` function for `fstate`, where `get` returns the state passed along the state monad and `set s` changes the state to `s`. -/ def get {σ : Type} : fstate σ σ := sorry def set {σ : Type} (s : σ) : fstate σ unit := sorry namespace fstate /- The following line declares some variables as default arguments to definitions that refer to them. -/ variables {σ α β γ : Type} /- 1.4. Define the monadic operator `pure` for `fstate`, in such a way that it will satisfy the monadic laws. -/ protected def bind (f : fstate σ α) (g : α → fstate σ β) : fstate σ β | s := f s >>= function.uncurry g #check function.uncurry /- We set up the `>>=` syntax on `fstate`: -/ instance : has_bind (fstate σ) := ⟨@fstate.bind σ⟩ @[simp] lemma bind_apply (f : fstate σ α) (g : α → fstate σ β) (s : σ) : (f >>= g) s = f s >>= function.uncurry g := by refl protected def pure (a : α) : fstate σ α := sorry /- We set up the syntax for `pure` on `fstate`: -/ instance : has_pure (fstate σ) := ⟨@fstate.pure σ⟩ @[simp] lemma pure_apply (a : α) (s : σ) : (pure a : fstate σ α) s = some (a, s) := by refl /- 1.3. Register `fstate` as a monad. **Hint:** `pure` and `bind` are introduced using `protected`, so their names are `fstate.pure` and `fstate.bind`. **Hint**: The `funext` lemma is useful when you need to prove equality between two functions. **Hint**: Sometimes one invocation of `cases` is not enough. In particular, `uncurry f p` requires that `p` is a pair of the form `(a, b)`. **Hint**: `cases f s` only works when `f s` appears in your goal, so you may need to unfold some constants before you can do a `cases`. -/ instance : lawful_monad (fstate σ) := { pure_bind := begin intros α β a f, apply funext, intro s, refl end, bind_pure := sorry, bind_assoc := sorry, .. fstate.has_bind, .. fstate.has_pure } end fstate /- Question 2: Kleisli Operator The kleisly operator `>=>` (not to be confused with `>>=`) is useful for pipelining monadic operations. -/ open monad /- The following two lines declare some variables as default arguments to definitions that refer to them. -/ variables {m : Type → Type} [lawful_monad m] {α β γ δ : Type} variables {f : α → m β} {g : β → m γ} {h : γ → m δ} def kleisli (f : α → m β) (g : β → m γ) : (α → m γ) := λa, f a >>= g infixr ` >=> `:90 := kleisli /- 2.1. Prove that `pure` is a left and right unit for the Kleisli operator. -/ lemma pure_kleisli : pure >=> f = f := sorry lemma kleisli_pure : f >=> pure = f := sorry /- 2.2. Prove associativity of the Kleisli operator. -/ lemma kleisli_assoc : (f >=> g) >=> h = f >=> (g >=> h) := sorry end LoVe
33ba529b7ae3f2fd9ff751c3cce772cfbac21d54
2b647fea4979e0ef600a0df5a8038e0ffa8aa3e5
/src/refined.lean
caf1b2ec04ecbecda1dd33164cf56a8e25db587d
[ "Apache-2.0" ]
permissive
Ja1941/descent-step
f14521728703c1ac820ccb267eb8c4b0235b3475
5691a1424b2002429afda31f192b1122182e520e
refs/heads/main
1,685,992,556,031
1,625,237,833,000
1,625,237,833,000
381,720,708
0
0
null
null
null
null
UTF-8
Lean
false
false
27,072
lean
import data.int.modeq import data.int.parity import tactic.omega.main import tactic import ring_theory.int.basic import ring_theory.coprime import tactic.slim_check import data.real.basic import tactic.ring_exp /- Proof that p ∣ a^2 + b^2 where a b are coprime → p = x^2 + y^2 This is known as descent step, used by Euler proving p ≡ 1 [MOD 4] ↔ p = x^2 + y^2. Descent step combined with reciprocity step, p ≡ 1 [MOD 4] → p ∣ a^2 + b^2 where a b are coprime, finishes the proof. This method can be generalise for x^2 + 2y^2 and x^2 + 3y^2. More of this can be found in "Primes of the Form x^2 + ny^2" by David A. Cox. The proof is actually elementary, but it took me almost 1k lines because I was not yet familiar with how lean defines nat, int, divide and mod etc and I was stuck switching between nat and int. I shorten it down to 600 lines. Although I think I can do better than that, I will advance to other topics rather than spending a lot of time on redundancy because this method is quite elementary and just solves for n = 1, 2, 3 and I just do it for practice. I am not sure if I can formalise the whole book in lean because it requires stuffs like genus theory, higher reciprocity and class field theory. -/ open int --definition of prime in ring theory implies that in natural number theorem prime_pos_int {n : ℤ} (npos : n > 0) (h : prime n) : 2 ≤ n ∧ ∀ (m : ℤ), m > 0 → m ∣ n → m = 1 ∨ m = n := begin have hneq : n ≠ 1, intro hn, apply h.2.1, rw hn, norm_num, split, omega, intros m mpos hm, cases hm with k hk, have hdvd : m * k ∣ m ∨ m * k ∣ k, rw ←hk, apply h.2.2, rw hk, cases hdvd with hdvd hdvd, { right, suffices : m*k = m*1, replace : k = 1, from (mul_right_inj' (by linarith)).mp this, rw [hk, this], simp, simp, exact dvd_antisymm (by linarith) (by linarith) hdvd ⟨k, by simp⟩ }, left, apply dvd_antisymm; try {linarith}, { have : k ≠ 0, intro hf, rw hf at hk, rw hk at h, simp at h, exact h, apply (mul_dvd_mul_iff_right this).mp, simp, exact hdvd }, { exact ⟨m, by simp⟩ } end --this helps when we lift a prime number in int theorem int_prime_eq_nat_prime {a : ℕ} : nat.prime a ↔ prime (a : ℤ) := begin split; intro hp, cases hp with a2 advd, split, linarith, split, { intro hf, rw is_unit_iff_exists_inv at hf, cases hf with b hf, replace hf : (a : ℤ) ∣ 1, from ⟨b, hf.symm⟩, norm_cast at hf, have hf : a = 1, from nat.dvd_one.mp hf, linarith }, { intros b c hdvd, by_contradiction hf, push_neg at hf, cases hf with h₁ h₂, have : is_coprime ↑a b, rw ←gcd_eq_one_iff_coprime, have leftdvd : ↑(int.gcd ↑a b) ∣ (a : ℤ), from (a : ℤ).gcd_dvd_left b, norm_cast at leftdvd, cases advd ((a : ℤ).gcd b) leftdvd with ht hf, exact ht, have : ↑(int.gcd ↑a b) ∣ b, from (a : ℤ).gcd_dvd_right b, rw hf at this, exact absurd this h₁, have hac : ↑a ∣ c, from is_coprime.dvd_of_dvd_mul_left this hdvd, exact h₂ hac }, have apos : (a : ℤ) > 0, norm_cast, have := hp.1, norm_cast at this, exact pos_iff_ne_zero.mpr this, cases prime_pos_int (apos) hp with a2 advd, split; zify, { exact a2 }, { intros m hm, apply advd, norm_cast, rw pos_iff_ne_zero, intro m0, rw [m0, zero_dvd_iff] at hm, linarith, norm_cast, exact hm} end lemma int_dvd_one (x k : ℤ) (h : 1 = k*x) (xnonneg : x ≥ 0) : x = 1 := begin replace h : x ∣ 1, from ⟨k, by {rw h, ring}⟩, lift x to ℕ using xnonneg, norm_cast at *, exact nat.dvd_one.mp h end lemma sq_prime {x : ℤ} (h : prime (x^2)) : false := begin cases em (x = 0) with hn ht, rw hn at h, norm_num at h, exact h, replace h := prime_pos_int (pow_bit0_pos ht 1) h, rw ←sqr_abs at h, cases h with h2 h, cases (h (abs x) (abs_pos.2 ht) (by {rw pow_two, exact dvd.intro (abs x) rfl})) with hx hx, rw hx at h2, linarith, replace hx := eq.trans (mul_one (abs x)) hx, rw [pow_two] at hx, replace ht := abs_pos.2 ht, have := (mul_right_inj' (by linarith)).1 hx, rw ←this at h2, linarith end lemma prime_sq_sq (x y : ℤ) (h : prime (x^2 + y^2)) : is_coprime x y := begin rw ←gcd_eq_one_iff_coprime, cases (int.gcd_dvd_left x y) with n hn, cases (int.gcd_dvd_right x y) with m hm, have cal : x^2 + y^2 = ↑(x.gcd y) * (n * ↑(x.gcd y) * n + m * ↑(x.gcd y) * m), have hx2 : x^2 = (↑(x.gcd y) * n)^2, begin conv begin to_lhs, rw hn, end, end, have hy2 : y^2 = (↑(x.gcd y) * m)^2, begin conv begin to_lhs, rw hm, end, end, rw [hx2, hy2, pow_two, pow_two], rw [mul_assoc, mul_assoc, mul_add], ring, cases em (x = 0 ∧ y = 0) with hn ht, rw [hn.1, hn.2] at h, norm_num at h, exfalso, exact h, have sumpos : x^2 + y^2 > 0, rw not_and_distrib at ht, cases ht with ht ht; have := pow_bit0_pos ht 1; have := pow_two_nonneg x; have := pow_two_nonneg y; linarith, cases (prime_pos_int sumpos h) with h₁ h₂, have gcdpos : ((x.gcd y) : ℤ) > 0, have hnonneg : ((x.gcd y) : ℤ) ≥ 0, have := zero_le (gcd x y), linarith, have hnonzero : ((x.gcd y) : ℤ) ≠ 0, simp, intro hn, rw int.gcd_eq_zero_iff at hn, exact ht hn, exact (ne.symm hnonzero).le_iff_lt.1 hnonneg, specialize h₂ ↑(x.gcd y) gcdpos (dvd.intro _ (eq.symm cal)), clear ht, cases h₂ with h₂ h₂, linarith, cases em (x = 0 ∨ y = 0) with hf ht, cases hf with hf hf; rw hf at h; norm_num at h; exfalso; exact sq_prime h, replace ht : x ≠ 0 ∧ y ≠ 0, exact not_or_distrib.1 ht, replace cal := eq.trans h₂ cal, replace cal : 1 = (n * ↑(x.gcd y) * n + m * ↑(x.gcd y) * m), replace cal := eq.trans (mul_one ↑(x.gcd y)) cal, exact (mul_right_inj' (by linarith)).1 cal, rw h₂ at cal, rw [mul_assoc, mul_assoc, mul_comm (x ^ 2 + y ^ 2) n, mul_comm (x ^ 2 + y ^ 2) m, ←mul_assoc, ←mul_assoc] at cal, rw [←add_mul, ←h₂] at cal, have := int_dvd_one _ _ cal (by linarith), linarith end lemma sq_sq_zero (x y : ℤ) (h : x^2 + y^2 = 0) : x = 0 ∧ y = 0 := begin by_contradiction hn, cases (not_and_distrib.1 hn) with h0 h0; have := pow_bit0_pos h0 1; have := pow_two_nonneg x; have := pow_two_nonneg y; linarith end lemma neg_coprime {x y : ℤ} (h : is_coprime x y) : is_coprime (-x) y := begin unfold is_coprime at *, rcases h with ⟨a, b, h⟩, use (-a), use b, simp, exact h end lemma abs_coprime {x y : ℤ} (h : is_coprime x y) : is_coprime (abs x) (abs y) := begin unfold is_coprime at *, rcases h with ⟨a, b, h⟩, cases em (x ≥ 0) with hx hx; cases em (y ≥ 0) with hy hy, use a, use b, rw [abs_of_nonneg hx, abs_of_nonneg hy], exact h, push_neg at hy, use a, use (-b), rw [abs_of_nonneg hx, abs_of_neg hy], rw ←h, ring, push_neg at hx, use (-a), use b, rw [abs_of_neg hx, abs_of_nonneg hy], rw ←h, ring, push_neg at hx, push_neg at hy, use (-a), use (-b), rw [abs_of_neg hx, abs_of_neg hy], rw ←h, ring end lemma qr_thm_alter (a p : ℤ) (h0 : p > 0) : ∃ q r : ℤ, a = p*q + r ∧ (abs r : ℝ) ≤ (p : ℝ)/2 := begin have key := (div_add_mod a p).symm, cases em ((↑(a % p) : ℝ) ≤ (p/2 : ℝ)) with hl hl, use a / p, use a % p, split, exact key, rw abs_of_nonneg, exact hl, simp, exact mod_nonneg a (show p ≠ 0, by linarith), use ((a / p) + 1), use (a % p - p), split, ring, rw mul_comm, exact key, push_neg at hl, rw abs_of_neg, simp, linarith, simp, exact mod_lt_of_pos a h0 end theorem sum_of_sq_dvd_sum_of_sq {N q : ℤ} (hN : ∃ a b : ℤ, (is_coprime a b ∧ N = a^2 + b^2)) (hpd : prime q ∧ q ∣ N) (hq : ∃ x y : ℤ, q = x^2 + y^2) : ∃ c d : ℤ, is_coprime c d ∧ N/q = c^2 + d^2 := begin rcases hN with ⟨a, b, hab, hN⟩, rcases hq with ⟨x, y, hq⟩, have h₁ : q ∣ x^2 * N - a^2 * q, cases hpd.2 with k hk, use x^2 * k - a^2, rw [mul_sub_left_distrib, hk, mul_left_comm (x^2) q k, mul_comm (a^2) q], have h₂ : x^2 * N - a^2 * q = (x*b + a*y) * (x*b - a*y), calc x^2 * N - a^2 * q = x^2 * (a^2 + b^2) - a^2 * (x^2 + y^2) : by rw [hN, hq] ... = x^2 * b^2 - a^2 * y^2 : by {rw [mul_add, mul_add, ←sub_sub, mul_comm], simp} ... = (x*b)^2 - (a*y)^2 : by rw [←(mul_pow x b 2), ←(mul_pow a y 2)] ... = (x*b + a*y) * (x*b - a*y) : sq_sub_sq _ _, rw h₂ at h₁, clear h₂, revert a, have key : ∀ (a : ℤ), is_coprime a b → N = a ^ 2 + b ^ 2 → q ∣ (x * b + a * y) → (∃ (c d : ℤ), is_coprime c d ∧ N / q = c ^ 2 + d ^ 2), intros a hab hN h, cases h with d hd, have cal : (a - d*y)*y = a*y - d*y^2, by ring, rw [(eq_sub_of_add_eq' hd), hq, (add_mul (x^2) (y^2) d), (mul_comm (y^2) d), ←add_sub, ←add_sub, sub_sub, (add_comm (x*b) (d * y^2)), ←sub_sub] at cal, simp at cal, rw [tactic.ring.add_neg_eq_sub, pow_two, mul_assoc, ←mul_sub] at cal, have h₃ : x ∣ a - d*y, have : x ∣ (a - d*y)*y, rw cal, exact dvd.intro (x*d - b) rfl, exact is_coprime.dvd_of_dvd_mul_right (prime_sq_sq x y (by {rw ←hq, exact hpd.1})) this, cases h₃ with c hc, rw hc at cal, cases em (x = 0 ∨ y = 0) with hf ht, cases hf with hf hf; rw hq at hpd; rw hf at hpd; norm_num at hpd; exfalso; exact sq_prime hpd.1, replace ht : x ≠ 0 ∧ y ≠ 0, exact not_or_distrib.1 ht, rw mul_assoc at cal, replace cal := (mul_right_inj' ht.1).1 cal, have ha : a = x*c + d*y, by linarith, have hb : b = x*d - c*y, by linarith, have key : N = q * (c^2 + d^2), calc N = a^2 + b^2 : hN ... = (x*c + d*y)^2 + (x*d - c*y)^2 : by rw [ha, hb] ... = x^2*c^2 + x^2*d^2 + y^2*c^2 + y^2*d^2 : by ring ... = x^2*(c^2 + d^2) + y^2*(c^2 + d^2) : by ring ... = (x^2 + y^2) * (c^2 + d^2) : by ring ... = q * (c^2 + d^2) : by rw ←hq, use c, use d, split, cases (int.gcd_dvd_left c d) with n hn, cases (int.gcd_dvd_right c d) with m hm, rw hn at ha, rw hn at hb, have : d * y = ↑(c.gcd d) * m * y, begin conv begin to_lhs, rw hm, end, end, rw this at ha, clear this, have : x * d = x * (↑(c.gcd d) * m), begin conv begin to_lhs, rw hm, end, end, rw this at hb, clear this, rw [←mul_assoc, mul_comm x _, mul_assoc, mul_assoc, ←mul_add] at ha, rw [←mul_assoc, mul_comm x _, mul_assoc, mul_assoc, ←mul_sub] at hb, replace ha := dvd.intro _ (eq.symm ha), replace hb := dvd.intro _ (eq.symm hb), have gcd_dvd := int.dvd_gcd ha hb, rw ←gcd_eq_one_iff_coprime, rw ←gcd_eq_one_iff_coprime at hab, cases gcd_dvd with k hk, rw [hab, mul_comm] at hk, have := zero_le (c.gcd d), simp at hk, have := int_dvd_one _ _ hk (by linarith), linarith, rw key, cases em (q = 0) with hw hr, rw hw at hpd, unfold prime at hpd, exact absurd (rfl) (hpd.1.1), rw mul_comm, exact int.mul_div_cancel (c^2 + d^2) hr, intros a hab hN h, cases (prime.div_or_div hpd.1 h) with h h, exact key a hab hN h, apply (key (-a)), exact neg_coprime hab, rw hN, ring, simp, exact h end lemma dvd_leq {a b : ℤ} (anonneg : a ≥ 0) (bpos : b > 0) (hdvd : a ∣ b) : a ≤ b := begin lift a to ℕ using anonneg, lift b to ℕ using (show b ≥ 0, by linarith), norm_cast at *, exact nat.le_of_dvd bpos hdvd end lemma dvd_abs {a b : ℤ} (h : a ∣ b) : a ∣ abs b := begin cases em (b ≥ 0) with hb hb, rw abs_of_nonneg hb, exact h, push_neg at hb, rw abs_of_neg hb, exact (dvd_neg a b).2 h end --some calculations involving inequality, not really important as it's easy for people lemma simp_cal {m n p : ℤ} (habsm: ((abs m) : ℝ) ≤ ↑p / 2) (habsn: ((abs n) : ℝ) ≤ ↑p / 2) (ppos : p ≥ 0) : ((m^2) : ℝ) + ((n^2) : ℝ) ≤ (p^2/2 : ℝ) := begin rw [pow_two, pow_two, ←abs_mul_abs_self ↑m, ←abs_mul_abs_self ↑n], have hm : (abs ↑m) * (abs ↑m ) ≤ ((p / 2) : ℝ) * ((p / 2) : ℝ), have absm : ((abs m) : ℝ) ≥ 0, from abs_nonneg ↑m, exact mul_self_le_mul_self absm habsm, have hn : (abs ↑n) * (abs ↑n) ≤ ((p / 2) : ℝ) * ((p / 2) : ℝ), have absn : ((abs n) : ℝ) ≥ 0, from abs_nonneg ↑n, exact mul_self_le_mul_self absn habsn, have : ((p ^ 2 / 2) : ℝ) = ((p / 2) : ℝ) * ((p / 2) : ℝ) + ((p / 2) : ℝ) * ((p / 2) : ℝ), ring, rw this, exact add_le_add hm hn end lemma largest_divisor {p q N : ℤ} (hdvd: p*q ∣ N) (hp : p > 0) (hq : q > 0) (hN : N > 0) (hleq : (N : ℝ) ≤ (p^2/2 : ℝ) ) : q < p := begin cases hdvd with k hdvd, rw hdvd at hleq, rw pow_two at hleq, simp at hleq, have ppos : (p : ℝ) > 0, simp, linarith, rw mul_assoc at hleq, have : (q : ℝ) * (k : ℝ) ≤ (p : ℝ) / 2, apply (mul_le_mul_left ppos).1, linarith, have hk : k > 0, have : p*q > 0, exact mul_pos hp hq, by_contradiction hk, push_neg at hk, have : N ≤ 0, rw hdvd, exact linarith.mul_nonpos hk this, linarith, have h₁ : (q : ℝ) ≤ (q : ℝ)*(k : ℝ), have hk1 : (k : ℝ) ≥ 1, norm_cast, linarith, suffices : (q : ℝ)*1 ≤ (q : ℝ)*(k : ℝ), linarith, have hq1 : (q : ℝ) > 0, simp, exact hq, exact (mul_le_mul_left hq1).2 hk1, have h₂ : (p : ℝ)/2 ≤ (p : ℝ), linarith, have leq : (q : ℝ) ≤ (p : ℝ), by linarith, simp at leq, have neq : q ≠ p, intro hf, rw hf at hleq, rw hf at this, rw hf at h₁, linarith, exact (ne.le_iff_lt neq).1 leq end lemma descent_wlog {p : ℤ} (hp : prime p) (ho : odd p) (ppos : p > 0) : (∃ a b : ℤ, int.gcd a b = 1 ∧ p ∣ a^2 + b^2) → (∃ m n : ℤ, int.gcd m n = 1 ∧ p ∣ m^2 + n^2 ∧ (m^2 + n^2 : ℝ) ≤ (p^2/2 : ℝ)) := begin intro h, rcases h with ⟨a, b, hab, hdvd⟩, rcases qr_thm_alter (abs a) p ppos with ⟨q, r, hqr, hr⟩, rcases qr_thm_alter (abs b) p ppos with ⟨q', r', hqr', hr'⟩, have hdvd : p ∣ r^2 + r'^2, cases hdvd with k hk, have : a^2 = (abs a)^2, by norm_num, rw this at hk, clear this, have : b^2 = (abs b)^2, by norm_num, rw this at hk, clear this, rw [hqr, hqr'] at hk, have key : p * (k - (q*p*q + 2*q*r + q'*p*q' + 2*q'*r')) = r^2 + r'^2, rw [mul_sub, ←hk], ring, rw ←key, exact dvd.intro _ rfl, cases em (r = 0 ∨ r' = 0) with hl h0, cases hl with h0 h0; rw h0 at *; norm_num at *, have : p ∣ r' → false, intro hpr, cases hpr with k hpr, rw hpr at *, rw ←mul_add at hqr', have hn : p ∣ int.gcd a b, have := gcd_eq_one_iff_coprime.2 (abs_coprime (gcd_eq_one_iff_coprime.1 hab)), rw [hab, ←this], apply int.dvd_gcd, rw hqr, exact dvd.intro q rfl, rw hqr', exact dvd.intro (q' + k) rfl, rw hab at hn, cases hn with m hn, rw mul_comm at hn, exfalso, simp at hn, rw (int_dvd_one p m hn (by linarith)) at hp, simp at hp, exact hp, rw pow_two at hdvd, cases prime.div_or_div hp hdvd with hpr hpr; exact absurd hpr this, have : p ∣ r → false, intro hpr, cases hpr with k hpr, rw hpr at *, rw ←mul_add at hqr, have hn : p ∣ int.gcd a b, have := gcd_eq_one_iff_coprime.2 (abs_coprime (gcd_eq_one_iff_coprime.1 hab)), rw [hab, ←this], apply int.dvd_gcd, rw hqr, exact dvd.intro (q + k) rfl, rw hqr', exact dvd.intro q' rfl, rw hab at hn, cases hn with m hn, rw mul_comm at hn, exfalso, simp at hn, rw (int_dvd_one p m hn (by linarith)) at hp, simp at hp, exact hp, rw pow_two at hdvd, cases prime.div_or_div hp hdvd with hpr hpr; exact absurd hpr this, push_neg at h0, have gcdpos : ((int.gcd r r') : ℤ) > 0, have : int.gcd r r' ≠ 0, by {intro hn, rw int.gcd_eq_zero_iff at hn, exact h0.1 hn.1}, simp, exact pos_iff_ne_zero.2 this, have : ¬(p ∣ ↑(int.gcd r r')), intro hn, have h₁ : (p : ℝ) ≤ (((int.gcd r r') : ℤ) : ℝ), have h₁ : p ≤ ↑(int.gcd r r'), apply dvd_leq, linarith, exact gcdpos, exact hn, simp, norm_cast, exact h₁, have h₂ : (((int.gcd r r') : ℤ) : ℝ) ≤ ((abs r) : ℝ), have h₂ : ↑(int.gcd r r') ≤ abs r, apply dvd_leq, linarith, exact abs_pos.2 h0.1, exact dvd_abs (int.gcd_dvd_left r r'), norm_cast, exact h₂, have h₃ : (p / 2 : ℝ) < (p : ℝ), from half_lt_self (show (p : ℝ) > 0, by {simp, exact ppos}), linarith, cases (int.gcd_dvd_left r r') with n hn, cases (int.gcd_dvd_right r r') with m hm, have cal : r^2 + r'^2 = (↑(r.gcd r'))^2 * (n^2 + m^2), calc r^2 + r'^2 = (↑(r.gcd r') * n)^2 + (↑(r.gcd r') * m)^2 : by rw [←hn, ←hm] ... = (↑(r.gcd r'))^2 * (n^2 + m^2) : by ring, rw cal at hdvd, cases prime.div_or_div hp hdvd with hpdvd hpdvd, rw pow_two at hpdvd, cases prime.div_or_div hp hpdvd with hpdvd hpdvd; exact absurd hpdvd this, use m, use n, split, have hcoprime : ↑(int.gcd m n) = (1 : ℤ), cases (int.gcd_dvd_left m n) with k hk, cases (int.gcd_dvd_right m n) with l hl, rw hk at hm, rw hl at hn, rw ←mul_assoc at hm, rw ←mul_assoc at hn, have gcddvd : ↑(r.gcd r') * ↑(m.gcd n) ∣ ↑(r.gcd r'), apply int.dvd_gcd, use l, exact hn, use k, exact hm, cases gcddvd with k hk, rw mul_assoc at hk, replace hk := eq.trans (mul_one _) hk, have h1 := (mul_right_inj' (show ↑(r.gcd r') ≠ (0 : ℤ), by linarith)).1 hk, rw mul_comm at h1, exact int_dvd_one ↑(m.gcd n) k h1 (coe_zero_le _), linarith, split, rw add_comm, exact hpdvd, have habsm : (↑(abs m) : ℝ) ≤ p/2, suffices : (↑(abs m) : ℝ) ≤ (↑(abs r') : ℝ), simp at *, linarith, rw [hm, abs_mul], simp, have : abs (m : ℝ) > 0, have : (m : ℝ) ≠ 0, intro m0, simp at m0, rw m0 at hm, exact h0.2 hm, exact abs_pos.2 this, rw (le_mul_iff_one_le_left this), simp, linarith, have habsn : (↑(abs n) : ℝ) ≤ p/2, suffices : (↑(abs n) : ℝ) ≤ (↑(abs r) : ℝ), simp at *, linarith, rw [hn, abs_mul], simp, have : abs (n : ℝ) > 0, have : (n : ℝ) ≠ 0, intro n0, simp at n0, rw n0 at hn, exact h0.1 hn, exact abs_pos.2 this, rw (le_mul_iff_one_le_left this), simp, linarith, simp at habsm, simp at habsn, exact simp_cal habsm habsn (show p ≥ 0, by linarith) end lemma dvd_imp_leq_nat {a m : ℕ} (h : a ∣ m) (hm : m ≠ 0) : a ≤ m := begin cases h with k hk, rw hk, suffices : k ≥ 1, have : a*1 ≤ a*k, from (mul_le_mul_left' this a), linarith, by_contradiction, push_neg at h, have : k = 0, by linarith, rw this at *, simp at *, exact hm hk end lemma prime_divisor_nat {a : ℕ} (ha : a ≥ 2) : ∃ p, (nat.prime p) ∧ p ∣ a := begin induction a using nat.strong_induction_on with a hi, dsimp at hi, cases em (nat.prime a) with hpri hpri, use a, split, exact hpri, simp, unfold nat.prime at hpri, push_neg at hpri, cases (hpri ha) with m hm, have : m < a, have : m ≤ a, from dvd_imp_leq_nat (hm.1) (by linarith), exact (ne.le_iff_lt hm.2.2).1 this, cases em (m = 0) with h0 h0, rw h0 at *, simp at *, exfalso, apply hm.2, symmetry, exact hm.1, have : m ≥ 2, rcases hm with ⟨h₀, h₁, h₂⟩, by_contradiction hf, push_neg at hf, interval_cases m, cases (hi m (by linarith) (by linarith)) with p hpdvd, use p, split, exact hpdvd.1, exact dvd_trans hpdvd.2 hm.1, apply h0, refl, apply h₁, refl end lemma prime_divisor_int {a : ℤ} (ha: a ≥ 2) : ∃ p, (prime p) ∧ p ∣ a ∧ p > 0 := begin lift a to ℕ using (show a ≥ 0, by linarith), norm_cast at ha, rcases (prime_divisor_nat ha) with ⟨p, hp, pdvda⟩, use (p : ℤ), norm_cast, have ppos : p ≠ 0, intro hf, rw hf at hp, norm_num at hp, exact ⟨int_prime_eq_nat_prime.mp hp, pdvda, nat.prime.pos hp⟩ end lemma prime_parity {p : ℕ} (hp : nat.prime p) : p = 2 ∨ odd p := begin by_cases p2 : p = 2, left, exact p2, right, have podd : ¬even p, intro hdvd, replace hdvd : 2 ∣ p, from even_iff_two_dvd.mp hdvd, cases (hp.2 2 hdvd) with hf hf, linarith, apply p2, rw hf, exact nat.odd_iff_not_even.mpr podd end lemma exists_prime_divisor {a : ℕ} (leq : 2 ≤ a) : (∃ n : ℕ, a = 2 ^ n) ∨ ∃ p, nat.prime p ∧ p ∣ a ∧ odd p := begin induction a using nat.strong_induction_on with a hi, dsimp at hi, rcases (prime_divisor_nat leq) with ⟨p, hp, k, hk⟩, by_cases k2 : k < 2, { by_cases kval : k = 0, rw kval at hk, simp at hk, linarith, have knonneg : k > 0, exact pos_iff_ne_zero.mpr kval,replace kval : k = 1, by linarith, rw kval at hk, simp at hk, rw hk, cases (prime_parity hp) with p2 podd, { left, use 1, rw p2, simp}, { right, use p, exact ⟨hp, by simp, podd⟩} }, push_neg at k2, have ka : k < a, have ka : k ≤ a, apply dvd_imp_leq_nat, use p, rw hk, ring, linarith, have : k ≠ a, intro hf, rw hf at hk, replace hf : p = 1, suffices : 1*a = p*a, from (mul_left_inj' (by linarith)).mp (eq.symm this), simp, exact hk, rw hf at hp, norm_num at hp, exact (ne.le_iff_lt this).mp ka, cases (hi k ka k2) with hn hq, cases hn with n hn, cases (prime_parity hp) with p2 podd, left, use (n + 1), rw [hk, hn, p2], ring_exp, right, use p, rw hk, exact ⟨hp, dvd.intro k rfl, podd⟩, rcases hq with ⟨q, hq, qdvd, qodd⟩, right, use q, rw hk, exact ⟨hq, dvd_mul_of_dvd_right qdvd p, qodd⟩ end lemma sum_sq_zero {a b : ℤ} (h : a^2 + b^2 = 0) : a = 0 ∧ b = 0 := begin have hal : a^2 ≥ 0, from pow_two_nonneg a, have hbl : b^2 ≥ 0, from pow_two_nonneg b, have hau : a^2 ≤ a^2 + b^2, linarith, have hbu : b^2 ≤ a^2 + b^2, linarith, rw h at hau, rw h at hbu, split, suffices : a^2 = 0, exact pow_eq_zero this, interval_cases (a^2), suffices : b^2 = 0, exact pow_eq_zero this, interval_cases (b^2), all_goals{assumption} end lemma sub_descent_step {p : ℕ} (hp : nat.prime p) (ho : odd p) : (∃ a b : ℤ, ∃ n : ℕ, int.gcd a b = 1 ∧ (p : ℤ) * (2^n) = a^2 + b^2) → (∃ x y : ℤ, ↑p = x^2 + y^2) := begin rintros ⟨a, b, n, gcdab, hpdvd⟩, induction n with n hi generalizing a b, simp at hpdvd, exact ⟨a, b, hpdvd⟩, rw nat.succ_eq_add_one at hpdvd, have key : ∃ a b : ℤ, int.gcd a b = 1 ∧ ↑p*2^n = a^2 + b^2, have prime_two : prime (2 : ℤ), apply int_prime_eq_nat_prime.mp, norm_num, have two_dvd : 2 ∣ (p : ℤ)*2^(n+1), use (↑p*2^n), ring_exp, rcases (sum_of_sq_dvd_sum_of_sq ⟨a, b, gcd_eq_one_iff_coprime.mp gcdab, hpdvd⟩ ⟨prime_two, two_dvd⟩ ⟨1, 1, by norm_num⟩) with ⟨c, d, gcdcd, h⟩, use [c, d], split, exact gcd_eq_one_iff_coprime.mpr gcdcd, rw ←h, rw [pow_add, ←mul_assoc], simp, rw (int.mul_div_cancel (↑p * 2 ^ n) (show (2 : ℤ) ≠ 0, by linarith)), rcases key with ⟨c, d, gcdcd, h⟩, exact hi c d gcdcd h end theorem descent_step {p : ℤ} (hp : prime p) (ho : odd p) (ppos : p > 0) : (∃ a b : ℤ, int.gcd a b = 1 ∧ p ∣ a^2 + b^2) → (∃ x y : ℤ, p = x^2 + y^2) := begin rintros ⟨a, b, gcdab, hpdvd⟩, lift p to ℕ using (show p ≥ 0, by linarith), induction p using nat.strong_induction_on with p hi generalizing a b, dsimp at hi, have : ∃ a b : ℤ, int.gcd a b = 1 ∧ (p : ℤ) ∣ a^2 + b^2, use a, use b, exact ⟨gcdab, hpdvd⟩, have hmn := (descent_wlog hp ho ppos this), clear this, rcases hmn with ⟨a', b', gcdab', hpdvd', ineq⟩, have hab : a'^2 + b'^2 > 0, have hab : a'^2 + b'^2 ≥ 0, have a_sq' := pow_two_nonneg a', have b_sq' := pow_two_nonneg b', linarith, have ab0 : a'^2 + b'^2 ≠ 0, intro hf, rw (int.gcd_eq_zero_iff.mpr (sum_sq_zero hf)) at gcdab', linarith, exact (ne.symm ab0).le_iff_lt.mp hab, obtain ⟨N, hN⟩ := hpdvd', have Nnonneg : N ≥ 0, by_contradiction hf, push_neg at hf, have habf : a'^2 + b'^2 < 0, rw hN, exact linarith.mul_neg hf ppos, linarith, lift N to ℕ using Nnonneg, induction N using nat.strong_induction_on with N hii generalizing a' b', dsimp at hii, by_cases Nval : N < 2, { replace Nval : N = 0 ∨ N = 1, by omega, cases Nval with Nval Nval; rw Nval at hN; simp at hN, rcases (sum_sq_zero hN) with ⟨rfl, rfl⟩, simp at gcdab', exfalso, exact gcdab', use a', use b', rw hN}, push_neg at Nval, rcases (exists_prime_divisor Nval) with ⟨n, hn⟩ | ⟨q, hq, hqdvd, hqodd⟩, replace hp : nat.prime p, from int_prime_eq_nat_prime.mpr hp, simp at ho, replace ho : odd p, from nat.odd_iff_not_even.mpr ho, apply (sub_descent_step hp ho), use [a', b', n], exact ⟨gcdab', by {rw [hN, hn], simp}⟩, cases hqdvd with k hk, rw mul_comm at hk, have kle : k < N, have kleq := dvd_imp_leq_nat ⟨q, hk⟩ (show N ≠ 0, by linarith), have kneq : k ≠ N, intro hf, rw hf at hk, have equi : N*1 = N*q, simp, rw ←hk, have qvalue := (mul_right_inj' (show N ≠ 0, by linarith)).1 equi, rw ←qvalue at hq, norm_num at hq, exact (ne.le_iff_lt kneq).mp kleq, have key : ∃ (a b : ℤ), is_coprime a b ∧ a^2 + b^2 = ↑p*k, have qdvd : ↑q ∣ a'^2 + b'^2, use ↑p*k, rw [hN, hk], simp, ring, have qpos : (q : ℤ) > 0, have q0 : q ≠ 0, intro hf, rw hf at hk, simp at hk, rw hk at Nval, linarith, norm_cast, exact nat.prime.pos hq, have q_sq : ∃ a b : ℤ, (q : ℤ) = a^2 + b^2, have qle : q < p, have qdvd : (p : ℤ)*q ∣ a'^2 + b'^2, use k, rw [hN, hk], norm_cast, ring, have Npos : a'^2 + b'^2 > 0, from hab, have Nleq : (a'^2 + b'^2 : ℝ) ≤ (p^2/2 : ℝ), from ineq, zify, exact (largest_divisor qdvd ppos qpos Npos (by {simp, exact Nleq})), have hq' : prime ↑q, from int_prime_eq_nat_prime.mp hq, have hqodd' : odd (q : ℤ), simp, exact nat.odd_iff_not_even.mp hqodd, apply (hi q qle hq' hqodd' qpos a' b'), exact gcdab', rw [hN, hk], use ↑p*k, simp, ring, have key := sum_of_sq_dvd_sum_of_sq ⟨a', b', gcd_eq_one_iff_coprime.mp gcdab', show a'^2 + b'^2 = a'^2 + b'^2, by refl⟩ ⟨int_prime_eq_nat_prime.mp hq, qdvd⟩ q_sq, rw [hN, hk] at key, rcases key with ⟨c, d, gcdcd, eq⟩, use [c, d], split, exact gcdcd, rw ←eq, simp, rw ←mul_assoc, exact ((p : ℤ) * ↑k).mul_div_cancel (by linarith), rcases key with ⟨c, d, gcdcd, cd_sq⟩, rw ←gcd_eq_one_iff_coprime at gcdcd, have sumineq : (c^2 : ℝ) + ↑d^2 ≤ a'^2 + b'^2, norm_cast, rw [cd_sq, hN], exact (mul_le_mul_left (ppos)).mpr (by linarith), have pos : c^2 + d^2 > 0, have nonneg : c^2 + d^2 ≥ 0, have := pow_two_nonneg c, have := pow_two_nonneg d, linarith, have nonzero : c^2 + d^2 ≠ 0, intro hf, rw (int.gcd_eq_zero_iff.mpr (sum_sq_zero hf)) at gcdcd, linarith, exact (ne.symm nonzero).le_iff_lt.mp nonneg, simp at ineq, exact hii k kle c d gcdcd (by linarith) pos cd_sq end
bdc5b4634e3fd9689a2369ce34abcd857f00bfc4
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/num/bitwise.lean
6d695c3d337fd1c8bb2ae59abc5f6c376d9d18eb
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
7,581
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Bitwise operations using binary representation of integers. -/ import data.num.basic import data.bitvec.core namespace pos_num def lor : pos_num → pos_num → pos_num | 1 (bit0 q) := bit1 q | 1 q := q | (bit0 p) 1 := bit1 p | p 1 := p | (bit0 p) (bit0 q) := bit0 (lor p q) | (bit0 p) (bit1 q) := bit1 (lor p q) | (bit1 p) (bit0 q) := bit1 (lor p q) | (bit1 p) (bit1 q) := bit1 (lor p q) def land : pos_num → pos_num → num | 1 (bit0 q) := 0 | 1 _ := 1 | (bit0 p) 1 := 0 | _ 1 := 1 | (bit0 p) (bit0 q) := num.bit0 (land p q) | (bit0 p) (bit1 q) := num.bit0 (land p q) | (bit1 p) (bit0 q) := num.bit0 (land p q) | (bit1 p) (bit1 q) := num.bit1 (land p q) def ldiff : pos_num → pos_num → num | 1 (bit0 q) := 1 | 1 _ := 0 | (bit0 p) 1 := num.pos (bit0 p) | (bit1 p) 1 := num.pos (bit0 p) | (bit0 p) (bit0 q) := num.bit0 (ldiff p q) | (bit0 p) (bit1 q) := num.bit0 (ldiff p q) | (bit1 p) (bit0 q) := num.bit1 (ldiff p q) | (bit1 p) (bit1 q) := num.bit0 (ldiff p q) def lxor : pos_num → pos_num → num | 1 1 := 0 | 1 (bit0 q) := num.pos (bit1 q) | 1 (bit1 q) := num.pos (bit0 q) | (bit0 p) 1 := num.pos (bit1 p) | (bit1 p) 1 := num.pos (bit0 p) | (bit0 p) (bit0 q) := num.bit0 (lxor p q) | (bit0 p) (bit1 q) := num.bit1 (lxor p q) | (bit1 p) (bit0 q) := num.bit1 (lxor p q) | (bit1 p) (bit1 q) := num.bit0 (lxor p q) def test_bit : pos_num → nat → bool | 1 0 := tt | 1 (n+1) := ff | (bit0 p) 0 := ff | (bit0 p) (n+1) := test_bit p n | (bit1 p) 0 := tt | (bit1 p) (n+1) := test_bit p n def one_bits : pos_num → nat → list nat | 1 d := [d] | (bit0 p) d := one_bits p (d+1) | (bit1 p) d := d :: one_bits p (d+1) def shiftl (p : pos_num) : nat → pos_num | 0 := p | (n+1) := bit0 (shiftl n) def shiftr : pos_num → nat → num | p 0 := num.pos p | 1 (n+1) := 0 | (bit0 p) (n+1) := shiftr p n | (bit1 p) (n+1) := shiftr p n end pos_num namespace num def lor : num → num → num | 0 q := q | p 0 := p | (pos p) (pos q) := pos (p.lor q) def land : num → num → num | 0 q := 0 | p 0 := 0 | (pos p) (pos q) := p.land q def ldiff : num → num → num | 0 q := 0 | p 0 := p | (pos p) (pos q) := p.ldiff q def lxor : num → num → num | 0 q := q | p 0 := p | (pos p) (pos q) := p.lxor q def shiftl : num → nat → num | 0 n := 0 | (pos p) n := pos (p.shiftl n) def shiftr : num → nat → num | 0 n := 0 | (pos p) n := p.shiftr n def test_bit : num → nat → bool | 0 n := ff | (pos p) n := p.test_bit n def one_bits : num → list nat | 0 := [] | (pos p) := p.one_bits 0 end num /-- See `snum`. -/ @[derive has_reflect, derive decidable_eq] inductive nzsnum : Type | msb : bool → nzsnum | bit : bool → nzsnum → nzsnum /-- Alternative representation of integers using a sign bit at the end. The convention on sign here is to have the argument to `msb` denote the sign of the MSB itself, with all higher bits set to the negation of this sign. The result is interpreted in two's complement. 13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb tt)))) -13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb ff)))) As with `num`, a special case must be added for zero, which has no msb, but by two's complement symmetry there is a second special case for -1. Here the `bool` field indicates the sign of the number. 0 = ..0000000(base 2) = zero ff -1 = ..1111111(base 2) = zero tt -/ @[derive has_reflect, derive decidable_eq] inductive snum : Type | zero : bool → snum | nz : nzsnum → snum instance : has_coe nzsnum snum := ⟨snum.nz⟩ instance : has_zero snum := ⟨snum.zero ff⟩ instance : has_one nzsnum := ⟨nzsnum.msb tt⟩ instance : has_one snum := ⟨snum.nz 1⟩ instance : inhabited nzsnum := ⟨1⟩ instance : inhabited snum := ⟨0⟩ /- The snum representation uses a bit string, essentially a list of 0 (ff) and 1 (tt) bits, and the negation of the MSB is sign-extended to all higher bits. -/ namespace nzsnum notation a :: b := bit a b def sign : nzsnum → bool | (msb b) := bnot b | (b :: p) := sign p @[pattern] def not : nzsnum → nzsnum | (msb b) := msb (bnot b) | (b :: p) := bnot b :: not p prefix ~ := not def bit0 : nzsnum → nzsnum := bit ff def bit1 : nzsnum → nzsnum := bit tt def head : nzsnum → bool | (msb b) := b | (b :: p) := b def tail : nzsnum → snum | (msb b) := snum.zero (bnot b) | (b :: p) := p end nzsnum namespace snum open nzsnum def sign : snum → bool | (zero z) := z | (nz p) := p.sign @[pattern] def not : snum → snum | (zero z) := zero (bnot z) | (nz p) := ~p prefix ~ := not @[pattern] def bit : bool → snum → snum | b (zero z) := if b = z then zero b else msb b | b (nz p) := p.bit b notation a :: b := bit a b def bit0 : snum → snum := bit ff def bit1 : snum → snum := bit tt theorem bit_zero (b) : b :: zero b = zero b := by cases b; refl theorem bit_one (b) : b :: zero (bnot b) = msb b := by cases b; refl end snum namespace nzsnum open snum def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p : nzsnum, C p | (msb b) := by rw ←bit_one; exact s b (snum.zero (bnot b)) (z (bnot b)) | (bit b p) := s b p (drec' p) end nzsnum namespace snum open nzsnum def head : snum → bool | (zero z) := z | (nz p) := p.head def tail : snum → snum | (zero z) := zero z | (nz p) := p.tail def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p, C p | (zero b) := z b | (nz p) := p.drec' z s def rec' {α} (z : bool → α) (s : bool → snum → α → α) : snum → α := drec' z s def test_bit : nat → snum → bool | 0 p := head p | (n+1) p := test_bit n (tail p) def succ : snum → snum := rec' (λ b, cond b 0 1) (λb p succp, cond b (ff :: succp) (tt :: p)) def pred : snum → snum := rec' (λ b, cond b (~1) ~0) (λb p predp, cond b (ff :: p) (tt :: predp)) protected def neg (n : snum) : snum := succ ~n instance : has_neg snum := ⟨snum.neg⟩ -- First bit is 0 or 1 (tt), second bit is 0 or -1 (tt) def czadd : bool → bool → snum → snum | ff ff p := p | ff tt p := pred p | tt ff p := succ p | tt tt p := p end snum namespace snum def bits : snum → Π n, vector bool n | p 0 := vector.nil | p (n+1) := head p :: bits (tail p) n def cadd : snum → snum → bool → snum := rec' (λ a p c, czadd c a p) $ λa p IH, rec' (λb c, czadd c b (a :: p)) $ λb q _ c, bitvec.xor3 a b c :: IH q (bitvec.carry a b c) protected def add (a b : snum) : snum := cadd a b ff instance : has_add snum := ⟨snum.add⟩ protected def sub (a b : snum) : snum := a + -b instance : has_sub snum := ⟨snum.sub⟩ protected def mul (a : snum) : snum → snum := rec' (λ b, cond b (-a) 0) $ λb q IH, cond b (bit0 IH + a) (bit0 IH) instance : has_mul snum := ⟨snum.mul⟩ end snum
6a4b2456786c589b6d9aaf133834c0a7a00c5afe
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/ring_theory/int/basic.lean
b79abe2f931546c3e5904e12de9a7a21c384ce71
[ "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
13,905
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import data.int.basic import data.int.gcd import ring_theory.multiplicity import ring_theory.principal_ideal_domain /-! # Divisibility over ℕ and ℤ This file collects results for the integers and natural numbers that use abstract algebra in their proofs or cases of ℕ and ℤ being examples of structures in abstract algebra. ## Main statements * `nat.prime_iff`: `nat.prime` coincides with the general definition of `prime` * `nat.irreducible_iff_prime`: a non-unit natural number is only divisible by `1` iff it is prime * `nat.factors_eq`: the multiset of elements of `nat.factors` is equal to the factors given by the `unique_factorization_monoid` instance * ℤ is a `normalization_monoid` * ℤ is a `gcd_monoid` ## Tags prime, irreducible, natural numbers, integers, normalization monoid, gcd monoid, greatest common divisor, prime factorization, prime factors, unique factorization, unique factors -/ theorem nat.prime_iff {p : ℕ} : p.prime ↔ prime p := begin split; intro h, { refine ⟨h.ne_zero, ⟨_, λ a b, _⟩⟩, { rw nat.is_unit_iff, apply h.ne_one }, { apply h.dvd_mul.1 } }, { refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.ne_one rfl }, exact (add_le_add_right (zero_le p) 2 : _ ) }, { cases hm with n hn, cases h.2.2 m n (hn ▸ dvd_refl _) with hpm hpn, { right, apply nat.dvd_antisymm (dvd.intro _ hn.symm) hpm }, { left, cases n, { exfalso, rw [hn, mul_zero] at h, apply h.ne_zero rfl }, apply nat.eq_of_mul_eq_mul_right (nat.succ_pos _), rw [← hn, one_mul], apply nat.dvd_antisymm hpn (dvd.intro m _), rw [mul_comm, hn], }, } } end theorem nat.irreducible_iff_prime {p : ℕ} : irreducible p ↔ prime p := begin refine ⟨λ h, _, irreducible_of_prime⟩, rw ← nat.prime_iff, refine ⟨_, λ m hm, _⟩, { cases p, { exfalso, apply h.ne_zero rfl }, cases p, { exfalso, apply h.not_unit is_unit_one, }, exact (add_le_add_right (zero_le p) 2 : _ ) }, { cases hm with n hn, cases h.is_unit_or_is_unit hn with um un, { left, rw nat.is_unit_iff.1 um, }, { right, rw [hn, nat.is_unit_iff.1 un, mul_one], } } end namespace nat instance : wf_dvd_monoid ℕ := ⟨begin apply rel_hom.well_founded _ (with_top.well_founded_lt nat.lt_wf), refine ⟨λ x, if x = 0 then ⊤ else x, _⟩, intros a b h, cases a, { exfalso, revert h, simp [dvd_not_unit] }, cases b, {simp [succ_ne_zero, with_top.coe_lt_top]}, cases dvd_and_not_dvd_iff.2 h with h1 h2, simp only [succ_ne_zero, with_top.coe_lt_coe, if_false], apply lt_of_le_of_ne (nat.le_of_dvd (nat.succ_pos _) h1) (λ con, h2 _), rw con, end⟩ instance : unique_factorization_monoid ℕ := ⟨λ _, nat.irreducible_iff_prime⟩ end nat namespace int section normalization_monoid instance : normalization_monoid ℤ := { norm_unit := λa:ℤ, if 0 ≤ a then 1 else -1, norm_unit_zero := if_pos (le_refl _), norm_unit_mul := assume a b hna hnb, begin cases hna.lt_or_lt with ha ha; cases hnb.lt_or_lt with hb hb; simp [mul_nonneg_iff, ha.le, ha.not_le, hb.le, hb.not_le] end, norm_unit_coe_units := assume u, (units_eq_one_or u).elim (assume eq, eq.symm ▸ if_pos zero_le_one) (assume eq, eq.symm ▸ if_neg (not_le_of_gt $ show (-1:ℤ) < 0, by dec_trivial)), } lemma normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : normalize z = z := show z * ↑(ite _ _ _) = z, by rw [if_pos h, units.coe_one, mul_one] lemma normalize_of_neg {z : ℤ} (h : z < 0) : normalize z = -z := show z * ↑(ite _ _ _) = -z, by rw [if_neg (not_le_of_gt h), units.coe_neg, units.coe_one, mul_neg_one] lemma normalize_coe_nat (n : ℕ) : normalize (n : ℤ) = n := normalize_of_nonneg (coe_nat_le_coe_nat_of_le $ nat.zero_le n) theorem coe_nat_abs_eq_normalize (z : ℤ) : (z.nat_abs : ℤ) = normalize z := begin by_cases 0 ≤ z, { simp [nat_abs_of_nonneg h, normalize_of_nonneg h] }, { simp [of_nat_nat_abs_of_nonpos (le_of_not_ge h), normalize_of_neg (lt_of_not_ge h)] } end end normalization_monoid section gcd_monoid instance : gcd_monoid ℤ := { gcd := λa b, int.gcd a b, lcm := λa b, int.lcm a b, gcd_dvd_left := assume a b, int.gcd_dvd_left _ _, gcd_dvd_right := assume a b, int.gcd_dvd_right _ _, dvd_gcd := assume a b c, dvd_gcd, normalize_gcd := assume a b, normalize_coe_nat _, gcd_mul_lcm := by intros; rw [← int.coe_nat_mul, gcd_mul_lcm, coe_nat_abs_eq_normalize], lcm_zero_left := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_left _, lcm_zero_right := assume a, coe_nat_eq_zero.2 $ nat.lcm_zero_right _, .. int.normalization_monoid } lemma coe_gcd (i j : ℤ) : ↑(int.gcd i j) = gcd_monoid.gcd i j := rfl lemma coe_lcm (i j : ℤ) : ↑(int.lcm i j) = gcd_monoid.lcm i j := rfl lemma nat_abs_gcd (i j : ℤ) : nat_abs (gcd_monoid.gcd i j) = int.gcd i j := rfl lemma nat_abs_lcm (i j : ℤ) : nat_abs (gcd_monoid.lcm i j) = int.lcm i j := rfl end gcd_monoid lemma exists_unit_of_abs (a : ℤ) : ∃ (u : ℤ) (h : is_unit u), (int.nat_abs a : ℤ) = u * a := begin cases (nat_abs_eq a) with h, { use [1, is_unit_one], rw [← h, one_mul], }, { use [-1, is_unit_one.neg], rw [ ← neg_eq_iff_neg_eq.mp (eq.symm h)], simp only [neg_mul_eq_neg_mul_symm, one_mul] } end lemma gcd_eq_nat_abs {a b : ℤ} : int.gcd a b = nat.gcd a.nat_abs b.nat_abs := rfl lemma gcd_eq_one_iff_coprime {a b : ℤ} : int.gcd a b = 1 ↔ is_coprime a b := begin split, { intro hg, obtain ⟨ua, hua, ha⟩ := exists_unit_of_abs a, obtain ⟨ub, hub, hb⟩ := exists_unit_of_abs b, use [(nat.gcd_a (int.nat_abs a) (int.nat_abs b)) * ua, (nat.gcd_b (int.nat_abs a) (int.nat_abs b)) * ub], rw [mul_assoc, ← ha, mul_assoc, ← hb, mul_comm, mul_comm _ (int.nat_abs b : ℤ), ← nat.gcd_eq_gcd_ab, ←gcd_eq_nat_abs, hg, int.coe_nat_one] }, { rintro ⟨r, s, h⟩, by_contradiction hg, obtain ⟨p, ⟨hp, ha, hb⟩⟩ := nat.prime.not_coprime_iff_dvd.mp hg, apply nat.prime.not_dvd_one hp, rw [←coe_nat_dvd, int.coe_nat_one, ← h], exact dvd_add (dvd_mul_of_dvd_right (coe_nat_dvd_left.mpr ha) _) (dvd_mul_of_dvd_right (coe_nat_dvd_left.mpr hb) _) } end lemma coprime_iff_nat_coprime {a b : ℤ} : is_coprime a b ↔ nat.coprime a.nat_abs b.nat_abs := by rw [←gcd_eq_one_iff_coprime, nat.coprime_iff_gcd_eq_one, gcd_eq_nat_abs] lemma sq_of_gcd_eq_one {a b c : ℤ} (h : int.gcd a b = 1) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := begin have h' : gcd_monoid.gcd a b = 1, { rw [← coe_gcd, h], dec_trivial }, obtain ⟨d, ⟨u, hu⟩⟩ := exists_associated_pow_of_mul_eq_pow h' heq, use d, rw ← hu, cases int.units_eq_one_or u with hu' hu'; { rw hu', simp } end lemma sq_of_coprime {a b c : ℤ} (h : is_coprime a b) (heq : a * b = c ^ 2) : ∃ (a0 : ℤ), a = a0 ^ 2 ∨ a = - (a0 ^ 2) := sq_of_gcd_eq_one (gcd_eq_one_iff_coprime.mpr h) heq lemma nat_abs_euclidean_domain_gcd (a b : ℤ) : int.nat_abs (euclidean_domain.gcd a b) = int.gcd a b := begin apply nat.dvd_antisymm; rw ← int.coe_nat_dvd, { rw int.nat_abs_dvd, exact int.dvd_gcd (euclidean_domain.gcd_dvd_left _ _) (euclidean_domain.gcd_dvd_right _ _) }, { rw int.dvd_nat_abs, exact euclidean_domain.dvd_gcd (int.gcd_dvd_left _ _) (int.gcd_dvd_right _ _) } end end int theorem irreducible_iff_nat_prime : ∀(a : ℕ), irreducible a ↔ nat.prime a | 0 := by simp [nat.not_prime_zero] | 1 := by simp [nat.prime, one_lt_two] | (n + 2) := have h₁ : ¬n + 2 = 1, from dec_trivial, begin simp [h₁, nat.prime, irreducible_iff, (≥), nat.le_add_left 2 n, (∣)], refine forall_congr (assume a, forall_congr $ assume b, forall_congr $ assume hab, _), by_cases a = 1; simp [h], split, { assume hb, simpa [hb] using hab.symm }, { assume ha, subst ha, have : n + 2 > 0, from dec_trivial, refine nat.eq_of_mul_eq_mul_left this _, rw [← hab, mul_one] } end lemma nat.prime_iff_prime_int {p : ℕ} : p.prime ↔ _root_.prime (p : ℤ) := ⟨λ hp, ⟨int.coe_nat_ne_zero_iff_pos.2 hp.pos, mt int.is_unit_iff_nat_abs_eq.1 hp.ne_one, λ a b h, by rw [← int.dvd_nat_abs, int.coe_nat_dvd, int.nat_abs_mul, hp.dvd_mul] at h; rwa [← int.dvd_nat_abs, int.coe_nat_dvd, ← int.dvd_nat_abs, int.coe_nat_dvd]⟩, λ hp, nat.prime_iff.2 ⟨int.coe_nat_ne_zero.1 hp.1, mt nat.is_unit_iff.1 $ λ h, by simpa [h, not_prime_one] using hp, λ a b, by simpa only [int.coe_nat_dvd, (int.coe_nat_mul _ _).symm] using hp.2.2 a b⟩⟩ /-- Maps an associate class of integers consisting of `-n, n` to `n : ℕ` -/ def associates_int_equiv_nat : associates ℤ ≃ ℕ := begin refine ⟨λz, z.out.nat_abs, λn, associates.mk n, _, _⟩, { refine (assume a, quotient.induction_on' a $ assume a, associates.mk_eq_mk_iff_associated.2 $ associated.symm $ ⟨norm_unit a, _⟩), show normalize a = int.nat_abs (normalize a), rw [int.coe_nat_abs_eq_normalize, normalize_idem] }, { intro n, dsimp, rw [associates.out_mk ↑n, ← int.coe_nat_abs_eq_normalize, int.nat_abs_of_nat, int.nat_abs_of_nat] } end lemma int.prime.dvd_mul {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : p ∣ m.nat_abs ∨ p ∣ n.nat_abs := begin apply (nat.prime.dvd_mul hp).mp, rw ← int.nat_abs_mul, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_mul' {m n : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ m * n) : (p : ℤ) ∣ m ∨ (p : ℤ) ∣ n := begin rw [int.coe_nat_dvd_left, int.coe_nat_dvd_left], exact int.prime.dvd_mul hp h end lemma int.prime.dvd_pow {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : p ∣ n.nat_abs := begin apply @nat.prime.dvd_of_dvd_pow _ _ k hp, rw ← int.nat_abs_pow, exact int.coe_nat_dvd_left.mp h end lemma int.prime.dvd_pow' {n : ℤ} {k p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ n ^ k) : (p : ℤ) ∣ n := begin rw int.coe_nat_dvd_left, exact int.prime.dvd_pow hp h end lemma prime_two_or_dvd_of_dvd_two_mul_pow_self_two {m : ℤ} {p : ℕ} (hp : nat.prime p) (h : (p : ℤ) ∣ 2 * m ^ 2) : p = 2 ∨ p ∣ int.nat_abs m := begin cases int.prime.dvd_mul hp h with hp2 hpp, { apply or.intro_left, exact le_antisymm (nat.le_of_dvd zero_lt_two hp2) (nat.prime.two_le hp) }, { apply or.intro_right, rw [sq, int.nat_abs_mul] at hpp, exact (or_self _).mp ((nat.prime.dvd_mul hp).mp hpp)} end open unique_factorization_monoid theorem nat.factors_eq {n : ℕ} : factors n = n.factors := begin cases n, { simp }, rw [← multiset.rel_eq, ← associated_eq_eq], apply factors_unique (irreducible_of_factor) _, { rw [multiset.coe_prod, nat.prod_factors (nat.succ_pos _)], apply factors_prod (nat.succ_ne_zero _) }, { apply_instance }, { intros x hx, rw [nat.irreducible_iff_prime, ← nat.prime_iff], exact nat.prime_of_mem_factors hx } end lemma nat.factors_multiset_prod_of_irreducible {s : multiset ℕ} (h : ∀ (x : ℕ), x ∈ s → irreducible x) : unique_factorization_monoid.factors (s.prod) = s := begin rw [← multiset.rel_eq, ← associated_eq_eq], apply (unique_factorization_monoid.factors_unique irreducible_of_factor h (factors_prod _)), rw [ne.def, multiset.prod_eq_zero_iff], intro con, exact not_irreducible_zero (h 0 con), end namespace multiplicity lemma finite_int_iff_nat_abs_finite {a b : ℤ} : finite a b ↔ finite a.nat_abs b.nat_abs := by simp only [finite_def, ← int.nat_abs_dvd_abs_iff, int.nat_abs_pow] lemma finite_int_iff {a b : ℤ} : finite a b ↔ (a.nat_abs ≠ 1 ∧ b ≠ 0) := by rw [finite_int_iff_nat_abs_finite, finite_nat_iff, pos_iff_ne_zero, int.nat_abs_ne_zero] instance decidable_nat : decidable_rel (λ a b : ℕ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_nat_iff.symm instance decidable_int : decidable_rel (λ a b : ℤ, (multiplicity a b).dom) := λ a b, decidable_of_iff _ finite_int_iff.symm end multiplicity lemma induction_on_primes {P : ℕ → Prop} (h₀ : P 0) (h₁ : P 1) (h : ∀ p a : ℕ, p.prime → P a → P (p * a)) (n : ℕ) : P n := begin apply unique_factorization_monoid.induction_on_prime, exact h₀, { intros n h, rw nat.is_unit_iff.1 h, exact h₁, }, { intros a p _ hp ha, exact h p a (nat.prime_iff.2 hp) ha, }, end lemma int.associated_nat_abs (k : ℤ) : associated k k.nat_abs := associated_of_dvd_dvd (int.coe_nat_dvd_right.mpr (dvd_refl _)) (int.nat_abs_dvd.mpr (dvd_refl _)) lemma int.prime_iff_nat_abs_prime {k : ℤ} : prime k ↔ nat.prime k.nat_abs := begin rw nat.prime_iff_prime_int, rw prime_iff_of_associated (int.associated_nat_abs k), end theorem int.associated_iff_nat_abs {a b : ℤ} : associated a b ↔ a.nat_abs = b.nat_abs := begin rw [←dvd_dvd_iff_associated, ←int.nat_abs_dvd_abs_iff, ←int.nat_abs_dvd_abs_iff, dvd_dvd_iff_associated], exact associated_iff_eq, end lemma int.associated_iff {a b : ℤ} : associated a b ↔ (a = b ∨ a = -b) := begin rw int.associated_iff_nat_abs, exact int.nat_abs_eq_nat_abs_iff, end namespace int lemma gmultiples_nat_abs (a : ℤ) : add_subgroup.gmultiples (a.nat_abs : ℤ) = add_subgroup.gmultiples a := le_antisymm (add_subgroup.gmultiples_subset (mem_gmultiples_iff.mpr (dvd_nat_abs.mpr (dvd_refl a)))) (add_subgroup.gmultiples_subset (mem_gmultiples_iff.mpr (nat_abs_dvd.mpr (dvd_refl a)))) lemma span_nat_abs (a : ℤ) : ideal.span ({a.nat_abs} : set ℤ) = ideal.span {a} := by { rw ideal.span_singleton_eq_span_singleton, exact (associated_nat_abs _).symm } end int
69b0de0b284dda1e520ddb0d1a5c3f4ed9d54639
6214e13b31733dc9aeb4833db6a6466005763162
/src/preservation.lean
af2f0d9bdf26ae717642ef287414432ef1665bd0
[]
no_license
joshua0pang/esverify-theory
272a250445f3aeea49a7e72d1ab58c2da6618bbe
8565b123c87b0113f83553d7732cd6696c9b5807
refs/heads/master
1,585,873,849,081
1,527,304,393,000
1,527,304,393,000
154,901,199
1
0
null
1,540,593,067,000
1,540,593,067,000
null
UTF-8
Lean
false
false
219,599
lean
import .definitions3 .strengthening .vcgen lemma env_implies_rest {P: prop} {σ: env} {x: var} {v: value}: (⊩ (σ[x↦v]) : P) → (∃Q, (⊩ σ : Q) ∧ ∀σ', σ' ⊨ vc.implies P.to_vc Q.to_vc) := assume σ_verified: ⊩ (σ[x↦v]) : P, begin cases σ_verified, case env.dvcgen.tru Q _ σ_verified ih { from have ∀σ', σ' ⊨ vc.implies (prop.and Q (x ≡ value.true)).to_vc Q.to_vc, from λσ', vc.implies.of_and_left, show ∃(Q_1 : prop), (⊩ σ : Q_1) ∧ ∀σ', σ' ⊨ vc.implies (prop.and Q (x ≡ value.true)).to_vc Q_1.to_vc, from exists.intro Q ⟨σ_verified, this⟩ }, case env.dvcgen.fls Q _ σ_verified { from have ∀σ', σ' ⊨ vc.implies (prop.and Q (x ≡ value.false)).to_vc Q.to_vc, from λσ', vc.implies.of_and_left, show ∃(Q_1 : prop), (⊩ σ : Q_1) ∧ ∀σ', σ' ⊨ vc.implies (prop.and Q (x ≡ value.false)).to_vc Q_1.to_vc, from exists.intro Q ⟨σ_verified, this⟩ }, case env.dvcgen.num n Q _ σ_verified { from have ∀σ', σ' ⊨ vc.implies (prop.and Q (x ≡ value.num n)).to_vc Q.to_vc, from λσ', vc.implies.of_and_left, show ∃(Q_1 : prop), (⊩ σ : Q_1) ∧ ∀σ', σ' ⊨ vc.implies (prop.and Q (x ≡ value.num n)).to_vc Q_1.to_vc, from exists.intro Q ⟨σ_verified, this⟩ }, case env.dvcgen.func σ₂ f fx R S e Q Q₂ Q₃ x_not_in_σ f_not_in_σ₂ fx_not_in_σ₂ f_neq_fx σ₁_verified σ₂_verified fx_in_R fv_R fv_S e_verified func_vc { from let funcp := prop.subst_env (σ₂[f↦value.func f fx R S e σ₂]) (prop.func f fx R (Q₃ (term.app f fx) ⋀ S)) in have ∀σ', σ' ⊨ vc.implies (Q ⋀ x ≡ value.func f fx R S e σ₂ ⋀ funcp).to_vc Q.to_vc, from λσ', vc.implies.of_and_left, show ∃Q_1, (⊩ σ : Q_1) ∧ ∀σ', σ' ⊨ vc.implies (prop.and Q ((x ≡ (value.func f fx R S e σ₂)) ⋀ funcp)).to_vc Q_1.to_vc, from exists.intro Q ⟨σ₁_verified, this⟩ } end lemma env_equiv_of_translation_valid {σ: env} {P: prop}: (⊩ σ: P) → ∀σ', (σ' ⊨ P.to_vc) → (∀x, x ∈ σ → (σ x = σ' x)) := assume σ_verified: ⊩ σ: P, assume σ': env, assume P_valid: σ' ⊨ P.to_vc, assume x: var, assume x_in_σ: x ∈ σ, begin induction σ_verified, case env.dvcgen.empty { cases x_in_σ }, case env.dvcgen.tru σ'' y Q _ _ ih { by_cases (y = x ∧ option.is_none (env.apply σ'' x)) with h, have h1: σ' ⊨ prop.to_vc (prop.term (y ≡ value.true)), from (valid_env.to_vc_and.elim P_valid).right, unfold prop.to_vc at h1, have h2: (σ' y = value.true), from valid_env.subst_of_eq h1, change (env.apply (σ''[y↦value.true]) x = σ' x), unfold env.apply, simp[h], rw[←h.left], from h2.symm, change (env.apply (σ''[y↦value.true]) x = σ' x), unfold env.apply, simp[h], cases not_and_distrib.mp h, cases env.contains.inv x_in_σ, have : (y ≠ x), from a_2, have : (x ≠ y), from this.symm, contradiction, have h1: σ' ⊨ prop.to_vc Q, from (valid_env.to_vc_and.elim P_valid).left, from ih h1 a_3, have h1: σ' ⊨ prop.to_vc Q, from (valid_env.to_vc_and.elim P_valid).left, have h2, from option.some_iff_not_none.mpr a_2, have h4, from option.is_some_iff_exists.mp h2, have h5, from env.contains_apply_equiv.right.mp h4, from ih h1 h5 }, case env.dvcgen.fls σ'' y Q _ _ ih { by_cases (y = x ∧ option.is_none (env.apply σ'' x)) with h, have h1: σ' ⊨ prop.to_vc (y ≡ value.false), from (valid_env.to_vc_and.elim P_valid).right, have h2: (σ' y = value.false), from valid_env.subst_of_eq h1, change (env.apply (σ''[y↦value.false]) x = σ' x), unfold env.apply, simp[h], rw[←h.left], from h2.symm, change (env.apply (σ''[y↦value.false]) x = σ' x), unfold env.apply, simp[h], cases not_and_distrib.mp h, cases env.contains.inv x_in_σ, have : (y ≠ x), from a_2, have : (x ≠ y), from this.symm, contradiction, have h1: σ' ⊨ prop.to_vc Q, from (valid_env.to_vc_and.elim P_valid).left, from ih h1 a_3, have h1: σ' ⊨ prop.to_vc Q, from (valid_env.to_vc_and.elim P_valid).left, have h2, from option.some_iff_not_none.mpr a_2, have h4, from option.is_some_iff_exists.mp h2, have h5, from env.contains_apply_equiv.right.mp h4, from ih h1 h5 }, case env.dvcgen.num n σ'' y Q _ _ ih { by_cases (y = x ∧ option.is_none (env.apply σ'' x)) with h, have h1: σ' ⊨ prop.to_vc (y ≡ value.num n), from (valid_env.to_vc_and.elim P_valid).right, have h2: (σ' y = value.num n), from valid_env.subst_of_eq h1, change (env.apply (σ''[y↦value.num n]) x = σ' x), unfold env.apply, simp[h], rw[←h.left], from h2.symm, change (env.apply (σ''[y↦value.num n]) x = σ' x), unfold env.apply, simp[h], cases not_and_distrib.mp h, cases env.contains.inv x_in_σ, have : (y ≠ x), from a_2, have : (x ≠ y), from this.symm, contradiction, have h1: σ' ⊨ prop.to_vc Q, from (valid_env.to_vc_and.elim P_valid).left, from ih h1 a_3, have h1: σ' ⊨ prop.to_vc Q, from (valid_env.to_vc_and.elim P_valid).left, have h2, from option.some_iff_not_none.mpr a_2, have h4, from option.is_some_iff_exists.mp h2, have h5, from env.contains_apply_equiv.right.mp h4, from ih h1 h5 }, case env.dvcgen.func f σ₂ σ₁ g gx R S e Q₁ Q₂ Q₃ _ _ _ _ _ _ _ fv_R fv_S e_verified _ ih₁ ih₂ { by_cases (f = x ∧ option.is_none (env.apply σ₁ x)) with h, have h0, from (valid_env.to_vc_and.elim P_valid).right, have h1: σ' ⊨ prop.to_vc (f ≡ value.func g gx R S e σ₂), from (valid_env.to_vc_and.elim h0).left, have h2: (σ' f = value.func g gx R S e σ₂), from valid_env.subst_of_eq h1, change (env.apply (σ₁[f↦value.func g gx R S e σ₂]) x = σ' x), unfold env.apply, simp[h], rw[←h.left], from h2.symm, change (env.apply (σ₁[f↦value.func g gx R S e σ₂]) x = σ' x), unfold env.apply, simp[h], cases not_and_distrib.mp h, cases env.contains.inv x_in_σ, have : (f ≠ x), from a_7, have : (x ≠ f), from this.symm, contradiction, have h1: σ' ⊨ prop.to_vc Q₁, from (valid_env.to_vc_and.elim P_valid).left, from ih₁ h1 a_8, have h1: σ' ⊨ prop.to_vc Q₁, from (valid_env.to_vc_and.elim P_valid).left, have h2, from option.some_iff_not_none.mpr a_7, have h4, from option.is_some_iff_exists.mp h2, have h5, from env.contains_apply_equiv.right.mp h4, from ih₁ h1 h5 } end lemma propctx_apply_pq {P: prop} {Q: propctx} {t: term}: (↑P ⋀ Q) t = (P ⋀ Q t) := have h1: P.to_propctx t = P, from unchanged_of_apply_propctx_without_hole, show (↑P ⋀ Q) t = (P ⋀ Q t), by calc (↑P ⋀ Q) t = propctx.apply (propctx.and ↑P Q) t : rfl ... = (propctx.apply ↑P t ⋀ propctx.apply Q t) : by unfold propctx.apply ... = (P.to_propctx t ⋀ propctx.apply Q t) : rfl ... = (P ⋀ propctx.apply Q t) : by rw[h1] lemma propctx_apply_hpq {P₁ P₂: prop} {Q: propctx} {t: term}: (↑P₁ ⋀ ↑P₂ ⋀ Q) t = (P₁ ⋀ P₂ ⋀ Q t) := have h1: P₁.to_propctx t = P₁, from unchanged_of_apply_propctx_without_hole, have h2: P₂.to_propctx t = P₂, from unchanged_of_apply_propctx_without_hole, show (↑P₁ ⋀ ↑P₂ ⋀ Q) t = (P₁ ⋀ P₂ ⋀ Q t), by calc (↑P₁ ⋀ ↑P₂ ⋀ Q) t = propctx.apply (propctx.and ↑P₁ (propctx.and ↑P₂ Q)) t : rfl ... = (propctx.apply ↑P₁ t ⋀ propctx.apply (propctx.and ↑P₂ Q) t) : by unfold propctx.apply ... = (P₁.to_propctx t ⋀ propctx.apply (propctx.and ↑P₂ Q) t) : rfl ... = (P₁ ⋀ propctx.apply (propctx.and ↑P₂ Q) t) : by rw[h1] ... = (P₁ ⋀ propctx.apply ↑P₂ t ⋀ propctx.apply Q t) : by unfold propctx.apply ... = (P₁ ⋀ P₂.to_propctx t ⋀ propctx.apply Q t) : rfl ... = (P₁ ⋀ P₂ ⋀ propctx.apply Q t) : by rw[h2] lemma free_in_prop.apply_propctx_exis {P₁: prop} {Q: propctx} {x: var} {t: term} {S: set var}: FV P₁ ⊆ (S ∪ set.insert x ∅) → FV ((propctx.exis x (P₁ ⋀ Q)) t) ⊆ S ∪ FV (Q t) := assume h0: FV P₁ ⊆ S ∪ set.insert x ∅, have h1: P₁.to_propctx t = P₁, from unchanged_of_apply_propctx_without_hole, have ((propctx.exis x (P₁ ⋀ Q)) t) = prop.exis x (P₁ ⋀ Q t), by calc (propctx.exis x (↑P₁ ⋀ Q)) t = propctx.apply (propctx.exis x (↑P₁ ⋀ Q)) t : rfl ... = prop.exis x (propctx.apply (↑P₁ ⋀ Q) t) : by unfold propctx.apply ... = prop.exis x (propctx.apply (propctx.and ↑P₁ Q) t) : rfl ... = prop.exis x (propctx.apply ↑P₁ t ⋀ propctx.apply Q t) : by unfold propctx.apply ... = prop.exis x (P₁.to_propctx t ⋀ propctx.apply Q t) : rfl ... = prop.exis x (P₁ ⋀ propctx.apply Q t) : by rw[h1], have h2: FV ((propctx.exis x (P₁ ⋀ Q)) t) ⊆ FV (prop.exis x (P₁ ⋀ Q t)), from @eq.subst prop (λa, FV a ⊆ FV (prop.exis x (P₁ ⋀ Q t))) (prop.exis x (P₁ ⋀ Q t)) ((propctx.exis x (P₁ ⋀ Q)) t) this.symm (set.subset.refl (FV (prop.exis x (P₁ ⋀ Q t)))), have h3: FV (prop.exis x (P₁ ⋀ Q t)) ⊆ S ∪ FV (Q t), from ( assume z: var, assume : z ∈ FV (prop.exis x (P₁ ⋀ Q t)), have z_neq_x: z ≠ x, from (free_in_prop.exis.inv this).left, have z ∈ FV (P₁ ⋀ Q t), from (free_in_prop.exis.inv this).right, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P₁, have z ∈ (S ∪ set.insert x ∅), from set.mem_of_subset_of_mem h0 this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ S, show z ∈ S ∪ FV (Q t), from set.mem_union_left (FV (Q t)) this ) ( assume : z ∈ set.insert x ∅, have z = x, from set.eq_of_mem_singleton this, show z ∈ S ∪ FV (Q t), from absurd this z_neq_x ) ) ( assume : z ∈ FV (Q t), show z ∈ S ∪ FV (Q t), from set.mem_union_right S this ) ), show FV ((propctx.exis x (P₁ ⋀ Q)) t) ⊆ S ∪ FV (Q t), from set.subset.trans h2 h3 lemma vc.implies.apply_propctx_exis {P₁ P₂: prop} {Q: propctx} {x: var} {t: term} {σ: env}: (σ ⊨ vc.implies P₁.to_vc P₂.to_vc) → σ ⊨ vc.implies (P₁ ⋀ Q t).to_vc ((propctx.exis x (P₂ ⋀ Q)) t).to_vc := assume h0: σ ⊨ vc.implies P₁.to_vc P₂.to_vc, have h1: P₂.to_propctx t = P₂, from unchanged_of_apply_propctx_without_hole, have ((propctx.exis x (P₂ ⋀ Q)) t) = prop.exis x (P₂ ⋀ Q t), by calc (propctx.exis x (↑P₂ ⋀ Q)) t = propctx.apply (propctx.exis x (↑P₂ ⋀ Q)) t : rfl ... = prop.exis x (propctx.apply (↑P₂ ⋀ Q) t) : by unfold propctx.apply ... = prop.exis x (propctx.apply (propctx.and ↑P₂ Q) t) : rfl ... = prop.exis x (propctx.apply ↑P₂ t ⋀ propctx.apply Q t) : by unfold propctx.apply ... = prop.exis x (P₂.to_propctx t ⋀ propctx.apply Q t) : rfl ... = prop.exis x (P₂ ⋀ propctx.apply Q t) : by rw[h1], have h2: σ ⊨ vc.implies (prop.exis x (P₂ ⋀ propctx.apply Q t)).to_vc ((propctx.exis x (P₂ ⋀ Q)) t).to_vc, from this ▸ vc.implies.self, have h3: σ ⊨ vc.implies (P₁ ⋀ Q t).to_vc (P₂ ⋀ Q t).to_vc, from vc.implies.same_right (λ_, h0), have h4: σ ⊨ vc.implies (P₂ ⋀ Q t).to_vc (prop.exis x (P₂ ⋀ Q t)).to_vc, from vc.implies.exis, show σ ⊨ vc.implies (P₁ ⋀ Q t).to_vc ((propctx.exis x (P₂ ⋀ Q)) t).to_vc, from vc.implies.trans (vc.implies.trans h3 h4) h2 lemma free_dominates_helper {R: spec} {P P₁ P₂: prop} {Q: propctx} {x: var}: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc) → (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc) → (FV P₁ = set.insert x ∅) → (x ∈ FV P₂) → (FV P₂ ⊆ FV P ∪ set.insert x ∅) → (FV (↑R ⋀ P ⋀ P₁) = FV ((↑R ⋀ P) ⋀ P₂)) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ P₁).to_vc ((↑R ⋀ P) ⋀ P₂).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ P₁) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) v) ⊆ FV ((↑(P ⋀ P₁) ⋀ Q) v)) := assume h1: ∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc, assume h2: ∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc, assume h3a: FV P₁ = set.insert x ∅, assume h3b: x ∈ FV P₂, assume h3c: FV P₂ ⊆ FV P ∪ set.insert x ∅, have h4a: FV (↑R ⋀ P ⋀ P₁) = FV (↑R ⋀ P ⋀ P₂), from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV (↑R ⋀ P ⋀ P₁), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z R, show z ∈ FV (↑R ⋀ P ⋀ P₂), from free_in_prop.and₁ this ) ( assume : z ∈ FV (P ⋀ P₁), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P, have z ∈ FV (P ⋀ P₂), from free_in_prop.and₁ this, show z ∈ FV (↑R ⋀ P ⋀ P₂), from free_in_prop.and₂ this ) ( assume : z ∈ FV P₁, have z ∈ set.insert x ∅, from h3a ▸ this, have z = x, from set.eq_of_mem_singleton this, have z ∈ FV P₂, from this.symm ▸ h3b, have z ∈ FV (P ⋀ P₂), from free_in_prop.and₂ this, show z ∈ FV (↑R ⋀ P ⋀ P₂), from free_in_prop.and₂ this ) ) ) ( assume z: var, assume : z ∈ FV (↑R ⋀ P ⋀ P₂), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z R, show z ∈ FV (↑R ⋀ P ⋀ P₁), from free_in_prop.and₁ this ) ( assume : z ∈ FV (P ⋀ P₂), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P, have z ∈ FV (P ⋀ P₁), from free_in_prop.and₁ this, show z ∈ FV (↑R ⋀ P ⋀ P₁), from free_in_prop.and₂ this ) ( assume : z ∈ FV P₂, have z ∈ FV P ∪ set.insert x ∅, from set.mem_of_subset_of_mem h3c this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P, have z ∈ FV (P ⋀ P₁), from free_in_prop.and₁ this, show z ∈ FV (↑R ⋀ P ⋀ P₁), from free_in_prop.and₂ this ) ( assume : z ∈ set.insert x ∅, have z ∈ FV P₁, from h3a.symm ▸ this, have z ∈ FV (P ⋀ P₁), from free_in_prop.and₂ this, show z ∈ FV (↑R ⋀ P ⋀ P₁), from free_in_prop.and₂ this ) ) ) ), have h4b: FV (↑R ⋀ P ⋀ P₂) = FV ((↑R ⋀ P) ⋀ P₂), from free_in_prop.and_assoc, have h4: FV (↑R ⋀ P ⋀ P₁ ) = FV ((↑R ⋀ P) ⋀ P₂), from eq.trans h4a h4b, have h5: ∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ P₁).to_vc ((↑R ⋀ P) ⋀ P₂).to_vc, from ( assume σ: env, have h5a: σ ⊨ vc.implies (↑R ⋀ P ⋀ P₁).to_vc ((↑R ⋀ P) ⋀ P₁).to_vc, from vc.implies.and_assoc, have h5b: σ ⊨ vc.implies ((↑R ⋀ P) ⋀ P₁).to_vc ((↑R ⋀ P) ⋀ P₂).to_vc, from vc.implies.same_left ( assume : σ ⊨ (↑R ⋀ P).to_vc, have σ ⊨ (P).to_vc, from (valid_env.to_vc_and.elim this).right, h1 σ this ), show σ ⊨ vc.implies (↑R ⋀ P ⋀ P₁).to_vc ((↑R ⋀ P) ⋀ P₂).to_vc, from vc.implies.trans h5a h5b ), have h6: (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ P₁) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) t).to_vc), from ( assume σ: env, assume t: term, have h6: ((↑(P ⋀ P₁) ⋀ Q) t) = ((P ⋀ P₁) ⋀ Q t), from propctx_apply_pq, have h7: ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) t) = (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) t), from propctx_apply_pq, have h8a: σ ⊨ vc.implies ((P ⋀ P₁) ⋀ Q t).to_vc (P ⋀ P₁ ⋀ Q t).to_vc, from vc.implies.and_assoc.symm, have h8b: σ ⊨ vc.implies (P ⋀ P₁ ⋀ Q t).to_vc (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) t).to_vc, from vc.implies.same_left ( assume : σ ⊨ P.to_vc, show σ ⊨ vc.implies (P₁ ⋀ Q t).to_vc ((propctx.exis x (↑P₂ ⋀ Q)) t).to_vc, from vc.implies.apply_propctx_exis (h2 σ this) ), have h9: σ ⊨ vc.implies ((P ⋀ P₁) ⋀ Q t).to_vc (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) t).to_vc, from vc.implies.trans h8a h8b, show σ ⊨ vc.implies ((↑(P ⋀ P₁) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) t).to_vc, from h6.symm ▸ h7.symm ▸ h9 ), have h7: (∀v: value, FV ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) v) ⊆ FV ((↑(P ⋀ P₁) ⋀ Q) v)), from ( assume v: value, have h6: ((↑(P ⋀ P₁) ⋀ Q) v) = ((P ⋀ P₁) ⋀ Q v), from propctx_apply_pq, have h7: ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) v) = (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) v), from propctx_apply_pq, have h9a: FV ((propctx.exis x (P₂.to_propctx ⋀ Q)) v) ⊆ FV P ∪ FV (Q v), from @free_in_prop.apply_propctx_exis P₂ Q x v (FV P) h3c, have h9a: FV (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) v) ⊆ FV (P ⋀ P₁ ⋀ Q v), from ( assume z: var, assume : z ∈ FV (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) v), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ P₁ ⋀ Q v), from free_in_prop.and₁ this ) ( assume : z ∈ FV ((propctx.exis x (↑P₂ ⋀ Q)) v), have z ∈ FV P ∪ FV (Q v), from set.mem_of_subset_of_mem h9a this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ P₁ ⋀ Q v), from free_in_prop.and₁ this ) ( assume : z ∈ FV (Q v), have z ∈ FV (P₁ ⋀ Q v), from free_in_prop.and₂ this, show z ∈ FV (P ⋀ P₁ ⋀ Q v), from free_in_prop.and₂ this ) ) ), have h9b: FV (P ⋀ P₁ ⋀ Q v) ⊆ FV ((P ⋀ P₁) ⋀ Q v), from set.subset_of_eq free_in_prop.and_assoc, have h9c: FV (P ⋀ (propctx.exis x (↑P₂ ⋀ Q)) v) ⊆ FV ((P ⋀ P₁) ⋀ Q v), from set.subset.trans h9a h9b, show FV ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) v) ⊆ FV ((↑(P ⋀ P₁) ⋀ Q) v), from h6.symm ▸ h7.symm ▸ h9c ), ⟨h4, ⟨h5, ⟨h6, h7⟩⟩⟩ lemma free_dominates_helper_eq_free {R: spec} {P P₁ P₂: prop} {Q: propctx} {x: var}: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc) → (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc) → (FV P₁ = set.insert x ∅) → (FV P₂ = set.insert x ∅) → (FV (↑R ⋀ P ⋀ P₁) = FV ((↑R ⋀ P) ⋀ P₂)) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ P₁).to_vc ((↑R ⋀ P) ⋀ P₂).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ P₁) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis x (↑P₂ ⋀ Q)) v) ⊆ FV ((↑(P ⋀ P₁) ⋀ Q) v)) := assume h1: ∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc, assume h2: ∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies P₁.to_vc P₂.to_vc, assume h3a: FV P₁ = set.insert x ∅, assume h3a2: FV P₂ = set.insert x ∅, have x ∈ set.insert x ∅, from set.mem_singleton x, have h3b: x ∈ FV P₂, from h3a2.symm ▸ this, have h3c: FV P₂ ⊆ FV P ∪ set.insert x ∅, from ( assume z: var, assume : z ∈ FV P₂, have z ∈ set.insert x ∅, from h3a2 ▸ this, have z = x, from set.eq_of_mem_singleton this, have z ∈ set.insert x ∅, from this.symm ▸ set.mem_singleton x, show z ∈ FV P ∪ set.insert x ∅, from set.mem_union_right (FV P) this ), free_dominates_helper h1 h2 h3a h3b h3c lemma exp.preservation {R: spec} {σ σ': env} {P: prop} {e e': exp} {Q: propctx}: (⊩ σ : P) → FV (spec.to_prop R) ⊆ FV P → (σ ⊨ R.to_prop.to_vc) → (R ⋀ P ⊩ e : Q) → ((R, σ, e) ⟹ (R, σ', e')) → ∃Q', (⊩ₛ (R, σ', e') : Q') ∧ (∀σ' t, σ' ⊨ vc.implies (Q' t).to_vc ((↑P ⋀ Q) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ Q) v) ⊆ FV (Q' v)) := assume σ_verified: ⊩ σ : P, assume fv_R: FV (spec.to_prop R) ⊆ FV P, assume R_valid: (σ ⊨ R.to_prop.to_vc), assume e_verified: R ⋀ P ⊩ e : Q, assume e_steps: ((R, σ, e) ⟹ (R, σ', e')), begin cases e_verified, case exp.dvcgen.tru x e' Q x_not_free e'_verified { cases e_steps, case dstep.tru { from have x ∉ σ, from ( assume : x ∈ σ, have x ∈ σ.dom, from this, have x ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have x ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from x_not_free this ), have σ'_verified: ⊩ (σ[x↦value.true]) : P ⋀ x ≡ value.true, from env.dvcgen.tru this σ_verified, have fv_R': FV R.to_prop ⊆ FV (P ⋀ x ≡ value.true), from set.subset.trans fv_R free_in_prop.and_left_subset, have R_valid': σ[x↦value.true] ⊨ R.to_prop.to_vc, from valid_with_additional_var R_valid, have h1: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (x ≡ value.true) (x ≡ value.true)), from λ_ _, vc.implies.self, have h2: FV (prop.term (x ≡ value.true)) = set.insert x ∅, from set.eq_of_subset_of_subset ( assume z: var, assume : free_in_prop z (x ≡ value.true), have free_in_term z (x ≡ value.true), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, show z ∈ set.insert x ∅, from (set.mem_singleton_iff z x).mpr this ) ( assume : free_in_term z value.true, show z ∈ set.insert x ∅, from absurd this free_in_term.value.inv ) ) ( assume z: var, assume : z ∈ set.insert x ∅, have z = x, from (set.mem_singleton_iff z x).mp this, have free_in_term z x, from this ▸ free_in_term.var z, have free_in_term z (x ≡ value.true), from free_in_term.binop₁ this, show free_in_prop z (x ≡ value.true), from free_in_prop.term this ), have h3: (FV (↑R ⋀ P ⋀ (x ≡ value.true)) = FV ((↑R ⋀ P) ⋀ (x ≡ value.true))) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ (x ≡ value.true)).to_vc ((↑R ⋀ P) ⋀ (x ≡ value.true)).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ (x ≡ value.true)) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑(x ≡ value.true) ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis x (↑(x ≡ value.true) ⋀ Q)) v) ⊆ FV ((↑(P ⋀ (x ≡ value.true)) ⋀ Q) v)), from @free_dominates_helper_eq_free R P (x ≡ value.true) (x ≡ value.true) Q x h1 h1 h2 h2, have e'_verified': ↑R ⋀ P ⋀ x ≡ value.true ⊩ e' : Q, from strengthen_exp e'_verified (↑R ⋀ P ⋀ x ≡ value.true) h3.left h3.right.left, have h4: ⊩ₛ (R, σ[x↦value.true], e') : ↑(P ⋀ x ≡ value.true) ⋀ Q, from stack.dvcgen.top σ'_verified fv_R' R_valid' e'_verified', exists.intro (↑(P ⋀ x ≡ value.true) ⋀ Q) ⟨h4, h3.right.right⟩ } }, case exp.dvcgen.fals x e' Q x_not_free e'_verified { cases e_steps, case dstep.fals { from have x ∉ σ, from ( assume : x ∈ σ, have x ∈ σ.dom, from this, have x ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have x ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from x_not_free this ), have σ'_verified: ⊩ (σ[x↦value.false]) : P ⋀ x ≡ value.false, from env.dvcgen.fls this σ_verified, have fv_R': FV R.to_prop ⊆ FV (P ⋀ x ≡ value.false), from set.subset.trans fv_R free_in_prop.and_left_subset, have R_valid': σ[x↦value.false] ⊨ R.to_prop.to_vc, from valid_with_additional_var R_valid, have h1: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (x ≡ value.false) (x ≡ value.false)), from λ_ _, vc.implies.self, have h2: FV (prop.term (x ≡ value.false)) = set.insert x ∅, from set.eq_of_subset_of_subset ( assume z: var, assume : free_in_prop z (x ≡ value.false), have free_in_term z (x ≡ value.false), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, show z ∈ set.insert x ∅, from (set.mem_singleton_iff z x).mpr this ) ( assume : free_in_term z value.false, show z ∈ set.insert x ∅, from absurd this free_in_term.value.inv ) ) ( assume z: var, assume : z ∈ set.insert x ∅, have z = x, from (set.mem_singleton_iff z x).mp this, have free_in_term z x, from this ▸ free_in_term.var z, have free_in_term z (x ≡ value.false), from free_in_term.binop₁ this, show free_in_prop z (x ≡ value.false), from free_in_prop.term this ), have h3: (FV (↑R ⋀ P ⋀ (x ≡ value.false)) = FV ((↑R ⋀ P) ⋀ (x ≡ value.false))) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ (x ≡ value.false)).to_vc ((↑R ⋀ P) ⋀ (x ≡ value.false)).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ (x ≡ value.false)) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑(x ≡ value.false) ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis x (↑(x ≡ value.false) ⋀ Q)) v) ⊆ FV ((↑(P ⋀ (x ≡ value.false)) ⋀ Q) v)), from @free_dominates_helper_eq_free R P (x ≡ value.false) (x ≡ value.false) Q x h1 h1 h2 h2, have e'_verified': ↑R ⋀ P ⋀ x ≡ value.false ⊩ e' : Q, from strengthen_exp e'_verified (↑R ⋀ P ⋀ x ≡ value.false) h3.left h3.right.left, have h4: ⊩ₛ (R, σ[x↦value.false], e') : ↑(P ⋀ x ≡ value.false) ⋀ Q, from stack.dvcgen.top σ'_verified fv_R' R_valid' e'_verified', exists.intro (↑(P ⋀ x ≡ value.false) ⋀ Q) ⟨h4, h3.right.right⟩ } }, case exp.dvcgen.num x n e' Q x_not_free e'_verified { cases e_steps, case dstep.num { from have x ∉ σ, from ( assume : x ∈ σ, have x ∈ σ.dom, from this, have x ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have x ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from x_not_free this ), have σ'_verified: ⊩ (σ[x↦value.num n]) : P ⋀ x ≡ value.num n, from env.dvcgen.num this σ_verified, have fv_R': FV R.to_prop ⊆ FV (P ⋀ x ≡ value.num n), from set.subset.trans fv_R free_in_prop.and_left_subset, have R_valid': σ[x↦value.num n] ⊨ R.to_prop.to_vc, from valid_with_additional_var R_valid, have h1: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (x ≡ value.num n) (x ≡ value.num n)), from λ_ _, vc.implies.self, have h2: FV (prop.term (x ≡ value.num n)) = set.insert x ∅, from set.eq_of_subset_of_subset ( assume z: var, assume : free_in_prop z (x ≡ value.num n), have free_in_term z (x ≡ value.num n), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, show z ∈ set.insert x ∅, from (set.mem_singleton_iff z x).mpr this ) ( assume : free_in_term z (value.num n), show z ∈ set.insert x ∅, from absurd this free_in_term.value.inv ) ) ( assume z: var, assume : z ∈ set.insert x ∅, have z = x, from (set.mem_singleton_iff z x).mp this, have free_in_term z x, from this ▸ free_in_term.var z, have free_in_term z (x ≡ value.num n), from free_in_term.binop₁ this, show free_in_prop z (x ≡ value.num n), from free_in_prop.term this ), have h3: (FV (↑R ⋀ P ⋀ (x ≡ value.num n)) = FV ((↑R ⋀ P) ⋀ (x ≡ value.num n))) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ (x ≡ value.num n)).to_vc ((↑R ⋀ P) ⋀ (x ≡ value.num n)).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ (x ≡ value.num n)) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis x (↑(x ≡ value.num n) ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis x (↑(x ≡ value.num n) ⋀ Q)) v) ⊆ FV ((↑(P ⋀ (x ≡ value.num n)) ⋀ Q) v)), from @free_dominates_helper_eq_free R P (x ≡ value.num n) (x ≡ value.num n) Q x h1 h1 h2 h2, have e'_verified': ↑R ⋀ P ⋀ x ≡ value.num n ⊩ e' : Q, from strengthen_exp e'_verified (↑R ⋀ P ⋀ x ≡ value.num n) h3.left h3.right.left, have h4: ⊩ₛ (R, σ[x↦value.num n], e') : ↑(P ⋀ x ≡ value.num n) ⋀ Q, from stack.dvcgen.top σ'_verified fv_R' R_valid' e'_verified', exists.intro (↑(P ⋀ x ≡ value.num n) ⋀ Q) ⟨h4, h3.right.right⟩ } }, case exp.dvcgen.func f x R' S' e₁ e₂ Q₁ Q₂ f_not_in x_not_in f_neq_x x_free_in_R' fv_R' fv_S' e₁_verified e₂_verified func_vc { cases e_steps, case dstep.closure { from have f_not_in_σ: f ∉ σ, from ( assume : f ∈ σ, have f ∈ σ.dom, from this, have f ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have f ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from f_not_in this ), have x_not_in_σ: x ∉ σ, from ( assume : x ∈ σ, have x ∈ σ.dom, from this, have x ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have x ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from x_not_in this ), have fv_R'': FV R'.to_prop ⊆ FV P ∪ { f, x }, from ( assume z: var, assume : z ∈ FV R'.to_prop, have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_R' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ { f, x }, from set.mem_union_left { f, x } this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ { f, x }, from set.mem_union_left { f, x } this ) ) ( assume : z ∈ {f, x}, show z ∈ FV P ∪ { f, x }, from set.mem_union_right (FV P) this ) ), have fv_S'': FV S'.to_prop ⊆ FV P ∪ { f, x }, from ( assume z: var, assume : z ∈ FV S'.to_prop, have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_S' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ { f, x }, from set.mem_union_left { f, x } this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ { f, x }, from set.mem_union_left { f, x } this ) ) ( assume : z ∈ {f, x}, show z ∈ FV P ∪ { f, x }, from set.mem_union_right (FV P) this ) ), have e₁_verified': P ⋀ spec.func f x R' S' ⋀ R' ⊩ e₁ : Q₁, from ( have FV P = FV (↑R ⋀ P), from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV P, show z ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this ) ( assume z: var, assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, show z ∈ FV P, from set.mem_of_subset_of_mem fv_R this ) id ), have h1: FV (P ⋀ spec.func f x R' S' ⋀ R') = FV ((↑R ⋀ P) ⋀ ↑(spec.func ↑f x R' S') ⋀ ↑R'), from free_in_prop.same_right this, have h2: ∀σ', σ' ⊨ vc.implies (P ⋀ spec.func f x R' S' ⋀ R').to_vc ((↑R ⋀ P) ⋀ ↑(spec.func ↑f x R' S') ⋀ ↑R').to_vc, from ( assume σ': env, show σ' ⊨ vc.implies (P ⋀ spec.func f x R' S' ⋀ R').to_vc ((↑R ⋀ P) ⋀ ↑(spec.func ↑f x R' S') ⋀ ↑R').to_vc, from vc.implies.same_right ( assume _, show σ' ⊨ vc.implies P.to_vc (↑R ⋀ P).to_vc, by begin apply valid_env.mpr, assume h4, apply valid_env.to_vc_and, have h5: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' h4, from valid_env.equiv_env h5 R_valid, from h4 end ) ), show (P ⋀ spec.func f x R' S' ⋀ R') ⊩ e₁ : Q₁, from strengthen_exp e₁_verified (P ⋀ spec.func f x R' S' ⋀ R') h1 h2 ), have func_vc': ⦃prop.implies (P ⋀ spec.func f x R' S' ⋀ R' ⋀ Q₁ (term.app f x)) S'⦄, from ( assume σ': env, have h2: σ' ⊨ vc.implies P.to_vc (↑R ⋀ P).to_vc, by begin apply valid_env.mpr, assume h4, apply valid_env.to_vc_and, have h5: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' h4, from valid_env.equiv_env h5 R_valid, from h4 end, have h3: FV (↑R ⋀ P) ⊆ FV P, by begin assume y, assume h4, cases (free_in_prop.and.inv h4) with h5 h5, have h6: y ∈ FV R.to_prop, from h5, from set.mem_of_mem_of_subset h6 fv_R, from h5 end, strengthen_vc_with_q h2 h3 (func_vc σ') ), let vf := value.func f x R' S' e₁ σ in let P' := (↑(f ≡ vf) ⋀ prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))) in let Q' := (prop.func f x R' (Q₁ (term.app f x) ⋀ S')) in have σ'_verified: ⊩ (σ[f↦vf]) : P ⋀ P', from env.dvcgen.func f_not_in_σ f_not_in_σ x_not_in_σ f_neq_x σ_verified σ_verified x_free_in_R' fv_R'' fv_S'' e₁_verified' func_vc', have fv_R'': FV R.to_prop ⊆ FV (P ⋀ P'), from set.subset.trans fv_R free_in_prop.and_left_subset, have R_valid': σ[f↦vf] ⊨ R.to_prop.to_vc, from valid_with_additional_var R_valid, have h1: (∀σ', (σ' ⊨ P.to_vc) → σ' ⊨ vc.implies P'.to_vc Q'.to_vc), from ( assume σ': env, assume P_valid: σ' ⊨ P.to_vc, show σ' ⊨ vc.implies (↑(f ≡ vf) ⋀ prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))).to_vc Q'.to_vc, from vc.implies.left_elim ( assume : σ' ⊨ prop.to_vc (f ≡ vf), have f_is_vf: σ' f = vf, from valid_env.subst_of_eq this, have (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' P_valid, have (∀y, y ∈ (σ[f↦vf]) → ((σ[f↦vf]) y = σ' y)), from env.equiv_of_rest_and_same this f_not_in_σ f_is_vf, show σ' ⊨ vc.implies (prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))).to_vc (prop.func f x R' (Q₁ (term.app f x) ⋀ S')).to_vc, from vc.implies.equiv_subst this ) ), have h2: (∀σ', (σ' ⊨ P.to_vc) → σ' ⊨ vc.implies P'.to_vc Q'.to_vc), from ( assume σ': env, assume P_valid: σ' ⊨ P.to_vc, show σ' ⊨ vc.implies (↑(f ≡ vf) ⋀ prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))).to_vc Q'.to_vc, from vc.implies.left_elim ( assume : σ' ⊨ prop.to_vc (f ≡ vf), have f_is_vf: σ' f = vf, from valid_env.subst_of_eq this, have (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' P_valid, have (∀y, y ∈ (σ[f↦vf]) → ((σ[f↦vf]) y = σ' y)), from env.equiv_of_rest_and_same this f_not_in_σ f_is_vf, show σ' ⊨ vc.implies (prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))).to_vc (prop.func f x R' (Q₁ (term.app f x) ⋀ S')).to_vc, from vc.implies.equiv_subst this ) ), have h3a: FV P' = set.insert f ∅, from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV P', have z ∈ FV (↑(f ≡ vf) ⋀ prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))), from this, or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (f ≡ vf), have free_in_term z (f ≡ vf), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, show z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this ) ( assume : free_in_term z vf, show z ∈ set.insert f ∅, from absurd this free_in_term.value.inv ) ) ( assume h: z ∈ FV (prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))), have closed (prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))), from prop_func_closed σ'_verified, show z ∈ set.insert f ∅, from absurd h (this z) ) ) ( assume z: var, assume : z ∈ set.insert f ∅, have z = f, from (set.mem_singleton_iff z f).mp this, have free_in_term z f, from this ▸ free_in_term.var z, have free_in_term z (f ≡ vf), from free_in_term.binop₁ this, have free_in_prop z (f ≡ vf), from free_in_prop.term this, show z ∈ FV (↑(f ≡ vf) ⋀ prop.subst_env (σ[f↦vf]) (prop.func f x R' (Q₁ (term.app f x) ⋀ S'))), from free_in_prop.and₁ this ), have h3b: f ∈ FV Q', from ( have free_in_term f f, from free_in_term.var f, have free_in_term f (term.unop unop.isFunc f), from free_in_term.unop this, have free_in_prop f (term.unop unop.isFunc f), from free_in_prop.term this, show f ∈ FV Q', from free_in_prop.and₁ this ), have h3c: FV Q' ⊆ FV P ∪ set.insert f ∅, from ( assume z: var, assume : z ∈ FV Q', have z ∈ FV (prop.func f x R' (Q₁ (term.app f x) ⋀ S')), from this, or.elim (free_in_prop.func.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume h3c1: (z ≠ x ∧ (free_in_prop z R' ∨ free_in_prop z (Q₁ (term.app f x) ⋀ S'))), have z_neq_x: z ≠ x, from h3c1.left, or.elim (h3c1.right) ( assume : free_in_prop z R', have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_R' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ) ( assume : z ∈ {f, x}, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume : z = x, show z ∈ FV P ∪ set.insert f ∅, from absurd this z_neq_x ) ) ) ( assume : free_in_prop z (Q₁ (term.app f x) ⋀ S'), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (Q₁ (term.app f x)), have z ∈ FV (term.app f x) ∨ z ∈ FV ((↑R ⋀ P) ⋀ (spec.func f x R' S') ⋀ R'), from exp.post_free e₁_verified (term.app f x) this, or.elim this ( assume : z ∈ FV (term.app f x), or.elim (free_in_term.app.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, show z ∈ FV P ∪ set.insert f ∅, from absurd this z_neq_x ) ) ( assume : z ∈ FV ((↑R ⋀ P) ⋀ (spec.func f x R' S') ⋀ R'), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ) ( assume : free_in_prop z (↑(spec.func f x R' S') ⋀ ↑R'), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (spec.func f x R' S'), have h: free_in_prop z (spec.func f x R' S').to_prop, from this, have (spec.func f x R' S').to_prop = (prop.func f x R'.to_prop S'.to_prop), by unfold spec.to_prop, have free_in_prop z (prop.func f x R' S'), from this ▸ h, or.elim (free_in_prop.func.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume h3c1: (z ≠ x ∧ (free_in_prop z R' ∨ free_in_prop z S')), or.elim (h3c1.right) ( assume : free_in_prop z R', have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_R' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ) ( assume : z ∈ {f, x}, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume : z = x, show z ∈ FV P ∪ set.insert f ∅, from absurd this z_neq_x ) ) ) ( assume : free_in_prop z S', have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_S' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ) ( assume : z ∈ {f, x}, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume : z = x, show z ∈ FV P ∪ set.insert f ∅, from absurd this z_neq_x ) ) ) ) ) ( assume : free_in_prop z R', have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_R' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ) ( assume : z ∈ {f, x}, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume : z = x, show z ∈ FV P ∪ set.insert f ∅, from absurd this z_neq_x ) ) ) ) ) ) ( assume : free_in_prop z S', have z ∈ FV (prop.and ↑R P) ∪ {f, x}, from set.mem_of_subset_of_mem fv_S' this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ( assume : z ∈ FV (P), show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_left (set.insert f ∅) this ) ) ( assume : z ∈ {f, x}, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ set.insert f ∅, from (set.mem_singleton_iff z f).mpr this, show z ∈ FV P ∪ set.insert f ∅, from set.mem_union_right (FV P) this ) ( assume : z = x, show z ∈ FV P ∪ set.insert f ∅, from absurd this z_neq_x ) ) ) ) ) ), have h4: (FV (↑R ⋀ P ⋀ P') = FV ((↑R ⋀ P) ⋀ Q')) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ P').to_vc ((↑R ⋀ P) ⋀ Q').to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ P') ⋀ Q₂) t).to_vc ((↑P ⋀ propctx.exis f (↑Q' ⋀ Q₂)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis f (↑Q' ⋀ Q₂)) v) ⊆ FV ((↑(P ⋀ P') ⋀ Q₂) v)), from @free_dominates_helper R P P' Q' Q₂ f h1 h2 h3a h3b h3c, have e'_verified': ↑R ⋀ P ⋀ P' ⊩ e' : Q₂, from strengthen_exp e₂_verified (↑R ⋀ P ⋀ P') h4.left h4.right.left, have h3: ⊩ₛ (R, σ[f↦value.func f x R' S' e₁ σ], e') : ↑(P ⋀ P') ⋀ Q₂, from stack.dvcgen.top σ'_verified fv_R'' R_valid' e'_verified', exists.intro (↑(P ⋀ P') ⋀ Q₂) ⟨h3, h4.right.right⟩ } }, case exp.dvcgen.unop op x y e' Q x_free_in_P y_not_free e'_verified vc_valid { cases e_steps, case dstep.unop vx vy x_is_vx vy_is_op { from have y_not_in_σ: y ∉ σ, from ( assume : y ∈ σ, have y ∈ σ.dom, from this, have y ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have y ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from y_not_free this ), have σ'_verified: ⊩ (σ[y↦vy]) : P ⋀ y ≡ vy, from ( or.elim (unop_result_not_function vy_is_op) ( assume vy_is_true: vy = value.true, have σ'_verified: ⊩ (σ[y↦value.true]) : P ⋀ y ≡ value.true, from env.dvcgen.tru y_not_in_σ σ_verified, show ⊩ (σ[y↦vy]) : P ⋀ y ≡ vy, from vy_is_true.symm ▸ σ'_verified ) ( assume vy_is_false: vy = value.false, have σ'_verified: ⊩ (σ[y↦value.false]) : P ⋀ y ≡ value.false, from env.dvcgen.fls y_not_in_σ σ_verified, show ⊩ (σ[y↦vy]) : P ⋀ y ≡ vy, from vy_is_false.symm ▸ σ'_verified ) ), have fv_R': FV R.to_prop ⊆ FV (P ⋀ y ≡ vy), from set.subset.trans fv_R free_in_prop.and_left_subset, have R_valid': σ[y↦vy] ⊨ R.to_prop.to_vc, from valid_with_additional_var R_valid, have h1: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (prop.term (y ≡ vy)).to_vc (prop.term (y ≡ term.unop op x)).to_vc), from ( assume σ': env, assume : σ' ⊨ P.to_vc, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, have h_impl: ((σ' ⊨ prop.to_vc (y ≡ vy)) → (σ' ⊨ prop.to_vc (y ≡ term.unop op x))), from ( assume : σ' ⊨ prop.to_vc (y ≡ vy), have y_is_vy: σ' y = some vy, from valid_env.subst_of_eq this, have y_subst: term.subst_env σ' y = vy, from (term.subst_env.var.right vy).mp y_is_vy, have σ' x = vx, from eq_value_of_equiv_subst env_equiv x_is_vx, have x_subst: term.subst_env σ' x = vx, from (term.subst_env.var.right vx).mp this, have unop.apply op vx = some vy, from vy_is_op, have ⊨ vy ≡ term.unop op vx, from valid.unop.mp this, have h2: ⊨ (term.subst_env σ' y) ≡ term.unop op (term.subst_env σ' x), from x_subst.symm ▸ y_subst.symm ▸ this, have term.subst_env σ' (term.unop op x) = term.unop op (term.subst_env σ' x), from term.subst_env.unop, have ⊨ term.subst_env σ' y ≡ term.subst_env σ' (term.unop op x), from this.symm ▸ h2, have h3: ⊨ term.binop binop.eq (term.subst_env σ' y) (term.subst_env σ' (term.unop op x)), from this, have term.subst_env σ' (term.binop binop.eq y (term.unop op x)) = term.binop binop.eq (term.subst_env σ' y) (term.subst_env σ' (term.unop op x)), from term.subst_env.binop, have h4: ⊨ term.subst_env σ' (term.binop binop.eq y (term.unop op x)), from this.symm ▸ h3, have vc.subst_env σ' (term.binop binop.eq y (term.unop op x)) = term.subst_env σ' (term.binop binop.eq y (term.unop op x)), from vc.subst_env.term, have ⊨ vc.subst_env σ' (term.binop binop.eq y (term.unop op x)), from this.symm ▸ h4, have h5: σ' ⊨ vc.term (y ≡ term.unop op x), from this, have (prop.term (y ≡ term.unop op x)).to_vc = vc.term (y ≡ term.unop op x), by unfold prop.to_vc, show σ' ⊨ (prop.term (y ≡ term.unop op x)).to_vc, from this.symm ▸ h5 ), valid_env.mpr h_impl ), have h2: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (prop.term (y ≡ vy)).to_vc (prop.term (y ≡ term.unop op x)).to_vc), from ( assume σ': env, assume : σ' ⊨ P.to_vc, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, have h_impl: ((σ' ⊨ prop.to_vc (y ≡ vy)) → (σ' ⊨ prop.to_vc (y ≡ term.unop op x))), from ( assume : σ' ⊨ prop.to_vc (y ≡ vy), have y_is_vy: σ' y = some vy, from valid_env.subst_of_eq this, have y_subst: term.subst_env σ' y = vy, from (term.subst_env.var.right vy).mp y_is_vy, have σ' x = vx, from eq_value_of_equiv_subst env_equiv x_is_vx, have x_subst: term.subst_env σ' x = vx, from (term.subst_env.var.right vx).mp this, have unop.apply op vx = some vy, from vy_is_op, have ⊨ vy ≡ term.unop op vx, from valid.unop.mp this, have h2: ⊨ (term.subst_env σ' y) ≡ term.unop op (term.subst_env σ' x), from x_subst.symm ▸ y_subst.symm ▸ this, have term.subst_env σ' (term.unop op x) = term.unop op (term.subst_env σ' x), from term.subst_env.unop, have ⊨ term.subst_env σ' y ≡ term.subst_env σ' (term.unop op x), from this.symm ▸ h2, have h3: ⊨ term.binop binop.eq (term.subst_env σ' y) (term.subst_env σ' (term.unop op x)), from this, have term.subst_env σ' (term.binop binop.eq y (term.unop op x)) = term.binop binop.eq (term.subst_env σ' y) (term.subst_env σ' (term.unop op x)), from term.subst_env.binop, have h4: ⊨ term.subst_env σ' (term.binop binop.eq y (term.unop op x)), from this.symm ▸ h3, have vc.subst_env σ' (term.binop binop.eq y (term.unop op x)) = term.subst_env σ' (term.binop binop.eq y (term.unop op x)), from vc.subst_env.term, have ⊨ vc.subst_env σ' (term.binop binop.eq y (term.unop op x)), from this.symm ▸ h4, have h5: σ' ⊨ vc.term (y ≡ term.unop op x), from this, have (prop.term (y ≡ term.unop op x)).to_vc = vc.term (y ≡ term.unop op x), by unfold prop.to_vc, show σ' ⊨ (prop.term (y ≡ term.unop op x)).to_vc, from this.symm ▸ h5 ), valid_env.mpr h_impl ), have h3a: FV (prop.term (y ≡ vy)) = set.insert y ∅, from set.eq_of_subset_of_subset ( assume z: var, assume : free_in_prop z (y ≡ vy), have free_in_term z (y ≡ vy), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z y, have z = y, from free_in_term.var.inv this, show z ∈ set.insert y ∅, from (set.mem_singleton_iff z y).mpr this ) ( assume : free_in_term z vy, show z ∈ set.insert y ∅, from absurd this free_in_term.value.inv ) ) ( assume z: var, assume : z ∈ set.insert y ∅, have z = y, from (set.mem_singleton_iff z y).mp this, have free_in_term z y, from this ▸ free_in_term.var z, have free_in_term z (y ≡ vy), from free_in_term.binop₁ this, show free_in_prop z (y ≡ vy), from free_in_prop.term this ), have h3b: y ∈ FV (prop.term (y ≡ term.unop op x)), from ( have free_in_term y y, from free_in_term.var y, have free_in_term y (y ≡ term.unop op x), from free_in_term.binop₁ this, show free_in_prop y (y ≡ term.unop op x), from free_in_prop.term this ), have h3c: FV (prop.term (y ≡ term.unop op x)) ⊆ FV P ∪ set.insert y ∅, from ( assume z: var, assume : z ∈ FV (prop.term (y ≡ term.unop op x)), have free_in_term z (y ≡ term.unop op x), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z y, have z = y, from free_in_term.var.inv this, have z ∈ set.insert y ∅, from (set.mem_singleton_iff z y).mpr this, show z ∈ FV P ∪ set.insert y ∅, from set.mem_union_right (FV P) this ) ( assume : free_in_term z (term.unop op x), have free_in_term z x, from free_in_term.unop.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV P ∪ set.insert y ∅, from set.mem_union_left (set.insert y ∅) this ) ( assume : z ∈ FV P, show z ∈ FV P ∪ set.insert y ∅, from set.mem_union_left (set.insert y ∅) this ) ) ), have h4: (FV (↑R ⋀ P ⋀ (y ≡ vy)) = FV ((↑R ⋀ P) ⋀ (y ≡ term.unop op x))) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ (y ≡ vy)).to_vc ((↑R ⋀ P) ⋀ (y ≡ term.unop op x)).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ (y ≡ vy)) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis y (↑(y ≡ term.unop op x) ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis y (↑(y ≡ term.unop op x) ⋀ Q)) v) ⊆ FV ((↑(P ⋀ (y ≡ vy)) ⋀ Q) v)), from @free_dominates_helper R P (y ≡ vy) (y ≡ term.unop op x) Q y h1 h2 h3a h3b h3c, have e'_verified': ↑R ⋀ P ⋀ y ≡ vy ⊩ e' : Q, from strengthen_exp e'_verified (↑R ⋀ P ⋀ y ≡ vy) h4.left h4.right.left, have h3: ⊩ₛ (R, σ[y↦vy], e') : ↑(P ⋀ y ≡ vy) ⋀ Q, from stack.dvcgen.top σ'_verified fv_R' R_valid' e'_verified', exists.intro (↑(P ⋀ y ≡ vy) ⋀ Q) ⟨h3, h4.right.right⟩ } }, case exp.dvcgen.binop op x y z e' Q x_free_in_P y_free_in_P z_not_free e'_verified vc_valid { cases e_steps, case dstep.binop vx vy vz x_is_vx y_is_vy vz_is_op { from have z_not_in_σ: z ∉ σ, from ( assume : z ∈ σ, have z ∈ σ.dom, from this, have z ∈ FV P, from (free_iff_contains σ_verified) ▸ this, have z ∈ FV (↑R ⋀ P), from free_in_prop.and₂ this, show «false», from z_not_free this ), have σ'_verified: ⊩ (σ[z↦vz]) : P ⋀ z ≡ vz, from ( or.elim (binop_result_not_function vz_is_op) ( assume vz_is_true: vz = value.true, have σ'_verified: ⊩ (σ[z↦value.true]) : P ⋀ z ≡ value.true, from env.dvcgen.tru z_not_in_σ σ_verified, show ⊩ (σ[z↦vz]) : P ⋀ z ≡ vz, from vz_is_true.symm ▸ σ'_verified ) ( assume : (vz = value.false) ∨ (∃n, vz = value.num n), or.elim this ( assume vz_is_false: vz = value.false, have σ'_verified: ⊩ (σ[z↦value.false]) : P ⋀ z ≡ value.false, from env.dvcgen.fls z_not_in_σ σ_verified, show ⊩ (σ[z↦vz]) : P ⋀ z ≡ vz, from vz_is_false.symm ▸ σ'_verified ) ( assume : ∃n, vz = value.num n, let ⟨n, vz_is_num⟩ := this in have σ'_verified: ⊩ (σ[z↦value.num n]) : P ⋀ z ≡ value.num n, from env.dvcgen.num z_not_in_σ σ_verified, show ⊩ (σ[z↦vz]) : P ⋀ z ≡ vz, from vz_is_num.symm ▸ σ'_verified ) ) ), have fv_R': FV R.to_prop ⊆ FV (P ⋀ z ≡ vz), from set.subset.trans fv_R free_in_prop.and_left_subset, have R_valid': σ[z↦vz] ⊨ R.to_prop.to_vc, from valid_with_additional_var R_valid, have h1: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (prop.term (z ≡ vz)).to_vc (prop.term (z ≡ term.binop op x y)).to_vc), from ( assume σ': env, assume : σ' ⊨ P.to_vc, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, have h_impl: ((σ' ⊨ prop.to_vc (z ≡ vz)) → (σ' ⊨ prop.to_vc (z ≡ term.binop op x y))), from ( assume : σ' ⊨ prop.to_vc (z ≡ vz), have z_is_vz: σ' z = some vz, from valid_env.subst_of_eq this, have z_subst: term.subst_env σ' z = vz, from (term.subst_env.var.right vz).mp z_is_vz, have σ' x = vx, from eq_value_of_equiv_subst env_equiv x_is_vx, have x_subst: term.subst_env σ' x = vx, from (term.subst_env.var.right vx).mp this, have σ' y = vy, from eq_value_of_equiv_subst env_equiv y_is_vy, have y_subst: term.subst_env σ' y = vy, from (term.subst_env.var.right vy).mp this, have binop.apply op vx vy = some vz, from vz_is_op, have ⊨ vz ≡ term.binop op vx vy, from valid.binop.mp this, have h2: ⊨ (term.subst_env σ' z) ≡ term.binop op (term.subst_env σ' x) (term.subst_env σ' y), from x_subst.symm ▸ y_subst.symm ▸ z_subst.symm ▸ this, have term.subst_env σ' (term.binop op x y) = term.binop op (term.subst_env σ' x) (term.subst_env σ' y), from term.subst_env.binop, have ⊨ term.subst_env σ' z ≡ term.subst_env σ' (term.binop op x y), from this.symm ▸ h2, have h3: ⊨ term.binop binop.eq (term.subst_env σ' z) (term.subst_env σ' (term.binop op x y)), from this, have term.subst_env σ' (term.binop binop.eq z (term.binop op x y)) = term.binop binop.eq (term.subst_env σ' z) (term.subst_env σ' (term.binop op x y)), from term.subst_env.binop, have h4: ⊨ term.subst_env σ' (term.binop binop.eq z (term.binop op x y)), from this.symm ▸ h3, have vc.subst_env σ' (term.binop binop.eq z (term.binop op x y)) = term.subst_env σ' (term.binop binop.eq z (term.binop op x y)), from vc.subst_env.term, have ⊨ vc.subst_env σ' (term.binop binop.eq z (term.binop op x y)), from this.symm ▸ h4, have h5: σ' ⊨ vc.term (z ≡ term.binop op x y), from this, have (prop.term (z ≡ term.binop op x y)).to_vc = vc.term (z ≡ term.binop op x y), by unfold prop.to_vc, show σ' ⊨ (prop.term (z ≡ term.binop op x y)).to_vc, from this.symm ▸ h5 ), valid_env.mpr h_impl ), have h2: (∀σ, (σ ⊨ P.to_vc) → σ ⊨ vc.implies (prop.term (z ≡ vz)).to_vc (prop.term (z ≡ term.binop op x y)).to_vc), from ( assume σ': env, assume : σ' ⊨ P.to_vc, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, have h_impl: ((σ' ⊨ prop.to_vc (z ≡ vz)) → (σ' ⊨ prop.to_vc (z ≡ term.binop op x y))), from ( assume : σ' ⊨ prop.to_vc (z ≡ vz), have z_is_vz: σ' z = some vz, from valid_env.subst_of_eq this, have z_subst: term.subst_env σ' z = vz, from (term.subst_env.var.right vz).mp z_is_vz, have σ' x = vx, from eq_value_of_equiv_subst env_equiv x_is_vx, have x_subst: term.subst_env σ' x = vx, from (term.subst_env.var.right vx).mp this, have σ' y = vy, from eq_value_of_equiv_subst env_equiv y_is_vy, have y_subst: term.subst_env σ' y = vy, from (term.subst_env.var.right vy).mp this, have binop.apply op vx vy = some vz, from vz_is_op, have ⊨ vz ≡ term.binop op vx vy, from valid.binop.mp this, have h2: ⊨ (term.subst_env σ' z) ≡ term.binop op (term.subst_env σ' x) (term.subst_env σ' y), from x_subst.symm ▸ y_subst.symm ▸ z_subst.symm ▸ this, have term.subst_env σ' (term.binop op x y) = term.binop op (term.subst_env σ' x) (term.subst_env σ' y), from term.subst_env.binop, have ⊨ term.subst_env σ' z ≡ term.subst_env σ' (term.binop op x y), from this.symm ▸ h2, have h3: ⊨ term.binop binop.eq (term.subst_env σ' z) (term.subst_env σ' (term.binop op x y)), from this, have term.subst_env σ' (term.binop binop.eq z (term.binop op x y)) = term.binop binop.eq (term.subst_env σ' z) (term.subst_env σ' (term.binop op x y)), from term.subst_env.binop, have h4: ⊨ term.subst_env σ' (term.binop binop.eq z (term.binop op x y)), from this.symm ▸ h3, have vc.subst_env σ' (term.binop binop.eq z (term.binop op x y)) = term.subst_env σ' (term.binop binop.eq z (term.binop op x y)), from vc.subst_env.term, have ⊨ vc.subst_env σ' (term.binop binop.eq z (term.binop op x y)), from this.symm ▸ h4, have h5: σ' ⊨ vc.term (z ≡ term.binop op x y), from this, have ((prop.term (z ≡ term.binop op x y)).to_vc = vc.term (z ≡ term.binop op x y)), by unfold prop.to_vc, show (σ' ⊨ (prop.term (z ≡ term.binop op x y)).to_vc), from this.symm ▸ h5 ), valid_env.mpr h_impl ), have h3a: FV (prop.term (z ≡ vz)) = set.insert z ∅, from set.eq_of_subset_of_subset ( assume x: var, assume : free_in_prop x (z ≡ vz), have free_in_term x (z ≡ vz), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term x z, have x = z, from free_in_term.var.inv this, show x ∈ set.insert z ∅, from (set.mem_singleton_iff x z).mpr this ) ( assume : free_in_term x vz, show x ∈ set.insert z ∅, from absurd this free_in_term.value.inv ) ) ( assume x: var, assume : x ∈ set.insert z ∅, have x = z, from (set.mem_singleton_iff x z).mp this, have free_in_term x z, from this ▸ free_in_term.var x, have free_in_term x (z ≡ vz), from free_in_term.binop₁ this, show free_in_prop x (z ≡ vz), from free_in_prop.term this ), have h3b: z ∈ FV (prop.term (z ≡ term.binop op x y)), from ( have free_in_term z z, from free_in_term.var z, have free_in_term z (z ≡ term.binop op x y), from free_in_term.binop₁ this, show free_in_prop z (z ≡ term.binop op x y), from free_in_prop.term this ), have h3c: FV (prop.term (z ≡ term.binop op x y)) ⊆ FV P ∪ set.insert z ∅, from ( assume a: var, assume : a ∈ FV (prop.term (z ≡ term.binop op x y)), have free_in_term a (z ≡ term.binop op x y), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term a z, have a = z, from free_in_term.var.inv this, have a ∈ set.insert z ∅, from (set.mem_singleton_iff a z).mpr this, show a ∈ FV P ∪ set.insert z ∅, from set.mem_union_right (FV P) this ) ( assume : free_in_term a (term.binop op x y), or.elim (free_in_term.binop.inv this) ( assume : free_in_term a x, have a = x, from free_in_term.var.inv this, have a ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : a ∈ FV ↑R, have a ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show a ∈ FV P ∪ set.insert z ∅, from set.mem_union_left (set.insert z ∅) this ) ( assume : a ∈ FV P, show a ∈ FV P ∪ set.insert z ∅, from set.mem_union_left (set.insert z ∅) this ) ) ( assume : free_in_term a y, have a = y, from free_in_term.var.inv this, have a ∈ FV (↑R ⋀ P), from this.symm ▸ y_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : a ∈ FV ↑R, have a ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show a ∈ FV P ∪ set.insert z ∅, from set.mem_union_left (set.insert z ∅) this ) ( assume : a ∈ FV P, show a ∈ FV P ∪ set.insert z ∅, from set.mem_union_left (set.insert z ∅) this ) ) ) ), have h4: (FV (↑R ⋀ P ⋀ (z ≡ vz)) = FV ((↑R ⋀ P) ⋀ (z ≡ term.binop op x y))) ∧ (∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ (z ≡ vz)).to_vc ((↑R ⋀ P) ⋀ (z ≡ term.binop op x y)).to_vc) ∧ (∀σ t, σ ⊨ vc.implies ((↑(P ⋀ (z ≡ vz)) ⋀ Q) t).to_vc ((↑P ⋀ propctx.exis z (↑(z ≡ term.binop op x y) ⋀ Q)) t).to_vc) ∧ (∀v: value, FV ((↑P ⋀ propctx.exis z (↑(z ≡ term.binop op x y) ⋀ Q)) v) ⊆ FV ((↑(P ⋀ (z ≡ vz)) ⋀ Q) v)), from @free_dominates_helper R P (z ≡ vz) (z ≡ term.binop op x y) Q z h1 h2 h3a h3b h3c, have e'_verified': ↑R ⋀ P ⋀ z ≡ vz ⊩ e' : Q, from strengthen_exp e'_verified (↑R ⋀ P ⋀ z ≡ vz) h4.left h4.right.left, have h3: ⊩ₛ (R, σ[z↦vz], e') : ↑(P ⋀ z ≡ vz) ⋀ Q, from stack.dvcgen.top σ'_verified fv_R' R_valid' e'_verified', exists.intro (↑(P ⋀ z ≡ vz) ⋀ Q) ⟨h3, h4.right.right⟩ } }, case exp.dvcgen.app y f x e' Q' f_free_in_P x_free_in_P _ e'_verified vc_valid { cases e_steps }, case exp.dvcgen.ite x e₂ e₁ Q₁ Q₂ x_free_in_P e₁_verified e₂_verified vc_valid { cases e_steps, case dstep.ite_true x_is_true { from have h1: FV (↑R ⋀ P) = FV ((↑R ⋀ P) ⋀ x), from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV (↑R ⋀ P), show z ∈ FV ((↑R ⋀ P) ⋀ x), from free_in_prop.and₁ this ) ( assume z: var, assume : z ∈ FV ((↑R ⋀ P) ⋀ x), or.elim (free_in_prop.and.inv this) id ( assume : free_in_prop z x, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, show z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P ) ), have h2: ∀σ', σ' ⊨ vc.implies (↑R ⋀ P).to_vc ((↑R ⋀ P) ⋀ x).to_vc, from λσ', vc.implies.and_right_intro ( assume : σ' ⊨ (↑R ⋀ P).to_vc, have σ' ⊨ P.to_vc, from (valid_env.to_vc_and.elim this).right, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, show σ' ⊨ (prop.term x).to_vc, from ( have σ' x = some value.true, from eq_value_of_equiv_subst env_equiv x_is_true, have x_subst: term.subst_env σ' x = value.true, from (term.subst_env.var.right value.true).mp this, have ⊨ value.true, from valid.tru, have h7: ⊨ term.subst_env σ' x, from x_subst.symm ▸ this, have vc.subst_env σ' x = term.subst_env σ' x, from vc.subst_env.term, have ⊨ vc.subst_env σ' x, from this.symm ▸ h7, have h8: σ' ⊨ vc.term x, from this, have (prop.term x).to_vc = vc.term x, by unfold prop.to_vc, show σ' ⊨ (prop.term x).to_vc, from this.symm ▸ h8 ) ), have e'_verified: ↑R ⋀ P ⊩ e' : Q₁, from strengthen_exp e₁_verified (↑R ⋀ P) h1 h2, have h3: ⊩ₛ (R, σ, e') : P ⋀ Q₁, from stack.dvcgen.top σ_verified fv_R R_valid e'_verified, have hb1: ∀t, ((↑P ⋀ Q₁) t) = (P ⋀ Q₁ t), from λt, propctx_apply_pq, have hb2: ∀t, ((↑P ⋀ (propctx.implies ↑x Q₁) ⋀ (propctx.implies ↑(prop.not x) Q₂)) t) = (P ⋀ (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t), from λt, propctx_apply_pq, have hb5: ∀t, (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t = (prop.implies ↑x (Q₁ t) ⋀ prop.implies (prop.not x) (Q₂ t)), from ( assume t: term, have hb3: (prop.term x).to_propctx t = (prop.term x), from unchanged_of_apply_propctx_without_hole, have hb4: (prop.not x).to_propctx t = (prop.not x), from unchanged_of_apply_propctx_without_hole, show (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t = (prop.implies ↑x (Q₁ t) ⋀ prop.implies (prop.not x) (Q₂ t)), by calc (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t = propctx.apply (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t : rfl ... = (propctx.apply (propctx.implies ↑x Q₁) t ⋀ propctx.apply (propctx.implies ↑(prop.not x) Q₂) t) : by unfold propctx.apply ... = (propctx.apply (propctx.or (propctx.not ↑x) Q₁) t ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : rfl ... = (((propctx.apply (propctx.not ↑x) t) ⋁ (propctx.apply Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : by unfold propctx.apply ... = (((prop.not (propctx.apply ↑x t)) ⋁ (propctx.apply Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : by unfold propctx.apply ... = (((prop.not ((prop.term x).to_propctx t)) ⋁ (Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : rfl ... = ((prop.not (prop.term x) ⋁ (Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : by rw[hb3] ... = ((prop.implies x (Q₁ t)) ⋀ propctx.apply (propctx.or (propctx.not ↑(prop.not x)) Q₂) t) : rfl ... = ((prop.implies x (Q₁ t)) ⋀ (propctx.apply (propctx.not ↑(prop.not x)) t ⋁ propctx.apply Q₂ t)) : by unfold propctx.apply ... = ((prop.implies x (Q₁ t)) ⋀ (prop.not (propctx.apply ↑(prop.not x) t) ⋁ propctx.apply Q₂ t)) : by unfold propctx.apply ... = ((prop.implies x (Q₁ t)) ⋀ (prop.not ((prop.not x).to_propctx t) ⋁ propctx.apply Q₂ t)) : rfl ... = ((prop.implies x (Q₁ t)) ⋀ (prop.not (prop.not x) ⋁ propctx.apply Q₂ t)) : by rw[hb4] ... = ((prop.implies x (Q₁ t)) ⋀ (prop.implies (prop.not x) (Q₂ t))) : rfl ), have h4: ∀σ' t, σ' ⊨ vc.implies ((↑P ⋀ Q₁) t).to_vc ((↑P ⋀ (propctx.implies ↑x Q₁) ⋀ (propctx.implies ↑(prop.not x) Q₂)) t).to_vc, from ( assume σ': env, assume t: term, have h5: σ' ⊨ vc.implies (P ⋀ Q₁ t).to_vc (P ⋀ prop.implies x (Q₁ t) ⋀ prop.implies (prop.not x) (Q₂ t)).to_vc, from vc.implies.same_left begin assume : σ' ⊨ P.to_vc, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, have h6: (σ' x = some value.true), from eq_value_of_equiv_subst env_equiv x_is_true, have x_subst: (term.subst_env σ' x = value.true), from (term.subst_env.var.right value.true).mp h6, apply valid_env.mpr, assume h7, apply valid_env.to_vc_and, unfold prop.implies, unfold prop.to_vc, apply valid_env.or₂, from h7, unfold prop.implies, unfold prop.to_vc, apply valid_env.or₁, apply valid_env.not_not.mpr, change (σ'⊨prop.to_vc (prop.term (term.var x))), unfold prop.to_vc, change (⊨ vc.subst_env σ' (term.var x)), rw[vc.subst_env.term], change (⊨vc.term (term.subst_env σ' x)), rw[x_subst], from valid.tru end, (hb1 t).symm ▸ (hb2 t).symm ▸ (hb5 t).symm ▸ h5 ), have h5: ∀v: value, FV ((↑P ⋀ propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) v) ⊆ FV ((↑P ⋀ Q₁) v), from ( assume v: value, have h6: FV (P ⋀ prop.implies x (Q₁ v) ⋀ prop.implies (prop.not x) (Q₂ v)) ⊆ FV (P ⋀ Q₁ v), from ( assume z: var, assume : z ∈ FV (P ⋀ prop.implies x (Q₁ v) ⋀ prop.implies (prop.not x) (Q₂ v)), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV (prop.implies x (Q₁ v) ⋀ prop.implies (prop.not x) (Q₂ v)), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (prop.implies x (Q₁ v)), or.elim (free_in_prop.implies.inv this) ( assume : free_in_prop z x, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ) ( assume : z ∈ FV (Q₁ v), show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₂ this ) ) ( assume : z ∈ FV (prop.implies (prop.not x) (Q₂ v)), or.elim (free_in_prop.implies.inv this) ( assume : z ∈ FV (prop.not x), have free_in_prop z x, from free_in_prop.not.inv this, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ) ( assume : z ∈ FV (Q₂ v), or.elim (exp.post_free e₂_verified v this) ( assume : z ∈ FV (term.value v), show z ∈ FV (P ⋀ Q₁ v), from absurd this free_in_term.value.inv ) ( assume : z ∈ FV ((↑R ⋀ P) ⋀ prop.not x), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ) ( assume : z ∈ FV (prop.not x), have free_in_prop z x, from free_in_prop.not.inv this, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₁ v), from free_in_prop.and₁ this ) ) ) ) ) ) ), (hb1 v).symm ▸ (hb2 v).symm ▸ (hb5 v).symm ▸ h6 ), exists.intro (↑P ⋀ Q₁) ⟨h3, ⟨h4, h5⟩⟩ }, case dstep.ite_false x_is_false { from have h1: FV (↑R ⋀ P) = FV ((↑R ⋀ P) ⋀ prop.not x), from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV (↑R ⋀ P), show z ∈ FV ((↑R ⋀ P) ⋀ prop.not x), from free_in_prop.and₁ this ) ( assume z: var, assume : z ∈ FV ((↑R ⋀ P) ⋀ prop.not x), or.elim (free_in_prop.and.inv this) id ( assume : free_in_prop z (prop.not x), have free_in_prop z x, from free_in_prop.not.inv this, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, show z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P ) ), have h2: ∀σ', σ' ⊨ vc.implies (↑R ⋀ P).to_vc ((↑R ⋀ P) ⋀ prop.not x).to_vc, from λσ', vc.implies.and_right_intro ( assume : σ' ⊨ (↑R ⋀ P).to_vc, have σ' ⊨ P.to_vc, from (valid_env.to_vc_and.elim this).right, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, show σ' ⊨ (prop.not x).to_vc, from ( have σ' x = some value.false, from eq_value_of_equiv_subst env_equiv x_is_false, have x_subst: term.subst_env σ' x = value.false, from (term.subst_env.var.right value.false).mp this, have ⊨ vc.not value.false, from valid.not_false, have h7: ⊨ vc.not (term.subst_env σ' x), from x_subst.symm ▸ this, have vc.subst_env σ' x = term.subst_env σ' x, from vc.subst_env.term, have h8: ⊨ vc.not (vc.subst_env σ' x), from this.symm ▸ h7, have vc.subst_env σ' (vc.not x) = vc.not (vc.subst_env σ' x), from vc.subst_env.not, have ⊨ vc.subst_env σ' (vc.not x), from this.symm ▸ h8, have h9: σ' ⊨ vc.not x, from this, have (prop.not x).to_vc = vc.not x, by begin unfold prop.to_vc, change (vc.not (prop.to_vc (prop.term x)) = vc.not ↑x), unfold prop.to_vc, congr end, show σ' ⊨ (prop.not x).to_vc, from this.symm ▸ h9 ) ), have e'_verified: ↑R ⋀ P ⊩ e' : Q₂, from strengthen_exp e₂_verified (↑R ⋀ P) h1 h2, have h3: ⊩ₛ (R, σ, e') : P ⋀ Q₂, from stack.dvcgen.top σ_verified fv_R R_valid e'_verified, have hb1: ∀t, ((↑P ⋀ Q₂) t) = (P ⋀ Q₂ t), from λt, propctx_apply_pq, have hb2: ∀t, ((↑P ⋀ (propctx.implies ↑x Q₁) ⋀ (propctx.implies ↑(prop.not x) Q₂)) t) = (P ⋀ (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t), from λt, propctx_apply_pq, have hb5: ∀t, (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t = (prop.implies ↑x (Q₁ t) ⋀ prop.implies (prop.not x) (Q₂ t)), from ( assume t: term, have hb3: (prop.term x).to_propctx t = (prop.term x), from unchanged_of_apply_propctx_without_hole, have hb4: (prop.not x).to_propctx t = (prop.not x), from unchanged_of_apply_propctx_without_hole, show (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t = (prop.implies ↑x (Q₁ t) ⋀ prop.implies (prop.not x) (Q₂ t)), by calc (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t = propctx.apply (propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) t : rfl ... = (propctx.apply (propctx.implies ↑x Q₁) t ⋀ propctx.apply (propctx.implies ↑(prop.not x) Q₂) t) : by unfold propctx.apply ... = (propctx.apply (propctx.or (propctx.not ↑x) Q₁) t ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : rfl ... = (((propctx.apply (propctx.not ↑x) t) ⋁ (propctx.apply Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : by unfold propctx.apply ... = (((prop.not (propctx.apply ↑x t)) ⋁ (propctx.apply Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : by unfold propctx.apply ... = (((prop.not ((prop.term x).to_propctx t)) ⋁ (Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : rfl ... = ((prop.not (prop.term x) ⋁ (Q₁ t)) ⋀ (propctx.implies ↑(prop.not x) Q₂) t) : by rw[hb3] ... = ((prop.implies x (Q₁ t)) ⋀ propctx.apply (propctx.or (propctx.not ↑(prop.not x)) Q₂) t) : rfl ... = ((prop.implies x (Q₁ t)) ⋀ (propctx.apply (propctx.not ↑(prop.not x)) t ⋁ propctx.apply Q₂ t)) : by unfold propctx.apply ... = ((prop.implies x (Q₁ t)) ⋀ (prop.not (propctx.apply ↑(prop.not x) t) ⋁ propctx.apply Q₂ t)) : by unfold propctx.apply ... = ((prop.implies x (Q₁ t)) ⋀ (prop.not ((prop.not x).to_propctx t) ⋁ propctx.apply Q₂ t)) : rfl ... = ((prop.implies x (Q₁ t)) ⋀ (prop.not (prop.not x) ⋁ propctx.apply Q₂ t)) : by rw[hb4] ... = ((prop.implies x (Q₁ t)) ⋀ (prop.implies (prop.not x) (Q₂ t))) : rfl ), have h4: ∀σ' t, σ' ⊨ vc.implies ((↑P ⋀ Q₂) t).to_vc ((↑P ⋀ (propctx.implies ↑x Q₁) ⋀ (propctx.implies ↑(prop.not x) Q₂)) t).to_vc, from ( assume σ': env, assume t: term, have h5: σ' ⊨ vc.implies (P ⋀ Q₂ t).to_vc (P ⋀ prop.implies x (Q₁ t) ⋀ prop.implies (prop.not x) (Q₂ t)).to_vc, from vc.implies.same_left begin assume : σ' ⊨ P.to_vc, have env_equiv: (∀y, y ∈ σ → (σ y = σ' y)), from env_equiv_of_translation_valid σ_verified σ' this, have h6: (σ' x = some value.false), from eq_value_of_equiv_subst env_equiv x_is_false, have x_subst: (term.subst_env σ' x = value.false), from (term.subst_env.var.right value.false).mp h6, apply valid_env.mpr, assume h7, apply valid_env.to_vc_and, unfold prop.implies, unfold prop.to_vc, apply valid_env.or₁, change (σ' ⊨ prop.to_vc (prop.not (term.var x))), unfold prop.to_vc, change (σ' ⊨ vc.not (prop.to_vc (prop.term (term.var x)))), unfold prop.to_vc, change (⊨ vc.subst_env σ' (vc.not (term.var x))), rw[vc.subst_env.not], rw[vc.subst_env.term], change (⊨ vc.not (vc.term (term.subst_env σ' x))), rw[x_subst], from valid.not_false, unfold prop.implies, unfold prop.to_vc, apply valid_env.or₂, from h7 end, (hb1 t).symm ▸ (hb2 t).symm ▸ (hb5 t).symm ▸ h5 ), have h5: ∀v: value, FV ((↑P ⋀ propctx.and (propctx.implies ↑x Q₁) (propctx.implies ↑(prop.not x) Q₂)) v) ⊆ FV ((↑P ⋀ Q₂) v), from ( assume v: value, have h6: FV (P ⋀ prop.implies x (Q₁ v) ⋀ prop.implies (prop.not x) (Q₂ v)) ⊆ FV (P ⋀ Q₂ v), from ( assume z: var, assume : z ∈ FV (P ⋀ prop.implies x (Q₁ v) ⋀ prop.implies (prop.not x) (Q₂ v)), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV (prop.implies x (Q₁ v) ⋀ prop.implies (prop.not x) (Q₂ v)), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (prop.implies x (Q₁ v)), or.elim (free_in_prop.implies.inv this) ( assume : free_in_prop z x, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ) ( assume : z ∈ FV (Q₁ v), or.elim (exp.post_free e₁_verified v this) ( assume : z ∈ FV (term.value v), show z ∈ FV (P ⋀ Q₂ v), from absurd this free_in_term.value.inv ) ( assume : z ∈ FV ((↑R ⋀ P) ⋀ x), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (↑R ⋀ P), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ) ( assume : z ∈ FV (prop.term x), have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ) ) ) ) ( assume : z ∈ FV (prop.implies (prop.not x) (Q₂ v)), or.elim (free_in_prop.implies.inv this) ( assume : z ∈ FV (prop.not x), have free_in_prop z x, from free_in_prop.not.inv this, have free_in_term z x, from free_in_prop.term.inv this, have z = x, from free_in_term.var.inv this, have z ∈ FV (↑R ⋀ P), from this.symm ▸ x_free_in_P, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV ↑R, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ( assume : z ∈ FV P, show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₁ this ) ) ( assume : z ∈ FV (Q₂ v), show z ∈ FV (P ⋀ Q₂ v), from free_in_prop.and₂ this ) ) ) ), (hb1 v).symm ▸ (hb2 v).symm ▸ (hb5 v).symm ▸ h6 ), exists.intro (↑P ⋀ Q₂) ⟨h3, ⟨h4, h5⟩⟩ } }, case exp.dvcgen.return x x_free_in_P { cases e_steps } end lemma inlined_dominates_spec {σ σ₁: env} {P: prop} {Q: propctx} {f x: var} {R S: spec} {e: exp}: (⊩ σ₁ : P) → (f ∉ σ₁) → (x ∉ σ₁) → (x ≠ f) → (σ ⊨ P.to_vc) → (σ f = value.func f x R S e σ₁) → (⊩ (σ₁[f↦value.func f x R S e σ₁]) : (P ⋀ f ≡ value.func f x R S e σ₁ ⋀ prop.subst_env (σ₁[f↦value.func f x R S e σ₁]) (prop.func f x R (Q (term.app f x) ⋀ S)))) → (σ ⊨ vc.implies (prop.subst_env (σ₁[f↦value.func f x R S e σ₁]) (prop.func f x R (Q (term.app f x) ⋀ S))).to_vc (spec.func f x R S).to_prop.to_vc) := let vf := value.func f x R S e σ₁ in let forallp' := (prop.implies R.to_prop (prop.pre f x) ⋀ prop.implies (prop.post f x) (Q (term.app f x) ⋀ S.to_prop)) in let forallp := (prop.implies R.to_prop (prop.pre f x) ⋀ prop.implies (prop.post f x) S.to_prop) in let P' := P ⋀ f ≡ value.func f x R S e σ₁ ⋀ prop.subst_env (σ₁[f↦vf]) (prop.func f x R (Q (term.app f x) ⋀ S)) in assume σ₁_verified: ⊩ σ₁ : P, assume f_not_in_σ₁: f ∉ σ₁, assume x_not_in_σ₁: x ∉ σ₁, assume x_neq_f: x ≠ f, assume P_valid: σ ⊨ P.to_vc, assume f_is_vf: σ f = value.func f x R S e σ₁, assume σ₁f_verified: ⊩ (σ₁[f↦vf]) : P', have (∀y, y ∈ σ₁ → (σ₁ y = σ y)), from env_equiv_of_translation_valid σ₁_verified σ P_valid, have env_equiv: (∀y, y ∈ (σ₁[f↦vf]) → ((σ₁[f↦vf]) y = σ y)), from env.equiv_of_rest_and_same this f_not_in_σ₁ f_is_vf, have h1: (σ₁[f↦vf]) f = σ f, from env_equiv f (env.contains.same), have σ f = vf, from eq.trans h1.symm (env.apply_of_contains f_not_in_σ₁), have h2: term.subst_env σ f = vf, from (term.subst_env.var.right vf).mp this, have h3: (prop.subst_env (σ₁[f↦vf]) (prop.func f x R (Q (term.app f x) ⋀ S))) = (term.unop unop.isFunc vf ⋀ prop.forallc x (prop.subst_env (σ₁[f↦vf]) forallp')), from ( have h3: prop.func f x R (Q (term.app f x) ⋀ S) = (term.unop unop.isFunc f ⋀ prop.forallc x forallp'), from rfl, have h4: prop.subst_env (σ₁[f↦vf]) (term.unop unop.isFunc f ⋀ prop.forallc x forallp') = (prop.subst_env (σ₁[f↦vf]) (term.unop unop.isFunc f) ⋀ prop.subst_env (σ₁[f↦vf]) (prop.forallc x forallp')), from prop.subst_env.and, have h5: prop.subst_env (σ₁[f↦vf]) (term.unop unop.isFunc f) = term.subst_env (σ₁[f↦vf]) (term.unop unop.isFunc f), from prop.subst_env.term, have h6: term.subst_env (σ₁[f↦vf]) (term.unop unop.isFunc f) = term.unop unop.isFunc (term.subst_env (σ₁[f↦vf]) f), from term.subst_env.unop, have h7: term.subst_env (σ₁[f↦vf]) f = vf, from (term.subst_env.var.right vf).mp (env.apply_of_contains f_not_in_σ₁), have ¬ (x = f ∨ x ∈ σ₁), from not_or_distrib.mpr ⟨x_neq_f, x_not_in_σ₁⟩, have x ∉ (σ₁[f↦vf]), from mt env.contains.inv this, have h8: prop.subst_env (σ₁[f↦vf]) (prop.forallc x forallp') = prop.forallc x (prop.subst_env (σ₁[f↦vf]) forallp'), from prop.subst_env.forallc_not_in this, show (prop.subst_env (σ₁[f↦vf]) (prop.func f x R (Q (term.app f x) ⋀ S))) = (term.unop unop.isFunc vf ⋀ prop.forallc x (prop.subst_env (σ₁[f↦vf]) forallp')), from h7 ▸ h8 ▸ h7 ▸ h6 ▸ h5 ▸ h3.symm ▸ h4 ), have h4: spec.to_prop (spec.func f x R S) = (term.unop unop.isFunc f ⋀ prop.forallc x forallp), by unfold spec.to_prop, have h5: σ ⊨ vc.implies (term.unop unop.isFunc vf) (term.unop unop.isFunc f), from valid_env.mpr ( assume : σ ⊨ prop.to_vc (term.unop unop.isFunc vf), have unop.apply unop.isFunc vf = value.true, by unfold unop.apply, have ⊨ value.true ≡ term.unop unop.isFunc vf, from valid.unop.mp this, have ⊨ term.unop unop.isFunc vf, from valid.eq.true.mpr this, have h72: ⊨ term.unop unop.isFunc (term.subst_env σ f), from h2.symm ▸ this, have term.subst_env σ (term.unop unop.isFunc f) = term.unop unop.isFunc (term.subst_env σ f), from term.subst_env.unop, have ⊨ term.subst_env σ (term.unop unop.isFunc f), from this.symm ▸ h72, have h73: ⊨ vc.term (term.subst_env σ (term.unop unop.isFunc f)), from this, have vc.subst_env σ (term.unop unop.isFunc f) = vc.term (term.subst_env σ (term.unop unop.isFunc f)), from vc.subst_env.term, have ⊨ vc.subst_env σ (term.unop unop.isFunc f), from this.symm ▸ h73, have h74: σ ⊨ term.unop unop.isFunc f, from this, have prop.to_vc (prop.term (term.unop unop.isFunc f)) = vc.term (term.unop unop.isFunc f), by unfold prop.to_vc, show σ ⊨ (prop.term (term.unop unop.isFunc f)).to_vc, from this.symm ▸ h74 ), have h6: σ ⊨ vc.implies (prop.forallc x (prop.subst_env (σ₁[f↦vf]) forallp')).to_vc (prop.forallc x forallp).to_vc, by begin apply valid_env.mpr, assume h1, unfold prop.to_vc, rw[vc.subst_env.univ], apply valid.univ.mp, assume v: value, unfold prop.to_vc at h1, rw[vc.subst_env.univ] at h1, have h2, from valid.univ.mpr h1 v, have h3: (vc.substt x ↑v (vc.subst_env (env.without σ x) (prop.to_vc (prop.subst_env (σ₁[f↦vf]) forallp'))) = vc.subst x v (vc.subst_env (env.without σ x) (prop.to_vc (prop.subst_env (σ₁[f↦vf]) forallp')))), from vc.substt_value_eq_subst, rw[h3] at h2, have : ¬ (x = f ∨ x ∈ σ₁), from not_or_distrib.mpr ⟨x_neq_f, x_not_in_σ₁⟩, have h61: x ∉ (σ₁[f↦vf]), from mt env.contains.inv this, have : (∀y, y ∈ (σ₁[f↦vf]) → ((σ₁[f↦vf]) y = (σ.without x) y)), from env.remove_unimportant_equivalence env_equiv h61, have : (∀y, y ∈ (σ₁[f↦vf]) → ((σ₁[f↦vf]) y = (σ.without x[x↦v]) y)), from env.equiv_of_not_contains this h61, have h7: (((σ.without x)[x↦v]) ⊨ vc.implies (prop.subst_env (σ₁[f↦vf]) forallp').to_vc forallp'.to_vc), from vc.implies.equiv_subst this, have h82: (((σ.without x)[x↦v]) ⊨ vc.implies (prop.implies (prop.post f x) (Q (term.app f x) ⋀ S.to_prop)).to_vc (prop.implies (prop.post f x) S.to_prop).to_vc), by begin apply valid_env.mpr, assume h1, unfold prop.implies, unfold prop.implies at h1, unfold prop.to_vc, unfold prop.to_vc at h1, cases valid_env.or.elim h1 with h2 h2, apply valid_env.or₁, from h2, apply valid_env.or₂, from (valid_env.to_vc_and.elim h2).right end, have h8: (((σ.without x)[x↦v]) ⊨ vc.implies forallp'.to_vc forallp.to_vc), from vc.implies.same_left (λ_, h82), have h81, from valid_env.mp h8, have h9: (vc.subst x v (vc.subst_env (env.without σ x) (prop.to_vc forallp)) = vc.subst_env (env.without σ x[x↦v]) (prop.to_vc forallp)), by unfold vc.subst_env, rw[h9], apply h81, have h71, from valid_env.mp h7, apply h71, have h10: (vc.subst x v (vc.subst_env (env.without σ x) (prop.to_vc (prop.subst_env (σ₁[f↦vf]) forallp'))) = vc.subst_env (env.without σ x[x↦v]) (prop.to_vc (prop.subst_env (σ₁[f↦vf]) forallp'))), by unfold vc.subst_env, rw[h10] at h2, from h2 end, have h7: σ ⊨ vc.implies (prop.term (term.unop unop.isFunc vf) ⋀ prop.forallc x (prop.subst_env (σ₁[f↦vf]) forallp')).to_vc (prop.term (term.unop unop.isFunc f) ⋀ prop.forallc x forallp).to_vc, from vc.implies.and_intro h5 (λ_, h6), show σ ⊨ vc.implies (prop.subst_env (σ₁[f↦value.func f x R S e σ₁]) (prop.func f x R (Q (term.app f x) ⋀ S))).to_vc (spec.to_prop (spec.func f x R S)).to_vc, from h3.symm ▸ h4.symm ▸ h7 theorem preservation {s: dstack} {Q: propctx}: (⊩ₛ s : Q) → ∀s', (s ⟹ s') → ∃Q', (⊩ₛ s' : Q') ∧ (∀σ' t, σ' ⊨ vc.implies (Q' t).to_vc (Q t).to_vc) ∧ (∀v: value, FV (Q v) ⊆ FV (Q' v)) := assume s_verified: ⊩ₛ s : Q, begin induction s_verified, case stack.dvcgen.top σ e R P Q σ_verified fv_R R_valid e_verified { assume s', assume s_steps: ((R, σ, e) ⟹ s'), have R_closed: closed_subst σ R.to_prop, from ( assume z: var, assume : z ∈ FV R.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R this, show z ∈ σ.dom, from (free_iff_contains σ_verified).symm ▸ this ), cases s_steps, case dstep.tru x e { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.fals x e { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.num n e x { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.closure R' S' f x e₁ e₂ { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.unop op x y e { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.binop op x y z e { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.app f x y σ₂ g R₂ S₂ gx e₁ e₂ vₓ f_is_func x_is_vₓ { cases e_verified, case exp.dvcgen.app Q f_free x_free y_not_free e₂_verified func_vc { from have ∃σ' Q', ⊩ (σ'[f ↦ value.func g gx R₂ S₂ e₁ σ₂]) : Q', from env.dvcgen.inv σ_verified f_is_func, let ⟨σ', Q', ha1⟩ := this in have ∃Q₁ Q₂ Q₃, f ∉ σ' ∧ g ∉ σ₂ ∧ gx ∉ σ₂ ∧ g ≠ gx ∧ (⊩ σ' : Q₁) ∧ (⊩ σ₂ : Q₂) ∧ gx ∈ FV R₂.to_prop.to_vc ∧ FV R₂.to_prop ⊆ FV Q₂ ∪ { g, gx } ∧ FV S₂.to_prop ⊆ FV Q₂ ∪ { g, gx } ∧ (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂ ⊩ e₁ : Q₃) ∧ ⦃prop.implies (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂ ⋀ Q₃ (term.app g gx)) S₂⦄ ∧ (Q' = (Q₁ ⋀ ((f ≡ (value.func g gx R₂ S₂ e₁ σ₂)) ⋀ prop.subst_env (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]) (prop.func g gx R₂ (Q₃ (term.app g gx) ⋀ S₂))))), from env.dvcgen.func.inv ha1, let ⟨Q₁, Q₂, Q₃, ha2⟩ := this in let Q₂' := (Q₂ ⋀ ((g ≡ (value.func g gx R₂ S₂ e₁ σ₂)) ⋀ prop.subst_env (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]) (prop.func g gx R₂ (Q₃ (term.app g gx) ⋀ S₂)))) in have ha3: ⊩ (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]) : Q₂', from env.dvcgen.func ha2.right.left ha2.right.left ha2.right.right.left ha2.right.right.right.left ha2.right.right.right.right.right.left ha2.right.right.right.right.right.left ha2.right.right.right.right.right.right.left ha2.right.right.right.right.right.right.right.left ha2.right.right.right.right.right.right.right.right.left ha2.right.right.right.right.right.right.right.right.right.left ha2.right.right.right.right.right.right.right.right.right.right.left, have ∃σ'' Q'', ⊩ (σ''[x ↦ vₓ]) : Q'', from env.dvcgen.inv σ_verified x_is_vₓ, let ⟨σ'', Q'', ha4⟩ := this in have gx ∉ (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]), from ( assume : gx ∈ (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]), or.elim (env.contains.inv this) ( assume : gx = g, show «false», from ha2.right.right.right.left this.symm ) ( assume : gx ∈ σ₂, show «false», from ha2.right.right.left this ) ), have ∃P₃', ⊩ (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ]) : Q₂' ⋀ P₃', from env.dvcgen.copy ha3 this ha4, let ⟨P₃', ha5⟩ := this in let P₃ := Q₂' ⋀ P₃' in have ha6: Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂ ⊩ e₁ : Q₃, from ha2.right.right.right.right.right.right.right.right.right.left, have ha7: FV R₂.to_prop ⊆ FV P₃, from ( have hb1: FV P₃ = (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ]).dom, from (free_iff_contains ha5).symm, have hb2: (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ]).dom = (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]).dom ∪ set.insert gx ∅, from env.dom.inv, have hb3: (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]).dom = σ₂.dom ∪ set.insert g ∅, from env.dom.inv, have hb4: FV P₃ = σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from eq.trans hb1 (hb3 ▸ hb2), have hb5: FV P₃ = σ₂.dom ∪ {g, gx}, by {simp at hb4, rw[set.two_elems_of_insert] at hb4, from hb4}, have hb6: FV Q₂ = σ₂.dom, from (free_iff_contains ha2.right.right.right.right.right.left).symm, have hb7: FV R₂.to_prop ⊆ σ₂.dom ∪ {g, gx}, from ( assume x: var, assume : x ∈ FV R₂.to_prop, have x ∈ FV Q₂ ∪ {g, gx}, from set.mem_of_mem_of_subset this ha2.right.right.right.right.right.right.right.left, show x ∈ σ₂.dom ∪ {g, gx}, from hb6 ▸ this ), show FV R₂.to_prop ⊆ FV P₃, from hb5.symm ▸ hb7 ), have ha8: FV (↑R₂ ⋀ P₃) = FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), from ( have hb1: FV P₃ = (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ]).dom, from (free_iff_contains ha5).symm, have hb2: (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ]).dom = (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]).dom ∪ set.insert gx ∅, from env.dom.inv, have hb3: (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]).dom = σ₂.dom ∪ set.insert g ∅, from env.dom.inv, have hb4: FV P₃ = σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from eq.trans hb1 (hb3 ▸ hb2), have hb8: FV (P₃ ⋀ R₂) = FV P₃ ∪ FV R₂.to_prop, from free_in_prop.and_elim, have FV P₃ ∪ FV R₂.to_prop = FV P₃, from set.union_eq_self_of_subset_right ha7, have hb9: FV (P₃ ⋀ R₂) = FV P₃, from eq.trans hb8 this, let forallp := (prop.implies R₂.to_prop (prop.pre g gx) ⋀ prop.implies (prop.post g gx) S₂.to_prop) in have hb5: FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂) = σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.eq_of_subset_of_subset ( assume x: var, have hb6: x ∈ FV Q₂ ∪ {g, gx} → x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from ( assume : x ∈ FV Q₂ ∪ {g, gx}, or.elim (set.mem_or_mem_of_mem_union this) ( assume hb2: x ∈ FV Q₂, have FV Q₂ = σ₂.dom, from (free_iff_contains ha2.right.right.right.right.right.left).symm, have x ∈ σ₂.dom, from this ▸ hb2, have x ∈ σ₂.dom ∪ set.insert g ∅, from set.mem_union_left (set.insert g ∅) this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_left (set.insert gx ∅) this ) ( assume : x ∈ {g, gx}, have (x = g) ∨ (x = gx), from set.two_elems_mem this, or.elim this ( assume : x = g, have x ∈ set.insert g ∅, from set.mem_singleton_of_eq this, have x ∈ σ₂.dom ∪ set.insert g ∅, from set.mem_union_right σ₂.dom this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_left (set.insert gx ∅) this ) ( assume : x = gx, have x ∈ set.insert gx ∅, from set.mem_singleton_of_eq this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_right (σ₂.dom ∪ set.insert g ∅) this ) ) ), assume : x ∈ FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), or.elim (free_in_prop.and.inv this) ( assume hb2: x ∈ FV Q₂, have FV Q₂ = σ₂.dom, from (free_iff_contains ha2.right.right.right.right.right.left).symm, have x ∈ σ₂.dom, from this ▸ hb2, have x ∈ σ₂.dom ∪ set.insert g ∅, from set.mem_union_left (set.insert g ∅) this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_left (set.insert gx ∅) this ) ( assume : free_in_prop x (spec.func g gx R₂ S₂ ⋀ R₂), or.elim (free_in_prop.and.inv this) ( assume h1: free_in_prop x (spec.func g gx R₂ S₂), have spec.to_prop (spec.func g gx R₂ S₂) = (prop.func g gx R₂.to_prop S₂.to_prop), by unfold spec.to_prop, have free_in_prop x (prop.func g gx R₂.to_prop S₂.to_prop), from this ▸ h1, or.elim (free_in_prop.and.inv this) ( assume : free_in_prop x (term.unop unop.isFunc g), have free_in_term x (term.unop unop.isFunc g), from free_in_prop.term.inv this, have free_in_term x g, from free_in_term.unop.inv this, have x = g, from free_in_term.var.inv this, have x ∈ set.insert g ∅, from set.mem_singleton_of_eq this, have x ∈ σ₂.dom ∪ set.insert g ∅, from set.mem_union_right σ₂.dom this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_left (set.insert gx ∅) this ) ( assume x_free_in_forallp: free_in_prop x (prop.forallc gx forallp), have free_in_prop x forallp, from (free_in_prop.forallc.inv x_free_in_forallp).right, or.elim (free_in_prop.and.inv this) ( assume : free_in_prop x (prop.implies R₂.to_prop (prop.pre g gx)), or.elim (free_in_prop.implies.inv this) ( assume : x ∈ FV R₂.to_prop, have x ∈ FV Q₂ ∪ {g, gx}, from set.mem_of_mem_of_subset this ha2.right.right.right.right.right.right.right.left, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from hb6 this ) ( assume : free_in_prop x (prop.pre g gx), have free_in_term x g ∨ free_in_term x gx, from free_in_prop.pre.inv this, or.elim this ( assume : free_in_term x g, have x = g, from free_in_term.var.inv this, have x ∈ set.insert g ∅, from set.mem_singleton_of_eq this, have x ∈ σ₂.dom ∪ set.insert g ∅, from set.mem_union_right σ₂.dom this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_left (set.insert gx ∅) this ) ( assume : free_in_term x gx, have x = gx, from free_in_term.var.inv this, have x ∈ set.insert gx ∅, from set.mem_singleton_of_eq this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_right (σ₂.dom ∪ set.insert g ∅) this ) ) ) ( assume : free_in_prop x (prop.implies (prop.post g gx) S₂.to_prop), or.elim (free_in_prop.implies.inv this) ( assume : free_in_prop x (prop.post g gx), have free_in_term x g ∨ free_in_term x gx, from free_in_prop.post.inv this, or.elim this ( assume : free_in_term x g, have x = g, from free_in_term.var.inv this, have x ∈ set.insert g ∅, from set.mem_singleton_of_eq this, have x ∈ σ₂.dom ∪ set.insert g ∅, from set.mem_union_right σ₂.dom this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_left (set.insert gx ∅) this ) ( assume : free_in_term x gx, have x = gx, from free_in_term.var.inv this, have x ∈ set.insert gx ∅, from set.mem_singleton_of_eq this, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from set.mem_union_right (σ₂.dom ∪ set.insert g ∅) this ) ) ( assume : free_in_prop x S₂.to_prop, have x ∈ FV Q₂ ∪ {g, gx}, from set.mem_of_mem_of_subset this ha2.right.right.right.right.right.right.right.right.left, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from hb6 this ) ) ) ) ( assume : x ∈ FV R₂.to_prop, have x ∈ FV Q₂ ∪ {g, gx}, from set.mem_of_mem_of_subset this ha2.right.right.right.right.right.right.right.left, show x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, from hb6 this ) ) ) ( assume x: var, assume : x ∈ σ₂.dom ∪ set.insert g ∅ ∪ set.insert gx ∅, or.elim (set.mem_or_mem_of_mem_union this) ( assume : x ∈ σ₂.dom ∪ set.insert g ∅, or.elim (set.mem_or_mem_of_mem_union this) ( assume hb2: x ∈ σ₂.dom, have FV Q₂ = σ₂.dom, from (free_iff_contains ha2.right.right.right.right.right.left).symm, have x ∈ FV Q₂, from this.symm ▸ hb2, show x ∈ FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), from free_in_prop.and₁ this ) ( assume : x ∈ set.insert g ∅, have x = g, from (set.mem_singleton_iff x g).mp this, have free_in_term x g, from this ▸ free_in_term.var x, have free_in_term x (term.unop unop.isFunc g), from free_in_term.unop this, have free_in_prop x (term.unop unop.isFunc g), from free_in_prop.term this, have h1: x ∈ FV (prop.func g gx R₂ S₂), from free_in_prop.and₁ this, have spec.to_prop (spec.func g gx R₂ S₂) = (prop.func g gx R₂.to_prop S₂.to_prop), by unfold spec.to_prop, have free_in_prop x (spec.to_prop (spec.func g gx R₂ S₂)), from this.symm ▸ h1, have free_in_prop x (spec.func g gx R₂ S₂), from this, have free_in_prop x (spec.func g gx R₂ S₂ ⋀ R₂), from free_in_prop.and₁ this, show x ∈ FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), from free_in_prop.and₂ this ) ) ( assume : x ∈ set.insert gx ∅, have x = gx, from (set.mem_singleton_iff x gx).mp this, have x ∈ FV R₂.to_prop.to_vc, from this.symm ▸ ha2.right.right.right.right.right.right.left, have x ∈ FV R₂.to_prop, from set.mem_of_mem_of_subset this free_in_prop_of_free_in_to_vc, have free_in_prop x (spec.func g gx R₂ S₂ ⋀ R₂), from free_in_prop.and₂ this, show x ∈ FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), from free_in_prop.and₂ this ) ), have FV P₃ = FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), from eq.trans hb4 hb5.symm, show FV (↑R₂ ⋀ P₃) = FV (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂), from eq.trans free_in_prop.and_symm (eq.trans hb9 this) ), have ha9: σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ] ⊨ prop.to_vc (spec.to_prop R₂), from ( have env_has_f: f ∈ σ, from env.contains_apply_equiv.right.mp (exists.intro (value.func g gx R₂ S₂ e₁ σ₂) f_is_func), have env_has_x: x ∈ σ, from env.contains_apply_equiv.right.mp (exists.intro vₓ x_is_vₓ), have closed_subst σ (↑(term.unop unop.isFunc f) ⋀ prop.pre f x), from ( assume z: var, assume : z ∈ FV (↑(term.unop unop.isFunc f) ⋀ prop.pre f x), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (term.unop unop.isFunc f), have free_in_term z (term.unop unop.isFunc f), from free_in_prop.term.inv this, have free_in_term z f, from free_in_term.unop.inv this, have z = f, from free_in_term.var.inv this, show z ∈ σ, from this.symm ▸ env_has_f ) ( assume : z ∈ FV (prop.pre f x), or.elim (free_in_prop.pre.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, show z ∈ σ, from this.symm ▸ env_has_f ) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, show z ∈ σ, from this.symm ▸ env_has_x ) ) ), have h3: σ ⊨ (↑(term.unop unop.isFunc f) ⋀ prop.pre f x).to_vc, from consequent_of_pre_P_call σ_verified R_closed R_valid env_has_x this func_vc, have (prop.and (prop.term (term.unop unop.isFunc f)) (prop.pre f x)).to_vc = ((prop.term (term.unop unop.isFunc f)).to_vc ⋀ (prop.pre f x).to_vc), by unfold prop.to_vc, have σ ⊨ ((prop.term (term.unop unop.isFunc f)).to_vc ⋀ (prop.pre f x).to_vc), from this ▸ h3, have h4: σ ⊨ (prop.pre f x).to_vc, from (valid_env.and.elim this).right, have (prop.pre f x).to_vc = vc.pre f x, by unfold prop.to_vc, have h5: σ ⊨ vc.pre f x, from this ▸ h4, have vc.subst_env σ (vc.pre f x) = vc.pre (term.subst_env σ f) (term.subst_env σ x), from vc.subst_env.pre, have h6: ⊨ vc.pre (term.subst_env σ f) (term.subst_env σ x), from this ▸ h5, have term.subst_env σ f = value.func g gx R₂ S₂ e₁ σ₂, from (term.subst_env.var.right (value.func g gx R₂ S₂ e₁ σ₂)).mp f_is_func, have h7: ⊨ vc.pre (value.func g gx R₂ S₂ e₁ σ₂) (term.subst_env σ x), from this ▸ h6, have term.subst_env σ x = vₓ, from (term.subst_env.var.right vₓ).mp x_is_vₓ, have ⊨ vc.pre (value.func g gx R₂ S₂ e₁ σ₂) vₓ, from this ▸ h7, show (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ] ⊨ R₂.to_prop.to_vc), from valid.pre.mpr this ), have ∀σ, σ ⊨ vc.implies (R₂.to_prop ⋀ P₃).to_vc (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂).to_vc, from ( assume σ: env, have hb1: σ ⊨ vc.implies (R₂.to_prop ⋀ P₃).to_vc (P₃ ⋀ R₂).to_vc, from vc.implies.and_symm, have hb4: σ ⊨ vc.implies (P₃ ⋀ ↑R₂).to_vc (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂).to_vc, from ( have (∃Q, (⊩ (σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂]) : Q) ∧ ∀σ', σ' ⊨ vc.implies P₃.to_vc Q.to_vc), from env_implies_rest ha5, let ⟨Q₂'', ⟨hb1, hb2⟩⟩ := this in have Q₂' = Q₂'', from env.dvcgen.inj ha3 Q₂'' hb1, have hb3: σ ⊨ vc.implies P₃.to_vc Q₂'.to_vc, from this.symm ▸ hb2 σ, have σ ⊨ vc.implies Q₂'.to_vc (Q₂ ⋀ spec.func g gx R₂ S₂).to_vc, from ( vc.implies.same_left ( assume Q₂_valid: σ ⊨ Q₂.to_vc, vc.implies.left_elim ( assume : σ ⊨ (prop.term (g ≡ value.func g gx R₂ S₂ e₁ σ₂)).to_vc, have (σ g = value.func g gx R₂ S₂ e₁ σ₂), from valid_env.subst_of_eq this, inlined_dominates_spec ha2.right.right.right.right.right.left ha2.right.left ha2.right.right.left ha2.right.right.right.left.symm Q₂_valid this ha3 ) ) ), have σ ⊨ vc.implies P₃.to_vc (Q₂ ⋀ spec.func g gx R₂ S₂).to_vc, from vc.implies.trans hb3 this, have hb8: σ ⊨ vc.implies (P₃ ⋀ ↑R₂).to_vc ((Q₂ ⋀ spec.func g gx R₂ S₂) ⋀ R₂).to_vc, from vc.implies.same_right (λ_, this), have hb9: σ ⊨ vc.implies ((Q₂ ⋀ spec.func g gx R₂ S₂) ⋀ R₂).to_vc (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂).to_vc, from vc.implies.and_assoc.symm, show σ ⊨ vc.implies (P₃ ⋀ ↑R₂).to_vc (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂).to_vc, from vc.implies.trans hb8 hb9 ), show σ ⊨ vc.implies (↑R₂ ⋀ P₃).to_vc (Q₂ ⋀ spec.func g gx R₂ S₂ ⋀ R₂).to_vc, from vc.implies.trans hb1 hb4 ), have ↑R₂ ⋀ P₃ ⊩ e₁ : Q₃, from strengthen_exp ha6 (↑R₂ ⋀ P₃) ha8 this, have h5: ⊩ₛ (R₂, σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ], e₁) : P₃ ⋀ Q₃, from stack.dvcgen.top ha5 ha7 ha9 this, have h6: y ∉ σ, from ( have y ∉ FV P, from ( assume : y ∈ FV P, have y ∈ FV (R.to_prop ⋀ P), from free_in_prop.and₂ this, show «false», from y_not_free this ), show y ∉ σ, from mt (free_of_contains σ_verified) this ), have h7: (↑R ⋀ P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⊩ e₂ : Q), from ( have ha1: FV (↑R ⋀ P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x) = FV ((↑R ⋀ P) ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x), from free_in_prop.and_assoc, have ∀σ, σ ⊨ vc.implies (↑R ⋀ P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x).to_vc ((↑R ⋀ P) ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x).to_vc, from λσ, vc.implies.and_assoc, show (↑R ⋀ P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⊩ e₂ : Q), from strengthen_exp e₂_verified (↑R ⋀ P ⋀ prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x) ha1 this ), have h8: ⦃ prop.implies (↑R ⋀ P ⋀ prop.call x) (↑(term.unop unop.isFunc f) ⋀ prop.pre f x) ⦄, from ( assume σ: env, have ha1: σ ⊨ vc.implies (↑R ⋀ P ⋀ prop.call x).to_vc ((↑R ⋀ P) ⋀ prop.call x).to_vc, from vc.implies.and_assoc, have FV (↑R ⋀ P ⋀ prop.call x) = FV ((↑R ⋀ P) ⋀ prop.call x), from free_in_prop.and_assoc, have ha2: FV ((↑R ⋀ P) ⋀ prop.call x) ⊆ FV (↑R ⋀ P ⋀ prop.call x), from set.subset_of_eq this.symm, strengthen_vc ha1 ha2 (func_vc σ) ), have h9: (R₂, σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ], e₁) ⟹* (R₂, σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ], e₁), from trans_dstep.rfl, have h10: ∀σ' t, (σ' ⊨ ((↑P₃ ⋀ Q₃) t).to_vc) → σ' ⊨ ((Q₂' ⋀ P₃') ⋀ Q₃ t).to_vc, from ( assume σ': env, assume t: term, assume h10a: σ' ⊨ ((↑(Q₂' ⋀ P₃') ⋀ Q₃) t).to_vc, have h11: (↑(Q₂' ⋀ P₃') ⋀ Q₃) t = ((Q₂' ⋀ P₃') ⋀ Q₃ t), from propctx_apply_pq, show σ' ⊨ ((Q₂'⋀ P₃') ⋀ Q₃ t).to_vc, from @eq.subst prop (λa, σ' ⊨ a.to_vc) ((↑(Q₂' ⋀ P₃') ⋀ Q₃) t) ((Q₂' ⋀ P₃') ⋀ Q₃ t) h11 h10a ), have h11: ∀v: value, FV ((Q₂' ⋀ P₃') ⋀ Q₃ v) ⊆ FV ((↑P₃ ⋀ Q₃) v), from ( assume v: value, have h11: (↑P₃ ⋀ Q₃) v = (P₃ ⋀ Q₃ v), from propctx_apply_pq, have FV ((Q₂'⋀ P₃') ⋀ Q₃ v) ⊆ FV (P₃ ⋀ Q₃ v), from set.subset.refl (FV (P₃ ⋀ Q₃ v)), show FV ((Q₂'⋀ P₃') ⋀ Q₃ v) ⊆ FV ((↑P₃ ⋀ Q₃) v), from h11.symm ▸ this ), have h12: ⊩ₛ dstack.cons (R₂, σ₂[g↦value.func g gx R₂ S₂ e₁ σ₂][gx↦vₓ], e₁) R σ y f x e₂ : P ⋀ propctx.exis y (prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⋀ Q), from stack.dvcgen.cons h5 h6 σ_verified ha2.right.right.right.right.right.left ha5 fv_R R_valid f_is_func x_is_vₓ h7 ha2.right.right.right.right.right.right.right.right.right.left h10 h11 h8 h9, have h13: ∀σ' t, σ' ⊨ vc.implies ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post f x) ⋀ ↑(y ≡ term.app f x) ⋀ Q)) t).to_vc ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post f x) ⋀ ↑(y ≡ term.app f x) ⋀ Q)) t).to_vc, from λσ' t, vc.implies.self, have h14: ∀v: value, (FV ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post f x) ⋀ ↑(y ≡ term.app f x) ⋀ Q)) v) ⊆ FV ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post f x) ⋀ ↑(y ≡ term.app f x) ⋀ Q)) v)), from λv, set.subset.refl (FV ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post f x) ⋀ ↑(y ≡ term.app f x) ⋀ Q)) v)), exists.intro ( P ⋀ propctx.exis y (prop.call x ⋀ prop.post f x ⋀ y ≡ term.app f x ⋀ Q)) ⟨h12, ⟨h13, h14⟩⟩ } }, case dstep.ite_true x e₁ e₂ { from exp.preservation σ_verified fv_R R_valid e_verified s_steps }, case dstep.ite_false x e₁ e₂ { from exp.preservation σ_verified fv_R R_valid e_verified s_steps } }, case stack.dvcgen.cons P P' P'' s' σ σ' f g x y fx R' R S e e' vₓ Q₂ Q₃ Q₂' s'_verified y_not_in_σ σ_verified σ'_verified σ''_verified fv_R' R'_valid g_is_func x_is_v cont e'_verified Q₂'_dom Q₂'_fv pre_vc steps ih { assume s''', assume s_steps: (dstack.cons s' R' σ y g x e ⟹ s'''), cases s_steps, case dstep.ctx s'' s'_steps { from have (∃ (Q' : propctx), (⊩ₛ s'' : Q') ∧ (∀σ' t, σ' ⊨ vc.implies (Q' t).to_vc (Q₂' t).to_vc) ∧ (∀v: value, FV (Q₂' v) ⊆ FV (Q' v))), from ih s'' s'_steps, let ⟨Q', ⟨h1, ⟨h2, h3⟩⟩⟩ := this in have new_steps: ((R, σ'[f↦value.func f fx R S e' σ'][fx↦vₓ], e') ⟹* s''), from trans_dstep.trans steps s'_steps, have h4: ∀ (σ' : env) (t : term), (σ' ⊨ (Q' t).to_vc) → σ' ⊨ (P'' ⋀ Q₂ t).to_vc, from ( assume σ'': env, assume t: term, have h4: σ'' ⊨ vc.implies (Q' t).to_vc (Q₂' t).to_vc, from h2 σ'' t, have h5: σ'' ⊨ vc.implies (Q₂' t).to_vc (P'' ⋀ Q₂ t).to_vc, from valid_env.mpr (Q₂'_dom σ'' t), have h6: σ'' ⊨ vc.implies (Q' t).to_vc (P'' ⋀ Q₂ t).to_vc, from vc.implies.trans h4 h5, valid_env.mp h6 ), have h5: ∀v: value, (FV (P'' ⋀ Q₂ v) ⊆ FV (Q' v)), from ( assume v: value, have h7: FV (Q₂' v) ⊆ FV (Q' v), from h3 v, have h8: FV (P'' ⋀ Q₂ v) ⊆ FV (Q₂' v), from Q₂'_fv v, show FV (P'' ⋀ Q₂ v) ⊆ FV (Q' v), from set.subset.trans h8 h7 ), have h6: ⊩ₛ dstack.cons s'' R' σ y g x e : P ⋀ propctx.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃), from stack.dvcgen.cons h1 y_not_in_σ σ_verified σ'_verified σ''_verified fv_R' R'_valid g_is_func x_is_v cont e'_verified h4 h5 pre_vc new_steps, have h7: ∀σ'' t, σ'' ⊨ vc.implies ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) t).to_vc ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) t).to_vc, from λσ'' t, vc.implies.self, have h8: ∀v: value, FV ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) v) ⊆ FV ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) v), from λv, set.subset.refl (FV ((↑P ⋀ propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) v)), exists.intro ( P ⋀ propctx.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃)) ⟨h6, ⟨h7, h8⟩⟩ }, case dstep.return σ₁ σ₂ f₁ x₁ y₁ R'₁ R₁ S₁ e₁ vy₁ vx₁ y_is_vy₁ g_is_func₁ x_is_vx₁ { from have ∃P₁ Q₁, (⊩ σ₁: P₁) ∧ (FV R'₁.to_prop ⊆ FV P₁) ∧ (σ₁ ⊨ R'₁.to_prop.to_vc) ∧ (R'₁ ⋀ P₁ ⊩ exp.return y₁ : Q₁), from stack.dvcgen.top.inv s'_verified, let ⟨P₁, Q₁, ⟨σ₁_verified, ⟨h1, ⟨h2, h3⟩⟩⟩⟩ := this in have ∃σ' Q', ⊩ (σ'[y₁↦vy₁]) : Q', from env.dvcgen.inv σ₁_verified y_is_vy₁, let ⟨σ₁', Q₁', h4⟩ := this in have ∃P₃, (⊩ (σ[y↦vy₁]) : P ⋀ P₃), from env.dvcgen.copy σ_verified y_not_in_σ h4, let ⟨P₃, h5⟩ := this in have h6: FV R'.to_prop ⊆ FV (P ⋀ P₃), from ( assume z: var, assume : z ∈ FV R'.to_prop, have z ∈ FV P, from set.mem_of_subset_of_mem fv_R' this, show z ∈ FV (P ⋀ P₃), from free_in_prop.and₁ this ), have h7: y ∉ FV R'.to_prop.to_vc, from ( assume : y ∈ FV R'.to_prop.to_vc, have y ∈ FV R'.to_prop, from free_in_prop_of_free_in_to_vc this, have h10: y ∈ FV P, from set.mem_of_subset_of_mem fv_R' this, have σ.dom = FV P, from free_iff_contains σ_verified, have y ∈ σ.dom, from this.symm ▸ h10, have y ∈ σ, from this, show «false», from y_not_in_σ this ), have h8: (σ[y↦vy₁] ⊨ R'.to_prop.to_vc), from valid_with_additional_var R'_valid, have g_in_σ: g ∈ σ, from env.contains_apply_equiv.right.mp (exists.intro (value.func f fx R S e' σ') g_is_func), have x_in_σ: x ∈ σ, from env.contains_apply_equiv.right.mp (exists.intro vₓ x_is_v), have h9: (FV (↑R' ⋀ P ⋀ P₃) = FV (↑R' ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x)), from ( let sy: set var := set.insert y ∅ in have h12: (σ[y↦vy₁]).dom = FV (P ⋀ P₃), from free_iff_contains h5, have h13: (σ[y↦vy₁]).dom = (σ.dom ∪ sy), from env.dom.inv, have h14: FV (P ⋀ P₃) = (σ.dom ∪ sy), from eq.trans h12.symm h13, have h15: FV (↑R' ⋀ P ⋀ P₃) = (σ.dom ∪ sy), from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV (↑R' ⋀ P ⋀ P₃), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z R', have h10: z ∈ FV P, from set.mem_of_subset_of_mem fv_R' this, have σ.dom = FV P, from free_iff_contains σ_verified, have z ∈ σ.dom, from this.symm ▸ h10, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ( assume : z ∈ FV (P ⋀ P₃), show z ∈ (σ.dom ∪ sy), from h14 ▸ this ) ) ( assume z: var, assume : z ∈ σ.dom ∪ sy, have z ∈ FV (P ⋀ P₃), from h14.symm ▸ this, show z ∈ FV (↑R' ⋀ P ⋀ P₃), from free_in_prop.and₂ this ), have h18: FV (↑R' ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x) = σ.dom ∪ sy, from set.eq_of_subset_of_subset ( assume z: var, assume : z ∈ FV (↑R' ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z R', have h10: z ∈ FV P, from set.mem_of_subset_of_mem fv_R' this, have σ.dom = FV P, from free_iff_contains σ_verified, have z ∈ σ.dom, from this.symm ▸ h10, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ( assume : z ∈ FV (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), or.elim (free_in_prop.and.inv this) ( assume h10: z ∈ FV P, have σ.dom = FV P, from free_iff_contains σ_verified, have z ∈ σ.dom, from this.symm ▸ h10, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ( assume : z ∈ FV (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (prop.call x), have free_in_term z x, from free_in_prop.call.inv this, have z = x, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ x_in_σ, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ( assume : z ∈ FV (prop.post g x ⋀ y ≡ term.app g x), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (prop.post g x), or.elim (free_in_prop.post.inv this) ( assume : free_in_term z g, have z = g, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ g_in_σ, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ x_in_σ, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ) ( assume : free_in_prop z (y ≡ term.app g x), have free_in_term z (y ≡ term.app g x), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z y, have z = y, from free_in_term.var.inv this, have z ∈ sy, from set.mem_singleton_of_eq this, show z ∈ σ.dom ∪ sy, from set.mem_union_right σ.dom this ) ( assume : free_in_term z (term.app g x), or.elim (free_in_term.app.inv this) ( assume : free_in_term z g, have z = g, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ g_in_σ, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ x_in_σ, show z ∈ σ.dom ∪ sy, from set.mem_union_left sy this ) ) ) ) ) ) ) ( assume z: var, assume : z ∈ σ.dom ∪ sy, or.elim (set.mem_or_mem_of_mem_union this) ( assume h10: z ∈ σ.dom, have σ.dom = FV P, from free_iff_contains σ_verified, have z ∈ FV P, from this ▸ h10, have z ∈ FV (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), from free_in_prop.and₁ this, show z ∈ FV (↑R' ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), from free_in_prop.and₂ this ) ( assume : z ∈ sy, have h: z = y, from (set.mem_singleton_iff z y).mp this, have free_in_term y (term.var y), from free_in_term.var y, have free_in_term z y, from h.symm ▸ this, have free_in_term z (y ≡ term.app g x), from free_in_term.binop₁ this, have free_in_prop z (y ≡ term.app g x), from free_in_prop.term this, have z ∈ FV (prop.post g x ⋀ y ≡ term.app g x), from free_in_prop.and₂ this, have z ∈ FV (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), from free_in_prop.and₂ this, have z ∈ FV (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), from free_in_prop.and₂ this, show z ∈ FV (↑R' ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x), from free_in_prop.and₂ this ) ), eq.trans h15 h18.symm ), have h10: ∀σ₃, (σ₃ ⊨ (P ⋀ P₃).to_vc) → (σ₃ ⊨ vc.post g x ⋀ y ≡ term.app g x), from ( assume σ₃: env, assume P_P₃_valid: σ₃ ⊨ (P ⋀ P₃).to_vc, have P_valid: σ₃ ⊨ P.to_vc, from (valid_env.to_vc_and.elim P_P₃_valid).left, have env_equiv: (∀z, z ∈ σ → (σ z = σ₃ z)), from env_equiv_of_translation_valid σ_verified σ₃ P_valid, have env_equiv2: (∀z, z ∈ (σ[y↦vy₁]) → ((σ[y↦vy₁]) z = σ₃ z)), from env_equiv_of_translation_valid h5 σ₃ P_P₃_valid, have h21: σ₃ ⊨ P₃.to_vc, from (valid_env.to_vc_and.elim P_P₃_valid).right, have σ₃ g = (value.func f₁ x₁ R₁ S₁ e₁ σ₂), from eq.trans (env_equiv g g_in_σ).symm g_is_func₁, have h23: term.subst_env σ₃ g = value.func f₁ x₁ R₁ S₁ e₁ σ₂, from (term.subst_env.var.right (value.func f₁ x₁ R₁ S₁ e₁ σ₂)).mp this, have σ₃ x = vx₁, from eq.trans (env_equiv x x_in_σ).symm x_is_vx₁, have h24: term.subst_env σ₃ x = vx₁, from (term.subst_env.var.right vx₁).mp this, have (σ[y↦vy₁]) y = vy₁, from env.apply_of_contains y_not_in_σ, have σ₃ y = vy₁, from eq.trans (env_equiv2 y env.contains.same).symm this, have h25: term.subst_env σ₃ y = vy₁, from (term.subst_env.var.right vy₁).mp this, have some vx₁ = some vₓ, from eq.trans x_is_vx₁.symm x_is_v, have h65: vx₁ = vₓ, from option.some.inj this, have some (value.func f₁ x₁ R₁ S₁ e₁ σ₂) = some (value.func f fx R S e' σ'), from eq.trans g_is_func₁.symm g_is_func, have (value.func f₁ x₁ R₁ S₁ e₁ σ₂) = (value.func f fx R S e' σ'), from option.some.inj this, have h66: f₁ = f, from (value.func.inj this).left, have h67: x₁ = fx, from (value.func.inj this).right.left, have h68: R₁ = R, from (value.func.inj this).right.right.left, have h69: S₁ = S, from (value.func.inj this).right.right.right.left, have h70: e₁ = e', from (value.func.inj this).right.right.right.right.left, have h71: σ₂ = σ', from (value.func.inj this).right.right.right.right.right, have h49: σ₃ ⊨ vc.post g x, from ( have ∃P₁ Q₁', (⊩ σ₁: P₁) ∧ (FV R'₁.to_prop ⊆ FV P₁) ∧ (σ₁ ⊨ R'₁.to_prop.to_vc) ∧ (R'₁ ⋀ P₁ ⊩ exp.return y₁: Q₁'), from stack.dvcgen.top.inv s'_verified, let ⟨P₁, Q₁', ⟨σ₁_verified, ⟨fv_R'₁, ⟨R'₁_valid, return_verified⟩⟩⟩⟩ := this in have h42: σ₁.dom = FV P₁, from free_iff_contains σ₁_verified, have y₁_in_σ₁: y₁ ∈ σ₁, from env.contains_apply_equiv.right.mp (exists.intro vy₁ y_is_vy₁), have h26: term.subst_env σ₁ y₁ = vy₁, from (term.subst_env.var.right vy₁).mp y_is_vy₁, have R'₁ ⋀ P₁ ⊩ exp.return y₁ : y₁ ≣ •, from exp.dvcgen.return (exp.dvcgen.return.inv return_verified), have ⊩ₛ (R'₁, σ₁, exp.return y₁) : P₁ ⋀ y₁ ≣ •, from stack.dvcgen.top σ₁_verified fv_R'₁ R'₁_valid this, have h44: Q₂' = (P₁ ⋀ y₁ ≣ •), from stack.dvcgen.inj s'_verified (P₁ ⋀ y₁ ≣ •) this, have h45b: σ₁ ⊨ P₁.to_vc, from env_translation_valid σ₁_verified, have h46: σ₁ ⊨ P''.to_vc, from ( have h47: Q₂' vy₁ = (P₁.to_propctx ⋀ y₁ ≣ •) vy₁, from h44 ▸ rfl, have h48: (P₁.to_propctx ⋀ y₁ ≣ •) vy₁ = (P₁ ⋀ (y₁ ≣ •) vy₁), from propctx_apply_pq, have ((y₁ ≣ •):propctx) vy₁ = (y₁ ≡ vy₁), by { change (propctx.apply (propctx.term (y₁ ≣ •)) vy₁ = ↑(y₁ ≡ vy₁)), unfold propctx.apply, change (↑(termctx.apply (termctx.binop binop.eq y₁ •) vy₁) = ↑(y₁ ≡ vy₁)), unfold termctx.apply, change (↑((term.to_termctx y₁) vy₁ ≡ vy₁) = ↑(↑y₁ ≡ vy₁)), rw[@unchanged_of_apply_termctx_without_hole y₁ vy₁] }, have h49: Q₂' vy₁ = (P₁ ⋀ y₁ ≡ vy₁), from eq.trans h47 (this ▸ h48), have ⊨ vy₁ ≡ vy₁, from valid.refl, have ⊨ (term.subst_env σ₁ y₁) ≡ vy₁, from h26.symm ▸ this, have h50: ⊨ (term.subst_env σ₁ y₁) ≡ (term.subst_env σ₁ vy₁), from (@term.subst_env.value σ₁ vy₁).symm ▸ this, have term.subst_env σ₁ (y₁ ≡ vy₁) = (term.subst_env σ₁ y₁ ≡ term.subst_env σ₁ vy₁), from term.subst_env.binop, have h51: ⊨ term.subst_env σ₁ (y₁ ≡ vy₁), from this.symm ▸ h50, have vc.subst_env σ₁ (y₁ ≡ vy₁) = vc.term (term.subst_env σ₁ (y₁ ≡ vy₁)), from vc.subst_env.term, have ⊨ vc.subst_env σ₁ (y₁ ≡ vy₁), from this.symm ▸ h51, have h52: σ₁ ⊨ y₁ ≡ vy₁, from this, have prop.to_vc (prop.term (y₁ ≡ vy₁)) = vc.term (y₁ ≡ vy₁), by unfold prop.to_vc, have h53: σ₁ ⊨ prop.to_vc (y₁ ≡ vy₁) , from this.symm ▸ h52, have h53b: closed_subst σ₁ (prop.term (y₁ ≡ vy₁)), from ( assume z: var, assume : free_in_prop z (y₁ ≡ vy₁), have free_in_term z (y₁ ≡ vy₁), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z y₁, have z = y₁, from free_in_term.var.inv this, have z ∈ σ₁, from this.symm ▸ y₁_in_σ₁, show z ∈ σ₁.dom, from this ) ( assume : free_in_term z vy₁, show z ∈ σ₁.dom, from absurd this free_in_term.value.inv ) ), have closed_subst σ₁ (prop.term (y₁ ≡ vy₁)).to_vc, from to_vc_closed_subst_of_closed h53b, have σ₁ ⊨ prop.to_vc (y₁ ≡ vy₁), from h53, have σ₁ ⊨ (P₁ ⋀ y₁ ≡ vy₁).to_vc, from valid_env.to_vc_and h45b this, have h54: σ₁ ⊨ (Q₂' vy₁).to_vc, from h49.symm ▸ this, have σ₁ ⊨ vc.implies (Q₂' vy₁).to_vc (P'' ⋀ Q₂ vy₁).to_vc, from valid_env.mpr (Q₂'_dom σ₁ vy₁), have h55: σ₁ ⊨ (P'' ⋀ Q₂ vy₁).to_vc, from valid_env.mp this h54, show σ₁ ⊨ P''.to_vc, from (valid_env.to_vc_and.elim h55).left ), have env_equiv3: (∀z, z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) → (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ] z = σ₁ z)), from env_equiv_of_translation_valid σ''_verified σ₁ h46, have fx_is_vₓ: (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) fx = vₓ, from env.apply_of_vcgen σ''_verified, have fx_in_σ'': fx ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.same, have σ₁ fx = vₓ, from eq.trans (env_equiv3 fx fx_in_σ'').symm fx_is_vₓ, have h34: term.subst_env σ₁ fx = vₓ, from (term.subst_env.var.right vₓ).mp this, have fx_in_σ₁: fx ∈ σ₁, from env.contains_apply_equiv.right.mp (exists.intro vₓ this), have (σ'[f↦value.func f fx R S e' σ']) f = value.func f fx R S e' σ', from exists.elim (env.rest_verified σ''_verified) (λ_, env.apply_of_vcgen), have f_is_vf: (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) f = value.func f fx R S e' σ', from env.apply_of_rest_apply this, have f_in_σ'': f ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest env.contains.same, have h35a: σ₁ f = (value.func f fx R S e' σ'), from eq.trans (env_equiv3 f f_in_σ'').symm f_is_vf, have h35: term.subst_env σ₁ f = value.func f fx R S e' σ', from (term.subst_env.var.right (value.func f fx R S e' σ')).mp h35a, have f_in_σ₁: f ∈ σ₁, from env.contains_apply_equiv.right.mp (exists.intro (value.func f fx R S e' σ') h35a), have h36: σ₁ ⊨ (P'' ⋀ Q₂ (term.app f fx)).to_vc, from ( have h37: Q₂' (term.app f fx) = (P₁.to_propctx ⋀ y₁ ≣ •) (term.app f fx), from h44 ▸ rfl, have h38: (P₁.to_propctx ⋀ y₁ ≣ •) (term.app f fx) = (P₁ ⋀ (y₁ ≣ •) (term.app f fx)), from propctx_apply_pq, have ((y₁ ≣ •):propctx) (term.app f fx) = (y₁ ≡ term.app f fx), by { change (propctx.apply (propctx.term (y₁ ≣ •)) (term.app f fx) = (y₁ ≡ term.app f fx)), unfold propctx.apply, change (↑(termctx.apply (termctx.binop binop.eq y₁ •) (term.app f fx)) = ↑(y₁ ≡ term.app f fx)), unfold termctx.apply, change (↑((term.to_termctx y₁) (term.app ↑f ↑fx) ≡ term.app ↑f ↑fx) = ↑(↑y₁ ≡ term.app ↑f ↑fx)), rw[@unchanged_of_apply_termctx_without_hole y₁ (term.app f fx)] }, have h39: Q₂' (term.app f fx) = (P₁ ⋀ (y₁ ≡ term.app f fx)), from eq.trans h37 (this ▸ h38), have h40: σ₁ ⊨ y₁ ≡ term.app f fx, from ( have (R₁, σ₂[f₁↦value.func f₁ x₁ R₁ S₁ e₁ σ₂][x₁↦vx₁], e₁) ⟹* (R'₁, σ₁, exp.return y₁), from h65.symm ▸ h66.symm ▸ h67.symm ▸ h68.symm ▸ h69.symm ▸ h70.symm ▸ h71.symm ▸ steps, have h73: (σ₂[f₁↦value.func f₁ x₁ R₁ S₁ e₁ σ₂][x₁↦vx₁], e₁) ⟶* (σ₁, exp.return y₁), from step_of_dstep this, have ⊨ vy₁ ≡ term.app (value.func f₁ x₁ R₁ S₁ e₁ σ₂) vx₁, from valid.app h73 y_is_vy₁, have ⊨ vy₁ ≡ term.app (value.func f fx R S e' σ') vₓ, from h65 ▸ h66 ▸ h67 ▸ h68 ▸ h69 ▸ h70 ▸ h71 ▸ this, have h76: ⊨ (term.subst_env σ₁ y₁) ≡ term.app (term.subst_env σ₁ f) (term.subst_env σ₁ fx), from h26.symm ▸ h34.symm ▸ h35.symm ▸ this, have term.subst_env σ₁ (term.app f fx) = term.app (term.subst_env σ₁ f) (term.subst_env σ₁ fx), from term.subst_env.app, have h77: ⊨ term.subst_env σ₁ y₁ ≡ term.subst_env σ₁ (term.app f fx), from this.symm ▸ h76, have term.subst_env σ₁ (y₁ ≡ term.app f fx) = (term.subst_env σ₁ y₁ ≡ term.subst_env σ₁ (term.app f fx)), from term.subst_env.binop, have h78: ⊨ term.subst_env σ₁ (y₁ ≡ term.app f fx), from this.symm ▸ h77, have vc.subst_env σ₁ (y₁ ≡ term.app f fx) = vc.term (term.subst_env σ₁ (y₁ ≡ term.app f fx)), from vc.subst_env.term, have ⊨ vc.subst_env σ₁ (y₁ ≡ term.app f fx), from this.symm ▸ h78, show σ₁ ⊨ y₁ ≡ term.app f fx, from this ), have prop.to_vc (prop.term (y₁ ≡ term.app f fx)) = vc.term (y₁ ≡ term.app f fx), by unfold prop.to_vc, have h41: σ₁ ⊨ prop.to_vc (y₁ ≡ term.app f fx) , from this.symm ▸ h40, have h42b: closed_subst σ₁ (prop.term (y₁ ≡ term.app f fx)), from ( assume z: var, assume : free_in_prop z (y₁ ≡ term.app f fx), have free_in_term z (y₁ ≡ term.app f fx), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z y₁, have z = y₁, from free_in_term.var.inv this, have z ∈ σ₁, from this.symm ▸ y₁_in_σ₁, show z ∈ σ₁.dom, from this ) ( assume : free_in_term z (term.app f fx), or.elim (free_in_term.app.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ σ₁, from this.symm ▸ f_in_σ₁, show z ∈ σ₁.dom, from this ) ( assume : free_in_term z fx, have z = fx, from free_in_term.var.inv this, have z ∈ σ₁, from this.symm ▸ fx_in_σ₁, show z ∈ σ₁.dom, from this ) ) ), have closed_subst σ₁ (prop.term (y₁ ≡ term.app f fx)).to_vc, from to_vc_closed_subst_of_closed h42b, have σ₁ ⊨ prop.to_vc (y₁ ≡ term.app f fx), from h41, have σ₁ ⊨ (P₁ ⋀ y₁ ≡ term.app f fx).to_vc, from valid_env.to_vc_and h45b this, have h43: σ₁ ⊨ (Q₂' (term.app f fx)).to_vc, from h39.symm ▸ this, have σ₁ ⊨ vc.implies (Q₂' (term.app f fx)).to_vc (P'' ⋀ Q₂ (term.app f fx)).to_vc, from valid_env.mpr (Q₂'_dom σ₁ (term.app f fx)), show σ₁ ⊨ (P'' ⋀ Q₂ (term.app f fx)).to_vc, from valid_env.mp this h43 ), have ∃Q', (⊩ (σ'[f↦value.func f fx R S e' σ']) : Q') ∧ ∀σ', (σ' ⊨ vc.implies P''.to_vc Q'.to_vc), from env_implies_rest σ''_verified, let ⟨Q', h90⟩ := this in have ∃Q₁ Q₂ Q₃, f ∉ σ' ∧ f ∉ σ' ∧ fx ∉ σ' ∧ f ≠ fx ∧ (⊩ σ' : Q₁) ∧ (⊩ σ' : Q₂) ∧ fx ∈ FV R.to_prop.to_vc ∧ FV R.to_prop ⊆ FV Q₂ ∪ { f, fx } ∧ FV S.to_prop ⊆ FV Q₂ ∪ { f, fx } ∧ (Q₂ ⋀ spec.func f fx R S ⋀ R ⊩ e' : Q₃) ∧ ⦃prop.implies (Q₂ ⋀ spec.func f fx R S ⋀ R ⋀ Q₃ (term.app f fx)) S⦄ ∧ (Q' = (Q₁ ⋀ ((f ≡ (value.func f fx R S e' σ')) ⋀ prop.subst_env (σ'[f↦value.func f fx R S e' σ']) (prop.func f fx R (Q₃ (term.app f fx) ⋀ S))))), from env.dvcgen.func.inv h90.left, let ⟨QQ₁, QQ₂, QQ₃, ⟨f_not_in_σ', ⟨_, ⟨fx_not_in_σ', ⟨f_neq_fx, ⟨σ'_veri_QQ₁, ⟨σ'_veri_QQ₂, ⟨fx_in_R, ⟨fv_R, ⟨fv_S, ⟨e'_verified_QQ₃, ⟨func_vc, Q'_is⟩⟩⟩⟩⟩⟩⟩⟩⟩⟩⟩⟩ := this in have h91a: QQ₁ = QQ₂, from env.dvcgen.inj σ'_veri_QQ₁ QQ₂ σ'_veri_QQ₂, have h91b: QQ₁ = P', from env.dvcgen.inj σ'_veri_QQ₁ P' σ'_verified, have h91c: QQ₂ = P', from eq.trans h91a.symm h91b, have P' ⋀ spec.func f fx R S ⋀ R ⊩ e' : QQ₃, from h91c ▸ e'_verified_QQ₃, have h91d: QQ₃ = Q₂, from exp.dvcgen.inj this Q₂ e'_verified, have h37: closed_subst (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) (Q₂ (term.app f fx)), from ( assume z: var, assume : z ∈ FV (Q₂ (term.app f fx)), have z ∈ FV (term.app f fx) ∨ z ∈ FV (P' ⋀ ↑(spec.func ↑f fx R S) ⋀ ↑R), from exp.post_free e'_verified (term.app f fx) this, or.elim this ( assume : z ∈ FV (term.app f fx), or.elim (free_in_term.app.inv this) ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : free_in_term z fx, have z = fx, from free_in_term.var.inv this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from this.symm ▸ env.contains.same ) ) ( assume : z ∈ FV (P' ⋀ ↑(spec.func ↑f fx R S) ⋀ ↑R), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : free_in_prop z (↑(spec.func ↑f fx R S) ⋀ ↑R), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (spec.func ↑f fx R S), have h: free_in_prop z (spec.func ↑f fx R S).to_prop, from this, have spec.to_prop (spec.func f fx R S) = (prop.func f fx R.to_prop S.to_prop), by unfold spec.to_prop, have free_in_prop z (prop.func ↑f fx R S), from this ▸ h, have z ∈ FV (term.var f) ∨ (z ≠ fx ∧ (z ∈ FV R.to_prop ∨ z ∈ FV S.to_prop)), from free_in_prop.func.inv this, or.elim this ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ≠ fx ∧ (z ∈ FV R.to_prop ∨ z ∈ FV S.to_prop), have z_neq_fx: z ≠ fx, from this.left, or.elim this.right ( assume : z ∈ FV R.to_prop, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_R this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from absurd this z_neq_fx ) ) ) ( assume : z ∈ FV S.to_prop, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_S this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from absurd this z_neq_fx ) ) ) ) ) ( assume : free_in_prop z R, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_R this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from this.symm ▸ env.contains.same ) ) ) ) ) ), have σ₁ ⊨ (Q₂ (term.app f fx)).to_vc, from (valid_env.to_vc_and.elim h36).right, have h38: ⊨ vc.subst_env σ₁ (Q₂ (term.app f fx)).to_vc, from this, have closed_subst (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) (Q₂ (term.app f fx)).to_vc, from to_vc_closed_subst_of_closed h37, have (vc.subst_env (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) (Q₂ (term.app f fx)).to_vc = vc.subst_env σ₁ (Q₂ (term.app f fx)).to_vc), from vc.subst_env_equivalent_env env_equiv3 this, have h97d: ⊨ vc.subst_env (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) (Q₂ (term.app f fx)).to_vc, from this.symm ▸ h38, have R = R'₁, from pre_preserved steps, have σ₁ ⊨ R.to_prop.to_vc, from this.symm ▸ R'₁_valid, have h98b: σ₁ ⊨ ((P'' ⋀ Q₂ (term.app f fx)) ⋀ ↑R).to_vc, from valid_env.to_vc_and h36 this, have σ₁ ⊨ vc.implies ((P'' ⋀ Q₂ (term.app f fx)) ⋀ ↑R).to_vc (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc, from ( have hc1: σ₁ ⊨ vc.implies ((P'' ⋀ Q₂ (term.app f fx)) ⋀ ↑R).to_vc (↑R ⋀ P'' ⋀ Q₂ (term.app f fx)).to_vc, from vc.implies.and_symm, have hc2: σ₁ ⊨ vc.implies (↑R ⋀ P'' ⋀ Q₂ (term.app f fx)).to_vc (↑R ⋀ Q₂ (term.app f fx) ⋀ P' ⋀ ↑(spec.func f fx R S)).to_vc, from vc.implies.same_left ( assume _, have hc1: σ₁ ⊨ vc.implies (P'' ⋀ Q₂ (term.app f fx)).to_vc (Q₂ (term.app f fx) ⋀ P'').to_vc, from vc.implies.and_symm, have hc2: σ₁ ⊨ vc.implies (Q₂ (term.app f fx) ⋀ P'').to_vc (Q₂ (term.app f fx) ⋀ P' ⋀ spec.func f fx R S).to_vc, from vc.implies.same_left ( assume _, have hc1: σ₁ ⊨ vc.implies P''.to_vc Q'.to_vc, from h90.right σ₁, have hc2: σ₁ ⊨ vc.implies Q'.to_vc (P' ⋀ f ≡ (value.func f fx R S e' σ') ⋀ prop.subst_env (σ'[f↦value.func f fx R S e' σ']) (prop.func f fx R (Q₂ (term.app f fx) ⋀ S))).to_vc, from h91b ▸ h91d ▸ (@eq.subst prop (λa, σ₁ ⊨ vc.implies Q'.to_vc a.to_vc) Q' (QQ₁ ⋀ f ≡ (value.func f fx R S e' σ') ⋀ prop.subst_env (σ'[f↦value.func f fx R S e' σ']) (prop.func f fx R (QQ₃ (term.app f fx) ⋀ S))) Q'_is (@vc.implies.self σ₁ Q'.to_vc)), have hc3: σ₁ ⊨ vc.implies (P' ⋀ f ≡ (value.func f fx R S e' σ') ⋀ prop.subst_env (σ'[f↦value.func f fx R S e' σ']) (prop.func f fx R (Q₂ (term.app f fx) ⋀ S))).to_vc (P' ⋀ spec.func f fx R S).to_vc, from vc.implies.same_left (λP_valid, vc.implies.left_elim ( assume _, have ⊩ (σ'[f↦value.func f fx R S e' σ']) : (P' ⋀ f ≡ value.func f fx R S e' σ' ⋀ prop.subst_env (σ'[f↦value.func f fx R S e' σ']) (prop.func f fx R (Q₂ (term.app f fx) ⋀ S))), from h91b ▸ h91d ▸ Q'_is ▸ h90.left, show σ₁ ⊨ vc.implies (prop.subst_env (σ'[f↦value.func f fx R S e' σ']) (prop.func f fx R (Q₂ (term.app f fx) ⋀ S))).to_vc (spec.func f fx R S).to_prop.to_vc, from inlined_dominates_spec σ'_verified f_not_in_σ' fx_not_in_σ' f_neq_fx.symm P_valid h35a this )), show σ₁ ⊨ vc.implies P''.to_vc (P' ⋀ spec.func f fx R S).to_vc, from vc.implies.trans hc1 (vc.implies.trans hc2 hc3) ), show σ₁ ⊨ vc.implies (P'' ⋀ Q₂ (term.app f fx)).to_vc (Q₂ (term.app f fx) ⋀ P' ⋀ spec.func f fx R S).to_vc, from vc.implies.trans hc1 hc2 ), have hc3: σ₁ ⊨ vc.implies (↑R ⋀ Q₂ (term.app f fx) ⋀ P' ⋀ spec.func f fx R S).to_vc ((↑R ⋀ Q₂ (term.app f fx)) ⋀ P' ⋀ spec.func f fx R S).to_vc, from vc.implies.and_assoc, have hc4: σ₁ ⊨ vc.implies ((↑R ⋀ Q₂ (term.app f fx)) ⋀ P' ⋀ spec.func f fx R S).to_vc ((P' ⋀ spec.func f fx R S) ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc, from vc.implies.and_symm, have hc5: σ₁ ⊨ vc.implies ((P' ⋀ spec.func f fx R S) ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc, from vc.implies.and_assoc.symm, show σ₁ ⊨ vc.implies ((P'' ⋀ Q₂ (term.app f fx)) ⋀ ↑R).to_vc (P' ⋀ spec.func f fx R S ⋀ R ⋀ Q₂ (term.app f fx)).to_vc, from vc.implies.trans hc1 (vc.implies.trans hc2 (vc.implies.trans hc3 (vc.implies.trans hc4 hc5))) ), have h98b: σ₁ ⊨ (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc, from valid_env.mp this h98b, have h98c: closed_subst (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) (prop.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)) S), from ( assume z: var, assume : z ∈ FV (prop.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)) S), or.elim (free_in_prop.implies.inv this) ( assume : z ∈ FV (P' ⋀ ↑(spec.func ↑f fx R S) ⋀ ↑R ⋀ Q₂ (term.app f fx)), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : free_in_prop z (↑(spec.func ↑f fx R S) ⋀ ↑R ⋀ Q₂ (term.app f fx)), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (spec.func ↑f fx R S), have h: free_in_prop z (spec.func ↑f fx R S).to_prop, from this, have spec.to_prop (spec.func f fx R S) = (prop.func f fx R.to_prop S.to_prop), by unfold spec.to_prop, have free_in_prop z (prop.func ↑f fx R S), from this ▸ h, have z ∈ FV (term.var f) ∨ (z ≠ fx ∧ (z ∈ FV R.to_prop ∨ z ∈ FV S.to_prop)), from free_in_prop.func.inv this, or.elim this ( assume : free_in_term z f, have z = f, from free_in_term.var.inv this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ≠ fx ∧ (z ∈ FV R.to_prop ∨ z ∈ FV S.to_prop), have z_neq_fx: z ≠ fx, from this.left, or.elim this.right ( assume : z ∈ FV R.to_prop, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_R this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from absurd this z_neq_fx ) ) ) ( assume : z ∈ FV S.to_prop, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_S this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from absurd this z_neq_fx ) ) ) ) ) ( assume : z ∈ FV (↑R ⋀ Q₂ (term.app f fx)), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z R, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_R this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from this.symm ▸ env.contains.same ) ) ) ( assume : z ∈ FV (Q₂ (term.app f fx)), show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from h37 this ) ) ) ) ( assume : free_in_prop z S, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_S this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from this.symm ▸ env.contains.same ) ) ) ), have closed_subst σ₁ (prop.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)) S), from ( assume z: var, assume : z ∈ FV (prop.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)) S), have z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from h98c this, have z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]).dom, from this, show z ∈ σ₁.dom, from set.mem_of_subset_of_mem (env.dom_subset_of_equivalent_env env_equiv3) this ), -- have closed_subst σ₁ (prop.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)) S).to_vc, -- from to_vc_closed_subst_of_closed this, have h98a: σ₁ ⊨ (prop.implies (P' ⋀ ↑(spec.func f fx R S) ⋀ ↑R ⋀ Q₂ (term.app f fx)) S).to_vc, from h91c ▸ h91d ▸ func_vc σ₁ (h91c.symm ▸ h91d.symm ▸ this), have σ₁ ⊨ (prop.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)) S).to_vc, from h98a, -- valid_env.to_vc_of_instantiated_n this h98a, have σ₁ ⊨ ((P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).not ⋁ S.to_prop).to_vc, from this, have h98d: σ₁ ⊨ ((P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).not.to_vc ⋁ S.to_prop.to_vc), from valid_env.to_vc_or_elim this, have (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).not.to_vc = (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc.not, by unfold prop.to_vc, have σ₁ ⊨ ((P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc.not ⋁ S.to_prop.to_vc), from this ▸ h98d, have σ₁ ⊨ vc.implies (P' ⋀ spec.func f fx R S ⋀ ↑R ⋀ Q₂ (term.app f fx)).to_vc S.to_prop.to_vc, from this, have σ₁ ⊨ S.to_prop.to_vc, from valid_env.mp this h98b, have h98z: ⊨ vc.subst_env σ₁ S.to_prop.to_vc, from this, have closed_subst (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) S.to_prop, from ( assume z: var, assume : z ∈ FV S.to_prop, have z ∈ FV P' ∪ { f, fx }, from h91c ▸ set.mem_of_subset_of_mem fv_S this, or.elim (set.mem_or_mem_of_mem_union this) ( assume : z ∈ FV P', have z ∈ σ'.dom, from (free_iff_contains σ'_verified).symm ▸ this, have z ∈ σ', from this, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from env.contains.rest this, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z ∈ { f, fx }, or.elim (set.two_elems_mem this) ( assume : z = f, have z ∈ (σ'[f↦value.func f fx R S e' σ']), from this.symm ▸ env.contains.same, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from env.contains.rest this ) ( assume : z = fx, show z ∈ (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]), from this.symm ▸ env.contains.same ) ) ), have closed_subst (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) S.to_prop.to_vc, from to_vc_closed_subst_of_closed this, have (vc.subst_env (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) S.to_prop.to_vc = vc.subst_env σ₁ S.to_prop.to_vc), from vc.subst_env_equivalent_env env_equiv3 this, have h98d: ⊨ vc.subst_env (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) S.to_prop.to_vc, from this.symm ▸ h98z, have ⊨ vc.subst_env (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ]) (vc.and (Q₂ (term.app f fx)).to_vc S.to_prop.to_vc), from valid_env.and h97d h98d, have (σ'[f↦value.func f fx R S e' σ'][fx↦vₓ] ⊨ (Q₂ (term.app f fx)).to_vc ⋀ S.to_prop.to_vc), from this, have ⊨ vc.post (value.func f fx R S e' σ') vₓ, from valid.post.mp σ'_verified e'_verified this, have ⊨ vc.post (value.func f₁ x₁ R₁ S₁ e₁ σ₂) vx₁, from h65.symm ▸ h66.symm ▸ h67.symm ▸ h68.symm ▸ h69.symm ▸ h70.symm ▸ h71.symm ▸ this, have h56: ⊨ vc.post (term.subst_env σ₃ g) (term.subst_env σ₃ x), from h23.symm ▸ h24.symm ▸ h25.symm ▸ this, have vc.subst_env σ₃ (vc.post g x) = vc.post (term.subst_env σ₃ g) (term.subst_env σ₃ x), from vc.subst_env.post, have ⊨ vc.subst_env σ₃ (vc.post g x), from this.symm ▸ h56, show σ₃ ⊨ vc.post g x, from this ), have h79: σ₃ ⊨ y ≡ term.app g x, from ( have (R₁, σ₂[f₁↦value.func f₁ x₁ R₁ S₁ e₁ σ₂][x₁↦vx₁], e₁) ⟹* (R'₁, σ₁, exp.return y₁), from h65.symm ▸ h66.symm ▸ h67.symm ▸ h68.symm ▸ h69.symm ▸ h70.symm ▸ h71.symm ▸ steps, have h73: (σ₂[f₁↦value.func f₁ x₁ R₁ S₁ e₁ σ₂][x₁↦vx₁], e₁) ⟶* (σ₁, exp.return y₁), from step_of_dstep this, have ⊨ vy₁ ≡ term.app (value.func f₁ x₁ R₁ S₁ e₁ σ₂) vx₁, from valid.app h73 y_is_vy₁, have h76: ⊨ (term.subst_env σ₃ y) ≡ term.app (term.subst_env σ₃ g) (term.subst_env σ₃ x), from h23.symm ▸ h24.symm ▸ h25.symm ▸ this, have term.subst_env σ₃ (term.app g x) = term.app (term.subst_env σ₃ g) (term.subst_env σ₃ x), from term.subst_env.app, have h77: ⊨ term.subst_env σ₃ y ≡ term.subst_env σ₃ (term.app g x), from this.symm ▸ h76, have term.subst_env σ₃ (y ≡ term.app g x) = (term.subst_env σ₃ y ≡ term.subst_env σ₃ (term.app g x)), from term.subst_env.binop, have h78: ⊨ term.subst_env σ₃ (y ≡ term.app g x), from this.symm ▸ h77, have vc.subst_env σ₃ (y ≡ term.app g x) = vc.term (term.subst_env σ₃ (y ≡ term.app g x)), from vc.subst_env.term, have ⊨ vc.subst_env σ₃ (y ≡ term.app g x), from this.symm ▸ h78, show σ₃ ⊨ y ≡ term.app g x, from this ), show σ₃ ⊨ vc.post g x ⋀ y ≡ term.app g x, from valid_env.and h49 h79 ), have h10p: ∀σ₃, σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from λσ₃, vc.implies.same_left ( assume P_P₃_valid: σ₃ ⊨ (P ⋀ P₃).to_vc, have P_valid: σ₃ ⊨ P.to_vc, from (valid_env.to_vc_and.elim P_P₃_valid).left, have env_equiv: (∀z, z ∈ σ → (σ z = σ₃ z)), from env_equiv_of_translation_valid σ_verified σ₃ P_valid, have env_equiv2: (∀z, z ∈ (σ[y↦vy₁]) → ((σ[y↦vy₁]) z = σ₃ z)), from env_equiv_of_translation_valid h5 σ₃ P_P₃_valid, have h_impl: (σ₃ ⊨ (prop.call vx₁).to_vc) → (σ₃ ⊨ (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc), from ( assume : σ₃ ⊨ (prop.call vx₁).to_vc, have h49: σ₃ ⊨ vc.post g x ⋀ y ≡ term.app g x, from h10 σ₃ P_P₃_valid, have prop.to_vc (prop.post g x) = vc.post g x, by unfold prop.to_vc, have h50: σ₃ ⊨ (prop.post g x).to_vc ⋀ y ≡ term.app g x, from this.symm ▸ h49, have prop.to_vc (prop.term (y ≡ term.app g x)) = vc.term (y ≡ term.app g x), by unfold prop.to_vc, have h80: σ₃ ⊨ (prop.post g x).to_vc ⋀ prop.to_vc (y ≡ term.app g x), from this.symm ▸ h50, have prop.to_vc (prop.and (prop.post g x) (y ≡ term.app g x)) = ((prop.post g x).to_vc ⋀ prop.to_vc (y ≡ term.app g x)), by unfold prop.to_vc, have h81: σ₃ ⊨ (prop.post g x ⋀ y ≡ term.app g x).to_vc, from this.symm ▸ h80, have prop.to_vc (prop.call x) = vc.term value.true, by unfold prop.to_vc, have h82: σ₃ ⊨ prop.to_vc (prop.call x), from this.symm ▸ valid_env.true, have h83: σ₃ ⊨ (prop.call x).to_vc ⋀ (prop.post g x ⋀ y ≡ term.app g x).to_vc, from valid_env.and h82 h81, have prop.to_vc (prop.and (prop.call x) (prop.post g x ⋀ y ≡ term.app g x)) = ((prop.call x).to_vc ⋀ (prop.post g x ⋀ y ≡ term.app g x).to_vc), by unfold prop.to_vc, show σ₃ ⊨ (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from this.symm ▸ h83 ), show σ₃ ⊨ vc.implies (prop.call vx₁).to_vc (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from valid_env.mpr h_impl ), have h10n: ∀σ₃, σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from λσ₃, vc.implies.same_left ( assume P_P₃_valid: σ₃ ⊨ (P ⋀ P₃).to_vc, have P_valid: σ₃ ⊨ P.to_vc, from (valid_env.to_vc_and.elim P_P₃_valid).left, have env_equiv: (∀z, z ∈ σ → (σ z = σ₃ z)), from env_equiv_of_translation_valid σ_verified σ₃ P_valid, have env_equiv2: (∀z, z ∈ (σ[y↦vy₁]) → ((σ[y↦vy₁]) z = σ₃ z)), from env_equiv_of_translation_valid h5 σ₃ P_P₃_valid, have h_impl: (σ₃ ⊨ (prop.call vx₁).to_vc) → (σ₃ ⊨ (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc), from ( assume : σ₃ ⊨ (prop.call vx₁).to_vc, have h49: σ₃ ⊨ vc.post g x ⋀ y ≡ term.app g x, from h10 σ₃ P_P₃_valid, have prop.to_vc (prop.post g x) = vc.post g x, by unfold prop.to_vc, have h50: σ₃ ⊨ (prop.post g x).to_vc ⋀ y ≡ term.app g x, from this.symm ▸ h49, have prop.to_vc (prop.term (y ≡ term.app g x)) = vc.term (y ≡ term.app g x), by unfold prop.to_vc, have h80: σ₃ ⊨ (prop.post g x).to_vc ⋀ prop.to_vc (y ≡ term.app g x), from this.symm ▸ h50, have prop.to_vc (prop.and (prop.post g x) (y ≡ term.app g x)) = ((prop.post g x).to_vc ⋀ prop.to_vc (y ≡ term.app g x)), by unfold prop.to_vc, have h81: σ₃ ⊨ (prop.post g x ⋀ y ≡ term.app g x).to_vc, from this.symm ▸ h80, have prop.to_vc (prop.call x) = vc.term value.true, by unfold prop.to_vc, have h82: σ₃ ⊨ prop.to_vc (prop.call x), from this.symm ▸ valid_env.true, have h83: σ₃ ⊨ (prop.call x).to_vc ⋀ (prop.post g x ⋀ y ≡ term.app g x).to_vc, from valid_env.and h82 h81, have prop.to_vc (prop.and (prop.call x) (prop.post g x ⋀ y ≡ term.app g x)) = ((prop.call x).to_vc ⋀ (prop.post g x ⋀ y ≡ term.app g x).to_vc), by unfold prop.to_vc, show σ₃ ⊨ (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from this.symm ▸ h83 ), show σ₃ ⊨ vc.implies (prop.call vx₁).to_vc (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from valid_env.mpr h_impl ), have h11: ∀σ, σ ⊨ vc.implies (↑R' ⋀ P ⋀ P₃).to_vc (↑R' ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from ( assume σ₃: env, vc.implies.same_left ( assume : σ₃ ⊨ R'.to_prop.to_vc, have h17: σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ P ⋀ P₃).to_vc ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc, from vc.implies.and_symm, have h18: σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from h10p σ₃, have h19: σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc ((P₃ ⋀ P) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from vc.implies.same_right (λ_, vc.implies.and_symm), have h20: σ₃ ⊨ vc.implies ((P₃ ⋀ P) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc (P₃ ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from vc.implies.and_assoc.symm, have σ₃ ⊨ vc.implies (P₃ ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc ((P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x) ⋀ P₃).to_vc, from vc.implies.and_symm, have h21: σ₃ ⊨ vc.implies (P₃ ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from vc.implies.and_elim_left this, have h16: σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ P ⋀ P₃).to_vc (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from vc.implies.trans h17 (vc.implies.trans h18 (vc.implies.trans h19 (vc.implies.trans h20 h21))), have h13: σ₃ ⊨ vc.implies (P ⋀ P₃).to_vc (prop.call vx₁ ⋀ P ⋀ P₃).to_vc, by begin apply valid_env.mpr, assume h13a, apply valid_env.to_vc_and, unfold prop.to_vc, from valid_env.true, from h13a end, show σ₃ ⊨ vc.implies (P ⋀ P₃).to_vc (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from vc.implies.trans h13 h16 ) ), have h12: R' ⋀ P ⋀ P₃ ⊩ e : Q₃, from strengthen_exp cont (R' ⋀ P ⋀ P₃) h9 h11, have h13: ⊩ₛ (R', σ[y↦vy₁], e) : ↑(P ⋀ P₃) ⋀ Q₃, from stack.dvcgen.top h5 h6 h8 h12, have h14: ∀σ₃ t, σ₃ ⊨ vc.implies ((↑(P ⋀ P₃) ⋀ Q₃) t).to_vc ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t).to_vc, from ( assume σ₃: env, assume t: term, have σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from h10n σ₃, have σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc (((P ⋀ P₃) ⋀ prop.call x) ⋀ prop.post g x ⋀ y ≡ term.app g x).to_vc, from vc.implies.trans this vc.implies.and_assoc, have σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁).to_vc ((((P ⋀ P₃) ⋀ prop.call x) ⋀ prop.post g x) ⋀ y ≡ term.app g x).to_vc, from vc.implies.trans this vc.implies.and_assoc, have σ₃ ⊨ vc.implies (((P ⋀ P₃) ⋀ prop.call vx₁) ⋀ Q₃ t).to_vc (((((P ⋀ P₃) ⋀ prop.call x) ⋀ prop.post g x) ⋀ y ≡ term.app g x) ⋀ Q₃ t).to_vc, from vc.implies.same_right (λ_, this), have σ₃ ⊨ vc.implies (((P ⋀ P₃) ⋀ prop.call vx₁) ⋀ Q₃ t).to_vc ((((P ⋀ P₃) ⋀ prop.call x) ⋀ prop.post g x) ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans this vc.implies.and_assoc.symm, have σ₃ ⊨ vc.implies (((P ⋀ P₃) ⋀ prop.call vx₁) ⋀ Q₃ t).to_vc (((P ⋀ P₃) ⋀ prop.call x) ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans this vc.implies.and_assoc.symm, have σ₃ ⊨ vc.implies (((P ⋀ P₃) ⋀ prop.call vx₁) ⋀ Q₃ t).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans this vc.implies.and_assoc.symm, have σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ prop.call vx₁ ⋀ Q₃ t).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans vc.implies.and_assoc this, have σ₃ ⊨ vc.implies ((P ⋀ P₃) ⋀ Q₃ t ⋀ prop.call vx₁).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans (vc.implies.same_left (λ_, vc.implies.and_symm)) this, have σ₃ ⊨ vc.implies (((P ⋀ P₃) ⋀ Q₃ t) ⋀ prop.call vx₁).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans vc.implies.and_assoc.symm this, have σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc ((P ⋀ P₃) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans vc.implies.and_symm this, have σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc ((P₃ ⋀ P) ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans this (vc.implies.same_right (λ_, vc.implies.and_symm)), have σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc (P₃ ⋀ P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.trans this vc.implies.and_assoc.symm, have h17: σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc, from vc.implies.and_elim_right this, have σ₃ ⊨ vc.implies (P ⋀ prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t).to_vc (P ⋀ prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t)).to_vc, from vc.implies.same_left (λ_, vc.implies.exis), have h20: σ₃ ⊨ vc.implies (prop.call vx₁ ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc (P ⋀ prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t)).to_vc, from vc.implies.trans h17 this, have h21a: (prop.call x).to_propctx t = prop.call x, from unchanged_of_apply_propctx_without_hole, have h21b: (prop.post g x).to_propctx t = prop.post g x, from unchanged_of_apply_propctx_without_hole, have h21c: (prop.term (y ≡ term.app g x)).to_propctx t = prop.term (y ≡ term.app g x), from unchanged_of_apply_propctx_without_hole, have (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t = prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t), by calc (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t = propctx.apply (propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) t : rfl ... = prop.exis y (propctx.apply (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : by unfold propctx.apply ... = prop.exis y (propctx.apply (propctx.and ↑(prop.call x) (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) t) : rfl ... = prop.exis y (propctx.apply ↑(prop.call x) t ⋀ propctx.apply (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : by unfold propctx.apply ... = prop.exis y ((prop.call x).to_propctx t ⋀ propctx.apply (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : rfl ... = prop.exis y (prop.call x ⋀ propctx.apply (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : by rw[h21a] ... = prop.exis y (prop.call x ⋀ propctx.apply (propctx.and ↑(prop.post g x) (↑(y ≡ term.app g x) ⋀ Q₃)) t) : rfl ... = prop.exis y (prop.call x ⋀ propctx.apply ↑(prop.post g x) t ⋀ propctx.apply (↑(y ≡ term.app g x) ⋀ Q₃) t) : by unfold propctx.apply ... = prop.exis y (prop.call x ⋀ (prop.post g x).to_propctx t ⋀ propctx.apply (↑(y ≡ term.app g x) ⋀ Q₃) t) : rfl ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ propctx.apply (↑(y ≡ term.app g x) ⋀ Q₃) t) : by rw[h21b] ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ propctx.apply (propctx.and ↑(prop.term (y ≡ term.app g x)) Q₃) t) : rfl ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ propctx.apply ↑(prop.term (y ≡ term.app g x)) t ⋀ propctx.apply Q₃ t) : by unfold propctx.apply ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ (prop.term (y ≡ term.app g x)).to_propctx t ⋀ propctx.apply Q₃ t) : rfl ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ prop.term (y ≡ term.app g x) ⋀ propctx.apply Q₃ t) : by rw[h21c], have h21: σ₃ ⊨ vc.implies (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc (P⋀ (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t).to_vc, from this.symm ▸ h20, have ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) = (P⋀ (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t), from propctx_apply_pq, have h22: σ₃ ⊨ vc.implies (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t).to_vc ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t).to_vc, from this.symm ▸ h21, have ((↑((prop.call vx₁)) ⋀ ↑(P ⋀ P₃) ⋀ Q₃) t) = (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from propctx_apply_hpq, have h23: σ₃ ⊨ vc.implies ((↑((prop.call vx₁)) ⋀ ↑(P ⋀ P₃) ⋀ Q₃) t).to_vc ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t).to_vc, from this.symm ▸ h22, have h24: σ₃ ⊨ vc.implies ((↑(P ⋀ P₃) ⋀ Q₃) t).to_vc ((↑((prop.call vx₁)) ⋀ ↑(P ⋀ P₃) ⋀ Q₃) t).to_vc, by begin apply valid_env.mpr, assume h13a, apply valid_env.to_vc_and, unfold prop.to_vc, from valid_env.true, from h13a end, show σ₃ ⊨ vc.implies ((↑(P ⋀ P₃) ⋀ Q₃) t).to_vc ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t).to_vc, from vc.implies.trans h24 h23 ), have h15: ∀t, FV ((↑P ⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) ⊆ FV ((↑(P ⋀ P₃) ⋀ Q₃) t), from ( assume t: term, have h18: FV (P ⋀ prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t)) ⊆ σ.dom ∪ FV (Q₃ t), from ( assume z: var, assume : z ∈ FV (P ⋀ prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t)), or.elim (free_in_prop.and.inv this) ( assume h10: z ∈ FV P, have σ.dom = FV P, from free_iff_contains σ_verified, have z ∈ σ.dom, from this.symm ▸ h10, show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_left (FV (Q₃ t)) this ) ( assume : z ∈ FV (prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t)), have z_neq_y: z ≠ y, from (free_in_prop.exis.inv this).left, have z ∈ FV (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t), from (free_in_prop.exis.inv this).right, or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (prop.call x), have free_in_term z x, from free_in_prop.call.inv this, have z = x, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ x_in_σ, show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_left (FV (Q₃ t)) this ) ( assume : z ∈ FV (prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t), or.elim (free_in_prop.and.inv this) ( assume : z ∈ FV (prop.post g x), or.elim (free_in_prop.post.inv this) ( assume : free_in_term z g, have z = g, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ g_in_σ, show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_left (FV (Q₃ t)) this ) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ x_in_σ, show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_left (FV (Q₃ t)) this ) ) ( assume : free_in_prop z (y ≡ term.app g x ⋀ Q₃ t), or.elim (free_in_prop.and.inv this) ( assume : free_in_prop z (y ≡ term.app g x), have free_in_term z (y ≡ term.app g x), from free_in_prop.term.inv this, or.elim (free_in_term.binop.inv this) ( assume : free_in_term z y, have z = y, from free_in_term.var.inv this, show z ∈ σ.dom ∪ FV (Q₃ t), from absurd this z_neq_y ) ( assume : free_in_term z (term.app g x), or.elim (free_in_term.app.inv this) ( assume : free_in_term z g, have z = g, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ g_in_σ, show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_left (FV (Q₃ t)) this ) ( assume : free_in_term z x, have z = x, from free_in_term.var.inv this, have z ∈ σ, from this.symm ▸ x_in_σ, show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_left (FV (Q₃ t)) this ) ) ) ( assume : z ∈ FV (Q₃ t), show z ∈ σ.dom ∪ FV (Q₃ t), from set.mem_union_right σ.dom this ) ) ) ) ), have h19: σ.dom ∪ FV (Q₃ t) ⊆ FV (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from ( assume z: var, assume : z ∈ σ.dom ∪ FV (Q₃ t), or.elim (set.mem_or_mem_of_mem_union this) ( assume h10: z ∈ σ.dom, have σ.dom = FV P, from free_iff_contains σ_verified, have z ∈ FV P, from this ▸ h10, have z ∈ FV (P ⋀ P₃), from free_in_prop.and₁ this, have z ∈ FV ((P ⋀ P₃) ⋀ Q₃ t), from free_in_prop.and₁ this, show z ∈ FV (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from free_in_prop.and₂ this ) ( assume : z ∈ FV (Q₃ t), have z ∈ FV ((P ⋀ P₃) ⋀ Q₃ t), from free_in_prop.and₂ this, show z ∈ FV (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from free_in_prop.and₂ this ) ), have h20: FV (P ⋀ prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t)) ⊆ FV (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from set.subset.trans h18 h19, have h21a: (prop.call x).to_propctx t = prop.call x, from unchanged_of_apply_propctx_without_hole, have h21b: (prop.post g x).to_propctx t = prop.post g x, from unchanged_of_apply_propctx_without_hole, have h21c: (prop.term (y ≡ term.app g x)).to_propctx t = prop.term (y ≡ term.app g x), from unchanged_of_apply_propctx_without_hole, have (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t = prop.exis y (prop.call x ⋀ prop.post g x ⋀ y ≡ term.app g x ⋀ Q₃ t), by calc (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t = propctx.apply (propctx.exis y (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) t : rfl ... = prop.exis y (propctx.apply (↑(prop.call x) ⋀ ↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : by unfold propctx.apply ... = prop.exis y (propctx.apply (propctx.and ↑(prop.call x) (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃)) t) : rfl ... = prop.exis y (propctx.apply ↑(prop.call x) t ⋀ propctx.apply (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : by unfold propctx.apply ... = prop.exis y ((prop.call x).to_propctx t ⋀ propctx.apply (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : rfl ... = prop.exis y (prop.call x ⋀ propctx.apply (↑(prop.post g x) ⋀ ↑(y ≡ term.app g x) ⋀ Q₃) t) : by rw[h21a] ... = prop.exis y (prop.call x ⋀ propctx.apply (propctx.and ↑(prop.post g x) (↑(y ≡ term.app g x) ⋀ Q₃)) t) : rfl ... = prop.exis y (prop.call x ⋀ propctx.apply ↑(prop.post g x) t ⋀ propctx.apply (↑(y ≡ term.app g x) ⋀ Q₃) t) : by unfold propctx.apply ... = prop.exis y (prop.call x ⋀ (prop.post g x).to_propctx t ⋀ propctx.apply (↑(y ≡ term.app g x) ⋀ Q₃) t) : rfl ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ propctx.apply (↑(y ≡ term.app g x) ⋀ Q₃) t) : by rw[h21b] ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ propctx.apply (propctx.and ↑(prop.term (y ≡ term.app g x)) Q₃) t) : rfl ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ propctx.apply ↑(prop.term (y ≡ term.app g x)) t ⋀ propctx.apply Q₃ t) : by unfold propctx.apply ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ (prop.term (y ≡ term.app g x)).to_propctx t ⋀ propctx.apply Q₃ t) : rfl ... = prop.exis y (prop.call x ⋀ prop.post g x ⋀ prop.term (y ≡ term.app g x) ⋀ propctx.apply Q₃ t) : by rw[h21c], have h21: FV (P⋀ (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) ⊆ FV (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from this.symm ▸ h20, have ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) = (P⋀ (propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t), from propctx_apply_pq, have h22: FV ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) ⊆ FV (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from this.symm ▸ h21, have ((↑((prop.call vx₁)) ⋀ ↑(P ⋀ P₃) ⋀ Q₃) t) = (((prop.call vx₁)) ⋀ (P ⋀ P₃) ⋀ Q₃ t), from propctx_apply_hpq, have h23: FV ((↑P ⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) ⊆ FV ((↑((prop.call vx₁)) ⋀ ↑(P ⋀ P₃) ⋀ Q₃) t), from this.symm ▸ h22, have h24: FV ((↑((prop.call vx₁)) ⋀ ↑(P ⋀ P₃) ⋀ Q₃) t) ⊆ FV ((↑(P ⋀ P₃) ⋀ Q₃) t), by begin assume z, assume h25, have h26: z ∈ FV (propctx.apply (propctx.and ↑(prop.call ↑vx₁) (↑(P⋀P₃) ⋀ Q₃)) t), from h25, unfold propctx.apply at h26, cases (free_in_prop.and.inv h26) with h27 h28, have h28: free_in_prop z ((prop.call ↑vx₁).to_propctx t), from h27, have h29: ((prop.call ↑vx₁).to_propctx t = (prop.call ↑vx₁)), from unchanged_of_apply_propctx_without_hole, rw[h29] at h28, have h30, from free_in_prop.call.inv h28, have h31: ¬ free_in_term z ↑vx₁, from free_in_term.value.inv, contradiction, from h28 end, show FV ((↑P⋀ propctx.exis y (↑(prop.call ↑x) ⋀ ↑(prop.post ↑g ↑x) ⋀ ↑(↑y ≡ term.app ↑g ↑x) ⋀ Q₃)) t) ⊆ FV ((↑(P ⋀ P₃) ⋀ Q₃) t), from set.subset.trans h23 h24 ), exists.intro (↑(P ⋀ P₃) ⋀ Q₃) ⟨h13, ⟨h14, λv, h15 v⟩⟩ } } end
3f654ef38808af609937edbee5d4e8d8b7086832
9dd3f3912f7321eb58ee9aa8f21778ad6221f87c
/tests/lean/run/ind7.lean
55414098d38c135c028fe5b8895733a65d227ae9
[ "Apache-2.0" ]
permissive
bre7k30/lean
de893411bcfa7b3c5572e61b9e1c52951b310aa4
5a924699d076dab1bd5af23a8f910b433e598d7a
refs/heads/master
1,610,900,145,817
1,488,006,845,000
1,488,006,845,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
198
lean
namespace list inductive {u} list (A : Type u) : Type u | nil : list | cons : A → list → list check list.{1} check list.cons.{1} check list.rec.{1 1} end list check list.list.{1}