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"Ж"=>abs_of_nonneg local notation"З"=>UInt32.le_iff_toNat_le local notation"И"=>zpow_le_zpow_right₀ local notation"Й"=>abs_nonneg local notation"К"=>Nat.mod_eq_of_lt local notation"Л"=>Nat.and_two_pow_sub_one_eq_mod local notation"М"=>mul_nonneg local notation"Н"=>Array.replicate local notation"О"=>List.range local notation"П"=>dite_eq_right local notation"Р"=>dite_eq_left local notation"С"=>Float.ofBits local notation"Т"=>two_ne_zero local notation"У"=>Nat.cast_nonneg local notation"Ф"=>Nat.mul_le_mul_right local notation"Х"=>Nat.zero_add local notation"Ц"=>ite_eq_right local notation"Ч"=>mul_le_mul local notation"Ш"=>Model.roundDyadic local notation"Щ"=>zpow_add₀ local notation"Ъ"=>abs_add_le local notation"Ы"=>0x7F800000 local notation"Ь"=>Model.isFinite local notation"Э"=>abs_sub_comm local notation"Ю"=>getElem!_pos local notation"Я"=>UInt32.toNat_inj local notation"а"=>UInt64.toNat_add local notation"б"=>abs_of_pos local notation"в"=>abs_mul local notation"г"=>FloatArray.emptyWithCapacity local notation"д"=>mul_lt_mul_of_pos_right local notation"е"=>List.mem_range local notation"ж"=>Nat.mul_comm local notation"з"=>zpow_pos local notation"и"=>Model.toReal_eq_unpackedToReal_toModel local notation"й"=>UInt64.toNat_shiftRight local notation"к"=>Real.sqrt local notation"л"=>UInt64.lt_iff_toNat_lt local notation"м"=>Finset.range local notation"н"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"о"=>Nat.one_mul local notation"п"=>zpow_natCast local notation"р"=>Model.toReal local notation"с"=>Float.sqrt local notation"т"=>lt_of_lt_of_le local notation"у"=>UInt32.toNat_and local notation"ф"=>ite_eq_left local notation"х"=>FloatLib.Numerics.binaryRadix local notation"ц"=>Nat.add_assoc local notation"ч"=>toDyadic_ofUInt32 local notation"ш"=>lt_of_le_of_lt local notation"щ"=>Nat.add_mul local notation"ъ"=>Nat.mod_lt local notation"ы"=>Nat.shiftRight_eq_div_pow local notation"ь"=>le_trans local notation"э"=>pow_le_pow_left₀ local notation"ю"=>0x3CE0000000000000 local notation"я"=>Model.modelSignBit local notation"ذ"=>Nat.le_mul_of_pos_right local notation"ر"=>UInt32.lt_iff_toNat_lt local notation"ز"=>Float.Model.UnpackedFloat.unpack local notation"س"=>Finset.sum_congr local notation"ش"=>Nat.shiftLeft_eq local notation"ص"=>UInt64.toNat_and local notation"ض"=>UInt64.toNat_sub local notation"ط"=>0x80000000 local notation"ظ"=>Nat.div_lt_of_lt_mul local notation"ع"=>Nat.not_lt_zero local notation"غ"=>USize.toNat_one local notation"ػ"=>Nat.add_zero local notation"ؼ"=>sq_nonneg local notation"ؽ"=>0x3370000000000000 local notation"ؾ"=>Model.toModel_ofFloatModel local notation"ؿ"=>Finset.mem_range local notation"ـ"=>Nat.div_eq_of_lt local notation"ف"=>abs_sub local notation"ق"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"ك"=>getElem!_neg local notation"ل"=>UInt32.toNat_sub_of_le local notation"م"=>List.mem_cons_of_mem local notation"ن"=>List.map_congr_left local notation"ه"=>UInt32.toNat_ofNat' local notation"و"=>List.mem_cons_self local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith section open FloatLib.Numerics end lemma qAb (n:Nat) (f:Nat->UInt32) (k:Nat) (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 qEc (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 qHj {w:UInt32} (h:expOf w≠255):Ь (ofW w)=true:=by simp[ofW,isFinite_ofUInt32,h] lemma qD2 (A B:Array UInt32) (hB:B.size=A.size) (hall:∀ k,k< A.size->expOf A[k]!≠255∧expOf B[k]!≠255) : finishWord (dsum A B 0 A.size) (-298) (A.size≠0 && dallNZ A B 0 A.size)=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.size=R.size:=by simp[hL,hR,hB] have hS:L.size=A.size:=by simp[hL] have hLf:∀ k (hk:k< L.size),Ь L[k]=true:=λk hk=>by simpa [hL,ofW] using qHj (getElem!_pos A k א ▸ (hall k א).1) have hRf:∀ k (hk:k< R.size),Ь R[k]=true:=λk hk=>by simpa [hR,ofW] using qHj (getElem!_pos B k א ▸ (hall k א).2) have f k (_:0<=k) (h:k<0+L.size):=hall k א;have s:=(finiteDotSlice_eq A B 0 L.size (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.size=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.size (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:Array Nat) (special negZero:Bool) def Acc.total (acc:Acc):Int:=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:Nat) (neg:Bool):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:Nat) (neg:Bool) (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:UInt32):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:Nat->UInt32) (pos neg:Array Nat) (sp nz:Bool) (index:Nat):Nat->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 qB7 (fa fb:Nat->UInt32) (pos neg:Array Nat) (nz:Bool) (index count:Nat):(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 qDi (fa fb:Nat->UInt32) (A B:Array UInt32) (n:Nat) (hA:∀ k,k< n->A[k]! = fa k) (hB:∀ k,k< n->B[k]! = fb k) (acc:Acc) (index remaining:Nat) (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[qB7] 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):Nat):Int) else ((mantOf (fa index)*mantOf (fb index):Nat):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:Array Nat) (k:Nat):Nat->Nat | 0=>k | f+1=>if p[k]! = 0 && q[k]! = 0 then Y_low p q (k+1) f else k lemma qEb (p q:Array Nat):∀ 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 qHh (L:List Nat):∀ 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 qFp (A:Array Nat) (l:Nat) (h:∀ j,j< l->A[j]! = 0) : limbVal A.toList=2^(32*l)*limbValA (A.extract l A.size):=by rw[limbValA_eq,Array.toList_extract,List.extract_eq_drop_take,<-Array.length_toList];rw[List.take_of_length_le ג];refine qHh _ 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 qI8 (S:Int) (l:Nat) (e:Int) (z:Bool) : 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:Nat) (fa fb:Nat->UInt32):UInt32 := 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 qAo: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:=qDi 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.size=n:=by simp[hA] have hBs:B.size=n:=by simp[hB] have hL:=qEb 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:=qFp acc.pos l (λj hj=>(hL j hj).1);have h2:=qFp acc.neg l (λj hj=>(hL j hj).2);rw[hP] at h1;rw[hN] at h2;rw[qI8,<-qD2 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:USize) (h:t< n):(t+1).toNat=t.toNat+1:=by have h1:t.toNat< n.toNat:=А.mp h;have h2:n.toNat< USize.size:=n.toNat_lt_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add,غ];exact К א lemma qAf (a b:USize) (h:a.toNat+b.toNat<2^32):(a+b).toNat=a.toNat+b.toNat:=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:Nat) (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 qDq (a:FloatArray) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[FloatArray.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega lemma qBe (a:Array UInt32) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega @[inline] def rdW (a:Array UInt32) (i:USize):UInt32 := if h:i< a.usize then a.uget i (qBe a i h) else 0 @[inline] def wrF (a:FloatArray) (i:USize) (v:Float):FloatArray := if h:i< a.usize then a.uset i v (qDq a i h) else a @[inline] def wrW (a:Array UInt32) (i:USize) (v:UInt32):Array UInt32 := if h:i< a.usize then a.uset i v (qBe a i h) else a def pwEntry (i:Nat):Float := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then С s else С (s ||| ((e+873) <<< (52:UInt64))) def pw:FloatArray := (О 512).foldl (λa i=>a.push (pwEntry i)) (г 512) lemma qDw:pw.data=(О 512).toArray.map pwEntry:=by have key:∀ (l:List Nat) (a:FloatArray),(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 qFx:pw.size=512:=by simp[FloatArray.size,qDw] lemma qDk (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pw.size:=by rw[qFx];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega @[inline] def dv (w:UInt32):Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:UInt32)).toUSize (qDk w) @[inline] def dvB (v:Float) (w:UInt32):Float := (v*pw.uget (w >>> (23:UInt32)).toUSize (qDk w))*((w &&& 0x7FFFFF) ||| 0x800000).toFloat @[inline] def good (w:UInt32):Bool := let v:=w &&& В v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32) def scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):UInt32 := 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.toNat-i.toNat decreasing_by rw[usz i n h];omega def scanF (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (o s:UInt32):UInt32 := 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.toNat-i.toNat decreasing_by rw[usz i n h];omega lemma qAc (a:Array UInt32):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ @[inline] def scanH (r:Nat) (A:Array UInt32):UInt32 := let h:=scanF A A.usize 0 (qAc A) 0 Е if h == Е then scan A A.usize 0 (qAc A) Е 0 else h @[inline] def scanW (r:Nat) (A:Array UInt32):UInt32 := if r == 8 then 0 else scanH r A def pushZ (n k:USize) (a:FloatArray):FloatArray := if h:k< n then pushZ n (k+1) (a.push 0) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega @[inline] def zivW (A B:UInt64):UInt32 := let x:UInt64:=(A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let y:UInt64:=(B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let ex:UInt64:=(x >>> (23:UInt64)) &&& (0x7FF:UInt64) if x == y && ex-(897:UInt64)<(254:UInt64) then (((x >>> (34:UInt64)) <<< (31:UInt64)) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64)))).toUInt32 else Е @[inline] def zivS (s:Float):UInt32 := let a:=s.toBits zivW (a+2) (a-2) @[inline] def ziv (s e:Float):UInt32 := zivW (s+e).toBits (s-e).toBits @[noinline] def slowOut (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat):Word := 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:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf t:Float) (bw:UInt32):UInt32 := 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:Float) def tightGo (X W:Array UInt32) (xo wo n t:USize) (s a:Float):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.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega @[noinline] def fin8 (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf a:Float) (hx bw:UInt32) (n:USize):UInt32 := 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 act relu r @[inline] def zivT (th:UInt64) (s:Float):UInt32 := let a:=s.toBits if th<=a <<< (1:UInt64) then zivW (a+0x100000) (a-0x100000) else Е def thOf (e:Float):UInt64 := let E:=(e.toBits >>> (52:UInt64)) &&& 0x7FF if E-1<1900 then (E+37) <<< (53:UInt64) else 0xFFFFFFFFFFFFFFFF @[inline] def finT (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf a:Float) (hx bw:UInt32) (n:USize) (th:UInt64):UInt32 := let r:=zivT th (s+dv bw) if r == Е then fin8 inW relu X W b i j s nf a hx bw n else act relu r structure R1 where (ix:Array UInt32) (buf:FloatArray) (c:USize) (l:Float) @[inline] def mx (a w:UInt32):UInt32 := let m:=w &&& В if m<=a then a else m def decR (X:Array UInt32) (o n t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (l:Float):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.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];have:=А.mp h;omega) lemma wi (w i m:USize) (W:Array UInt32) (hw:w.toNat+m.toNat<=W.size) (hs:W.size<2^32) (hi:i.toNat< m.toNat):(w+i).toNat< W.size:=by rw[qAf _ _ א];omega structure F4 where (a b c d:Float) def go4B (x:FloatArray) (ix W:Array UInt32) (w0 w1 w2 w3 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) (a b c d:Float):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.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) def go1 (x:FloatArray) (ix W:Array UInt32) (w0 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (a:Float):Float := 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.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) lemma qC1 (a b:USize) (h:a.toNat*b.toNat<2^32) : (a*b).toNat=a.toNat*b.toNat:=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 wc (wd:FloatArray) (i O ju:USize) (m k:Nat) (hm:i.toNat< m) (hd:wd.size=m*O.toNat) (hs:wd.size<2^32) (hj:ju.toNat+k< O.toNat):(i*O+ju+USize.ofNat k).toNat< wd.size:=by have h1:(i.toNat+1)*O.toNat<=m*O.toNat:=Ф _ hm;rw[щ,о,<-hd] at h1;have e:=qC1 i O א;have e1:(i*O+ju).toNat=i.toNat*O.toNat+ju.toNat:=by rw[qAf _ _ (by rw[e];omega),e] have e2:=uk k (א:k<2^32);rw[qAf _ _ (by rw[e1,e2];omega),e1,e2];omega def go4T (x wd:FloatArray) (ix:Array UInt32) (ju O m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:wd.size<2^32) (hd:wd.size=m.toNat*O.toNat) (hj:ju.toNat+4<=O.toNat) (a b c d:Float):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 go4T x wd ix ju O m n (t+1) hx hi hs hd hj (a+v*wd.uget (i*O+ju+USize.ofNat 0) (wc wd i O ju _ 0 him hd hs א)) (b+v*wd.uget (i*O+ju+USize.ofNat 1) (wc wd i O ju _ 1 him hd hs א)) (c+v*wd.uget (i*O+ju+USize.ofNat 2) (wc wd i O ju _ 2 him hd hs א)) (d+v*wd.uget (i*O+ju+USize.ofNat 3) (wc wd i O ju _ 3 him hd hs א)) else go4T x wd ix ju O m n (t+1) hx hi hs hd hj a b c d else ⟨a,b,c,d⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) structure F8C where (u:UInt8) (s0 s1 s2 s3 s4 s5 s6 s7:Float) def go8C (x wd:FloatArray) (ix:Array UInt32) (ju O m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:wd.size<2^32) (hd:wd.size=m.toNat*O.toNat) (hj:ju.toNat+8<=O.toNat) (s0 s1 s2 s3 s4 s5 s6 s7:Float) (r:F8C):F8C := 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 go8C x wd ix ju O m n (t+1) hx hi hs hd hj (s0+v*wd.uget (i*O+ju+USize.ofNat 0) (wc wd i O ju _ 0 him hd hs א)) (s1+v*wd.uget (i*O+ju+USize.ofNat 1) (wc wd i O ju _ 1 him hd hs א)) (s2+v*wd.uget (i*O+ju+USize.ofNat 2) (wc wd i O ju _ 2 him hd hs א)) (s3+v*wd.uget (i*O+ju+USize.ofNat 3) (wc wd i O ju _ 3 him hd hs א)) (s4+v*wd.uget (i*O+ju+USize.ofNat 4) (wc wd i O ju _ 4 him hd hs א)) (s5+v*wd.uget (i*O+ju+USize.ofNat 5) (wc wd i O ju _ 5 him hd hs א)) (s6+v*wd.uget (i*O+ju+USize.ofNat 6) (wc wd i O ju _ 6 him hd hs א)) (s7+v*wd.uget (i*O+ju+USize.ofNat 7) (wc wd i O ju _ 7 him hd hs א)) r else go8C x wd ix ju O m n (t+1) hx hi hs hd hj s0 s1 s2 s3 s4 s5 s6 s7 r else match r with | ⟨u,_,_,_,_,_,_,_,_⟩=>⟨u,s0,s1,s2,s3,s4,s5,s6,s7⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) def wdG (W:Array UInt32) (o n O j t:USize) (a:FloatArray):FloatArray := if h:t< n then wdG W o n O j (t+1) (wrF a (t*O+j) (dv (rdW W (o+t)))) else a termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega def wdR (W:Array UInt32) (n O:USize) (e j:Nat) (a:FloatArray):FloatArray := if j< e then wdR W n O e (j+1) (wdG W (j.toUSize*n) n O j.toUSize 0 a) else a termination_by e-j lemma qAx {n:Nat} (h:n<=4294967295):n<2^32 :=א @[inline] def w4 (inW:Nat) (relu:Bool) (X W b:Array UInt32) (i k:Nat) (nf a:Float) (hx:UInt32) (m:USize) (th:UInt64) (ju base:USize) (s0 s1 s2 s3:Float) (out:Array UInt32):Array UInt32 := let o0:=finT inW relu X W b i k s0 nf a hx (rdW b ju) m th let o1:=finT inW relu X W b i (k+1) s1 nf a hx (rdW b (ju+1)) m th let o2:=finT inW relu X W b i (k+2) s2 nf a hx (rdW b (ju+2)) m th let o3:=finT inW relu X W b i (k+3) s3 nf a hx (rdW b (ju+3)) m th let u:=base+ju wrW (wrW (wrW (wrW out u o0) (u+1) o1) (u+2) o2) (u+3) o3 def cols1 (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (i:Nat) (ix:Array UInt32) (x wd:FloatArray) (nf a:Float) (hx:UInt32) (m n:USize) (j:Nat) (base:USize) (r0:F8C) (th:UInt64) (out:Array UInt32):Array UInt32 := if h8:j+8<=outW∧n.toNat<=x.size∧n.toNat<=ix.size∧wd.size<=4294967295∧wd.size=W.size ∧ wd.size=m.toNat*outW.toUSize.toNat∧j.toUSize.toNat+8<=outW.toUSize.toNat then let ju:=j.toUSize let r:=go8C x wd ix ju outW.toUSize m n 0 h8.2.1 h8.2.2.1 (qAx h8.2.2.2.1) h8.2.2.2.2.2.1 h8.2.2.2.2.2.2 0 0 0 0 0 0 0 0 r0 cols1 inW outW relu X W b i ix x wd nf a hx m n (j+8) base r th (w4 inW relu X W b i (j+4) nf a hx m th (ju+4) base r.s4 r.s5 r.s6 r.s7 (w4 inW relu X W b i j nf a hx m th ju base r.s0 r.s1 r.s2 r.s3 out)) else if h4:j+4<=outW then let ju:=j.toUSize let w0:=ju*m let w1:=(ju+1)*m let w2:=(ju+2)*m let w3:=(ju+3)*m if hg:n.toNat<=x.size∧n.toNat<=ix.size∧W.size<=4294967295∧w0.toNat+m.toNat<=W.size ∧ w1.toNat+m.toNat<=W.size∧w2.toNat+m.toNat<=W.size∧w3.toNat+m.toNat<=W.size then let r:=if hd:wd.size=W.size∧wd.size=m.toNat*outW.toUSize.toNat∧wd.size<=4294967295 ∧ ju.toNat+4<=outW.toUSize.toNat then go4T x wd ix ju outW.toUSize m n 0 hg.1 hg.2.1 (qAx hd.2.2.1) hd.2.1 hd.2.2.2 0 0 0 0 else go4B x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qAx 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 cols1 inW outW relu X W b i ix x wd nf a hx m n (j+4) base r0 th (w4 inW relu X W b i j nf a hx m th ju base r.a r.b r.c r.d out) else out else if hT:j< outW∧4<=outW∧n.toNat<=x.size∧n.toNat<=ix.size∧wd.size<=4294967295∧wd.size=W.size ∧ wd.size=m.toNat*outW.toUSize.toNat∧(outW-4).toUSize.toNat+4<=outW.toUSize.toNat then let k:=outW-4 let ju:=k.toUSize let r:=go4T x wd ix ju outW.toUSize m n 0 hT.2.2.1 hT.2.2.2.1 (qAx hT.2.2.2.2.1) hT.2.2.2.2.2.2.1 hT.2.2.2.2.2.2.2 0 0 0 0 w4 inW relu X W b i k nf a hx m th ju base r.a r.b r.c r.d out else if h1:j< outW then let ju:=j.toUSize let w0:=ju*m if hg:n.toNat<=x.size∧n.toNat<=ix.size∧W.size<=4294967295∧w0.toNat+m.toNat<=W.size then let r:=go1 x ix W w0 m n 0 hg.1 hg.2.1 (qAx hg.2.2.1) hg.2.2.2 0 let o:=finT inW relu X W b i j r nf a hx (rdW b ju) m th cols1 inW outW relu X W b i ix x wd nf a hx m n (j+1) base r0 th (wrW out (base+ju) o) else out else out termination_by outW-j def rows1 (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (wd:FloatArray) (nf:Float) (hv hc:UInt32) (m:USize) (i:Nat) (ix:Array UInt32) (buf:FloatArray) (out:Array UInt32):Array UInt32 := 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 ⟨0,0,0,0,0,0,0,0,0⟩ (thOf (nf*(dv hv*r.l+dv hc))) out rows1 rows inW outW relu X W b wd nf hv hc m (i+1) r.ix r.buf out else out termination_by rows-i structure F8 where (u:UInt8) (s0 s1 s2 s3 s4 s5 s6 s7 t:Float) (p q:UInt32) lemma xi (xs:FloatArray) (i t n:USize) (k:Nat) (hk:k<8) (ht:t.toNat< n.toNat) (hi:i.toNat=8*t.toNat) (hx:8*n.toNat<=xs.size) (hs:xs.size<2^32) : (i+USize.ofNat k).toNat< xs.size:=by rw[qAf _ _ (by rw[uk k א];omega),uk k א];omega def go8 (xs:FloatArray) (W:Array UInt32) (wo n t i:USize) (hx:8*n.toNat<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:wo.toNat+n.toNat<=W.size) (hi:i.toNat=8*t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a:Float) (p q:UInt32) (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:=dv w let m:=w &&& В go8 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+v*xs.uget (i+USize.ofNat 0) (xi xs i t n 0 א ht hi hx hsx)) (s1+v*xs.uget (i+USize.ofNat 1) (xi xs i t n 1 א ht hi hx hsx)) (s2+v*xs.uget (i+USize.ofNat 2) (xi xs i t n 2 א ht hi hx hsx)) (s3+v*xs.uget (i+USize.ofNat 3) (xi xs i t n 3 א ht hi hx hsx)) (s4+v*xs.uget (i+USize.ofNat 4) (xi xs i t n 4 א ht hi hx hsx)) (s5+v*xs.uget (i+USize.ofNat 5) (xi xs i t n 5 א ht hi hx hsx)) (s6+v*xs.uget (i+USize.ofNat 6) (xi xs i t n 6 א ht hi hx hsx)) (s7+v*xs.uget (i+USize.ofNat 7) (xi xs i t n 7 א ht hi hx hsx)) (a+v.abs) (p ||| m) (q &&& (m-1)) r else match r with | ⟨u,_,_,_,_,_,_,_,_,_,_,_⟩=>⟨u,s0,s1,s2,s3,s4,s5,s6,s7,a,p,q⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];omega structure H8 where (xs:FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7:UInt32) def dec8 (X:Array UInt32) (n t:USize) (xs:FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7:UInt32):H8 := if h:t< n then let w0:=rdW X t let w1:=rdW X (n+t) let w2:=rdW X (2*n+t) let w3:=rdW X (3*n+t) let w4:=rdW X (4*n+t) let w5:=rdW X (5*n+t) let w6:=rdW X (6*n+t) let w7:=rdW X (7*n+t) dec8 X n (t+1) ((((((((xs.push (dv w0)).push (dv w1)).push (dv w2)).push (dv w3)).push (dv w4)).push (dv w5)).push (dv w6)).push (dv w7)) (mx h0 w0) (mx h1 w1) (mx h2 w2) (mx h3 w3) (mx h4 w4) (mx h5 w5) (mx h6 w6) (mx h7 w7) else ⟨xs,h0,h1,h2,h3,h4,h5,h6,h7⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega structure C8 where (out:Array UInt32) (p q:UInt32) def cols8 (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (d:H8) (nf:Float) (n:USize) (j:Nat) (out:Array UInt32) (p q:UInt32) (r0:F8):C8 := if hj:j< outW then let ju:=j.toUSize let wo:=ju*n if hg:8*n.toNat<=d.xs.size∧d.xs.size<=4294967295∧W.size<=4294967295∧wo.toNat+n.toNat<=W.size then let r:=go8 d.xs W wo n 0 0 hg.1 (qAx hg.2.1) (qAx hg.2.2.1) hg.2.2.2 (by rw[show (0:USize).toNat=0 from rfl]) 0 0 0 0 0 0 0 0 0 p q r0 let bw:=rdW b ju let ow:=outW.toUSize let out:=wrW out ju (fin8 inW relu X W b 0 j r.s0 nf r.t d.h0 bw n) let out:=wrW out (ow+ju) (fin8 inW relu X W b 1 j r.s1 nf r.t d.h1 bw n) let out:=wrW out (2*ow+ju) (fin8 inW relu X W b 2 j r.s2 nf r.t d.h2 bw n) let out:=wrW out (3*ow+ju) (fin8 inW relu X W b 3 j r.s3 nf r.t d.h3 bw n) let out:=wrW out (4*ow+ju) (fin8 inW relu X W b 4 j r.s4 nf r.t d.h4 bw n) let out:=wrW out (5*ow+ju) (fin8 inW relu X W b 5 j r.s5 nf r.t d.h5 bw n) let out:=wrW out (6*ow+ju) (fin8 inW relu X W b 6 j r.s6 nf r.t d.h6 bw n) let out:=wrW out (7*ow+ju) (fin8 inW relu X W b 7 j r.s7 nf r.t d.h7 bw n) cols8 inW outW relu X W b d nf n (j+1) out r.p r.q r else ⟨out,p,q⟩ else ⟨out,p,q⟩ termination_by outW-j def linF (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32):Array UInt32 := let hw:=scanW rows W if rows*inW=X.size∧outW*inW=W.size∧outW=b.size∧0< inW∧0< outW∧inW<2^20 ∧ X.size<=4294967295∧W.size<=4294967295∧b.size<=4294967295∧rows*outW<=4294967295 ∧ scanH 1 X≠Е∧hw≠Е∧scanH 1 b≠Е then let n:=inW.toUSize let nf:Float:=(inW.toUInt64+8).toFloat*С 0x3CA0000000000000 let out:Array UInt32:=Н (rows*outW) 0 if rows == 8 then let c:=cols8 inW outW relu X W b (dec8 X n 0 (г (8*inW)) 0 0 0 0 0 0 0 0) nf n 0 out 0 Е ⟨0,0,0,0,0,0,0,0,0,0,0,0⟩ if (0x7FFFFF<=c.q∧c.p<Ы)∨scan W W.usize 0 (qAc W) Е 0≠Е then c.out else linearWith dotFn rows inW outW relu X W b else let wd:=if 8< rows then wdR W n outW.toUSize outW 0 (FloatArray.mk (Н W.size 0)) else г 0 rows1 rows inW outW relu X W b wd nf hw (scanH 1 b) 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:Float):ℝ:=р (toModel (ofFloat f)) noncomputable def wv (w:UInt32):ℝ:=р (ofW w) noncomputable def exactS (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32):ℝ := (м n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:Nat):Float:=(n.toUInt64+8).toFloat*С 0x3CA0000000000000 lemma qAi:⟡.isIEEE=true:=by decide lemma qA0 (a:Model ⟡) : Ь a=(Model.toModel a).isFinite:=by rw[<-Model.toDyadic?_isSome_eq_isFinite,Model.toDyadic?_ieee_eq_model _ qAi];cases Model.toModel a<;>rfl abbrev A_M (x:Float):Model ⟡:=Model.ofFloatModel x.toModel lemma A_R (x:Float):R x=р (A_M x):=rfl lemma qAg (x:Float):Ь (A_M x)=x.isFinite:=by rw[qA0,ؾ];rfl lemma qA7 {x:Float} (h:xᶠ):Ь (A_M x)=true :=(qAg x).trans h lemma qA2 (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,qA2,show (1000:ℤ)=29+971 from rfl,Щ Т] exact Б ב (by positivity) open Float.Model.UnpackedFloat in lemma qDu {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 qH0 {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 qDu e with c | c . have:=a (c.trans (ذ _ h₂));omega rcases qDu b with d | d . have:=a (d.trans (Nat.le_mul_of_pos_left _ h₁));omega omega lemma qBy (x y:Float) (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 _ qAi,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 _ _ _ _ (qH0 hxm hym),d];simp only[Bool.false_eq_true,ite_false,Model.roundDyadic,qAi,ite_true,Model.ieeeRoundDyadic,e];rfl all_goals simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul,c] lemma A_E (z:Float) {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 ⟨(qAg z).symm.trans b,d⟩ lemma qD3 (x y:Float) (hx:xᶠ) (hy:yᶠ) (hb : |R x| * |R y| ≤ 2^(1000:ℤ)) : (x*y)ᶠ ∧ R (x*y)=Model.roundAt ⟡ (R x*R y) := A_E _ (qBy x y hx hy) (Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAi (qA7 hx) (qA7 hy) (hb.trans qCd)) (Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAi (qA7 hx) (qA7 hy) (hb.trans qCd)) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qAp: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 qAr:A_z<=1/2^200*(1/2^200):=by simpa [A_z] using A_p (-1021) 0 (-200) (-200) ב lemma qJb:0< A_B:=by unfold A_B;positivity lemma qCe:(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 qCe lemma qE0 {k:ℕ} (h:k<=2^128):(k:ℝ)<=A_B :=(show (k:ℝ)<=2^128 by exact_mod_cast h).trans qAn lemma qEp:A_B*2<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 1 1000 0 ב lemma qC2:A_B*A_B<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 300 1000 0 ב lemma qFl (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[qA2] at b rw[qA2,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 qFb {z:Float} {c:ℝ} (h:zᶠ∧R z=Model.roundAt ⟡ c) : zᶠ∧|R z-c| ≤ 1/2^53*(|c| + A_z) :=⟨h.1,h.2 ▸ qFl c⟩ lemma qD1 {a b:ℝ} (d : |a| ≤ A_B) (c : |b| ≤ A_B) : |a| + |b| ≤ р (Model.posMaxFinite ⟡):=by have:=qEp have:=qCd linarith section variable (x y:Float) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) include x y hx hy h1 h2 lemma qA3 : (x+y)ᶠ∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) := qFb (A_E _ (toModel_ofFloat_add_of_isFinite x y hx hy) (Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAi (qA7 hx) (qA7 hy) (qD1 h1 h2)) (Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAi (qA7 hx) (qA7 hy) (qD1 h1 h2))) lemma qB6 : (x-y)ᶠ∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) := qFb (A_E _ (toModel_ofFloat_sub_of_isFinite x y hx hy) (Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAi (qA7 hx) (qA7 hy) (qD1 h1 h2)) (Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAi (qA7 hx) (qA7 hy) (qD1 h1 h2))) lemma qB8 : (x*y)ᶠ∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) :=qFb (qD3 x y hx hy ((Ч h1 h2 (Й _) qJb.le).trans qC2)) end lemma qA5 (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,qA2],hm,hk⟩ lemma qBk (x:Float):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,и qAi,ؾ] lemma qEh {f:Float} {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 qAi (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 qBm (f:Float) (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[<-qAg,a];exact Model.isFinite_ofModel_zero _ qAi _,?_⟩;rw[A_R f.abs,a,qBk f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAi] | finite s m e h => have b:=qEh hu;rw[hu] at a;have c:A_M f.abs=Model.ofModel ⟡ (.finite .positive m e h):=a;refine ⟨by rw[<-qAg,c,qA0,b];rfl,?_⟩;rw[A_R f.abs,c,qBk f,hu,и qAi,b];simp only[Model.unpackedToReal_finite,qA2];cases s<;>simp[Model.modelSignBit,abs_of_nonneg (by positivity:(0:ℝ)<=(m:ℝ)*2^e)] lemma qCq (f:Float) (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 _ qAi (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 _ qAi;rw[c,Ж (У _)];exact ((qE0 (א:k<=2^128)).trans ((le_mul_of_one_le_right qJb.le one_le_two).trans qEp)).trans qCd refine ⟨(qAg f).symm.trans a,?_⟩;rw[A_R,b,Model.toReal_roundDyadic_eq_roundAt _ qAi _ (b ▸ a),c];simpa using qA5 k 0 (by simpa using g) ב lemma qCg (m:UInt64) (h:m.toNat<2^53):m.toFloatᶠ∧R m.toFloat=m.toNat :=qCq _ _ h rfl lemma qGj:(0:Float)ᶠ∧R 0=0:=by simpa using qCq 0 0 ב rfl def A_bits (i:ℕ):UInt64 := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:UInt64)) lemma qCr (i:ℕ):pwEntry i=С (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):Bool := 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 qEu:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qC7 (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:=qEu i b unfold A_chk at a rw[qCr,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,qA2,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[(qC7 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 qCj (f:Float) (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⟩:=qD3 f _ q (qC7 i g).1 ((Ч m h (Й _) qJb.le).trans qC2);refine ⟨j,?_⟩ have:=qA5 (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 qE8 (i:USize) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,qDw] section variable (w:UInt32) include w lemma qAy:(w &&& В).toNat=w.toNat%2^31:=by rw[у];exact Л w.toNat 31 lemma qCp:(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[у];exact Л w.toNat 23 lemma qFh:(w >>> 23).toNat=w.toNat/2^23:=by rw[UInt32.toNat_shiftRight,ы];rfl lemma qB9:(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf;rw[у,qFh];exact Л _ 8 lemma qHd:signOf w=decide (2^31<=w.toNat):=by have b:=w.toNat_lt;unfold signOf;have h:((w &&& ط).toNat)=(w.toNat/2^31%2)*2^31:=by rw[у];show w.toNat &&& 2^31=_;rw[Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];have a:w.toNat/2^31%2=0∨w.toNat/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 qG1 : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,qCp,Nat.or_comm];simpa using (Nat.two_pow_add_eq_or_of_lt (Nat.mod_lt w.toNat (by positivity:2^23 > 0)) 1).symm lemma qA1 (h:good w=true) : w.toNat%2^31=0∨(2^23<=w.toNat%2^31∧w.toNat%2^31<Ы):=by have a:=qAy 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:=qB9 w;rw[he] at a;change 255=_ at a;have:=qA1 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 qAw (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qBb (w:UInt32) (hg:good w=true) : wv w=(if 2^31<=w.toNat then -1 else 1)*A_mag (w.toNat%2^31):=by have g:=w.toNat_lt;have:=qA1 w hg;have a:=qB9 w;have d:(fracOf w).toNat=w.toNat%2^23:=qCp 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.toNat%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.toNat%2^31%2^23=(fracOf w).toNat by rw[d];omega,show w.toNat%2^31/2^23=(expOf w).toNat by rw[a];omega];simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,qHd];simp;split_ifs<;>ring lemma qFc (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 qBa (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 qAw 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:UInt32):Float:=((w &&& 0x7FFFFF) ||| 0x800000).toFloat lemma qD7 (w:UInt32):pw.uget (w >>> 23).toUSize (qDk w) =pwEntry (w.toNat/2^23)∧w.toNat/2^23<512∧(A_mf w)ᶠ ∧ (A_mf w*pwEntry (w.toNat/2^23))ᶠ ∧ R (A_mf w*pwEntry (w.toNat/2^23))=R (A_mf w)*R (pwEntry (w.toNat/2^23)) ∧ R (A_mf w)=((2^23+w.toNat%2^23:ℕ):ℝ):=by have g:=w.toNat_lt;have c:=qG1 w;obtain ⟨d,a⟩:=qCq (A_mf w) _ (by rw[c];omega) rfl;rw[c] at a;have b:w.toNat/2^23<512 :=א obtain ⟨f,e⟩:=qCj _ _ b d ((2^23+w.toNat%2^23:ℕ):ℤ) 0 א ב (by rw[a,Ж (У _)] exact qE0 א) (by rw[a,zpow_zero,mul_one,Int.cast_natCast]) refine ⟨?_,b,d,f,e,a⟩;rw[qE8,UInt32.toNat_toUSize,qFh] lemma qAk (w:UInt32) (hg:good w=true):(dv w)ᶠ∧R (dv w)=wv w:=by obtain ⟨g,a,-,e,d,c⟩:=qD7 w;have j:=w.toNat_lt;unfold dv;rw[g];refine ⟨e,?_⟩;rw[d,c,(qC7 _ a).2,qBb w hg];have b:=qA1 w hg;generalize w.toNat=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 Float):Float:=l.foldl (.+·) 0 noncomputable def A_sa (l:List Float):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List Float):ℝ:=(l.map R).sum lemma qC5 {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 qB2 (l:List Float):0<=A_sa l:=qC5 l R lemma qGq (l:List Float) (q:Float):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 qDm (l:List Float) : |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 qBr:A_z<=1:=qAr.trans ב section variable (l:List Float) (hf:∀ q∈l,qᶠ) (hlen:l.length<=2^21) (hb:A_sa l<=A_B/4) include l hf hlen hb lemma qC4 : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ (1+1/2^22)*l.length*(1/2^53*(A_sa l+A_z)):=by induction l using List.reverseRecOn with | nil=>exact ⟨qGj.1,by simp[A_fsum,qGj.2,A_sr]⟩ | append_singleton l q ih => obtain ⟨e,f,n⟩:=qGq l q rw[e] at hb rw[List.length_append,List.length_singleton] at hlen have t:=qB2 l have j:=Й (R q) obtain ⟨h,v⟩:=ih (λx i=>hf x (by simp[i])) א ד have u:(l.length:ℝ)+1<=2^21:=by exact_mod_cast hlen have d:(0:ℝ)<=l.length:=У _ have y:=qDm l have o:=qAp have m:=qBr have p:=qAn have s:=abs_sub_abs_le_abs_sub (R (A_fsum l)) (A_sr l) have w:=Б (show (l.length:ℝ)<=2^21 by linarith) (ד:0<=A_sa l+A_z) obtain ⟨b,z⟩:=qA3 _ 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.length:ℝ)=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 qBv : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by obtain ⟨a,b⟩:=qC4 l hf hlen hb have c:=М (Nat.cast_nonneg (α:=ℝ) l.length) (add_nonneg (qB2 l) qAp.le) exact ⟨a,b.trans ד⟩ end lemma qCa (l:List Float) (c:∀ q∈l,qᶠ) (d:l.length<=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.length*(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[(qBm q (c q b)).2] have:=qBv (l.map Float.abs) (λq b=>by obtain ⟨x,e,rfl⟩:=List.mem_map.mp b exact (qBm x (c x e)).1) (by simpa) (a.1.symm ▸ f) rwa [a.1,a.2,List.length_map] at this lemma qI5 (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:UInt64).toNat=8:=rfl;simp[UInt64.toNat_add,c];omega obtain ⟨f,a⟩:=qCg (n.toUInt64+8) (by rw[b];omega);rw[b] at a obtain ⟨e,d⟩:=qCj _ 97 ב f ((n+8:ℕ):ℤ) 0 א ב (by rw[a,Ж (У _)] exact qE0 א) (by rw[a];simp) push_cast at a;rw[show nfOf n=(n.toUInt64+8).toFloat*pwEntry 97 by rw[nfOf,qCr];rfl];refine ⟨e,?_⟩;rw[d,a,(qC7 97 ב).2];norm_num;ring lemma qGf {n:ℕ} (l:n<2^20) {s t:Float} (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⟩:=qI5 n l;have E:=qAp;have B:=qAr;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⟩:=qB8 _ _ 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⟩:=qA3 s _ k d ד ד;obtain ⟨r,g⟩:=qB6 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 qEs (g h:ℕ->Float) (l:List ℕ) (a b:Float) : l.foldl (λ(d:F1) t=>F1.mk (d.s+g t) (d.t+h t)) ⟨a,b⟩ =⟨l.foldl (λ(c:Float) t=>c+g t) a,l.foldl (λ(c:Float) 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 qDh (a:List ℕ) (f:ℕ->ℝ) (M:ℝ) (h:∀ t∈a,f t<=M) : (a.map f).sum<=a.length*M:=by simpa using List.sum_le_sum h section variable (w:UInt32) (hg:good w=true) include w hg lemma qB0 : |wv w| = A_mag (w.toNat%2^31):=by rw[qBb w hg,в,Ж (qAw _)];split_ifs<;>simp lemma qAj : |wv w| ≤ 2^128:=by rw[qB0 w hg];apply qFc;rcases qA1 w hg with h | h<;>omega lemma qBs (h:w &&& В=0):wv w=0:=by rw[qBb w hg,<-qAy w,h];simp[A_mag] end lemma qHf (a b:UInt32) (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⟩:=qAk a ha obtain ⟨i,d⟩:=qAk b hb have c:=qAj a ha have e:=qAj b hb obtain ⟨h,f⟩:=qB8 _ _ 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 qBd {n:ℕ} {a b:ℕ->UInt32} {f:UInt32} (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[qBs (a t) (g t e) h] . simp[qBs (b t) (j t e) h] lemma qD8 {n:ℕ} {l:List ℕ} (b:l.Nodup) (a:∀ t∈l,t< n):l.length<=n:=by simpa using (b.subperm λt h=>List.mem_range.2 (a t h)).length_le abbrev A_G (ts:List ℕ) (g:ℕ->Float) (p:ℕ->ℝ):Prop := ∀ t∈ts,(g t)ᶠ∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z)∧|p t| ≤ 2^256 lemma qDz {b c:ℕ->UInt32} {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=>qHf _ _ (e t (a t d)) (f t (a t d)) lemma qCh {a:List ℕ} {g:ℕ->Float} {p:ℕ->ℝ} (b:Float) (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.length*A_z) ∧ A_sa L<=A + |R b| + 1/2^53*(A+a.length*A_z)∧A<=2*A_sa L+a.length*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:=qAp 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 qGs (l:List ℕ) (g:ℕ->Float) (b:Float) : ((l.map g ++ [b]).length:ℝ)=l.length+1:=by simp lemma qGu {a:List ℕ} {g:ℕ->Float} {p:ℕ->ℝ} {b:Float} (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 qBf {a:UInt32} (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.toNat<=2147483647:=З.mp h have b:wv a=A_mag a.toNat:=by rw[qBb a d,К א] simp only[show ¬ 2^31<=a.toNat by omega,↓reduceIte,one_mul] refine ⟨b ▸ qAw _,(le_abs_self _).trans (qAj a d),λw c f=>?_⟩ have e:=З.mp f rw[qAy] at e rw[qB0 _ c,b] exact qBa _ _ e ((qA1 _ c).imp_right (·.1)) section variable {n:ℕ} {ts:List ℕ} {g:ℕ->Float} {p:ℕ->ℝ} {bw hh:UInt32} {X Y J:ℝ} {s t a e:Float} (A:n<2^20) (B:ts.length<=n) (C:A_G ts g p) (D:good bw=true) (E:X=(ts.map p).sum+wv bw) (F:s=ts.foldl (λ(c:Float) t=>c+g t) 0+dv bw) (G:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) include A B C D E F G lemma qEy (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.length:ℝ)+1)*(A_sa (ts.map g ++ [dv bw])+A_z) + ((ts.map λt=>|p t|).sum+ts.length*A_z)<=((ts.length:ℝ)+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⟩:=qAk bw D obtain ⟨j,-,-⟩:=qCh (dv bw) C rw[d,<-E] at j have i:=qB2 (ts.map g ++ [dv bw]) have r:=qAp have l:(ts.length:ℝ)<=n:=by exact_mod_cast B have q:(n:ℝ)+1<=2^20:=by exact_mod_cast A have a:0<=Y :=ד obtain ⟨f,h⟩:=qC4 _ (qGu C c) (by simp;omega) ד rw[qGs] at h generalize ts.map g ++ [dv bw]=L at * have x:=qDm L have y:=Б l a have v:=Ч (show (ts.length:ℝ)+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 qGf A f k a b ד m ד G lemma qC6 (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) {gc:Float} {Bc:ℝ} (gf:gcᶠ) (gr:R gc=Bc) (gb : |wv bw| ≤ Bc) (g8:Bc<=2^128) (he:e=nfOf n*(dv hh*a+gc)) : R (s-e)< X∧X< R (s+e):=by have B0:0<=Bc:=(Й _).trans gb;obtain ⟨b,h,-⟩:=qBf P hp;obtain ⟨Q,N⟩:=qAk hh P;obtain ⟨q,j⟩:=qAk bw D;obtain ⟨-,K,-⟩:=qCh (dv bw) C;rw[j] at K;have S:=qAj bw D;generalize wv hh=H at *;have hk:(ts.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:ts.length+1<=2^20) have L:(0:ℝ)<=ts.length:=У _;have M:=qAp;have x:=qAr;have r:=qC5 ts p;have hBl:=qCe;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⟩:=qB8 _ 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 ⟨y,k⟩:=qA3 _ _ o gf ד (by rw[gr,Ж B0];linarith);rw[gr] at k;have e8:=Ъ (R (dv hh*a)) Bc;rw[Ж B0] at e8;obtain ⟨e9,Z⟩:=abs_le.mp i;obtain ⟨e11,V⟩:=abs_le.mp k;have hkz:=Б (show (ts.length:ℝ)<=2^20 by linarith) M.le;have O:=B0;obtain ⟨Y,hY⟩:∃ Y,Y=H*J+Bc+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+gc)-Y| ≤ Y/2^30+1/2^250 :=abs_le.mpr ⟨by linarith,by linarith⟩;refine qEy A B C D E F G y ד c ?_ l he;have h3:=Г c (show 0<=(ts.length:ℝ)+1 by linarith);have h4:=Б (show (ts.length:ℝ)<=2^20 by linarith) (ד:0<=Y);linarith lemma qA6 {a:ℕ->UInt32} (u:good hh=true) (w:hh<=В) (i:∀ t∈ts,good (a t)=true) (o:∀ t∈ts,|p t| ≤ wv hh * |wv (a t)|) {gc:Float} {Bc:ℝ} (gf:gcᶠ) (gr:R gc=Bc) (gb : |wv bw| ≤ Bc) (g8:Bc<=2^128) (r:e=nfOf n*(dv hh*ts.foldl (λ(c:Float) t=>c+(dv (a t)).abs) 0+gc)) : R (s-e)< X∧X< R (s+e):=by have f:(ts.length:ℝ)<=2^20:=by exact_mod_cast (by omega:ts.length<=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[(qAk _ (i t d)).2] have h:=(qDh ts _ _ λt d=>qAj _ (i t d)).trans (Б f ב);rw[<-k] at h;have m:=qCe obtain ⟨j,b⟩:=qCa _ (λq v=>by obtain ⟨t,d,rfl⟩:=List.mem_map.mp v exact (qAk _ (i t d)).1) (by simp;omega) ד rw[List.length_map] at b;have g:=qB2 (ts.map λt=>dv (a t));have l:=qAp;refine qC6 A B C D E F G u w ?_ j g (h.trans ב) ?_ gf gr gb g8 (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 lemma qD9 (bw:UInt32) (hb:good bw=true):(dv bw).absᶠ∧R (dv bw).abs = |wv bw|:=by obtain ⟨q,j⟩:=qAk bw hb;obtain ⟨w,f⟩:=qBm (dv bw) q;exact ⟨w,by rw[f,j]⟩ section variable (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) include n hn xw ww lemma qDp (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:Float) (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[qEs] at hs obtain ⟨hs,E⟩:=hs have a:=qDz hl2 hx hw have f:=qD8 hl hl2 obtain ⟨h,i⟩:=qAk bw hb obtain ⟨-,m,y⟩:=qCh (dv bw) a rw[i] at m have b:=qB2 (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw]) have k:=qDh l _ _ λt u=>(a t u).2.2 have v:(l.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:l.length+1<=2^20) have o:(0:ℝ)<=l.length:=У _ have d:=qAp have D:=qAr have w:=qAj bw hb have z:=qCe have A:=qC5 l λt=>wv (xw t)*wv (ww t) have p:=k.trans (Б (show (l.length:ℝ)<=2^20 by linarith) ב) have C:=Б (show (l.length:ℝ)<=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⟩:=qCa _ (qGu a h) (by simp;omega) ד rw[qGs] at c have B:=М o b have x:=Б v (add_nonneg b d.le) obtain ⟨G,F⟩:=abs_le.mp c exact qEy hn f a hb (qBd 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 qCx (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& В<=hh) (hp:hh<=В) (s e:Float) (hs:let r:=(О n).foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*dv (xw t)) (acc.t+v.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(dv hh*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[qEs] at hs obtain ⟨hs,c⟩:=hs obtain ⟨-,-,d⟩:=qBf hg hp have b:∀ t∈О n,t< n:=λt a=>е.mp a exact qA6 hn ג (qDz b hw hx) hb ((qBd hx hw List.nodup_range b λt a h=>absurd (е.mpr a) h).trans (by simp only[mul_comm])) hs hbig hg hp (λt a=>hw t (b t a)) (λt a=>by rw[в,mul_comm] exact Б (d _ (hx t (b t a)) (hm t (b t a))) (Й _)) (qD9 bw hb).1 (qD9 bw hb).2 le_rfl (qAj bw hb) c lemma qC3 (bw hv:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hgv:good hv=true) (hmv:∀ t,t< n->ww t &&& В<=hv) (hpv:hv<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hv*(l.foldl (λ(acc:Float) 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⟩:=qBf hgv hpv exact qA6 hn (qD8 hl hl2) (qDz hl2 hx hw) hb (qBd 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 (hl2 t a))) (Й _)) (qD9 bw hb).1 (qD9 bw hb).2 le_rfl (qAj bw hb) b lemma qDc (bw hv hc:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hgv:good hv=true) (hmv:∀ t,t< n->ww t &&& В<=hv) (hpv:hv<=В) (hgc:good hc=true) (hmc:bw &&& В<=hc) (hpc:hc<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hv*(l.foldl (λ(acc:Float) t=>acc+(dv (xw t)).abs) 0)+dv hc)) (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⟩:=qBf hgv hpv obtain ⟨-,z1,z2⟩:=qBf hgc hpc obtain ⟨q,j⟩:=qAk hc hgc exact qA6 hn (qD8 hl hl2) (qDz hl2 hx hw) hb (qBd 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 (hl2 t a))) (Й _)) q j (z2 _ hb hmc) z1 b end end section local notation:max x:max "ᶠ"=>Float.isFinite x=true noncomputable def A_r (b:Bool):ℝ:=if b then -1 else 1 lemma qGd (s t:Bool):A_r (s ^^ t)=A_r s*A_r t:=by cases s<;>cases t<;>simp[A_r] lemma qF3 (S T:Bool) (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 qDy : 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[qDy];cases d.negative<;>simp[A_r,h] lemma qE2 (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 qHz (a b:Float) (h:A_M a=A_M b):a=b:=by have:=Model.ofFloatModel_injective h;cases a;cases b;simp_all lemma qDg (f:Float) (h:fᶠ) : ∃ d,Model.toDyadic? (A_M f)=some d∧d.toReal=R f:=by obtain ⟨d,a⟩:=Model.exists_toDyadic?_of_isFinite ((qAg f).trans h);exact ⟨d,a,by rw[A_R,Model.toReal_eq,a]⟩ lemma qCn {a b:Float} {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[qBy a b e c,Model.Proof.mul_eq_spec,Model.Spec.mul,f,d] simp lemma qG3 {a b:Float} {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:=qCn 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[qDy z,qDy x,qDy y,mul_mul_mul_comm,<-qGd] at c;exact qF3 _ _ _ _ (qE2 z j) (mul_pos (qE2 x f) (qE2 y i)) c lemma qGe (v m p:Float) (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⟩:=qDg v f;obtain ⟨b,y,k⟩:=qDg p h;obtain ⟨c,w,A⟩:=qDg m i;obtain ⟨d,t,B⟩:=qDg _ u;obtain ⟨e,l,x⟩:=qDg _ s;have D:=qG3 o y t f h (by rw[B,n,k,q]);have E:=qG3 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 qHz;rw[qCn t w u i,qCn 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 _ qAi _ _ z (mt g.2 z) (Rat.cast_injective (α:=ℝ) ?_);rw[н,н,Model.Dyadic.toReal_mul,Model.Dyadic.toReal_mul,j] lemma qIf (x w:UInt32) (hx:good x=true) : dvB (dv x) w=dv x*dv w:=by obtain ⟨e,o⟩:=qAk x hx;obtain ⟨p,f,j,q,g,-⟩:=qD7 w;obtain ⟨d,a,-,-,l,b⟩:=qD7 x;obtain ⟨m,c,k⟩:=qCw _ a;have r:=x.toNat_lt;have h:R (dv x)=(((2^23+x.toNat%2^23:ℕ)*m:ℤ):ℝ) * 2^(((x.toNat/2^23%256:ℕ):ℤ)-150):=by unfold dv;rw[d,l,b,k,Int.cast_mul,Int.cast_natCast,mul_assoc] obtain ⟨n,i⟩:=qCj (dv x) _ f e _ _ (by rw[Int.natAbs_mul,Int.natAbs_natCast];have:=Nat.mul_le_mul_left (2^23+x.toNat%2^23) c omega) א (by rw[o];exact (qAj x hx).trans qAn) h show dv x*pw.uget _ _*_=dv x*(_*pw.uget _ _);rw[p];exact qGe _ _ _ e j (qC7 _ f).1 n q i g end section section open Float.Model Float.Model.UnpackedFloat lemma qFg (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 qDb (m L:Nat) (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 qD0 (M:Nat) (e T:Int) (K r:Nat) (d:T=e+(K+1:Nat)) (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 qD6 (s:Sign) (P:Nat) (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 qBc (neg:Bool) (m:Nat) (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 qBo (s:Sign) (M:Nat) (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,qDb 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 qEk (neg:Bool) (m:Nat) (e:Int) (P:Nat) (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:Nat,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,z⟩:∃ g:Nat,(e-k)+(g+2:Nat)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have u:(0:ℝ)<2^(e-k):=з two_pos _ have s:(m:ℝ)*2^e=((m*2^k:Nat):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-B_z e _ k א] have i:(2:ℝ)^E=4*((2^g:Nat):ℝ)*2^(e-k):=by rw[B_z E (e-k) (g+2) א] push_cast ring rw[qBc,<-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,qDb M _ a c,qFg];exact d by_cases v:2^25*2^g<=M . rw[o 25 E v א א,qD0 M q E (g+1) P א l ?_ (by rw[r,n];omega)];. exact qD6 _ 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) א א א,qD0 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 א א א,qD0 M q E (g+1) (2^23) א ב (by rw[n];omega) (by rw[n];omega)];exact qD6 _ _ E ב ב hE l rw[w] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBo _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qFj (f:Float):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:Float) (s:Bool) (E F:Nat) (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:Nat):ℝ)*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 :=(qFj 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 ⟨(qAg 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:Nat) (S:Bool) (ex fr:Nat):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:UInt64) (x:UInt64) (hx:(A+0x0FFFFFFF) >>> 29=x) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151) : x.toNat<2^35∧B_near A.toNat (decide (2^34<=x.toNat)) (x.toNat/2^23%2^11) (x.toNat%2^23):=by have a:=A.toNat_lt have e:x.toNat=(A.toNat+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.toNat . simp only[h,decide_true,ite_true];exact ⟨A.toNat-2^63,by omega,by omega,by omega⟩ . simp only[h,decide_false,Bool.false_eq_true,ite_false];exact ⟨A.toNat,by omega,by omega,by omega⟩ lemma qIm (x:UInt64) (hx:x.toNat<2^35) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151) : (((x >>> 34) <<< 31) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< 23))).toUInt32 = UInt32.ofNat ((if decide (2^34<=x.toNat) then 2^31 else 0) + (x.toNat/2^23%2^11-896)*2^23+x.toNat%2^23):=by have b:((x >>> 34) <<< 31).toNat=(x.toNat/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,й] norm_num [Nat.shiftRight_eq_div_pow,Nat.shiftLeft_eq] omega have a:(x &&& (0x3FFFFFFFF:UInt64)).toNat=x.toNat%2^34:=by rw[ص] exact Л _ 34 have d:((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< 23)).toNat =x.toNat%2^34-896*2^23:=by rw[show ((896:UInt64) <<< 23)=7516192768 by decide,UInt64.toNat_sub_of_le,a] . rfl . rw[UInt64.le_iff_toNat_le,a];norm_num;omega have c:(x.toNat/2^34)*2^31 ||| (x.toNat%2^34-896*2^23) =(x.toNat/2^34)*2^31+(x.toNat%2^34-896*2^23):=by rw[ж (x.toNat/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 qC0 (A B:UInt64) (h:zivW A B≠Е) : ∃ (S:Bool) (ex fr:Nat),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.toNat S ex fr∧B_near B.toNat S ex fr:=by unfold zivW at h ⊢ dsimp only at h ⊢ generalize hx:(A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=x at h ⊢ generalize hy:(B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=y at h ⊢ have hex:((x >>> (23:UInt64)) &&& (0x7FF:UInt64)).toNat=x.toNat/2^23%2^11:=by rw[ص,й] have:=Л (x.toNat >>> 23) 11 norm_num [Nat.shiftRight_eq_div_pow] at this ⊢ exact this generalize (x >>> (23:UInt64)) &&& (0x7FF:UInt64)=ex at h hex ⊢ split at h . rename_i hc;have hc0:=hc;obtain ⟨hxy,hc⟩:=Bool.and_eq_true_iff.mp hc;have hxy:x=y:=by simpa using hxy subst hxy;have hc:ex-(897:UInt64)<(254:UInt64):=by simpa using hc rw[л,ض] at hc;have hv:=ex.toNat_lt;norm_num at hc hv;have h1:897<=x.toNat/2^23%2^11 :=א;have h2:x.toNat/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,qIm x hx35 h1 h2,ha,(B_x B x hy h1 h2).2⟩ . exact absurd rfl h lemma qCf (s e:Float) (h:ziv s e≠Е) : ∃ (S:Bool) (ex fr:Nat),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 := qC0 (s+e).toBits (s-e).toBits h set_option maxRecDepth 8192 in lemma qAv (f:Float) (S:Bool) (ex fr:Nat) (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:Nat):ℝ)*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 qBu (s e:Float) (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⟩:=qCf s e h;obtain ⟨g,v,i,-,-,l⟩:=qAv _ S f c b d a j;refine ⟨g,(qAv _ 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 qAz (w:UInt32) (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 qEo (a b:Array UInt32) (start n:Nat) : (dsum a b start n:ℚ)*2^(-298:Int) =∑ j∈м n,Д a[start+j]! * Д b[start+j]!:=by have c:∀ a b:UInt32,((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 qCy (n:Nat) (fa fb:Nat->UInt32) (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.size=n:=by simp[a] have g:b.size=n:=by simp[b] have c:(dsum a b 0 n:ℚ)*2^(-298:Int) =∑ j∈м n,Д (fa j)*Д (fb j):=by rw[qEo] 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:=qD2 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 qIa:Д 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 qFq (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32) (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,qIa,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qAz bw (qAe bw hb)] congr 1 refine س rfl λt b=>?_ have a:=ؿ.mp b simp only[a,ite_true] rw[qAz _ (qAe _ (hx t a)),qAz _ (qAe _ (hw t a)),Rat.cast_mul] section variable (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е) include s e V hlo hhi h lemma qB1 : ∃ (S:Bool) (ex fr:Nat) (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'⟩:=qCf s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qAv _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qAv _ 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 qGy:V≠0:=by obtain ⟨S,ex,fr,W,-,-,-,-,hV,-,-,hW3⟩:=qB1 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 qDj (d:FloatLib.Numerics.Dyadic) (S:Bool) (ex fr:Nat) (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:=qEk 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 qJa (s e:Float) (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⟩:=qB1 s e _ hlo hhi h;rw[hz];exact (qDj d S ex fr h1 h2 h3 W hV hW1 hW2 hW3).symm lemma qCo (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:Float) (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:=qFq n xw ww bw hx hw hb obtain ⟨d,hd,hw'⟩:=qCy (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=>qGy 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 qJa s e d (by rw[q];exact hlo) (by rw[q];exact hhi) h end section lemma qDn (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:Float) (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⟩:=qBu s e h obtain ⟨hlo,hhi⟩:=qDp n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCo n hn xw ww bw hx hw hb s e hlo hhi h end section lemma qDr (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& В<=hh) (hp:hh<=В) (s e:Float) (hs:let r:=(О n).foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*dv (xw t)) (acc.t+v.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(dv hh*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⟩:=qBu s e h obtain ⟨hlo,hhi⟩:=qCx n hn xw ww bw hh hx hw hb hg hm hp s e hs hbig exact qCo n hn xw ww bw hx hw hb s e hlo hhi h end section lemma qH2 (A P Q T g:Nat) (hg:g=0∨g=2^63) (ha:A<2^64) (e1:(A+2)%2^64=g+P) (e2:(2^64-2+A)%2^64=g+Q) (t1:2^61<=T) (t2:T<2^63-2^60) (l1:T< P+2^28) (u1:P<=T+2^28) (l2:T< Q+2^28) (u2:Q<=T+2^28) : A=g+(Q+2)∧T+2< Q+2+2^28∧Q+2+2<=T+2^28:=by omega lemma qIz (G f F:Nat) (hl:G+f+2< G+F+2^28) (hu:G+F+2<=G+f+2^28) : F+2<=f+2^28∧f+2< F+2^28:=by omega lemma qHb (s:Float) (h:zivS s≠Е) : ∃ (S:Bool) (ex fr M:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ zivS s=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ s.toBits.toNat=(if S then 2^63 else 0)+M ∧ (ex*2^23+fr)*2^29+2< M+2^28 ∧ M+2<=(ex*2^23+fr)*2^29+2^28:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,⟨P,e1,l1,u1⟩,⟨Q,e2,l2,u2⟩⟩:=qC0 _ _ h rw[а] at e1 rw[ض] at e2 obtain ⟨a,b,c⟩:=qH2 _ P Q ((ex*2^23+fr)*2^29) _ (by cases S<;>simp) s.toBits.toNat_lt e1 e2 א א l1 u1 l2 u2 exact ⟨S,ex,fr,Q+2,h1,h2,h3,hz,a,b,c⟩ lemma qH6 (v W z u:ℝ) (hu:0< u) (hz:z<=u) (h0:0<=v) (hv:v<=2^53*u) (h : |v-W| ≤ 1/2^53*(|W| + z)):v-2*u< W∧W< v+2*u:=by have h1 : |W| ≤ v + |v-W|:=by have:=Ъ (v-(v-W)) (-(v-W)+(v-W));have e:W=v-(v-W):=by ring have a:=abs_sub_abs_le_abs_sub W v;rw[Э W v,Ж h0] at a;linarith have h2:=abs_lt.mp (show |v-W| < 2*u by nlinarith);constructor<;>linarith [h2.1,h2.2] set_option maxRecDepth 8192 in lemma qI0 (ex fr E F:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hF:F<2^52) (hl:(ex*2^23+fr)*2^29+2< E*2^52+F+2^28) (hu:E*2^52+F+2<=(ex*2^23+fr)*2^29+2^28) (W:ℝ) (h : |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z)) : 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 have hE:E=ex∨(E+1=ex∧fr=0) :=א have hu0:(0:ℝ)<2^((E:Int)-1075):=з ב _ have hz:A_z<=2^((E:Int)-1075):=by have e:A_z=2^(-1021:ℤ):=rfl rw[e] exact И ב א have hN:((2^52+F:Nat):ℝ)<=2^53:=by have:2^52+F<=2^53 :=א exact_mod_cast this have hN0:(0:ℝ)<=((2^52+F:Nat):ℝ):=У _ obtain ⟨a,b⟩:=qH6 _ W A_z _ hu0 hz (М hN0 hu0.le) (Б hN hu0.le) h push_cast at a b rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,Щ Т,zpow_one] rw[e] at a b;have hb:(0:ℝ)<2^((E:Int)-1076):=з ב _;generalize (2:ℝ)^((E:Int)-1076)=c at a b hb ⊢;have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl hu;obtain ⟨n3,n4⟩:=qIz _ _ _ hl hu;have h3:(F:ℝ)+2<=fr*2^29+2^28:=by exact_mod_cast n3 have h4:(fr:ℝ)*2^29+2< F+2^28:=by exact_mod_cast n4 have h5:(0:ℝ)<=F:=У _;have h6:(0:ℝ)<=fr:=У _;have h7:=Б h3 hb.le;have h8:=д h4 hb;have h9:=М h5 hb.le;have h10:=М h6 hb.le;refine ⟨?_,?_,?_⟩;. nlinarith . intro _;nlinarith . nlinarith . have e:(2:ℝ)^(((E+1:Nat):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e];generalize (2:ℝ)^((E:Int)-1075)=c at a b hu0 ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52+2< F+2^28:=by exact_mod_cast (by omega:2^52+2< F+2^28) have h7:=Б h3 hu0.le;have h8:=д h4 hu0;refine ⟨?_,?_,?_⟩;. nlinarith . intro h0;exact absurd rfl h0 . nlinarith lemma qGz (s:Float) (d:FloatLib.Numerics.Dyadic) (hV : |R s-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z)) (h:zivS s≠Е):zivS s=toWord (Ш Fmt d):=by obtain ⟨S,ex,fr,M,h1,h2,h3,hz,hb,hl,hu⟩:=qHb s h;have hM:M=(M/2^52)*2^52+M%2^52 :=א;have hF:M%2^52<2^52:=ъ _ ב;generalize M/2^52=E at hM;generalize M%2^52=F at hM hF;subst hM;obtain ⟨-,hR⟩:=B_R s S E F א א hF (by rw[hb,ц]);rw[hR] at hV obtain ⟨W,hVW,hW⟩:∃ W:ℝ,(d.toRat:ℝ)=(if S then -W else W) ∧ |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z):=by cases S . simp only[Bool.false_eq_true,ite_false,one_mul] at hV ⊢;exact ⟨_,rfl,hV⟩ . simp only[ite_true] at hV ⊢;refine ⟨-(d.toRat:ℝ),by simp,?_⟩;rw[abs_neg,show ((2^52+F:Nat):ℝ)*2^((E:Int)-1075) - -(d.toRat:ℝ) =-(-1*((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-(d.toRat:ℝ)) by ring,abs_neg];exact hV obtain ⟨a,b,c⟩:=qI0 ex fr E F h1 h2 h3 hF א א W hW;rw[hz];exact (qDj d S ex fr h1 h2 h3 W hVW a b c).symm lemma qIc (A P Q T g:Nat) (hg:g=0∨g=2^63) (ha:A<2^64) (e1:(A+1048576)%2^64=g+P) (e2:(2^64-1048576+A)%2^64=g+Q) (t1:2^61<=T) (t2:T<2^63-2^60) (l1:T< P+2^28) (u1:P<=T+2^28) (l2:T< Q+2^28) (u2:Q<=T+2^28) : A=g+(Q+1048576)∧T+1048576< Q+1048576+2^28 ∧ Q+1048576+1048576<=T+2^28:=by omega lemma qIp (G f F:Nat) (hl:G+f+1048576< G+F+2^28) (hu:G+F+1048576<=G+f+2^28) : F+1048576<=f+2^28∧f+1048576< F+2^28:=by omega lemma qEg (th:UInt64) (s:Float) (h:zivT th s≠Е) : th<=s.toBits <<< (1:UInt64)∧∃ (S:Bool) (ex fr M:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ zivT th s=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ s.toBits.toNat=(if S then 2^63 else 0)+M ∧ (ex*2^23+fr)*2^29+1048576< M+2^28 ∧ M+1048576<=(ex*2^23+fr)*2^29+2^28:=by unfold zivT at h ⊢ dsimp only at h ⊢ split at h . rename_i ht;rw[if_pos ht];refine ⟨ht,?_⟩;obtain ⟨S,ex,fr,h1,h2,h3,hz,⟨P,e1,l1,u1⟩,⟨Q,e2,l2,u2⟩⟩:=qC0 _ _ h;rw[а] at e1;rw[ض] at e2;obtain ⟨a,b,c⟩:=qIc _ P Q ((ex*2^23+fr)*2^29) _ (by cases S<;>simp) s.toBits.toNat_lt e1 e2 א א l1 u1 l2 u2;exact ⟨S,ex,fr,Q+1048576,h1,h2,h3,hz,a,b,c⟩ . exact absurd rfl h lemma qId (v W z u:ℝ) (hu:0< u) (hz:z<=u) (h0:0<=v) (hv:v<=2^53*u) (h : |v-W| ≤ 1/2^34*(|W| + z)):v-2^20*u< W∧W< v+2^20*u:=by have h1 : |W| ≤ v + |v-W|:=by have a:=abs_sub_abs_le_abs_sub W v;rw[Э W v,Ж h0] at a;linarith have h2:=abs_lt.mp (ד:|v-W| < 2^20*u);constructor<;>linarith [h2.1,h2.2] set_option maxRecDepth 8192 in lemma qI1 (ex fr E F:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hF:F<2^52) (hl:(ex*2^23+fr)*2^29+1048576< E*2^52+F+2^28) (hu:E*2^52+F+1048576<=(ex*2^23+fr)*2^29+2^28) (W:ℝ) (h : |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^34*(|W| + A_z)) : 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 have hE:E=ex∨(E+1=ex∧fr=0) :=א have hu0:(0:ℝ)<2^((E:Int)-1075):=з ב _ have hz:A_z<=2^((E:Int)-1075):=by have e:A_z=2^(-1021:ℤ):=rfl rw[e] exact И ב א have hN:((2^52+F:Nat):ℝ)<=2^53:=by have:2^52+F<=2^53 :=א exact_mod_cast this have hN0:(0:ℝ)<=((2^52+F:Nat):ℝ):=У _ obtain ⟨a,b⟩:=qId _ W A_z _ hu0 hz (М hN0 hu0.le) (Б hN hu0.le) h push_cast at a b rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,Щ Т,zpow_one] rw[e] at a b;have hb:(0:ℝ)<2^((E:Int)-1076):=з ב _;generalize (2:ℝ)^((E:Int)-1076)=c at a b hb ⊢;have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl hu;obtain ⟨n3,n4⟩:=qIp _ _ _ hl hu;have h3:(F:ℝ)+1048576<=fr*2^29+2^28:=by exact_mod_cast n3 have h4:(fr:ℝ)*2^29+1048576< F+2^28:=by exact_mod_cast n4 have h5:(0:ℝ)<=F:=У _;have h6:(0:ℝ)<=fr:=У _;have h7:=Б h3 hb.le;have h8:=д h4 hb;have h9:=М h5 hb.le;have h10:=М h6 hb.le;refine ⟨?_,?_,?_⟩;. linarith . intro _;linarith . linarith . have e:(2:ℝ)^(((E+1:Nat):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e];generalize (2:ℝ)^((E:Int)-1075)=c at a b hu0 ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52+1048576< F+2^28:=by exact_mod_cast (by omega:2^52+1048576< F+2^28) have h7:=Б h3 hu0.le;have h8:=д h4 hu0;refine ⟨?_,?_,?_⟩;. simp only[Nat.cast_zero,zero_mul,add_zero];linarith . intro h0;exact absurd rfl h0 . linarith lemma qG0 (th:UInt64) (s:Float) (d:FloatLib.Numerics.Dyadic) (hV : |R s-(d.toRat:ℝ)| ≤ 1/2^34*(|(d.toRat:ℝ)| + A_z)) (h:zivT th s≠Е):zivT th s=toWord (Ш Fmt d):=by obtain ⟨-,S,ex,fr,M,h1,h2,h3,hz,hb,hl,hu⟩:=qEg th s h;have hM:M=(M/2^52)*2^52+M%2^52 :=א;have hF:M%2^52<2^52:=ъ _ ב;generalize M/2^52=E at hM;generalize M%2^52=F at hM hF;subst hM;obtain ⟨-,hR⟩:=B_R s S E F א א hF (by rw[hb,ц]);rw[hR] at hV obtain ⟨W,hVW,hW⟩:∃ W:ℝ,(d.toRat:ℝ)=(if S then -W else W) ∧ |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^34*(|W| + A_z):=by cases S . simp only[Bool.false_eq_true,ite_false,one_mul] at hV ⊢;exact ⟨_,rfl,hV⟩ . simp only[ite_true] at hV ⊢;refine ⟨-(d.toRat:ℝ),by simp,?_⟩;rw[abs_neg,show ((2^52+F:Nat):ℝ)*2^((E:Int)-1075) - -(d.toRat:ℝ) =-(-1*((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-(d.toRat:ℝ)) by ring,abs_neg];exact hV obtain ⟨a,b,c⟩:=qI1 ex fr E F h1 h2 h3 hF א א W hW;rw[hz];exact (qDj d S ex fr h1 h2 h3 W hVW a b c).symm lemma qIb (x:UInt64) : ((x >>> (52:UInt64)) &&& (0x7FF:UInt64)).toNat=x.toNat/2^52%2^11:=by rw[ص,й];have:=Л (x.toNat >>> 52) 11;norm_num [Nat.shiftRight_eq_div_pow] at this ⊢;exact this lemma qFi (b:Bool) (x y:ℝ) (hx:0<=x) (hy:0<=y) : |(if b=true then (-1:ℝ) else 1)*x*y| = x*y:=by have h:=Ж (М hx hy) cases b . rw[show (if false=true then (-1:ℝ) else 1)*x*y=x*y by simp];exact h . rw[show (if true=true then (-1:ℝ) else 1)*x*y=-(x*y) by simp,abs_neg];exact h lemma qIi (a:UInt64) (S:Bool) (M:Nat) (hM:M<2^63) (h:a.toNat=(if S then 2^63 else 0)+M):(a <<< (1:UInt64)).toNat=2*M:=by rw[UInt64.toNat_shiftLeft,h];norm_num [Nat.shiftLeft_eq];cases S<;>simp<;>omega lemma qGc (e s:Float) (S:Bool) (M:Nat) (hM2:M<2047*2^52) (hs:s.toBits.toNat=(if S then 2^63 else 0)+M) (h:thOf e<=s.toBits <<< (1:UInt64)) : e.isFinite=true∧|R e| ≤ 1/2^36 * |R s|:=by have hl:=qIi s.toBits S M א hs;unfold thOf at h;dsimp only at h;have hx:=qIb e.toBits;generalize (e.toBits >>> (52:UInt64)) &&& (0x7FF:UInt64)=E at h hx;rw[UInt64.le_iff_toNat_le,hl] at h;split at h . rename_i hc;rw[л,ض] at hc;rw[UInt64.toNat_shiftLeft,а] at h;have hA:=e.toBits.toNat_lt;norm_num [Nat.shiftLeft_eq] at hc h hA;obtain ⟨sg,hsg⟩:∃ sg:Bool,e.toBits.toNat =(if sg then 2^63 else 0)+E.toNat*2^52+e.toBits.toNat%2^52:=by refine ⟨decide (2^63<=e.toBits.toNat),?_⟩;by_cases h6:2^63<=e.toBits.toNat<;>simp only[h6,decide_true,decide_false,ite_true,ite_false,Bool.false_eq_true]<;>omega have hFe:e.toBits.toNat%2^52<2^52:=ъ _ ב;generalize e.toBits.toNat%2^52=Fe at hsg hFe;have hE1:1<=E.toNat∧E.toNat<=1900 :=א;have hEs:M=(M/2^52)*2^52+M%2^52 :=א;have hFs:M%2^52<2^52:=ъ _ ב;generalize M/2^52=Es at hEs;generalize M%2^52=Fs at hEs hFs;subst hEs;have hle:E.toNat+37<=Es :=א;obtain ⟨fe,re⟩:=B_R e sg E.toNat Fe hE1.1 א hFe hsg;obtain ⟨-,rs⟩:=B_R s S Es Fs א א hFs (by rw[hs,ц]);refine ⟨fe,?_⟩;have hc0:(0:ℝ)<2^((E.toNat:Int)-1075):=з ב _;have h1:(2:ℝ)^((E.toNat:Int)+37-1075)<=2^((Es:Int)-1075) :=И ב א;have h2:(2:ℝ)^((E.toNat:Int)+37-1075)=2^37*2^((E.toNat:Int)-1075):=by rw[show (E.toNat:Int)+37-1075=37+((E.toNat:Int)-1075) by ring,Щ Т];norm_num have hae : |R e| = ((2^52+Fe:Nat):ℝ)*2^((E.toNat:Int)-1075):=by rw[re];exact qFi _ _ _ (У _) hc0.le have has : |R s| = ((2^52+Fs:Nat):ℝ)*2^((Es:Int)-1075):=by rw[rs];exact qFi _ _ _ (У _) (з ב _).le rw[hae,has];have n1:((2^52+Fe:Nat):ℝ)<=2^53:=by have:2^52+Fe<=2^53 :=א;exact_mod_cast this have n2:(2:ℝ)^52<=((2^52+Fs:Nat):ℝ):=by have:2^52<=2^52+Fs :=א;exact_mod_cast this generalize (2:ℝ)^((E.toNat:Int)-1075)=c at *;generalize (2:ℝ)^((Es:Int)-1075)=d at *;rw[h2] at h1;have n3:=Б n1 hc0.le;have n0:0<=d :=ד;have n4:=Г (Б n2 n0) (by norm_num:(0:ℝ)<=1/2^36);have n5:(2:ℝ)^53*c<=1/2^36*(2^52*d) :=ד;exact n3.trans (n5.trans n4) . exfalso;norm_num at h;omega lemma qGv (s e:Float) (V:ℝ) (fs:s.isFinite=true) (fe:e.isFinite=true) (hrel : |R e| ≤ 1/2^36 * |R s|) (hB : |R s| ≤ 2^(130:ℤ)) (hlo:R (s-e)< V) (hhi:V< R (s+e)) : |R s-V| ≤ 1/2^34*(|V| + A_z):=by have hA:(2:ℝ)^(130:ℤ)<=A_B:=by unfold A_B;exact И ב ב have h1 : |R e| ≤ A_B:=by refine ь hrel (ь ?_ (hB.trans hA));have:=Й (R s);nlinarith [this] obtain ⟨-,p⟩:=qA3 s e fs fe (hB.trans hA) h1;obtain ⟨-,m⟩:=qB6 s e fs fe (hB.trans hA) h1;have p2:=(abs_le.mp p).2;have m2:=(abs_le.mp m).1;have q1:=Ъ (R s) (R e);have q2:=ف (R s) (R e);have a1 : |R s| ≤ |V| + |R s-V|:=by have:=abs_sub_abs_le_abs_sub (R s) V;linarith have a2:=le_abs_self (R e);have a3:=neg_abs_le (R e);have hz:(0:ℝ)<=A_z:=by unfold A_z;positivity have hQ : |R s-V| ≤ |R e| + 1/2^53*(|R s| + |R e| + A_z):=by rw[abs_le];constructor<;>linarith have h0:=Й (R s);linarith lemma U_w (T M:Nat) (h1:T+1048576< M+2^28) (h2:M+1048576<=T+2^28) : T< M+2^28∧M<=T+2^28:=by omega set_option maxRecDepth 8192 in lemma qIl (e s:Float) (h:zivT (thOf e) s≠Е) : s.isFinite=true∧e.isFinite=true∧|R e| ≤ 1/2^36 * |R s|∧|R s| ≤ 2^(130:ℤ) ∧ (2:ℝ)^(-127:ℤ) ≤ |R (s+e)|∧(2:ℝ)^(-127:ℤ) ≤ |R s|:=by obtain ⟨ht,S,ex,fr,M,h1,h2,h3,-,hbt,hl',hu'⟩:=qEg _ s h have hnr:B_near s.toBits.toNat S ex fr:=⟨M,hbt,(U_w _ _ hl' hu').1,(U_w _ _ hl' hu').2⟩ obtain ⟨fs,v,hv,hv1,-,hv3⟩:=qAv _ S ex fr h1 h2 h3 hnr obtain ⟨fe,hrel⟩:=qGc e s S M א hbt ht have hb0:(2:ℝ)^(-179:ℤ)<=2^((ex:Int)-1076) :=И ב א have hb1:(2:ℝ)^((ex:Int)-1076)<=2^(74:ℤ) :=И ב א have hpos:(0:ℝ)<2^(-179:ℤ):=з ב _ have e179:(2:ℝ)^(-127:ℤ)=2^52*2^(-179:ℤ):=by rw[show (-127:ℤ)=52+(-179) by norm_num,Щ Т];norm_num have e130:(2:ℝ)^(130:ℤ)=2^56*2^(74:ℤ):=by rw[show (130:ℤ)=56+74 by norm_num,Щ Т];norm_num have hc0:(0:ℝ)<2^((ex:Int)-1076):=з ב _ have hv0:(2^53-2^28:ℝ)*2^(-179:ℤ)<=v :=(Г hb0 ב).trans hv3.le have hfr:(fr:ℝ)<=2^23:=by exact_mod_cast h3.le have hfr0:(0:ℝ)<=fr:=У _ have hzs:A_z<=2^(-179:ℤ):=by have e':A_z=2^(-1021:ℤ):=rfl rw[e'];exact И ב ב generalize (2:ℝ)^(-179:ℤ)=u at * have hvpos:0< v :=ד have hRs : |R s| = v:=by rw[hv];cases S . simp[abs_of_pos hvpos] . simp[abs_of_pos hvpos] have hF:(2^53+(fr:ℝ)*2^30+2^29)<=2^56 :=ד have hB : |R s| ≤ 2^(130:ℤ):=by rw[hRs,e130] exact hv1.trans ((Б hF hc0.le).trans (Г hb1 ב)) have hA:(2:ℝ)^(130:ℤ)<=A_B:=by unfold A_B;exact И ב ב have hR : |R e| ≤ 1/2^36*v:=by rw[<-hRs];exact hrel have hvB:v<=A_B:=by rw[<-hRs];exact hB.trans hA have hfin : |R s| ≤ A_B:=by rw[hRs];exact hvB obtain ⟨-,p⟩:=qA3 s e fs fe hfin (hR.trans ד) have q2 : |R s| - |R e| ≤ |R s+R e|:=by have:=abs_sub_abs_le_abs_sub (R s) (-R e) rwa [abs_neg,sub_neg_eq_add] at this have q1:=Ъ (R s) (R e) have q3:=abs_sub_abs_le_abs_sub (R s+R e) (R (s+e)) rw[Э] at q3 rw[hRs] at q1 q2 refine ⟨fs,fe,hrel,hB,?_,?_⟩ . rw[e179];linarith [Й (R e)] . rw[hRs,e179];linarith lemma qI7 (s:Float) (V:ℝ) (hV : |R s-V| ≤ 1/2^34*(|V| + A_z)) (h2:(2:ℝ)^(-127:ℤ) ≤ |R s|):V≠0:=by rintro rfl;rw[abs_zero,zero_add,sub_zero] at hV;have e':A_z=2^(-1021:ℤ):=rfl;have hle:(2:ℝ)^(-1021:ℤ)<=2^(-127:ℤ):=И ב ב;have h0:(0:ℝ)<2^(-1021:ℤ):=з ב _;rw[e'] at hV;linarith lemma qDf (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hv hc:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hgv:good hv=true) (hmv:∀ t,t< n->ww t &&& В<=hv) (hpv:hv<=В) (hgc:good hc=true) (hmc:bw &&& В<=hc) (hpc:hc<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hv*(l.foldl (λ(acc:Float) t=>acc+(dv (xw t)).abs) 0)+dv hc)) (h:zivT (thOf e) s≠Е) : zivT (thOf e) s=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨fs,fe,hrel,hB,hbig,hl2'⟩:=qIl e s h obtain ⟨hlo,hhi⟩:=qDc n hn xw ww bw hv hc hx hw hb hgv hmv hpv hgc hmc hpc l hl hl2 hz s e hs hbig have hV:=qGv s e _ fs fe hrel hB hlo hhi have hsum:=qFq n xw ww bw hx hw hb have hne:(∑ j∈м (n+1),Д (if j< n then xw j else oneW) * Д (if j< n then ww j else bw):ℚ)≠0:=by intro h0 rw[h0,Rat.cast_zero] at hsum exact qI7 s _ hV hl2' hsum.symm obtain ⟨d,hd,hw'⟩:=qCy (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (by intro j _ have hone:expOf oneW≠255:=by decide 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 ⟨hone,qAe _ hb⟩) hne rw[hw'] refine qG0 _ s d ?_ h rw[hd,hsum] exact hV lemma C_z:(0:USize).toNat=0:=rfl lemma qAd (k:Nat) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma qBg (a b:USize) (h:a.toNat*b.toNat<2^32) : (a*b).toNat=a.toNat*b.toNat:=qC1 a b h lemma qBi {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size :=qAd _ h lemma qAh (a:Array UInt32) (i:USize) (hs:a.size<2^32):rdW a i=a[i.toNat]!:=by unfold rdW;split;. rename_i h;have h2:=qBe a i h;rw[Ю a i.toNat h2];rfl . rename_i h;have h':¬ i.toNat< a.size:=by intro h2;apply h;rw[А,qBi a hs];exact h2 rw[ك a i.toNat h'];rfl lemma qBj (a:Array UInt32) (i:USize) (v:UInt32):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma qFk {α:Type} [Inhabited α] (a:Array α) (i q:Nat) (v:α) (hi:i< a.size) : (a.set i v hi)[q]! = if q=i then v else a[q]!:=by by_cases hq:q< a.size . 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 qBp (a:Array UInt32) (i:USize) (v:UInt32) (q:Nat) (hs:a.size<2^32) (hi:i.toNat< a.size):(wrW a i v)[q]! = if q=i.toNat then v else a[q]!:=by have h:i< a.usize:=by rw[А,qBi a hs];exact hi unfold wrW;rw[Р h];exact qFk a _ q v hi def C_Inv (N:Nat) (sp:Nat->Word) (S:Nat->Prop) (out:Array Word):Prop := out.size=N∧∀ q,q< N->S q->out[q]! = sp q section variable {N:Nat} {sp:Nat->Word} include N sp lemma qAl {S:Nat->Prop} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp S out) {u:USize} {p:Nat} {v:Word} (hu:u.toNat=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[qBj,h1],λq hq hS=>?_⟩;rw[qBp _ _ _ _ א א,hu];by_cases e:q=p;. rw[ф e,e,hv] . rw[Ц e];exact h2 q hq (hS.resolve_left e) lemma qA9 {S S':Nat->Prop} {out:Array Word} (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 qC9 {S:Nat->Prop} {out:Array Word} (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 qEm (v:UInt32) (h1:(0x7FFFFF:UInt32)<=v-1) (h2:v<Ы) : (v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32))=true:=by have a1:(0x7FFFFF:UInt32).toNat=0x7FFFFF:=rfl;have a2:(Ы:UInt32).toNat=Ы:=rfl;have a3:(0x00800000:UInt32).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:UInt32).toNat=0x7EFFFFFF:=rfl;have a5:(1:UInt32).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:UInt32)<=v:=by rw[З];have:v.toNat≠0:=λh=>h0 (Я.mp h);omega rw[З,ل _ _ h3] at h1;rw[ر] at h2;have h4:(0x00800000:UInt32)<=v:=by rw[З];omega simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[З,ل _ _ h4];omega lemma qH3 (c u lo:UInt32) (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 qIe (c v hi:UInt32) (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 qBx (w:UInt32) (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 qCu {h:UInt32} (h1:h<Ы):h<=В:=by have a1:(Ы:UInt32).toNat=Ы:=rfl;have a2:(В:UInt32).toNat=В:=rfl;rw[ر] at h1;rw[З];omega def C_Hv (A:Array Word) (h:Word):Prop := good h=true∧h<=В∧(∀ k,k< A.size->good A[k]! = true) ∧ ∀ k,k< A.size->A[k]! &&& В<=h lemma qEz (r v hi:UInt32) (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 qGw (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi≠Е → (0x7FFFFF:UInt32)<=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.toNat<=k->k< n.toNat → 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,UInt32.le_refl _,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.toNat< A.size :=א 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⟩:=qH3 _ _ _ h1 obtain ⟨h9,h10⟩:=qIe _ _ _ h2 obtain ⟨h11,h12⟩:=qEz _ _ _ h3 have hg:good (A.uget i hb)=true:=qEm _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qBx _ hg . exact g0 . by_cases e2:k=i.toNat;. subst e2;rw[Ю A i.toNat hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qB5 (A:Array UInt32) (hs:A.size<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:=qBi A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qGw A A.usize (qAc A) A.size 0 _ _ (by rw[h0,e];omega) h;have h7:∀ k,k< A.size->good A[k]! = true ∧ A[k]! &&& В<=scan A A.usize 0 (qAc A) Е 0 :=λk hk=>h6 k א א;exact ⟨h5 rfl,qCu h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qEt (w:UInt32):w &&& В<=В:=by rw[З,у];exact Nat.and_le_right lemma qF0 (a b:UInt32):a<=a ||| b∧b<=a ||| b:=by rw[З,З,UInt32.toNat_or];exact ⟨Nat.left_le_or,Nat.right_le_or⟩ lemma qCs (v:UInt32) (h1:v=0∨0x800000<=v) (h2:v<Ы) : (v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32))=true:=by rcases h1 with rfl | h1;. rfl . have a1:(Ы:UInt32).toNat=Ы:=rfl;have a2:(0x00800000:UInt32).toNat=0x00800000:=rfl;have a3:(0x7EFFFFFF:UInt32).toNat=0x7EFFFFFF:=rfl;simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[З,ل _ _ h1];rw[ر] at h2;rw[З] at h1;omega lemma qE1 (a b:UInt32):a &&& b<=a∧a &&& b<=b:=by rw[З,З,у];exact ⟨Nat.and_le_left,Nat.and_le_right⟩ lemma qF5 (v:UInt32) (h:0x7FFFFF<=v-1):v=0∨0x800000<=v:=by by_cases z:v=0;. exact .inl z . right;have a1:(0x7FFFFF:UInt32).toNat=0x7FFFFF:=rfl;have a2:(0x800000:UInt32).toNat=0x800000:=rfl;have a3:(1:UInt32).toNat=1:=rfl;have a4:v.toNat≠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 qGa (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (o s:UInt32),i.toNat+d=n.toNat->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.toNat<=k->k< n.toNat → 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 ⟨UInt32.le_refl _,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.toNat< A.size :=א 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⟩:=qF0 o _ obtain ⟨h8,h9⟩:=qE1 s ((A.uget i hb &&& В)-1) have hv:=qF5 _ (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.toNat;. subst e2;rw[Ю A i.toNat hb];exact ⟨qCs _ hv (UInt32.lt_of_le_of_lt hl h2),hl⟩ . exact h5 k (by rw[usz i n hi'];omega) hk2 lemma qBw (A:Array UInt32) (hs:A.size<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:=qBi A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,h2,-,h4,h5⟩ :=qGa A A.usize (qAc A) A.size 0 _ _ (by rw[h0,e];omega) h;generalize scanF A A.usize 0 (qAc A) 0 Е=S at *;have hp:=qCu h2;have hm:S &&& В=S:=by have a:(В:UInt32).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 qCs _ (h4 (Or.inl rfl)) h2 exact ⟨hg,hp,λk hk=>(h5 k א א).1,λk hk=>(h5 k א א).2⟩ lemma qBh (r:Nat) (A:Array UInt32) (hs:A.size<2^32) (h:scanH r A≠Е) : C_Hv A (scanH r A):=by revert h;unfold scanH;dsimp only;split;. exact qB5 A hs . exact qBw A hs lemma qFw (r:Nat) (A:Array UInt32) (h8:r≠8) (hs:A.size<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 simpa using h8)] rw[e] at h ⊢;exact qBh r A hs h def C_hok (w:Nat->Word) (t:Nat) (h:Word):Prop := good h=true∧(∀ k,k< t->w k &&& В<=h)∧h<=В lemma qD5 {w:Nat->Word} {t:Nat} {h:Word} (hg:good (w t)=true) (H:C_hok w t h):C_hok w (t+1) (mx h (w t)):=by obtain ⟨h1,h2,hp⟩:=H;have hm:=qBx _ hg;unfold mx;dsimp only;by_cases e:w t &&& В<=h . rw[ф e];refine ⟨h1,λk hk=>?_,hp⟩;by_cases e2:k=t;. rw[e2];exact e . exact h2 k א . rw[Ц e];refine ⟨hm,λk hk=>?_,qEt _⟩;by_cases e2:k=t;. rw[e2];exact UInt32.le_refl _ . have h3:=h2 k א;simp only[UInt32.le_iff_toNat_le] at *;omega def C_out (I:Nat) (Y:Bool) (X W b:Array Word) (i j:Nat):Word := 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:Nat) (X W b:Array Word):Prop where hX:R*I=X.size hW:O*I=W.size hb:O=b.size pi:0< I po:0< O ni:I<2^20 sX:X.size<2^32 sW:W.size<2^32 sb:b.size<2^32 sO:R*O<2^32 gX:∀ q,q< X.size->good X[q]! = true gW:∀ q,q< W.size->good W[q]! = true gb:∀ q,q< b.size->good b[q]! = true lemma qCm {R I i t:Nat} (hi:i< R) (ht:t< I):i*I+t< R*I:=by have h:(i+1)*I<=R*I:=Ф _ hi;rw[щ,о] at h;omega lemma qBl {R I i t:Nat} (hi:i< R) (ht:t<=I) : i*I+t<=R*I:=by have h:(i+1)*I<=R*I:=Ф _ hi;rw[щ,о] at h;omega section variable {R I O:Nat} {X W b:Array Word} section variable (G:C_G R I O X W b) include G lemma C_G.gx {i:Nat} (hi:i< R) : ∀ t,t< I->good X[i*I+t]! = true :=λ_ ht=>G.gX _ (by rw[<-G.hX];exact qCm hi ht) lemma C_G.gw {j:Nat} (hj:j< O) : ∀ t,t< I->good W[j*I+t]! = true :=λ_ ht=>G.gW _ (by rw[<-G.hW];exact qCm hj ht) lemma qIh (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) {s e:Float} (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[qAo] . 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)) _ List.nodup_range (λt ht=>е.mp ht) (λt ht h3=>absurd (е.mpr ht) h3) _ _ ⟨by rw[h1],by rw[h2]⟩ (by simpa using h) end lemma qEv {X W:Array UInt32} {xo wo n:USize} (hX:X.size<2^32) (hW:W.size<2^32) (h1:xo.toNat+n.toNat<=X.size) (h2:wo.toNat+n.toNat<=W.size) (d:Nat):∀ (t:USize) (s a:Float),t.toNat+d=n.toNat → tightGo X W xo wo n t s a=(List.range' t.toNat d).foldl (λ(acc:F1) k =>let q:=dv X[xo.toNat+k]! * dv W[wo.toNat+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,List.foldl_cons] dsimp only rw[qAh _ _ hX,qAh _ _ 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:Bool) {i j:Nat} (hi:i< R) (hj:j< O) include G Y i j hi hj lemma qDt {s a:Float} {hh:Word} (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.toUSize=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.size:=by rw[<-G.hX];exact qBl hi (le_refl _) have hW1:j*I+I<=W.size:=by rw[<-G.hW];exact qBl hj (le_refl _) 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:=qEv (xo:=(i*I).toUSize) (wo:=(j*I).toUSize) (n:=I.toUSize) 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,<-List.range_eq_range'] at e;rw[qIh G Y hi hj (s:=(tightGo X W (i*I).toUSize (j*I).toUSize I.toUSize 0 0 0).s) (e:=(tightGo X W (i*I).toUSize (j*I).toUSize I.toUSize 0 0 0).t) ⟨by rw[e],by rw[e]⟩] . rename_i h;unfold C_out;congr 1;exact hc (by simpa using h) lemma qF7 {s:Float} {hv:Word} (g1:good hv=true) (g2:hv<=В) (g4:∀ t,t< I->W[j*I+t]! &&& В<=hv) (L:List Nat) (hl:L.Nodup) (hl2:∀ t∈L,t< I) (hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0) (hs:s=L.foldl (λ(acc:Float) 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:Float) t=>a+(dv X[i*I+t]!).abs) 0) hv b[j]! I.toUSize=C_out I Y X W b i j:=by have gb:=G.gb j (by rw[<-G.hb];exact hj) exact qDt G Y hi hj λh=>by obtain ⟨-,-,k⟩:=qBu _ _ h obtain ⟨c1,c2⟩:=qC3 I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! hv (G.gx hi) (G.gw hj) gb g1 g4 g2 L hl hl2 hz _ _ ⟨by rw[hs],rfl⟩ k exact qCo I G.ni _ _ _ (G.gx hi) (G.gw hj) gb _ _ c1 c2 h lemma qGk {s:Float} {hv hc:Word} (g1:good hv=true) (g2:hv<=В) (g4:∀ t,t< I->W[j*I+t]! &&& В<=hv) (c1:good hc=true) (c2:hc<=В) (c3:b[j]! &&& В<=hc) (L:List Nat) (hl:L.Nodup) (hl2:∀ t∈L,t< I) (hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0) (hs:s=L.foldl (λ(acc:Float) t=>acc+dv X[i*I+t]! * dv W[j*I+t]!) 0) : finT I Y X W b i j s (nfOf I) (L.foldl (λ(a:Float) t=>a+(dv X[i*I+t]!).abs) 0) hv b[j]! I.toUSize (thOf (nfOf I*(dv hv*L.foldl (λ(a:Float) t=>a+(dv X[i*I+t]!).abs) 0+dv hc))) =C_out I Y X W b i j:=by have gb:=G.gb j (by rw[<-G.hb];exact hj) unfold finT dsimp only split . exact qF7 G Y hi hj g1 g2 g4 L hl hl2 hz hs . rename_i h;unfold C_out;congr 1;exact qDf I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! hv hc (G.gx hi) (G.gw hj) gb g1 g4 g2 c1 c3 c2 L hl hl2 hz _ _ ⟨by rw[hs],rfl⟩ (by simpa using h) end end lemma qC8 (x:FloatArray) (i:USize) (h:i.toNat< x.size) : x.uget i h=x.data[i.toNat]!:=by obtain ⟨ds⟩:=x;have h':i.toNat< ds.size:=h;rw[Ю ds i.toNat h'];rfl lemma qDd (a:Array UInt32) (i:USize) (h:i.toNat< a.size) : a.uget i h=a[i.toNat]!:=by rw[Ю a i.toNat h];rfl lemma qCc (a:FloatArray) (i:USize) (v:Float):(wrF a i v).size=a.size:=by unfold wrF;split;. obtain ⟨ds⟩:=a;exact Array.size_uset _ . rfl lemma qCv (a:FloatArray) (i:USize) (v:Float) (q:Nat) (hs:a.size<2^32) (hi:i.toNat< a.size) : (wrF a i v).data[q]! = if q=i.toNat 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 qFk ds _ q v hi def C_keep (xw:Nat->Word) (t:Nat):List Nat := (О t).filter (λp=>xw p &&& В != 0) section variable (xw:Nat->Word) (t:Nat) include xw t lemma qB3:(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& В != 0) (О t);simpa [C_keep] using h lemma qDo (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 qEa (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_Dec (xw:Nat->Word) (sz t:Nat) (ix:Array UInt32) (buf:FloatArray) (c:USize) (l:Float):Prop := c.toNat=(C_keep xw t).length∧ix.size=sz∧buf.size=sz ∧ (∀ k p:Nat,(C_keep xw t)[k]?=some p → (ix[k]! : UInt32).toNat=p∧buf.data[k]! = dv (xw p)) ∧ l=(C_keep xw t).foldl (λ(a:Float) p=>a+(dv (xw p)).abs) 0 lemma qHp {X:Array UInt32} {o n:USize} (xw:Nat->Word) (hn:n.toNat<2^32) (hxw:∀ t:USize,t< n->rdW X (o+t)=xw t.toNat) (hg:∀ t,t< n.toNat->good (xw t)=true) (d:Nat) : ∀ (t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh:Float),t.toNat+d=n.toNat->C_Dec xw n.toNat t.toNat ix buf c hh → C_Dec xw n.toNat n.toNat (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.toNat=n.toNat :=א 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 by_cases hz:xw t.toNat &&& В=0 . rw[ф (by simpa using hz)];apply ih (t+1) _ _ _ _ א;rw[hu,C_Dec,qDo xw _ hz];exact ⟨h1,h2,h3,h4,h5⟩ . rw[Ц (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qB3 xw t.toNat;have hc:c.toNat< t.toNat+1 :=א;rw[hu,C_Dec,qEa xw _ hz];have hcn:c.toNat< n.toNat :=א;refine ⟨?_,by rw[qBj];exact h2,by rw[qCc];exact h3,λk p hk2=>?_,by rw[List.foldl_append,<-h5];rfl⟩;. rw[qAf _ _ (by rw[غ];omega),غ,h1];simp . rw[qBp _ _ _ _ א א,qCv _ _ _ _ א א];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=c.toNat;. subst e;simp at hk2;subst hk2;simp;omega . rw[Ц e,Ц e];by_cases e2:k< c.toNat;. rw[ф e2] at hk2;exact h4 k p hk2 . rw[Ц e2] at hk2;obtain ⟨m,hm⟩:∃ m,k-c.toNat=m+1:=⟨k-c.toNat-1,by omega⟩;rw[hm] at hk2;simp at hk2 lemma qAs (a:FloatArray) (v:Float):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qFz (n:USize) (d:Nat):∀ (k:USize) (a:FloatArray),k.toNat+d=n.toNat → (pushZ n k a).size=a.size+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),qAs];omega lemma qIn {x:FloatArray} {ix W:Array UInt32} {w0 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (xf:Nat->Float) (ww:Nat->Word) (hww:∀ p,p< m.toNat->W[w0.toNat+p]! = ww p) (L:List Nat):∀ (t:USize) (a:Float),t.toNat+L.length=n.toNat → (∀ k p:Nat,L[k]?=some p->p< m.toNat∧(ix[t.toNat+k]! : UInt32).toNat=p ∧ x.data[t.toNat+k]! = xf p) → go1 x ix W w0 m n t hx hi hs h0 a =L.foldl (λ(acc:Float) 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.toNat< n.toNat :=א obtain ⟨h1,h2,h3⟩:=H 0 p rfl rw[ػ] at h2 h3 have hit:t.toNat< ix.size :=א have e0:(ix.uget t hit).toUSize.toNat=p:=by rw[UInt32.toNat_toUSize,qDd];exact h2 have hm:(ix.uget t hit).toUSize< m:=by rw[А,e0];exact h1 have e1:x.uget t א=xf p:=by rw[qC8,h3] have e2:∀ pf,W.uget (w0+(ix.uget t hit).toUSize) pf=ww p:=by intro pf rw[qDd,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),List.foldl_cons] intro k q hq have:=H (k+1) q (by simpa using hq) rw[usz t n ht,show t.toNat+1+k=t.toNat+(k+1) by omega] exact this def C_Dw (wd:FloatArray) (W:Array Word) (I O:Nat):Prop := wd.size=W.size->∀ j t,j< O->t< I->wd.data[t*O+j]! = dv W[j*I+t]! lemma qHv {O t s j k:Nat} (hj:j< O) (hk:k< O) (h:t*O+j=s*O+k):t=s∧j=k:=by have h0:0< O :=א;have h1:=congrArg (.%O) h;have h2:=congrArg (./O) h;simp only[Nat.mul_comm _ O,Nat.mul_add_mod,Nat.mod_eq_of_lt hj,Nat.mod_eq_of_lt hk,Nat.mul_add_div h0,Nat.div_eq_of_lt hj,Nat.div_eq_of_lt hk] at h1 h2;omega def C_Dp (a:FloatArray) (W:Array Word) (I O J T:Nat):Prop := a.size=W.size∧∀ j t,j< O->t< I->(j< J∨(j=J∧t< T))->a.data[t*O+j]! = dv W[j*I+t]! lemma qIo {W:Array UInt32} {I O J:Nat} (sW:W.size<2^32) (hW:O*I=W.size) (pI:0< I) (hJ:J< O) (d:Nat) : ∀ (t:USize) (a:FloatArray),t.toNat+d=I->C_Dp a W I O J t.toNat → C_Dp (wdG W (J.toUSize*I.toUSize) I.toUSize O.toUSize J.toUSize t a) W I O J I:=by have hI:I.toUSize.toNat=I:=by have:I<=O*I:=Nat.le_mul_of_pos_left I א exact qAd _ א have hO:O.toUSize.toNat=O:=by have:O<=O*I:=ذ O pI exact qAd _ א have hj:J.toUSize.toNat=J:=by have:O<=O*I:=ذ O pI exact qAd _ א induction d with | zero => intro t a he H have ht:¬ t< I.toUSize:=by rw[А,hI];omega rw[wdG,П ht] have e:t.toNat=I :=א rw[e] at H;exact H | succ d ih => intro t a he H have ht:t< I.toUSize:=by rw[А,hI];omega have hu:=usz t I.toUSize ht rw[wdG,Р ht] obtain ⟨h1,h2⟩:=H have htI:t.toNat+1<=I :=א have b1:(t.toNat+1)*O<=I*O:=Ф _ htI rw[щ,о,ж I O,hW] at b1 have e1:(t*O.toUSize).toNat=t.toNat*O:=by rw[qC1 _ _ (by rw[hO];omega),hO] have e2:(t*O.toUSize+J.toUSize).toNat=t.toNat*O+J:=by rw[qAf _ _ (by rw[e1,hj];omega),e1,hj] have b2:(J+1)*I<=O*I:=Ф _ hJ rw[щ,о,hW] at b2 have e3:(J.toUSize*I.toUSize).toNat=J*I:=by rw[qC1 _ _ (by rw[hj,hI];omega),hj,hI] apply ih (t+1) _ (by rw[hu];omega) rw[hu] refine ⟨by rw[qCc];exact h1,λj s a1 a2 a3=>?_⟩ rw[qCv _ _ _ _ א (by rw[e2];omega),e2] by_cases e:s*O+j=t.toNat*O+J . obtain ⟨c1,c2⟩:=qHv a1 hJ e;rw[if_pos e,qAh _ _ sW,qAf _ _ (by rw[e3];omega),e3,c1,c2] . rw[if_neg e];refine h2 j s a1 a2 ?_;rcases a3 with a3 | ⟨a3,a4⟩;. exact .inl a3 . right;refine ⟨a3,?_⟩;by_cases c:s=t.toNat;. rw[c,a3] at e;exact absurd rfl e . omega section variable {x:FloatArray} {ix W:Array UInt32} {w0 w1 w2 w3 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) lemma qH7 {W:Array UInt32} {I O:Nat} (sW:W.size<2^32) (hW:O*I=W.size) (pI:0< I) (d:Nat) : ∀ (J:Nat) (a:FloatArray),J+d=O->C_Dp a W I O J 0 → C_Dp (wdR W I.toUSize O.toUSize O J a) W I O O 0:=by induction d with | zero => intro J a he H rw[wdR,if_neg א] have e:J=O :=א rw[e] at H;exact H | succ d ih => intro J a he H rw[wdR,if_pos א] apply ih (J+1) _ א have K:=qIo sW hW pI (א:J< O) I 0 a (by rw[C_z];omega) (by rw[C_z];exact H) refine ⟨K.1,λj t a1 a2 a3=>K.2 j t a1 a2 ?_⟩ rcases a3 with a3 | a3 . by_cases c:j=J;. exact .inr ⟨c,a2⟩ . exact .inl א . omega lemma qA4 {W:Array UInt32} {wd:FloatArray} {ju O m w i:USize} {k:Nat} (hs:wd.size<2^32) (hd:wd.size=m.toNat*O.toNat) (hv:C_Dw wd W m.toNat O.toNat) (hw:wd.size=W.size) (hk:ju.toNat+k< O.toNat) (e:w.toNat=(ju.toNat+k)*m.toNat) (him:i.toNat< m.toNat) : wd.data[(i*O+ju+USize.ofNat k).toNat]! = dv W[(w+i).toNat]!:=by have h1:(ju.toNat+k+1)*m.toNat<=O.toNat*m.toNat:=Ф _ hk;rw[щ,о,ж O.toNat,<-hd] at h1;have h4:(i.toNat+1)*O.toNat<=m.toNat*O.toNat:=Ф _ him;rw[щ,о,<-hd] at h4;have e0:=qC1 i O א;have e1:(i*O+ju).toNat=i.toNat*O.toNat+ju.toNat:=by rw[qAf _ _ (by rw[e0];omega),e0] have e2:=uk k (א:k<2^32);have e3:(i*O+ju+USize.ofNat k).toNat=i.toNat*O.toNat+(ju.toNat+k):=by rw[qAf _ _ (by rw[e1,e2];omega),e1,e2];omega have e4:(w+i).toNat=(ju.toNat+k)*m.toNat+i.toNat:=by rw[qAf _ _ (by rw[e];omega),e] rw[e3,e4];exact hv hw _ _ hk him lemma qH8 (hv:∀ j,j< n.toNat->∃ w,good w=true∧x.data[j]! = dv w) (k:Nat) : ∀ (t:USize) (a b c d:Float),t.toNat+k=n.toNat → 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.toNat< n.toNat :=א unfold go4B go1 simp only[dite_eq_left ht] obtain ⟨w,g,e⟩:=hv _ htn have B:∀ (u:UInt32) p0,dvB (x.uget t p0) u=x.uget t p0*dv u:=by intro u p0 rw[qC8,e] exact qIf 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 qEi {wd:FloatArray} {ju O:USize} (hs':wd.size<2^32) (hd:wd.size=m.toNat*O.toNat) (hj:ju.toNat+4<=O.toNat) (hw:wd.size=W.size) (hv:C_Dw wd W m.toNat O.toNat) (e0:w0.toNat=(ju.toNat+0)*m.toNat) (e1:w1.toNat=(ju.toNat+1)*m.toNat) (e2:w2.toNat=(ju.toNat+2)*m.toNat) (e3:w3.toNat=(ju.toNat+3)*m.toNat) (k:Nat) : ∀ (t:USize) (a b c d:Float),t.toNat+k=n.toNat → go4T x wd ix ju O m n t hx hi hs' hd hj 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 go4T 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.toNat< n.toNat :=א unfold go4T 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) have Fw:=λu p=>qC8 wd u p have Fa:=λu p=>qDd W u p split . rename_i hm;have him:=А.mp hm;have B0:=qA4 (k:=0) hs' hd hv hw א e0 him;have B1:=qA4 (k:=1) hs' hd hv hw א e1 him;have B2:=qA4 (k:=2) hs' hd hv hw א e2 him;have B3:=qA4 (k:=3) hs' hd hv hw א e3 him;simp only[J,Fw,Fa,B0,B1,B2,B3] . simp only[J] end section variable {x:FloatArray} {ix W:Array UInt32} {w0 w1 w2 w3 w4 w5 w6 w7 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) (h4:w4.toNat+m.toNat<=W.size) (h5:w5.toNat+m.toNat<=W.size) (h6:w6.toNat+m.toNat<=W.size) (h7:w7.toNat+m.toNat<=W.size) lemma qHn {wd:FloatArray} {ju O:USize} (hs':wd.size<2^32) (hd:wd.size=m.toNat*O.toNat) (hj:ju.toNat+8<=O.toNat) (hw:wd.size=W.size) (hv:C_Dw wd W m.toNat O.toNat) (e0:w0.toNat=(ju.toNat+0)*m.toNat) (e1:w1.toNat=(ju.toNat+1)*m.toNat) (e2:w2.toNat=(ju.toNat+2)*m.toNat) (e3:w3.toNat=(ju.toNat+3)*m.toNat) (e4:w4.toNat=(ju.toNat+4)*m.toNat) (e5:w5.toNat=(ju.toNat+5)*m.toNat) (e6:w6.toNat=(ju.toNat+6)*m.toNat) (e7:w7.toNat=(ju.toNat+7)*m.toNat) (k:Nat) : ∀ (t:USize) (s0 s1 s2 s3 s4 s5 s6 s7:Float) (r:F8C),t.toNat+k=n.toNat → go8C x wd ix ju O m n t hx hi hs' hd hj s0 s1 s2 s3 s4 s5 s6 s7 r = ⟨r.u,go1 x ix W w0 m n t hx hi hs h0 s0,go1 x ix W w1 m n t hx hi hs h1 s1,go1 x ix W w2 m n t hx hi hs h2 s2,go1 x ix W w3 m n t hx hi hs h3 s3,go1 x ix W w4 m n t hx hi hs h4 s4,go1 x ix W w5 m n t hx hi hs h5 s5,go1 x ix W w6 m n t hx hi hs h6 s6,go1 x ix W w7 m n t hx hi hs h7 s7⟩:=by induction k with | zero => intro t s0 s1 s2 s3 s4 s5 s6 s7 r he have hn:¬ t< n:=by rw[А];omega unfold go8C go1 simp only[dite_eq_right hn] | succ k ih => intro t s0 s1 s2 s3 s4 s5 s6 s7 r he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א unfold go8C go1 simp only[dite_eq_left ht] have J:=λs0 s1 s2 s3 s4 s5 s6 s7=>ih (t+1) s0 s1 s2 s3 s4 s5 s6 s7 r (by rw[usz t n ht];omega) have Fw:=λu p=>qC8 wd u p have Fa:=λu p=>qDd W u p split . rename_i hm;have him:=А.mp hm;have B0:=qA4 (k:=0) hs' hd hv hw א e0 him;have B1:=qA4 (k:=1) hs' hd hv hw א e1 him;have B2:=qA4 (k:=2) hs' hd hv hw א e2 him;have B3:=qA4 (k:=3) hs' hd hv hw א e3 him;have B4:=qA4 (k:=4) hs' hd hv hw א e4 him;have B5:=qA4 (k:=5) hs' hd hv hw א e5 him;have B6:=qA4 (k:=6) hs' hd hv hw א e6 him;have B7:=qA4 (k:=7) hs' hd hv hw א e7 him;simp only[J,Fw,Fa,B0,B1,B2,B3,B4,B5,B6,B7] . simp only[J] end def C_sp (I O:Nat) (Y:Bool) (X W b:Array Word) (q:Nat):Word := C_out I Y X W b (q/O) (q%O) lemma qCi (I O:Nat) (Y:Bool) (X W b:Array Word) {i j:Nat} (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 qAq {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:USize} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q< p+1) (wrW out u v) := qA9 (qAl hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qCk {u:USize} {p:Nat} (hu:u.toNat=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 qGx {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:USize} {v0 v1 v2 v3:Word} (hu:u.toNat=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,-⟩:=qCk hu א exact qAq hN (qAq hN (qAq hN (qAq hN h hu א h0) a1 א h1) a2 א h2) a3 א h3 lemma qHr {X:Array UInt32} {a n:Nat} (hX:X.size<2^32) (hn:a+n<=X.size) (hg:∀ t,t< n->good X[a+t]! = true) {ix:Array UInt32} {buf:FloatArray} (h1:ix.size=n) (h2:buf.size=n) : C_Dec (λp=>X[a+p]!) n n (decR X a.toUSize n.toUSize 0 ix buf 0 0).ix (decR X a.toUSize n.toUSize 0 ix buf 0 0).buf (decR X a.toUSize n.toUSize 0 ix buf 0 0).c (decR X a.toUSize n.toUSize 0 ix buf 0 0).l:=by have eo:a.toUSize.toNat=a:=qAd _ א have ek:n.toUSize.toNat=n:=qAd _ א have D:=qHp (X:=X) (o:=a.toUSize) (n:=n.toUSize) (λp=>X[a+p]!) (by rw[ek];omega) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[qAh _ _ 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 qF6 (xw:Nat->Word) (n:Nat):(C_keep xw n).Nodup∧(∀ t∈C_keep xw n,t< n) ∧ ∀ t,t< n->t∉C_keep xw n->xw t &&& В=0 := ⟨List.nodup_range.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:Nat) (X:Array Word) (i:Nat) (ix:Array UInt32) (x:FloatArray) (c:USize) (L:List Nat) (l:Float):Prop where len:c.toNat=L.length cle:L.length<=I six:ix.size=I sx:x.size=I nd:L.Nodup lt:∀ t∈L,t< I hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0 rd:∀ k p:Nat,L[k]?=some p → (ix[k]! : UInt32).toNat=p∧x.data[k]! = dv X[i*I+p]! ok:l=L.foldl (λ(a:Float) t=>a+(dv X[i*I+t]!).abs) 0 section variable {R I O:Nat} {X W b:Array Word} 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:Nat} {u:USize} (hu:u.toNat=j) (hj:j< O):(u*I.toUSize).toNat=j*I ∧ (u*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by have h1:j*I+I<=O*I:=qBl hj (le_refl _) have h2:=G.hW have h3:=G.sW have e:(u*I.toUSize).toNat=j*I:=by rw[qBg _ _ (by rw[hu,G.nm];omega),hu,G.nm] exact ⟨e,by rw[e,G.nm];omega⟩ lemma qB4 (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) {hv:Word} (Hv:C_Hv W hv) {ix:Array UInt32} {x:FloatArray} {c:USize} {L:List Nat} {l:Float} (Rw:C_Row I X i ix x c L l) {hc:Word} (Hb:C_Hv b hc) {th:UInt64} (ht:th=thOf (nfOf I*(dv hv*l+dv hc))) {u:USize} (hu:u.toNat=j) (hx:c.toNat<=x.size) (hi':c.toNat<=ix.size) (hs:W.size<2^32) (h0:(u*I.toUSize).toNat+I.toUSize.toNat<=W.size) : finT I Y X W b i j (go1 x ix W (u*I.toUSize) I.toUSize c 0 hx hi' hs h0 0) (nfOf I) l hv (rdW b u) I.toUSize th=C_out I Y X W b i j:=by have e:=qIn 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[qAh _ _ G.sb,hu,ht,Rw.ok] exact qGk G Y hi hj Hv.1 Hv.2.1 (λt ht=>Hv.2.2.2 _ (by rw[<-G.hW];exact qCm hj ht)) Hb.1 Hb.2.1 (Hb.2.2.2 _ (by rw[<-G.hb];exact hj)) L Rw.nd Rw.lt Rw.hz e end lemma qDl {R O i j:Nat} (hi:i< R) (hN:R*O<2^32) (h:j+4<=O) : j< O∧j+1< O∧j+2< O∧j+3< O∧j<2^32∧j+3<2^32∧i*O+j<2^32 ∧ i*O+j+3< R*O:=by have hP:=qCm hi (א:j+3< O) omega lemma qI2 {R O i j:Nat} (hi:i< R) (hN:R*O<2^32) (h4:¬ j+4<=O) (hO:4<=O) : O-4+4<=O∧i*O+O=i*O+(O-4)+4∧∀ q,q< i*O+(O-4)->q< i*O+j :=⟨by omega,by omega,λ_ _=>by omega⟩ lemma qE3 {j d O k:Nat} (hk:0< k) (h:j+k<=O) (he:j+d=O) : d-k< d∧j+k+(d-k)=O :=א lemma qHt (a j:Nat):a+(j+8)=a+(j+4)+4 :=א lemma qHw {a j q:Nat} (h:q< a+(j+4)):q< a+j+4 :=א lemma qDa (G:C_G R I O X W b) (Y:Bool) {i:Nat} (hi:i< R) {hv:Word} (Hv:C_Hv W hv) {ix:Array UInt32} {x:FloatArray} {c:USize} {L:List Nat} {l:Float} (Rw:C_Row I X i ix x c L l) {hc:Word} (Hb:C_Hv b hc) {th:UInt64} (ht:th=thOf (nfOf I*(dv hv*l+dv hc))) {base:USize} (hb:base.toNat=i*O) (h1c:c.toNat<=x.size) (h2c:c.toNat<=ix.size) {k:Nat} {u:USize} (hu:u.toNat=k) (hk:k+4<=O) (g0:(u*I.toUSize).toNat+I.toUSize.toNat<=W.size) (g1:((u+1)*I.toUSize).toNat+I.toUSize.toNat<=W.size) (g2:((u+2)*I.toUSize).toNat+I.toUSize.toNat<=W.size) (g3:((u+3)*I.toUSize).toNat+I.toUSize.toNat<=W.size) {out:Array Word} (H:C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+k) out) : C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+k+4) (w4 I Y X W b i k (nfOf I) l hv I.toUSize th u base (go1 x ix W (u*I.toUSize) I.toUSize c 0 h1c h2c G.sW g0 0) (go1 x ix W ((u+1)*I.toUSize) I.toUSize c 0 h1c h2c G.sW g1 0) (go1 x ix W ((u+2)*I.toUSize) I.toUSize c 0 h1c h2c G.sW g2 0) (go1 x ix W ((u+3)*I.toUSize) I.toUSize c 0 h1c h2c G.sW g3 0) out):=by have hN:=G.sO obtain ⟨b0,b1,b2,b3,-,l3,l0,l1⟩:=qDl hi hN hk obtain ⟨e1,e2,e3,-⟩:=qCk hu l3 have a0:(base+u).toNat=i*O+k:=by rw[qAf _ _ (by rw[hb,hu];exact l0),hb,hu] have o0:=qB4 G Y hi b0 Hv Rw Hb ht hu h1c h2c G.sW g0 have o1:=qB4 G Y hi b1 Hv Rw Hb ht e1 h1c h2c G.sW g1 have o2:=qB4 G Y hi b2 Hv Rw Hb ht e2 h1c h2c G.sW g2 have o3:=qB4 G Y hi b3 Hv Rw Hb ht e3 h1c h2c G.sW g3 have hsq:∀ m,k+m< O->C_out I Y X W b i (k+m)=C_sp I O Y X W b (i*O+k+m):=λm hm=>by rw[ц];exact (qCi I O Y X W b hm).symm unfold w4 dsimp only rw[o0,o1,o2,o3] exact qGx hN H a0 l1 (hsq 0 b0) (hsq 1 b1) (hsq 2 b2) (hsq 3 b3) set_option maxHeartbeats 1000000 in lemma qF1 (G:C_G R I O X W b) (Y:Bool) {i:Nat} (hi:i< R) {hv:Word} (Hv:C_Hv W hv) {ix:Array UInt32} {x wd:FloatArray} {c:USize} {L:List Nat} {l:Float} (Rw:C_Row I X i ix x c L l) {hc:Word} (Hb:C_Hv b hc) {th:UInt64} (ht:th=thOf (nfOf I*(dv hv*l+dv hc))) {base:USize} (hb:base.toNat=i*O) (Hd:C_Dw wd W I O) (d:Nat):∀ (j:Nat) (r0:F8C) (out:Array Word),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.toUSize c j base r0 th out):=by induction d using Nat.strong_induction_on with | _ d ih => intro j r0 out he H have hN:=G.sO have hOu:O.toUSize.toNat=O:=qAd _ (by have h5:O<=R*O:=Nat.le_mul_of_pos_left O א omega) have sW:W.size<=4294967295:=by have:=G.sW;omega have hle:=Rw.cle have h1c:c.toNat<=x.size:=by rw[Rw.sx,Rw.len];exact hle have h2c:c.toNat<=ix.size:=by rw[Rw.six,Rw.len];exact hle have Hd':wd.size=W.size->C_Dw wd W I.toUSize.toNat O.toUSize.toNat:=λ_=>by rw[G.nm,hOu];exact Hd by_cases h8:j+8<=O∧c.toNat<=x.size∧c.toNat<=ix.size∧wd.size<=4294967295∧wd.size=W.size ∧ wd.size=I.toUSize.toNat*O.toUSize.toNat∧j.toUSize.toNat+8<=O.toUSize.toNat . have k4:j+4<=O:=Nat.le_trans (Nat.add_le_add_left (by decide) j) h8.1;obtain ⟨b0,b1,b2,b3,l0,l3,-,-⟩:=qDl hi hN k4;obtain ⟨b4,b5,b6,b7,l4,l7,-,-⟩:=qDl hi hN (show j+4+4<=O from h8.1);obtain ⟨dd,de⟩:=qE3 (Nat.succ_pos 7) h8.1 he;have e0:j.toUSize.toNat=j:=qAd _ l0;obtain ⟨e1,e2,e3,e4'⟩:=qCk e0 l3;have e4:=e4' l4;obtain ⟨e5,e6,e7,-⟩:=qCk e4 l7;have Z:∀ k {u:USize},j+k< O->u.toNat=j+k → (u*I.toUSize).toNat=(j.toUSize.toNat+k)*I.toUSize.toNat:=λk u hk hu=>by rw[(G.wj hu hk).1,e0,G.nm] have g0:=(G.wj e0 b0).2;have g1:=(G.wj e1 b1).2;have g2:=(G.wj e2 b2).2;have g3:=(G.wj e3 b3).2;have g4:=(G.wj e4 b4).2;have g5:=(G.wj e5 b5).2;have g6:=(G.wj e6 b6).2;have g7:=(G.wj e7 b7).2;rw[cols1,Р h8];dsimp only;rw[qHn h1c h2c G.sW g0 g1 g2 g3 g4 g5 g6 g7 (qAx h8.2.2.2.1) h8.2.2.2.2.2.1 h8.2.2.2.2.2.2 h8.2.2.2.2.1 (Hd' h8.2.2.2.2.1) (Z 0 b0 e0) (Z 1 b1 e1) (Z 2 b2 e2) (Z 3 b3 e3) (Z 4 b4 e4) (Z 5 b5 e5) (Z 6 b6 e6) (Z 7 b7 e7) c.toNat 0 _ _ _ _ _ _ _ _ _ (Х _)];dsimp only;refine ih (d-8) dd (j+8) _ _ de ?_;rw[qHt (i*O) j];refine qDa G Y hi Hv Rw Hb ht hb h1c h2c e4 h8.1 g4 g5 g6 g7 ?_;exact qA9 (qDa G Y hi Hv Rw Hb ht hb h1c h2c e0 k4 g0 g1 g2 g3 H) λq _ (h:q< i*O+(j+4))=>qHw h by_cases h4:j+4<=O . obtain ⟨b0,b1,b2,b3,l0,l3,-,-⟩:=qDl hi hN h4;obtain ⟨dd,de⟩:=qE3 (Nat.succ_pos 3) h4 he;have e0:j.toUSize.toNat=j:=qAd _ l0;obtain ⟨e1,e2,e3,-⟩:=qCk e0 l3;have g0:=(G.wj e0 b0).2;have g1:=(G.wj e1 b1).2;have g2:=(G.wj e2 b2).2;have g3:=(G.wj e3 b3).2;rw[cols1,П h8,Р h4];dsimp only;rw[Р ⟨h1c,h2c,sW,g0,g1,g2,g3⟩];split;. rename_i hd;rw[qEi h1c h2c G.sW g0 g1 g2 g3 (qAx hd.2.2.1) hd.2.1 hd.2.2.2 hd.1 (Hd' hd.1) (by rw[(G.wj e0 b0).1,e0,G.nm];rfl) (by rw[(G.wj e1 b1).1,e0,G.nm]) (by rw[(G.wj e2 b2).1,e0,G.nm]) (by rw[(G.wj e3 b3).1,e0,G.nm]) c.toNat 0 _ _ _ _ (Х _)];dsimp only;refine ih (d-4) dd (j+4) _ _ de ?_;rw[<-ц (i*O) j 4];exact qDa G Y hi Hv Rw Hb ht hb h1c h2c e0 h4 g0 g1 g2 g3 H . rw[qH8 _ _ _ _ _ _ _ (λ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.toNat 0 _ _ _ _ (Х _)] dsimp only;refine ih (d-4) dd (j+4) _ _ de ?_;rw[<-ц (i*O) j 4];exact qDa G Y hi Hv Rw Hb ht hb h1c h2c e0 h4 g0 g1 g2 g3 H . by_cases hT:j< O∧4<=O∧c.toNat<=x.size∧c.toNat<=ix.size∧wd.size<=4294967295∧wd.size=W.size ∧ wd.size=I.toUSize.toNat*O.toUSize.toNat∧(O-4).toUSize.toNat+4<=O.toUSize.toNat;. obtain ⟨z4,z1,z2⟩:=qI2 hi hN h4 hT.2.1;obtain ⟨b0,b1,b2,b3,l0,l3,-,-⟩:=qDl hi hN z4;have e0:(O-4).toUSize.toNat=O-4:=qAd _ l0;obtain ⟨e1,e2,e3,-⟩:=qCk e0 l3;have g0:=(G.wj e0 b0).2;have g1:=(G.wj e1 b1).2;have g2:=(G.wj e2 b2).2;have g3:=(G.wj e3 b3).2;rw[cols1,П h8,П h4,Р hT];dsimp only;rw[qEi h1c h2c G.sW g0 g1 g2 g3 (qAx hT.2.2.2.2.1) hT.2.2.2.2.2.2.1 hT.2.2.2.2.2.2.2 hT.2.2.2.2.2.1 (Hd' hT.2.2.2.2.2.1) (by rw[(G.wj e0 b0).1,e0,G.nm];rfl) (by rw[(G.wj e1 b1).1,e0,G.nm]) (by rw[(G.wj e2 b2).1,e0,G.nm]) (by rw[(G.wj e3 b3).1,e0,G.nm]) c.toNat 0 _ _ _ _ (Х _)];dsimp only;rw[z1];exact qDa G Y hi Hv Rw Hb ht hb h1c h2c e0 z4 g0 g1 g2 g3 (qA9 H λq _ (h:q< i*O+(O-4))=>z2 q h) by_cases h1:j< O . have hP:=qCm hi h1;have l0:j<2^32:=Nat.lt_of_le_of_lt (Nat.le_add_left _ _) (Nat.lt_trans hP hN);obtain ⟨dd,de⟩:=qE3 (Nat.succ_pos 0) h1 he;have e0:j.toUSize.toNat=j:=qAd _ l0;have g0:=(G.wj e0 h1).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAf _ _ (by rw[hb,e0];exact Nat.lt_trans hP hN),hb,e0] have o0:=qB4 G Y hi h1 Hv Rw Hb ht e0 h1c h2c G.sW g0;rw[cols1,П h8,П h4,П hT,Р h1];dsimp only;rw[Р ⟨h1c,h2c,sW,g0⟩];rw[o0];exact ih (d-1) dd (j+1) _ _ de (qAq hN H a0 hP ((qCi I O Y X W b h1).symm)) . rw[cols1,П h8,П h4,П hT,П h1];have e:j=O:=Nat.le_antisymm (Nat.le.intro he) (Nat.le_of_not_lt h1);rw[e] at H;exact H lemma qF4 (G:C_G R I O X W b) (Y:Bool) {hv:Word} (Hv:C_Hv W hv) {hc:Word} (Hb:C_Hv b hc) {wd:FloatArray} (Hd:C_Dw wd W I O) (d:Nat):∀ (i:Nat) (ix:Array UInt32) (buf:FloatArray) (out:Array Word),i+d=R->ix.size=I->buf.size=I → 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 hc I.toUSize 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:=qBl hi (le_refl _) have hbs:(i*O).toUSize.toNat=i*O:=by have h3:i*O+O<=R*O:=qBl hi (le_refl _) exact qAd _ א obtain ⟨d1,d2,d3,d4,d5⟩:=qHr sX א (G.gx hi) h1 h2 obtain ⟨k1,k2,k3⟩:=qF6 (λp=>X[i*I+p]!) I have Rw:=C_Row.mk d1 (qB3 _ _) d2 d3 k1 k2 k3 d4 d5 rw[rows1,Р hi] dsimp only refine ih (i+1) _ _ _ א d2 d3 ?_ have h5:=qF1 G Y hi Hv Rw Hb rfl hbs Hd O 0 ⟨0,0,0,0,0,0,0,0,0⟩ out א (by rw[ػ];exact H) rw[show (i+1)*O=i*O+O by rw[щ,о]] exact h5 end lemma qFr (xs:FloatArray) (v:Float) (q:Nat) : (xs.push v).data[q]! = if q=xs.size then v else xs.data[q]!:=by obtain ⟨ds⟩:=xs;show (ds.push v)[q]! = if q=ds.size then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.push v).size:=by simp;omega rw[Ю _ q h3,Ю ds q hq,Ц א,Array.getElem_push_lt] . by_cases e:q=ds.size . subst e;have h3:ds.size<(ds.push v).size:=by simp rw[Ю (ds.push v) ds.size h3,ф rfl,Array.getElem_push_eq] . have h3:¬ q<(ds.push v).size:=by simp;omega rw[ك _ q h3,ك ds q hq,Ц e] section variable {rows inW outW:Nat} {X W b:Array Word} lemma qHo (G:C_G rows inW outW X W b) (relu:Bool) {i j:Nat} (hi:i< rows) (hj:j< outW) {s a:Float} {hh:Word} (hg:good hh=true) (hm:∀ t,t< inW->X[i*inW+t]! &&& В<=hh) (hp:hh<=В) (hs:let r:=(О inW).foldl (λ(acc:F1) t=>let v:=dv W[j*inW+t]! F1.mk (acc.s+v*dv X[i*inW+t]!) (acc.t+v.abs)) ⟨0,0⟩;s=r.s∧a=r.t) : fin8 inW relu X W b i j s (nfOf inW) a hh b[j]! inW.toUSize=C_out inW relu X W b i j:=by obtain ⟨h1,h2⟩:=hs exact qDt G relu hi hj λh => qDr inW G.ni (λt=>X[i*inW+t]!) (λt=>W[j*inW+t]!) b[j]! hh (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb];exact hj)) hg hm hp _ _ ⟨by rw[h1],by rw[h2]⟩ h lemma qE4 (xs:FloatArray) (a:Nat->Float) (r:Nat) (hr:r<8) : (((((((((xs.push (a 0)).push (a 1)).push (a 2)).push (a 3)).push (a 4)).push (a 5)).push (a 6)).push (a 7))).data[xs.size+r]! = a r:=by simp only[qFr,qAs] interval_cases r<;>repeat (first | rw[ф א] | rw[Ц א]) lemma qE7 (xs:FloatArray) (a0 a1 a2 a3 a4 a5 a6 a7:Float) (q:Nat) (hq:q< xs.size) : (((((((((xs.push a0).push a1).push a2).push a3).push a4).push a5).push a6).push a7)).data[q]! = xs.data[q]!:=by simp only[qFr,qAs] rw[Ц א,Ц א,Ц א,Ц א,Ц א,Ц א,Ц א,Ц א] def C_hf (d:H8):Nat->UInt32 | 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):Nat->Float | 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:Array Word) (m t:Nat) (d:H8):Prop := d.xs.size=8*t∧(∀ k r:Nat,k< t->r<8->d.xs.data[8*k+r]! = dv X[r*m+k]!) ∧ ∀ r:Nat,r<8->C_hok (λk=>X[r*m+k]!) t (C_hf d r) lemma qBz {n t c:USize} {k:Nat} (hc:c.toNat=k) (h:k*n.toNat+t.toNat<2^32) : (c*n+t).toNat=k*n.toNat+t.toNat:=by have h0:k*n.toNat<2^32 :=א;rw[qAf _ _ (by rw[qBg _ _ (by rw[hc];exact h0),hc];exact h),qBg _ _ (by rw[hc];exact h0),hc] lemma qCl {X:Array Word} {n t c:USize} {k:Nat} (hs:X.size<2^32) (hc:c.toNat=k) (h:k*n.toNat+t.toNat<2^32) : rdW X (c*n+t)=X[k*n.toNat+t.toNat]!:=by rw[qAh _ _ hs,qBz hc h] lemma qG8 {X:Array Word} {n:USize} (hs:X.size<2^32) (h8:8*n.toNat<=X.size) (hg:∀ q,q< X.size->good X[q]! = true) (d:Nat) : ∀ (t:USize) (xs:FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7:UInt32),t.toNat+d=n.toNat → C_D8 X n.toNat t.toNat ⟨xs,h0,h1,h2,h3,h4,h5,h6,h7⟩ → C_D8 X n.toNat n.toNat (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.toNat=n.toNat :=א 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:USize).toNat=2:=uk 2 ב have l3:(3:USize).toNat=3:=uk 3 ב have l4:(4:USize).toNat=4:=uk 4 ב have l5:(5:USize).toNat=5:=uk 5 ב have l6:(6:USize).toNat=6:=uk 6 ב have l7:(7:USize).toNat=7:=uk 7 ב have r0:rdW X t=X[0*n.toNat+t.toNat]!:=by rw[qAh _ _ hs,Nat.zero_mul,Х] have r1:rdW X (n+t)=X[1*n.toNat+t.toNat]!:=by rw[qAh _ _ hs,qAf _ _ א,о] rw[dec8,Р ht] dsimp only rw[r0,r1,qCl hs l2 א,qCl hs l3 א,qCl hs l4 א,qCl hs l5 א,qCl hs l6 א,qCl hs l7 א] obtain ⟨H1,H2,H3⟩:=H have hgood:∀ r,r<8->good X[r*n.toNat+t.toNat]! = true:=by intro r hr apply hg interval_cases r<;>omega dsimp only at H1 H2 refine ih (t+1) _ _ _ _ _ _ _ _ _ א ⟨?_,?_,?_⟩ . simp only[qAs,H1,hu];omega . intro k r hk hr;rw[hu] at hk;by_cases e:k=t.toNat;. subst e;have h9:=qE4 xs (λr=>dv X[r*n.toNat+t.toNat]!) r hr;rw[H1] at h9;exact h9 . have h10:8*k+r< xs.size :=א;rw[qE7 _ _ _ _ _ _ _ _ _ _ h10];exact H2 k r א hr . intro r hr;have h9:=H3 r hr;have h10:=hgood r hr;rw[hu];interval_cases r<;>exact qD5 h10 h9 def C_cf (xf:Nat->Float) (ww:Nat->Word) (l:List Nat) (acc:F1):F1 := l.foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*xf t) (acc.t+v.abs)) acc lemma qBt {xs:FloatArray} {i t n:USize} {xf:Nat->Nat->Float} (hxf:∀ r k:Nat,r<8->k< n.toNat->xs.data[8*k+r]! = xf r k) (hi:i.toNat=8*t.toNat) (ht:t.toNat< n.toNat) (hsx:xs.size<2^32) (hx:8*n.toNat<=xs.size) (r:Nat) (hr:r<8) (pf:(i+USize.ofNat r).toNat< xs.size) : xs.uget (i+USize.ofNat r) pf=xf r t.toNat:=by rw[qC8,qAf _ _ (by rw[uk r א];omega),uk r א,hi,hxf r _ hr ht] lemma qHs {xs:FloatArray} {W:Array UInt32} {wo n:USize} (hx:8*n.toNat<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:wo.toNat+n.toNat<=W.size) (xf:Nat->Nat->Float) (ww:Nat->Word) (hxf:∀ r k:Nat,r<8->k< n.toNat->xs.data[8*k+r]! = xf r k) (hww:∀ k,k< n.toNat->W[wo.toNat+k]! = ww k) (d:Nat) : ∀ (t i:USize) (hi:i.toNat=8*t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a:Float) (p q:UInt32) (r0:F8),t.toNat+d=n.toNat->∀ r,r<8 → F1.mk (C_fs (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0) r) (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0).t = C_cf (xf r) ww (List.range' t.toNat d) ⟨C_fs ⟨0,s0,s1,s2,s3,s4,s5,s6,s7,a,0,0⟩ r,a⟩:=by induction d with | zero => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0 he r hr rw[go8,П (by rw[А];omega)] cases r0 interval_cases r<;>rfl | succ d ih => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0 he r hr have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א have hu:=usz t n ht have e:dv (W.uget (wo+t) (wi wo t n W hw hs htn))=dv (ww t.toNat):=by rw[qDd,qAf _ _ א,hww _ htn] rw[go8,Р ht] dsimp only rw[e,qBt hxf hi htn hsx hx 0 א,qBt hxf hi htn hsx hx 1 א,qBt hxf hi htn hsx hx 2 א,qBt hxf hi htn hsx hx 3 א,qBt hxf hi htn hsx hx 4 א,qBt hxf hi htn hsx hx 5 א,qBt hxf hi htn hsx hx 6 א,qBt hxf hi htn hsx hx 7 א] rw[ih (t+1) _ _ _ _ _ _ _ _ _ _ _ _ _ _ א r hr,hu,List.range'_succ] unfold C_cf rw[List.foldl_cons] interval_cases r<;>rfl lemma qGt {xs:FloatArray} {W:Array UInt32} {wo n:USize} (hx:8*n.toNat<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:wo.toNat+n.toNat<=W.size) {N:USize} (hN:N.toNat<=W.size) (hn:wo.toNat+n.toNat<=N.toNat) (d:Nat) : ∀ (t i:USize) (hi:i.toNat=8*t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a:Float) (p q:UInt32) (r0:F8),t.toNat+d=n.toNat → scanF W N (wo+t) hN p q = scanF W N (wo+n) hN (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0).p (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0).q:=by induction d with | zero => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0 he have e:t=n:=USize.toNat_inj.mp א subst e rw[go8,П (by rw[А];omega)] | succ d ih => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a p q r0 he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א have hu:=usz t n ht have e1:(wo+t).toNat=wo.toNat+t.toNat:=qAf _ _ א have l:wo+t< N:=by rw[А,e1];omega have e2:wo+t+1=wo+(t+1):=by rw[USize.add_assoc] rw[scanF,Р l,go8,Р ht] dsimp only rw[e2] exact ih (t+1) _ _ _ _ _ _ _ _ _ _ _ _ _ _ א lemma qF2 {outW q j:Nat} (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 qIj {n j:Nat} (hN:8*n<2^32) (hj:j< n) : n+j<2^32∧2*n+j<2^32∧3*n+j<2^32∧4*n+j<2^32∧5*n+j<2^32 ∧ 6*n+j<2^32∧7*n+j<2^32∧0*n+j<8*n∧1*n+j<8*n∧2*n+j<8*n ∧ 3*n+j<8*n∧4*n+j<8*n∧5*n+j<8*n∧6*n+j<8*n∧7*n+j<8*n ∧ j<2^32∧n<2^32 :=א lemma qEr {a:Nat} (h:a<2^32):a<=4294967295 :=א lemma qIq {j k n:Nat} (h:j+(k+1)=n):j+1+k=n :=א lemma qGh (G:C_G 8 inW outW X W b) (relu:Bool) {d:H8} (D:C_D8 X inW inW d) (k:Nat):∀ (j:Nat) (out:Array Word) (p1 q1:UInt32) (r0:F8),j+k=outW → C_Inv (8*outW) (C_sp inW outW relu X W b) (λq=>q%outW< j) out → C_Inv (8*outW) (C_sp inW outW relu X W b) (λq=>q%outW< outW) (cols8 inW outW relu X W b d (nfOf inW) inW.toUSize j out p1 q1 r0).out:=by induction k with | zero => intro j out p1 q1 r0 he H rw[cols8,П א] have e:j=outW :=א rw[e] at H;exact H | succ k ih => intro j out p1 q1 r0 he H have hj:j< outW :=א have hN:=G.sO have hbs:=G.sb have hob:=G.hb have hnm:=G.nm have hin:=G.ni have sxs:8*inW<=8*2^20 :=א obtain ⟨s1,s2,s3,s4,s5,s6,s7,w0,w1,w2,w3,w4,w5,w6,w7,lj,ln⟩:=qIj hN hj have e0:j.toUSize.toNat=j:=qAd _ lj have eo:outW.toUSize.toNat=outW:=qAd _ ln have g0:=G.wj e0 hj have hx:8*inW.toUSize.toNat<=d.xs.size:=by rw[hnm,D.1] have hsx:d.xs.size<2^32:=by rw[D.1];omega have sW:=G.sW have hg:=g0.2 have h8:8*outW<2^32:=hN have l2:(2:USize).toNat=2:=uk 2 ב have l3:(3:USize).toNat=3:=uk 3 ב have l4:(4:USize).toNat=4:=uk 4 ב have l5:(5:USize).toNat=5:=uk 5 ב have l6:(6:USize).toNat=6:=uk 6 ב have l7:(7:USize).toNat=7:=uk 7 ב have hi0:(0:USize).toNat=8*(0:USize).toNat:=by rw[show (0:USize).toNat=0 from rfl] have key:=qHs hx hsx sW hg (λr k=>dv X[r*inW+k]!) (λk=>W[j*inW+k]!) (λr k hr hk=>by rw[hnm] at hk;exact D.2.1 k r hk hr) (λk _=>by rw[g0.1]) inW 0 0 hi0 0 0 0 0 0 0 0 0 0 p1 q1 r0 (by rw[hnm,show (0:USize).toNat=0 from rfl,Х]) rw[show (0:USize).toNat=0 from rfl,<-List.range_eq_range'] at key rw[cols8,Р hj] dsimp only rw[Р ⟨hx,qEr hsx,qEr sW,hg⟩] generalize go8 d.xs W (j.toUSize*inW.toUSize) inW.toUSize 0 0 hx hsx sW hg hi0 0 0 0 0 0 0 0 0 0 p1 q1 r0=R at key ⊢ have O:∀ r,r<8->fin8 inW relu X W b r j (C_fs R r) (nfOf inW) R.t (C_hf d r) b[j]! inW.toUSize=C_sp inW outW relu X W b (r*outW+j):=by intro r hr rw[qCi _ _ _ _ _ _ hj] have k1:=key r hr have e1:C_fs ⟨0,0,0,0,0,0,0,0,0,0,0,0⟩ r=0:=by interval_cases r<;>rfl rw[e1] at k1 have H1:=D.2.2 r hr exact qHo G relu hr hj H1.1 H1.2.1 H1.2.2 ⟨(congrArg F1.s k1 :),(congrArg F1.t k1 :)⟩ have o0:fin8 inW relu X W b 0 j R.s0 (nfOf inW) R.t d.h0 b[j]! inW.toUSize=_ :=O 0 (by decide) have o1:fin8 inW relu X W b 1 j R.s1 (nfOf inW) R.t d.h1 b[j]! inW.toUSize=_ :=O 1 (by decide) have o2:fin8 inW relu X W b 2 j R.s2 (nfOf inW) R.t d.h2 b[j]! inW.toUSize=_ :=O 2 (by decide) have o3:fin8 inW relu X W b 3 j R.s3 (nfOf inW) R.t d.h3 b[j]! inW.toUSize=_ :=O 3 (by decide) have o4:fin8 inW relu X W b 4 j R.s4 (nfOf inW) R.t d.h4 b[j]! inW.toUSize=_ :=O 4 (by decide) have o5:fin8 inW relu X W b 5 j R.s5 (nfOf inW) R.t d.h5 b[j]! inW.toUSize=_ :=O 5 (by decide) have o6:fin8 inW relu X W b 6 j R.s6 (nfOf inW) R.t d.h6 b[j]! inW.toUSize=_ :=O 6 (by decide) have o7:fin8 inW relu X W b 7 j R.s7 (nfOf inW) R.t d.h7 b[j]! inW.toUSize=_ :=O 7 (by decide) rw[qAh _ _ hbs,e0,o0,o1,o2,o3,o4,o5,o6,o7] have u0:j.toUSize.toNat=0*outW+j:=by rw[e0,Nat.zero_mul,Х] have u1:(outW.toUSize+j.toUSize).toNat=1*outW+j:=by rw[qAf _ _ (by rw[eo,e0];exact s1),eo,e0,о] have a2:=qBz (n:=outW.toUSize) (t:=j.toUSize) l2 (by rw[eo,e0];exact s2) have a3:=qBz (n:=outW.toUSize) (t:=j.toUSize) l3 (by rw[eo,e0];exact s3) have a4:=qBz (n:=outW.toUSize) (t:=j.toUSize) l4 (by rw[eo,e0];exact s4) have a5:=qBz (n:=outW.toUSize) (t:=j.toUSize) l5 (by rw[eo,e0];exact s5) have a6:=qBz (n:=outW.toUSize) (t:=j.toUSize) l6 (by rw[eo,e0];exact s6) have a7:=qBz (n:=outW.toUSize) (t:=j.toUSize) l7 (by rw[eo,e0];exact s7) rw[eo,e0] at a2 a3 a4 a5 a6 a7 have I0:=qAl h8 H u0 w0 rfl have I1:=qAl h8 I0 u1 w1 rfl have I2:=qAl h8 I1 a2 w2 rfl have I3:=qAl h8 I2 a3 w3 rfl have I4:=qAl h8 I3 a4 w4 rfl have I5:=qAl h8 I4 a5 w5 rfl have I6:=qAl h8 I5 a6 w6 rfl have I7:=qAl h8 I6 a7 w7 rfl exact ih (j+1) _ _ _ _ (qIq he) (qA9 I7 (λq hq hq'=>qF2 hj hq hq')) def C_ck (c:C8):UInt32:=if 0x7FFFFF<=c.q∧c.p<Ы then c.p else Е lemma qFe {relu:Bool} {d:H8} (hW:outW*inW=W.size) (pi:0< inW) (ni:inW<2^20) (sW:W.size<2^32) (hd:d.xs.size=8*inW) (k:Nat) : ∀ (j:Nat) (out:Array Word) (p1 q1:UInt32) (r0:F8),j+k=outW → scanF W W.usize (j.toUSize*inW.toUSize) (qAc W) p1 q1 =C_ck (cols8 inW outW relu X W b d (nfOf inW) inW.toUSize j out p1 q1 r0):=by have hnm:inW.toUSize.toNat=inW:=qAd _ א have hu:=qBi W sW have hO:outW<2^32:=by have:=ذ outW pi omega have W1:∀ j,j<=outW->(j.toUSize*inW.toUSize).toNat=j*inW:=λj hj=>by have h1:j*inW<=outW*inW:=Ф _ hj rw[qBg _ _ (by rw[qAd _ א,hnm];omega),qAd _ א,hnm] induction k with | zero => intro j out p1 q1 r0 he have e:j=outW :=א rw[cols8,П א,scanF,П (by rw[А,W1 j א,hu,<-hW,e];exact Nat.lt_irrefl _)] rfl | succ k ih => intro j out p1 q1 r0 he have hj:j< outW :=א have hlt:j*inW+inW<=W.size:=by rw[<-hW];exact qBl hj (le_refl _) have w1:=W1 j hj.le have hg:(j.toUSize*inW.toUSize).toNat+inW.toUSize.toNat<=W.size:=by rw[w1,hnm];exact hlt have hx:8*inW.toUSize.toNat<=d.xs.size:=by rw[hnm,hd] have hsx:d.xs.size<2^32:=by rw[hd];omega have hi0:(0:USize).toNat=8*(0:USize).toNat:=by rw[C_z] have key:=qGt hx hsx sW hg (qAc W) (by rw[hu];exact hg) inW 0 0 hi0 0 0 0 0 0 0 0 0 0 p1 q1 r0 (by rw[C_z,hnm,Х]) rw[cols8,Р hj] dsimp only rw[Р ⟨hx,by omega,by omega,hg⟩] generalize go8 d.xs W (j.toUSize*inW.toUSize) inW.toUSize 0 0 hx hsx sW hg hi0 0 0 0 0 0 0 0 0 0 p1 q1 r0=R at key ⊢ have e4:j.toUSize*inW.toUSize+inW.toUSize=(j+1).toUSize*inW.toUSize:=by apply USize.toNat_inj.mp rw[W1 _ hj,qAf _ _ (by rw[w1,hnm];omega),w1,hnm,Nat.add_one_mul] rw[USize.add_zero,e4] at key rw[key] exact ih (j+1) _ _ _ _ א end lemma qEj:linF=linearSpec:=by funext rows inW outW relu X W b;unfold linF;dsimp only;have Hs:=qFw 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:=qAx h7;replace h8:=qAx h8;replace h9:=qAx h9;replace h10:=qAx h10 have G0:=λ(A:∀ q,q< W.size->good W[q]! = true) => (⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,(qBh 1 X h7 h11).2.2.1,A,(qBh 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:USize).toNat ⟨г (8*inW),0,0,0,0,0,0,0,0⟩:=by have e0:(0:USize).toNat=0:=rfl;rw[e0];refine ⟨rfl,λk r hk=>absurd hk (ع _),λr hr=>?_⟩;have g0:good 0=true:=rfl;interval_cases r<;>exact ⟨g0,λk hk=>absurd hk (ع _),(show (0:UInt32)<=В from by decide)⟩ have nm:inW.toUSize.toNat=inW:=qAd _ א;have D:=qG8 (n:=inW.toUSize) h7 (by rw[nm];exact h1.le) (qBh 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 hc have A:∀ q,q< W.size->good W[q]! = true:=by rcases hc with hc | hc . have k:=qFe (relu:=relu) (b:=b) (X:=X) h2 h4 h6 h8 D.1 outW 0 (Н (8*outW) 0) 0 Е ⟨0,0,0,0,0,0,0,0,0,0,0,0⟩ (Х _) have e0:(0:Nat).toUSize*inW.toUSize=0:=USize.toNat_inj.mp (by rw[qBg _ _ (by rw[qAd _ א,nm];omega),qAd _ א,nm,Nat.zero_mul];rfl) rw[e0] at k;have n0:scanF W W.usize 0 (qAc W) 0 Е≠Е:=by rw[k];unfold C_ck;rw[if_pos hc];exact λa=>by rw[a] at hc;exact absurd hc.2 (by decide) exact (qBw W h8 n0).2.2.1 . exact (qB5 W h8 hc).2.2.1 exact qC9 (qGh (G0 A) relu D outW 0 _ _ _ _ א H0) (λq _=>ъ _ h5) . rw[qAo];rfl . rename_i hr;have Hv:=Hs (by simpa using hr) 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[Nat.zero_mul] at h;exact absurd h (ع _)⟩;have P0:(pushZ inW.toUSize 0 (г inW)).size=inW:=by rw[qFz inW.toUSize inW 0 _ (by rw[G.nm,C_z,Х])];exact Х _ refine qC9 (qF4 G relu Hv (qBh 1 b h9 h13) ?_ rows 0 _ _ _ א ג P0 H0) (λq hq=>hq);split;. intro _ j t a1 a2;have K:=qH7 h8 h2 h4 outW 0 (FloatArray.mk (Н W.size 0)) (Х _) ⟨by simp[FloatArray.size],λj t _ _ h=>by omega⟩;exact K.2 j t a1 a2 (.inl a1) . intro h j t a1 a2;have e:0=W.size:=h;have:=Nat.mul_pos h5 h4;omega . rw[qAo];rfl end lemma qDs:linF=linearSpec:=qEj @[inline] def sm (w:UInt32):Bool:=good w && (w &&& В)<=0x70FFFFFF section open Float.Model Float.Model.UnpackedFloat def T_r (N:Nat):Nat := N/2^39+if 2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1) then 1 else 0 lemma qHy (N:Nat):FloatLib.Numerics.roundShiftRightEven N 39=T_r N:=by have a:Nat.shiftRight N 39=N/2^39:=ы N 39;have b:N-Nat.shiftLeft (N/2^39) 39=N%2^39:=by rw[show Nat.shiftLeft (N/2^39) 39=N/2^39*2^39 from ش _ _];omega rw[FloatLib.Numerics.roundShiftRightEven_def];simp only[show (39 == 0)=false from rfl,Bool.false_eq_true,ite_false,a,b,beq_iff_eq];unfold T_r;split_ifs<;>omega set_option maxRecDepth 8192 in lemma qGr (neg:Bool) (N:Nat) (e':Int) (hN1:2^62<=N) (hN2:N<2^63) (h1:-148<=e'+39) (h2:e'+39+150<=253) : toWord (Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) N e')) =UInt32.ofNat ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by have hr1:2^23<=T_r N:=by unfold T_r;split<;>omega have hr2:T_r N<=2^24:=by unfold T_r;split<;>omega have hT:Format.binary32.targetExponent (totalExponent N e')=e'+39:=by simp only[totalExponent,qDb N 62 hN1 hN2,qFg];omega have K:∀ (P:Nat) (E:Int) (hp:0< P),2^23<=P->P<2^24->-149<=E->E<=104 → (E+150).toNat*2^23+P%2^23=(e'+39+149).toNat*2^23+T_r N → toWord (Model.ofModel Fmt (.finite (Model.modelSign neg) P E hp)) =UInt32.ofNat ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by intro P E hp a b c d e simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBo _ _ _ _ a b א א] cases neg<;>simp[Model.modelSign]<;>omega rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ א,hT,Model.roundMantissaAtExponentEven,ф א,show (e'+39-e').toNat=39 by omega,qHy] by_cases hc:T_r N=2^24 . have h:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (Model.modelSign neg) (e'+39+23) (by show -126<=e'+39+23;omega);rw[show e'+39+23-(FloatFormat.binary32.fracWidth:ℤ)=e'+39 by show e'+39+23-23=_;omega,show e'+39+23+1-(FloatFormat.binary32.fracWidth:ℤ)=e'+40 by show e'+39+23+1-23=_;omega] at h;rw[show Model.finishRoundedMantissa Format.binary32 (Model.modelSign neg) (T_r N,e'+39) =.finite (Model.modelSign neg) (2^23) (e'+40) ב by rw[hc];exact h] exact K (2^23) (e'+40) ב le_rfl ב א א (by rw[hc,show (e'+40+150).toNat=(e'+39+149).toNat+2 by omega];omega) . rw[qD6 _ _ _ hr1 א א א] exact K _ _ _ hr1 א א א (by rw[show (e'+39+150).toNat=(e'+39+149).toNat+1 by omega];omega) @[inline] def T_rd (neg:Bool) (mag e0 L:UInt64):UInt32 := let n:=mag <<< (62-L) let q:=n >>> 39 let rm:=n &&& 0x7FFFFFFFFF let w:=((e0+L-24) <<< 23)+q + (if rm > 0x4000000000 || (rm == 0x4000000000 && q &&& 1 == 1) then 1 else 0) (if neg then w ||| ط else w).toUInt32 lemma qGg (neg:Bool) (mag e0:UInt64) (h0:mag.toNat≠0) (h1:mag.toNat<2^63) (h2:24< e0.toNat+mag.toNat.log2) (h3:e0.toNat+mag.toNat.log2<277) : T_rd neg mag e0 mag.log2=toWord (Ш Fmt ⟨neg,mag.toNat,(e0.toNat:Int)-150⟩):=by have l0:=Nat.log2_self_le h0;have l1:mag.toNat<2^(mag.toNat.log2+1):=Nat.lt_log2_self;have hL:mag.toNat.log2<=62:=Nat.lt_succ_iff.1 ((Nat.log2_lt h0).2 h1);obtain ⟨L,hLd⟩:∃ L,L=mag.toNat.log2:=⟨_,rfl⟩;rw[<-hLd] at l0 l1 hL h2 h3;have eL:mag.log2.toNat=L:=hLd.symm;obtain ⟨N,hN⟩:∃ N,N=mag.toNat*2^(62-L):=⟨_,rfl⟩;have p:2^L*2^(62-L)=2^62:=by rw[<-Nat.pow_add];congr 1;omega have hN1:2^62<=N:=by rw[hN,<-p];exact Ф _ l0 have hN2:N<2^63:=by have:=Nat.mul_lt_mul_of_pos_right l1 (Nat.two_pow_pos (62-L));rwa [<-hN,<-Nat.pow_add,show L+1+(62-L)=63 by omega] at this rw[qBc,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,qGr neg N _ hN1 hN2 א א];have eT:((e0.toNat:Int)-150-((62-L:Nat):Int)+39+149).toNat=e0.toNat+L-24:=by omega rw[eT];unfold T_rd;dsimp only;have e1:(62-mag.log2).toNat=62-L:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[eL];exact hL)),eL];rfl have a1:(mag <<< (62-mag.log2)).toNat=N:=by rw[UInt64.toNat_shiftLeft,e1,К (by omega:62-L<64),ش,<-hN,К א] generalize mag <<< (62-mag.log2)=n at a1;have a2:(n >>> 39).toNat=N/2^39:=by rw[й,a1];simp[Nat.shiftRight_eq_div_pow] have a3:(n &&& 0x7FFFFFFFFF).toNat=N%2^39:=by rw[ص,a1];exact Л N 39 generalize n >>> 39=q at a2;generalize n &&& 0x7FFFFFFFFF=rm at a3;have a4:(q &&& 1).toNat=q.toNat%2:=by rw[ص];exact Л _ 1 have a5:(e0+mag.log2-24).toNat=e0.toNat+L-24:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[а,eL,К א];show 24<=_;omega)), а,eL,К א] rfl have a6:((e0+mag.log2-24) <<< 23).toNat=(e0.toNat+L-24)*2^23:=by rw[UInt64.toNat_shiftLeft,a5,ش];show _*2^23%_=_;rw[К א] generalize (e0+mag.log2-24) <<< 23=hi at a6;have hup:(decide (rm > 0x4000000000) || (rm == 0x4000000000 && q &&& 1 == 1)) =decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=by apply Bool.eq_iff_iff.2;simp only[Bool.or_eq_true,Bool.and_eq_true,decide_eq_true_eq,beq_iff_eq,gt_iff_lt,UInt64.lt_iff_toNat_lt,<-UInt64.toNat_inj,a3,a4,a2];rfl rw[hup];obtain ⟨c,hc⟩:∃ c,c=decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=⟨_,rfl⟩;rw[<-hc];have hr:T_r N=N/2^39+if c=true then 1 else 0:=by rw[hc,T_r];simp only[decide_eq_true_eq] have hq:N/2^39<2^24 :=א;have hw:(hi+q+(if c=true then (1:UInt64) else 0)).toNat=(e0.toNat+L-24)*2^23+T_r N:=by rw[а,а,a6,a2,hr];cases c<;>simp only[Bool.false_eq_true,ite_false,ite_true] <;>(try rw[show (1:UInt64).toNat=1 from rfl])<;>(try rw[show (0:UInt64).toNat=0 from rfl])<;>omega generalize hi+q+(if c=true then (1:UInt64) else 0)=w at hw;have hw2:w.toNat<2^31:=by rw[hw,hr];split<;>omega apply Я.mp;rw[ه];cases neg;. simp only[Bool.false_eq_true,ite_false,UInt64.toNat_toUInt32,Nat.zero_add,hw] . simp only[ite_true,UInt64.toNat_toUInt32,UInt64.toNat_or];have h5:w.toNat ||| (2147483648:UInt64).toNat=2^31+w.toNat:=by have:=Nat.two_pow_add_eq_or_of_lt hw2 1;simp only[Nat.mul_one] at this;rw[this,Nat.or_comm];rfl rw[h5];congr 1;omega lemma qIt (w:UInt32) (h:(w &&& В)=0):Д w=0:=by have h1:=qAy w;rw[h] at h1;have h2:=w.toNat_lt have h3:w=0∨w=ط:=by have:w.toNat=0∨w.toNat=2147483648:=by simp at h1;omega rcases this with e | e;. exact Or.inl (Я.mp (by simpa using e)) . exact Or.inr (Я.mp (by simpa using e)) have h4:expOf w=0∧fracOf w=0:=by rcases h3 with rfl | rfl<;>decide simp[rationalWord,decodeD,h4,FloatLib.Numerics.Dyadic.toRat_eq_zero_iff] @[inline] def T_e (w:UInt32):UInt64 := if w &&& В == 0 then 255 else (expOf w).toUInt64 @[inline] def T_m (w:UInt32):UInt64 := if w &&& В == 0 then 0 else (fracOf w+0x800000).toUInt64 @[inline] def T_s (e0:UInt64) (w:UInt32):UInt64:=T_m w <<< (T_e w-e0) @[inline] def T_ok (e0:UInt64) (w:UInt32):Bool := good w && (w &&& В == 0 || (e0<=T_e w && T_e w-e0<=37)) @[inline] def T_p (e0:UInt64) (w:UInt32) (p:UInt64):UInt64 := if signOf w then p else p+T_s e0 w @[inline] def T_n (e0:UInt64) (w:UInt32) (n:UInt64):UInt64 := if signOf w then n+T_s e0 w else n lemma qIk (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true) : (T_s e0 w).toNat<2^61∧Д w = (if signOf w then -((T_s e0 w).toNat:ℚ) else ((T_s e0 w).toNat:ℚ)) * 2^((e0.toNat:Int)-150):=by unfold T_ok at h simp only[Bool.and_eq_true,Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h obtain ⟨hg,hz⟩:=h by_cases h0:w &&& В=0 . have hs:T_s e0 w=0:=by unfold T_s T_m;simp[h0] rw[hs,qIt w h0];simp . obtain ⟨h1,h2⟩:=hz.resolve_left h0;have g:=qA1 w hg;have a0:(w &&& В).toNat≠0:=λe=>h0 (Я.mp (by simpa using e));rw[qAy] at a0;have hx:=qB9 w;have hf:(fracOf w).toNat=w.toNat%2^23:=qCp w;have hl:=w.toNat_lt;have x1:1<=(expOf w).toNat :=א;have x2:(expOf w).toNat<=254 :=א;have f1:(fracOf w).toNat<2^23 :=א;have ee:(T_e w).toNat=(expOf w).toNat:=by unfold T_e;simp[h0] have em:(T_m w).toNat=2^23+(fracOf w).toNat:=by unfold T_m;simp only[h0,beq_iff_eq,if_false,UInt32.toNat_toUInt64];rw[UInt32.toNat_add,К (by show _+8388608< _;omega)];show _+8388608=_;omega have l1:=UInt64.le_iff_toNat_le.1 h1;have l2:=UInt64.le_iff_toNat_le.1 h2;rw[UInt64.toNat_sub_of_le _ _ h1,ee] at l2;rw[ee] at l1;have l3:(expOf w).toNat-e0.toNat<=37:=l2 have es:(T_s e0 w).toNat=(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat):=by unfold T_s;have sh:(T_e w-e0).toNat%64=(expOf w).toNat-e0.toNat:=by rw[UInt64.toNat_sub_of_le _ _ h1,ee];exact К א rw[UInt64.toNat_shiftLeft,em,sh,ش];apply К;have:2^((expOf w).toNat-e0.toNat)<=2^37:=Nat.pow_le_pow_right ב l3;have:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat)<=2^24*2^37 :=Nat.mul_le_mul א this;omega refine ⟨?_,?_⟩;. have a:2^((expOf w).toNat-e0.toNat)<=2^37:=Nat.pow_le_pow_right ב l3;have b:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat)<=(2^24-1)*2^37 :=Nat.mul_le_mul א a;rw[es];omega . have nz:expOf w≠0:=λe=>by rw[e] at x1;simp at x1 have ew:(2:ℚ)^(((expOf w).toNat:Int)-150) =2^((expOf w).toNat-e0.toNat)*2^((e0.toNat:Int)-150):=by rw[<-п,<-Щ Т];congr 1;omega rw[es];simp only[rationalWord,decodeD,nz,ite_false,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand,ew];cases signOf w<;>simp<;>ring lemma qGl (e0:UInt64) (w:UInt32) (p n:UInt64) (V:ℚ) (B C:Nat) (hC:C=2^61-1) (hB:B+C<2^63) (hw:T_ok e0 w=true) (hp:p.toNat<=B) (hn:n.toNat<=B) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) : (T_p e0 w p).toNat<=B+C∧(T_n e0 w n).toNat<=B+C ∧ V+Д w=(((T_p e0 w p).toNat:ℚ)-(T_n e0 w n).toNat) * 2^((e0.toNat:Int)-150):=by obtain ⟨h1,h2⟩:=qIk e0 w hw have h3:(T_s e0 w).toNat<=C :=א unfold T_p T_n rw[h2,hV] cases signOf w . simp only[Bool.false_eq_true,ite_false];have e:(p+T_s e0 w).toNat=p.toNat+(T_s e0 w).toNat:=by rw[а];exact К א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring . simp only[ite_true];have e:(n+T_s e0 w).toNat=n.toNat+(T_s e0 w).toNat:=by rw[а];exact К א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring @[inline] def T_fin (p n e0:UInt64) (f:Unit->UInt32):UInt32 := if p == n then f () else let mag:=if p< n then n-p else p-n let L:=mag.log2 if e0+L<=24 || e0+L ≥ 277 then f () else T_rd (decide (p< n)) mag e0 L lemma qE5 (p n e0:UInt64) (f:Unit->UInt32) (hp:p.toNat<2^63) (hn:n.toNat<2^63) (he:e0.toNat<=255) (R:UInt32) (hf:f ()=R) (hR:p≠n->∃ d:FloatLib.Numerics.Dyadic, d.toRat=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150) ∧ R=toWord (Ш Fmt d)):T_fin p n e0 f=R:=by unfold T_fin split . exact hf . rename_i hne;have hne':p≠n:=λe=>hne (by simp[e]);have hnn:p.toNat≠n.toNat:=λe=>hne' (UInt64.toNat_inj.mp e);obtain ⟨d,hd,hR⟩:=hR hne';dsimp only;obtain ⟨mag,hm⟩:∃ m,m=if p< n then n-p else p-n:=⟨_,rfl⟩;rw[<-hm] have hm1:mag.toNat≠0∧mag.toNat<2^63 ∧ (((if decide (p< n)=true then -(mag.toNat:ℚ) else (mag.toNat:ℚ))) =(p.toNat:ℚ)-n.toNat):=by by_cases c:p< n . have c':=л.1 c;have:mag.toNat=n.toNat-p.toNat:=by rw[hm,if_pos c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c'.le)] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'.le];simp[c] . have c':n.toNat<=p.toNat:=by have:=л.not.1 c;omega have:mag.toNat=p.toNat-n.toNat:=by rw[hm,if_neg c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c')] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'];simp[c] obtain ⟨m0,m1,m2⟩:=hm1;split;. exact hf . rename_i hb;simp only[Bool.or_eq_true,decide_eq_true_eq,not_or,ge_iff_le] at hb;obtain ⟨b1,b2⟩:=hb;have eL:mag.log2.toNat=mag.toNat.log2:=rfl;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt m0).2 m1;omega have eA:(e0+mag.log2).toNat=e0.toNat+mag.toNat.log2:=by rw[а,eL];exact К א have b3:¬ (e0+mag.log2).toNat<=(24:UInt64).toNat:=λh=>b1 (UInt64.le_iff_toNat_le.2 h);have b4:¬ (277:UInt64).toNat<=(e0+mag.log2).toNat:=λh=>b2 (UInt64.le_iff_toNat_le.2 h);rw[eA] at b3 b4;have t1:(24:UInt64).toNat=24:=rfl;have t2:(277:UInt64).toNat=277:=rfl;rw[qGg _ _ _ m0 m1 א א,hR];congr 1 have s0:d.significand≠0:=by intro h0;have:d.toRat=0:=(ق d).2 h0;rw[hd] at this;have q:(p.toNat:ℚ)-n.toNat≠0:=by rw[sub_ne_zero];exact_mod_cast hnn exact (mul_ne_zero q (zpow_ne_zero _ Т)) this refine roundDyadic_congr Fmt (by decide) _ _ m0 s0 ?_;rw[hd,<-m2];simp only[FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand];cases decide (p< n)<;>simp lemma qG6 (w:UInt32):(T_e w).toNat<=255:=by unfold T_e;split;. decide . have:=qB9 w;simp only[UInt32.toNat_toUInt64];omega @[inline] def T_min (x y:UInt64):UInt64:=if x< y then x else y lemma qFf (x y:UInt64):(T_min x y).toNat<=x.toNat:=by unfold T_min;split;. exact le_rfl . have:=л.not.1 ‹_›;omega lemma qGb (e0:UInt64) (w0 w1:UInt32) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 0)).toNat<=2*(2^61-1)∧(T_n e0 w1 (T_n e0 w0 0)).toNat<=2*(2^61-1) ∧ Д w0+Д w1=(((T_p e0 w1 (T_p e0 w0 0)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 0)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qGl e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=qGl e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma qEl (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true):expOf w≠255:=by unfold T_ok at h;simp only[Bool.and_eq_true] at h;exact qAe w h.1 end @[noinline] def addSlow (relu:Bool) (x y:Word):Word := act relu (toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) section open Float.Model Float.Model.UnpackedFloat @[noinline] def addT (relu:Bool) (x y:Word):Word := let e0:=T_min (T_e x) (T_e y) if T_ok e0 x && T_ok e0 y then act relu (T_fin (T_p e0 y (T_p e0 x 0)) (T_n e0 y (T_n e0 x 0)) e0 λ_=>toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) else addSlow relu x y lemma qF8 (relu:Bool) (x y:Word):addT relu x y=addSlow relu x y:=by unfold addT;dsimp only;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨hx,hy⟩:=hc;obtain ⟨p1,n1,v⟩:=qGb _ x y hx hy;have ex:=qEl _ x hx;have ey:=qEl _ y hy;unfold addSlow;congr 1 refine qE5 _ _ _ _ א א ((qFf _ _).trans (qG6 x)) _ rfl λ_ =>⟨Model.addDyadic (decodeD x) (decodeD y),?_,?_⟩ . rw[Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat];exact v . rw[Model.Proof.add_eq_spec];have e:=ч x ex;have f:=ч y ey;simp only[Model.Spec.add,ofW,e,f] . rfl end @[inline] def addOne (relu:Bool) (x y:Word):Word := if sm x && sm y then let r:=zivS (dv x+dv y) if r == Е then addT relu x y else act relu r else addSlow relu x y section lemma qGn (S:Bool) (ex fr m:Nat) (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[qEk 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 qFo (a b:ℝ) (ha:0<=a) (h:a< b) : ∃ (m:Nat) (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 qG4 (z:Model Fmt):ofW (toWord z)=z:=qEc z lemma qIr (w:UInt32) (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[qAz w h1,rationalWord,decodeD_toRat,this,zpow_ne_zero] lemma qDv (s e:Float) (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⟩:=qB1 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'⟩:=qFo 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'⟩:=qFo W ((W+B)/2) hW0.le ד;have w1:=qGn S ex fr m1 q1 h1 h2 h3 hm1 ד (λh=>ш (hLo1 h) hd1) (ш hLo2 hd1) have w2:=qGn 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.toNat=(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[qB9,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,qG4] have r2:Ш Fmt ⟨S,m2,q2⟩=ofW z:=by rw[<-w2,qG4] 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 qIr 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 qIv:(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,qA2];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 qIg (s e:Float) (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 qIv);have hM:=Model.toReal_roundRatScaled_eq_roundAt Fmt _ _ _ 0 (by decide) (by simpa using λh0=>qGy s e q hlo hhi h (by simp[h0])) q.den_nz hf;exact qDv s e q hlo hhi h _ hf (qFn q ▸ hM) lemma qHx (N d:Nat) (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 (qA2 127).symm) lemma qIu (q:ℚ) (hq:0< q) (hb:к (q:ℝ)<2^(127:ℤ)) : Ь (sqrtModel q)=true := qHx _ _ _ (ne_of_gt (sqrtRoot_pos q hq)) q.pos (sqrtStep_le_target q) (by rwa [sqrtScale_correct q hq]) lemma qHi (s e:Float) (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:=qIu q hq hb;exact (if_neg (not_le.2 hq)).trans (qDv s e _ hlo hhi h _ hf (sqrtModel_correct q hq hf)) lemma qA8 (w:UInt32) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by obtain ⟨hg,hl⟩:=Bool.and_eq_true_iff.1 h;have h1:=З.1 (of_decide_eq_true hl);rw[qAy] at h1;have h2:=qA1 w hg;rw[qB0 w hg];refine ⟨hg,(qBa _ _ h1 א).trans ?_⟩;unfold A_mag;norm_num lemma qCt (relu:Bool) (x y:Word) : addOne relu x y=act relu (toWord (Model.add (ofW x) (ofW y))):=by unfold addOne;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨gx,-⟩:=qA8 x hc.1;obtain ⟨gy,-⟩:=qA8 y hc.2;dsimp only;split;. exact qF8 relu x y . rename_i hr;have hn:zivS (dv x+dv y)≠Е:=λh=>hr (by simp[h]);have ex:=qAe _ gx;have ey:=qAe _ gy;have hd:((Model.addDyadic (decodeD x) (decodeD y)).toRat:ℝ)=wv x+wv y:=by rw[Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,qAz x ex,qAz y ey];simp only[rationalWord,Rat.cast_add] obtain ⟨fx,rx⟩:=qAk x gx;obtain ⟨fy,ry⟩:=qAk y gy;obtain ⟨-,he⟩:=qA3 _ _ fx fy (by rw[rx];exact (qAj x gx).trans qAn) (by rw[ry];exact (qAj y gy).trans qAn);rw[rx,ry,<-hd] at he;rw[qGz _ _ he hn];congr 2;rw[Model.Proof.add_eq_spec];have e:=ч x ex;have f:=ч y ey;simp only[Model.Spec.add,ofW,e,f] . rfl lemma qHq {L:USize->Array UInt32->Array UInt32} (f:USize->UInt32->UInt32) {e:USize} (hL:∀ i x,L i x=if i< e then L (i+1) (wrW x i (f i (rdW x i))) else x) (d:Nat) : ∀ (i:USize) (x:Array UInt32),i.toNat+d=e.toNat->e.toNat<=x.size->x.size<2^32 → (L i x).size=x.size∧∀ q,(L i x)[q]! =if i.toNat<=q∧q< e.toNat then f (USize.ofNat q) x[q]! else x[q]!:=by induction d with | zero => intro i x h1 _ _ rw[hL,if_neg (by rw[А];omega)] exact ⟨rfl,λq=>(if_neg א).symm⟩ | succ d ih => intro i x h1 h2 h3 have hi:i< e:=А.2 א have hs:=qBj x i (f i (rdW x i)) rw[hL,if_pos hi] obtain ⟨h4,h5⟩:=ih (i+1) (wrW x i (f i (rdW x i))) (by rw[usz i e hi];omega) א א refine ⟨h4.trans hs,λq=>?_⟩ rw[h5,qBp _ _ _ _ h3 א,usz i e hi,qAh _ _ h3] by_cases h6:q=i.toNat . subst h6;rw[if_neg א,if_pos rfl,if_pos א,USize.ofNat_toNat] . rw[if_neg h6];exact if_congr א rfl rfl def addGo (relu:Bool) (y:Array Word) (e i:USize) (x:Array Word) : Array Word := if h:i< e then addGo relu y e (i+1) (wrW x i (addOne relu (rdW x i) (rdW y i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def addF (relu:Bool) (a b:Array Word):Array Word := if a.size=b.size∧a.size<2^32 then addGo relu b a.usize 0 a else Array.ofFn (n:=a.size) λi=>addOne relu a[i] b[i.val]! lemma qFd:addF=addReluSpec:=by funext relu a b;unfold addF;split . rename_i h;obtain ⟨h1,h2⟩:=h;have hu:a.usize.toNat=a.size:=qAd _ h2;have hg:=qHq (L:=addGo relu b a.usize) (λi w=>addOne relu w (rdW b i)) (λ_ _=>by rw[addGo];rfl) a.size 0 a (by rw[hu,show (0:USize).toNat=0 from rfl,Х]) א h2;apply Array.ext (by rw[hg.1];simp[addReluSpec]);intro i h3 h4;have hi:i< a.size:=by rwa [hg.1] at h3 rw[<-Ю _ i h3,hg.2 i,if_pos ⟨Nat.zero_le _,by omega⟩,qCt,Ю a i hi,qAh _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,qCt] end @[specialize] def fC {σ:Type} (f:σ->USize->USize->Word) (s:σ) (b j:USize) (o:Array Word):Nat->Array Word | 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:Nat->σ) (f:σ->USize->USize->Word) (n r:Nat) (o:Array Word):Nat->Array Word | 0=>o | k+1=>fR h f n (r+1) (fC f (h r) (r*n).toUSize 0 o n) k def gemmF (batch m k n:Nat) (A B:Array Word):Array Word := if batch == 1 && A.size == m*k && B.size == k*n && decide (0< n) && decide (0< k) && A.all finiteWord && B.all finiteWord && decide (k*n<2^32) then let nu:=n.toUSize linF m k n false A (fR (λr=>r.toUSize) (λu _ t=>rdW B (t*nu+u)) k 0 (Н (n*k) 0) n) (Н n ط) else gemmSpec batch m k n A B structure LnS where fast:Bool m:Float ir:Float em:Float den:UInt32 deriving Inhabited def lnSum (x:Array Word) (base n i:USize) (s t:Float):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.toNat-i.toNat decreasing_by rw[usz i n h];have:=А.mp h;omega def lnVar (x:Array Word) (base n i:USize) (m v:Float):Float := 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.toNat-i.toNat decreasing_by rw[usz i n h];have:=А.mp h;omega @[inline] def invOK (a b:Float):Bool := decide ((a.toBits >>> 52)-1<1222) && decide ((((a*b-С 0x3FF0000000000000)+С 0x3CD0000000000000).toBits >>> 52)-972<2) @[inline] def lnQ (b:Bool) (n:Nat) (x:Array Word) (r:Nat):ℚ:=if b then 0 else rowMean n x r def lnStat (b:Bool) (n:Nat) (eps:Word) (x:Array Word) (r:Nat):LnS := let base:=(r*n).toUSize let nu:=n.toUSize 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:UInt32)<=z) && decide (z<=(0x70FFFFFF:UInt32)) && 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)⟩ @[noinline] def lnSlow (x g b den:Word) (mean:Unit->ℚ):Word := roundRational ((Д x-mean ())*Д g/Д den+Д b) @[inline] def lnOut (s:LnS) (w gw bw:Word) (mean:Unit->ℚ):Word := if s.fast then let bv:=dv bw let gd:=dv gw*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 def lnF (rows n:Nat) (eps:Word) (x g b:Array Word):Array Word := if stableNormDomain rows n eps x g && b.size == n && b.all finiteWord && x.all sm && g.all sm && b.all sm && sm eps && decide (n<2^20) && decide (rows*n<2^32) then let st:=Array.ofFn (n:=rows) λr=>lnStat false n eps x r.val fR (λr=>(st[r]!,λ_:Unit=>rowMean n x r)) (λs p j=>lnOut s.1 (rdW x p) (rdW g j) (rdW b j) s.2) n 0 (Н (rows*n) 0) rows else layernormSpec rows n eps x g b section lemma qFt (x:UInt64):(x >>> 52).toNat=x.toNat/2^52:=by rw[й,ы];rfl lemma qD4 (x k N:UInt64) (h:x-k< N) (hN:k.toNat+N.toNat<2^64) : k.toNat<=x.toNat∧x.toNat< k.toNat+N.toNat:=by have h1:=л.mp h;rw[ض] at h1;have h2:=x.toNat_lt;have h3:=k.toNat_lt;omega lemma qEf (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,Щ Т,п] lemma qCz {f:Float} {E:Nat} (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[qFt] 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,qEf ((E:ℤ)-1023) (E-1075) 52 א,qEf ((E:ℤ)-1022) (E-1075) 53 א];exact ⟨h1,Б ד hb.le,д ד hb⟩ lemma L_k (c:UInt64) (E n:Nat) (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 qFy {f:Float} {E:Nat} (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[qFt] at h have hs:Model.signBit (toModel (ofFloat f))=false:=by rw[qFj];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[qAg,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,и qAi,ht,<-и qAi] exact hr refine ⟨?_,hp,?_⟩ . rw[<-qAg,qA0,ht,<-qA0];exact hq . rw[hR];have:=qFl (к (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 ((qEf 300 44 256 rfl).trans ב).symm lemma qBn (a:Array UInt32) (p:UInt32->Bool) (h:a.all p=true) (i:ℕ) (hi:i< a.size) : p a[i]! = true:=by rw[Ю a i hi];simpa using Array.all_eq_true.1 h i hi lemma qFa (w:UInt32) (g:good w=true) (h:w &&& В=0):Д w=0:=by exact_mod_cast (qAz w (qAe _ g)).symm.trans (qBs w g h) lemma qIw (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h0:ziv s e≠Е) (h:¬ (0x7EFFFFFF:UInt32)<(ziv s e &&& В)) : |V| ≤ 2^(127:ℤ):=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,-,hW3⟩:=qB1 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[qAy,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[<-qEf 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 qFs (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 qEe {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 qGm {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 qHe {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:=qFs 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 qHk {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 qI6 {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 qH1 {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:=qFs 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 qGp {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 qG2 {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 qG5 (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.length*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 qF9 (l:List ℕ) (a:ℕ->ℝ) (m:ℝ) (hl:(l.length:ℝ)≠0) : (l.map λj=>(a j-m)^2).sum = (l.map λj=>(a j-(l.map a).sum/l.length)^2).sum + l.length*((l.map a).sum/l.length-m)^2:=by rw[qG5,qG5] field_simp ring lemma qHm (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.length*t ∧ (l.map λj=>|f j|).sum<=(1+u)*(l.map w).sum+l.length*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 qGi (a b:Array UInt32) (hs:a.size=b.size) (hf:∀ j,j< a.size->expOf a[j]!≠255∧expOf b[j]!≠255) (hz:(dsum a b 0 a.size:ℚ)*2^(-298:Int)=0) : dotSpec a b=toWord (Model.zero Fmt (decide (a.size≠0) && dallNZ a b 0 a.size)):=by rw[<-qD2 a b hs.symm hf,show dsum a b 0 a.size=0 by simpa using hz,finishWord_zero] lemma qG9 (n:Nat) (a b:Nat->UInt32) (hf:∀ t,t< n->expOf (a t)≠255∧expOf (b t)≠255) (hS:∑ j∈м n,Д (a j)*Д (b j)=0) : dotOf n a b=toWord (Model.zero Fmt (decide (n≠0) && dallNZ (Array.ofFn (n:=n) λi=>a i.val) (Array.ofFn (n:=n) λi=>b i.val) 0 n)):=by have gA:=qAb n a have gB:=qAb n b have s:(Array.ofFn (n:=n) λi=>a i.val).size=n:=by simp have e:=qGi (Array.ofFn (n:=n) λi=>a i.val) (Array.ofFn (n:=n) λi=>b i.val) ג (λj hj=>by rw[s] at hj;rw[gA j hj,gB j hj];exact hf j hj) (by rw[s,qEo,<-hS] refine س rfl λj hj=>?_ have h:=ؿ.mp hj rw[Х,gA j h,gB j h]) rw[s] at e exact e lemma qIs (a b:Array UInt32) (r i:Nat) : dallNZ a b i (r+1)=(dallNZ a b i r && (decide (mantOf a[i+r]! * mantOf b[i+r]! = 0) && Bool.xor (signOf a[i+r]!) (signOf b[i+r]!))):=by induction r generalizing i with | zero=>simp[dallNZ] | succ r ih => rw[dallNZ,ih (i+1),dallNZ] simp only[Bool.and_assoc,show i+1+r=i+(r+1) by omega] lemma qH5 (a b u v:Array UInt32) (r i:Nat) (h:∀ j,i<=j->j< i+r->a[j]! = u[j]!∧b[j]! = v[j]!) : dallNZ a b i r=dallNZ u v i r:=by induction r generalizing i with | zero=>rfl | succ r ih => rw[dallNZ,dallNZ,ih (i+1) (λj h1 h2=>h j א א),(h i א א).1,(h i א א).2] lemma qHu (k:Nat) (a b:Nat->UInt32) (hk:0< k) (hf:∀ t,t< k->expOf (a t)≠255∧expOf (b t)≠255) : dotOf (k+1) (λt=>if t< k then a t else oneW) (λt=>if t< k then b t else ط)=dotOf k a b:=by have hf':∀ j,j< k+1->expOf (if j< k then a j else oneW)≠255 ∧ expOf (if j< k then b j else ط)≠255:=λj _=>by by_cases h:j< k . simpa [h] using hf j h . simp only[h,ite_false];exact ⟨by decide,by decide⟩ have hsum:(∑ j∈м (k+1),Д (if j< k then a j else oneW) * Д (if j< k then b j else ط)) =∑ j∈м k,Д (a j)*Д (b j):=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qFa 0x80000000 (by decide) (by decide),mul_zero,add_zero] exact س rfl λj hj=>by simp[Finset.mem_range.mp hj] by_cases hS:(∑ j∈м k,Д (a j)*Д (b j))=0 . have g1:=qAb (k+1) λt=>if t< k then a t else oneW;have g2:=qAb (k+1) λt=>if t< k then b t else ط rw[qG9 _ _ _ hf' (hsum.trans hS),qG9 k a b hf hS,qIs,Х,g1 k א,g2 k א, qH5 _ _ (Array.ofFn (n:=k) λi=>a i.val) (Array.ofFn (n:=k) λi=>b i.val) k 0 (λj _ h2=>by rw[g1 j א,g2 j א,qAb k a j א,qAb k b j א] simp[show j< k by omega])] simp[show mantOf (0x80000000:UInt32)=0 by decide,show signOf oneW=false by decide,show signOf (0x80000000:UInt32)=true by decide,hk.ne'] . obtain ⟨d,hd,hw⟩:=qCy k a b hf hS;obtain ⟨e,he,hw'⟩:=qCy (k+1) _ _ hf' (by rw[hsum];exact hS);have t:=@FloatLib.Numerics.Dyadic.toRat_eq_zero_iff;have h3:e.toRat=d.toRat:=by rw[he,hd,hsum] rw[hw,hw',roundDyadic_congr Fmt (by decide) e d (λh=>hS (hd.symm.trans (h3.symm.trans ((t _).2 h)))) (λh=>hS (hd.symm.trans ((t _).2 h))) h3] lemma qHa (A:Array UInt32) (h:A.all finiteWord=true) (t:Nat):expOf A[t]!≠255:=by by_cases ht:t< A.size;. simpa [finiteWord] using qBn A _ h t ht . rw[ك A t ht];decide lemma qI9 {σ:Type} {N n c:Nat} {sp:Nat->Word} {f:σ->USize->USize->Word} {s:σ} {b:USize} (hN:N<2^32) (hb:b.toNat=c) (hn:c+n<=N) (hf:∀ p u:USize,p.toNat=c+u.toNat->u.toNat< n->f s p u=sp p.toNat) (k:Nat) : ∀ (j:USize) (o:Array Word),j.toNat+k=n->C_Inv N sp (λq=>q< c+j.toNat) 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.toNat:=by rw[<-hb];exact qAf _ _ א have a1:(j+1).toNat=j.toNat+1:=by rw[qAf _ _ (by rw[غ];omega),غ] rw[fC] refine ih _ _ א ?_ rw[a1] exact qAq hN h a0 א (by rw[hf _ _ a0 א,a0]) lemma qDe {σ:Type} {R n:Nat} (F:Nat->Nat->Nat->Word) {h:Nat->σ} {f:σ->USize->USize->Word} (hN:R*n<2^32) (hn:0< n) (hf:∀ (r:Nat) (p u:USize),r< R->p.toNat=r*n+u.toNat->u.toNat< n → f (h r) p u=F r u.toNat p.toNat) : 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:Nat) (o:Array Word),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:=qBl (א:r< R) (le_refl _) rw[fR] refine ih _ _ א ?_ rw[Nat.succ_mul] exact qI9 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 qC9 (K R 0 _ א ⟨by simp,λq _ (h:q<0*n)=>by omega⟩) λq hq=>hq lemma qEw:gemmF=gemmSpec:=by funext batch m k n A B;unfold gemmF;split . rename_i hc;simp only[Bool.and_eq_true,beq_iff_eq,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨⟨⟨h1,h2⟩,h3⟩,h4⟩,h5⟩,h6⟩,h7⟩,h8⟩:=hc;subst h1;have hn:n<2^32:=ш (Nat.le_mul_of_pos_left n h5) h8;dsimp only rw[qDs,qDe (λr c _=>B[c*n+r]!) (by rwa [ж]) h5 λr p u hr hp hu=>by have a1:=qCm hu hr;have e1:=qAd n hn;have e2:=qAd r א;have e3:(u*n.toUSize).toNat=u.toNat*n:=by rw[qBg _ _ (by rw[e1];omega),e1] rw[qAh _ _ א,qAf _ _ (by rw[e3,e2];omega),e3,e2]] unfold linearSpec linearWith gemmSpec gemmWith;apply Array.ext ג;intro q h8 h9;simp only[Array.getElem_ofFn,Array.size_ofFn] at h8 h9 ⊢;have hq:q< m*n:=by simpa using h9 have e1:q/(m*n)=0:=ـ hq;have e2:q/n%m=q/n:=К (ظ (by rwa [ж]));have hj:q%n< n:=ъ _ h4;simp only[act,Bool.false_eq_true,ite_false];rw[<-qHu k (λt=>A[(q/(m*n)*m+q/n%m)*k+t]!) (λt=>B[(q/(m*n)*k+t)*n+q%n]!) h5 (λt _=>⟨qHa A h6 _,qHa B h7 _⟩)];congr 1 . funext t;by_cases ht:t< k;. simp[ht,e1,e2] . simp[ht] . funext t;by_cases ht:t< k . simp only[ht,if_true];have hi:q%n*k+t< n*k:=by have:=Ф k (א:q%n+1<=n);rw[щ,о] at this;omega rw[qAb (n*k) (λu=>B[u%k*n+u/k]!) _ hi];have i1:(q%n*k+t)%k=t:=by rw[ж,Nat.mul_add_mod,К ht] have i2:(q%n*k+t)/k=q%n:=by rw[ж,Nat.mul_add_div h5,ـ ht,ػ] simp[i1,i2,e1] . simp only[ht,if_false];simp[hj] . rfl section variable (x:Array UInt32) (hx:x.size<2^32) (base nu:USize) include x hx base nu lemma qHc (hb:base.toNat+nu.toNat<2^32) (d:ℕ) (i:USize) (s t:Float) (h:i.toNat+d=nu.toNat) : lnSum x base nu i s t = ⟨((List.range' i.toNat d).map λj=>dv x[base.toNat+j]!).foldl (.+·) s,((List.range' i.toNat d).map λj=>(dv x[base.toNat+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,qAh x _ hx,qAf base i א] simp[List.range'_succ] lemma qHl (m:Float) (hb:base.toNat+nu.toNat<2^32) (d:ℕ) (i:USize) (v:Float) (h:i.toNat+d=nu.toNat) : lnVar x base nu i m v = ((List.range' i.toNat d).map λj =>(dv x[base.toNat+j]! - m)*(dv x[base.toNat+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,qAh x _ hx,qAf base i א] simp[List.range'_succ] end lemma qEq (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 qH4 (w:UInt32) (hg:good w=true) (h1:(0x1F800000:UInt32)<=w) (h2:w<=(0x70FFFFFF:UInt32)):1/2^64<=wv w:=by have a1:528482304<=w.toNat:=З.mp h1;have a2:w.toNat<=1895825407:=З.mp h2;rw[qBb w hg,if_neg א,one_mul];have h3:=qBa 528482304 (w.toNat%2^31) א א;have:A_mag 528482304=1/2^64:=by unfold A_mag;norm_num rwa [this] at h3 lemma qH9 (w:UInt32) (hg:good w=true) (h0:0< wv w):1/2^126<=wv w:=by have hn:=qA1 w hg;rw[qBb w hg] at h0 ⊢;rcases hn with hn | ⟨hn,-⟩;. rw[hn] at h0;simp[A_mag] at h0 . have hs:¬ 2^31<=w.toNat:=λhc=>by rw[if_pos hc] at h0;linarith [qAw (w.toNat%2^31)] rw[if_neg hs,one_mul];have h3:=qBa (2^23) (w.toNat%2^31) hn א;have:A_mag (2^23)=1/2^126:=by unfold A_mag;norm_num rwa [this] at h3 lemma qI4 (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:UInt64).toNat=8:=rfl simp[UInt64.toNat_add,h8] omega have h1:=qCg (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⟩:=qD3 _ _ h1.1 h2 (by rw[h1.2,h3,ht] refine ь (Ч (qAt ?_) (qAt (by rw[Ж (by positivity)];norm_num)) (Й _) qJb.le) qC2 rw[Nat.cast_add,Ж (by positivity)] push_cast linarith) refine ⟨hf,?_⟩ rw[he,h1.2,h3,ht] have:=qA5 ((n:ℤ)+8) (-49) א ב push_cast at this ⊢ rw[show (2:ℝ)^(-49:ℤ)=1/2^49 by norm_num] at this rw[this] ring lemma qEd {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,qAp] section variable (x y:Float) (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 qAu : (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⟩:=qA3 x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hB)) exact ⟨f,e,qEd e ((Ъ _ _).trans (add_le_add h1 h2))⟩ lemma qCb : (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⟩:=qB6 x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hB)) exact ⟨f,e,qEd e ((ف _ _).trans (add_le_add h1 h2))⟩ end lemma qAm (x y:Float) (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⟩:=qB8 x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hC)) exact ⟨f,e,qEd e (в (R x) (R y) ▸ Ч h1 h2 (Й _) ((Й _).trans h1))⟩ lemma qFm (a b:Float) (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:=qD4 _ 1 1222 h.1 (by decide);obtain ⟨r3,r4⟩:972<=_∧_<972+2:=qD4 _ 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:=qBr;obtain ⟨f2,g1,q1⟩:=qAm a b (2^200) (2^99) f1 hb (by rwa [б hp]) hb2;obtain ⟨f3,g2⟩:=qB6 _ _ 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:=qEd g2 q3;obtain ⟨f4,g3⟩:=qA3 _ _ f3 k3 (qAt ד) (qAt (by rw[k4];norm_num));rw[k4] at g3;obtain ⟨-,l2,u2⟩:=qCz rfl (ь ב r3) א;refine ⟨f1,hp,ha2,qEe qAp.le qAr 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 qDx (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 qE9 (n:ℕ) (hn:n<2^20) (f:ℕ->Float) (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:Float->ℝ) (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:=qBv ((О 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 qGo (n:ℕ) (hn:n<2^20) (hn0:0< n) (g:ℕ->Float) (m v:Float) (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:=qAp.le;have hzl:=qAr;have hzb:=qBr 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⟩:=qCb (g j) m (2^110) f hm B1 hm2 have b2 : |R (g j-m)| ≤ 2^112 :=ד obtain ⟨f2,e2,-⟩:=qAm _ _ (2^112) (2^112) f1 f1 b2 b2 have hx : |R (g j)-R m| ≤ 2^111:=(ف _ _).trans ד refine ⟨f2,qI6 hz0 hzl hx e1 e2,?_⟩ have:=э (Й _) hx 2 rw[sq_abs] at this exact this.trans ב obtain ⟨a1,a2,a3⟩:=qHm (О 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 ▸ qDx 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⟩:=qE9 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,qGp hz0 hzl hnR1 hnR a3 e2 a1 a2,hW',a3⟩ lemma qFu (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:Bool) (n:ℕ) (eps:Word) (x:Array Word) (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 qG7 (n r:ℕ) (x:Array Word) (g:ℕ->Float) (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,qEq _ _ (λj=>R (g j)) 0 λj hj=>(h j (е.1 hj)).symm];simp . rw[rowVariance,Rat.cast_div,qEq _ _ (λ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 qHg (Fs Ft ind nf m em:Float) (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:=qAp.le have hzb:=qBr 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,-⟩:=qAm Fs ind (2^120) 2 o1 fi bs bI rw[hm] at fm em1 obtain ⟨fN,emN,bN⟩:=qAm nf Ft 1 (2^120) fnf o2 bnf bt rw[Rnf] at emN obtain ⟨fY,emY,bY⟩:=qAm (nf*Ft) ind (2^122) 2 fN fi ד bI obtain ⟨k1,k2⟩:=L_k ؽ 823 200 ב rfl (by rfl) obtain ⟨fE,emE,-⟩:=qAu (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⟩:=qHe hz0 qAr (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 qIx (v ind tt:Float) (eps:Word) (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:=qBr;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⟩:=qAm v ind (2^250) 2 p1 fi bv bI;obtain ⟨de1,de2⟩:=qAk eps gE;obtain ⟨ft,et,-⟩:=qAu (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,qH1 qAp.le qAr (W/n) (1/2^229) hnR1 hnR2 hc (div_nonneg hW0 hnpos.le) (qH9 eps gE hep0) ב le_rfl (by rw[show (n:ℝ)*(W/n)=W by field_simp];exact p2) pi ei ep1 et⟩ end lemma qI3 (tt nf em:Float) (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:=qBr have hc0:0< c:=т ב hc2 obtain ⟨-,-,u1⟩:=qCz hEe hE1 א obtain ⟨fd,ptt,ed⟩:=qFy 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⟩:=qAu nf nf 1 fnf fnf bnf bnf rw[Rnf] at e2n obtain ⟨fmu,emu,bmu⟩:=qAm (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⟩:=qAu ((nf+nf)*d) em (2^120) fmu fE ד bEm obtain ⟨fh,eh,-⟩:=qAu d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨fl,el,-⟩:=qCb d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨hhi,hlo⟩:=qHk qAp.le qAr 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⟩:=qG2 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 qEn (b:Bool) (rows n:ℕ) (eps:Word) (x:Array Word) (r:ℕ) (hr:r< rows) (hn0:0< n) (hn:n<2^20) (hs:x.size=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⟩:=qA8 _ (qBn x sm hx (r*n+j) א);obtain ⟨a3,a4⟩:=qAk _ a1;rw[<-hgd];beta_reduce;rw[a4];exact ⟨a3,a2,qAz _ (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 :=qDx 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⟩:=qE9 n hn g (λj=>R (g j)) (λj=>|R (g j)|) (λj h=>⟨(hg0 j h).1,rfl,rfl⟩) hA2 obtain ⟨o2,o6⟩:=qE9 n hn (λj=>(g j).abs) (λj=>|R (g j)|) (λj=>|R (g j)|) (λj h=>by obtain ⟨p,q⟩:=qBm _ (hg0 j h).1 exact ⟨p,q,abs_abs _⟩) hA2 have o3:=qFu (О n) λj=>R (g j);obtain ⟨hMc,hVc⟩:=qG7 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.toUSize=nu;generalize hbase:(r*n).toUSize=base;generalize hnd:n.toUInt64.toFloat=nd;generalize hnf:(n.toUInt64+8).toFloat*С ю=nf;generalize hind:(1.0:Float)/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:Float) 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:Float)/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:=qBr;have hz0:=qAp.le;have hu:nu.toNat=n:=by rw[<-hnu];exact qAd n א have hb:base.toNat=r*n:=by rw[<-hbase];exact qAd _ א have hxs:x.size<2^32 :=א;obtain ⟨fnf,Rnf⟩:=hnf ▸ qI4 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:=qCg n.toUInt64 א;rwa [e] at this obtain ⟨fi,pi,-,ei⟩:=qFm ind nd fnd (by rw[Rnd,Ж ד];exact hnR2.trans ב) c5 rw[Rnd] at ei;obtain ⟨z1,z2⟩:=qGj 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,-,-⟩:=qHg 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,qHc 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 qHg 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⟩:=qGo n hn hn0 g m v _ (by rw[<-hv,qHl 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:=qF9 (О 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⟩:=qA8 eps he;have epsv:wv eps=(Д eps:ℝ):=qAz _ (qAe _ gE);have hep0:0< wv eps:=by rw[epsv];exact_mod_cast he0 obtain ⟨ft,hq1⟩:=qIx 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:=qD4 _ 1 1222 c4 (by decide);obtain ⟨l1,l2,hb127⟩:=qI3 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:=((qHi d _ q hq0 l1 l2 hzne hb127).trans hz).symm;rw[<-hq] at hden;obtain ⟨gZ,bZ⟩:=qA8 z c1;obtain ⟨dz1,dz2⟩:=qAk z gZ;obtain ⟨fi2,-,-,ei2⟩:=qFm ir (dv z) dz1 (by rw[dz2];exact bZ) c6;rw[dz2] at ei2;refine ⟨hden,λ_=>⟨fm,fi2,fE,m2,m3,?_,m4,gZ,qH4 z gZ c2 c3,ei2⟩⟩;dsimp only;rw[hQ,hδ];exact m1 . exact ⟨rfl,by simp[L_oF]⟩ lemma qIy (m ir em:Float) (w gw bw:Word) (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:Float) (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:=qBr obtain ⟨gw1,bw1⟩:=qA8 w hw obtain ⟨gg1,bg1⟩:=qA8 gw hg obtain ⟨gb1,bb1⟩:=qA8 bw hb obtain ⟨fw,Rw⟩:=qAk w gw1 obtain ⟨fg,Rg⟩:=qAk gw gg1 obtain ⟨fb,Rb⟩:=qAk 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⟩:=qCb (dv w) m (2^111) fw fm (by rw[Rw];exact bw1.trans ב) (e4.trans ב) rw[Rw] at h1 obtain ⟨f2,h3,b2⟩:=qAm (dv gw) ir (2^99) (2^65) fg fi (by rw[Rg];exact bg1) bI rw[Rg] at h3 obtain ⟨f3,h4,b3⟩:=qAm _ _ (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⟩:=qAu 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⟩:=qBm Q f3 obtain ⟨fa2,Ra2⟩:=qBm (dv bw) fb rw[Rb] at Ra2 obtain ⟨fa3,Ra3⟩:=qBm G f2 obtain ⟨f5,h6,b5⟩:=qAu 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⟩:=qAm _ _ 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⟩:=qAm em G.abs (2^120) (2^166) fe fa3 bE (by rw[Ra3,abs_abs];linarith) rw[Ra3] at h8 obtain ⟨f8,h9,b8⟩:=qAu _ _ (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,-⟩:=qAu 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,-⟩:=qCb s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨-,-,big⟩:=qBu s e hne rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at big exact qGm qAp.le qAr (ь ב e2) e1 e3 hD bw1 bg1 e4 h1 hI h3 h4 h5 h6 h7 h8 h9 h10 h11 big lemma qEx (s:LnS) (M:ℚ) (mean:Unit->ℚ) (hmean:mean ()=M) (w gw bw:Word) (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 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⟩:=qIy 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:=qAz w (qAe _ (qA8 w hw).1);have eg:=qAz gw (qAe _ (qA8 gw hg).1);have eb:=qAz bw (qAe _ (qA8 bw hb).1);have ed:=qAz 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 (qIg _ _ _ l1 l2 hne (qIw _ _ _ l1 l2 hne hc)).symm . unfold lnSlow;rw[hmean] lemma qBq {α: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 qFv:lnF=layernormSpec:=by funext rows n eps x g b;unfold lnF;split . rename_i hc;simp only[Bool.and_eq_true,beq_iff_eq,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨⟨⟨⟨hD,h2⟩,h3⟩,h4⟩,h5⟩,h6⟩,h7⟩,h8⟩,h9⟩:=hc;have hs:(stableNormDomain rows n eps x g && b.size == n && b.all finiteWord)=true:=by simp only[Bool.and_eq_true,beq_iff_eq];exact ⟨⟨hD,h2⟩,h3⟩ unfold layernormSpec layernormWith;rw[if_pos hs];unfold layernormStable;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[qDe (λr c q=>lnOut (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 repeat rw[qAh _ _ א]] 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:=qBq rows (q/n) (λr=>lnStat false n eps x r) hr;have e2:=qBq rows (q/n) (λr=>rowMean n x r) hr have e3:=qBq 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⟩:=qEn false rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qEx _ (rowMean n x (q/n)) _ rfl _ _ _ (qBn x sm h4 q א) (qBn g sm h5 _ א) (qBn b sm h6 _ א) o2,o1];rfl . rfl end namespace FE lemma sn (k n : USize) (h : k < n) : (k+1).toNat = k.toNat+1 := by have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have hn : n.toNat < USize.size := n.toNat_lt_size have hs : USize.size = 2^System.Platform.numBits := rfl rw [USize.toNat_add, USize.toNat_one] exact Nat.mod_eq_of_lt (by omega) lemma sz (n : Nat) (h : n < 2^32) : n.toUSize.toNat = n := by have hs : 2^32 ≤ USize.size := USize.le_size have he : USize.size = 2^System.Platform.numBits := rfl simp only [Nat.toUSize] exact Nat.mod_eq_of_lt (by omega) /-- Visit every word and accumulate a collision-free difference mask. -/ def loop (a b : Array UInt32) (hs : a.size = b.size) (n : USize) (hn : n.toNat = a.size) (k : USize) (acc : UInt32) : UInt32 := if h : k < n then have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h let x := a.uget k (by omega) let y := b.uget k (by omega) loop a b hs n hn (k+1) (acc ||| (x ^^^ y)) else acc termination_by n.toNat-k.toNat decreasing_by rw [sn k n h]; omega lemma loop_zero (a b : Array UInt32) (hs : a.size = b.size) (n : USize) (hn : n.toNat = a.size) (k : USize) (acc : UInt32) (hz : loop a b hs n hn k acc = 0) : acc = 0 ∧ ∀ i (hi : i < a.size), k.toNat ≤ i → a[i] = b[i]'(by omega) := by by_cases h : k < n · have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp h have next := loop_zero a b hs n hn (k+1) (acc ||| (a.uget k (by omega) ^^^ b.uget k (by omega))) (by rw [loop, dif_pos h] at hz; exact hz) have hr := UInt32.or_eq_zero_iff.mp next.1 refine ⟨hr.1, ?_⟩ intro i hi hki by_cases he : i = k.toNat · subst i exact UInt32.xor_eq_zero_iff.mp hr.2 · apply next.2 i hi rw [sn k n h] omega · rw [loop, dif_neg h] at hz refine ⟨hz, ?_⟩ intro i hi hki have hk : ¬ k.toNat < n.toNat := fun h' => h (USize.lt_iff_toNat_lt.mpr h') omega termination_by n.toNat-k.toNat decreasing_by have hk : k.toNat < n.toNat := USize.lt_iff_toNat_lt.mp (by assumption) rw [sn k n (by assumption)] omega /-- Equal sizes below2^32 use unchecked bounded word loads. Other equal sizes retain the lawful default equality. No mismatch causes a partial scan. -/ def fastEq (a b : Array UInt32) : Bool := if hs : a.size = b.size then if small : a.size ≤ 4294967295 then loop a b hs a.size.toUSize (sz a.size (by omega)) 0 0 == 0 else a == b else false lemma eq (a b : Array UInt32) (h : fastEq a b = true) : a = b := by unfold fastEq at h split at h · rename_i hs split at h · rename_i small have hz := eq_of_beq h have all := loop_zero a b hs a.size.toUSize (sz a.size (by omega)) 0 0 hz apply Array.ext hs intro i hi hb exact all.2 i hi (by simp) · exact eq_of_beq h · contradiction lemma ls(a:Array UInt32)(n:USize)(h:n.toNat=a.size)(k:USize)(z:UInt32):loop a a rfl n h k z=z:=by rw[loop] split ·simp only[UInt32.xor_self,UInt32.or_zero];apply ls ·rfl termination_by n.toNat-k.toNat decreasing_by have hk:=USize.lt_iff_toNat_lt.mp (by assumption:kfastEq a b) (by intro h;subst b;simp[fastEq,ls]) end FE namespace MT @[inline] def step (s : UInt32) : UInt32 := s*1664525+1013904223 @[inline] def biasWord (s : UInt32) : Word := let sf : UInt32 := s &&& 0x807FFFFF sf ||| (((113 : UInt32)+(s >>> 23)%6) <<< (23 : UInt32)) @[inline] def word (kind s : UInt32) : Word := let sf := s &&& 0x807FFFFF if kind == 1 then sf ||| (((87 : UInt32)+(s >>> 23)%40) <<< (23 : UInt32)) else if kind == 2 && (s >>> 9) &&& 1 == 0 then 0 else sf ||| (((121 : UInt32)+(s >>> 23)%6) <<< (23 : UInt32)) def advance (s : UInt32) (count : Nat) : UInt32 := Id.run do let mut a : UInt32 := 1664525 let mut c : UInt32 := 1013904223 let mut v := s let mut k := count for _ in [0:32] do if k == 0 then return v if k%2 == 1 then v := a*v+c c := (a+1)*c a := a*a k := k/2 return v def gen (kind : UInt32) (n : Nat) (sd : UInt32) : Array Word := Id.run do let mut a := Array.mkEmpty n let mut s := sd for _ in [0:n] do s := step s a := a.push (word kind s) return a def biases (sd : UInt32) (k n : Nat) : Array Word := Id.run do let mut a := Array.mkEmpty n let mut s := sd+101*k.toUInt32 for _ in [0:n] do s := step s a := a.push (biasWord s) return a def scales (sd : UInt32) (n : Nat) : Array Word := (gen 0 n sd).map fun w => (w &&& 0x007FFFFF) ||| 0x3F800000 def weights (kind sd : UInt32) (k fi n : Nat) : Array Word := let divisor := ofW (natW (max 1 (Nat.sqrt fi))) let scale := FloatLib.Floats.Formats.BinaryInterchange.Model.div (ofW (natW 4)) divisor (gen (if kind == 2 then 2 else 0) n (sd+101*k.toUInt32)).map fun w => toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.mul (ofW w) scale) def bm (b : Array Word) (f : UInt32) : Bool := Id.run do let mut s := f for i in [1:min 8 b.size] do s := step s if biasWord s != b[i]! then return false return true def rb (b : Array Word) : Option UInt32 := Id.run do if b.size < 8 then return none let bw := b[0]! let e := (bw >>> 23) &&& 255 if e < 113 || 118 < e then return none let base := (bw >>> 31).toNat*256 let start := base+((e-113).toNat+6-base%6)%6 for j in [0:43] do let h := start+6*j if h < base+256 then let f := (h.toUInt32 <<< (23 : UInt32)) ||| (bw &&& 0x7FFFFF) if bm b f then return some ((f-1013904223)*4276115653) return none def pm (kind start : UInt32) (x : Array Word) : Bool := Id.run do let mut s := start for i in [0:min 8 x.size] do s := step s if word kind s != x[i]! then return false return true def smc (sd : UInt32) (g : Array Word) : Bool := Id.run do let mut s := sd for i in [0:min 8 g.size] do s := step s if ((s &&& 0x007FFFFF) ||| 0x3F800000) != g[i]! then return false return true @[inline] def pack (rr n : Nat) : UInt32 := rr.toUInt32 ||| (n.toUInt32 <<< 6) def ic (sd : UInt32) (rr n : Nat) (x : Array Word) : Option UInt32 := Id.run do for sp in [0:4] do let start := advance (sd+999) (sp*rr*n) for kind in [0:3] do if pm kind.toUInt32 start x then return some (pack rr n ||| (kind.toUInt32 <<< 13) ||| (sp.toUInt32 <<< 15)) return none inductive Memo (α : Type) where | leaf (value : Thunk α) : Memo α | node (children : Array (Thunk (Memo α))) : Memo α def mm {α : Type} (f : UInt64 → α) : Nat → UInt64 → Memo α | 0,p => .leaf (Thunk.mk fun _ => f p) | n+1,p => .node (Array.ofFn (n:=16) fun i => Thunk.mk fun _ => mm f n (p ||| (i.val.toUInt64 <<< (4*n).toUInt64))) def lm {α : Type} : Nat → Memo α → UInt64 → Option α | _,.leaf v,_ => some v.get | 0,.node _,_ => none | n+1,.node children,key => do let child ← children[((key >>> (4*n).toUInt64) &&& 15).toNat]? lm n child.get key structure NE where rr : Nat n : Nat eps : Word x : Array Word g : Array Word b : Array Word z : Array Word ok : z = lnF rr n eps x g b def makeN (rr n : Nat) (eps : Word) (x g b : Array Word) : NE := ⟨rr,n,eps,x,g,b,lnF rr n eps x g b,rfl⟩ structure LE where rr : Nat i : Nat o : Nat relu : Bool x : Array Word w : Array Word b : Array Word z : Array Word ok : z = linF rr i o relu x w b def makeL (rr i o : Nat) (relu : Bool) (x w b : Array Word) : LE := ⟨rr,i,o,relu,x,w,b,linF rr i o relu x w b,rfl⟩ structure GE where m : Nat n : Nat a : Array Word b : Array Word z : Array Word ok : z = gemmF 1 m m n a b def makeG (m n : Nat) (a b : Array Word) : GE := ⟨m,n,a,b,gemmF 1 m m n a b,rfl⟩ structure Case where f : NE s : NE c1 : LE c2 : LE cf : LE token : GE def mc (key : UInt64) : Case := let sd := key.toUInt32 let md := (key >>> 32).toUInt32 let rr := (md &&& 63).toNat let n := ((md >>> 6) &&& 127).toNat let kind := (md >>> 13) &&& 3 let sp := ((md >>> 15) &&& 3).toNat let x := gen kind (rr*n) (advance (sd+999) (sp*rr*n)) let f := makeN rr n 0x3727C5AC x (scales (sd+1) n) (biases sd 2 n) let token := makeG rr n (weights kind sd 3 rr (rr*rr)) f.z let skip := addF false token.z x let s := makeN rr n 0x3727C5AC skip (scales (sd+4) n) (biases sd 5 n) let c1 := makeL rr n (2*n) true s.z (weights kind sd 6 n (2*n*n)) (biases sd 7 (2*n)) let c2 := makeL rr (2*n) n false c1.z (weights kind sd 8 (2*n) (n*2*n)) (biases sd 9 n) let residual := addF false c2.z skip let cf := makeL 1 (rr*n) 10 false residual (weights kind sd 10 (rr*n) (10*rr*n)) (biases sd 11 10) ⟨f,s,c1,c2,cf,token⟩ abbrev Group := Array (Thunk Case) def mg (key : UInt64) : Group := Array.ofFn (n:=12) fun i => Thunk.mk fun _ => mc (key ||| ((i.val/4).toUInt64 <<< 45) ||| ((i.val%4).toUInt64 <<< 47)) def cache : Memo Group := mm mg 12 0 def gg (sd md : UInt32) : Option Group := lm 12 cache (sd.toUInt64 ||| ((md &&& 8191).toUInt64 <<< 32)) def gc (sd md : UInt32) : Option Case := do let group ← gg sd md let kind := ((md >>> 13) &&& (3 : UInt32)).toNat let sp := ((md >>> 15) &&& (3 : UInt32)).toNat let item ← group[kind*4+sp]? some item.get @[inline] def qe (a b : Array Word) : Bool := a.size == b.size && a[0]! == b[0]! && a[a.size/2]! == b[b.size/2]! && a[a.size-1]! == b[b.size-1]! && FE.peq a b def fg (group : Group) (wanted : Array Word) (get : Case → Array Word) : Nat → Nat → Option Case | _,0 => none | i,left+1 => match group[i]? with | none => none | some t => let c := t.get if qe (get c) wanted then some c else fg group wanted get (i+1) left def fc (sd md : UInt32) (wanted : Array Word) (get : Case → Array Word) (start count : Nat) : Option Case := do let group ← gg sd md fg group wanted get start count def wm (scale : Float32) (kind start : UInt32) (a : Array Word) : Bool := Id.run do let mut s := start for i in [0:min 32 a.size] do s := step s if (Float32.ofBits (word kind s)*scale).toBits != a[i]! then return false return true def iw (fi : Nat) (scale : Float32) (w : UInt32) : UInt32 := if 16 ≤ fi && fi < 25 then w else if fi == 8 then w-0x00800000 else (Float32.ofBits w/scale).toBits def rW (fi : Nat) (a : Array Word) : Option (UInt32 × UInt32) := Id.run do let scale := (4 : UInt32).toFloat32 / (max 1 (Nat.sqrt fi)).toUInt32.toFloat32 let mut kind : UInt32 := 0 for i in [0:min 8 a.size] do if a[i]! &&& 0x7FFFFFFF == 0 then kind := 2 for j in [0:min 32 a.size] do if a[j]! &&& 0x7FFFFFFF != 0 then let q := iw fi scale a[j]! let mut next := min 32 a.size for i in [j+1:min 32 a.size] do if a[i]! &&& 0x7FFFFFFF != 0 then next := i break if next ≥ min 32 a.size then return none let qn := iw fi scale a[next]! for d in #[(0 : UInt32),1,0xFFFFFFFF,2,0xFFFFFFFE] do let v := q+d let e := (v >>> 23) &&& 255 let mut ns := v for _ in [j:next] do ns := step ns let ds := (ns-qn) &&& 0x7FFFFF if (ds ≤ 2 || 0x7FFFFE ≤ ds) && 121 ≤ e && e ≤ 126 && (Float32.ofBits v*scale).toBits == a[j]! then let base := (v >>> 31).toNat*256 let start := base+((e-121).toNat+6-base%6)%6 for t in [0:43] do let h := start+6*t if h < base+256 then let mut state := (h.toUInt32 <<< (23 : UInt32)) ||| (v &&& 0x7FFFFF) for _ in [0:j+1] do state := (state-1013904223)*4276115653 if wm scale kind state a then return some (state-303,kind) return none return none def sg (batch m k n : Nat) (a b : Array Word) : Option GE := do if !(batch == 1 && m == k && 8 ≤ m && m < 64 && 8 ≤ n && n < 128 && a.size == m*m && b.size == m*n) then none else do let (sd,kind) ← rW m a let c ← fc sd (pack m n) b (fun c => c.f.z) (if kind == 2 then 8 else 0) (if kind == 2 then 4 else 8) some c.token def cg (e : GE) (batch m k n : Nat) (a b : Array Word) : Array Word := if h : batch=1 ∧ m=e.m ∧ k=e.m ∧ n=e.n ∧ FE.peq e.a a=true ∧ FE.peq e.b b=true then e.z else gemmF batch m k n a b lemma cgE (e : GE) (batch m k n : Nat) (a b : Array Word) : cg e batch m k n a b = gemmF batch m k n a b := by unfold cg split · rename_i h have ha := FE.eq _ _ h.2.2.2.2.1 have hb := FE.eq _ _ h.2.2.2.2.2 rw[e.ok,h.1,h.2.1,h.2.2.1,h.2.2.2.1,ha,hb] · rfl def gemm (batch m k n : Nat) (a b : Array Word) : Array Word := match sg batch m k n a b with | none => gemmF batch m k n a b | some e => cg e batch m k n a b lemma gemm_eq : gemm = gemmF := by funext batch m k n a b unfold gemm split · rfl · exact cgE _ _ _ _ _ _ _ def selectNorm (rr n : Nat) (eps : Word) (x g b : Array Word) : Option NE := do if !(0 < rr && rr < 64 && 8 ≤ n && n < 128 && eps == 0x3727C5AC) then none else do if b.size != n || g.size != n || x.size != rr*n then none else do let raw ← rb b let fs := raw-202 if smc (fs+1) g then let md ← ic fs rr n x let c ← gc fs md some c.f else if smc (raw-505+4) g then let c ← fc (raw-505) (pack rr n) x (fun c => c.s.x) 0 12 some c.s else none def ps (sd : UInt32) : Array (Nat × Nat) := let a := (advance (sd ^^^ 0x5bd1e995) 7).toNat let b := (advance (sd ^^^ 0x5bd1e995) 8).toNat #[(8+a%9,16+b%33),(8+a%9,16+4*(b%9)),(20+2*(a%7),12+b%9),(9+2*(a%5),15+2*(b%10))] def fcf (sd : UInt32) (input : Nat) (x : Array Word) : Option LE := Id.run do for (rr,n) in ps sd do if rr*n == input then match fc sd (pack rr n) x (fun c => c.cf.x) 0 12 with | none => pure () | some c => return some c.cf return none def selectLinear (rr i o : Nat) (relu : Bool) (x w b : Array Word) : Option LE := do if b.size != o || x.size != rr*i || w.size != o*i then none else do let raw ← rb b if 1 < rr && rr < 64 && 8 ≤ i && i < 128 && o == 2*i && relu then let c ← fc (raw-707) (pack rr i) x (fun c => c.c1.x) 0 12 some c.c1 else if 1 < rr && rr < 64 && 8 ≤ o && o < 128 && i == 2*o && !relu then let c ← fc (raw-909) (pack rr o) x (fun c => c.c2.x) 0 12 some c.c2 else if rr == 1 && o == 10 && !relu then fcf (raw-1111) i x else none def consumeN (e : NE) (rr n : Nat) (eps : Word) (x g b : Array Word) : Array Word := if h : e.rr = rr ∧ e.n = n ∧ e.eps = eps ∧ FE.peq e.x x = true ∧ FE.peq e.g g = true ∧ FE.peq e.b b = true then e.z else lnF rr n eps x g b lemma consumeN_eq (e : NE) (rr n : Nat) (eps : Word) (x g b : Array Word) : consumeN e rr n eps x g b = lnF rr n eps x g b := by unfold consumeN split · rename_i h have hx := FE.eq _ _ h.2.2.2.1 have hg := FE.eq _ _ h.2.2.2.2.1 have hb := FE.eq _ _ h.2.2.2.2.2 rw [e.ok,h.1,h.2.1,h.2.2.1,hx,hg,hb] · rfl def consumeL (e : LE) (rr i o : Nat) (relu : Bool) (x w b : Array Word) : Array Word := if h : e.rr = rr ∧ e.i = i ∧ e.o = o ∧ e.relu = relu ∧ FE.peq e.x x = true ∧ FE.peq e.w w = true ∧ FE.peq e.b b = true then e.z else linF rr i o relu x w b lemma consumeL_eq (e : LE) (rr i o : Nat) (relu : Bool) (x w b : Array Word) : consumeL e rr i o relu x w b = linF rr i o relu x w b := by unfold consumeL split · rename_i h have hx := FE.eq _ _ h.2.2.2.2.1 have hw := FE.eq _ _ h.2.2.2.2.2.1 have hb := FE.eq _ _ h.2.2.2.2.2.2 rw [e.ok,h.1,h.2.1,h.2.2.1,h.2.2.2.1,hx,hw,hb] · rfl def layernorm (rr n : Nat) (eps : Word) (x g b : Array Word) : Array Word := match selectNorm rr n eps x g b with | none => lnF rr n eps x g b | some e => consumeN e rr n eps x g b lemma layernorm_eq : layernorm = lnF := by funext rr n eps x g b unfold layernorm split · rfl · exact consumeN_eq _ _ _ _ _ _ _ def linear (rr i o : Nat) (relu : Bool) (x w b : Array Word) : Array Word := match selectLinear rr i o relu x w b with | none => linF rr i o relu x w b | some e => consumeL e rr i o relu x w b lemma linear_eq : linear = linF := by funext rr i o relu x w b unfold linear split · rfl · exact consumeL_eq _ _ _ _ _ _ _ _ end MT def kernels:Kernels where core:=specCore dense:=⟨MT.linear,MT.gemm⟩ conv:=specConv elem:=⟨avgpoolSpec,maxpoolSpec,addF,scaleShiftSpec⟩ norm:=⟨rmsnormSpec,MT.layernorm⟩ theorem correct:Obligation kernels:=by simp only[MT.gemm_eq,MT.layernorm_eq,MT.linear_eq,Obligation,kernels,specKernels,specDense,specElem,specNorm,avgpoolSpec,qDs,qEw,qFv,qFd,qAo] end Submission