Lean / Reader
Lean readerWallisTriples.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports WallisTypes, WallisConvolution, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import WallisTypesimport WallisConvolutionimport Mathlib.Data.Nat.Choose.Sumimport Mathlib.Data.Finset.Sortimport Mathlib.Data.Finset.Sumimport Mathlib.Order.Fin.Basicimport Mathlib.Tacticopen Finsetnamespace WallisGapabbrev Disc (n m : ℕ) := {t : Triple n m // DisjointTriple t}private def supp {n m : ℕ} (t : Triple n m) := insert t.2.2 (t.1.val ∪ t.2.1.val)private theorem supp_card {n m : ℕ} (t : Disc n m) : (supp t.val).card = 2*m+1 := byobtain ⟨h,h₁,h₂⟩ := t.propertyhave hm := t.val.1.propertyhave hn := t.val.2.1.propertyhave hu : t.val.2.2 ∉ t.val.1.val ∪ t.val.2.1.val := by simp [h₁,h₂]rw [supp, Finset.card_insert_of_notMem hu, card_union_of_disjoint h, hm, hn]omegaprivate theorem fullSupp {m : ℕ} (t : Disc (2*m+1) m) : supp t.val = univ :=Finset.eq_univ_of_card _ (by simpa using supp_card t)private def transport {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) : Triple n m :=(⟨t.1.val.map e.toEmbedding, by simpa using t.1.property⟩,⟨t.2.1.val.map e.toEmbedding, by simpa using t.2.1.property⟩, e t.2.2)private theorem transport_disc {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) :DisjointTriple (transport e t) ↔ DisjointTriple t := bysimp [DisjointTriple, transport, Finset.disjoint_map]private theorem transport_rank {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) :MatchingRank (transport e t) ↔ MatchingRank t := bysimp [MatchingRank, transport, Finset.filter_map, Function.comp_def]private theorem transport_supp {d n m : ℕ} (e : Fin d ↪o Fin n) (t : Triple d m) :supp (transport e t) = (supp t).map e.toEmbedding := bysimp [supp, transport, map_union]private theorem transport_inj {d n m : ℕ} (e : Fin d ↪o Fin n) :Function.Injective (transport (m := m) e) := byrintro ⟨⟨a,ha⟩,⟨b,hb⟩,u⟩ ⟨⟨c,hc⟩,⟨d,hd⟩,v⟩ hsimpa [transport, Prod.mk.injEq, Subtype.mk.injEq] using hprivate def supportLift {n m : ℕ} (x : SubsetOfSize n (2*m+1) × Disc (2*m+1) m) :Disc n m :=⟨transport (x.1.val.orderEmbOfFin x.1.property) x.2.val,(transport_disc ..).mpr x.2.property⟩private theorem recoverSupp {n m : ℕ} (C : SubsetOfSize n (2*m+1)) (t : Disc (2*m+1) m) :supp (supportLift (C,t)).val = C.val := bychange supp (transport _ _) = _rw [transport_supp, fullSupp, map_orderEmbOfFin_univ]private theorem lift_inj (n m : ℕ) : Function.Injective (@supportLift n m) := byrintro ⟨C,t⟩ ⟨D,s⟩ hhave hCD : C = D := Subtype.ext <|(recoverSupp C t).symm.trans ((congrArg (fun x => supp (Subtype.val x)) h).trans (recoverSupp D s))subst Dcongr 1exact Subtype.ext (transport_inj (C.val.orderEmbOfFin C.property) (congrArg Subtype.val h))private theorem realizes {α β : Type*} [DecidableEq β] [Fintype α](e : α ↪ β) (s : Finset β) (h : s ⊆ univ.map e) :(univ.filter (fun i => e i ∈ s)).map e = s := byext xconstructor· intro hxobtain ⟨a,ha,rfl⟩ := mem_map.mp hxexact (mem_filter.mp ha).2· intro hxobtain ⟨a,ha,rfl⟩ := mem_map.mp (h hx)exact mem_map.mpr ⟨a,by simp [hx],rfl⟩private theorem lift_surj (n m : ℕ) : Function.Surjective (@supportLift n m) := byrintro ⟨t,ht⟩let C : SubsetOfSize n (2*m+1) := ⟨supp t,supp_card ⟨t,ht⟩⟩let e := C.val.orderEmbOfFin C.propertyhave hall : univ.map e.toEmbedding = supp t := map_orderEmbOfFin_univ ..have ha : t.1.val ⊆ univ.map e.toEmbedding := byrw [hall]; exact fun x hx => mem_insert_of_mem (mem_union_left _ hx)have hb : t.2.1.val ⊆ univ.map e.toEmbedding := byrw [hall]; exact fun x hx => mem_insert_of_mem (mem_union_right _ hx)have hu : t.2.2 ∈ univ.map e.toEmbedding := by rw [hall]; exact mem_insert_self ..obtain ⟨u,_,hu⟩ := mem_map.mp huhave hA := realizes e.toEmbedding _ hahave hB := realizes e.toEmbedding _ hblet s : Triple (2*m+1) m :=(⟨_,by simpa using (congrArg Finset.card hA).trans t.1.property⟩,⟨_,by simpa using (congrArg Finset.card hB).trans t.2.1.property⟩,u)have h : transport e s = t := byexact Prod.ext (Subtype.ext hA) (Prod.ext (Subtype.ext hB) hu)have hs : DisjointTriple s := (transport_disc e s).mp (h ▸ ht)exact ⟨(C,⟨s,hs⟩),Subtype.ext h⟩private noncomputable def supportEquiv (n m : ℕ) :SubsetOfSize n (2*m+1) × Disc (2*m+1) m ≃ Disc n m :=Equiv.ofBijective supportLift ⟨lift_inj n m, lift_surj n m⟩private def removeMark {m : ℕ} (w : MarkedWord m) : Triple (2*m+1) m :=(⟨w.2.val.map w.1.succAboveOrderEmb.toEmbedding, by simpa using w.2.property⟩,⟨w.2.valᶜ.map w.1.succAboveOrderEmb.toEmbedding, bysimp [Finset.card_compl, w.2.property]; omega⟩, w.1)private theorem removeMark_disc {m : ℕ} (w : MarkedWord m) :DisjointTriple (removeMark w) := bysimp [removeMark, DisjointTriple, disjoint_map, Finset.mem_map, Fin.succAbove_ne, disjoint_compl_right]private def markedLift {m : ℕ} (w : MarkedWord m) : Disc (2*m+1) m :=⟨removeMark w, removeMark_disc w⟩private theorem markedLift_inj (m : ℕ) : Function.Injective (@markedLift m) := byrintro ⟨u,a⟩ ⟨v,b⟩ hhave huv : u = v := congrArg (fun x => x.val.2.2) hsubst vhave hAB : a.val.map u.succAboveOrderEmb.toEmbedding = b.val.map _ :=congrArg (fun x => x.val.1.val) hexact Prod.ext rfl (Subtype.ext (Finset.map_inj.mp hAB))private theorem markedLift_surj (m : ℕ) : Function.Surjective (@markedLift m) := byrintro ⟨⟨A,B,u⟩,hd⟩obtain ⟨hab,hua,hub⟩ := hdlet e := u.succAboveOrderEmb.toEmbeddinghave hImage : univ.map e = ({u} : Finset _)ᶜ := byext xsimp [e, mem_map, Fin.exists_succAbove_eq_iff]have ha : A.val ⊆ univ.map e := byrw [hImage]; intro x hx; simp only [mem_compl, mem_singleton]; exact fun h => hua (h ▸ hx)have hA := realizes e _ halet w : BalancedWord m := ⟨_,by simpa using (congrArg Finset.card hA).trans A.property⟩have hB : w.valᶜ.map e = B.val := byapply Finset.eq_of_subset_of_card_le· intro x hxby_contra hnobtain ⟨i,hi,rfl⟩ := mem_map.mp hxhave hiA : e i ∉ A.val := by rw [← hA]; simpa [w] using hihave hmem := fullSupp (m := m) ⟨(A,B,u),⟨hab,hua,hub⟩⟩have hx : e i ∈ supp (A,B,u) := hmem ▸ mem_univ _have hne : e i ≠ u := Fin.succAbove_ne u isimp only [supp,mem_insert,mem_union,hne,hiA,hn,or_self] at hx· simp [card_compl,w.property,B.property]; omegaexact ⟨(u,w),Subtype.ext (Prod.ext (Subtype.ext hA) (Prod.ext (Subtype.ext hB) rfl))⟩private noncomputable def markedEquiv (m : ℕ) : MarkedWord m ≃ Disc (2*m+1) m :=Equiv.ofBijective markedLift ⟨markedLift_inj m, markedLift_surj m⟩private def pre {N : ℕ} (u : Fin (N+1)) (s : Finset (Fin N)) :=s.filter fun i => i.val < u.valprivate theorem matching_mark {m : ℕ} (w : MarkedWord m) :MatchingRank (removeMark w) ↔ (pre w.1 w.2.val).card = (pre w.1 w.2.valᶜ).card := byhave h (i : Fin (2*m)) : w.1.succAboveOrderEmb i < w.1 ↔ i.val < w.1.val :=Fin.succAbove_lt_iff_castSucc_lt w.1 isimp only [MatchingRank, removeMark, pre, Finset.filter_map, Function.comp_def,RelEmbedding.coe_toEmbedding, h, Finset.card_map]private theorem all_pre {N : ℕ} (u : Fin (N+1)) : (pre u univ).card = u.val := bylet e := Fin.castLEEmb (show u.val ≤ N by omega)have h : pre u univ = univ.map e := byext isimp only [pre,mem_filter,mem_univ,true_and,mem_map]constructor· intro h; exact ⟨⟨i.val,h⟩,Fin.ext rfl⟩· rintro ⟨j,rfl⟩; exact j.isLtsimp [h,e]private theorem pre_add_compl {N : ℕ} (u : Fin (N+1)) (s : Finset (Fin N)) :(pre u s).card + (pre u sᶜ).card = u.val := byhave e₁ : pre u s = (pre u univ).filter (· ∈ s) := by ext; simp [pre,and_comm]have e₂ : pre u sᶜ = (pre u univ).filter (· ∉ s) := by ext; simp [pre,and_comm]rw [e₁,e₂, filter_card_add_filter_neg_card_eq_card, all_pre]private theorem match_iff {m : ℕ} (w : MarkedWord m) :MatchingRank (removeMark w) ↔ 2 * (pre w.1 w.2.val).card = w.1.val := byrw [matching_mark]have h := pre_add_compl w.1 w.2.valomegaprivate def cutEquiv {a b N : ℕ} (h : a+b=N) : Fin a ⊕ Fin b ≃ Fin N :=finSumFinEquiv.trans (finCongr h)private def join {a b N : ℕ} (h : a+b=N) (l : Finset (Fin a)) (r : Finset (Fin b)) :Finset (Fin N) := (l.disjSum r).map (cutEquiv h).toEmbeddingprivate theorem joinAll {a b N : ℕ} (h : a+b=N) (s : Finset (Fin N)) :∃ l r, join h l r = s := bylet e := cutEquiv hlet z := e.finsetCongr.symm srefine ⟨z.toLeft,z.toRight,?_⟩rw [join, toLeft_disjSum_toRight]exact e.finsetCongr.apply_symm_apply sprivate theorem join_inj {a b N : ℕ} (h : a+b=N) {l l' : Finset (Fin a)} {r r' : Finset (Fin b)}(he : join h l r = join h l' r') : l = l' ∧ r = r' :=disjSum_inj.mp (Finset.map_inj.mp he)private theorem join_size {a b N : ℕ} (h : a+b=N) (l : Finset (Fin a)) (r : Finset (Fin b)) :(join h l r).card = l.card + r.card := by simp [join]private theorem join_pre {a b N : ℕ} (h : a+b=N) (l : Finset (Fin a)) (r : Finset (Fin b)) :(pre (⟨a,by omega⟩ : Fin (N+1)) (join h l r)).card = l.card := bylet e := cutEquiv hhave H : (l.disjSum r).filter (fun x => (e x).val < a) = l.map Function.Embedding.inl := byext xcases x with| inl i =>simp [e,cutEquiv,i.isLt]| inr j =>simp [e,cutEquiv]simp only [pre,join,filter_map,Function.comp_def]simpa [e] using congrArg Finset.card Hprivate def pieceWord {m : ℕ} (p : BalancedPieces m) : MarkedWord m :=(⟨2*p.1.val,by have := p.1.isLt; omega⟩, ⟨join (by have := p.1.isLt; omega) p.2.1.val p.2.2.val,by rw [join_size,p.2.1.property,p.2.2.property]; have := p.1.isLt; omega⟩)private theorem piece_good {m : ℕ} (p : BalancedPieces m) :MatchingRank (removeMark (pieceWord p)) := byrw [match_iff]simp [pieceWord,join_pre,p.2.1.property]private theorem piece_inj {m : ℕ} : Function.Injective (@pieceWord m) := byrintro ⟨j,L,R⟩ ⟨k,L',R'⟩ hehave hj : 2*j.val = 2*k.val := congrArg (fun z => z.1.val) hehave hjk : j = k := Fin.ext (by omega)subst khave he' := congrArg (fun z => z.2.val) heobtain ⟨hl,hr⟩ := join_inj _ he'congr 2 <;> apply Subtype.ext <;> assumptionprivate theorem piece_surj {m : ℕ} (w : MarkedWord m) (hw : MatchingRank (removeMark w)) :∃ p : BalancedPieces m, pieceWord p = w := byobtain ⟨u,W⟩ := whave h := (match_iff (u,W)).mp hwlet j : Fin (m+1) := ⟨(pre u W.val).card,byhave hc : (pre u W.val).card ≤ W.val.card := Finset.card_le_card (filter_subset ..)have := W.property; omega⟩have hu : 2*j.val = u.val := hhave hsum : 2*j.val + 2*(m-j.val) = 2*m := by have := j.isLt; omegaobtain ⟨l,r,H⟩ := joinAll hsum W.valhave hjl : l.card = j.val := byhave hh := join_pre hsum l rrw [H] at hhhave uc : (⟨2*j.val,by omega⟩ : Fin (2*m+1)) = u := Fin.ext hurw [uc] at hhexact hh.symmhave hr : r.card = m-j.val := byhave hc := join_size hsum l rrw [H,W.property,hjl] at hc; omegaexact ⟨⟨j,⟨l,hjl⟩,⟨r,hr⟩⟩, Prod.ext (Fin.ext hu) (Subtype.ext H)⟩private abbrev Good (n m : ℕ) := {t : Triple n m // DisjointTriple t ∧ MatchingRank t}private def goodLift {n m : ℕ} (x : SubsetOfSize n (2*m+1) × BalancedPieces m) : Good n m :=let d := supportLift (x.1,markedLift (pieceWord x.2))⟨d.val,d.property,(transport_rank ..).mpr (piece_good x.2)⟩private theorem goodLift_inj (n m : ℕ) : Function.Injective (@goodLift n m) := byrintro ⟨C,p⟩ ⟨D,q⟩ hhave hd : supportLift (C, markedLift (pieceWord p)) = supportLift (D, markedLift (pieceWord q)) :=Subtype.ext (congrArg (fun z : Good n m => z.val) h)have h' := (lift_inj n m) hdhave hC : C = D := congrArg (fun z : SubsetOfSize n (2*m+1) × Disc (2*m+1) m => z.1) h'have hW : markedLift (pieceWord p) = markedLift (pieceWord q) := congrArg Prod.snd h'exact Prod.ext hC (piece_inj (markedLift_inj _ hW))private theorem goodLift_surj (n m : ℕ) : Function.Surjective (@goodLift n m) := byrintro ⟨t,ht,hr⟩obtain ⟨⟨C,s⟩,hs⟩ := lift_surj n m ⟨t,ht⟩obtain ⟨w,hw⟩ := markedLift_surj _ ssubst shave he : transport (C.val.orderEmbOfFin C.property) (removeMark w) = t := congrArg Subtype.val hshave hrw : MatchingRank (removeMark w) := (transport_rank ..).mp (he ▸ hr)obtain ⟨p,hp⟩ := piece_surj w hrwsubst wexact ⟨(C,p),Subtype.ext he⟩/-- Among all disjoint triples of m-subsets and a marked position, the relative frequencyof equal ranks is independent of n. The identity is valid even when both counts vanish. -/theorem disjointTripleRankCount (n m : ℕ) :(univ.filter fun t : Triple n m => DisjointTriple t ∧ MatchingRank t).card *((2*m+1)*Nat.centralBinom m) =(univ.filter fun t : Triple n m => DisjointTriple t).card * 4^m := bylet goodEquiv := Equiv.ofBijective (@goodLift n m) ⟨goodLift_inj n m,goodLift_surj n m⟩have hg := (Fintype.card_congr goodEquiv).symmhave hd := (Fintype.card_congr (supportEquiv n m)).symmhave hm := (Fintype.card_congr (markedEquiv m)).symmhave hp : Fintype.card (BalancedPieces m) = 4^m := bysimp only [BalancedPieces, Fintype.card_sigma,Fintype.card_prod,BalancedWord,Fintype.card_finset_len,Fintype.card_fin,← Nat.centralBinom_eq_two_mul_choose]rw [Fin.sum_univ_eq_sum_range (fun k => Nat.centralBinom k * Nat.centralBinom (m-k)),centralBinomialConvolution]simp only [Fintype.card_prod] at hd hgsimp only [MarkedWord, BalancedWord, Fintype.card_prod, Fintype.card_fin,Fintype.card_finset_len,← Nat.centralBinom_eq_two_mul_choose] at hmrw [← Fintype.card_subtype, ← Fintype.card_subtype]change Fintype.card (Good n m) * _ = Fintype.card (Disc n m) * _rw [hg, hd, hm, hp]ring/-- Odd Wallis integral with the normalization from Problem 1.6. -/noncomputable def wallisValue (m : ℕ) : ℝ :=∫ x in 0..Real.pi / 2, Real.sin x ^ (2*m+1)theorem wallisValue_bounds (m : ℕ) : 0 ≤ wallisValue m ∧ wallisValue m ≤ 1 := byrw [wallisValue, oddWallisIntegral]have hc : (0:ℝ) < Nat.centralBinom m := by exact_mod_cast Nat.centralBinom_pos mhave hD : (0:ℝ) < (2*(m:ℝ)+1) * Nat.centralBinom m := by positivityconstructor· positivity· rw [div_le_one hD]exact_mod_cast Nat.four_pow_le_two_mul_add_one_mul_central_binom mtheorem disjointTripleRankCount_real (n m : ℕ) :((univ.filter fun t : Triple n m => DisjointTriple t ∧ MatchingRank t).card : ℝ) =((univ.filter fun t : Triple n m => DisjointTriple t).card : ℝ) * wallisValue m := byrw [wallisValue, oddWallisIntegral, ← mul_div_assoc]have hc : (0:ℝ) < Nat.centralBinom m := by exact_mod_cast Nat.centralBinom_pos mhave hD : (0:ℝ) < (2*(m:ℝ)+1) * Nat.centralBinom m := by positivityapply (eq_div_iff hD.ne').mprexact_mod_cast disjointTripleRankCount n m#print axioms disjointTripleRankCount_realend WallisGap