import ContractExact namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Floats.ExecFloat.Binary local notation"А"=>USize.lt_iff_toNat_lt local notation"Б"=>mul_le_mul_of_nonneg_right local notation"В"=>0x7FFFFFFF local notation"Г"=>mul_le_mul_of_nonneg_left local notation"Д"=>rationalWord local notation"Е"=>0xFFFFFFFF local notation"Ж"=>UInt32.le_iff_toNat_le local notation"З"=>List.range local notation"И"=>abs_of_nonneg local notation"Й"=>FloatArray.emptyWithCapacity local notation"К"=>Array.replicate local notation"Л"=>abs_nonneg local notation"М"=>Nat.mod_eq_of_lt local notation"Н"=>Nat.and_two_pow_sub_one_eq_mod local notation"О"=>Float.ofBits local notation"П"=>dite_eq_left local notation"Р"=>List.mem_range local notation"С"=>mul_nonneg local notation"Т"=>Nat.zero_add local notation"У"=>0x7F800000 local notation"Ф"=>ite_eq_right local notation"Х"=>mul_le_mul local notation"Ц"=>zpow_le_zpow_right₀ local notation"Ч"=>two_ne_zero local notation"Ш"=>dite_eq_right local notation"Щ"=>Model.roundDyadic local notation"Ъ"=>abs_add_le local notation"Ы"=>Model.isFinite local notation"Ь"=>Nat.mul_le_mul_right local notation"Э"=>zpow_add₀ local notation"Ю"=>Nat.cast_nonneg local notation"Я"=>getElem!_pos local notation"а"=>UInt32.toNat_inj local notation"б"=>abs_of_pos local notation"в"=>abs_mul local notation"г"=>abs_sub_comm local notation"д"=>UInt64.toNat_add local notation"е"=>Nat.mul_comm local notation"ж"=>Model.toReal_eq_unpackedToReal_toModel local notation"з"=>Real.sqrt local notation"и"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"й"=>zpow_natCast local notation"к"=>Model.toReal local notation"л"=>Finset.range local notation"м"=>Float.sqrt local notation"н"=>lt_of_lt_of_le local notation"о"=>mul_lt_mul_of_pos_right local notation"п"=>UInt32.toNat_and local notation"р"=>UInt64.toNat_shiftRight local notation"с"=>ite_eq_left local notation"т"=>FloatLib.Numerics.binaryRadix local notation"у"=>UInt32.lt_iff_toNat_lt local notation"ф"=>UInt64.lt_iff_toNat_lt local notation"х"=>zpow_pos local notation"ц"=>toDyadic_ofUInt32 local notation"ч"=>lt_of_le_of_lt local notation"ш"=>Nat.div_lt_of_lt_mul local notation"щ"=>Nat.shiftRight_eq_div_pow local notation"ъ"=>Nat.not_lt_zero local notation"ы"=>pow_le_pow_left₀ local notation"ь"=>0x3CE0000000000000 local notation"э"=>Model.modelSignBit local notation"ю"=>Nat.add_zero local notation"я"=>Float.Model.UnpackedFloat.unpack local notation"ذ"=>Finset.sum_congr local notation"ر"=>Nat.mod_lt local notation"ز"=>Nat.shiftLeft_eq local notation"س"=>0x80000000 local notation"ش"=>le_trans local notation"ص"=>UInt32.toNat_toUSize local notation"ض"=>USize.toNat_one local notation"ط"=>sq_nonneg local notation"ظ"=>0x3370000000000000 local notation"ع"=>Model.toModel_ofFloatModel local notation"غ"=>Finset.mem_range local notation"ػ"=>List.nodup_range local notation"ؼ"=>Nat.div_eq_of_lt local notation"ؽ"=>Nat.le_mul_of_pos_right local notation"ؾ"=>UInt64.toNat_and local notation"ؿ"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"ـ"=>getElem!_neg local notation"ف"=>Nat.zero_mul local notation"ق"=>UInt32.toNat_sub_of_le local notation"ك"=>List.foldl_cons local notation"ل"=>Bool.and_eq_true_iff local notation"م"=>List.mem_cons_of_mem local notation"ن"=>List.range_eq_range' local notation"ه"=>Nat.one_mul local notation"و"=>UInt32.le_refl local notation"ى"=>abs_sub local notation"ي"=>le_refl local notation"Ա"=>List.map_congr_left local notation"Բ"=>UInt32.toNat_ofNat' local notation"Գ"=>Nat.add_assoc local notation"Դ"=>List.mem_cons_self local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith 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 qEb (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,Model.ofUInt32,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z @[inline] def keyOf (w:Word):Int := if signOf w then -((mantOf w <<< idxOf w:Nat):Int) else ((mantOf w <<< idxOf w:Nat):Int) @[inline] def maxW (a b:Word):Word := if expOf a=255∨expOf b=255 then toWord (Model.maximum (ofW a) (ofW b)) else match compare (keyOf a) (keyOf b) with | .lt=>b | .gt=>a | .eq=>toWord (if Model.isZero (ofW a) && Model.isZero (ofW b) then Model.zero Fmt (Model.signBit (ofW a) && Model.signBit (ofW b)) else ofW a) lemma qGw {w:UInt32} (h:expOf w≠255):Ы (ofW w)=true:=by simp[ofW,isFinite_ofUInt32,h] lemma qEg (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 qGw (getElem!_pos A k א ▸ (hall k א).1) have hRf:∀ k (hk:k< R.size),Ы R[k]=true:=λk hk=>by simpa [hR,ofW] using qGw (getElem!_pos B k א ▸ (hall k א).2) have f k (_:0<=k) (h:k<0+L.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 qCb (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 qC6 (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[qCb] at hfin;exact Bool.noConfusion hfin . have he':=not_or.mp he obtain ⟨h1,h2,h3,h4,h5⟩ := Acc.add_spec ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))) hp hn (mul_mant_lt _ _) (by have:=idxOf_le _ he'.1;have:=idxOf_le _ he'.2;omega) have hstep:dotStep ⟨p,q,s,z⟩ (fa index) (fb index) = Acc.add ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))):=by simp[dotStep,he] rw[dotLoop,hstep] at hfin ⊢;obtain ⟨hP,hN,hs,hall,hD,hz⟩:=ih _ (index+1) א h1 h2 hfin;refine ⟨hP,hN,by rw[<-h3,hs],?_,?_,?_⟩ . intro k k1 k2;rcases Nat.lt_or_ge k (index+1) with hk | hk;. have:k=index :=א;subst this;rw[ha,hb];exact he' . exact hall k hk א . have sv2:(if Bool.xor (signOf (fa index)) (signOf (fb index)) then -((mantOf (fa index)*mantOf (fb index):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 qD3 (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 qHd (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 qFr (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 qHd _ l λj hj=>?_;have:=h j hj;rw[getElem!_def] at this;simp only[Array.getElem!_toList,getElem!_def] at *;cases e:A[j]?<;>simp_all lemma qIo (S:Int) (l: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 qAi:dotFn=dotOf:=by funext n fa fb;unfold dotFn dotOf;set A:=Array.ofFn (n:=n) λi=>fa i with hA;set B:=Array.ofFn (n:=n) λi=>fb i with hB;set acc:=dotLoop fa fb (К 18 0) (К 18 0) false true 0 n with hacc;by_cases hsp:acc.special=true;. simp[hsp] have hsp':acc.special=false:=by simpa using hsp have hF:=qC6 fa fb A B n (qAb n fa) (qAb n fb) ⟨К 18 0,К 18 0,false,true⟩ 0 n א ג ג hsp';obtain ⟨hP,hN,-,hall,hD,hz⟩:=hF;have hAs:A.size=n:=by simp[hA] have hBs:B.size=n:=by simp[hB] have hL:=qD3 acc.pos acc.neg 18 0 (λj hj=>absurd hj (ъ _));simp only[hsp',Bool.false_eq_true,ite_false];have e0:(⟨К 18 0,К 18 0,false,true⟩:Acc).total=0:=by decide rw[e0,Int.zero_add] at hD;rw[Bool.true_and] at hz;generalize Y_low acc.pos acc.neg 0 18=l at hL ⊢;have h1:=qFr acc.pos l (λj hj=>(hL j hj).1);have h2:=qFr acc.neg l (λj hj=>(hL j hj).2);rw[hP] at h1;rw[hN] at h2;rw[qIo,<-qEg A B א (λk hk=>hall k א א),hAs,<-hD,<-hz];congr 1;have:acc.total=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList:=rfl;rw[this,h1,h2];push_cast;ring lemma usz (t n: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 qDo (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 qBd (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 (qBd 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 (qDo 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 (qBd 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 qDx: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 qGe:pw.size=512:=by simp[FloatArray.size,qDx] lemma qCh (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pw.size:=by rw[qGe];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega def pwEntryN (i:Nat):Float := if i%256 == 0 || i%256 == 255 then О 0x7FF8000000000000 else pwEntry i def pwN:FloatArray := (З 512).foldl (λa i=>a.push (pwEntryN i)) (Й 512) lemma qDj:pwN.data=(З 512).toArray.map pwEntryN:=by have key:∀ (l:List Nat) (a:FloatArray),(l.foldl (λa i=>a.push (pwEntryN i)) a).data=a.data ++ (l.toArray.map pwEntryN):=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[pwN,key];simp[FloatArray.emptyWithCapacity] lemma qBx (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pwN.size:=by have e:pwN.size=512:=by simp[FloatArray.size,qDj] rw[e];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 (qCh w) @[inline] def dvB (v:Float) (w:UInt32):Float := (v*pw.uget (w >>> (23:UInt32)).toUSize (qCh 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:=if r == 1 then scanF A A.usize 0 (qAc A) 0 Е else Е if h == Е then scan A A.usize 0 (qAc A) Е 0 else h @[inline] def scanW (r:Nat) (A:Array UInt32):UInt32 := if r == 8 || r == 1 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 @[inline] 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 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) def go4D (x wd: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) (hd:wd.size=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 go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd (a+v*wd.uget (w0+i) (hd ▸ wi w0 i m W h0 hs him)) (b+v*wd.uget (w1+i) (hd ▸ wi w1 i m W h1 hs him)) (c+v*wd.uget (w2+i) (hd ▸ wi w2 i m W h2 hs him)) (d+v*wd.uget (w3+i) (hd ▸ wi w3 i m W h3 hs him)) else go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd a b c d else ⟨a,b,c,d⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) lemma qA9 {n:Nat} (h:n<=4294967295):n<2^32 :=א 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) (out:Array UInt32):Array UInt32 := 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 then go4D x wd ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 hd 0 0 0 0 else go4B x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 (qA9 hg.2.2.1) hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 let o0:=fin8 inW relu X W b i j r.a nf a hx (rdW b ju) m let o1:=fin8 inW relu X W b i (j+1) r.b nf a hx (rdW b (ju+1)) m let o2:=fin8 inW relu X W b i (j+2) r.c nf a hx (rdW b (ju+2)) m let o3:=fin8 inW relu X W b i (j+3) r.d nf a hx (rdW b (ju+3)) m let out:=wrW out (base+ju) o0 let out:=wrW out (base+ju+1) o1 let out:=wrW out (base+ju+2) o2 let out:=wrW out (base+ju+3) o3 cols1 inW outW relu X W b i ix x wd nf a hx m n (j+4) base out else out else if h1:j< outW then let ju:=j.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 (qA9 hg.2.2.1) hg.2.2.2 0 let o:=fin8 inW relu X W b i j r nf a hx (rdW b ju) m cols1 inW outW relu X W b i ix x wd nf a hx m n (j+1) base (wrW out (base+ju) o) else out else out termination_by outW-j def rows1 (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (wd:FloatArray) (nf:Float) (hv: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 out rows1 rows inW outW relu X W b wd nf hv m (i+1) r.ix r.buf out else out termination_by rows-i @[inline] def dvN (w:UInt32):Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pwN.uget (w >>> (23:UInt32)).toUSize (qBx w) @[inline] def dvNB (v:Float) (w:UInt32):Float := (v*pwN.uget (w >>> (23:UInt32)).toUSize (qBx w))*((w &&& 0x7FFFFF) ||| 0x800000).toFloat structure F5 where (a b c d:Float) (p:UInt32) def go4N (x:FloatArray) (W:Array UInt32) (w0 w1 w2 w3 n t:USize) (hx:n.toNat<=x.size) (hs:W.size<2^32) (h0:w0.toNat+n.toNat<=W.size) (h1:w1.toNat+n.toNat<=W.size) (h2:w2.toNat+n.toNat<=W.size) (h3:w3.toNat+n.toNat<=W.size) (a b c d:Float) (p:UInt32):F5 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let u0:=W.uget (w0+t) (wi w0 t n W h0 hs ht) let u1:=W.uget (w1+t) (wi w1 t n W h1 hs ht) let u2:=W.uget (w2+t) (wi w2 t n W h2 hs ht) let u3:=W.uget (w3+t) (wi w3 t n W h3 hs ht) go4N x W w0 w1 w2 w3 n (t+1) hx hs h0 h1 h2 h3 (a+v*dvN u0) (b+dvNB v u1) (c+v*dvN u2) (d+dvNB v u3) (p ||| u0 ||| u1 ||| u2 ||| u3) else ⟨a,b,c,d,p⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];omega structure CN where (out:Array UInt32) (ok:Bool) def colsN (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (x:FloatArray) (nf l:Float) (n:USize) (j:Nat) (out:Array UInt32):CN := if h:j< outW∧4<=outW then let k:=if j+4<=outW then j else outW-4 let ju:=k.toUSize let w0:=ju*n let w1:=(ju+1)*n let w2:=(ju+2)*n let w3:=(ju+3)*n if hg:n.toNat<=x.size∧W.size<=4294967295∧w0.toNat+n.toNat<=W.size ∧ w1.toNat+n.toNat<=W.size∧w2.toNat+n.toNat<=W.size∧w3.toNat+n.toNat<=W.size then let r:=go4N x W w0 w1 w2 w3 n 0 hg.1 (qA9 hg.2.1) hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2 0 0 0 0 0 let p:=r.p &&& В if (r.a+r.b+r.c+r.d).isFinite && p<У then let o0:=fin8 inW relu X W b 0 k r.a nf l p (rdW b ju) n let o1:=fin8 inW relu X W b 0 (k+1) r.b nf l p (rdW b (ju+1)) n let o2:=fin8 inW relu X W b 0 (k+2) r.c nf l p (rdW b (ju+2)) n let o3:=fin8 inW relu X W b 0 (k+3) r.d nf l p (rdW b (ju+3)) n let out:=wrW out ju o0 let out:=wrW out (ju+1) o1 let out:=wrW out (ju+2) o2 let out:=wrW out (ju+3) o3 colsN inW outW relu X W b x nf l n (k+4) out else ⟨out,false⟩ else ⟨out,false⟩ else ⟨out,decide (outW<=j)⟩ termination_by outW-j decreasing_by split<;>omega 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 (qA9 hg.2.1) (qA9 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 if rows == 1 then let r:=decR X (0*inW).toUSize n 0 (К inW 0) (pushZ n 0 (Й inW)) 0 0 let c:CN:=if r.c == n then colsN inW outW relu X W b r.buf nf r.l n 0 out else ⟨out,false⟩ if c.ok then c.out else let h1:=scanH 1 W if h1≠Е then cols1 inW outW relu X W b 0 r.ix r.buf (Й 0) nf r.l h1 n r.c 0 (0*outW).toUSize out else linearWith dotFn rows inW outW relu X W b else let wd:=if 8< rows then W.foldl (λa w=>a.push (dv w)) (Й W.size) else Й 0 rows1 rows inW outW relu X W b wd nf hw n 0 (К inW 0) (pushZ n 0 (Й inW)) out else linearWith dotFn rows inW outW relu X W b section local notation:max x:max "ᶠ"=>Float.isFinite x=true local notation "⟡"=>FloatFormat.binary64 local notation "⟠"=>Float.Model.Format.binary64 noncomputable def R (f: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 qAj:⟡.isIEEE=true:=by decide lemma qAy (a:Model ⟡) : Ы a=(Model.toModel a).isFinite:=by rw[<-Model.toDyadic?_isSome_eq_isFinite,Model.toDyadic?_ieee_eq_model _ qAj];cases Model.toModel a<;>rfl abbrev A_M (x:Float):Model ⟡:=Model.ofFloatModel x.toModel lemma A_R (x:Float):R x=к (A_M x):=rfl lemma qAh (x:Float):Ы (A_M x)=x.isFinite:=by rw[qAy,ع];rfl lemma qA3 {x:Float} (h:xᶠ):Ы (A_M x)=true :=(qAh x).trans h lemma qA0 (e:ℤ):Formats.Flocq.bpow т e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma qCd:(2:ℝ)^(1000:ℤ)<=к (Model.posMaxFinite ⟡):=by rw[Model.toReal_posMaxFinite,show Model.pow2 ⟡.fracWidth + ⟡.maxFiniteFracField=2^53-1 by decide +kernel, show ⟡.maxNormalExponent-Int.ofNat ⟡.fracWidth=971 by decide +kernel,Model.bpow,qA0,show (1000:ℤ)=29+971 from rfl,Э Ч] exact Б ב (by positivity) open Float.Model.UnpackedFloat in lemma qDv {b:BitVec 64} {s m e h} (a:unpack ⟠ b=.finite s m e h) : 2^52<=m∨e=-1074:=by unfold Float.Model.UnpackedFloat.unpack at a;simp only at a;rw[show (⟠.exponentBias:ℤ)=1023 by decide] at a;split_ifs at a with h1 h2 h3;. injection a with _ _ he;rw[show (unpackExponent (spec:=⟠) b).toNat=0 by simp[h3]] at he;omega . injection a with _ hm;rw[<-hm,BitVec.toNat_append];exact .inl (ش (by simp[Nat.shiftLeft_eq]) Nat.left_le_or) open Float.Model.UnpackedFloat in lemma qH1 {s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂} {f g:BitVec 64} (e:unpack ⟠ f=.finite s₁ m₁ e₁ h₁) (b:unpack ⟠ g=.finite s₂ m₂ e₂ h₂) : e₁+e₂<=⟠.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have a:2^52<=m₁*m₂->52<=(m₁*m₂).log2:=(Nat.le_log2 (Nat.mul_ne_zero h₁.ne' h₂.ne')).2 simp only[Float.Model.Format.targetExponent,Float.Model.totalExponent,Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] norm_num rcases qDv e with c | c . have:=a (c.trans (ؽ _ h₂));omega rcases qDv b with d | d . have:=a (d.trans (Nat.le_mul_of_pos_left _ h₁));omega omega lemma qB0 (x y: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 _ qAj,Model.toModel_ofFloatModel];change x.toModel.unpack.isFinite=true at a;change y.toModel.unpack.isFinite=true at b;have c:∀ s t:Float.Model.UnpackedFloat.Sign,Model.ofModel ⟡ (.zero (s*t)) =Model.zero _ (э s ^^ э t):=λs t=>by cases s<;>cases t<;>simp[Model.modelSignBit] <;>first | exact (Model.negZero_eq_ofModel_zero _).symm | exact (Model.posZero_eq_ofModel_zero _).symm cases hxm:x.toModel.unpack<;>cases hym:y.toModel.unpack <;>simp only[hxm,hym,Float.Model.UnpackedFloat.isFinite,Bool.false_eq_true] at a b case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => have d:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] have e:Model.modelSign (э s₁ ^^ э s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul];rw[Model.roundWithAccuracy_exact_eq_round_of_le_targetExponent _ _ _ _ (qH1 hxm hym),d];simp only[Bool.false_eq_true,ite_false,Model.roundDyadic,qAj,ite_true,Model.ieeeRoundDyadic,e];rfl all_goals simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul,c] lemma A_E (z: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 ⟨(qAh z).symm.trans b,d⟩ lemma qEd (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 _ (qB0 x y hx hy) (Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (hb.trans qCd)) (Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (hb.trans qCd)) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qAr:0< A_z:=by unfold A_z;positivity lemma A_p (a b c d:ℤ) (h:a+b<=c+d):(2:ℝ)^a*2^b<=2^c*2^d:=by rw[<-Э Ч,<-Э Ч];exact Ц one_le_two h lemma qAw:A_z<=1/2^200*(1/2^200):=by simpa [A_z] using A_p (-1021) 0 (-200) (-200) ב lemma qI1:0< A_B:=by unfold A_B;positivity lemma qCc:(2:ℝ)^256*2^42<=A_B:=by simpa [A_B] using A_p 256 42 300 0 ב lemma qAn:(2:ℝ)^128<=A_B :=(by norm_num:(2:ℝ)^128<=2^256*2^42).trans qCc lemma qE7 {k:ℕ} (h:k<=2^128):(k:ℝ)<=A_B :=(show (k:ℝ)<=2^128 by exact_mod_cast h).trans qAn lemma qE3:A_B*2<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 1 1000 0 ב lemma qDe:A_B*A_B<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 300 1000 0 ב lemma qFp (x:ℝ) : |Model.roundAt ⟡ x-x| ≤ 1/2^53*(|x| + A_z):=by rw[show (1/2^53*(|x| + A_z):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ) by rw[show (1/2^53:ℝ)=2^(-53:ℤ) by norm_num,show (-1074:ℤ)=-53+-1021 from rfl,Э Ч (-53),mul_add] rfl] by_cases a:x=0 . rw[a,Model.roundAt_zero,sub_self,abs_zero];positivity refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf ⟡) x/2<=_ rw[Formats.Flocq.ulp.of_ne_zero _ _ x a] simp only[Formats.Flocq.cexp,Model.fexpOf,Formats.Flocq.fltExp] have b:=Formats.Flocq.bpow_magnitude_sub_one_le т x a rw[qA0] at b rw[qA0,show ((⟡.fracWidth:ℤ)+1)=53 from rfl,show ⟡.minSubnormalExponent=-1074 from rfl] generalize Formats.Flocq.magnitude т x=M at b ⊢ have d:(0:ℝ)<=2^(-53:ℤ) * |x|:=by positivity have c:(0:ℝ)<2^(-1074:ℤ):=by positivity rcases le_total (-1074) (M-53) with h | h . rw[max_eq_left h,show (2:ℝ)^(M-53)/2=2^(-53:ℤ)*2^(M-1) by rw[<-Э Ч,show (-53:ℤ)+(M-1)=M-53-1 by ring,zpow_sub₀ Ч _ 1,zpow_one]] linarith [Г b (show (0:ℝ)<=2^(-53:ℤ) by positivity)] . rw[max_eq_right h];exact (half_le_self c.le).trans (le_add_of_nonneg_left d) lemma qFc {z:Float} {c:ℝ} (h:zᶠ∧R z=Model.roundAt ⟡ c) : zᶠ∧|R z-c| ≤ 1/2^53*(|c| + A_z) :=⟨h.1,h.2 ▸ qFp c⟩ lemma qEe {a b:ℝ} (d : |a| ≤ A_B) (c : |b| ≤ A_B) : |a| + |b| ≤ к (Model.posMaxFinite ⟡):=by have:=qE3 have:=qCd linarith section variable (x y:Float) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) include x y hx hy h1 h2 lemma qBf : (x+y)ᶠ∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) := qFc (A_E _ (toModel_ofFloat_add_of_isFinite x y hx hy) (Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2)) (Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2))) lemma qDf : (x-y)ᶠ∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) := qFc (A_E _ (toModel_ofFloat_sub_of_isFinite x y hx hy) (Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2)) (Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAj (qA3 hx) (qA3 hy) (qEe h1 h2))) lemma qCf : (x*y)ᶠ∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) :=qFc (qEd x y hx hy ((Х h1 h2 (Л _) qI1.le).trans qDe)) end lemma qA2 (m k:ℤ) (hm:m.natAbs<2^53) (hk:-1074<=k) : Model.roundAt ⟡ ((m:ℝ)*2^k)=(m:ℝ)*2^k:=by apply Formats.Flocq.round_preserves_generic;exact (Formats.Flocq.generic_format_FLT_iff (⟡.minSubnormalExponent) (⟡.fracWidth+1) (by decide) _).2 ⟨by decide,⟨m,k⟩,by simp[Formats.Flocq.toReal,qA0],hm,hk⟩ lemma qBk (x:Float):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,ж qAj,ع] lemma qE0 {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 qAj (A_M f) (by rw[a];rfl);rw[a] at b;change _=Float.Model.UnpackedFloat.finite .positive m e h at b;rw[<-b];exact Model.toModel_canonicalizeModel _ . rw[<-a];exact Model.toModel_canonicalizeModel _ lemma qBl (f: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[<-qAh,a];exact Model.isFinite_ofModel_zero _ qAj _,?_⟩;rw[A_R f.abs,a,qBk f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAj] | finite s m e h => have b:=qE0 hu;rw[hu] at a;have c:A_M f.abs=Model.ofModel ⟡ (.finite .positive m e h):=a;refine ⟨by rw[<-qAh,c,qAy,b];rfl,?_⟩;rw[A_R f.abs,c,qBk f,hu,ж qAj,b];simp only[Model.unpackedToReal_finite,qA0];cases s<;>simp[Model.modelSignBit,abs_of_nonneg (by positivity:(0:ℝ)<=(m:ℝ)*2^e)] lemma qCl (f: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 _ qAj (k:ℤ) . rw[<-и,FloatLib.Numerics.Dyadic.ofScaledInt_toRat];simp have a:Ы (A_M f)=true:=by rw[b];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qAj;rw[c,И (Ю _)];exact ((qE7 (א:k<=2^128)).trans ((le_mul_of_one_le_right qI1.le one_le_two).trans qE3)).trans qCd refine ⟨(qAh f).symm.trans a,?_⟩;rw[A_R,b,Model.toReal_roundDyadic_eq_roundAt _ qAj _ (b ▸ a),c];simpa using qA2 k 0 (by simpa using g) ב lemma qCk (m:UInt64) (h:m.toNat<2^53):m.toFloatᶠ∧R m.toFloat=m.toNat :=qCl _ _ h rfl lemma qHg:(0:Float)ᶠ∧R 0=0:=by simpa using qCl 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 qCu (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 qEm:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qDa (i:ℕ) (b:i<512) : (pwEntry i)ᶠ∧R (pwEntry i) =if i%256=0 then 0 else (if 256<=i then -1 else 1)*2^(((i%256:ℕ):ℤ)-150):=by have a:=qEm i b unfold A_chk at a rw[qCu,qBk] change (О (A_bits i)).toModel.unpack.isFinite=true∧_ rw[show (О (A_bits i)).toModel.unpack =я ⟠ (A_bits i).toBitVec from Model.unpack_pack_unpack _ _] generalize я ⟠ (A_bits i).toBitVec=u at a cases u with | notANumber | infinity _=>simp at a | zero s => simp only[beq_iff_eq] at a exact ⟨rfl,by simp[a]⟩ | finite s m e h => simp only[Bool.and_eq_true,beq_iff_eq,bne_iff_ne] at a obtain ⟨⟨⟨g,d⟩,f⟩,c⟩:=a refine ⟨rfl,?_⟩ rw[Model.unpackedToReal_finite,qA0,f,g,d,mul_assoc, show ((2^52:ℕ):ℝ)*2^(((i%256:ℕ):ℤ)-202) =2^(((i%256:ℕ):ℤ)-150) by rw[Nat.cast_pow,Nat.cast_ofNat,<-й,<-Э Ч] congr 1 push_cast ring] simp only[decide_eq_true_eq,c,↓reduceIte] lemma qCw (i:ℕ) (a:i<512):∃ σ:ℤ,σ.natAbs<=1 ∧ R (pwEntry i)=σ*2^(((i%256:ℕ):ℤ)-150):=by rw[(qDa i a).2];split_ifs;exacts [⟨0,by norm_num,by simp⟩,⟨-1,by norm_num,by simp⟩,⟨1,by norm_num,by simp⟩] lemma qCm (f: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⟩:=qEd f _ q (qDa i g).1 ((Х m h (Л _) qI1.le).trans qDe);refine ⟨j,?_⟩ have:=qA2 (c*b) (k+E) (by rw[Int.natAbs_mul];have:=Nat.mul_le_mul_left c.natAbs a omega) א rw[Э Ч] at this;push_cast at this;rw[n,d,l,show (c:ℝ)*2^k*(b*2^E)=c*b*(2^k*2^E) by ring];exact this lemma qDc (i:USize) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,qDx] section variable (w:UInt32) include w lemma qAq:(w &&& В).toNat=w.toNat%2^31:=by rw[п];exact Н w.toNat 31 lemma qCo:(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[п];exact Н w.toNat 23 lemma qD8:(w >>> 23).toNat=w.toNat/2^23:=by rw[UInt32.toNat_shiftRight,щ];rfl lemma qCa:(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf;rw[п,qD8];exact Н _ 8 lemma qGl: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 qHa : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,qCo,Nat.or_comm];simpa using (Nat.two_pow_add_eq_or_of_lt (Nat.mod_lt w.toNat (by positivity:2^23 > 0)) 1).symm lemma qAz (h:good w=true) : w.toNat%2^31=0∨(2^23<=w.toNat%2^31∧w.toNat%2^31<У):=by have a:=qAq w;have b:=(w &&& В).toNat_lt;unfold good at h;simp only[Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-a,h];rfl . right;rw[Ж,UInt32.toNat_sub] at h;rw[<-a];generalize (w &&& В).toNat=v at *;simp at h;omega lemma qAe (h:good w=true):expOf w≠255:=by intro he;have a:=qCa w;rw[he] at a;change 255=_ at a;have:=qAz w h;have:=w.toNat_lt;omega end noncomputable def A_mag (v:ℕ):ℝ := if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma qAs (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qA7 (w: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:=qAz w hg;have a:=qCa w;have d:(fracOf w).toNat=w.toNat%2^23:=qCo w;have f:wv w=(decodeD w).toReal:=by unfold wv ofW;rw[Model.toReal_eq,ц w (qAe w hg)] rw[f];unfold decodeD A_mag;by_cases c:w.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,qGl];simp;split_ifs<;>ring lemma qFb (v:ℕ) (h:v<У):A_mag v<=2^128:=by unfold A_mag;split_ifs with h0;. positivity . exact (Х (show ((2^23+v%2^23:ℕ):ℝ)<=((2^24:ℕ):ℝ) from Nat.cast_le.2 א) (Ц one_le_two (by omega:_<=(104:ℤ))) (by positivity) (by positivity)).trans_eq ב lemma qA6 (a b:ℕ) (hab:a<=b) (ha:a=0∨2^23<=a):A_mag a<=A_mag b:=by rcases ha with rfl | ha;. simpa [A_mag] using qAs b unfold A_mag;simp only[show ¬ a=0 by omega,show ¬ b=0 by omega,↓reduceIte];have c:a/2^23<=b/2^23:=Nat.div_le_div_right hab;rcases c.eq_or_lt with d | g;. rw[d];exact Б (Nat.cast_le.2 א) (by positivity) . have e:(2:ℝ)^(((a/2^23:ℕ):ℤ)-150)*2<=2^(((b/2^23:ℕ):ℤ)-150):=by rw[<-zpow_add_one₀ Ч];exact Ц one_le_two א have f:((2^23:ℕ):ℝ)<=((2^23+b%2^23:ℕ):ℝ):=Nat.cast_le.2 א calc _<=((2^23*2:ℕ):ℝ)*2^(((a/2^23:ℕ):ℤ)-150) := Б (Nat.cast_le.2 א) (by positivity) _=((2^23:ℕ):ℝ)*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by push_cast;ring _<=_:=Х f e (by positivity) (by positivity) abbrev A_mf (w:UInt32):Float:=((w &&& 0x7FFFFF) ||| 0x800000).toFloat lemma qD1 (w:UInt32):pw.uget (w >>> 23).toUSize (qCh 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:=qHa w;obtain ⟨d,a⟩:=qCl (A_mf w) _ (by rw[c];omega) rfl;rw[c] at a;have b:w.toNat/2^23<512 :=א obtain ⟨f,e⟩:=qCm _ _ b d ((2^23+w.toNat%2^23:ℕ):ℤ) 0 א ב (by rw[a,И (Ю _)] exact qE7 א) (by rw[a,zpow_zero,mul_one,Int.cast_natCast]) refine ⟨?_,b,d,f,e,a⟩;rw[qDc,ص,qD8] lemma qAm (w:UInt32) (hg:good w=true):(dv w)ᶠ∧R (dv w)=wv w:=by obtain ⟨g,a,-,e,d,c⟩:=qD1 w;have j:=w.toNat_lt;unfold dv;rw[g];refine ⟨e,?_⟩;rw[d,c,(qDa _ a).2,qA7 w hg];have b:=qAz w hg;generalize w.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 qC4 {a:Type} (l:List a) (f:a->ℝ):0<=(l.map λq=>|f q|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨q,_,rfl⟩;exact Л _) lemma qCe (l:List Float):0<=A_sa l:=qC4 l R lemma qGr (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 qC7 (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 qBo:A_z<=1:=qAw.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 qDd : (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 ⟨qHg.1,by simp[A_fsum,qHg.2,A_sr]⟩ | append_singleton l q ih => obtain ⟨e,f,n⟩:=qGr l q rw[e] at hb rw[List.length_append,List.length_singleton] at hlen have t:=qCe l have j:=Л (R q) obtain ⟨h,v⟩:=ih (λx i=>hf x (by simp[i])) א ד have u:(l.length:ℝ)+1<=2^21:=by exact_mod_cast hlen have d:(0:ℝ)<=l.length:=Ю _ have y:=qC7 l have o:=qAr have m:=qBo have p:=qAn have s:=abs_sub_abs_le_abs_sub (R (A_fsum l)) (A_sr l) have w:=Б (show (l.length:ℝ)<=2^21 by linarith) (ד:0<=A_sa l+A_z) obtain ⟨b,z⟩:=qBf _ q h (hf q ג) ד ד rw[n,e,f,List.length_append,List.length_singleton] refine ⟨b,?_⟩ push_cast generalize R (A_fsum l)=r at * generalize R q=a at * generalize A_sa l=P at * generalize A_sr l=S at * generalize (l.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 qBz : (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⟩:=qDd l hf hlen hb have c:=С (Nat.cast_nonneg (α:=ℝ) l.length) (add_nonneg (qCe l) qAr.le) exact ⟨a,b.trans ד⟩ end lemma qB7 (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[(qBl q (c q b)).2] have:=qBz (l.map Float.abs) (λq b=>by obtain ⟨x,e,rfl⟩:=List.mem_map.mp b exact (qBl x (c x e)).1) (by simpa) (a.1.symm ▸ f) rwa [a.1,a.2,List.length_map] at this lemma qIn (n:ℕ) (g:n<2^20) : (nfOf n)ᶠ∧R (nfOf n)=((n:ℝ)+8)/2^53:=by have b:(n.toUInt64+8).toNat=n+8:=by have c:(8:UInt64).toNat=8:=rfl;simp[UInt64.toNat_add,c];omega obtain ⟨f,a⟩:=qCk (n.toUInt64+8) (by rw[b];omega);rw[b] at a obtain ⟨e,d⟩:=qCm _ 97 ב f ((n+8:ℕ):ℤ) 0 א ב (by rw[a,И (Ю _)] exact qE7 א) (by rw[a];simp) push_cast at a;rw[show nfOf n=(n.toUInt64+8).toFloat*pwEntry 97 by rw[nfOf,qCu];rfl];refine ⟨e,?_⟩;rw[d,a,(qDa 97 ב).2];norm_num;ring lemma qGb {n:ℕ} (l:n<2^20) {s t: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⟩:=qIn n l;have E:=qAr;have B:=qAw;have a:0<=((n:ℝ)+8)/2^53:=by positivity have C:=abs_sub_abs_le_abs_sub (R t) Y;rw[И e] at C;have i : |R t| ≤ 2*Y+1/2^250 :=ד;have x : |R (nfOf n)*R t| ≤ 1/2^30*(2*Y+1/2^250):=by rw[в,b,И a];exact Х ד i (Л _) ב obtain ⟨d,f⟩:=qCf _ _ h m (by rw[b,И a];linarith) ד;have D:=abs_sub_abs_le_abs_sub (R (nfOf n*t)) (R (nfOf n)*R t);obtain ⟨z,j⟩:=qBf s _ k d ד ד;obtain ⟨r,g⟩:=qDf s _ k d ד ד;have H:=abs_sub_abs_le_abs_sub (R (s+nfOf n*t)) (R s+R (nfOf n*t));have q:=Ъ (R s) (R (nfOf n*t));have p:=ى (R s) (R (nfOf n*t));rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at c;have G:=Г (abs_le.mp u).1 a;have M:=Б (show (n:ℝ)<=2^20 by linarith) e;rw[b] at f x D;obtain ⟨K,J⟩:=abs_le.mp y;obtain ⟨O,N⟩:=abs_le.mp j;obtain ⟨F,L⟩:=abs_le.mp g;obtain ⟨P,-⟩:=abs_le.mp f;constructor<;>linarith lemma qEv (g h:ℕ->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 qC9 (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 qB5 : |wv w| = A_mag (w.toNat%2^31):=by rw[qA7 w hg,в,И (qAs _)];split_ifs<;>simp lemma qAo : |wv w| ≤ 2^128:=by rw[qB5 w hg];apply qFb;rcases qAz w hg with h | h<;>omega lemma qBr (h:w &&& В=0):wv w=0:=by rw[qA7 w hg,<-qAq w,h];simp[A_mag] end lemma qHc (a b: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⟩:=qAm a ha obtain ⟨i,d⟩:=qAm b hb have c:=qAo a ha have e:=qAo b hb obtain ⟨h,f⟩:=qCf _ _ j i (g ▸ c.trans qAn) (d ▸ e.trans qAn) rw[g,d] at f exact ⟨h,f,by rw[в] exact (Х c e (Л _) (by positivity)).trans_eq ב⟩ lemma qBs {n:ℕ} {a b:ℕ->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[qBr (a t) (g t e) h] . simp[qBr (b t) (j t e) h] lemma qFu {n:ℕ} {l:List ℕ} (b:l.Nodup) (a:∀ t∈l,t< n):l.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 qFj {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=>qHc _ _ (e t (a t d)) (f t (a t d)) lemma qCr {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:=qAr obtain ⟨o,r⟩:=abs_le.mp f obtain ⟨n,q⟩:=abs_le.mp d have k:=Л (p x) have h:=abs_sub_abs_le_abs_sub (p x) (R (g x)) have l:=abs_sub_abs_le_abs_sub (R (g x)) (p x) rw[г] at h refine ⟨abs_le.mpr ⟨?_,?_⟩,?_,?_⟩<;>push_cast<;>linarith lemma qHh (l:List ℕ) (g:ℕ->Float) (b:Float) : ((l.map g ++ [b]).length:ℝ)=l.length+1:=by simp lemma qHf {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 qCs {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[qA7 a d,М א] simp only[show ¬ 2^31<=a.toNat by omega,↓reduceIte,one_mul] refine ⟨b ▸ qAs _,(le_abs_self _).trans (qAo a d),λw c f=>?_⟩ have e:=Ж.mp f rw[qAq] at e rw[qB5 _ c,b] exact qA6 _ _ e ((qAz _ c).imp_right (·.1)) section variable {n:ℕ} {ts:List ℕ} {g:ℕ->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 qEz (k:tᶠ) (b:Y<=A_B/8) (o:A_sa (ts.map g ++ [dv bw])+A_z<=(1+1/2^22)*Y) (u:(1+1/2^22)*((ts.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⟩:=qAm bw D obtain ⟨j,-,-⟩:=qCr (dv bw) C rw[d,<-E] at j have i:=qCe (ts.map g ++ [dv bw]) have r:=qAr have l:(ts.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⟩:=qDd _ (qHf C c) (by simp;omega) ד rw[qHh] at h generalize ts.map g ++ [dv bw]=L at * have x:=qC7 L have y:=Б l a have v:=Х (show (ts.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 qGb A f k a b ד m ד G lemma qDi (P:good hh=true) (hp:hh<=В) (U:(ts.map λt=>|p t|).sum<=wv hh*J) (m:aᶠ) (d:0<=J) (z:J<=2^149) (v : |R a-J| ≤ (J+A_z)/2^32) (he:e=nfOf n*(dv hh*a+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by obtain ⟨b,h,-⟩:=qCs P hp;obtain ⟨Q,N⟩:=qAm hh P;obtain ⟨q,j⟩:=qAm bw D;obtain ⟨-,K,-⟩:=qCr (dv bw) C;rw[j] at K;have S:=qAo bw D;generalize wv hh=H at *;have hk:(ts.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:ts.length+1<=2^20) have L:(0:ℝ)<=ts.length:=Ю _;have M:=qAr;have x:=qAw;have r:=qC4 ts p;have hBl:=qCc;have T:=qAn;have u:=abs_sub_abs_le_abs_sub (R a) J;rw[И d] at u;obtain ⟨e1,W⟩:=abs_le.mp v;have e3:=Г e1 b;have e4:=Г W b;have e5:=Х h x M.le ב;have e6 : |H*R a| ≤ H*(J+(J+A_z)/2^32):=by rw[в,И b];exact Г ד b have hW:=С b d;have hHJ:H*J<=2^128*2^149:=Х h z d ב;obtain ⟨o,i⟩:=qCf _ a Q m (by rw[N,И b];linarith) ד;rw[N] at i;have e7:=abs_sub_abs_le_abs_sub (R (dv hh*a)) (H*R a);obtain ⟨w,f⟩:=qBl (dv bw) q;rw[j] at f;obtain ⟨y,k⟩:=qBf _ _ o w ד (by rw[f,abs_abs];linarith);rw[f] at k;have e8:=Ъ (R (dv hh*a)) |wv bw|;rw[abs_abs] at e8;obtain ⟨e9,Z⟩:=abs_le.mp i;obtain ⟨e11,V⟩:=abs_le.mp k;have hkz:=Б (show (ts.length:ℝ)<=2^20 by linarith) M.le;have O:=Л (wv bw);obtain ⟨Y,hY⟩:∃ Y,Y=H*J + |wv bw| + A_z:=⟨_,rfl⟩;have c:A_sa (ts.map g ++ [dv bw])+A_z<=(1+1/2^22)*Y :=ד;have I:(ts.map λt=>|p t|).sum+A_z<=Y :=ד;have l : |R (dv hh*a+(dv bw).abs)-Y| ≤ Y/2^30+1/2^250 :=abs_le.mpr ⟨by linarith,by linarith⟩;refine qEz A B C D E F G y ד c ?_ l he;have h3:=Г c (show 0<=(ts.length:ℝ)+1 by linarith);have h4:=Б (show (ts.length:ℝ)<=2^20 by linarith) (ד:0<=Y);linarith lemma qBm {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)|) (r:e=nfOf n*(dv hh*ts.foldl (λ(c:Float) t=>c+(dv (a t)).abs) 0+(dv bw).abs)) : 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[(qAm _ (i t d)).2] have h:=(qC9 ts _ _ λt d=>qAo _ (i t d)).trans (Б f ב);rw[<-k] at h;have m:=qCc obtain ⟨j,b⟩:=qB7 _ (λq v=>by obtain ⟨t,d,rfl⟩:=List.mem_map.mp v exact (qAm _ (i t d)).1) (by simp;omega) ד rw[List.length_map] at b;have g:=qCe (ts.map λt=>dv (a t));have l:=qAr;refine qDi A B C D E F G u w ?_ j g (h.trans ב) ?_ (r.trans (by simp[A_fsum,List.foldl_map]));. rw[k,<-List.sum_map_mul_left];exact List.sum_le_sum o . refine b.trans ?_;have:=Б f (add_nonneg g l.le);linarith end section variable (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) include n hn xw ww lemma qDm (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[qEv] at hs obtain ⟨hs,E⟩:=hs have a:=qFj hl2 hx hw have f:=qFu hl hl2 obtain ⟨h,i⟩:=qAm bw hb obtain ⟨-,m,y⟩:=qCr (dv bw) a rw[i] at m have b:=qCe (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw]) have k:=qC9 l _ _ λt u=>(a t u).2.2 have v:(l.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:l.length+1<=2^20) have o:(0:ℝ)<=l.length:=Ю _ have d:=qAr have D:=qAw have w:=qAo bw hb have z:=qCc have A:=qC4 l λt=>wv (xw t)*wv (ww t) have p:=k.trans (Б (show (l.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⟩:=qB7 _ (qHf a h) (by simp;omega) ד rw[qHh] at c have B:=С o b have x:=Б v (add_nonneg b d.le) obtain ⟨G,F⟩:=abs_le.mp c exact qEz hn f a hb (qBs hx hw hl hl2 hz) hs hbig j g ד ד (abs_le.mpr ⟨by linarith,by linarith⟩) (E.trans (by simp[A_fsum,List.foldl_map])) lemma qCy (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[qEv] at hs obtain ⟨hs,c⟩:=hs obtain ⟨-,-,d⟩:=qCs hg hp have b:∀ t∈З n,t< n:=λt a=>Р.mp a exact qBm hn ג (qFj b hw hx) hb ((qBs hx hw ػ 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))) (Л _)) c lemma qC8 (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⟩:=qCs hgv hpv exact qBm hn (qFu hl hl2) (qFj hl2 hx hw) hb (qBs hx hw hl hl2 λt i z=>.inl (hz t i z)) hs hbig hgv hpv (λt a=>hx t (hl2 t a)) (λt a=>by rw[в,mul_comm] exact Б (c _ (hw t (hl2 t a)) (hmv t (hl2 t a))) (Л _)) 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 qF9 (s t:Bool):A_r (s ^^ t)=A_r s*A_r t:=by cases s<;>cases t<;>simp[A_r] lemma qF8 (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 qD4 : d.toReal=A_r d.negative*((d.significand:ℝ)*2^d.exponent):=by obtain ⟨n,m,e⟩:=d;cases n<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,A_r] lemma A_s:d.toReal=0 ↔ d.significand=0:=by have h:(2:ℝ)^d.exponent≠0:=zpow_ne_zero _ Ч;rw[qD4];cases d.negative<;>simp[A_r,h] lemma qE9 (h:d.significand≠0) : 0<(d.significand:ℝ)*2^d.exponent :=mul_pos (Nat.cast_pos.mpr (Nat.pos_of_ne_zero h)) (х two_pos _) end lemma qIe (a b:Float) (h:A_M a=A_M b):a=b:=by have:=Model.ofFloatModel_injective h;cases a;cases b;simp_all lemma qDk (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 ((qAh f).trans h);exact ⟨d,a,by rw[A_R,Model.toReal_eq,a]⟩ lemma qCq {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[qB0 a b e c,Model.Proof.mul_eq_spec,Model.Spec.mul,f,d] simp lemma qHe {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:=qCq m n k l;simp only[g,↓reduceIte] at d;rw[d,Model.toDyadic?_zero] at e;cases e;show Model.signBit _=_;cases x.negative<;>cases y.negative<;>simp[Model.zero] . obtain ⟨f,i⟩:=Nat.mul_ne_zero_iff.mp g;have j:z.significand≠0:=λh=>by rw[(A_s z).2 h] at c;exact mul_ne_zero (mt (A_s x).1 f) (mt (A_s y).1 i) c.symm rw[qD4 z,qD4 x,qD4 y,mul_mul_mul_comm,<-qF9] at c;exact qF8 _ _ _ _ (qE9 z j) (mul_pos (qE9 x f) (qE9 y i)) c lemma qGg (v m p: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⟩:=qDk v f;obtain ⟨b,y,k⟩:=qDk p h;obtain ⟨c,w,A⟩:=qDk m i;obtain ⟨d,t,B⟩:=qDk _ u;obtain ⟨e,l,x⟩:=qDk _ s;have D:=qHe o y t f h (by rw[B,n,k,q]);have E:=qHe w y l i h (by rw[x,A,k,r]);have C:(d.negative ^^ c.negative)=(a.negative ^^ e.negative):=by rw[D,E];cases a.negative<;>cases b.negative<;>cases c.negative<;>rfl have j:d.toReal*c.toReal=a.toReal*e.toReal:=by rw[B,A,n,x,q,r];ring have g:d.significand*c.significand=0 ↔ a.significand*e.significand=0:=by simp only[Nat.mul_eq_zero,<-A_s,<-mul_eq_zero,j] apply qIe;rw[qCq t w u i,qCq o l f s];by_cases z:d.significand*c.significand=0;. simp only[z,g.1 z,↓reduceIte,C] . simp only[z,mt g.2 z,↓reduceIte];refine ExactRuntime.roundDyadic_congr _ qAj _ _ z (mt g.2 z) (Rat.cast_injective (α:=ℝ) ?_);rw[и,и,Model.Dyadic.toReal_mul,Model.Dyadic.toReal_mul,j] lemma qFw (x w:UInt32) (hx:good x=true) : dvB (dv x) w=dv x*dv w:=by obtain ⟨e,o⟩:=qAm x hx;obtain ⟨p,f,j,q,g,-⟩:=qD1 w;obtain ⟨d,a,-,-,l,b⟩:=qD1 x;obtain ⟨m,c,k⟩:=qCw _ a;have r:=x.toNat_lt;have h:R (dv x)=(((2^23+x.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⟩:=qCm (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 (qAo x hx).trans qAn) h show dv x*pw.uget _ _*_=dv x*(_*pw.uget _ _);rw[p];exact qGg _ _ _ e j (qDa _ f).1 n q i g end section section open Float.Model Float.Model.UnpackedFloat lemma qFq (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma B_z (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,Э Ч,й] lemma qDg (m L: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 qEa (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 qEf (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 qBb (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 qBn (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,qDg M 23 hM1 hM2] rw[show (E+↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat=(E+150).toNat by simp only[Format.exponentBias];norm_num;ring_nf] have c:¬ (2^Format.binary32.exponentBits<=(E+150).toNat+1):=by show ¬ (2^8<=(E+150).toNat+1);omega have a:(23+1=Format.binary32.mantissaBits):=rfl have b:s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl simp only[c,ite_false,a,ite_true,packComponents,BitVec.toNat_append,b,BitVec.toNat_ofNat] rw[М (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (ر _ (by positivity):M%2^23<2^23)] split_ifs<;>simp[Nat.shiftLeft_eq] omega lemma qE4 (neg: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[qBb,<-ExactRuntime.round_scale _ _ m e k hm] have p:m*2^k≠0:=Nat.mul_ne_zero hm (by positivity) rw[s,i] at hhi rw[s,i] at hlo1 hlo2 have H:0<2^g:=by positivity generalize m*2^k=M at * generalize e-k=q at * generalize (2:ℝ)^q=b at * have f:∀ A B:ℕ,(A:ℝ)*b< B*b->A< B :=λA B h=>by exact_mod_cast lt_of_mul_lt_mul_right h u.le push_cast at hhi hlo1 hlo2 have G:M<4*(P*2^g)+2*2^g:=f _ _ (by push_cast;linarith) have t:P≠2^23->4*(P*2^g)< M+2*2^g :=λh=>f _ _ (by push_cast;linarith [hlo1 h]) have y:P=2^23->33554431*2^g< M :=λh=>f _ _ (by push_cast;linarith [hlo2 h]) have C:P*2^g<=(2^24-1)*2^g:=Ь _ א have r:2*(P*2^(g+1))=4*(P*2^g):=by rw[Nat.pow_succ];ring have n:2^(g+1)=2*2^g:=by rw[Nat.pow_succ];ring have w:UnpackedFloat.round Format.binary32 (Model.modelSign neg) M q =.finite (Model.modelSign neg) P E l:=by rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ p] have o:∀ (L:ℕ) (T:ℤ),2^L*2^g<=M->M<2^L*2^g*2 → max (((L+g:ℕ):ℤ)+1+q-24) (-149)=T → Format.binary32.targetExponent (totalExponent M q)=T:=λL T a c d=>by rw[<-Nat.pow_add] at a c simp only[totalExponent,qDg M _ a c,qFq];exact d by_cases v:2^25*2^g<=M . rw[o 25 E v א א,qEa M q E (g+1) P א l ?_ (by rw[r,n];omega)];. exact qEf _ P E hP1 hP2 hE l . rw[r,n];by_cases j:P=2^23;. subst j;omega . have:=t j;omega have j:P=2^23:=by by_contra hne have:=t hne have:(2^23+1)*2^g<=P*2^g:=Ь _ א omega have D:=y j subst j by_cases x:-148<=E . rw[o 24 (E-1) א א א,qEa M q (E-1) g (2^24) א ב א א];have:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (Model.modelSign neg) (E-1+23) (by show -126<=E-1+23;omega);rw[show (FloatFormat.binary32.fracWidth:ℤ)=23 from rfl] at this;rwa [(א:E-1+23-23=E-1),(א:E-1+23+1-23=E)] at this . rw[o 24 E א א א,qEa M q E (g+1) (2^23) א ב (by rw[n];omega) (by rw[n];omega)];exact qEf _ _ E ב ב hE l rw[w] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBn _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qFs (f: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 :=(qFs f).trans (by cases s<;>simp at h ⊢<;>omega) have c:=Model.ofFields_signBit_expField_fracField (toModel (ofFloat f)) rw[g,e,i,show f.toBits.toNat/2^52%2^11=E by split at h<;>omega,show f.toBits.toNat%2^52=F by split at h<;>omega] at c have b:E<2^11 :=א have a:Ы (Model.ofFields B_D s E F)=true:=by rw[Model.isFinite_ofFields B_D _ _ _ b hF] show (E != 2047)=true simp;omega have d:=Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ b hF a rw[c,if_neg (א:¬ E=0)] at d rw[c] at a refine ⟨(qAh f).symm.trans a,?_⟩ rw[R,Model.toReal_eq,d] simp only[show Model.pow2 B_D.fracWidth=2^52 from rfl, show Int.ofNat E-Int.ofNat B_D.exponentBias-Int.ofNat B_D.fracWidth=(E:ℤ)-1075 by show (E:ℤ)-1023-52=_;omega] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def B_near (A: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 qHw (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 qEs (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⟩:=ل.mp hc;have hxy:x=y:=by simpa using hxy subst hxy;have hc:ex-(897:UInt64)<(254:UInt64):=by simpa using hc rw[ф,UInt64.toNat_sub] 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,qHw x hx35 h1 h2,ha,(B_x B x hy h1 h2).2⟩ . exact absurd rfl h lemma qB6 (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 := qEs (s+e).toBits (s-e).toBits h set_option maxRecDepth 8192 in lemma qA4 (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 qBw (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⟩:=qB6 s e h;obtain ⟨g,v,i,-,-,l⟩:=qA4 _ S f c b d a j;refine ⟨g,(qA4 _ S f c b d a k).1,?_⟩ exact (show (2:ℝ)^(-127:ℤ)<=(2^53-2^28)*2^(-179:ℤ) by norm_num).trans ((Г (Ц one_le_two א) ב).trans (l.le.trans (by rw[i];cases S<;>simp[le_abs_self]))) lemma qAx (w: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 qEl (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 qEc (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[qEl] refine ذ rfl λj m=>?_ have h:=غ.mp m rw[Т,qAb n fa j h,qAb n fb j h] have l:dsum a b 0 n≠0:=λh=>hn (by rw[<-c,h];simp) have Z:=qEg a b (by rw[e,g]) λk f=>by rw[e] at f rw[qAb n fa k f,qAb n fb k f];exact hf k f rw[e] at Z refine ⟨ofIntDyadic (dsum a b 0 n) (-298),by rw[toRat_ofIntDyadic,c],?_⟩ show dotSpec a b=_ rw[<-Z];simp[finishWord,l] lemma qIg:Д oneW=1:=by norm_num [rationalWord,decodeD,show expOf oneW=127 by decide,show fracOf oneW=0 by decide,show signOf oneW=false by decide,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand] lemma qHv (n: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,qIg,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qAx bw (qAe bw hb)] congr 1 refine ذ rfl λt b=>?_ have a:=غ.mp b simp only[a,ite_true] rw[qAx _ (qAe _ (hx t a)),qAx _ (qAe _ (hw t a)),Rat.cast_mul] section variable (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е) include s e V hlo hhi h lemma qB8 : ∃ (S: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'⟩:=qB6 s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qA4 _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qA4 _ S ex fr h1 h2 h3 hb' rw[hva] at hhi rw[hvb] at hlo refine ⟨S,ex,fr,if S then -V else V,h1,h2,h3,hz,?_,?_,?_,?_⟩ all_goals cases S all_goals simp only[Bool.false_eq_true,ite_false,ite_true,neg_neg] at hhi hlo ⊢ . exact н hhi hia . linarith . exact λh=>(lob1 h).trans hlo . exact λh=>by linarith [loa1 h] . exact lob.trans hlo . linarith lemma qG5:V≠0:=by obtain ⟨S,ex,fr,W,-,-,-,-,hV,-,-,hW3⟩:=qB8 s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=х ב _;have hA:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076):=С ב hb0.le;intro h0;rw[h0] at hV;cases S;. simp only[Bool.false_eq_true,ite_false] at hV;linarith . simp only[ite_true] at hV;linarith end lemma qEo (d:FloatLib.Numerics.Dyadic) (S: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:=qE4 neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hword,e1,e2] lemma qIq (s e: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⟩:=qB8 s e _ hlo hhi h;rw[hz];exact (qEo d S ex fr h1 h2 h3 W hV hW1 hW2 hW3).symm lemma qCn (n: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:=qHv n xw ww bw hx hw hb obtain ⟨d,hd,hw'⟩:=qEc (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (λj _=>by by_cases hj:j< n . simp only[hj,ite_true];exact ⟨qAe _ (hx j hj),qAe _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,qAe _ hb⟩) λh0=>qG5 s e _ hlo hhi h (by rw[<-hsum,h0,Rat.cast_zero]) have q:(d.toRat:ℝ)=exactS n xw ww bw:=by rw[hd,hsum] rw[hw'] exact qIq s e d (by rw[q];exact hlo) (by rw[q];exact hhi) h end section lemma qDn (n: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⟩:=qBw s e h obtain ⟨hlo,hhi⟩:=qDm n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCn n hn xw ww bw hx hw hb s e hlo hhi h end section lemma qDl (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⟩:=qBw s e h obtain ⟨hlo,hhi⟩:=qCy n hn xw ww bw hh hx hw hb hg hm hp s e hs hbig exact qCn n hn xw ww bw hx hw hb s e hlo hhi h end section 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 qBa (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 qBe {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size :=qAd _ h lemma qAg (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:=qBd a i h;rw[Я a i.toNat h2];rfl . rename_i h;have h':¬ i.toNat< a.size:=by intro h2;apply h;rw[А,qBe a hs];exact h2 rw[ـ a i.toNat h'];rfl lemma qBh (a:Array UInt32) (i:USize) (v:UInt32):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma qFo {α: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 qBu (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[А,qBe a hs];exact hi unfold wrW;rw[П h];exact qFo 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 qAk {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[qBh,h1],λq hq hS=>?_⟩;rw[qBu _ _ _ _ א א,hu];by_cases e:q=p;. rw[с e,e,hv] . rw[Ф e];exact h2 q hq (hS.resolve_left e) lemma qA5 {S S':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 qBv {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 qEn (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 qHu (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 qH2 (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 qB2 (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 qCx {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 qFa (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 qHj (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,و _,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⟩:=qHu _ _ _ h1 obtain ⟨h9,h10⟩:=qH2 _ _ _ h2 obtain ⟨h11,h12⟩:=qFa _ _ _ h3 have hg:good (A.uget i hb)=true:=qEn _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qB2 _ hg . exact g0 . by_cases e2:k=i.toNat;. subst e2;rw[Я A i.toNat hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qBi (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:=qBe A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qHj 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,qCx h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qCv (w:UInt32):w &&& В<=В:=by rw[Ж,п];exact Nat.and_le_right lemma qF6 (a b:UInt32):a<=a ||| b∧b<=a ||| b:=by rw[Ж,Ж,UInt32.toNat_or];exact ⟨Nat.left_le_or,Nat.right_le_or⟩ lemma qBt (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 qFe (a b:UInt32):a &&& b<=a∧a &&& b<=b:=by rw[Ж,Ж,п];exact ⟨Nat.and_le_left,Nat.and_le_right⟩ lemma qGi (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 qF3 (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 ⟨و _,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⟩:=qF6 o _ obtain ⟨h8,h9⟩:=qFe s ((A.uget i hb &&& В)-1) have hv:=qGi _ (UInt32.le_trans h3 h9) have hl:=UInt32.le_trans h7 h1 refine ⟨UInt32.le_trans h6 h1,h2,UInt32.le_trans h3 h8,λg0=>h4 ?_,λk hk1 hk2=>?_⟩ . rcases hv with hv | hv . rcases g0 with g0 | g0;. left;rw[g0,hv];rfl . right;exact UInt32.le_trans g0 h6 . right;exact UInt32.le_trans hv h7 . by_cases e2:k=i.toNat;. subst e2;rw[Я A i.toNat hb];exact ⟨qBt _ hv (UInt32.lt_of_le_of_lt hl h2),hl⟩ . exact h5 k (by rw[usz i n hi'];omega) hk2 lemma qB1 (A: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:=qBe A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,h2,-,h4,h5⟩ :=qF3 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:=qCx 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 qBt _ (h4 (Or.inl rfl)) h2 exact ⟨hg,hp,λk hk=>(h5 k א א).1,λk hk=>(h5 k א א).2⟩ lemma qA8 (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;by_cases c:(r == 1)=true . rw[if_pos c];split;. exact qBi A hs . exact qB1 A hs . rw[if_neg c];exact qBi A hs lemma qGd (r:Nat) (A:Array UInt32) (h8:r≠8) (h1:r≠1) (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 simp[h8,h1])] rw[e] at h ⊢;exact qA8 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 qEh {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:=qB2 _ 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=>?_,qCv _⟩;by_cases e2:k=t;. rw[e2];exact و _ . 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 qCj {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[Nat.add_mul,ه] at h;omega lemma qBc {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[Nat.add_mul,ه] 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 qCj 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 qCj hj ht) lemma qHx (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[qAi] . rename_i h;unfold C_out;congr 1;exact qDn I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb];exact hj)) _ ػ (λt ht=>Р.mp ht) (λt ht h3=>absurd (Р.mpr ht) h3) _ _ ⟨by rw[h1],by rw[h2]⟩ (by simpa using h) end lemma qE2 {X W: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,ك] dsimp only rw[qAg _ _ hX,qAg _ _ hW,qAf _ _ א,qAf _ _ א] rw[ih (t+1) _ _ (by rw[usz t n ht];omega),usz t n ht] section variable (G:C_G R I O X W b) (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) include G Y i j hi hj lemma qDw {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 qBc hi (ي _) have hW1:j*I+I<=W.size:=by rw[<-G.hW];exact qBc hj (ي _) have sX:=G.sX have sW:=G.sW have e1:(i*I).toUSize.toNat=i*I:=qAd _ א have e2:(j*I).toUSize.toNat=j*I:=qAd _ א have e3:I.toUSize.toNat=I:=qAd _ hin unfold fin8 dsimp only split . have e:=qE2 (xo:=(i*I).toUSize) (wo:=(j*I).toUSize) (n:=I.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,<-ن] at e;rw[qHx 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 qDu {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 qDw G Y hi hj λh=>by obtain ⟨-,-,k⟩:=qBw _ _ h obtain ⟨c1,c2⟩:=qC8 I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! hv (G.gx hi) (G.gw hj) gb g1 g4 g2 L hl hl2 hz _ _ ⟨by rw[hs],rfl⟩ k exact qCn I G.ni _ _ _ (G.gx hi) (G.gw hj) gb _ _ c1 c2 h end end lemma qC5 (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 qC3 (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 qD6 (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 qEt (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 qFo 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 qBj:(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& В != 0) (З t);simpa [C_keep] using h lemma qDp (h:xw t &&& В=0) : C_keep xw (t+1)=C_keep xw t:=by simp[C_keep,List.range_succ,List.filter_append,h] lemma qD5 (h:¬ xw t &&& В=0) : C_keep xw (t+1)=C_keep xw t ++ [t]:=by simp[C_keep,List.range_succ,List.filter_append,h] end def C_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 qG6 {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,qDp xw _ hz];exact ⟨h1,h2,h3,h4,h5⟩ . rw[Ф (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qBj xw t.toNat;have hc:c.toNat< t.toNat+1 :=א;rw[hu,C_Dec,qD5 xw _ hz];have hcn:c.toNat< n.toNat :=א;refine ⟨?_,by rw[qBh];exact h2,by rw[qD6];exact h3,λk p hk2=>?_,by rw[List.foldl_append,<-h5];rfl⟩;. rw[qAf _ _ (by rw[ض];omega),ض,h1];simp . rw[qBu _ _ _ _ א א,qEt _ _ _ _ א א];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=c.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 qAu (a:FloatArray) (v:Float):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qDy (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),qAu];omega lemma qH6 {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[ص,qC3];exact h2 have hm:(ix.uget t hit).toUSize< m:=by rw[А,e0];exact h1 have e1:x.uget t א=xf p:=by rw[qC5,h3] have e2:∀ pf,W.uget (w0+(ix.uget t hit).toUSize) pf=ww p:=by intro pf rw[qC3,qAf _ _ (by rw[e0];omega),e0,hww p h1] rw[go1,П ht] dsimp only rw[П hm] rw[e1,e2,ih (t+1) _ (by rw[usz t n ht];omega),ك] intro k q hq have:=H (k+1) q (by simpa using hq) rw[usz t n ht,show t.toNat+1+k=t.toNat+(k+1) by omega] exact this def C_Dw (wd:FloatArray) (W:Array Word):Prop := wd.size=W.size->∀ q,q< W.size->wd.data[q]! = dv W[q]! 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 qH0 (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[qC5,e] exact qFw w u g have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] lemma qHq {wd:FloatArray} (hd:wd.size=W.size) (hv:C_Dw wd W) (k:Nat) : ∀ (t:USize) (a b c d:Float),t.toNat+k=n.toNat → go4D x wd ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 hd a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4D go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א unfold go4D go1 simp only[dite_eq_left ht] have B:∀ (u:USize) p1,dv (W.uget u p1)=wd.uget u (hd ▸ p1):=λu p1=>by rw[qC5,hv hd _ p1,qC3] have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] end def C_sp (I O: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 qAp {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) := qA5 (qAk hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qFk {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 qEq {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,-⟩:=qFk hu א exact qAp hN (qAp hN (qAp hN (qAp hN h hu א h0) a1 א h1) a2 א h2) a3 א h3 lemma qEy {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:=qG6 (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[qAg _ _ hX,qAf _ _ (by rw[eo];omega),eo]) (λt ht=>hg t (by rwa [ek] at ht)) n 0 ix buf 0 0 (by rw[C_z,ek,Т]) ⟨by simp[C_keep],by rw[ek];exact h1,by rw[ek];exact h2,λk p hk=>by simp[C_keep] at hk,by simp[C_keep]⟩ rw[ek] at D exact D lemma qDs (xw: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 := ⟨ػ.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:=qBc hj (ي _) have h2:=G.hW have h3:=G.sW have e:(u*I.toUSize).toNat=j*I:=by rw[qBa _ _ (by rw[hu,G.nm];omega),hu,G.nm] exact ⟨e,by rw[e,G.nm];omega⟩ lemma qB9 (Y: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) {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) : fin8 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=C_out I Y X W b i j:=by have e:=qH6 hx hi' hs h0 (λp=>dv X[i*I+p]!) (λp=>W[j*I+p]!) (λp _=>by rw[(G.wj hu hj).1]) L 0 0 (by rw[C_z,Т];exact Rw.len.symm) (λk p hk=>by have h1:=Rw.rd k p hk rw[C_z,Т,G.nm] exact ⟨Rw.lt p (List.mem_of_getElem? hk),h1⟩) rw[qAg _ _ G.sb,hu,Rw.ok] exact qDu G Y hi hj Hv.1 Hv.2.1 (λt ht=>Hv.2.2.2 _ (by rw[<-G.hW];exact qCj hj ht)) L Rw.nd Rw.lt Rw.hz e lemma qD0 (Y: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) {base:USize} (hb:base.toNat=i*O) (Hd:C_Dw wd W) (d:Nat):∀ (j:Nat) (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 out):=by induction d using Nat.strong_induction_on with | _ d ih => intro j out he H have hN:=G.sO have sW:W.size<=4294967295:=by have:=G.sW;omega have hbs:=G.sb have hob:=G.hb 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 hsp:∀ k,j+k< O->C_out I Y X W b i (j+k) =C_sp I O Y X W b (i*O+j+k):=λk hk=>by rw[Գ];exact (qCi I O Y X W b hk).symm by_cases h4:j+4<=O . have hP:=qCj hi (א:j+3< O);have e0:j.toUSize.toNat=j:=qAd _ א;obtain ⟨e1,e2,e3,-⟩:=qFk e0 א;have g0:=(G.wj e0 א).2;have g1:=(G.wj e1 א).2;have g2:=(G.wj e2 א).2;have g3:=(G.wj e3 א).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAf _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qB9 G Y hi (א:j< O) Hv Rw e0 h1c h2c G.sW g0;have o1:=qB9 G Y hi (א:j+1< O) Hv Rw e1 h1c h2c G.sW g1;have o2:=qB9 G Y hi (א:j+2< O) Hv Rw e2 h1c h2c G.sW g2;have o3:=qB9 G Y hi (א:j+3< O) Hv Rw e3 h1c h2c G.sW g3;rw[cols1,П h4];dsimp only;rw[П ⟨h1c,h2c,sW,g0,g1,g2,g3⟩];split;rename_i hd;rw[qHq _ _ _ _ _ _ _ hd Hd c.toNat 0 _ _ _ _ (Т _)];swap rw[qH0 _ _ _ _ _ _ _ (λj hj=>by rw[Rw.len] at hj;have hp:=List.getElem?_eq_getElem hj exact ⟨_,G.gx hi _ (Rw.lt _ (List.mem_of_getElem? hp)),(Rw.rd j _ hp).2⟩) c.toNat 0 _ _ _ _ (Т _)] all_goals dsimp only;rw[o0,o1,o2,o3];refine ih (d-4) א (j+4) _ א ?_;rw[show i*O+(j+4)=i*O+j+4 by omega];exact qEq hN H a0 א (hsp 0 א) (hsp 1 א) (hsp 2 א) (hsp 3 א) . by_cases h1:j< O . have hP:=qCj hi h1;have e0:j.toUSize.toNat=j:=qAd _ א;have g0:=(G.wj e0 h1).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAf _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qB9 G Y hi h1 Hv Rw e0 h1c h2c G.sW g0;rw[cols1,Ш h4,П h1];dsimp only;rw[П ⟨h1c,h2c,sW,g0⟩];rw[o0];exact ih (d-1) א (j+1) _ א (qAp hN H a0 hP (hsp 0 א)) . rw[cols1,Ш h4,Ш h1];have e:j=O :=א;rw[e] at H;exact H lemma qGf (Y:Bool) {hv:Word} (Hv:C_Hv W hv) {wd:FloatArray} (Hd:C_Dw wd W) (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 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:=qBc hi (ي _) have hbs:(i*O).toUSize.toNat=i*O:=by have h3:i*O+O<=R*O:=qBc hi (ي _) exact qAd _ א obtain ⟨d1,d2,d3,d4,d5⟩:=qEy sX א (G.gx hi) h1 h2 obtain ⟨k1,k2,k3⟩:=qDs (λp=>X[i*I+p]!) I have Rw:=C_Row.mk d1 (qBj _ _) d2 d3 k1 k2 k3 d4 d5 rw[rows1,П hi] dsimp only refine ih (i+1) _ _ _ א d2 d3 ?_ have h5:=qD0 G Y hi Hv Rw hbs Hd O 0 out א (by rw[ю];exact H) rw[show (i+1)*O=i*O+O by rw[Nat.add_mul,ه]] exact h5 end end lemma qFx (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] lemma qIy (l:List UInt32) (a:FloatArray) : (l.foldl (λa w=>a.push (dv w)) a).data=a.data ++ (l.map dv).toArray:=by induction l generalizing a with | nil=>simp | cons w l ih => rw[ك,ih];simp[FloatArray.push] section variable {rows inW outW:Nat} {X W b:Array Word} lemma qHm (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 qDw G relu hi hj λh => qDl 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 qFg (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[qFx,qAu] interval_cases r<;>repeat (first | rw[с א] | rw[Ф א]) lemma qFl (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[qFx,qAu] 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 qB3 {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[qBa _ _ (by rw[hc];exact h0),hc];exact h),qBa _ _ (by rw[hc];exact h0),hc] lemma qCp {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[qAg _ _ hs,qB3 hc h] lemma qGq {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[qAg _ _ hs,ف,Т] have r1:rdW X (n+t)=X[1*n.toNat+t.toNat]!:=by rw[qAg _ _ hs,qAf _ _ א,ه] rw[dec8,П ht] dsimp only rw[r0,r1,qCp hs l2 א,qCp hs l3 א,qCp hs l4 א,qCp hs l5 א,qCp hs l6 א,qCp hs l7 א] obtain ⟨H1,H2,H3⟩:=H 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[qAu,H1,hu];omega . intro k r hk hr;rw[hu] at hk;by_cases e:k=t.toNat;. subst e;have h9:=qFg 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[qFl _ _ _ _ _ _ _ _ _ _ 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 qEh 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 qBq {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[qC5,qAf _ _ (by rw[uk r א];omega),uk r א,hi,hxf r _ hr ht] lemma qIf {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[qC3,qAf _ _ א,hww _ htn] rw[go8,П ht] dsimp only rw[e,qBq hxf hi htn hsx hx 0 א,qBq hxf hi htn hsx hx 1 א,qBq hxf hi htn hsx hx 2 א,qBq hxf hi htn hsx hx 3 א,qBq hxf hi htn hsx hx 4 א,qBq hxf hi htn hsx hx 5 א,qBq hxf hi htn hsx hx 6 א,qBq hxf hi htn hsx hx 7 א] rw[ih (t+1) _ _ _ _ _ _ _ _ _ _ _ _ _ _ א r hr,hu,List.range'_succ] unfold C_cf rw[ك] interval_cases r<;>rfl lemma qGn {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 qF4 {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 qF7 (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 :=א have e0:j.toUSize.toNat=j:=qAd _ א have eo:outW.toUSize.toNat=outW:=qAd _ א 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:=qIf 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,<-ن] at key 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 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 qHm 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 א have o1:fin8 inW relu X W b 1 j R.s1 (nfOf inW) R.t d.h1 b[j]! inW.toUSize=_ :=O 1 א have o2:fin8 inW relu X W b 2 j R.s2 (nfOf inW) R.t d.h2 b[j]! inW.toUSize=_ :=O 2 א have o3:fin8 inW relu X W b 3 j R.s3 (nfOf inW) R.t d.h3 b[j]! inW.toUSize=_ :=O 3 א have o4:fin8 inW relu X W b 4 j R.s4 (nfOf inW) R.t d.h4 b[j]! inW.toUSize=_ :=O 4 א have o5:fin8 inW relu X W b 5 j R.s5 (nfOf inW) R.t d.h5 b[j]! inW.toUSize=_ :=O 5 א have o6:fin8 inW relu X W b 6 j R.s6 (nfOf inW) R.t d.h6 b[j]! inW.toUSize=_ :=O 6 א have o7:fin8 inW relu X W b 7 j R.s7 (nfOf inW) R.t d.h7 b[j]! inW.toUSize=_ :=O 7 א rw[qAg _ _ hbs,e0,o0,o1,o2,o3,o4,o5,o6,o7] have u0:j.toUSize.toNat=0*outW+j:=by rw[e0];omega have u1:(outW.toUSize+j.toUSize).toNat=1*outW+j:=by rw[qAf _ _ (by rw[eo,e0];omega),eo,e0];omega have a2:=qB3 (n:=outW.toUSize) (t:=j.toUSize) l2 (by rw[eo,e0];omega) have a3:=qB3 (n:=outW.toUSize) (t:=j.toUSize) l3 (by rw[eo,e0];omega) have a4:=qB3 (n:=outW.toUSize) (t:=j.toUSize) l4 (by rw[eo,e0];omega) have a5:=qB3 (n:=outW.toUSize) (t:=j.toUSize) l5 (by rw[eo,e0];omega) have a6:=qB3 (n:=outW.toUSize) (t:=j.toUSize) l6 (by rw[eo,e0];omega) have a7:=qB3 (n:=outW.toUSize) (t:=j.toUSize) l7 (by rw[eo,e0];omega) rw[eo,e0] at a2 a3 a4 a5 a6 a7 have I0:=qAk h8 H u0 א rfl have I1:=qAk h8 I0 u1 א rfl have I2:=qAk h8 I1 a2 א rfl have I3:=qAk h8 I2 a3 א rfl have I4:=qAk h8 I3 a4 א rfl have I5:=qAk h8 I4 a5 א rfl have I6:=qAk h8 I5 a6 א rfl have I7:=qAk h8 I6 a7 א rfl exact ih (j+1) _ _ _ _ א (qA5 I7 (λq hq hq'=>qF4 hj hq hq')) def C_ck (c:C8):UInt32:=if 0x7FFFFF<=c.q∧c.p<У then c.p else Е lemma qE8 {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:=qBe 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[qBa _ _ (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 qBc hj (ي _) 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:=qGn 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 section open Float.Model Float.Model.UnpackedFloat local notation:max x:max "ᶠ"=>Float.isFinite x=true lemma qD2 (s:Sign):(Float.Model.pack (UnpackedFloat.infinity s)).unpack.isFinite=false:=by cases s<;>decide +kernel lemma qFh:(Float.Model.pack UnpackedFloat.notANumber).unpack.isFinite=false:=by decide +kernel lemma qC1 (x y:Float) (h:(x*y)ᶠ):xᶠ∧yᶠ:=by change (Float.Model.mul x.toModel y.toModel).unpack.isFinite=true at h;change x.toModel.unpack.isFinite=true∧y.toModel.unpack.isFinite=true;rw[Float.Model.mul] at h;cases a:x.toModel.unpack<;>cases b:y.toModel.unpack <;>simp only[a,b,UnpackedFloat.mul,qD2,qFh,Bool.false_eq_true] at h<;>exact ⟨rfl,rfl⟩ lemma qC2 (x y:Float) (h:(x+y)ᶠ):xᶠ∧yᶠ:=by change (Float.Model.add x.toModel y.toModel).unpack.isFinite=true at h;change x.toModel.unpack.isFinite=true∧y.toModel.unpack.isFinite=true;rw[Float.Model.add] at h cases a:x.toModel.unpack<;>cases b:y.toModel.unpack <;>simp only[a,b,UnpackedFloat.add,qD2,qFh,Bool.false_eq_true] at h <;> first | exact ⟨rfl,rfl⟩ | (split at h<;>simp only[qD2,qFh,Bool.false_eq_true] at h) lemma qHz:(О 0x7FF8000000000000).isFinite=false:=by decide +kernel lemma qFz (i:USize) (h:i.toNat< pwN.size):pwN.uget i h=pwEntryN i.toNat:=by simp[FloatArray.uget,qDj] def N_ok (w:UInt32):Prop:=0x800000<=w &&& В∧w &&& В<У lemma qEx {w:UInt32} (h:N_ok w):good w=true :=qBt _ (Or.inr h.1) h.2 lemma N_w (w:UInt32) (h:(pwN.uget (w >>> (23:UInt32)).toUSize (qBx w))ᶠ) : pwN.uget (w >>> (23:UInt32)).toUSize (qBx w)=pw.uget (w >>> (23:UInt32)).toUSize (qCh w) ∧ N_ok w:=by have s:=qD8 w rw[qFz,ص,s] at h rw[qFz,qDc,ص,s] unfold pwEntryN at h ⊢ have e:=qAq w have g:=w.toNat_lt by_cases c:(w.toNat/2^23%256 == 0 || w.toNat/2^23%256 == 255)=true . rw[if_pos c,qHz] at h;exact absurd h (by decide) . rw[if_neg c];simp only[Bool.or_eq_true,beq_iff_eq,not_or] at c;refine ⟨rfl,?_,?_⟩;. rw[Ж,e,show (0x800000:UInt32).toNat=0x800000 from rfl];omega . rw[у,e,show (У:UInt32).toNat=У from rfl];omega lemma qIj {w:UInt32} (h:(dvN w)ᶠ):dvN w=dv w∧N_ok w:=by obtain ⟨e,o⟩:=N_w w (qC1 _ _ h).2;exact ⟨by unfold dvN dv;rw[e],o⟩ lemma qF1 {v:Float} {w:UInt32} (h:(dvNB v w)ᶠ):dvNB v w=dvB v w∧N_ok w:=by obtain ⟨e,o⟩:=N_w w (qC1 _ _ (qC1 _ _ h).1).2;exact ⟨by unfold dvNB dvB;rw[e],o⟩ def N_ff (f:Nat->Float) (L:List Nat) (a:Float):Float := L.foldl (λacc s=>acc+f s) a lemma qB4 (f:Nat->Float) (L:List Nat) : ∀ a,(N_ff f L a)ᶠ->aᶠ∧∀ s∈L,(f s)ᶠ:=by induction L with | nil=>exact λa h=>⟨h,λs hs=>by simp at hs⟩ | cons p L ih => intro a h;obtain ⟨h1,h2⟩:=ih _ h;obtain ⟨h3,h4⟩:=qC2 _ _ h1;refine ⟨h3,λs hs=>?_⟩;rcases List.mem_cons.mp hs with e | e;. rw[e];exact h4 . exact h2 s e lemma qC0 (a b:UInt32):a &&& В<=(a ||| b) &&& В ∧ b &&& В<=(a ||| b) &&& В:=by rw[Ж,Ж];simp only[UInt32.toNat_and,UInt32.toNat_or,Nat.and_or_distrib_right];exact ⟨Nat.left_le_or,Nat.right_le_or⟩ section variable {x:FloatArray} {W:Array UInt32} {w0 w1 w2 w3 n:USize} lemma qIa (hx:n.toNat<=x.size) (hs:W.size<2^32) (h0:w0.toNat+n.toNat<=W.size) (h1:w1.toNat+n.toNat<=W.size) (h2:w2.toNat+n.toNat<=W.size) (h3:w3.toNat+n.toNat<=W.size) (k:Nat) : ∀ (t:USize) (a b c d:Float) (p:UInt32),t.toNat+k=n.toNat → go4N x W w0 w1 w2 w3 n t hx hs h0 h1 h2 h3 a b c d p = ⟨N_ff (λs=>x.data[s]! * dvN W[w0.toNat+s]!) (List.range' t.toNat k) a,N_ff (λs=>dvNB x.data[s]! W[w1.toNat+s]!) (List.range' t.toNat k) b,N_ff (λs=>x.data[s]! * dvN W[w2.toNat+s]!) (List.range' t.toNat k) c,N_ff (λs=>dvNB x.data[s]! W[w3.toNat+s]!) (List.range' t.toNat k) d, (List.range' t.toNat k).foldl (λp s=>p ||| W[w0.toNat+s]! ||| W[w1.toNat+s]! ||| W[w2.toNat+s]! ||| W[w3.toNat+s]!) p⟩:=by induction k with | zero => intro t a b c d p he rw[go4N,Ш (by rw[А];omega)] rfl | succ k ih => intro t a b c d p he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א have U:=λ(u:USize) (hu:u.toNat+n.toNat<=W.size) pf =>(qC3 W (u+t) pf).trans (by rw[qAf _ _ א]) rw[go4N,П ht] dsimp only rw[qC5,U w0 h0,U w1 h1,U w2 h2,U w3 h3,ih (t+1) _ _ _ _ _ (by rw[usz t n ht];omega),usz t n ht,List.range'_succ] rfl end lemma qGt {a b c d:Float} (h:(a+b+c+d)ᶠ):aᶠ∧bᶠ∧cᶠ∧dᶠ:=by obtain ⟨h1,h4⟩:=qC2 _ _ h;obtain ⟨h2,h3⟩:=qC2 _ _ h1;obtain ⟨h0,h5⟩:=qC2 _ _ h2;exact ⟨h0,h5,h3,h4⟩ section variable {x:FloatArray} {W:Array UInt32} {u:Nat} {L:List Nat} include x W u L lemma qBy (h:(N_ff (λs=>x.data[s]! * dvN W[u+s]!) L 0)ᶠ) : (∀ s∈L,N_ok W[u+s]!)∧N_ff (λs=>x.data[s]! * dvN W[u+s]!) L 0 =L.foldl (λacc s=>acc+x.data[s]! * dv W[u+s]!) 0:=by have F:=(qB4 _ _ _ h).2 have G:=λs hs=>qIj (qC1 _ _ (F s hs)).2 exact ⟨λs hs=>(G s hs).2,List.foldl_ext _ _ _ λacc s hs=>by show acc+x.data[s]! * dvN W[u+s]! = _ rw[(G s hs).1]⟩ lemma qDh (h:(N_ff (λs=>dvNB x.data[s]! W[u+s]!) L 0)ᶠ):∀ s∈L,N_ok W[u+s]! :=λs hs=>(qF1 ((qB4 _ _ _ h).2 s hs)).2 lemma qDt (hx:∀ s∈L,∃ w,good w=true∧x.data[s]! = dv w) (h:(N_ff (λs=>dvNB x.data[s]! W[u+s]!) L 0)ᶠ) : N_ff (λs=>dvNB x.data[s]! W[u+s]!) L 0 =L.foldl (λacc s=>acc+x.data[s]! * dv W[u+s]!) 0:=by refine List.foldl_ext _ _ _ λacc s hs=>?_ obtain ⟨w,g,e⟩:=hx s hs obtain ⟨e1,o⟩:=qF1 ((qB4 _ _ _ h).2 s hs) show acc+dvNB x.data[s]! W[u+s]! = _ rw[e1,e,qFw w _ g] end lemma qHt (A B C D:Nat->UInt32) (L:List Nat):∀ p,let f:=L.foldl (λp s=>p ||| A s ||| B s ||| C s ||| D s) p p &&& В<=f &&& В∧∀ s∈L,A s &&& В<=f &&& В ∧ B s &&& В<=f &&& В∧C s &&& В<=f &&& В ∧ D s &&& В<=f &&& В:=by induction L with | nil=>exact λp=>⟨و _,λs hs=>by simp at hs⟩ | cons q L ih => intro p obtain ⟨h1,h2⟩:=ih (p ||| A q ||| B q ||| C q ||| D q) simp only[List.foldl_cons] generalize L.foldl (λp s=>p ||| A s ||| B s ||| C s ||| D s) (p ||| A q ||| B q ||| C q ||| D q)=f at h1 h2 ⊢ obtain ⟨a1,a2⟩:=qC0 (p ||| A q ||| B q ||| C q) (D q) obtain ⟨b1,b2⟩:=qC0 (p ||| A q ||| B q) (C q) obtain ⟨c1,c2⟩:=qC0 (p ||| A q) (B q) obtain ⟨d1,d2⟩:=qC0 p (A q) have t1:=UInt32.le_trans a1 h1 have t2:=UInt32.le_trans b1 t1 have t3:=UInt32.le_trans c1 t2 refine ⟨UInt32.le_trans d1 t3,λs hs=>?_⟩ rcases List.mem_cons.mp hs with e | e . rw[e];exact ⟨UInt32.le_trans d2 t3,UInt32.le_trans c2 t2,UInt32.le_trans b2 t1,UInt32.le_trans a2 h1⟩ . exact h2 s e section variable {I O:Nat} {X W b:Array Word} def N_q (I O:Nat) (Y:Bool) (X W b:Array UInt32) (x:FloatArray) (nf l:Float) (n:USize) (k:Nat) (out:Array UInt32):CN := if hg:n.toNat<=x.size∧W.size<=4294967295∧(k.toUSize*n).toNat+n.toNat<=W.size ∧ ((k.toUSize+1)*n).toNat+n.toNat<=W.size∧((k.toUSize+2)*n).toNat+n.toNat<=W.size ∧ ((k.toUSize+3)*n).toNat+n.toNat<=W.size then let r:=go4N x W (k.toUSize*n) ((k.toUSize+1)*n) ((k.toUSize+2)*n) ((k.toUSize+3)*n) n 0 hg.1 (qA9 hg.2.1) hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2 0 0 0 0 0 let p:=r.p &&& В if (r.a+r.b+r.c+r.d).isFinite && p<У then colsN I O Y X W b x nf l n (k+4) (wrW (wrW (wrW (wrW out k.toUSize (fin8 I Y X W b 0 k r.a nf l p (rdW b k.toUSize) n)) (k.toUSize+1) (fin8 I Y X W b 0 (k+1) r.b nf l p (rdW b (k.toUSize+1)) n)) (k.toUSize+2) (fin8 I Y X W b 0 (k+2) r.c nf l p (rdW b (k.toUSize+2)) n)) (k.toUSize+3) (fin8 I Y X W b 0 (k+3) r.d nf l p (rdW b (k.toUSize+3)) n)) else ⟨out,false⟩ else ⟨out,false⟩ lemma qHy {Y:Bool} {x:FloatArray} {nf l:Float} {n:USize} {j:Nat} {out:Array UInt32} (h:j< O∧4<=O):colsN I O Y X W b x nf l n j out =N_q I O Y X W b x nf l n (if j+4<=O then j else O-4) out:=by rw[colsN,П h] rfl set_option maxRecDepth 8000 in lemma qHo {Y:Bool} {x:FloatArray} {l:Float} (hW:O*I=W.size) (pi:0< I) (ni:I<2^20) (sW:W.size<2^32) (hxs:x.size=I) (d:Nat) : ∀ (j:Nat) (out:Array Word),O-j=d → (colsN I O Y X W b x (nfOf I) l I.toUSize j out).ok=true → (∀ j',j<=j'->j'< O->∀ s,s< I->N_ok W[j'*I+s]!) ∧ ∀ (G:C_G 1 I O X W b),(∀ s,s< I->x.data[s]! = dv X[0*I+s]!) → l=(З I).foldl (λ(a:Float) t=>a+(dv X[0*I+t]!).abs) 0 → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< j) out → C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< O) (colsN I O Y X W b x (nfOf I) l I.toUSize j out).out:=by have hnm:I.toUSize.toNat=I:=qAd _ א have hO:O<2^32:=by have:=ؽ O pi omega have W1:∀ {u:USize} {k:Nat},u.toNat=k->k< O->(u*I.toUSize).toNat=k*I:=λ{u k} hu hk=>by have h1:k*I<=O*I:=Ь _ hk.le rw[qBa _ _ (by rw[hu,hnm];omega),hu,hnm] induction d using Nat.strong_induction_on with | _ d ih => intro j out he ok by_cases h:j< O∧4<=O . rw[qHy h] at ok ⊢;have hk:(if j+4<=O then j else O-4)<=j∧(if j+4<=O then j else O-4)+4<=O ∧ j<(if j+4<=O then j else O-4)+4:=by split<;>omega generalize (if j+4<=O then j else O-4)=k at hk ok ⊢;obtain ⟨k1,k2,k3⟩:=hk;have e0:k.toUSize.toNat=k:=qAd _ א;obtain ⟨e1,e2,e3,-⟩:=qFk e0 א;have z0:=W1 e0 (א:k< O);have z1:=W1 e1 (א:k+1< O);have z2:=W1 e2 (א:k+2< O);have z3:=W1 e3 (א:k+3< O);have B:=λm (hm:m< O)=>(qBc (t:=I) hm (ي I)).trans_eq hW;have g0:(k.toUSize*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by rw[z0,hnm];exact B _ א have g1:((k.toUSize+1)*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by rw[z1,hnm];exact B _ א have g2:((k.toUSize+2)*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by rw[z2,hnm];exact B _ א have g3:((k.toUSize+3)*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by rw[z3,hnm];exact B _ א have hx0:I.toUSize.toNat<=x.size:=by rw[hnm,hxs] have sW':W.size<=4294967295 :=א;unfold N_q at ok ⊢;rw[П ⟨hx0,sW',g0,g1,g2,g3⟩] at ok ⊢;have R:=qIa hx0 sW g0 g1 g2 g3 I 0 0 0 0 0 0 (by rw[C_z,hnm,Т]);dsimp only at ok ⊢;rw[R] at ok ⊢;rw[C_z,z0,z1,z2,z3,<-ن] at ok ⊢;dsimp only at ok ⊢ by_cases c:((N_ff (λs=>x.data[s]! * dvN W[k*I+s]!) (З I) 0 + N_ff (λs=>dvNB x.data[s]! W[(k+1)*I+s]!) (З I) 0 + N_ff (λs=>x.data[s]! * dvN W[(k+2)*I+s]!) (З I) 0 + N_ff (λs=>dvNB x.data[s]! W[(k+3)*I+s]!) (З I) 0).isFinite && decide (((З I).foldl (λp s=>p ||| W[k*I+s]! ||| W[(k+1)*I+s]! ||| W[(k+2)*I+s]! ||| W[(k+3)*I+s]!) 0) &&& В<У))=true . rw[if_pos c] at ok ⊢;obtain ⟨c1,c2⟩:=ل.mp c;have c3:=of_decide_eq_true c2;obtain ⟨f0,f1,f2,f3⟩:=qGt c1;have o0:=(qBy f0).1;have o2:=(qBy f2).1;have o1:=qDh f1;have o3:=qDh f3;have IH:=ih (O-(k+4)) א (k+4) _ rfl ok;refine ⟨λj' a b' s hs=>?_,λG Rx Hk H=>?_⟩ . have m:=Р.2 hs;have:j'=k∨j'=k+1∨j'=k+2∨j'=k+3∨k+4<=j' :=א;rcases this with rfl | rfl | rfl | rfl | t;. exact o0 s m . exact o1 s m . exact o2 s m . exact o3 s m . exact IH.1 j' t b' s hs . have Or:=qHt (λs=>W[k*I+s]!) (λs=>W[(k+1)*I+s]!) (λs=>W[(k+2)*I+s]!) (λs=>W[(k+3)*I+s]!) (З I) 0;dsimp only at Or;generalize (З I).foldl (λp s=>p ||| W[k*I+s]! ||| W[(k+1)*I+s]! ||| W[(k+2)*I+s]! ||| W[(k+3)*I+s]!) 0=P at Or c3 IH ⊢;have Z:=Р.2 pi;have hl:0x800000<=P &&& В:=UInt32.le_trans (o0 0 Z).1 (Or.2 0 Z).1;have idem:(P &&& В) &&& В=P &&& В:=by rw[UInt32.and_assoc];rfl have gv:good (P &&& В)=true:=qEx ⟨by rw[idem];exact hl,by rw[idem];exact c3⟩;have hv1:P &&& В<=В:=qCv _;have RX:∀ t∈З I,x.data[t]! = dv X[0*I+t]!:=λt ht=>Rx t (Р.1 ht) have FE:∀ u:Nat,(З I).foldl (λacc s=>acc+x.data[s]! * dv W[u+s]!) 0 =(З I).foldl (λacc t=>acc+dv X[0*I+t]! * dv W[u+t]!) 0:=λu => List.foldl_ext _ _ _ λacc t ht=>by show acc+x.data[t]! * _=_;rw[RX t ht] have Ex:∀ s∈З I,∃ w,good w=true∧x.data[s]! = dv w:=λs hs =>⟨_,G.gx (א:0<1) s (Р.1 hs),RX s hs⟩ have V:∀ m (_:m< O) (sm:Float) (_:sm=(З I).foldl (λacc t=>acc+dv X[0*I+t]! * dv W[m*I+t]!) 0) (_:∀ t,t< I->W[m*I+t]! &&& В<=P &&& В), fin8 I Y X W b 0 m sm (nfOf I) l (P &&& В) b[m]! I.toUSize=C_sp I O Y X W b m:=λm hm sm hs g4=>by have e:=qCi I O Y X W b (i:=0) hm rw[ف,Т] at e rw[e,Hk] exact qDu G Y (א:0<1) hm gv hv1 g4 (З I) ػ (λt ht=>Р.1 ht) (λt ht hn=>absurd (Р.2 ht) hn) hs have r0:=qAg b k.toUSize G.sb;have r1:=qAg b (k.toUSize+1) G.sb;have r2:=qAg b (k.toUSize+2) G.sb;have r3:=qAg b (k.toUSize+3) G.sb;rw[e0] at r0;rw[e1] at r1;rw[e2] at r2;rw[e3] at r3;have Q0:=V k א _ ((qBy f0).2.trans (FE _)) λt ht=>(Or.2 t (Р.2 ht)).1;have Q1:=V (k+1) א _ ((qDt Ex f1).trans (FE _)) λt ht=>(Or.2 t (Р.2 ht)).2.1;have Q2:=V (k+2) א _ ((qBy f2).2.trans (FE _)) λt ht=>(Or.2 t (Р.2 ht)).2.2.1;have Q3:=V (k+3) א _ ((qDt Ex f3).trans (FE _)) λt ht=>(Or.2 t (Р.2 ht)).2.2.2;have H1:C_Inv (1*O) (C_sp I O Y X W b) (λq=>q< k) out :=qA5 H λq _ (hq:q< k)=>show q< j by omega rw[<-r0] at Q0;rw[<-r1] at Q1;rw[<-r2] at Q2;rw[<-r3] at Q3;have H2:=qEq G.sO H1 e0 א Q0 Q1 Q2 Q3;exact IH.2 G Rx Hk H2 . rw[if_neg c] at ok;exact absurd ok ג . rw[colsN,Ш h] at ok ⊢;have hj:O<=j:=by simpa using ok exact ⟨λj' a b'=>by omega,λ_ _ _ H=>qA5 H λq _ (hq:q< O)=>show q< j by omega⟩ end end lemma qEr:linF=linearSpec:=by funext rows inW outW relu X W b;unfold linF;dsimp only;have Hs:=qGd rows W;generalize scanW rows W=hw at Hs ⊢;split . rename_i h;obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13⟩:=h;replace h7:=qA9 h7;replace h8:=qA9 h8;replace h9:=qA9 h9;replace h10:=qA9 h10 have G0:=λ(A:∀ q,q< W.size->good W[q]! = true) => (⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,(qA8 1 X h7 h11).2.2.1,A,(qA8 1 b h9 h13).2.2.1⟩:C_G rows inW outW X W b) have e:linearSpec rows inW outW relu X W b=Array.ofFn (n:=rows*outW) λq =>C_sp inW outW relu X W b q.val:=rfl;have hnf:(inW.toUInt64+8).toFloat*О 0x3CA0000000000000=nfOf inW:=rfl;rw[e,hnf];split . rename_i hr;have hr8:rows=8:=by simpa using hr subst hr8;have H0:C_Inv (8*outW) (C_sp inW outW relu X W b) (λq=>q%outW<0) (К (8*outW) 0) :=⟨by simp,λq _ h=>absurd h (ъ _)⟩;have D0:C_D8 X inW.toUSize.toNat (0: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:=qGq (n:=inW.toUSize) h7 (by rw[nm];exact h1.le) (qA8 1 X h7 h11).2.2.1 inW 0 _ 0 0 0 0 0 0 0 0 (by rw[nm,C_z,Т]) D0;rw[nm] at D;split . rename_i hc have A:∀ q,q< W.size->good W[q]! = true:=by rcases hc with hc | hc . have k:=qE8 (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[qBa _ _ (by rw[qAd _ א,nm];omega),qAd _ א,nm,ف];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 (qB1 W h8 n0).2.2.1 . exact (qBi W h8 hc).2.2.1 exact qBv (qF7 (G0 A) relu D outW 0 _ _ _ _ א H0) (λq _=>ر _ h5) . rw[qAi];rfl . rename_i hr;split . rename_i hr1;have e1:rows=1:=by simpa using hr1 subst e1;have GX:=(qA8 1 X h7 h11).2.2.1;have sX1:0*inW+inW<=X.size :=א;have nm:inW.toUSize.toNat=inW:=qAd _ א;have P0:(pushZ inW.toUSize 0 (Й inW)).size=inW:=by rw[qDy inW.toUSize inW 0 _ (by rw[nm,C_z,Т])];exact Т _ obtain ⟨d1,d2,d3,d4,d5⟩:=qEy (a:=0*inW) (n:=inW) h7 sX1 (λt ht=>GX _ א) (ix:=К inW 0) ג P0;obtain ⟨k1,k2,k3⟩:=qDs (λp=>X[0*inW+p]!) inW;have Rw:=C_Row.mk d1 (qBj _ _) d2 d3 k1 k2 k3 d4 d5;generalize decR X (0*inW).toUSize inW.toUSize 0 (К inW 0) (pushZ inW.toUSize 0 (Й inW)) 0 0=r at Rw ⊢;have H0:C_Inv (1*outW) (C_sp inW outW relu X W b) (λq=>q<0) (К (1*outW) 0) :=⟨by simp,λq _ h=>absurd h (ъ _)⟩ have Fb:(if scanH 1 W≠Е then cols1 inW outW relu X W b 0 r.ix r.buf (Й 0) (nfOf inW) r.l (scanH 1 W) inW.toUSize r.c 0 (0*outW).toUSize (К (1*outW) 0) else linearWith dotFn 1 inW outW relu X W b) =Array.ofFn (n:=1*outW) λq=>C_sp inW outW relu X W b q.val:=by split . rename_i hv;have Hv:=qA8 1 W h8 hv;have G:=G0 Hv.2.2.1;have hb0:(0*outW).toUSize.toNat=0*outW:=qAd _ א;refine qBv (qD0 G relu (א:0<1) Hv Rw hb0 ?_ outW 0 _ (Т _) (by rw[ف,ю];exact H0)) (λq hq=>by omega);intro h q hq;have e:0=W.size:=h;omega . rw[qAi];rfl by_cases h0:(r.c == inW.toUSize)=true . rw[if_pos h0];split . rename_i hc;obtain ⟨A1,A2⟩:=qHo h2 h4 h6 h8 Rw.sx _ 0 _ rfl hc have A:∀ q,q< W.size->good W[q]! = true:=λq hq=>by have e:=qEx (A1 (q/inW) (Nat.zero_le _) (ш (by rw[е,h2];exact hq)) (q%inW) (ر _ h4)) rwa [Nat.div_add_mod' q inW] at e have le:(C_keep (λp=>X[0*inW+p]!) inW).length=inW:=by rw[<-Rw.len,beq_iff_eq.mp h0,nm] have Le:C_keep (λp=>X[0*inW+p]!) inW=З inW:=by unfold C_keep at le ⊢;apply List.filter_eq_self.2;apply (List.length_filter_eq_length_iff).1;rw[le,List.length_range] have Rx:∀ s,s< inW->r.buf.data[s]! = dv X[0*inW+s]!:=λs hs =>(Rw.rd s s (by rw[Le];simp[hs])).2;have Hl:=Rw.ok;rw[Le] at Hl;exact qBv (A2 (G0 A) Rx Hl H0) (λq hq=>by omega) . exact Fb . rw[if_neg h0];exact Fb . rename_i hr1;have Hv:=Hs (by simpa using hr) (by simpa using hr1) h8 h12;have G:=G0 Hv.2.2.1;have H0:C_Inv (rows*outW) (C_sp inW outW relu X W b) (λq=>q<0*outW) (К (rows*outW) 0) :=⟨by simp,λq _ h=>by rw[ف] at h;exact absurd h (ъ _)⟩;have P0:(pushZ inW.toUSize 0 (Й inW)).size=inW:=by rw[qDy inW.toUSize inW 0 _ (by rw[G.nm,C_z,Т])];exact Т _ refine qBv (qGf G relu Hv ?_ rows 0 _ _ _ א ג P0 H0) (λq hq=>hq);split;. intro _ q hq;rw[<-Array.foldl_toList,qIy];simp[hq,FloatArray.emptyWithCapacity] . intro h q hq;have e:0=W.size:=h;omega . rw[qAi];rfl end lemma qDz:linF=linearSpec:=qEr @[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 qIc (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 qHk (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,qDg N 62 hN1 hN2,qFq];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[qBn _ _ _ _ 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,qIc] 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[qEf _ _ _ 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 qF5 (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[qBb,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,qHk 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:=qAq 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 qId (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:=qAz w hg;have a0:(w &&& В).toNat≠0:=λe=>h0 (а.mp (by simpa using e));rw[qAq] at a0;have hx:=qCa w;have hf:(fracOf w).toNat=w.toNat%2^23:=qCo 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 qG4 (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⟩:=qId 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 qFd (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[qF5 _ _ _ 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 qGu (w:UInt32):(T_e w).toNat<=255:=by unfold T_e;split;. decide . have:=qCa w;simp only[UInt32.toNat_toUInt64];omega @[inline] def T_min (x y:UInt64):UInt64:=if x< y then x else y lemma qE5 (x y:UInt64):(T_min x y).toNat<=x.toNat:=by unfold T_min;split;. exact le_rfl . have:=ф.not.1 ‹_›;omega lemma qGa (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⟩:=qG4 e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=qG4 e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma qEu (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 qGj (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⟩:=qGa _ x y hx hy;have ex:=qEu _ x hx;have ey:=qEu _ y hy;unfold addSlow;congr 1 refine qFd _ _ _ _ א א ((qE5 _ _).trans (qGu 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 qGm (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[qE4 S m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hE];push_cast;linarith) (λ_=>by rw[hE];push_cast;linarith [hW2 א]) (λ_=>by rw[hE];linarith),show ((ex:Int)-1046+150).toNat=ex-896 by omega,show (2^23+fr)%2^23=fr by omega] lemma qFt (a b:ℝ) (ha:0<=a) (h:a< b) : ∃ (m: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 qGz (z:Model Fmt):ofW (toWord z)=z:=qEb z lemma qIm (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[qAx w h1,rationalWord,decodeD_toRat,this,zpow_ne_zero] lemma qDq (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⟩:=qB8 s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=х ב _;have hA2:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076) :=С ב hb0.le obtain ⟨Lo,hLo0,hLoW,hLo1,hLo2⟩:∃ Lo:ℝ,0<=Lo∧Lo< W ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<=Lo) ∧ (2^53-2^28)*2^((ex:Int)-1076)<=Lo:=by by_cases hfr:fr=0;. exact ⟨_,hA2,hW3,λh=>absurd hfr h,le_rfl⟩ . have hfr1:(1:ℝ)<=fr:=by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hfr refine ⟨_,?_,hW2 hfr,λ_=>le_rfl,?_⟩;. exact С (by nlinarith) hb0.le . exact Б (by nlinarith) hb0.le have hW0:0< W:=ч hLo0 hLoW;obtain ⟨m1,q1,hm1,hd1,hd1'⟩:=qFt Lo W hLo0 hLoW;set B:ℝ:=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) with hB;obtain ⟨m2,q2,hm2,hd2,hd2'⟩:=qFt W ((W+B)/2) hW0.le ד;have w1:=qGm S ex fr m1 q1 h1 h2 h3 hm1 ד (λh=>ч (hLo1 h) hd1) (ч hLo2 hd1) have w2:=qGm S ex fr m2 q2 h1 h2 h3 hm2 ד (λh=>by exact ч (hLo1 h) ד) (ч hLo2 ד) rw[hz];set z:=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) with hzd;have hzn:z.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[qCa,hzn];split<;>omega have he0:expOf z≠0:=λh=>by rw[h] at hexp;simp at hexp;omega have he1:expOf z≠255:=λh=>by rw[h] at hexp;simp at hexp;omega have r1:Щ Fmt ⟨S,m1,q1⟩=ofW z:=by rw[<-w1,qGz] have r2:Щ Fmt ⟨S,m2,q2⟩=ofW z:=by rw[<-w2,qGz] have fz:Ы (ofW z)=true:=by simp[ofW,isFinite_ofUInt32,he1] have t1:=Model.toReal_roundDyadic_eq_roundAt Fmt (by decide) ⟨S,m1,q1⟩ (by rw[r1];exact fz);have t2:=Model.toReal_roundDyadic_eq_roundAt Fmt (by decide) ⟨S,m2,q2⟩ (by rw[r2];exact fz);rw[r1] at t1;rw[r2] at t2 have hv:к (ofW z)=Model.roundAt Fmt V:=by apply le_antisymm . cases S;. rw[t1];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . rw[t2];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . cases S;. rw[t2];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . rw[t1];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith have hnz:к M≠0:=by rw[hM,<-hv];exact qIm z he0 he1 have:=Model.eq_of_toReal_eq_of_nonzero hf fz (hM.trans hv.symm) hnz;rw[this];have hr:toWord (ofW z)=z:=by rw[<-r1,w1] exact hr lemma qIx:(2:ℝ)^(127:ℤ)<=к (Model.posMaxFinite Fmt):=by rw[Model.toReal_posMaxFinite,show Model.pow2 Fmt.fracWidth+Fmt.maxFiniteFracField=2^24-1 by decide +kernel,show Fmt.maxNormalExponent-Int.ofNat Fmt.fracWidth=104 by decide +kernel,Model.bpow,qA0];norm_num lemma qFn (q:ℚ) : Model.signedScaledRatToReal (decide (q<0)) q.num.natAbs q.den 0=(q:ℝ):=by rw[Model.signedScaledRatToReal,Model.scaledRatToReal,Nat.cast_natAbs,Rat.cast_def];by_cases hq:q<0;. simp[hq,abs_of_neg (Rat.num_neg.2 hq)];ring . simp[hq,abs_of_nonneg (Rat.num_nonneg.2 (not_lt.1 hq))] lemma qH7 (s e: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 qIx);have hM:=Model.toReal_roundRatScaled_eq_roundAt Fmt _ _ _ 0 (by decide) (by simpa using λh0=>qG5 s e q hlo hhi h (by simp[h0])) q.den_nz hf;exact qDq s e q hlo hhi h _ hf (qFn q ▸ hM) lemma qH4 (N d: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 (qA0 127).symm) lemma qIp (q:ℚ) (hq:0< q) (hb:з (q:ℝ)<2^(127:ℤ)) : Ы (sqrtModel q)=true := qH4 _ _ _ (ne_of_gt (sqrtRoot_pos q hq)) q.pos (sqrtStep_le_target q) (by rwa [sqrtScale_correct q hq]) lemma qGs (s e: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:=qIp q hq hb;exact (if_neg (not_le.2 hq)).trans (qDq s e _ hlo hhi h _ hf (sqrtModel_correct q hq hf)) lemma qA1 (w:UInt32) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by obtain ⟨hg,hl⟩:=ل.1 h;have h1:=Ж.1 (of_decide_eq_true hl);rw[qAq] at h1;have h2:=qAz w hg;rw[qB5 w hg];refine ⟨hg,(qA6 _ _ h1 א).trans ?_⟩;unfold A_mag;norm_num lemma qH8 (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 qIl (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 qG7 (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⟩⟩:=qEs _ _ h rw[д] at e1 rw[UInt64.toNat_sub] at e2 obtain ⟨a,b,c⟩:=qH8 _ 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 qH5 (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 qIz (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⟩:=qH5 _ 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⟩:=qIl _ _ _ 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 qGo (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⟩:=qG7 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⟩:=qIz ex fr E F h1 h2 h3 hF א א W hW;rw[hz];exact (qEo d S ex fr h1 h2 h3 W hVW a b c).symm 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,-⟩:=qA1 x hc.1;obtain ⟨gy,-⟩:=qA1 y hc.2;dsimp only;split;. exact qGj 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,qAx x ex,qAx y ey];simp only[rationalWord,Rat.cast_add] obtain ⟨fx,rx⟩:=qAm x gx;obtain ⟨fy,ry⟩:=qAm y gy;obtain ⟨-,he⟩:=qBf _ _ fx fy (by rw[rx];exact (qAo x gx).trans qAn) (by rw[ry];exact (qAo y gy).trans qAn);rw[rx,ry,<-hd] at he;rw[qGo _ _ 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 qGy {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:=qBh 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,qBu _ _ _ _ h3 א,usz i e hi,qAg _ _ 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 qFi: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:=qGy (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,qAg _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,qCt] end section @[noinline] def E_fma (relu:Bool) (w sw tw:UInt32):UInt32 := act relu (toWord (Model.fma (ofW w) (ofW sw) (ofW tw))) @[inline] def E_act (relu:Bool) (r:UInt32):UInt32 := if relu && signOf r then 0 else r @[inline] def E_el (relu g:Bool) (sw tw:UInt32) (y z:Float) (w:UInt32):UInt32 := if g && good w then let r:=zivS (dv w*y+z) if r == Е then E_fma relu w sw tw else E_act relu r else E_fma relu w sw tw @[specialize] def E_map (f:UInt32->UInt32) (e i:USize) (x:Array UInt32):Array UInt32 := if h:i< e then E_map f e (i+1) (wrW x i (f (rdW x i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def E_chs (relu:Bool) (c hw:Nat) (s t:Array UInt32) (ch:Nat) (x:Array UInt32):Array UInt32 := if ch< c then let sw:=s[ch]! let tw:=t[ch]! let y:=dv sw let z:=dv tw E_chs relu c hw s t (ch+1) (E_map (E_el relu (good sw && good tw) sw tw y z) ((ch+1)*hw).toUSize (ch*hw).toUSize x) else x termination_by c-ch def scaleF (relu:Bool) (c hw:Nat) (x s t:Array UInt32):Array UInt32 := if x.size=c*hw∧x.size<2^32 then E_chs relu c hw s t 0 x else scaleShiftSpec relu c hw x s t @[noinline] def E_dot (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := dotFn (k*k) (λt=>X[b.toNat+(t/k*w+t%k)]!) (λ_=>oneW) section open Float.Model Float.Model.UnpackedFloat def E_dotT (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := if k=2 then let i:=b.toNat let a0:=X[i]! let a1:=X[i+1]! let a2:=X[i+w]! let a3:=X[i+(w+1)]! let e0:=T_min (T_min (T_e a0) (T_e a1)) (T_min (T_e a2) (T_e a3)) if T_ok e0 a0 && T_ok e0 a1 && T_ok e0 a2 && T_ok e0 a3 then T_fin (T_p e0 a3 (T_p e0 a2 (T_p e0 a1 (T_p e0 a0 0)))) (T_n e0 a3 (T_n e0 a2 (T_n e0 a1 (T_n e0 a0 0)))) e0 λ_=>E_dot X b k w else E_dot X b k w else E_dot X b k w end @[noinline] def E_div (n:Nat) (s:UInt32):UInt32 := toWord (Model.div (ofW s) (ofW (natW n))) @[inline] def E_quo (n:Nat) (j s:UInt32):UInt32 := if j< expOf s∧expOf s≠255 then s-(j <<< (23:UInt32)) else E_div n s @[inline] def E_tl (X:Array UInt32) (b:USize) (k w n:Nat) (j z:UInt32) (nf zb s a:Float) (o:UInt32):UInt32 := if o=0 then z else let r:=ziv (s+zb) (nf*(a+zb.abs)) E_quo n j (if r == Е then E_dotT X b k w else r) def E_win (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (u nf zb:Float) (b m t:USize) (s a:Float) (o:UInt32):UInt32 := if h:t< m then let v:=rdW X (b+(rdW off t).toUSize) let d:=dv v*u E_win X off k w n j z u nf zb b m (t+1) (s+d) (a+d.abs) (o ||| v) else E_tl X b k w n j z nf zb s a o termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega @[specialize] def G_x (f:USize->UInt32) (sx:USize) (W x:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if x< W then G_x f sx W (x+1) (b+sx) (q+1) (wrW out q (f b)) else out termination_by W-x @[specialize] def G_y (f:USize->UInt32) (sy sx:USize) (W H y:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if y< H then G_y f sy sx W H (y+1) (b+sy) (q+W.toUSize) (G_x f sx W 0 b q out) else out termination_by H-y @[specialize] def G_c (f:USize->UInt32) (sc sy sx:USize) (W H c ch:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if ch< c then G_c f sc sy sx W H c (ch+1) (b+sc) (q+(H*W).toUSize) (G_y f sy sx W H 0 b q out) else out termination_by c-ch def E_off (k w:Nat):Array UInt32 := Array.ofFn (n:=k*k) λt=>(t.val/k*w+t.val%k).toUInt32 @[noinline] def E_grid (c h w k n:Nat) (X off:Array UInt32) (j z:UInt32) (u nf zb zr:Float) : Array UInt32 := G_c (λb=>E_win X off k w n j z u nf zb b n.toUSize 0 zr zr 0) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (К (c*(h/k*(w/k))) 0) def avgF (c h w k:Nat) (X:Array UInt32):Array UInt32 := if c*h*w<=X.size∧X.size<2^32∧k*k<2^20 ∧ scan X X.usize 0 (qAc X) Е 0≠Е then let n:=k*k let off:=E_off k w let j:UInt32:=if n=4 then 2 else if n=64 then 6 else 255 let z:UInt32:=if 0< k∧k<17 then 0 else E_div n (dotFn n (λ_=>0) (λ_=>oneW)) E_grid c h w k n X off j z (dv oneW) (nfOf n) (dv 0) 0 else avgpoolSpec c h w k X 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 def rmsF (rows n:Nat) (eps:Word) (x g:Array Word):Array Word := if stableNormDomain rows n eps x g && x.all sm && g.all sm && sm eps && decide (n<2^20) && decide (rows*n<2^32) then let st:=Array.ofFn (n:=rows) λr=>lnStat true n eps x r.val fR (λr=>st[r]!) (λs p j => let w:=rdW x p if w &&& В == 0 then 0 else lnOut s w (rdW g j) 0 λ_=>0) n 0 (К (rows*n) 0) rows else rmsnormSpec rows n eps x g section lemma qFv (x:UInt64):(x >>> 52).toNat=x.toNat/2^52:=by rw[р,щ];rfl lemma qEj (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[UInt64.toNat_sub] at h1;have h2:=x.toNat_lt;have h3:=k.toNat_lt;omega lemma qEi (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,Э Ч,й] lemma qCz {f: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[qFv] at h;obtain ⟨F,hF,hM⟩:∃ F<2^52,f.toBits.toNat=E*2^52+F :=⟨f.toBits.toNat%2^52,by omega,by omega⟩;obtain ⟨h1,h2⟩:=B_R f false E F hE1 hE hF (by simpa using hM);have hb:(0:ℝ)<2^((E:ℤ)-1075):=х ב _;have hF0:(0:ℝ)<=F:=Ю _;have hF2:(F:ℝ)<2^52:=by exact_mod_cast hF simp only[Bool.false_eq_true,ite_false,one_mul] at h2;push_cast at h2;rw[h2,qEi ((E:ℤ)-1023) (E-1075) 52 א,qEi ((E:ℤ)-1022) (E-1075) 53 א];exact ⟨h1,Б ד hb.le,о ד hb⟩ lemma L_k (c: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 qGk {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[qFv] at h have hs:Model.signBit (toModel (ofFloat f))=false:=by rw[qFs];simp only[decide_eq_false_iff_not,not_le];omega have hm:toModel (ofFloat f)=A_M f:=toModel_ofFloat f have hfm:Ы (A_M f)=true:=by rw[qAh,hf] rw[hm] at hs have hr:=Model.toReal_sqrt_eq_roundAt (A_M f) (by decide) hfm (Or.inr hs) have hq:=Model.isFinite_sqrt_of_isFinite (A_M f) (by decide) hfm (Or.inr hs) have hd:A_M (м f)=Model.canonicalizeModel (Model.sqrt (A_M f)):=by have h3:=toModel_ofFloat_sqrt f rw[hm,toModel_ofFloat] at h3 exact h3 have ht:Model.toModel (A_M (м f))=Model.toModel (Model.sqrt (A_M f)):=by rw[hd,Model.toModel_canonicalizeModel] have hR:R (м f)=Model.roundAt FloatFormat.binary64 (з (R f)):=by rw[A_R,ж qAj,ht,<-ж qAj] exact hr refine ⟨?_,hp,?_⟩ . rw[<-qAh,qAy,ht,<-qAy];exact hq . rw[hR];have:=qFp (з (R f));rwa [И (Real.sqrt_nonneg _)] at this lemma qAa (x y:ℝ) : |x| ≤ |y| + |x-y|:=by have:=abs_sub_abs_le_abs_sub x y;linarith lemma qAt {k:ℝ} (h:k<=2^256*2^44):k<=A_B :=h.trans_eq ((qEi 300 44 256 rfl).trans ב).symm lemma qBg (a: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 qE6 (w:UInt32) (g:good w=true) (h:w &&& В=0):Д w=0:=by exact_mod_cast (qAx w (qAe _ g)).symm.trans (qBr w g h) lemma qIu (s e: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⟩:=qB8 s e V hlo hhi h0 have hb:(0:ℝ)<2^((ex:Int)-1076):=х ב _ have hW0:0< W:=lt_trans (mul_pos ב hb) hW3 have hN:((if S then 2^31 else 0)+(ex-896)*2^23+fr)<2^32:=by split<;>omega have hex:ex<=1149:=by by_contra hc apply h apply у.mpr rw[qAq,hz,UInt32.toNat_ofNat_of_lt' hN,(א:ex=1150)] show 2130706431< _ split<;>omega have hF:(fr:ℝ)+1<=2^23:=by exact_mod_cast (א:fr+1<=2^23) rw[hV] have hW:W<=2^54*2^(73:ℤ):=hW1.le.trans (Х ד (Ц ב א) hb.le (by positivity)) rw[<-qEi 127 73 54 rfl] at hW split<;>simp only[abs_neg,abs_of_pos hW0,hW] lemma L_I {n I:ℝ} (hn:1<=n) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48):I<=2:=by have:=Г hn h3.le;linarith [(abs_le.mp h4).2] lemma qF0 (n I v t Sb E T:ℝ) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (ht : |t| ≤ Sb) (hv : |v-n*t| ≤ n*E+T) (hE:0<=E) : |v*I-t| ≤ E*(1+1/2^48)+T*I+Sb*(1/2^48):=by have a1 : |(v-n*t)*I| ≤ (n*E+T)*I:=by rw[в,б h3];exact Б hv h3.le have a2:=Г (abs_le.mp h4).2 hE have a3 : |t*(I*n-1)| ≤ Sb*(1/2^48):=by rw[в];exact Х ht h4 (Л _) ((Л _).trans ht) have a4:=Ъ ((v-n*t)*I) (t*(I*n-1)) rw[show (v-n*t)*I+t*(I*n-1)=v*I-t by ring] at a4 linarith section variable {z:ℝ} (hz0:0<=z) (hz:z<=1/2^200*(1/2^200)) include hz0 hz lemma qD9 {P rp rs rc:ℝ} (h1 : |rp-P| ≤ 1/2^53*(|P| + z)) (h2 : |rs-(rp-1)| ≤ 1/2^53*(|rp-1| + z)) (h3 : |rc-(rs+1/2^50)| ≤ 1/2^53*(|rs+1/2^50| + z)) (lo:1/2^51<=rc) (hi:rc<=1/2^49) : |P-1| ≤ 1/2^48:=by have a1:=qAa (rs+1/2^50) rc;rw[г (rs+1/2^50) rc] at a1;have a2 : |rc| = rc:=И (ش ב lo);obtain ⟨c1,c2⟩:=abs_le.mp h3;have a3 : |rs| ≤ 1/2^49:=by rw[abs_le];constructor<;>linarith [Л (rs+1/2^50)] have b1:=qAa (rp-1) rs;rw[г (rp-1) rs] at b1;have b2 : |rp-1| ≤ 1/2^48:=by linarith [Л (rp-1)] have c3:=qAa (P-1) (rp-1);rw[show P-1-(rp-1)=-(rp-P) by ring,abs_neg] at c3;have c4:=qAa P 1;rw[abs_one] at c4;linarith [Л (P-1)] lemma qG0 {a Mr Rm Em g D b I RA Rg Rq RY RE Rsa Rcs Reg Hi Lo:ℝ} (hE0:0<=Em) (hE1:Em<=2^120) (hM:2 * |Mr-Rm| ≤ Em) (hD:1/2^64<=D) (ha : |a| ≤ 2^99) (hg : |g| ≤ 2^99) (hm : |Rm| ≤ 2^110) (h1 : |RA-(a-Rm)| ≤ 1/2^53*(|a-Rm| + z)) (h2 : |I*D-1| ≤ 1/2^48) (h3 : |Rg-g*I| ≤ 1/2^53*(|g*I| + z)) (h4 : |Rq-RA*Rg| ≤ 1/2^53*(|RA*Rg| + z)) (h5 : |RY-(Rq+b)| ≤ 1/2^53*(|Rq+b| + z)) (h6 : |Rsa-(|Rq| + |b|)| ≤ 1/2^53*(|(|Rq| + |b|)| + z)) (h7 : |Rcs-1/2^46*Rsa| ≤ 1/2^53*(|1/2^46*Rsa| + z)) (h8 : |Reg-Em*(|Rg|)| ≤ 1/2^53*(|Em*(|Rg|)| + z)) (h9 : |RE-(Rcs+Reg)| ≤ 1/2^53*(|Rcs+Reg| + z)) (h10 : |Hi-(RY+RE)| ≤ 1/2^53*(|RY+RE| + z)) (h11 : |Lo-(RY-RE)| ≤ 1/2^53*(|RY-RE| + z)) (hbig:1/2^127 ≤ |Hi|) : Lo<(a-Mr)*g/D+b∧(a-Mr)*g/D+b< Hi:=by have hDp:0< D:=н (by positivity) hD;generalize hG:g/D=G;generalize hX:a-Rm=X at h1;rw[show (a-Mr)*g/D+b=X*G+(Rm-Mr)*G+b by rw[<-hG,<-hX];field_simp;ring];have hGb : |G| ≤ 2^163:=by rw[<-hG,abs_div,б hDp,div_le_iff₀ hDp];linarith have hXb : |X| ≤ 2^111:=hX ▸ (ى _ _).trans ד;have hgI : |g*I-G| ≤ 1/2^48 * |G|:=by rw[show g*I-G=G*(I*D-1) by rw[<-hG];field_simp,в,mul_comm];exact Б h2 (Л _) generalize g*I=Y at h3 hgI;clear hG hX hDp hD ha hg hm h2;have nX:=Л X;have nG:=Л G;have nA:=Л RA;have nR:=Л Rg;have a1:=qAa X RA;rw[г X RA] at a1;have a3 : |X| ≤ (1+2/2^53) * |RA| + 2/2^53*z :=ד;have a4 : |X-RA| ≤ 2/2^53 * |RA| + 2/2^53*z:=by rw[г];linarith have a5 : |RA| ≤ 2^165:=by have:=qAa RA X;linarith have b1:=qAa Y G;have b2:=qAa G Rg;have b3:=abs_sub_le G Y Rg;rw[г G Y,г Y Rg] at b3;have b4 : |G| ≤ (1+1/2^47) * |Rg| + 2/2^53*z :=ד;have b5 : |G-Rg| ≤ 2/2^48 * |Rg| + 2/2^53*z :=ד;have c1 : |Rg| ≤ 2^165:=by have:=qAa Rg G;rw[г] at this;linarith have f1:=Ъ ((X-RA)*G) (RA*(G-Rg));rw[в,в,show (X-RA)*G+RA*(G-Rg)=X*G-RA*Rg by ring] at f1;have f2:=Х a4 b4 nG ד;have f3:=Г b5 nA;have zr:z * |RA| ≤ z*2^165:=Г a5 hz0;have zg:z * |Rg| ≤ z*2^165:=Г c1 hz0;have zz:z*z<=z*2^165:=Г ד hz0;have ze:z*Em<=z*2^165:=Г ד hz0;have nzr:=С hz0 nA;have nzg:=С hz0 nR;have nzz:=С hz0 hz0;have nP:=С nA nR;have f4 : |X*G-RA*Rg| ≤ 9/2^50*(|RA| * |Rg|)+1/2^230 :=ד;have p1 : |RA*Rg| ≤ (1+2/2^53) * |Rq| + 2/2^53*z:=by have:=qAa (RA*Rg) Rq;rw[г (RA*Rg) Rq] at this;have nq:=Л (RA*Rg);linarith rw[<-в] at f4;have q1 : |X*G-Rq| ≤ 5/2^49 * |Rq| + 1/2^229:=by have:=ى (X*G-RA*Rg) (Rq-RA*Rg);rw[show X*G-RA*Rg-(Rq-RA*Rg)=X*G-Rq by ring] at this;have nq:=Л Rq;linarith generalize hT:Em * |Rg| = T at h8;have nT:0<=T:=hT ▸ С hE0 nR;have e1 : |(Rm-Mr)*G| ≤ 5/2^3*T+1/2^230:=by rw[в,г];have h : |Mr-Rm| * |G| ≤ (Em/2)*((1+1/2^47) * |Rg| + 2/2^53*z) :=Х ד b4 nG ד;have:=С hz0 hE0;subst hT;linarith rw[И nT] at h8;have nq:=Л Rq;have nb:=Л b;have nS:0 ≤ |Rq| + |b| :=ד;rw[И nS] at h6;have s0 : |Rq+b| ≤ |Rq| + |b|:=Ъ _ _;have s1 : |Rsa| ≤ 2*(|Rq| + |b|)+z:=by have:=qAa Rsa (|Rq| + |b|);rw[И nS] at this;linarith have h7' : |1/2^46*Rsa| = 1/2^46 * |Rsa|:=by rw[в,б (by positivity:(0:ℝ)<1/2^46)] rw[h7'] at h7;have s2 : |Rcs| ≤ 1/2^44*(|Rq| + |b|)+z:=by have:=qAa Rcs (1/2^46*Rsa);rw[h7'] at this;linarith have s3 : |Reg| ≤ 2*T+z:=by have:=qAa Reg T;rw[И nT] at this;linarith have s4 : |RY| ≤ 2*(|Rq| + |b|)+z:=by have:=qAa RY (Rq+b);linarith have s5:=Ъ Rcs Reg;have s6 : |RE| ≤ 1/2^43*(|Rq| + |b|)+4*T+4*z:=by have:=qAa RE (Rcs+Reg);linarith have s7:=Ъ RY RE;have s8:=ى RY RE;have big:1/2^130 ≤ |Rq| + |b| + T:=by have:=qAa Hi (RY+RE);linarith obtain ⟨u1,u2⟩:=abs_le.mp q1;obtain ⟨v1,v2⟩:=abs_le.mp e1;obtain ⟨w1,w2⟩:=abs_le.mp h5;obtain ⟨x1,x2⟩:=abs_le.mp h6;obtain ⟨y1,y2⟩:=abs_le.mp h7;obtain ⟨z1,z2⟩:=abs_le.mp h8;obtain ⟨o1,o2⟩:=abs_le.mp h9;obtain ⟨r1,r2⟩:=abs_le.mp h10;obtain ⟨t1,t2⟩:=abs_le.mp h11;constructor<;>linarith lemma qG3 {n Rs Rt I Rm N Y Em c:ℝ} (r Sb:ℝ) (hn1:1<=n) (hn2:n<=2^20) (hc:c=(n+8)/2^49) (hr : |r| ≤ Sb) (hS:Sb<=2^99) (h1 : |Rs-n*r| ≤ 2*n*(1/2^53*(n*Sb+z))) (h2 : |Rt-n*Sb| ≤ 2*n*(1/2^53*(n*Sb+z))) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (h5 : |Rm-Rs*I| ≤ 1/2^53*(|Rs*I| + z)) (h6 : |N-c*Rt| ≤ 1/2^53*(|c*Rt| + z)) (h7 : |Y-N*I| ≤ 1/2^53*(|N*I| + z)) (h8 : |Em-(Y+1/2^200)| ≤ 1/2^53*(|Y+1/2^200| + z)) : 2 * |r-Rm| ≤ Em∧Em<=2^120∧1/2^201<=Em∧|Rm| ≤ 2^110:=by have hS0:0<=Sb:=ش (Л _) hr;have nS:0<=n*Sb:=С ד hS0;have nSb:n*Sb<=2^20*2^99:=Х hn2 hS hS0 ב;generalize hW:n*Sb+z=W at h1 h2;have nW:0<=W :=ד;have hI2:=L_I hn1 h3 h4;generalize hE:201/100/2^53*W+Sb/2^48=E have key:∀ v t:ℝ,|t| ≤ Sb->|v-n*t| ≤ 2*n*(1/2^53*W)->|v*I-t| ≤ E := λv t ht hv=>by have:=qF0 n I v t Sb (2*(1/2^53*W)) 0 h3 h4 ht ד ד;linarith have hr1:=key Rs r hr h1;have hA1:=key Rt Sb (by rw[И hS0]) h2;generalize hA:Rt*I=A at hA1;have hE0:0<=E:=by rw[<-hE];positivity have hEb:E<=2^80:=by rw[<-hE];norm_num at nSb hS ⊢;linarith have hc0:0<=c:=by rw[hc];positivity have hc1:c<=1/2^28:=by rw[hc];norm_num at hn2 ⊢;linarith have b0:=qAa A Sb;rw[И hS0] at b0;have b1 : |N*I-c*A| ≤ 1/2^53*(|c*A| + 2*z):=by rw[<-hA,show N*I-c*(Rt*I)=(N-c*Rt)*I by ring,в,б h3,show |c*(Rt*I)| = |c*Rt| * I by rw[в,в,в,б h3];ring];have:=Б h6 h3.le;have:=Г hI2 hz0;linarith have b2 : |c*A| ≤ 1/2^28*(Sb+E):=by rw[в,И hc0];exact Х hc1 ד (Л _) ב have b3:c*Sb-1/2^28*E<=c*A:=by have h9 : |c*(A-Sb)| ≤ 1/2^28*E:=by rw[в,И hc0];exact Х hc1 hA1 (Л _) ב linarith [(abs_le.mp h9).1] have b4:c*Sb=(n*Sb+8*Sb)/2^49:=by rw[hc];ring have c1:=qAa (N*I) (c*A);have c2:=qAa Y (N*I);have c3:=Ъ Y (1/2^200);rw[б (by norm_num:(0:ℝ)<1/2^200)] at c3;have d1:=qAa Rm (Rs*I);have d2:=qAa (Rs*I) r;have d3:=abs_sub_le r (Rs*I) Rm;rw[г r (Rs*I),г (Rs*I) Rm] at d3;obtain ⟨e1,e2⟩:=abs_le.mp b1;obtain ⟨e3,e4⟩:=abs_le.mp h7;obtain ⟨e5,e6⟩:=abs_le.mp h8;rw[<-hE] at hr1 b2 b3 hEb;subst hW;have ha:=Л r;have g1:=le_abs_self (c*A);exact ⟨by linarith,by linarith,by linarith,by linarith⟩ lemma qHr {s c Em dd Rn2 Rmu ee hi lo:ℝ} (hs0:0<=s) (hs1:s<=2^101) (hc0:9/2^49<=c) (hc1:c<=1/2^20) (hE:1/2^201<=Em) (hE1:Em<=2^120) (h1 : |dd-s| ≤ 1/2^53*(s+z)) (h2 : |Rn2-(c+c)| ≤ 1/2^53*(|c+c| + z)) (h3 : |Rmu-Rn2*dd| ≤ 1/2^53*(|Rn2*dd| + z)) (h4 : |ee-(Rmu+Em)| ≤ 1/2^53*(|Rmu+Em| + z)) (h5 : |hi-(dd+ee)| ≤ 1/2^53*(|dd+ee| + z)) (h6 : |lo-(dd-ee)| ≤ 1/2^53*(|dd-ee| + z)) : s*(1+3/2*c)+9/10*Em<=hi∧lo<=s*(1-3/2*c)-9/10*Em:=by have hcp:0< c:=н ב hc0;have hcs:0<=c*s:=С hcp.le hs0;have hcs2:=Б hc0 hs0;have hcc : |c+c| = c+c:=б ד;rw[hcc] at h2;have d1:=qAa dd s;rw[И hs0] at d1;have d2 : |dd| ≤ 3*s+z :=ד;have d3 : |dd| ≤ 2^103:=by norm_num at hs1 hz d2 ⊢;linarith have m1:=Ъ ((Rn2-(c+c))*dd) ((c+c)*(dd-s));rw[в,в,hcc,show (Rn2-(c+c))*dd+(c+c)*(dd-s)=Rn2*dd-(c+c)*s by ring] at m1;have m2:=Х h2 d2 (Л _) (by positivity);have m3:=Г h1 (by linarith:0<=c+c);have zs:=Х hz hs1 hs0 ב;have zc:z*c<=z*1:=Г ד hz0;have zz:z*z<=z*1:=Г ד hz0;have nzs:=С hz0 hs0;have nzc:=С hz0 hcp.le;have nzz:=С hz0 hz0;have m4 : |Rn2*dd-(c+c)*s| ≤ 1/2^49*(c*s)+1/2^230:=by norm_num at zs ⊢;linarith generalize Rn2*dd=M at h3 m4;rw[show (c+c)*s=2*(c*s) by ring] at m4;have n1:=qAa M (2*(c*s));rw[И (by linarith:0<=2*(c*s))] at n1;have n2:=qAa Rmu M;have n3:=Ъ Rmu Em;rw[б (by linarith [(by norm_num:(0:ℝ)<1/2^201)]:0< Em)] at n3;have n4:=qAa ee (Rmu+Em);have n5:=Ъ dd ee;have n6:=ى dd ee;obtain ⟨a1,a2⟩:=abs_le.mp h1;obtain ⟨b1,b2⟩:=abs_le.mp m4;obtain ⟨c1,c2⟩:=abs_le.mp h3;obtain ⟨d4,d5⟩:=abs_le.mp h4;obtain ⟨e1,e2⟩:=abs_le.mp h5;obtain ⟨f1,f2⟩:=abs_le.mp h6;have hcs3:=Б hc1 hs0;constructor<;>linarith lemma qIw {x Rd RQ:ℝ} (hx : |x| ≤ 2^111) (h1 : |Rd-x| ≤ 1/2^53*(|x| + z)) (h2 : |RQ-Rd*Rd| ≤ 1/2^53*(|Rd*Rd| + z)) : |RQ-x^2| ≤ 4/2^53*x^2+1/2^250:=by have nA:=Л x have zA:=Х hz hx nA ב have zz:z*z<=z*1:=Г ד hz0 have nzA:=С hz0 nA have nzz:=С hz0 hz0 have hsq : |x| ^ 2=x^2:=sq_abs x generalize he:Rd-x=e at h1 have q1:=ы (Л e) h1 2 have q3 : |2*x*e| ≤ 2 * |x| * (1/2^53*(|x| + z)):=by rw[в,в,abs_two] exact Г h1 (by positivity) have q6 : |Rd*Rd-x^2| ≤ |2*x*e| + |e| ^ 2:=by rw[show Rd*Rd-x^2=2*x*e+e^2 by rw[<-he];ring,<-abs_pow] exact Ъ _ _ have q8:=qAa (Rd*Rd) (x^2) rw[И (ط x)] at q8 have q9:=qAa (RQ-x^2) (RQ-Rd*Rd) rw[show RQ-x^2-(RQ-Rd*Rd)=Rd*Rd-x^2 by ring] at q9 have nX:=ط x have s1 : |Rd*Rd-x^2| ≤ 201/100/2^53*x^2+1/2^254 :=ד linarith lemma qH9 {n Rv I Rp Rt ep c:ℝ} (Ws tau:ℝ) (hn1:1<=n) (hn2:n<=2^20) (hc:c=(n+8)/2^49) (hWs:0<=Ws) (hep:1/2^126<=ep) (ht0:0<=tau) (ht:tau<=1/2^229) (h1 : |Rv-n*Ws| ≤ (2*n+6)/2^53*(n*Ws)+tau) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (h5 : |Rp-Rv*I| ≤ 1/2^53*(|Rv*I| + z)) (h6 : |Rt-(Rp+ep)| ≤ 1/2^53*(|Rp+ep| + z)) : |Rt-(Ws+ep)| ≤ c/2*(Ws+ep):=by have hep0:0< ep:=н ב hep have hI2:=L_I hn1 h3 h4 have hX:0<=(2*n+6)/2^53*Ws:=С ד hWs have b1:=qF0 n I Rv Ws Ws _ tau h3 h4 (by rw[И hWs]) ד hX have a3:=Г hI2 ht0 have nnW:=С (by linarith:0<=n) hWs have nnE:=С (by linarith:0<=n) hep0.le have nnW2:=Б hn2 hWs have b2:=qAa (Rv*I) Ws rw[И hWs] at b2 have b3:=Ъ Rp ep rw[б hep0] at b3 have b4:=qAa Rp (Rv*I) have b5:=Ъ (Rt-(Rp+ep)) ((Rp-Rv*I)+(Rv*I-Ws)) have b6:=Ъ (Rp-Rv*I) (Rv*I-Ws) rw[show Rt-(Rp+ep)+(Rp-Rv*I+(Rv*I-Ws))=Rt-(Ws+ep) by ring] at b5 rw[hc] linarith lemma qG8 {n W Rv Sq Si:ℝ} (hn1:1<=n) (hn2:n<=2^20) (hW:0<=W) (h1 : |Rv-Sq| ≤ 2*n*(1/2^53*(Si+z))) (h2 : |Sq-W| ≤ 4/2^53*W+n*(1/2^250)) (h3:Si<=(1+4/2^53)*W+n*(1/2^250)) : |Rv-W| ≤ (2*n+6)/2^53*W+1/2^229:=by have hn0:0<=n :=ד have a1:=Г h3 hn0 have a3:=Б hn2 hW have a4:=Х hn2 hn2 hn0 ב have a5:=Х hn2 hz hz0 ב have a6:=abs_sub_le Rv Sq W linarith end lemma qHl {T s c Em lo hi:ℝ} (hT:0< T) (hs:0< s) (hc0:0< c) (hc1:c<=1/2^20) (hE:0<=Em) (h1:T<=s^2*(1+c)) (h2:s^2*(1-c)-Em^2/4<=T) (h3:s*(1+3/2*c)+9/10*Em<=hi) (h4:lo<=s*(1-3/2*c)-9/10*Em) : lo< з T∧з T< hi:=by have hsT:=Real.sqrt_pos.mpr hT;have hq:0< s^2*c:=by positivity constructor . by_cases hl:lo<0;. linarith . rw[not_lt] at hl;rw[Real.lt_sqrt hl];generalize hk:s*(1-3/2*c)=k at h4;have hk0:0<=k-9/10*Em :=ד;have q1:=ы hl h4 2;have q3:=С hE hk0 have q4:k^2<=s^2*(1-3/2*c):=by have h5:(1-3/2*c)^2<=1-3/2*c:=by nlinarith calc k^2=s^2*(1-3/2*c)^2:=by rw[<-hk];ring _<=_:=Г h5 (ط _) have q5:=ط Em;nlinarith . have hh:0< hi:=by have h5:0< s*(1+3/2*c):=by positivity linarith rw[Real.sqrt_lt' hh];have q1:=ы (by positivity) h3 2;have r1:0<=s^2*c^2:=by positivity have r2:0<=s*Em*(1+3/2*c):=by positivity have r3:=ط Em;nlinarith lemma qHp (l:List ℕ) (a:ℕ->ℝ) (c:ℝ) : (l.map λj=>(a j-c)^2).sum =(l.map λj=>a j^2).sum-2*c*(l.map a).sum+l.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 qGh (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[qHp,qHp] field_simp ring lemma qGx (l:List ℕ) (f w:ℕ->ℝ) (u t:ℝ) (hw:∀ j∈l,0<=w j) (h:∀ j∈l,|f j-w j| ≤ u*w j+t) (hu:0<=u) (ht:0<=t) : |(l.map f).sum-(l.map w).sum| ≤ u*(l.map w).sum+l.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 qHb (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[<-qEg a b hs.symm hf,show dsum a b 0 a.size=0 by simpa using hz,finishWord_zero] lemma qHn (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:=qHb (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,qEl,<-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 qIh (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 qHs (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,qE6 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[qHn _ _ _ hf' (hsum.trans hS),qHn k a b hf hS,qIs,Т,g1 k א,g2 k א, qIh _ _ (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⟩:=qEc k a b hf hS;obtain ⟨e,he,hw'⟩:=qEc (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 qGv (A:Array UInt32) (h:A.all finiteWord=true) (t:Nat):expOf A[t]!≠255:=by by_cases ht:t< A.size;. simpa [finiteWord] using qBg A _ h t ht . rw[ـ A t ht];decide lemma qIr {σ: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 qAp hN h a0 א (by rw[hf _ _ a0 א,a0]) lemma qDb {σ: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:=qBc (א:r< R) (ي _) rw[fR] refine ih _ _ א ?_ rw[Nat.succ_mul] exact qIr hN (qAd _ א) h1 (λp u h2 h3=>by rw[hf r p u א h2 h3] show F _ _ _=F _ _ _ rw[h2,е r n,Nat.mul_add_div hn,Nat.mul_add_mod,ؼ h3,М h3,ю]) n 0 o (Т _) H exact qBv (K R 0 _ א ⟨by simp,λq _ (h:q<0*n)=>by omega⟩) λq hq=>hq 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[qDz,qDb (λr c _=>B[c*n+r]!) (by rwa [е]) h5 λr p u hr hp hu=>by have a1:=qCj hu hr;have e1:=qAd n hn;have e2:=qAd r א;have e3:(u*n.toUSize).toNat=u.toNat*n:=by rw[qBa _ _ (by rw[e1];omega),e1] rw[qAg _ _ א,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[<-qHs k (λt=>A[(q/(m*n)*m+q/n%m)*k+t]!) (λt=>B[(q/(m*n)*k+t)*n+q%n]!) h5 (λt _=>⟨qGv A h6 _,qGv 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[Nat.add_mul,ه] 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 qG9 (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,qAg x _ hx,qAf base i א] simp[List.range'_succ] lemma qG2 (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,qAg x _ hx,qAf base i א] simp[List.range'_succ] end lemma qEp (l:List ℕ) (f:ℕ->ℚ) (u:ℕ->ℝ) (c:ℚ) (h:∀ j∈l,(f j:ℝ)=u j) : ((l.foldl (λt j=>t+f j) c:ℚ):ℝ)=c+(l.map u).sum:=by induction l generalizing c with | nil=>simp | cons j l ih => simp only[List.foldl_cons,List.map_cons,List.sum_cons];rw[ih _ λk hk=>h k (م _ hk),<-h j Դ];push_cast;ring lemma qIb (w: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[qA7 w hg,if_neg א,one_mul];have h3:=qA6 528482304 (w.toNat%2^31) א א;have:A_mag 528482304=1/2^64:=by unfold A_mag;norm_num rwa [this] at h3 lemma qH3 (w:UInt32) (hg:good w=true) (h0:0< wv w):1/2^126<=wv w:=by have hn:=qAz w hg;rw[qA7 w hg] at h0 ⊢;rcases hn with hn | ⟨hn,-⟩;. rw[hn] at h0;simp[A_mag] at h0 . have hs:¬ 2^31<=w.toNat:=λhc=>by rw[if_pos hc] at h0;linarith [qAs (w.toNat%2^31)] rw[if_neg hs,one_mul];have h3:=qA6 (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 qIi (n:ℕ) (hn:n<2^20) : ((n.toUInt64+8).toFloat*О ь).isFinite=true ∧ R ((n.toUInt64+8).toFloat*О ь)=((n:ℝ)+8)/2^49:=by have ht:(n.toUInt64+8).toNat=n+8:=by have h8:(8:UInt64).toNat=8:=rfl simp[UInt64.toNat_add,h8] omega have h1:=qCk (n.toUInt64+8) (by rw[ht];omega) obtain ⟨h2,h3⟩:=L_k ь 974 49 ב rfl (by rfl) have h4:(n:ℝ)+8<=2^21:=by exact_mod_cast (א:n+8<=2^21) obtain ⟨hf,he⟩:=qEd _ _ h1.1 h2 (by rw[h1.2,h3,ht] refine ش (Х (qAt ?_) (qAt (by rw[И (by positivity)];norm_num)) (Л _) qI1.le) qDe rw[Nat.cast_add,И (by positivity)] push_cast linarith) refine ⟨hf,?_⟩ rw[he,h1.2,h3,ht] have:=qA2 ((n:ℤ)+8) (-49) א ב push_cast at this ⊢ rw[show (2:ℝ)^(-49:ℤ)=1/2^49 by norm_num] at this rw[this] ring lemma qD7 {r x B:ℝ} (e : |r-x| ≤ 1/2^53*(|x| + A_z)) (h : |x| ≤ B) : |r| ≤ 9/8*B+A_z:=by linarith [qAa r x,Л x,qAr] section variable (x y: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 qAv : (x+y).isFinite=true∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) ∧ |R (x+y)| ≤ 9/8*(B+B)+A_z:=by obtain ⟨f,e⟩:=qBf x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hB)) exact ⟨f,e,qD7 e ((Ъ _ _).trans (add_le_add h1 h2))⟩ lemma qCg : (x-y).isFinite=true∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) ∧ |R (x-y)| ≤ 9/8*(B+B)+A_z:=by obtain ⟨f,e⟩:=qDf x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hB)) exact ⟨f,e,qD7 e ((ى _ _).trans (add_le_add h1 h2))⟩ end lemma qAl (x y: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⟩:=qCf x y hx hy (h1.trans (qAt hB)) (h2.trans (qAt hC)) exact ⟨f,e,qD7 e (в (R x) (R y) ▸ Х h1 h2 (Л _) ((Л _).trans h1))⟩ lemma qFy (a b: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:=qEj _ 1 1222 h.1 (by decide);obtain ⟨r3,r4⟩:972<=_∧_<972+2:=qEj _ 972 2 h.2 (by decide);obtain ⟨f1,l1,u1⟩:=qCz rfl r1 א;have hp:0< R a:=н (х ב _) l1;have ha2:R a<=2^200:=u1.le.trans ((Ц ב א).trans_eq (by norm_num:(2:ℝ)^(200:ℤ)=2^200));obtain ⟨k1,k2⟩:=L_k 0x3FF0000000000000 1023 0 ב rfl (by rfl);obtain ⟨k3,k4⟩:=L_k 0x3CD0000000000000 973 50 ב rfl (by rfl);rw[pow_zero,div_one] at k2;have hz1:=qBo;obtain ⟨f2,g1,q1⟩:=qAl a b (2^200) (2^99) f1 hb (by rwa [б hp]) hb2;obtain ⟨f3,g2⟩:=qDf _ _ f2 k1 (qAt ד) (qAt (by rw[k2];norm_num));rw[k2] at g2;have q3:=ى (R (a*b)) 1;rw[abs_one] at q3;have q2:=qD7 g2 q3;obtain ⟨f4,g3⟩:=qBf _ _ f3 k3 (qAt ד) (qAt (by rw[k4];norm_num));rw[k4] at g3;obtain ⟨-,l2,u2⟩:=qCz rfl (ش ב r3) א;refine ⟨f1,hp,ha2,qD9 qAr.le qAw g1 g2 g3 ?_ ?_⟩;. exact (by norm_num:(1/2^51:ℝ)=2^(-51:ℤ)).trans_le ((Ц ב א).trans l2) . exact u2.le.trans ((Ц ב א).trans_eq (by norm_num:(2:ℝ)^(-49:ℤ)=1/2^49)) lemma qDr (n:ℕ) (f:ℕ->ℝ) (B:ℝ) (h:∀ j,j< n->f j<=B) : ((З n).map f).sum<=n*B:=by simpa using List.sum_le_length_nsmul ((List.range n).map f) B (by simpa using λj hj=>h j hj) lemma qFm (n:ℕ) (hn:n<2^20) (f:ℕ->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:=qBz ((З n).map f) (λq hq=>by obtain ⟨j,hj,rfl⟩:=List.mem_map.1 hq exact (hf j (Р.1 hj)).1) (by simp;omega) (by rw[A_sa,e1];exact hb) rwa [A_sa,A_sr,e1,e2,List.length_map,List.length_range] at this lemma qGp (n:ℕ) (hn:n<2^20) (hn0:0< n) (g:ℕ->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:=qAr.le;have hzl:=qAw;have hzb:=qBo have hq:∀ j,j< n->((g j-m)*(g j-m)).isFinite=true ∧ |R ((g j-m)*(g j-m))-(R (g j)-R m)^2| ≤ 4/2^53*(R (g j)-R m)^2+1/2^250∧(R (g j)-R m)^2<=2^222:=by intro j hj obtain ⟨f,b⟩:=hg j hj have B1 : |R (g j)| ≤ 2^110:=b.trans ב obtain ⟨f1,e1,b1⟩:=qCg (g j) m (2^110) f hm B1 hm2 have b2 : |R (g j-m)| ≤ 2^112 :=ד obtain ⟨f2,e2,-⟩:=qAl _ _ (2^112) (2^112) f1 f1 b2 b2 have hx : |R (g j)-R m| ≤ 2^111:=(ى _ _).trans ד refine ⟨f2,qIw hz0 hzl hx e1 e2,?_⟩ have:=ы (Л _) hx 2 rw[sq_abs] at this exact this.trans ב obtain ⟨a1,a2,a3⟩:=qGx (З n) (λj=>R ((g j-m)*(g j-m))) (λj=>(R (g j)-R m)^2) (4/2^53) (1/2^250) (λj _=>ط _) (λj hj=>(hq j (Р.1 hj)).2.1) ב ב;simp only[List.length_range] at a1 a2;rw[<-hW] at a1 a2 a3;have hW':W<=n*2^222:=hW ▸ qDr n _ _ λj h=>(hq j h).2.2;have hnR:(n:ℝ)<=2^20:=by exact_mod_cast hn.le have hnR1:(1:ℝ)<=n:=by exact_mod_cast hn0 have h1:(n:ℝ)*2^222<=2^20*2^222:=by gcongr have h2:(n:ℝ)*(1/2^250)<=2^20*(1/2^250):=by gcongr obtain ⟨f2,e2⟩:=qFm n hn _ _ (λj=>|R ((g j-m)*(g j-m))|) (λj h=>⟨(hq j h).1,rfl,rfl⟩) (by refine ش a2 ?_;have:=qAt (k:=2^256*2^44) le_rfl linarith) rw[<-hv] at e2 f2;exact ⟨f2,qG8 hz0 hzl hnR1 hnR a3 e2 a1 a2,hW',a3⟩ lemma qF2 (l:List ℕ) (f:ℕ->ℝ) : |(l.map f).sum| ≤ (l.map λj=>|f j|).sum:=by induction l with | nil=>simp | cons j l ih => simp only[List.map_cons,List.sum_cons];exact (Ъ _ _).trans (add_le_add le_rfl ih) def L_oF (s:LnS) (M:ℝ):Prop := s.fast=true->s.m.isFinite=true∧s.ir.isFinite=true∧s.em.isFinite=true ∧ R s.em<=2^120∧1/2^201<=R s.em∧2 * |M-R s.m| ≤ R s.em ∧ |R s.m| ≤ 2^110∧good s.den=true∧1/2^64<=wv s.den ∧ |R s.ir*wv s.den-1| ≤ 1/2^48 def L_ok (b: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 qHi (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,qEp _ _ (λj=>R (g j)) 0 λj hj=>(h j (Р.1 hj)).symm];simp . rw[rowVariance,Rat.cast_div,qEp _ _ (λj=>(R (g j)-(q:ℝ))^2) 0 λj hj=>by rw[h j (Р.1 hj)];push_cast;rfl] simp section variable (n:ℕ) (hnR1:(1:ℝ)<=n) (hnR2:(n:ℝ)<=2^20) include n hnR1 hnR2 lemma qG1 (Fs Ft ind nf m em: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:=qAr.le have hzb:=qBo have hc0:0< c:=by rw[hc];positivity have hc1:c<=1/2^20:=by rw[hc];linarith have bI : |R ind| ≤ 2:=by rw[б pi];exact L_I hnR1 pi ei have hSa0:0<=Sa:=(Л _).trans o3 have hSb:=Б hnR2 (by norm_num:(0:ℝ)<=2^99) have o7:=Х hnR2 (by linarith:Sa+A_z<=2^119+1) ד ב have bs : |R Fs| ≤ 2^120:=by linarith [qAa (R Fs) S] have bt : |R Ft| ≤ 2^120:=by have:=qAa (R Ft) Sa rw[И hSa0] at this linarith have bnf : |R nf| ≤ 1:=by rw[Rnf,б hc0];linarith obtain ⟨fm,em1,-⟩:=qAl Fs ind (2^120) 2 o1 fi bs bI rw[hm] at fm em1 obtain ⟨fN,emN,bN⟩:=qAl nf Ft 1 (2^120) fnf o2 bnf bt rw[Rnf] at emN obtain ⟨fY,emY,bY⟩:=qAl (nf*Ft) ind (2^122) 2 fN fi ד bI obtain ⟨k1,k2⟩:=L_k ظ 823 200 ב rfl (by rfl) obtain ⟨fE,emE,-⟩:=qAv (nf*Ft*ind) _ (2^125) fY k1 ד (by rw[k2];norm_num) rw[k2,hem] at emE rw[hem] at fE have hnpos:(0:ℝ)< n :=ד have hnp:=hnpos.ne' have eS:(n:ℝ)*(S/n)=S:=by field_simp have eA:(n:ℝ)*(Sa/n)=Sa:=by field_simp obtain ⟨m1,m2,m3,m4⟩:=qG3 hz0 qAw (S/n) (Sa/n) hnR1 hnR2 hc (by rw[abs_div,б hnpos];exact div_le_div_of_nonneg_right o3 hnpos.le) (by rw[div_le_iff₀ hnpos];linarith) (by rw[eS,eA];exact o5) (by rw[eA];exact o6) pi ei em1 emN emY emE exact ⟨fm,fE,m2,m3,m1,m4⟩ lemma qIv (v ind tt: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:=qBo;have bI : |R ind| ≤ 2:=by rw[б pi];exact L_I hnR1 pi ei have bW:=Б hnR2 (by norm_num:(0:ℝ)<=2^222);have bv : |R v| ≤ 2^250:=by have a1:=qAa (R v) W;rw[И hW0] at a1;have a3:=Б hnR2 hW0;linarith obtain ⟨fp,ep1,bp⟩:=qAl v ind (2^250) 2 p1 fi bv bI;obtain ⟨de1,de2⟩:=qAm eps gE;obtain ⟨ft,et,-⟩:=qAv (v*ind) (dv eps) (2^253) fp de1 ד (by rw[de2];exact bE.trans ב);rw[de2,htt] at et;rw[htt] at ft;exact ⟨ft,qH9 qAr.le qAw (W/n) (1/2^229) hnR1 hnR2 hc (div_nonneg hW0 hnpos.le) (qH3 eps gE hep0) ב le_rfl (by rw[show (n:ℝ)*(W/n)=W by field_simp];exact p2) pi ei ep1 et⟩ end lemma qIk (tt nf em: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:=qBo have hc0:0< c:=н ב hc2 obtain ⟨-,-,u1⟩:=qCz hEe hE1 א obtain ⟨fd,ptt,ed⟩:=qGk hEe hE1 א generalize м tt=d at fd ed ⊢ have ht2:R tt<=2^200:=u1.le.trans ((Ц ב א).trans_eq (by norm_num:(2:ℝ)^(200:ℤ)=2^200)) generalize hs':з (R tt)=s' at ed have hs0:0< s':=by rw[<-hs'];exact Real.sqrt_pos.mpr ptt have hsq:s'^2=R tt:=by rw[<-hs'];exact Real.sq_sqrt ptt.le have hs1:s'<=2^100:=by rw[<-hs',Real.sqrt_le_iff] exact ⟨by norm_num,ht2.trans ב⟩ have bd : |R d| ≤ 2^102:=by have a1:=qAa (R d) s' rw[б hs0] at a1 linarith have bnf : |R nf| ≤ 1:=by rw[Rnf,б hc0];linarith obtain ⟨f2n,e2n,b2n⟩:=qAv nf nf 1 fnf fnf bnf bnf rw[Rnf] at e2n obtain ⟨fmu,emu,bmu⟩:=qAl (nf+nf) d 4 (2^102) f2n fd ד bd have hEm0:0<=R em:=ش ב m3 have bEm : |R em| ≤ 2^120:=by rw[И hEm0];exact m2 obtain ⟨fe,ee,be⟩:=qAv ((nf+nf)*d) em (2^120) fmu fE ד bEm obtain ⟨fh,eh,-⟩:=qAv d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨fl,el,-⟩:=qCg d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨hhi,hlo⟩:=qHr qAr.le qAw hs0.le (hs1.trans ב) hc2 hc1 m3 m2 ed e2n emu ee eh el obtain ⟨b1,b2⟩:=abs_le.mp hq1 have hδ2:δ^2<=R em^2/4:=by have a1:(2 * |δ|)^2<=R em^2:=ы (by positivity) m1 2 rw[mul_pow,sq_abs] at a1 linarith have nP:0<=c*P:=(mul_pos hc0 hP0).le have a3:=Б (by linarith:c<=1) nP have a4:=С hc0.le nP have nd2:=ط δ have h1':T<=s'^2*(1+c):=by rw[hsq,hT] have a1:=Б (by linarith:P-c/2*P<=R tt) (by linarith:0<=1+c) linarith have h2':s'^2*(1-c)-R em^2/4<=T:=by rw[hsq,hT] have a1:=Б (by linarith:R tt<=P+c/2*P) (by linarith:0<=1-c) linarith obtain ⟨l1,l2⟩:=qHl hT0 hs0 hc0 hc1 hEm0 h1' h2' hhi hlo refine ⟨l1,l2,?_⟩ rw[Real.sqrt_lt' (by positivity),show ((2:ℝ)^(127:ℤ))^2=2^254 by norm_num] have a1:=Х (hsq.trans_le ht2) (by linarith:1+c<=2) ד ב linarith lemma qE1 (b: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⟩:=qA1 _ (qBg x sm hx (r*n+j) א);obtain ⟨a3,a4⟩:=qAm _ a1;rw[<-hgd];beta_reduce;rw[a4];exact ⟨a3,a2,qAx _ (qAe _ a1)⟩ have hnR1:(1:ℝ)<=n:=by exact_mod_cast hn0 have hnR2:(n:ℝ)<=2^20:=by exact_mod_cast hn.le have o4:((З n).map λj=>|R (g j)|).sum<=n*2^99 :=qDr n _ _ λj h=>(hg0 j h).2.1;have hA2:((З n).map λj=>|R (g j)|).sum<=A_B/4:=by have:=qAt (k:=2^256*2^44) le_rfl;have:=Б hnR2 (by norm_num:(0:ℝ)<=2^99);linarith obtain ⟨o1,o5⟩:=qFm n hn g (λj=>R (g j)) (λj=>|R (g j)|) (λj h=>⟨(hg0 j h).1,rfl,rfl⟩) hA2 obtain ⟨o2,o6⟩:=qFm n hn (λj=>(g j).abs) (λj=>|R (g j)|) (λj=>|R (g j)|) (λj h=>by obtain ⟨p,q⟩:=qBl _ (hg0 j h).1 exact ⟨p,q,abs_abs _⟩) hA2 have o3:=qF2 (З n) λj=>R (g j);obtain ⟨hMc,hVc⟩:=qHi n r x g (λj h=>(hg0 j h).2.2) (lnQ b n x r);generalize hQ:((lnQ b n x r:ℚ):ℝ)=Q at hVc;generalize hS:((З n).map λj=>R (g j)).sum=S at o3 o5 hMc;generalize ((З n).map λj=>|R (g j)|).sum=Sa at o3 o4 o5 o6;unfold lnStat;dsimp only;generalize hnu:n.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:=qBo;have hz0:=qAr.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 ▸ qIi n hn;obtain ⟨c,hc⟩:∃ c:ℝ,c=((n:ℝ)+8)/2^49:=⟨_,rfl⟩;rw[<-hc] at Rnf;have hc1:c<=1/2^20:=by rw[hc];linarith have hc2:9/2^49<=c:=by rw[hc];linarith obtain ⟨fnd,Rnd⟩:nd.isFinite=true∧R nd=n:=by rw[<-hnd];have e:n.toUInt64.toNat=n:=by simp;omega have:=qCk n.toUInt64 א;rwa [e] at this obtain ⟨fi,pi,-,ei⟩:=qFy ind nd fnd (by rw[Rnd,И ד];exact hnR2.trans ב) c5 rw[Rnd] at ei;obtain ⟨z1,z2⟩:=qHg obtain ⟨fm,fE,m2,m3,m1,m4⟩:m.isFinite=true∧em.isFinite=true∧R em<=2^120 ∧ 1/2^201<=R em∧2 * |Q-R m| ≤ R em∧|R m| ≤ 2^110:=by cases b with | true => have e1:a=F1.mk 0 0:=ha.symm;have e2:m=0:=hm.symm;have e3:Q=0:=by rw[<-hQ];simp[lnQ] subst e1;obtain ⟨-,fE,m2,m3,-,-⟩:=qG1 n hnR1 hnR2 0 0 ind nf _ em 0 0 c hc z1 z1 ג (by positivity) (by rw[z2];simp;positivity) (by rw[z2];simp;positivity) fnf Rnf fi pi ei rfl hem;rw[e2,e3,z2];refine ⟨z1,fE,m2,m3,?_,by norm_num⟩;simp only[sub_zero,abs_zero,mul_zero];exact ش ב m3 | false => replace ha:lnSum x base nu 0 0 0=a:=ha;replace hm:a.s*ind=m:=hm;have hA:a=⟨A_fsum ((З n).map g),A_fsum ((З n).map λj=>(g j).abs)⟩:=by rw[<-ha,qG9 x hxs base nu א n 0 0 0 (by simp[hu])];simp only[USize.toNat_zero,hb,<-hgd,A_fsum,List.range_eq_range'] subst hA;rw[hQ.symm.trans hMc];exact qG1 n hnR1 hnR2 _ _ ind nf m em S Sa c hc o1 o2 o3 o4 o5 o6 fnf Rnf fi pi ei hm hem obtain ⟨p1,p2,p3,hW0⟩:=qGp n hn hn0 g m v _ (by rw[<-hv,qG2 x hxs base nu m א n 0 0 (by simp[hu])] simp only[USize.toNat_zero,hb,<-hgd,A_fsum,List.range_eq_range']) rfl (λj h=>⟨(hg0 j h).1,(hg0 j h).2.1⟩) fm m4 generalize hW:((З n).map λj=>(R (g j)-R m)^2).sum=W at p2 p3 hW0;generalize hV0:((З n).map λj=>(R (g j)-Q)^2).sum=V0 at hVc;generalize hδ:Q-R m=δ at m1;have hnpos:(0:ℝ)< n :=ד have hWs:W/n=V0/n+δ^2:=by cases b with | true => have e2:m=0:=hm.symm;have e3:Q=0:=by rw[<-hQ];simp[lnQ] rw[<-hδ,<-hW,<-hV0,e2,e3,z2];simp | false => have hs2:=qGh (З n) (λj=>R (g j)) (R m) (by simpa using hnp);simp only[List.length_range] at hs2;rw[hS,hW,show S/n=Q from (hQ.symm.trans hMc).symm,hV0,hδ] at hs2;rw[hs2];field_simp obtain ⟨gE,bE⟩:=qA1 eps he;have epsv:wv eps=(Д eps:ℝ):=qAx _ (qAe _ gE);have hep0:0< wv eps:=by rw[epsv];exact_mod_cast he0 obtain ⟨ft,hq1⟩:=qIv n hnR1 hnR2 v ind tt eps W c hc p1 p2 p3 hW0 fi pi ei gE bE hep0 htt;generalize hq:rowVariance n x r (lnQ b n x r)+Д eps=q;have hV1:0<=V0/n:=by rw[<-hV0];refine div_nonneg (List.sum_nonneg λy hy=>?_) hnpos.le;obtain ⟨j,-,rfl⟩:=List.mem_map.1 hy;exact ط _ have hT:(q:ℝ)=(W/n+wv eps)-δ^2:=by rw[<-hq,Rat.cast_add,hVc,epsv,hWs];ring have hT0:(0:ℝ)< q:=by rw[hT,hWs];linarith have hP0:0< W/n+wv eps:=by have:=div_nonneg hW0 hnpos.le;linarith obtain ⟨r1,r2⟩:1<=_∧_<1+1222:=qEj _ 1 1222 c4 (by decide);obtain ⟨l1,l2,hb127⟩:=qIk tt nf em _ r1 א rfl c _ δ (q:ℝ) hc1 hc2 fnf Rnf fE m2 m3 hP0 hq1 m1 hT hT0;rw[hd] at l1 l2;have hq0:0< q:=by exact_mod_cast hT0 have hzne:ziv d ((nf+nf)*d+em)≠Е:=by rw[hz];intro e0;rw[e0] at c3;exact absurd c3 (by decide) have hden:=((qGs d _ q hq0 l1 l2 hzne hb127).trans hz).symm;rw[<-hq] at hden;obtain ⟨gZ,bZ⟩:=qA1 z c1;obtain ⟨dz1,dz2⟩:=qAm z gZ;obtain ⟨fi2,-,-,ei2⟩:=qFy ir (dv z) dz1 (by rw[dz2];exact bZ) c6;rw[dz2] at ei2;refine ⟨hden,λ_=>⟨fm,fi2,fE,m2,m3,?_,m4,gZ,qIb z gZ c2 c3,ei2⟩⟩;dsimp only;rw[hQ,hδ];exact m1 . exact ⟨rfl,by simp[L_oF]⟩ lemma qI0 (m ir em: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:=qBo obtain ⟨gw1,bw1⟩:=qA1 w hw obtain ⟨gg1,bg1⟩:=qA1 gw hg obtain ⟨gb1,bb1⟩:=qA1 bw hb obtain ⟨fw,Rw⟩:=qAm w gw1 obtain ⟨fg,Rg⟩:=qAm gw gg1 obtain ⟨fb,Rb⟩:=qAm bw gb1 have hD0:0< D:=н ב hD have bI : |R ir| ≤ 2^65:=by have a1:=qAa (R ir*D) 1 rw[abs_one,в,б hD0] at a1 have a2:=Г hD (Л (R ir)) linarith obtain ⟨f1,h1,b1⟩:=qCg (dv w) m (2^111) fw fm (by rw[Rw];exact bw1.trans ב) (e4.trans ב) rw[Rw] at h1 obtain ⟨f2,h3,b2⟩:=qAl (dv gw) ir (2^99) (2^65) fg fi (by rw[Rg];exact bg1) bI rw[Rg] at h3 obtain ⟨f3,h4,b3⟩:=qAl _ _ (2^113) (2^166) f1 f2 ד ד generalize hQ:(dv w-m)*(dv gw*ir)=Q at hs he f3 h4 b3 generalize dv gw*ir=G at he f2 h3 b2 h4 hQ obtain ⟨f4,h5,b4⟩:=qAv Q (dv bw) (2^250*2^31) f3 fb (by norm_num at b3 hzb ⊢;linarith) (by rw[Rb];exact bb1.trans ב) rw[Rb,<-hs] at h5 rw[<-hs] at f4 b4 obtain ⟨fa1,Ra1⟩:=qBl Q f3 obtain ⟨fa2,Ra2⟩:=qBl (dv bw) fb rw[Rb] at Ra2 obtain ⟨fa3,Ra3⟩:=qBl G f2 obtain ⟨f5,h6,b5⟩:=qAv Q.abs (dv bw).abs (2^250*2^31) fa1 fa2 (by rw[Ra1,abs_abs];norm_num at b3 hzb ⊢;linarith) (by rw[Ra2,abs_abs];exact bb1.trans ב) rw[Ra1,Ra2] at h6 obtain ⟨k1,k2⟩:=L_k 0x3D10000000000000 977 46 ב rfl (by rfl) obtain ⟨f6,h7,b6⟩:=qAl _ _ 1 (2^250*2^33) k1 f5 (by rw[k2];norm_num) (by norm_num at b5 hzb ⊢;linarith) rw[k2] at h7 have bE : |R em| ≤ 2^120:=by rw[И (ش ב e2)];exact e1 obtain ⟨f7,h8,b7⟩:=qAl em G.abs (2^120) (2^166) fe fa3 bE (by rw[Ra3,abs_abs];linarith) rw[Ra3] at h8 obtain ⟨f8,h9,b8⟩:=qAv _ _ (2^250*2^38) f6 f7 (by norm_num at b6 hzb ⊢;linarith) (by norm_num at b7 hzb ⊢;linarith) rw[<-he] at f8 h9 b8 obtain ⟨f9,h10,-⟩:=qAv s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨f10,h11,-⟩:=qCg s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨-,-,big⟩:=qBw s e hne rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at big exact qG0 qAr.le qAw (ش ב e2) e1 e3 hD bw1 bg1 e4 h1 hI h3 h4 h5 h6 h7 h8 h9 h10 h11 big lemma qEk (s:LnS) (M:ℚ) (mean:Unit->ℚ) (hmean:mean ()=M) (w gw bw: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⟩:=qI0 s.m s.ir s.em w gw bw M (wv s.den) fm fi fe e1 e2 e3 e4 hD hI hw hg hb _ _ rfl rfl hne have ew:=qAx w (qAe _ (qA1 w hw).1);have eg:=qAx gw (qAe _ (qA1 gw hg).1);have eb:=qAx bw (qAe _ (qA1 bw hb).1);have ed:=qAx s.den (qAe _ gd);have hV:(((Д w-M)*Д gw/Д s.den+Д bw:ℚ):ℝ) =(wv w-M)*wv gw/wv s.den+wv bw:=by rw[ew,eg,eb,ed];push_cast;ring rw[<-hV] at l1 l2;exact (qH7 _ _ _ l1 l2 hne (qIu _ _ _ l1 l2 hne hc)).symm . unfold lnSlow;rw[hmean] lemma qBp {α:Type} [Inhabited α] (rows k:ℕ) (f:ℕ->α) (hk:k< rows) : (Array.ofFn (n:=rows) λr=>f r.val)[k]! = f k:=by rw[Я _ k (by simpa using hk),Array.getElem_ofFn] lemma qGc: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[qDb (λ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[qAg _ _ א]] apply Array.ext ג;intro q h1 h1';simp only[Array.getElem_ofFn,Array.size_ofFn] at h1 h1' ⊢;have hr:q/n< rows:=ш (by rwa [е]);have hj:q%n< n:=ر _ h11;have e1:=qBp rows (q/n) (λr=>lnStat false n eps x r) hr;have e2:=qBp rows (q/n) (λr=>rowMean n x r) hr have e3:=qBp rows (q/n) (λr=>sqrtRational (rowVariance n x r ((Array.ofFn (n:=rows) λr=>rowMean n x r.val)[r]!)+Д eps)) hr beta_reduce at e1 e2 e3;rw[e1,e3,e2];obtain ⟨o1,o2⟩:=qE1 false rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qEk _ (rowMean n x (q/n)) _ rfl _ _ _ (qBg x sm h4 q א) (qBg g sm h5 _ א) (qBg b sm h6 _ א) o2,o1];rfl . rfl lemma qFf:rmsF=rmsnormSpec:=by funext rows n eps x g;unfold rmsF;split . rename_i hc;simp only[Bool.and_eq_true,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨hD,h4⟩,h5⟩,h7⟩,h8⟩,h9⟩:=hc;unfold rmsnormSpec rmsnormWith;rw[if_pos hD];unfold rmsnormStable;dsimp only;unfold stableNormDomain at hD;simp only[Bool.and_eq_true,beq_iff_eq,gt_iff_lt,decide_eq_true_eq] at hD;obtain ⟨⟨⟨⟨⟨⟨⟨h10,h11⟩,h12⟩,h13⟩,-⟩,-⟩,-⟩,h14⟩:=hD rw[qDb (λr c q=>if x[q]! &&& В == 0 then 0 else lnOut (Array.ofFn (n:=rows) λr=>lnStat true n eps x r.val)[r]! x[q]! g[c]! 0 λ_=>0) h9 h11 λr p u hr hp hu=>by repeat rw[qAg _ _ א]] apply Array.ext ג;intro q h1 h1';simp only[Array.getElem_ofFn,Array.size_ofFn] at h1 h1' ⊢;have hr:q/n< rows:=ш (by rwa [е]);have hj:q%n< n:=ر _ h11;have e1:=qBp rows (q/n) (λr=>lnStat true n eps x r) hr;have e3:=qBp rows (q/n) (λr=>sqrtRational (rowVariance n x r 0+Д eps)) hr;beta_reduce at e1 e3;rw[e3];have hw:=qBg x sm h4 q א;split;. rename_i h0;rw[qE6 _ (qA1 _ hw).1 (by simpa using h0),zero_mul,zero_div];rfl . rw[e1];obtain ⟨o1,o2⟩:=qE1 true rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qEk _ 0 _ rfl _ _ _ hw (qBg g sm h5 _ א) (by decide) o2,o1,qE6 0 (by decide) rfl,sub_zero,add_zero];rfl . rfl end def kernels:Kernels where core:=specCore dense:=⟨linF,gemmF⟩ conv:=specConv elem:=⟨avgpoolSpec,maxpoolSpec,addF,scaleShiftSpec⟩ norm:=⟨rmsF,lnF⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specDense,specElem,specNorm,avgpoolSpec,qDz,qEw,qGc,qFf,qFi,qAi] end Submission