import ContractExact open Nat renaming add_assoc -> GB open Bool renaming false_eq_true -> GC open USize renaming ofNat -> GD open FloatLib.Floats.Formats.BinaryInterchange.Model renaming isFinite -> GE open FloatLib.Floats.Formats.BinaryInterchange.Model renaming roundDyadic -> GF open FloatLib.Floats.Formats.BinaryInterchange.Model renaming toReal -> GG open Float.Model.Format renaming binary64 -> GH open Array renaming replicate -> GI open Array renaming ofFn -> GJ open FloatArray renaming emptyWithCapacity -> GK open Nat renaming shiftRight_eq_div_pow -> GM open FloatLib.Floats.Formats.BinaryInterchange.Model renaming isNaN_eq_false_of_toDyadic?_some -> GN open UInt64 renaming toNat_sub_of_le -> GO open Bool renaming and_eq_true -> GQ open Finset renaming range -> GR open FloatLib.Floats.Formats.BinaryInterchange.Model renaming toModel_ofFloatModel -> GS open Float renaming ofBits -> GT open FloatLib.Floats.Formats.BinaryInterchange.Model renaming toModel -> GU open FloatLib.Floats.Formats.BinaryInterchange.Model renaming ofModel -> GV open FloatLib.Floats.Formats.BinaryInterchange.Model renaming ofUInt32 -> GW open Bool renaming or_eq_true -> GX open List renaming range -> GY open List renaming foldl_cons -> GZ open Nat renaming add_zero -> Ga open UInt32 renaming toNat_zero -> Gb namespace Submission local notation "ZQ" => UInt32 local notation "ZR" => UInt64 local notation "ZS" => Array local notation "ZT" => Bool local notation "ZU" => Nat local notation "ZV" => FloatArray local notation "ZW" => Float local notation "ZX" => USize local notation "ZY" => Int local notation "ZZ" => Real local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith local notation"А"=>USize.lt_iff_toNat_lt local notation"Б"=>mul_le_mul_of_nonneg_right local notation"В"=>Nat.and_two_pow_sub_one_eq_mod local notation"Г"=>Nat.mod_eq_of_lt local notation"Д"=>ite_eq_right local notation"Е"=>getElem!_pos local notation"Ж"=>UInt32.le_iff_toNat_le local notation"З"=>Nat.mul_le_mul_right local notation"И"=>UInt32.toNat_inj local notation"Й"=>mul_le_mul_of_nonneg_left local notation"К"=>Nat.zero_add local notation"Л"=>ite_eq_left local notation"М"=>zpow_add₀ local notation"Н"=>Nat.pos_of_ne_zero local notation"О"=>UInt32.toNat_and local notation"П"=>abs_nonneg local notation"Р"=>dite_eq_left local notation"С"=>two_ne_zero local notation"Т"=>UInt32.toNat_toUSize local notation"У"=>abs_of_nonneg local notation"Ф"=>zpow_le_zpow_right₀ local notation"Х"=>dite_eq_right local notation"Ц"=>zpow_natCast local notation"Ч"=>abs_add_le local notation"Ш"=>mul_lt_mul_of_pos_right local notation"Щ"=>USize.toNat_one local notation"Ъ"=>Nat.mod_lt local notation"Ы"=>UInt64.toNat_add local notation"Ь"=>mul_nonneg local notation"Э"=>Nat.cast_nonneg local notation"Ю"=>GM local notation"Я"=>Ga local notation"а"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"б"=>getElem!_neg local notation"в"=>lt_of_le_of_lt local notation"г"=>mul_le_mul local notation"д"=>Nat.pow_le_pow_right local notation"е"=>UInt32.toNat_ofNat' local notation"ж"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"з"=>GB local notation"и"=>Nat.lt_or_eq_of_le local notation"й"=>Nat.le_mul_of_pos_right local notation"к"=>Nat.pos_of_mul_pos_left local notation"л"=>UInt32.toNat_shiftRight local notation"м"=>UInt64.toNat_shiftRight local notation"н"=>List.mem_range local notation"о"=>Nat.mul_comm local notation"п"=>UInt32.lt_iff_toNat_lt local notation"р"=>UInt64.lt_iff_toNat_lt local notation"с"=>Nat.div_eq_of_lt local notation"т"=>Nat.shiftLeft_eq local notation"у"=>Bool.and_eq_true_iff local notation"ф"=>Nat.add_mul local notation"х"=>Nat.div_lt_of_lt_mul local notation"ц"=>Nat.one_mul local notation"ч"=>Nat.pow_add local notation"ш"=>Nat.lt_succ_iff local notation"щ"=>Nat.two_pow_add_eq_or_of_lt local notation"ъ"=>Array.ext local notation"ы"=>Finset.sum_congr local notation"ь"=>Nat.exists_eq_add_of_le local notation"э"=>UInt64.toNat_and local notation"ю"=>zpow_pos local notation"я"=>lt_of_mul_lt_mul_right local notation"ذ"=>Nat.div_div_eq_div_mul local notation"ر"=>Nat.succ_mul local notation"ز"=>Nat.succ_pos local notation"س"=>UInt32.toNat_sub_of_le local notation"ش"=>List.mem_filter local notation"ص"=>Nat.mod_add_div local notation"ض"=>Array.getElem_push_eq local notation"ط"=>Array.getElem_push_lt local notation"ظ"=>le_trans local notation"ع"=>Rat.cast_zero open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats structure Acc where pos:ZS ZU neg:ZS ZU special:ZT negZero:ZT def Acc.empty:Acc:=⟨GI 18 0,GI 18 0,false,true⟩ def Acc.total (acc:Acc):ZY:=(limbVal acc.pos.toList:ZY)-limbVal acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:ZU) (neg:ZT):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⟩ @[inline] def Acc.markSpecial (acc:Acc):Acc:= match acc with | ⟨pos,ng,_,nz⟩=>⟨pos,ng,true,nz⟩ lemma Acc.add_spec (acc:Acc) (m idx:ZU) (neg:ZT) (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:ZY) 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,GC,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 lemma Acc.total_empty:Acc.empty.total=0:=by decide section open FloatLib.Numerics lemma CS (s:ZT) (m:ZU) (e:ZY) (j:ZU) (h:j+25<=m.log2):GF Fmt ⟨s,shiftRightJam m j,e+j⟩=GF Fmt ⟨s,m,e⟩:=by have hm0:m≠0:=by rintro rfl;simp at h have hpow:2^m.log2<=m:=Nat.log2_self_le hm0;have hm:2^(j+1)<=m:=ظ (д ב א) hpow;have hlog:(shiftRightJam m j).log2=m.log2-j:=log2_shiftRightJam m j hm;have hne:shiftRightJam m j≠0:=shiftRightJam_ne_zero m j (ظ (д ב א) hm);rw[<-Model.roundDyadicGeneral_eq_roundDyadic,<-Model.roundDyadicGeneral_eq_roundDyadic];unfold Model.roundDyadicGeneral;simp only[hne,hm0,beq_iff_eq,ite_false,hlog];have htot:((m.log2-j:ZU):ZY)+(e+j)=(m.log2:ZY)+e:=א;rw[htot];have hfw:Fmt.fracWidth=23:=rfl;have hsub:Fmt.minSubnormalExponent=-149:=rfl;have hmin:Fmt.minNormalExponent=-126:=rfl;rw[hfw,hsub,hmin];split_ifs with h1 h2 h3 h4<;>try omega . rfl . congr 1;obtain ⟨a,ha⟩:∃ a:ZU,e - -149=Int.negSucc a:=⟨(-(e+149)-1).toNat,by omega⟩;have hb:e+j - -149=Int.negSucc (a-j):=א;rw[ha,hb];dsimp only;rw[show a-j+1=(a+1)-j by omega];exact roundShiftRightEven_shiftRightJam m j (a+1) א . congr 1;rw[show m.log2-j-23=(m.log2-23)-j by omega];exact roundShiftRightEven_shiftRightJam m j _ א def winVal (a:ZS ZU) (i:ZU):ZU->ZU | 0=>0 | k+1=>a[i]! + 2^32*winVal a (i+1) k lemma BQ (a:ZS ZU) (i k m:ZU):winVal a i (k+m)=winVal a i k+2^(32*k)*winVal a (i+k) m:=by induction k generalizing i with | zero=>simp[winVal] | succ k ih => rw[show k+1+m=(k+m)+1 by omega,winVal,winVal,ih,show i+1+k=i+(k+1) by omega];rw[show 32*(k+1)=32+32*k by ring,pow_add];ring lemma DA (a b:ZS ZU) (i j k:ZU) (h:∀ t,t< k->a[i+t]! = b[j+t]!):winVal a i k=winVal b j k:=by induction k generalizing i j with | zero=>rfl | succ k ih => rw[winVal,winVal,ih (i+1) (j+1) (λt ht=>by rw[show i+1+t=i+(t+1) by omega,show j+1+t=j+(t+1) by omega] exact h (t+1) א),show a[i]! = b[j]! from by simpa using h 0 א] lemma BH (a:ZS ZU) (i k:ZU):winVal a i k=0 ↔ ∀ t,t< k->a[i+t]! = 0:=by induction k generalizing i with | zero=>simp[winVal] | succ k ih => rw[winVal,Nat.add_eq_zero_iff,Nat.mul_eq_zero,ih (i+1)];have hp:(2:ℕ)^32≠0:=ב;constructor . rintro ⟨h0,h1 | h1⟩ t ht;. exact absurd h1 hp . rcases t with _ | t;. simpa using h0 . rw[show i+(t+1)=i+1+t by omega];exact h1 t א . intro h;refine ⟨by simpa using h 0 א,Or.inr λt ht=>?_⟩;rw[show i+1+t=i+(t+1) by omega];exact h (t+1) א lemma EI (a:ZS ZU) (i k:ZU) (h:∀ t,t< k->a[i+t]! < 2^32):winVal a i k<2^(32*k):=by induction k generalizing i with | zero=>simp[winVal] | succ k ih => have h0:=h 0 א have h1:=ih (i+1) (λt ht=>by rw[show i+1+t=i+(t+1) by omega];exact h (t+1) א) simp only[Ga] at h0;rw[winVal,show 32*(k+1)=32+32*k by ring,pow_add];nlinarith lemma Br (a:ZS ZU) (t:ZU) (h:a.size<=t):a[t]! = 0:=by simp[getElem!_neg a t א] lemma CE (a:ZS ZU) (i k:ZU) (h:a.size<=i+k):winVal a i k=limbVal (a.toList.drop i):=by induction k generalizing i with | zero=>simp[winVal,List.drop_of_length_le (by simpa using h:a.toList.length<=i),limbVal] | succ k ih => rw[winVal,ih (i+1) א];by_cases hi:i< a.size . have hl:i< a.toList.length:=by simpa using hi simp only[List.drop_eq_getElem_cons hl,limbVal,getElem!_pos a i hi,Array.getElem_toList] . rw[Br a i א,List.drop_of_length_le (by simp;omega),List.drop_of_length_le (by simp;omega)];simp[limbVal] lemma Cj (a:ZS ZU) (lo n:ZU) (hlow:∀ t,t< lo->a[t]! = 0) (hhigh:∀ t,lo+n<=t->a[t]! = 0):limbVal a.toList=2^(32*lo)*winVal a lo n:=by rw[<-List.drop_zero (l:=a.toList),<-CE a 0 (lo+(n+a.size)) א,BQ,BQ];rw[(BH a 0 lo).2 (λt ht=>by simpa using hlow t ht),(BH a (0+lo+n) a.size).2 (λt _=>hhigh _ א)];simp def diffGo (a b:ZS ZU) (i ca cb br:ZU) (out:ZS ZU): ZU->ZS ZU × ZU × ZU × ZU | 0=>(out,ca,cb,br) | k+1 => let ta:=a[i]! + ca let tb:=b[i]! + cb let t:=ta%4294967296+4294967296-tb%4294967296-br diffGo a b (i+1) (ta/4294967296) (tb/4294967296) (1-t/4294967296) (out.push (t%4294967296)) k lemma Bh (a b:ZS ZU) (i ca cb br:ZU) (out:ZS ZU) (k:ZU) (hbr:br<=1) (hout:∀ t,t< out.size->out[t]! < 2^32):let r:=diffGo a b i ca cb br out k r.1.size=out.size+k∧(∀ t,t< r.1.size->r.1[t]! < 2^32) ∧ (winVal r.1 0 r.1.size:ZY)+2^(32*(out.size+k))*(r.2.1-r.2.2.1-r.2.2.2:ZY) = winVal out 0 out.size+2^(32*out.size) * ((winVal a i k:ZY)-winVal b i k+ca-cb-br):=by induction k generalizing i ca cb br out with | zero=>exact ⟨by simp[diffGo],by simpa [diffGo] using hout,by simp[diffGo,winVal]⟩ | succ k ih => simp only[diffGo] set ta:=a[i]! + ca set tb:=b[i]! + cb set t:=ta%4294967296+4294967296-tb%4294967296-br have htb:tb%4294967296<4294967296:=Ъ _ ב have hta:ta%4294967296<4294967296:=Ъ _ ב have ht2:t<2*4294967296:=א have hq:t/4294967296<=1:=א have hd:t%4294967296<2^32:=by have:=Ъ t (by norm_num:4294967296 > 0);norm_num;omega have hout':∀ s,s<(out.push (t%4294967296)).size->(out.push (t%4294967296))[s]! < 2^32:=by intro s hs simp only[Array.size_push] at hs rcases и (ш.mp hs) with hs | rfl . rw[Е _ s (by simp;omega),ط hs,<-Е out s hs];exact hout s hs . rw[Е _ _ ג,ض];exact hd obtain ⟨h1,h2,h3⟩:=ih (i+1) (ta/4294967296) (tb/4294967296) (1-t/4294967296) (out.push (t%4294967296)) א hout' refine ⟨by simp[h1];omega,h2,?_⟩ have hpush:winVal (out.push (t%4294967296)) 0 (out.size+1) =winVal out 0 out.size+2^(32*out.size)*(t%4294967296):=by rw[BQ,DA _ out 0 0 out.size (λs hs=>by rw[Е _ _ (by simp;omega),ط א, <-Е out _ א])] simp only[winVal,Nat.zero_add,Nat.mul_zero,Ga] rw[Е _ _ ג,ض] simp only[Array.size_push] at h3 ⊢ rw[show out.size+(k+1)=out.size+1+k by omega] rw[h3,hpush,winVal,winVal] have hstep:((t%4294967296:ZU):ZY)+4294967296*((ta/4294967296:ZU)-(tb/4294967296:ZU)-(1-t/4294967296:ZU)) =(a[i]!:ZY)-b[i]! + ca-cb-br:=by have e1:=ص ta 4294967296 have e2:=ص tb 4294967296 have e3:=ص t 4294967296 have e4:(1-t/4294967296:ZU)=1-(t/4294967296:ZY):=א have e5:(t:ZY)=ta%4294967296+4294967296-tb%4294967296-br:=א push_cast [e4] omega rw[show 32*(out.size+1)=32*out.size+32 by ring,pow_add] simp only[Nat.cast_add,Nat.cast_mul,Nat.cast_pow,Nat.cast_ofNat] linear_combination (2:ZY)^(32*out.size)*hstep def topGo (r:ZS ZU):ZU->Option ZU | 0=>none | k+1=>if r[k]! != 0 then some k else topGo r k def anyGo (r:ZS ZU):ZU->ZT | 0=>false | k+1=>r[k]! != 0 || anyGo r k lemma Dj (r:ZS ZU) (k:ZU) (h:topGo r k=none):∀ t,t< k->r[t]! = 0:=by induction k with | zero=>intro t ht;omega | succ k ih => simp only[topGo] at h;split at h;. cases h . rename_i hk;intro t ht;rcases и (ш.mp ht) with ht | rfl;. exact ih h t ht . simpa using hk lemma Ca (r:ZS ZU) (k h:ZU) (hh:topGo r k=some h):h< k∧r[h]!≠0∧∀ t,h< t->t< k->r[t]! = 0:=by induction k with | zero=>simp[topGo] at hh | succ k ih => simp only[topGo] at hh;split at hh;. rename_i hk;cases hh;exact ⟨by omega,by simpa using hk,λt h1 h2=>by omega⟩ . rename_i hk;obtain ⟨a,b,c⟩:=ih hh;refine ⟨by omega,b,λt h1 h2=>?_⟩;rcases и (ш.mp h2) with h2 | rfl;. exact c t h1 h2 . simpa using hk lemma CG (r:ZS ZU) (k:ZU):anyGo r k=false ↔ ∀ t,t< k->r[t]! = 0:=by induction k with | zero=>simp[anyGo] | succ k ih => simp only[anyGo,Bool.or_eq_false_iff,bne_eq_false_iff_eq,ih];constructor . rintro ⟨h0,h1⟩ t ht;rcases и (ш.mp ht) with ht | rfl;. exact h1 t ht . exact h0 . intro h;exact ⟨h k א,λt ht=>h t א⟩ lemma Ef (r:ZS ZU) (k:ZU):anyGo r k=(winVal r 0 k != 0):=by have hz:=BH r 0 k;simp only[Nat.zero_add] at hz;cases h:anyGo r k;. rw[hz.2 ((CG r k).1 h)];rfl . have:winVal r 0 k≠0:=λh0=>by have:=(CG r k).2 (hz.1 h0);simp_all simp[this] lemma Dp (low x y h:ZU) (hlow:low<2^(32*h)): shiftRightJam (low+2^(32*h)*(x+2^32*y)) (32*h+2) =if x%4 != 0 || low != 0 then (y*1073741824+x/4) ||| 1 else y*1073741824+x/4:=by have hX0:0<2^(32*h):=by positivity have hdiv:2^(32*h+2)=4*2^(32*h):=by rw[pow_add];ring have hs:x%4<4:=Ъ x ב have hA:low+2^(32*h)*(x%4)<4*2^(32*h):=by nlinarith have hsplit:low+2^(32*h)*(x+2^32*y) =(low+2^(32*h)*(x%4))+4*2^(32*h)*(y*1073741824+x/4):=by have h4:=ص x 4 rw[show (2:ZU)^32=4*1073741824 by norm_num] conv_lhs=>rw[<-h4] ring have hz:low+2^(32*h)*(x%4)=0 ↔ ¬(x%4≠0∨low≠0):=by constructor . intro h0;have h1:=Nat.eq_zero_of_add_eq_zero h0;rcases Nat.mul_eq_zero.mp h1.2 with h2 | h2<;>omega . intro h0;simp only[not_or,not_not] at h0;simp[h0.1,h0.2] unfold shiftRightJam dsimp only rw[hdiv,hsplit,Nat.add_mul_div_left _ _ א,с hA,К,Nat.add_mul_mod_self_left,Г hA] by_cases hc:x%4≠0∨low≠0 . have:¬ (low+2^(32*h)*(x%4)=0):=λh0=>hz.mp h0 hc;have hb:(x%4 != 0 || low != 0)=true:=by simpa [bne_iff_ne] using hc simp only[beq_iff_eq,this,ite_false,hb,ite_true] . have:low+2^(32*h)*(x%4)=0:=hz.mpr hc;have hb:(x%4 != 0 || low != 0)=false:=by simpa [bne_iff_ne] using hc simp only[beq_iff_eq,this,ite_true,hb];rfl def finishDigits (neg:ZT) (r:ZS ZU) (e:ZY) (negZero:ZT):ZQ:= match topGo r r.size with | none=>finishWord 0 e negZero | some 0=>finishWord (if neg then -(r[0]!:ZY) else r[0]!) e negZero | some (h+1) => let x:=r[h]! let v:=r[h+1]! * 1073741824+x/4 let m:=if x%4 != 0 || anyGo r h then v ||| 1 else v toWord (GF Fmt ⟨neg,m,e+((32*h+2:ZU):ZY)⟩) lemma CD (neg:ZT) (R:ZU) (e:ZY) (negZero:ZT) (hR:R≠0):finishWord (if neg then -(R:ZY) else R) e negZero=toWord (GF Fmt ⟨neg,R,e⟩):=by have hpos:(0:ZY)< R:=by exact_mod_cast Н hR cases neg<;>simp[finishWord,ofIntDyadic,hR,not_lt.mpr hpos.le,Nat.pos_of_ne_zero hR] lemma Bu (neg:ZT) (r:ZS ZU) (e:ZY) (negZero:ZT) (hr:∀ t,t< r.size->r[t]! < 2^32): finishDigits neg r e negZero =finishWord (if neg then -(winVal r 0 r.size:ZY) else winVal r 0 r.size) e negZero:=by unfold finishDigits split . rename_i hnone;rw[(BH r 0 r.size).2 (λt ht=>by simpa using Dj r _ hnone t ht)];simp . rename_i hsome;obtain ⟨h1,h2,h3⟩:=Ca r _ 0 hsome;rw[show r.size=1+(r.size-1) by omega,BQ,(BH r (0+1) (r.size-1)).2 (λt ht=>h3 _ א א)];simp[winVal] . rename_i h hsome;obtain ⟨h1,h2,h3⟩:=Ca r _ (h+1) hsome;have hR:winVal r 0 r.size=winVal r 0 h+2^(32*h)*(r[h]! + 2^32*r[h+1]!):=by rw[show r.size=h+(2+(r.size-h-2)) by omega,BQ,BQ,(BH r (0+h+2) _).2 (λt ht=>h3 _ א א)];simp[winVal] have hlow:winVal r 0 h<2^(32*h):=EI r 0 h (λt ht=>hr _ א) have hR0:winVal r 0 r.size≠0:=by rw[hR];have:0<2^(32*h)*(r[h]! + 2^32*r[h+1]!):=by have:=Н h2;positivity omega have hlog:32*h+2+25<=(winVal r 0 r.size).log2:=by rw[Nat.le_log2 hR0,hR];have:1<=r[h+1]!:=Н h2 calc 2^(32*h+2+25)<=2^(32*h)*2^32:=by rw[<-pow_add];exact д ב א _<=2^(32*h)*(r[h]! + 2^32*r[h+1]!):=by gcongr;nlinarith _<=_:=א rw[CD neg _ e negZero hR0,<-CS neg _ e _ hlog,hR,Dp _ _ _ h hlow,Ef] def highLimb (pos neg:ZS ZU):ZU->ZU | 0=>0 | k+1=>if pos[k]! != 0 || neg[k]! != 0 then k+1 else highLimb pos neg k lemma CJ (pos neg:ZS ZU) (k:ZU):∀ t,highLimb pos neg k<=t->t< k->pos[t]! = 0∧neg[t]! = 0:=by induction k with | zero=>intro t _ ht;omega | succ k ih => intro t h1 h2;simp only[highLimb] at h1;split at h1;. omega . rename_i hk;rcases и (ш.mp h2) with h2 | rfl;. exact ih t h1 h2 . simpa [GX,bne_iff_ne,not_or] using hk def finishFast (pos neg:ZS ZU) (e:ZY) (negZero:ZT):ZQ:= let lo:=lowLimb pos neg pos.size 0 let n:=highLimb pos neg (max pos.size neg.size)+1-lo let e':=e+((32*lo:ZU):ZY) let d:=diffGo pos neg lo 0 0 0 (Array.emptyWithCapacity n) n if d.2.1=0∧d.2.2.1=0∧d.2.2.2=0 then finishDigits false d.1 e' negZero else let d':=diffGo neg pos lo 0 0 0 (Array.emptyWithCapacity n) n if d'.2.1=0∧d'.2.2.1=0∧d'.2.2.2=0 then finishDigits true d'.1 e' negZero else finishLimbs pos neg e negZero lemma CK (pos neg:ZS ZU) (e:ZY) (negZero:ZT): finishFast pos neg e negZero =finishWord ((limbVal pos.toList:ZY)-limbVal neg.toList) e negZero:=by unfold finishFast dsimp only set lo:=lowLimb pos neg pos.size 0 set n:=highLimb pos neg (max pos.size neg.size)+1-lo have hlow:=lowLimb_zero pos neg pos.size 0 have hhigh:=CJ pos neg (max pos.size neg.size) have hzero (a:ZS ZU) (ha:a=pos∨a=neg):limbVal a.toList=2^(32*lo)*winVal a lo n:=by apply Cj a lo n . intro t ht;rcases ha with rfl | rfl;. exact (hlow t א ht).1 . exact (hlow t א ht).2 . intro t ht;by_cases hts:t< max pos.size neg.size . rcases ha with rfl | rfl;. exact (hhigh t א hts).1 . exact (hhigh t א hts).2 . rcases ha with rfl | rfl<;>exact Br _ t א have hP:=hzero pos (Or.inl rfl) have hN:=hzero neg (Or.inr rfl) rw[show (Array.emptyWithCapacity n:ZS ZU)=#[] from rfl] have hd:=Bh pos neg lo 0 0 0 #[] n א ג have hd':=Bh neg pos lo 0 0 0 #[] n א ג have hw0:winVal (#[]:ZS ZU) 0 0=0:=rfl simp only[List.size_toArray,List.length_nil,Nat.mul_zero,pow_zero,one_mul,hw0,Nat.cast_zero,zero_add] at hd hd' obtain ⟨-,hb,hv⟩:=hd obtain ⟨-,hb',hv'⟩:=hd' split_ifs with h1 h2 . rw[Bu false _ _ _ hb,finishWord_scale,hP,hN];simp only[GC,ite_false];congr 1;rw[h1.1,h1.2.1,h1.2.2] at hv;push_cast at hv ⊢;linear_combination (2:ZY)^(32*lo)*hv . rw[Bu true _ _ _ hb',finishWord_scale,hP,hN];simp only[ite_true];congr 1;rw[h2.1,h2.2.1,h2.2.2] at hv';push_cast at hv' ⊢;linear_combination (-(2:ZY)^(32*lo))*hv' . exact finishLimbs_eq pos neg e negZero end @[inline] def dotStep (acc:Acc) (wa wb:ZQ):Acc:= if expOf wa=255∨expOf wb=255 then acc.markSpecial else acc.add (mantOf wa*mantOf wb) (idxOf wa+idxOf wb) (Bool.xor (signOf wa) (signOf wb)) @[specialize] def dotLoop (fa fb:ZU->ZQ) (pos neg:ZS ZU) (sp nz:ZT) (index:ZU):ZU->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 @[inline] def B2 (n:ZU) (fa fb:ZU->ZQ):ZQ:= let acc:=dotLoop fa fb (GI 18 0) (GI 18 0) false true 0 n if acc.special then dotSpec (GJ (n:=n) λi=>fa i) (GJ (n:=n) λi=>fb i) else finishFast acc.pos acc.neg (-298) (n≠0 && acc.negZero) lemma Fp (wa wb:ZQ): (if Bool.xor (signOf wa) (signOf wb) then -((mantOf wa*mantOf wb:ZU):ZY) else ((mantOf wa*mantOf wb:ZU):ZY))=sv wa*sv wb:=by unfold sv;cases signOf wa<;>cases signOf wb<;>simp lemma CR (fa fb:ZU->ZQ) (pos neg:ZS ZU) (nz:ZT) (index count:ZU):(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.markSpecial 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 DE (fa fb:ZU->ZQ) (A B:ZS ZQ) (n:ZU) (hA:∀ k,k< n->A[k]! = fa k) (hB:∀ k,k< n->B[k]! = fb k) (acc:Acc) (index remaining:ZU) (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): 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,Acc.markSpecial] 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[CR] 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 ⟨hs,hall,hD,hz⟩:=ih _ (index+1) א h1 h2 hfin;refine ⟨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 א . rw[hD,h5,dsum,ha,hb,Fp];ring . rw[hz,h4,dallNZ,ha,hb];simp[Bool.and_assoc] lemma BL (n:ZU) (f:ZU->ZQ) (k:ZU) (hk:k< n):(GJ (n:=n) λi=>f i.val)[k]! = f k:=by rw[Е _ k (by simpa using hk),Array.getElem_ofFn] lemma Bq (n:ZU) (fa fb:ZU->ZQ): B2 n fa fb =dotSpec (GJ (n:=n) λi=>fa i) (GJ (n:=n) λi=>fb i):=by unfold B2 set A:=GJ (n:=n) λi=>fa i with hA set B:=GJ (n:=n) λi=>fb i with hB set acc:=dotLoop fa fb (GI 18 0) (GI 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 obtain ⟨-,hall,hD,hz⟩:=DE fa fb A B n (BL n fa) (BL n fb) Acc.empty 0 n א (by simp[Acc.empty]) (by simp[Acc.empty]) hsp';have hD:acc.total=dsum A B 0 n:=by have:=hD;simp only[Acc.total_empty,Int.zero_add] at this;exact this have hz:acc.negZero=dallNZ A B 0 n:=by have:=hz;simp only[Acc.empty,Bool.true_and] at this;exact this simp only[hsp',GC,ite_false];rw[CK,show ((limbVal acc.pos.toList:ZY)-limbVal acc.neg.toList)=acc.total from rfl];have hAsize:A.size=n:=by simp[hA] have hBsize:B.size=n:=by simp[hB] set L:=A.map (GW (fmt:=Fmt)) with hL;set R:=B.map (GW (fmt:=Fmt)) with hR;have hLR:L.size=R.size:=by simp[hL,hR,hAsize,hBsize] have hLsize:L.size=n:=by simp[hL,hAsize] have hLfin:∀ k (hk:k< L.size),GE L[k]=true:=by intro k hk;have hk':k< A.size:=by simpa [hL] using hk have:=(hall k א א).1;rw[Е A k hk'] at this;simp[hL,isFinite_ofUInt32,this] have hRfin:∀ k (hk:k< R.size),GE R[k]=true:=by intro k hk;have hk':k< B.size:=by simpa [hR] using hk have:=(hall k א א).2;rw[Е B k hk'] at this;simp[hR,isFinite_ofUInt32,this] have hslice:Model.Reduction.Semantics.finiteDotSlice L R 0 L.size ג (by simp[hLR])=(acc.total:ℚ)*2^(-298:ℤ):=by have:=finiteDotSlice_eq A B 0 L.size (by simp[hL]) (by simp[hL,hAsize,hBsize]) (by intro k k1 k2;exact hall k k1 א);exact this.trans (by rw[hLsize,hD]) have hexact:=Model.Reduction.Internal.dotState_exact_toRat (destination:=Fmt) L R hLR hLfin hRfin;unfold dotSpec Model.dot;by_cases hS0:acc.total=0 . have hz0:(Model.Reduction.Internal.dotState L R hLR:Model.Reduction.Internal.State Fmt).exact.significand=0:=by rw[<-а,hexact,hslice,hS0];simp rw[Model.Reduction.dotWithStatus_eq_zero_of_finite Fmt L R .nearestEven hLR hLfin hRfin hz0,hS0,finishWord_zero];unfold Model.Reduction.Internal.dotState;obtain ⟨h1,h2⟩:=dotStateLoop_flags A B {} 0 L.size (by simp[hL]) (by simp[hL,hAsize,hBsize]) (by intro k k1 k2;exact hall k k1 א);have h1':(Model.Reduction.Internal.dotStateLoop L R ({}:Model.Reduction.Internal.State Fmt) 0 L.size ג (by simp[hLR])).sawFiniteTerm=decide (L.size≠0):=by rw[show decide (L.size≠0)=(false || decide (L.size≠0)) by simp];exact h1 have h2':(Model.Reduction.Internal.dotStateLoop L R ({}:Model.Reduction.Internal.State Fmt) 0 L.size ג (by simp[hLR])).allTermsNegativeZero=dallNZ A B 0 L.size:=by rw[show dallNZ A B 0 L.size=(true && dallNZ A B 0 L.size) by simp];exact h2 simp only[Except.map,h1',h2'];rw[hz];simp[hLsize] . have hnz:Model.Reduction.Semantics.finiteDot L.toList R.toList≠.ok 0:=by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L R hLR];intro h;injection h with h;rw[hslice] at h;simp at h;exact hS0 h rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L R .nearestEven hLR hLfin hRfin hnz];simp only[Except.map];exact finishWord_of_ne _ _ _ _ hS0 (by rw[hexact,hslice]) lemma Bg (a:ZS ZQ) (n:ZU) (h:n=a.size):(GJ (n:=n) λi=>a[i.val]!)=a:=by subst h;apply ъ ג;intro i h1 h2;rw[Array.getElem_ofFn,Е a i h2] lemma Du (n:ZU) (c:ZQ):(GJ (n:=n) λ_=>c)=GI n c:=by apply ъ ג;intro i h1 h2;simp def B3 (a b:ZS ZQ):ZQ:= if a.size=b.size then B2 a.size (λi=>a[i]!) (λi=>b[i]!) else dotSpec a b def G9 (x:ZS ZQ):ZQ:=B2 x.size (λi=>x[i]!) (λ_=>oneW) lemma D4 (a b:ZS ZQ):B3 a b=dotSpec a b:=by unfold B3;split;. rename_i h;rw[Bq,Bg a _ rfl,Bg b _ h] . rfl lemma D1 (x:ZS ZQ):G9 x=sumSpec x:=by rw[G9,Bq,Bg x _ rfl,Du,sumSpec] lemma BJ:B2=dotOf:=funext λn=>funext λfa=>funext λfb=>Bq n fa fb lemma Cc (w:Word):toWord (ofW w)=w:=by apply И.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact Г w.toBitVec.isLt lemma CT (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,GW,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z @[inline] def keyOf (w:Word):ZY:= if signOf w then -((mantOf w <<< idxOf w:ZU):ZY) else ((mantOf w <<< idxOf w:ZU):ZY) lemma E7 (w:Word):keyOf w=sv w*2^idxOf w:=by unfold keyOf sv;split<;>simp[Nat.shiftLeft_eq] lemma BN (w:Word):(decodeD w).toReal=(keyOf w:ℝ)*2^(-149:ZY):=by rw[<-ж,decodeD_toRat,E7];push_cast;rw[zpow_sub₀ С,Ц];ring @[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 DO (a b:Word):Model.cmpDyadic (decodeD a) (decodeD b)=compare (keyOf a) (keyOf b):=by have hs:(0:ℝ)<2^(-149:ZY):=ю two_pos _;cases h:compare (keyOf a) (keyOf b);. rw[Model.cmpDyadic_lt_iff,BN,BN];exact Ш (by exact_mod_cast compare_lt_iff_lt.mp h) hs . rw[Model.cmpDyadic_eq_iff,BN,BN,compare_eq_iff_eq.mp h] . rw[Model.cmpDyadic_gt_iff,BN,BN];exact Ш (by exact_mod_cast compare_gt_iff_gt.mp h) hs lemma EG (a b:Word):ofW (maxW a b)=Model.maximum (ofW a) (ofW b):=by unfold maxW;split;. exact CT _ . rename_i hs;have ha:=toDyadic_ofUInt32 a (λh=>hs (Or.inl h));have hb:=toDyadic_ofUInt32 b (λh=>hs (Or.inr h));have hna:=GN ha;have hnb:=GN hb;have hc:Model.compare (ofW a) (ofW b)=some _:=Model.compare_eq_some_cmpDyadic_of_toDyadic? _ _ ha hb;rw[DO] at hc;have hnone:Model.chooseNaN2 (ofW a) (ofW b)=none:=(Model.chooseNaN2_eq_none_iff _ _).2 ⟨hna,hnb⟩;unfold Model.maximum;rw[Model.withNaNSelection_of_none _ _ hnone];dsimp only;rw[Model.compareNonNaN_eq_of_compare_eq_some _ _ _ _ hc];cases compare (keyOf a) (keyOf b)<;>simp only[CT] def maxpoolW (c h w k:ZU) (inp:ZS Word):ZS Word:= GJ (n:=c*(h/k*(w/k))) λq => let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) (GY (k*k)).foldl (λacc t=>maxW acc (poolAt h w k inp ch y x t)) (poolAt h w k inp ch y x 0) lemma De (p:ZU->Word) (l:List ZU) (acc:Word): l.foldl (λm t=>Model.maximum m (ofW (p t))) (ofW acc) =ofW (l.foldl (λa t=>maxW a (p t)) acc):=by induction l generalizing acc with | nil=>rfl | cons t l ih=>simp only[GZ,<-EG,ih] lemma DL:maxpoolW=maxpoolSpec:=by funext c h w k inp;simp only[maxpoolW,maxpoolSpec,De,Cc] lemma usz (t n:ZX) (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 BO (a b:ZX) (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:ZU) (h:k<2^32):(GD 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 Bl (a:ZV) (i:ZX) (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 BT (a:ZS ZQ) (i:ZX) (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 rdF (a:ZV) (i:ZX):ZW:= if h:i< a.usize then a.uget i (Bl a i h) else 0 @[inline] def rdW (a:ZS ZQ) (i:ZX):ZQ:= if h:i< a.usize then a.uget i (BT a i h) else 0 @[inline] def wrF (a:ZV) (i:ZX) (v:ZW):ZV:= if h:i< a.usize then a.uset i v (Bl a i h) else a @[inline] def wrW (a:ZS ZQ) (i:ZX) (v:ZQ):ZS ZQ:= if h:i< a.usize then a.uset i v (BT a i h) else a def pwEntry (i:ZU):ZW:= let e:=(i%256).toUInt64 let s:ZR:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then GT s else GT (s ||| ((e+873) <<< (52:ZR))) def pw:ZV:= (GY 512).foldl (λa i=>a.push (pwEntry i)) (GK 512) lemma DQ:pw.data=(GY 512).toArray.map pwEntry:=by have key:∀ (l:List ZU) (a:ZV),(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[GZ,ih];simp[FloatArray.push] simp only[pw,key];simp[GK] lemma FC:pw.size=512:=by simp[FloatArray.size,DQ] lemma FS (w:ZQ):(w >>> (23:ZQ)).toUSize.toNat< pw.size:=by rw[FC];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [GM] at h ⊢;omega @[inline] def dv (w:ZQ):ZW:= ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:ZQ)).toUSize (FS w) @[inline] def good (w:ZQ):ZT:= let v:=w &&& 0x7FFFFFFF v == 0 || v-(0x00800000:ZQ)<=(0x7EFFFFFF:ZQ) def scan (A:ZS ZQ) (n i:ZX) (hn:n.toNat<=A.size) (lo hi:ZQ):ZQ:= if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& 0x7FFFFFFF 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<0x7F800000 then hi else 0xFFFFFFFF termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];omega lemma BR (a:ZS ZQ):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ def pushZ (n k:ZX) (a:ZV):ZV:= 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 GA (s e:ZW):ZQ:= let x:ZR:=((s+e).toBits+(0x0FFFFFFF:ZR)) >>> (29:ZR) let y:ZR:=((s-e).toBits+(0x0FFFFFFF:ZR)) >>> (29:ZR) let ex:ZR:=(x >>> (23:ZR)) &&& (0x7FF:ZR) if x == y && ex-(897:ZR)<(254:ZR) then (((x >>> (34:ZR)) <<< (31:ZR)) ||| ((x &&& (0x3FFFFFFFF:ZR))-((896:ZR) <<< (23:ZR)))).toUInt32 else 0xFFFFFFFF structure F1 where (s t:ZW) structure R1 where (ix:ZS ZQ) (buf:ZV) (c:ZX) (hi:ZQ) @[inline] def mx (a w:ZQ):ZQ:= let m:=w &&& 0x7FFFFFFF if m<=a then a else m def decR (X:ZS ZQ) (o n t:ZX) (ix:ZS ZQ) (buf:ZV) (c:ZX) (hi:ZQ):R1:= if h:t< n then let w:=rdW X (o+t) if w &&& 0x7FFFFFFF == 0 then decR X o n (t+1) ix buf c hi else decR X o n (t+1) (wrW ix c t.toUInt32) (wrF buf c (dv w)) (c+1) (mx hi w) else ⟨ix,buf,c,hi⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];have:=А.mp h;omega) lemma wi (w i m:ZX) (W:ZS ZQ) (hw:w.toNat+m.toNat<=W.size) (hs:W.size<2^32) (hi:i.toNat< m.toNat):(w+i).toNat< W.size:=by rw[BO _ _ א];omega structure F4 where (a b c d:ZW) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary noncomputable def R (f:ZW):ℝ:=GG (toModel (ofFloat f)) noncomputable def wv (w:ZQ):ℝ:=GG (ofW w) noncomputable def B4 (n:ZU) (xw ww:ZU->ZQ) (bw:ZQ):ℝ:= (GR n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:ZU):ZW:=(n.toUInt64+8).toFloat*GT 0x3CC0000000000000 lemma BS:FloatFormat.binary64.isIEEE=true:=by decide lemma Bi (a:Model FloatFormat.binary64):GE a=(GU a).isFinite:=by have hd:=Model.toDyadic?_ieee_eq_model _ BS a;cases hm:GU a with | notANumber => rw[hm] at hd;cases h:GE a with | false=>rfl | true => obtain ⟨d,hd'⟩:=Model.exists_toDyadic?_of_isFinite h;rw[hd] at hd';cases hd' | infinity s => rw[hm] at hd;cases h:GE a with | false=>rfl | true => obtain ⟨d,hd'⟩:=Model.exists_toDyadic?_of_isFinite h;rw[hd] at hd';cases hd' | zero s => rw[hm] at hd;exact Model.isFinite_eq_true_of_toDyadic?_some hd | finite s m e h => rw[hm] at hd;exact Model.isFinite_eq_true_of_toDyadic?_some hd abbrev A_M (x:ZW):Model FloatFormat.binary64:=Model.ofFloatModel x.toModel lemma A_R (x:ZW):R x=GG (A_M x):=rfl lemma BI (x:ZW):GE (A_M x)=x.isFinite:=by rw[Bi,GS];rfl lemma Be (e:ℤ):Formats.Flocq.bpow FloatLib.Numerics.binaryRadix e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma BE:(2:ℝ)^(1000:ℤ)<=GG (Model.posMaxFinite FloatFormat.binary64):=by rw[Model.toReal_posMaxFinite] have h1:Model.pow2 FloatFormat.binary64.fracWidth+FloatFormat.binary64.maxFiniteFracField =2^53-1:=by decide +kernel have h2:FloatFormat.binary64.maxNormalExponent-Int.ofNat FloatFormat.binary64.fracWidth =971:=by decide +kernel rw[h1,h2];have h3:Model.bpow 971=(2:ℝ)^(971:ℤ):=by simp only[Model.bpow,Be] rw[h3,show (1000:ℤ)=29+971 from rfl,М ב];apply Б _ (by positivity);norm_num lemma FP (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true) (hb:|R x| + |R y| ≤ 2^(1000:ℤ)): (x+y).isFinite=true ∧ R (x+y)=Model.roundAt FloatFormat.binary64 (R x+R y):=by have hm:A_M (x+y)=Model.add (A_M x) (A_M y):=toModel_ofFloat_add_of_isFinite x y hx hy have hfx:GE (A_M x)=true:=by rw[BI,hx] have hfy:GE (A_M y)=true:=by rw[BI,hy] constructor . rw[<-BI,hm];exact Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ BS hfx hfy (hb.trans BE) . rw[A_R,hm];exact Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ BS hfx hfy (hb.trans BE) lemma FJ (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true) (hb:|R x| + |R y| ≤ 2^(1000:ℤ)): (x-y).isFinite=true ∧ R (x-y)=Model.roundAt FloatFormat.binary64 (R x-R y):=by have hm:A_M (x-y)=Model.sub (A_M x) (A_M y):=toModel_ofFloat_sub_of_isFinite x y hx hy have hfx:GE (A_M x)=true:=by rw[BI,hx] have hfy:GE (A_M y)=true:=by rw[BI,hy] constructor . rw[<-BI,hm];exact Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ BS hfx hfy (hb.trans BE) . rw[A_R,hm];exact Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ BS hfx hfy (hb.trans BE) open Float.Model.UnpackedFloat in lemma Cw (b:BitVec 64) (s m e h) (hu:Float.Model.UnpackedFloat.unpack GH b=.finite s m e h):(2^52<=m∧-1074<=e)∨e=-1074:=by unfold Float.Model.UnpackedFloat.unpack at hu;simp only at hu;have hb:(Float.Model.Format.binary64.exponentBias:ℤ)=1023:=by decide rw[hb] at hu;split_ifs at hu with h1 h2 h3 h4 . injection hu with _ _ he;right;have:(unpackExponent (spec:=GH) b).toNat=0:=by simp[h3] rw[this] at he;omega . injection hu with _ hm he;left;have:(unpackExponent (spec:=GH) b).toNat≠0:=by intro h0;exact h3 (BitVec.eq_of_toNat_eq (by simpa using h0)) constructor;. rw[<-hm,BitVec.toNat_append];refine ظ ?_ Nat.left_le_or;simp[Nat.shiftLeft_eq] . omega open Float.Model.UnpackedFloat in lemma Cz (spec:Float.Model.Format) (s:Sign) (m:ℕ) (e:ℤ) (h:e<=spec.targetExponent (Float.Model.totalExponent m e)):Float.Model.UnpackedFloat.round spec s m e=roundWithAccuracy spec s m e .exact:=by unfold Float.Model.UnpackedFloat.round decreaseExponent;have h0:(e-spec.targetExponent (Float.Model.totalExponent m e)).toNat=0:=א;simp[h0] open Float.Model.UnpackedFloat in lemma Ft (s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂) (b₁ b₂:BitVec 64) (hu₁:unpack GH b₁=.finite s₁ m₁ e₁ h₁) (hu₂:unpack GH b₂=.finite s₂ m₂ e₂ h₂): e₁+e₂<=Float.Model.Format.binary64.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have c₁:=Cw _ _ _ _ _ hu₁ have c₂:=Cw _ _ _ _ _ hu₂ have hm0:m₁*m₂≠0:=Nat.mul_ne_zero h₁.ne' h₂.ne' have hlog:2^52<=m₁*m₂->52<=(m₁*m₂).log2:=λh=>(Nat.le_log2 hm0).2 h simp only[Float.Model.Format.targetExponent,Float.Model.totalExponent,Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] rcases c₁ with ⟨c₁,_⟩ | c₁ . have:=hlog (ظ c₁ (й _ h₂));apply le_max_of_le_left;norm_num;omega . rcases c₂ with ⟨c₂,_⟩ | c₂;. have:=hlog (ظ c₂ (Nat.le_mul_of_pos_left _ h₁));apply le_max_of_le_left;norm_num;omega . apply le_max_of_le_right;norm_num;omega open Float.Model.UnpackedFloat in lemma DI (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true):A_M (x*y)=Model.mul (A_M x) (A_M y):=by have hX:GU (A_M x)=x.toModel.unpack:=GS _;have hY:GU (A_M y)=y.toModel.unpack:=GS _;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,Model.toDyadic?_ieee_eq_model _ BS (A_M x),Model.toDyadic?_ieee_eq_model _ BS (A_M y),hX,hY];change x.toModel.unpack.isFinite=true at hx;change y.toModel.unpack.isFinite=true at hy;cases hxm:x.toModel.unpack<;>cases hym:y.toModel.unpack <;>simp only[hxm,hym,Float.Model.UnpackedFloat.isFinite,GC] at hx hy case zero.zero s₁ s₂ => cases s₁<;>cases s₂<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul] all_goals first | (rw[Model.negZero_eq_ofModel_zero];rfl) | (rw[Model.posZero_eq_ofModel_zero];rfl) case zero.finite s₁ s₂ m₂ e₂ h₂ => cases s₁<;>cases s₂<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul] all_goals first | (rw[Model.negZero_eq_ofModel_zero];rfl) | (rw[Model.posZero_eq_ofModel_zero];rfl) case finite.zero s₁ m₁ e₁ h₁ s₂ => cases s₁<;>cases s₂<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul] all_goals first | (rw[Model.negZero_eq_ofModel_zero];rfl) | (rw[Model.posZero_eq_ofModel_zero];rfl) case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => simp only[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul];have hle:=Ft s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂ _ _ hxm hym;have h0:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] rw[<-Cz _ _ _ _ hle,h0];have hs:Model.modelSign (Model.modelSignBit s₁ ^^ Model.modelSignBit s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[GC,ite_false,GF,BS,ite_true,Model.ieeeRoundDyadic,hs];rfl lemma Cx (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true) (hb:|R x| * |R y| ≤ 2^(1000:ℤ)): (x*y).isFinite=true ∧ R (x*y)=Model.roundAt FloatFormat.binary64 (R x*R y):=by have hm:A_M (x*y)=Model.mul (A_M x) (A_M y):=DI x y hx hy have hfx:GE (A_M x)=true:=by rw[BI,hx] have hfy:GE (A_M y)=true:=by rw[BI,hy] constructor . rw[<-BI,hm];exact Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ BS hfx hfy (hb.trans BE) . rw[A_R,hm];exact Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ BS hfx hfy (hb.trans BE) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma By:0< A_z:=by unfold A_z;positivity lemma CY:A_z<=1/2^200*(1/2^200):=by have:(1/2^200*(1/2^200):ℝ)=2^(-400:ℤ):=by rw[show (-400:ℤ)=-200+-200 from rfl,М ב];norm_num rw[this];exact Ф ב ב lemma Ce:0< A_B:=by unfold A_B;positivity lemma BX:(2:ℝ)^256*2^42<=A_B:=by have:((2:ℝ)^256*2^42)=2^(298:ℤ):=by rw[show (298:ℤ)=256+42 from rfl,М ב];norm_num rw[this];exact Ф ב ב lemma D9:A_B*2<=2^(1000:ℤ):=by have:A_B*2=2^(301:ℤ):=by unfold A_B;rw[show (301:ℤ)=300+1 from rfl,М ב];norm_num rw[this];exact Ф ב ב lemma DF:A_B*A_B<=2^(1000:ℤ):=by have:A_B*A_B=2^(600:ℤ):=by unfold A_B;rw[show (600:ℤ)=300+300 from rfl,М ב] rw[this];exact Ф ב ב lemma Dl (x:ℝ): |Model.roundAt FloatFormat.binary64 x-x| ≤ 1/2^53*(|x| + A_z):=by have ez:(1/2^53*(|x| + A_z):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ):=by have e1:(1/2^53:ℝ)=2^(-53:ℤ):=ב rw[e1,show (-1074:ℤ)=-53+-1021 from rfl,М ב (-53),mul_add] rfl rw[ez] have hu:(0:ℝ)<=(2:ℝ)^(-53:ℤ) * |x|:=by positivity have ht:(0:ℝ)<(2:ℝ)^(-1074:ℤ):=by positivity by_cases hx:x=0 . subst hx;rw[Model.roundAt_zero];simp only[sub_self,abs_zero];linarith refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf FloatFormat.binary64) x/2<=_ rw[Formats.Flocq.ulp.of_ne_zero _ _ x hx] simp only[Formats.Flocq.cexp,Model.fexpOf,Formats.Flocq.fltExp] have hl:=Formats.Flocq.bpow_magnitude_sub_one_le FloatLib.Numerics.binaryRadix x hx rw[Be] at hl rw[Be,show ((FloatFormat.binary64.fracWidth:ℤ)+1)=53 from rfl,show FloatFormat.binary64.minSubnormalExponent=-1074 from rfl] generalize Formats.Flocq.magnitude FloatLib.Numerics.binaryRadix x=M at hl ⊢ rcases le_total (-1074) (M-53) with h | h . rw[max_eq_left h];have e:(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] rw[e];have:=Й hl (show (0:ℝ)<=2^(-53:ℤ) by positivity);linarith . rw[max_eq_right h];generalize (2:ℝ)^(-1074:ℤ)=t at ht ⊢;generalize (2:ℝ)^(-53:ℤ) * |x| = v at hu ⊢;linarith lemma DG (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1:|R x| ≤ A_B) (h2:|R y| ≤ A_B):(x+y).isFinite=true∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z):=by obtain ⟨hf,he⟩:=FP x y hx hy (by have:=D9;linarith);exact ⟨hf,he ▸ Dl _⟩ lemma FU (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1:|R x| ≤ A_B) (h2:|R y| ≤ A_B):(x-y).isFinite=true∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z):=by obtain ⟨hf,he⟩:=FJ x y hx hy (by have:=D9;linarith);exact ⟨hf,he ▸ Dl _⟩ lemma CP (x y:ZW) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1:|R x| ≤ A_B) (h2:|R y| ≤ A_B):(x*y).isFinite=true∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z):=by have hb:|R x| * |R y| ≤ 2^(1000:ℤ):=(г h1 h2 (П _) Ce.le).trans DF;obtain ⟨hf,he⟩:=Cx x y hx hy hb;exact ⟨hf,he ▸ Dl _⟩ lemma BU (m k:ℤ) (hm:m.natAbs<2^53) (hk:-1074<=k):Model.roundAt FloatFormat.binary64 ((m:ℝ)*2^k)=(m:ℝ)*2^k:=by apply Formats.Flocq.round_preserves_generic;apply (Formats.Flocq.generic_format_FLT_iff (FloatFormat.binary64.minSubnormalExponent) (FloatFormat.binary64.fracWidth+1) (by decide) _).2;refine ⟨by decide,⟨m,k⟩,?_,?_,?_⟩;. simp[Formats.Flocq.toReal,Be] . exact hm . exact hk lemma BW (x:ZW):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,Model.toReal_eq_unpackedToReal_toModel BS,GS] lemma E1 (f:ZW) (s m e h) (hu:f.toModel.unpack=.finite s m e h):(GV FloatFormat.binary64 (.finite .positive m e h)).toModel=.finite .positive m e h:=by have hm:GU (A_M f)=.finite s m e h:=by rw[GS,hu] cases s with | positive => rw[<-hm];exact Model.toModel_canonicalizeModel _ | negative => have hf:(GU (A_M f)).isFinite=true:=by rw[hm];rfl have hn:=Model.toModel_neg_of_isFinite BS (A_M f) hf;rw[hm] at hn;have h2:Float.Model.UnpackedFloat.neg (.finite .negative m e h) =.finite .positive m e h:=rfl;rw[h2] at hn;rw[<-hn];exact Model.toModel_canonicalizeModel _ lemma CN (f:ZW) (hf:f.isFinite=true):f.abs.isFinite=true∧R f.abs = |R f|:=by have hA:A_M f.abs=GV FloatFormat.binary64 f.toModel.unpack.abs:=Model.ofFloatModel_pack _;change f.toModel.unpack.isFinite=true at hf;cases hu:f.toModel.unpack with | notANumber=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | infinity s=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | zero s => rw[hu] at hA;constructor;. rw[<-BI,hA];exact Model.isFinite_ofModel_zero _ BS _ . rw[A_R f.abs,hA,BW f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ BS] | finite s m e h => rw[hu] at hA;have hk:=E1 f s m e h hu;change GU (GV FloatFormat.binary64 (.finite .positive m e h))=_ at hk;constructor;. rw[<-BI,hA,Bi];change (GU (GV FloatFormat.binary64 (.finite .positive m e h))).isFinite=_;rw[hk];rfl . rw[A_R f.abs,hA,BW f,hu];change GG (GV FloatFormat.binary64 (.finite .positive m e h))=_;rw[Model.toReal_eq_unpackedToReal_toModel BS,hk];simp only[Model.unpackedToReal_finite,Be];have h0:(0:ℝ)<=(m:ℝ)*2^e:=by positivity cases s<;>simp[Model.modelSignBit,abs_of_nonneg h0] lemma Ct (f:ZW) (k:ℕ) (hk:k<2^53) (hf:f.toModel=Float.Model.pack (Float.Model.UnpackedFloat.ofNat GH k)):f.isFinite=true∧R f=k:=by have hA:A_M f=GF FloatFormat.binary64 (FloatLib.Numerics.Dyadic.ofScaledInt k 0):=by change Model.ofFloatModel f.toModel=_;rw[hf,Model.ofFloatModel_pack];exact Model.ofModel_ofInt_eq_roundDyadic _ BS (k:ℤ) have hd:(FloatLib.Numerics.Dyadic.ofScaledInt k 0).toReal=k:=by rw[<-ж,FloatLib.Numerics.Dyadic.ofScaledInt_toRat];simp have hkR:(k:ℝ)<=2^(1000:ℤ):=by have h1:(k:ℝ)<=2^53:=by exact_mod_cast hk.le have h2:(2:ℝ)^53<=2^256*2^42:=ב calc (k:ℝ)<=2^53:=h1 _<=2^256*2^42:=h2 _<=A_B:=BX _<=A_B*2:=by linarith [Ce] _<=_:=D9 have hfin:GE (A_M f)=true:=by rw[hA];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ BS;rw[hd,У (Э _)];exact hkR.trans BE refine ⟨by rw[<-BI];exact hfin,?_⟩;rw[A_R,hA,Model.toReal_roundDyadic_eq_roundAt _ BS _ (hA ▸ hfin),hd];have:=BU k 0 (by simpa using hk) ב;simpa using this lemma FE (m:ZQ) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat:=Ct _ _ h rfl lemma F6 (m:ZR) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat:=Ct _ _ h rfl lemma Bk:(0:ZW).toModel.unpack=.zero .positive:=by rfl lemma G7:(0:ZW).isFinite=true∧R 0=0:=by refine ⟨?_,?_⟩;. change (0:ZW).toModel.unpack.isFinite=true;rw[Bk];rfl . rw[BW,Bk];simp def A_bits (i:ℕ):ZR:= let e:=(i%256).toUInt64 let s:ZR:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:ZR)) lemma DX (i:ℕ):pwEntry i=GT (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):ZT:= match Float.Model.UnpackedFloat.unpack GH (A_bits i).toBitVec with | .finite s m e _=>(Model.modelSignBit s == decide (256<=i)) && m == 2^52 && e == ((i%256:ℕ):ℤ)-202 && (i%256 != 0) | .zero _=>i%256 == 0 | _=>false lemma Da:∀ i,i<512->A_chk i=true:=by decide +kernel lemma G6 (i:ℕ) (hi:i<512): (pwEntry i).isFinite=true∧R (pwEntry i) =if i%256=0 then 0 else (if 256<=i then -1 else 1)*2^(((i%256:ℕ):ℤ)-150):=by have hc:=Da i hi rw[DX] have hU:(GT (A_bits i)).toModel.unpack =Float.Model.UnpackedFloat.unpack GH (A_bits i).toBitVec:=Model.unpack_pack_unpack _ _ unfold A_chk at hc rw[BW,hU] change (GT (A_bits i)).toModel.unpack.isFinite=true∧_ rw[hU] cases hu:Float.Model.UnpackedFloat.unpack GH (A_bits i).toBitVec with | notANumber=>rw[hu] at hc;simp at hc | infinity s=>rw[hu] at hc;simp at hc | zero s => rw[hu] at hc simp only[beq_iff_eq] at hc refine ⟨rfl,?_⟩ simp[hc] | finite s m e h => rw[hu] at hc simp only[GQ,beq_iff_eq,bne_iff_ne] at hc obtain ⟨⟨⟨h1,h2⟩,h3⟩,h4⟩:=hc refine ⟨rfl,?_⟩ have e:((2^52:ℕ):ℝ)*2^(((i%256:ℕ):ℤ)-202) =2^(((i%256:ℕ):ℤ)-150):=by rw[Nat.cast_pow,Nat.cast_ofNat,<-Ц,<-М ב] congr 1 push_cast ring rw[Model.unpackedToReal_finite,Be,h3,h1,h2,mul_assoc,e] simp only[decide_eq_true_eq,h4,↓reduceIte] lemma Eb (i:ZX) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,DQ] lemma BA (w:ZQ):(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[О];exact В w.toNat 31 lemma Bx (w:ZQ):(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[О];exact В w.toNat 23 lemma EB (w:ZQ):(w >>> (23:ZQ)).toNat=w.toNat/2^23:=by rw[л,Ю];rfl lemma Bw (w:ZQ):(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf;rw[О,EB];exact В _ 8 lemma DC (w:ZQ):signOf w=decide (2^31<=w.toNat):=by have hw:=w.toNat_lt;unfold signOf;have h:((w &&& 0x80000000).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 h2:w.toNat/2^31%2=0∨w.toNat/2^31%2=1:=א;rcases h2 with h2 | h2<;>rw[h2]<;>rfl rw[Bool.eq_iff_iff];simp only[bne_iff_ne,ne_eq,decide_eq_true_eq];rw[<-И,h];simp only[Gb];omega lemma Fj (w:ZQ):((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,Bx,Nat.or_comm];have h:w.toNat%2^23<2^23:=Ъ _ (by positivity);have:=щ h 1;simpa using this.symm lemma C2 (w:ZQ) (h:good w=true):w.toNat%2^31=0∨(2^23<=w.toNat%2^31∧w.toNat%2^31<0x7F800000):=by have hv:=BA w;have hlt:=(w &&& 0x7FFFFFFF).toNat_lt;unfold good at h;simp only[GX,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-hv,h];rfl . right;rw[Ж,UInt32.toNat_sub] at h;rw[<-hv];generalize (w &&& 0x7FFFFFFF).toNat=v at *;simp at h;omega noncomputable def A_mag (v:ℕ):ℝ:= if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma BD (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma CA (w:ZQ) (hg:good w=true):wv w=(if 2^31<=w.toNat then -1 else 1)*A_mag (w.toNat%2^31):=by have hN:=w.toNat_lt;have hgd:=C2 w hg;have hE:=Bw w;have hF:(fracOf w).toNat=w.toNat%2^23:=Bx w;have h255:expOf w≠255:=by intro h;rw[h] at hE;change 255=_ at hE;omega have hv:wv w=(decodeD w).toReal:=by unfold wv ofW;rw[Model.toReal_eq,toDyadic_ofUInt32 w h255] rw[hv];unfold decodeD A_mag;by_cases h0:w.toNat%2^31=0 . have e0:expOf w=0:=by apply И.mp;rw[hE];show _=0;omega have f0:fracOf w=0:=by apply И.mp;rw[hF];show _=0;omega simp only[e0,f0,h0,↓reduceIte];simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] . have e0:expOf w≠0:=by intro h;rw[h] at hE;change 0=_ at hE;omega simp only[e0,h0,↓reduceIte];have f1:w.toNat%2^31%2^23=(fracOf w).toNat:=by rw[hF];omega have f2:w.toNat%2^31/2^23=(expOf w).toNat:=by rw[hE];omega rw[f1,f2];simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,DC];simp;split_ifs<;>ring lemma EU (v:ℕ) (h:v<0x7F800000):A_mag v<=2^128:=by unfold A_mag;split_ifs with h0;. positivity . have h1:((2^23+v%2^23:ℕ):ℝ)<=2^24:=by have:2^23+v%2^23<=2^24:=א;exact_mod_cast this have h2:(2:ℝ)^(((v/2^23:ℕ):ℤ)-150)<=2^(104:ℤ):=Ф ב א;have h3:(2:ℝ)^(104:ℤ)=2^104:=ב;rw[h3] at h2 calc _<=(2:ℝ)^24*2^104:=г h1 h2 (by positivity) (by positivity) _=2^128:=ב lemma CV (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 BD b unfold A_mag;simp only[show ¬ a=0 by omega,show ¬ b=0 by omega,↓reduceIte];have hdiv:a/2^23<=b/2^23:=Nat.div_le_div_right hab;rcases hdiv.eq_or_lt with he | hl;. rw[he];apply Б _ (by positivity);have:2^23+a%2^23<=2^23+b%2^23:=א;exact_mod_cast this . have h1:((2^23+a%2^23:ℕ):ℝ)<=2^23*2:=by have:2^23+a%2^23<=2^23*2:=א;exact_mod_cast this have h2:(2:ℝ)^(((a/2^23:ℕ):ℤ)-150)*2<=2^(((b/2^23:ℕ):ℤ)-150):=by rw[<-zpow_add_one₀ ב];exact Ф ב א have h3:(2:ℝ)^23<=((2^23+b%2^23:ℕ):ℝ):=by have:2^23<=2^23+b%2^23:=א;exact_mod_cast this calc _<=((2:ℝ)^23*2)*2^(((a/2^23:ℕ):ℤ)-150):= Б h1 (by positivity) _=2^23*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by ring _<=_:=г h3 h2 (by positivity) (by positivity) lemma Ch (w:ZQ) (hg:good w=true):(dv w).isFinite=true∧R (dv w)=wv w:=by have hN:=w.toNat_lt;have hgd:=C2 w hg;have hM:=Fj w;have hm:=FE ((w &&& 0x7FFFFF) ||| 0x800000) (by rw[hM];omega);have hI:(w >>> (23:ZQ)).toUSize.toNat=w.toNat/2^23:=by rw[Т,EB] have hp:=G6 (w.toNat/2^23) א;unfold dv;rw[Eb,hI];have hR1:|R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| ≤ 2^24:=by rw[hm.2,hM,У (Э _)];have:2^23+w.toNat%2^23<=2^24:=א;exact_mod_cast this have hR2:|R (pwEntry (w.toNat/2^23))| ≤ 2^106:=by rw[hp.2];split_ifs;. simp all_goals simp only[abs_mul,abs_neg,abs_one,one_mul];rw[abs_of_pos (by positivity)];have:(2:ℝ)^(((w.toNat/2^23%256:ℕ):ℤ)-150)<=2^(106:ℤ):=Ф ב א;simpa using this have hb:|R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| * |R (pwEntry (w.toNat/2^23))| ≤ 2^(1000:ℤ):=by have h1:=BX;have e1:(2:ℝ)^24<=2^256*2^42:=ב;have e2:(2:ℝ)^106<=2^256*2^42:=ב;exact (г (hR1.trans (e1.trans h1)) (hR2.trans (e2.trans h1)) (П _) Ce.le).trans DF obtain ⟨hf,he⟩:=Cx _ _ hm.1 hp.1 hb;refine ⟨hf,?_⟩;rw[he,hm.2,hp.2,CA w hg,hM];rcases hgd with h0 | ⟨h1,h2⟩;. have e0:w.toNat/2^23%256=0:=א;rw[h0];simp only[e0,↓reduceIte,mul_zero,Model.roundAt_zero];simp[A_mag] . have e0:¬ w.toNat/2^23%256=0:=א;have e1:w.toNat%2^31/2^23=w.toNat/2^23%256:=א;have e2:w.toNat%2^31%2^23=w.toNat%2^23:=א;have e3:(256<=w.toNat/2^23) ↔ 2^31<=w.toNat:=א;unfold A_mag;rw[e1,e2];simp only[e0,e3,show ¬ w.toNat%2^31=0 by omega,↓reduceIte];have hk:-1074<=((w.toNat/2^23%256:ℕ):ℤ)-150:=א;have hMl:2^23+w.toNat%2^23<2^53:=א;generalize ((w.toNat/2^23%256:ℕ):ℤ)-150=k at hk ⊢;generalize 2^23+w.toNat%2^23=M at hMl ⊢;by_cases hs:2^31<=w.toNat;. have:=BU (-(M:ℤ)) k (by simpa using hMl) hk;simp only[hs,↓reduceIte];push_cast at this;rw[show (M:ℝ)*(-1*2^k)=-(M:ℝ)*2^k by ring,show -1*((M:ℝ)*2^k)=-(M:ℝ)*2^k by ring];exact this . have:=BU (M:ℤ) k (by simpa using hMl) hk;simp only[hs,↓reduceIte,one_mul];push_cast at this;exact this def A_fsum (l:List ZW):ZW:=l.foldl (.+·) 0 noncomputable def A_sa (l:List ZW):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List ZW):ℝ:=(l.map R).sum lemma Bb (l:List ZW):0<=A_sa l:=by induction l with | nil=>simp[A_sa] | cons q l ih => simp only[A_sa,List.map_cons,List.sum_cons] at ih ⊢;positivity lemma Bv (l:List ZW) (q:ZW):A_sa (l ++ [q])=A_sa l + |R q|:=by simp[A_sa] lemma C7 (l:List ZW) (q:ZW):A_sr (l ++ [q])=A_sr l+R q:=by simp[A_sr] lemma Ba (l:List ZW) (q:ZW):A_fsum (l ++ [q])=A_fsum l+q:=by simp[A_fsum] lemma Bo (l:List ZW):|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 Bc (l:List ZW) (hf:∀ q∈l,q.isFinite=true) (hlen:l.length<=2^21) (hb:A_sa l<=A_B/4): (A_fsum l).isFinite=true ∧ |R (A_fsum l)-A_sr l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by induction l using List.reverseRecOn with | nil => simp only[A_fsum,List.foldl_nil,List.length_nil] refine ⟨G7.1,?_⟩ simp[G7.2,A_sr] | append_singleton l q ih => rw[Bv] at hb have hP:=Bb l have hq0:=П (R q) obtain ⟨h1,h2⟩:=ih (λx hx=>hf x (by simp[hx])) (by simp at hlen;omega) ד have hk:(l.length:ℝ)+1<=2^21:=by simp at hlen exact_mod_cast hlen have hk0:(0:ℝ)<=l.length:=Э _ have hS:=Bo l have hz:=By have hz1:=CY have hB:=Ce have hBl:=BX rw[Ba,Bv,C7,List.length_append,List.length_singleton] have hB1:(8:ℝ)<=A_B:=by have:(8:ℝ)<=2^256*2^42:=ב linarith have hz':A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=ב linarith push_cast set r:=R (A_fsum l) set a:=R q set P:=A_sa l set S:=A_sr l set k:ℝ:=(l.length:ℝ) set W:=(1/2^53:ℝ)*(P + |a| + A_z) with hWdef have hW:(1/2^53:ℝ)*(P+A_z)<=W:=by rw[hWdef] apply Й _ ב linarith have hW0:0<=W:=by rw[hWdef];positivity have hD:|r-S| ≤ 2*k*W:=h2.trans (Й hW (by positivity)) have hk2:2*k*(1/2^53)<=1:=by norm_num at hk ⊢ linarith have hUD:(1/2^53:ℝ) * |r-S| ≤ W:= calc (1/2^53:ℝ) * |r-S| ≤ 1/2^53*(2*k*W):= Й hD ב _=(2*k*(1/2^53))*W:=by ring _<=1*W:=Б hk2 hW0 _=W:=one_mul _ have hDP:|r-S| ≤ P+1:= calc |r-S| ≤ 2*k*(1/2^53*(P+A_z)):=h2 _=(2*k*(1/2^53))*(P+A_z):=by ring _<=1*(P+A_z):=Б hk2 (by positivity) _<=P+1:=ד have hr:|r| ≤ A_B:=by have e:r=(r-S)+S:=by ring have:=Ч (r-S) S rw[<-e] at this linarith have ha:|a| ≤ A_B:=ד obtain ⟨hf3,he⟩:=DG (A_fsum l) q h1 (hf q ג) hr ha refine ⟨hf3,?_⟩ have t1:|R (A_fsum l+q)-(S+a)| ≤ |R (A_fsum l+q)-(r+a)| + |r-S|:=by have e:R (A_fsum l+q)-(S+a)=(R (A_fsum l+q)-(r+a))+(r-S):=by ring rw[e] exact Ч _ _ have t2:|r+a| ≤ |r-S| + P + |a|:=by have e:r+a=(r-S)+S+a:=by ring rw[e] have h3:=Ч ((r-S)+S) a have h4:=Ч (r-S) S linarith have t3:(1/2^53:ℝ)*(|r+a| + A_z)<=1/2^53 * |r-S| + W:=by rw[hWdef,<-mul_add] apply Й _ ב linarith calc |R (A_fsum l+q)-(S+a)| ≤ W+W+2*k*W:=ד _=2*(k+1)*W:=by ring lemma G5 (n:ℕ) (hn:n<2^20):(nfOf n).isFinite=true∧R (nfOf n)=((n:ℝ)+8)/2^51:=by have ht:(n.toUInt64+8).toNat=n+8:=by have h8:(8:ZR).toNat=8:=rfl;simp[UInt64.toNat_add,h8];omega have h1:=F6 (n.toUInt64+8) (by rw[ht];omega);have hu:(GT 0x3CC0000000000000).toModel.unpack =.finite .positive (2^52) (-103) (by decide):=by rfl have h2:(GT 0x3CC0000000000000).isFinite=true:=by change (GT 0x3CC0000000000000).toModel.unpack.isFinite=true;rw[hu];rfl have h3:R (GT 0x3CC0000000000000)=1/2^51:=by rw[BW,hu,Model.unpackedToReal_finite,Be];simp;norm_num have h4:(n:ℝ)+8<=2^21:=by have:n+8<=2^21:=א;exact_mod_cast this obtain ⟨hf,he⟩:=Cx _ _ h1.1 h2 (by rw[h1.2,h3,ht];have h5:(1:ℝ)<=2^256*2^42:=ב;have h6:=BX;have h7:(2:ℝ)^21<=2^256*2^42:=ב;refine ظ (г ?_ ?_ (П _) Ce.le) DF;. rw[Nat.cast_add,У (by positivity)];push_cast;linarith . rw[У (by positivity)];have:(1:ℝ)/2^51<=1:=ב linarith) unfold nfOf;refine ⟨hf,?_⟩;rw[he,h1.2,h3,ht];have:=BU ((n:ℤ)+8) (-51) א ב;push_cast at this ⊢;have e:(2:ℝ)^(-51:ℤ)=1/2^51:=ב;rw[e] at this;rw[this];ring lemma Fc (c Y S T E z:ℝ) (hc1:8<=c) (hc2:c<=2^20+8) (hz1:z<=1/2^200*(1/2^200)) (hY:0<=Y) (hS:|S| ≤ 2*Y) (hT:|T-Y| ≤ Y/2^30+1/2^250) (hE:|E-c/2^51*T| ≤ 1/2^53*(|c/2^51*T| + z)) (hbig:1/2^127 ≤ |S+E| + 1/2^53*(|S+E| + z)):4*(c-7)*(1/2^53*Y)+1/2^53*(|S| + |E| + z)< E:=by have hc0:0< c:=ד;obtain ⟨hT1,hT2⟩:=abs_le.mp hT;have hT':|T| ≤ 2*Y+1/2^250:=by apply abs_le.mpr;constructor<;>linarith have hcG:c/2^51<=1/2^30:=by have:(2:ℝ)^20+8<=2^21:=ב calc c/2^51<=2^21/2^51:=by gcongr;linarith _=1/2^30:=ב set G:=c/2^51*T with hGdef have hG:|G| ≤ (2*Y+1/2^250)/2^30:=by rw[hGdef,abs_mul,abs_of_pos (by positivity)] calc _<=(1/2^30:ℝ)*(2*Y+1/2^250):= г hcG hT' (П _) (by positivity) _=_:=by ring have hE1:|E| ≤ |E-G| + |G|:=by have e:E=(E-G)+G:=by ring have:=Ч (E-G) G;rwa [<-e] at this have hSE:=Ч S E;have hG0:=П G;have hE0:=П E;have hYl:1/2^129<=Y:=ד;have hT3:Y-Y/2^30-1/2^250<=T:=ד;have hT4:0<=Y-Y/2^30-1/2^250:=ד;have hT5:0<=T:=ד;have hG1:0<=G:=by rw[hGdef];positivity rw[У hG1] at hE hE1 hG;have hE2:=(abs_le.mp hE).1;have hG2:c*(Y-Y/2^30-1/2^250)/2^51<=G:=by rw[hGdef,show c/2^51*T=c*T/2^51 by ring];gcongr have hw1:c*Y<=2^21*Y:=by have:c<=2^21:=by have:(2:ℝ)^20+8<=2^21:=ב;linarith exact Б this hY have hG3:c*(Y-Y/2^30-1/2^250) =c*Y-c*Y/2^30-c*(1/2^250):=by ring rw[hG3] at hG2 have hc3:c*(1/2^250)<=2^21*(1/2^250):=by have:c<=2^21:=by have:(2:ℝ)^20+8<=2^21:=ב;linarith exact Б this (by positivity) have hgoal:4*(c-7)*(1/2^53*Y)=c*Y/2^51-28*Y/2^53:=by ring rw[hgoal];linarith lemma DR (n:ℕ) (hn:n<2^20) (s t:ZW) (hs:s.isFinite=true) (ht:t.isFinite=true) (X Y:ℝ) (hY0:0<=Y) (hYB:Y<=A_B/8) (h1:|R s-X| ≤ 4*((n:ℝ)+1)*(1/2^53*Y)) (h2:|R t-Y| ≤ Y/2^30+1/2^250) (h3:|R s| ≤ 2*Y) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+nfOf n*t)|):R (s-nfOf n*t)< X∧X< R (s+nfOf n*t):=by have hB:=Ce;have hB8:(8:ℝ)<=A_B:=by have h:(8:ℝ)<=2^256*2^42:=ב;linarith [BX] have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hn0:(0:ℝ)<=n:=Э n;obtain ⟨hnf,hnv⟩:=G5 n hn have hT:|R t| ≤ 3/8*A_B:=by have e:R t=(R t-Y)+Y:=by ring have:=Ч (R t-Y) Y;rw[<-e,У hY0] at this;have h:(1:ℝ)/2^250<=1:=ב;have h':Y/2^30<=Y:=by apply div_le_self hY0;norm_num linarith have hT3:|R t| ≤ A_B:=ד have hN:|R (nfOf n)| ≤ A_B:=by rw[hnv,У (by positivity)];have:((n:ℝ)+8)/2^51<=1:=by rw[div_le_one (by positivity)];norm_num at hnR ⊢;linarith linarith obtain ⟨hef,heE⟩:=CP _ _ hnf ht hN hT3 have hNT:|R (nfOf n)*R t| ≤ |R t|:=by rw[abs_mul,hnv,У (show 0<=((n:ℝ)+8)/2^51 by positivity)];have:((n:ℝ)+8)/2^51<=1:=by rw[div_le_one (by positivity)];norm_num at hnR ⊢;linarith nlinarith [П (R t)] have hz:=By;have hz1:=CY;have hz2:A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=ב;linarith have hE:|R (nfOf n*t)| ≤ A_B:=by have e:R (nfOf n*t)=(R (nfOf n*t)-R (nfOf n)*R t)+R (nfOf n)*R t:=by ring have:=Ч (R (nfOf n*t)-R (nfOf n)*R t) (R (nfOf n)*R t);rw[<-e] at this;linarith have hSB:|R s| ≤ A_B:=ד;obtain ⟨hpf,hpE⟩:=DG s _ hs hef hSB hE;obtain ⟨hmf,hmE⟩:=FU s _ hs hef hSB hE;have hSb1:|R s+R (nfOf n*t)| ≤ |R s| + |R (nfOf n*t)|:=Ч _ _;have hSb2:|R s-R (nfOf n*t)| ≤ |R s| + |R (nfOf n*t)|:=abs_sub _ _;rw[hnv] at heE have hP:|R (s+nfOf n*t)| ≤ |R s+R (nfOf n*t)| + |R (s+nfOf n*t)-(R s+R (nfOf n*t))|:=by have e:R (s+nfOf n*t)=(R s+R (nfOf n*t)) + (R (s+nfOf n*t)-(R s+R (nfOf n*t))):=by ring have:=Ч (R s+R (nfOf n*t)) (R (s+nfOf n*t)-(R s+R (nfOf n*t))) rwa [<-e] at this have hbig':(1:ℝ)/2^127 ≤ |R s+R (nfOf n*t)| + 1/2^53*(|R s+R (nfOf n*t)| + A_z):=by have e:(2:ℝ)^(-127:ℤ)=1/2^127:=ב;rw[e] at hbig;linarith have key:=Fc ((n:ℝ)+8) Y (R s) (R t) (R (nfOf n*t)) A_z ד (by norm_num at hnR ⊢;linarith) hz1 hY0 h3 h2 heE hbig';have e7:4*((n:ℝ)+8-7)=4*((n:ℝ)+1):=by ring rw[e7] at key;obtain ⟨h1a,h1b⟩:=abs_le.mp h1;obtain ⟨hpa,hpb⟩:=abs_le.mp hpE;obtain ⟨hma,hmb⟩:=abs_le.mp hmE;constructor<;>linarith lemma FZ (g h:ℕ->ZW) (l:List ℕ) (a b:ZW): l.foldl (λ(acc:F1) t=>F1.mk (acc.s+g t) (acc.t+h t)) ⟨a,b⟩ =⟨(l.map g).foldl (.+·) a,(l.map h).foldl (.+·) b⟩:=by induction l generalizing a b with | nil=>rfl | cons x l ih=>simp only[GZ,List.map_cons,ih] lemma D2 (l:List ZW) (hf:∀ q∈l,q.isFinite=true):A_sa (l.map Float.abs)=A_sa l∧A_sr (l.map Float.abs)=A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => obtain ⟨h1,h2⟩:=ih (λx hx=>hf x (by simp[hx]));have h3:=(CN q (hf q ג)).2;simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.map_map] at h1 h2 ⊢;simp only[Function.comp_def,h3,abs_abs];simp only[Function.comp_def] at h1 h2;rw[h1,h2];simp lemma Cu (ts:List ℕ) (q:ℕ->ZW) (p:ℕ->ℝ) (hq:∀ t∈ts,|R (q t)-p t| ≤ 1/2^53*(|p t| + A_z)): |A_sr (ts.map q)-(ts.map p).sum| ≤ 1/2^53*((ts.map λt=>|p t|).sum+ts.length*A_z) ∧ A_sa (ts.map q)<=(ts.map λt=>|p t|).sum + 1/2^53*((ts.map λt=>|p t|).sum+ts.length*A_z) ∧ (ts.map λt=>|p t|).sum<=2*A_sa (ts.map q)+ts.length*A_z:=by induction ts with | nil=>simp[A_sa,A_sr] | cons x ts ih => obtain ⟨h1,h2,h3⟩:=ih (λt ht=>hq t (by simp[ht])) have h4:=hq x ג simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.map_map,List.length_cons] at h1 h2 h3 ⊢ simp only[Function.comp_def] at h1 h2 h3 ⊢ have hz:=By obtain ⟨h5,h6⟩:=abs_le.mp h4 obtain ⟨h7,h8⟩:=abs_le.mp h1 have hpq:|p x| ≤ |R (q x)| + 1/2^53*(|p x| + A_z):=by have e:p x=R (q x)-(R (q x)-p x):=by ring have:=abs_sub (R (q x)) (R (q x)-p x) rw[<-e] at this linarith have hqp:|R (q x)| ≤ |p x| + 1/2^53*(|p x| + A_z):=by have e:R (q x)=p x+(R (q x)-p x):=by ring have:=Ч (p x) (R (q x)-p x) rw[<-e] at this linarith have hp0:=П (p x) refine ⟨abs_le.mpr ⟨?_,?_⟩,?_,?_⟩<;>push_cast<;>nlinarith lemma D7 (ts:List ℕ) (f:ℕ->ℝ) (M:ℝ) (h:∀ t∈ts,f t<=M):(ts.map f).sum<=ts.length*M:=by induction ts with | nil=>simp | cons x ts ih => have h1:=ih (λt ht=>h t (by simp[ht]));have h2:=h x ג;simp only[List.map_cons,List.sum_cons,List.length_cons];push_cast;linarith lemma Bp (w:ZQ) (hg:good w=true):|wv w| = A_mag (w.toNat%2^31):=by rw[CA w hg,abs_mul,У (BD _)];split_ifs<;>simp lemma BB (w:ZQ) (hg:good w=true):|wv w| ≤ 2^128:=by rw[Bp w hg];apply EU;rcases C2 w hg with h | h<;>omega lemma C9 (w:ZQ) (hg:good w=true) (h:w &&& 0x7FFFFFFF=0):wv w=0:=by rw[CA w hg,<-BA w,h];simp[A_mag] lemma CC:(2:ℝ)^128<=A_B:=by have h:(2:ℝ)^128<=2^256*2^42:=ב;linarith [BX] lemma E0 (a b:ZQ) (ha:good a=true) (hb:good b=true): (dv a*dv b).isFinite=true ∧ |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 ⟨fa,ra⟩:=Ch a ha obtain ⟨fb,rb⟩:=Ch b hb have la:=BB a ha have lb:=BB b hb have hab:|wv a*wv b| ≤ 2^256:=by rw[abs_mul] calc |wv a| * |wv b| ≤ 2^128*2^128:=г la lb (П _) (by positivity) _=2^256:=ב obtain ⟨h1,h2⟩:=CP _ _ fa fb (by rw[ra];exact la.trans CC) (by rw[rb];exact lb.trans CC) rw[ra,rb] at h2 exact ⟨h1,h2,hab⟩ lemma DN (n:ℕ) (xw ww:ℕ->ZQ) (bw:ZQ) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (h0:∀ t,t< n->t∉l->wv (xw t)*wv (ww t)=0):B4 n xw ww bw=(l.map λt=>wv (xw t)*wv (ww t)).sum+wv bw:=by unfold B4;congr 1;rw[<-List.sum_toFinset _ hl];symm;apply Finset.sum_subset;. intro t ht;simp only[List.mem_toFinset] at ht;simpa using hl2 t ht . intro t ht hnl;exact h0 t (by simpa using ht) (by simpa using hnl) lemma Cl (n:ℕ) (xw ww:ℕ->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0):B4 n xw ww bw=(l.map λt=>wv (xw t)*wv (ww t)).sum+wv bw:= DN n xw ww bw l hl hl2 λt h1 h2=>by rcases hz t h1 h2 with h | h . simp[C9 (xw t) (hx t h1) h] . simp[C9 (ww t) (hw t h1) h] lemma Ez (n:ℕ) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n):l.length<=n:=by have h:l.toFinset ⊆ GR n:=by intro t ht;simp only[List.mem_toFinset] at ht;simpa using hl2 t ht have:=Finset.card_le_card h;rwa [List.toFinset_card_of_nodup hl,Finset.card_range] at this lemma DB (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g:ℕ->ZW) (p:ℕ->ℝ) (b:ZW) (bv:ℝ) (hg:∀ t∈ts,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ 2^256) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl:|bv| ≤ 2^128) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n*A_fsum ((ts.map g ++ [b]).map Float.abs))|): R (A_fsum (ts.map g ++ [b])-nfOf n*A_fsum ((ts.map g ++ [b]).map Float.abs)) < (ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (A_fsum (ts.map g ++ [b]) + nfOf n*A_fsum ((ts.map g ++ [b]).map Float.abs)):=by have hfq:∀ q∈ts.map g ++ [b],q.isFinite=true:=by intro q hq simp only[List.mem_append,List.mem_map,List.mem_singleton] at hq rcases hq with ⟨t,ht,rfl⟩ | rfl . exact (hg t ht).1 . exact hbf obtain ⟨hA1,hA2,hA3⟩:=Cu ts g p (λt ht=>(hg t ht).2.1) have hAle:=D7 ts (λt=>|p t|) _ (λt ht=>(hg t ht).2.2) have hsa:=Bv (ts.map g) b have hsr:=C7 (ts.map g) b have hsag:=Bb (ts.map g) have hsa0:=Bb (ts.map g ++ [b]) rw[hbv] at hsa hsr have hk:(ts.length:ℝ)<=n:=by exact_mod_cast hlen have hk0:(0:ℝ)<=ts.length:=Э _ have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hz:=By have hz1:=CY have hz2:A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=ב linarith have hA0:0<=(ts.map λt=>|p t|).sum:=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨t,_,rfl⟩;exact П _) set A:=(ts.map λt=>|p t|).sum set k:ℝ:=(ts.length:ℝ) have hkz:k*A_z<=2^20:=by nlinarith have hAl:A<=2^20*2^256:=by refine hAle.trans (Б ?_ (by positivity)) linarith have hBl:=BX have hnum:(2:ℝ)^20*2^256*3+2^128+2^20+2<=2^256*2^42/8:=by norm_num have hsaB:A_sa (ts.map g ++ [b])+A_z<=A_B/8:=by have hU:(1/2^53:ℝ)*(A+k*A_z)<=A+k*A_z:=by have:0<=A+k*A_z:=by positivity nlinarith linarith have hB8:(1:ℝ)<=A_B/8:=by have:(1:ℝ)<=2^256*2^42/8:=ב linarith have hlen1:(ts.map g ++ [b]).length<=2^21:=by simp;omega obtain ⟨hf1,he1⟩:=Bc _ hfq hlen1 ד obtain ⟨habs1,habs2⟩:=D2 _ hfq obtain ⟨hf2,he2⟩:=Bc ((ts.map g ++ [b]).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=List.mem_map.mp hq exact (CN x (hfq x hx)).1) (by simpa using hlen1) (by rw[habs1];linarith) rw[habs1,habs2] at he2 have hl1:(((ts.map g ++ [b]).length:ℕ):ℝ)=k+1:=by simp[k] have hl2:(((ts.map g ++ [b]).map Float.abs).length:ℝ)=k+1:=by simp[k] rw[hl1] at he1 rw[hl2] at he2 generalize hsaD:A_sa (ts.map g ++ [b])=sa at * set Y:=sa+A_z with hYdef have hY0:0<=Y:=by positivity have hUY:0<=(1/2^53:ℝ)*Y:=by positivity have hKU:2*(k+1)*(1/2^53*Y)<=Y/2^30:=by have h:k+1<=2^20:=ד have:=Б h hUY norm_num at this ⊢ linarith have hS1:|A_sr (ts.map g ++ [b])| ≤ sa:=by rw[<-hsaD];exact Bo _ obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨he2a,he2b⟩:=abs_le.mp he2 refine DR n hn _ _ hf1 hf2 _ Y hY0 ד ?_ ?_ ?_ hbig . have hKY:sa+k*A_z<=(k+1)*Y:=by rw[hYdef];nlinarith [Ь hk0 hsa0] have hU2:(1/2^53:ℝ)*(A+k*A_z)<=2*(k+1)*(1/2^53*Y):=by have h1:A+k*A_z<=2*(sa+k*A_z):=by linarith [П bv] have h2:A+k*A_z<=2*((k+1)*Y):=ד calc (1/2^53:ℝ)*(A+k*A_z)<=1/2^53*(2*((k+1)*Y)):= Й h2 ב _=_:=by ring have hn4:4*(k+1)*(1/2^53*Y)<=4*((n:ℝ)+1)*(1/2^53*Y):=Б ד hUY;obtain ⟨hA1a,hA1b⟩:=abs_le.mp hA1;apply abs_le.mpr;constructor<;>linarith . apply abs_le.mpr;constructor . have:(0:ℝ)<=1/2^250:=ב;have:A_z<=1/2^250:=by have:(1/2^200*(1/2^200):ℝ)<=1/2^250:=ב;linarith linarith . linarith . have e:R (A_fsum (ts.map g ++ [b])) =(R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b]))+A_sr (ts.map g ++ [b]):=by ring have h1:=Ч (R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b])) (A_sr (ts.map g ++ [b]));rw[<-e] at h1;have h2:Y/2^30<=Y:=by apply div_le_self hY0;norm_num linarith lemma CF (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List ZU) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:ZW) (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)< B4 n xw ww bw∧B4 n xw ww bw< R (s+e):=by set g:ℕ->ZW:=λt=>dv (xw t)*dv (ww t) with hg set p:ℕ->ℝ:=λt=>wv (xw t)*wv (ww t) with hp have hfold:l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩ = ⟨(l.map g).foldl (.+·) 0,(l.map λt=>(g t).abs).foldl (.+·) 0⟩:= FZ g (λt=>(g t).abs) l 0 0 obtain ⟨hs1,hs2⟩:=hs rw[hfold] at hs1 hs2 have hlen:=Ez n l hl hl2 have hgt:∀ t∈l,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ 2^256:=λt ht=>E0 _ _ (hx t (hl2 t ht)) (hw t (hl2 t ht)) obtain ⟨hbf,hbv⟩:=Ch bw hb have hS:s=A_fsum (l.map g ++ [dv bw]):=by rw[hs1,Ba];rfl have hE:e=nfOf n*A_fsum ((l.map g ++ [dv bw]).map Float.abs):=by rw[hs2] congr 1 rw[List.map_append,List.map_map,List.map_singleton,Ba] rfl rw[hS,hE] at hbig ⊢ rw[Cl n xw ww bw hx hw l hl hl2 hz] exact DB n hn l hlen g p (dv bw) (wv bw) hgt hbf hbv (BB bw hb) hbig lemma C6 (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List ZU) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0) (s e:ZW) (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)< B4 n xw ww bw∧B4 n xw ww bw< R (s+e):= CF n hn xw ww bw hx hw hb l hl hl2 (λt h1 h2=>Or.inl (hz t h1 h2)) s e hs hbig lemma Eg (H J Rj Rg Rt b z k:ℝ) (hH0:0<=H) (hH:H<=2^128) (hJ:0<=J) (hb:0<=b) (hz0:0<=z) (hz1:z<=1/2^200*(1/2^200)) (hk:k<=2^20) (hj:|Rj-J| ≤ 2*k*(1/2^53*(J+z))) (hg:|Rg-H*Rj| ≤ 1/2^53*(|H*Rj| + z)) (ht:|Rt-(Rg+b)| ≤ 1/2^53*(|Rg+b| + z)): |Rt-(H*J+b+z)| ≤ (H*J+b+z)/2^30+1/2^250:=by have e1:|Rj-J| ≤ (J+z)/2^32:=by refine hj.trans ?_ have h:0<=(1/2^53:ℝ)*(J+z):=Ь ב (add_nonneg hJ hz0) have:=Б hk h norm_num at this ⊢ linarith have e2:|H*Rj-H*J| ≤ (H*J+H*z)/2^32:=by rw[<-mul_sub,abs_mul,У hH0] calc H * |Rj-J| ≤ H*((J+z)/2^32):=Й e1 hH0 _=_:=by ring have e3:H*z<=1/2^250:=by calc H*z<=2^128*(1/2^200*(1/2^200)):= г hH hz1 hz0 ב _<=1/2^250:=ב have hW:0<=H*J:=Ь hH0 hJ have hZ0:0<=H*z:=Ь hH0 hz0 generalize H*J=W at * generalize H*z=Z at * generalize H*Rj=G at * obtain ⟨e2a,e2b⟩:=abs_le.mp e2 obtain ⟨hga,hgb⟩:=abs_le.mp hg obtain ⟨hta,htb⟩:=abs_le.mp ht have hG:|G| ≤ W+(W+Z)/2^32:=by apply abs_le.mpr constructor<;>linarith have hRg:|Rg| ≤ 2*W+1/2^200:=by apply abs_le.mpr constructor<;>linarith have hR:|Rg+b| ≤ 2*W+b+1/2^200:=by have:=Ч Rg b rw[У hb] at this linarith apply abs_le.mpr constructor<;>linarith lemma EK (ts:List ℕ) (f g:ℕ->ℝ) (H:ℝ) (h:∀ t∈ts,f t<=H*g t):(ts.map f).sum<=H*(ts.map g).sum:=by induction ts with | nil=>simp | cons x ts ih => have h1:=ih (λt ht=>h t (by simp[ht]));have h2:=h x ג;simp only[List.map_cons,List.sum_cons,mul_add];linarith lemma D6 (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g:ℕ->ZW) (p:ℕ->ℝ) (b f a:ZW) (bv H J K:ℝ) (hg:∀ t∈ts,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z)) (hAJ:(ts.map λt=>|p t|).sum<=H*J) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl:|bv| ≤ 2^128) (hf:f.isFinite=true) (hH:R f=H) (hH0:0<=H) (hHl:H<=2^128) (hjf:a.isFinite=true) (hJ0:0<=J) (hJ2:J<=2^149) (hK:K<=2^20) (hje:|R a-J| ≤ 2*K*(1/2^53*(J+A_z))) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (A_fsum (ts.map g ++ [b])+nfOf n*(f*a+b.abs))|): R (A_fsum (ts.map g ++ [b])-nfOf n*(f*a+b.abs))<(ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (A_fsum (ts.map g ++ [b])+nfOf n*(f*a+b.abs)):=by have hfq:∀ q∈ts.map g ++ [b],q.isFinite=true:=by intro q hq simp only[List.mem_append,List.mem_map,List.mem_singleton] at hq rcases hq with ⟨t,ht,rfl⟩ | rfl . exact (hg t ht).1 . exact hbf obtain ⟨hA1,hA2,hA3⟩:=Cu ts g p (λt ht=>(hg t ht).2) have hsa:=Bv (ts.map g) b have hsr:=C7 (ts.map g) b have hsa0:=Bb (ts.map g ++ [b]) rw[hbv] at hsa hsr have hk:(ts.length:ℝ)<=n:=by exact_mod_cast hlen have hk0:(0:ℝ)<=ts.length:=Э _ have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hz:=By have hz1:=CY have hz2:A_z<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=ב linarith have hA0:0<=(ts.map λt=>|p t|).sum:=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨t,_,rfl⟩;exact П _) set A:=(ts.map λt=>|p t|).sum set k:ℝ:=(ts.length:ℝ) have hBl:=BX have hB8:(1:ℝ)<=A_B/8:=by have:(1:ℝ)<=2^256*2^42/8:=ב linarith have hkR:k<=2^20:=ד have hk2:2*K*(1/2^53)<=1:=by norm_num at hK ⊢ linarith have hje2:|R a-J| ≤ J+1:= calc |R a-J| ≤ 2*K*(1/2^53*(J+A_z)):=hje _=(2*K*(1/2^53))*(J+A_z):=by ring _<=1*(J+A_z):=Б hk2 ד _<=J+1:=ד have hRj:|R a| ≤ 2^151:=by have e:R a=(R a-J)+J:=by ring have h1:=Ч (R a-J) J rw[<-e,У hJ0] at h1 have:(2:ℝ)*2^149+1<=2^151:=ב linarith have h151:(2:ℝ)^151<=A_B:=by have:(2:ℝ)^151<=2^256*2^42:=ב linarith obtain ⟨hgf,hge⟩:=CP f a hf hjf (by rw[hH,У hH0];linarith [CC]) (hRj.trans h151) rw[hH] at hge have hHj:|H*R a| ≤ 2^128*2^151:=by rw[abs_mul,У hH0] exact г hHl hRj (П _) ב have hgb:|R (f*a)| ≤ A_B:=by have e:R (f*a)=(R (f*a)-H*R a)+H*R a:=by ring have h1:=Ч (R (f*a)-H*R a) (H*R a) rw[<-e] at h1 have:(2:ℝ)^128*2^151*2+1<=2^256*2^42:=ב have h2:(1/2^53:ℝ)*(|H*R a| + A_z) ≤ |H*R a| + 1:=by have:=П (H*R a) linarith linarith obtain ⟨haf,hav⟩:=CN b hbf rw[hbv] at hav obtain ⟨htf,hte⟩:=DG (f*a) b.abs hgf haf hgb (by rw[hav,abs_abs];linarith [CC]) rw[hav] at hte have key2:=Eg H J (R a) (R (f*a)) (R (f*a+b.abs)) |bv| A_z K hH0 hHl hJ0 (П _) hz.le hz1 hK hje hge hte have hHJ:H*J<=2^128*2^149:=г hHl hJ2 hJ0 ב have hHJ0:0<=H*J:=Ь hH0 hJ0 have hbv0:=П bv have hkz:k*A_z<=2^20*A_z:=Б hkR hz.le have hYB:H*J + |bv| + A_z<=A_B/8:=by have:(2:ℝ)^128*2^149+2^128+1<=2^256*2^42/8:=ב linarith have hsaY:A_sa (ts.map g ++ [b])+A_z<=3/2*(H*J + |bv| + A_z):=by linarith have hlen1:(ts.map g ++ [b]).length<=2^21:=by simp;omega obtain ⟨hf1,he1⟩:=Bc _ hfq hlen1 ד have hl1:(((ts.map g ++ [b]).length:ℕ):ℝ)=k+1:=by simp[k] rw[hl1] at he1 have hS1:=Bo (ts.map g ++ [b]) set Y:=H*J + |bv| + A_z with hYdef have hY0:0<=Y:=ד have hUY:0<=(1/2^53:ℝ)*Y:=Ь ב hY0 have hD1:2*(k+1)*(1/2^53*(A_sa (ts.map g ++ [b])+A_z)) ≤ 3*(k+1)*(1/2^53*Y):= calc 2*(k+1)*(1/2^53*(A_sa (ts.map g ++ [b])+A_z)) <=2*(k+1)*(1/2^53*(3/2*Y)):=by apply Й _ ד exact Й hsaY ב _=_:=by ring have hkY:A+k*A_z<=(k+1)*Y:=by have h1:A+A_z<=Y:=ד have h2:=Й h1 (ד:0<=k+1) have h3:=Ь hk0 hA0 linarith have hD2:(1/2^53:ℝ)*(A+k*A_z)<=(k+1)*(1/2^53*Y):= calc (1/2^53:ℝ)*(A+k*A_z)<=1/2^53*((k+1)*Y):= Й hkY ב _=_:=by ring have hn4:4*(k+1)*(1/2^53*Y)<=4*((n:ℝ)+1)*(1/2^53*Y):=Б ד hUY have hK3:3*(k+1)*(1/2^53*Y)<=Y/2:=by have h:k+1<=2^20:=ד have:=Б h hUY norm_num at this ⊢ linarith obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨hA1a,hA1b⟩:=abs_le.mp hA1 refine DR n hn _ _ hf1 htf _ Y hY0 hYB ?_ key2 ?_ hbig . apply abs_le.mpr;constructor<;>linarith . have e:R (A_fsum (ts.map g ++ [b])) =(R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b]))+A_sr (ts.map g ++ [b]):=by ring have h1:=Ч (R (A_fsum (ts.map g ++ [b]))-A_sr (ts.map g ++ [b])) (A_sr (ts.map g ++ [b]));rw[<-e] at h1;linarith lemma CI (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g v:ℕ->ZW) (p a:ℕ->ℝ) (b f:ZW) (bv H:ℝ) (hg:∀ t∈ts,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ H * |a t|) (hv:∀ t∈ts,(v t).isFinite=true∧R (v t)=a t∧|a t| ≤ 2^128) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl:|bv| ≤ 2^128) (hf:f.isFinite=true) (hH:R f=H) (hH0:0<=H) (hHl:H<=2^128) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (A_fsum (ts.map g ++ [b]) + nfOf n*(f*A_fsum (ts.map λt=>(v t).abs)+b.abs))|): R (A_fsum (ts.map g ++ [b])-nfOf n*(f*A_fsum (ts.map λt=>(v t).abs)+b.abs)) < (ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (A_fsum (ts.map g ++ [b]) + nfOf n*(f*A_fsum (ts.map λt=>(v t).abs)+b.abs)):=by have hkR:(ts.length:ℝ)<=2^20:=by have:ts.length<=2^20:=א exact_mod_cast this have hJ0:0<=(ts.map λt=>|a t|).sum:=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨t,_,rfl⟩;exact П _) have hJl:=D7 ts (λt=>|a t|) _ (λt ht=>(hv t ht).2.2) have hAJ:=EK ts (λt=>|p t|) (λt=>|a t|) H (λt ht=>(hg t ht).2.2) have hvl:(ts.map λt=>(v t).abs)=(ts.map v).map Float.abs:=by simp[List.map_map,Function.comp_def] have hvf:∀ q∈ts.map v,q.isFinite=true:=by intro q hq obtain ⟨t,ht,rfl⟩:=List.mem_map.mp hq exact (hv t ht).1 obtain ⟨hv1,hv2⟩:=D2 (ts.map v) hvf have hJ1:A_sa (ts.map v)=(ts.map λt=>|a t|).sum:=by simp only[A_sa,List.map_map] congr 1 apply List.map_congr_left intro t ht simp[(hv t ht).2.1] rw[hvl] at hbig ⊢ rw[hJ1] at hv1 hv2 set J:=(ts.map λt=>|a t|).sum have hJ2:J<=2^20*2^128:=hJl.trans (Б hkR ב) have hBl:=BX have hlenv:((ts.map v).map Float.abs).length<=2^21:=by simp;omega obtain ⟨hjf,hje⟩:=Bc ((ts.map v).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=List.mem_map.mp hq exact (CN x (hvf x hx)).1) hlenv (by rw[hv1] have:(2:ℝ)^20*2^128<=2^256*2^42/4:=ב linarith) rw[hv1,hv2] at hje have hl3:(((ts.map v).map Float.abs).length:ℝ)=(ts.length:ℝ):=by simp rw[hl3] at hje exact D6 n hn ts hlen g p b f _ bv H J _ (λt ht=>⟨(hg t ht).1,(hg t ht).2.1⟩) hAJ hbf hbv hbl hf hH hH0 hHl hjf hJ0 (hJ2.trans ב) hkR hje hbig lemma FB (hh:ZQ) (hg:good hh=true) (hp:hh<=0x7FFFFFFF): 0<=wv hh∧wv hh<=2^128 ∧ ∀ w,good w=true->w &&& 0x7FFFFFFF<=hh->|wv w| ≤ wv hh:=by have hhN:hh.toNat<2^31:=by have:=Ж.mp hp change hh.toNat<=2147483647 at this omega have hHeq:wv hh=A_mag hh.toNat:=by rw[CA hh hg,Г hhN] simp only[show ¬ 2^31<=hh.toNat by omega,↓reduceIte,one_mul] refine ⟨by rw[hHeq];exact BD _,ظ (le_abs_self _) (BB hh hg),?_⟩ intro w hg1 hm have h1:=Ж.mp hm rw[BA] at h1 rw[Bp _ hg1,hHeq] apply CV _ _ h1 rcases C2 _ hg1 with h | h . exact Or.inl h . exact Or.inr h.1 lemma DD (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw hh:ZQ) (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 &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (l:List ZU) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:ZW) (hs:s=l.foldl (λ(acc:ZW) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:ZW) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|): R (s-e)< B4 n xw ww bw∧B4 n xw ww bw< R (s+e):=by set g:ℕ->ZW:=λt=>dv (xw t)*dv (ww t) with hgd set p:ℕ->ℝ:=λt=>wv (xw t)*wv (ww t) with hpd set v:ℕ->ZW:=λt=>dv (ww t) with hvd obtain ⟨hs1,hs2⟩:=hs obtain ⟨hH0,hHl,hb1⟩:=FB hh hg hp obtain ⟨hbf,hbv⟩:=Ch bw hb obtain ⟨hhf,hhv⟩:=Ch hh hg have hgt:∀ t∈l,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z) ∧ |p t| ≤ wv hh * |wv (ww t)|:=by intro t ht have ht':=hl2 t ht obtain ⟨h1,h2,-⟩:=E0 _ _ (hx t ht') (hw t ht') refine ⟨h1,h2,?_⟩ simp only[hpd,abs_mul] exact Б (hb1 _ (hx t ht') (hm t ht')) (П _) have hvt:∀ t∈l,(v t).isFinite=true∧R (v t)=wv (ww t)∧|wv (ww t)| ≤ 2^128:=by intro t ht obtain ⟨h1,h2⟩:=Ch _ (hw t (hl2 t ht)) exact ⟨h1,h2,BB _ (hw t (hl2 t ht))⟩ have hS:s=A_fsum (l.map g ++ [dv bw]):=by rw[hs1,Ba] simp[A_fsum,List.foldl_map,hgd] have hE:e=nfOf n*(dv hh*A_fsum (l.map λt=>(v t).abs)+(dv bw).abs):=by rw[hs2] simp[A_fsum,List.foldl_map,hvd] rw[hS,hE] at hbig ⊢ rw[Cl n xw ww bw hx hw l hl hl2 hz] exact CI n hn l (Ez n l hl hl2) g v p (λt=>wv (ww t)) (dv bw) (dv hh) (wv bw) (wv hh) hgt hvt hbf hbv (BB bw hb) hhf hhv hH0 hHl hbig end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary section open Float.Model Float.Model.UnpackedFloat lemma Cr (m K:ZU): (ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1)) =⟨m/2^(K+1),(m/2^K)%2 != 0,decide (m%2^K≠0)⟩:=by show Nat.repeat ExtendedMantissa.shiftRightOne (K+1) _=_ induction K with | zero => simp[Nat.repeat,ExtendedMantissa.shiftRightOne,ExtendedMantissa.ofMantissaAndAccuracy,Nat.mod_one] | succ K ih => show ExtendedMantissa.shiftRightOne (Nat.repeat _ (K+1) _)=_ rw[ih] simp only[ExtendedMantissa.shiftRightOne,ExtendedMantissa.mk.injEq] refine ⟨?_,?_,?_⟩ . rw[ذ,<-Nat.pow_succ] . trivial . rw[Nat.mod_pow_succ];have h:0<2^K:=by positivity by_cases h1:m%2^K=0<;>by_cases h2:m/2^K%2=0<;>simp[h1,h2];omega lemma C4 (H a b:ZU) (hb:b< H):(H*a+b)/H=a∧(H*a+b)%H=b:=by have hH:0< H:=א;constructor;. rw[Nat.mul_add_div hH,с hb,Я] . rw[Nat.mul_add_mod,Г hb] lemma Db (m K r:ZU) (h1:2*r*2^K< m+2^K) (h2:m<(2*r+1)*2^K):(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1)).roundedMantissa=r:=by rw[Cr,Nat.pow_succ];have hH:0<2^K:=by positivity generalize 2^K=H at *;have hdm:=Nat.div_add_mod m (H*2);have hlt:m%(H*2)< H*2:=Ъ _ א;generalize m/(H*2)=q at *;generalize m%(H*2)=t at *;subst hdm have hq:q=r∨q+1=r:=by rcases Nat.lt_trichotomy q r with h | h | h;. right;by_contra hne;have:q+2<=r:=א;have h3:(q+2)*H<=r*H:=З H this;nlinarith . exact Or.inl h . exfalso;have h3:(r+1)*H<=q*H:=З H h;nlinarith rcases hq with rfl | rfl . have ht:t< H:=by nlinarith obtain ⟨d1,d2⟩:=C4 H (2*q) t ht;rw[show H*2*q+t=H*(2*q)+t by ring,d1,d2] by_cases h0:t=0 <;> simp[ExtendedMantissa.roundedMantissa,ExtendedMantissa.accuracy,Accuracy.roundToNearestEven,h0] . have ht:H< t:=by nlinarith obtain ⟨t',rfl⟩:∃ t',t=H+t':=⟨t-H,by omega⟩;have ht':t'< H:=א;obtain ⟨d1,d2⟩:=C4 H (2*q+1) t' ht';rw[show H*2*q+(H+t')=H*(2*q+1)+t' by ring,d1,d2];have h0:t'≠0:=א;have h2':(2*q+1)%2=1:=א;simp[ExtendedMantissa.roundedMantissa,ExtendedMantissa.accuracy,Accuracy.roundToNearestEven,h0,h2'] lemma CB (t:ZY):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma Bz (m L:ZU) (h1:2^L<=m) (h2:m<2^(L+1)):m.log2=L:=by have hne:m≠0:=by have:0<2^L:=by positivity omega apply le_antisymm;. have:=(Nat.log2_lt hne).2 h2;omega . exact (Nat.le_log2 hne).2 h1 lemma BG (s:Sign) (m:ZU) (e T:ZY) (K r:ZU) (hT:Format.binary32.targetExponent (totalExponent m e)=T) (hK:T=e+(K+1:ZU)) (h1:2*r*2^K< m+2^K) (h2:m<(2*r+1)*2^K): UnpackedFloat.round Format.binary32 s m e =let p:=shiftToTargetExponent Format.binary32 r T .exact if h:p.1.mantissa=0 then .zero s else .finite s p.1.mantissa p.2 (Н h):=by unfold UnpackedFloat.round decreaseExponent roundWithAccuracy simp only[hT] have h0:(e-T).toNat=0:=א have hs:shiftToTargetExponent Format.binary32 m e .exact =(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1),T):=by unfold shiftToTargetExponent shiftToExponent simp only[hT] have h3:(T-e).toNat=K+1:=א rw[h3,hK] simp only[h0,Nat.shiftLeft_zero,Int.natCast_zero,Int.sub_zero,hs,Db m K r h1 h2] lemma FA (r:ZU) (T:ZY) (h1:2^23<=r) (h2:r<2^24) (hT:-149<=T): shiftToTargetExponent Format.binary32 r T .exact =(ExtendedMantissa.ofMantissaAndAccuracy r .exact,T):=by have hl:=Bz r 23 h1 h2 unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,CB] have h3:max (((23:ZU):ZY)+1+T-24) (-149)=T:=א simp only[h3,sub_self,Int.toNat_zero,Int.natCast_zero,Int.add_zero] rfl lemma EV (T:ZY) (hT:-150<=T): shiftToTargetExponent Format.binary32 (2^24) T .exact =(ExtendedMantissa.ofMantissaAndAccuracy (2^23) .exact,T+1):=by have hl:(2^24:ZU).log2=24:=Bz _ 24 ב ב unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,CB] have h3:max (((24:ZU):ZY)+1+T-24) (-149)-T=1:=א rw[h3] rfl lemma Ck (s:Sign) (P:ZU) (E:ZY) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hp:0< P): (let p:=shiftToTargetExponent Format.binary32 P E .exact if h:p.1.mantissa=0 then UnpackedFloat.zero s else .finite s p.1.mantissa p.2 (Н h))=.finite s P E hp:=by have hne:P≠0:=א simp only[FA P E hP1 hP2 hE,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false] lemma Cs (s:Sign) (E:ZY) (hE:-148<=E) (hp:0<2^23): (let p:=shiftToTargetExponent Format.binary32 (2^24) (E-1) .exact if h:p.1.mantissa=0 then UnpackedFloat.zero s else .finite s p.1.mantissa p.2 (Н h))=.finite s (2^23) E hp:=by have hne:(2^23:ZU)≠0:=א simp only[EV (E-1) א,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false,sub_add_cancel] lemma D3 (s:Sign) (m:ZU) (e:ZY) (P:ZU) (E:ZY) (g:ZU) (hp:0< P) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hg:e+(g+2:ZU)=E) (hhi:2*m<(2*P+1)*2^(g+2)) (hlo1:P≠2^23->2*P*2^(g+2)<2*m+2^(g+2)) (hlo2:P=2^23->33554431*2^(g+2)<4*m):UnpackedFloat.round Format.binary32 s m e=.finite s P E hp:=by have e1:2^(g+2)=4*2^g:=by rw[ч];ring have e2:2^(g+1)=2*2^g:=by rw[Nat.pow_succ];ring have e3:2^(g+25)=2^25*2^g:=by rw[ч];ring have e4:2^(g+25+1)=2^26*2^g:=by rw[ч,ч];ring have e5:2^(g+24)=2^24*2^g:=by rw[ч];ring have hG:0<2^g:=by positivity rw[e1] at hhi;by_cases hc:2^(g+25)<=m . have hL:m.log2=g+25:=by apply Bz m _ hc;rw[e4];have:(2*P+1)*(4*2^g)<=(2^25-1)*(4*2^g):=З _ א;omega have hT:Format.binary32.targetExponent (totalExponent m e)=E:=by simp only[totalExponent,hL,CB];omega rw[BG s m e E (g+1) P hT א ?_ ?_];. exact Ck s P E hP1 hP2 hE hp . rw[e2];by_cases hP:P=2^23;. subst hP;rw[e3] at hc;omega . have:=hlo1 hP;rw[e1] at this;nlinarith . rw[e2];nlinarith . have hP:P=2^23:=by by_contra hne;have h1:=hlo1 hne;rw[e1] at h1;have h2:2*(2^23+1)*(4*2^g)<=2*P*(4*2^g):=З _ א;rw[e3] at hc;omega have h1:=hlo2 hP;subst hP;rw[e1] at h1;rw[e3] at hc;have hL:m.log2=g+24:=by apply Bz m _ (by rw[e5];omega);rw[show g+24+1=g+25 from rfl,e3];omega by_cases hE':-148<=E . have hT:Format.binary32.targetExponent (totalExponent m e)=E-1:=by simp only[totalExponent,hL,CB];omega rw[BG s m e (E-1) g (2^24) hT א א א];exact Cs s E hE' hp . have hE2:E=-149:=א;have hT:Format.binary32.targetExponent (totalExponent m e)=E:=by simp only[totalExponent,hL,CB];omega rw[BG s m e E (g+1) (2^23) hT א (by rw[e2];omega) (by rw[e2];omega)];exact Ck s _ E ב ב hE hp lemma BC (neg:ZT) (m:ZU) (e:ZY): GF Fmt ⟨neg,m,e⟩ =GV Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e):=by simp[GF,Model.ieeeRoundDyadic] rfl lemma Dk (s:Sign):s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl lemma Bf (s:Sign) (M:ZU) (E:ZY) (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 have hl:M.log2=23:=Bz M 23 hM1 hM2 simp only[UnpackedFloat.pack,hl] have hbias:(E+↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat=(E+150).toNat:=by simp only[Format.exponentBias];norm_num;ring_nf rw[hbias] have hno:¬ (2^Format.binary32.exponentBits<=(E+150).toNat+1):=by show ¬ (2^8<=(E+150).toNat+1);omega simp only[hno,ite_false] have hnorm:(23+1=Format.binary32.mantissaBits):=rfl simp only[hnorm,ite_true,packComponents,BitVec.toNat_append,Dk,BitVec.toNat_ofNat] have h8:(E+150).toNat%2^8=(E+150).toNat:=Г א rw[h8,<-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 EF (neg:ZT) (m:ZU) (e:ZY) (P:ZU) (E:ZY) (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 (GF Fmt ⟨neg,m,e⟩) =UInt32.ofNat ((if neg then 2^31 else 0)+(E+150).toNat*2^23+P%2^23):=by have hp:0< P:=א obtain ⟨k,hk⟩:∃ k:ZU,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,hg⟩:∃ g:ZU,(e-k)+(g+2:ZU)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have hb:(0:ℝ)<2^(e-k):=ю ב _ have hV:(m:ℝ)*2^e=((m*2^k:ZU):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-Ц,<-М С] congr 2;ring have hEE:(2:ℝ)^E=((2^(g+2):ZU):ℝ)*2^(e-k):=by push_cast rw[<-Ц,<-М С,<-hg] congr 1;push_cast;ring rw[hV,hEE] at hhi have h1:2*(m*2^k)<(2*P+1)*2^(g+2):=by have:(2:ℝ)*((m*2^k:ZU):ℝ)<(2*P+1)*((2^(g+2):ZU):ℝ):=я ד hb.le exact_mod_cast this have h2:P≠2^23->2*P*2^(g+2)<2*(m*2^k)+2^(g+2):=by intro hne have h3:=hlo1 hne rw[hV,hEE] at h3 have:(2:ℝ)*P*((2^(g+2):ZU):ℝ) < 2*((m*2^k:ZU):ℝ)+((2^(g+2):ZU):ℝ):=я ד hb.le exact_mod_cast this have h3:P=2^23->33554431*2^(g+2)<4*(m*2^k):=by intro hne have h3:=hlo2 hne rw[hV,hEE] at h3 have:(33554431:ℝ)*((2^(g+2):ZU):ℝ)<4*((m*2^k:ZU):ℝ):=я ד hb.le exact_mod_cast this rw[BC,<-ExactRuntime.round_scale _ _ m e k hm,D3 _ (m*2^k) (e-k) P E g hp hP1 hP2 hE hg h1 h2 h3] simp only[toWord,Model.toNatBits,GV,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[Bf _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma Fv (f:ZW):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 lemma Fn (f:ZW):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 lemma FN (f:ZW):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 Fx (f:ZW) (s:ZT) (E F:ZU) (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):Model.toDyadic? (toModel (ofFloat f))=some ⟨s,2^52+F,(E:ZY)-1075⟩:=by have he:=Fv f;have hf:=Fn f;have hs:=FN f;have he':f.toBits.toNat/2^52%2^11=E:=by split at h<;>omega have hf':f.toBits.toNat%2^52=F:=by split at h<;>omega have hs':decide (2^63<=f.toBits.toNat)=s:=by cases s;. simp only[GC,ite_false] at h;simp;omega . simp only[ite_true] at h;simp;omega rw[he'] at he;rw[hf'] at hf;rw[hs'] at hs;have hx:=Model.ofFields_signBit_expField_fracField (toModel (ofFloat f));rw[he,hf,hs] at hx;rw[<-hx];have hfin:GE (Model.ofFields B_D s E F)=true:=by rw[Model.isFinite_ofFields B_D _ _ _ (by show E<2^11;omega) hF];show (E != 2047)=true;simp;omega rw[Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ (by show E<2^11;omega) hF hfin];have hE0:E≠0:=א;simp only[hE0,ite_false,show B_D.exponentBias=1023 from rfl,show B_D.fracWidth=52 from rfl];simp only[show Model.pow2 52=2^52 from rfl];congr 2;simp only[Int.ofNat_eq_natCast];push_cast;ring lemma Dn (f:ZW) (d:FloatLib.Numerics.Dyadic) (h:Model.toDyadic? (toModel (ofFloat f))=some d):f.isFinite=true:=by rw[Model.toDyadic?_ieee_eq_model _ (by decide)] at h;show (GU (toModel (ofFloat f))).isFinite=true;revert h;generalize GU (toModel (ofFloat f))=u;cases u<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.isFinite] lemma B_R (f:ZW) (s:ZT) (E F:ZU) (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:ZU):ℝ)*2^((E:ZY)-1075):=by have hd:=Fx f s E F hE1 hE hF h refine ⟨Dn f _ hd,?_⟩ rw[R,Model.toReal_eq,hd] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def B_near (A:ZU) (S:ZT) (ex fr:ZU):Prop:= ∃ M,A=(if S then 2^63 else 0)+M∧(ex*2^23+fr)*2^29< M+2^28 ∧ M<=(ex*2^23+fr)*2^29+2^28 lemma B_x (A x:ZU) (hA:A<2^64) (hx:x=(A+268435455)%2^64/2^29) (h1:897<=x/2^23%2^11) (h2:x/2^23%2^11<1151):B_near A (decide (2^34<=x)) (x/2^23%2^11) (x%2^23):=by unfold B_near;by_cases h:2^34<=x;. simp only[h,decide_true,ite_true];exact ⟨A-2^63,by omega,by omega,by omega⟩ . simp only[h,decide_false,GC,ite_false];exact ⟨A,by omega,by omega,by omega⟩ lemma CH (A:ZR):((A+(0x0FFFFFFF:ZR)) >>> (29:ZR)).toNat=(A.toNat+268435455)%2^64/2^29:=by rw[м,Ы];norm_num [GM] lemma Dw (x:ZR):((x >>> (23:ZR)) &&& (0x7FF:ZR)).toNat=x.toNat/2^23%2^11:=by rw[э,м];have:=В (x.toNat >>> 23) 11;norm_num [GM] at this ⊢;exact this lemma Ej (v:ZR) (h:v-(897:ZR)<(254:ZR)):897<=v.toNat∧v.toNat<1151:=by rw[р,UInt64.toNat_sub] at h;have hv:=v.toNat_lt;norm_num at h hv;omega lemma FL (x:ZR) (hx:x.toNat<2^35) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151): (((x >>> (34:ZR)) <<< (31:ZR)) ||| ((x &&& (0x3FFFFFFFF:ZR))-((896:ZR) <<< (23:ZR)))).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 ha:((x >>> (34:ZR)) <<< (31:ZR)).toNat=(x.toNat/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,м] norm_num [GM,Nat.shiftLeft_eq] omega have hb:((x &&& (0x3FFFFFFFF:ZR))-((896:ZR) <<< (23:ZR))).toNat =x.toNat%2^34-896*2^23:=by have h2:(x &&& (0x3FFFFFFFF:ZR)).toNat=x.toNat%2^34:=by rw[э] exact В _ 34 have h3:((896:ZR) <<< (23:ZR))=7516192768:=by decide rw[h3,GO] . rw[h2];rfl . rw[UInt64.le_iff_toNat_le,h2];norm_num;omega have hc:(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 (щ א _).symm apply И.mp rw[UInt64.toNat_toUInt32,UInt64.toNat_or,ha,hb,hc,е] by_cases h:2^34<=x.toNat . simp only[h,decide_true,ite_true];omega . simp only[h,decide_false,GC,ite_false];omega lemma BZ (s e:ZW) (h:GA s e≠0xFFFFFFFF): ∃ (S:ZT) (ex fr:ZU),897<=ex∧ex<1151∧fr<2^23 ∧ GA 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:=by unfold GA at h ⊢ dsimp only at h ⊢ generalize (s+e).toBits=A at h ⊢ generalize (s-e).toBits=B at h ⊢ have hAn:=CH A have hBn:=CH B generalize (A+(0x0FFFFFFF:ZR)) >>> (29:ZR)=x at h hAn ⊢ generalize (B+(0x0FFFFFFF:ZR)) >>> (29:ZR)=y at h hBn ⊢ have hex:=Dw x generalize (x >>> (23:ZR)) &&& (0x7FF:ZR)=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 obtain ⟨h1,h2⟩:=Ej ex (by simpa using hc);rw[hex] at h1 h2;subst hxy;have hA:=A.toNat_lt;have hB:=B.toNat_lt;have hx35:x.toNat<2^35:=א;simp only[hc0,ite_true];exact ⟨_,_,_,h1,h2,by omega,FL x hx35 h1 h2,B_x A.toNat x.toNat hA hAn h1 h2,B_x B.toNat x.toNat hB hBn h1 h2⟩ . exact absurd rfl h set_option maxRecDepth 8192 in lemma BP (f:ZW) (S:ZT) (ex fr:ZU) (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:ZY)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:ZY)-1076)< v) ∧ (2^53-2^28)*2^((ex:ZY)-1076)< v:=by obtain ⟨M,hA,hl,hu⟩:=hn have hF:M%2^52<2^52:=Ъ _ ב have hM:M=(M/2^52)*2^52+M%2^52:=א generalize M/2^52=E at hM generalize M%2^52=F at hM hF subst hM have hb:(0:ℝ)<2^((ex:ZY)-1076):=ю ב _ have hE:E=ex∨(E+1=ex∧fr=0):=א obtain ⟨hfin,hR⟩:=B_R f S E F א א hF (by rw[hA,з]) refine ⟨hfin,((2^52+F:ZU):ℝ)*2^((E:ZY)-1075),?_,?_⟩ . rw[hR];cases S<;>simp;ring rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:ZY)-1075)=2*2^((E:ZY)-1076):=by rw[show (E:ZY)-1075=1+((E:ZY)-1076) by ring,М С,zpow_one] rw[e];generalize (2:ℝ)^((E:ZY)-1076)=b at hb ⊢;have h3:(F:ℝ)<=fr*2^29+2^28:=by exact_mod_cast (by omega:F<=fr*2^29+2^28) have h4':fr*2^29< F+2^28:=by have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl;exact Nat.lt_of_add_lt_add_left (by rw[з] at hl;exact hl) have h4:(fr:ℝ)*2^29< F+2^28:=by exact_mod_cast h4' 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;push_cast;refine ⟨?_,?_,?_⟩;. linarith . intro _;linarith . linarith . have e:(2:ℝ)^(((E+1:ZU):ZY)-1076)=2^((E:ZY)-1075):=by congr 1;push_cast;ring rw[e] at hb ⊢;generalize (2:ℝ)^((E:ZY)-1075)=b at hb ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52< F+2^28:=by exact_mod_cast (by omega:2^52< F+2^28) have h7:=Б h3 hb.le;have h8:=Ш h4 hb;push_cast;refine ⟨?_,?_,?_⟩;. linarith . intro h0;exact absurd rfl h0 . linarith lemma C1 (s e:ZW) (h:GA s e≠0xFFFFFFFF):(s+e).isFinite=true∧(s-e).isFinite=true∧(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|:=by obtain ⟨S,ex,fr,h1,h2,h3,-,ha,hb⟩:=BZ s e h;obtain ⟨f1,v,hv,-,-,h5⟩:=BP _ S ex fr h1 h2 h3 ha;obtain ⟨f2,-⟩:=BP _ S ex fr h1 h2 h3 hb;refine ⟨f1,f2,?_⟩;have hb0:(2:ℝ)^(-179:ℤ)<=2^((ex:ZY)-1076):=Ф ב א;have hpos:(0:ℝ)<2^(-179:ℤ):=ю ב _;have e1:(2:ℝ)^(-127:ℤ)=2^52*2^(-179:ℤ):=by rw[<-Ц,<-М С];norm_num have hv1:(2:ℝ)^(-127:ℤ)<=v:=by rw[e1];nlinarith have hv0:0< v:=lt_of_lt_of_le (ю ב _) hv1;have:|R (s+e)| = v:=by rw[hv];cases S<;>simp[abs_of_pos hv0] rwa [this] lemma BF (w:ZQ) (h:expOf w≠255):wv w=(rationalWord w:ℝ):=by rw[wv,Model.toReal_eq,ofW,toDyadic_ofUInt32 w h];simp only[rationalWord,FloatLib.Numerics.Dyadic.cast_toRat] lemma BM (w:ZQ) (h:good w=true):expOf w≠255:=by intro he;have he':w.toNat/2^23%2^8=255:=by have:=congrArg UInt32.toNat he;rw[expOf,О,л,show (255:ZQ).toNat=2^8-1 from rfl,В] at this;simpa [GM] using this have hv:(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[О,show (0x7FFFFFFF:ZQ).toNat=2^31-1 from rfl,В] simp only[good,GX,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. have:=congrArg UInt32.toNat h;rw[hv] at this;have h0:(0:ZQ).toNat=0:=rfl;omega . rw[Ж,UInt32.toNat_sub,hv] at h;norm_num at h;omega lemma Ee (a b:ZQ): ((sv a:ℚ)*sv b*(2:ℚ)^(idxOf a+idxOf b))*2^(-298:ZY) =rationalWord a*rationalWord b:=by simp only[rationalWord,decodeD_toRat] rw[show (-298:ZY)=-149+-149 by norm_num,М С] rw[zpow_sub₀ С,zpow_sub₀ С,Ц,Ц,pow_add] norm_num [zpow_neg,zpow_natCast] ring lemma FW (a b:ZS ZQ) (start n:ZU): (dsum a b start n:ℚ)*2^(-298:ZY) =∑ j∈GR n,rationalWord a[start+j]! * rationalWord b[start+j]!:=by 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] conv_rhs=>rw[Finset.sum_range_succ'] simp only[Ga] rw[Ee] have ht:(∑ j∈GR n,rationalWord a[start+1+j]! * rationalWord b[start+1+j]!) = ∑ j∈GR n,rationalWord a[start+(j+1)]! * rationalWord b[start+(j+1)]!:=by apply ы rfl intro j _ simp[Nat.add_comm,Nat.add_left_comm] rw[ht] ring lemma EL (a b:ZS ZQ) (hs:a.size=b.size) (hf:∀ j,j< a.size->expOf a[j]!≠255∧expOf b[j]!≠255) (hn:(dsum a b 0 a.size:ℚ)*2^(-298:ZY)≠0): ∃ d:FloatLib.Numerics.Dyadic,d.toRat=(dsum a b 0 a.size:ℚ)*2^(-298:ZY) ∧ dotSpec a b=toWord (GF Fmt d):=by let L:=a.map (GW (fmt:=Fmt)) let K:=b.map (GW (fmt:=Fmt)) have hLK:L.size=K.size:=by simp[L,K,hs] have hLf:∀ j (hj:j< L.size),GE L[j]=true:=by intro j hj have hj':j< a.size:=by simpa [L] using hj have he:=(hf j hj').1 rw[Е a j hj'] at he simp[L,isFinite_ofUInt32,he] have hKf:∀ j (hj:j< K.size),GE K[j]=true:=by intro j hj have hj':j< b.size:=by simpa [K] using hj have he:=(hf j א).2 rw[Е b j hj'] at he simp[K,isFinite_ofUInt32,he] have hex:=Model.Reduction.Internal.dotState_exact_toRat (destination:=Fmt) L K hLK hLf hKf have hmath:Model.Reduction.Semantics.finiteDotSlice L K 0 L.size ג (by simp[hLK])=(dsum a b 0 a.size:ℚ)*2^(-298:ZY):=by simpa [L,K] using finiteDotSlice_eq a b 0 a.size ג (by simp[hs]) (by intro j _ hj;exact hf j א) have hn':Model.Reduction.Semantics.finiteDot L.toList K.toList≠.ok 0:=by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L K hLK,hmath] simpa using hn refine ⟨(Model.Reduction.Internal.dotState L K hLK:Model.Reduction.Internal.State Fmt).exact,hex.trans hmath,?_⟩ unfold dotSpec Model.dot rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L K .nearestEven hLK hLf hKf hn'] rfl lemma CO (n:ZU) (fa fb:ZU->ZQ) (hf:∀ j,j< n->expOf (fa j)≠255∧expOf (fb j)≠255) (hn:(∑ j∈GR n,rationalWord (fa j)*rationalWord (fb j))≠0): ∃ d:FloatLib.Numerics.Dyadic, d.toRat=(∑ j∈GR n,rationalWord (fa j)*rationalWord (fb j)) ∧ dotOf n fa fb=toWord (GF Fmt d):=by have hsum:(dsum (GJ (n:=n) λi=>fa i) (GJ (n:=n) λi=>fb i) 0 n:ℚ) *2^(-298:ZY)=∑ j∈GR n,rationalWord (fa j)*rationalWord (fb j):=by rw[FW] apply ы rfl intro j hj have hj':=Finset.mem_range.mp hj rw[К,BL n fa j hj',BL n fb j hj'] have hs:(GJ (n:=n) λi=>fa i).size=n:=by simp obtain ⟨d,hd,hw⟩:=EL (GJ (n:=n) λi=>fa i) (GJ (n:=n) λi=>fb i) ג (by intro j hj have hj':j< n:=by simpa using hj rw[BL n fa j hj',BL n fb j hj'] exact hf j hj') (by rw[hs,hsum];exact hn) rw[hs,hsum] at hd exact ⟨d,hd,hw⟩ lemma Dm:rationalWord oneW=1:=by have he:expOf oneW=127:=by decide have hf:fracOf oneW=0:=by decide have hs:signOf oneW=false:=by decide norm_num [rationalWord,decodeD,he,hf,hs,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand] lemma Eu (n:ZU) (xw ww:ZU->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true): ((∑ j∈GR (n+1),rationalWord (if j< n then xw j else oneW) * rationalWord (if j< n then ww j else bw):ℚ):ℝ)=B4 n xw ww bw:=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,Dm,one_mul] rw[Rat.cast_add,Rat.cast_sum,B4,BF bw (BM bw hb)] congr 1 apply ы rfl intro t ht have ht':=Finset.mem_range.mp ht simp only[ht',ite_true] rw[BF _ (BM _ (hx t ht')),BF _ (BM _ (hw t ht')),Rat.cast_mul] lemma Ci (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:ZW) (hlo:R (s-e)< B4 n xw ww bw) (hhi:B4 n xw ww bw< R (s+e)) (h:GA s e≠0xFFFFFFFF): GA 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 ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=BZ s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=BP _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=BP _ S ex fr h1 h2 h3 hb' have hsum:=Eu n xw ww bw hx hw hb generalize B4 n xw ww bw=V at hlo hhi hsum have hb0:(0:ℝ)<2^((ex:ZY)-1076):=ю ב _ have hE:(2:ℝ)^((ex:ZY)-1046)=2^30*2^((ex:ZY)-1076):=by rw[<-Ц,<-М С] congr 1;push_cast;ring rw[hva] at hhi rw[hvb] at hlo obtain ⟨W,hVW,hW1,hW2,hW3⟩:∃ W:ℝ,V=(if S then -W else W) ∧ W<(2^53+fr*2^30+2^29)*2^((ex:ZY)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:ZY)-1076)< W) ∧ (2^53-2^28)*2^((ex:ZY)-1076)< W:=by cases S . simp only[GC,ite_false] at hhi hlo ⊢;exact ⟨V,rfl,lt_of_lt_of_le hhi hia,λh=>(lob1 h).trans hlo,lob.trans hlo⟩ . simp only[ite_true] at hhi hlo ⊢;exact ⟨-V,by simp,by linarith,λh=>by linarith [loa1 h],by linarith⟩ have hW0:0< W:=by refine в ?_ hW3 exact Ь ב hb0.le have hne:(∑ j∈GR (n+1),rationalWord (if j< n then xw j else oneW) * rationalWord (if j< n then ww j else bw):ℚ)≠0:=by intro h0 rw[h0,ع] at hsum rw[<-hsum] at hVW cases S . simp only[GC,ite_false] at hVW;linarith . simp only[ite_true] at hVW;linarith obtain ⟨⟨neg,m,q⟩,hd,hw'⟩:=CO (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (by intro j _ have hone:expOf oneW≠255:=by decide by_cases hj:j< n . simp only[hj,ite_true];exact ⟨BM _ (hx j hj),BM _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨hone,BM _ hb⟩) hne have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=V:=by rw[<-ж,hd,hsum] 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[GC,ite_false,ite_true,Int.ofNat_eq_natCast,Int.cast_neg,Int.cast_natCast] at hdV hVW . 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:=EF neg m q (2^23+fr) ((ex:ZY)-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:ZY)-1046+150).toNat=ex-896:=א have e2:(2^23+fr)%2^23=fr:=א rw[hz,hw',hword,e1,e2] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma DJ (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List ZU) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:ZW) (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:GA s e≠0xFFFFFFFF): GA 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⟩:=C1 s e h obtain ⟨hlo,hhi⟩:=CF n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact Ci n hn xw ww bw hx hw hb s e hlo hhi h lemma D5 (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw hh:ZQ) (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 &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (l:List ZU) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:ZW) (hs:s=l.foldl (λ(acc:ZW) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:ZW) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (h:GA s e≠0xFFFFFFFF): GA 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⟩:=C1 s e h obtain ⟨hlo,hhi⟩:=DD n hn xw ww bw hh hx hw hb hg hm hp l hl hl2 hz s e hs hbig exact Ci n hn xw ww bw hx hw hb s e hlo hhi h end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma BK (k:ZU) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma EM (a b:ZX) (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 Cf {α:Type} (a:ZS α) (h:a.size<2^32):a.usize.toNat=a.size:=BK _ h lemma B7 (a:ZS ZQ) (i:ZX) (hs:a.size<2^32):rdW a i=a[i.toNat]!:=by unfold rdW;split;. rename_i h;have h2:=BT a i h;rw[Е a i.toNat h2];rfl . rename_i h;have h':¬ i.toNat< a.size:=by intro h2;apply h;rw[А,Cf a hs];exact h2 rw[б a i.toNat h'];rfl lemma B9 (a:ZS ZQ) (i:ZX) (v:ZQ):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma Bt (a:ZS ZQ) (i:ZX) (v:ZQ) (q:ZU) (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[А,Cf a hs];exact hi unfold wrW;rw[Р h];by_cases hq:q< a.size . have h3:q<(a.uset i v (BT a i h)).size:=by simpa using hq rw[Е _ q h3,Е a q hq];simp only[Array.uset,Array.getElem_set];by_cases e:q=i.toNat<;>simp[e,eq_comm] . have h3:¬ q<(a.uset i v (BT a i h)).size:=by simpa using hq rw[б _ q h3,б a q hq,Д א] def C_Inv (N:ZU) (sp:ZU->Word) (S:ZU->Prop) (out:ZS Word):Prop:= out.size=N∧∀ q,q< N->S q->out[q]! = sp q lemma E4 {N:ZU} {sp:ZU->Word} {S:ZU->Prop} {out:ZS Word} (hN:N<2^32) (h:C_Inv N sp S out) {u:ZX} {p:ZU} {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[B9,h1],λq hq hS=>?_⟩;rw[Bt _ _ _ _ א א,hu];by_cases e:q=p;. rw[Л e,e,hv] . rw[Д e];exact h2 q hq (hS.resolve_left e) lemma Dr {N:ZU} {sp:ZU->Word} {S S':ZU->Prop} {out:ZS 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 FY {N:ZU} {sp:ZU->Word} {S:ZU->Prop} {out:ZS Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S q):out=GJ (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h;apply ъ;. 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] lemma Dd (v:ZQ) (h1:(0x7FFFFF:ZQ)<=v-1) (h2:v<0x7F800000):(v == 0 || v-(0x00800000:ZQ)<=(0x7EFFFFFF:ZQ))=true:=by have a1:(0x7FFFFF:ZQ).toNat=0x7FFFFF:=rfl;have a2:(0x7F800000:ZQ).toNat=0x7F800000:=rfl;have a3:(0x00800000:ZQ).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:ZQ).toNat=0x7EFFFFFF:=rfl;have a5:(1:ZQ).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:ZQ)<=v:=by rw[Ж];have:v.toNat≠0:=λh=>h0 (И.mp h);omega rw[Ж,س _ _ h3] at h1;rw[п] at h2;have h4:(0x00800000:ZQ)<=v:=by rw[Ж];omega simp only[GX,decide_eq_true_eq];right;rw[Ж,س _ _ h4];omega lemma Fw (c u lo:ZQ) (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 Fr (c v hi:ZQ) (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 Bj (w:ZQ) (h:good w=true):good (w &&& 0x7FFFFFFF)=true:=by have e:(w &&& 0x7FFFFFFF) &&& 0x7FFFFFFF=w &&& 0x7FFFFFFF:=by rw[UInt32.and_assoc,UInt32.and_self] unfold good at h ⊢;simpa only [e] using h lemma EE {h:ZQ} (h1:h<0x7F800000):h<=0x7FFFFFFF:=by have a1:(0x7F800000:ZQ).toNat=0x7F800000:=rfl;have a2:(0x7FFFFFFF:ZQ).toNat=0x7FFFFFFF:=rfl;rw[п] at h1;rw[Ж];omega def C_Hv (A:ZS Word) (h:Word):Prop:= good h=true∧h<=0x7FFFFFFF∧(∀ k,k< A.size->good A[k]! = true) ∧ ∀ k,k< A.size->A[k]! &&& 0x7FFFFFFF<=h lemma EW (r v hi:ZQ) (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 Ei (A:ZS ZQ) (n:ZX) (hn:n.toNat<=A.size) (d:ZU): ∀ (i:ZX) (lo hi:ZQ),i.toNat+d=n.toNat->scan A n i hn lo hi≠0xFFFFFFFF → (0x7FFFFF:ZQ)<=lo∧hi<0x7F800000∧hi<=scan A n i hn lo hi ∧ scan A n i hn lo hi<0x7F800000∧(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]! &&& 0x7FFFFFFF<=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<0x7F800000 then hi else 0xFFFFFFFF:=by rw[scan,Х (by rw[А];omega)] rw[e] at h ⊢ by_cases c:0x007FFFFF<=lo∧hi<0x7F800000 . rw[Л c];exact ⟨c.1,c.2,UInt32.le_refl _,c.2,id,λk h1 h2=>by omega⟩ . rw[Д c] at h;exact absurd rfl h | succ d ih => intro i lo hi he h have hi':i< n:=by rw[А];omega have hb:i.toNat< A.size:=א have e:scan A n i hn lo hi=scan A n (i+1) hn (if (A.uget i hb &&& 0x7FFFFFFF)-1< lo then (A.uget i hb &&& 0x7FFFFFFF)-1 else lo) (if hi< A.uget i hb &&& 0x7FFFFFFF then A.uget i hb &&& 0x7FFFFFFF 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⟩:=Fw _ _ _ h1 obtain ⟨h9,h10⟩:=Fr _ _ _ h2 obtain ⟨h11,h12⟩:=EW _ _ _ h3 have hg:good (A.uget i hb)=true:=Dd _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact Bj _ 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 B8 (A:ZS ZQ) (hs:A.size<2^32) (h:scan A A.usize 0 (BR A) 0xFFFFFFFF 0≠0xFFFFFFFF):C_Hv A (scan A A.usize 0 (BR A) 0xFFFFFFFF 0):=by have e:=Cf A hs;have h0:(0:ZX).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩:=Ei A A.usize (BR A) A.size 0 _ _ (by rw[h0,e];omega) h;have h7:∀ k,k< A.size->good A[k]! = true ∧ A[k]! &&& 0x7FFFFFFF<=scan A A.usize 0 (BR A) 0xFFFFFFFF 0:=λk hk=>h6 k א א;exact ⟨h5 rfl,EE h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma EH (w:ZQ):w &&& 0x7FFFFFFF<=0x7FFFFFFF:=by rw[Ж,О];exact Nat.and_le_right def C_hok (w:ZU->Word) (t:ZU) (h:Word):Prop:= good h=true∧(∀ k,k< t->w k &&& 0x7FFFFFFF<=h)∧h<=0x7FFFFFFF lemma Df {w:ZU->Word} {t:ZU} {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:=Bj _ hg;unfold mx;dsimp only;by_cases e:w t &&& 0x7FFFFFFF<=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=>?_,EH _⟩;by_cases e2:k=t;. rw[e2];exact UInt32.le_refl _ . have h3:=h2 k א;simp only[UInt32.le_iff_toNat_le] at *;omega lemma Dv {w:ZU->Word} {t:ZU} {h:Word} (hz:w t &&& 0x7FFFFFFF=0) (H:C_hok w t h):C_hok w (t+1) h:=by obtain ⟨h1,h2,hp⟩:=H;refine ⟨h1,λk hk=>?_,hp⟩;by_cases e2:k=t;. rw[e2,hz,Ж];exact Nat.zero_le _ . exact h2 k א lemma Dh {rows inW i t:ZU} (hi:i< rows) (ht:t< inW):i*inW+t< rows*inW:=by have h:(i+1)*inW<=rows*inW:=З _ hi;rw[ф,ц] at h;omega lemma Eq {rows inW i t:ZU} (hi:i< rows) (ht:t<=inW):i*inW+t<=rows*inW:=by have h:(i+1)*inW<=rows*inW:=З _ hi;rw[ф,ц] at h;omega lemma Cg (x:ZV) (i:ZX) (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 ES (a:ZS ZQ) (i:ZX) (h:i.toNat< a.size):a.uget i h=a[i.toNat]!:=by rw[Е a i.toNat h];rfl lemma BY (a:ZV) (i:ZX) (v:ZW):(wrF a i v).size=a.size:=by unfold wrF;split;. obtain ⟨ds⟩:=a;exact Array.size_uset _ . rfl lemma Bd (a:ZV) (i:ZX) (v:ZW) (q:ZU) (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 [BK _ hs] unfold wrF;rw[Р h];obtain ⟨ds⟩:=a;have hi':i.toNat< ds.size:=hi;show (ds.set i.toNat v hi')[q]! = if q=i.toNat then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.set i.toNat v hi').size:=by simpa using hq rw[Е _ q h3,Е ds q hq];simp only[Array.getElem_set];by_cases e:q=i.toNat<;>simp[e,eq_comm] . have h3:¬ q<(ds.set i.toNat v hi').size:=by simpa using hq rw[б _ q h3,б ds q hq,Д א] def C_keep (xw:ZU->Word) (t:ZU):List ZU:= (GY t).filter (λp=>xw p &&& 0x7FFFFFFF != 0) lemma CZ (xw:ZU->Word) (t:ZU):(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& 0x7FFFFFFF != 0) (GY t);simpa [C_keep] using h lemma DK (xw:ZU->Word) (t:ZU) (h:xw t &&& 0x7FFFFFFF=0):C_keep xw (t+1)=C_keep xw t:=by simp[C_keep,List.range_succ,List.filter_append,h] lemma Ds (xw:ZU->Word) (t:ZU) (h:¬ xw t &&& 0x7FFFFFFF=0):C_keep xw (t+1)=C_keep xw t ++ [t]:=by simp[C_keep,List.range_succ,List.filter_append,h] def C_Dec (xw:ZU->Word) (sz t:ZU) (ix:ZS ZQ) (buf:ZV) (c:ZX) (hh:Word):Prop:= c.toNat=(C_keep xw t).length∧ix.size=sz∧buf.size=sz ∧ (∀ k p:ZU,(C_keep xw t)[k]?=some p → (ix[k]!:ZQ).toNat=p∧buf.data[k]! = dv (xw p))∧C_hok xw t hh lemma Fh {X:ZS ZQ} {o n:ZX} (xw:ZU->Word) (hn:n.toNat<2^32) (hxw:∀ t:ZX,t< n->rdW X (o+t)=xw t.toNat) (hg:∀ t,t< n.toNat->good (xw t)=true) (d:ZU): ∀ (t:ZX) (ix:ZS ZQ) (buf:ZV) (c:ZX) (hh:Word),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).hi:=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 &&& 0x7FFFFFFF=0 . rw[Л (by simpa using hz)];apply ih (t+1) _ _ _ _ א;rw[hu,C_Dec,DK xw _ hz];exact ⟨h1,h2,h3,h4,Dv hz h5⟩ . rw[Д (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=CZ xw t.toNat;have hc:c.toNat< t.toNat+1:=א;rw[hu,C_Dec,Ds xw _ hz];have hcn:c.toNat< n.toNat:=א;refine ⟨?_,by rw[B9];exact h2,by rw[BY];exact h3,λk p hk2=>?_,Df (hg _ א) h5⟩;. rw[BO _ _ (by rw[Щ];omega),Щ,h1];simp . rw[Bt _ _ _ _ א א,Bd _ _ _ _ א א];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 C8 (a:ZV) (v:ZW):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma Cv (n:ZX) (d:ZU):∀ (k:ZX) (a:ZV),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),C8];omega lemma BV {N:ZU} {sp:ZU->Word} {p:ZU} {out:ZS Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:ZX} {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):= Dr (E4 hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma FK (xs:ZV) (v:ZW) (q:ZU):(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,Д א,ط] . 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,ض] . have h3:¬ q<(ds.push v).size:=by simp;omega rw[б _ q h3,б ds q hq,Д e] structure A9 where (lo hi:ZQ) def AS (A:ZS ZQ) (n i:ZX) (hn:n.toNat<=A.size) (lo hi:ZQ):A9:= if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& 0x7FFFFFFF let u:=v-1 AS A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else ⟨lo,hi⟩ termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];omega def AE (v:ZW) (n k:ZX) (a:ZV):ZV:= if h:k< n then AE v n (k+1) (a.push v) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega def AD (X:ZS ZQ) (o n t:ZX) (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (a:ZV):ZV:= if h:t< n then AD X o n (t+1) hs ho (a.push (dv (X.uget (o+t) (wi o t n X ho hs (А.mp h))))) else a termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega def AL (s:ConvShape) (X:ZS ZQ) (hs:X.size<2^32) (z:ZW) (R r:ZU) (a:ZV):ZV:= if hr:r< R then let hp:=s.h+2*s.pad let yy:=r%hp let o:=((r/hp*s.h+(yy-s.pad))*s.w).toUSize let pu:=s.pad.toUSize if hg:s.pad<=yy∧yy-s.pad< s.h∧o.toNat+s.w.toUSize.toNat<=X.size then AL s X hs z R (r+1) (AE z pu 0 (AD X o s.w.toUSize 0 hs hg.2.2 (AE z pu 0 a))) else AL s X hs z R (r+1) (AE z (s.w+2*s.pad).toUSize 0 a) else a termination_by R-r def AM (s:ConvShape) (t:ZU):ZU:= (t/(s.kh*s.kw)*(s.h+2*s.pad)+t/s.kw%s.kh)*(s.w+2*s.pad)+t%s.kw structure A1 where (dw relu f4:ZT) (s:ConvShape) (X W b:ZS ZQ) (k C ho wo wp:ZU) (step:ZX) (hi:ZQ) (xd:ZV) (nf:ZW) (tab:ZS ZQ) structure A5 where (g:A1) (wd:ZV) (off:ZS ZQ) (e l1 l4:ZX) (e0 bv ze:ZW) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:g.xd.size<2^32) (h1:∀ b:ZX,b< l1->∀ j (h:j< off.size),b.toNat+off[j].toNat+0< g.xd.size) (h4:∀ b:ZX,b< l4->∀ j (h:j< off.size),b.toNat+off[j].toNat+3< g.xd.size) (hz:ze=0) (l16:ZX) (h16:∀ b:ZX,bif t< g.k then windowAt g.s g.X y x ((if g.dw then o*g.k else 0)+t) else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) @[inline] def AG (c:A5) (o y x:ZU) (b:ZX):ZQ:= if hb:b< c.l1 then let q:=AK c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h1 b hb) c.ze c.ze let r:=GA (q.s+c.bv) (c.g.nf*(q.t+c.bv.abs)) if r == 0xFFFFFFFF then AT c.g o y x else act c.g.relu r else AT c.g o y x @[inline] def AF (c:A5) (o y x:ZU) (b:ZX) (a:ZW):ZQ:= let r:=GA (a+c.bv) (c.g.nf*(c.e0+c.bv.abs)) if r == 0xFFFFFFFF then AG c o y x b else act c.g.relu r def AB (c:A5) (o y x:ZU) (b q:ZX) (rr:ZV) (out:ZS ZQ): ZS ZQ:= if h4:x+4<=c.g.wo∧c.g.f4=true∧b< c.l4 then let r:=AI c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h4 b h4.2.2) c.ze c.ze c.ze c.ze rr let o0:=AF c o y x b (rdF r 0) let o1:=AF c o y (x+1) (b+1) (rdF r 1) let o2:=AF c o y (x+2) (b+2) (rdF r 2) let o3:=AF c o y (x+3) (b+3) (rdF r 3) let out:=wrW out q o0 let out:=wrW out (q+1) o1 let out:=wrW out (q+2) o2 let out:=wrW out (q+3) o3 AB c o y (x+4) (b+4) (q+4) r out else if x< c.g.wo then AB c o y (x+1) (b+c.g.step) (q+1) rr (wrW out q (AG c o y x b)) else out termination_by c.g.wo-x def AQ (c:A5) (o y:ZU) (q:ZX) (out:ZS ZQ):ZS ZQ:= if y< c.g.ho then AQ c o (y+1) (q+c.g.wo.toUSize) (AB c o y 0 (y.toUSize*c.g.step*c.g.wp.toUSize) q (pushZ 4 0 (GK 4)) out) else out termination_by c.g.ho-y structure D8 where (a0 a1 a2 a3 a4 a5 a6 a7:ZW) def AJ (xd wd:ZV) (off:ZS ZQ) (b e i:ZX) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:jmax m v.toNat) 0:=by have key:∀ (l:List ZQ) (i:ZU),i<=l.foldl (λm v=>max m v.toNat) i ∧ ∀ v∈l,v.toNat<=l.foldl (λm v=>max m v.toNat) i:=by intro l;induction l with | nil=>intro i;simp | cons x l ih => intro i;obtain ⟨h1,h2⟩:=ih (max i x.toNat);simp only[GZ,List.mem_cons,forall_eq_or_imp];exact ⟨by omega,by omega,h2⟩ rw[<-Array.foldl_toList];exact (key a.toList 0).2 _ ג lemma Et {n m:ZU} (d:ZU) (off:ZS ZQ) (hs:n<2^32) (hm:∀ j (h:j< off.size),off[j].toNat<=m) (b:ZX) (hb:b<(n-(m+d)).toUSize) (j:ZU) (h:j< off.size):b.toNat+off[j].toNat+d< n:=by have h1:=А.mp hb;rw[Nat.toUSize_eq,uk _ א] at h1;have:=hm j h;omega def AO (g:A1) (hs:g.xd.size<2^32) (o:ZU) (q:ZX) (ix off:ZS ZQ) (buf:ZV) (out:ZS ZQ):ZS ZQ:= if o< g.C then let r:=decR g.W (o*g.k).toUSize g.k.toUSize 0 ix buf 0 0 let t:=AW g o r.ix r.buf r.c 0 off 0 let m:=t.off.foldl (λm v=>max m v.toNat) 0 if h:r.c.toNat<=t.off.size∧r.c.toNat<=r.buf.size then let c:A5:=⟨g,r.buf,t.off,r.c,(g.xd.size-(m+0)).toUSize, (g.xd.size-(m+3)).toUSize,dv g.hi*t.a,dv (rdW g.b o.toUSize),0,h.1,h.2,hs, Et 0 t.off hs (Cq t.off),Et 3 t.off hs (Cq t.off),rfl,(g.xd.size-(m+15)).toUSize,Et 15 t.off hs (Cq t.off)⟩ AO g hs (o+1) (q+(g.ho*g.wo).toUSize) r.ix t.off r.buf (AR c (g.xd.size-(m+7)).toUSize (Et 7 t.off hs (Cq t.off)) o 0 q out) else out else out termination_by g.C-o def A2 (dw:ZT) (s:ConvShape) (relu:ZT) (X W b:ZS ZQ) (k C:ZU): ZS ZQ:= let hp:=s.h+2*s.pad let wp:=s.w+2*s.pad let rx:=AS X X.usize 0 (BR X) 0xFFFFFFFF 0 if hg:X.size<2^32∧0x007FFFFF<=rx.lo∧rx.hi<0x7F800000∧X.size=s.cin*s.h*s.w ∧ W.size=C*k∧b.size=C∧0< s.stride∧s.stride<2^32∧0< k∧k<2^20 ∧ s.kh<=hp∧s.kw<=wp∧hp<2^32∧wp<2^32∧W.size<2^32∧b.size<2^32 ∧ C*(s.ho*s.wo)<2^32∧s.cin*hp*wp<2^32 ∧ scan W W.usize 0 (BR W) 0xFFFFFFFF 0≠0xFFFFFFFF ∧ scan b b.usize 0 (BR b) 0xFFFFFFFF 0≠0xFFFFFFFF then let tight:=(rx.hi >>> (23:ZQ))-((rx.lo+1) >>> (23:ZQ)) > 20 let R:=s.cin*hp let xd:=AL s X hg.1 (dv 0) R 0 (GK (R*wp)) if hs:xd.size<2^32 then let g:A1:=⟨dw,relu,s.stride == 1,s,X,W,b,k,C,s.ho,s.wo,wp, s.stride.toUSize,rx.hi,xd,(k.toUInt64+8).toFloat*GT 0x3CC0000000000000, GJ (n:=k) λt=>(AM s t.val).toUInt32⟩ AO g hs 0 0 (GI k 0) (GI k 0) (pushZ k.toUSize 0 (GK k)) (GI (C*(s.ho*s.wo)) 0) else if dw then depthwiseWith B2 s relu X W b else conv2dWith B2 s relu X W b else if dw then depthwiseWith B2 s relu X W b else conv2dWith B2 s relu X W b def B0 (s:ConvShape) (relu:ZT) (X W b:ZS ZQ):ZS ZQ:= A2 false s relu X W b (s.cin*s.kh*s.kw) s.cout def B1 (s:ConvShape) (relu:ZT) (X W b:ZS ZQ):ZS ZQ:= A2 true s relu X W b (s.kh*s.kw) s.cin def AP (s:ConvShape) (X:ZS Word) (c yy xx:ZU):Word:= if yy< s.pad∨xx< s.pad∨yy-s.pad ≥ s.h∨xx-s.pad ≥ s.w then 0 else X[(c*s.h+(yy-s.pad))*s.w+(xx-s.pad)]! def A3 (s:ConvShape) (X:ZS Word) (q:ZU):ZW:= dv (AP s X (q/(s.w+2*s.pad)/(s.h+2*s.pad)) (q/(s.w+2*s.pad)%(s.h+2*s.pad)) (q%(s.w+2*s.pad))) def A7 (F:ZU->ZW) (m:ZU) (a:ZV):Prop:= a.size=m∧∀ q,q< m->a.data[q]! = F q lemma CW {F:ZU->ZW} {m:ZU} {a:ZV} {v:ZW} (h:A7 F m a) (hv:v=F m):A7 F (m+1) (a.push v):=by refine ⟨by rw[C8,h.1],λq hq=>?_⟩;rw[FK,h.1];by_cases e:q=m;. rw[Л e,e,hv] . rw[Д e];exact h.2 q א lemma C3 {F:ZU->ZW} {v:ZW} {n:ZX} (d:ZU): ∀ (k:ZX) (m:ZU) (a:ZV),k.toNat+d=n.toNat->A7 F m a → (∀ i,m<=i->i< m+d->F i=v)->A7 F (m+d) (AE v n k a):=by induction d with | zero => intro k m a he H _ rw[AE,Х (by rw[А];omega)];exact H | succ d ih => intro k m a he H hv have ht:k< n:=by rw[А];omega rw[AE,Р ht,show m+(d+1)=m+1+d by omega] exact ih (k+1) (m+1) _ (by rw[usz k n ht];omega) (CW H (hv m א א).symm) (λi h1 h2=>hv i א א) lemma Dt {F:ZU->ZW} {X:ZS ZQ} {o n:ZX} (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (d:ZU): ∀ (t:ZX) (m:ZU) (a:ZV),t.toNat+d=n.toNat->A7 F m a → (∀ j,j< d->F (m+j)=dv X[o.toNat+t.toNat+j]!) → A7 F (m+d) (AD X o n t hs ho a):=by induction d with | zero => intro t m a he H _ rw[AD,Х (by rw[А];omega)];exact H | succ d ih => intro t m a he H hv have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat:=א rw[AD,Р ht,show m+(d+1)=m+1+d by omega] have e1:=hv 0 א rw[Я,Я] at e1 refine ih (t+1) (m+1) _ (by rw[usz t n ht];omega) (CW H ?_) (λj hj=>?_) . rw[ES,BO _ _ א,e1] . have:=hv (j+1) א;rw[usz t n ht,show m+1+j=m+(j+1) by omega,show o.toNat+(t.toNat+1)+j=o.toNat+t.toNat+(j+1) by omega];exact this lemma Bs {c:ZU} (a:ZU) {b:ZU} (h:b< c):(a*c+b)/c=a∧(a*c+b)%c=b:=by have h0:0< c:=א;rw[о a c,Nat.mul_add_div h0,Nat.mul_add_mod,с h,Г h];exact ⟨Я _,rfl⟩ lemma EQ (s:ConvShape) (X:ZS Word) (r:ZU) {i:ZU} (hi:i< s.w+2*s.pad): A3 s X (r*(s.w+2*s.pad)+i) =dv (AP s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i):=by unfold A3;rw[(Bs r hi).1,(Bs r hi).2] lemma DS (s:ConvShape) (X:ZS ZQ) (hs:X.size<2^32) (hX:X.size=s.cin*s.h*s.w) (hh:s.h+2*s.pad<2^32) (hw:s.w+2*s.pad<2^32) {R:ZU} (hR:R=s.cin*(s.h+2*s.pad)) (d:ZU): ∀ (r:ZU) (a:ZV),r+d=R → A7 (A3 s X) (r*(s.w+2*s.pad)) a → A7 (A3 s X) (R*(s.w+2*s.pad)) (AL s X hs (dv 0) R r a):=by induction d with | zero => intro r a he H rw[AL,Х א] rw[(א:R=r)];exact H | succ d ih => intro r a he H have hr:r< R:=א have hP:=Н (λe:s.h+2*s.pad=0=>by rw[hR,e] at hr;simp at hr) have hc:r/(s.h+2*s.pad)< s.cin:=х (by rw[о,<-hR];exact hr) have hy:=Ъ r hP have e0:(0:ZX).toNat=0:=rfl have ep:s.pad.toUSize.toNat=s.pad:=BK _ א have ew:s.w.toUSize.toNat=s.w:=BK _ א have eW:(s.w+2*s.pad).toUSize.toNat=s.w+2*s.pad:=BK _ hw have hF:∀ i,i< s.w+2*s.pad->A3 s X (r*(s.w+2*s.pad)+i) =dv (AP s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i):=λi hi=>EQ s X r hi have hz:∀ i,i< s.w+2*s.pad->(r%(s.h+2*s.pad)< s.pad∨i< s.pad ∨ r%(s.h+2*s.pad)-s.pad ≥ s.h∨i-s.pad ≥ s.w) → A3 s X (r*(s.w+2*s.pad)+i)=dv 0:=by intro i hi hc;rw[hF i hi,AP,Л hc] have hO:r%(s.h+2*s.pad)-s.pad< s.h->(r/(s.h+2*s.pad)*s.h + (r%(s.h+2*s.pad)-s.pad))*s.w+s.w<=X.size:=λg1=>by rw[hX];exact Eq (Dh hc g1) (le_refl _) have hstep:(r+1)*(s.w+2*s.pad)=r*(s.w+2*s.pad)+s.pad+s.w+s.pad:=by rw[ф,ц];omega rw[AL,Р hr] dsimp only split . rename_i hg;obtain ⟨g1,g2,g3⟩:=hg;have h1:=hO g2;apply ih (r+1) _ א;rw[hstep];generalize hm:r*(s.w+2*s.pad)=m at H hF hz have P1:=C3 (F:=A3 s X) (v:=dv 0) (n:=s.pad.toUSize) s.pad 0 m a (by rw[e0,ep,К]) H (λi h1 h2=>by obtain ⟨u,rfl⟩:=ь h1 exact hz u א (Or.inr (Or.inl א))) have P2:=Dt (F:=A3 s X) hs g3 s.w 0 _ _ (by rw[e0,ew,К]) P1 (λj hj=>by rw[з m,hF _ א,AP,Д א,e0,Я,BK _ א,Nat.add_sub_cancel_left]) exact C3 (F:=A3 s X) (v:=dv 0) (n:=s.pad.toUSize) s.pad 0 _ _ (by rw[e0,ep,К]) P2 (λi h1 h2=>by obtain ⟨u,rfl⟩:=ь h1;rw[з m,з m] exact hz _ א (Or.inr (Or.inr (Or.inr א)))) . rename_i hg;apply ih (r+1) _ א;have hnr:r%(s.h+2*s.pad)< s.pad∨r%(s.h+2*s.pad)-s.pad ≥ s.h:=by by_contra hn;apply hg;have g1:r%(s.h+2*s.pad)-s.pad< s.h:=א;have h1:=hO g1;refine ⟨by omega,g1,?_⟩;rw[BK _ א,ew];exact h1 rw[ф,ц] exact C3 (F:=A3 s X) (v:=dv 0) (n:=(s.w+2*s.pad).toUSize) _ 0 _ _ (by rw[e0,eW,К]) H (λi h1 h2=>by obtain ⟨u,rfl⟩:=ь h1 exact hz u א א) def AV (g:A1) (o:ZU):ZU:=if g.dw then o*g.k else 0 def AY (g:A1) (o y x t:ZU):Word:=windowAt g.s g.X y x (AV g o+t) def AN (g:A1) (o y x:ZU):Word:= act g.relu (dotOf (g.k+1) (λt=>if t< g.k then AY g o y x t else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) lemma Co (g:A1) (o y x:ZU):AT g o y x=AN g o y x:=by unfold AT AN AY AV;rw[BJ] def A0 (g:A1) (y x:ZU):ZU:=y*g.s.stride*g.wp+x*g.s.stride structure A4 (g:A1):Prop where hk:g.k=if g.dw then g.s.kh*g.s.kw else g.s.cin*g.s.kh*g.s.kw hC:g.C=if g.dw then g.s.cin else g.s.cout ho:g.ho=g.s.ho wo:g.wo=g.s.wo wp:g.wp=g.s.w+2*g.s.pad st:g.step.toNat=g.s.stride nf:g.nf=nfOf g.k f4:g.f4=true->g.s.stride=1 p0:0< g.s.stride pk:0< g.k nk:g.k<2^20 kh:g.s.kh<=g.s.h+2*g.s.pad kw:g.s.kw<=g.s.w+2*g.s.pad xd:A7 (A3 g.s g.X) (g.s.cin*(g.s.h+2*g.s.pad)*(g.s.w+2*g.s.pad)) g.xd xs:g.xd.size<2^32 Hx:C_Hv g.X g.hi HW:∀ q:ZU,good g.W[q]! = true Hb:∀ q:ZU,good g.b[q]! = true sW:g.W.size=g.C*g.k sW2:g.W.size<2^32 sb:g.b.size<2^32 sN:g.C*(g.ho*g.wo)<2^32 wp2:g.wp<2^32 tb:g.tab=GJ (n:=g.k) λt=>(AM g.s t.val).toUInt32 lemma Dy {A:ZS Word} {h:Word}:good (0:Word)=true∧(0:Word) &&& 0x7FFFFFFF<=h:=by refine ⟨rfl,?_⟩;rw[Ж];exact Nat.le_trans (Nat.le_of_eq rfl) (Nat.zero_le _) lemma Ev {A:ZS Word} (H:∀ k:ZU,k< A.size->good A[k]! = true) (q:ZU):good A[q]! = true:=by by_cases h:q< A.size;. exact H q h . rw[б A q h];rfl lemma Cd {s:ConvShape} {X:ZS Word} {hi:Word} (H:C_Hv X hi) (c yy xx:ZU):good (AP s X c yy xx)=true∧AP s X c yy xx &&& 0x7FFFFFFF<=hi:=by unfold AP;split;. exact Dy (A:=X) . rename_i q;generalize (c*s.h+(yy-s.pad))*s.w+(xx-s.pad)=n;by_cases h:n< X.size;. exact ⟨H.2.2.1 n h,H.2.2.2 n h⟩ . rw[б X n h];exact Dy (A:=X) lemma Fl {g:A1} (ok:A4 g) {o t:ZU} (hC:o< g.C) (ht:t< g.k):AV g o+t< g.s.cin*(g.s.kh*g.s.kw):=by have h1:=ok.hk;have h2:=ok.hC;unfold AV;cases hd:g.dw;. simp only[hd,GC,if_false] at h1 h2 ⊢;rw[h1] at ht;rw[К,<-Nat.mul_assoc];exact ht . simp only[hd,if_true] at h1 h2 ⊢;rw[h2] at hC;rw[h1] at ht ⊢;exact Dh hC ht lemma Ew {a b st n:ZU} (h0:0< st) (hk:b<=a) (hy:n<(a-b)/st+1):n*st+b<=a:=by have h1:n<=(a-b)/st:=א;have h2:=З st h1;have h3:=Nat.div_mul_le_self (a-b) st;omega lemma CU {g:A1} (ok:A4 g) {o y x t:ZU} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo) (ht:t< g.k): A0 g y x+AM g.s (AV g o+t)< g.xd.size ∧ g.xd.data[A0 g y x+AM g.s (AV g o+t)]! = dv (AY g o y x t):=by have hb:=Fl ok hC ht have hkk:0< g.s.kh*g.s.kw:=Н (λe=>by rw[e] at hb;simp at hb) have hkh:0< g.s.kh:=Nat.pos_of_mul_pos_right hkk have hkw:0< g.s.kw:=к hkk have hY:=Ew ok.p0 ok.kh (by have:=hy;rw[ok.ho] at this;exact this) have hX:=Ew ok.p0 ok.kw (by have:=hx;rw[ok.wo] at this;exact this) unfold AY generalize AV g o+t=u at hb ⊢ have hc:u/(g.s.kh*g.s.kw)< g.s.cin:=х (by rw[о];exact hb) have hdy:=Ъ (u/g.s.kw) hkh have hdx:=Ъ u hkw have e:A0 g y x+AM g.s u=(u/(g.s.kh*g.s.kw)*(g.s.h+2*g.s.pad) + (y*g.s.stride+u/g.s.kw%g.s.kh))*(g.s.w+2*g.s.pad) + (x*g.s.stride+u%g.s.kw):=by unfold A0 AM;rw[ok.wp];ring have hyy:y*g.s.stride+u/g.s.kw%g.s.kh< g.s.h+2*g.s.pad:=א have hxx:x*g.s.stride+u%g.s.kw< g.s.w+2*g.s.pad:=א rw[e] have hq:=Dh (Dh hc hyy) hxx refine ⟨by rw[ok.xd.1];exact hq,?_⟩ rw[ok.xd.2 _ hq,A3,(Bs _ hxx).1,(Bs _ hxx).2,(Bs _ hyy).1,(Bs _ hyy).2] rfl lemma ER {xd wd:ZV} {off:ZS ZQ} {b e:ZX} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+0< xd.size) (xf wf:ZU->ZW) (L:List ZU):∀ (i:ZX) (a t:ZW),i.toNat+L.length=e.toNat → (∀ j p:ZU,L[j]?=some p->xd.data[b.toNat+(off[i.toNat+j]!:ZQ).toNat]! = xf p ∧ wd.data[i.toNat+j]! = wf p) → AK xd wd off b e i he hw hs hb a t=L.foldl (λ(acc:F1) p =>let q:=xf p*wf p F1.mk (acc.s+q) (acc.t+q.abs)) ⟨a,t⟩:=by induction L with | nil => intro i a t h0 _ rw[AK,Х (by rw[А];simp at h0;omega)] rfl | cons p L ih => intro i a t h0 H simp only[List.length_cons] at h0 have ht:i< e:=by rw[А];omega have htn:i.toNat< e.toNat:=א have hit:i.toNat< off.size:=א obtain ⟨h1,h2⟩:=H 0 p rfl rw[Я] at h1 h2 have e1:off.uget i hit=off[i.toNat]!:=ES _ _ _ have hbd:=hb i.toNat hit have e2:∀ pf,xd.uget (b+(off.uget i hit).toUSize) pf=xf p:=by intro pf have hbd':b.toNat+(off.uget i hit).toNat+0< xd.size:=hbd rw[Cg,BO _ _ (by rw[Т];omega),Т,e1,h1] have e3:wd.uget i א=wf p:=by rw[Cg,h2] rw[AK,Р ht] dsimp only rw[e2,e3,ih (i+1) _ _ (by rw[usz i e ht];omega),GZ] intro j q hq have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this lemma Er (a:ZV) (i:ZX) (hs:a.size<2^32) (hi:i.toNat< a.size):rdF a i=a.data[i.toNat]!:=by have h:i< a.usize:=by rw[А,FloatArray.usize,BK _ hs];exact hi unfold rdF;rw[Р h,Cg] def A8 (R:ZV) (F:F4):Prop:= R.size=4∧rdF R 0=F.a∧rdF R 1=F.b∧rdF R 2=F.c∧rdF R 3=F.d lemma FR (r:ZV) (hr:r.size=4) (a b c d:ZW):A8 (wrF (wrF (wrF (wrF r 0 a) 1 b) 2 c) 3 d) ⟨a,b,c,d⟩:=by have u0:(0:ZX).toNat=0:=rfl;have u1:(1:ZX).toNat=1:=Щ;have u2:(2:ZX).toNat=2:=uk 2 ב;have u3:(3:ZX).toNat=3:=uk 3 ב;have s1:(wrF r 0 a).size=4:=by rw[BY,hr] have s2:(wrF (wrF r 0 a) 1 b).size=4:=by rw[BY,s1] have s3:(wrF (wrF (wrF r 0 a) 1 b) 2 c).size=4:=by rw[BY,s2] have s4:(wrF (wrF (wrF (wrF r 0 a) 1 b) 2 c) 3 d).size=4:=by rw[BY,s3] have g1:=λq=>Bd r 0 a q (by rw[hr];norm_num) (by rw[hr,u0];norm_num);have g2:=λq=>Bd _ 1 b q (by rw[s1];norm_num) (by rw[s1,u1];norm_num);have g3:=λq=>Bd _ 2 c q (by rw[s2];norm_num) (by rw[s2,u2];norm_num);have g4:=λq=>Bd _ 3 d q (by rw[s3];norm_num) (by rw[s3,u3];norm_num);have rd:=λ(i:ZX) (hi:i.toNat<4) =>Er _ i (by rw[s4];norm_num) (by rw[s4];exact hi);refine ⟨s4,?_,?_,?_,?_⟩;. rw[rd 0 (by rw[u0];norm_num),g4,g3,g2,g1];simp[u0,u1,u2,u3] . rw[rd 1 (by rw[u1];norm_num),g4,g3,g2,g1];simp[u0,u1,u2,u3] . rw[rd 2 (by rw[u2];norm_num),g4,g3,g2,g1];simp[u0,u1,u2,u3] . rw[rd 3 (by rw[u3];norm_num),g4,g3,g2,g1];simp[u0,u1,u2,u3] lemma EY {xd wd:ZV} {off:ZS ZQ} {b e:ZX} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+3< xd.size) (r:ZV) (hr:r.size=4) (xf:ZU->ZU->ZW) (wf:ZU->ZW) (L:List ZU):∀ (i:ZX) (a0 a1 a2 a3:ZW),i.toNat+L.length=e.toNat → (∀ j p:ZU,L[j]?=some p->wd.data[i.toNat+j]! = wf p∧∀ d,d<4 → xd.data[b.toNat+(off[i.toNat+j]!:ZQ).toNat+d]! = xf d p) → A8 (AI xd wd off b e i he hw hs hb a0 a1 a2 a3 r) ⟨L.foldl (λacc p=>acc+xf 0 p*wf p) a0,L.foldl (λacc p=>acc+xf 1 p*wf p) a1,L.foldl (λacc p=>acc+xf 2 p*wf p) a2,L.foldl (λacc p=>acc+xf 3 p*wf p) a3⟩:=by induction L with | nil => intro i a0 a1 a2 a3 h0 _ rw[AI,Х (by rw[А];simp at h0;omega)] exact FR r hr a0 a1 a2 a3 | cons p L ih => intro i a0 a1 a2 a3 h0 H simp only[List.length_cons] at h0 have ht:i< e:=by rw[А];omega have htn:i.toNat< e.toNat:=א have hit:i.toNat< off.size:=א obtain ⟨h1,h2⟩:=H 0 p rfl rw[Я] at h1 h2 have e1:off.uget i hit=off[i.toNat]!:=ES _ _ _ have hbd:b.toNat+(off.uget i hit).toNat+3< xd.size:=hb i.toNat hit have ep:(b+(off.uget i hit).toUSize).toNat=b.toNat+(off[i.toNat]!:ZQ).toNat:=by rw[BO _ _ (by rw[Т];omega),Т,e1] rw[e1] at hbd have e2:∀ d,d<4->∀ pf,xd.uget ((b+(off.uget i hit).toUSize)+GD d) pf=xf d p:=by intro d hd pf rw[Cg,BO _ _ (by rw[ep,uk d א];omega),ep,uk d א,(h2 d hd)] have e0:∀ pf,xd.uget (b+(off.uget i hit).toUSize) pf=xf 0 p:=by intro pf rw[Cg,ep];exact h2 0 א have e3:wd.uget i א=wf p:=by rw[Cg,h1] rw[AI,Р ht] dsimp only rw[e0,e2 1 א,e2 2 א,e2 3 א,e3] refine ih (i+1) _ _ _ _ (by rw[usz i e ht];omega) λj q hq=>?_ have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this lemma EZ {xd wd:ZV} {off:ZS ZQ} {b e:ZX} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:jacc+xf 0 p*wf p) a0, L.foldl (λacc p=>acc+xf 1 p*wf p) a1, L.foldl (λacc p=>acc+xf 2 p*wf p) a2, L.foldl (λacc p=>acc+xf 3 p*wf p) a3, L.foldl (λacc p=>acc+xf 4 p*wf p) a4, L.foldl (λacc p=>acc+xf 5 p*wf p) a5, L.foldl (λacc p=>acc+xf 6 p*wf p) a6, L.foldl (λacc p=>acc+xf 7 p*wf p) a7, L.foldl (λacc p=>acc+xf 8 p*wf p) a8, L.foldl (λacc p=>acc+xf 9 p*wf p) a9, L.foldl (λacc p=>acc+xf 10 p*wf p) a10, L.foldl (λacc p=>acc+xf 11 p*wf p) a11, L.foldl (λacc p=>acc+xf 12 p*wf p) a12, L.foldl (λacc p=>acc+xf 13 p*wf p) a13, L.foldl (λacc p=>acc+xf 14 p*wf p) a14, L.foldl (λacc p=>acc+xf 15 p*wf p) a15⟩:=by induction L with | nil => intro i a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 h0 _ rw[AH,Х (by rw[А];simp at h0;omega)] rfl | cons p L ih => intro i a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 a11 a12 a13 a14 a15 h0 H simp only[List.length_cons] at h0 have ht:i?_ have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this lemma Ea {xd wd:ZV} {off:ZS ZQ} {b e:ZX} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:jacc+xf 0 p*wf p) a0, L.foldl (λacc p=>acc+xf 1 p*wf p) a1, L.foldl (λacc p=>acc+xf 2 p*wf p) a2, L.foldl (λacc p=>acc+xf 3 p*wf p) a3, L.foldl (λacc p=>acc+xf 4 p*wf p) a4, L.foldl (λacc p=>acc+xf 5 p*wf p) a5, L.foldl (λacc p=>acc+xf 6 p*wf p) a6, L.foldl (λacc p=>acc+xf 7 p*wf p) a7⟩:=by induction L with | nil => intro i a0 a1 a2 a3 a4 a5 a6 a7 h0 _ rw[AJ,Х (by rw[А];simp at h0;omega)] rfl | cons p L ih => intro i a0 a1 a2 a3 a4 a5 a6 a7 h0 H simp only[List.length_cons] at h0 have ht:i?_ have:=H (j+1) q (by simpa using hq) rw[usz i e ht,show i.toNat+1+j=i.toNat+(j+1) by omega] exact this structure A6 (c:A5) (o:ZU) (L:List ZU):Prop where ok:A4 c.g hC:o< c.g.C len:c.e.toNat=L.length nd:L.Nodup lt:∀ t∈L,t< c.g.k hz:∀ t,t< c.g.k->t∉L->c.g.W[o*c.g.k+t]! &&& 0x7FFFFFFF=0 rd:∀ j p:ZU,L[j]?=some p → (c.off[j]!:ZQ).toNat=AM c.g.s (AV c.g o+p) ∧ c.wd.data[j]! = dv c.g.W[o*c.g.k+p]! e0:c.e0=dv c.g.hi*L.foldl (λacc t=>acc+(dv c.g.W[o*c.g.k+t]!).abs) 0 bv:c.bv=dv c.g.b[o]! lemma Ex {c:A5} {o:ZU} {L:List ZU} (h:A6 c o L) {j p:ZU} (hj:L[j]?=some p):p< c.g.k:=h.lt p (List.mem_of_getElem? hj) lemma Cm {c:A5} {o:ZU} {L:List ZU} (h:A6 c o L) {y x:ZU} {b:ZX} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=A0 c.g y x):AG c o y x b=AN c.g o y x:=by unfold AG;split . rename_i hl have e:=ER c.he c.hw c.hs (c.h1 b hl) (λp=>dv (AY c.g o y x p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 (by rw[show (0:ZX).toNat=0 from rfl,К];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj;rw[show (0:ZX).toNat=0 from rfl,К,h1.1,hb] exact ⟨(CU h.ok h.hC hy hx (Ex h hj)).2,h1.2⟩) dsimp only;rw[c.hz,e,h.bv,h.ok.nf] by_cases hz:GA ((L.foldl (λ(acc:F1) p =>let q:=dv (AY c.g o y x p)*dv c.g.W[o*c.g.k+p]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩).s+dv c.g.b[o]!) (nfOf c.g.k*((L.foldl (λ(acc:F1) p => let q:=dv (AY c.g o y x p)*dv c.g.W[o*c.g.k+p]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩).t+(dv c.g.b[o]!).abs))=0xFFFFFFFF . rw[Л (by simpa using hz)];exact Co _ _ _ _ . rw[Д (by simpa using hz)];unfold AN;congr 1;exact DJ c.g.k h.ok.nk (AY c.g o y x) (λt=>c.g.W[o*c.g.k+t]!) c.g.b[o]! (λt _=>(Cd h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) L h.nd h.lt (λt ht hn=>Or.inr (h.hz t ht hn)) _ _ ⟨rfl,rfl⟩ hz . exact Co _ _ _ _ lemma C5 {c:A5} {o:ZU} {L:List ZU} (h:A6 c o L) {y x:ZU} {b:ZX} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=A0 c.g y x) {a:ZW} (ha:a=L.foldl (λacc t=>acc+dv (AY c.g o y x t)*dv c.g.W[o*c.g.k+t]!) 0):AF c o y x b a=AN c.g o y x:=by unfold AF;dsimp only;rw[h.bv,h.ok.nf,h.e0];by_cases hz:GA (a+dv c.g.b[o]!) (nfOf c.g.k*(dv c.g.hi*L.foldl (λacc t =>acc+(dv c.g.W[o*c.g.k+t]!).abs) 0+(dv c.g.b[o]!).abs))=0xFFFFFFFF;. rw[Л (by simpa using hz)];exact Cm h hy hx hb . rw[Д (by simpa using hz)];unfold AN;congr 1;exact D5 c.g.k h.ok.nk (AY c.g o y x) (λt=>c.g.W[o*c.g.k+t]!) c.g.b[o]! c.g.hi (λt _=>(Cd h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) h.ok.Hx.1 (λt _=>(Cd h.ok.Hx _ _ _).2) h.ok.Hx.2.1 L h.nd h.lt (λt ht hn=>Or.inr (h.hz t ht hn)) _ _ ⟨by rw[ha],rfl⟩ hz def AU (g:A1) (q:ZU):Word:= AN g (q/(g.ho*g.wo)) (q/g.wo%g.ho) (q%g.wo) lemma Eh (g:A1) {o y x:ZU} (hy:y< g.ho) (hx:x< g.wo):AN g o y x=AU g ((o*g.ho+y)*g.wo+x):=by unfold AU;rw[о g.ho g.wo,<-ذ,(Bs _ hx).1,(Bs _ hx).2,(Bs _ hy).1,(Bs _ hy).2] lemma Di {g:A1} (ok:A4 g) {o y x:ZU} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo):A0 g y x<2^32:=by have h:=(CU ok hC hy hx ok.pk).1;have:=ok.xs;omega lemma DU {c:A5} {o:ZU} {L:List ZU} (h:A6 c o L) {y Q:ZU} (hy:y< c.g.ho) (hQ:Q=(o*c.g.ho+y)*c.g.wo) (hN:Q+c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:ZU): ∀ (x:ZU) (b q:ZX) (rr:ZV) (out:ZS Word),x+d=c.g.wo → (x< c.g.wo->b.toNat=A0 c.g y x)->q.toNat=Q+x->rr.size=4 → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (AU c.g) (λp=>p< Q+x) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (AU c.g) (λp=>p< Q+c.g.wo) (AB c o y x b q rr out):=by induction d using Nat.strong_induction_on with | _ d ih => intro x b q rr out he hb hq hrr H have sN:=h.ok.sN have hsp:∀ x',x'< c.g.wo->AN c.g o y x'=AU c.g (Q+x'):=by intro x' hx';rw[hQ];exact Eh c.g hy hx' have l2:(2:ZX).toNat=2:=uk 2 ב have l3:(3:ZX).toNat=3:=uk 3 ב have l4:(4:ZX).toNat=4:=uk 4 ב by_cases h4:x+4<=c.g.wo∧c.g.f4=true∧b< c.l4 . have st1:=h.ok.f4 h4.2.1;have hb0:=hb א;have hB:∀ k,A0 c.g y (x+k)=A0 c.g y x+k:=by intro k;unfold A0;rw[st1];simp only[Nat.mul_one];omega have bB:=λk (hk:x+k< c.g.wo)=>Di h.ok h.hC hy hk;have b0:=bB 3 א;rw[hB] at b0;have a1:(b+1).toNat=A0 c.g y (x+1):=by rw[hB,BO _ _ (by rw[Щ];omega),hb0,Щ] have a2:(b+2).toNat=A0 c.g y (x+2):=by rw[hB,BO _ _ (by rw[l2];omega),hb0,l2] have a3:(b+3).toNat=A0 c.g y (x+3):=by rw[hB,BO _ _ (by rw[l3];omega),hb0,l3] obtain ⟨R0,R1,R2,R3,R4⟩:=EY c.he c.hw c.hs (c.h4 b h4.2.2) rr hrr (λk p=>dv (AY c.g o y (x+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 (by rw[show (0:ZX).toNat=0 from rfl,К];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj;rw[show (0:ZX).toNat=0 from rfl,К,h1.1];refine ⟨h1.2,λk hk=>?_⟩;rw[show b.toNat+AM c.g.s (AV c.g o+p)+k =A0 c.g y (x+k)+AM c.g.s (AV c.g o+p) by rw[hB,hb0];omega] exact (CU h.ok h.hC hy א (Ex h hj)).2) have o0:AF c o y x b (L.foldl (λacc p=>acc+dv (AY c.g o y (x+0) p) * dv c.g.W[o*c.g.k+p]!) 0)=AN c.g o y x:=C5 h hy א hb0 rfl have o1:=C5 h hy (א:x+1< c.g.wo) a1 (a:=(L.foldl (λacc p=>acc+dv (AY c.g o y (x+1) p) * dv c.g.W[o*c.g.k+p]!) 0)) rfl have o2:=C5 h hy (א:x+2< c.g.wo) a2 (a:=(L.foldl (λacc p=>acc+dv (AY c.g o y (x+2) p) * dv c.g.W[o*c.g.k+p]!) 0)) rfl have o3:=C5 h hy (א:x+3< c.g.wo) a3 (a:=(L.foldl (λacc p=>acc+dv (AY c.g o y (x+3) p) * dv c.g.W[o*c.g.k+p]!) 0)) rfl rw[AB,Р h4];dsimp only;rw[c.hz,R1,R2,R3,R4];dsimp only;rw[o0,o1,o2,o3];refine ih (d-4) א (x+4) _ _ _ _ א ?_ ?_ R0 ?_;. intro hx;have hbb:=Di h.ok h.hC hy hx;rw[hB] at hbb ⊢;rw[BO _ _ (by rw[l4];omega),hb0,l4] . rw[BO _ _ (by rw[l4,hq];omega),hq,l4];omega . have q1:(q+1).toNat=Q+x+1:=by rw[BO _ _ (by rw[Щ,hq];omega),hq,Щ] have q2:(q+2).toNat=Q+x+1+1:=by rw[BO _ _ (by rw[l2,hq];omega),hq,l2] have q3:(q+3).toNat=Q+x+1+1+1:=by rw[BO _ _ (by rw[l3,hq];omega),hq,l3] have s1:AN c.g o y (x+1)=AU c.g (Q+x+1):=by rw[з];exact hsp _ א have s2:AN c.g o y (x+2)=AU c.g (Q+x+1+1):=by rw[show Q+x+1+1=Q+(x+2) by omega];exact hsp _ א have s3:AN c.g o y (x+3)=AU c.g (Q+x+1+1+1):=by rw[show Q+x+1+1+1=Q+(x+3) by omega];exact hsp _ א rw[show Q+(x+4)=Q+x+1+1+1+1 by omega];exact BV sN (BV sN (BV sN (BV sN H hq א (hsp _ א)) q1 א s1) q2 א s2) q3 א s3 . by_cases h1:x< c.g.wo . have hb0:=hb h1;rw[AB,Х h4,Л h1];refine ih (d-1) א (x+1) _ _ _ _ א ?_ ?_ hrr ?_ . intro hx;have hbb:=Di h.ok h.hC hy hx;have e:A0 c.g y (x+1)=A0 c.g y x+c.g.s.stride:=by unfold A0;rw[ф,ц];omega rw[e] at hbb ⊢;rw[BO _ _ (by rw[hb0,h.ok.st];exact hbb),hb0,h.ok.st] . rw[BO _ _ (by rw[Щ,hq];omega),hq,Щ];omega . rw[Cm h hy h1 hb0];exact BV sN H hq א (hsp _ h1) . rw[AB,Х h4,Д h1];have e:x=c.g.wo:=א;rw[e] at H;exact H lemma DV {c:A5} {o:ZU} {L:List ZU} (h:A6 c o L) {y Q:ZU} (hy:ypp intro x b q rr out he hb hq hrr H have sN:=h.ok.sN have hsp:∀ x',x'dv (AY c.g o y (x+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (by rw[show (0:ZX).toNat=0 from rfl,К];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj rw[show (0:ZX).toNat=0 from rfl,К,h1.1] refine ⟨h1.2,λk hk=>?_⟩ rw[show b.toNat+AM c.g.s (AV c.g o+p)+k = A0 c.g y (x+k)+AM c.g.s (AV c.g o+p) by rw[hB,hb0];omega] exact (CU h.ok h.hC hy (by omega) (Ex h hj)).2) have eq0:AF c o y x b (L.foldl (λacc p=>acc+dv (AY c.g o y x p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y x:=by apply C5 h hy (by omega) (by simpa using hb0) rfl have l1:(1:ZX).toNat=1:=uk 1 ב have eq1:AF c o y (x+1) (b+1) (L.foldl (λacc p=>acc+dv (AY c.g o y (x+1) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+1):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l1]; have:=Di h.ok h.hC hy (show x+1acc+dv (AY c.g o y (x+2) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+2):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l2]; have:=Di h.ok h.hC hy (show x+2acc+dv (AY c.g o y (x+3) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+3):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l3]; have:=Di h.ok h.hC hy (show x+3acc+dv (AY c.g o y (x+4) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+4):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l4]; have:=Di h.ok h.hC hy (show x+4acc+dv (AY c.g o y (x+5) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+5):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l5]; have:=Di h.ok h.hC hy (show x+5acc+dv (AY c.g o y (x+6) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+6):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l6]; have:=Di h.ok h.hC hy (show x+6acc+dv (AY c.g o y (x+7) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+7):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l7]; have:=Di h.ok h.hC hy (show x+7acc+dv (AY c.g o y (x+8) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+8):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l8]; have:=Di h.ok h.hC hy (show x+8acc+dv (AY c.g o y (x+9) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+9):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l9]; have:=Di h.ok h.hC hy (show x+9acc+dv (AY c.g o y (x+10) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+10):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l10]; have:=Di h.ok h.hC hy (show x+10acc+dv (AY c.g o y (x+11) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+11):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l11]; have:=Di h.ok h.hC hy (show x+11acc+dv (AY c.g o y (x+12) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+12):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l12]; have:=Di h.ok h.hC hy (show x+12acc+dv (AY c.g o y (x+13) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+13):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l13]; have:=Di h.ok h.hC hy (show x+13acc+dv (AY c.g o y (x+14) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+14):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l14]; have:=Di h.ok h.hC hy (show x+14acc+dv (AY c.g o y (x+15) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+15):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l15]; have:=Di h.ok h.hC hy (show x+15ppppppppppppppppdv (AY c.g o y (x+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 0 0 0 0 (by rw[show (0:ZX).toNat=0 from rfl,К];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj rw[show (0:ZX).toNat=0 from rfl,К,h1.1] refine ⟨h1.2,λk hk=>?_⟩ rw[show b.toNat+AM c.g.s (AV c.g o+p)+k = A0 c.g y (x+k)+AM c.g.s (AV c.g o+p) by rw[hB,hb0];omega] exact (CU h.ok h.hC hy (by omega) (Ex h hj)).2) have eq0:AF c o y x b (L.foldl (λacc p=>acc+dv (AY c.g o y x p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y x:=by apply C5 h hy (by omega) (by simpa using hb0) rfl have l1:(1:ZX).toNat=1:=uk 1 ב have eq1:AF c o y (x+1) (b+1) (L.foldl (λacc p=>acc+dv (AY c.g o y (x+1) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+1):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l1]; have:=Di h.ok h.hC hy (show x+1acc+dv (AY c.g o y (x+2) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+2):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l2]; have:=Di h.ok h.hC hy (show x+2acc+dv (AY c.g o y (x+3) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+3):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l3]; have:=Di h.ok h.hC hy (show x+3acc+dv (AY c.g o y (x+4) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+4):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l4]; have:=Di h.ok h.hC hy (show x+4acc+dv (AY c.g o y (x+5) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+5):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l5]; have:=Di h.ok h.hC hy (show x+5acc+dv (AY c.g o y (x+6) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+6):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l6]; have:=Di h.ok h.hC hy (show x+6acc+dv (AY c.g o y (x+7) p)*dv c.g.W[o*c.g.k+p]!) 0) = AN c.g o y (x+7):=by apply C5 h hy (by omega) (by rw[hB,BO _ _ (by rw[l7]; have:=Di h.ok h.hC hy (show x+7ppppppppp<(o*c.g.ho+y)*c.g.wo) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (AU c.g) (λp=>p<(o*c.g.ho+c.g.ho)*c.g.wo) (AQ c o y q out):=by induction d with | zero => intro y q out he _ H rw[AQ,Д א] rw[(א:y=c.g.ho)] at H;exact H | succ d ih => intro y q out he hq H have hy:y< c.g.ho:=א have sN:=h.ok.sN have hwo:0< c.g.wo:=by rw[h.ok.wo];exact ز _ have hA:(o*c.g.ho+(y+1))*c.g.wo<=(o*c.g.ho+c.g.ho)*c.g.wo:=З _ א have hA2:(o*c.g.ho+(y+1))*c.g.wo=(o*c.g.ho+y)*c.g.wo+c.g.wo:=by rw[<-з,ф,ц] have hY:y<=(o*c.g.ho+y)*c.g.wo:=Nat.le_trans א (й _ hwo) have eW:c.g.wo.toUSize.toNat=c.g.wo:=BK _ א have eY:y.toUSize.toNat=y:=BK _ א have bB:=Di h.ok h.hC hy hwo have hwp:0< c.g.wp:=by have:=h.ok.wp have hkw:0< c.g.s.kw:=by have hb:=Fl h.ok h.hC h.ok.pk have hkk:0< c.g.s.kh*c.g.s.kw:=Н (λe=>by rw[e] at hb;simp at hb) exact к hkk have:=h.ok.kw omega have e1:A0 c.g y 0=y*c.g.s.stride*c.g.wp:=by unfold A0;simp rw[e1] at bB have hS:y*c.g.s.stride<=y*c.g.s.stride*c.g.wp:=й _ hwp have eS:(y.toUSize*c.g.step).toNat=y*c.g.s.stride:=by rw[EM _ _ (by rw[eY,h.ok.st];omega),eY,h.ok.st] have eP:c.g.wp.toUSize.toNat=c.g.wp:=BK _ h.ok.wp2 have ebase:(y.toUSize*c.g.step*c.g.wp.toUSize).toNat=A0 c.g y 0:=by rw[EM _ _ (by rw[eS,eP];exact bB),eS,eP,e1] rw[AQ,Л hy] refine ih (y+1) _ _ א ?_ ?_ . rw[BO _ _ (by rw[eW,hq];omega),hq,eW,hA2] . rw[hA2];refine DU h hy rfl א c.g.wo 0 _ _ _ _ (К _) (λ_=>ebase) (by rw[Я];exact hq) ?_ (by rw[Я];exact H);rw[Cv 4 4 0 _ (by rw[show (0:ZX).toNat=0 from rfl,К];exact (uk 4 ב).symm)];rfl lemma DZ {c:A5} {o:ZU} {L:List ZU} (h:A6 c o L) (hN:(o*c.g.ho+c.g.ho)*c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (l8:ZX) (h8:∀ b:ZX,bp<(o*c.g.ho+y)*c.g.wo) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (AU c.g) (λp=>p<(o*c.g.ho+c.g.ho)*c.g.wo) (AR c l8 h8 o y q out):=by induction d with | zero => intro y q out he _ H rw[AR,Д א] rw[(א:y=c.g.ho)] at H;exact H | succ d ih => intro y q out he hq H have hy:y< c.g.ho:=א have sN:=h.ok.sN have hwo:0< c.g.wo:=by rw[h.ok.wo];exact ز _ have hA:(o*c.g.ho+(y+1))*c.g.wo<=(o*c.g.ho+c.g.ho)*c.g.wo:=З _ א have hA2:(o*c.g.ho+(y+1))*c.g.wo=(o*c.g.ho+y)*c.g.wo+c.g.wo:=by rw[<-з,ф,ц] have hY:y<=(o*c.g.ho+y)*c.g.wo:=Nat.le_trans א (й _ hwo) have eW:c.g.wo.toUSize.toNat=c.g.wo:=BK _ א have eY:y.toUSize.toNat=y:=BK _ א have bB:=Di h.ok h.hC hy hwo have hwp:0< c.g.wp:=by have:=h.ok.wp have hkw:0< c.g.s.kw:=by have hb:=Fl h.ok h.hC h.ok.pk have hkk:0< c.g.s.kh*c.g.s.kw:=Н (λe=>by rw[e] at hb;simp at hb) exact к hkk have:=h.ok.kw omega have e1:A0 c.g y 0=y*c.g.s.stride*c.g.wp:=by unfold A0;simp rw[e1] at bB have hS:y*c.g.s.stride<=y*c.g.s.stride*c.g.wp:=й _ hwp have eS:(y.toUSize*c.g.step).toNat=y*c.g.s.stride:=by rw[EM _ _ (by rw[eY,h.ok.st];omega),eY,h.ok.st] have eP:c.g.wp.toUSize.toNat=c.g.wp:=BK _ h.ok.wp2 have ebase:(y.toUSize*c.g.step*c.g.wp.toUSize).toNat=A0 c.g y 0:=by rw[EM _ _ (by rw[eS,eP];exact bB),eS,eP,e1] rw[AR,Л hy] refine ih (y+1) _ _ א ?_ ?_ . rw[BO _ _ (by rw[eW,hq];omega),hq,eW,hA2] . rw[hA2];refine DV h hy rfl א l8 h8 c.g.wo 0 _ _ _ _ (К _) (λ_=>ebase) (by rw[Я];exact hq) ?_ (by rw[Я];exact H);rw[Cv 4 4 0 _ (by rw[show (0:ZX).toNat=0 from rfl,К];exact (uk 4 ב).symm)];rfl lemma D0 {g:A1} {c:A5} {o:ZU} {L:List ZU} (hg:c.g=g) (h:A6 c o L) (hN:(o*g.ho+g.ho)*g.wo<=g.C*(g.ho*g.wo)) {l8:ZX} {h8:∀ b:ZX,bp<(o*g.ho+0)*g.wo) out): C_Inv (g.C*(g.ho*g.wo)) (AU g) (λp=>p<(o*g.ho+g.ho)*g.wo) (AR c l8 h8 o 0 q out):=by subst hg exact DZ h hN l8 h8 c.g.ho 0 q out (К _) hq H lemma F0 (s:ConvShape) (o:ZU) {p:ZU} (hp:p< s.kh*s.kw): AM s (o*(s.kh*s.kw)+p) =o*((s.h+2*s.pad)*(s.w+2*s.pad))+AM s p:=by have hkk:0< s.kh*s.kw:=א have hkh:0< s.kh:=Nat.pos_of_mul_pos_right hkk have hkw:0< s.kw:=к hkk have e1:(o*(s.kh*s.kw)+p)/s.kw=o*s.kh+p/s.kw:=by rw[<-Nat.mul_assoc,Nat.add_comm,Nat.add_mul_div_right _ _ hkw,Nat.add_comm] have e2:(o*(s.kh*s.kw)+p)%s.kw=p%s.kw:=by rw[<-Nat.mul_assoc,Nat.add_comm,Nat.add_mul_mod_self_right] unfold AM rw[(Bs o hp).1,с hp,e1,e2,Nat.add_comm (o*s.kh),Nat.add_mul_mod_self_right] ring lemma FI (a b:ZU):a.toUInt32+b.toUInt32=(a+b).toUInt32:=(UInt32.ofNat_add a b).symm lemma Ek {g:A1} (ok:A4 g) (o:ZU) {p:ZU} (hp:p< g.k) {u:ZQ} (hu:u.toNat=p): (if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0)+rdW g.tab u.toUSize =(AM g.s (AV g o+p)).toUInt32:=by have hz:g.tab.size=g.k:=by rw[ok.tb];simp have e:g.tab[p]! = (AM g.s p).toUInt32:=by rw[ok.tb,Е _ p (by simpa using hp),Array.getElem_ofFn] rw[B7 _ _ (by have:=ok.nk;omega),Т,hu,e] have h1:=ok.hk unfold AV cases hd:g.dw . simp only[GC,if_false,UInt32.zero_add,Nat.zero_add] . simp only[hd,if_true] at h1 ⊢;rw[h1,F0 g.s o (by rw[<-h1];exact hp),ok.wp,FI] def AX (g:A1) (o:ZU) (L:List ZU) (j:ZU):Word:= (AM g.s (AV g o+L[j]?.getD 0)).toUInt32 lemma EO {g:A1} (ok:A4 g) (o:ZU) {ix:ZS ZQ} {buf:ZV} {e:ZX} {L:List ZU} {N:ZU} (wf:ZU->ZW) (hk:∀ p∈L,p< g.k) (hN:N<2^32) (hix:ix.size<2^32) (hbuf:buf.size<2^32) (hL:e.toNat=L.length) (he:e.toNat<=buf.size) (heN:e.toNat<=N) (hr:∀ j p:ZU,L[j]?=some p->(ix[j]!:ZQ).toNat=p∧buf.data[j]! = wf p) (d:ZU): ∀ (i:ZX) (off:ZS ZQ) (a:ZW),i.toNat+d=e.toNat → C_Inv N (AX g o L) (λq=>q< i.toNat) off → C_Inv N (AX g o L) (λq=>q< e.toNat) (AW g o ix buf e i off a).off ∧ (AW g o ix buf e i off a).a =(L.drop i.toNat).foldl (λacc t=>acc+(wf t).abs) a:=by induction d with | zero => intro i off a h0 H rw[AW,Х (by rw[А];omega)] rw[(א:i.toNat=e.toNat)] at H ⊢ exact ⟨H,by rw[List.drop_eq_nil_of_le א];rfl⟩ | succ d ih => intro i off a h0 H have ht:i< e:=by rw[А];omega have hl:i.toNat< L.length:=א have hp:L[i.toNat]?=some L[i.toNat]:=List.getElem?_eq_getElem hl obtain ⟨h1,h2⟩:=hr _ _ hp have hi2:=ih (i+1) (wrW off i (AM g.s (AV g o+L[i.toNat])).toUInt32) (a+(wf L[i.toNat]).abs) (by rw[usz i e ht];omega) (by rw[usz i e ht] exact BV hN H rfl א (by rw[AX,hp];rfl)) rw[AW,Р ht,B7 _ _ hix,Ek ok o (hk _ (List.getElem_mem hl)) h1,Er _ _ hbuf א,h2,List.drop_eq_getElem_cons hl,GZ] rw[usz i e ht] at hi2 exact hi2 lemma DW {g:A1} (ok:A4 g) (hs:g.xd.size<2^32) (d:ZU): ∀ (o:ZU) (q:ZX) (ix off:ZS ZQ) (buf:ZV) (out:ZS Word),o+d=g.C->q.toNat=o*(g.ho*g.wo)->ix.size=g.k->off.size=g.k->buf.size=g.k → C_Inv (g.C*(g.ho*g.wo)) (AU g) (λp=>p< o*(g.ho*g.wo)) out → C_Inv (g.C*(g.ho*g.wo)) (AU g) (λp=>p< g.C*(g.ho*g.wo)) (AO g hs o q ix off buf out):=by induction d with | zero => intro o q ix off buf out he _ _ _ _ H rw[AO,Д א] rw[(א:o=g.C)] at H;exact H | succ d ih => intro o q ix off buf out he hq h1 h2 h3 H have hC:o< g.C:=א have hkn:g.k<2^32:=lt_trans ok.nk ב have sW:=ok.sW have sW2:=ok.sW2 have sN:=ok.sN have hP:o*g.k+g.k<=g.W.size:=by rw[sW];exact Eq hC (le_refl _) have eo:(o*g.k).toUSize.toNat=o*g.k:=BK _ א have ek:g.k.toUSize.toNat=g.k:=BK _ hkn have hO:(o+1)*(g.ho*g.wo)<=g.C*(g.ho*g.wo):=З _ hC have hO2:(o+1)*(g.ho*g.wo)=o*(g.ho*g.wo)+g.ho*g.wo:=by rw[ф,ц] have hpos:0< g.ho*g.wo:=by rw[ok.ho,ok.wo];exact Nat.mul_pos (ز _) (ز _) have eH:(g.ho*g.wo).toUSize.toNat=g.ho*g.wo:=BK _ א have eOo:o.toUSize.toNat=o:=by have:o<=o*(g.ho*g.wo):=й _ hpos exact BK _ א have D:=Fh (X:=g.W) (o:=(o*g.k).toUSize) (n:=g.k.toUSize) (λp=>g.W[o*g.k+p]!) (by rw[ek];exact hkn) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[B7 _ _ sW2,BO _ _ (by rw[eo];omega),eo]) (λt _=>ok.HW _) g.k 0 ix buf 0 0 (by rw[show (0:ZX).toNat=0 from rfl,ek,К]) ⟨by simp[C_keep],by rw[ek];exact h1,by rw[ek];exact h3,λk p hk=>by simp[C_keep] at hk,rfl,λk hk=>absurd hk (Nat.not_lt_zero _),(by decide)⟩ rw[ek] at D obtain ⟨d1,d2,d3,d4,-⟩:=D rw[AO,Л hC] dsimp only have hlen:=CZ (λp=>g.W[o*g.k+p]!) g.k have T:=EO ok o (N:=g.k) (L:=C_keep (λp=>g.W[o*g.k+p]!) g.k) (λp=>dv g.W[o*g.k+p]!) (λt ht=>н.mp (ش.mp ht).1) hkn א א d1 א א d4 (C_keep (λp=>g.W[o*g.k+p]!) g.k).length 0 off 0 (by rw[show (0:ZX).toNat=0 from rfl,К];exact d1.symm) ⟨h2,λq _ hq=>absurd hq (Nat.not_lt_zero _)⟩ obtain ⟨⟨T1,T3⟩,T2⟩:=T rw[Р (by rw[T1,d3,d1];exact ⟨hlen,hlen⟩)] refine ih (o+1) _ _ _ _ _ א ?_ d2 T1 d3 ?_ . rw[BO _ _ (by rw[eH,hq];omega),hq,eH,hO2] . have E:(o+1)*(g.ho*g.wo)=(o*g.ho+g.ho)*g.wo:=by ring rw[E] refine D0 (L:=C_keep (λp=>g.W[o*g.k+p]!) g.k) rfl ⟨ok,hC,d1,(List.nodup_range).filter _,λt ht=>н.mp (ش.mp ht).1,λt ht hn=>?_,λj p hj=>?_,?_,?_⟩ (by rw[<-E];exact hO) (by rw[Я,hq];ring) (by rw[show (o*g.ho+0)*g.wo=o*(g.ho*g.wo) by ring];exact H) . by_contra hc;exact hn (ش.mpr ⟨н.mpr ht,by simpa using hc⟩) . have hjl:j<(C_keep (λp=>g.W[o*g.k+p]!) g.k).length:=(List.getElem?_eq_some_iff.mp hj).1;have hpk:p< g.k:=н.mp (ش.mp (List.mem_of_getElem? hj)).1;have e1:=T3 j א א;rw[AX,hj] at e1;have e2:=(CU ok hC (y:=0) (x:=0) (by rw[ok.ho];exact ز _) (by rw[ok.wo];exact ز _) hpk).1;have e3:AM g.s (AV g o+p)<2^32:=by have:=ok.xs;omega refine ⟨?_,(d4 j p hj).2⟩;rw[e1];simpa using Nat.mod_eq_of_lt e3 . rw[show (0:ZX).toNat=0 from rfl,List.drop_zero] at T2;exact congrArg (λa=>dv g.hi*a) T2 . show dv (rdW g.b o.toUSize)=_;rw[B7 _ _ ok.sb,eOo] lemma Dx (A:ZS ZQ) (n:ZX) (hn:n.toNat<=A.size) (d:ZU): ∀ (i:ZX) (lo hi:ZQ),i.toNat+d=n.toNat->scan A n i hn lo hi = if 0x007FFFFF<=(AS A n i hn lo hi).lo∧(AS A n i hn lo hi).hi<0x7F800000 then (AS A n i hn lo hi).hi else 0xFFFFFFFF:=by induction d with | zero => intro i lo hi he have h:¬ i< n:=by rw[А];omega rw[scan,AS] simp only[dite_eq_right h] | succ d ih => intro i lo hi he have h:i< n:=by rw[А];omega rw[scan,AS] simp only[dite_eq_left h] exact ih (i+1) _ _ (by rw[usz i n h];omega) lemma Ep {g:A1} (ok:A4 g) (hs:g.xd.size<2^32): AO g hs 0 0 (GI g.k 0) (GI g.k 0) (pushZ g.k.toUSize 0 (GK g.k)) (GI (g.C*(g.ho*g.wo)) 0) =if g.dw then depthwiseSpec g.s g.relu g.X g.W g.b else conv2dSpec g.s g.relu g.X g.W g.b:=by have e:=BK g.k (lt_trans ok.nk ב) refine (FY (DW ok hs g.C 0 0 _ _ _ _ (К _) (by rw[Nat.zero_mul];rfl) ג ג ?_ ⟨by simp,λq _ h=>?_⟩) (λq h=>h)).trans ?_ . rw[Cv g.k.toUSize g.k 0 _ (by rw[e,show (0:ZX).toNat=0 from rfl,К])];exact К _ . rw[Nat.zero_mul] at h;exact absurd h (Nat.not_lt_zero _) . obtain ⟨dw,relu,f4,s,X,W,b,k,C,ho,wo,wp,step,hi,xd,nf,tab⟩:=g;have h1:k=if dw then s.kh*s.kw else s.cin*s.kh*s.kw:=ok.hk;have h2:C=if dw then s.cin else s.cout:=ok.hC;have h3:ho=s.ho:=ok.ho;have h4:wo=s.wo:=ok.wo;subst h1 h2 h3 h4;cases dw;. refine congrArg (GJ (n:=s.cout*(s.ho*s.wo))) (funext λq=>?_);simp[AU,AN,AY,AV] . refine congrArg (GJ (n:=s.cin*(s.ho*s.wo))) (funext λq=>?_);simp[AU,AN,AY,AV] lemma EJ (dw:ZT) (s:ConvShape) (relu:ZT) (X W b:ZS ZQ) {k C:ZU} (hk:k=if dw then s.kh*s.kw else s.cin*s.kh*s.kw) (hC:C=if dw then s.cin else s.cout): A2 dw s relu X W b k C =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by have hfb:(if dw then depthwiseWith B2 s relu X W b else conv2dWith B2 s relu X W b) =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by rw[BJ];rfl unfold A2 dsimp only rw[<-hfb] generalize (if dw then depthwiseWith B2 s relu X W b else conv2dWith B2 s relu X W b)=R at hfb ⊢ split . rename_i hg;split . rename_i hs;obtain ⟨g1,g2,g3,g4,g5,g6,g7,g8,g9,g10,g11,g12,g13,g14,g15,g16,g17,g18,g19,g20⟩:=hg;have e:=Dx X X.usize (BR X) (X.usize.toNat) 0 0xFFFFFFFF 0 (by rw[show (0:ZX).toNat=0 from rfl,К]);rw[Л ⟨g2,g3⟩] at e;have Hx:=B8 X g1 (by rw[e];intro h;rw[h] at g3;exact absurd g3 (by decide));rw[e] at Hx;refine (Ep ?_ _).trans hfb.symm exact ⟨hk,hC,rfl,rfl,rfl,BK _ g8,rfl,λh=>beq_iff_eq.mp h,g7,g9,g10,g11,g12, DS s X g1 g4 g13 g14 rfl _ 0 _ (К _) ⟨by rw[Nat.zero_mul];rfl,λq hq=>by rw[Nat.zero_mul] at hq;omega⟩, hs,Hx,Ev (B8 W g15 g19).2.2.1,Ev (B8 b g16 g20).2.2.1, g5,g15,g16,g17,g14,rfl⟩ . rfl . rfl lemma Cb:B0=conv2dSpec:=by funext s relu X W b;exact EJ false s relu X W b rfl rfl lemma DH:B1=depthwiseSpec:=by funext s relu X W b;exact EJ true s relu X W b rfl rfl end @[inline] def sm (w:ZQ):ZT:=good w && (w &&& 0x7FFFFFFF)<=0x70FFFFFF section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat lemma Eo (s:Sign) (m:ZU) (e T:ZY) (K r:ZU) (hT:Format.binary32.targetExponent (totalExponent m e)=T) (hK:T=e+(K+1:ZU)) (he:(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1)).roundedMantissa=r): UnpackedFloat.round Format.binary32 s m e =let p:=shiftToTargetExponent Format.binary32 r T .exact if h:p.1.mantissa=0 then .zero s else .finite s p.1.mantissa p.2 (Н h):=by unfold UnpackedFloat.round decreaseExponent roundWithAccuracy simp only[hT] have h0:(e-T).toNat=0:=א have hs:shiftToTargetExponent Format.binary32 m e .exact =(ExtendedMantissa.ofMantissaAndAccuracy m .exact >>> (K+1),T):=by unfold shiftToTargetExponent shiftToExponent simp only[hT] have h3:(T-e).toNat=K+1:=א rw[h3,hK] simp only[h0,Nat.shiftLeft_zero,Int.natCast_zero,Int.sub_zero,hs,he] def Av (N:ZU):ZU:= N/2^39+if 2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1) then 1 else 0 lemma G1 (N:ZU):(ExtendedMantissa.ofMantissaAndAccuracy N .exact >>> (38+1)).roundedMantissa=Av N:=by rw[Cr];have d:=Nat.div_add_mod N (2^39);have l:N%2^39<2^39:=Ъ _ ב;unfold Av;simp[ExtendedMantissa.roundedMantissa,ExtendedMantissa.accuracy,Accuracy.roundToNearestEven];generalize hb:(N/274877906944%2 == 1)=b;generalize hc:(!decide (N%274877906944=0))=c;cases b<;>cases c<;>simp at hb hc ⊢<;>(try split_ifs)<;>omega lemma FX (neg:ZT) (N:ZU) (e':ZY) (hN1:2^62<=N) (hN2:N<2^63) (h1:-148<=e'+39) (h2:e'+39+150<=253): toWord (GV 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+Av N):=by have hr1:2^23<=Av N:=by unfold Av;have:2^23<=N/2^39:=א split<;>omega have hr2:Av N<=2^24:=by unfold Av;have:N/2^39<2^24:=א split<;>omega have hL:N.log2=62:=Bz N 62 hN1 hN2 have hT:Format.binary32.targetExponent (totalExponent N e')=e'+39:=by simp only[totalExponent,hL,CB];omega rw[Eo _ N e' _ 38 _ hT (by push_cast;ring) (G1 N)] by_cases hc:Av N=2^24 . rw[hc,show e'+39=e'+40-1 by ring,Cs _ (e'+40) א (by norm_num:0<2^23)];simp only[toWord,Model.toNatBits,GV,Model.ofModelBits];congr 1;show (UnpackedFloat.pack Format.binary32 _).toNat=_;rw[Bf _ _ _ _ ב ב א א];have e:(e'+40+150).toNat=(e'+40-1+149).toNat+2:=א;rw[e];cases neg<;>simp[Model.modelSign]<;>omega . have hp:0< Av N:=א;rw[Ck _ _ _ hr1 א א hp];simp only[toWord,Model.toNatBits,GV,Model.ofModelBits];congr 1;show (UnpackedFloat.pack Format.binary32 _).toNat=_;rw[Bf _ _ _ _ hr1 א א א];have e:(e'+39+150).toNat=(e'+39+149).toNat+1:=א;rw[e];cases neg<;>simp[Model.modelSign]<;>omega @[inline] def Aw (neg:ZT) (mag e0 L:ZR):ZQ:= 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 ||| 0x80000000 else w).toUInt32 lemma F7 (neg:ZT) (mag e0:ZR) (h0:mag.toNat≠0) (h1:mag.toNat<2^63) (h2:24< e0.toNat+mag.toNat.log2) (h3:e0.toNat+mag.toNat.log2<277):Aw neg mag e0 mag.log2=toWord (GF Fmt ⟨neg,mag.toNat,(e0.toNat:ZY)-150⟩):=by have l0:2^mag.toNat.log2<=mag.toNat:=Nat.log2_self_le h0;have l1:mag.toNat<2^(mag.toNat.log2+1):=Nat.lt_log2_self;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt h0).2 h1;omega obtain ⟨L,hLd⟩:∃ L,L=mag.toNat.log2:=⟨_,rfl⟩;rw[<-hLd] at l0 l1 hL h2 h3;have eL:mag.log2.toNat=L:=by rw[hLd];rfl obtain ⟨N,hN⟩:∃ N,N=mag.toNat*2^(62-L):=⟨_,rfl⟩;have p:2^L*2^(62-L)=2^62:=by rw[<-ч];congr 1;omega have hN1:2^62<=N:=by rw[hN,<-p];exact З _ l0 have hN2:N<2^63:=by have:mag.toNat*2^(62-L)<2^(L+1)*2^(62-L):=Nat.mul_lt_mul_of_pos_right l1 (by positivity);rw[hN] calc _< _:=this _=2^63:=by rw[<-ч];congr 1;omega rw[BC,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,FX neg N _ hN1 hN2 א א];have eT:((e0.toNat:ZY)-150-((62-L:ZU):ZY)+39+149).toNat=e0.toNat+L-24:=by omega rw[eT];unfold Aw;dsimp only;have e1:(62-mag.log2).toNat=62-L:=by rw[GO _ _ (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[GM] 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[GO _ _ (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[GX,GQ,decide_eq_true_eq,beq_iff_eq,gt_iff_lt,UInt64.lt_iff_toNat_lt,<-UInt64.toNat_inj,a3,a4,a2];rfl rw[hup] have hw:(hi+q+(if decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1))=true then (1:ZR) else 0)).toNat=(e0.toNat+L-24)*2^23+Av N:=by unfold Av;simp only[decide_eq_true_eq];have hq:N/2^39<2^24:=א;have b1:(hi+q).toNat=(e0.toNat+L-24)*2^23+N/2^39:=by rw[Ы,a6,a2];exact Г א by_cases hc:2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1);. rw[if_pos hc,if_pos hc,Ы,b1];show (_+1)%2^64=_;rw[Г א];omega . rw[if_neg hc,if_neg hc,Ы,b1];show (_+0)%2^64=_;rw[Г א];omega generalize (hi+q+(if decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1))=true then (1:ZR) else 0))=w at hw have hw2:w.toNat<2^31:=by have:Av N<=2^24:=by unfold Av;have:N/2^39<2^24:=א;split<;>omega omega apply И.mp;rw[е];cases neg;. simp only[GC,ite_false,UInt64.toNat_toUInt32,Nat.zero_add,hw] . simp only[ite_true,UInt64.toNat_toUInt32,UInt64.toNat_or];have h5:w.toNat ||| (2147483648:ZR).toNat=2^31+w.toNat:=by have:=щ hw2 1;simp only[Nat.mul_one] at this;rw[this,Nat.or_comm];rfl rw[h5];congr 1;omega lemma Fy (w:ZQ) (h:(w &&& 0x7FFFFFFF)=0):rationalWord w=0:=by have h1:=BA w;rw[h] at h1;have h2:=w.toNat_lt have h3:w=0∨w=0x80000000:=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 Ao (w:ZQ):ZR:= if w &&& 0x7FFFFFFF == 0 then 255 else (expOf w).toUInt64 @[inline] def Aq (w:ZQ):ZR:= if w &&& 0x7FFFFFFF == 0 then 0 else (fracOf w+0x800000).toUInt64 @[inline] def Ax (e0:ZR) (w:ZQ):ZR:=Aq w <<< (Ao w-e0) @[inline] def At (e0:ZR) (w:ZQ):ZT:= good w && (w &&& 0x7FFFFFFF == 0 || (e0<=Ao w && Ao w-e0<=37)) @[inline] def Au (e0:ZR) (w:ZQ) (p:ZR):ZR:= if signOf w then p else p+Ax e0 w @[inline] def As (e0:ZR) (w:ZQ) (n:ZR):ZR:= if signOf w then n+Ax e0 w else n lemma Fq (e0:ZR) (w:ZQ) (h:At e0 w=true): (Ax e0 w).toNat<2^61∧rationalWord w = (if signOf w then -((Ax e0 w).toNat:ℚ) else ((Ax e0 w).toNat:ℚ)) * 2^((e0.toNat:ZY)-150):=by unfold At at h simp only[GQ,GX,beq_iff_eq,decide_eq_true_eq] at h obtain ⟨hg,hz⟩:=h by_cases h0:w &&& 0x7FFFFFFF=0 . have hs:Ax e0 w=0:=by unfold Ax Aq;simp[h0] rw[hs,Fy w h0];simp . obtain ⟨h1,h2⟩:=hz.resolve_left h0;have g:=C2 w hg;have a0:(w &&& 0x7FFFFFFF).toNat≠0:=λe=>h0 (И.mp (by simpa using e));rw[BA] at a0;have hx:=Bw w;have hf:(fracOf w).toNat=w.toNat%2^23:=Bx 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:(Ao w).toNat=(expOf w).toNat:=by unfold Ao;simp[h0] have em:(Aq w).toNat=2^23+(fracOf w).toNat:=by unfold Aq;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[GO _ _ h1,ee] at l2;rw[ee] at l1;have l3:(expOf w).toNat-e0.toNat<=37:=l2 have es:(Ax e0 w).toNat=(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat):=by unfold Ax;have sh:(Ao w-e0).toNat%64=(expOf w).toNat-e0.toNat:=by rw[GO _ _ h1,ee];exact Г א rw[UInt64.toNat_shiftLeft,em,sh,т];apply Г;have:2^((expOf w).toNat-e0.toNat)<=2^37:=д ב 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:=д ב 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:ZY)-150) =2^((expOf w).toNat-e0.toNat)*2^((e0.toNat:ZY)-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 Dq (e0:ZR) (w:ZQ) (p n:ZR) (V:ℚ) (B C:ZU) (hC:C=2^61-1) (hB:B+C<2^63) (hw:At e0 w=true) (hp:p.toNat<=B) (hn:n.toNat<=B) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:ZY)-150)): (Au e0 w p).toNat<=B+C∧(As e0 w n).toNat<=B+C ∧ V+rationalWord w=(((Au e0 w p).toNat:ℚ)-(As e0 w n).toNat) * 2^((e0.toNat:ZY)-150):=by obtain ⟨h1,h2⟩:=Fq e0 w hw have h3:(Ax e0 w).toNat<=C:=א unfold Au As rw[h2,hV] cases signOf w . simp only[GC,ite_false];have e:(p+Ax e0 w).toNat=p.toNat+(Ax e0 w).toNat:=by rw[Ы];exact Г א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring . simp only[ite_true];have e:(n+Ax e0 w).toNat=n.toNat+(Ax e0 w).toNat:=by rw[Ы];exact Г א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring @[inline] def Ap (p n e0:ZR) (f:Unit->ZQ):ZQ:= 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 Aw (decide (p< n)) mag e0 L lemma CQ (p n e0:ZR) (f:Unit->ZQ) (hp:p.toNat<2^63) (hn:n.toNat<2^63) (he:e0.toNat<=255) (R:ZQ) (hf:f ()=R) (hR:p≠n->∃ d:FloatLib.Numerics.Dyadic, d.toRat=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:ZY)-150) ∧ R=toWord (GF Fmt d)):Ap p n e0 f=R:=by unfold Ap 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,GO _ _ (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,GO _ _ (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[GX,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:ZR).toNat:=λh=>b1 (UInt64.le_iff_toNat_le.2 h);have b4:¬ (277:ZR).toNat<=(e0+mag.log2).toNat:=λh=>b2 (UInt64.le_iff_toNat_le.2 h);rw[eA] at b3 b4;have t1:(24:ZR).toNat=24:=rfl;have t2:(277:ZR).toNat=277:=rfl;rw[F7 _ _ _ 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 Dz (w:ZQ):(Ao w).toNat<=255:=by unfold Ao;split;. decide . have:=Bw w;simp only[UInt32.toNat_toUInt64];omega @[inline] def Ar (x y:ZR):ZR:=if x< y then x else y lemma Bn (x y:ZR):(Ar x y).toNat<=x.toNat:=by unfold Ar;split;. exact le_rfl . have:=р.not.1 ‹_›;omega lemma DP (e0:ZR) (w0 w1:ZQ) (h0:At e0 w0=true) (h1:At e0 w1=true): (Au e0 w1 (Au e0 w0 0)).toNat<=2*(2^61-1)∧(As e0 w1 (As e0 w0 0)).toNat<=2*(2^61-1) ∧ rationalWord w0+rationalWord w1=(((Au e0 w1 (Au e0 w0 0)).toNat:ℚ) - (As e0 w1 (As e0 w0 0)).toNat)*2^((e0.toNat:ZY)-150):=by obtain ⟨a1,a2,a3⟩:=Dq e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=Dq e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma E6 (e0:ZR) (w0 w1:ZQ) (p n:ZR) (V:ℚ) (hp:p.toNat<=2*(2^61-1)) (hn:n.toNat<=2*(2^61-1)) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:ZY)-150)) (h0:At e0 w0=true) (h1:At e0 w1=true): (Au e0 w1 (Au e0 w0 p)).toNat<2^63∧(As e0 w1 (As e0 w0 n)).toNat<2^63 ∧ V+rationalWord w0+rationalWord w1=(((Au e0 w1 (Au e0 w0 p)).toNat:ℚ) - (As e0 w1 (As e0 w0 n)).toNat)*2^((e0.toNat:ZY)-150):=by obtain ⟨a1,a2,a3⟩:=Dq e0 w0 p n V _ _ rfl ב h0 hp hn hV obtain ⟨b1,b2,b3⟩:=Dq e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 exact ⟨by omega,by omega,b3⟩ lemma C0 (e0:ZR) (w:ZQ) (h:At e0 w=true):expOf w≠255:=by unfold At at h;simp only[GQ] at h;exact BM w h.1 end @[noinline] def addSlow (relu:ZT) (x y:Word):Word:= act relu (toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat @[noinline] def addT (relu:ZT) (x y:Word):Word:= let e0:=Ar (Ao x) (Ao y) if At e0 x && At e0 y then act relu (Ap (Au e0 y (Au e0 x 0)) (As e0 y (As e0 x 0)) e0 λ_=>toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) else addSlow relu x y lemma F9 (relu:ZT) (x y:Word):addT relu x y=addSlow relu x y:=by unfold addT;dsimp only;split . rename_i hc;simp only[GQ] at hc;obtain ⟨hx,hy⟩:=hc;obtain ⟨p1,n1,v⟩:=DP _ x y hx hy;have ex:=C0 _ x hx;have ey:=C0 _ y hy;unfold addSlow;congr 1 refine CQ _ _ _ _ א א ((Bn _ _).trans (Dz 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:=toDyadic_ofUInt32 x ex;have f:=toDyadic_ofUInt32 y ey;simp only[Model.Spec.add,ofW,e,f] . rfl end @[inline] def addOne (relu:ZT) (nf zr u:ZW) (x y:Word):Word:= if sm x && sm y then let q:=dv x*u let c:=dv y let r:=GA ((zr+q)+c) (nf*((zr+q.abs)+c.abs)) if r == 0xFFFFFFFF then addT relu x y else act relu r else addSlow relu x y section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma El (s e:ZW) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:GA s e≠0xFFFFFFFF): ∃ (S:ZT) (ex fr:ZU) (W:ℝ),897<=ex∧ex<1151∧fr<2^23 ∧ GA 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:ZY)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:ZY)-1076)< W) ∧ (2^53-2^28)*2^((ex:ZY)-1076)< W:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=BZ s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=BP _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=BP _ 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[GC,ite_false,ite_true,neg_neg] at hhi hlo ⊢ . exact lt_of_lt_of_le hhi hia . linarith . exact λh=>(lob1 h).trans hlo . exact λh=>by linarith [loa1 h] . exact lob.trans hlo . linarith lemma Fz (ex:ZU) (h1:897<=ex) (h2:ex<1151):((ex:ZY)-1046+150).toNat=ex-896:=by omega lemma Fk (S:ZT) (ex fr m:ZU) (q:ZY) (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:ZY)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:ZY)-1076)<(m:ℝ)*2^q) (hW3:(2^53-2^28)*2^((ex:ZY)-1076)<(m:ℝ)*2^q): toWord (GF Fmt ⟨S,m,q⟩) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hE:(2:ℝ)^((ex:ZY)-1046)=2^30*2^((ex:ZY)-1076):=by rw[<-Ц,<-М С] congr 1;push_cast;ring have hword:=EF S m q (2^23+fr) ((ex:ZY)-1046) hm א א א א (by rw[hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hE];push_cast;linarith) (by intro _;rw[hE];linarith) have e2:(2^23+fr)%2^23=fr:=א rw[hword,Fz ex h1 h2,e2] lemma ED (a b:ℝ) (ha:0<=a) (h:a< b):∃ (m:ZU) (q:ZY),m≠0∧a<(m:ℝ)*2^q∧(m:ℝ)*2^q<=b:=by obtain ⟨k,hk⟩:=exists_pow_lt_of_lt_one (ד:0< b-a) (show (1/2:ℝ)<1 by norm_num);have h2:(0:ℝ)<2^k:=by positivity have hk':1/(2:ℝ)^k< b-a:=by simpa [one_div,inv_pow] using hk have hb:0<=b*2^k:=by have:0<=b:=ד;positivity refine ⟨⌊b*2^k⌋₊,-(k:ZY),?_,?_,?_⟩;. intro h0;have h1:=Nat.lt_floor_add_one (b*2^k);rw[h0] at h1;simp only[Nat.cast_zero,zero_add] at h1;have:b*2^k*(1/2^k)<1*(1/2^k):=Ш h1 (by positivity);rw[mul_assoc,mul_one_div_cancel h2.ne',mul_one,one_mul] at this;linarith . have h1:=Nat.lt_floor_add_one (b*2^k);rw[zpow_neg,Ц,<-div_eq_mul_inv,lt_div_iff₀ h2];have:a*2^k<(b-1/2^k)*2^k:=Ш ד h2;rw[sub_mul,one_div,inv_mul_cancel₀ h2.ne'] at this;linarith . have h1:=Nat.floor_le hb;rw[zpow_neg,Ц,<-div_eq_mul_inv,div_le_iff₀ h2];exact h1 lemma Fg (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,GW,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z lemma G3 (w:ZQ) (h0:expOf w≠0) (h1:expOf w≠255):wv w≠0:=by rw[BF w h1];have hm:mantOf w≠0:=by have:=fracOf_toNat_lt w;unfold mantOf;simp[h0] have hs:sv w≠0:=by unfold sv;split<;>simp[hm] have hr:rationalWord w≠0:=by simp only[rationalWord,decodeD_toRat];exact mul_ne_zero (by exact_mod_cast hs) (zpow_ne_zero _ С) exact_mod_cast hr lemma F8 (s e:ZW) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:GA s e≠0xFFFFFFFF) (M:Model Fmt) (hf:GE M=true) (hM:GG M=Model.roundAt Fmt V):toWord M=GA s e:=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=El s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:ZY)-1076):=ю ב _;have hA2:(0:ℝ)<=(2^53-2^28)*2^((ex:ZY)-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:ZY)-1076)<=Lo) ∧ (2^53-2^28)*2^((ex:ZY)-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'⟩:=ED Lo W hLo0 hLoW;set B:ℝ:=(2^53+fr*2^30+2^29)*2^((ex:ZY)-1076) with hB;obtain ⟨m2,q2,hm2,hd2,hd2'⟩:=ED W ((W+B)/2) hW0.le ד;have w1:=Fk S ex fr m1 q1 h1 h2 h3 hm1 ד (λh=>в (hLo1 h) hd1) (в hLo2 hd1) have w2:=Fk 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[Bw,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:GF Fmt ⟨S,m1,q1⟩=ofW z:=by rw[<-w1,Fg] have r2:GF Fmt ⟨S,m2,q2⟩=ofW z:=by rw[<-w2,Fg] have fz:GE (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:GG (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:GG M≠0:=by rw[hM,<-hv];exact G3 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 G2:(2:ℝ)^(127:ℤ)<=GG (Model.posMaxFinite Fmt):=by rw[Model.toReal_posMaxFinite];have h1:Model.pow2 Fmt.fracWidth+Fmt.maxFiniteFracField=2^24-1:=by decide +kernel have h2:Fmt.maxNormalExponent-Int.ofNat Fmt.fracWidth=104:=by decide +kernel rw[h1,h2];have h3:Model.bpow 104=(2:ℝ)^(104:ℤ):=by simp only[Model.bpow,Be] rw[h3,show (127:ℤ)=23+104 from rfl,М ב];apply Б _ (by positivity);norm_num lemma Fm (w:ZQ) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by unfold sm at h;simp only[GQ,decide_eq_true_eq] at h;obtain ⟨hg,hl⟩:=h;refine ⟨hg,?_⟩;have h1:w.toNat%2^31<=0x70FFFFFF:=by have:=Ж.mp hl;rwa [BA] at this have h2:=C2 w hg;rw[Bp w hg];refine (CV _ _ h1 א).trans ?_;have:A_mag 0x70FFFFFF=(16777215:ℝ)*2^(75:ℤ):=by unfold A_mag;norm_num rw[this];norm_num lemma CM (w:ZQ) (h:good w=true):GE (ofW w)=true:=by have he:=BM w h;simp[ofW,isFinite_ofUInt32,he] lemma FO:wv oneW=1:=by rw[BF oneW (by decide),Dm];simp lemma Cp (relu:ZT) (x y:Word):addOne relu (nfOf 1) 0 (dv oneW) x y=act relu (toWord (Model.add (ofW x) (ofW y))):=by unfold addOne;split . rename_i hc;simp only[GQ] at hc;obtain ⟨gx,lx⟩:=Fm x hc.1;obtain ⟨gy,ly⟩:=Fm y hc.2;dsimp only;split;. exact F9 relu x y . rename_i hr;have hn:GA ((0+dv x*dv oneW)+dv y) (nfOf 1*((0+(dv x*dv oneW).abs)+(dv y).abs))≠0xFFFFFFFF:=λh=>hr (by simp[h]);generalize hs:(0+dv x*dv oneW)+dv y=S at hn;generalize he:nfOf 1*((0+(dv x*dv oneW).abs)+(dv y).abs)=E at hn;obtain ⟨-,-,hbig⟩:=C1 S E hn;obtain ⟨hlo,hhi⟩:=C6 1 ב (λ_=>x) (λ_=>oneW) y (λ_ _=>gx) (λ_ _=>by decide) gy [0] ג ג (λt ht h=>absurd (by simp;omega) h) S E (by simp only[GZ,List.foldl_nil];exact ⟨hs.symm,he.symm⟩) hbig;have hv:B4 1 (λ_=>x) (λ_=>oneW) y=GG (ofW x)+GG (ofW y):=by simp only[B4,Finset.sum_range_one,FO,mul_one];rfl rw[hv] at hlo hhi;have hb:|GG (ofW x)| + |GG (ofW y)| ≤ GG (Model.posMaxFinite Fmt):=by have e:(2:ℝ)^99+2^99<=2^(127:ℤ):=ב;exact (add_le_add lx ly).trans (e.trans G2) have hf:=Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ rfl (CM x gx) (CM y gy) hb;have hM:=Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ rfl (CM x gx) (CM y gy) hb;have:=F8 S E _ hlo hhi hn _ hf hM;rw[this] . rfl lemma DT {L:ZX->ZS ZQ->ZS ZQ} (f:ZX->ZQ->ZQ) {e:ZX} (hL:∀ i x,L i x=if i< e then L (i+1) (wrW x i (f i (rdW x i))) else x) (d:ZU): ∀ (i:ZX) (x:ZS ZQ),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 (GD 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=>(Д א).symm⟩ | succ d ih => intro i x h1 h2 h3 have hi:i< e:=by rw[А];omega have hs:=B9 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,Bt _ _ _ _ h3 א,usz i e hi,B7 _ _ h3] by_cases h6:q=i.toNat . subst h6;rw[if_neg א,if_pos rfl,if_pos ⟨Nat.le_refl _,by omega⟩,USize.ofNat_toNat] . rw[if_neg h6];by_cases h7:i.toNat+1<=q∧q< e.toNat;. rw[if_pos h7,if_pos ⟨by omega,h7.2⟩] . rw[if_neg h7,if_neg א] def addGo (relu:ZT) (nf zr u:ZW) (y:ZS Word) (e i:ZX) (x:ZS Word): ZS Word:= if h:i< e then addGo relu nf zr u y e (i+1) (wrW x i (addOne relu nf zr u (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 Ay (relu:ZT) (a b:ZS Word):ZS Word:= if a.size=b.size∧a.size<2^32 then addGo relu (nfOf 1) 0 (dv oneW) b a.usize 0 a else let nf:=nfOf 1 GJ (n:=a.size) λi=>addOne relu nf 0 (dv oneW) a[i] b[i.val]! lemma EP:Ay=addReluSpec:=by funext relu a b;unfold Ay;split . rename_i h;obtain ⟨h1,h2⟩:=h;have hu:a.usize.toNat=a.size:=BK _ h2;have hg:=DT (L:=addGo relu (nfOf 1) 0 (dv oneW) b a.usize) (λi w=>addOne relu (nfOf 1) 0 (dv oneW) w (rdW b i)) (λ_ _=>by rw[addGo];rfl) a.size 0 a (by rw[hu,show (0:ZX).toNat=0 from rfl,К]) א h2;apply ъ (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⟩,Cp,Е a i hi,B7 _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,Cp] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary @[noinline] def Ah (relu:ZT) (w sw tw:ZQ):ZQ:= act relu (toWord (Model.fma (ofW w) (ofW sw) (ofW tw))) @[inline] def AZ (relu:ZT) (r:ZQ):ZQ:= if relu && signOf r then 0 else r @[inline] def Af (relu g:ZT) (sw tw:ZQ) (y z nf zr:ZW) (w:ZQ):ZQ:= if g && good w then let q:=dv w*y let r:=GA ((zr+q)+z) (nf*((zr+q.abs)+z.abs)) if r == 0xFFFFFFFF then Ah relu w sw tw else AZ relu r else Ah relu w sw tw @[specialize] def Aj (f:ZQ->ZQ) (e i:ZX) (x:ZS ZQ):ZS ZQ:= if h:i< e then Aj 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 Aa (relu:ZT) (c hw:ZU) (s t:ZS ZQ) (nf zr:ZW) (ch:ZU) (x:ZS ZQ):ZS ZQ:= if ch< c then let sw:=s[ch]! let tw:=t[ch]! let y:=dv sw let z:=dv tw Aa relu c hw s t nf zr (ch+1) (Aj (Af relu (good sw && good tw) sw tw y z nf zr) ((ch+1)*hw).toUSize (ch*hw).toUSize x) else x termination_by c-ch def G8 (relu:ZT) (c hw:ZU) (x s t:ZS ZQ):ZS ZQ:= if x.size=c*hw∧x.size<2^32 then Aa relu c hw s t (nfOf 1) 0 0 x else scaleShiftSpec relu c hw x s t @[noinline] def Ad (X:ZS ZQ) (b:ZX) (k w:ZU):ZQ:= B2 (k*k) (λt=>X[b.toNat+(t/k*w+t%k)]!) (λ_=>oneW) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat def Ae (X:ZS ZQ) (b:ZX) (k w:ZU):ZQ:= 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:=Ar (Ar (Ao a0) (Ao a1)) (Ar (Ao a2) (Ao a3)) if At e0 a0 && At e0 a1 && At e0 a2 && At e0 a3 then Ap (Au e0 a3 (Au e0 a2 (Au e0 a1 (Au e0 a0 0)))) (As e0 a3 (As e0 a2 (As e0 a1 (As e0 a0 0)))) e0 λ_=>Ad X b k w else Ad X b k w else Ad X b k w lemma E3 (X:ZS ZQ) (b:ZX) (k w:ZU):Ae X b k w=Ad X b k w:=by unfold Ae;split . rename_i hk;subst hk;dsimp only;split . rename_i hc;simp only[GQ] at hc;obtain ⟨⟨⟨h0,h1⟩,h2⟩,h3⟩:=hc;generalize hi:b.toNat=i at *;obtain ⟨p1,n1,v⟩:=DP _ _ _ h0 h1;obtain ⟨p2,n2,v2⟩:=E6 _ _ _ _ _ _ p1 n1 v h2 h3;refine CQ _ _ _ _ p2 n2 ((Bn _ _).trans ((Bn _ _).trans (Dz _))) _ rfl λhne=>?_;have q0:b.toNat+(0/2*w+0%2)=i:=by simp[hi] have q1:b.toNat+(1/2*w+1%2)=i+1:=by simp[hi] have q2:b.toNat+(2/2*w+2%2)=i+w:=by simp[hi] have q3:b.toNat+(3/2*w+3%2)=i+(w+1):=by simp[hi] have hsum:(∑ j∈GR (2*2),rationalWord X[b.toNat+(j/2*w+j%2)]! * rationalWord oneW)=rationalWord X[i]! + rationalWord X[i+1]! + rationalWord X[i+w]! + rationalWord X[i+(w+1)]!:=by rw[show 2*2=4 from rfl] simp only[Finset.sum_range_succ,Finset.sum_range_zero,Dm,mul_one,zero_add,q0,q1,q2,q3] have o:expOf oneW≠255:=by decide have hf:∀ j,j<2*2->expOf X[b.toNat+(j/2*w+j%2)]!≠255∧expOf oneW≠255:=by intro j hj;rcases (by omega:j=0∨j=1∨j=2∨j=3) with rfl | rfl | rfl | rfl;. rw[q0];exact ⟨C0 _ _ h0,o⟩ . rw[q1];exact ⟨C0 _ _ h1,o⟩ . rw[q2];exact ⟨C0 _ _ h2,o⟩ . rw[q3];exact ⟨C0 _ _ h3,o⟩ obtain ⟨d,h4,h5⟩:=CO (2*2) (λj=>X[b.toNat+(j/2*w+j%2)]!) (λ_=>oneW) hf (by rw[hsum,v2] exact mul_ne_zero (sub_ne_zero.2 λe=>hne (UInt64.toNat_inj.mp (Nat.cast_injective e))) (zpow_ne_zero _ С)) refine ⟨d,?_,?_⟩;. rw[h4,hsum];exact v2 . unfold Ad;rw[BJ];exact h5 . rfl . rfl end @[noinline] def Ac (n:ZU) (s:ZQ):ZQ:= toWord (Model.div (ofW s) (ofW (natW n))) @[inline] def Al (n:ZU) (j s:ZQ):ZQ:= if j< expOf s∧expOf s≠255 then s-(j <<< (23:ZQ)) else Ac n s @[inline] def Am (X:ZS ZQ) (b:ZX) (k w n:ZU) (j z:ZQ) (nf zb s a:ZW) (o:ZQ):ZQ:= if o=0 then z else let r:=GA (s+zb) (nf*(a+zb.abs)) Al n j (if r == 0xFFFFFFFF then Ae X b k w else r) def An (X off:ZS ZQ) (k w n:ZU) (j z:ZQ) (u nf zb:ZW) (b m t:ZX) (s a:ZW) (o:ZQ):ZQ:= if h:t< m then let v:=rdW X (b+(rdW off t).toUSize) let d:=dv v*u An X off k w n j z u nf zb b m (t+1) (s+d) (a+d.abs) (o ||| v) else Am 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:ZX->ZQ) (sx:ZX) (W x:ZU) (b q:ZX) (out:ZS ZQ):ZS ZQ:= 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:ZX->ZQ) (sy sx:ZX) (W H y:ZU) (b q:ZX) (out:ZS ZQ):ZS ZQ:= 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:ZX->ZQ) (sc sy sx:ZX) (W H c ch:ZU) (b q:ZX) (out:ZS ZQ):ZS ZQ:= 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 Ak (k w:ZU):ZS ZQ:= GJ (n:=k*k) λt=>(t.val/k*w+t.val%k).toUInt32 @[noinline] def Ai (c h w k n:ZU) (X off:ZS ZQ) (j z:ZQ) (u nf zb zr:ZW): ZS ZQ:= G_c (λb=>An 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 (GI (c*(h/k*(w/k))) 0) def Az (c h w k:ZU) (X:ZS ZQ):ZS ZQ:= if c*h*w<=X.size∧X.size<2^32∧k*k<2^20 ∧ scan X X.usize 0 (BR X) 0xFFFFFFFF 0≠0xFFFFFFFF then let n:=k*k let off:=Ak k w let j:ZQ:=if n=4 then 2 else if n=64 then 6 else 255 let z:=Ac n (B2 n (λ_=>0) (λ_=>oneW)) Ai c h w k n X off j z (dv oneW) (nfOf n) (dv 0) 0 else avgpoolSpec c h w k X lemma Fe (s e:ZW) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:GA s e≠0xFFFFFFFF):V≠0:=by obtain ⟨S,ex,fr,h1,h2,h3,-,ha,hb⟩:=BZ s e h;obtain ⟨-,va,hva,-,-,loa⟩:=BP _ S ex fr h1 h2 h3 ha;obtain ⟨-,vb,hvb,-,-,lob⟩:=BP _ S ex fr h1 h2 h3 hb;have h0:(0:ℝ)<=(2^53-2^28)*2^((ex:ZY)-1076):=Ь ב (zpow_nonneg ב _);rintro rfl;rw[hva] at hhi;rw[hvb] at hlo;cases S;. simp only[GC,ite_false] at hlo;linarith . simp only[ite_true] at hhi;linarith lemma E9 (n:ZU) (hn:n<2^20) (xw ww:ZU->ZQ) (bw:ZQ) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:ZW) (d:FloatLib.Numerics.Dyadic) (hd:(d.toRat:ℝ)=B4 n xw ww bw) (hlo:R (s-e)< B4 n xw ww bw) (hhi:B4 n xw ww bw< R (s+e)) (h:GA s e≠0xFFFFFFFF):GA s e=toWord (GF Fmt d):=by have hV:=Fe s e _ hlo hhi h;have hsum:=Eu n xw ww bw hx hw hb have e0:∀ c:FloatLib.Numerics.Dyadic,(c.toRat:ℝ)=B4 n xw ww bw → c.significand≠0:=λc hc h0=>hV (by rw[<-hc,(а c).mpr h0,ع]) obtain ⟨d',hd',hw'⟩:=CO (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 ⟨BM _ (hx j hj),BM _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,BM _ hb⟩) (λh0=>hV (by rw[<-hsum,h0,ع])) rw[Ci n hn xw ww bw hx hw hb s e hlo hhi h,hw',roundDyadic_congr Fmt (by decide) d' d (e0 d' (by rw[hd',hsum])) (e0 d hd) (Rat.cast_injective (α:=ℝ) (by rw[hd',hsum,hd]))] def Ab (x y z:ZQ):FloatLib.Numerics.Dyadic:= Model.addDyadic ⟨Bool.xor (decodeD x).negative (decodeD y).negative, (decodeD x).significand*(decodeD y).significand, (decodeD x).exponent+(decodeD y).exponent⟩ (decodeD z) lemma E8 (x y z:ZQ) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255):Model.fma (ofW x) (ofW y) (ofW z)=GF Fmt (Ab x y z):=by rw[Model.Proof.fma_eq_spec];have e:=toDyadic_ofUInt32 x hx;have f:=toDyadic_ofUInt32 y hy;have g:=toDyadic_ofUInt32 z hz have o:Model.chooseNaN3 (GW x:Model Fmt) (GW y) (GW z) =none:=(Model.chooseNaN3_eq_none_iff _ _ _).2 ⟨GN e,GN f,GN g⟩ simp only[Model.Spec.fma,ofW,o,Model.isInf_eq_false_of_toDyadic?_some e,Model.isInf_eq_false_of_toDyadic?_some f,Model.isInf_eq_false_of_toDyadic?_some g,Bool.or_false,GC,ite_false,e,f,g];rfl lemma FF (x y z:ZQ) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255):((Ab x y z).toRat:ℝ)=wv x*wv y+wv z:=by have j:=FloatLib.Numerics.Dyadic.mul_toRat (decodeD x) (decodeD y);simp only[FloatLib.Numerics.Dyadic.mul,FloatLib.Numerics.Dyadic.mulFields] at j;rw[Ab,Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,j,BF x hx,BF y hy,BF z hz];simp only[rationalWord,Rat.cast_add,Rat.cast_mul] lemma F3 (relu:ZT) (r:ZQ) (hf:expOf r≠255):AZ relu r=act relu r:=by cases relu;. rfl . show (if signOf r then 0 else r) =if signOf r && !(expOf r=255 && fracOf r != 0) then 0 else r;simp only[hf,decide_false,Bool.false_and,Bool.not_false,Bool.and_true] lemma ET (relu:ZT) (s e:ZW) (h:GA s e≠0xFFFFFFFF):AZ relu (GA s e)=act relu (GA s e):=by apply F3;obtain ⟨S,ex,fr,h1,h2,h3,hz,-,-⟩:=BZ s e h;intro h0;have h4:(expOf (GA s e)).toNat=255:=by rw[h0];rfl rw[Bw,hz,е] at h4;split at h4<;>omega lemma FD (relu g:ZT) (sw tw w:ZQ) (hg:g=true->good sw=true∧good tw=true):Af relu g sw tw (dv sw) (dv tw) (nfOf 1) 0 w=Ah relu w sw tw:=by unfold Af;split . rename_i hc;obtain ⟨h1,h2⟩:=у.mp hc;obtain ⟨h3,h4⟩:=hg h1;dsimp only;split;. rfl . rename_i hr;have hr':=λh=>hr (beq_iff_eq.mpr h);obtain ⟨-,-,hbig⟩:=C1 _ _ hr';obtain ⟨lo,hi⟩:=C6 1 ב (λ_=>w) (λ_=>sw) tw (λ_ _=>h2) (λ_ _=>h3) h4 [0] ג ג (λt h5 h6=>absurd (by simp;omega) h6) (0+dv w*dv sw+dv tw) (nfOf 1*(0+(dv w*dv sw).abs+(dv tw).abs)) ⟨rfl,rfl⟩ hbig;have e1:B4 1 (λ_=>w) (λ_=>sw) tw=wv w*wv sw+wv tw:=by simp[B4] have hf:=BM _ h2;have hg1:=BM _ h3;have hg2:=BM _ h4;rw[ET _ _ _ hr',E9 1 ב (λ_=>w) (λ_=>sw) tw (λ_ _=>h2) (λ_ _=>h3) h4 _ _ (Ab w sw tw) (by rw[e1,FF _ _ _ hf hg1 hg2]) lo hi hr',Ah,E8 _ _ _ hf hg1 hg2] . rfl lemma Do (relu:ZT) (c hw:ZU) (s t:ZS ZQ) (d:ZU):∀ (ch:ZU) (x:ZS ZQ),ch+d=c->x.size=c*hw->x.size<2^32 → (Aa relu c hw s t (nfOf 1) 0 ch x).size=x.size∧∀ q,q< c*hw → (Aa relu c hw s t (nfOf 1) 0 ch x)[q]! =if ch*hw<=q then Ah relu x[q]! s[q/hw]! t[q/hw]! else x[q]!:=by induction d with | zero => intro ch x h1 _ _ rw[Aa,if_neg א] refine ⟨rfl,λq hq=>?_⟩ rw[if_neg (by rw[(א:ch=c)];omega)] | succ d ih => intro ch x h1 h2 h3 rw[Aa,if_pos א] dsimp only have h4:(ch+1)*hw<=c*hw:=З _ א have h5:(ch+1)*hw=ch*hw+hw:=ر _ _ have e1:((ch+1)*hw).toUSize.toNat=(ch+1)*hw:=BK _ א have e0:(ch*hw).toUSize.toNat=ch*hw:=BK _ א obtain ⟨h6,h7⟩:=DT (λ_=>Af relu (good s[ch]! && good t[ch]!) s[ch]! t[ch]! (dv s[ch]!) (dv t[ch]!) (nfOf 1) 0) (L:=Aj _ ((ch+1)*hw).toUSize) (λ_ _=>by rw[Aj];rfl) hw (ch*hw).toUSize x א א h3 obtain ⟨h8,h9⟩:=ih (ch+1) _ א (h6.trans h2) א refine ⟨h8.trans h6,λq hq=>?_⟩ rw[h9 q hq,h7,e0,e1,FD _ _ _ _ _ (λh=>у.mp h)] by_cases a:(ch+1)*hw<=q . rw[if_pos a,if_neg א,if_pos א] . rw[if_neg a];by_cases b:ch*hw<=q;. rw[if_pos ⟨b,by omega⟩,if_pos b,Nat.div_eq_of_lt_le b א] . rw[if_neg א,if_neg b] lemma Dc:G8=scaleShiftSpec:=by funext relu c hw x s t;unfold G8;split . rename_i h;obtain ⟨h1,h2⟩:=h;obtain ⟨h3,h4⟩:=Do relu c hw s t c 0 x א h1 h2;apply ъ;. simp[scaleShiftSpec,h3,h1] . intro i hi hj;have hi':i< c*hw:=by rwa [h3,h1] at hi rw[<-Е _ i hi,h4 i hi',if_pos ג];simp[scaleShiftSpec,Ah] . rfl lemma Ed (w:ZQ):toWord (ofW w)=w:=by apply И.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact Г w.toBitVec.isLt lemma CX (w:ZQ) (h:expOf w≠255):GG (ofW w)=(sv w:ℝ)*2^((idxOf w:ℤ)-149):=by rw[show GG (ofW w)=wv w from rfl,BF w h,rationalWord,decodeD_toRat];push_cast;rfl lemma FM (s j:ZQ) (hj:j.toNat<=6) (a:j.toNat<(expOf s).toNat): (expOf (s-(j <<< (23:ZQ)))).toNat=(expOf s).toNat-j.toNat ∧ fracOf (s-(j <<< (23:ZQ)))=fracOf s ∧ signOf (s-(j <<< (23:ZQ)))=signOf s:=by rw[Bw] at a have hs:=s.toNat_lt have e:(j <<< (23:ZQ)).toNat=j.toNat*2^23:=by rw[UInt32.toNat_shiftLeft,т] exact Г (by simp;omega) have f:j <<< (23:ZQ)<=s:=by rw[Ж,e];omega have d:(s-(j <<< (23:ZQ))).toNat=s.toNat-j.toNat*2^23:=by rw[س _ _ f,e] refine ⟨?_,?_,?_⟩ . rw[Bw,Bw,d];omega . apply И.mp;rw[fracOf,fracOf,Bx,Bx,d];omega . rw[DC,DC,d];simp;omega lemma Cn (s y j:ZQ) (f:j.toNat<=6) (a:expOf s≠255) (b:j.toNat<(expOf s).toNat) (d:expOf y≠255) (g:signOf y=false) (e:mantOf y=2^23) (k:idxOf y=126+j.toNat):toWord (Model.div (ofW s) (ofW y))=s-(j <<< (23:ZQ)):=by have A:sv y=2^23:=by unfold sv;rw[g,e];rfl have B:Model.isZero (ofW y)=false:=by rw[ofW,Model.isZero_eq_beq_zero_of_toDyadic?_some (toDyadic_ofUInt32 y d),decodeD_sig_zero,e];decide obtain ⟨C,D,E⟩:=FM s j f b;generalize s-(j <<< (23:ZQ))=r at C D E ⊢;have N:=expOf_toNat_lt s;have O:(expOf s).toNat≠255:=λF=>a (И.mp (by simpa using F));have l:expOf r≠255:=by intro h;have n:=congrArg UInt32.toNat h;simp at n;omega have q:expOf r≠0:=by intro h;have n:=congrArg UInt32.toNat h;simp at n;omega have t:expOf s≠0:=by intro h;rw[h] at b;simp at b have G:sv r=sv s:=by unfold sv mantOf;rw[E,D,if_neg q,if_neg t] have H:(idxOf r:ℤ)=(idxOf s:ℤ)-j.toNat:=by unfold idxOf;rw[if_neg q,if_neg t,C];omega have o:GE (ofW s)=true:=by simp[ofW,isFinite_ofUInt32,a] have u:GE (ofW y)=true:=by simp[ofW,isFinite_ofUInt32,d] have v:GE (ofW r)=true:=by simp[ofW,isFinite_ofUInt32,l] have x:GG (ofW y)=2^j.toNat:=by rw[CX _ d,A,k];push_cast;rw[show ((126:ℤ)+j.toNat-149)=(j.toNat:ℤ)+(-23) by ring,М С,Ц];field_simp;norm_num have I:GG (ofW r)=GG (ofW s)/2^j.toNat:=by rw[CX _ l,CX _ a,G,H,show ((idxOf s:ℤ)-j.toNat-149) =((idxOf s:ℤ)-149)+-(j.toNat:ℤ) by ring,М С,zpow_neg,Ц];field_simp have J:|GG (ofW s)/GG (ofW y)| ≤ GG (Model.posMaxFinite Fmt):=by rw[x,abs_div,abs_of_pos (by positivity:(0:ℝ)<2^j.toNat)];exact (div_le_self (П _) (one_le_pow₀ ב)).trans (Model.abs_toReal_le_posMaxFinite_of_isIEEE_of_isFinite _ (by decide) o) have z:GE (Model.div (ofW s) (ofW y))=true:=Model.isFinite_div_of_abs_div_le_posMaxFinite _ _ (by decide) o u B J;have p:=Model.toReal_div_eq_roundAt _ _ (by decide) o u B z;rw[x,<-I,Model.roundAt_toReal_eq _ v] at p have K:GG (Model.div (ofW s) (ofW y))≠0:=by rw[p,CX _ l];apply mul_ne_zero _ (zpow_ne_zero _ С);have:sv r≠0:=by unfold sv mantOf;split<;>omega exact_mod_cast this rw[Model.eq_of_toReal_eq_of_nonzero z v p K,Ed] lemma Ec (n:ZU) (j s:ZQ) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)):Al n j s=Ac n s:=by unfold Al;split . rename_i h;obtain ⟨h1,h2⟩:=h;have h3:=expOf_toNat_lt s;rw[п] at h1;rcases hj with rfl | ⟨rfl,rfl⟩ | ⟨rfl,rfl⟩;. exfalso;simp at h1;omega . exact (Cn s _ 2 (by decide) h2 h1 (by decide) (by decide) (by decide) (by decide)).symm . exact (Cn s _ 6 (by decide) h2 h1 (by decide) (by decide) (by decide) (by decide)).symm . rfl def Ag (xw:ZU->ZQ) (l:List ZU) (c:F1):F1:= l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv oneW F1.mk (acc.s+q) (acc.t+q.abs)) c lemma E5 (X off:ZS ZQ) (k w n:ZU) (j z:ZQ) (nf zb:ZW) (b m:ZX) (xw:ZU->ZQ) (hx:∀ t:ZX,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:ZU):∀ (t:ZX) (s a:ZW) (o:ZQ),t.toNat+d=m.toNat → An X off k w n j z (dv oneW) nf zb b m t s a o=Am X b k w n j z nf zb (Ag xw (List.range' t.toNat d) ⟨s,a⟩).s (Ag xw (List.range' t.toNat d) ⟨s,a⟩).t ((List.range' t.toNat d).foldl (λc i=>c ||| xw i) o):=by induction d with | zero => intro t s a o h rw[An,Х (by rw[А];omega)] rfl | succ d ih => intro t s a o h have ht:t< m:=by rw[А];omega rw[An,Р ht] dsimp only rw[hx t ht,ih (t+1) _ _ _ (by rw[usz t m ht];omega),usz t m ht,List.range'_succ] rfl lemma Fs (xw:ZU->ZQ) (l:List ZU):∀ o:ZQ,l.foldl (λc i=>c ||| xw i) o=0->o=0∧∀ i∈l,xw i=0:=by induction l with | nil=>exact λo h=>⟨h,by simp⟩ | cons i l ih => intro o h;obtain ⟨h1,h2⟩:=ih _ h;obtain ⟨h3,h4⟩:=UInt32.or_eq_zero_iff.mp h1;exact ⟨h3,List.forall_mem_cons.mpr ⟨h4,h2⟩⟩ lemma Fa (n:ZU) (hn:n<2^20) (xw:ZU->ZQ) (hg:∀ t,t< n->good (xw t)=true) (r:GA ((Ag xw (GY n) ⟨0,0⟩).s+dv 0) (nfOf n*((Ag xw (GY n) ⟨0,0⟩).t+(dv 0).abs))≠0xFFFFFFFF): GA ((Ag xw (GY n) ⟨0,0⟩).s+dv 0) (nfOf n*((Ag xw (GY n) ⟨0,0⟩).t+(dv 0).abs))=dotOf n xw (λ_=>oneW):=by obtain ⟨-,-,hbig⟩:=C1 _ _ r obtain ⟨hlo,hhi⟩:=C6 n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) (GY n) List.nodup_range ג (λt h1 h2=>absurd (н.mpr h1) h2) _ _ ⟨rfl,rfl⟩ hbig have hsum:((∑ j∈GR n,rationalWord (xw j)*rationalWord oneW:ℚ):ℝ) =B4 n xw (λ_=>oneW) 0:=by rw[B4,C9 0 (by decide) (by decide),add_zero,Rat.cast_sum] refine ы rfl λt ht=>?_ rw[Rat.cast_mul,BF _ (BM _ (hg t (Finset.mem_range.mp ht))),BF oneW (by decide)] obtain ⟨d,hd,hw⟩:=CO n xw (λ_=>oneW) (λj hj=>⟨BM _ (hg j hj),by decide⟩) λh=>Fe _ _ _ hlo hhi r (by rw[<-hsum,h,ع]) rw[hw] exact E9 n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) _ _ d (by rw[hd,hsum]) hlo hhi r lemma F2 (X:ZS ZQ) (b:ZX) (k w n:ZU) (hn:n<2^20) (j:ZQ) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) (xw:ZU->ZQ) (hg:∀ t,t< n->good (xw t)=true) (hs:Ad X b k w=dotOf n xw (λ_=>oneW)): Am X b k w n j (Ac n (B2 n (λ_=>0) (λ_=>oneW))) (nfOf n) (dv 0) (Ag xw (GY n) ⟨0,0⟩).s (Ag xw (GY n) ⟨0,0⟩).t ((GY n).foldl (λc i=>c ||| xw i) 0)=Ac n (dotOf n xw (λ_=>oneW)):=by unfold Am split . rename_i h1;obtain ⟨-,h5⟩:=Fs xw _ _ h1;have e:dotOf n xw (λ_=>oneW)=dotOf n (λ_=>0) (λ_=>oneW):=by unfold dotOf;congr 2;funext i;exact h5 _ (н.mpr i.isLt) rw[e,BJ] . dsimp only;rw[Ec _ _ _ hj];congr 1;split;. rw[E3];exact hs . rename_i hr;exact Fa _ hn xw hg (λh=>hr (beq_iff_eq.mpr h)) lemma Dg {c h w k ch y x t:ZU} (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (ht:t< k*k):(ch*h+y*k+t/k)*w+x*k+t%k< c*h*w:=by have hk:0< k:=by rcases Nat.eq_zero_or_pos k with rfl | h0;. simp at hy . exact h0 have h1:t/k< k:=х ht;have h2:t%k< k:=Ъ _ hk;have h3:(y+1)*k<=h:=(Nat.le_div_iff_mul_le hk).mp hy;have h4:(x+1)*k<=w:=(Nat.le_div_iff_mul_le hk).mp hx;rw[ر] at h3 h4;have h5:=З h (show ch+1<=c from hc);rw[ر] at h5;have h6:=З w (א:ch*h+y*k+t/k+1<=c*h);rw[ر] at h6;omega def G_P (N:ZU) (sp:ZU->ZQ) (out:ZS ZQ) (p:ZU):Prop:= out.size=N∧N<2^32∧∀ i,i< p->out[i]! = sp i lemma G0 {N p:ZU} {sp:ZU->ZQ} {out:ZS ZQ} (h:G_P N sp out p) (hp:p< N) (q:ZX) (hq:q.toNat=p):G_P N sp (wrW out q (sp p)) (p+1):=by obtain ⟨h1,h2,h3⟩:=h;refine ⟨(B9 _ _ _).trans h1,h2,λi hi=>?_⟩;rw[Bt _ _ _ _ א א,hq];by_cases e:i=p;. rw[if_pos e,e] . rw[if_neg e];exact h3 i א lemma Ff (a s i:ZU):(a+(i+1)*s).toUSize=(a+i*s).toUSize+s.toUSize:=by rw[show a+(i+1)*s=a+i*s+s by ring];simp only[Nat.toUSize_eq,USize.ofNat_add] lemma Cy {L:ZU->ZX->ZX->ZS ZQ->ZS ZQ} {g:ZX->ZX->ZS ZQ->ZS ZQ} {sb S n N q0 b0:ZU} {sp:ZU->ZQ} (hL:∀ i b q o,L i b q o =if i< n then L (i+1) (b+sb.toUSize) (q+S.toUSize) (g b q o) else o) (hs:∀ i o,i< n->G_P N sp o (q0+i*S) → G_P N sp (g (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+(i+1)*S)) (d:ZU): ∀ i o,i+d=n->G_P N sp o (q0+i*S) → G_P N sp (L i (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+n*S):=by induction d with | zero => intro i o h1 h2 rw[hL,if_neg א,<-h1] exact h2 | succ d ih => intro i o h1 h2 rw[hL,if_pos א,<-Ff,<-Ff] exact ih (i+1) _ א (hs i o א h2) lemma Fo {f:ZX->ZQ} {sc sy sx W H c N:ZU} {sp:ZU->ZQ} (hN:c*(H*W)=N) (hs:∀ ch y t,ch< c->y< H->t< W → f (ch*sc+y*sy+t*sx).toUSize=sp (ch*(H*W)+y*W+t)) (o:ZS ZQ) (ho:G_P N sp o 0): G_P N sp (G_c f sc.toUSize sy.toUSize sx.toUSize W H c 0 0 0 o) N:=by have:=Cy (L:=G_c f sc.toUSize sy.toUSize sx.toUSize W H c) (sb:=sc) (S:=H*W) (n:=c) (N:=N) (q0:=0) (b0:=0) (sp:=sp) (λ_ _ _ _=>by rw[G_c]) (λch o a1 h=>by have h4:(ch+1)*(H*W)<=N:=hN ▸ З _ a1 have:=Cy (L:=G_y f sy.toUSize sx.toUSize W H) (sb:=sy) (S:=W) (n:=H) (N:=N) (q0:=ch*(H*W)) (b0:=ch*sc) (sp:=sp) (λ_ _ _ _=>by rw[G_y]) (λy o a2 h=>by have h5:(y+1)*W<=H*W:=З _ a2 rw[ر] at h4 h5 have:=Cy (L:=G_x f sx.toUSize W) (g:=λb q o=>wrW o q (f b)) (sb:=sx) (S:=1) (n:=W) (N:=N) (q0:=ch*(H*W)+y*W) (b0:=ch*sc+y*sy) (sp:=sp) (λ_ _ _ _=>by rw[G_x];rfl) (λi o a3 h=>by have e1:ch*(H*W)+y*W+(i+1)*1 =ch*(H*W)+y*W+i*1+1:=א have e2:ch*(H*W)+y*W+i=ch*(H*W)+y*W+i*1:=א rw[e1,hs ch y i a1 a2 a3,e2] exact G0 h א _ (BK _ (by have:=h.2.1;omega))) W 0 o א (by simpa using h) simpa [Nat.succ_mul,GB] using this) H 0 o א (by simpa using h) simpa [Nat.succ_mul,GB] using this) c 0 o א (by simpa using ho) simp only[Nat.zero_mul,Nat.zero_add,Ga,hN] at this exact this lemma FT {c H W q:ZU} (hq:q< c*(H*W)):q/(H*W)< c∧q/W%H< H∧q%W< W:=by have hp:0< H*W:=Н (by rintro e;simp[e] at hq);exact ⟨х (by rwa [о]),Ъ _ (Nat.pos_of_mul_pos_right hp),Ъ _ (к hp)⟩ lemma Fb (f:ZX->ZQ) (c h w k:ZU) (hs:c*h*w<2^32): G_c f (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (GI (c*(h/k*(w/k))) 0) = GJ (n:=c*(h/k*(w/k))) λq =>let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) f ((ch*h+y*k)*w+x*k).toUSize:=by have hN:c*(h/k*(w/k))<2^32:=by have h1:h/k*(w/k)<=h*w:=Nat.mul_le_mul (Nat.div_le_self _ _) (Nat.div_le_self _ _) exact в ((Nat.mul_le_mul_left c h1).trans (Nat.mul_assoc c h w).symm.le) hs generalize hH:h/k=H at * generalize hW:w/k=W at * have hp:=Fo (f:=f) (sc:=h*w) (sy:=k*w) (sx:=k) (W:=W) (H:=H) (c:=c) (sp:=λq=>f ((q/(H*W)*h+q/W%H*k)*w+q%W*k).toUSize) rfl (λch y t h1 h2 h3=>by obtain ⟨d1,d2⟩:=C4 W (H*ch+y) t h3 obtain ⟨d3,d4⟩:=C4 H ch y h2 have e:ch*(H*W)+y*W+t=W*(H*ch+y)+t:=by ring rw[e,d2,о H W,<-ذ,d1,d3,d4] congr 2 ring) (GI (c*(H*W)) 0) ⟨by simp,hN,by simp⟩ apply ъ (by rw[hp.1,Array.size_ofFn]) intro i h1 h2 rw[<-Е _ i h1,hp.2.2 i (by simpa using h2),Array.getElem_ofFn] lemma Es {c h w k ch y x:ZU} {X:ZS ZQ} (h1:c*h*w<2^32) (h2:X.size<2^32) (h3:k*k<2^32) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (t:ZX) (ht:t.toNat< k*k): rdW X (((ch*h+y*k)*w+x*k).toUSize+(rdW (Ak k w) t).toUSize) =poolAt h w k X ch y x t.toNat:=by have h4:=Dg hc hy hx ht have e0:(ch*h+y*k+t.toNat/k)*w+x*k+t.toNat%k =(ch*h+y*k)*w+x*k+(t.toNat/k*w+t.toNat%k):=by ring rw[e0] at h4 have hz:(Ak k w).size<2^32:=by rw[Ak,Array.size_ofFn];exact h3 rw[B7 _ _ hz,Ak,BL (k*k) (λt=>(t/k*w+t%k).toUInt32) _ ht,B7 _ _ h2] have e1:((t.toNat/k*w+t.toNat%k).toUInt32.toUSize).toNat =t.toNat/k*w+t.toNat%k:=by rw[Т,Nat.toUInt32_eq,е] exact Г א have e2:((ch*h+y*k)*w+x*k).toUSize.toNat=(ch*h+y*k)*w+x*k:=BK _ א show _=X[(ch*h+y*k+t.toNat/k)*w+x*k+t.toNat%k]! rw[BO _ _ (by rw[e1,e2];omega),e1,e2,e0] lemma FQ {c h w k ch y x:ZU} {X:ZS ZQ} (h1:c*h*w<=X.size) (h2:X.size<2^32) (h3:k*k<2^20) (hg:∀ i,i< X.size->good X[i]! = true) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (j:ZQ) (hj:j=255∨(j=2∧k*k=4)∨(j=6∧k*k=64)): An X (Ak k w) k w (k*k) j (Ac (k*k) (B2 (k*k) (λ_=>0) (λ_=>oneW))) (dv oneW) (nfOf (k*k)) (dv 0) ((ch*h+y*k)*w+x*k).toUSize (k*k).toUSize 0 0 0 0 = Ac (k*k) (dotOf (k*k) (poolAt h w k X ch y x) (λ_=>oneW)):=by have hn:(k*k).toUSize.toNat=k*k:=BK _ א have hk:0< k*k:=by rcases Nat.eq_zero_or_pos k with rfl | h0 . simp at hy . exact Nat.mul_pos h0 h0 have hI:∀ t,t< k*k->(ch*h+y*k+t/k)*w+x*k+t%k< c*h*w:=λt ht=>Dg hc hy hx ht have e2:((ch*h+y*k)*w+x*k).toUSize.toNat=(ch*h+y*k)*w+x*k:=BK _ (by have:=hI 0 hk;simp at this;omega) rw[E5 X _ k w (k*k) j _ _ _ _ _ (poolAt h w k X ch y x) (λt ht=>Es א h2 א hc hy hx t (by rw[А,hn] at ht;exact ht)) (k*k) 0 0 0 0 (by rw[hn,show (0:ZX).toNat=0 from rfl,К]), show (0:ZX).toNat=0 from rfl,<-List.range_eq_range'] refine F2 X _ k w _ h3 j hj _ (λt ht=>hg _ (by have:=hI t ht;omega)) ?_ unfold Ad rw[BJ] congr 1 funext t rw[e2] show _=X[(ch*h+y*k+t/k)*w+x*k+t%k]! congr 1 ring lemma FH:Az=avgpoolSpec:=by funext c h w k X;unfold Az;split . rename_i hc;obtain ⟨h1,h2,h3,h4⟩:=hc;simp only[avgpoolSpec,avgpoolWith];rw[Ai,Fb _ c h w k א];apply congrArg GJ;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=FT hq;dsimp only;refine FQ h1 h2 h3 (B8 X h2 h4).2.2.1 a1 a2 a3 _ ?_;split_ifs with e1 e2;. exact Or.inr (Or.inl ⟨rfl,e1⟩) . exact Or.inr (Or.inr ⟨rfl,e2⟩) . exact Or.inl rfl . rfl end section open ExactRuntime def B5 (rows inW outW:ZU) (relu:ZT) (X W b:ZS ZQ):ZS ZQ:= if rows=1 then B0 ⟨inW,1,1,outW,1,1,1,0⟩ relu X W b else linearWith B2 rows inW outW relu X W b lemma G4 (inW outW:ZU) (relu:ZT) (X W b:ZS ZQ):linearSpec 1 inW outW relu X W b=conv2dSpec ⟨inW,1,1,outW,1,1,1,0⟩ relu X W b:=by simp only[linearSpec,linearWith,conv2dSpec,conv2dWith,ConvShape.ho,ConvShape.wo];apply ъ;. simp . intro q h1 h2;have hq:q< outW:=by simpa using h1 simp only[Array.getElem_ofFn];have a1:q/outW=0:=с hq;have a2:q%outW=q:=Г hq;have e2:(1+2*0-1)/1+1=1:=by simp simp only[a1,a2,e2,windowAt,Nat.mul_one,Nat.div_one,Nat.mod_one,Nat.zero_mul,Nat.zero_add,Ga,Nat.sub_zero];congr 2 lemma F5:B5=linearSpec:=by funext rows inW outW relu X W b;unfold B5;split;. subst_vars;rw[G4,Cb] . rw[BJ];rfl end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats @[inline] def ordW (w:Word):ZQ:= let m:=w &&& 0x7FFFFFFF if w >>> (31:ZQ) == 0 then 0x80000000+m else 0x80000000-m @[inline] def maxF (a b:Word):Word:= if a &&& 0x7F800000 == 0x7F800000 || b &&& 0x7F800000 == 0x7F800000 then maxW a b else let p:=ordW a let q:=ordW b if p< q then b else if q< p then a else if a == b then a else maxW a b def M_K (m:ZU):ZU:= if m/2^23=0 then m%2^23 else (2^23+m%2^23)*2^(m/2^23-1) lemma Bm:StrictMono M_K:=by refine strictMono_nat_of_lt_succ λm=>?_;unfold M_K;by_cases hc:m%2^23+1<2^23 . rw[show (m+1)/2^23=m/2^23 by omega,show (m+1)%2^23=m%2^23+1 by omega];split;. omega . exact Ш א (Nat.two_pow_pos _) . rw[show (m+1)/2^23=m/2^23+1 by omega,show (m+1)%2^23=0 by omega,show m%2^23=2^23-1 by omega];cases m/2^23 with | zero=>norm_num | succ e => have:=Nat.two_pow_pos e;rw[Д א,Д א,Nat.add_sub_cancel,Nat.add_sub_cancel,pow_succ];omega lemma CL (w:Word):(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31 ∧ (expOf w).toNat=w.toNat%2^31/2^23∧(fracOf w).toNat=w.toNat%2^31%2^23:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x7FFFFFFF:ZQ).toNat=2^31-1:=rfl;have a2:(0xFF:ZQ).toNat=2^8-1:=rfl;have a3:(0x7FFFFF:ZQ).toNat=2^23-1:=rfl;have a4:(23:ZQ).toNat%32=23:=rfl;unfold expOf fracOf;rw[О,О,О,л,a1,a2,a3,a4,В,В,В,Ю];omega lemma E2 (w:Word):signOf w=true ↔ 2^31<=w.toNat:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x80000000:ZQ).toNat=2^31:=rfl;unfold signOf;rw[bne_iff_ne,ne_eq,<-И,О,a1,Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];by_cases c:w.toNat/2^31%2=1;. rw[decide_eq_true c];simp only[Bool.toNat_true,Gb];omega . rw[decide_eq_false c];simp only[Bool.toNat_false,Gb];omega lemma En (w:Word):mantOf w <<< idxOf w=M_K (w.toNat%2^31):=by obtain ⟨-,h1,h2⟩:=CL w;unfold mantOf idxOf M_K;rw[т];by_cases h:expOf w=0;. rw[Л h,Л h,Л (by rw[<-h1,h];rfl),h2,pow_zero,Nat.mul_one] . rw[Д h,Д h,Д (λe=>h (И.mp (by rw[h1,e];rfl))),h1,h2] lemma Ey (w:Word):keyOf w=if w.toNat<2^31 then (M_K (w.toNat%2^31):ZY) else -(M_K (w.toNat%2^31):ZY):=by unfold keyOf;rw[En];by_cases c:w.toNat<2^31;. rw[Л c,Д (λe=>by have:=(E2 w).mp e;omega)] . rw[Д c,Л ((E2 w).mpr א)] lemma EA (w:Word):(ordW w).toNat =if w.toNat<2^31 then 2^31+w.toNat%2^31 else 2^31-w.toNat%2^31:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x80000000:ZQ).toNat=2^31:=rfl;have a2:(31:ZQ).toNat%32=31:=rfl;have e:(w >>> (31:ZQ)).toNat=w.toNat/2^31:=by rw[л,a2,Ю] have hm:=(CL w).1;unfold ordW;dsimp only;by_cases c:w.toNat<2^31;. have e0:w >>> (31:ZQ)=0:=И.mp (by rw[e,Gb];omega);rw[e0,Л (show ((0:ZQ) == 0)=true from rfl),Л c,UInt32.toNat_add,hm,a1];omega . have e0:(w >>> (31:ZQ) == 0)=false:=by rw[beq_eq_false_iff_ne];intro h0;rw[h0,Gb] at e;omega rw[e0,Д ג,Д c,UInt32.toNat_sub,hm,a1];omega lemma FV (a b:Word) (h:ordW a< ordW b):keyOf a< keyOf b:=by have h':=п.mp h;rw[EA,EA] at h';rw[Ey,Ey];have ha:a.toNat<2^32:=a.toNat_lt;have hb:b.toNat<2^32:=b.toNat_lt;have p0:∀ m,0< m->0< M_K m:=λm hm=>by have:=Bm hm;omega by_cases c1:a.toNat<2^31<;>by_cases c2:b.toNat<2^31 <;>simp only[c1,c2,ite_true,ite_false] at h' ⊢;. have:=Bm (א:a.toNat%2^31< b.toNat%2^31);omega . omega . by_cases hm:0< a.toNat%2^31;. have:=p0 _ hm;omega . have:=p0 (b.toNat%2^31) א;omega . have:=Bm (א:b.toNat%2^31< a.toNat%2^31);omega lemma Fu (x m n:ZU):x &&& (m <<< n)=((x >>> n) &&& m) <<< n:=by apply Nat.eq_of_testBit_eq;intro i;simp only[Nat.testBit_and,Nat.testBit_shiftLeft,Nat.testBit_shiftRight];by_cases h:n<=i;. simp[h,Nat.add_sub_cancel' h] . simp[h] lemma EC (w:Word) (h:¬ (w &&& 0x7F800000 == 0x7F800000)=true):expOf w≠255:=by intro e;apply h;have e1:=(CL w).2.1;rw[e,show (255:ZQ).toNat=255 from rfl] at e1;rw[beq_iff_eq,<-И,О,show (0x7F800000:ZQ).toNat=255 <<< 23 from rfl,Fu,show (255:ZU)=2^8-1 from rfl,В,Ю];have:w.toNat<2^32:=w.toNat_lt;rw[(א:w.toNat/2^23%2^8=2^8-1)] lemma Fi (a:Word) (hz:Model.isZero (ofW a)=true):a=0∨a=0x80000000:=by obtain ⟨-,h1,h2⟩:=CL a;have h:a.toNat<2^32:=a.toNat_lt;have e:ofW a=_:=ofUInt32_eq_ofFields a;rw[e,Model.isZero_ofFields Fmt _ _ _ (expOf_toNat_lt a) (fracOf_toNat_lt a)] at hz;have hz':((expOf a).toNat == 0 && (fracOf a).toNat == 0)=true:=hz;simp only[GQ,beq_iff_eq] at hz';rw[h1,h2] at hz';by_cases c:a.toNat<2^31;. exact Or.inl (И.mp (by rw[Gb];omega)) . exact Or.inr (И.mp (by rw[show (0x80000000:ZQ).toNat=2^31 from rfl];omega)) lemma Fd (a:Word) (ha:expOf a≠255):maxW a a=a:=by unfold maxW;rw[Д (by simpa using ha),compare_eq_iff_eq.mpr rfl];dsimp only;by_cases hz:Model.isZero (ofW a)=true;. rcases Fi a hz with rfl | rfl<;>decide . rw[Д (by simpa using hz)];exact Cc a lemma FG (a b:Word):maxF a b=maxW a b:=by unfold maxF;by_cases g:(a &&& 0x7F800000 == 0x7F800000 || b &&& 0x7F800000 == 0x7F800000)=true;. rw[Л g] . rw[Д g];simp only[GX,not_or] at g;have ha:=EC a g.1;have hb:=EC b g.2;have hn:¬ (expOf a=255∨expOf b=255):=by simp[ha,hb] dsimp only;by_cases p:ordW a< ordW b;. rw[Л p];unfold maxW;rw[Д hn,compare_lt_iff_lt.mpr (FV a b p)] . rw[Д p];by_cases q:ordW b< ordW a;. rw[Л q];unfold maxW;rw[Д hn,compare_gt_iff_gt.mpr (FV b a q)] . rw[Д q];by_cases e:(a == b)=true;. rw[Л e,eq_of_beq e,Fd b hb] . rw[Д e] def mpWin (X off:ZS ZQ) (b m t:ZX) (acc:ZQ):ZQ:= if h:t< m then mpWin X off b m (t+1) (maxF acc (rdW X (b+(rdW off t).toUSize))) else acc termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega def B6 (c h w k:ZU) (inp:ZS Word):ZS Word:= if c*h*w<2^32∧inp.size<2^32∧k*k<2^32 then let off:=Ak k w G_c (λb=>mpWin inp off b (k*k).toUSize 0 (rdW inp b)) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (GI (c*(h/k*(w/k))) 0) else maxpoolW c h w k inp lemma EN (X off:ZS ZQ) (b m:ZX) (xw:ZU->ZQ) (hx:∀ t:ZX,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:ZU): ∀ (t:ZX) (a:ZQ),t.toNat+d=m.toNat → mpWin X off b m t a=(List.range' t.toNat d).foldl (λa i=>maxF a (xw i)) a:=by induction d with | zero => intro t a h rw[mpWin,Х (by rw[А];omega)] rfl | succ d ih => intro t a h have ht:t< m:=by rw[А];omega rw[mpWin,Р ht,hx t ht,ih (t+1) _ (by rw[usz t m ht];omega),usz t m ht,List.range'_succ,GZ] lemma DM:B6=maxpoolSpec:=by rw[<-DL];funext c h w k inp;unfold B6;split . rename_i hc;obtain ⟨h1,h2,h3⟩:=hc;rw[Fb _ c h w k h1];simp only[maxpoolW];apply congrArg GJ;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=FT hq;dsimp only;have hn:(k*k).toUSize.toNat=k*k:=BK _ h3 have e:∀ {ch y x:ZU},ch< c->y< h/k->x< w/k → rdW inp ((ch*h+y*k)*w+x*k).toUSize=poolAt h w k inp ch y x 0:=by intro ch y x b1 b2 b3 have h4:=Dg (t:=0) b1 b2 b3 (by rcases Nat.eq_zero_or_pos k with rfl | h0;. simp at b2 . exact Nat.mul_pos h0 h0) simp only[Nat.zero_div,Nat.zero_mod,Ga] at h4;rw[B7 _ _ h2,BK _ א];simp[poolAt] rw[e a1 a2 a3,EN inp _ _ _ (poolAt h w k inp _ _ _) (λt ht=>Es h1 h2 h3 a1 a2 a3 t (by rw[А,hn] at ht;exact ht)) (k*k) 0 _ (by rw[hn,show (0:ZX).toNat=0 from rfl,К]),show (0:ZX).toNat=0 from rfl,<-List.range_eq_range'];simp only[FG] . rfl end def baseKernels:Kernels where core:=⟨G9,B3⟩ dense:=⟨B5,gemmWith B2⟩ conv:=⟨B0,B1⟩ elem:=⟨Az,B6,Ay,G8⟩ norm:=⟨rmsnormWith B2,layernormWith B2⟩ theorem base_correct:Obligation baseKernels:=by simp only[Obligation,baseKernels,specKernels,specCore,specDense,specConv,specElem,specNorm,gemmSpec,avgpoolSpec,rmsnormSpec,layernormSpec,F5,Cb,DH,EP,Dc,FH,DM,BJ,funext D1,funext λa=>funext (D4 a)] end Submission namespace Submission open ExactRuntime @[inline] def win4 (X : Array Word) (w : Nat) (z : Word) (u nf zb : Float) (b : USize) : Word := let a0 := rdW X b let a1 := rdW X (b+1) let a2 := rdW X (b+w.toUSize) let a3 := rdW X (b+w.toUSize+1) let d0 := dv a0*u let d1 := dv a1*u let d2 := dv a2*u let d3 := dv a3*u Am X b 2 w 4 2 z nf zb ((((0:Float)+d0)+d1)+d2+d3) ((((0:Float)+d0.abs)+d1.abs)+d2.abs+d3.abs) (a0|||a1|||a2|||a3) lemma off (w : Nat) (t : USize) (ht : t.toNat<4) : rdW (Ak 2 w) t = (t.toNat/2*w+t.toNat%2).toUInt32 := by rw [B7 _ _ (by simp [Ak])] unfold Ak exact BL 4 (fun t=>(t/2*w+t%2).toUInt32) t.toNat ht lemma cast (w : Nat) (hw : w<2^32) : w.toUInt32.toUSize = w.toUSize := by apply USize.toNat_inj.mp rw [UInt32.toNat_toUSize,Nat.toUInt32_eq,UInt32.toNat_ofNat',Nat.mod_eq_of_lt hw,BK w hw] lemma win (X : Array Word) (w : Nat) (hw : w+1<2^32) (z : Word) (u nf zb : Float) (b : USize) : win4 X w z u nf zb b = An X (Ak 2 w) 2 w 4 2 z u nf zb b 4 0 0 0 0 := by have h0 := off w 0 (by simp) have h1 := off w 1 (by simp) have h2 := off w 2 (by simp) have h3 := off w 3 (by simp) norm_num [USize.toNat_ofNat_of_lt_32] at h0 h1 h2 h3 unfold win4 have hlt0 : (0:USize)<4 := by simp [USize.lt_iff_toNat_lt] have hlt1 : (1:USize)<4 := by simp [USize.lt_iff_toNat_lt] have hlt2 : (2:USize)<4 := by simp [USize.lt_iff_toNat_lt] have hlt3 : (3:USize)<4 := by simp [USize.lt_iff_toNat_lt] have hinc1 : (1:USize)+1=2 := by apply USize.toNat_inj.mp;rw[BO 1 1 (by simp)];simp have hinc2 : (2:USize)+1=3 := by apply USize.toNat_inj.mp;rw[BO 2 1 (by simp)];simp have hinc3 : (3:USize)+1=4 := by apply USize.toNat_inj.mp;rw[BO 3 1 (by simp)];simp rw [An,dif_pos hlt0,USize.zero_add] rw [An,dif_pos hlt1,hinc1] rw [An,dif_pos hlt2,hinc2] rw [An,dif_pos hlt3,hinc3] rw [An,dif_neg (show ¬(4:USize)<4 from by simp [USize.lt_iff_toNat_lt])] have h3' : rdW (Ak 2 w) 3=(w+1).toUInt32 := by simpa [Nat.toUInt32_eq,UInt32.ofNat_add] using h3 rw [h0,h1,h2,h3'] have hz : (0:UInt32).toUSize=(0:USize) := by apply USize.toNat_inj.mp;simp [UInt32.toNat_toUSize] have ho : (1:UInt32).toUSize=(1:USize) := by apply USize.toNat_inj.mp;simp [UInt32.toNat_toUSize] rw [hz,ho,cast w (by omega),cast (w+1) hw] simp [Nat.toUSize_eq,USize.ofNat_add, USize.add_assoc] def fastAvg (c h w k : Nat) (X : Array Word) : Array Word := if c*h*w<=X.size && X.size<2^32 && k*k<2^20 && X.all good then let n:=k*k let j:UInt32:=if n=4 then 2 else if n=64 then 6 else 255 let z:=Ac n (B2 n (fun _=>0) (fun _=>oneW)) if k==2 && w+1<2^32 then G_c (win4 X w z (dv oneW) (nfOf 4) (dv 0)) (h*w).toUSize (2*w).toUSize 2 (w/2) (h/2) c 0 0 0 (GI (c*(h/2*(w/2))) 0) else Ai c h w k n X (Ak k w) j z (dv oneW) (nfOf n) (dv 0) 0 else Az c h w k X lemma grid (c h w k : Nat) (X : Array Word) (h1:c*h*w≤X.size) (h2:X.size<2^32) (h3:k*k<2^20) (hg:∀i,i0) (fun _=>oneW))) (dv oneW) (nfOf (k*k)) (dv 0) 0 = avgpoolSpec c h w k X := by unfold avgpoolSpec avgpoolWith rw [Ai,Fb _ c h w k (lt_of_le_of_lt h1 h2)] apply congrArg GJ funext ⟨q,hq⟩ obtain ⟨a1,a2,a3⟩ := FT hq dsimp only refine FQ h1 h2 h3 hg a1 a2 a3 _ ?_ split_ifs with e1 e2 · exact Or.inr (Or.inl ⟨rfl,e1⟩) · exact Or.inr (Or.inr ⟨rfl,e2⟩) · exact Or.inl rfl lemma avg_eq : fastAvg=avgpoolSpec := by funext c h w k X unfold fastAvg split · rename_i hg simp only [GQ,decide_eq_true_eq] at hg obtain ⟨⟨⟨h1,h2⟩,h3⟩,ha⟩ := hg have hall : ∀i,i Ac (k.val*k.val) (B2 (k.val*k.val) (fun _=>0) (fun _=>oneW)) def PT_zero (k:Nat) : Word := if h:k<16 then PT_zeros[k]'(by simp [PT_zeros];exact h) else Ac (k*k) (B2 (k*k) (fun _=>0) (fun _=>oneW)) lemma PT_zero_eq (k:Nat) : PT_zero k=Ac (k*k) (B2 (k*k) (fun _=>0) (fun _=>oneW)) := by unfold PT_zero split · simp only [PT_zeros,Array.getElem_ofFn] · rfl def PT_avg (c h w k : Nat) (X : Array Word) : Array Word := if c*h*w<=X.size && X.size<2^32 && k*k<2^20 && X.all good then let n:=k*k let j:UInt32:=if n=4 then 2 else if n=64 then 6 else 255 let z:=PT_zero k if k==2 && w+1<2^32 then G_c (win4 X w z (dv oneW) (nfOf 4) (dv 0)) (h*w).toUSize (2*w).toUSize 2 (w/2) (h/2) c 0 0 0 (GI (c*(h/2*(w/2))) 0) else Ai c h w k n X (Ak k w) j z (dv oneW) (nfOf n) (dv 0) 0 else Az c h w k X def kernels : Kernels := { baseKernels with elem := { baseKernels.elem with avgpool := PT_avg } } lemma PT_avg_eq_old : PT_avg=fastAvg := by funext c h w k X simp only [PT_avg,fastAvg,PT_zero_eq] lemma PT_avg_eq : PT_avg=avgpoolSpec := PT_avg_eq_old.trans Submission.avg_eq theorem correct : Obligation kernels := by simpa only [kernels,PT_avg_eq,baseKernels,FH] using base_correct end Submission