import ContractExact namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Floats.ExecFloat.Binary local notation"А"=>USize.lt_iff_toNat_lt local notation"Б"=>mul_le_mul_of_nonneg_right local notation"В"=>0x7FFFFFFF local notation"Г"=>mul_le_mul_of_nonneg_left local notation"Д"=>rationalWord local notation"Е"=>0xFFFFFFFF local notation"Ж"=>UInt32.le_iff_toNat_le local notation"З"=>List.range local notation"И"=>abs_of_nonneg local notation"Й"=>FloatArray.emptyWithCapacity local notation"К"=>Array.replicate local notation"Л"=>abs_nonneg local notation"М"=>Nat.mod_eq_of_lt local notation"Н"=>Nat.and_two_pow_sub_one_eq_mod local notation"О"=>Float.ofBits local notation"П"=>dite_eq_left local notation"Р"=>List.mem_range local notation"С"=>mul_nonneg local notation"Т"=>Nat.zero_add local notation"У"=>0x7F800000 local notation"Ф"=>ite_eq_right local notation"Х"=>mul_le_mul local notation"Ц"=>zpow_le_zpow_right₀ local notation"Ч"=>two_ne_zero local notation"Ш"=>dite_eq_right local notation"Щ"=>Model.roundDyadic local notation"Ъ"=>abs_add_le local notation"Ы"=>Model.isFinite local notation"Ь"=>Nat.mul_le_mul_right local notation"Э"=>zpow_add₀ local notation"Ю"=>Nat.cast_nonneg local notation"Я"=>getElem!_pos local notation"а"=>UInt32.toNat_inj local notation"б"=>abs_of_pos local notation"в"=>abs_mul local notation"г"=>abs_sub_comm local notation"д"=>UInt64.toNat_add local notation"е"=>Nat.mul_comm local notation"ж"=>Model.toReal_eq_unpackedToReal_toModel local notation"з"=>Real.sqrt local notation"и"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"й"=>zpow_natCast local notation"к"=>Model.toReal local notation"л"=>Finset.range local notation"м"=>Float.sqrt local notation"н"=>lt_of_lt_of_le local notation"о"=>mul_lt_mul_of_pos_right local notation"п"=>UInt32.toNat_and local notation"р"=>UInt64.toNat_shiftRight local notation"с"=>ite_eq_left local notation"т"=>FloatLib.Numerics.binaryRadix local notation"у"=>UInt32.lt_iff_toNat_lt local notation"ф"=>UInt64.lt_iff_toNat_lt local notation"х"=>zpow_pos local notation"ц"=>toDyadic_ofUInt32 local notation"ч"=>lt_of_le_of_lt local notation"ш"=>Nat.div_lt_of_lt_mul local notation"щ"=>Nat.shiftRight_eq_div_pow local notation"ъ"=>Nat.not_lt_zero local notation"ы"=>pow_le_pow_left₀ local notation"ь"=>0x3CE0000000000000 local notation"э"=>Model.modelSignBit local notation"ю"=>Nat.add_zero local notation"я"=>Float.Model.UnpackedFloat.unpack local notation"ذ"=>Finset.sum_congr local notation"ر"=>Nat.mod_lt local notation"ز"=>Nat.shiftLeft_eq local notation"س"=>0x80000000 local notation"ش"=>le_trans local notation"ص"=>UInt32.toNat_toUSize local notation"ض"=>USize.toNat_one local notation"ط"=>sq_nonneg local notation"ظ"=>0x3370000000000000 local notation"ع"=>Model.toModel_ofFloatModel local notation"غ"=>Finset.mem_range local notation"ػ"=>List.nodup_range local notation"ؼ"=>Nat.div_eq_of_lt local notation"ؽ"=>Nat.le_mul_of_pos_right local notation"ؾ"=>UInt64.toNat_and local notation"ؿ"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"ـ"=>getElem!_neg local notation"ف"=>Nat.zero_mul local notation"ق"=>UInt32.toNat_sub_of_le local notation"ك"=>List.foldl_cons local notation"ل"=>Bool.and_eq_true_iff local notation"م"=>List.mem_cons_of_mem local notation"ن"=>List.range_eq_range' local notation"ه"=>Nat.one_mul local notation"و"=>UInt32.le_refl local notation"ى"=>abs_sub local notation"ي"=>le_refl local notation"Ա"=>List.map_congr_left local notation"Բ"=>UInt32.toNat_ofNat' local notation"Գ"=>Nat.add_assoc local notation"Դ"=>List.mem_cons_self local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith local notation"ѐ"=>Array UInt32 local notation"ё"=>Array Word local notation"ђ"=>FloatArray local notation"ѓ"=>UInt32 local notation"є"=>USize local notation"ѕ"=>Float local notation"і"=>UInt64 local notation"ї"=>Array Nat local notation"ј"=>Bool local notation"љ"=>List Nat local notation"њ"=>Word local notation"ћ"=>Nat open USize (toNat) open UInt32 (toNat) open UInt64 (toNat) local notation:max"Ѡ"x:max=>toNat x open Array (size) open FloatArray (size) local notation:max"ѡ"x:max=>size x open Nat (toUSize) open UInt32 (toUSize) local notation:max"Ѣ"x:max=>toUSize x local notation:max"ѣ"x:max=>List.length x section open FloatLib.Numerics end lemma qAb (n:ћ) (f:ћ->ѓ) (k:ћ) (hk:k< n) : (Array.ofFn (n:=n) λi=>f i.val)[k]! = f k:=by rw[Я _ k (by simpa using hk),Array.getElem_ofFn] lemma qEb (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,Model.ofUInt32,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z lemma qGw {w:ѓ} (h:expOf w≠255):Ы (ofW w)=true:=by simp[ofW,isFinite_ofUInt32,h] lemma qEg (A B:ѐ) (hB:ѡB=ѡA) (hall:∀ k,k< ѡA->expOf A[k]!≠255∧expOf B[k]!≠255) : finishWord (dsum A B 0 ѡA) (-298) (ѡA≠0 && dallNZ A B 0 ѡA)=dotSpec A B:=by set L:=A.map (Model.ofUInt32 (fmt:=Fmt)) with hL;set R:=B.map (Model.ofUInt32 (fmt:=Fmt)) with hR;have hLR:ѡL=ѡR:=by simp[hL,hR,hB] have hS:ѡL=ѡA:=by simp[hL] have hLf:∀ k (hk:k< ѡL),Ы L[k]=true:=λk hk=>by simpa [hL,ofW] using qGw (getElem!_pos A k א ▸ (hall k א).1) have hRf:∀ k (hk:k< ѡR),Ы R[k]=true:=λk hk=>by simpa [hR,ofW] using qGw (getElem!_pos B k א ▸ (hall k א).2) have f k (_:0<=k) (h:k<0+ѡL):=hall k א;have s:=(finiteDotSlice_eq A B 0 ѡL (by simp[hL]) (by simp[hL,hB]) f).trans (by rw[hS]);have x:=(Model.Reduction.Internal.dotState_exact_toRat (destination:=Fmt) L R hLR hLf hRf).trans s;unfold dotSpec Model.dot;by_cases h0:dsum A B 0 ѡA=0;. rw[Model.Reduction.dotWithStatus_eq_zero_of_finite Fmt L R .nearestEven hLR hLf hRf ((ؿ _).1 (by simp[x,h0])),h0,finishWord_zero];obtain ⟨h1,h2⟩:=dotStateLoop_flags A B {} 0 ѡL (by simp[hL]) (by simp[hL,hB]) f;simp only[Except.map,Model.Reduction.Internal.dotState];rw[h1,h2];simp[hS] . rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L R .nearestEven hLR hLf hRf (by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L R hLR,s] simpa using h0)] exact finishWord_of_ne _ _ _ _ h0 x structure Acc where (pos neg:ї) (special negZero:ј) def Acc.total (acc:Acc):Int:=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:ћ) (neg:ј):Acc := match acc with | ⟨pos,ng,sp,nz⟩ => if m=0 then ⟨pos,ng,sp,nz && neg⟩ else if neg then ⟨pos,addLimbs64 ng m.toUInt64 idx,sp,false⟩ else ⟨addLimbs64 pos m.toUInt64 idx,ng,sp,false⟩ lemma Acc.add_spec (acc:Acc) (m idx:ћ) (neg:ј) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hm:m<2^64) (hi:idx/32+1<18) : (acc.add m idx neg).pos.size=18∧(acc.add m idx neg).neg.size=18 ∧ (acc.add m idx neg).special=acc.special ∧ (acc.add m idx neg).negZero=(acc.negZero && (decide (m=0) && neg)) ∧ (acc.add m idx neg).total=acc.total+(if neg then -(m:Int) else m)*2^idx:=by obtain ⟨pos,ng,sp,nz⟩:=acc;simp only at hp hn;by_cases hm0:m=0;. simp[Acc.add,Acc.total,hm0,hp,hn] . cases neg;. refine ⟨by simp[Acc.add,hm0,addLimbs64_size,hp],by simp[Acc.add,hm0,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,Bool.false_eq_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring . refine ⟨by simp[Acc.add,hm0,hp],by simp[Acc.add,hm0,addLimbs64_size,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,ite_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring @[inline] def dotStep (acc:Acc) (wa wb:ѓ):Acc := if expOf wa=255∨expOf wb=255 then ⟨acc.pos,acc.neg,true,acc.negZero⟩ else acc.add (mantOf wa*mantOf wb) (idxOf wa+idxOf wb) (Bool.xor (signOf wa) (signOf wb)) @[specialize] def dotLoop (fa fb:ћ->ѓ) (pos neg:ї) (sp nz:ј) (index:ћ):ћ->Acc | 0=>⟨pos,neg,sp,nz⟩ | count+1 => match dotStep ⟨pos,neg,sp,nz⟩ (fa index) (fb index) with | ⟨p,n,s,z⟩=>dotLoop fa fb p n s z (index+1) count lemma qCb (fa fb:ћ->ѓ) (pos neg:ї) (nz:ј) (index count:ћ):(dotLoop fa fb pos neg true nz index count).special=true:=by induction count generalizing pos neg nz index with | zero=>rfl | succ count ih => rw[dotLoop];have hs:(dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)).special=true:=by unfold dotStep Acc.add;split_ifs<;>rfl generalize dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)=r at hs ⊢;obtain ⟨p,q,s,z⟩:=r;simp only at hs;subst hs;exact ih .. lemma qC6 (fa fb:ћ->ѓ) (A B:ѐ) (n:ћ) (hA:∀ k,k< n->A[k]! = fa k) (hB:∀ k,k< n->B[k]! = fb k) (acc:Acc) (index remaining:ћ) (h:index+remaining<=n) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hfin:(dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).special =false) : (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).pos.size=18 ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).neg.size=18 ∧ acc.special=false ∧ (∀ k,index<=k->k< index+remaining->expOf A[k]!≠255∧expOf B[k]!≠255) ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).total =acc.total+dsum A B index remaining ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).negZero =(acc.negZero && dallNZ A B index remaining):=by induction remaining generalizing acc index with | zero=>obtain ⟨p,n,s,z⟩:=acc;simp_all [dotLoop,dsum,dallNZ] | succ count ih => obtain ⟨p,q,s,z⟩:=acc simp only at hp hn have ha:=hA index א have hb:=hB index א by_cases he:expOf (fa index)=255∨expOf (fb index)=255 . exfalso;rw[dotLoop] at hfin;have hs:(dotStep ⟨p,q,s,z⟩ (fa index) (fb index)).special=true:=by simp[dotStep,he] generalize dotStep ⟨p,q,s,z⟩ (fa index) (fb index)=r at hs hfin;obtain ⟨p',q',s',z'⟩:=r;simp only at hs;subst hs;rw[qCb] at hfin;exact Bool.noConfusion hfin . have he':=not_or.mp he obtain ⟨h1,h2,h3,h4,h5⟩ := Acc.add_spec ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))) hp hn (mul_mant_lt _ _) (by have:=idxOf_le _ he'.1;have:=idxOf_le _ he'.2;omega) have hstep:dotStep ⟨p,q,s,z⟩ (fa index) (fb index) = Acc.add ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))):=by simp[dotStep,he] rw[dotLoop,hstep] at hfin ⊢;obtain ⟨hP,hN,hs,hall,hD,hz⟩:=ih _ (index+1) א h1 h2 hfin;refine ⟨hP,hN,by rw[<-h3,hs],?_,?_,?_⟩ . intro k k1 k2;rcases Nat.lt_or_ge k (index+1) with hk | hk;. have:k=index :=א;subst this;rw[ha,hb];exact he' . exact hall k hk א . have sv2:(if Bool.xor (signOf (fa index)) (signOf (fb index)) then -((mantOf (fa index)*mantOf (fb index):ћ):Int) else ((mantOf (fa index)*mantOf (fb index):ћ):Int)) =sv (fa index)*sv (fb index):=by unfold sv;cases signOf (fa index)<;>cases signOf (fb index)<;>simp rw[hD,h5,dsum,ha,hb,sv2];ring . rw[hz,h4,dallNZ,ha,hb];simp[Bool.and_assoc] def Y_low (p q:ї) (k:ћ):ћ->ћ | 0=>k | f+1=>if p[k]! = 0 && q[k]! = 0 then Y_low p q (k+1) f else k lemma qD3 (p q:ї):∀ f k,(∀ j,j< k->p[j]! = 0∧q[j]! = 0) → ∀ j,j< Y_low p q k f->p[j]! = 0∧q[j]! = 0:=by intro f;induction f with | zero=>exact λk h j hj=>h j hj | succ f ih => intro k h j hj;unfold Y_low at hj;split at hj . rename_i hk;simp only[Bool.and_eq_true,decide_eq_true_eq] at hk;refine ih (k+1) (λi hi=>?_) j hj;rcases Nat.lt_or_ge i k with h1 | h1;. exact h i h1 . have:i=k :=א;subst this;exact hk . exact h j hj lemma qHd (L:љ):∀ l,(∀ j,j< l->L[j]! = 0) → limbVal L=2^(32*l)*limbVal (L.drop l):=by induction L with | nil=>simp[limbVal] | cons x xs ih => intro l h;cases l with | zero=>simp | succ l => have h0:x=0:=by simpa using h 0 א have:=ih l (λj hj=>by simpa using h (j+1) א);simp only[limbVal,List.drop_succ_cons,h0,this];rw[Nat.mul_add 32 l 1,pow_add];ring lemma qFr (A:ї) (l:ћ) (h:∀ j,j< l->A[j]! = 0) : limbVal A.toList=2^(32*l)*limbValA (A.extract l ѡA):=by rw[limbValA_eq,Array.toList_extract,List.extract_eq_drop_take,<-Array.length_toList];rw[List.take_of_length_le ג];refine qHd _ l λj hj=>?_;have:=h j hj;rw[getElem!_def] at this;simp only[Array.getElem!_toList,getElem!_def] at *;cases e:A[j]?<;>simp_all lemma qIo (S:Int) (l:ћ) (e:Int) (z:ј) : finishWord S (e+32*l) z=finishWord (2^(32*l)*S) e z:=by by_cases hS:S=0;. simp[hS,finishWord] have h2:(2:Int)^(32*l)*S≠0:=mul_ne_zero (by positivity) hS rw[finishWord_of_ne (ofIntDyadic (2^(32*l)*S) e) S (e+32*l) z hS (by rw[toRat_ofIntDyadic,Э ב,show (32:ℤ)*l=((32*l:ℕ):ℤ) by push_cast;ring,й];push_cast;ring)] simp[finishWord,h2] @[inline] def dotFn (n:ћ) (fa fb:ћ->ѓ):ѓ := let acc:=dotLoop fa fb (К 18 0) (К 18 0) false true 0 n if acc.special then dotSpec (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) else let l:=Y_low acc.pos acc.neg 0 18 finishWord ((limbValA (acc.pos.extract l 18):Int)-limbValA (acc.neg.extract l 18)) (-298+32*l) (n≠0 && acc.negZero) lemma qAi:dotFn=dotOf:=by funext n fa fb;unfold dotFn dotOf;set A:=Array.ofFn (n:=n) λi=>fa i with hA;set B:=Array.ofFn (n:=n) λi=>fb i with hB;set acc:=dotLoop fa fb (К 18 0) (К 18 0) false true 0 n with hacc;by_cases hsp:acc.special=true;. simp[hsp] have hsp':acc.special=false:=by simpa using hsp have hF:=qC6 fa fb A B n (qAb n fa) (qAb n fb) ⟨К 18 0,К 18 0,false,true⟩ 0 n א ג ג hsp';obtain ⟨hP,hN,-,hall,hD,hz⟩:=hF;have hAs:ѡA=n:=by simp[hA] have hBs:ѡB=n:=by simp[hB] have hL:=qD3 acc.pos acc.neg 18 0 (λj hj=>absurd hj (ъ _));simp only[hsp',Bool.false_eq_true,ite_false];have e0:(⟨К 18 0,К 18 0,false,true⟩:Acc).total=0:=by decide rw[e0,Int.zero_add] at hD;rw[Bool.true_and] at hz;generalize Y_low acc.pos acc.neg 0 18=l at hL ⊢;have h1:=qFr acc.pos l (λj hj=>(hL j hj).1);have h2:=qFr acc.neg l (λj hj=>(hL j hj).2);rw[hP] at h1;rw[hN] at h2;rw[qIo,<-qEg A B א (λk hk=>hall k א א),hAs,<-hD,<-hz];congr 1;have:acc.total=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList:=rfl;rw[this,h1,h2];push_cast;ring lemma usz (t n:є) (h:t< n):(t+1).toNat=Ѡt+1:=by have h1:Ѡt< Ѡn:=А.mp h;have h2:Ѡn< USize.size:=n.toNat_lt_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add,ض];exact М א lemma qAf (a b:є) (h:Ѡa+Ѡb<2^32):(a+b).toNat=Ѡa+Ѡb:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add];exact М א lemma uk (k:ћ) (h:k<2^32):(USize.ofNat k).toNat=k:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_ofNat'];exact М א lemma qDo (a:ђ) (i:є) (h:i< a.usize):Ѡi< ѡa:=by have h1:=А.mp h;have h2:a.usize.toNat<=ѡa:=by simp only[FloatArray.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega lemma qBd (a:ѐ) (i:є) (h:i< a.usize):Ѡi< ѡa:=by have h1:=А.mp h;have h2:a.usize.toNat<=ѡa:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega @[inline] def rdW (a:ѐ) (i:є):ѓ := if h:i< a.usize then a.uget i (qBd a i h) else 0 @[inline] def wrF (a:ђ) (i:є) (v:ѕ):ђ := if h:i< a.usize then a.uset i v (qDo a i h) else a @[inline] def wrW (a:ѐ) (i:є) (v:ѓ):ѐ := if h:i< a.usize then a.uset i v (qBd a i h) else a def pwEntry (i:ћ):ѕ := let e:=(i%256).toUInt64 let s:і:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then О s else О (s ||| ((e+873) <<< (52:і))) def pw:ђ := (З 512).foldl (λa i=>a.push (pwEntry i)) (Й 512) lemma qDx:pw.data=(З 512).toArray.map pwEntry:=by have key:∀ (l:љ) (a:ђ),(l.foldl (λa i=>a.push (pwEntry i)) a).data=a.data ++ (l.toArray.map pwEntry):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pw,key];simp[FloatArray.emptyWithCapacity] lemma qGe:ѡpw=512:=by simp[FloatArray.size,qDx] lemma qCh (w:ѓ):(w >>> (23:ѓ)).toUSize.toNat< ѡpw:=by rw[qGe];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega def pwEntryN (z k i:ћ):ѕ := if i%256a.push (pwEntryN z k i)) (Й 512) lemma qDj (z k:ћ):(pwTb z k).data=(З 512).toArray.map (pwEntryN z k):=by have key:∀ (l:љ) (a:ђ),(l.foldl (λa i=>a.push (pwEntryN z k i)) a).data=a.data ++ (l.toArray.map (pwEntryN z k)):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pwTb,key];simp[FloatArray.emptyWithCapacity] lemma qTd (z k:ћ):(pwTb z k).size=512:=by simp[FloatArray.size,qDj] lemma qBx {T:ђ} (hT:ѡT=512) (w:ѓ):(w >>> (23:ѓ)).toUSize.toNat< ѡT:=by rw[hT];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow,UInt32.size] at h ⊢;omega def tNa:ђ:=pwTb 1 125 def tNb:ђ:=pwTb 1 126 def tNc:ђ:=pwTb 1 127 def tNd:ђ:=pwTb 1 128 def tZa:ђ:=pwTb 0 125 def tZb:ђ:=pwTb 0 126 def tZc:ђ:=pwTb 0 127 def tZd:ђ:=pwTb 0 128 @[inline] def tN (n:ћ):ђ:=if 256<=n then tNa else if 64<=n then tNb else if 16<=n then tNc else tNd @[inline] def tZ (n:ћ):ђ:=if 256<=n then tZa else if 64<=n then tZb else if 16<=n then tZc else tZd @[inline] def hvI (n:ћ):ѓ:=if 256<=n then 0x3E7FFFFF else if 64<=n then 0x3EFFFFFF else if 16<=n then 0x3F7FFFFF else 0x3FFFFFFF lemma qFz (z k:ћ) (i:є) (h:Ѡi< (pwTb z k).size):(pwTb z k).uget i h=pwEntryN z k Ѡi:=by simp[FloatArray.uget,qDj] def TOK (z:ћ) (T:ђ) (h:ѓ):Prop:=∃ k,ѡT=512∧(∀ (i:є) (p:Ѡi< ѡT),T.uget i p=pwEntryN z k Ѡi)∧2<=k∧k<=128∧Ѡh+1=k*2^23 lemma qTs {z:ћ} {T:ђ} {h:ѓ} (o:TOK z T h):ѡT=512:=by obtain ⟨k,e,-⟩:=o;exact e lemma qTN (n:ћ):TOK 1 (tN n) (hvI n):=by unfold tN hvI;split . exact ⟨125,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ . split . exact ⟨126,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ . split . exact ⟨127,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ . exact ⟨128,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ lemma qTZ (n:ћ):TOK 0 (tZ n) (hvI n):=by unfold tZ hvI;split . exact ⟨125,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ . split . exact ⟨126,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ . split . exact ⟨127,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ . exact ⟨128,qTd _ _,qFz _ _,by norm_num,by norm_num,rfl⟩ @[inline] def dv (w:ѓ):ѕ := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:ѓ)).toUSize (qCh w) @[inline] def dvB (v:ѕ) (w:ѓ):ѕ := (v*pw.uget (w >>> (23:ѓ)).toUSize (qCh w))*((w &&& 0x7FFFFF) ||| 0x800000).toFloat @[inline] def good (w:ѓ):ј := let v:=w &&& В v == 0 || v-(0x00800000:ѓ)<=(0x7EFFFFFF:ѓ) def scan (A:ѐ) (n i:є) (hn:Ѡn<=ѡA) (lo hi:ѓ):ѓ := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& В let u:=v-1 scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else if 0x007FFFFF<=lo∧hi<У then hi else Е termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];omega def scanF (A:ѐ) (n i:є) (hn:Ѡn<=ѡA) (o s:ѓ):ѓ := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& В scanF A n (i+1) hn (o ||| v) (s &&& (v-1)) else if 0x7FFFFF<=s∧o<У then o else Е termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];omega lemma qAc (a:ѐ):a.usize.toNat<=ѡa:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ @[inline] def scanH (r:ћ) (A:ѐ):ѓ := let h:=if r == 1 then scanF A A.usize 0 (qAc A) 0 Е else Е if h == Е then scan A A.usize 0 (qAc A) Е 0 else h @[inline] def scanW (r:ћ) (A:ѐ):ѓ := if r == 8 || r == 1 then 0 else scanH r A def pushZ (n k:є) (a:ђ):ђ := if h:k< n then pushZ n (k+1) (a.push 0) else a termination_by Ѡn-Ѡk decreasing_by rw[usz k n h];have:=А.mp h;omega @[inline] def zivW (A B:і):ѓ := let x:і:=(A+(0x0FFFFFFF:і)) >>> (29:і) let y:і:=(B+(0x0FFFFFFF:і)) >>> (29:і) let ex:і:=(x >>> (23:і)) &&& (0x7FF:і) if x == y && ex-(897:і)<(254:і) then (((x >>> (34:і)) <<< (31:і)) ||| ((x &&& (0x3FFFFFFFF:і))-((896:і) <<< (23:і)))).toUInt32 else Е @[inline] def ziv (s e:ѕ):ѓ := zivW (s+e).toBits (s-e).toBits @[noinline] def slowOut (inW:ћ) (relu:ј) (X W b:ё) (i j:ћ):њ := act relu (dotFn (inW+1) (λt=>if t< inW then X[i*inW+t]! else oneW) (λt=>if t< inW then W[j*inW+t]! else b[j]!)) @[inline] def fin (inW:ћ) (relu:ј) (X W b:ё) (i j:ћ) (s nf t:ѕ) (bw:ѓ):ѓ := let bv:=dv bw let r:=ziv (s+bv) (nf*(t+bv.abs)) if r == Е then slowOut inW relu X W b i j else act relu r structure F1 where (s t:ѕ) def tightGo (X W:ѐ) (xo wo n t:є) (s a:ѕ):F1 := if h:t< n then let q:=dv (rdW X (xo+t))*dv (rdW W (wo+t)) tightGo X W xo wo n (t+1) (s+q) (a+q.abs) else ⟨s,a⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];have:=А.mp h;omega @[inline] def fin8 (inW:ћ) (relu:ј) (X W b:ё) (i j:ћ) (s nf a:ѕ) (hx bw:ѓ) (n:є):ѓ := let bv:=dv bw let r:=ziv (s+bv) (nf*(dv hx*a+bv.abs)) if r == Е then let q:=tightGo X W (i*inW).toUSize (j*inW).toUSize n 0 0 0 fin inW relu X W b i j q.s nf q.t bw else r &&& ((if relu then (0:ѓ) else Е) ||| ((r >>> (31:ѓ))-1)) structure R1 where (ix:ѐ) (buf:ђ) (c:є) (l:ѕ) def decR (X:ѐ) (o n t:є) (ix:ѐ) (buf:ђ) (c:є) (l:ѕ):R1 := if h:t< n then let w:=rdW X (o+t) if w &&& В == 0 then decR X o n (t+1) ix buf c l else let v:=dv w decR X o n (t+1) (wrW ix c t.toUInt32) (wrF buf c v) (c+1) (l+v.abs) else ⟨ix,buf,c,l⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];have:=А.mp h;omega) lemma wi (w i m:є) (W:ѐ) (hw:Ѡw+Ѡm<=ѡW) (hs:ѡW<2^32) (hi:Ѡi< Ѡm):(w+i).toNat< ѡW:=by rw[qAf _ _ א];omega def armT:ј:=System.Platform.target.startsWith "aarch64" structure F4 where (a b c d:ѕ) def go4B (x:ђ) (ix W:ѐ) (w0 w1 w2 w3 m n t:є) (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (a b c d:ѕ):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let q0:=v*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) let q1:=dvB v (W.uget (w1+i) (wi w1 i m W h1 hs him)) let q2:=v*dv (W.uget (w2+i) (wi w2 i m W h2 hs him)) let q3:=dvB v (W.uget (w3+i) (wi w3 i m W h3 hs him)) go4B x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 (a+q0) (b+q1) (c+q2) (d+q3) else go4B x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 a b c d else ⟨a,b,c,d⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) def go4E (x:ђ) (ix W:ѐ) (w0 w1 w2 w3 m n t:є) (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (a b c d:ѕ):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm go4E x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 (a+v*dv (W.uget (w0+i) (wi w0 i m W h0 hs him))) (b+v*dv (W.uget (w1+i) (wi w1 i m W h1 hs him))) (c+v*dv (W.uget (w2+i) (wi w2 i m W h2 hs him))) (d+v*dv (W.uget (w3+i) (wi w3 i m W h3 hs him))) else go4E x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 a b c d else ⟨a,b,c,d⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) def go1 (x:ђ) (ix W:ѐ) (w0 m n t:є) (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (a:ѕ):ѕ := if h:t< n then have ht:=А.mp h let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let q:=x.uget t א*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) go1 x ix W w0 m n (t+1) hx hi hs h0 (a+q) else go1 x ix W w0 m n (t+1) hx hi hs h0 a else a termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) def go4D (x wd:ђ) (ix W:ѐ) (w0 w1 w2 w3 m n t:є) (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (hd:ѡwd=ѡW) (a b c d:ѕ):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd (a+v*wd.uget (w0+i) (hd ▸ wi w0 i m W h0 hs him)) (b+v*wd.uget (w1+i) (hd ▸ wi w1 i m W h1 hs him)) (c+v*wd.uget (w2+i) (hd ▸ wi w2 i m W h2 hs him)) (d+v*wd.uget (w3+i) (hd ▸ wi w3 i m W h3 hs him)) else go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd a b c d else ⟨a,b,c,d⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) lemma qA9 {n:ћ} (h:n<=4294967295):n<2^32 :=א def cols1 (inW outW:ћ) (relu:ј) (X W b:ѐ) (i:ћ) (ix:ѐ) (x wd:ђ) (nf a:ѕ) (hx:ѓ) (m n:є) (j:ћ) (base:є) (out:ѐ):ѐ := if h4:j+4<=outW then let ju:=Ѣj let w0:=ju*m let w1:=(ju+1)*m let w2:=(ju+2)*m let w3:=(ju+3)*m if hg:Ѡn<=ѡx∧Ѡn<=ѡix∧ѡW<=4294967295∧Ѡw0+Ѡm<=ѡW ∧ Ѡw1+Ѡm<=ѡW∧Ѡw2+Ѡm<=ѡW∧Ѡw3+Ѡm<=ѡW then let r:=if hd:ѡwd=ѡW then go4D x wd ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 hd 0 0 0 0 else if armT then go4B x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 else go4E x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 let o0:=fin8 inW relu X W b i j r.a nf a hx (rdW b ju) m let o1:=fin8 inW relu X W b i (j+1) r.b nf a hx (rdW b (ju+1)) m let o2:=fin8 inW relu X W b i (j+2) r.c nf a hx (rdW b (ju+2)) m let o3:=fin8 inW relu X W b i (j+3) r.d nf a hx (rdW b (ju+3)) m let out:=wrW out (base+ju) o0 let out:=wrW out (base+ju+1) o1 let out:=wrW out (base+ju+2) o2 let out:=wrW out (base+ju+3) o3 cols1 inW outW relu X W b i ix x wd nf a hx m n (j+4) base out else out else if h1:j< outW then let ju:=Ѣj let w0:=ju*m if hg:Ѡn<=ѡx∧Ѡn<=ѡix∧ѡW<=4294967295∧Ѡw0+Ѡm<=ѡW then let r:=go1 x ix W w0 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.1) hg.2.2.2 0 let o:=fin8 inW relu X W b i j r nf a hx (rdW b ju) m cols1 inW outW relu X W b i ix x wd nf a hx m n (j+1) base (wrW out (base+ju) o) else out else out termination_by outW-j def rows1 (rows inW outW:ћ) (relu:ј) (X W b:ѐ) (wd:ђ) (nf:ѕ) (hv:ѓ) (m:є) (i:ћ) (ix:ѐ) (buf:ђ) (out:ѐ):ѐ := if h:i< rows then let r:=decR X (i*inW).toUSize m 0 ix buf 0 0 let out:=cols1 inW outW relu X W b i r.ix r.buf wd nf r.l hv m r.c 0 (i*outW).toUSize out rows1 rows inW outW relu X W b wd nf hv m (i+1) r.ix r.buf out else out termination_by rows-i @[inline] def dvN (T:ђ) (hT:ѡT=512) (w:ѓ):ѕ := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*T.uget (w >>> (23:ѓ)).toUSize (qBx hT w) @[inline] def dvNB (T:ђ) (hT:ѡT=512) (v:ѕ) (w:ѓ):ѕ := (v*T.uget (w >>> (23:ѓ)).toUSize (qBx hT w))*((w &&& 0x7FFFFF) ||| 0x800000).toFloat @[inline] def dvU (T:ђ) (hT:ѡT=512) (v:ѕ) (w:ѓ):ѕ:=v*dvN T hT w @[specialize] def go4N (f:ѕ->ѓ->ѕ) (T:ђ) (hT:ѡT=512) (x:ђ) (W:ѐ) (w0 w1 w2 w3 n t:є) (hx:Ѡn<=ѡx) (hs:ѡW<2^32) (h0:Ѡw0+Ѡn<=ѡW) (h1:Ѡw1+Ѡn<=ѡW) (h2:Ѡw2+Ѡn<=ѡW) (h3:Ѡw3+Ѡn<=ѡW) (a b c d:ѕ):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let u0:=W.uget (w0+t) (wi w0 t n W h0 hs ht) let u1:=W.uget (w1+t) (wi w1 t n W h1 hs ht) let u2:=W.uget (w2+t) (wi w2 t n W h2 hs ht) let u3:=W.uget (w3+t) (wi w3 t n W h3 hs ht) go4N f T hT x W w0 w1 w2 w3 n (t+1) hx hs h0 h1 h2 h3 (a+v*dvN T hT u0) (b+f v u1) (c+v*dvN T hT u2) (d+f v u3) else ⟨a,b,c,d⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];omega structure CN where (out:ѐ) (ok:ј) @[specialize] def colsN (f:ѕ->ѓ->ѕ) (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b:ѐ) (x:ђ) (nf l:ѕ) (n:є) (j:ћ) (out:ѐ):CN := if h:j< outW∧4<=outW then let k:=if j+4<=outW then j else outW-4 let ju:=Ѣk let w0:=ju*n let w1:=(ju+1)*n let w2:=(ju+2)*n let w3:=(ju+3)*n if hg:Ѡn<=ѡx∧ѡW<=4294967295∧Ѡw0+Ѡn<=ѡW ∧ Ѡw1+Ѡn<=ѡW∧Ѡw2+Ѡn<=ѡW∧Ѡw3+Ѡn<=ѡW then let r:=go4N f T hT x W w0 w1 w2 w3 n 0 hg.1 (qA9 hg.2.1) hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2 0 0 0 0 if (r.a+r.b+r.c+r.d).isFinite then let o0:=fin8 inW relu X W b 0 k r.a nf l hv (rdW b ju) n let o1:=fin8 inW relu X W b 0 (k+1) r.b nf l hv (rdW b (ju+1)) n let o2:=fin8 inW relu X W b 0 (k+2) r.c nf l hv (rdW b (ju+2)) n let o3:=fin8 inW relu X W b 0 (k+3) r.d nf l hv (rdW b (ju+3)) n let out:=wrW out ju o0 let out:=wrW out (ju+1) o1 let out:=wrW out (ju+2) o2 let out:=wrW out (ju+3) o3 colsN f T hT hv inW outW relu X W b x nf l n (k+4) out else ⟨out,false⟩ else ⟨out,false⟩ else ⟨out,decide (outW<=j)⟩ termination_by outW-j decreasing_by split<;>omega @[inline] def colsP (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b:ѐ) (x:ђ) (nf l:ѕ) (n:є) (j:ћ) (out:ѐ):CN := if armT then colsN (dvNB T hT) T hT hv inW outW relu X W b x nf l n j out else colsN (dvU T hT) T hT hv inW outW relu X W b x nf l n j out structure R4 where (iz:ѐ) (z:є) def decZ (X:ѐ) (o n t:є) (iz:ѐ) (z:є):R4 := if h:t< n then decZ X o n (t+1) (wrW iz z t.toUInt32) (if rdW X (o+t) &&& В == 0 then z+1 else z) else ⟨iz,z⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];have:=А.mp h;omega structure F4Q where (a b c d:ѕ) (q:ѓ) @[inline] def qZw (q u0 u1 u2 u3:ѓ):ѓ:= q &&& ((u0 &&& В)-1) &&& ((u1 &&& В)-1) &&& ((u2 &&& В)-1) &&& ((u3 &&& В)-1) @[specialize] def go4C (f:ѕ->ѓ->ѕ) (T:ђ) (hT:ѡT=512) (x:ђ) (ix W:ѐ) (w0 w1 w2 w3 m n t:є) (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (a b c d:ѕ) (q:ѓ):F4Q := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let u0:=W.uget (w0+i) (wi w0 i m W h0 hs him) let u1:=W.uget (w1+i) (wi w1 i m W h1 hs him) let u2:=W.uget (w2+i) (wi w2 i m W h2 hs him) let u3:=W.uget (w3+i) (wi w3 i m W h3 hs him) go4C f T hT x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 (a+v*dvN T hT u0) (b+f v u1) (c+v*dvN T hT u2) (d+f v u3) (qZw q u0 u1 u2 u3) else go4C f T hT x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 a b c d q else ⟨a,b,c,d,q⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) structure PQ where (p q:ѓ) def go4U (iz W:ѐ) (w0 w1 w2 w3 m n t:є) (hi:Ѡn<=ѡiz) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (p q:ѓ):PQ := if h:t< n then have ht:=А.mp h let i:=(iz.uget t א).toUSize if hm:i< m then have him:=А.mp hm let u0:=W.uget (w0+i) (wi w0 i m W h0 hs him) let u1:=W.uget (w1+i) (wi w1 i m W h1 hs him) let u2:=W.uget (w2+i) (wi w2 i m W h2 hs him) let u3:=W.uget (w3+i) (wi w3 i m W h3 hs him) go4U iz W w0 w1 w2 w3 m n (t+1) hi hs h0 h1 h2 h3 (p ||| u0 ||| u1 ||| u2 ||| u3) (qZw q u0 u1 u2 u3) else go4U iz W w0 w1 w2 w3 m n (t+1) hi hs h0 h1 h2 h3 p q else ⟨p,q⟩ termination_by Ѡn-Ѡt decreasing_by all_goals (rw[usz t n h];omega) @[specialize] def colsC (f:ѕ->ѓ->ѕ) (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b ix iz:ѐ) (x:ђ) (nf l:ѕ) (m n z:є) (j:ћ) (out:ѐ) (p q:ѓ):CN := if h:j< outW∧4<=outW then let k:=if j+4<=outW then j else outW-4 let ju:=Ѣk let w0:=ju*m let w1:=(ju+1)*m let w2:=(ju+2)*m let w3:=(ju+3)*m if hg:Ѡn<=ѡx∧Ѡn<=ѡix∧Ѡz<=ѡiz∧ѡW<=4294967295∧Ѡw0+Ѡm<=ѡW ∧ Ѡw1+Ѡm<=ѡW∧Ѡw2+Ѡm<=ѡW∧Ѡw3+Ѡm<=ѡW then let r:=go4C f T hT x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.2.1) hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2.1 hg.2.2.2.2.2.2.2 0 0 0 0 q if (r.a+r.b+r.c+r.d).isFinite then let u:=go4U iz W w0 w1 w2 w3 m z 0 hg.2.2.1 (qA9 hg.2.2.2.1) hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2.1 hg.2.2.2.2.2.2.2 p r.q let o0:=fin8 inW relu X W b 0 k r.a nf l hv (rdW b ju) m let o1:=fin8 inW relu X W b 0 (k+1) r.b nf l hv (rdW b (ju+1)) m let o2:=fin8 inW relu X W b 0 (k+2) r.c nf l hv (rdW b (ju+2)) m let o3:=fin8 inW relu X W b 0 (k+3) r.d nf l hv (rdW b (ju+3)) m let out:=wrW out ju o0 let out:=wrW out (ju+1) o1 let out:=wrW out (ju+2) o2 let out:=wrW out (ju+3) o3 colsC f T hT hv inW outW relu X W b ix iz x nf l m n z (k+4) out u.p u.q else ⟨out,false⟩ else ⟨out,false⟩ else ⟨out,decide (outW<=j) && decide (0x7FFFFF<=q) && decide (p &&& В<У)⟩ termination_by outW-j decreasing_by split<;>omega @[inline] def colsG (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b ix iz:ѐ) (x:ђ) (nf l:ѕ) (m n z:є) (j:ћ) (out:ѐ) (p q:ѓ):CN := if armT then colsC (dvNB T hT) T hT hv inW outW relu X W b ix iz x nf l m n z j out p q else colsC (dvU T hT) T hT hv inW outW relu X W b ix iz x nf l m n z j out p q @[inline] def colsA (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b ix:ѐ) (x:ђ) (nf l:ѕ) (n c:є):CN := if armT then let s:=decZ X (0*inW).toUSize n 0 (К inW 0) 0 colsG T hT hv inW outW relu X W b ix s.iz x nf l n c s.z 0 (К (1*outW) 0) 0 Е else ⟨К 0 0,false⟩ @[inline] def hasZ (W:ѐ):ј:= rdW W 0 &&& В == 0 || rdW W 1 &&& В == 0 || rdW W 2 &&& В == 0 || rdW W 3 &&& В == 0 structure F8 where (u:UInt8) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (p q:ѓ) lemma xi (xs:ђ) (i t n:є) (k:ћ) (hk:k<8) (ht:Ѡt< Ѡn) (hi:Ѡi=8*Ѡt) (hx:8*Ѡn<=ѡxs) (hs:ѡxs<2^32) : (i+USize.ofNat k).toNat< ѡxs:=by rw[qAf _ _ (by rw[uk k א];omega),uk k א];omega def go8Z (T:ђ) (hT:ѡT=512) (xs:ђ) (W:ѐ) (wo n t i:є) (hx:8*Ѡn<=ѡxs) (hsx:ѡxs<2^32) (hs:ѡW<2^32) (hw:Ѡwo+Ѡn<=ѡW) (hi:Ѡi=8*Ѡt) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (q:ѓ) (r:F8):F8 := if h:t< n then have ht:=А.mp h let w:=W.uget (wo+t) (wi wo t n W hw hs ht) let v:=dvN T hT w go8Z T hT xs W wo n (t+1) (i+USize.ofNat 8) hx hsx hs hw (by rw[usz t n h,qAf _ _ (by rw[uk 8 א];omega),uk 8 א];omega) (s0+xs.uget (i+USize.ofNat 0) (xi xs i t n 0 א ht hi hx hsx)*v) (s1+xs.uget (i+USize.ofNat 1) (xi xs i t n 1 א ht hi hx hsx)*v) (s2+xs.uget (i+USize.ofNat 2) (xi xs i t n 2 א ht hi hx hsx)*v) (s3+xs.uget (i+USize.ofNat 3) (xi xs i t n 3 א ht hi hx hsx)*v) (s4+xs.uget (i+USize.ofNat 4) (xi xs i t n 4 א ht hi hx hsx)*v) (s5+xs.uget (i+USize.ofNat 5) (xi xs i t n 5 א ht hi hx hsx)*v) (s6+xs.uget (i+USize.ofNat 6) (xi xs i t n 6 א ht hi hx hsx)*v) (s7+xs.uget (i+USize.ofNat 7) (xi xs i t n 7 א ht hi hx hsx)*v) (q &&& ((w &&& В)-1)) r else match r with | ⟨u,_,_,_,_,_,_,_,_,_,_⟩=>⟨u,s0,s1,s2,s3,s4,s5,s6,s7,0,q⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];omega def go8N (T:ђ) (hT:ѡT=512) (xs:ђ) (W:ѐ) (wo n t i:є) (hx:8*Ѡn<=ѡxs) (hsx:ѡxs<2^32) (hs:ѡW<2^32) (hw:Ѡwo+Ѡn<=ѡW) (hi:Ѡi=8*Ѡt) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (r:F8):F8 := if h:t< n then have ht:=А.mp h let v:=dvN T hT (W.uget (wo+t) (wi wo t n W hw hs ht)) go8N T hT xs W wo n (t+1) (i+USize.ofNat 8) hx hsx hs hw (by rw[usz t n h,qAf _ _ (by rw[uk 8 א];omega),uk 8 א];omega) (s0+xs.uget (i+USize.ofNat 0) (xi xs i t n 0 א ht hi hx hsx)*v) (s1+xs.uget (i+USize.ofNat 1) (xi xs i t n 1 א ht hi hx hsx)*v) (s2+xs.uget (i+USize.ofNat 2) (xi xs i t n 2 א ht hi hx hsx)*v) (s3+xs.uget (i+USize.ofNat 3) (xi xs i t n 3 א ht hi hx hsx)*v) (s4+xs.uget (i+USize.ofNat 4) (xi xs i t n 4 א ht hi hx hsx)*v) (s5+xs.uget (i+USize.ofNat 5) (xi xs i t n 5 א ht hi hx hsx)*v) (s6+xs.uget (i+USize.ofNat 6) (xi xs i t n 6 א ht hi hx hsx)*v) (s7+xs.uget (i+USize.ofNat 7) (xi xs i t n 7 א ht hi hx hsx)*v) r else match r with | ⟨u,_,_,_,_,_,_,_,_,_,_⟩=>⟨u,s0,s1,s2,s3,s4,s5,s6,s7,0,0⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];omega structure H8 where (xs:ђ) (h0 h1 h2 h3 h4 h5 h6 h7:ѕ) def z0:ђ:=pushZ 128 0 (Й 128) def z1:ђ:=pushZ 1024 0 (Й 1024) def z4:ђ:=pushZ 4096 0 (Й 4096) def z8:ђ:=pushZ 8192 0 (Й 8192) @[inline] def zFor (m:ћ):ђ:= if m<=128 then z0 else if m<=1024 then z1 else if m<=4096 then z4 else if m<=8192 then z8 else pushZ Ѣm 0 (Й m) @[inline] def wr8 (xs:ђ) (i:є) (a0 a1 a2 a3 a4 a5 a6 a7:ѕ):ђ := wrF (wrF (wrF (wrF (wrF (wrF (wrF (wrF xs (i+USize.ofNat 0) a0) (i+USize.ofNat 1) a1) (i+USize.ofNat 2) a2) (i+USize.ofNat 3) a3) (i+USize.ofNat 4) a4) (i+USize.ofNat 5) a5) (i+USize.ofNat 6) a6) (i+USize.ofNat 7) a7 def dec8 (X:ѐ) (n t:є) (xs:ђ) (h0 h1 h2 h3 h4 h5 h6 h7:ѕ):H8 := if h:t< n then let v0:=dv (rdW X t) let v1:=dv (rdW X (n+t)) let v2:=dv (rdW X (2*n+t)) let v3:=dv (rdW X (3*n+t)) let v4:=dv (rdW X (4*n+t)) let v5:=dv (rdW X (5*n+t)) let v6:=dv (rdW X (6*n+t)) let v7:=dv (rdW X (7*n+t)) dec8 X n (t+1) (wr8 xs (8*t) v0 v1 v2 v3 v4 v5 v6 v7) (h0+v0.abs) (h1+v1.abs) (h2+v2.abs) (h3+v3.abs) (h4+v4.abs) (h5+v5.abs) (h6+v6.abs) (h7+v7.abs) else ⟨xs,h0,h1,h2,h3,h4,h5,h6,h7⟩ termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];have:=А.mp h;omega def cols8N (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b:ѐ) (d:H8) (nf:ѕ) (n:є) (j:ћ) (out:ѐ) (r0:F8):CN := if hj:j< outW then let ju:=Ѣj let wo:=ju*n if hg:8*Ѡn<=d.xs.size∧d.xs.size<=4294967295∧ѡW<=4294967295∧Ѡwo+Ѡn<=ѡW then let r:=go8N T hT d.xs W wo n 0 0 hg.1 (qA9 hg.2.1) (qA9 hg.2.2.1) hg.2.2.2 (by rw[show (0:є).toNat=0 from rfl]) 0 0 0 0 0 0 0 0 r0 if (r.s0+r.s1+r.s2+r.s3+r.s4+r.s5+r.s6+r.s7).isFinite then let bw:=rdW b ju let ow:=ѢoutW let out:=wrW out ju (fin8 inW relu X W b 0 j r.s0 nf d.h0 hv bw n) let out:=wrW out (ow+ju) (fin8 inW relu X W b 1 j r.s1 nf d.h1 hv bw n) let out:=wrW out (2*ow+ju) (fin8 inW relu X W b 2 j r.s2 nf d.h2 hv bw n) let out:=wrW out (3*ow+ju) (fin8 inW relu X W b 3 j r.s3 nf d.h3 hv bw n) let out:=wrW out (4*ow+ju) (fin8 inW relu X W b 4 j r.s4 nf d.h4 hv bw n) let out:=wrW out (5*ow+ju) (fin8 inW relu X W b 5 j r.s5 nf d.h5 hv bw n) let out:=wrW out (6*ow+ju) (fin8 inW relu X W b 6 j r.s6 nf d.h6 hv bw n) let out:=wrW out (7*ow+ju) (fin8 inW relu X W b 7 j r.s7 nf d.h7 hv bw n) cols8N T hT hv inW outW relu X W b d nf n (j+1) out r else ⟨out,false⟩ else ⟨out,false⟩ else ⟨out,true⟩ termination_by outW-j def cols8Z (T:ђ) (hT:ѡT=512) (hv:ѓ) (inW outW:ћ) (relu:ј) (X W b:ѐ) (d:H8) (nf:ѕ) (n:є) (j:ћ) (out:ѐ) (q:ѓ) (r0:F8):CN := if hj:j< outW then let ju:=Ѣj let wo:=ju*n if hg:8*Ѡn<=d.xs.size∧d.xs.size<=4294967295∧ѡW<=4294967295∧Ѡwo+Ѡn<=ѡW then let r:=go8Z T hT d.xs W wo n 0 0 hg.1 (qA9 hg.2.1) (qA9 hg.2.2.1) hg.2.2.2 (by rw[show (0:є).toNat=0 from rfl]) 0 0 0 0 0 0 0 0 q r0 if (r.s0+r.s1+r.s2+r.s3+r.s4+r.s5+r.s6+r.s7).isFinite then let bw:=rdW b ju let ow:=ѢoutW let out:=wrW out ju (fin8 inW relu X W b 0 j r.s0 nf d.h0 hv bw n) let out:=wrW out (ow+ju) (fin8 inW relu X W b 1 j r.s1 nf d.h1 hv bw n) let out:=wrW out (2*ow+ju) (fin8 inW relu X W b 2 j r.s2 nf d.h2 hv bw n) let out:=wrW out (3*ow+ju) (fin8 inW relu X W b 3 j r.s3 nf d.h3 hv bw n) let out:=wrW out (4*ow+ju) (fin8 inW relu X W b 4 j r.s4 nf d.h4 hv bw n) let out:=wrW out (5*ow+ju) (fin8 inW relu X W b 5 j r.s5 nf d.h5 hv bw n) let out:=wrW out (6*ow+ju) (fin8 inW relu X W b 6 j r.s6 nf d.h6 hv bw n) let out:=wrW out (7*ow+ju) (fin8 inW relu X W b 7 j r.s7 nf d.h7 hv bw n) cols8Z T hT hv inW outW relu X W b d nf n (j+1) out r.q r else ⟨out,false⟩ else ⟨out,false⟩ else ⟨out,decide (0x7FFFFF<=q)⟩ termination_by outW-j /-! Weight setup cache: inside one process a layer's weights stay fixed, so their decoded form and bound are built once per weight array and reused by later calls; outputs are unchanged. -/ def wdOf (W:ѐ):ђ:=W.foldl (λa w=>a.push (dv w)) (Й ѡW) abbrev WV (W:ѐ):=({p:ђ×ѓ//p=(wdOf W,scanH 1 W)}) local instance wvSub (W:ѐ):Subsingleton (WV W):=⟨λa b=>Subtype.ext (a.2.trans b.2.symm)⟩ structure WE where (w:ѐ) (p:ђ×ѓ) (g:є) (ok:p=(wdOf w,scanH 1 w)) def wdF (W:ѐ) (g:є) (s:Array WE) (i:ћ):Option (WV W):= if h:i< s.size then if s[i].g==g then if hk:withPtrEq s[i].w W (λ_=>s[i].w==W) (λe=>by rw[e];exact beq_self_eq_true W) then some ⟨s[i].p,by rw[←eq_of_beq hk];exact s[i].ok⟩ else wdF W g s (i+1) else wdF W g s (i+1) else none termination_by s.size-i @[noinline] def wdS:ST.Ref Unit (Array WE):=(ST.Prim.mkRef #[] (Void.mk ())).val @[inline] def wdGet (W:ѐ):WV W:= withPtrAddr W (λg=> let s:=(ST.Prim.Ref.get wdS (Void.mk ())).val match wdF W g s 0 with | some v=>v | none=> let p:=(wdOf W,scanH 1 W) let old:=(ST.Prim.Ref.swap wdS (#[⟨W,p,g,rfl⟩]++(if s.size<16 then s else s.pop)) (Void.mk ())).val match wdF W g old 0 with | some v=>v | none=>⟨p,rfl⟩) (λ_ _=>Subsingleton.elim _ _) def linF (rows inW outW:ћ) (relu:ј) (X W b:ѐ):ѐ := let hw:=scanW rows W if rows*inW=ѡX∧outW*inW=ѡW∧outW=ѡb∧0< inW∧0< outW∧inW<2^20 ∧ ѡX<=4294967295∧ѡW<=4294967295∧ѡb<=4294967295∧rows*outW<=4294967295 ∧ scanH 1 X≠Е∧hw≠Е∧scanH 1 b≠Е then let n:=ѢinW let nf:ѕ:=(inW.toUInt64+8).toFloat*О 0x3CA0000000000000 let out:ѐ:=К (rows*outW) 0 if rows == 8 then let d:=dec8 X n 0 (zFor (8*inW)) 0 0 0 0 0 0 0 0 let e:=cols8N (tN inW) (qTs (qTN inW)) (hvI inW) inW outW relu X W b d nf n 0 out ⟨0,0,0,0,0,0,0,0,0,0,0⟩ if e.ok then e.out else let c:=cols8Z (tZ inW) (qTs (qTZ inW)) (hvI inW) inW outW relu X W b d nf n 0 (К (8*outW) 0) Е ⟨0,0,0,0,0,0,0,0,0,0,0⟩ if c.ok then c.out else linearWith dotFn rows inW outW relu X W b else if rows == 1 then let r:=decR X (0*inW).toUSize n 0 (К inW 0) (zFor inW) 0 0 let p:=(wdGet W).1 if p.2≠Е then cols1 inW outW relu X W b 0 r.ix r.buf p.1 nf r.l p.2 n r.c 0 (0*outW).toUSize (К (1*outW) 0) else linearWith dotFn rows inW outW relu X W b else let wd:=if 8< rows then W.foldl (λa w=>a.push (dv w)) (Й ѡW) else Й 0 rows1 rows inW outW relu X W b wd nf hw n 0 (К inW 0) (pushZ n 0 (Й inW)) out else linearWith dotFn rows inW outW relu X W b section local notation:max x:max "ᶠ"=>Float.isFinite x=true local notation "⟡"=>FloatFormat.binary64 local notation "⟠"=>Float.Model.Format.binary64 noncomputable def R (f:ѕ):ℝ:=к (toModel (ofFloat f)) noncomputable def wv (w:ѓ):ℝ:=к (ofW w) noncomputable def exactS (n:ћ) (xw ww:ћ->ѓ) (bw:ѓ):ℝ := (л n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:ћ):ѕ:=(n.toUInt64+8).toFloat*О 0x3CA0000000000000 lemma qAj:⟡.isIEEE=true:=by decide lemma qAy (a:Model ⟡) : Ы a=(Model.toModel a).isFinite:=by rw[<-Model.toDyadic?_isSome_eq_isFinite,Model.toDyadic?_ieee_eq_model _ qAj];cases Model.toModel a<;>rfl abbrev A_M (x:ѕ):Model ⟡:=Model.ofFloatModel x.toModel lemma A_R (x:ѕ):R x=к (A_M x):=rfl lemma qAh (x:ѕ):Ы (A_M x)=x.isFinite:=by rw[qAy,ع];rfl lemma qA3 {x:ѕ} (h:xᶠ):Ы (A_M x)=true :=(qAh x).trans h lemma qA0 (e:ℤ):Formats.Flocq.bpow т e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma qCd:(2:ℝ)^(1000:ℤ)<=к (Model.posMaxFinite ⟡):=by rw[Model.toReal_posMaxFinite,show Model.pow2 ⟡.fracWidth + ⟡.maxFiniteFracField=2^53-1 by decide +kernel, show ⟡.maxNormalExponent-Int.ofNat ⟡.fracWidth=971 by decide +kernel,Model.bpow,qA0,show (1000:ℤ)=29+971 from rfl,Э Ч] exact Б ב (by positivity) open Float.Model.UnpackedFloat in lemma qDv {b:BitVec 64} {s m e h} (a:unpack ⟠ b=.finite s m e h) : 2^52<=m∨e=-1074:=by unfold Float.Model.UnpackedFloat.unpack at a;simp only at a;rw[show (⟠.exponentBias:ℤ)=1023 by decide] at a;split_ifs at a with h1 h2 h3;. injection a with _ _ he;rw[show (unpackExponent (spec:=⟠) b).toNat=0 by simp[h3]] at he;omega . injection a with _ hm;rw[<-hm,BitVec.toNat_append];exact .inl (ش (by simp[Nat.shiftLeft_eq]) Nat.left_le_or) open Float.Model.UnpackedFloat in lemma qH1 {s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂} {f g:BitVec 64} (e:unpack ⟠ f=.finite s₁ m₁ e₁ h₁) (b:unpack ⟠ g=.finite s₂ m₂ e₂ h₂) : e₁+e₂<=⟠.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have a:2^52<=m₁*m₂->52<=(m₁*m₂).log2:=(Nat.le_log2 (Nat.mul_ne_zero h₁.ne' h₂.ne')).2 simp only[Float.Model.Format.targetExponent,Float.Model.totalExponent,Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] norm_num rcases qDv e with c | c . have:=a (c.trans (ؽ _ h₂));omega rcases qDv b with d | d . have:=a (d.trans (Nat.le_mul_of_pos_left _ h₁));omega omega lemma qB0 (x y:ѕ) (a:xᶠ) (b:yᶠ) : A_M (x*y)=Model.mul (A_M x) (A_M y):=by change Model.ofFloatModel (Float.Model.mul x.toModel y.toModel)=_;rw[Float.Model.mul,Model.ofFloatModel_pack,Model.Proof.mul_eq_spec,Model.Spec.mul];simp only[Model.toDyadic?_ieee_eq_model _ qAj,Model.toModel_ofFloatModel];change x.toModel.unpack.isFinite=true at a;change y.toModel.unpack.isFinite=true at b;have c:∀ s t:Float.Model.UnpackedFloat.Sign,Model.ofModel ⟡ (.zero (s*t)) =Model.zero _ (э s ^^ э t):=λs t=>by cases s<;>cases t<;>simp[Model.modelSignBit] <;>first | exact (Model.negZero_eq_ofModel_zero _).symm | exact (Model.posZero_eq_ofModel_zero _).symm cases hxm:x.toModel.unpack<;>cases hym:y.toModel.unpack <;>simp only[hxm,hym,Float.Model.UnpackedFloat.isFinite,Bool.false_eq_true] at a b case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => have d:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] have e:Model.modelSign (э s₁ ^^ э s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul];rw[Model.roundWithAccuracy_exact_eq_round_of_le_targetExponent _ _ _ _ (qH1 hxm hym),d];simp only[Bool.false_eq_true,ite_false,Model.roundDyadic,qAj,ite_true,Model.ieeeRoundDyadic,e];rfl all_goals simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul,c] lemma A_E (z:ѕ) {a:Model ⟡} {c:ℝ} (h:A_M z=a) (b:Ы a=true) (d:к a=Model.roundAt ⟡ c) : zᶠ∧R z=Model.roundAt ⟡ c:=by subst h;exact ⟨(qAh z).symm.trans b,d⟩ lemma qEd (x y:ѕ) (hx:xᶠ) (hy:yᶠ) (hb : |R x| * |R y| ≤ 2^(1000:ℤ)) : (x*y)ᶠ ∧ R (x*y)=Model.roundAt ⟡ (R x*R y) := A_E _ (qB0 x y hx hy) (Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (hb.trans qCd)) (Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (hb.trans qCd)) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qAr:0< A_z:=by unfold A_z;positivity lemma A_p (a b c d:ℤ) (h:a+b<=c+d):(2:ℝ)^a*2^b<=2^c*2^d:=by rw[<-Э Ч,<-Э Ч];exact Ц one_le_two h lemma qAw:A_z<=1/2^200*(1/2^200):=by simpa [A_z] using A_p (-1021) 0 (-200) (-200) ב lemma qI1:0< A_B:=by unfold A_B;positivity lemma qCc:(2:ℝ)^256*2^42<=A_B:=by simpa [A_B] using A_p 256 42 300 0 ב lemma qAn:(2:ℝ)^128<=A_B :=(by norm_num:(2:ℝ)^128<=2^256*2^42).trans qCc lemma qE7 {k:ℕ} (h:k<=2^128):(k:ℝ)<=A_B :=(show (k:ℝ)<=2^128 by exact_mod_cast h).trans qAn lemma qE3:A_B*2<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 1 1000 0 ב lemma qDe:A_B*A_B<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 300 1000 0 ב lemma qFp (x:ℝ) : |Model.roundAt ⟡ x-x| ≤ 1/2^53*(|x| + A_z):=by rw[show (1/2^53*(|x| + A_z):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ) by rw[show (1/2^53:ℝ)=2^(-53:ℤ) by norm_num,show (-1074:ℤ)=-53+-1021 from rfl,Э Ч (-53),mul_add] rfl] by_cases a:x=0 . rw[a,Model.roundAt_zero,sub_self,abs_zero];positivity refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf ⟡) x/2<=_ rw[Formats.Flocq.ulp.of_ne_zero _ _ x a] simp only[Formats.Flocq.cexp,Model.fexpOf,Formats.Flocq.fltExp] have b:=Formats.Flocq.bpow_magnitude_sub_one_le т x a rw[qA0] at b rw[qA0,show ((⟡.fracWidth:ℤ)+1)=53 from rfl,show ⟡.minSubnormalExponent=-1074 from rfl] generalize Formats.Flocq.magnitude т x=M at b ⊢ have d:(0:ℝ)<=2^(-53:ℤ) * |x|:=by positivity have c:(0:ℝ)<2^(-1074:ℤ):=by positivity rcases le_total (-1074) (M-53) with h | h . rw[max_eq_left h,show (2:ℝ)^(M-53)/2=2^(-53:ℤ)*2^(M-1) by rw[<-Э Ч,show (-53:ℤ)+(M-1)=M-53-1 by ring,zpow_sub₀ Ч _ 1,zpow_one]] linarith [Г b (show (0:ℝ)<=2^(-53:ℤ) by positivity)] . rw[max_eq_right h];exact (half_le_self c.le).trans (le_add_of_nonneg_left d) lemma qFc {z:ѕ} {c:ℝ} (h:zᶠ∧R z=Model.roundAt ⟡ c) : zᶠ∧|R z-c| ≤ 1/2^53*(|c| + A_z) :=⟨h.1,h.2 ▸ qFp c⟩ lemma qEe {a b:ℝ} (d : |a| ≤ A_B) (c : |b| ≤ A_B) : |a| + |b| ≤ к (Model.posMaxFinite ⟡):=by have:=qE3 have:=qCd linarith section variable (x y:ѕ) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) include x y hx hy h1 h2 lemma qBf : (x+y)ᶠ∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) := qFc (A_E _ (toModel_ofFloat_add_of_isFinite x y hx hy) (Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2)) (Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2))) lemma qDf : (x-y)ᶠ∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) := qFc (A_E _ (toModel_ofFloat_sub_of_isFinite x y hx hy) (Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2)) (Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2))) lemma qCf : (x*y)ᶠ∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) :=qFc (qEd x y hx hy ((Х h1 h2 (Л _) qI1.le).trans qDe)) end lemma qA2 (m k:ℤ) (hm:m.natAbs<2^53) (hk:-1074<=k) : Model.roundAt ⟡ ((m:ℝ)*2^k)=(m:ℝ)*2^k:=by apply Formats.Flocq.round_preserves_generic;exact (Formats.Flocq.generic_format_FLT_iff (⟡.minSubnormalExponent) (⟡.fracWidth+1) (by decide) _).2 ⟨by decide,⟨m,k⟩,by simp[Formats.Flocq.toReal,qA0],hm,hk⟩ lemma qBk (x:ѕ):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,ж qAj,ع] lemma qE0 {f:ѕ} {s m e h} (c:f.toModel.unpack=.finite s m e h) : (Model.ofModel ⟡ (.finite .positive m e h)).toModel=.finite .positive m e h:=by have a:Model.toModel (A_M f)=.finite s m e h:=(ع _).trans c;cases s;. have b:=Model.toModel_neg_of_isFinite qAj (A_M f) (by rw[a];rfl);rw[a] at b;change _=Float.Model.UnpackedFloat.finite .positive m e h at b;rw[<-b];exact Model.toModel_canonicalizeModel _ . rw[<-a];exact Model.toModel_canonicalizeModel _ lemma qBl (f:ѕ) (hf:fᶠ):f.absᶠ∧R f.abs = |R f|:=by have a:A_M f.abs=Model.ofModel ⟡ f.toModel.unpack.abs :=Model.ofFloatModel_pack _;change f.toModel.unpack.isFinite=true at hf;cases hu:f.toModel.unpack with | notANumber | infinity _=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | zero s => rw[hu] at a;refine ⟨by rw[<-qAh,a];exact Model.isFinite_ofModel_zero _ qAj _,?_⟩;rw[A_R f.abs,a,qBk f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAj] | finite s m e h => have b:=qE0 hu;rw[hu] at a;have c:A_M f.abs=Model.ofModel ⟡ (.finite .positive m e h):=a;refine ⟨by rw[<-qAh,c,qAy,b];rfl,?_⟩;rw[A_R f.abs,c,qBk f,hu,ж qAj,b];simp only[Model.unpackedToReal_finite,qA0];cases s<;>simp[Model.modelSignBit,abs_of_nonneg (by positivity:(0:ℝ)<=(m:ℝ)*2^e)] lemma qCl (f:ѕ) (k:ℕ) (g:k<2^53) (e:f.toModel=Float.Model.pack (Float.Model.UnpackedFloat.ofNat ⟠ k)) : fᶠ∧R f=k:=by obtain ⟨d,b,c⟩:∃ d,A_M f=Щ ⟡ d∧d.toReal=k:=by refine ⟨.ofScaledInt k 0,?_,?_⟩;. change Model.ofFloatModel f.toModel=_;rw[e,Model.ofFloatModel_pack];exact Model.ofModel_ofInt_eq_roundDyadic _ qAj (k:ℤ) . rw[<-и,FloatLib.Numerics.Dyadic.ofScaledInt_toRat];simp have a:Ы (A_M f)=true:=by rw[b];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qAj;rw[c,И (Ю _)];exact ((qE7 (א:k<=2^128)).trans ((le_mul_of_one_le_right qI1.le one_le_two).trans qE3)).trans qCd refine ⟨(qAh f).symm.trans a,?_⟩;rw[A_R,b,Model.toReal_roundDyadic_eq_roundAt _ qAj _ (b ▸ a),c];simpa using qA2 k 0 (by simpa using g) ב lemma qCk (m:і) (h:Ѡm<2^53):m.toFloatᶠ∧R m.toFloat=Ѡm :=qCl _ _ h rfl lemma qHg:(0:ѕ)ᶠ∧R 0=0:=by simpa using qCl 0 0 ב rfl def A_bits (i:ℕ):і := let e:=(i%256).toUInt64 let s:і:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:і)) lemma qCu (i:ℕ):pwEntry i=О (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):ј := match я ⟠ (A_bits i).toBitVec with | .finite s m e _=>(э s == decide (256<=i)) && m == 2^52 && e == ((i%256:ℕ):ℤ)-202 && (i%256 != 0) | .zero _=>i%256 == 0 | _=>false lemma qEm:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qDa (i:ℕ) (b:i<512) : (pwEntry i)ᶠ∧R (pwEntry i) =if i%256=0 then 0 else (if 256<=i then -1 else 1)*2^(((i%256:ℕ):ℤ)-150):=by have a:=qEm i b unfold A_chk at a rw[qCu,qBk] change (О (A_bits i)).toModel.unpack.isFinite=true∧_ rw[show (О (A_bits i)).toModel.unpack =я ⟠ (A_bits i).toBitVec from Model.unpack_pack_unpack _ _] generalize я ⟠ (A_bits i).toBitVec=u at a cases u with | notANumber | infinity _=>simp at a | zero s => simp only[beq_iff_eq] at a exact ⟨rfl,by simp[a]⟩ | finite s m e h => simp only[Bool.and_eq_true,beq_iff_eq,bne_iff_ne] at a obtain ⟨⟨⟨g,d⟩,f⟩,c⟩:=a refine ⟨rfl,?_⟩ rw[Model.unpackedToReal_finite,qA0,f,g,d,mul_assoc, show ((2^52:ℕ):ℝ)*2^(((i%256:ℕ):ℤ)-202) =2^(((i%256:ℕ):ℤ)-150) by rw[Nat.cast_pow,Nat.cast_ofNat,<-й,<-Э Ч] congr 1 push_cast ring] simp only[decide_eq_true_eq,c,↓reduceIte] lemma qCw (i:ℕ) (a:i<512):∃ σ:ℤ,σ.natAbs<=1 ∧ R (pwEntry i)=σ*2^(((i%256:ℕ):ℤ)-150):=by rw[(qDa i a).2];split_ifs;exacts [⟨0,by norm_num,by simp⟩,⟨-1,by norm_num,by simp⟩,⟨1,by norm_num,by simp⟩] lemma qCm (f:ѕ) (i:ℕ) (g:i<512) (q:fᶠ) (c k:ℤ) (r:c.natAbs<2^53) (s:-900<=k) (m : |R f| ≤ A_B) (l:R f=c*2^k) : (f*pwEntry i)ᶠ∧R (f*pwEntry i)=R f*R (pwEntry i):=by obtain ⟨b,a,d⟩:=qCw i g;have o:((i%256:ℕ):ℤ)-150<=128∧-150<=((i%256:ℕ):ℤ)-150 :=א;generalize ((i%256:ℕ):ℤ)-150=E at o d;have p : |(b:ℝ)| ≤ 1:=by rw[<-Int.cast_abs,<-Nat.cast_natAbs];exact_mod_cast a have e:=х (two_pos (α:=ℝ)) E;have h : |R (pwEntry i)| ≤ A_B:=by rw[d,в,б e,<-one_mul A_B];exact Х p (Ц one_le_two א) e.le zero_le_one obtain ⟨j,n⟩:=qEd f _ q (qDa i g).1 ((Х m h (Л _) qI1.le).trans qDe);refine ⟨j,?_⟩ have:=qA2 (c*b) (k+E) (by rw[Int.natAbs_mul];have:=Nat.mul_le_mul_left c.natAbs a omega) א rw[Э Ч] at this;push_cast at this;rw[n,d,l,show (c:ℝ)*2^k*(b*2^E)=c*b*(2^k*2^E) by ring];exact this lemma qDc (i:є) (h:Ѡi< ѡpw):pw.uget i h=pwEntry Ѡi:=by simp[FloatArray.uget,qDx] section variable (w:ѓ) include w lemma qAq:(w &&& В).toNat=Ѡw%2^31:=by rw[п];exact Н Ѡw 31 lemma qCo:(w &&& 0x7FFFFF).toNat=Ѡw%2^23:=by rw[п];exact Н Ѡw 23 lemma qD8:(w >>> 23).toNat=Ѡw/2^23:=by rw[UInt32.toNat_shiftRight,щ];rfl lemma qCa:(expOf w).toNat=Ѡw/2^23%2^8:=by unfold expOf;rw[п,qD8];exact Н _ 8 lemma qGl:signOf w=decide (2^31<=Ѡw):=by have b:=w.toNat_lt;unfold signOf;have h:((w &&& س).toNat)=(Ѡw/2^31%2)*2^31:=by rw[п];show Ѡw &&& 2^31=_;rw[Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];have a:Ѡw/2^31%2=0∨Ѡw/2^31%2=1 :=א;rcases a with a | a<;>rw[a]<;>rfl rw[Bool.eq_iff_iff];simp only[bne_iff_ne,ne_eq,decide_eq_true_eq];rw[<-а,h];simp only[UInt32.toNat_zero];omega lemma qHa : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+Ѡw%2^23:=by rw[UInt32.toNat_or,qCo,Nat.or_comm];simpa using (Nat.two_pow_add_eq_or_of_lt (Nat.mod_lt Ѡw (by positivity:2^23 > 0)) 1).symm lemma qAz (h:good w=true) : Ѡw%2^31=0∨(2^23<=Ѡw%2^31∧Ѡw%2^31<У):=by have a:=qAq w;have b:=(w &&& В).toNat_lt;unfold good at h;simp only[Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-a,h];rfl . right;rw[Ж,UInt32.toNat_sub] at h;rw[<-a];generalize (w &&& В).toNat=v at *;simp at h;omega lemma qAe (h:good w=true):expOf w≠255:=by intro he;have a:=qCa w;rw[he] at a;change 255=_ at a;have:=qAz w h;have:=w.toNat_lt;omega end noncomputable def A_mag (v:ℕ):ℝ := if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma qAs (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qA7 (w:ѓ) (hg:good w=true) : wv w=(if 2^31<=Ѡw then -1 else 1)*A_mag (Ѡw%2^31):=by have g:=w.toNat_lt;have:=qAz w hg;have a:=qCa w;have d:(fracOf w).toNat=Ѡw%2^23:=qCo w;have f:wv w=(decodeD w).toReal:=by unfold wv ofW;rw[Model.toReal_eq,ц w (qAe w hg)] rw[f];unfold decodeD A_mag;by_cases c:Ѡw%2^31=0;. have b:expOf w=0:=а.mp (by rw[a];show _=0;omega);have e:fracOf w=0:=а.mp (by rw[d];show _=0;omega);simp only[b,e,c,↓reduceIte];simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] . have b:expOf w≠0:=λh=>by rw[h] at a;change 0=_ at a;omega simp only[b,c,↓reduceIte];rw[show Ѡw%2^31%2^23=(fracOf w).toNat by rw[d];omega,show Ѡw%2^31/2^23=(expOf w).toNat by rw[a];omega];simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,qGl];simp;split_ifs<;>ring lemma qFb (v:ℕ) (h:v<У):A_mag v<=2^128:=by unfold A_mag;split_ifs with h0;. positivity . exact (Х (show ((2^23+v%2^23:ℕ):ℝ)<=((2^24:ℕ):ℝ) from Nat.cast_le.2 א) (Ц one_le_two (by omega:_<=(104:ℤ))) (by positivity) (by positivity)).trans_eq ב lemma qA6 (a b:ℕ) (hab:a<=b) (ha:a=0∨2^23<=a):A_mag a<=A_mag b:=by rcases ha with rfl | ha;. simpa [A_mag] using qAs b unfold A_mag;simp only[show ¬ a=0 by omega,show ¬ b=0 by omega,↓reduceIte];have c:a/2^23<=b/2^23:=Nat.div_le_div_right hab;rcases c.eq_or_lt with d | g;. rw[d];exact Б (Nat.cast_le.2 א) (by positivity) . have e:(2:ℝ)^(((a/2^23:ℕ):ℤ)-150)*2<=2^(((b/2^23:ℕ):ℤ)-150):=by rw[<-zpow_add_one₀ Ч];exact Ц one_le_two א have f:((2^23:ℕ):ℝ)<=((2^23+b%2^23:ℕ):ℝ):=Nat.cast_le.2 א calc _<=((2^23*2:ℕ):ℝ)*2^(((a/2^23:ℕ):ℤ)-150) := Б (Nat.cast_le.2 א) (by positivity) _=((2^23:ℕ):ℝ)*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by push_cast;ring _<=_:=Х f e (by positivity) (by positivity) abbrev A_mf (w:ѓ):ѕ:=((w &&& 0x7FFFFF) ||| 0x800000).toFloat lemma qD1 (w:ѓ):pw.uget (w >>> 23).toUSize (qCh w) =pwEntry (Ѡw/2^23)∧Ѡw/2^23<512∧(A_mf w)ᶠ ∧ (A_mf w*pwEntry (Ѡw/2^23))ᶠ ∧ R (A_mf w*pwEntry (Ѡw/2^23))=R (A_mf w)*R (pwEntry (Ѡw/2^23)) ∧ R (A_mf w)=((2^23+Ѡw%2^23:ℕ):ℝ):=by have g:=w.toNat_lt;have c:=qHa w;obtain ⟨d,a⟩:=qCl (A_mf w) _ (by rw[c];omega) rfl;rw[c] at a;have b:Ѡw/2^23<512 :=א obtain ⟨f,e⟩:=qCm _ _ b d ((2^23+Ѡw%2^23:ℕ):ℤ) 0 א ב (by rw[a,И (Ю _)] exact qE7 א) (by rw[a,zpow_zero,mul_one,Int.cast_natCast]) refine ⟨?_,b,d,f,e,a⟩;rw[qDc,ص,qD8] lemma qAm (w:ѓ) (hg:good w=true):(dv w)ᶠ∧R (dv w)=wv w:=by obtain ⟨g,a,-,e,d,c⟩:=qD1 w;have j:=w.toNat_lt;unfold dv;rw[g];refine ⟨e,?_⟩;rw[d,c,(qDa _ a).2,qA7 w hg];have b:=qAz w hg;generalize Ѡw=v at *;rcases b with h | ⟨l,k⟩;. rw[h];simp only[show v/2^23%256=0 by omega,↓reduceIte,mul_zero,show A_mag 0=0 by simp[A_mag]] . have f:(256<=v/2^23) ↔ 2^31<=v :=א;unfold A_mag;rw[(א:v%2^31/2^23=v/2^23%256),(א:v%2^31%2^23=v%2^23),if_neg (א:¬ v/2^23%256=0),if_neg (א:¬ v%2^31=0)];by_cases i:2^31<=v<;>simp only[f,i,↓reduceIte]<;>ring def A_fsum (l:List ѕ):ѕ:=l.foldl (.+·) 0 noncomputable def A_sa (l:List ѕ):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List ѕ):ℝ:=(l.map R).sum lemma qC4 {a:Type} (l:List a) (f:a->ℝ):0<=(l.map λq=>|f q|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨q,_,rfl⟩;exact Л _) lemma qCe (l:List ѕ):0<=A_sa l:=qC4 l R lemma qGr (l:List ѕ) (q:ѕ):A_sa (l ++ [q])=A_sa l + |R q| ∧ A_sr (l ++ [q])=A_sr l+R q∧A_fsum (l ++ [q])=A_fsum l+q:=by simp[A_sa,A_sr,A_fsum] lemma qC7 (l:List ѕ) : |A_sr l| ≤ A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => simp only[A_sa,A_sr,List.map_cons,List.sum_cons] at ih ⊢;exact (Ъ _ _).trans ד lemma qBo:A_z<=1:=qAw.trans ב section variable (l:List ѕ) (hf:∀ q∈l,qᶠ) (hlen:ѣl<=2^21) (hb:A_sa l<=A_B/4) include l hf hlen hb lemma qDd : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ (1+1/2^22)*ѣl*(1/2^53*(A_sa l+A_z)):=by induction l using List.reverseRecOn with | nil=>exact ⟨qHg.1,by simp[A_fsum,qHg.2,A_sr]⟩ | append_singleton l q ih => obtain ⟨e,f,n⟩:=qGr l q rw[e] at hb rw[List.length_append,List.length_singleton] at hlen have t:=qCe l have j:=Л (R q) obtain ⟨h,v⟩:=ih (λx i=>hf x (by simp[i])) א ד have u:(ѣl:ℝ)+1<=2^21:=by exact_mod_cast hlen have d:(0:ℝ)<=ѣl:=Ю _ have y:=qC7 l have o:=qAr have m:=qBo have p:=qAn have s:=abs_sub_abs_le_abs_sub (R (A_fsum l)) (A_sr l) have w:=Б (show (ѣl:ℝ)<=2^21 by linarith) (ד:0<=A_sa l+A_z) obtain ⟨b,z⟩:=qBf _ q h (hf q ג) ד ד rw[n,e,f,List.length_append,List.length_singleton] refine ⟨b,?_⟩ push_cast generalize R (A_fsum l)=r at * generalize R q=a at * generalize A_sa l=P at * generalize A_sr l=S at * generalize (ѣl:ℝ)=k at * obtain ⟨W,g⟩:∃ W:ℝ,W=1/2^53*(P + |a| + A_z):=⟨_,rfl⟩ have c:0<=W :=ד rw[<-g] refine (abs_sub_le _ (r+a) (S+a)).trans ?_ rw[add_sub_add_right_eq_sub] linarith [Ъ r a,Г (show 1/2^53*(P+A_z)<=W by linarith) d,Б (ד:k<=2^21) c] lemma qBz : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ 2*ѣl*(1/2^53*(A_sa l+A_z)):=by obtain ⟨a,b⟩:=qDd l hf hlen hb have c:=С (Nat.cast_nonneg (α:=ℝ) ѣl) (add_nonneg (qCe l) qAr.le) exact ⟨a,b.trans ד⟩ end lemma qB7 (l:List ѕ) (c:∀ q∈l,qᶠ) (d:ѣl<=2^21) (f:A_sa l<=A_B/4) : (A_fsum (l.map Float.abs))ᶠ ∧ |R (A_fsum (l.map Float.abs))-A_sa l| ≤ 2*ѣl*(1/2^53*(A_sa l+A_z)):=by have a:A_sa (l.map Float.abs)=A_sa l∧A_sr (l.map Float.abs)=A_sa l:=by constructor<;>simp only[A_sa,A_sr,List.map_map]<;>congr 1 <;>exact Ա λq b=>by simp[(qBl q (c q b)).2] have:=qBz (l.map Float.abs) (λq b=>by obtain ⟨x,e,rfl⟩:=List.mem_map.mp b exact (qBl x (c x e)).1) (by simpa) (a.1.symm ▸ f) rwa [a.1,a.2,List.length_map] at this lemma qIn (n:ℕ) (g:n<2^20) : (nfOf n)ᶠ∧R (nfOf n)=((n:ℝ)+8)/2^53:=by have b:(n.toUInt64+8).toNat=n+8:=by have c:(8:і).toNat=8:=rfl;simp[UInt64.toNat_add,c];omega obtain ⟨f,a⟩:=qCk (n.toUInt64+8) (by rw[b];omega);rw[b] at a obtain ⟨e,d⟩:=qCm _ 97 ב f ((n+8:ℕ):ℤ) 0 א ב (by rw[a,И (Ю _)] exact qE7 א) (by rw[a];simp) push_cast at a;rw[show nfOf n=(n.toUInt64+8).toFloat*pwEntry 97 by rw[nfOf,qCu];rfl];refine ⟨e,?_⟩;rw[d,a,(qDa 97 ב).2];norm_num;ring lemma qGb {n:ℕ} (l:n<2^20) {s t:ѕ} (k:sᶠ) (m:tᶠ) {X Y:ℝ} (e:0<=Y) (v:Y<=A_B/8) (y : |R s-X| ≤ ((n:ℝ)+4)*(1/2^53*Y)+1/2^250) (u : |R t-Y| ≤ Y/2^30+1/2^250) (I : |R s| ≤ 2*Y) (c:(2:ℝ)^(-127:ℤ) ≤ |R (s+nfOf n*t)|) : R (s-nfOf n*t)< X∧X< R (s+nfOf n*t):=by have w:(8:ℝ)<=A_B:=(by norm_num:(8:ℝ)<=2^128).trans qAn;have A:(n:ℝ)+1<=2^20:=by exact_mod_cast l have o:(0:ℝ)<=n:=Ю n;obtain ⟨h,b⟩:=qIn n l;have E:=qAr;have B:=qAw;have a:0<=((n:ℝ)+8)/2^53:=by positivity have C:=abs_sub_abs_le_abs_sub (R t) Y;rw[И e] at C;have i : |R t| ≤ 2*Y+1/2^250 :=ד;have x : |R (nfOf n)*R t| ≤ 1/2^30*(2*Y+1/2^250):=by rw[в,b,И a];exact Х ד i (Л _) ב obtain ⟨d,f⟩:=qCf _ _ h m (by rw[b,И a];linarith) ד;have D:=abs_sub_abs_le_abs_sub (R (nfOf n*t)) (R (nfOf n)*R t);obtain ⟨z,j⟩:=qBf s _ k d ד ד;obtain ⟨r,g⟩:=qDf s _ k d ד ד;have H:=abs_sub_abs_le_abs_sub (R (s+nfOf n*t)) (R s+R (nfOf n*t));have q:=Ъ (R s) (R (nfOf n*t));have p:=ى (R s) (R (nfOf n*t));rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at c;have G:=Г (abs_le.mp u).1 a;have M:=Б (show (n:ℝ)<=2^20 by linarith) e;rw[b] at f x D;obtain ⟨K,J⟩:=abs_le.mp y;obtain ⟨O,N⟩:=abs_le.mp j;obtain ⟨F,L⟩:=abs_le.mp g;obtain ⟨P,-⟩:=abs_le.mp f;constructor<;>linarith lemma qEv (g h:ℕ->ѕ) (l:List ℕ) (a b:ѕ) : l.foldl (λ(d:F1) t=>F1.mk (d.s+g t) (d.t+h t)) ⟨a,b⟩ =⟨l.foldl (λ(c:ѕ) t=>c+g t) a,l.foldl (λ(c:ѕ) t=>c+h t) b⟩:=by induction l generalizing a b with | nil=>rfl | cons x l ih=>simp only[List.foldl_cons,ih] lemma qC9 (a:List ℕ) (f:ℕ->ℝ) (M:ℝ) (h:∀ t∈a,f t<=M) : (a.map f).sum<=ѣa*M:=by simpa using List.sum_le_sum h section variable (w:ѓ) (hg:good w=true) include w hg lemma qB5 : |wv w| = A_mag (Ѡw%2^31):=by rw[qA7 w hg,в,И (qAs _)];split_ifs<;>simp lemma qAo : |wv w| ≤ 2^128:=by rw[qB5 w hg];apply qFb;rcases qAz w hg with h | h<;>omega lemma qBr (h:w &&& В=0):wv w=0:=by rw[qA7 w hg,<-qAq w,h];simp[A_mag] end lemma qHc (a b:ѓ) (ha:good a=true) (hb:good b=true) : (dv a*dv b)ᶠ ∧ |R (dv a*dv b)-wv a*wv b| ≤ 1/2^53*(|wv a*wv b| + A_z) ∧ |wv a*wv b| ≤ 2^256:=by obtain ⟨j,g⟩:=qAm a ha obtain ⟨i,d⟩:=qAm b hb have c:=qAo a ha have e:=qAo b hb obtain ⟨h,f⟩:=qCf _ _ j i (g ▸ c.trans qAn) (d ▸ e.trans qAn) rw[g,d] at f exact ⟨h,f,by rw[в] exact (Х c e (Л _) (by positivity)).trans_eq ב⟩ lemma qBs {n:ℕ} {a b:ℕ->ѓ} {f:ѓ} (g:∀ t,t< n->good (a t)=true) (j:∀ t,t< n->good (b t)=true) {l:List ℕ} (k:l.Nodup) (d:∀ t∈l,t< n) (i:∀ t,t< n->t∉l->a t &&& В=0∨b t &&& В=0) : exactS n a b f=(l.map λt=>wv (a t)*wv (b t)).sum+wv f:=by unfold exactS;congr 1;rw[<-List.sum_toFinset _ k];refine (Finset.sum_subset (λt c=>by simpa using d t (by simpa using c)) λt c h=>?_).symm;have e:=غ.mp c;rcases i t e (by simpa using h) with h | h;. simp[qBr (a t) (g t e) h] . simp[qBr (b t) (j t e) h] lemma qFu {n:ℕ} {l:List ℕ} (b:l.Nodup) (a:∀ t∈l,t< n):ѣl<=n:=by simpa using (b.subperm λt h=>List.mem_range.2 (a t h)).length_le abbrev A_G (ts:List ℕ) (g:ℕ->ѕ) (p:ℕ->ℝ):Prop := ∀ t∈ts,(g t)ᶠ∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z)∧|p t| ≤ 2^256 lemma qFj {b c:ℕ->ѓ} {l:List ℕ} {n:ℕ} (a:∀ t∈l,t< n) (e:∀ t,t< n->good (b t)=true) (f:∀ t,t< n->good (c t)=true) : A_G l (λt=>dv (b t)*dv (c t)) λt=>wv (b t)*wv (c t) :=λt d=>qHc _ _ (e t (a t d)) (f t (a t d)) lemma qCr {a:List ℕ} {g:ℕ->ѕ} {p:ℕ->ℝ} (b:ѕ) (c:A_G a g p) : let A:=(a.map λt=>|p t|).sum let L:=a.map g ++ [b] |A_sr L-((a.map p).sum+R b)| ≤ 1/2^53*(A+ѣa*A_z) ∧ A_sa L<=A + |R b| + 1/2^53*(A+ѣa*A_z)∧A<=2*A_sa L+ѣa*A_z:=by induction a with | nil=>simp[A_sa,A_sr] | cons x a ih => obtain ⟨d,i,j⟩:=ih (λt e=>c t (by simp[e])) have f:=(c x ג).2.1 simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.length_cons,List.cons_append] at d i j ⊢ have m:=qAr obtain ⟨o,r⟩:=abs_le.mp f obtain ⟨n,q⟩:=abs_le.mp d have k:=Л (p x) have h:=abs_sub_abs_le_abs_sub (p x) (R (g x)) have l:=abs_sub_abs_le_abs_sub (R (g x)) (p x) rw[г] at h refine ⟨abs_le.mpr ⟨?_,?_⟩,?_,?_⟩<;>push_cast<;>linarith lemma qHh (l:List ℕ) (g:ℕ->ѕ) (b:ѕ) : ((l.map g ++ [b]).length:ℝ)=ѣl+1:=by simp lemma qHf {a:List ℕ} {g:ℕ->ѕ} {p:ℕ->ℝ} {b:ѕ} (d:A_G a g p) (c:bᶠ):∀ q∈a.map g ++ [b],qᶠ:=by simp only[List.mem_append,List.mem_map,List.mem_singleton];rintro q (⟨t,e,rfl⟩ | rfl);. exact (d t e).1 . exact c lemma qCs {a:ѓ} (d:good a=true) (h:a<=В) : 0<=wv a∧wv a<=2^128 ∧ ∀ w,good w=true->w &&& В<=a->|wv w| ≤ wv a:=by have g:Ѡa<=2147483647:=Ж.mp h have b:wv a=A_mag Ѡa:=by rw[qA7 a d,М א] simp only[show ¬ 2^31<=Ѡa by omega,↓reduceIte,one_mul] refine ⟨b ▸ qAs _,(le_abs_self _).trans (qAo a d),λw c f=>?_⟩ have e:=Ж.mp f rw[qAq] at e rw[qB5 _ c,b] exact qA6 _ _ e ((qAz _ c).imp_right (·.1)) section variable {n:ℕ} {ts:List ℕ} {g:ℕ->ѕ} {p:ℕ->ℝ} {bw hh:ѓ} {X Y J:ℝ} {s t a e:ѕ} (A:n<2^20) (B:ѣts<=n) (C:A_G ts g p) (D:good bw=true) (E:X=(ts.map p).sum+wv bw) (F:s=ts.foldl (λ(c:ѕ) t=>c+g t) 0+dv bw) (G:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) include A B C D E F G lemma qEz (k:tᶠ) (b:Y<=A_B/8) (o:A_sa (ts.map g ++ [dv bw])+A_z<=(1+1/2^22)*Y) (u:(1+1/2^22)*((ѣts:ℝ)+1)*(A_sa (ts.map g ++ [dv bw])+A_z) + ((ts.map λt=>|p t|).sum+ѣts*A_z)<=((ѣts:ℝ)+4)*Y+1/2^240) (m : |R t-Y| ≤ Y/2^30+1/2^250) (h:e=nfOf n*t) : R (s-e)< X∧X< R (s+e):=by subst h obtain rfl:s=A_fsum (ts.map g ++ [dv bw]):=by simp[F,A_fsum,List.foldl_map] obtain ⟨c,d⟩:=qAm bw D obtain ⟨j,-,-⟩:=qCr (dv bw) C rw[d,<-E] at j have i:=qCe (ts.map g ++ [dv bw]) have r:=qAr have l:(ѣts:ℝ)<=n:=by exact_mod_cast B have q:(n:ℝ)+1<=2^20:=by exact_mod_cast A have a:0<=Y :=ד obtain ⟨f,h⟩:=qDd _ (qHf C c) (by simp;omega) ד rw[qHh] at h generalize ts.map g ++ [dv bw]=L at * have x:=qC7 L have y:=Б l a have v:=Х (show (ѣts:ℝ)+1<=2^20 by linarith) o ד ב have w:=abs_sub_le (R (A_fsum L)) (A_sr L) X have z:=abs_sub_abs_le_abs_sub (R (A_fsum L)) (A_sr L) exact qGb A f k a b ד m ד G lemma qDi (P:good hh=true) (hp:hh<=В) (U:(ts.map λt=>|p t|).sum<=wv hh*J) (m:aᶠ) (d:0<=J) (z:J<=2^149) (v : |R a-J| ≤ (J+A_z)/2^32) (he:e=nfOf n*(dv hh*a+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by obtain ⟨b,h,-⟩:=qCs P hp;obtain ⟨Q,N⟩:=qAm hh P;obtain ⟨q,j⟩:=qAm bw D;obtain ⟨-,K,-⟩:=qCr (dv bw) C;rw[j] at K;have S:=qAo bw D;generalize wv hh=H at *;have hk:(ѣts:ℝ)+1<=2^20:=by exact_mod_cast (by omega:ѣts+1<=2^20) have L:(0:ℝ)<=ѣts:=Ю _;have M:=qAr;have x:=qAw;have r:=qC4 ts p;have hBl:=qCc;have T:=qAn;have u:=abs_sub_abs_le_abs_sub (R a) J;rw[И d] at u;obtain ⟨e1,W⟩:=abs_le.mp v;have e3:=Г e1 b;have e4:=Г W b;have e5:=Х h x M.le ב;have e6 : |H*R a| ≤ H*(J+(J+A_z)/2^32):=by rw[в,И b];exact Г ד b have hW:=С b d;have hHJ:H*J<=2^128*2^149:=Х h z d ב;obtain ⟨o,i⟩:=qCf _ a Q m (by rw[N,И b];linarith) ד;rw[N] at i;have e7:=abs_sub_abs_le_abs_sub (R (dv hh*a)) (H*R a);obtain ⟨w,f⟩:=qBl (dv bw) q;rw[j] at f;obtain ⟨y,k⟩:=qBf _ _ o w ד (by rw[f,abs_abs];linarith);rw[f] at k;have e8:=Ъ (R (dv hh*a)) |wv bw|;rw[abs_abs] at e8;obtain ⟨e9,Z⟩:=abs_le.mp i;obtain ⟨e11,V⟩:=abs_le.mp k;have hkz:=Б (show (ѣts:ℝ)<=2^20 by linarith) M.le;have O:=Л (wv bw);obtain ⟨Y,hY⟩:∃ Y,Y=H*J + |wv bw| + A_z:=⟨_,rfl⟩;have c:A_sa (ts.map g ++ [dv bw])+A_z<=(1+1/2^22)*Y :=ד;have I:(ts.map λt=>|p t|).sum+A_z<=Y :=ד;have l : |R (dv hh*a+(dv bw).abs)-Y| ≤ Y/2^30+1/2^250 :=abs_le.mpr ⟨by linarith,by linarith⟩;refine qEz A B C D E F G y ד c ?_ l he;have h3:=Г c (show 0<=(ѣts:ℝ)+1 by linarith);have h4:=Б (show (ѣts:ℝ)<=2^20 by linarith) (ד:0<=Y);linarith lemma qBm {a:ℕ->ѓ} (u:good hh=true) (w:hh<=В) (i:∀ t∈ts,good (a t)=true) (o:∀ t∈ts,|p t| ≤ wv hh * |wv (a t)|) (r:e=nfOf n*(dv hh*ts.foldl (λ(c:ѕ) t=>c+(dv (a t)).abs) 0+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by have f:(ѣts:ℝ)<=2^20:=by exact_mod_cast (by omega:ѣts<=2^20) have k:A_sa (ts.map λt=>dv (a t))=(ts.map λt=>|wv (a t)|).sum:=by simp only[A_sa,List.map_map];congr 1;exact Ա λt d=>by simp[(qAm _ (i t d)).2] have h:=(qC9 ts _ _ λt d=>qAo _ (i t d)).trans (Б f ב);rw[<-k] at h;have m:=qCc obtain ⟨j,b⟩:=qB7 _ (λq v=>by obtain ⟨t,d,rfl⟩:=List.mem_map.mp v exact (qAm _ (i t d)).1) (by simp;omega) ד rw[List.length_map] at b;have g:=qCe (ts.map λt=>dv (a t));have l:=qAr;refine qDi A B C D E F G u w ?_ j g (h.trans ב) ?_ (r.trans (by simp[A_fsum,List.foldl_map]));. rw[k,<-List.sum_map_mul_left];exact List.sum_le_sum o . refine b.trans ?_;have:=Б f (add_nonneg g l.le);linarith end section variable (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) include n hn xw ww lemma qDm (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:ѕ) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by simp only[qEv] at hs obtain ⟨hs,E⟩:=hs have a:=qFj hl2 hx hw have f:=qFu hl hl2 obtain ⟨h,i⟩:=qAm bw hb obtain ⟨-,m,y⟩:=qCr (dv bw) a rw[i] at m have b:=qCe (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw]) have k:=qC9 l _ _ λt u=>(a t u).2.2 have v:(ѣl:ℝ)+1<=2^20:=by exact_mod_cast (by omega:ѣl+1<=2^20) have o:(0:ℝ)<=ѣl:=Ю _ have d:=qAr have D:=qAw have w:=qAo bw hb have z:=qCc have A:=qC4 l λt=>wv (xw t)*wv (ww t) have p:=k.trans (Б (show (ѣl:ℝ)<=2^20 by linarith) ב) have C:=Б (show (ѣl:ℝ)<=2^20 by linarith) d.le have g:A_sa (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw])+A_z<=A_B/8 :=ד obtain ⟨j,c⟩:=qB7 _ (qHf a h) (by simp;omega) ד rw[qHh] at c have B:=С o b have x:=Б v (add_nonneg b d.le) obtain ⟨G,F⟩:=abs_le.mp c exact qEz hn f a hb (qBs hx hw hl hl2 hz) hs hbig j g ד ד (abs_le.mpr ⟨by linarith,by linarith⟩) (E.trans (by simp[A_fsum,List.foldl_map])) lemma qC8 (bw hv:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hgv:good hv=true) (hpv:hv<=В) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hmv:∀ t∈l,ww t &&& В<=hv) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (s e:ѕ) (hs:s=l.foldl (λ(acc:ѕ) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hv*(l.foldl (λ(acc:ѕ) t=>acc+(dv (xw t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by obtain ⟨hs,b⟩:=hs obtain ⟨-,-,c⟩:=qCs hgv hpv exact qBm hn (qFu hl hl2) (qFj hl2 hx hw) hb (qBs hx hw hl hl2 λt i z=>.inl (hz t i z)) hs hbig hgv hpv (λt a=>hx t (hl2 t a)) (λt a=>by rw[в,mul_comm] exact Б (c _ (hw t (hl2 t a)) (hmv t a)) (Л _)) b end end section local notation:max x:max "ᶠ"=>Float.isFinite x=true noncomputable def A_r (b:ј):ℝ:=if b then -1 else 1 lemma qF9 (s t:ј):A_r (s ^^ t)=A_r s*A_r t:=by cases s<;>cases t<;>simp[A_r] lemma qF8 (S T:ј) (X Y:ℝ) (a:0< X) (b:0< Y) (h:A_r S*X=A_r T*Y) : S=T:=by cases S<;>cases T<;>simp[A_r] at h ⊢<;>linarith section variable (d:FloatLib.Numerics.Dyadic) include d lemma qD4 : d.toReal=A_r d.negative*((d.significand:ℝ)*2^d.exponent):=by obtain ⟨n,m,e⟩:=d;cases n<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,A_r] lemma A_s:d.toReal=0 ↔ d.significand=0:=by have h:(2:ℝ)^d.exponent≠0:=zpow_ne_zero _ Ч;rw[qD4];cases d.negative<;>simp[A_r,h] lemma qE9 (h:d.significand≠0) : 0<(d.significand:ℝ)*2^d.exponent :=mul_pos (Nat.cast_pos.mpr (Nat.pos_of_ne_zero h)) (х two_pos _) end lemma qIe (a b:ѕ) (h:A_M a=A_M b):a=b:=by have:=Model.ofFloatModel_injective h;cases a;cases b;simp_all lemma qDk (f:ѕ) (h:fᶠ) : ∃ d,Model.toDyadic? (A_M f)=some d∧d.toReal=R f:=by obtain ⟨d,a⟩:=Model.exists_toDyadic?_of_isFinite ((qAh f).trans h);exact ⟨d,a,by rw[A_R,Model.toReal_eq,a]⟩ lemma qCq {a b:ѕ} {x y:FloatLib.Numerics.Dyadic} (f:Model.toDyadic? (A_M a)=some x) (d:Model.toDyadic? (A_M b)=some y) (e:aᶠ) (c:bᶠ) : A_M (a*b)=if x.significand*y.significand=0 then Model.zero _ (x.negative ^^ y.negative) else Щ _ ⟨x.negative ^^ y.negative,x.significand*y.significand,x.exponent+y.exponent⟩:=by rw[qB0 a b e c,Model.Proof.mul_eq_spec,Model.Spec.mul,f,d] simp lemma qHe {a b:ѕ} {x y z:FloatLib.Numerics.Dyadic} (m:Model.toDyadic? (A_M a)=some x) (n:Model.toDyadic? (A_M b)=some y) (e:Model.toDyadic? (A_M (a*b))=some z) (k:aᶠ) (l:bᶠ) (c:z.toReal=x.toReal*y.toReal):z.negative=(x.negative ^^ y.negative):=by by_cases g:x.significand*y.significand=0;. have d:=qCq m n k l;simp only[g,↓reduceIte] at d;rw[d,Model.toDyadic?_zero] at e;cases e;show Model.signBit _=_;cases x.negative<;>cases y.negative<;>simp[Model.zero] . obtain ⟨f,i⟩:=Nat.mul_ne_zero_iff.mp g;have j:z.significand≠0:=λh=>by rw[(A_s z).2 h] at c;exact mul_ne_zero (mt (A_s x).1 f) (mt (A_s y).1 i) c.symm rw[qD4 z,qD4 x,qD4 y,mul_mul_mul_comm,<-qF9] at c;exact qF8 _ _ _ _ (qE9 z j) (mul_pos (qE9 x f) (qE9 y i)) c lemma qGg (v m p:ѕ) (f:vᶠ) (i:mᶠ) (h:pᶠ) (u:(v*p)ᶠ) (s:(m*p)ᶠ) (q:R (v*p)=R v*R p) (r:R (m*p)=R m*R p):v*p*m=v*(m*p):=by obtain ⟨a,o,n⟩:=qDk v f;obtain ⟨b,y,k⟩:=qDk p h;obtain ⟨c,w,A⟩:=qDk m i;obtain ⟨d,t,B⟩:=qDk _ u;obtain ⟨e,l,x⟩:=qDk _ s;have D:=qHe o y t f h (by rw[B,n,k,q]);have E:=qHe w y l i h (by rw[x,A,k,r]);have C:(d.negative ^^ c.negative)=(a.negative ^^ e.negative):=by rw[D,E];cases a.negative<;>cases b.negative<;>cases c.negative<;>rfl have j:d.toReal*c.toReal=a.toReal*e.toReal:=by rw[B,A,n,x,q,r];ring have g:d.significand*c.significand=0 ↔ a.significand*e.significand=0:=by simp only[Nat.mul_eq_zero,<-A_s,<-mul_eq_zero,j] apply qIe;rw[qCq t w u i,qCq o l f s];by_cases z:d.significand*c.significand=0;. simp only[z,g.1 z,↓reduceIte,C] . simp only[z,mt g.2 z,↓reduceIte];refine ExactRuntime.roundDyadic_congr _ qAj _ _ z (mt g.2 z) (Rat.cast_injective (α:=ℝ) ?_);rw[и,и,Model.Dyadic.toReal_mul,Model.Dyadic.toReal_mul,j] lemma qFw (x w:ѓ) (hx:good x=true) : dvB (dv x) w=dv x*dv w:=by obtain ⟨e,o⟩:=qAm x hx;obtain ⟨p,f,j,q,g,-⟩:=qD1 w;obtain ⟨d,a,-,-,l,b⟩:=qD1 x;obtain ⟨m,c,k⟩:=qCw _ a;have r:=x.toNat_lt;have h:R (dv x)=(((2^23+Ѡx%2^23:ℕ)*m:ℤ):ℝ) * 2^(((Ѡx/2^23%256:ℕ):ℤ)-150):=by unfold dv;rw[d,l,b,k,Int.cast_mul,Int.cast_natCast,mul_assoc] obtain ⟨n,i⟩:=qCm (dv x) _ f e _ _ (by rw[Int.natAbs_mul,Int.natAbs_natCast];have:=Nat.mul_le_mul_left (2^23+Ѡx%2^23) c omega) א (by rw[o];exact (qAo x hx).trans qAn) h show dv x*pw.uget _ _*_=dv x*(_*pw.uget _ _);rw[p];exact qGg _ _ _ e j (qDa _ f).1 n q i g end section section open Float.Model Float.Model.UnpackedFloat lemma qFq (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma B_z (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,Э Ч,й] lemma qDg (m L:ћ) (h1:2^L<=m) (h2:m<2^(L+1)):m.log2=L :=(Nat.log2_eq_iff (by have:=Nat.two_pow_pos L;omega)).2 ⟨h1,h2⟩ lemma qEa (M:ћ) (e T:Int) (K r:ћ) (d:T=e+(K+1:ћ)) (c:0< r) (f:2*(r*2^K)< M+2^K) (g:M<2*(r*2^K)+2^K) : Model.roundMantissaAtExponentEven M e T=r:=by have b:r <<< (K+1)=2*(r*2^K):=by rw[ز,Nat.pow_succ];ring have a:Model.pow2 (K+1-1)=2^K:=Model.pow2_eq_two_pow K;rw[Model.roundMantissaAtExponentEven,с א,show (T-e).toNat=K+1 by omega];rcases le_total (2*(r*2^K)) M with h | h;. have:=Model.roundShiftRightEven_shiftLeft_add_of_lt_half r (M-2*(r*2^K)) (K+1) א (by rw[a];omega);rwa [b,Nat.add_sub_cancel' h] at this . have:=Model.roundShiftRightEven_shiftLeft_sub_of_lt_half r (2*(r*2^K)-M) (K+1) c א (by rw[a];omega);rwa [b,Nat.sub_sub_self h] at this lemma qEf (s:Sign) (P:ћ) (E:Int) (h1:2^23<=P) (h2:P<2^24) (hE:-149<=E) (hp:0< P):Model.finishRoundedMantissa Format.binary32 s (P,E)=.finite s P E hp:=by have:=Model.finishRoundedMantissa_normalized FloatFormat.binary32 s P (E+23) h1 h2 (by show -126<=E+23;omega) rwa [show E+23-(FloatFormat.binary32.fracWidth:ℤ)=E from Int.add_sub_cancel E 23] at this lemma qBb (neg:ј) (m:ћ) (e:Int) : Щ Fmt ⟨neg,m,e⟩ =Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e):=by simp[Model.roundDyadic,Model.ieeeRoundDyadic] rfl lemma qBn (s:Sign) (M:ћ) (E:Int) (h:0< M) (hM1:2^23<=M) (hM2:M<2^24) (hb1:1<=E+150) (hb2:E+150<=254) : (UnpackedFloat.pack Format.binary32 (.finite s M E h)).toNat =(if s=.negative then 2^31 else 0)+(E+150).toNat*2^23+M%2^23:=by simp only[UnpackedFloat.pack,qDg M 23 hM1 hM2] rw[show (E+↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat=(E+150).toNat by simp only[Format.exponentBias];norm_num;ring_nf] have c:¬ (2^Format.binary32.exponentBits<=(E+150).toNat+1):=by show ¬ (2^8<=(E+150).toNat+1);omega have a:(23+1=Format.binary32.mantissaBits):=rfl have b:s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl simp only[c,ite_false,a,ite_true,packComponents,BitVec.toNat_append,b,BitVec.toNat_ofNat] rw[М (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (ر _ (by positivity):M%2^23<2^23)] split_ifs<;>simp[Nat.shiftLeft_eq] omega lemma qE4 (neg:ј) (m:ћ) (e:Int) (P:ћ) (E:Int) (hm:m≠0) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hE2:E+150<=254) (hhi:2*((m:ℝ)*2^e)<(2*P+1)*2^E) (hlo1:P≠2^23->(2*P-1)*2^E<2*((m:ℝ)*2^e)) (hlo2:P=2^23->33554431*2^E<4*((m:ℝ)*2^e)) : toWord (Щ Fmt ⟨neg,m,e⟩) =UInt32.ofNat ((if neg then 2^31 else 0)+(E+150).toNat*2^23+P%2^23):=by have l:0< P :=א obtain ⟨k,F⟩:∃ k:ћ,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,z⟩:∃ g:ћ,(e-k)+(g+2:ћ)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have u:(0:ℝ)<2^(e-k):=х two_pos _ have s:(m:ℝ)*2^e=((m*2^k:ћ):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-B_z e _ k א] have i:(2:ℝ)^E=4*((2^g:ћ):ℝ)*2^(e-k):=by rw[B_z E (e-k) (g+2) א] push_cast ring rw[qBb,<-ExactRuntime.round_scale _ _ m e k hm] have p:m*2^k≠0:=Nat.mul_ne_zero hm (by positivity) rw[s,i] at hhi rw[s,i] at hlo1 hlo2 have H:0<2^g:=by positivity generalize m*2^k=M at * generalize e-k=q at * generalize (2:ℝ)^q=b at * have f:∀ A B:ℕ,(A:ℝ)*b< B*b->A< B :=λA B h=>by exact_mod_cast lt_of_mul_lt_mul_right h u.le push_cast at hhi hlo1 hlo2 have G:M<4*(P*2^g)+2*2^g:=f _ _ (by push_cast;linarith) have t:P≠2^23->4*(P*2^g)< M+2*2^g :=λh=>f _ _ (by push_cast;linarith [hlo1 h]) have y:P=2^23->33554431*2^g< M :=λh=>f _ _ (by push_cast;linarith [hlo2 h]) have C:P*2^g<=(2^24-1)*2^g:=Ь _ א have r:2*(P*2^(g+1))=4*(P*2^g):=by rw[Nat.pow_succ];ring have n:2^(g+1)=2*2^g:=by rw[Nat.pow_succ];ring have w:UnpackedFloat.round Format.binary32 (Model.modelSign neg) M q =.finite (Model.modelSign neg) P E l:=by rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ p] have o:∀ (L:ℕ) (T:ℤ),2^L*2^g<=M->M<2^L*2^g*2 → max (((L+g:ℕ):ℤ)+1+q-24) (-149)=T → Format.binary32.targetExponent (totalExponent M q)=T:=λL T a c d=>by rw[<-Nat.pow_add] at a c simp only[totalExponent,qDg M _ a c,qFq];exact d by_cases v:2^25*2^g<=M . rw[o 25 E v א א,qEa M q E (g+1) P א l ?_ (by rw[r,n];omega)];. exact qEf _ P E hP1 hP2 hE l . rw[r,n];by_cases j:P=2^23;. subst j;omega . have:=t j;omega have j:P=2^23:=by by_contra hne have:=t hne have:(2^23+1)*2^g<=P*2^g:=Ь _ א omega have D:=y j subst j by_cases x:-148<=E . rw[o 24 (E-1) א א א,qEa M q (E-1) g (2^24) א ב א א];have:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (Model.modelSign neg) (E-1+23) (by show -126<=E-1+23;omega);rw[show (FloatFormat.binary32.fracWidth:ℤ)=23 from rfl] at this;rwa [(א:E-1+23-23=E-1),(א:E-1+23+1-23=E)] at this . rw[o 24 E א א א,qEa M q E (g+1) (2^23) א ב (by rw[n];omega) (by rw[n];omega)];exact qEf _ _ E ב ב hE l rw[w] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBn _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qFs (f:ѕ):Model.signBit (toModel (ofFloat f))=decide (2^63<=f.toBits.toNat):=by rw[Model.signBit_eq_msb];show f.toBits.toBitVec.msb=_;rw[BitVec.msb_eq_decide];rfl lemma B_R (f:ѕ) (s:ј) (E F:ћ) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : f.isFinite=true ∧ R f=(if s then -1 else 1)*((2^52+F:ћ):ℝ)*2^((E:Int)-1075):=by have g:Model.expField (toModel (ofFloat f))=f.toBits.toNat/2^52%2^11:=by show ((f.toBits.toBitVec >>> 52) &&& BitVec.ofNat 64 2047).toNat=_ rw[BitVec.toNat_and,BitVec.toNat_ushiftRight,BitVec.toNat_ofNat,щ] exact Н _ 11 have e:Model.fracField (toModel (ofFloat f))=f.toBits.toNat%2^52:=by show (f.toBits.toBitVec &&& BitVec.ofNat 64 (2^52-1)).toNat=_ rw[BitVec.toNat_and,BitVec.toNat_ofNat] exact Н _ 52 have i:Model.signBit (toModel (ofFloat f))=s :=(qFs f).trans (by cases s<;>simp at h ⊢<;>omega) have c:=Model.ofFields_signBit_expField_fracField (toModel (ofFloat f)) rw[g,e,i,show f.toBits.toNat/2^52%2^11=E by split at h<;>omega,show f.toBits.toNat%2^52=F by split at h<;>omega] at c have b:E<2^11 :=א have a:Ы (Model.ofFields B_D s E F)=true:=by rw[Model.isFinite_ofFields B_D _ _ _ b hF] show (E != 2047)=true simp;omega have d:=Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ b hF a rw[c,if_neg (א:¬ E=0)] at d rw[c] at a refine ⟨(qAh f).symm.trans a,?_⟩ rw[R,Model.toReal_eq,d] simp only[show Model.pow2 B_D.fracWidth=2^52 from rfl, show Int.ofNat E-Int.ofNat B_D.exponentBias-Int.ofNat B_D.fracWidth=(E:ℤ)-1075 by show (E:ℤ)-1023-52=_;omega] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def B_near (A:ћ) (S:ј) (ex fr:ћ):Prop := ∃ M,A=(if S then 2^63 else 0)+M∧(ex*2^23+fr)*2^29< M+2^28 ∧ M<=(ex*2^23+fr)*2^29+2^28 lemma B_x (A:і) (x:і) (hx:(A+0x0FFFFFFF) >>> 29=x) (h1:897<=Ѡx/2^23%2^11) (h2:Ѡx/2^23%2^11<1151) : Ѡx<2^35∧B_near ѠA (decide (2^34<=Ѡx)) (Ѡx/2^23%2^11) (Ѡx%2^23):=by have a:=A.toNat_lt have e:Ѡx=(ѠA+268435455)%2^64/2^29:=by rw[<-hx,р,д] norm_num [Nat.shiftRight_eq_div_pow] refine ⟨by omega,?_⟩ unfold B_near by_cases h:2^34<=Ѡx . simp only[h,decide_true,ite_true];exact ⟨ѠA-2^63,by omega,by omega,by omega⟩ . simp only[h,decide_false,Bool.false_eq_true,ite_false];exact ⟨ѠA,by omega,by omega,by omega⟩ lemma qHw (x:і) (hx:Ѡx<2^35) (h1:897<=Ѡx/2^23%2^11) (h2:Ѡx/2^23%2^11<1151) : (((x >>> 34) <<< 31) ||| ((x &&& (0x3FFFFFFFF:і))-((896:і) <<< 23))).toUInt32 = UInt32.ofNat ((if decide (2^34<=Ѡx) then 2^31 else 0) + (Ѡx/2^23%2^11-896)*2^23+Ѡx%2^23):=by have b:((x >>> 34) <<< 31).toNat=(Ѡx/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,р] norm_num [Nat.shiftRight_eq_div_pow,Nat.shiftLeft_eq] omega have a:(x &&& (0x3FFFFFFFF:і)).toNat=Ѡx%2^34:=by rw[ؾ] exact Н _ 34 have d:((x &&& (0x3FFFFFFFF:і))-((896:і) <<< 23)).toNat =Ѡx%2^34-896*2^23:=by rw[show ((896:і) <<< 23)=7516192768 by decide,UInt64.toNat_sub_of_le,a] . rfl . rw[UInt64.le_iff_toNat_le,a];norm_num;omega have c:(Ѡx/2^34)*2^31 ||| (Ѡx%2^34-896*2^23) =(Ѡx/2^34)*2^31+(Ѡx%2^34-896*2^23):=by rw[е (Ѡx/2^34)] exact (Nat.two_pow_add_eq_or_of_lt א _).symm apply а.mp rw[UInt64.toNat_toUInt32,UInt64.toNat_or,b,d,c,Բ] simp only[decide_eq_true_eq] split_ifs<;>omega lemma qEs (A B:і) (h:zivW A B≠Е) : ∃ (S:ј) (ex fr:ћ),897<=ex∧ex<1151∧fr<2^23 ∧ zivW A B=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near ѠA S ex fr∧B_near ѠB S ex fr:=by unfold zivW at h ⊢ dsimp only at h ⊢ generalize hx:(A+(0x0FFFFFFF:і)) >>> (29:і)=x at h ⊢ generalize hy:(B+(0x0FFFFFFF:і)) >>> (29:і)=y at h ⊢ have hex:((x >>> (23:і)) &&& (0x7FF:і)).toNat=Ѡx/2^23%2^11:=by rw[ؾ,р] have:=Н (Ѡx >>> 23) 11 norm_num [Nat.shiftRight_eq_div_pow] at this ⊢ exact this generalize (x >>> (23:і)) &&& (0x7FF:і)=ex at h hex ⊢ split at h . rename_i hc;have hc0:=hc;obtain ⟨hxy,hc⟩:=ل.mp hc;have hxy:x=y:=by simpa using hxy subst hxy;have hc:ex-(897:і)<(254:і):=by simpa using hc rw[ф,UInt64.toNat_sub] at hc;have hv:=ex.toNat_lt;norm_num at hc hv;have h1:897<=Ѡx/2^23%2^11 :=א;have h2:Ѡx/2^23%2^11<1151 :=א;simp only[hc0,ite_true];obtain ⟨hx35,ha⟩:=B_x A x hx h1 h2;exact ⟨_,_,_,h1,h2,by omega,qHw x hx35 h1 h2,ha,(B_x B x hy h1 h2).2⟩ . exact absurd rfl h lemma qR0 (Y:ј) (r:ѓ) (he:expOf r≠255) : r &&& ((if Y then (0:ѓ) else Е) ||| ((r >>> (31:ѓ))-1))=act Y r:=by have b:=r.toNat_lt have f:∀ u:ѓ,u&&&Е=u:=λu=>UInt32.toNat_inj.mp (by rw[UInt32.toNat_and];show Ѡu&&&(2^32-1)=_;rw[Nat.and_two_pow_sub_one_eq_mod];exact Nat.mod_eq_of_lt u.toNat_lt) by_cases h:2^31<=Ѡr . have e:r>>>(31:ѓ)=1:=UInt32.toNat_inj.mp (by rw[UInt32.toNat_shiftRight];show Ѡr>>>31=1;rw[Nat.shiftRight_eq_div_pow];omega) have g:(r&&&0x80000000!=0)=true:=by have:(r&&&0x80000000).toNat≠0:=by rw[UInt32.toNat_and];show Ѡr&&&2^31≠0;rw[Nat.and_two_pow];simp [Nat.testBit_eq_decide_div_mod_eq];omega simp only[bne_iff_ne];intro c;rw[c] at this;exact this rfl cases Y<;>simp[act,reluW,e,g,he,f] . have e:r>>>(31:ѓ)=0:=UInt32.toNat_inj.mp (by rw[UInt32.toNat_shiftRight];show Ѡr>>>31=0;rw[Nat.shiftRight_eq_div_pow];omega) have g:(r&&&0x80000000)=0:=UInt32.toNat_inj.mp (by rw[UInt32.toNat_and];show Ѡr&&&2^31=0;rw[Nat.and_two_pow];simp [Nat.testBit_eq_decide_div_mod_eq];omega) cases Y<;>simp[act,reluW,e,g] lemma qRe (w:ѓ):(expOf w).toNat=Ѡw/2^23%2^8:=by unfold expOf rw[UInt32.toNat_and,UInt32.toNat_shiftRight] show (Ѡw>>>23)&&&(2^8-1)=_ rw[Nat.and_two_pow_sub_one_eq_mod,Nat.shiftRight_eq_div_pow] lemma qR1 (r:ѓ) (S:ј) (ex fr:ћ) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (hz:r=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr)):expOf r≠255:=by intro e;have a:=qRe r;rw[e] at a;change 255=_ at a;rw[hz,UInt32.toNat_ofNat'] at a;cases S<;>simp only[Bool.false_eq_true,ite_false,ite_true] at a<;>omega lemma qB6 (s e:ѕ) (h:ziv s e≠Е) : ∃ (S:ј) (ex fr:ћ),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near (s+e).toBits.toNat S ex fr∧B_near (s-e).toBits.toNat S ex fr := qEs (s+e).toBits (s-e).toBits h set_option maxRecDepth 8192 in lemma qA4 (f:ѕ) (S:ј) (ex fr:ћ) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hn:B_near f.toBits.toNat S ex fr) : f.isFinite=true∧∃ v:ℝ,R f=(if S then -v else v) ∧ v<=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< v) ∧ (2^53-2^28)*2^((ex:Int)-1076)< v:=by obtain ⟨M,k,o,n⟩:=hn have d:M%2^52<2^52:=ر _ ב have b:M=(M/2^52)*2^52+M%2^52 :=א generalize M/2^52=E at b generalize M%2^52=F at b d subst b have c:(0:ℝ)<2^((ex:Int)-1076):=х ב _ have m:E=ex∨(E+1=ex∧fr=0) :=א obtain ⟨a,l⟩:=B_R f S E F א א d (by rw[k,Գ]) obtain ⟨w,j,i,e,g⟩:∃ w:ℕ,((2^52+F:ћ):ℝ)*2^((E:Int)-1075) =w*2^((ex:Int)-1076)∧w<=2^53+fr*2^30+2^29 ∧ (fr≠0->2^53+fr*2^30< w+2^29)∧2^53< w+2^28:=by rcases m with rfl | ⟨rfl,rfl⟩ . refine ⟨2*(2^52+F),?_,by omega,by omega,by omega⟩;rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,Э Ч,zpow_one];push_cast;ring . exact ⟨2^52+F,by congr 2;push_cast;ring,by omega,by omega,by omega⟩ refine ⟨a,(w:ℝ)*2^((ex:Int)-1076),?_,Б ?_ c.le,λh=>о ?_ c,о ?_ c⟩ . rw[l,mul_assoc,j];cases S<;>simp . exact_mod_cast i . rw[sub_lt_iff_lt_add];exact_mod_cast e h . rw[sub_lt_iff_lt_add];exact_mod_cast g lemma qBw (s e:ѕ) (h:ziv s e≠Е) : (s+e).isFinite=true∧(s-e).isFinite=true∧(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|:=by obtain ⟨S,f,c,b,d,a,-,j,k⟩:=qB6 s e h;obtain ⟨g,v,i,-,-,l⟩:=qA4 _ S f c b d a j;refine ⟨g,(qA4 _ S f c b d a k).1,?_⟩ exact (show (2:ℝ)^(-127:ℤ)<=(2^53-2^28)*2^(-179:ℤ) by norm_num).trans ((Г (Ц one_le_two א) ב).trans (l.le.trans (by rw[i];cases S<;>simp[le_abs_self]))) lemma qAx (w:ѓ) (h:expOf w≠255):wv w=(Д w:ℝ):=by rw[wv,Model.toReal_eq,ofW,ц w h];simp only[rationalWord,FloatLib.Numerics.Dyadic.cast_toRat] lemma qEl (a b:ѐ) (start n:ћ) : (dsum a b start n:ℚ)*2^(-298:Int) =∑ j∈л n,Д a[start+j]! * Д b[start+j]!:=by have c:∀ a b:ѓ,((sv a:ℚ)*sv b*(2:ℚ)^(idxOf a+idxOf b))*2^(-298:Int) =Д a*Д b:=λa b=>by simp only[rationalWord,decodeD_toRat] rw[show (-298:Int)=-149+-149 by norm_num,Э Ч] rw[zpow_sub₀ Ч,zpow_sub₀ Ч,й,й,pow_add] norm_num [zpow_neg,zpow_natCast] ring induction n generalizing start with | zero=>simp[dsum] | succ n ih => rw[dsum,Int.cast_add,Int.cast_mul,Int.cast_mul,Int.cast_pow,Int.cast_ofNat,add_mul,ih,Finset.sum_range_succ',ю,c,add_comm] congr 1 apply ذ rfl intro j _ rw[Գ,Nat.add_comm 1 j] lemma qEc (n:ћ) (fa fb:ћ->ѓ) (hf:∀ j,j< n->expOf (fa j)≠255∧expOf (fb j)≠255) (hn:(∑ j∈л n,Д (fa j)*Д (fb j))≠0) : ∃ d:FloatLib.Numerics.Dyadic, d.toRat=(∑ j∈л n,Д (fa j)*Д (fb j)) ∧ dotOf n fa fb=toWord (Щ Fmt d):=by let a:=Array.ofFn (n:=n) λi=>fa i let b:=Array.ofFn (n:=n) λi=>fb i have e:ѡa=n:=by simp[a] have g:ѡb=n:=by simp[b] have c:(dsum a b 0 n:ℚ)*2^(-298:Int) =∑ j∈л n,Д (fa j)*Д (fb j):=by rw[qEl] refine ذ rfl λj m=>?_ have h:=غ.mp m rw[Т,qAb n fa j h,qAb n fb j h] have l:dsum a b 0 n≠0:=λh=>hn (by rw[<-c,h];simp) have Z:=qEg a b (by rw[e,g]) λk f=>by rw[e] at f rw[qAb n fa k f,qAb n fb k f];exact hf k f rw[e] at Z refine ⟨ofIntDyadic (dsum a b 0 n) (-298),by rw[toRat_ofIntDyadic,c],?_⟩ show dotSpec a b=_ rw[<-Z];simp[finishWord,l] lemma qIg:Д oneW=1:=by norm_num [rationalWord,decodeD,show expOf oneW=127 by decide,show fracOf oneW=0 by decide,show signOf oneW=false by decide,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand] lemma qHv (n:ћ) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) : ((∑ j∈л (n+1),Д (if j< n then xw j else oneW) * Д (if j< n then ww j else bw):ℚ):ℝ)=exactS n xw ww bw:=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qIg,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qAx bw (qAe bw hb)] congr 1 refine ذ rfl λt b=>?_ have a:=غ.mp b simp only[a,ite_true] rw[qAx _ (qAe _ (hx t a)),qAx _ (qAe _ (hw t a)),Rat.cast_mul] section variable (s e:ѕ) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е) include s e V hlo hhi h lemma qB8 : ∃ (S:ј) (ex fr:ћ) (W:ℝ),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ V=(if S then -W else W) ∧ W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=qB6 s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qA4 _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qA4 _ S ex fr h1 h2 h3 hb' rw[hva] at hhi rw[hvb] at hlo refine ⟨S,ex,fr,if S then -V else V,h1,h2,h3,hz,?_,?_,?_,?_⟩ all_goals cases S all_goals simp only[Bool.false_eq_true,ite_false,ite_true,neg_neg] at hhi hlo ⊢ . exact н hhi hia . linarith . exact λh=>(lob1 h).trans hlo . exact λh=>by linarith [loa1 h] . exact lob.trans hlo . linarith lemma qG5:V≠0:=by obtain ⟨S,ex,fr,W,-,-,-,-,hV,-,-,hW3⟩:=qB8 s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=х ב _;have hA:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076):=С ב hb0.le;intro h0;rw[h0] at hV;cases S;. simp only[Bool.false_eq_true,ite_false] at hV;linarith . simp only[ite_true] at hV;linarith end lemma qEo (d:FloatLib.Numerics.Dyadic) (S:ј) (ex fr:ћ) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (W:ℝ) (hd:(d.toRat:ℝ)=if S then -W else W) (hW1:W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) (hW3:(2^53-2^28)*2^((ex:Int)-1076)< W) : toWord (Щ Fmt d) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hb0:(0:ℝ)<2^((ex:Int)-1076):=х ב _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-й,<-Э Ч] congr 1;push_cast;ring have hW0:0< W:=ч (С ב hb0.le) hW3 obtain ⟨neg,m,q⟩:=d have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=if S then -W else W:=by rw[<-и,hd] have hq:(0:ℝ)<2^q:=х ב _ have hm0:(0:ℝ)<=m:=Ю _ have hmq:=С hm0 hq.le have hMW:(m:ℝ)*2^q=W∧neg=S:=by simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] at hdV cases neg<;>cases S <;> simp only[Bool.false_eq_true,ite_false,ite_true,Int.ofNat_eq_natCast,Int.cast_neg,Int.cast_natCast] at hdV . exact ⟨by linarith,rfl⟩ . exfalso;linarith . exfalso;linarith . exact ⟨by linarith,rfl⟩ obtain ⟨hMW,rfl⟩:=hMW have hm:m≠0:=by rintro rfl simp at hMW linarith have hword:=qE4 neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hword,e1,e2] lemma qIq (s e:ѕ) (d:FloatLib.Numerics.Dyadic) (hlo:R (s-e)<(d.toRat:ℝ)) (hhi:(d.toRat:ℝ)< R (s+e)) (h:ziv s e≠Е) : ziv s e=toWord (Щ Fmt d):=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=qB8 s e _ hlo hhi h;rw[hz];exact (qEo d S ex fr h1 h2 h3 W hV hW1 hW2 hW3).symm lemma qCn (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:ѕ) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠Е) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by have hsum:=qHv n xw ww bw hx hw hb obtain ⟨d,hd,hw'⟩:=qEc (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (λj _=>by by_cases hj:j< n . simp only[hj,ite_true];exact ⟨qAe _ (hx j hj),qAe _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,qAe _ hb⟩) λh0=>qG5 s e _ hlo hhi h (by rw[<-hsum,h0,Rat.cast_zero]) have q:(d.toRat:ℝ)=exactS n xw ww bw:=by rw[hd,hsum] rw[hw'] exact qIq s e d (by rw[q];exact hlo) (by rw[q];exact hhi) h end section lemma qDn (n:ћ) (hn:n<2^20) (xw ww:ћ->ѓ) (bw:ѓ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:љ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:ѕ) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (h:ziv s e≠Е) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBw s e h obtain ⟨hlo,hhi⟩:=qDm n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCn n hn xw ww bw hx hw hb s e hlo hhi h end section end section lemma C_z:(0:є).toNat=0:=rfl lemma qAd (k:ћ) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma qBa (a b:є) (h:Ѡa*Ѡb<2^32) : (a*b).toNat=Ѡa*Ѡb:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_mul];exact М א lemma qBe {α:Type} (a:Array α) (h:ѡa<2^32):a.usize.toNat=ѡa :=qAd _ h lemma qAg (a:ѐ) (i:є) (hs:ѡa<2^32):rdW a i=a[Ѡi]!:=by unfold rdW;split;. rename_i h;have h2:=qBd a i h;rw[Я a Ѡi h2];rfl . rename_i h;have h':¬ Ѡi< ѡa:=by intro h2;apply h;rw[А,qBe a hs];exact h2 rw[ـ a Ѡi h'];rfl lemma qBh (a:ѐ) (i:є) (v:ѓ):(wrW a i v).size=ѡa:=by unfold wrW;split<;>simp lemma qFo {α:Type} [Inhabited α] (a:Array α) (i q:ћ) (v:α) (hi:i< ѡa) : (a.set i v hi)[q]! = if q=i then v else a[q]!:=by by_cases hq:q< ѡa . rw[Я _ q (by simpa using hq),getElem!_pos a q hq,Array.getElem_set];by_cases e:q=i;. simp[e] . rw[Ф e,Ф (λh=>e h.symm)] . rw[ـ _ q (by simpa using hq),getElem!_neg a q hq,ite_eq_right א] lemma qBu (a:ѐ) (i:є) (v:ѓ) (q:ћ) (hs:ѡa<2^32) (hi:Ѡi< ѡa):(wrW a i v)[q]! = if q=Ѡi then v else a[q]!:=by have h:i< a.usize:=by rw[А,qBe a hs];exact hi unfold wrW;rw[П h];exact qFo a _ q v hi def C_Inv (N:ћ) (sp:ћ->њ) (S:ћ->Prop) (out:ё):Prop := ѡout=N∧∀ q,q< N->S q->out[q]! = sp q section variable {N:ћ} {sp:ћ->њ} include N sp lemma qAk {S:ћ->Prop} {out:ё} (hN:N<2^32) (h:C_Inv N sp S out) {u:є} {p:ћ} {v:њ} (hu:Ѡu=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q=p∨S q) (wrW out u v):=by obtain ⟨h1,h2⟩:=h;refine ⟨by rw[qBh,h1],λq hq hS=>?_⟩;rw[qBu _ _ _ _ א א,hu];by_cases e:q=p;. rw[с e,e,hv] . rw[Ф e];exact h2 q hq (hS.resolve_left e) lemma qA5 {S S':ћ->Prop} {out:ё} (h:C_Inv N sp S out) (hS:∀ q,q< N->S' q->S q):C_Inv N sp S' out :=⟨h.1,λq hq h'=>h.2 q hq (hS q hq h')⟩ lemma qBv {S:ћ->Prop} {out:ё} (h:C_Inv N sp S out) (hS:∀ q,q< N->S q) : out=Array.ofFn (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h;apply Array.ext;. simp[h1] . intro q h3 h4;have h5:q< N :=א;have:=h2 q h5 (hS q h5);rw[Я out q h3] at this;simp[this] end lemma qEn (v:ѓ) (h1:(0x7FFFFF:ѓ)<=v-1) (h2:v<У) : (v == 0 || v-(0x00800000:ѓ)<=(0x7EFFFFFF:ѓ))=true:=by have a1:(0x7FFFFF:ѓ).toNat=0x7FFFFF:=rfl;have a2:(У:ѓ).toNat=У:=rfl;have a3:(0x00800000:ѓ).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:ѓ).toNat=0x7EFFFFFF:=rfl;have a5:(1:ѓ).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:ѓ)<=v:=by rw[Ж];have:Ѡv≠0:=λh=>h0 (а.mp h);omega rw[Ж,ق _ _ h3] at h1;rw[у] at h2;have h4:(0x00800000:ѓ)<=v:=by rw[Ж];omega simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[Ж,ق _ _ h4];omega lemma qHu (c u lo:ѓ) (h:c<=if u< lo then u else lo):c<=u∧c<=lo:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qH2 (c v hi:ѓ) (h:(if hi< v then v else hi)< c):v< c∧hi< c:=by simp only[UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qB2 (w:ѓ) (h:good w=true):good (w &&& В)=true:=by have e:(w &&& В) &&& В=w &&& В:=by rw[UInt32.and_assoc,UInt32.and_self] unfold good at h ⊢;simpa only [e] using h lemma qCx {h:ѓ} (h1:h<У):h<=В:=by have a1:(У:ѓ).toNat=У:=rfl;have a2:(В:ѓ).toNat=В:=rfl;rw[у] at h1;rw[Ж];omega def C_Hv (A:ё) (h:њ):Prop := good h=true∧h<=В∧(∀ k,k< ѡA->good A[k]! = true) ∧ ∀ k,k< ѡA->A[k]! &&& В<=h lemma qFa (r v hi:ѓ) (h:(if hi< v then v else hi)<=r):v<=r∧hi<=r:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qHj (A:ѐ) (n:є) (hn:Ѡn<=ѡA) (d:ћ) : ∀ (i:є) (lo hi:ѓ),Ѡi+d=Ѡn->scan A n i hn lo hi≠Е → (0x7FFFFF:ѓ)<=lo∧hi<У∧hi<=scan A n i hn lo hi ∧ scan A n i hn lo hi<У∧(good hi=true->good (scan A n i hn lo hi)=true) ∧ ∀ k,Ѡi<=k->k< Ѡn → good A[k]! = true∧A[k]! &&& В<=scan A n i hn lo hi:=by induction d with | zero => intro i lo hi he h have e:scan A n i hn lo hi =if 0x007FFFFF<=lo∧hi<У then hi else Е:=by rw[scan,Ш (by rw[А];omega)] rw[e] at h ⊢ by_cases c:0x007FFFFF<=lo∧hi<У . rw[с c];exact ⟨c.1,c.2,و _,c.2,id,λk h1 h2=>by omega⟩ . rw[Ф c] at h;exact absurd rfl h | succ d ih => intro i lo hi he h have hi':i< n:=by rw[А];omega have hb:Ѡi< ѡA :=א have e:scan A n i hn lo hi=scan A n (i+1) hn (if (A.uget i hb &&& В)-1< lo then (A.uget i hb &&& В)-1 else lo) (if hi< A.uget i hb &&& В then A.uget i hb &&& В else hi):=by rw[scan,П hi'] rw[e] at h ⊢ obtain ⟨h1,h2,h3,h4,h5,h6⟩:=ih (i+1) _ _ (by rw[usz i n hi'];omega) h obtain ⟨h7,h8⟩:=qHu _ _ _ h1 obtain ⟨h9,h10⟩:=qH2 _ _ _ h2 obtain ⟨h11,h12⟩:=qFa _ _ _ h3 have hg:good (A.uget i hb)=true:=qEn _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qB2 _ hg . exact g0 . by_cases e2:k=Ѡi;. subst e2;rw[Я A Ѡi hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qBi (A:ѐ) (hs:ѡA<2^32) (h:scan A A.usize 0 (qAc A) Е 0≠Е) : C_Hv A (scan A A.usize 0 (qAc A) Е 0):=by have e:=qBe A hs;have h0:(0:є).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qHj A A.usize (qAc A) ѡA 0 _ _ (by rw[h0,e];omega) h;have h7:∀ k,k< ѡA->good A[k]! = true ∧ A[k]! &&& В<=scan A A.usize 0 (qAc A) Е 0 :=λk hk=>h6 k א א;exact ⟨h5 rfl,qCx h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qF6 (a b:ѓ):a<=a ||| b∧b<=a ||| b:=by rw[Ж,Ж,UInt32.toNat_or];exact ⟨Nat.left_le_or,Nat.right_le_or⟩ lemma qBt (v:ѓ) (h1:v=0∨0x800000<=v) (h2:v<У) : (v == 0 || v-(0x00800000:ѓ)<=(0x7EFFFFFF:ѓ))=true:=by rcases h1 with rfl | h1;. rfl . have a1:(У:ѓ).toNat=У:=rfl;have a2:(0x00800000:ѓ).toNat=0x00800000:=rfl;have a3:(0x7EFFFFFF:ѓ).toNat=0x7EFFFFFF:=rfl;simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[Ж,ق _ _ h1];rw[у] at h2;rw[Ж] at h1;omega lemma qFe (a b:ѓ):a &&& b<=a∧a &&& b<=b:=by rw[Ж,Ж,п];exact ⟨Nat.and_le_left,Nat.and_le_right⟩ lemma qGi (v:ѓ) (h:0x7FFFFF<=v-1):v=0∨0x800000<=v:=by by_cases z:v=0;. exact .inl z . right;have a1:(0x7FFFFF:ѓ).toNat=0x7FFFFF:=rfl;have a2:(0x800000:ѓ).toNat=0x800000:=rfl;have a3:(1:ѓ).toNat=1:=rfl;have a4:Ѡv≠0:=λe=>z (а.mp (by rw[e];rfl));have a5:=v.toNat_lt;have h1:=Ж.mp h;rw[a1,UInt32.toNat_sub,a3] at h1;rw[Ж,a2];omega lemma qF3 (A:ѐ) (n:є) (hn:Ѡn<=ѡA) (d:ћ) : ∀ (i:є) (o s:ѓ),Ѡi+d=Ѡn->scanF A n i hn o s≠Е → o<=scanF A n i hn o s∧scanF A n i hn o s<У∧0x7FFFFF<=s ∧ ((o=0∨0x800000<=o)->scanF A n i hn o s=0∨0x800000<=scanF A n i hn o s) ∧ ∀ k,Ѡi<=k->k< Ѡn → good A[k]! = true∧A[k]! &&& В<=scanF A n i hn o s:=by induction d with | zero => intro i o s he h rw[scanF,Ш (by rw[А];omega)] at h ⊢ by_cases c:0x7FFFFF<=s∧o<У . rw[с c];exact ⟨و _,c.2,c.1,id,λk h1 h2=>by omega⟩ . rw[Ф c] at h;exact absurd rfl h | succ d ih => intro i o s he h have hi':i< n:=by rw[А];omega have hb:Ѡi< ѡA :=א rw[scanF,П hi'] at h ⊢ obtain ⟨h1,h2,h3,h4,h5⟩:=ih (i+1) _ _ (by rw[usz i n hi'];omega) h obtain ⟨h6,h7⟩:=qF6 o _ obtain ⟨h8,h9⟩:=qFe s ((A.uget i hb &&& В)-1) have hv:=qGi _ (UInt32.le_trans h3 h9) have hl:=UInt32.le_trans h7 h1 refine ⟨UInt32.le_trans h6 h1,h2,UInt32.le_trans h3 h8,λg0=>h4 ?_,λk hk1 hk2=>?_⟩ . rcases hv with hv | hv . rcases g0 with g0 | g0;. left;rw[g0,hv];rfl . right;exact UInt32.le_trans g0 h6 . right;exact UInt32.le_trans hv h7 . by_cases e2:k=Ѡi;. subst e2;rw[Я A Ѡi hb];exact ⟨qBt _ hv (UInt32.lt_of_le_of_lt hl h2),hl⟩ . exact h5 k (by rw[usz i n hi'];omega) hk2 lemma qB1 (A:ѐ) (hs:ѡA<2^32) (h:scanF A A.usize 0 (qAc A) 0 Е≠Е) : C_Hv A (scanF A A.usize 0 (qAc A) 0 Е):=by have e:=qBe A hs;have h0:(0:є).toNat=0:=rfl;obtain ⟨-,h2,-,h4,h5⟩ :=qF3 A A.usize (qAc A) ѡA 0 _ _ (by rw[h0,e];omega) h;generalize scanF A A.usize 0 (qAc A) 0 Е=S at *;have hp:=qCx h2;have hm:S &&& В=S:=by have a:(В:ѓ).toNat=2^31-1:=rfl;apply а.mp;rw[Ж,a] at hp;rw[п,a,Н];omega have hg:good S=true:=by unfold good;dsimp only;rw[hm];exact qBt _ (h4 (Or.inl rfl)) h2 exact ⟨hg,hp,λk hk=>(h5 k א א).1,λk hk=>(h5 k א א).2⟩ lemma qA8 (r:ћ) (A:ѐ) (hs:ѡA<2^32) (h:scanH r A≠Е) : C_Hv A (scanH r A):=by revert h;unfold scanH;dsimp only;by_cases c:(r == 1)=true . rw[if_pos c];split;. exact qBi A hs . exact qB1 A hs . rw[if_neg c];exact qBi A hs lemma qGd (r:ћ) (A:ѐ) (h8:r≠8) (h1:r≠1) (hs:ѡA<2^32) (h:scanW r A≠Е):C_Hv A (scanW r A):=by have e:scanW r A=scanH r A:=by unfold scanW;rw[if_neg (by simp[h8,h1])] rw[e] at h ⊢;exact qA8 r A hs h def C_out (I:ћ) (Y:ј) (X W b:ё) (i j:ћ):њ := act Y (dotOf (I+1) (λt=>if t< I then X[i*I+t]! else oneW) (λt=>if t< I then W[j*I+t]! else b[j]!)) structure C_G (R I O:ћ) (X W b:ё):Prop where hX:R*I=ѡX hW:O*I=ѡW hb:O=ѡb pi:0< I po:0< O ni:I<2^20 sX:ѡX<2^32 sW:ѡW<2^32 sb:ѡb<2^32 sO:R*O<2^32 gX:∀ q,q< ѡX->good X[q]! = true gW:∀ q,q< ѡW->good W[q]! = true gb:∀ q,q< ѡb->good b[q]! = true lemma qCj {R I i t:ћ} (hi:i< R) (ht:t< I):i*I+t< R*I:=by have h:(i+1)*I<=R*I:=Ь _ hi;rw[Nat.add_mul,ه] at h;omega lemma qBc {R I i t:ћ} (hi:i< R) (ht:t<=I) : i*I+t<=R*I:=by have h:(i+1)*I<=R*I:=Ь _ hi;rw[Nat.add_mul,ه] at h;omega section variable {R I O:ћ} {X W b:ё} section variable (G:C_G R I O X W b) include G lemma C_G.gx {i:ћ} (hi:i< R) : ∀ t,t< I->good X[i*I+t]! = true :=λ_ ht=>G.gX _ (by rw[<-G.hX];exact qCj hi ht) lemma C_G.gw {j:ћ} (hj:j< O) : ∀ t,t< I->good W[j*I+t]! = true :=λ_ ht=>G.gW _ (by rw[<-G.hW];exact qCj hj ht) lemma qHx (Y:ј) {i j:ћ} (hi:i< R) (hj:j< O) {s e:ѕ} (hs:let r:=(З I).foldl (λ(acc:F1) t => let q:=dv X[i*I+t]! * dv W[j*I+t]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s∧e=r.t) : fin I Y X W b i j s (nfOf I) e b[j]! = C_out I Y X W b i j:=by obtain ⟨h1,h2⟩:=hs unfold fin dsimp only split . unfold slowOut C_out;rw[qAi] . rename_i h;unfold C_out;congr 1;exact qDn I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb];exact hj)) _ ػ (λt ht=>Р.mp ht) (λt ht h3=>absurd (Р.mpr ht) h3) _ _ ⟨by rw[h1],by rw[h2]⟩ (by simpa using h) end lemma qE2 {X W:ѐ} {xo wo n:є} (hX:ѡX<2^32) (hW:ѡW<2^32) (h1:Ѡxo+Ѡn<=ѡX) (h2:Ѡwo+Ѡn<=ѡW) (d:ћ):∀ (t:є) (s a:ѕ),Ѡt+d=Ѡn → tightGo X W xo wo n t s a=(List.range' Ѡt d).foldl (λ(acc:F1) k =>let q:=dv X[Ѡxo+k]! * dv W[Ѡwo+k]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨s,a⟩:=by induction d with | zero => intro t s a he rw[tightGo,Ш (by rw[А];omega)] rfl | succ d ih => intro t s a he have ht:t< n:=by rw[А];omega rw[tightGo,П ht,List.range'_succ,ك] dsimp only rw[qAg _ _ hX,qAg _ _ hW,qAf _ _ א,qAf _ _ א] rw[ih (t+1) _ _ (by rw[usz t n ht];omega),usz t n ht] section variable (G:C_G R I O X W b) (Y:ј) {i j:ћ} (hi:i< R) (hj:j< O) include G Y i j hi hj lemma qDw {s a:ѕ} {hh:њ} (hc:ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs))≠Е → ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs)) = dotOf (I+1) (λt=>if t< I then X[i*I+t]! else oneW) (λt=>if t< I then W[j*I+t]! else b[j]!)) : fin8 I Y X W b i j s (nfOf I) a hh b[j]! ѢI=C_out I Y X W b i j:=by have hin:I<2^32:=lt_trans G.ni ב have hX1:i*I+I<=ѡX:=by rw[<-G.hX];exact qBc hi (ي _) have hW1:j*I+I<=ѡW:=by rw[<-G.hW];exact qBc hj (ي _) have sX:=G.sX have sW:=G.sW have e1:(i*I).toUSize.toNat=i*I:=qAd _ א have e2:(j*I).toUSize.toNat=j*I:=qAd _ א have e3:I.toUSize.toNat=I:=qAd _ hin unfold fin8 dsimp only split . have e:=qE2 (xo:=(i*I).toUSize) (wo:=(j*I).toUSize) (n:=ѢI) G.sX G.sW (by rw[e1,e3];exact hX1) (by rw[e2,e3];exact hW1) I 0 0 0 (by rw[e3,C_z,Т]);rw[C_z,e1,e2,<-ن] at e;rw[qHx G Y hi hj (s:=(tightGo X W (i*I).toUSize (j*I).toUSize ѢI 0 0 0).s) (e:=(tightGo X W (i*I).toUSize (j*I).toUSize ѢI 0 0 0).t) ⟨by rw[e],by rw[e]⟩] . rename_i h have hn:=(by simpa using h:ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs))≠Е) obtain ⟨S,ex,fr,a1,a2,a3,hz,-⟩:=qB6 _ _ hn rw[qR0 _ _ (qR1 _ S ex fr a1 a2 a3 hz)];unfold C_out;congr 1;exact hc hn lemma qDL {s:ѕ} {hv:њ} (g1:good hv=true) (g2:hv<=В) (L:љ) (hl:L.Nodup) (hl2:∀ t∈L,t< I) (g4:∀ t∈L,W[j*I+t]! &&& В<=hv) (hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0) (hs:s=L.foldl (λ(acc:ѕ) t=>acc+dv X[i*I+t]! * dv W[j*I+t]!) 0) : fin8 I Y X W b i j s (nfOf I) (L.foldl (λ(a:ѕ) t=>a+(dv X[i*I+t]!).abs) 0) hv b[j]! ѢI=C_out I Y X W b i j:=by have gb:=G.gb j (by rw[<-G.hb];exact hj) exact qDw G Y hi hj λh=>by obtain ⟨-,-,k⟩:=qBw _ _ h obtain ⟨c1,c2⟩:=qC8 I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! hv (G.gx hi) (G.gw hj) gb g1 g2 L hl hl2 g4 hz _ _ ⟨by rw[hs],rfl⟩ k exact qCn I G.ni _ _ _ (G.gx hi) (G.gw hj) gb _ _ c1 c2 h lemma qDu {s:ѕ} {hv:њ} (g1:good hv=true) (g2:hv<=В) (g4:∀ t,t< I->W[j*I+t]! &&& В<=hv) (L:љ) (hl:L.Nodup) (hl2:∀ t∈L,t< I) (hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0) (hs:s=L.foldl (λ(acc:ѕ) t=>acc+dv X[i*I+t]! * dv W[j*I+t]!) 0) : fin8 I Y X W b i j s (nfOf I) (L.foldl (λ(a:ѕ) t=>a+(dv X[i*I+t]!).abs) 0) hv b[j]! ѢI=C_out I Y X W b i j:= qDL G Y hi hj g1 g2 L hl hl2 (λt ht=>g4 t (hl2 t ht)) hz hs end end lemma qC5 (x:ђ) (i:є) (h:Ѡi< ѡx) : x.uget i h=x.data[Ѡi]!:=by obtain ⟨ds⟩:=x;have h':Ѡi< ѡds:=h;rw[Я ds Ѡi h'];rfl lemma qC3 (a:ѐ) (i:є) (h:Ѡi< ѡa) : a.uget i h=a[Ѡi]!:=by rw[Я a Ѡi h];rfl lemma qD6 (a:ђ) (i:є) (v:ѕ):(wrF a i v).size=ѡa:=by unfold wrF;split;. obtain ⟨ds⟩:=a;exact Array.size_uset _ . rfl lemma qEt (a:ђ) (i:є) (v:ѕ) (q:ћ) (hs:ѡa<2^32) (hi:Ѡi< ѡa) : (wrF a i v).data[q]! = if q=Ѡi then v else a.data[q]!:=by have h:i< a.usize:=by rw[А,FloatArray.usize];rwa [qAd _ hs] unfold wrF;rw[П h];obtain ⟨ds⟩:=a;exact qFo ds _ q v hi def C_keep (xw:ћ->њ) (t:ћ):љ := (З t).filter (λp=>xw p &&& В != 0) section variable (xw:ћ->њ) (t:ћ) include xw t lemma qBj:(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& В != 0) (З t);simpa [C_keep] using h lemma qDp (h:xw t &&& В=0) : C_keep xw (t+1)=C_keep xw t:=by simp[C_keep,List.range_succ,List.filter_append,h] lemma qD5 (h:¬ xw t &&& В=0) : C_keep xw (t+1)=C_keep xw t ++ [t]:=by simp[C_keep,List.range_succ,List.filter_append,h] end def C_drop (xw:ћ->њ) (t:ћ):љ:= (З t).filter (λp=>xw p &&& В == 0) def C_Dz (xw:ћ->њ) (sz t:ћ) (iz:ѐ) (z:є):Prop:= Ѡz=(C_drop xw t).length∧ѡiz=sz∧∀ k p:ћ,(C_drop xw t)[k]?=some p->(iz[k]! : ѓ).toNat=p lemma qZj (xw:ћ->њ) (t:ћ):(C_drop xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& В == 0) (З t);simpa [C_drop] using h lemma qZp (xw:ћ->њ) (t:ћ) (h:xw t &&& В=0):C_drop xw (t+1)=C_drop xw t ++ [t]:=by simp[C_drop,List.range_succ,List.filter_append,h] lemma qZr (xw:ћ->њ) (t:ћ) (h:¬ xw t &&& В=0):C_drop xw (t+1)=C_drop xw t:=by simp[C_drop,List.range_succ,List.filter_append,h] def C_Dec (xw:ћ->њ) (sz t:ћ) (ix:ѐ) (buf:ђ) (c:є) (l:ѕ):Prop := Ѡc=(C_keep xw t).length∧ѡix=sz∧(sz<=ѡbuf∧ѡbuf<2^32) ∧ (∀ k p:ћ,(C_keep xw t)[k]?=some p → (ix[k]! : ѓ).toNat=p∧buf.data[k]! = dv (xw p)) ∧ l=(C_keep xw t).foldl (λ(a:ѕ) p=>a+(dv (xw p)).abs) 0 lemma qG6 {X:ѐ} {o n:є} (xw:ћ->њ) (hn:Ѡn<2^32) (hxw:∀ t:є,t< n->rdW X (o+t)=xw Ѡt) (hg:∀ t,t< Ѡn->good (xw t)=true) (d:ћ) : ∀ (t:є) (ix:ѐ) (buf:ђ) (c:є) (hh:ѕ),Ѡt+d=Ѡn->C_Dec xw Ѡn Ѡt ix buf c hh → C_Dec xw Ѡn Ѡn (decR X o n t ix buf c hh).ix (decR X o n t ix buf c hh).buf (decR X o n t ix buf c hh).c (decR X o n t ix buf c hh).l:=by induction d with | zero => intro t ix buf c hh he H rw[decR,Ш (by rw[А];omega)] have e:Ѡt=Ѡn :=א rw[e] at H;exact H | succ d ih => intro t ix buf c hh he H have ht:t< n:=by rw[А];omega have hu:=usz t n ht rw[decR,П ht] dsimp only rw[hxw t ht] obtain ⟨h1,h2,h3,h4,h5⟩:=H have h31:=h3.1 have h32:=h3.2 by_cases hz:xw Ѡt &&& В=0 . rw[с (by simpa using hz)];apply ih (t+1) _ _ _ _ א;rw[hu,C_Dec,qDp xw _ hz];exact ⟨h1,h2,h3,h4,h5⟩ . rw[Ф (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qBj xw Ѡt;have hc:Ѡc< Ѡt+1 :=א;rw[hu,C_Dec,qD5 xw _ hz];have hcn:Ѡc< Ѡn :=א;refine ⟨?_,by rw[qBh];exact h2,by rw[qD6];exact h3,λk p hk2=>?_,by rw[List.foldl_append,<-h5];rfl⟩;. rw[qAf _ _ (by rw[ض];omega),ض,h1];simp . rw[qBu _ _ _ _ א א,qEt _ _ _ _ א א];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=Ѡc;. subst e;simp at hk2;subst hk2;simp;omega . rw[Ф e,Ф e];by_cases e2:k< Ѡc;. rw[с e2] at hk2;exact h4 k p hk2 . rw[Ф e2] at hk2;obtain ⟨m,hm⟩:∃ m,k-Ѡc=m+1:=⟨k-Ѡc-1,by omega⟩;rw[hm] at hk2;simp at hk2 lemma qZD {X:ѐ} {o n:є} (xw:ћ->њ) (hn:Ѡn<2^32) (hxw:∀ t:є,t< n->rdW X (o+t)=xw Ѡt) (d:ћ) : ∀ (t:є) (iz:ѐ) (z:є),Ѡt+d=Ѡn->C_Dz xw Ѡn Ѡt iz z → C_Dz xw Ѡn Ѡn (decZ X o n t iz z).iz (decZ X o n t iz z).z:=by induction d with | zero => intro t iz z he H rw[decZ,Ш (by rw[А];omega)] have e:Ѡt=Ѡn :=א rw[e] at H;exact H | succ d ih => intro t iz z he H have ht:t< n:=by rw[А];omega have hu:=usz t n ht rw[decZ,П ht] rw[hxw t ht] obtain ⟨h1,h2,h4⟩:=H have hk:=qZj xw Ѡt have hcn:Ѡz< Ѡn:=by omega have hs:ѡiz<2^32:=by omega have hzs:Ѡz< ѡiz:=by omega have eT:(t.toUInt32).toNat=Ѡt:=by simp;omega by_cases hz:xw Ѡt &&& В=0 . rw[if_pos (by simpa using hz)];apply ih (t+1) _ _ א;rw[hu,C_Dz,qZp xw _ hz];refine ⟨?_,by rw[qBh];exact h2,λk p hk2=>?_⟩ . rw[qAf _ _ (by rw[ض];omega),ض,h1];simp . rw[qBu _ _ _ _ hs hzs];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=Ѡz . subst e;simp at hk2;subst hk2;rw[if_pos rfl];exact eT . rw[if_neg e];by_cases e2:k< Ѡz . rw[if_pos e2] at hk2;exact h4 k p hk2 . rw[if_neg e2] at hk2;obtain ⟨m,hm⟩:∃ m,k-Ѡz=m+1:=⟨k-Ѡz-1,by omega⟩;rw[hm] at hk2;simp at hk2 . rw[if_neg (by simpa using hz)];apply ih (t+1) _ _ א;rw[hu,C_Dz,qZr xw _ hz];refine ⟨h1,by rw[qBh];exact h2,λk p hk2=>?_⟩ have kl:k< Ѡz:=by rw[h1];exact (List.getElem?_eq_some_iff.mp hk2).1 rw[qBu _ _ _ _ hs hzs,if_neg (by omega)];exact h4 k p hk2 lemma qZE {X:ѐ} {a n:ћ} (hX:ѡX<2^32) (hn:a+n<=ѡX) (hn2:n<2^32) : C_Dz (λp=>X[a+p]!) n n (decZ X Ѣa Ѣn 0 (К n 0) 0).iz (decZ X Ѣa Ѣn 0 (К n 0) 0).z:=by have eo:a.toUSize.toNat=a:=qAd _ א have ek:n.toUSize.toNat=n:=qAd _ א have D:=qZD (X:=X) (o:=Ѣa) (n:=Ѣn) (λp=>X[a+p]!) (by rw[ek];omega) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[qAg _ _ hX,qAf _ _ (by rw[eo];omega),eo]) n 0 (К n 0) 0 (by rw[ek,C_z,Т]) ⟨by simp[C_drop],by rw[ek];simp,λk p hk=>by simp[C_drop] at hk⟩ rw[ek] at D exact D lemma qAu (a:ђ) (v:ѕ):(a.push v).size=ѡa+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qDy (n:є) (d:ћ):∀ (k:є) (a:ђ),Ѡk+d=Ѡn → (pushZ n k a).size=ѡa+d:=by induction d with | zero => intro k a he;rw[pushZ,Ш (by rw[А];omega)];rfl | succ d ih => intro k a he;have ht:k< n:=by rw[А];omega rw[pushZ,П ht,ih (k+1) _ (by rw[usz k n ht];omega),qAu];omega lemma qH6 {x:ђ} {ix W:ѐ} {w0 m n:є} (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (xf:ћ->ѕ) (ww:ћ->њ) (hww:∀ p,p< Ѡm->W[Ѡw0+p]! = ww p) (L:љ):∀ (t:є) (a:ѕ),Ѡt+ѣL=Ѡn → (∀ k p:ћ,L[k]?=some p->p< Ѡm∧(ix[Ѡt+k]! : ѓ).toNat=p ∧ x.data[Ѡt+k]! = xf p) → go1 x ix W w0 m n t hx hi hs h0 a =L.foldl (λ(acc:ѕ) p=>acc+xf p*dv (ww p)) a:=by induction L with | nil => intro t a he _ rw[go1,Ш (by rw[А];simp at he;omega)] rfl | cons p L ih => intro t a he H simp only[List.length_cons] at he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א obtain ⟨h1,h2,h3⟩:=H 0 p rfl rw[ю] at h2 h3 have hit:Ѡt< ѡix :=א have e0:(ix.uget t hit).toUSize.toNat=p:=by rw[ص,qC3];exact h2 have hm:(ix.uget t hit).toUSize< m:=by rw[А,e0];exact h1 have e1:x.uget t א=xf p:=by rw[qC5,h3] have e2:∀ pf,W.uget (w0+(ix.uget t hit).toUSize) pf=ww p:=by intro pf rw[qC3,qAf _ _ (by rw[e0];omega),e0,hww p h1] rw[go1,П ht] dsimp only rw[П hm] rw[e1,e2,ih (t+1) _ (by rw[usz t n ht];omega),ك] intro k q hq have:=H (k+1) q (by simpa using hq) rw[usz t n ht,show Ѡt+1+k=Ѡt+(k+1) by omega] exact this def C_Dw (wd:ђ) (W:ё):Prop := ѡwd=ѡW->∀ q,q< ѡW->wd.data[q]! = dv W[q]! section variable {x:ђ} {ix W:ѐ} {w0 w1 w2 w3 m n:є} (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) lemma qH0 (hv:∀ j,j< Ѡn->∃ w,good w=true∧x.data[j]! = dv w) (k:ћ) : ∀ (t:є) (a b c d:ѕ),Ѡt+k=Ѡn → go4B x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4B go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א unfold go4B go1 simp only[dite_eq_left ht] obtain ⟨w,g,e⟩:=hv _ htn have B:∀ (u:ѓ) p0,dvB (x.uget t p0) u=x.uget t p0*dv u:=by intro u p0 rw[qC5,e] exact qFw w u g have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] lemma qHE (k:ћ) : ∀ (t:є) (a b c d:ѕ),Ѡt+k=Ѡn → go4E x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4E go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א unfold go4E go1 simp only[dite_eq_left ht] have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[J] lemma qHq {wd:ђ} (hd:ѡwd=ѡW) (hv:C_Dw wd W) (k:ћ) : ∀ (t:є) (a b c d:ѕ),Ѡt+k=Ѡn → go4D x wd ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 hd a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4D go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א unfold go4D go1 simp only[dite_eq_left ht] have B:∀ (u:є) p1,dv (W.uget u p1)=wd.uget u (hd ▸ p1):=λu p1=>by rw[qC5,hv hd _ p1,qC3] have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] end def C_sp (I O:ћ) (Y:ј) (X W b:ё) (q:ћ):њ := C_out I Y X W b (q/O) (q%O) lemma qCi (I O:ћ) (Y:ј) (X W b:ё) {i j:ћ} (hj:j< O) : C_sp I O Y X W b (i*O+j)=C_out I Y X W b i j:=by unfold C_sp;have h0:0< O :=א;rw[е i O,Nat.mul_add_div h0,Nat.mul_add_mod,ؼ hj,М hj,ю] lemma qAp {N:ћ} {sp:ћ->њ} {p:ћ} {out:ё} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:є} {v:њ} (hu:Ѡu=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q< p+1) (wrW out u v) := qA5 (qAk hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qFk {u:є} {p:ћ} (hu:Ѡu=p) (h:p+3<2^32) : (u+1).toNat=p+1∧(u+2).toNat=p+2∧(u+3).toNat=p+3 ∧ (p+4<2^32->(u+4).toNat=p+4):=by have l:∀ k,k<5->p+k<2^32->(u+USize.ofNat k).toNat=p+k:=λk hk h=>by rw[qAf _ _ (by rw[uk k א];omega),uk k א,hu] exact ⟨l 1 א א,l 2 א א,l 3 א א,l 4 א⟩ lemma qEq {N:ћ} {sp:ћ->њ} {p:ћ} {out:ё} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:є} {v0 v1 v2 v3:њ} (hu:Ѡu=p) (hp:p+4<=N) (h0:v0=sp p) (h1:v1=sp (p+1)) (h2:v2=sp (p+2)) (h3:v3=sp (p+3)):C_Inv N sp (λq=>q< p+4) (wrW (wrW (wrW (wrW out u v0) (u+1) v1) (u+2) v2) (u+3) v3):=by obtain ⟨a1,a2,a3,-⟩:=qFk hu א exact qAp hN (qAp hN (qAp hN (qAp hN h hu א h0) a1 א h1) a2 א h2) a3 א h3 lemma qEy {X:ѐ} {a n:ћ} (hX:ѡX<2^32) (hn:a+n<=ѡX) (hg:∀ t,t< n->good X[a+t]! = true) {ix:ѐ} {buf:ђ} (h1:ѡix=n) (h2:n<=ѡbuf∧ѡbuf<2^32) : C_Dec (λp=>X[a+p]!) n n (decR X Ѣa Ѣn 0 ix buf 0 0).ix (decR X Ѣa Ѣn 0 ix buf 0 0).buf (decR X Ѣa Ѣn 0 ix buf 0 0).c (decR X Ѣa Ѣn 0 ix buf 0 0).l:=by have eo:a.toUSize.toNat=a:=qAd _ א have ek:n.toUSize.toNat=n:=qAd _ א have D:=qG6 (X:=X) (o:=Ѣa) (n:=Ѣn) (λp=>X[a+p]!) (by rw[ek];omega) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[qAg _ _ hX,qAf _ _ (by rw[eo];omega),eo]) (λt ht=>hg t (by rwa [ek] at ht)) n 0 ix buf 0 0 (by rw[C_z,ek,Т]) ⟨by simp[C_keep],by rw[ek];exact h1,by rw[ek];exact h2,λk p hk=>by simp[C_keep] at hk,by simp[C_keep]⟩ rw[ek] at D exact D lemma qDs (xw:ћ->њ) (n:ћ):(C_keep xw n).Nodup∧(∀ t∈C_keep xw n,t< n) ∧ ∀ t,t< n->t∉C_keep xw n->xw t &&& В=0 := ⟨ػ.filter _,λt ht=>Р.mp (List.mem_filter.mp ht).1, λt ht hn=>by by_contra hc exact hn (List.mem_filter.mpr ⟨Р.mpr ht,by simpa using hc⟩)⟩ structure C_Row (I:ћ) (X:ё) (i:ћ) (ix:ѐ) (x:ђ) (c:є) (L:љ) (l:ѕ):Prop where len:Ѡc=ѣL cle:ѣL<=I six:ѡix=I sx:I<=ѡx∧ѡx<2^32 nd:L.Nodup lt:∀ t∈L,t< I hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0 rd:∀ k p:ћ,L[k]?=some p → (ix[k]! : ѓ).toNat=p∧x.data[k]! = dv X[i*I+p]! ok:l=L.foldl (λ(a:ѕ) t=>a+(dv X[i*I+t]!).abs) 0 section variable {R I O:ћ} {X W b:ё} section variable (G:C_G R I O X W b) include G lemma C_G.nm:I.toUSize.toNat=I :=qAd _ (lt_trans G.ni ב) lemma C_G.wj {j:ћ} {u:є} (hu:Ѡu=j) (hj:j< O):(u*ѢI).toNat=j*I ∧ (u*ѢI).toNat+I.toUSize.toNat<=ѡW:=by have h1:j*I+I<=O*I:=qBc hj (ي _) have h2:=G.hW have h3:=G.sW have e:(u*ѢI).toNat=j*I:=by rw[qBa _ _ (by rw[hu,G.nm];omega),hu,G.nm] exact ⟨e,by rw[e,G.nm];omega⟩ lemma qB9 (Y:ј) {i j:ћ} (hi:i< R) (hj:j< O) {hv:њ} (Hv:C_Hv W hv) {ix:ѐ} {x:ђ} {c:є} {L:љ} {l:ѕ} (Rw:C_Row I X i ix x c L l) {u:є} (hu:Ѡu=j) (hx:Ѡc<=ѡx) (hi':Ѡc<=ѡix) (hs:ѡW<2^32) (h0:(u*ѢI).toNat+I.toUSize.toNat<=ѡW) : fin8 I Y X W b i j (go1 x ix W (u*ѢI) ѢI c 0 hx hi' hs h0 0) (nfOf I) l hv (rdW b u) ѢI=C_out I Y X W b i j:=by have e:=qH6 hx hi' hs h0 (λp=>dv X[i*I+p]!) (λp=>W[j*I+p]!) (λp _=>by rw[(G.wj hu hj).1]) L 0 0 (by rw[C_z,Т];exact Rw.len.symm) (λk p hk=>by have h1:=Rw.rd k p hk rw[C_z,Т,G.nm] exact ⟨Rw.lt p (List.mem_of_getElem? hk),h1⟩) rw[qAg _ _ G.sb,hu,Rw.ok] exact qDu G Y hi hj Hv.1 Hv.2.1 (λt ht=>Hv.2.2.2 _ (by rw[<-G.hW];exact qCj hj ht)) L Rw.nd Rw.lt Rw.hz e lemma qD0 (Y:ј) {i:ћ} (hi:i< R) {hv:њ} (Hv:C_Hv W hv) {ix:ѐ} {x wd:ђ} {c:є} {L:љ} {l:ѕ} (Rw:C_Row I X i ix x c L l) {base:є} (hb:Ѡbase=i*O) (Hd:C_Dw wd W) (d:ћ):∀ (j:ћ) (out:ё),j+d=O → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+j) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+O) (cols1 I O Y X W b i ix x wd (nfOf I) l hv ѢI c j base out):=by induction d using Nat.strong_induction_on with | _ d ih => intro j out he H have hN:=G.sO have sW:ѡW<=4294967295:=by have:=G.sW;omega have hbs:=G.sb have hob:=G.hb have hle:=Rw.cle have h1c:Ѡc<=ѡx:=by rw[Rw.len];exact hle.trans Rw.sx.1 have h2c:Ѡc<=ѡix:=by rw[Rw.six,Rw.len];exact hle have hsp:∀ k,j+k< O->C_out I Y X W b i (j+k) =C_sp I O Y X W b (i*O+j+k):=λk hk=>by rw[Գ];exact (qCi I O Y X W b hk).symm by_cases h4:j+4<=O . have hP:=qCj hi (א:j+3< O);have e0:j.toUSize.toNat=j:=qAd _ א;obtain ⟨e1,e2,e3,-⟩:=qFk e0 א;have g0:=(G.wj e0 א).2;have g1:=(G.wj e1 א).2;have g2:=(G.wj e2 א).2;have g3:=(G.wj e3 א).2;have a0:(base+Ѣj).toNat=i*O+j:=by rw[qAf _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qB9 G Y hi (א:j< O) Hv Rw e0 h1c h2c G.sW g0;have o1:=qB9 G Y hi (א:j+1< O) Hv Rw e1 h1c h2c G.sW g1;have o2:=qB9 G Y hi (א:j+2< O) Hv Rw e2 h1c h2c G.sW g2;have o3:=qB9 G Y hi (א:j+3< O) Hv Rw e3 h1c h2c G.sW g3;rw[cols1,П h4];dsimp only;rw[П ⟨h1c,h2c,sW,g0,g1,g2,g3⟩];split;rename_i hd;rw[qHq _ _ _ _ _ _ _ hd Hd Ѡc 0 _ _ _ _ (Т _)];swap split rw[qH0 _ _ _ _ _ _ _ (λj hj=>by rw[Rw.len] at hj;have hp:=List.getElem?_eq_getElem hj exact ⟨_,G.gx hi _ (Rw.lt _ (List.mem_of_getElem? hp)),(Rw.rd j _ hp).2⟩) Ѡc 0 _ _ _ _ (Т _)] swap rw[qHE _ _ _ _ _ _ _ Ѡc 0 _ _ _ _ (Т _)] all_goals dsimp only;rw[o0,o1,o2,o3];refine ih (d-4) א (j+4) _ א ?_;rw[show i*O+(j+4)=i*O+j+4 by omega];exact qEq hN H a0 א (hsp 0 א) (hsp 1 א) (hsp 2 א) (hsp 3 א) . by_cases h1:j< O . have hP:=qCj hi h1;have e0:j.toUSize.toNat=j:=qAd _ א;have g0:=(G.wj e0 h1).2;have a0:(base+Ѣj).toNat=i*O+j:=by rw[qAf _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qB9 G Y hi h1 Hv Rw e0 h1c h2c G.sW g0;rw[cols1,Ш h4,П h1];dsimp only;rw[П ⟨h1c,h2c,sW,g0⟩];rw[o0];exact ih (d-1) א (j+1) _ א (qAp hN H a0 hP (hsp 0 א)) . rw[cols1,Ш h4,Ш h1];have e:j=O :=א;rw[e] at H;exact H lemma qGf (Y:ј) {hv:њ} (Hv:C_Hv W hv) {wd:ђ} (Hd:C_Dw wd W) (d:ћ):∀ (i:ћ) (ix:ѐ) (buf:ђ) (out:ё),i+d=R->ѡix=I->(I<=ѡbuf∧ѡbuf<2^32) → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< R*O) (rows1 R I O Y X W b wd (nfOf I) hv ѢI i ix buf out):=by induction d with | zero => intro i ix buf out he _ _ H rw[rows1,Ш א] have e:i=R :=א rw[e] at H;exact H | succ d ih => intro i ix buf out he h1 h2 H have hi:i< R :=א have hin:I<2^32:=lt_trans G.ni ב have hN:=G.sO have sX:=G.sX have hX:=G.hX have hP:i*I+I<=R*I:=qBc hi (ي _) have hbs:(i*O).toUSize.toNat=i*O:=by have h3:i*O+O<=R*O:=qBc hi (ي _) exact qAd _ א obtain ⟨d1,d2,d3,d4,d5⟩:=qEy sX א (G.gx hi) h1 h2 obtain ⟨k1,k2,k3⟩:=qDs (λp=>X[i*I+p]!) I have Rw:=C_Row.mk d1 (qBj _ _) d2 d3 k1 k2 k3 d4 d5 rw[rows1,П hi] dsimp only refine ih (i+1) _ _ _ א d2 d3 ?_ have h5:=qD0 G Y hi Hv Rw hbs Hd O 0 out א (by rw[ю];exact H) rw[show (i+1)*O=i*O+O by rw[Nat.add_mul,ه]] exact h5 end end lemma qIy (l:List ѓ) (a:ђ) : (l.foldl (λa w=>a.push (dv w)) a).data=a.data ++ (l.map dv).toArray:=by induction l generalizing a with | nil=>simp | cons w l ih => rw[ك,ih];simp[FloatArray.push] section variable {rows inW outW:ћ} {X W b:ё} def C_hf (d:H8):ћ->ѕ | 0=>d.h0 | 1=>d.h1 | 2=>d.h2 | 3=>d.h3 | 4=>d.h4 | 5=>d.h5 | 6=>d.h6 | _=>d.h7 def C_fs (d:F8):ћ->ѕ | 0=>d.s0 | 1=>d.s1 | 2=>d.s2 | 3=>d.s3 | 4=>d.s4 | 5=>d.s5 | 6=>d.s6 | _=>d.s7 def C_D8 (X:ё) (m t:ћ) (d:H8):Prop := (8*m<=d.xs.size∧d.xs.size<2^32)∧(∀ k r:ћ,k< t->r<8->d.xs.data[8*k+r]! = dv X[r*m+k]!) ∧ ∀ r:ћ,r<8->C_hf d r=(List.range t).foldl (λ(a:ѕ) k=>a+(dv X[r*m+k]!).abs) 0 lemma qB3 {n t c:є} {k:ћ} (hc:Ѡc=k) (h:k*Ѡn+Ѡt<2^32) : (c*n+t).toNat=k*Ѡn+Ѡt:=by have h0:k*Ѡn<2^32 :=א;rw[qAf _ _ (by rw[qBa _ _ (by rw[hc];exact h0),hc];exact h),qBa _ _ (by rw[hc];exact h0),hc] lemma qCp {X:ё} {n t c:є} {k:ћ} (hs:ѡX<2^32) (hc:Ѡc=k) (h:k*Ѡn+Ѡt<2^32) : rdW X (c*n+t)=X[k*Ѡn+Ѡt]!:=by rw[qAg _ _ hs,qB3 hc h] lemma qWc (a:ђ) (i:є) (j:ћ) (hj:Ѡi=j) (v:ѕ) (hs:ѡa<2^32) (hi:j< ѡa) (q:ћ):(wrF a i v).data[q]! = if q=j then v else a.data[q]!:=by rw[qEt a i v q hs (by rw[hj];exact hi),hj] lemma qW8 (xs:ђ) (t:є) (a0 a1 a2 a3 a4 a5 a6 a7:ѕ) (hs:ѡxs<2^32) (hb:8*Ѡt+8<=ѡxs) : (wr8 xs (8*t) a0 a1 a2 a3 a4 a5 a6 a7).size=ѡxs∧∀ q,(wr8 xs (8*t) a0 a1 a2 a3 a4 a5 a6 a7).data[q]! = if q=8*Ѡt+7 then a7 else if q=8*Ѡt+6 then a6 else if q=8*Ѡt+5 then a5 else if q=8*Ѡt+4 then a4 else if q=8*Ѡt+3 then a3 else if q=8*Ѡt+2 then a2 else if q=8*Ѡt+1 then a1 else if q=8*Ѡt+0 then a0 else xs.data[q]!:=by have l8:(8:є).toNat=8:=uk 8 ב have e8:(8*t).toNat=8*Ѡt:=by rw[qBa _ _ (by rw[l8];omega),l8] have E:∀ k,k<8->(8*t+USize.ofNat k).toNat=8*Ѡt+k:=λk hk=>by rw[qAf _ _ (by rw[e8,uk k א];omega),e8,uk k א] unfold wr8 refine ⟨by simp only[qD6],λq=>?_⟩ rw[qWc _ _ _ (E 7 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 6 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 5 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 4 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 3 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 2 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 1 א) _ (by simp only[qD6];exact hs) (by simp only[qD6];omega) q, qWc _ _ _ (E 0 א) _ hs (by omega) q] lemma qZs (m:ћ) (h:m<2^32):(pushZ (USize.ofNat m) 0 (Й m)).size=m:=by rw[qDy (USize.ofNat m) m 0 _ (by rw[uk m h,C_z,Т])];exact Т _ lemma qZf (m:ћ) (h:m<2^32):m<=(zFor m).size∧(zFor m).size<2^32:=by unfold zFor;split . have e:ѡz0=128:=qZs 128 ב rw[e];omega . split . have e:ѡz1=1024:=qZs 1024 ב rw[e];omega . split . have e:ѡz4=4096:=qZs 4096 ב rw[e];omega . split . have e:ѡz8=8192:=qZs 8192 ב rw[e];omega . have e:(pushZ Ѣm 0 (Й m)).size=m:=qZs m h rw[e];omega lemma qGq {X:ё} {n:є} (hs:ѡX<2^32) (h8:8*Ѡn<=ѡX) (hg:∀ q,q< ѡX->good X[q]! = true) (d:ћ) : ∀ (t:є) (xs:ђ) (h0 h1 h2 h3 h4 h5 h6 h7:ѕ),Ѡt+d=Ѡn → C_D8 X Ѡn Ѡt ⟨xs,h0,h1,h2,h3,h4,h5,h6,h7⟩ → C_D8 X Ѡn Ѡn (dec8 X n t xs h0 h1 h2 h3 h4 h5 h6 h7):=by induction d with | zero => intro t xs h0 h1 h2 h3 h4 h5 h6 h7 he H rw[dec8,Ш (by rw[А];omega)] have e:Ѡt=Ѡn :=א rw[e] at H;exact H | succ d ih => intro t xs h0 h1 h2 h3 h4 h5 h6 h7 he H have ht:t< n:=by rw[А];omega have hu:=usz t n ht have l2:(2:є).toNat=2:=uk 2 ב have l3:(3:є).toNat=3:=uk 3 ב have l4:(4:є).toNat=4:=uk 4 ב have l5:(5:є).toNat=5:=uk 5 ב have l6:(6:є).toNat=6:=uk 6 ב have l7:(7:є).toNat=7:=uk 7 ב have r0:rdW X t=X[0*Ѡn+Ѡt]!:=by rw[qAg _ _ hs,ف,Т] have r1:rdW X (n+t)=X[1*Ѡn+Ѡt]!:=by rw[qAg _ _ hs,qAf _ _ א,ه] rw[dec8,П ht] dsimp only rw[r0,r1,qCp hs l2 א,qCp hs l3 א,qCp hs l4 א,qCp hs l5 א,qCp hs l6 א,qCp hs l7 א] obtain ⟨H1,H2,H3⟩:=H dsimp only at H1 H2 obtain ⟨Z1,Z2⟩:=qW8 xs t (dv X[0*Ѡn+Ѡt]!) (dv X[1*Ѡn+Ѡt]!) (dv X[2*Ѡn+Ѡt]!) (dv X[3*Ѡn+Ѡt]!) (dv X[4*Ѡn+Ѡt]!) (dv X[5*Ѡn+Ѡt]!) (dv X[6*Ѡn+Ѡt]!) (dv X[7*Ѡn+Ѡt]!) H1.2 (by have:=H1.1;omega) refine ih (t+1) _ _ _ _ _ _ _ _ _ א ⟨?_,?_,?_⟩ . dsimp only;rw[Z1];exact H1 . intro k r hk hr;rw[hu] at hk;dsimp only;rw[Z2];by_cases e:k=Ѡt . subst e;interval_cases r<;>simp . rw[if_neg (by omega),if_neg (by omega),if_neg (by omega),if_neg (by omega),if_neg (by omega),if_neg (by omega),if_neg (by omega),if_neg (by omega)];exact H2 k r א hr . intro r hr;have h9:=H3 r hr;rw[hu,List.range_succ,List.foldl_append,<-h9];interval_cases r<;>rfl lemma qBq {xs:ђ} {i t n:є} {xf:ћ->ћ->ѕ} (hxf:∀ r k:ћ,r<8->k< Ѡn->xs.data[8*k+r]! = xf r k) (hi:Ѡi=8*Ѡt) (ht:Ѡt< Ѡn) (hsx:ѡxs<2^32) (hx:8*Ѡn<=ѡxs) (r:ћ) (hr:r<8) (pf:(i+USize.ofNat r).toNat< ѡxs) : xs.uget (i+USize.ofNat r) pf=xf r Ѡt:=by rw[qC5,qAf _ _ (by rw[uk r א];omega),uk r א,hi,hxf r _ hr ht] lemma qF4 {outW q j:ћ} (hj:j< outW) (hq:q<8*outW) (h:q%outW< j+1) : q=7*outW+j∨q=6*outW+j∨q=5*outW+j∨q=4*outW+j ∨ q=3*outW+j∨q=2*outW+j∨q=1*outW+j∨q=0*outW+j ∨ q%outW< j:=by by_cases e:q%outW< j . simp only[e,or_true] . have e2:q%outW=j :=א;have e3:=Nat.div_add_mod q outW;have e4:q/outW<8:=ш (by rwa [е]);rw[e2,е] at e3;generalize q/outW=a at e3 e4;interval_cases a<;>omega lemma qW9 {O j:ћ} {sp:ћ->њ} {out:ё} (h8:8*O<2^32) (hj:j< O) (H:C_Inv (8*O) sp (λq=>q%O< j) out) : C_Inv (8*O) sp (λq=>q%O< j+1) (wrW (wrW (wrW (wrW (wrW (wrW (wrW (wrW out Ѣj (sp (0*O+j))) (ѢO+Ѣj) (sp (1*O+j))) (2*ѢO+Ѣj) (sp (2*O+j))) (3*ѢO+Ѣj) (sp (3*O+j))) (4*ѢO+Ѣj) (sp (4*O+j))) (5*ѢO+Ѣj) (sp (5*O+j))) (6*ѢO+Ѣj) (sp (6*O+j))) (7*ѢO+Ѣj) (sp (7*O+j))):=by have e0:j.toUSize.toNat=j:=qAd _ א have eo:O.toUSize.toNat=O:=qAd _ א have l2:(2:є).toNat=2:=uk 2 ב have l3:(3:є).toNat=3:=uk 3 ב have l4:(4:є).toNat=4:=uk 4 ב have l5:(5:є).toNat=5:=uk 5 ב have l6:(6:є).toNat=6:=uk 6 ב have l7:(7:є).toNat=7:=uk 7 ב have u0:j.toUSize.toNat=0*O+j:=by rw[e0];omega have u1:(ѢO+Ѣj).toNat=1*O+j:=by rw[qAf _ _ (by rw[eo,e0];omega),eo,e0];omega have a2:=qB3 (n:=ѢO) (t:=Ѣj) l2 (by rw[eo,e0];omega) have a3:=qB3 (n:=ѢO) (t:=Ѣj) l3 (by rw[eo,e0];omega) have a4:=qB3 (n:=ѢO) (t:=Ѣj) l4 (by rw[eo,e0];omega) have a5:=qB3 (n:=ѢO) (t:=Ѣj) l5 (by rw[eo,e0];omega) have a6:=qB3 (n:=ѢO) (t:=Ѣj) l6 (by rw[eo,e0];omega) have a7:=qB3 (n:=ѢO) (t:=Ѣj) l7 (by rw[eo,e0];omega) rw[eo,e0] at a2 a3 a4 a5 a6 a7 have I0:=qAk h8 H u0 א rfl have I1:=qAk h8 I0 u1 א rfl have I2:=qAk h8 I1 a2 א rfl have I3:=qAk h8 I2 a3 א rfl have I4:=qAk h8 I3 a4 א rfl have I5:=qAk h8 I4 a5 א rfl have I6:=qAk h8 I5 a6 א rfl have I7:=qAk h8 I6 a7 א rfl exact qA5 I7 (λq hq hq'=>qF4 hj hq hq') end section open Float.Model Float.Model.UnpackedFloat local notation:max x:max "ᶠ"=>Float.isFinite x=true lemma qD2 (s:Sign):(Float.Model.pack (UnpackedFloat.infinity s)).unpack.isFinite=false:=by cases s<;>decide +kernel lemma qFh:(Float.Model.pack UnpackedFloat.notANumber).unpack.isFinite=false:=by decide +kernel lemma qC1 (x y:ѕ) (h:(x*y)ᶠ):xᶠ∧yᶠ:=by change (Float.Model.mul x.toModel y.toModel).unpack.isFinite=true at h;change x.toModel.unpack.isFinite=true∧y.toModel.unpack.isFinite=true;rw[Float.Model.mul] at h;cases a:x.toModel.unpack<;>cases b:y.toModel.unpack <;>simp only[a,b,UnpackedFloat.mul,qD2,qFh,Bool.false_eq_true] at h<;>exact ⟨rfl,rfl⟩ lemma qC2 (x y:ѕ) (h:(x+y)ᶠ):xᶠ∧yᶠ:=by change (Float.Model.add x.toModel y.toModel).unpack.isFinite=true at h;change x.toModel.unpack.isFinite=true∧y.toModel.unpack.isFinite=true;rw[Float.Model.add] at h cases a:x.toModel.unpack<;>cases b:y.toModel.unpack <;>simp only[a,b,UnpackedFloat.add,qD2,qFh,Bool.false_eq_true] at h <;> first | exact ⟨rfl,rfl⟩ | (split at h<;>simp only[qD2,qFh,Bool.false_eq_true] at h) lemma qHz:(О 0x7FF8000000000000).isFinite=false:=by decide +kernel def N_ok (w:ѓ):Prop:=0x800000<=w &&& В∧w &&& В<0x40000000 lemma qEx {w:ѓ} (h:N_ok w):good w=true :=qBt _ (Or.inr h.1) (by have t:w &&& В<0x40000000:=h.2;have a:(0x40000000:ѓ).toNat=0x40000000:=rfl;have b:(У:ѓ).toNat=У:=rfl;rw[у,a] at t;rw[у,b];omega) def N_v (hv w:ѓ):Prop:=N_ok w∧w &&& В<=hv def Z_ok (hv w:ѓ):Prop:=w &&& В<=hv lemma qTb {z:ћ} {T:ђ} {h:ѓ} (o:TOK z T h):Ѡh<0x40000000:=by obtain ⟨k,-,-,k2,k3,e⟩:=o;omega lemma qTg {z:ћ} {T:ђ} {h:ѓ} (o:TOK z T h):good h=true∧h<=В:=by obtain ⟨k,-,-,k2,k3,e⟩:=o have a:=qAq h refine ⟨qEx ⟨?_,?_⟩,?_⟩ . rw[Ж,a,show (0x800000:ѓ).toNat=0x800000 from rfl];omega . rw[у,a,show (0x40000000:ѓ).toNat=0x40000000 from rfl];omega . rw[Ж,show (В:ѓ).toNat=0x7FFFFFFF from rfl];omega lemma N_w {z:ћ} {T:ђ} {h:ѓ} (o:TOK z T h) (hT:ѡT=512) (w:ѓ) (f:(T.uget (w >>> (23:ѓ)).toUSize (qBx hT w))ᶠ) : T.uget (w >>> (23:ѓ)).toUSize (qBx hT w)=pw.uget (w >>> (23:ѓ)).toUSize (qCh w) ∧ z*0x800000<=(w &&& В).toNat ∧ w &&& В<=h:=by obtain ⟨k,-,eT,k2,k3,hk⟩:=o have s:=qD8 w rw[eT,ص,s] at f rw[eT,qDc,ص,s] unfold pwEntryN at f ⊢ have e:=qAq w have g:=w.toNat_lt by_cases c:(decide (Ѡw/2^23%256ѕ) (L:љ) (a:ѕ):ѕ := L.foldl (λacc s=>acc+f s) a lemma qB4 (f:ћ->ѕ) (L:љ) : ∀ a,(N_ff f L a)ᶠ->aᶠ∧∀ s∈L,(f s)ᶠ:=by induction L with | nil=>exact λa h=>⟨h,λs hs=>by simp at hs⟩ | cons p L ih => intro a h;obtain ⟨h1,h2⟩:=ih _ h;obtain ⟨h3,h4⟩:=qC2 _ _ h1;refine ⟨h3,λs hs=>?_⟩;rcases List.mem_cons.mp hs with e | e;. rw[e];exact h4 . exact h2 s e lemma qC0 (a b:ѓ):a &&& В<=(a ||| b) &&& В ∧ b &&& В<=(a ||| b) &&& В:=by rw[Ж,Ж];simp only[UInt32.toNat_and,UInt32.toNat_or,Nat.and_or_distrib_right];exact ⟨Nat.left_le_or,Nat.right_le_or⟩ section variable {T:ђ} {hT:ѡT=512} {x:ђ} {ix W:ѐ} {w0 w1 w2 w3 m n:є} (f:ѕ->ѓ->ѕ) lemma qIa (hx:Ѡn<=ѡx) (hs:ѡW<2^32) (h0:Ѡw0+Ѡn<=ѡW) (h1:Ѡw1+Ѡn<=ѡW) (h2:Ѡw2+Ѡn<=ѡW) (h3:Ѡw3+Ѡn<=ѡW) (k:ћ) : ∀ (t:є) (a b c d:ѕ),Ѡt+k=Ѡn → go4N f T hT x W w0 w1 w2 w3 n t hx hs h0 h1 h2 h3 a b c d = ⟨N_ff (λs=>x.data[s]! * dvN T hT W[Ѡw0+s]!) (List.range' Ѡt k) a,N_ff (λs=>f x.data[s]! W[Ѡw1+s]!) (List.range' Ѡt k) b,N_ff (λs=>x.data[s]! * dvN T hT W[Ѡw2+s]!) (List.range' Ѡt k) c,N_ff (λs=>f x.data[s]! W[Ѡw3+s]!) (List.range' Ѡt k) d⟩:=by induction k with | zero => intro t a b c d he rw[go4N,Ш (by rw[А];omega)] rfl | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א have U:=λ(u:є) (hu:Ѡu+Ѡn<=ѡW) pf =>(qC3 W (u+t) pf).trans (by rw[qAf _ _ א]) rw[go4N,П ht] dsimp only rw[qC5,U w0 h0,U w1 h1,U w2 h2,U w3 h3,ih (t+1) _ _ _ _ (by rw[usz t n ht];omega),usz t n ht,List.range'_succ] rfl lemma qZC (hx:Ѡn<=ѡx) (hi:Ѡn<=ѡix) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (xf:ћ->ѕ) (A B C D:ћ->њ) (ha:∀ p,p< Ѡm->W[Ѡw0+p]! = A p) (hb:∀ p,p< Ѡm->W[Ѡw1+p]! = B p) (hc:∀ p,p< Ѡm->W[Ѡw2+p]! = C p) (hd:∀ p,p< Ѡm->W[Ѡw3+p]! = D p) (L:љ):∀ (t:є) (a b c d:ѕ) (q:ѓ),Ѡt+ѣL=Ѡn → (∀ k p:ћ,L[k]?=some p->p< Ѡm∧(ix[Ѡt+k]! : ѓ).toNat=p∧x.data[Ѡt+k]! = xf p) → go4C f T hT x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d q = ⟨N_ff (λp=>xf p*dvN T hT (A p)) L a,N_ff (λp=>f (xf p) (B p)) L b,N_ff (λp=>xf p*dvN T hT (C p)) L c,N_ff (λp=>f (xf p) (D p)) L d,L.foldl (λq p=>qZw q (A p) (B p) (C p) (D p)) q⟩:=by induction L with | nil => intro t a b c d q he _ rw[go4C,Ш (by rw[А];simp at he;omega)] rfl | cons p L ih => intro t a b c d q he H simp only[List.length_cons] at he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א obtain ⟨g1,g2,g3⟩:=H 0 p rfl rw[ю] at g2 g3 have hit:Ѡt< ѡix :=א have e0:(ix.uget t hit).toUSize.toNat=p:=by rw[ص,qC3];exact g2 have hm:(ix.uget t hit).toUSize< m:=by rw[А,e0];exact g1 have e1:x.uget t א=xf p:=by rw[qC5,g3] have E:∀ (u:є) (ww:ћ->њ),(∀ p,p< Ѡm->W[Ѡu+p]! = ww p)->Ѡu+Ѡm<=ѡW->∀ pf,W.uget (u+(ix.uget t hit).toUSize) pf=ww p:=by intro u ww hww hu pf rw[qC3,qAf _ _ (by rw[e0];omega),e0,hww p g1] have J:=ih (t+1) (a+xf p*dvN T hT (A p)) (b+f (xf p) (B p)) (c+xf p*dvN T hT (C p)) (d+f (xf p) (D p)) (qZw q (A p) (B p) (C p) (D p)) (by rw[usz t n ht];omega) (λk r hr=>by have:=H (k+1) r (by simpa using hr) rw[usz t n ht,show Ѡt+1+k=Ѡt+(k+1) by omega] exact this) rw[go4C,П ht] dsimp only rw[П hm] rw[e1,E w0 A ha h0,E w1 B hb h1,E w2 C hc h2,E w3 D hd h3] exact J lemma qZU {iz:ѐ} (hi:Ѡn<=ѡiz) (hs:ѡW<2^32) (h0:Ѡw0+Ѡm<=ѡW) (h1:Ѡw1+Ѡm<=ѡW) (h2:Ѡw2+Ѡm<=ѡW) (h3:Ѡw3+Ѡm<=ѡW) (A B C D:ћ->њ) (ha:∀ p,p< Ѡm->W[Ѡw0+p]! = A p) (hb:∀ p,p< Ѡm->W[Ѡw1+p]! = B p) (hc:∀ p,p< Ѡm->W[Ѡw2+p]! = C p) (hd:∀ p,p< Ѡm->W[Ѡw3+p]! = D p) (L:љ):∀ (t:є) (p q:ѓ),Ѡt+ѣL=Ѡn → (∀ k r:ћ,L[k]?=some r->r< Ѡm∧(iz[Ѡt+k]! : ѓ).toNat=r) → go4U iz W w0 w1 w2 w3 m n t hi hs h0 h1 h2 h3 p q = ⟨L.foldl (λp s=>p ||| A s ||| B s ||| C s ||| D s) p,L.foldl (λq s=>qZw q (A s) (B s) (C s) (D s)) q⟩:=by induction L with | nil => intro t p q he _ rw[go4U,Ш (by rw[А];simp at he;omega)] rfl | cons r L ih => intro t p q he H simp only[List.length_cons] at he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א obtain ⟨g1,g2⟩:=H 0 r rfl rw[ю] at g2 have hit:Ѡt< ѡiz :=א have e0:(iz.uget t hit).toUSize.toNat=r:=by rw[ص,qC3];exact g2 have hm:(iz.uget t hit).toUSize< m:=by rw[А,e0];exact g1 have E:∀ (u:є) (ww:ћ->њ),(∀ p,p< Ѡm->W[Ѡu+p]! = ww p)->Ѡu+Ѡm<=ѡW->∀ pf,W.uget (u+(iz.uget t hit).toUSize) pf=ww r:=by intro u ww hww hu pf rw[qC3,qAf _ _ (by rw[e0];omega),e0,hww r g1] have J:=ih (t+1) (p ||| A r ||| B r ||| C r ||| D r) (qZw q (A r) (B r) (C r) (D r)) (by rw[usz t n ht];omega) (λk v hr=>by have:=H (k+1) v (by simpa using hr) rw[usz t n ht,show Ѡt+1+k=Ѡt+(k+1) by omega] exact this) rw[go4U,П ht] dsimp only rw[П hm] rw[E w0 A ha h0,E w1 B hb h1,E w2 C hc h2,E w3 D hd h3] exact J end lemma qGt {a b c d:ѕ} (h:(a+b+c+d)ᶠ):aᶠ∧bᶠ∧cᶠ∧dᶠ:=by obtain ⟨h1,h4⟩:=qC2 _ _ h;obtain ⟨h2,h3⟩:=qC2 _ _ h1;obtain ⟨h0,h5⟩:=qC2 _ _ h2;exact ⟨h0,h5,h3,h4⟩ section variable {T:ђ} {hT:ѡT=512} {hv:ѓ} {x:ђ} {W:ѐ} {u:ћ} {L:љ} include x W u L lemma qBy (oT:TOK 1 T hv) (h:(N_ff (λs=>x.data[s]! * dvN T hT W[u+s]!) L 0)ᶠ) : (∀ s∈L,N_v hv W[u+s]!)∧N_ff (λs=>x.data[s]! * dvN T hT W[u+s]!) L 0 =L.foldl (λacc s=>acc+x.data[s]! * dv W[u+s]!) 0:=by have F:=(qB4 _ _ _ h).2 have G:=λs hs=>qIj oT (qC1 _ _ (F s hs)).2 exact ⟨λs hs=>(G s hs).2,List.foldl_ext _ _ _ λacc s hs=>by show acc+x.data[s]! * dvN T hT W[u+s]! = _ rw[(G s hs).1]⟩ lemma qDh {f:ѕ->ѓ->ѕ} (hf:∀ v w,(f v w)ᶠ->N_v hv w) (h:(N_ff (λs=>f x.data[s]! W[u+s]!) L 0)ᶠ):∀ s∈L,N_v hv W[u+s]! :=λs hs=>hf _ _ ((qB4 _ _ _ h).2 s hs) lemma qDt {f:ѕ->ѓ->ѕ} (hf:∀ v w (y:ѓ),good y=true->v=dv y->(f v w)ᶠ->f v w=v*dv w) (hx:∀ s∈L,∃ w,good w=true∧x.data[s]! = dv w) (h:(N_ff (λs=>f x.data[s]! W[u+s]!) L 0)ᶠ) : N_ff (λs=>f x.data[s]! W[u+s]!) L 0 =L.foldl (λacc s=>acc+x.data[s]! * dv W[u+s]!) 0:=by refine List.foldl_ext _ _ _ λacc s hs=>?_ obtain ⟨w,g,e⟩:=hx s hs show acc+f x.data[s]! W[u+s]! = _ rw[hf _ _ w g e ((qB4 _ _ _ h).2 s hs)] end lemma qHt (A B C D:ћ->ѓ) (L:љ):∀ p,let f:=L.foldl (λp s=>p ||| A s ||| B s ||| C s ||| D s) p p &&& В<=f &&& В∧∀ s∈L,A s &&& В<=f &&& В ∧ B s &&& В<=f &&& В∧C s &&& В<=f &&& В ∧ D s &&& В<=f &&& В:=by induction L with | nil=>exact λp=>⟨و _,λs hs=>by simp at hs⟩ | cons q L ih => intro p obtain ⟨h1,h2⟩:=ih (p ||| A q ||| B q ||| C q ||| D q) simp only[List.foldl_cons] generalize L.foldl (λp s=>p ||| A s ||| B s ||| C s ||| D s) (p ||| A q ||| B q ||| C q ||| D q)=f at h1 h2 ⊢ obtain ⟨a1,a2⟩:=qC0 (p ||| A q ||| B q ||| C q) (D q) obtain ⟨b1,b2⟩:=qC0 (p ||| A q ||| B q) (C q) obtain ⟨c1,c2⟩:=qC0 (p ||| A q) (B q) obtain ⟨d1,d2⟩:=qC0 p (A q) have t1:=UInt32.le_trans a1 h1 have t2:=UInt32.le_trans b1 t1 have t3:=UInt32.le_trans c1 t2 refine ⟨UInt32.le_trans d1 t3,λs hs=>?_⟩ rcases List.mem_cons.mp hs with e | e . rw[e];exact ⟨UInt32.le_trans d2 t3,UInt32.le_trans c2 t2,UInt32.le_trans b2 t1,UInt32.le_trans a2 h1⟩ . exact h2 s e section variable {T:ђ} {hT:ѡT=512} {hv:ѓ} {I O:ћ} {X W b:ё} def N_q (f:ѕ->ѓ->ѕ) (T:ђ) (hT:ѡT=512) (hv:ѓ) (I O:ћ) (Y:ј) (X W b:ѐ) (x:ђ) (nf l:ѕ) (n:є) (k:ћ) (out:ѐ):CN := if hg:Ѡn<=ѡx∧ѡW<=4294967295∧(Ѣk*n).toNat+Ѡn<=ѡW ∧ ((Ѣk+1)*n).toNat+Ѡn<=ѡW∧((Ѣk+2)*n).toNat+Ѡn<=ѡW ∧ ((Ѣk+3)*n).toNat+Ѡn<=ѡW then let r:=go4N f T hT x W (Ѣk*n) ((Ѣk+1)*n) ((Ѣk+2)*n) ((Ѣk+3)*n) n 0 hg.1 (qA9 hg.2.1) hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2 0 0 0 0 if (r.a+r.b+r.c+r.d).isFinite then colsN f T hT hv I O Y X W b x nf l n (k+4) (wrW (wrW (wrW (wrW out Ѣk (fin8 I Y X W b 0 k r.a nf l hv (rdW b Ѣk) n)) (Ѣk+1) (fin8 I Y X W b 0 (k+1) r.b nf l hv (rdW b (Ѣk+1)) n)) (Ѣk+2) (fin8 I Y X W b 0 (k+2) r.c nf l hv (rdW b (Ѣk+2)) n)) (Ѣk+3) (fin8 I Y X W b 0 (k+3) r.d nf l hv (rdW b (Ѣk+3)) n)) else ⟨out,false⟩ else ⟨out,false⟩ lemma qHy {f:ѕ->ѓ->ѕ} {Y:ј} {x:ђ} {nf l:ѕ} {n:є} {j:ћ} {out:ѐ} (h:j< O∧4<=O):colsN f T hT hv I O Y X W b x nf l n j out =N_q f T hT hv I O Y X W b x nf l n (if j+4<=O then j else O-4) out:=by rw[colsN,П h] rfl set_option maxRecDepth 8000 in lemma qHo (oT:TOK 1 T hv) {f:ѕ->ѓ->ѕ} (hf1:∀ v w,(f v w)ᶠ->N_v hv w) (hf2:∀ v w (y:ѓ),good y=true->v=dv y->(f v w)ᶠ->f v w=v*dv w) {Y:ј} {x:ђ} {l:ѕ} (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (hxs:I<=ѡx∧ѡx<2^32) (d:ћ) : ∀ (j:ћ) (out:ё),O-j=d → (colsN f T hT hv I O Y X W b x (nfOf I) l ѢI j out).ok=true → (∀ j',j<=j'->j'< O->∀ s,s< I->N_v hv W[j'*I+s]!) ∧ ∀ (G:C_G 1 I O X W b),(∀ s,s< I->x.data[s]! = dv X[0*I+s]!) → l=(З I).foldl (λ(a:ѕ) t=>a+(dv X[0*I+t]!).abs) 0 → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< j) out → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< O) (colsN f T hT hv I O Y X W b x (nfOf I) l ѢI j out).out:=by have hnm:I.toUSize.toNat=I:=qAd _ א have hO:O<2^32:=by have:=ؽ O pi omega have W1:∀ {u:є} {k:ћ},Ѡu=k->k< O->(u*ѢI).toNat=k*I:=λ{u k} hu hk=>by have h1:k*I<=O*I:=Ь _ hk.le rw[qBa _ _ (by rw[hu,hnm];omega),hu,hnm] induction d using Nat.strong_induction_on with | _ d ih => intro j out he ok by_cases h:j< O∧4<=O . rw[qHy h] at ok ⊢;have hk:(if j+4<=O then j else O-4)<=j∧(if j+4<=O then j else O-4)+4<=O ∧ j<(if j+4<=O then j else O-4)+4:=by split<;>omega generalize (if j+4<=O then j else O-4)=k at hk ok ⊢;obtain ⟨k1,k2,k3⟩:=hk;have e0:k.toUSize.toNat=k:=qAd _ א;obtain ⟨e1,e2,e3,-⟩:=qFk e0 א;have z0:=W1 e0 (א:k< O);have z1:=W1 e1 (א:k+1< O);have z2:=W1 e2 (א:k+2< O);have z3:=W1 e3 (א:k+3< O);have B:=λm (hm:m< O)=>(qBc (t:=I) hm (ي I)).trans_eq hW;have g0:(Ѣk*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z0,hnm];exact B _ א have g1:((Ѣk+1)*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z1,hnm];exact B _ א have g2:((Ѣk+2)*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z2,hnm];exact B _ א have g3:((Ѣk+3)*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z3,hnm];exact B _ א have hx0:I.toUSize.toNat<=ѡx:=by rw[hnm];exact hxs.1 have sW':ѡW<=4294967295 :=א;unfold N_q at ok ⊢;rw[П ⟨hx0,sW',g0,g1,g2,g3⟩] at ok ⊢;have R:=qIa f (T:=T) (hT:=hT) hx0 sW g0 g1 g2 g3 I 0 0 0 0 0 (by rw[C_z,hnm,Т]);dsimp only at ok ⊢;rw[R] at ok ⊢;rw[C_z,z0,z1,z2,z3,<-ن] at ok ⊢;dsimp only at ok ⊢ by_cases c:((N_ff (λs=>x.data[s]! * dvN T hT W[k*I+s]!) (З I) 0 + N_ff (λs=>f x.data[s]! W[(k+1)*I+s]!) (З I) 0 + N_ff (λs=>x.data[s]! * dvN T hT W[(k+2)*I+s]!) (З I) 0 + N_ff (λs=>f x.data[s]! W[(k+3)*I+s]!) (З I) 0).isFinite)=true . rw[if_pos c] at ok ⊢;obtain ⟨f0,f1,f2,f3⟩:=qGt c;have o0:=(qBy oT f0).1;have o2:=(qBy oT f2).1;have o1:=qDh hf1 f1;have o3:=qDh hf1 f3;have IH:=ih (O-(k+4)) א (k+4) _ rfl ok;refine ⟨λj' a b' s hs=>?_,λG Rx Hk H=>?_⟩ . have m:=Р.2 hs;have:j'=k∨j'=k+1∨j'=k+2∨j'=k+3∨k+4<=j' :=א;rcases this with rfl | rfl | rfl | rfl | t;. exact o0 s m . exact o1 s m . exact o2 s m . exact o3 s m . exact IH.1 j' t b' s hs . have gv:=(qTg oT).1 have hv1:=(qTg oT).2 have RX:∀ t∈З I,x.data[t]! = dv X[0*I+t]!:=λt ht=>Rx t (Р.1 ht) have FE:∀ u:ћ,(З I).foldl (λacc s=>acc+x.data[s]! * dv W[u+s]!) 0 =(З I).foldl (λacc t=>acc+dv X[0*I+t]! * dv W[u+t]!) 0:=λu => List.foldl_ext _ _ _ λacc t ht=>by show acc+x.data[t]! * _=_;rw[RX t ht] have Ex:∀ s∈З I,∃ w,good w=true∧x.data[s]! = dv w:=λs hs =>⟨_,G.gx (א:0<1) s (Р.1 hs),RX s hs⟩ have V:∀ m (_:m< O) (sm:ѕ) (_:sm=(З I).foldl (λacc t=>acc+dv X[0*I+t]! * dv W[m*I+t]!) 0) (_:∀ t,t< I->W[m*I+t]! &&& В<=hv), fin8 I Y X W b 0 m sm (nfOf I) l hv b[m]! ѢI=C_sp I O Y X W b m:=λm hm sm hs g4=>by have e:=qCi I O Y X W b (i:=0) hm rw[ف,Т] at e rw[e,Hk] exact qDu G Y (א:0<1) hm gv hv1 g4 (З I) ػ (λt ht=>Р.1 ht) (λt ht hn=>absurd (Р.2 ht) hn) hs have r0:=qAg b Ѣk G.sb;have r1:=qAg b (Ѣk+1) G.sb;have r2:=qAg b (Ѣk+2) G.sb;have r3:=qAg b (Ѣk+3) G.sb;rw[e0] at r0;rw[e1] at r1;rw[e2] at r2;rw[e3] at r3;have Q0:=V k א _ ((qBy oT f0).2.trans (FE _)) λt ht=>(o0 t (Р.2 ht)).2;have Q1:=V (k+1) א _ ((qDt hf2 Ex f1).trans (FE _)) λt ht=>(o1 t (Р.2 ht)).2;have Q2:=V (k+2) א _ ((qBy oT f2).2.trans (FE _)) λt ht=>(o2 t (Р.2 ht)).2;have Q3:=V (k+3) א _ ((qDt hf2 Ex f3).trans (FE _)) λt ht=>(o3 t (Р.2 ht)).2;have H1:C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< k) out :=qA5 H λq _ (hq:q< k)=>show q< j by omega rw[<-r0] at Q0;rw[<-r1] at Q1;rw[<-r2] at Q2;rw[<-r3] at Q3;have H2:=qEq G.sO H1 e0 א Q0 Q1 Q2 Q3;exact IH.2 G Rx Hk H2 . rw[if_neg c] at ok;exact absurd ok ג . rw[colsN,Ш h] at ok ⊢;have hj:O<=j:=by simpa using ok exact ⟨λj' a b'=>by omega,λ_ _ _ H=>qA5 H λq _ (hq:q< O)=>show q< j by omega⟩ lemma qPp (oT:TOK 1 T hv) {Y:ј} {x:ђ} {l:ѕ} (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (hxs:I<=ѡx∧ѡx<2^32) (out:ё) (ok:(colsP T hT hv I O Y X W b x (nfOf I) l ѢI 0 out).ok=true):(∀ j',0<=j'->j'< O->∀ s,s< I->N_v hv W[j'*I+s]!) ∧ ∀ (G:C_G 1 I O X W b),(∀ s,s< I->x.data[s]! = dv X[0*I+s]!) → l=(З I).foldl (λ(a:ѕ) t=>a+(dv X[0*I+t]!).abs) 0 → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q<0) out → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< O) (colsP T hT hv I O Y X W b x (nfOf I) l ѢI 0 out).out:=by unfold colsP at ok ⊢ by_cases ar:armT=true . rw[if_pos ar] at ok ⊢ exact qHo oT (f:=dvNB T hT) (λv w h=>(qF1 oT h).2) (λv w y g e h=>by rw[(qF1 oT h).1,e,qFw y w g]) hW pi ni sW hxs _ 0 out rfl ok . rw[if_neg ar] at ok ⊢ exact qHo oT (f:=dvU T hT) (λv w h=>(qIj oT (qC1 _ _ h).2).2) (λv w y g e h=>by show v*dvN T hT w=_ rw[(qIj oT (qC1 _ _ h).2).1]) hW pi ni sW hxs _ 0 out rfl ok lemma qN1 {xs:ђ} {W:ѐ} {wo n:є} (hx:8*Ѡn<=ѡxs) (hsx:ѡxs<2^32) (hs:ѡW<2^32) (hw:Ѡwo+Ѡn<=ѡW) (ww:ћ->њ) (hww:∀ k,k< Ѡn->W[Ѡwo+k]! = ww k) (d:ћ) : ∀ (t i:є) (hi:Ѡi=8*Ѡt) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (r0:F8),Ѡt+d=Ѡn->∀ r,r<8 → C_fs (go8N T hT xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 r0) r = N_ff (λs=>xs.data[8*s+r]! * dvN T hT (ww s)) (List.range' Ѡt d) (C_fs ⟨0,s0,s1,s2,s3,s4,s5,s6,s7,0,0⟩ r):=by induction d with | zero => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 r0 he r hr rw[go8N,Ш (by rw[А];omega)] cases r0 interval_cases r<;>rfl | succ d ih => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 r0 he r hr have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א have hu:=usz t n ht have e:dvN T hT (W.uget (wo+t) (wi wo t n W hw hs htn))=dvN T hT (ww Ѡt):=by rw[qC3,qAf _ _ א,hww _ htn] have B:=λ(k:ћ) (hk:k<8)=>qBq (xf:=λr k=>xs.data[8*k+r]!) (λ_ _ _ _=>rfl) hi htn hsx hx k hk rw[go8N,П ht] dsimp only rw[e,B 0 א,B 1 א,B 2 א,B 3 א,B 4 א,B 5 א,B 6 א,B 7 א] rw[ih (t+1) _ _ _ _ _ _ _ _ _ _ _ א r hr,hu,List.range'_succ] unfold N_ff rw[ك] interval_cases r<;>rfl lemma qNz (oT:TOK 1 T hv) (f:ћ->ѕ) (ww:ћ->ѓ) (L:љ) (h:(N_ff (λs=>f s*dvN T hT (ww s)) L 0)ᶠ) : (∀ s∈L,N_v hv (ww s))∧N_ff (λs=>f s*dvN T hT (ww s)) L 0=L.foldl (λacc s=>acc+f s*dv (ww s)) 0:=by have F:=(qB4 _ _ _ h).2 have G:=λs hs=>qIj oT (qC1 _ _ (F s hs)).2 exact ⟨λs hs=>(G s hs).2,List.foldl_ext _ _ _ λacc s hs=>by show acc+f s*dvN T hT (ww s)=_ rw[(G s hs).1]⟩ lemma qS8 (R:F8) (H:(R.s0+R.s1+R.s2+R.s3+R.s4+R.s5+R.s6+R.s7)ᶠ):∀ r,r<8->(C_fs R r)ᶠ:=by obtain ⟨h1,h7⟩:=qC2 _ _ H obtain ⟨h2,h6⟩:=qC2 _ _ h1 obtain ⟨h3,h5⟩:=qC2 _ _ h2 obtain ⟨h4,h4'⟩:=qC2 _ _ h3 obtain ⟨h9,h3'⟩:=qC2 _ _ h4 obtain ⟨h10,h2'⟩:=qC2 _ _ h9 obtain ⟨h0,h1'⟩:=qC2 _ _ h10 intro r hr interval_cases r . exact h0 . exact h1' . exact h2' . exact h3' . exact h4' . exact h5 . exact h6 . exact h7 lemma qN8 (oT:TOK 1 T hv) {Y:ј} {d:H8} (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (hd:8*I<=d.xs.size∧d.xs.size<2^32) (k:ћ) : ∀ (j:ћ) (out:ё) (r0:F8),j+k=O → (cols8N T hT hv I O Y X W b d (nfOf I) ѢI j out r0).ok=true → (∀ j',j<=j'->j'< O->∀ s,s< I->N_v hv W[j'*I+s]!) ∧ ∀ (G:C_G 8 I O X W b),C_D8 X I I d → C_Inv (8*O) (C_sp I O Y X W b) (λq=>q%O< j) out → C_Inv (8*O) (C_sp I O Y X W b) (λq=>q%O< O) (cols8N T hT hv I O Y X W b d (nfOf I) ѢI j out r0).out:=by have hnm:I.toUSize.toNat=I:=qAd _ א have hO:O<2^32:=by have:=ؽ O pi omega have W1:∀ j,j<=O->(Ѣj*ѢI).toNat=j*I:=λj hj=>by have h1:j*I<=O*I:=Ь _ hj rw[qBa _ _ (by rw[qAd _ א,hnm];omega),qAd _ א,hnm] induction k with | zero => intro j out r0 he ok rw[cols8N,Ш א] have e:j=O :=א rw[e] exact ⟨λj' a b'=>by omega,λ_ _ H=>H⟩ | succ k ih => intro j out r0 he ok have hj:j< O :=א have hlt:j*I+I<=ѡW:=by rw[<-hW];exact qBc hj (ي _) have w1:=W1 j hj.le have hg:(Ѣj*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[w1,hnm];exact hlt have hx:8*I.toUSize.toNat<=d.xs.size:=by rw[hnm];exact hd.1 have hsx:d.xs.size<2^32:=hd.2 have hi0:(0:є).toNat=8*(0:є).toNat:=by rw[C_z] have key:=qN1 (T:=T) (hT:=hT) hx hsx sW hg (λk=>W[j*I+k]!) (λk _=>by rw[w1]) I 0 0 hi0 0 0 0 0 0 0 0 0 r0 (by rw[hnm,C_z,Т]) rw[C_z,<-ن] at key rw[cols8N,П hj] at ok ⊢ dsimp only at ok ⊢ rw[П ⟨hx,by omega,by omega,hg⟩] at ok ⊢ generalize go8N T hT d.xs W (Ѣj*ѢI) ѢI 0 0 hx hsx sW hg hi0 0 0 0 0 0 0 0 0 r0=R at key ok ⊢ by_cases c:(R.s0+R.s1+R.s2+R.s3+R.s4+R.s5+R.s6+R.s7).isFinite=true . rw[if_pos c] at ok ⊢ have F:=qS8 R c have Ks:∀ r,r<8->C_fs R r=N_ff (λs=>d.xs.data[8*s+r]! * dvN T hT W[j*I+s]!) (З I) 0:=λr hr=>by have k1:=key r hr have e1:C_fs ⟨0,0,0,0,0,0,0,0,0,0,0⟩ r=0:=by interval_cases r<;>rfl rw[e1] at k1 exact k1 have Zs:=λ(r:ћ) (hr:r<8)=>qNz oT (λs=>d.xs.data[8*s+r]!) (λs=>W[j*I+s]!) (З I) (by have f:=F r hr;rw[Ks r hr] at f;exact f) have IH:=ih (j+1) _ _ א ok refine ⟨λj' a b' s hs=>?_,λG D H=>?_⟩ . by_cases e:j'=j . rw[e];exact (Zs 0 א).1 s (Р.2 hs) . exact IH.1 j' א b' s hs . have hbs:=G.sb have e0:j.toUSize.toNat=j:=qAd _ א have h8:8*O<2^32:=G.sO have gv:=(qTg oT).1 have hv1:=(qTg oT).2 have O':∀ r,r<8->fin8 I Y X W b r j (C_fs R r) (nfOf I) (C_hf d r) hv b[j]! ѢI=C_sp I O Y X W b (r*O+j):=by intro r hr rw[qCi _ _ _ _ _ _ hj,D.2.2 r hr] obtain ⟨z1,z2⟩:=Zs r hr exact qDu G Y hr hj gv hv1 (λt ht=>(z1 t (Р.2 ht)).2) (З I) ػ (λt h=>Р.1 h) (λt ht hn=>absurd (Р.2 ht) hn) ((Ks r hr).trans (z2.trans (List.foldl_ext _ _ _ λacc t ht=>by show acc+d.xs.data[8*t+r]! * _=_ rw[D.2.1 t r (Р.1 ht) hr]))) have o0:fin8 I Y X W b 0 j R.s0 (nfOf I) d.h0 hv b[j]! ѢI=_ :=O' 0 א have o1:fin8 I Y X W b 1 j R.s1 (nfOf I) d.h1 hv b[j]! ѢI=_ :=O' 1 א have o2:fin8 I Y X W b 2 j R.s2 (nfOf I) d.h2 hv b[j]! ѢI=_ :=O' 2 א have o3:fin8 I Y X W b 3 j R.s3 (nfOf I) d.h3 hv b[j]! ѢI=_ :=O' 3 א have o4:fin8 I Y X W b 4 j R.s4 (nfOf I) d.h4 hv b[j]! ѢI=_ :=O' 4 א have o5:fin8 I Y X W b 5 j R.s5 (nfOf I) d.h5 hv b[j]! ѢI=_ :=O' 5 א have o6:fin8 I Y X W b 6 j R.s6 (nfOf I) d.h6 hv b[j]! ѢI=_ :=O' 6 א have o7:fin8 I Y X W b 7 j R.s7 (nfOf I) d.h7 hv b[j]! ѢI=_ :=O' 7 א rw[qAg _ _ hbs,e0,o0,o1,o2,o3,o4,o5,o6,o7] at ok ⊢ exact (ih (j+1) _ _ א ok).2 G D (qW9 h8 hj H) . rw[if_neg c] at ok;exact absurd ok ג lemma qZn (oT:TOK 0 T hv) (f:ћ->ѕ) (ww:ћ->ѓ) (L:љ) (h:(N_ff (λs=>f s*dvN T hT (ww s)) L 0)ᶠ) : (∀ s∈L,Z_ok hv (ww s))∧N_ff (λs=>f s*dvN T hT (ww s)) L 0=L.foldl (λacc s=>acc+f s*dv (ww s)) 0:=by have F:=(qB4 _ _ _ h).2 have G:=λs hs=>qZk oT (qC1 _ _ (F s hs)).2 exact ⟨λs hs=>(G s hs).2,List.foldl_ext _ _ _ λacc s hs=>by show acc+f s*dvN T hT (ww s)=_ rw[(G s hs).1]⟩ lemma qZg {hv w:ѓ} (t:Ѡhv<0x40000000) (h:Z_ok hv w) (g:w &&& В=0∨0x800000<=w &&& В):good w=true:=qBt _ g (by have c:=Ж.mp h;have b:(У:ѓ).toNat=У:=rfl;rw[у,b];omega) lemma qZm (ww:ћ->ѓ) (L:љ):∀ q,0x7FFFFF<=L.foldl (λq s=>q &&& ((ww s &&& В)-1)) q->0x7FFFFF<=q∧∀ s∈L,ww s &&& В=0∨0x800000<=ww s &&& В:=by induction L with | nil=>exact λq h=>⟨h,λs hs=>by simp at hs⟩ | cons p L ih=> intro q h obtain ⟨h1,h2⟩:=ih _ h obtain ⟨h8,h9⟩:=qFe q ((ww p &&& В)-1) refine ⟨UInt32.le_trans h1 h8,λs hs=>?_⟩ rcases List.mem_cons.mp hs with e | e . rw[e];exact qGi _ (UInt32.le_trans h1 h9) . exact h2 s e lemma qZ4q (A B C D:ћ->ѓ) (L:љ):∀ q,0x7FFFFF<=L.foldl (λq s=>qZw q (A s) (B s) (C s) (D s)) q->0x7FFFFF<=q∧∀ s∈L,(A s &&& В=0∨0x800000<=A s &&& В)∧(B s &&& В=0∨0x800000<=B s &&& В)∧(C s &&& В=0∨0x800000<=C s &&& В)∧(D s &&& В=0∨0x800000<=D s &&& В):=by induction L with | nil=>exact λq h=>⟨h,λs hs=>by simp at hs⟩ | cons p L ih=> intro q h obtain ⟨h1,h2⟩:=ih _ h obtain ⟨a1,a2⟩:=qFe (q &&& ((A p &&& В)-1) &&& ((B p &&& В)-1) &&& ((C p &&& В)-1)) ((D p &&& В)-1) obtain ⟨b1,b2⟩:=qFe (q &&& ((A p &&& В)-1) &&& ((B p &&& В)-1)) ((C p &&& В)-1) obtain ⟨c1,c2⟩:=qFe (q &&& ((A p &&& В)-1)) ((B p &&& В)-1) obtain ⟨d1,d2⟩:=qFe q ((A p &&& В)-1) have t1:=UInt32.le_trans h1 a1 have t2:=UInt32.le_trans t1 b1 have t3:=UInt32.le_trans t2 c1 refine ⟨UInt32.le_trans t3 d1,λs hs=>?_⟩ rcases List.mem_cons.mp hs with e | e . rw[e];exact ⟨qGi _ (UInt32.le_trans t3 d2),qGi _ (UInt32.le_trans t2 c2),qGi _ (UInt32.le_trans t1 b2),qGi _ (UInt32.le_trans h1 a2)⟩ . exact h2 s e lemma qZ1 {xs:ђ} {W:ѐ} {wo n:є} (hx:8*Ѡn<=ѡxs) (hsx:ѡxs<2^32) (hs:ѡW<2^32) (hw:Ѡwo+Ѡn<=ѡW) (ww:ћ->њ) (hww:∀ k,k< Ѡn->W[Ѡwo+k]! = ww k) (d:ћ) : ∀ (t i:є) (hi:Ѡi=8*Ѡt) (s0 s1 s2 s3 s4 s5 s6 s7:ѕ) (q:ѓ) (r0:F8),Ѡt+d=Ѡn → (∀ r,r<8->C_fs (go8Z T hT xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 q r0) r = N_ff (λs=>xs.data[8*s+r]! * dvN T hT (ww s)) (List.range' Ѡt d) (C_fs ⟨0,s0,s1,s2,s3,s4,s5,s6,s7,0,0⟩ r)) ∧ (go8Z T hT xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 q r0).q=(List.range' Ѡt d).foldl (λq s=>q &&& ((ww s &&& В)-1)) q:=by induction d with | zero => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 q r0 he rw[go8Z,Ш (by rw[А];omega)] cases r0 exact ⟨λr hr=>by interval_cases r<;>rfl,rfl⟩ | succ d ih => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 q r0 he have ht:t< n:=by rw[А];omega have htn:Ѡt< Ѡn :=א have hu:=usz t n ht have e:W.uget (wo+t) (wi wo t n W hw hs htn)=ww Ѡt:=by rw[qC3,qAf _ _ א,hww _ htn] have B:=λ(k:ћ) (hk:k<8)=>qBq (xf:=λr k=>xs.data[8*k+r]!) (λ_ _ _ _=>rfl) hi htn hsx hx k hk rw[go8Z,П ht] dsimp only rw[e,B 0 א,B 1 א,B 2 א,B 3 א,B 4 א,B 5 א,B 6 א,B 7 א] obtain ⟨J1,J2⟩:=ih (t+1) (i+USize.ofNat 8) (by rw[hu,qAf _ _ (by rw[uk 8 א];omega),uk 8 א];omega) _ _ _ _ _ _ _ _ _ r0 (by rw[hu];omega) have e5:List.range' (t+1).toNat d=List.range' (Ѡt+1) d:=by rw[hu] rw[e5] at J1 J2 refine ⟨λr hr=>(J1 r hr).trans ?_,J2.trans ?_⟩ . rw[List.range'_succ] unfold N_ff rw[ك] interval_cases r<;>rfl . rw[List.range'_succ];rfl set_option maxHeartbeats 1000000 in set_option maxRecDepth 8000 in lemma qZ8 (oT:TOK 0 T hv) {Y:ј} {d:H8} (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (hd:8*I<=d.xs.size∧d.xs.size<2^32) (k:ћ) : ∀ (j:ћ) (out:ё) (q:ѓ) (r0:F8),j+k=O → (cols8Z T hT hv I O Y X W b d (nfOf I) ѢI j out q r0).ok=true → 0x7FFFFF<=q∧(∀ j',j<=j'->j'< O->∀ s,s< I->good W[j'*I+s]! = true∧W[j'*I+s]! &&& В<=hv) ∧ ∀ (G:C_G 8 I O X W b),C_D8 X I I d → C_Inv (8*O) (C_sp I O Y X W b) (λq=>q%O< j) out → C_Inv (8*O) (C_sp I O Y X W b) (λq=>q%O< O) (cols8Z T hT hv I O Y X W b d (nfOf I) ѢI j out q r0).out:=by have hnm:I.toUSize.toNat=I:=qAd _ א have hO:O<2^32:=by have:=ؽ O pi omega have W1:∀ j,j<=O->(Ѣj*ѢI).toNat=j*I:=λj hj=>by have h1:j*I<=O*I:=Ь _ hj rw[qBa _ _ (by rw[qAd _ א,hnm];omega),qAd _ א,hnm] induction k with | zero => intro j out q r0 he ok rw[cols8Z,Ш א] at ok ⊢ have e:j=O :=א rw[e] exact ⟨of_decide_eq_true ok,λj' a b'=>by omega,λ_ _ H=>H⟩ | succ k ih => intro j out q r0 he ok have hj:j< O :=א have hlt:j*I+I<=ѡW:=by rw[<-hW];exact qBc hj (ي _) have w1:=W1 j hj.le have hg:(Ѣj*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[w1,hnm];exact hlt have hx:8*I.toUSize.toNat<=d.xs.size:=by rw[hnm];exact hd.1 have hsx:d.xs.size<2^32:=hd.2 have hi0:(0:є).toNat=8*(0:є).toNat:=by rw[C_z] obtain ⟨key,kq⟩:=qZ1 (T:=T) (hT:=hT) hx hsx sW hg (λk=>W[j*I+k]!) (λk _=>by rw[w1]) I 0 0 hi0 0 0 0 0 0 0 0 0 q r0 (by rw[hnm,C_z,Т]) rw[C_z,<-ن] at key kq rw[cols8Z,П hj] at ok ⊢ dsimp only at ok ⊢ rw[П ⟨hx,by omega,by omega,hg⟩] at ok ⊢ generalize go8Z T hT d.xs W (Ѣj*ѢI) ѢI 0 0 hx hsx sW hg hi0 0 0 0 0 0 0 0 0 q r0=R at key kq ok ⊢ by_cases c:(R.s0+R.s1+R.s2+R.s3+R.s4+R.s5+R.s6+R.s7).isFinite=true . rw[if_pos c] at ok ⊢ have F:=qS8 R c have Ks:∀ r,r<8->C_fs R r=N_ff (λs=>d.xs.data[8*s+r]! * dvN T hT W[j*I+s]!) (З I) 0:=λr hr=>by have k1:=key r hr have e1:C_fs ⟨0,0,0,0,0,0,0,0,0,0,0⟩ r=0:=by interval_cases r<;>rfl rw[e1] at k1 exact k1 have Zs:=λ(r:ћ) (hr:r<8)=>qZn oT (λs=>d.xs.data[8*s+r]!) (λs=>W[j*I+s]!) (З I) (by have f:=F r hr;rw[Ks r hr] at f;exact f) obtain ⟨q1,IH,-⟩:=ih (j+1) _ _ _ א ok rw[kq] at q1 obtain ⟨q2,q3⟩:=qZm (λs=>W[j*I+s]!) (З I) q q1 refine ⟨q2,λj' a b' s hs=>?_,λG D H=>?_⟩ . by_cases e:j'=j . rw[e];exact ⟨qZg (qTb oT) ((Zs 0 א).1 s (Р.2 hs)) (q3 s (Р.2 hs)),(Zs 0 א).1 s (Р.2 hs)⟩ . exact IH j' א b' s hs . have hbs:=G.sb have e0:j.toUSize.toNat=j:=qAd _ א have h8:8*O<2^32:=G.sO have gv:=(qTg oT).1 have hv1:=(qTg oT).2 have O':∀ r,r<8->fin8 I Y X W b r j (C_fs R r) (nfOf I) (C_hf d r) hv b[j]! ѢI=C_sp I O Y X W b (r*O+j):=by intro r hr rw[qCi _ _ _ _ _ _ hj,D.2.2 r hr] obtain ⟨z1,z2⟩:=Zs r hr exact qDu G Y hr hj gv hv1 (λt ht=>z1 t (Р.2 ht)) (З I) ػ (λt h=>Р.1 h) (λt ht hn=>absurd (Р.2 ht) hn) ((Ks r hr).trans (z2.trans (List.foldl_ext _ _ _ λacc t ht=>by show acc+d.xs.data[8*t+r]! * _=_ rw[D.2.1 t r (Р.1 ht) hr]))) have o0:fin8 I Y X W b 0 j R.s0 (nfOf I) d.h0 hv b[j]! ѢI=_ :=O' 0 א have o1:fin8 I Y X W b 1 j R.s1 (nfOf I) d.h1 hv b[j]! ѢI=_ :=O' 1 א have o2:fin8 I Y X W b 2 j R.s2 (nfOf I) d.h2 hv b[j]! ѢI=_ :=O' 2 א have o3:fin8 I Y X W b 3 j R.s3 (nfOf I) d.h3 hv b[j]! ѢI=_ :=O' 3 א have o4:fin8 I Y X W b 4 j R.s4 (nfOf I) d.h4 hv b[j]! ѢI=_ :=O' 4 א have o5:fin8 I Y X W b 5 j R.s5 (nfOf I) d.h5 hv b[j]! ѢI=_ :=O' 5 א have o6:fin8 I Y X W b 6 j R.s6 (nfOf I) d.h6 hv b[j]! ѢI=_ :=O' 6 א have o7:fin8 I Y X W b 7 j R.s7 (nfOf I) d.h7 hv b[j]! ѢI=_ :=O' 7 א rw[qAg _ _ hbs,e0,o0,o1,o2,o3,o4,o5,o6,o7] at ok ⊢ exact (ih (j+1) _ _ _ א ok).2.2 G D (qW9 h8 hj H) . rw[if_neg c] at ok;exact absurd ok ג def C_q (f:ѕ->ѓ->ѕ) (T:ђ) (hT:ѡT=512) (hv:ѓ) (I O:ћ) (Y:ј) (X W b ix iz:ѐ) (x:ђ) (nf l:ѕ) (m n z:є) (k:ћ) (out:ѐ) (p q:ѓ):CN := if hg:Ѡn<=ѡx∧Ѡn<=ѡix∧Ѡz<=ѡiz∧ѡW<=4294967295∧(Ѣk*m).toNat+Ѡm<=ѡW ∧ ((Ѣk+1)*m).toNat+Ѡm<=ѡW∧((Ѣk+2)*m).toNat+Ѡm<=ѡW ∧ ((Ѣk+3)*m).toNat+Ѡm<=ѡW then let r:=go4C f T hT x ix W (Ѣk*m) ((Ѣk+1)*m) ((Ѣk+2)*m) ((Ѣk+3)*m) m n 0 hg.1 hg.2.1 (qA9 hg.2.2.2.1) hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2.1 hg.2.2.2.2.2.2.2 0 0 0 0 q if (r.a+r.b+r.c+r.d).isFinite then let u:=go4U iz W (Ѣk*m) ((Ѣk+1)*m) ((Ѣk+2)*m) ((Ѣk+3)*m) m z 0 hg.2.2.1 (qA9 hg.2.2.2.1) hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2.1 hg.2.2.2.2.2.2.2 p r.q colsC f T hT hv I O Y X W b ix iz x nf l m n z (k+4) (wrW (wrW (wrW (wrW out Ѣk (fin8 I Y X W b 0 k r.a nf l hv (rdW b Ѣk) m)) (Ѣk+1) (fin8 I Y X W b 0 (k+1) r.b nf l hv (rdW b (Ѣk+1)) m)) (Ѣk+2) (fin8 I Y X W b 0 (k+2) r.c nf l hv (rdW b (Ѣk+2)) m)) (Ѣk+3) (fin8 I Y X W b 0 (k+3) r.d nf l hv (rdW b (Ѣk+3)) m)) u.p u.q else ⟨out,false⟩ else ⟨out,false⟩ lemma qZy {f:ѕ->ѓ->ѕ} {Y:ј} {ix iz:ѐ} {x:ђ} {nf l:ѕ} {m n z:є} {j:ћ} {out:ѐ} {p q:ѓ} (h:j< O∧4<=O):colsC f T hT hv I O Y X W b ix iz x nf l m n z j out p q =C_q f T hT hv I O Y X W b ix iz x nf l m n z (if j+4<=O then j else O-4) out p q:=by rw[colsC,П h] rfl lemma qZ9 {w P:ѓ} (g:w &&& В=0∨0x800000<=w &&& В) (h:w &&& В<=P) (t:P<У):good w=true:=qBt _ g (UInt32.lt_of_le_of_lt h t) lemma qZ6 {f:ѕ->ѓ->ѕ} (hf2:∀ v w (y:ѓ),good y=true->v=dv y->(f v w)ᶠ->f v w=v*dv w) (xf:ћ->ѕ) (ww:ћ->ѓ) (L:љ) (hx:∀ s∈L,∃ w,good w=true∧xf s=dv w) (h:(N_ff (λs=>f (xf s) (ww s)) L 0)ᶠ) : N_ff (λs=>f (xf s) (ww s)) L 0=L.foldl (λacc s=>acc+xf s*dv (ww s)) 0:=by refine List.foldl_ext _ _ _ λacc s hs=>?_ obtain ⟨w,g,e⟩:=hx s hs show acc+f (xf s) (ww s)=_ rw[hf2 _ _ w g e ((qB4 _ _ _ h).2 s hs)] set_option maxRecDepth 8000 in set_option maxHeartbeats 1000000 in lemma qZ7 (oT:TOK 0 T hv) {f:ѕ->ѓ->ѕ} (hf1:∀ v w,(f v w)ᶠ->Z_ok hv w) (hf2:∀ v w (y:ѓ),good y=true->v=dv y->(f v w)ᶠ->f v w=v*dv w) {Y:ј} {ix iz:ѐ} {x:ђ} {c z:є} {L Z:љ} {l:ѕ} (Rw:C_Row I X 0 ix x c L l) (Zl:Ѡz=ѣZ) (Zs:ѣZ<=ѡiz) (Zr:∀ k r:ћ,Z[k]?=some r->r< I∧(iz[k]! : ѓ).toNat=r) (Cv:∀ t,t< I->t∉L->t∈Z) (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (d:ћ) : ∀ (j:ћ) (out:ё) (p q:ѓ),O-j=d → (colsC f T hT hv I O Y X W b ix iz x (nfOf I) l ѢI c z j out p q).ok=true → (0x7FFFFF<=q∧p &&& В<У) ∧ (∀ j',j<=j'->j'< O->∀ s,s< I->good W[j'*I+s]! = true) ∧ ∀ (G:C_G 1 I O X W b),C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< j) out → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< O) (colsC f T hT hv I O Y X W b ix iz x (nfOf I) l ѢI c z j out p q).out:=by have hnm:I.toUSize.toNat=I:=qAd _ א have hO:O<2^32:=by have:=ؽ O pi omega have W1:∀ {u:є} {k:ћ},Ѡu=k->k< O->(u*ѢI).toNat=k*I:=λ{u k} hu hk=>by have h1:k*I<=O*I:=Ь _ hk.le rw[qBa _ _ (by rw[hu,hnm];omega),hu,hnm] have hle:=Rw.cle have h1c:Ѡc<=ѡx:=by rw[Rw.len];exact hle.trans Rw.sx.1 have h2c:Ѡc<=ѡix:=by rw[Rw.six,Rw.len];exact hle have hzc:Ѡz<=ѡiz:=by rw[Zl];exact Zs have LL:∀ k r:ћ,L[k]?=some r->r< I.toUSize.toNat∧(ix[(0:є).toNat+k]! : ѓ).toNat=r∧x.data[(0:є).toNat+k]! = dv X[0*I+r]!:=λk r hr=>by obtain ⟨u1,u2⟩:=Rw.rd k r hr rw[C_z,Т,hnm];exact ⟨Rw.lt r (List.mem_of_getElem? hr),u1,u2⟩ have LZ:∀ k r:ћ,Z[k]?=some r->r< I.toUSize.toNat∧(iz[(0:є).toNat+k]! : ѓ).toNat=r:=λk r hr=>by obtain ⟨u1,u2⟩:=Zr k r hr rw[C_z,Т,hnm];exact ⟨u1,u2⟩ induction d using Nat.strong_induction_on with | _ d ih => intro j out p q he ok by_cases h:j< O∧4<=O . rw[qZy h] at ok ⊢;have hk:(if j+4<=O then j else O-4)<=j∧(if j+4<=O then j else O-4)+4<=O ∧ j<(if j+4<=O then j else O-4)+4:=by split<;>omega generalize (if j+4<=O then j else O-4)=k at hk ok ⊢;obtain ⟨k1,k2,k3⟩:=hk;have e0:k.toUSize.toNat=k:=qAd _ א;obtain ⟨e1,e2,e3,-⟩:=qFk e0 א;have z0:=W1 e0 (א:k< O);have z1:=W1 e1 (א:k+1< O);have z2:=W1 e2 (א:k+2< O);have z3:=W1 e3 (א:k+3< O);have B:=λm (hm:m< O)=>(qBc (t:=I) hm (ي I)).trans_eq hW;have g0:(Ѣk*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z0,hnm];exact B _ א have g1:((Ѣk+1)*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z1,hnm];exact B _ א have g2:((Ѣk+2)*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z2,hnm];exact B _ א have g3:((Ѣk+3)*ѢI).toNat+I.toUSize.toNat<=ѡW:=by rw[z3,hnm];exact B _ א have sW':ѡW<=4294967295 :=א;unfold C_q at ok ⊢;rw[П ⟨h1c,h2c,hzc,sW',g0,g1,g2,g3⟩] at ok ⊢ have R:=qZC f (T:=T) (hT:=hT) h1c h2c sW g0 g1 g2 g3 (λs=>dv X[0*I+s]!) (λs=>W[k*I+s]!) (λs=>W[(k+1)*I+s]!) (λs=>W[(k+2)*I+s]!) (λs=>W[(k+3)*I+s]!) (λs _=>by rw[z0]) (λs _=>by rw[z1]) (λs _=>by rw[z2]) (λs _=>by rw[z3]) L 0 0 0 0 0 q (by rw[C_z,Т,Rw.len]) LL have U:=qZU hzc sW g0 g1 g2 g3 (λs=>W[k*I+s]!) (λs=>W[(k+1)*I+s]!) (λs=>W[(k+2)*I+s]!) (λs=>W[(k+3)*I+s]!) (λs _=>by rw[z0]) (λs _=>by rw[z1]) (λs _=>by rw[z2]) (λs _=>by rw[z3]) Z 0 p (L.foldl (λq p=>qZw q W[k*I+p]! W[(k+1)*I+p]! W[(k+2)*I+p]! W[(k+3)*I+p]!) q) (by rw[C_z,Т,Zl]) LZ dsimp only at ok ⊢;rw[R] at ok ⊢;dsimp only at ok ⊢ by_cases c0:((N_ff (λp=>dv X[0*I+p]! * dvN T hT W[k*I+p]!) L 0 + N_ff (λp=>f (dv X[0*I+p]!) W[(k+1)*I+p]!) L 0 + N_ff (λp=>dv X[0*I+p]! * dvN T hT W[(k+2)*I+p]!) L 0 + N_ff (λp=>f (dv X[0*I+p]!) W[(k+3)*I+p]!) L 0).isFinite)=true . rw[if_pos c0] at ok ⊢;rw[U] at ok ⊢;dsimp only at ok ⊢;obtain ⟨f0,f1,f2,f3⟩:=qGt c0;have Y0:=qZn oT (λs=>dv X[0*I+s]!) (λs=>W[k*I+s]!) L f0;have Y2:=qZn oT (λs=>dv X[0*I+s]!) (λs=>W[(k+2)*I+s]!) L f2;have o0:=Y0.1;have o2:=Y2.1;have o1:∀ s∈L,Z_ok hv W[(k+1)*I+s]!:=λs hs=>hf1 _ _ ((qB4 _ _ _ f1).2 s hs);have o3:∀ s∈L,Z_ok hv W[(k+3)*I+s]!:=λs hs=>hf1 _ _ ((qB4 _ _ _ f3).2 s hs) obtain ⟨⟨Q1,Q2⟩,IH1,IH2⟩:=ih (O-(k+4)) א (k+4) _ _ _ rfl ok obtain ⟨z5,z6⟩:=qZ4q (λs=>W[k*I+s]!) (λs=>W[(k+1)*I+s]!) (λs=>W[(k+2)*I+s]!) (λs=>W[(k+3)*I+s]!) Z _ Q1 obtain ⟨z7,z8⟩:=qZ4q (λs=>W[k*I+s]!) (λs=>W[(k+1)*I+s]!) (λs=>W[(k+2)*I+s]!) (λs=>W[(k+3)*I+s]!) L q z5 obtain ⟨p1,p2⟩:=qHt (λs=>W[k*I+s]!) (λs=>W[(k+1)*I+s]!) (λs=>W[(k+2)*I+s]!) (λs=>W[(k+3)*I+s]!) Z p refine ⟨⟨z7,UInt32.lt_of_le_of_lt p1 Q2⟩,λj' a b' s hs=>?_,λG H=>?_⟩ . have:j'=k∨j'=k+1∨j'=k+2∨j'=k+3∨k+4<=j' :=א;by_cases sL:s∈L . obtain ⟨c0',c1,c2,c3⟩:=z8 s sL;rcases this with rfl | rfl | rfl | rfl | t;. exact qZg (qTb oT) (o0 s sL) c0' . exact qZg (qTb oT) (o1 s sL) c1 . exact qZg (qTb oT) (o2 s sL) c2 . exact qZg (qTb oT) (o3 s sL) c3 . exact IH1 j' t b' s hs . have sZ:=Cv s hs sL;obtain ⟨c0',c1,c2,c3⟩:=z6 s sZ;obtain ⟨p3,p4,p5,p6⟩:=p2 s sZ;rcases this with rfl | rfl | rfl | rfl | t;. exact qZ9 c0' p3 Q2 . exact qZ9 c1 p4 Q2 . exact qZ9 c2 p5 Q2 . exact qZ9 c3 p6 Q2 . exact IH1 j' t b' s hs . have gv:=(qTg oT).1 have hv1:=(qTg oT).2 have Ex:∀ s∈L,∃ w,good w=true∧dv X[0*I+s]! = dv w:=λs hs =>⟨_,G.gx (א:0<1) s (Rw.lt s hs),rfl⟩ have V:∀ m (_:m< O) (sm:ѕ) (_:sm=L.foldl (λacc t=>acc+dv X[0*I+t]! * dv W[m*I+t]!) 0) (_:∀ t∈L,W[m*I+t]! &&& В<=hv), fin8 I Y X W b 0 m sm (nfOf I) l hv b[m]! ѢI=C_sp I O Y X W b m:=λm hm sm hs g4=>by have e:=qCi I O Y X W b (i:=0) hm rw[ف,Т] at e rw[e,Rw.ok] exact qDL G Y (א:0<1) hm gv hv1 L Rw.nd Rw.lt g4 Rw.hz hs have r0:=qAg b Ѣk G.sb;have r1:=qAg b (Ѣk+1) G.sb;have r2:=qAg b (Ѣk+2) G.sb;have r3:=qAg b (Ѣk+3) G.sb;rw[e0] at r0;rw[e1] at r1;rw[e2] at r2;rw[e3] at r3;have Q0':=V k א _ Y0.2 o0;have Q1':=V (k+1) א _ (qZ6 hf2 (λs=>dv X[0*I+s]!) (λs=>W[(k+1)*I+s]!) L Ex f1) o1;have Q2':=V (k+2) א _ Y2.2 o2;have Q3':=V (k+3) א _ (qZ6 hf2 (λs=>dv X[0*I+s]!) (λs=>W[(k+3)*I+s]!) L Ex f3) o3;have H1:C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< k) out :=qA5 H λq _ (hq:q< k)=>show q< j by omega rw[<-r0] at Q0';rw[<-r1] at Q1';rw[<-r2] at Q2';rw[<-r3] at Q3';have H2:=qEq G.sO H1 e0 א Q0' Q1' Q2' Q3';exact IH2 G H2 . rw[if_neg c0] at ok;exact absurd ok ג . rw[colsC,Ш h] at ok ⊢;have o9:=ok;simp only[Bool.and_eq_true,decide_eq_true_eq] at o9;obtain ⟨⟨hj,hq⟩,hp⟩:=o9 exact ⟨⟨hq,hp⟩,λj' a b'=>by omega,λ_ H=>qA5 H λq _ (hq:q< O)=>show q< j by omega⟩ lemma qPg (oT:TOK 0 T hv) {Y:ј} {ix iz:ѐ} {x:ђ} {c z:є} {L Z:љ} {l:ѕ} (Rw:C_Row I X 0 ix x c L l) (Zl:Ѡz=ѣZ) (Zs:ѣZ<=ѡiz) (Zr:∀ k r:ћ,Z[k]?=some r->r< I∧(iz[k]! : ѓ).toNat=r) (Cv:∀ t,t< I->t∉L->t∈Z) (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (out:ё) (p q:ѓ) (ok:(colsG T hT hv I O Y X W b ix iz x (nfOf I) l ѢI c z 0 out p q).ok=true):(∀ j',0<=j'->j'< O->∀ s,s< I->good W[j'*I+s]! = true) ∧ ∀ (G:C_G 1 I O X W b),C_Inv (1*O) (C_sp I O Y X W b) (λq=>q<0) out → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< O) (colsG T hT hv I O Y X W b ix iz x (nfOf I) l ѢI c z 0 out p q).out:=by unfold colsG at ok ⊢ by_cases ar:armT=true . rw[if_pos ar] at ok ⊢ exact (qZ7 oT (f:=dvNB T hT) (λv w h=>(qZB9 oT h).2) (λv w y g e h=>by rw[(qZB9 oT h).1,e,qFw y w g]) Rw Zl Zs Zr Cv hW pi ni sW _ 0 out p q rfl ok).2 . rw[if_neg ar] at ok ⊢ exact (qZ7 oT (f:=dvU T hT) (λv w h=>(qZk oT (qC1 _ _ h).2).2) (λv w y g e h=>by show v*dvN T hT w=_ rw[(qZk oT (qC1 _ _ h).2).1]) Rw Zl Zs Zr Cv hW pi ni sW _ 0 out p q rfl ok).2 lemma qPa (oT:TOK 0 T hv) {Y:ј} {ix:ѐ} {x:ђ} {c:є} {l:ѕ} (Rw:C_Row I X 0 ix x c (C_keep (λp=>X[0*I+p]!) I) l) (sX:ѡX<2^32) (sX1:0*I+I<=ѡX) (hW:O*I=ѡW) (pi:0< I) (ni:I<2^20) (sW:ѡW<2^32) (ok:(colsA T hT hv I O Y X W b ix x (nfOf I) l ѢI c).ok=true):(∀ j',0<=j'->j'< O->∀ s,s< I->good W[j'*I+s]! = true) ∧ ∀ (G:C_G 1 I O X W b),C_Inv (1*O) (C_sp I O Y X W b) (λq=>q<0) (К (1*O) 0) → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< O) (colsA T hT hv I O Y X W b ix x (nfOf I) l ѢI c).out:=by unfold colsA at ok ⊢ by_cases ar:armT=true . rw[if_pos ar] at ok ⊢ dsimp only at ok ⊢ have Dz:=qZE (X:=X) (a:=0*I) (n:=I) sX sX1 (by omega) generalize decZ X (0*I).toUSize ѢI 0 (К I 0) 0=s at Dz ok ⊢ obtain ⟨z1,z2,z3⟩:=Dz have Zs:(C_drop (λp=>X[0*I+p]!) I).length<=s.iz.size:=by rw[z2];exact qZj _ _ have Zr:∀ k r:ћ,(C_drop (λp=>X[0*I+p]!) I)[k]?=some r->r< I∧(s.iz[k]! : ѓ).toNat=r:=λk r hr=>⟨by have m:=List.mem_of_getElem? hr;exact Р.1 (List.mem_of_mem_filter m),z3 k r hr⟩ have Cv:∀ t,t< I->t∉C_keep (λp=>X[0*I+p]!) I->t∈C_drop (λp=>X[0*I+p]!) I:=λt ht hn=>List.mem_filter.2 ⟨Р.2 ht,by simpa using Rw.hz t ht hn⟩ exact qPg oT Rw z1 Zs Zr Cv hW pi ni sW _ _ _ ok . rw[if_neg ar] at ok exact absurd ok ג end end lemma qEr:linF=linearSpec:=by funext rows inW outW relu X W b;unfold linF;dsimp only;have Hs:=qGd rows W;generalize scanW rows W=hw at Hs ⊢;split . rename_i h;obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13⟩:=h;replace h7:=qA9 h7;replace h8:=qA9 h8;replace h9:=qA9 h9;replace h10:=qA9 h10 have G0:=λ(A:∀ q,q< ѡW->good W[q]! = true) => (⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,(qA8 1 X h7 h11).2.2.1,A,(qA8 1 b h9 h13).2.2.1⟩:C_G rows inW outW X W b) have e:linearSpec rows inW outW relu X W b=Array.ofFn (n:=rows*outW) λq =>C_sp inW outW relu X W b q.val:=rfl;have hnf:(inW.toUInt64+8).toFloat*О 0x3CA0000000000000=nfOf inW:=rfl;rw[e,hnf];split . rename_i hr;have hr8:rows=8:=by simpa using hr subst hr8;have H0:C_Inv (8*outW) (C_sp inW outW relu X W b) (λq=>q%outW<0) (К (8*outW) 0) :=⟨by simp,λq _ h=>absurd h (ъ _)⟩;have D0:C_D8 X inW.toUSize.toNat (0:є).toNat ⟨zFor (8*inW),0,0,0,0,0,0,0,0⟩:=by have e0:(0:є).toNat=0:=rfl;have nm0:inW.toUSize.toNat=inW:=qAd _ א;rw[e0,nm0];refine ⟨qZf _ (by omega),λk r hk=>absurd hk (ъ _),λr hr=>?_⟩;interval_cases r<;>rfl have nm:inW.toUSize.toNat=inW:=qAd _ א;have D:=qGq (n:=ѢinW) h7 (by rw[nm];exact h1.le) (qA8 1 X h7 h11).2.2.1 inW 0 _ 0 0 0 0 0 0 0 0 (by rw[nm,C_z,Т]) D0;rw[nm] at D;split . rename_i hk;obtain ⟨A1,A2⟩:=qN8 (qTN inW) h2 h4 h6 h8 D.1 outW 0 _ _ (Т _) hk have A:∀ q,q< ѡW->good W[q]! = true:=λq hq=>by have e:=qEx (A1 (q/inW) (Nat.zero_le _) (ш (by rw[е,h2];exact hq)) (q%inW) (ر _ h4)).1 rwa [Nat.div_add_mod' q inW] at e exact qBv (A2 (G0 A) D H0) (λq _=>ر _ h5) split . rename_i hc;obtain ⟨-,A1,A2⟩:=qZ8 (qTZ inW) h2 h4 h6 h8 D.1 outW 0 _ _ _ (Т _) hc have A:∀ q,q< ѡW->good W[q]! = true:=λq hq=>by have e:=(A1 (q/inW) (Nat.zero_le _) (ш (by rw[е,h2];exact hq)) (q%inW) (ر _ h4)).1 rwa [Nat.div_add_mod' q inW] at e exact qBv (A2 (G0 A) D H0) (λq _=>ر _ h5) . rw[qAi];rfl . rename_i hr;split . rename_i hr1;have e1:rows=1:=by simpa using hr1 subst e1;have GX:=(qA8 1 X h7 h11).2.2.1;have sX1:0*inW+inW<=ѡX :=א;have nm:inW.toUSize.toNat=inW:=qAd _ א;have P0:=qZf inW (by omega) obtain ⟨d1,d2,d3,d4,d5⟩:=qEy (a:=0*inW) (n:=inW) h7 sX1 (λt ht=>GX _ א) (ix:=К inW 0) ג P0;obtain ⟨k1,k2,k3⟩:=qDs (λp=>X[0*inW+p]!) inW;have Rw:=C_Row.mk d1 (qBj _ _) d2 d3 k1 k2 k3 d4 d5;generalize decR X (0*inW).toUSize ѢinW 0 (К inW 0) (zFor inW) 0 0=r at Rw ⊢;have H0:C_Inv (1*outW) (C_sp inW outW relu X W b) (λq=>q<0) (К (1*outW) 0) :=⟨by simp,λq _ h=>absurd h (ъ _)⟩ rw[(wdGet W).2];dsimp only;split . rename_i hv;have Hv:=qA8 1 W h8 hv;have G:=G0 Hv.2.2.1;have hb0:(0*outW).toUSize.toNat=0*outW:=qAd _ א;refine qBv (qD0 G relu (א:0<1) Hv Rw hb0 ?_ outW 0 _ (Т _) (by rw[ف,ю];exact H0)) (λq hq=>by omega);intro _ q hq;unfold wdOf;rw[<-Array.foldl_toList,qIy];simp[hq,FloatArray.emptyWithCapacity] . rw[qAi];rfl . rename_i hr1;have Hv:=Hs (by simpa using hr) (by simpa using hr1) h8 h12;have G:=G0 Hv.2.2.1;have H0:C_Inv (rows*outW) (C_sp inW outW relu X W b) (λq=>q<0*outW) (К (rows*outW) 0) :=⟨by simp,λq _ h=>by rw[ف] at h;exact absurd h (ъ _)⟩;have P0:(pushZ ѢinW 0 (Й inW)).size=inW:=by rw[qDy ѢinW inW 0 _ (by rw[G.nm,C_z,Т])];exact Т _ refine qBv (qGf G relu Hv ?_ rows 0 _ _ _ א ג ⟨P0.ge,by rw[P0];omega⟩ H0) (λq hq=>hq);split;. intro _ q hq;rw[<-Array.foldl_toList,qIy];simp[hq,FloatArray.emptyWithCapacity] . intro h q hq;have e:0=ѡW:=h;omega . rw[qAi];rfl end lemma qDz:linF=linearSpec:=qEr @[inline] def sm (w:ѓ):ј:=good w && (w &&& В)<=0x70FFFFFF section open Float.Model Float.Model.UnpackedFloat set_option maxRecDepth 8192 in end section open Float.Model Float.Model.UnpackedFloat end section lemma qGm (S:ј) (ex fr m:ћ) (q:Int) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (hm:m≠0) (hW1:(m:ℝ)*2^q<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<(m:ℝ)*2^q) (hW3:(2^53-2^28)*2^((ex:Int)-1076)<(m:ℝ)*2^q) : toWord (Щ Fmt ⟨S,m,q⟩) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-й,<-Э Ч] congr 1;omega rw[qE4 S m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hE];push_cast;linarith) (λ_=>by rw[hE];push_cast;linarith [hW2 א]) (λ_=>by rw[hE];linarith),show ((ex:Int)-1046+150).toNat=ex-896 by omega,show (2^23+fr)%2^23=fr by omega] lemma qFt (a b:ℝ) (ha:0<=a) (h:a< b) : ∃ (m:ћ) (q:Int),m≠0∧a<(m:ℝ)*2^q∧(m:ℝ)*2^q<=b:=by obtain ⟨k,hk⟩:=exists_pow_lt_of_lt_one (sub_pos.2 h) one_half_lt_one;have h2:(0:ℝ)<2^k:=by positivity rw[one_div_pow,div_lt_iff₀ h2] at hk;refine ⟨⌊b*2^k⌋₊,-k,?_⟩;rw[zpow_neg,й,<-div_eq_mul_inv,lt_div_iff₀ h2,div_le_iff₀ h2];have h1:=Nat.lt_floor_add_one (b*2^k);have h5:a*2^k<⌊b*2^k⌋₊ :=ד;exact ⟨(Nat.cast_pos.1 ((С ha h2.le).trans_lt h5)).ne',h5,Nat.floor_le (С (ha.trans h.le) h2.le)⟩ lemma qGz (z:Model Fmt):ofW (toWord z)=z:=qEb z lemma qIm (w:ѓ) (h0:expOf w≠0) (h1:expOf w≠255):wv w≠0:=by have:sv w≠0:=by unfold sv mantOf;split<;>simp[h0]<;>omega simp[qAx w h1,rationalWord,decodeD_toRat,this,zpow_ne_zero] lemma qDq (s e:ѕ) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е) (M:Model Fmt) (hf:Ы M=true) (hM:к M=Model.roundAt Fmt V):toWord M=ziv s e:=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=qB8 s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=х ב _;have hA2:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076) :=С ב hb0.le obtain ⟨Lo,hLo0,hLoW,hLo1,hLo2⟩:∃ Lo:ℝ,0<=Lo∧Lo< W ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<=Lo) ∧ (2^53-2^28)*2^((ex:Int)-1076)<=Lo:=by by_cases hfr:fr=0;. exact ⟨_,hA2,hW3,λh=>absurd hfr h,le_rfl⟩ . have hfr1:(1:ℝ)<=fr:=by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hfr refine ⟨_,?_,hW2 hfr,λ_=>le_rfl,?_⟩;. exact С (by nlinarith) hb0.le . exact Б (by nlinarith) hb0.le have hW0:0< W:=ч hLo0 hLoW;obtain ⟨m1,q1,hm1,hd1,hd1'⟩:=qFt Lo W hLo0 hLoW;set B:ℝ:=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) with hB;obtain ⟨m2,q2,hm2,hd2,hd2'⟩:=qFt W ((W+B)/2) hW0.le ד;have w1:=qGm S ex fr m1 q1 h1 h2 h3 hm1 ד (λh=>ч (hLo1 h) hd1) (ч hLo2 hd1) have w2:=qGm S ex fr m2 q2 h1 h2 h3 hm2 ד (λh=>by exact ч (hLo1 h) ד) (ч hLo2 ד) rw[hz];set z:=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) with hzd;have hzn:Ѡz=(if S then 2^31 else 0)+(ex-896)*2^23+fr:=by rw[hzd,Բ,М];. split<;>omega have hexp:(expOf z).toNat=ex-896:=by rw[qCa,hzn];split<;>omega have he0:expOf z≠0:=λh=>by rw[h] at hexp;simp at hexp;omega have he1:expOf z≠255:=λh=>by rw[h] at hexp;simp at hexp;omega have r1:Щ Fmt ⟨S,m1,q1⟩=ofW z:=by rw[<-w1,qGz] have r2:Щ Fmt ⟨S,m2,q2⟩=ofW z:=by rw[<-w2,qGz] have fz:Ы (ofW z)=true:=by simp[ofW,isFinite_ofUInt32,he1] have t1:=Model.toReal_roundDyadic_eq_roundAt Fmt (by decide) ⟨S,m1,q1⟩ (by rw[r1];exact fz);have t2:=Model.toReal_roundDyadic_eq_roundAt Fmt (by decide) ⟨S,m2,q2⟩ (by rw[r2];exact fz);rw[r1] at t1;rw[r2] at t2 have hv:к (ofW z)=Model.roundAt Fmt V:=by apply le_antisymm . cases S;. rw[t1];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . rw[t2];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . cases S;. rw[t2];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . rw[t1];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith have hnz:к M≠0:=by rw[hM,<-hv];exact qIm z he0 he1 have:=Model.eq_of_toReal_eq_of_nonzero hf fz (hM.trans hv.symm) hnz;rw[this];have hr:toWord (ofW z)=z:=by rw[<-r1,w1] exact hr lemma qIx:(2:ℝ)^(127:ℤ)<=к (Model.posMaxFinite Fmt):=by rw[Model.toReal_posMaxFinite,show Model.pow2 Fmt.fracWidth+Fmt.maxFiniteFracField=2^24-1 by decide +kernel,show Fmt.maxNormalExponent-Int.ofNat Fmt.fracWidth=104 by decide +kernel,Model.bpow,qA0];norm_num lemma qFn (q:ℚ) : Model.signedScaledRatToReal (decide (q<0)) q.num.natAbs q.den 0=(q:ℝ):=by rw[Model.signedScaledRatToReal,Model.scaledRatToReal,Nat.cast_natAbs,Rat.cast_def];by_cases hq:q<0;. simp[hq,abs_of_neg (Rat.num_neg.2 hq)];ring . simp[hq,abs_of_nonneg (Rat.num_nonneg.2 (not_lt.1 hq))] lemma qH7 (s e:ѕ) (q:ℚ) (hlo:R (s-e)<(q:ℝ)) (hhi:(q:ℝ)< R (s+e)) (h:ziv s e≠Е) (hb : |(q:ℝ)| ≤ 2^(127:ℤ)):roundRational q=ziv s e:=by have hf:=Model.isFinite_roundRatScaled_of_abs_le_posMaxFinite Fmt (decide (q<0)) q.num.natAbs q.den 0 (by decide) q.den_nz (qFn q ▸ hb.trans qIx);have hM:=Model.toReal_roundRatScaled_eq_roundAt Fmt _ _ _ 0 (by decide) (by simpa using λh0=>qG5 s e q hlo hhi h (by simp[h0])) q.den_nz hf;exact qDq s e q hlo hhi h _ hf (qFn q ▸ hM) lemma qH4 (N d:ћ) (x:Int) (hr:sqrtLower N d≠0) (hd:0< d) (hs:x<=(FloatFormat.toModel Fmt).targetExponent (Float.Model.totalExponent (sqrtLower N d) x)) (hb:з ((N:ℝ)/d)*FloatLib.Floats.Formats.Flocq.bpow т x < 2^(127:ℤ)) : Ы (Model.ofModel Fmt (Float.Model.UnpackedFloat.roundWithAccuracy (FloatFormat.toModel Fmt) .positive (sqrtLower N d) x (sqrtAccuracy N d)))=true := Model.isFinite_ofModel_roundWithAccuracy_of_lt_bpow Fmt rfl .positive _ _ _ (з ((N:ℝ)/d)) hr (sqrtAccuracy_correct N d hd) hs (hb.trans_eq (qA0 127).symm) lemma qIp (q:ℚ) (hq:0< q) (hb:з (q:ℝ)<2^(127:ℤ)) : Ы (sqrtModel q)=true := qH4 _ _ _ (ne_of_gt (sqrtRoot_pos q hq)) q.pos (sqrtStep_le_target q) (by rwa [sqrtScale_correct q hq]) lemma qGs (s e:ѕ) (q:ℚ) (hq:0< q) (hlo:R (s-e)< з (q:ℝ)) (hhi:з (q:ℝ)< R (s+e)) (h:ziv s e≠Е) (hb:з (q:ℝ)<2^(127:ℤ)):sqrtRational q=ziv s e:=by have hf:=qIp q hq hb;exact (if_neg (not_le.2 hq)).trans (qDq s e _ hlo hhi h _ hf (sqrtModel_correct q hq hf)) lemma qA1 (w:ѓ) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by obtain ⟨hg,hl⟩:=ل.1 h;have h1:=Ж.1 (of_decide_eq_true hl);rw[qAq] at h1;have h2:=qAz w hg;rw[qB5 w hg];refine ⟨hg,(qA6 _ _ h1 א).trans ?_⟩;unfold A_mag;norm_num set_option maxRecDepth 8192 in end section section open Float.Model Float.Model.UnpackedFloat end end @[specialize] def fC {σ:Type} (f:σ->є->є->њ) (s:σ) (b j:є) (o:ё):ћ->ё | 0=>o | k+1=>fC f s b (j+1) (wrW o (b+j) (f s (b+j) j)) k @[specialize] def fR {σ:Type} (h:ћ->σ) (f:σ->є->є->њ) (n r:ћ) (o:ё):ћ->ё | 0=>o | k+1=>fR h f n (r+1) (fC f (h r) (r*n).toUSize 0 o n) k structure LnS where fast:ј m:ѕ ir:ѕ em:ѕ den:ѓ deriving Inhabited def lnSum (x:ё) (base n i:є) (s t:ѕ):F1 := if h:i< n then let v:=dv (rdW x (base+i)) lnSum x base n (i+1) (s+v) (t+v.abs) else ⟨s,t⟩ termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];have:=А.mp h;omega def lnVar (x:ё) (base n i:є) (m v:ѕ):ѕ := if h:i< n then let d:=dv (rdW x (base+i))-m lnVar x base n (i+1) m (v+d*d) else v termination_by Ѡn-Ѡi decreasing_by rw[usz i n h];have:=А.mp h;omega @[inline] def invOK (a b:ѕ):ј := decide ((a.toBits >>> 52)-1<1222) && decide ((((a*b-О 0x3FF0000000000000)+О 0x3CD0000000000000).toBits >>> 52)-972<2) @[inline] def lnQ (b:ј) (n:ћ) (x:ё) (r:ћ):ℚ:=if b then 0 else rowMean n x r def lnStat (b:ј) (n:ћ) (eps:њ) (x:ё) (r:ћ):LnS := let base:=(r*n).toUSize let nu:=Ѣn let nd:=n.toUInt64.toFloat let nf:=(n.toUInt64+8).toFloat*О ь let ind:=1.0/nd let a:=if b then F1.mk 0 0 else lnSum x base nu 0 0 0 let m:=if b then 0 else a.s*ind let v:=lnVar x base nu 0 m 0 let em:=(nf*a.t)*ind+О ظ let tt:=v*ind+dv eps let d:=м tt let z:=ziv d ((nf+nf)*d+em) let ir:=1.0/dv z if sm z && decide ((0x1F800000:ѓ)<=z) && decide (z<=(0x70FFFFFF:ѓ)) && decide ((tt.toBits >>> 52)-1<1222) && invOK ind nd && invOK ir (dv z) then ⟨true,m,ir,em,z⟩ else ⟨false,0,0,0,sqrtRational (rowVariance n x r (lnQ b n x r)+Д eps)⟩ def meanI (n:ћ) (x:ё) (r:ћ):ℤ:= (З n).foldl (λt j=>t+sv x[r*n+j]!*((2^idxOf x[r*n+j]!:ℕ):ℤ)) 0 def meanQ (n:ћ) (x:ё) (r:ћ):ℚ:=(meanI n x r:ℚ)/((2^149*n:ℕ):ℚ) @[noinline] def lnSlow (x g b den:њ) (mean:Unit->ℚ):њ := roundRational ((Д x-mean ())*Д g/Д den+Д b) @[inline] def lnOutF (s:LnS) (w gw bw:њ) (gv bv:ѕ) (mean:Unit->ℚ):њ := if s.fast then let gd:=gv*s.ir let q:=(dv w-s.m)*gd let r:=ziv (q+bv) (О 0x3D10000000000000*(q.abs+bv.abs)+s.em*gd.abs) if 0x7EFFFFFF<(r &&& В) then lnSlow w gw bw s.den mean else r else lnSlow w gw bw s.den mean @[inline] def lnOut (s:LnS) (w gw bw:њ) (mean:Unit->ℚ):њ :=lnOutF s w gw bw (dv gw) (dv bw) mean @[inline] def rdF (a:ђ) (i:є):ѕ:= if h:i< a.usize then a.uget i (qDo a i h) else 0 def decG (A:ё) (n t:є) (o:ђ):ђ := if h:t< n then decG A n (t+1) (wrF o t (dv (rdW A t))) else o termination_by Ѡn-Ѡt decreasing_by rw[usz t n h];have:=А.mp h;omega @[inline] def gdF (r:ћ) (g:ё) (n:ћ):ђ:= if r == 1 then Й 0 else decG g Ѣn 0 (zFor n) @[inline] def gdR (r:ћ) (g:ё) (G:ђ) (j:є):ѕ:= if r == 1 then dv (rdW g j) else rdF G j @[inline] def smA (A:ё):ј:= let h:=scan A A.usize 0 (qAc A) Е 0 h != Е && decide (h<=0x70FFFFFF) @[inline] def normOK (rows n:ћ) (eps:њ) (x g:ё):ј:= decide (0< rows) && decide (0< n) && ѡx == rows*n && ѡg == n && decide (n<2^20) && decide (rows*n<2^32) && smA x && smA g && sm eps && !signOf eps && decide (0< mantOf eps) def lnF (rows n:ћ) (eps:њ) (x g b:ё):ё := if normOK rows n eps x g && ѡb == n && smA b then let st:=Array.ofFn (n:=rows) λr=>lnStat false n eps x r.val let G:=gdF rows g n let B:=gdF rows b n fR (λr=>(st[r]!,λ_:Unit=>meanQ n x r)) (λs p j=>lnOutF s.1 (rdW x p) (rdW g j) (rdW b j) (gdR rows g G j) (gdR rows b B j) s.2) n 0 (К (rows*n) 0) rows else layernormSpec rows n eps x g b def rmsF (rows n:ћ) (eps:њ) (x g:ё):ё := if normOK rows n eps x g then let st:=Array.ofFn (n:=rows) λr=>lnStat true n eps x r.val let G:=gdF rows g n fR (λr=>st[r]!) (λs p j => let w:=rdW x p if w &&& В == 0 then 0 else lnOutF s w (rdW g j) 0 (gdR rows g G j) (dv 0) λ_=>0) n 0 (К (rows*n) 0) rows else rmsnormSpec rows n eps x g section lemma qFv (x:і):(x >>> 52).toNat=Ѡx/2^52:=by rw[р,щ];rfl lemma qEj (x k N:і) (h:x-k< N) (hN:Ѡk+ѠN<2^64) : Ѡk<=Ѡx∧Ѡx< Ѡk+ѠN:=by have h1:=ф.mp h;rw[UInt64.toNat_sub] at h1;have h2:=x.toNat_lt;have h3:=k.toNat_lt;omega lemma qEi (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,Э Ч,й] lemma qCz {f:ѕ} {E:ћ} (h:(f.toBits >>> 52).toNat=E) (hE1:1<=E) (hE:E<2047) : f.isFinite=true∧(2:ℝ)^((E:ℤ)-1023)<=R f∧R f<2^((E:ℤ)-1022):=by rw[qFv] at h;obtain ⟨F,hF,hM⟩:∃ F<2^52,f.toBits.toNat=E*2^52+F :=⟨f.toBits.toNat%2^52,by omega,by omega⟩;obtain ⟨h1,h2⟩:=B_R f false E F hE1 hE hF (by simpa using hM);have hb:(0:ℝ)<2^((E:ℤ)-1075):=х ב _;have hF0:(0:ℝ)<=F:=Ю _;have hF2:(F:ℝ)<2^52:=by exact_mod_cast hF simp only[Bool.false_eq_true,ite_false,one_mul] at h2;push_cast at h2;rw[h2,qEi ((E:ℤ)-1023) (E-1075) 52 א,qEi ((E:ℤ)-1022) (E-1075) 53 א];exact ⟨h1,Б ד hb.le,о ד hb⟩ lemma L_k (c:і) (E n:ћ) (hE1:1<=E) (hn:E+n=1023) (h:(О c).toBits.toNat=E*2^52) : (О c).isFinite=true∧R (О c)=1/2^n:=by obtain ⟨h1,h2⟩:=B_R (О c) false E 0 hE1 א ב (by simpa using h);refine ⟨h1,?_⟩;rw[h2,show (E:ℤ)-1075=-(52+(n:ℤ)) by omega,zpow_neg,Э Ч];norm_num;field_simp lemma qGk {f:ѕ} {E:ћ} (h:(f.toBits >>> 52).toNat=E) (hE1:1<=E) (hE:E<2047) : (м f).isFinite=true∧0< R f ∧ |R (м f)-з (R f)| ≤ 1/2^53*(з (R f)+A_z):=by obtain ⟨hf,hl,-⟩:=qCz h hE1 hE have hp:0< R f:=н (х ב _) hl rw[qFv] at h have hs:Model.signBit (toModel (ofFloat f))=false:=by rw[qFs];simp only[decide_eq_false_iff_not,not_le];omega have hm:toModel (ofFloat f)=A_M f:=toModel_ofFloat f have hfm:Ы (A_M f)=true:=by rw[qAh,hf] rw[hm] at hs have hr:=Model.toReal_sqrt_eq_roundAt (A_M f) (by decide) hfm (Or.inr hs) have hq:=Model.isFinite_sqrt_of_isFinite (A_M f) (by decide) hfm (Or.inr hs) have hd:A_M (м f)=Model.canonicalizeModel (Model.sqrt (A_M f)):=by have h3:=toModel_ofFloat_sqrt f rw[hm,toModel_ofFloat] at h3 exact h3 have ht:Model.toModel (A_M (м f))=Model.toModel (Model.sqrt (A_M f)):=by rw[hd,Model.toModel_canonicalizeModel] have hR:R (м f)=Model.roundAt FloatFormat.binary64 (з (R f)):=by rw[A_R,ж qAj,ht,<-ж qAj] exact hr refine ⟨?_,hp,?_⟩ . rw[<-qAh,qAy,ht,<-qAy];exact hq . rw[hR];have:=qFp (з (R f));rwa [И (Real.sqrt_nonneg _)] at this lemma qAa (x y:ℝ) : |x| ≤ |y| + |x-y|:=by have:=abs_sub_abs_le_abs_sub x y;linarith lemma qAt {k:ℝ} (h:k<=2^256*2^44):k<=A_B :=h.trans_eq ((qEi 300 44 256 rfl).trans ב).symm lemma qBg (a:ѐ) (p:ѓ->ј) (h:a.all p=true) (i:ℕ) (hi:i< ѡa) : p a[i]! = true:=by rw[Я a i hi];simpa using Array.all_eq_true.1 h i hi lemma qE6 (w:ѓ) (g:good w=true) (h:w &&& В=0):Д w=0:=by exact_mod_cast (qAx w (qAe _ g)).symm.trans (qBr w g h) lemma qIu (s e:ѕ) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h0:ziv s e≠Е) (h:¬ (0x7EFFFFFF:ѓ)<(ziv s e &&& В)) : |V| ≤ 2^(127:ℤ):=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,-,hW3⟩:=qB8 s e V hlo hhi h0 have hb:(0:ℝ)<2^((ex:Int)-1076):=х ב _ have hW0:0< W:=lt_trans (mul_pos ב hb) hW3 have hN:((if S then 2^31 else 0)+(ex-896)*2^23+fr)<2^32:=by split<;>omega have hex:ex<=1149:=by by_contra hc apply h apply у.mpr rw[qAq,hz,UInt32.toNat_ofNat_of_lt' hN,(א:ex=1150)] show 2130706431< _ split<;>omega have hF:(fr:ℝ)+1<=2^23:=by exact_mod_cast (א:fr+1<=2^23) rw[hV] have hW:W<=2^54*2^(73:ℤ):=hW1.le.trans (Х ד (Ц ב א) hb.le (by positivity)) rw[<-qEi 127 73 54 rfl] at hW split<;>simp only[abs_neg,abs_of_pos hW0,hW] lemma L_I {n I:ℝ} (hn:1<=n) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48):I<=2:=by have:=Г hn h3.le;linarith [(abs_le.mp h4).2] lemma qF0 (n I v t Sb E T:ℝ) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (ht : |t| ≤ Sb) (hv : |v-n*t| ≤ n*E+T) (hE:0<=E) : |v*I-t| ≤ E*(1+1/2^48)+T*I+Sb*(1/2^48):=by have a1 : |(v-n*t)*I| ≤ (n*E+T)*I:=by rw[в,б h3];exact Б hv h3.le have a2:=Г (abs_le.mp h4).2 hE have a3 : |t*(I*n-1)| ≤ Sb*(1/2^48):=by rw[в];exact Х ht h4 (Л _) ((Л _).trans ht) have a4:=Ъ ((v-n*t)*I) (t*(I*n-1)) rw[show (v-n*t)*I+t*(I*n-1)=v*I-t by ring] at a4 linarith section variable {z:ℝ} (hz0:0<=z) (hz:z<=1/2^200*(1/2^200)) include hz0 hz lemma qD9 {P rp rs rc:ℝ} (h1 : |rp-P| ≤ 1/2^53*(|P| + z)) (h2 : |rs-(rp-1)| ≤ 1/2^53*(|rp-1| + z)) (h3 : |rc-(rs+1/2^50)| ≤ 1/2^53*(|rs+1/2^50| + z)) (lo:1/2^51<=rc) (hi:rc<=1/2^49) : |P-1| ≤ 1/2^48:=by have a1:=qAa (rs+1/2^50) rc;rw[г (rs+1/2^50) rc] at a1;have a2 : |rc| = rc:=И (ش ב lo);obtain ⟨c1,c2⟩:=abs_le.mp h3;have a3 : |rs| ≤ 1/2^49:=by rw[abs_le];constructor<;>linarith [Л (rs+1/2^50)] have b1:=qAa (rp-1) rs;rw[г (rp-1) rs] at b1;have b2 : |rp-1| ≤ 1/2^48:=by linarith [Л (rp-1)] have c3:=qAa (P-1) (rp-1);rw[show P-1-(rp-1)=-(rp-P) by ring,abs_neg] at c3;have c4:=qAa P 1;rw[abs_one] at c4;linarith [Л (P-1)] lemma qG0 {a Mr Rm Em g D b I RA Rg Rq RY RE Rsa Rcs Reg Hi Lo:ℝ} (hE0:0<=Em) (hE1:Em<=2^120) (hM:2 * |Mr-Rm| ≤ Em) (hD:1/2^64<=D) (ha : |a| ≤ 2^99) (hg : |g| ≤ 2^99) (hm : |Rm| ≤ 2^110) (h1 : |RA-(a-Rm)| ≤ 1/2^53*(|a-Rm| + z)) (h2 : |I*D-1| ≤ 1/2^48) (h3 : |Rg-g*I| ≤ 1/2^53*(|g*I| + z)) (h4 : |Rq-RA*Rg| ≤ 1/2^53*(|RA*Rg| + z)) (h5 : |RY-(Rq+b)| ≤ 1/2^53*(|Rq+b| + z)) (h6 : |Rsa-(|Rq| + |b|)| ≤ 1/2^53*(|(|Rq| + |b|)| + z)) (h7 : |Rcs-1/2^46*Rsa| ≤ 1/2^53*(|1/2^46*Rsa| + z)) (h8 : |Reg-Em*(|Rg|)| ≤ 1/2^53*(|Em*(|Rg|)| + z)) (h9 : |RE-(Rcs+Reg)| ≤ 1/2^53*(|Rcs+Reg| + z)) (h10 : |Hi-(RY+RE)| ≤ 1/2^53*(|RY+RE| + z)) (h11 : |Lo-(RY-RE)| ≤ 1/2^53*(|RY-RE| + z)) (hbig:1/2^127 ≤ |Hi|) : Lo<(a-Mr)*g/D+b∧(a-Mr)*g/D+b< Hi:=by have hDp:0< D:=н (by positivity) hD;generalize hG:g/D=G;generalize hX:a-Rm=X at h1;rw[show (a-Mr)*g/D+b=X*G+(Rm-Mr)*G+b by rw[<-hG,<-hX];field_simp;ring];have hGb : |G| ≤ 2^163:=by rw[<-hG,abs_div,б hDp,div_le_iff₀ hDp];linarith have hXb : |X| ≤ 2^111:=hX ▸ (ى _ _).trans ד;have hgI : |g*I-G| ≤ 1/2^48 * |G|:=by rw[show g*I-G=G*(I*D-1) by rw[<-hG];field_simp,в,mul_comm];exact Б h2 (Л _) generalize g*I=Y at h3 hgI;clear hG hX hDp hD ha hg hm h2;have nX:=Л X;have nG:=Л G;have nA:=Л RA;have nR:=Л Rg;have a1:=qAa X RA;rw[г X RA] at a1;have a3 : |X| ≤ (1+2/2^53) * |RA| + 2/2^53*z :=ד;have a4 : |X-RA| ≤ 2/2^53 * |RA| + 2/2^53*z:=by rw[г];linarith have a5 : |RA| ≤ 2^165:=by have:=qAa RA X;linarith have b1:=qAa Y G;have b2:=qAa G Rg;have b3:=abs_sub_le G Y Rg;rw[г G Y,г Y Rg] at b3;have b4 : |G| ≤ (1+1/2^47) * |Rg| + 2/2^53*z :=ד;have b5 : |G-Rg| ≤ 2/2^48 * |Rg| + 2/2^53*z :=ד;have c1 : |Rg| ≤ 2^165:=by have:=qAa Rg G;rw[г] at this;linarith have f1:=Ъ ((X-RA)*G) (RA*(G-Rg));rw[в,в,show (X-RA)*G+RA*(G-Rg)=X*G-RA*Rg by ring] at f1;have f2:=Х a4 b4 nG ד;have f3:=Г b5 nA;have zr:z * |RA| ≤ z*2^165:=Г a5 hz0;have zg:z * |Rg| ≤ z*2^165:=Г c1 hz0;have zz:z*z<=z*2^165:=Г ד hz0;have ze:z*Em<=z*2^165:=Г ד hz0;have nzr:=С hz0 nA;have nzg:=С hz0 nR;have nzz:=С hz0 hz0;have nP:=С nA nR;have f4 : |X*G-RA*Rg| ≤ 9/2^50*(|RA| * |Rg|)+1/2^230 :=ד;have p1 : |RA*Rg| ≤ (1+2/2^53) * |Rq| + 2/2^53*z:=by have:=qAa (RA*Rg) Rq;rw[г (RA*Rg) Rq] at this;have nq:=Л (RA*Rg);linarith rw[<-в] at f4;have q1 : |X*G-Rq| ≤ 5/2^49 * |Rq| + 1/2^229:=by have:=ى (X*G-RA*Rg) (Rq-RA*Rg);rw[show X*G-RA*Rg-(Rq-RA*Rg)=X*G-Rq by ring] at this;have nq:=Л Rq;linarith generalize hT:Em * |Rg| = T at h8;have nT:0<=T:=hT ▸ С hE0 nR;have e1 : |(Rm-Mr)*G| ≤ 5/2^3*T+1/2^230:=by rw[в,г];have h : |Mr-Rm| * |G| ≤ (Em/2)*((1+1/2^47) * |Rg| + 2/2^53*z) :=Х ד b4 nG ד;have:=С hz0 hE0;subst hT;linarith rw[И nT] at h8;have nq:=Л Rq;have nb:=Л b;have nS:0 ≤ |Rq| + |b| :=ד;rw[И nS] at h6;have s0 : |Rq+b| ≤ |Rq| + |b|:=Ъ _ _;have s1 : |Rsa| ≤ 2*(|Rq| + |b|)+z:=by have:=qAa Rsa (|Rq| + |b|);rw[И nS] at this;linarith have h7' : |1/2^46*Rsa| = 1/2^46 * |Rsa|:=by rw[в,б (by positivity:(0:ℝ)<1/2^46)] rw[h7'] at h7;have s2 : |Rcs| ≤ 1/2^44*(|Rq| + |b|)+z:=by have:=qAa Rcs (1/2^46*Rsa);rw[h7'] at this;linarith have s3 : |Reg| ≤ 2*T+z:=by have:=qAa Reg T;rw[И nT] at this;linarith have s4 : |RY| ≤ 2*(|Rq| + |b|)+z:=by have:=qAa RY (Rq+b);linarith have s5:=Ъ Rcs Reg;have s6 : |RE| ≤ 1/2^43*(|Rq| + |b|)+4*T+4*z:=by have:=qAa RE (Rcs+Reg);linarith have s7:=Ъ RY RE;have s8:=ى RY RE;have big:1/2^130 ≤ |Rq| + |b| + T:=by have:=qAa Hi (RY+RE);linarith obtain ⟨u1,u2⟩:=abs_le.mp q1;obtain ⟨v1,v2⟩:=abs_le.mp e1;obtain ⟨w1,w2⟩:=abs_le.mp h5;obtain ⟨x1,x2⟩:=abs_le.mp h6;obtain ⟨y1,y2⟩:=abs_le.mp h7;obtain ⟨z1,z2⟩:=abs_le.mp h8;obtain ⟨o1,o2⟩:=abs_le.mp h9;obtain ⟨r1,r2⟩:=abs_le.mp h10;obtain ⟨t1,t2⟩:=abs_le.mp h11;constructor<;>linarith lemma qG3 {n Rs Rt I Rm N Y Em c:ℝ} (r Sb:ℝ) (hn1:1<=n) (hn2:n<=2^20) (hc:c=(n+8)/2^49) (hr : |r| ≤ Sb) (hS:Sb<=2^99) (h1 : |Rs-n*r| ≤ 2*n*(1/2^53*(n*Sb+z))) (h2 : |Rt-n*Sb| ≤ 2*n*(1/2^53*(n*Sb+z))) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (h5 : |Rm-Rs*I| ≤ 1/2^53*(|Rs*I| + z)) (h6 : |N-c*Rt| ≤ 1/2^53*(|c*Rt| + z)) (h7 : |Y-N*I| ≤ 1/2^53*(|N*I| + z)) (h8 : |Em-(Y+1/2^200)| ≤ 1/2^53*(|Y+1/2^200| + z)) : 2 * |r-Rm| ≤ Em∧Em<=2^120∧1/2^201<=Em∧|Rm| ≤ 2^110:=by have hS0:0<=Sb:=ش (Л _) hr;have nS:0<=n*Sb:=С ד hS0;have nSb:n*Sb<=2^20*2^99:=Х hn2 hS hS0 ב;generalize hW:n*Sb+z=W at h1 h2;have nW:0<=W :=ד;have hI2:=L_I hn1 h3 h4;generalize hE:201/100/2^53*W+Sb/2^48=E have key:∀ v t:ℝ,|t| ≤ Sb->|v-n*t| ≤ 2*n*(1/2^53*W)->|v*I-t| ≤ E := λv t ht hv=>by have:=qF0 n I v t Sb (2*(1/2^53*W)) 0 h3 h4 ht ד ד;linarith have hr1:=key Rs r hr h1;have hA1:=key Rt Sb (by rw[И hS0]) h2;generalize hA:Rt*I=A at hA1;have hE0:0<=E:=by rw[<-hE];positivity have hEb:E<=2^80:=by rw[<-hE];norm_num at nSb hS ⊢;linarith have hc0:0<=c:=by rw[hc];positivity have hc1:c<=1/2^28:=by rw[hc];norm_num at hn2 ⊢;linarith have b0:=qAa A Sb;rw[И hS0] at b0;have b1 : |N*I-c*A| ≤ 1/2^53*(|c*A| + 2*z):=by rw[<-hA,show N*I-c*(Rt*I)=(N-c*Rt)*I by ring,в,б h3,show |c*(Rt*I)| = |c*Rt| * I by rw[в,в,в,б h3];ring];have:=Б h6 h3.le;have:=Г hI2 hz0;linarith have b2 : |c*A| ≤ 1/2^28*(Sb+E):=by rw[в,И hc0];exact Х hc1 ד (Л _) ב have b3:c*Sb-1/2^28*E<=c*A:=by have h9 : |c*(A-Sb)| ≤ 1/2^28*E:=by rw[в,И hc0];exact Х hc1 hA1 (Л _) ב linarith [(abs_le.mp h9).1] have b4:c*Sb=(n*Sb+8*Sb)/2^49:=by rw[hc];ring have c1:=qAa (N*I) (c*A);have c2:=qAa Y (N*I);have c3:=Ъ Y (1/2^200);rw[б (by norm_num:(0:ℝ)<1/2^200)] at c3;have d1:=qAa Rm (Rs*I);have d2:=qAa (Rs*I) r;have d3:=abs_sub_le r (Rs*I) Rm;rw[г r (Rs*I),г (Rs*I) Rm] at d3;obtain ⟨e1,e2⟩:=abs_le.mp b1;obtain ⟨e3,e4⟩:=abs_le.mp h7;obtain ⟨e5,e6⟩:=abs_le.mp h8;rw[<-hE] at hr1 b2 b3 hEb;subst hW;have ha:=Л r;have g1:=le_abs_self (c*A);exact ⟨by linarith,by linarith,by linarith,by linarith⟩ lemma qHr {s c Em dd Rn2 Rmu ee hi lo:ℝ} (hs0:0<=s) (hs1:s<=2^101) (hc0:9/2^49<=c) (hc1:c<=1/2^20) (hE:1/2^201<=Em) (hE1:Em<=2^120) (h1 : |dd-s| ≤ 1/2^53*(s+z)) (h2 : |Rn2-(c+c)| ≤ 1/2^53*(|c+c| + z)) (h3 : |Rmu-Rn2*dd| ≤ 1/2^53*(|Rn2*dd| + z)) (h4 : |ee-(Rmu+Em)| ≤ 1/2^53*(|Rmu+Em| + z)) (h5 : |hi-(dd+ee)| ≤ 1/2^53*(|dd+ee| + z)) (h6 : |lo-(dd-ee)| ≤ 1/2^53*(|dd-ee| + z)) : s*(1+3/2*c)+9/10*Em<=hi∧lo<=s*(1-3/2*c)-9/10*Em:=by have hcp:0< c:=н ב hc0;have hcs:0<=c*s:=С hcp.le hs0;have hcs2:=Б hc0 hs0;have hcc : |c+c| = c+c:=б ד;rw[hcc] at h2;have d1:=qAa dd s;rw[И hs0] at d1;have d2 : |dd| ≤ 3*s+z :=ד;have d3 : |dd| ≤ 2^103:=by norm_num at hs1 hz d2 ⊢;linarith have m1:=Ъ ((Rn2-(c+c))*dd) ((c+c)*(dd-s));rw[в,в,hcc,show (Rn2-(c+c))*dd+(c+c)*(dd-s)=Rn2*dd-(c+c)*s by ring] at m1;have m2:=Х h2 d2 (Л _) (by positivity);have m3:=Г h1 (by linarith:0<=c+c);have zs:=Х hz hs1 hs0 ב;have zc:z*c<=z*1:=Г ד hz0;have zz:z*z<=z*1:=Г ד hz0;have nzs:=С hz0 hs0;have nzc:=С hz0 hcp.le;have nzz:=С hz0 hz0;have m4 : |Rn2*dd-(c+c)*s| ≤ 1/2^49*(c*s)+1/2^230:=by norm_num at zs ⊢;linarith generalize Rn2*dd=M at h3 m4;rw[show (c+c)*s=2*(c*s) by ring] at m4;have n1:=qAa M (2*(c*s));rw[И (by linarith:0<=2*(c*s))] at n1;have n2:=qAa Rmu M;have n3:=Ъ Rmu Em;rw[б (by linarith [(by norm_num:(0:ℝ)<1/2^201)]:0< Em)] at n3;have n4:=qAa ee (Rmu+Em);have n5:=Ъ dd ee;have n6:=ى dd ee;obtain ⟨a1,a2⟩:=abs_le.mp h1;obtain ⟨b1,b2⟩:=abs_le.mp m4;obtain ⟨c1,c2⟩:=abs_le.mp h3;obtain ⟨d4,d5⟩:=abs_le.mp h4;obtain ⟨e1,e2⟩:=abs_le.mp h5;obtain ⟨f1,f2⟩:=abs_le.mp h6;have hcs3:=Б hc1 hs0;constructor<;>linarith lemma qIw {x Rd RQ:ℝ} (hx : |x| ≤ 2^111) (h1 : |Rd-x| ≤ 1/2^53*(|x| + z)) (h2 : |RQ-Rd*Rd| ≤ 1/2^53*(|Rd*Rd| + z)) : |RQ-x^2| ≤ 4/2^53*x^2+1/2^250:=by have nA:=Л x have zA:=Х hz hx nA ב have zz:z*z<=z*1:=Г ד hz0 have nzA:=С hz0 nA have nzz:=С hz0 hz0 have hsq : |x| ^ 2=x^2:=sq_abs x generalize he:Rd-x=e at h1 have q1:=ы (Л e) h1 2 have q3 : |2*x*e| ≤ 2 * |x| * (1/2^53*(|x| + z)):=by rw[в,в,abs_two] exact Г h1 (by positivity) have q6 : |Rd*Rd-x^2| ≤ |2*x*e| + |e| ^ 2:=by rw[show Rd*Rd-x^2=2*x*e+e^2 by rw[<-he];ring,<-abs_pow] exact Ъ _ _ have q8:=qAa (Rd*Rd) (x^2) rw[И (ط x)] at q8 have q9:=qAa (RQ-x^2) (RQ-Rd*Rd) rw[show RQ-x^2-(RQ-Rd*Rd)=Rd*Rd-x^2 by ring] at q9 have nX:=ط x have s1 : |Rd*Rd-x^2| ≤ 201/100/2^53*x^2+1/2^254 :=ד linarith lemma qH9 {n Rv I Rp Rt ep c:ℝ} (Ws tau:ℝ) (hn1:1<=n) (hn2:n<=2^20) (hc:c=(n+8)/2^49) (hWs:0<=Ws) (hep:1/2^126<=ep) (ht0:0<=tau) (ht:tau<=1/2^229) (h1 : |Rv-n*Ws| ≤ (2*n+6)/2^53*(n*Ws)+tau) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (h5 : |Rp-Rv*I| ≤ 1/2^53*(|Rv*I| + z)) (h6 : |Rt-(Rp+ep)| ≤ 1/2^53*(|Rp+ep| + z)) : |Rt-(Ws+ep)| ≤ c/2*(Ws+ep):=by have hep0:0< ep:=н ב hep have hI2:=L_I hn1 h3 h4 have hX:0<=(2*n+6)/2^53*Ws:=С ד hWs have b1:=qF0 n I Rv Ws Ws _ tau h3 h4 (by rw[И hWs]) ד hX have a3:=Г hI2 ht0 have nnW:=С (by linarith:0<=n) hWs have nnE:=С (by linarith:0<=n) hep0.le have nnW2:=Б hn2 hWs have b2:=qAa (Rv*I) Ws rw[И hWs] at b2 have b3:=Ъ Rp ep rw[б hep0] at b3 have b4:=qAa Rp (Rv*I) have b5:=Ъ (Rt-(Rp+ep)) ((Rp-Rv*I)+(Rv*I-Ws)) have b6:=Ъ (Rp-Rv*I) (Rv*I-Ws) rw[show Rt-(Rp+ep)+(Rp-Rv*I+(Rv*I-Ws))=Rt-(Ws+ep) by ring] at b5 rw[hc] linarith lemma qG8 {n W Rv Sq Si:ℝ} (hn1:1<=n) (hn2:n<=2^20) (hW:0<=W) (h1 : |Rv-Sq| ≤ 2*n*(1/2^53*(Si+z))) (h2 : |Sq-W| ≤ 4/2^53*W+n*(1/2^250)) (h3:Si<=(1+4/2^53)*W+n*(1/2^250)) : |Rv-W| ≤ (2*n+6)/2^53*W+1/2^229:=by have hn0:0<=n :=ד have a1:=Г h3 hn0 have a3:=Б hn2 hW have a4:=Х hn2 hn2 hn0 ב have a5:=Х hn2 hz hz0 ב have a6:=abs_sub_le Rv Sq W linarith end lemma qHl {T s c Em lo hi:ℝ} (hT:0< T) (hs:0< s) (hc0:0< c) (hc1:c<=1/2^20) (hE:0<=Em) (h1:T<=s^2*(1+c)) (h2:s^2*(1-c)-Em^2/4<=T) (h3:s*(1+3/2*c)+9/10*Em<=hi) (h4:lo<=s*(1-3/2*c)-9/10*Em) : lo< з T∧з T< hi:=by have hsT:=Real.sqrt_pos.mpr hT;have hq:0< s^2*c:=by positivity constructor . by_cases hl:lo<0;. linarith . rw[not_lt] at hl;rw[Real.lt_sqrt hl];generalize hk:s*(1-3/2*c)=k at h4;have hk0:0<=k-9/10*Em :=ד;have q1:=ы hl h4 2;have q3:=С hE hk0 have q4:k^2<=s^2*(1-3/2*c):=by have h5:(1-3/2*c)^2<=1-3/2*c:=by nlinarith calc k^2=s^2*(1-3/2*c)^2:=by rw[<-hk];ring _<=_:=Г h5 (ط _) have q5:=ط Em;nlinarith . have hh:0< hi:=by have h5:0< s*(1+3/2*c):=by positivity linarith rw[Real.sqrt_lt' hh];have q1:=ы (by positivity) h3 2;have r1:0<=s^2*c^2:=by positivity have r2:0<=s*Em*(1+3/2*c):=by positivity have r3:=ط Em;nlinarith lemma qHp (l:List ℕ) (a:ℕ->ℝ) (c:ℝ) : (l.map λj=>(a j-c)^2).sum =(l.map λj=>a j^2).sum-2*c*(l.map a).sum+ѣl*c^2:=by induction l with | nil=>simp | cons j l ih => simp only[List.map_cons,List.sum_cons,List.length_cons,ih] push_cast ring lemma qGh (l:List ℕ) (a:ℕ->ℝ) (m:ℝ) (hl:(ѣl:ℝ)≠0) : (l.map λj=>(a j-m)^2).sum = (l.map λj=>(a j-(l.map a).sum/ѣl)^2).sum + ѣl*((l.map a).sum/ѣl-m)^2:=by rw[qHp,qHp] field_simp ring lemma qGx (l:List ℕ) (f w:ℕ->ℝ) (u t:ℝ) (hw:∀ j∈l,0<=w j) (h:∀ j∈l,|f j-w j| ≤ u*w j+t) (hu:0<=u) (ht:0<=t) : |(l.map f).sum-(l.map w).sum| ≤ u*(l.map w).sum+ѣl*t ∧ (l.map λj=>|f j|).sum<=(1+u)*(l.map w).sum+ѣl*t∧0<=(l.map w).sum:=by induction l with | nil=>simp | cons j l ih => obtain ⟨i1,i2,i3⟩:=ih (λk hk=>hw k (م _ hk)) (λk hk=>h k (م _ hk)) have h0:=hw j Դ have h1:=h j Դ simp only[List.map_cons,List.sum_cons,List.length_cons] push_cast have a1:=Ъ (f j-w j) ((l.map f).sum-(l.map w).sum) rw[show f j-w j+((l.map f).sum-(l.map w).sum)=f j+(l.map f).sum-(w j+(l.map w).sum) by ring] at a1 have a2:=qAa (f j) (w j) rw[И h0] at a2 exact ⟨by linarith,by linarith,by linarith⟩ lemma qIr {σ:Type} {N n c:ћ} {sp:ћ->њ} {f:σ->є->є->њ} {s:σ} {b:є} (hN:N<2^32) (hb:Ѡb=c) (hn:c+n<=N) (hf:∀ p u:є,Ѡp=c+Ѡu->Ѡu< n->f s p u=sp Ѡp) (k:ћ) : ∀ (j:є) (o:ё),Ѡj+k=n->C_Inv N sp (λq=>q< c+Ѡj) o → C_Inv N sp (λq=>q< c+n) (fC f s b j o k):=by induction k with | zero => intro j o e h rw[fC,<-e];exact h | succ k ih => intro j o e h have a0:(b+j).toNat=c+Ѡj:=by rw[<-hb];exact qAf _ _ א have a1:(j+1).toNat=Ѡj+1:=by rw[qAf _ _ (by rw[ض];omega),ض] rw[fC] refine ih _ _ א ?_ rw[a1] exact qAp hN h a0 א (by rw[hf _ _ a0 א,a0]) lemma qDb {σ:Type} {R n:ћ} (F:ћ->ћ->ћ->њ) {h:ћ->σ} {f:σ->є->є->њ} (hN:R*n<2^32) (hn:0< n) (hf:∀ (r:ћ) (p u:є),r< R->Ѡp=r*n+Ѡu->Ѡu< n → f (h r) p u=F r Ѡu Ѡp) : fR h f n 0 (К (R*n) 0) R =Array.ofFn (n:=R*n) λq=>F (q.val/n) (q.val%n) q.val:=by let sp q:=F (q/n) (q%n) q have K:∀ (d r:ћ) (o:ё),r+d=R->C_Inv (R*n) sp (λq=>q< r*n) o → C_Inv (R*n) sp (λq=>q< R*n) (fR h f n r o d):=λd=>by induction d with | zero => intro r o e H subst e exact H | succ d ih => intro r o e H have h1:r*n+n<=R*n:=qBc (א:r< R) (ي _) rw[fR] refine ih _ _ א ?_ rw[Nat.succ_mul] exact qIr hN (qAd _ א) h1 (λp u h2 h3=>by rw[hf r p u א h2 h3] show F _ _ _=F _ _ _ rw[h2,е r n,Nat.mul_add_div hn,Nat.mul_add_mod,ؼ h3,М h3,ю]) n 0 o (Т _) H exact qBv (K R 0 _ א ⟨by simp,λq _ (h:q<0*n)=>by omega⟩) λq hq=>hq section variable (x:ѐ) (hx:ѡx<2^32) (base nu:є) include x hx base nu lemma qG9 (hb:Ѡbase+Ѡnu<2^32) (d:ℕ) (i:є) (s t:ѕ) (h:Ѡi+d=Ѡnu) : lnSum x base nu i s t = ⟨((List.range' Ѡi d).map λj=>dv x[Ѡbase+j]!).foldl (.+·) s,((List.range' Ѡi d).map λj=>(dv x[Ѡbase+j]!).abs).foldl (.+·) t⟩:=by induction d generalizing i s t with | zero => rw[lnSum,dif_neg (by rw[А];omega)];simp | succ d ih => have hn:i< nu:=by rw[А];omega have e1:=usz i nu hn rw[lnSum,dif_pos hn,ih (i+1) _ _ (by rw[e1];omega),e1,qAg x _ hx,qAf base i א] simp[List.range'_succ] lemma qG2 (m:ѕ) (hb:Ѡbase+Ѡnu<2^32) (d:ℕ) (i:є) (v:ѕ) (h:Ѡi+d=Ѡnu) : lnVar x base nu i m v = ((List.range' Ѡi d).map λj =>(dv x[Ѡbase+j]! - m)*(dv x[Ѡbase+j]! - m)).foldl (.+·) v:=by induction d generalizing i v with | zero => rw[lnVar,dif_neg (by rw[А];omega)];simp | succ d ih => have hn:i< nu:=by rw[А];omega have e1:=usz i nu hn rw[lnVar,dif_pos hn,ih (i+1) _ (by rw[e1];omega),e1,qAg x _ hx,qAf base i א] simp[List.range'_succ] end lemma qEp (l:List ℕ) (f:ℕ->ℚ) (u:ℕ->ℝ) (c:ℚ) (h:∀ j∈l,(f j:ℝ)=u j) : ((l.foldl (λt j=>t+f j) c:ℚ):ℝ)=c+(l.map u).sum:=by induction l generalizing c with | nil=>simp | cons j l ih => simp only[List.foldl_cons,List.map_cons,List.sum_cons];rw[ih _ λk hk=>h k (م _ hk),<-h j Դ];push_cast;ring lemma qIb (w:ѓ) (hg:good w=true) (h1:(0x1F800000:ѓ)<=w) (h2:w<=(0x70FFFFFF:ѓ)):1/2^64<=wv w:=by have a1:528482304<=Ѡw:=Ж.mp h1;have a2:Ѡw<=1895825407:=Ж.mp h2;rw[qA7 w hg,if_neg א,one_mul];have h3:=qA6 528482304 (Ѡw%2^31) א א;have:A_mag 528482304=1/2^64:=by unfold A_mag;norm_num rwa [this] at h3 lemma qH3 (w:ѓ) (hg:good w=true) (h0:0< wv w):1/2^126<=wv w:=by have hn:=qAz w hg;rw[qA7 w hg] at h0 ⊢;rcases hn with hn | ⟨hn,-⟩;. rw[hn] at h0;simp[A_mag] at h0 . have hs:¬ 2^31<=Ѡw:=λhc=>by rw[if_pos hc] at h0;linarith [qAs (Ѡw%2^31)] rw[if_neg hs,one_mul];have h3:=qA6 (2^23) (Ѡw%2^31) hn א;have:A_mag (2^23)=1/2^126:=by unfold A_mag;norm_num rwa [this] at h3 lemma qIi (n:ℕ) (hn:n<2^20) : ((n.toUInt64+8).toFloat*О ь).isFinite=true ∧ R ((n.toUInt64+8).toFloat*О ь)=((n:ℝ)+8)/2^49:=by have ht:(n.toUInt64+8).toNat=n+8:=by have h8:(8:і).toNat=8:=rfl simp[UInt64.toNat_add,h8] omega have h1:=qCk (n.toUInt64+8) (by rw[ht];omega) obtain ⟨h2,h3⟩:=L_k ь 974 49 ב rfl (by rfl) have h4:(n:ℝ)+8<=2^21:=by exact_mod_cast (א:n+8<=2^21) obtain ⟨hf,he⟩:=qEd _ _ h1.1 h2 (by rw[h1.2,h3,ht] refine ش (Х (qAt ?_) (qAt (by rw[И (by positivity)];norm_num)) (Л _) qI1.le) qDe rw[Nat.cast_add,И (by positivity)] push_cast linarith) refine ⟨hf,?_⟩ rw[he,h1.2,h3,ht] have:=qA2 ((n:ℤ)+8) (-49) א ב push_cast at this ⊢ rw[show (2:ℝ)^(-49:ℤ)=1/2^49 by norm_num] at this rw[this] ring lemma qD7 {r x B:ℝ} (e : |r-x| ≤ 1/2^53*(|x| + A_z)) (h : |x| ≤ B) : |r| ≤ 9/8*B+A_z:=by linarith [qAa r x,Л x,qAr] section variable (x y:ѕ) (B:ℝ) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ B) (h2 : |R y| ≤ B) (hB:B<=2^256*2^44:=by norm_num) include x y B hx hy h1 h2 hB lemma qAv : (x+y).isFinite=true∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) ∧ |R (x+y)| ≤ 9/8*(B+B)+A_z:=by obtain ⟨f,e⟩:=qBf x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hB)) exact ⟨f,e,qD7 e ((Ъ _ _).trans (add_le_add h1 h2))⟩ lemma qCg : (x-y).isFinite=true∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) ∧ |R (x-y)| ≤ 9/8*(B+B)+A_z:=by obtain ⟨f,e⟩:=qDf x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hB)) exact ⟨f,e,qD7 e ((ى _ _).trans (add_le_add h1 h2))⟩ end lemma qAl (x y:ѕ) (B C:ℝ) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ B) (h2 : |R y| ≤ C) (hB:B<=2^256*2^44:=by norm_num) (hC:C<=2^256*2^44:=by norm_num) : (x*y).isFinite=true∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) ∧ |R (x*y)| ≤ 9/8*(B*C)+A_z:=by obtain ⟨f,e⟩:=qCf x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hC)) exact ⟨f,e,qD7 e (в (R x) (R y) ▸ Х h1 h2 (Л _) ((Л _).trans h1))⟩ lemma qFy (a b:ѕ) (hb:b.isFinite=true) (hb2 : |R b| ≤ 2^99) (h:invOK a b=true) : a.isFinite=true∧0< R a∧R a<=2^200∧|R a*R b-1| ≤ 1/2^48:=by simp only[invOK,Bool.and_eq_true,decide_eq_true_eq] at h;obtain ⟨r1,r2⟩:1<=_∧_<1+1222:=qEj _ 1 1222 h.1 (by decide);obtain ⟨r3,r4⟩:972<=_∧_<972+2:=qEj _ 972 2 h.2 (by decide);obtain ⟨f1,l1,u1⟩:=qCz rfl r1 א;have hp:0< R a:=н (х ב _) l1;have ha2:R a<=2^200:=u1.le.trans ((Ц ב א).trans_eq (by norm_num:(2:ℝ)^(200:ℤ)=2^200));obtain ⟨k1,k2⟩:=L_k 0x3FF0000000000000 1023 0 ב rfl (by rfl);obtain ⟨k3,k4⟩:=L_k 0x3CD0000000000000 973 50 ב rfl (by rfl);rw[pow_zero,div_one] at k2;have hz1:=qBo;obtain ⟨f2,g1,q1⟩:=qAl a b (2^200) (2^99) f1 hb (by rwa [б hp]) hb2;obtain ⟨f3,g2⟩:=qDf _ _ f2 k1 (qAt ד) (qAt (by rw[k2];norm_num));rw[k2] at g2;have q3:=ى (R (a*b)) 1;rw[abs_one] at q3;have q2:=qD7 g2 q3;obtain ⟨f4,g3⟩:=qBf _ _ f3 k3 (qAt ד) (qAt (by rw[k4];norm_num));rw[k4] at g3;obtain ⟨-,l2,u2⟩:=qCz rfl (ش ב r3) א;refine ⟨f1,hp,ha2,qD9 qAr.le qAw g1 g2 g3 ?_ ?_⟩;. exact (by norm_num:(1/2^51:ℝ)=2^(-51:ℤ)).trans_le ((Ц ב א).trans l2) . exact u2.le.trans ((Ц ב א).trans_eq (by norm_num:(2:ℝ)^(-49:ℤ)=1/2^49)) lemma qDr (n:ℕ) (f:ℕ->ℝ) (B:ℝ) (h:∀ j,j< n->f j<=B) : ((З n).map f).sum<=n*B:=by simpa using List.sum_le_length_nsmul ((List.range n).map f) B (by simpa using λj hj=>h j hj) lemma qFm (n:ℕ) (hn:n<2^20) (f:ℕ->ѕ) (a b:ℕ->ℝ) (hf:∀ j,j< n->(f j).isFinite=true∧R (f j)=a j∧|a j| = b j) (hb:((З n).map b).sum<=A_B/4) : (A_fsum ((З n).map f)).isFinite=true ∧ |R (A_fsum ((З n).map f))-((З n).map a).sum| ≤ 2*n*(1/2^53*(((З n).map b).sum+A_z)):=by have s:∀ (u:ѕ->ℝ) (v:ℕ->ℝ),(∀ j,j< n->u (f j)=v j) → (((З n).map f).map u).sum=((З n).map v).sum:=λu v h=>by simp only[List.map_map] exact congrArg _ (Ա λj hj=>h j (Р.1 hj)) have e1:=s (λq=>|R q|) b λj h=>by simp only[(hf j h).2.1,(hf j h).2.2] have e2:=s R a λj h=>(hf j h).2.1 have:=qBz ((З n).map f) (λq hq=>by obtain ⟨j,hj,rfl⟩:=List.mem_map.1 hq exact (hf j (Р.1 hj)).1) (by simp;omega) (by rw[A_sa,e1];exact hb) rwa [A_sa,A_sr,e1,e2,List.length_map,List.length_range] at this lemma qGp (n:ℕ) (hn:n<2^20) (hn0:0< n) (g:ℕ->ѕ) (m v:ѕ) (W:ℝ) (hv:v=A_fsum ((З n).map λj=>(g j-m)*(g j-m))) (hW:W=((З n).map λj=>(R (g j)-R m)^2).sum) (hg:∀ j,j< n->(g j).isFinite=true∧|R (g j)| ≤ 2^99) (hm:m.isFinite=true) (hm2 : |R m| ≤ 2^110) : v.isFinite=true∧|R v-W| ≤ (2*n+6)/2^53*W+1/2^229∧W<=n*2^222∧0<=W:=by have hz0:=qAr.le;have hzl:=qAw;have hzb:=qBo have hq:∀ j,j< n->((g j-m)*(g j-m)).isFinite=true ∧ |R ((g j-m)*(g j-m))-(R (g j)-R m)^2| ≤ 4/2^53*(R (g j)-R m)^2+1/2^250∧(R (g j)-R m)^2<=2^222:=by intro j hj obtain ⟨f,b⟩:=hg j hj have B1 : |R (g j)| ≤ 2^110:=b.trans ב obtain ⟨f1,e1,b1⟩:=qCg (g j) m (2^110) f hm B1 hm2 have b2 : |R (g j-m)| ≤ 2^112 :=ד obtain ⟨f2,e2,-⟩:=qAl _ _ (2^112) (2^112) f1 f1 b2 b2 have hx : |R (g j)-R m| ≤ 2^111:=(ى _ _).trans ד refine ⟨f2,qIw hz0 hzl hx e1 e2,?_⟩ have:=ы (Л _) hx 2 rw[sq_abs] at this exact this.trans ב obtain ⟨a1,a2,a3⟩:=qGx (З n) (λj=>R ((g j-m)*(g j-m))) (λj=>(R (g j)-R m)^2) (4/2^53) (1/2^250) (λj _=>ط _) (λj hj=>(hq j (Р.1 hj)).2.1) ב ב;simp only[List.length_range] at a1 a2;rw[<-hW] at a1 a2 a3;have hW':W<=n*2^222:=hW ▸ qDr n _ _ λj h=>(hq j h).2.2;have hnR:(n:ℝ)<=2^20:=by exact_mod_cast hn.le have hnR1:(1:ℝ)<=n:=by exact_mod_cast hn0 have h1:(n:ℝ)*2^222<=2^20*2^222:=by gcongr have h2:(n:ℝ)*(1/2^250)<=2^20*(1/2^250):=by gcongr obtain ⟨f2,e2⟩:=qFm n hn _ _ (λj=>|R ((g j-m)*(g j-m))|) (λj h=>⟨(hq j h).1,rfl,rfl⟩) (by refine ش a2 ?_;have:=qAt (k:=2^256*2^44) le_rfl linarith) rw[<-hv] at e2 f2;exact ⟨f2,qG8 hz0 hzl hnR1 hnR a3 e2 a1 a2,hW',a3⟩ lemma qF2 (l:List ℕ) (f:ℕ->ℝ) : |(l.map f).sum| ≤ (l.map λj=>|f j|).sum:=by induction l with | nil=>simp | cons j l ih => simp only[List.map_cons,List.sum_cons];exact (Ъ _ _).trans (add_le_add le_rfl ih) def L_oF (s:LnS) (M:ℝ):Prop := s.fast=true->s.m.isFinite=true∧s.ir.isFinite=true∧s.em.isFinite=true ∧ R s.em<=2^120∧1/2^201<=R s.em∧2 * |M-R s.m| ≤ R s.em ∧ |R s.m| ≤ 2^110∧good s.den=true∧1/2^64<=wv s.den ∧ |R s.ir*wv s.den-1| ≤ 1/2^48 def L_ok (b:ј) (n:ℕ) (eps:њ) (x:ё) (r:ℕ) (s:LnS):Prop := s.den=sqrtRational (rowVariance n x r (lnQ b n x r)+Д eps) ∧ L_oF s (lnQ b n x r:ℚ) lemma qHi (n r:ℕ) (x:ё) (g:ℕ->ѕ) (h:∀ j,j< n → R (g j)=(Д x[r*n+j]! : ℝ)) (q:ℚ) : ((rowMean n x r:ℚ):ℝ)=((З n).map λj=>R (g j)).sum/n ∧ ((rowVariance n x r q:ℚ):ℝ) =((З n).map λj=>(R (g j)-(q:ℝ))^2).sum/n:=by constructor . rw[rowMean,Rat.cast_div,qEp _ _ (λj=>R (g j)) 0 λj hj=>(h j (Р.1 hj)).symm];simp . rw[rowVariance,Rat.cast_div,qEp _ _ (λj=>(R (g j)-(q:ℝ))^2) 0 λj hj=>by rw[h j (Р.1 hj)];push_cast;rfl] simp section variable (n:ℕ) (hnR1:(1:ℝ)<=n) (hnR2:(n:ℝ)<=2^20) include n hnR1 hnR2 lemma qG1 (Fs Ft ind nf m em:ѕ) (S Sa c:ℝ) (hc:c=((n:ℝ)+8)/2^49) (o1:Fs.isFinite=true) (o2:Ft.isFinite=true) (o3 : |S| ≤ Sa) (o4:Sa<=n*2^99) (o5 : |R Fs-S| ≤ 2*n*(1/2^53*(Sa+A_z))) (o6 : |R Ft-Sa| ≤ 2*n*(1/2^53*(Sa+A_z))) (fnf:nf.isFinite=true) (Rnf:R nf=c) (fi:ind.isFinite=true) (pi:0< R ind) (ei : |R ind*n-1| ≤ 1/2^48) (hm:Fs*ind=m) (hem:nf*Ft*ind+О ظ=em) : m.isFinite=true∧em.isFinite=true∧R em<=2^120∧1/2^201<=R em ∧ 2 * |S/n-R m| ≤ R em∧|R m| ≤ 2^110:=by have hz0:=qAr.le have hzb:=qBo have hc0:0< c:=by rw[hc];positivity have hc1:c<=1/2^20:=by rw[hc];linarith have bI : |R ind| ≤ 2:=by rw[б pi];exact L_I hnR1 pi ei have hSa0:0<=Sa:=(Л _).trans o3 have hSb:=Б hnR2 (by norm_num:(0:ℝ)<=2^99) have o7:=Х hnR2 (by linarith:Sa+A_z<=2^119+1) ד ב have bs : |R Fs| ≤ 2^120:=by linarith [qAa (R Fs) S] have bt : |R Ft| ≤ 2^120:=by have:=qAa (R Ft) Sa rw[И hSa0] at this linarith have bnf : |R nf| ≤ 1:=by rw[Rnf,б hc0];linarith obtain ⟨fm,em1,-⟩:=qAl Fs ind (2^120) 2 o1 fi bs bI rw[hm] at fm em1 obtain ⟨fN,emN,bN⟩:=qAl nf Ft 1 (2^120) fnf o2 bnf bt rw[Rnf] at emN obtain ⟨fY,emY,bY⟩:=qAl (nf*Ft) ind (2^122) 2 fN fi ד bI obtain ⟨k1,k2⟩:=L_k ظ 823 200 ב rfl (by rfl) obtain ⟨fE,emE,-⟩:=qAv (nf*Ft*ind) _ (2^125) fY k1 ד (by rw[k2];norm_num) rw[k2,hem] at emE rw[hem] at fE have hnpos:(0:ℝ)< n :=ד have hnp:=hnpos.ne' have eS:(n:ℝ)*(S/n)=S:=by field_simp have eA:(n:ℝ)*(Sa/n)=Sa:=by field_simp obtain ⟨m1,m2,m3,m4⟩:=qG3 hz0 qAw (S/n) (Sa/n) hnR1 hnR2 hc (by rw[abs_div,б hnpos];exact div_le_div_of_nonneg_right o3 hnpos.le) (by rw[div_le_iff₀ hnpos];linarith) (by rw[eS,eA];exact o5) (by rw[eA];exact o6) pi ei em1 emN emY emE exact ⟨fm,fE,m2,m3,m1,m4⟩ lemma qIv (v ind tt:ѕ) (eps:њ) (W c:ℝ) (hc:c=((n:ℝ)+8)/2^49) (p1:v.isFinite=true) (p2 : |R v-W| ≤ (2*n+6)/2^53*W+1/2^229) (p3:W<=n*2^222) (hW0:0<=W) (fi:ind.isFinite=true) (pi:0< R ind) (ei : |R ind*n-1| ≤ 1/2^48) (gE:good eps=true) (bE : |wv eps| ≤ 2^99) (hep0:0< wv eps) (htt:v*ind+dv eps=tt) : tt.isFinite=true∧|R tt-(W/n+wv eps)| ≤ c/2*(W/n+wv eps):=by have hnpos:(0:ℝ)< n :=ד;have hnp:=hnpos.ne';have hzb:=qBo;have bI : |R ind| ≤ 2:=by rw[б pi];exact L_I hnR1 pi ei have bW:=Б hnR2 (by norm_num:(0:ℝ)<=2^222);have bv : |R v| ≤ 2^250:=by have a1:=qAa (R v) W;rw[И hW0] at a1;have a3:=Б hnR2 hW0;linarith obtain ⟨fp,ep1,bp⟩:=qAl v ind (2^250) 2 p1 fi bv bI;obtain ⟨de1,de2⟩:=qAm eps gE;obtain ⟨ft,et,-⟩:=qAv (v*ind) (dv eps) (2^253) fp de1 ד (by rw[de2];exact bE.trans ב);rw[de2,htt] at et;rw[htt] at ft;exact ⟨ft,qH9 qAr.le qAw (W/n) (1/2^229) hnR1 hnR2 hc (div_nonneg hW0 hnpos.le) (qH3 eps gE hep0) ב le_rfl (by rw[show (n:ℝ)*(W/n)=W by field_simp];exact p2) pi ei ep1 et⟩ end lemma qIk (tt nf em:ѕ) (E:ℕ) (hE1:1<=E) (hE:E<=1222) (hEe:(tt.toBits >>> 52).toNat=E) (c P δ T:ℝ) (hc1:c<=1/2^20) (hc2:9/2^49<=c) (fnf:nf.isFinite=true) (Rnf:R nf=c) (fE:em.isFinite=true) (m2:R em<=2^120) (m3:1/2^201<=R em) (hP0:0< P) (hq1 : |R tt-P| ≤ c/2*P) (m1:2 * |δ| ≤ R em) (hT:T=P-δ^2) (hT0:0< T) : R (м tt-((nf+nf)*м tt+em))< з T ∧ з T< R (м tt+((nf+nf)*м tt+em)) ∧ з T<2^(127:ℤ):=by have hzb:=qBo have hc0:0< c:=н ב hc2 obtain ⟨-,-,u1⟩:=qCz hEe hE1 א obtain ⟨fd,ptt,ed⟩:=qGk hEe hE1 א generalize м tt=d at fd ed ⊢ have ht2:R tt<=2^200:=u1.le.trans ((Ц ב א).trans_eq (by norm_num:(2:ℝ)^(200:ℤ)=2^200)) generalize hs':з (R tt)=s' at ed have hs0:0< s':=by rw[<-hs'];exact Real.sqrt_pos.mpr ptt have hsq:s'^2=R tt:=by rw[<-hs'];exact Real.sq_sqrt ptt.le have hs1:s'<=2^100:=by rw[<-hs',Real.sqrt_le_iff] exact ⟨by norm_num,ht2.trans ב⟩ have bd : |R d| ≤ 2^102:=by have a1:=qAa (R d) s' rw[б hs0] at a1 linarith have bnf : |R nf| ≤ 1:=by rw[Rnf,б hc0];linarith obtain ⟨f2n,e2n,b2n⟩:=qAv nf nf 1 fnf fnf bnf bnf rw[Rnf] at e2n obtain ⟨fmu,emu,bmu⟩:=qAl (nf+nf) d 4 (2^102) f2n fd ד bd have hEm0:0<=R em:=ش ב m3 have bEm : |R em| ≤ 2^120:=by rw[И hEm0];exact m2 obtain ⟨fe,ee,be⟩:=qAv ((nf+nf)*d) em (2^120) fmu fE ד bEm obtain ⟨fh,eh,-⟩:=qAv d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨fl,el,-⟩:=qCg d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨hhi,hlo⟩:=qHr qAr.le qAw hs0.le (hs1.trans ב) hc2 hc1 m3 m2 ed e2n emu ee eh el obtain ⟨b1,b2⟩:=abs_le.mp hq1 have hδ2:δ^2<=R em^2/4:=by have a1:(2 * |δ|)^2<=R em^2:=ы (by positivity) m1 2 rw[mul_pow,sq_abs] at a1 linarith have nP:0<=c*P:=(mul_pos hc0 hP0).le have a3:=Б (by linarith:c<=1) nP have a4:=С hc0.le nP have nd2:=ط δ have h1':T<=s'^2*(1+c):=by rw[hsq,hT] have a1:=Б (by linarith:P-c/2*P<=R tt) (by linarith:0<=1+c) linarith have h2':s'^2*(1-c)-R em^2/4<=T:=by rw[hsq,hT] have a1:=Б (by linarith:R tt<=P+c/2*P) (by linarith:0<=1-c) linarith obtain ⟨l1,l2⟩:=qHl hT0 hs0 hc0 hc1 hEm0 h1' h2' hhi hlo refine ⟨l1,l2,?_⟩ rw[Real.sqrt_lt' (by positivity),show ((2:ℝ)^(127:ℤ))^2=2^254 by norm_num] have a1:=Х (hsq.trans_le ht2) (by linarith:1+c<=2) ד ב linarith lemma qE1 (b:ј) (rows n:ℕ) (eps:њ) (x:ё) (r:ℕ) (hr:r< rows) (hn0:0< n) (hn:n<2^20) (hs:ѡx=rows*n) (hrn:rows*n<2^32) (hx:x.all sm=true) (he:sm eps=true) (he0:0< Д eps) : L_ok b n eps x r (lnStat b n eps x r):=by have hmul:r*n+n<=rows*n:=by have:=Ь n hr;rwa [Nat.succ_mul] at this generalize hgd:(λj:ℕ=>dv x[r*n+j]!)=g;have hg0:∀ j,j< n->(g j).isFinite=true∧|R (g j)| ≤ 2^99 ∧ R (g j)=(Д x[r*n+j]! : ℝ):=λj h=>by obtain ⟨a1,a2⟩:=qA1 _ (qBg x sm hx (r*n+j) א);obtain ⟨a3,a4⟩:=qAm _ a1;rw[<-hgd];beta_reduce;rw[a4];exact ⟨a3,a2,qAx _ (qAe _ a1)⟩ have hnR1:(1:ℝ)<=n:=by exact_mod_cast hn0 have hnR2:(n:ℝ)<=2^20:=by exact_mod_cast hn.le have o4:((З n).map λj=>|R (g j)|).sum<=n*2^99 :=qDr n _ _ λj h=>(hg0 j h).2.1;have hA2:((З n).map λj=>|R (g j)|).sum<=A_B/4:=by have:=qAt (k:=2^256*2^44) le_rfl;have:=Б hnR2 (by norm_num:(0:ℝ)<=2^99);linarith obtain ⟨o1,o5⟩:=qFm n hn g (λj=>R (g j)) (λj=>|R (g j)|) (λj h=>⟨(hg0 j h).1,rfl,rfl⟩) hA2 obtain ⟨o2,o6⟩:=qFm n hn (λj=>(g j).abs) (λj=>|R (g j)|) (λj=>|R (g j)|) (λj h=>by obtain ⟨p,q⟩:=qBl _ (hg0 j h).1 exact ⟨p,q,abs_abs _⟩) hA2 have o3:=qF2 (З n) λj=>R (g j);obtain ⟨hMc,hVc⟩:=qHi n r x g (λj h=>(hg0 j h).2.2) (lnQ b n x r);generalize hQ:((lnQ b n x r:ℚ):ℝ)=Q at hVc;generalize hS:((З n).map λj=>R (g j)).sum=S at o3 o5 hMc;generalize ((З n).map λj=>|R (g j)|).sum=Sa at o3 o4 o5 o6;unfold lnStat;dsimp only;generalize hnu:Ѣn=nu;generalize hbase:(r*n).toUSize=base;generalize hnd:n.toUInt64.toFloat=nd;generalize hnf:(n.toUInt64+8).toFloat*О ь=nf;generalize hind:(1.0:ѕ)/nd=ind;generalize ha:(if b=true then F1.mk 0 0 else lnSum x base nu 0 0 0)=a;generalize hm:(if b=true then (0:ѕ) else a.s*ind)=m;generalize hv:lnVar x base nu 0 m 0=v;generalize hem:nf*a.t*ind+О ظ=em;generalize htt:v*ind+dv eps=tt;generalize hd:м tt=d;generalize hz:ziv d ((nf+nf)*d+em)=z;generalize hir:(1.0:ѕ)/dv z=ir;split . rename_i hc;simp only[Bool.and_eq_true,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨c1,c2⟩,c3⟩,c4⟩,c5⟩,c6⟩:=hc;have hnp:(n:ℝ)≠0 :=ד;have hzb:=qBo;have hz0:=qAr.le;have hu:Ѡnu=n:=by rw[<-hnu];exact qAd n א have hb:Ѡbase=r*n:=by rw[<-hbase];exact qAd _ א have hxs:ѡx<2^32 :=א;obtain ⟨fnf,Rnf⟩:=hnf ▸ qIi n hn;obtain ⟨c,hc⟩:∃ c:ℝ,c=((n:ℝ)+8)/2^49:=⟨_,rfl⟩;rw[<-hc] at Rnf;have hc1:c<=1/2^20:=by rw[hc];linarith have hc2:9/2^49<=c:=by rw[hc];linarith obtain ⟨fnd,Rnd⟩:nd.isFinite=true∧R nd=n:=by rw[<-hnd];have e:n.toUInt64.toNat=n:=by simp;omega have:=qCk n.toUInt64 א;rwa [e] at this obtain ⟨fi,pi,-,ei⟩:=qFy ind nd fnd (by rw[Rnd,И ד];exact hnR2.trans ב) c5 rw[Rnd] at ei;obtain ⟨z1,z2⟩:=qHg obtain ⟨fm,fE,m2,m3,m1,m4⟩:m.isFinite=true∧em.isFinite=true∧R em<=2^120 ∧ 1/2^201<=R em∧2 * |Q-R m| ≤ R em∧|R m| ≤ 2^110:=by cases b with | true => have e1:a=F1.mk 0 0:=ha.symm;have e2:m=0:=hm.symm;have e3:Q=0:=by rw[<-hQ];simp[lnQ] subst e1;obtain ⟨-,fE,m2,m3,-,-⟩:=qG1 n hnR1 hnR2 0 0 ind nf _ em 0 0 c hc z1 z1 ג (by positivity) (by rw[z2];simp;positivity) (by rw[z2];simp;positivity) fnf Rnf fi pi ei rfl hem;rw[e2,e3,z2];refine ⟨z1,fE,m2,m3,?_,by norm_num⟩;simp only[sub_zero,abs_zero,mul_zero];exact ش ב m3 | false => replace ha:lnSum x base nu 0 0 0=a:=ha;replace hm:a.s*ind=m:=hm;have hA:a=⟨A_fsum ((З n).map g),A_fsum ((З n).map λj=>(g j).abs)⟩:=by rw[<-ha,qG9 x hxs base nu א n 0 0 0 (by simp[hu])];simp only[USize.toNat_zero,hb,<-hgd,A_fsum,List.range_eq_range'] subst hA;rw[hQ.symm.trans hMc];exact qG1 n hnR1 hnR2 _ _ ind nf m em S Sa c hc o1 o2 o3 o4 o5 o6 fnf Rnf fi pi ei hm hem obtain ⟨p1,p2,p3,hW0⟩:=qGp n hn hn0 g m v _ (by rw[<-hv,qG2 x hxs base nu m א n 0 0 (by simp[hu])] simp only[USize.toNat_zero,hb,<-hgd,A_fsum,List.range_eq_range']) rfl (λj h=>⟨(hg0 j h).1,(hg0 j h).2.1⟩) fm m4 generalize hW:((З n).map λj=>(R (g j)-R m)^2).sum=W at p2 p3 hW0;generalize hV0:((З n).map λj=>(R (g j)-Q)^2).sum=V0 at hVc;generalize hδ:Q-R m=δ at m1;have hnpos:(0:ℝ)< n :=ד have hWs:W/n=V0/n+δ^2:=by cases b with | true => have e2:m=0:=hm.symm;have e3:Q=0:=by rw[<-hQ];simp[lnQ] rw[<-hδ,<-hW,<-hV0,e2,e3,z2];simp | false => have hs2:=qGh (З n) (λj=>R (g j)) (R m) (by simpa using hnp);simp only[List.length_range] at hs2;rw[hS,hW,show S/n=Q from (hQ.symm.trans hMc).symm,hV0,hδ] at hs2;rw[hs2];field_simp obtain ⟨gE,bE⟩:=qA1 eps he;have epsv:wv eps=(Д eps:ℝ):=qAx _ (qAe _ gE);have hep0:0< wv eps:=by rw[epsv];exact_mod_cast he0 obtain ⟨ft,hq1⟩:=qIv n hnR1 hnR2 v ind tt eps W c hc p1 p2 p3 hW0 fi pi ei gE bE hep0 htt;generalize hq:rowVariance n x r (lnQ b n x r)+Д eps=q;have hV1:0<=V0/n:=by rw[<-hV0];refine div_nonneg (List.sum_nonneg λy hy=>?_) hnpos.le;obtain ⟨j,-,rfl⟩:=List.mem_map.1 hy;exact ط _ have hT:(q:ℝ)=(W/n+wv eps)-δ^2:=by rw[<-hq,Rat.cast_add,hVc,epsv,hWs];ring have hT0:(0:ℝ)< q:=by rw[hT,hWs];linarith have hP0:0< W/n+wv eps:=by have:=div_nonneg hW0 hnpos.le;linarith obtain ⟨r1,r2⟩:1<=_∧_<1+1222:=qEj _ 1 1222 c4 (by decide);obtain ⟨l1,l2,hb127⟩:=qIk tt nf em _ r1 א rfl c _ δ (q:ℝ) hc1 hc2 fnf Rnf fE m2 m3 hP0 hq1 m1 hT hT0;rw[hd] at l1 l2;have hq0:0< q:=by exact_mod_cast hT0 have hzne:ziv d ((nf+nf)*d+em)≠Е:=by rw[hz];intro e0;rw[e0] at c3;exact absurd c3 (by decide) have hden:=((qGs d _ q hq0 l1 l2 hzne hb127).trans hz).symm;rw[<-hq] at hden;obtain ⟨gZ,bZ⟩:=qA1 z c1;obtain ⟨dz1,dz2⟩:=qAm z gZ;obtain ⟨fi2,-,-,ei2⟩:=qFy ir (dv z) dz1 (by rw[dz2];exact bZ) c6;rw[dz2] at ei2;refine ⟨hden,λ_=>⟨fm,fi2,fE,m2,m3,?_,m4,gZ,qIb z gZ c2 c3,ei2⟩⟩;dsimp only;rw[hQ,hδ];exact m1 . exact ⟨rfl,by simp[L_oF]⟩ lemma qI0 (m ir em:ѕ) (w gw bw:њ) (M D:ℝ) (fm:m.isFinite=true) (fi:ir.isFinite=true) (fe:em.isFinite=true) (e1:R em<=2^120) (e2:1/2^201<=R em) (e3:2 * |M-R m| ≤ R em) (e4 : |R m| ≤ 2^110) (hD:1/2^64<=D) (hI : |R ir*D-1| ≤ 1/2^48) (hw:sm w=true) (hg:sm gw=true) (hb:sm bw=true) (s e:ѕ) (hs:s=(dv w-m)*(dv gw*ir)+dv bw) (he:e=О 0x3D10000000000000*(((dv w-m)*(dv gw*ir)).abs+(dv bw).abs) + em*(dv gw*ir).abs) (hne:ziv s e≠Е) : R (s-e)<(wv w-M)*wv gw/D+wv bw∧(wv w-M)*wv gw/D+wv bw< R (s+e):=by have hzb:=qBo obtain ⟨gw1,bw1⟩:=qA1 w hw obtain ⟨gg1,bg1⟩:=qA1 gw hg obtain ⟨gb1,bb1⟩:=qA1 bw hb obtain ⟨fw,Rw⟩:=qAm w gw1 obtain ⟨fg,Rg⟩:=qAm gw gg1 obtain ⟨fb,Rb⟩:=qAm bw gb1 have hD0:0< D:=н ב hD have bI : |R ir| ≤ 2^65:=by have a1:=qAa (R ir*D) 1 rw[abs_one,в,б hD0] at a1 have a2:=Г hD (Л (R ir)) linarith obtain ⟨f1,h1,b1⟩:=qCg (dv w) m (2^111) fw fm (by rw[Rw];exact bw1.trans ב) (e4.trans ב) rw[Rw] at h1 obtain ⟨f2,h3,b2⟩:=qAl (dv gw) ir (2^99) (2^65) fg fi (by rw[Rg];exact bg1) bI rw[Rg] at h3 obtain ⟨f3,h4,b3⟩:=qAl _ _ (2^113) (2^166) f1 f2 ד ד generalize hQ:(dv w-m)*(dv gw*ir)=Q at hs he f3 h4 b3 generalize dv gw*ir=G at he f2 h3 b2 h4 hQ obtain ⟨f4,h5,b4⟩:=qAv Q (dv bw) (2^250*2^31) f3 fb (by norm_num at b3 hzb ⊢;linarith) (by rw[Rb];exact bb1.trans ב) rw[Rb,<-hs] at h5 rw[<-hs] at f4 b4 obtain ⟨fa1,Ra1⟩:=qBl Q f3 obtain ⟨fa2,Ra2⟩:=qBl (dv bw) fb rw[Rb] at Ra2 obtain ⟨fa3,Ra3⟩:=qBl G f2 obtain ⟨f5,h6,b5⟩:=qAv Q.abs (dv bw).abs (2^250*2^31) fa1 fa2 (by rw[Ra1,abs_abs];norm_num at b3 hzb ⊢;linarith) (by rw[Ra2,abs_abs];exact bb1.trans ב) rw[Ra1,Ra2] at h6 obtain ⟨k1,k2⟩:=L_k 0x3D10000000000000 977 46 ב rfl (by rfl) obtain ⟨f6,h7,b6⟩:=qAl _ _ 1 (2^250*2^33) k1 f5 (by rw[k2];norm_num) (by norm_num at b5 hzb ⊢;linarith) rw[k2] at h7 have bE : |R em| ≤ 2^120:=by rw[И (ش ב e2)];exact e1 obtain ⟨f7,h8,b7⟩:=qAl em G.abs (2^120) (2^166) fe fa3 bE (by rw[Ra3,abs_abs];linarith) rw[Ra3] at h8 obtain ⟨f8,h9,b8⟩:=qAv _ _ (2^250*2^38) f6 f7 (by norm_num at b6 hzb ⊢;linarith) (by norm_num at b7 hzb ⊢;linarith) rw[<-he] at f8 h9 b8 obtain ⟨f9,h10,-⟩:=qAv s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨f10,h11,-⟩:=qCg s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨-,-,big⟩:=qBw s e hne rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at big exact qG0 qAr.le qAw (ش ב e2) e1 e3 hD bw1 bg1 e4 h1 hI h3 h4 h5 h6 h7 h8 h9 h10 h11 big lemma qEk (s:LnS) (M:ℚ) (mean:Unit->ℚ) (hmean:mean ()=M) (w gw bw:њ) (hw:sm w=true) (hg:sm gw=true) (hb:sm bw=true) (h:L_oF s (M:ℝ)) : lnOut s w gw bw mean =roundRational ((Д w-M)*Д gw/Д s.den+Д bw):=by unfold lnOut lnOutF split . rename_i hf;obtain ⟨fm,fi,fe,e1,e2,e3,e4,gd,hD,hI⟩:=h hf;dsimp only;split;. unfold lnSlow;rw[hmean] . rename_i hc have hne:ziv ((dv w-s.m)*(dv gw*s.ir)+dv bw) (О 0x3D10000000000000*(((dv w-s.m)*(dv gw*s.ir)).abs+(dv bw).abs) + s.em*(dv gw*s.ir).abs)≠Е:=λe0=>by rw[e0] at hc exact hc (by decide) obtain ⟨l1,l2⟩:=qI0 s.m s.ir s.em w gw bw M (wv s.den) fm fi fe e1 e2 e3 e4 hD hI hw hg hb _ _ rfl rfl hne have ew:=qAx w (qAe _ (qA1 w hw).1);have eg:=qAx gw (qAe _ (qA1 gw hg).1);have eb:=qAx bw (qAe _ (qA1 bw hb).1);have ed:=qAx s.den (qAe _ gd);have hV:(((Д w-M)*Д gw/Д s.den+Д bw:ℚ):ℝ) =(wv w-M)*wv gw/wv s.den+wv bw:=by rw[ew,eg,eb,ed];push_cast;ring rw[<-hV] at l1 l2;exact (qH7 _ _ _ l1 l2 hne (qIu _ _ _ l1 l2 hne hc)).symm . unfold lnSlow;rw[hmean] lemma qBp {α:Type} [Inhabited α] (rows k:ℕ) (f:ℕ->α) (hk:k< rows) : (Array.ofFn (n:=rows) λr=>f r.val)[k]! = f k:=by rw[Я _ k (by simpa using hk),Array.getElem_ofFn] lemma qMf (f:ћ->њ) (L:љ) (a:ℤ): L.foldl (λt j=>t+Д (f j)) ((a:ℚ)/2^149)=((L.foldl (λt j=>t+sv (f j)*((2^idxOf (f j):ℕ):ℤ)) a:ℤ):ℚ)/2^149:=by induction L generalizing a with | nil=>rfl | cons p L ih=> simp only[List.foldl_cons] rw[<-ih] congr 1 have e:Д (f p)=(sv (f p):ℚ)*2^((idxOf (f p):ℤ)-149):=by rw[rationalWord,decodeD_toRat] rw[e,zpow_sub₀ (by norm_num:(2:ℚ)≠0),zpow_natCast] push_cast ring lemma qMq (n:ћ) (x:ё) (r:ћ):meanQ n x r=rowMean n x r:=by have h:=qMf (λj=>x[r*n+j]!) (З n) 0 simp only[Int.cast_zero,zero_div] at h unfold meanQ meanI rowMean rw[h] by_cases c:(n:ℚ)=0 . simp[c] . push_cast;rw[div_div];norm_num lemma qGD {A:ё} {n:є} (hs:ѡA<2^32) (d:ћ) : ∀ (t:є) (o:ђ),Ѡt+d=Ѡn→(Ѡn<=ѡo∧ѡo<2^32)→(∀ k,k< Ѡt->o.data[k]! = dv A[k]!) → (Ѡn<=(decG A n t o).size∧(decG A n t o).size<2^32)∧∀ k,k< Ѡn->(decG A n t o).data[k]! = dv A[k]!:=by induction d with | zero=> intro t o he H1 H2 rw[decG,Ш (by rw[А];omega)] have e:Ѡt=Ѡn:=א rw[e] at H2 exact ⟨H1,H2⟩ | succ d ih=> intro t o he H1 H2 have ht:t< n:=by rw[А];omega have hu:=usz t n ht have r0:rdW A t=A[Ѡt]!:=qAg _ _ hs rw[decG,П ht,r0] refine ih (t+1) _ (by rw[hu];omega) ⟨by rw[qD6];exact H1.1,by rw[qD6];exact H1.2⟩ ?_ intro k hk rw[hu] at hk rw[qEt _ _ _ _ H1.2 (by have:=H1.1;have:=А.mp ht;omega)] by_cases e:k=Ѡt . rw[if_pos e,e] . rw[if_neg e];exact H2 k (by omega) lemma qGR {A:ё} {n:ћ} (hs:ѡA<2^32) (hn:n<2^32) (u:є) (hu:Ѡu< n):rdF (decG A Ѣn 0 (zFor n)) u=dv A[Ѡu]!:=by have ek:n.toUSize.toNat=n:=qAd _ א have P0:=qZf n hn obtain ⟨⟨D1,D2⟩,D3⟩:=qGD (A:=A) (n:=Ѣn) hs n 0 (zFor n) (by rw[ek,C_z,Т]) (by rw[ek];exact P0) (λk hk=>by rw[C_z] at hk;omega) rw[ek] at D1 D3 generalize decG A Ѣn 0 (zFor n)=a at D1 D2 D3 ⊢ have h:u< a.usize:=by rw[А];have e:a.usize.toNat=ѡa:=by show (a.size.toUSize).toNat=ѡa exact qAd _ D2 rw[e];omega unfold rdF rw[П h,qC5,D3 _ hu] lemma qGS {r:ћ} {A:ё} {n:ћ} (hs:ѡA<2^32) (hn:n<2^32) (u:є) (hu:Ѡu< n):gdR r A (gdF r A n) u=dv A[Ѡu]!:=by unfold gdR gdF by_cases c:(r == 1)=true . rw[if_pos c,qAg _ _ hs] . rw[if_neg c,if_neg c];exact qGR hs hn u hu lemma qSm (A:ё) (hs:ѡA<2^32) (h:smA A=true):A.all sm=true∧A.all finiteWord=true:=by unfold smA at h dsimp only at h rw[Bool.and_eq_true] at h obtain ⟨h1,h2⟩:=h have hh:=of_decide_eq_true h2 obtain ⟨-,-,c3,c4⟩:=qBi A hs (by simpa using h1) constructor . rw[Array.all_eq_true] intro i hi have g:=c3 i hi have l:=c4 i hi rw[Я A i hi] at g l unfold sm rw[Bool.and_eq_true] exact ⟨g,decide_eq_true (UInt32.le_trans l hh)⟩ . rw[Array.all_eq_true] intro i hi have g:=c3 i hi rw[Я A i hi] at g simp[finiteWord,qAe _ g] lemma qEq0 (w:ѓ) (h1:signOf w=false) (h2:0< mantOf w):0lnOut (Array.ofFn (n:=rows) λr=>lnStat false n eps x r.val)[r]! x[q]! g[c]! b[c]! λ_=>rowMean n x r) h9 h11 λr p u hr hp hu=>by rw[qGS א א u hu,qGS א א u hu] repeat rw[qAg _ _ א] rfl] apply Array.ext ג;intro q h1 h1';simp only[Array.getElem_ofFn,Array.size_ofFn] at h1 h1' ⊢;have hr:q/n< rows:=ш (by rwa [е]);have hj:q%n< n:=ر _ h11;have e1:=qBp rows (q/n) (λr=>lnStat false n eps x r) hr;have e2:=qBp rows (q/n) (λr=>rowMean n x r) hr have e3:=qBp rows (q/n) (λr=>sqrtRational (rowVariance n x r ((Array.ofFn (n:=rows) λr=>rowMean n x r.val)[r]!)+Д eps)) hr beta_reduce at e1 e2 e3;rw[e1,e3,e2];obtain ⟨o1,o2⟩:=qE1 false rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qEk _ (rowMean n x (q/n)) _ rfl _ _ _ (qBg x sm h4 q א) (qBg g sm h5 _ א) (qBg b sm h6 _ א) o2,o1];rfl . rfl lemma qFf:rmsF=rmsnormSpec:=by funext rows n eps x g;unfold rmsF;split . rename_i hc;obtain ⟨hD,h4,h5,h7,h8,h9⟩:=qNO hc;unfold rmsnormSpec rmsnormWith;rw[if_pos hD];unfold rmsnormStable;dsimp only;unfold stableNormDomain at hD;simp only[Bool.and_eq_true,beq_iff_eq,gt_iff_lt,decide_eq_true_eq] at hD;obtain ⟨⟨⟨⟨⟨⟨⟨h10,h11⟩,h12⟩,h13⟩,-⟩,-⟩,-⟩,h14⟩:=hD rw[qDb (λr c q=>if x[q]! &&& В == 0 then 0 else lnOut (Array.ofFn (n:=rows) λr=>lnStat true n eps x r.val)[r]! x[q]! g[c]! 0 λ_=>0) h9 h11 λr p u hr hp hu=>by rw[qGS א א u hu] repeat rw[qAg _ _ א] rfl] apply Array.ext ג;intro q h1 h1';simp only[Array.getElem_ofFn,Array.size_ofFn] at h1 h1' ⊢;have hr:q/n< rows:=ш (by rwa [е]);have hj:q%n< n:=ر _ h11;have e1:=qBp rows (q/n) (λr=>lnStat true n eps x r) hr;have e3:=qBp rows (q/n) (λr=>sqrtRational (rowVariance n x r 0+Д eps)) hr;beta_reduce at e1 e3;rw[e3];have hw:=qBg x sm h4 q א;split;. rename_i h0;rw[qE6 _ (qA1 _ hw).1 (by simpa using h0),zero_mul,zero_div];rfl . rw[e1];obtain ⟨o1,o2⟩:=qE1 true rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qEk _ 0 _ rfl _ _ _ hw (qBg g sm h5 _ א) (by decide) o2,o1,qE6 0 (by decide) rfl,sub_zero,add_zero];rfl . rfl end def kernels:Kernels where core:=specCore dense:=⟨linF,gemmSpec⟩ conv:=specConv elem:=⟨avgpoolSpec,maxpoolSpec,addReluSpec,scaleShiftSpec⟩ norm:=⟨rmsF,lnF⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specDense,specElem,specNorm,avgpoolSpec,qDz,qGc,qFf,qAi] end Submission