import ContractExact namespace Submission 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"Ю"=>Nat.shiftRight_eq_div_pow local notation"Я"=>Nat.add_zero 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"з"=>Nat.add_assoc 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:Array Nat neg:Array Nat special:Bool negZero:Bool def Acc.empty:Acc:=⟨Array.replicate 18 0,Array.replicate 18 0,false,true⟩ def Acc.total (acc:Acc):Int:=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:Nat) (neg:Bool):Acc := match acc with | ⟨pos,ng,sp,nz⟩ => if m=0 then ⟨pos,ng,sp,nz && neg⟩ else if neg then ⟨pos,addLimbs64 ng m.toUInt64 idx,sp,false⟩ else ⟨addLimbs64 pos m.toUInt64 idx,ng,sp,false⟩ @[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:Nat) (neg:Bool) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hm:m<2^64) (hi:idx/32+1<18) : (acc.add m idx neg).pos.size=18∧(acc.add m idx neg).neg.size=18 ∧ (acc.add m idx neg).special=acc.special ∧ (acc.add m idx neg).negZero=(acc.negZero && (decide (m=0) && neg)) ∧ (acc.add m idx neg).total=acc.total+(if neg then -(m:Int) else m)*2^idx:=by obtain ⟨pos,ng,sp,nz⟩:=acc;simp only at hp hn;by_cases hm0:m=0;. simp[Acc.add,Acc.total,hm0,hp,hn] . cases neg;. refine ⟨by simp[Acc.add,hm0,addLimbs64_size,hp],by simp[Acc.add,hm0,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,Bool.false_eq_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring . refine ⟨by simp[Acc.add,hm0,hp],by simp[Acc.add,hm0,addLimbs64_size,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,ite_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring lemma Acc.total_empty:Acc.empty.total=0:=by decide section open FloatLib.Numerics lemma qCb (s:Bool) (m:Nat) (e:Int) (j:Nat) (h:j+25<=m.log2) : Model.roundDyadic Fmt ⟨s,shiftRightJam m j,e+j⟩=Model.roundDyadic 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:Nat):Int)+(e+j)=(m.log2:Int)+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:Nat,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:Array Nat) (i:Nat):Nat->Nat | 0=>0 | k+1=>a[i]! + 2^32*winVal a (i+1) k lemma qAj (a:Array Nat) (i k m:Nat) : 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 qDi (a b:Array Nat) (i j k:Nat) (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 qAa (a:Array Nat) (i k:Nat) : 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 qFe (a:Array Nat) (i k:Nat) (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[Nat.add_zero] at h0;rw[winVal,show 32*(k+1)=32+32*k by ring,pow_add];nlinarith lemma qBa (a:Array Nat) (t:Nat) (h:a.size<=t):a[t]! = 0:=by simp[getElem!_neg a t א] lemma qBx (a:Array Nat) (i k:Nat) (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[qBa a i א,List.drop_of_length_le (by simp;omega),List.drop_of_length_le (by simp;omega)];simp[limbVal] lemma qCs (a:Array Nat) (lo n:Nat) (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),<-qBx a 0 (lo+(n+a.size)) א,qAj,qAj];rw[(qAa a 0 lo).2 (λt ht=>by simpa using hlow t ht),(qAa a (0+lo+n) a.size).2 (λt _=>hhigh _ א)];simp def diffGo (a b:Array Nat) (i ca cb br:Nat) (out:Array Nat) : Nat->Array Nat × Nat × Nat × Nat | 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 qB0 (a b:Array Nat) (i ca cb br:Nat) (out:Array Nat) (k:Nat) (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:Int)+2^(32*(out.size+k))*(r.2.1-r.2.2.1-r.2.2.2:Int) = winVal out 0 out.size+2^(32*out.size) * ((winVal a i k:Int)-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[qAj,qDi _ out 0 0 out.size (λs hs=>by rw[Е _ _ (by simp;omega),ط א, <-Е out _ א])] simp only[winVal,Nat.zero_add,Nat.mul_zero,Nat.add_zero] 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:Nat):Int)+4294967296*((ta/4294967296:Nat)-(tb/4294967296:Nat)-(1-t/4294967296:Nat)) =(a[i]! : Int)-b[i]! + ca-cb-br:=by have e1:=ص ta 4294967296 have e2:=ص tb 4294967296 have e3:=ص t 4294967296 have e4:(1-t/4294967296:Nat)=1-(t/4294967296:Int) :=א have e5:(t:Int)=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:Int)^(32*out.size)*hstep def topGo (r:Array Nat):Nat->Option Nat | 0=>none | k+1=>if r[k]! != 0 then some k else topGo r k def anyGo (r:Array Nat):Nat->Bool | 0=>false | k+1=>r[k]! != 0 || anyGo r k lemma qEf (r:Array Nat) (k:Nat) (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 qCj (r:Array Nat) (k h:Nat) (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 qBz (r:Array Nat) (k:Nat):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 qFz (r:Array Nat) (k:Nat):anyGo r k=(winVal r 0 k != 0):=by have hz:=qAa r 0 k;simp only[Nat.zero_add] at hz;cases h:anyGo r k;. rw[hz.2 ((qBz r k).1 h)];rfl . have:winVal r 0 k≠0:=λh0=>by have:=(qBz r k).2 (hz.1 h0);simp_all simp[this] lemma qEl (low x y h:Nat) (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:Nat)^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:Bool) (r:Array Nat) (e:Int) (negZero:Bool):UInt32 := match topGo r r.size with | none=>finishWord 0 e negZero | some 0=>finishWord (if neg then -(r[0]! : Int) 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 (Model.roundDyadic Fmt ⟨neg,m,e+((32*h+2:Nat):Int)⟩) lemma qBw (neg:Bool) (R:Nat) (e:Int) (negZero:Bool) (hR:R≠0) : finishWord (if neg then -(R:Int) else R) e negZero=toWord (Model.roundDyadic Fmt ⟨neg,R,e⟩):=by have hpos:(0:Int)< R:=by exact_mod_cast Н hR cases neg<;>simp[finishWord,ofIntDyadic,hR,not_lt.mpr hpos.le,Nat.pos_of_ne_zero hR] lemma qBd (neg:Bool) (r:Array Nat) (e:Int) (negZero:Bool) (hr:∀ t,t< r.size->r[t]! < 2^32) : finishDigits neg r e negZero =finishWord (if neg then -(winVal r 0 r.size:Int) else winVal r 0 r.size) e negZero:=by unfold finishDigits split . rename_i hnone;rw[(qAa r 0 r.size).2 (λt ht=>by simpa using qEf r _ hnone t ht)];simp . rename_i hsome;obtain ⟨h1,h2,h3⟩:=qCj r _ 0 hsome;rw[show r.size=1+(r.size-1) by omega,qAj,(qAa r (0+1) (r.size-1)).2 (λt ht=>h3 _ א א)];simp[winVal] . rename_i h hsome;obtain ⟨h1,h2,h3⟩:=qCj 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,qAj,qAj,(qAa r (0+h+2) _).2 (λt ht=>h3 _ א א)];simp[winVal] have hlow:winVal r 0 h<2^(32*h):=qFe 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[qBw neg _ e negZero hR0,<-qCb neg _ e _ hlog,hR,qEl _ _ _ h hlow,qFz] def highLimb (pos neg:Array Nat):Nat->Nat | 0=>0 | k+1=>if pos[k]! != 0 || neg[k]! != 0 then k+1 else highLimb pos neg k lemma qC2 (pos neg:Array Nat) (k:Nat) : ∀ 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 [Bool.or_eq_true,bne_iff_ne,not_or] using hk def finishFast (pos neg:Array Nat) (e:Int) (negZero:Bool):UInt32 := 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:Nat):Int) 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 qC3 (pos neg:Array Nat) (e:Int) (negZero:Bool) : finishFast pos neg e negZero =finishWord ((limbVal pos.toList:Int)-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:=qC2 pos neg (max pos.size neg.size) have hzero (a:Array Nat) (ha:a=pos∨a=neg) : limbVal a.toList=2^(32*lo)*winVal a lo n:=by apply qCs 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 qBa _ t א have hP:=hzero pos (Or.inl rfl) have hN:=hzero neg (Or.inr rfl) rw[show (Array.emptyWithCapacity n:Array Nat)=#[] from rfl] have hd:=qB0 pos neg lo 0 0 0 #[] n א ג have hd':=qB0 neg pos lo 0 0 0 #[] n א ג have hw0:winVal (#[]:Array Nat) 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[qBd false _ _ _ hb,finishWord_scale,hP,hN];simp only[Bool.false_eq_true,ite_false];congr 1;rw[h1.1,h1.2.1,h1.2.2] at hv;push_cast at hv ⊢;linear_combination (2:Int)^(32*lo)*hv . rw[qBd 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:Int)^(32*lo))*hv' . exact finishLimbs_eq pos neg e negZero end @[inline] def dotStep (acc:Acc) (wa wb:UInt32):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:Nat->UInt32) (pos neg:Array Nat) (sp nz:Bool) (index:Nat):Nat->Acc | 0=>⟨pos,neg,sp,nz⟩ | count+1 => match dotStep ⟨pos,neg,sp,nz⟩ (fa index) (fb index) with | ⟨p,n,s,z⟩=>dotLoop fa fb p n s z (index+1) count @[inline] def dotFn (n:Nat) (fa fb:Nat->UInt32):UInt32 := let acc:=dotLoop fa fb (Array.replicate 18 0) (Array.replicate 18 0) false true 0 n if acc.special then dotSpec (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) else finishFast acc.pos acc.neg (-298) (n≠0 && acc.negZero) lemma qHx (wa wb:UInt32) : (if Bool.xor (signOf wa) (signOf wb) then -((mantOf wa*mantOf wb:Nat):Int) else ((mantOf wa*mantOf wb:Nat):Int))=sv wa*sv wb:=by unfold sv;cases signOf wa<;>cases signOf wb<;>simp lemma qCa (fa fb:Nat->UInt32) (pos neg:Array Nat) (nz:Bool) (index count:Nat):(dotLoop fa fb pos neg true nz index count).special=true:=by induction count generalizing pos neg nz index with | zero=>rfl | succ count ih => rw[dotLoop];have hs:(dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)).special=true:=by unfold dotStep Acc.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 qDm (fa fb:Nat->UInt32) (A B:Array UInt32) (n:Nat) (hA:∀ k,k< n->A[k]! = fa k) (hB:∀ k,k< n->B[k]! = fb k) (acc:Acc) (index remaining:Nat) (h:index+remaining<=n) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hfin:(dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).special =false) : 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[qCa] 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,qHx];ring . rw[hz,h4,dallNZ,ha,hb];simp[Bool.and_assoc] lemma qAe (n:Nat) (f:Nat->UInt32) (k:Nat) (hk:k< n) : (Array.ofFn (n:=n) λi=>f i.val)[k]! = f k:=by rw[Е _ k (by simpa using hk),Array.getElem_ofFn] lemma qB9 (n:Nat) (fa fb:Nat->UInt32) : dotFn n fa fb =dotSpec (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i):=by unfold dotFn set A:=Array.ofFn (n:=n) λi=>fa i with hA set B:=Array.ofFn (n:=n) λi=>fb i with hB set acc:=dotLoop fa fb (Array.replicate 18 0) (Array.replicate 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⟩:=qDm fa fb A B n (qAe n fa) (qAe 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',Bool.false_eq_true,ite_false];rw[qC3,show ((limbVal acc.pos.toList:Int)-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 (Model.ofUInt32 (fmt:=Fmt)) with hL;set R:=B.map (Model.ofUInt32 (fmt:=Fmt)) with hR;have hLR:L.size=R.size:=by simp[hL,hR,hAsize,hBsize] have hLsize:L.size=n:=by simp[hL,hAsize] have hLfin:∀ k (hk:k< L.size),Model.isFinite 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),Model.isFinite 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 qAz (a:Array UInt32) (n:Nat) (h:n=a.size) : (Array.ofFn (n:=n) λi=>a[i.val]!)=a:=by subst h;apply ъ ג;intro i h1 h2;rw[Array.getElem_ofFn,Е a i h2] lemma qEq (n:Nat) (c:UInt32) : (Array.ofFn (n:=n) λ_=>c)=Array.replicate n c:=by apply ъ ג;intro i h1 h2;simp def dotX (a b:Array UInt32):UInt32 := if a.size=b.size then dotFn a.size (λi=>a[i]!) (λi=>b[i]!) else dotSpec a b def sumX (x:Array UInt32):UInt32:=dotFn x.size (λi=>x[i]!) (λ_=>oneW) lemma qDd (a b:Array UInt32):dotX a b=dotSpec a b:=by unfold dotX;split;. rename_i h;rw[qB9,qAz a _ rfl,qAz b _ h] . rfl lemma qDa (x:Array UInt32):sumX x=sumSpec x:=by rw[sumX,qB9,qAz x _ rfl,qEq,sumSpec] lemma qAc:dotFn=dotOf :=funext λn=>funext λfa=>funext λfb=>qB9 n fa fb lemma qCl (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 qCc (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,Model.ofUInt32,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z @[inline] def keyOf (w:Word):Int := if signOf w then -((mantOf w <<< idxOf w:Nat):Int) else ((mantOf w <<< idxOf w:Nat):Int) lemma qF3 (w:Word):keyOf w=sv w*2^idxOf w:=by unfold keyOf sv;split<;>simp[Nat.shiftLeft_eq] lemma qAg (w:Word) : (decodeD w).toReal=(keyOf w:ℝ)*2^(-149:Int):=by rw[<-ж,decodeD_toRat,qF3];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 qDw (a b:Word) : Model.cmpDyadic (decodeD a) (decodeD b)=compare (keyOf a) (keyOf b):=by have hs:(0:ℝ)<2^(-149:Int):=ю two_pos _;cases h:compare (keyOf a) (keyOf b);. rw[Model.cmpDyadic_lt_iff,qAg,qAg];exact Ш (by exact_mod_cast compare_lt_iff_lt.mp h) hs . rw[Model.cmpDyadic_eq_iff,qAg,qAg,compare_eq_iff_eq.mp h] . rw[Model.cmpDyadic_gt_iff,qAg,qAg];exact Ш (by exact_mod_cast compare_gt_iff_gt.mp h) hs lemma qFc (a b:Word):ofW (maxW a b)=Model.maximum (ofW a) (ofW b):=by unfold maxW;split;. exact qCc _ . 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:=Model.isNaN_eq_false_of_toDyadic?_some ha;have hnb:=Model.isNaN_eq_false_of_toDyadic?_some hb;have hc:Model.compare (ofW a) (ofW b)=some _ :=Model.compare_eq_some_cmpDyadic_of_toDyadic? _ _ ha hb;rw[qDw] 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[qCc] def maxpoolW (c h w k:Nat) (inp:Array Word):Array Word := Array.ofFn (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) (List.range (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 qEa (p:Nat->Word) (l:List Nat) (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[List.foldl_cons,<-qFc,ih] lemma qDt:maxpoolW=maxpoolSpec:=by funext c h w k inp;simp only[maxpoolW,maxpoolSpec,qEa,qCl] lemma usz (t n:USize) (h:t< n):(t+1).toNat=t.toNat+1:=by have h1:t.toNat< n.toNat:=А.mp h;have h2:n.toNat< USize.size:=n.toNat_lt_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add,Щ];exact Г א lemma qAh (a b:USize) (h:a.toNat+b.toNat<2^32):(a+b).toNat=a.toNat+b.toNat:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add];exact Г א lemma uk (k:Nat) (h:k<2^32):(USize.ofNat k).toNat=k:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_ofNat'];exact Г א lemma qB4 (a:FloatArray) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[FloatArray.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega lemma qAm (a:Array UInt32) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega @[inline] def rdF (a:FloatArray) (i:USize):Float := if h:i< a.usize then a.uget i (qB4 a i h) else 0 @[inline] def rdW (a:Array UInt32) (i:USize):UInt32 := if h:i< a.usize then a.uget i (qAm a i h) else 0 @[inline] def wrF (a:FloatArray) (i:USize) (v:Float):FloatArray := if h:i< a.usize then a.uset i v (qB4 a i h) else a @[inline] def wrW (a:Array UInt32) (i:USize) (v:UInt32):Array UInt32 := if h:i< a.usize then a.uset i v (qAm a i h) else a def pwEntry (i:Nat):Float := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then Float.ofBits s else Float.ofBits (s ||| ((e+873) <<< (52:UInt64))) def pw:FloatArray := (List.range 512).foldl (λa i=>a.push (pwEntry i)) (FloatArray.emptyWithCapacity 512) lemma qDy:pw.data=(List.range 512).toArray.map pwEntry:=by have key:∀ (l:List Nat) (a:FloatArray),(l.foldl (λa i=>a.push (pwEntry i)) a).data=a.data ++ (l.toArray.map pwEntry):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pw,key];simp[FloatArray.emptyWithCapacity] lemma qGu:pw.size=512:=by simp[FloatArray.size,qDy] lemma qHa (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pw.size:=by rw[qGu];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega @[inline] def dv (w:UInt32):Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:UInt32)).toUSize (qHa w) @[inline] def good (w:UInt32):Bool := let v:=w &&& 0x7FFFFFFF v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32) def scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):UInt32 := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& 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 qAk (a:Array UInt32):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ def pushZ (n k:USize) (a:FloatArray):FloatArray := if h:k< n then pushZ n (k+1) (a.push 0) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega @[inline] def ziv (s e:Float):UInt32 := let x:UInt64:=((s+e).toBits+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let y:UInt64:=((s-e).toBits+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let ex:UInt64:=(x >>> (23:UInt64)) &&& (0x7FF:UInt64) if x == y && ex-(897:UInt64)<(254:UInt64) then (((x >>> (34:UInt64)) <<< (31:UInt64)) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64)))).toUInt32 else 0xFFFFFFFF structure F1 where (s t:Float) structure R1 where (ix:Array UInt32) (buf:FloatArray) (c:USize) (hi:UInt32) @[inline] def mx (a w:UInt32):UInt32 := let m:=w &&& 0x7FFFFFFF if m<=a then a else m def decR (X:Array UInt32) (o n t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hi:UInt32):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:USize) (W:Array UInt32) (hw:w.toNat+m.toNat<=W.size) (hs:W.size<2^32) (hi:i.toNat< m.toNat):(w+i).toNat< W.size:=by rw[qAh _ _ א];omega structure F4 where (a b c d:Float) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary noncomputable def R (f:Float):ℝ:=Model.toReal (toModel (ofFloat f)) noncomputable def wv (w:UInt32):ℝ:=Model.toReal (ofW w) noncomputable def exactS (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32):ℝ := (Finset.range n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:Nat):Float:=(n.toUInt64+8).toFloat*Float.ofBits 0x3CC0000000000000 lemma qAl:FloatFormat.binary64.isIEEE=true:=by decide lemma qB1 (a:Model FloatFormat.binary64) : Model.isFinite a=(Model.toModel a).isFinite:=by have hd:=Model.toDyadic?_ieee_eq_model _ qAl a;cases hm:Model.toModel a with | notANumber => rw[hm] at hd;cases h:Model.isFinite 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:Model.isFinite 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:Float):Model FloatFormat.binary64:=Model.ofFloatModel x.toModel lemma A_R (x:Float):R x=Model.toReal (A_M x):=rfl lemma qAb (x:Float):Model.isFinite (A_M x)=x.isFinite:=by rw[qB1,Model.toModel_ofFloatModel];rfl lemma qAx (e:ℤ):Formats.Flocq.bpow FloatLib.Numerics.binaryRadix e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma qA7:(2:ℝ)^(1000:ℤ)<=Model.toReal (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,qAx] rw[h3,show (1000:ℤ)=29+971 from rfl,М ב];apply Б _ (by positivity);norm_num lemma qH7 (x y:Float) (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:Model.isFinite (A_M x)=true:=by rw[qAb,hx] have hfy:Model.isFinite (A_M y)=true:=by rw[qAb,hy] constructor . rw[<-qAb,hm];exact Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAl hfx hfy (hb.trans qA7) . rw[A_R,hm];exact Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAl hfx hfy (hb.trans qA7) lemma qH1 (x y:Float) (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:Model.isFinite (A_M x)=true:=by rw[qAb,hx] have hfy:Model.isFinite (A_M y)=true:=by rw[qAb,hy] constructor . rw[<-qAb,hm];exact Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAl hfx hfy (hb.trans qA7) . rw[A_R,hm];exact Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAl hfx hfy (hb.trans qA7) open Float.Model.UnpackedFloat in lemma qD5 (b:BitVec 64) (s m e h) (hu:Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 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:=Float.Model.Format.binary64) b).toNat=0:=by simp[h3] rw[this] at he;omega . injection hu with _ hm he;left;have:(unpackExponent (spec:=Float.Model.Format.binary64) 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 qD8 (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 qIb (s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂) (b₁ b₂:BitVec 64) (hu₁:unpack Float.Model.Format.binary64 b₁=.finite s₁ m₁ e₁ h₁) (hu₂:unpack Float.Model.Format.binary64 b₂=.finite s₂ m₂ e₂ h₂) : e₁+e₂<=Float.Model.Format.binary64.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have c₁:=qD5 _ _ _ _ _ hu₁ have c₂:=qD5 _ _ _ _ _ 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 qDq (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) : A_M (x*y)=Model.mul (A_M x) (A_M y):=by have hX:Model.toModel (A_M x)=x.toModel.unpack:=Model.toModel_ofFloatModel _;have hY:Model.toModel (A_M y)=y.toModel.unpack:=Model.toModel_ofFloatModel _;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 _ qAl (A_M x),Model.toDyadic?_ieee_eq_model _ qAl (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,Bool.false_eq_true] 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:=qIb s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂ _ _ hxm hym;have h0:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] rw[<-qD8 _ _ _ _ hle,h0];have hs:Model.modelSign (Model.modelSignBit s₁ ^^ Model.modelSignBit s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[Bool.false_eq_true,ite_false,Model.roundDyadic,qAl,ite_true,Model.ieeeRoundDyadic,hs];rfl lemma qD6 (x y:Float) (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):=qDq x y hx hy have hfx:Model.isFinite (A_M x)=true:=by rw[qAb,hx] have hfy:Model.isFinite (A_M y)=true:=by rw[qAb,hy] constructor . rw[<-qAb,hm];exact Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAl hfx hfy (hb.trans qA7) . rw[A_R,hm];exact Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAl hfx hfy (hb.trans qA7) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qBh:0< A_z:=by unfold A_z;positivity lemma qCh: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 qCn:0< A_B:=by unfold A_B;positivity lemma qAq:(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 qDh: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 qDn: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 qEh (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[qAx] at hl rw[qAx,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 qDo (x y:Float) (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⟩:=qH7 x y hx hy (by have:=qDh;linarith);exact ⟨hf,he ▸ qEh _⟩ lemma qHc (x y:Float) (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⟩:=qH1 x y hx hy (by have:=qDh;linarith);exact ⟨hf,he ▸ qEh _⟩ lemma qC8 (x y:Float) (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 (П _) qCn.le).trans qDn;obtain ⟨hf,he⟩:=qD6 x y hx hy hb;exact ⟨hf,he ▸ qEh _⟩ lemma qAn (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,qAx] . exact hm . exact hk lemma qAp (x:Float):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,Model.toReal_eq_unpackedToReal_toModel qAl,Model.toModel_ofFloatModel] lemma qEx (f:Float) (s m e h) (hu:f.toModel.unpack=.finite s m e h) : (Model.ofModel FloatFormat.binary64 (.finite .positive m e h)).toModel=.finite .positive m e h:=by have hm:Model.toModel (A_M f)=.finite s m e h:=by rw[Model.toModel_ofFloatModel,hu] cases s with | positive => rw[<-hm];exact Model.toModel_canonicalizeModel _ | negative => have hf:(Model.toModel (A_M f)).isFinite=true:=by rw[hm];rfl have hn:=Model.toModel_neg_of_isFinite qAl (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 qC6 (f:Float) (hf:f.isFinite=true):f.abs.isFinite=true∧R f.abs = |R f|:=by have hA:A_M f.abs=Model.ofModel 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[<-qAb,hA];exact Model.isFinite_ofModel_zero _ qAl _ . rw[A_R f.abs,hA,qAp f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAl] | finite s m e h => rw[hu] at hA;have hk:=qEx f s m e h hu;change Model.toModel (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))=_ at hk;constructor;. rw[<-qAb,hA,qB1];change (Model.toModel (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))).isFinite=_;rw[hk];rfl . rw[A_R f.abs,hA,qAp f,hu];change Model.toReal (Model.ofModel FloatFormat.binary64 (.finite .positive m e h))=_;rw[Model.toReal_eq_unpackedToReal_toModel qAl,hk];simp only[Model.unpackedToReal_finite,qAx];have h0:(0:ℝ)<=(m:ℝ)*2^e:=by positivity cases s<;>simp[Model.modelSignBit,abs_of_nonneg h0] lemma qD2 (f:Float) (k:ℕ) (hk:k<2^53) (hf:f.toModel=Float.Model.pack (Float.Model.UnpackedFloat.ofNat Float.Model.Format.binary64 k)) : f.isFinite=true∧R f=k:=by have hA:A_M f=Model.roundDyadic 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 _ qAl (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:=qAq _<=A_B*2:=by linarith [qCn] _<=_:=qDh have hfin:Model.isFinite (A_M f)=true:=by rw[hA];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qAl;rw[hd,У (Э _)];exact hkR.trans qA7 refine ⟨by rw[<-qAb];exact hfin,?_⟩;rw[A_R,hA,Model.toReal_roundDyadic_eq_roundAt _ qAl _ (hA ▸ hfin),hd];have:=qAn k 0 (by simpa using hk) ב;simpa using this lemma qGw (m:UInt32) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat :=qD2 _ _ h rfl lemma qGo (m:UInt64) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat :=qD2 _ _ h rfl lemma qB3:(0:Float).toModel.unpack=.zero .positive:=by rfl lemma qIp:(0:Float).isFinite=true∧R 0=0:=by refine ⟨?_,?_⟩;. change (0:Float).toModel.unpack.isFinite=true;rw[qB3];rfl . rw[qAp,qB3];simp def A_bits (i:ℕ):UInt64 := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:UInt64)) lemma qE4 (i:ℕ):pwEntry i=Float.ofBits (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):Bool := match Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (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 qE6:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qIo (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:=qE6 i hi rw[qE4] have hU:(Float.ofBits (A_bits i)).toModel.unpack =Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (A_bits i).toBitVec :=Model.unpack_pack_unpack _ _ unfold A_chk at hc rw[qAp,hU] change (Float.ofBits (A_bits i)).toModel.unpack.isFinite=true∧_ rw[hU] cases hu:Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (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[Bool.and_eq_true,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,qAx,h3,h1,h2,mul_assoc,e] simp only[decide_eq_true_eq,h4,↓reduceIte] lemma qFv (i:USize) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,qDy] lemma qA3 (w:UInt32):(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[О];exact В w.toNat 31 lemma qBg (w:UInt32):(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[О];exact В w.toNat 23 lemma qF7 (w:UInt32):(w >>> (23:UInt32)).toNat=w.toNat/2^23:=by rw[л,Ю];rfl lemma qBf (w:UInt32):(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf;rw[О,qF7];exact В _ 8 lemma qDk (w:UInt32):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[UInt32.toNat_zero];omega lemma qHr (w:UInt32) : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,qBg,Nat.or_comm];have h:w.toNat%2^23<2^23:=Ъ _ (by positivity);have:=щ h 1;simpa using this.symm lemma qBl (w:UInt32) (h:good w=true) : w.toNat%2^31=0∨(2^23<=w.toNat%2^31∧w.toNat%2^31<0x7F800000):=by have hv:=qA3 w;have hlt:=(w &&& 0x7FFFFFFF).toNat_lt;unfold good at h;simp only[Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-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 qA6 (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qBt (w:UInt32) (hg:good w=true) : wv w=(if 2^31<=w.toNat then -1 else 1)*A_mag (w.toNat%2^31):=by have hN:=w.toNat_lt;have hgd:=qBl w hg;have hE:=qBf w;have hF:(fracOf w).toNat=w.toNat%2^23:=qBg 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,qDk];simp;split_ifs<;>ring lemma qFq (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 qCe (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 qA6 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 qCq (w:UInt32) (hg:good w=true):(dv w).isFinite=true∧R (dv w)=wv w:=by have hN:=w.toNat_lt;have hgd:=qBl w hg;have hM:=qHr w;have hm:=qGw ((w &&& 0x7FFFFF) ||| 0x800000) (by rw[hM];omega);have hI:(w >>> (23:UInt32)).toUSize.toNat=w.toNat/2^23:=by rw[Т,qF7] have hp:=qIo (w.toNat/2^23) א;unfold dv;rw[qFv,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:=qAq;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)) (П _) qCn.le).trans qDn obtain ⟨hf,he⟩:=qD6 _ _ hm.1 hp.1 hb;refine ⟨hf,?_⟩;rw[he,hm.2,hp.2,qBt 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:=qAn (-(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:=qAn (M:ℤ) k (by simpa using hMl) hk;simp only[hs,↓reduceIte,one_mul];push_cast at this;exact this def A_fsum (l:List Float):Float:=l.foldl (.+·) 0 noncomputable def A_sa (l:List Float):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List Float):ℝ:=(l.map R).sum lemma qAu (l:List Float):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 qBe (l:List Float) (q:Float):A_sa (l ++ [q])=A_sa l + |R q|:=by simp[A_sa] lemma qBq (l:List Float) (q:Float):A_sr (l ++ [q])=A_sr l+R q:=by simp[A_sr] lemma qAt (l:List Float) (q:Float):A_fsum (l ++ [q])=A_fsum l+q:=by simp[A_fsum] lemma qB7 (l:List Float) : |A_sr l| ≤ A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => simp only[A_sa,A_sr,List.map_cons,List.sum_cons] at ih ⊢;exact (Ч _ _).trans ד lemma qAv (l:List Float) (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 ⟨qIp.1,?_⟩ simp[qIp.2,A_sr] | append_singleton l q ih => rw[qBe] at hb have hP:=qAu 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:=qB7 l have hz:=qBh have hz1:=qCh have hB:=qCn have hBl:=qAq rw[qAt,qBe,qBq,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⟩:=qDo (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 qIn (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:UInt64).toNat=8:=rfl;simp[UInt64.toNat_add,h8];omega have h1:=qGo (n.toUInt64+8) (by rw[ht];omega);have hu:(Float.ofBits 0x3CC0000000000000).toModel.unpack =.finite .positive (2^52) (-103) (by decide):=by rfl have h2:(Float.ofBits 0x3CC0000000000000).isFinite=true:=by change (Float.ofBits 0x3CC0000000000000).toModel.unpack.isFinite=true;rw[hu];rfl have h3:R (Float.ofBits 0x3CC0000000000000)=1/2^51:=by rw[qAp,hu,Model.unpackedToReal_finite,qAx];simp;norm_num have h4:(n:ℝ)+8<=2^21:=by have:n+8<=2^21 :=א;exact_mod_cast this obtain ⟨hf,he⟩:=qD6 _ _ h1.1 h2 (by rw[h1.2,h3,ht];have h5:(1:ℝ)<=2^256*2^42 :=ב;have h6:=qAq;have h7:(2:ℝ)^21<=2^256*2^42 :=ב;refine ظ (г ?_ ?_ (П _) qCn.le) qDn;. 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:=qAn ((n:ℤ)+8) (-51) א ב;push_cast at this ⊢;have e:(2:ℝ)^(-51:ℤ)=1/2^51 :=ב;rw[e] at this;rw[this];ring lemma qHk (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 qDz (n:ℕ) (hn:n<2^20) (s t:Float) (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:=qCn;have hB8:(8:ℝ)<=A_B:=by have h:(8:ℝ)<=2^256*2^42 :=ב;linarith [qAq] have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hn0:(0:ℝ)<=n:=Э n;obtain ⟨hnf,hnv⟩:=qIn 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⟩:=qC8 _ _ 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:=qBh;have hz1:=qCh;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⟩:=qDo s _ hs hef hSB hE;obtain ⟨hmf,hmE⟩:=qHc 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:=qHk ((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 qHh (g h:ℕ->Float) (l:List ℕ) (a b:Float) : 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[List.foldl_cons,List.map_cons,ih] lemma qDb (l:List Float) (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:=(qC6 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 qD3 (ts:List ℕ) (q:ℕ->Float) (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:=qBh 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 qDg (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 qB8 (w:UInt32) (hg:good w=true) : |wv w| = A_mag (w.toNat%2^31):=by rw[qBt w hg,abs_mul,У (qA6 _)];split_ifs<;>simp lemma qA4 (w:UInt32) (hg:good w=true) : |wv w| ≤ 2^128:=by rw[qB8 w hg];apply qFq;rcases qBl w hg with h | h<;>omega lemma qBs (w:UInt32) (hg:good w=true) (h:w &&& 0x7FFFFFFF=0):wv w=0:=by rw[qBt w hg,<-qA3 w,h];simp[A_mag] lemma qBv:(2:ℝ)^128<=A_B:=by have h:(2:ℝ)^128<=2^256*2^42 :=ב;linarith [qAq] lemma qEw (a b:UInt32) (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⟩:=qCq a ha obtain ⟨fb,rb⟩:=qCq b hb have la:=qA4 a ha have lb:=qA4 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⟩:=qC8 _ _ fa fb (by rw[ra];exact la.trans qBv) (by rw[rb];exact lb.trans qBv) rw[ra,rb] at h2 exact ⟨h1,h2,hab⟩ lemma qDv (n:ℕ) (xw ww:ℕ->UInt32) (bw:UInt32) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (h0:∀ t,t< n->t∉l->wv (xw t)*wv (ww t)=0) : exactS n xw ww bw=(l.map λt=>wv (xw t)*wv (ww t)).sum+wv bw:=by unfold exactS;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 qCu (n:ℕ) (xw ww:ℕ->UInt32) (bw:UInt32) (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) : exactS n xw ww bw=(l.map λt=>wv (xw t)*wv (ww t)).sum+wv bw := qDv n xw ww bw l hl hl2 λt h1 h2=>by rcases hz t h1 h2 with h | h . simp[qBs (xw t) (hx t h1) h] . simp[qBs (ww t) (hw t h1) h] lemma qGj (n:ℕ) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n):l.length<=n:=by have h:l.toFinset ⊆ Finset.range 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 qDj (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g:ℕ->Float) (p:ℕ->ℝ) (b:Float) (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⟩:=qD3 ts g p (λt ht=>(hg t ht).2.1) have hAle:=qDg ts (λt=>|p t|) _ (λt ht=>(hg t ht).2.2) have hsa:=qBe (ts.map g) b have hsr:=qBq (ts.map g) b have hsag:=qAu (ts.map g) have hsa0:=qAu (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:=qBh have hz1:=qCh 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:=qAq 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⟩:=qAv _ hfq hlen1 ד obtain ⟨habs1,habs2⟩:=qDb _ hfq obtain ⟨hf2,he2⟩:=qAv ((ts.map g ++ [b]).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=List.mem_map.mp hq exact (qC6 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 qB7 _ obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨he2a,he2b⟩:=abs_le.mp he2 refine qDz 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 qBy (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by set g:ℕ->Float:=λ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⟩ := qHh g (λt=>(g t).abs) l 0 0 obtain ⟨hs1,hs2⟩:=hs rw[hfold] at hs1 hs2 have hlen:=qGj 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=>qEw _ _ (hx t (hl2 t ht)) (hw t (hl2 t ht)) obtain ⟨hbf,hbv⟩:=qCq bw hb have hS:s=A_fsum (l.map g ++ [dv bw]):=by rw[hs1,qAt];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,qAt] rfl rw[hS,hE] at hbig ⊢ rw[qCu n xw ww bw hx hw l hl hl2 hz] exact qDj n hn l hlen g p (dv bw) (wv bw) hgt hbf hbv (qA4 bw hb) hbig lemma qBp (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e) := qBy 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 qG0 (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 qFg (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 qDf (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g:ℕ->Float) (p:ℕ->ℝ) (b f a:Float) (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⟩:=qD3 ts g p (λt ht=>(hg t ht).2) have hsa:=qBe (ts.map g) b have hsr:=qBq (ts.map g) b have hsa0:=qAu (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:=qBh have hz1:=qCh 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:=qAq 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⟩:=qC8 f a hf hjf (by rw[hH,У hH0];linarith [qBv]) (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⟩:=qC6 b hbf rw[hbv] at hav obtain ⟨htf,hte⟩:=qDo (f*a) b.abs hgf haf hgb (by rw[hav,abs_abs];linarith [qBv]) rw[hav] at hte have key2:=qG0 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⟩:=qAv _ hfq hlen1 ד have hl1:(((ts.map g ++ [b]).length:ℕ):ℝ)=k+1:=by simp[k] rw[hl1] at he1 have hS1:=qB7 (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 qDz 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 qC1 (n:ℕ) (hn:n<2^20) (ts:List ℕ) (hlen:ts.length<=n) (g v:ℕ->Float) (p a:ℕ->ℝ) (b f:Float) (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:=qDg ts (λt=>|a t|) _ (λt ht=>(hv t ht).2.2) have hAJ:=qFg 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⟩:=qDb (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:=qAq have hlenv:((ts.map v).map Float.abs).length<=2^21:=by simp;omega obtain ⟨hjf,hje⟩:=qAv ((ts.map v).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=List.mem_map.mp hq exact (qC6 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 qDf 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 qGt (hh:UInt32) (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[qBt hh hg,Г hhN] simp only[show ¬ 2^31<=hh.toNat by omega,↓reduceIte,one_mul] refine ⟨by rw[hHeq];exact qA6 _,ظ (le_abs_self _) (qA4 hh hg),?_⟩ intro w hg1 hm have h1:=Ж.mp hm rw[qA3] at h1 rw[qB8 _ hg1,hHeq] apply qCe _ _ h1 rcases qBl _ hg1 with h | h . exact Or.inl h . exact Or.inr h.1 lemma qDl (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:Float) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by set g:ℕ->Float:=λt=>dv (xw t)*dv (ww t) with hgd set p:ℕ->ℝ:=λt=>wv (xw t)*wv (ww t) with hpd set v:ℕ->Float:=λt=>dv (ww t) with hvd obtain ⟨hs1,hs2⟩:=hs obtain ⟨hH0,hHl,hb1⟩:=qGt hh hg hp obtain ⟨hbf,hbv⟩:=qCq bw hb obtain ⟨hhf,hhv⟩:=qCq 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,-⟩:=qEw _ _ (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⟩:=qCq _ (hw t (hl2 t ht)) exact ⟨h1,h2,qA4 _ (hw t (hl2 t ht))⟩ have hS:s=A_fsum (l.map g ++ [dv bw]):=by rw[hs1,qAt] 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[qCu n xw ww bw hx hw l hl hl2 hz] exact qC1 n hn l (qGj n l hl hl2) g v p (λt=>wv (ww t)) (dv bw) (dv hh) (wv bw) (wv hh) hgt hvt hbf hbv (qA4 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 qD0 (m K:Nat) : (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 qBn (H a b:Nat) (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 qE7 (m K r:Nat) (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[qD0,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⟩:=qBn 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⟩:=qBn 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 qBu (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma qBi (m L:Nat) (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 qA9 (s:Sign) (m:Nat) (e T:Int) (K r:Nat) (hT:Format.binary32.targetExponent (totalExponent m e)=T) (hK:T=e+(K+1:Nat)) (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,qE7 m K r h1 h2] lemma qGs (r:Nat) (T:Int) (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:=qBi r 23 h1 h2 unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,qBu] have h3:max (((23:Nat):Int)+1+T-24) (-149)=T :=א simp only[h3,sub_self,Int.toNat_zero,Int.natCast_zero,Int.add_zero] rfl lemma qFr (T:Int) (hT:-150<=T) : shiftToTargetExponent Format.binary32 (2^24) T .exact =(ExtendedMantissa.ofMantissaAndAccuracy (2^23) .exact,T+1):=by have hl:(2^24:Nat).log2=24:=qBi _ 24 ב ב unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,qBu] have h3:max (((24:Nat):Int)+1+T-24) (-149)-T=1 :=א rw[h3] rfl lemma qCt (s:Sign) (P:Nat) (E:Int) (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[qGs P E hP1 hP2 hE,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false] lemma qD1 (s:Sign) (E:Int) (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:Nat)≠0 :=א simp only[qFr (E-1) א,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false,sub_add_cancel] lemma qDc (s:Sign) (m:Nat) (e:Int) (P:Nat) (E:Int) (g:Nat) (hp:0< P) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hg:e+(g+2:Nat)=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 qBi 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,qBu];omega rw[qA9 s m e E (g+1) P hT א ?_ ?_];. exact qCt 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 qBi 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,qBu];omega rw[qA9 s m e (E-1) g (2^24) hT א א א];exact qD1 s E hE' hp . have hE2:E=-149 :=א;have hT:Format.binary32.targetExponent (totalExponent m e)=E:=by simp only[totalExponent,hL,qBu];omega rw[qA9 s m e E (g+1) (2^23) hT א (by rw[e2];omega) (by rw[e2];omega)];exact qCt s _ E ב ב hE hp lemma qA5 (neg:Bool) (m:Nat) (e:Int) : Model.roundDyadic Fmt ⟨neg,m,e⟩ =Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e):=by simp[Model.roundDyadic,Model.ieeeRoundDyadic] rfl lemma qEg (s:Sign):s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl lemma qAy (s:Sign) (M:Nat) (E:Int) (h:0< M) (hM1:2^23<=M) (hM2:M<2^24) (hb1:1<=E+150) (hb2:E+150<=254) : (UnpackedFloat.pack Format.binary32 (.finite s M E h)).toNat =(if s=.negative then 2^31 else 0)+(E+150).toNat*2^23+M%2^23:=by have hl:M.log2=23:=qBi 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,qEg,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 qFb (neg:Bool) (m:Nat) (e:Int) (P:Nat) (E:Int) (hm:m≠0) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hE2:E+150<=254) (hhi:2*((m:ℝ)*2^e)<(2*P+1)*2^E) (hlo1:P≠2^23->(2*P-1)*2^E<2*((m:ℝ)*2^e)) (hlo2:P=2^23->33554431*2^E<4*((m:ℝ)*2^e)) : toWord (Model.roundDyadic 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:Nat,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,hg⟩:∃ g:Nat,(e-k)+(g+2:Nat)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have hb:(0:ℝ)<2^(e-k):=ю ב _ have hV:(m:ℝ)*2^e=((m*2^k:Nat):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-Ц,<-М С] congr 2;ring have hEE:(2:ℝ)^E=((2^(g+2):Nat):ℝ)*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:Nat):ℝ)<(2*P+1)*((2^(g+2):Nat):ℝ) :=я ד 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):Nat):ℝ) < 2*((m*2^k:Nat):ℝ)+((2^(g+2):Nat):ℝ) :=я ד 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):Nat):ℝ)<4*((m*2^k:Nat):ℝ) :=я ד hb.le exact_mod_cast this rw[qA5,<-ExactRuntime.round_scale _ _ m e k hm,qDc _ (m*2^k) (e-k) P E g hp hP1 hP2 hE hg h1 h2 h3] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qAy _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qId (f:Float) : 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 qHv (f:Float):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 qH5 (f:Float):Model.signBit (toModel (ofFloat f))=decide (2^63<=f.toBits.toNat):=by rw[Model.signBit_eq_msb];show f.toBits.toBitVec.msb=_;rw[BitVec.msb_eq_decide];rfl lemma qIf (f:Float) (s:Bool) (E F:Nat) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : Model.toDyadic? (toModel (ofFloat f))=some ⟨s,2^52+F,(E:Int)-1075⟩:=by have he:=qId f;have hf:=qHv f;have hs:=qH5 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[Bool.false_eq_true,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:Model.isFinite (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 qEj (f:Float) (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 (Model.toModel (toModel (ofFloat f))).isFinite=true;revert h;generalize Model.toModel (toModel (ofFloat f))=u;cases u<;>simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.isFinite] lemma B_R (f:Float) (s:Bool) (E F:Nat) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : f.isFinite=true ∧ R f=(if s then -1 else 1)*((2^52+F:Nat):ℝ)*2^((E:Int)-1075):=by have hd:=qIf f s E F hE1 hE hF h refine ⟨qEj f _ hd,?_⟩ rw[R,Model.toReal_eq,hd] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def B_near (A:Nat) (S:Bool) (ex fr:Nat):Prop := ∃ M,A=(if S then 2^63 else 0)+M∧(ex*2^23+fr)*2^29< M+2^28 ∧ M<=(ex*2^23+fr)*2^29+2^28 lemma B_x (A x:Nat) (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,Bool.false_eq_true,ite_false];exact ⟨A,by omega,by omega,by omega⟩ lemma qC0 (A:UInt64) : ((A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)).toNat=(A.toNat+268435455)%2^64/2^29:=by rw[м,Ы];norm_num [Nat.shiftRight_eq_div_pow] lemma qEs (x:UInt64) : ((x >>> (23:UInt64)) &&& (0x7FF:UInt64)).toNat=x.toNat/2^23%2^11:=by rw[э,м];have:=В (x.toNat >>> 23) 11;norm_num [Nat.shiftRight_eq_div_pow] at this ⊢;exact this lemma qG3 (v:UInt64) (h:v-(897:UInt64)<(254:UInt64)) : 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 qH3 (x:UInt64) (hx:x.toNat<2^35) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151) : (((x >>> (34:UInt64)) <<< (31:UInt64)) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64)))).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:UInt64)) <<< (31:UInt64)).toNat=(x.toNat/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,м] norm_num [Nat.shiftRight_eq_div_pow,Nat.shiftLeft_eq] omega have hb:((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64))).toNat =x.toNat%2^34-896*2^23:=by have h2:(x &&& (0x3FFFFFFFF:UInt64)).toNat=x.toNat%2^34:=by rw[э] exact В _ 34 have h3:((896:UInt64) <<< (23:UInt64))=7516192768:=by decide rw[h3,UInt64.toNat_sub_of_le] . 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,Bool.false_eq_true,ite_false];omega lemma qAs (s e:Float) (h:ziv s e≠0xFFFFFFFF) : ∃ (S:Bool) (ex fr:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near (s+e).toBits.toNat S ex fr∧B_near (s-e).toBits.toNat S ex fr:=by unfold ziv at h ⊢ dsimp only at h ⊢ generalize (s+e).toBits=A at h ⊢ generalize (s-e).toBits=B at h ⊢ have hAn:=qC0 A have hBn:=qC0 B generalize (A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=x at h hAn ⊢ generalize (B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=y at h hBn ⊢ have hex:=qEs x generalize (x >>> (23:UInt64)) &&& (0x7FF:UInt64)=ex at h hex ⊢ split at h . rename_i hc;have hc0:=hc;obtain ⟨hxy,hc⟩:=у.mp hc;have hxy:x=y:=by simpa using hxy obtain ⟨h1,h2⟩:=qG3 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,qH3 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 qAi (f:Float) (S:Bool) (ex fr:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hn:B_near f.toBits.toNat S ex fr) : f.isFinite=true∧∃ v:ℝ,R f=(if S then -v else v) ∧ v<=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< v) ∧ (2^53-2^28)*2^((ex:Int)-1076)< v:=by obtain ⟨M,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:Int)-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:Nat):ℝ)*2^((E:Int)-1075),?_,?_⟩ . rw[hR];cases S<;>simp;ring rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,М С,zpow_one] rw[e];generalize (2:ℝ)^((E:Int)-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:Nat):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e] at hb ⊢;generalize (2:ℝ)^((E:Int)-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 qBk (s e:Float) (h:ziv 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⟩:=qAs s e h;obtain ⟨f1,v,hv,-,-,h5⟩:=qAi _ S ex fr h1 h2 h3 ha;obtain ⟨f2,-⟩:=qAi _ S ex fr h1 h2 h3 hb;refine ⟨f1,f2,?_⟩;have hb0:(2:ℝ)^(-179:ℤ)<=2^((ex:Int)-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 qA8 (w:UInt32) (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 qAf (w:UInt32) (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:UInt32).toNat=2^8-1 from rfl,В] at this;simpa [Nat.shiftRight_eq_div_pow] using this have hv:(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[О,show (0x7FFFFFFF:UInt32).toNat=2^31-1 from rfl,В] simp only[good,Bool.or_eq_true,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:UInt32).toNat=0:=rfl;omega . rw[Ж,UInt32.toNat_sub,hv] at h;norm_num at h;omega lemma qFy (a b:UInt32) : ((sv a:ℚ)*sv b*(2:ℚ)^(idxOf a+idxOf b))*2^(-298:Int) =rationalWord a*rationalWord b:=by simp only[rationalWord,decodeD_toRat] rw[show (-298:Int)=-149+-149 by norm_num,М С] rw[zpow_sub₀ С,zpow_sub₀ С,Ц,Ц,pow_add] norm_num [zpow_neg,zpow_natCast] ring lemma qHe (a b:Array UInt32) (start n:Nat) : (dsum a b start n:ℚ)*2^(-298:Int) =∑ j∈Finset.range 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[Nat.add_zero] rw[qFy] have ht:(∑ j∈Finset.range n,rationalWord a[start+1+j]! * rationalWord b[start+1+j]!) = ∑ j∈Finset.range 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 qFh (a b:Array UInt32) (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:Int)≠0) : ∃ d:FloatLib.Numerics.Dyadic,d.toRat=(dsum a b 0 a.size:ℚ)*2^(-298:Int) ∧ dotSpec a b=toWord (Model.roundDyadic Fmt d):=by let L:=a.map (Model.ofUInt32 (fmt:=Fmt)) let K:=b.map (Model.ofUInt32 (fmt:=Fmt)) have hLK:L.size=K.size:=by simp[L,K,hs] have hLf:∀ j (hj:j< L.size),Model.isFinite 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),Model.isFinite 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:Int):=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 qC7 (n:Nat) (fa fb:Nat->UInt32) (hf:∀ j,j< n->expOf (fa j)≠255∧expOf (fb j)≠255) (hn:(∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j))≠0) : ∃ d:FloatLib.Numerics.Dyadic, d.toRat=(∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j)) ∧ dotOf n fa fb=toWord (Model.roundDyadic Fmt d):=by have hsum:(dsum (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) 0 n:ℚ) *2^(-298:Int)=∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j):=by rw[qHe] apply ы rfl intro j hj have hj':=Finset.mem_range.mp hj rw[К,qAe n fa j hj',qAe n fb j hj'] have hs:(Array.ofFn (n:=n) λi=>fa i).size=n:=by simp obtain ⟨d,hd,hw⟩:=qFh (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) ג (by intro j hj have hj':j< n:=by simpa using hj rw[qAe n fa j hj',qAe n fb j hj'] exact hf j hj') (by rw[hs,hsum];exact hn) rw[hs,hsum] at hd exact ⟨d,hd,hw⟩ lemma qEi: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 qGe (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) : ((∑ j∈Finset.range (n+1),rationalWord (if j< n then xw j else oneW) * rationalWord (if j< n then ww j else bw):ℚ):ℝ)=exactS n xw ww bw:=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qEi,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qA8 bw (qAf bw hb)] congr 1 apply ы rfl intro t ht have ht':=Finset.mem_range.mp ht simp only[ht',ite_true] rw[qA8 _ (qAf _ (hx t ht')),qA8 _ (qAf _ (hw t ht')),Rat.cast_mul] lemma qCr (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:Float) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠0xFFFFFFFF) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=qAs s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qAi _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qAi _ S ex fr h1 h2 h3 hb' have hsum:=qGe n xw ww bw hx hw hb generalize exactS n xw ww bw=V at hlo hhi hsum have hb0:(0:ℝ)<2^((ex:Int)-1076):=ю ב _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-Ц,<-М С] congr 1;push_cast;ring 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:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by cases S . simp only[Bool.false_eq_true,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∈Finset.range (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[Bool.false_eq_true,ite_false] at hVW;linarith . simp only[ite_true] at hVW;linarith obtain ⟨⟨neg,m,q⟩,hd,hw'⟩:=qC7 (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 ⟨qAf _ (hx j hj),qAf _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨hone,qAf _ 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[Bool.false_eq_true,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:=qFb neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hz,hw',hword,e1,e2] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary lemma qDr (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (h:ziv s e≠0xFFFFFFFF) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBk s e h obtain ⟨hlo,hhi⟩:=qBy n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCr n hn xw ww bw hx hw hb s e hlo hhi h lemma qDe (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=0∨ww t &&& 0x7FFFFFFF=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:Float) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (h:ziv s e≠0xFFFFFFFF) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBk s e h obtain ⟨hlo,hhi⟩:=qDl n hn xw ww bw hh hx hw hb hg hm hp l hl hl2 hz s e hs hbig exact qCr 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 qAd (k:Nat) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma qFi (a b:USize) (h:a.toNat*b.toNat<2^32) : (a*b).toNat=a.toNat*b.toNat:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_mul];exact Г א lemma qCo {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size :=qAd _ h lemma qA0 (a:Array UInt32) (i:USize) (hs:a.size<2^32):rdW a i=a[i.toNat]!:=by unfold rdW;split;. rename_i h;have h2:=qAm a i h;rw[Е a i.toNat h2];rfl . rename_i h;have h':¬ i.toNat< a.size:=by intro h2;apply h;rw[А,qCo a hs];exact h2 rw[б a i.toNat h'];rfl lemma qA2 (a:Array UInt32) (i:USize) (v:UInt32):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma qBc (a:Array UInt32) (i:USize) (v:UInt32) (q:Nat) (hs:a.size<2^32) (hi:i.toNat< a.size):(wrW a i v)[q]! = if q=i.toNat then v else a[q]!:=by have h:i< a.usize:=by rw[А,qCo a hs];exact hi unfold wrW;rw[Р h];by_cases hq:q< a.size . have h3:q<(a.uset i v (qAm 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 (qAm a i h)).size:=by simpa using hq rw[б _ q h3,б a q hq,Д א] def C_Inv (N:Nat) (sp:Nat->Word) (S:Nat->Prop) (out:Array Word):Prop := out.size=N∧∀ q,q< N->S q->out[q]! = sp q lemma qF0 {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp S out) {u:USize} {p:Nat} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q=p∨S q) (wrW out u v):=by obtain ⟨h1,h2⟩:=h;refine ⟨by rw[qA2,h1],λq hq hS=>?_⟩;rw[qBc _ _ _ _ א א,hu];by_cases e:q=p;. rw[Л e,e,hv] . rw[Д e];exact h2 q hq (hS.resolve_left e) lemma qEn {N:Nat} {sp:Nat->Word} {S S':Nat->Prop} {out:Array Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S' q->S q):C_Inv N sp S' out :=⟨h.1,λq hq h'=>h.2 q hq (hS q hq h')⟩ lemma qHg {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S q) : out=Array.ofFn (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h;apply ъ;. 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 qE9 (v:UInt32) (h1:(0x7FFFFF:UInt32)<=v-1) (h2:v<0x7F800000) : (v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32))=true:=by have a1:(0x7FFFFF:UInt32).toNat=0x7FFFFF:=rfl;have a2:(0x7F800000:UInt32).toNat=0x7F800000:=rfl;have a3:(0x00800000:UInt32).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:UInt32).toNat=0x7EFFFFFF:=rfl;have a5:(1:UInt32).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:UInt32)<=v:=by rw[Ж];have:v.toNat≠0:=λh=>h0 (И.mp h);omega rw[Ж,س _ _ h3] at h1;rw[п] at h2;have h4:(0x00800000:UInt32)<=v:=by rw[Ж];omega simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[Ж,س _ _ h4];omega lemma qIe (c u lo:UInt32) (h:c<=if u< lo then u else lo):c<=u∧c<=lo:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qHz (c v hi:UInt32) (h:(if hi< v then v else hi)< c):v< c∧hi< c:=by simp only[UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qB2 (w:UInt32) (h:good w=true):good (w &&& 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 qFa {h:UInt32} (h1:h<0x7F800000):h<=0x7FFFFFFF:=by have a1:(0x7F800000:UInt32).toNat=0x7F800000:=rfl;have a2:(0x7FFFFFFF:UInt32).toNat=0x7FFFFFFF:=rfl;rw[п] at h1;rw[Ж];omega def C_Hv (A:Array 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 qFs (r v hi:UInt32) (h:(if hi< v then v else hi)<=r):v<=r∧hi<=r:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qG2 (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi≠0xFFFFFFFF → (0x7FFFFF:UInt32)<=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⟩:=qIe _ _ _ h1 obtain ⟨h9,h10⟩:=qHz _ _ _ h2 obtain ⟨h11,h12⟩:=qFs _ _ _ h3 have hg:good (A.uget i hb)=true:=qE9 _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qB2 _ hg . exact g0 . by_cases e2:k=i.toNat;. subst e2;rw[Е A i.toNat hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qA1 (A:Array UInt32) (hs:A.size<2^32) (h:scan A A.usize 0 (qAk A) 0xFFFFFFFF 0≠0xFFFFFFFF) : C_Hv A (scan A A.usize 0 (qAk A) 0xFFFFFFFF 0):=by have e:=qCo A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qG2 A A.usize (qAk 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 (qAk A) 0xFFFFFFFF 0 :=λk hk=>h6 k א א;exact ⟨h5 rfl,qFa h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qFd (w:UInt32):w &&& 0x7FFFFFFF<=0x7FFFFFFF:=by rw[Ж,О];exact Nat.and_le_right def C_hok (w:Nat->Word) (t:Nat) (h:Word):Prop := good h=true∧(∀ k,k< t->w k &&& 0x7FFFFFFF<=h)∧h<=0x7FFFFFFF lemma qEb {w:Nat->Word} {t:Nat} {h:Word} (hg:good (w t)=true) (H:C_hok w t h):C_hok w (t+1) (mx h (w t)):=by obtain ⟨h1,h2,hp⟩:=H;have hm:=qB2 _ hg;unfold mx;dsimp only;by_cases e:w t &&& 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=>?_,qFd _⟩;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 qEr {w:Nat->Word} {t:Nat} {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 qEd {rows inW i t:Nat} (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 qGa {rows inW i t:Nat} (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 qCp (x:FloatArray) (i:USize) (h:i.toNat< x.size) : x.uget i h=x.data[i.toNat]!:=by obtain ⟨ds⟩:=x;have h':i.toNat< ds.size:=h;rw[Е ds i.toNat h'];rfl lemma qFo (a:Array UInt32) (i:USize) (h:i.toNat< a.size) : a.uget i h=a[i.toNat]!:=by rw[Е a i.toNat h];rfl lemma qAr (a:FloatArray) (i:USize) (v:Float):(wrF a i v).size=a.size:=by unfold wrF;split;. obtain ⟨ds⟩:=a;exact Array.size_uset _ . rfl lemma qAw (a:FloatArray) (i:USize) (v:Float) (q:Nat) (hs:a.size<2^32) (hi:i.toNat< a.size) : (wrF a i v).data[q]! = if q=i.toNat then v else a.data[q]!:=by have h:i< a.usize:=by rw[А,FloatArray.usize];rwa [qAd _ hs] unfold wrF;rw[Р h];obtain ⟨ds⟩:=a;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:Nat->Word) (t:Nat):List Nat := (List.range t).filter (λp=>xw p &&& 0x7FFFFFFF != 0) lemma qCi (xw:Nat->Word) (t:Nat):(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& 0x7FFFFFFF != 0) (List.range t);simpa [C_keep] using h lemma qDs (xw:Nat->Word) (t:Nat) (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 qEo (xw:Nat->Word) (t:Nat) (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:Nat->Word) (sz t:Nat) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh:Word):Prop := c.toNat=(C_keep xw t).length∧ix.size=sz∧buf.size=sz ∧ (∀ k p:Nat,(C_keep xw t)[k]?=some p → (ix[k]! : UInt32).toNat=p∧buf.data[k]! = dv (xw p))∧C_hok xw t hh lemma qHp {X:Array UInt32} {o n:USize} (xw:Nat->Word) (hn:n.toNat<2^32) (hxw:∀ t:USize,t< n->rdW X (o+t)=xw t.toNat) (hg:∀ t,t< n.toNat->good (xw t)=true) (d:Nat) : ∀ (t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh: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,qDs xw _ hz];exact ⟨h1,h2,h3,h4,qEr hz h5⟩ . rw[Д (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qCi xw t.toNat;have hc:c.toNat< t.toNat+1 :=א;rw[hu,C_Dec,qEo xw _ hz];have hcn:c.toNat< n.toNat :=א;refine ⟨?_,by rw[qA2];exact h2,by rw[qAr];exact h3,λk p hk2=>?_,qEb (hg _ א) h5⟩;. rw[qAh _ _ (by rw[Щ];omega),Щ,h1];simp . rw[qBc _ _ _ _ א א,qAw _ _ _ _ א א];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 qBr (a:FloatArray) (v:Float):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qD4 (n:USize) (d:Nat):∀ (k:USize) (a:FloatArray),k.toNat+d=n.toNat → (pushZ n k a).size=a.size+d:=by induction d with | zero => intro k a he;rw[pushZ,Х (by rw[А];omega)];rfl | succ d ih => intro k a he;have ht:k< n:=by rw[А];omega rw[pushZ,Р ht,ih (k+1) _ (by rw[usz k n ht];omega),qBr];omega lemma qAo {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:USize} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q< p+1) (wrW out u v) := qEn (qF0 hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qH2 (xs:FloatArray) (v:Float) (q:Nat) : (xs.push v).data[q]! = if q=xs.size then v else xs.data[q]!:=by obtain ⟨ds⟩:=xs;show (ds.push v)[q]! = if q=ds.size then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.push v).size:=by simp;omega rw[Е _ q h3,Е ds q hq,Д א,ط] . 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 D_Sc where (lo hi:UInt32) def D_scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):D_Sc := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& 0x7FFFFFFF let u:=v-1 D_scan 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 D_fill (v:Float) (n k:USize) (a:FloatArray):FloatArray := if h:k< n then D_fill 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 D_copy (X:Array UInt32) (o n t:USize) (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (a:FloatArray):FloatArray := if h:t< n then D_copy 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 D_img (s:ConvShape) (X:Array UInt32) (hs:X.size<2^32) (z:Float) (R r:Nat) (a:FloatArray):FloatArray := 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 D_img s X hs z R (r+1) (D_fill z pu 0 (D_copy X o s.w.toUSize 0 hs hg.2.2 (D_fill z pu 0 a))) else D_img s X hs z R (r+1) (D_fill z (s.w+2*s.pad).toUSize 0 a) else a termination_by R-r def D_off (s:ConvShape) (t:Nat):Nat := (t/(s.kh*s.kw)*(s.h+2*s.pad)+t/s.kw%s.kh)*(s.w+2*s.pad)+t%s.kw structure D_C where (dw relu f4:Bool) (s:ConvShape) (X W b:Array UInt32) (k C ho wo wp:Nat) (step:USize) (hi:UInt32) (xd:FloatArray) (nf:Float) (tab:Array UInt32) structure D_P where (g:D_C) (wd:FloatArray) (off:Array UInt32) (e l1 l4:USize) (e0 bv ze:Float) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:g.xd.size<2^32) (h1:∀ b:USize,b< l1->∀ j (h:j< off.size),b.toNat+off[j].toNat+0< g.xd.size) (h4:∀ b:USize,b< l4->∀ j (h:j< off.size),b.toNat+off[j].toNat+3< g.xd.size) (hz:ze=0) lemma qG6 {n d:Nat} (b:USize) (u:UInt32) (hs:n<2^32) (h:b.toNat+u.toNat+d< n):(b+u.toUSize).toNat+d< n:=by rw[qAh _ _ (by rw[Т];omega),Т];exact h lemma qFt {n:Nat} (p:USize) (k:Nat) (hs:n<2^32) (h:p.toNat+k< n) : (p+USize.ofNat k).toNat< n:=by rw[qAh _ _ (by rw[uk k א];omega),uk k א];exact h def D_go4R (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (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) (a0 a1 a2 a3:Float) (r:FloatArray):FloatArray := if h:i< e then have ht:=А.mp h let p:=b+(off.uget i א).toUSize have hp:p.toNat+3< xd.size:=qG6 _ _ hs (hb i.toNat א) let v:=wd.uget i א D_go4R xd wd off b e (i+1) he hw hs hb (a0+xd.uget p א*v) (a1+xd.uget (p+USize.ofNat 1) (qFt p 1 hs א)*v) (a2+xd.uget (p+USize.ofNat 2) (qFt p 2 hs א)*v) (a3+xd.uget (p+USize.ofNat 3) (qFt p 3 hs א)*v) r else wrF (wrF (wrF (wrF r 0 a0) 1 a1) 2 a2) 3 a3 termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega def D_goT (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (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) (a t:Float):F1 := if h:i< e then have ht:=А.mp h let q:=xd.uget (b+(off.uget i א).toUSize) (qG6 (d:=0) _ _ hs (hb i.toNat א))*wd.uget i א D_goT xd wd off b e (i+1) he hw hs hb (a+q) (t+q.abs) else ⟨a,t⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega @[noinline] def D_slow (g:D_C) (o y x:Nat):UInt32 := act g.relu (dotFn (g.k+1) (λt=>if 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 D_finT (c:D_P) (o y x:Nat) (b:USize):UInt32 := if hb:b< c.l1 then let q:=D_goT 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:=ziv (q.s+c.bv) (c.g.nf*(q.t+c.bv.abs)) if r == 0xFFFFFFFF then D_slow c.g o y x else act c.g.relu r else D_slow c.g o y x @[inline] def D_fin (c:D_P) (o y x:Nat) (b:USize) (a:Float):UInt32 := let r:=ziv (a+c.bv) (c.g.nf*(c.e0+c.bv.abs)) if r == 0xFFFFFFFF then D_finT c o y x b else act c.g.relu r def D_cols (c:D_P) (o y x:Nat) (b q:USize) (rr:FloatArray) (out:Array UInt32) : Array UInt32 := if h4:x+4<=c.g.wo∧c.g.f4=true∧b< c.l4 then let r:=D_go4R 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:=D_fin c o y x b (rdF r 0) let o1:=D_fin c o y (x+1) (b+1) (rdF r 1) let o2:=D_fin c o y (x+2) (b+2) (rdF r 2) let o3:=D_fin 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 D_cols c o y (x+4) (b+4) (q+4) r out else if x< c.g.wo then D_cols c o y (x+1) (b+c.g.step) (q+1) rr (wrW out q (D_finT c o y x b)) else out termination_by c.g.wo-x def D_rows (c:D_P) (o y:Nat) (q:USize) (out:Array UInt32):Array UInt32 := if y< c.g.ho then D_rows c o (y+1) (q+c.g.wo.toUSize) (D_cols c o y 0 (y.toUSize*c.g.step*c.g.wp.toUSize) q (pushZ 4 0 (FloatArray.emptyWithCapacity 4)) out) else out termination_by c.g.ho-y structure D_T where (off:Array UInt32) (a:Float) def D_tap (g:D_C) (o:Nat) (ix:Array UInt32) (buf:FloatArray) (e i:USize) (off:Array UInt32) (a:Float):D_T := if h:i< e then D_tap g o ix buf e (i+1) (wrW off i ((if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0) + rdW g.tab (rdW ix i).toUSize)) (a+(rdF buf i).abs) else ⟨off,a⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega lemma qCz (a:Array UInt32) (j:Nat) (h:j< a.size) : a[j].toNat<=a.foldl (λm v=>max m v.toNat) 0:=by have key:∀ (l:List UInt32) (i:Nat),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[List.foldl_cons,List.mem_cons,forall_eq_or_imp];exact ⟨by omega,by omega,h2⟩ rw[<-Array.foldl_toList];exact (key a.toList 0).2 _ ג lemma qGd {n m:Nat} (d:Nat) (off:Array UInt32) (hs:n<2^32) (hm:∀ j (h:j< off.size),off[j].toNat<=m) (b:USize) (hb:b<(n-(m+d)).toUSize) (j:Nat) (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 D_outs (g:D_C) (hs:g.xd.size<2^32) (o:Nat) (q:USize) (ix off:Array UInt32) (buf:FloatArray) (out:Array UInt32):Array UInt32 := if o< g.C then let r:=decR g.W (o*g.k).toUSize g.k.toUSize 0 ix buf 0 0 let t:=D_tap 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:D_P:=⟨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, qGd 0 t.off hs (qCz t.off),qGd 3 t.off hs (qCz t.off),rfl⟩ D_outs g hs (o+1) (q+(g.ho*g.wo).toUSize) r.ix t.off r.buf (D_rows c o 0 q out) else out else out termination_by g.C-o def D_F (dw:Bool) (s:ConvShape) (relu:Bool) (X W b:Array UInt32) (k C:Nat) : Array UInt32 := let hp:=s.h+2*s.pad let wp:=s.w+2*s.pad let rx:=D_scan X X.usize 0 (qAk 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 (qAk W) 0xFFFFFFFF 0≠0xFFFFFFFF ∧ scan b b.usize 0 (qAk b) 0xFFFFFFFF 0≠0xFFFFFFFF then let tight:=(rx.hi >>> (23:UInt32))-((rx.lo+1) >>> (23:UInt32)) > 20 let R:=s.cin*hp let xd:=D_img s X hg.1 (dv 0) R 0 (FloatArray.emptyWithCapacity (R*wp)) if hs:xd.size<2^32 then let g:D_C:=⟨dw,relu,s.stride == 1 && !tight,s,X,W,b,k,C,s.ho,s.wo,wp, s.stride.toUSize,rx.hi,xd,(k.toUInt64+8).toFloat*Float.ofBits 0x3CC0000000000000, Array.ofFn (n:=k) λt=>(D_off s t.val).toUInt32⟩ D_outs g hs 0 0 (Array.replicate k 0) (Array.replicate k 0) (pushZ k.toUSize 0 (FloatArray.emptyWithCapacity k)) (Array.replicate (C*(s.ho*s.wo)) 0) else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b def convF (s:ConvShape) (relu:Bool) (X W b:Array UInt32):Array UInt32 := D_F false s relu X W b (s.cin*s.kh*s.kw) s.cout def depthF (s:ConvShape) (relu:Bool) (X W b:Array UInt32):Array UInt32 := D_F true s relu X W b (s.kh*s.kw) s.cin def D_px (s:ConvShape) (X:Array Word) (c yy xx:Nat):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 D_Fx (s:ConvShape) (X:Array Word) (q:Nat):Float := dv (D_px 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 D_Pre (F:Nat->Float) (m:Nat) (a:FloatArray):Prop := a.size=m∧∀ q,q< m->a.data[q]! = F q lemma qCf {F:Nat->Float} {m:Nat} {a:FloatArray} {v:Float} (h:D_Pre F m a) (hv:v=F m):D_Pre F (m+1) (a.push v):=by refine ⟨by rw[qBr,h.1],λq hq=>?_⟩;rw[qH2,h.1];by_cases e:q=m;. rw[Л e,e,hv] . rw[Д e];exact h.2 q א lemma qBm {F:Nat->Float} {v:Float} {n:USize} (d:Nat) : ∀ (k:USize) (m:Nat) (a:FloatArray),k.toNat+d=n.toNat->D_Pre F m a → (∀ i,m<=i->i< m+d->F i=v)->D_Pre F (m+d) (D_fill v n k a):=by induction d with | zero => intro k m a he H _ rw[D_fill,Х (by rw[А];omega)];exact H | succ d ih => intro k m a he H hv have ht:k< n:=by rw[А];omega rw[D_fill,Р ht,show m+(d+1)=m+1+d by omega] exact ih (k+1) (m+1) _ (by rw[usz k n ht];omega) (qCf H (hv m א א).symm) (λi h1 h2=>hv i א א) lemma qEp {F:Nat->Float} {X:Array UInt32} {o n:USize} (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (d:Nat) : ∀ (t:USize) (m:Nat) (a:FloatArray),t.toNat+d=n.toNat->D_Pre F m a → (∀ j,j< d->F (m+j)=dv X[o.toNat+t.toNat+j]!) → D_Pre F (m+d) (D_copy X o n t hs ho a):=by induction d with | zero => intro t m a he H _ rw[D_copy,Х (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[D_copy,Р 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) (qCf H ?_) (λj hj=>?_) . rw[qFo,qAh _ _ א,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 qBb {c:Nat} (a:Nat) {b:Nat} (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 qFm (s:ConvShape) (X:Array Word) (r:Nat) {i:Nat} (hi:i< s.w+2*s.pad) : D_Fx s X (r*(s.w+2*s.pad)+i) =dv (D_px s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i):=by unfold D_Fx;rw[(qBb r hi).1,(qBb r hi).2] lemma qE0 (s:ConvShape) (X:Array UInt32) (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:Nat} (hR:R=s.cin*(s.h+2*s.pad)) (d:Nat) : ∀ (r:Nat) (a:FloatArray),r+d=R → D_Pre (D_Fx s X) (r*(s.w+2*s.pad)) a → D_Pre (D_Fx s X) (R*(s.w+2*s.pad)) (D_img s X hs (dv 0) R r a):=by induction d with | zero => intro r a he H rw[D_img,Х א] 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:USize).toNat=0:=rfl have ep:s.pad.toUSize.toNat=s.pad:=qAd _ א have ew:s.w.toUSize.toNat=s.w:=qAd _ א have eW:(s.w+2*s.pad).toUSize.toNat=s.w+2*s.pad:=qAd _ hw have hF:∀ i,i< s.w+2*s.pad->D_Fx s X (r*(s.w+2*s.pad)+i) =dv (D_px s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i) :=λi hi=>qFm 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) → D_Fx s X (r*(s.w+2*s.pad)+i)=dv 0:=by intro i hi hc;rw[hF i hi,D_px,Л 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 qGa (qEd 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[D_img,Р 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:=qBm (F:=D_Fx 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:=qEp (F:=D_Fx s X) hs g3 s.w 0 _ _ (by rw[e0,ew,К]) P1 (λj hj=>by rw[з m,hF _ א,D_px,Д א,e0,Я,qAd _ א,Nat.add_sub_cancel_left]) exact qBm (F:=D_Fx 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[qAd _ א,ew];exact h1 rw[ф,ц] exact qBm (F:=D_Fx 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 D_t0 (g:D_C) (o:Nat):Nat:=if g.dw then o*g.k else 0 def D_xw (g:D_C) (o y x t:Nat):Word:=windowAt g.s g.X y x (D_t0 g o+t) def D_out (g:D_C) (o y x:Nat):Word := act g.relu (dotOf (g.k+1) (λt=>if t< g.k then D_xw g o y x t else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) lemma qCx (g:D_C) (o y x:Nat):D_slow g o y x=D_out g o y x:=by unfold D_slow D_out D_xw D_t0;rw[qAc] def D_B (g:D_C) (y x:Nat):Nat:=y*g.s.stride*g.wp+x*g.s.stride structure D_Ok (g:D_C):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:D_Pre (D_Fx 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:Nat,good g.W[q]! = true Hb:∀ q:Nat,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=Array.ofFn (n:=g.k) λt=>(D_off g.s t.val).toUInt32 lemma qEu {A:Array 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 qGf {A:Array Word} (H:∀ k:Nat,k< A.size->good A[k]! = true) (q:Nat) : good A[q]! = true:=by by_cases h:q< A.size;. exact H q h . rw[б A q h];rfl lemma qCm {s:ConvShape} {X:Array Word} {hi:Word} (H:C_Hv X hi) (c yy xx:Nat) : good (D_px s X c yy xx)=true∧D_px s X c yy xx &&& 0x7FFFFFFF<=hi:=by unfold D_px;split;. exact qEu (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 qEu (A:=X) lemma qHt {g:D_C} (ok:D_Ok g) {o t:Nat} (hC:o< g.C) (ht:t< g.k) : D_t0 g o+t< g.s.cin*(g.s.kh*g.s.kw):=by have h1:=ok.hk;have h2:=ok.hC;unfold D_t0;cases hd:g.dw;. simp only[hd,Bool.false_eq_true,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 qEd hC ht lemma qGg {a b st n:Nat} (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 qCd {g:D_C} (ok:D_Ok g) {o y x t:Nat} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo) (ht:t< g.k) : D_B g y x+D_off g.s (D_t0 g o+t)< g.xd.size ∧ g.xd.data[D_B g y x+D_off g.s (D_t0 g o+t)]! = dv (D_xw g o y x t):=by have hb:=qHt 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:=qGg ok.p0 ok.kh (by have:=hy;rw[ok.ho] at this;exact this) have hX:=qGg ok.p0 ok.kw (by have:=hx;rw[ok.wo] at this;exact this) unfold D_xw generalize D_t0 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:D_B g y x+D_off 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 D_B D_off;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:=qEd (qEd hc hyy) hxx refine ⟨by rw[ok.xd.1];exact hq,?_⟩ rw[ok.xd.2 _ hq,D_Fx,(qBb _ hxx).1,(qBb _ hxx).2,(qBb _ hyy).1,(qBb _ hyy).2] rfl lemma qFn {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (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:Nat->Float) (L:List Nat):∀ (i:USize) (a t:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat]! = xf p ∧ wd.data[i.toNat+j]! = wf p) → D_goT 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[D_goT,Х (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]!:=qFo _ _ _ 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[qCp,qAh _ _ (by rw[Т];omega),Т,e1,h1] have e3:wd.uget i א=wf p:=by rw[qCp,h2] rw[D_goT,Р ht] dsimp only rw[e2,e3,ih (i+1) _ _ (by rw[usz i e ht];omega),List.foldl_cons] 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 qGb (a:FloatArray) (i:USize) (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,qAd _ hs];exact hi unfold rdF;rw[Р h,qCp] def D_R4 (R:FloatArray) (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 qH9 (r:FloatArray) (hr:r.size=4) (a b c d:Float) : D_R4 (wrF (wrF (wrF (wrF r 0 a) 1 b) 2 c) 3 d) ⟨a,b,c,d⟩:=by have u0:(0:USize).toNat=0:=rfl;have u1:(1:USize).toNat=1:=Щ;have u2:(2:USize).toNat=2:=uk 2 ב;have u3:(3:USize).toNat=3:=uk 3 ב;have s1:(wrF r 0 a).size=4:=by rw[qAr,hr] have s2:(wrF (wrF r 0 a) 1 b).size=4:=by rw[qAr,s1] have s3:(wrF (wrF (wrF r 0 a) 1 b) 2 c).size=4:=by rw[qAr,s2] have s4:(wrF (wrF (wrF (wrF r 0 a) 1 b) 2 c) 3 d).size=4:=by rw[qAr,s3] have g1:=λq=>qAw r 0 a q (by rw[hr];norm_num) (by rw[hr,u0];norm_num);have g2:=λq=>qAw _ 1 b q (by rw[s1];norm_num) (by rw[s1,u1];norm_num);have g3:=λq=>qAw _ 2 c q (by rw[s2];norm_num) (by rw[s2,u2];norm_num);have g4:=λq=>qAw _ 3 d q (by rw[s3];norm_num) (by rw[s3,u3];norm_num);have rd:=λ(i:USize) (hi:i.toNat<4) =>qGb _ 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 qFu {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (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:FloatArray) (hr:r.size=4) (xf:Nat->Nat->Float) (wf:Nat->Float) (L:List Nat):∀ (i:USize) (a0 a1 a2 a3:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->wd.data[i.toNat+j]! = wf p∧∀ d,d<4 → xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat+d]! = xf d p) → D_R4 (D_go4R 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[D_go4R,Х (by rw[А];simp at h0;omega)] exact qH9 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]!:=qFo _ _ _ 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]! : UInt32).toNat:=by rw[qAh _ _ (by rw[Т];omega),Т,e1] rw[e1] at hbd have e2:∀ d,d<4->∀ pf,xd.uget ((b+(off.uget i hit).toUSize)+USize.ofNat d) pf=xf d p:=by intro d hd pf rw[qCp,qAh _ _ (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[qCp,ep];exact h2 0 א have e3:wd.uget i א=wf p:=by rw[qCp,h1] rw[D_go4R,Р 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 structure D_PO (c:D_P) (o:Nat) (L:List Nat):Prop where ok:D_Ok 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:Nat,L[j]?=some p → (c.off[j]! : UInt32).toNat=D_off c.g.s (D_t0 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 qGh {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {j p:Nat} (hj:L[j]?=some p):p< c.g.k:=h.lt p (List.mem_of_getElem? hj) lemma qCv {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=D_B c.g y x) : D_finT c o y x b=D_out c.g o y x:=by unfold D_finT;split . rename_i hl have e:=qFn c.he c.hw c.hs (c.h1 b hl) (λp=>dv (D_xw c.g o y x p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 (by rw[show (0:USize).toNat=0 from rfl,К];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj;rw[show (0:USize).toNat=0 from rfl,К,h1.1,hb] exact ⟨(qCd h.ok h.hC hy hx (qGh h hj)).2,h1.2⟩) dsimp only;rw[c.hz,e,h.bv,h.ok.nf] by_cases hz:ziv ((L.foldl (λ(acc:F1) p =>let q:=dv (D_xw 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 (D_xw 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 qCx _ _ _ _ . rw[Д (by simpa using hz)];unfold D_out;congr 1;exact qDr c.g.k h.ok.nk (D_xw c.g o y x) (λt=>c.g.W[o*c.g.k+t]!) c.g.b[o]! (λt _=>(qCm 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 qCx _ _ _ _ lemma qBo {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=D_B c.g y x) {a:Float} (ha:a=L.foldl (λacc t=>acc+dv (D_xw c.g o y x t)*dv c.g.W[o*c.g.k+t]!) 0) : D_fin c o y x b a=D_out c.g o y x:=by unfold D_fin;dsimp only;rw[h.bv,h.ok.nf,h.e0];by_cases hz:ziv (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 qCv h hy hx hb . rw[Д (by simpa using hz)];unfold D_out;congr 1;exact qDe c.g.k h.ok.nk (D_xw c.g o y x) (λt=>c.g.W[o*c.g.k+t]!) c.g.b[o]! c.g.hi (λt _=>(qCm h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) h.ok.Hx.1 (λt _=>(qCm 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 D_sp (g:D_C) (q:Nat):Word := D_out g (q/(g.ho*g.wo)) (q/g.wo%g.ho) (q%g.wo) lemma qG1 (g:D_C) {o y x:Nat} (hy:y< g.ho) (hx:x< g.wo) : D_out g o y x=D_sp g ((o*g.ho+y)*g.wo+x):=by unfold D_sp;rw[о g.ho g.wo,<-ذ,(qBb _ hx).1,(qBb _ hx).2,(qBb _ hy).1,(qBb _ hy).2] lemma qEe {g:D_C} (ok:D_Ok g) {o y x:Nat} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo):D_B g y x<2^32:=by have h:=(qCd ok hC hy hx ok.pk).1;have:=ok.xs;omega lemma qE2 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y Q:Nat} (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:Nat) : ∀ (x:Nat) (b q:USize) (rr:FloatArray) (out:Array Word),x+d=c.g.wo → (x< c.g.wo->b.toNat=D_B c.g y x)->q.toNat=Q+x->rr.size=4 → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+x) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+c.g.wo) (D_cols 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->D_out c.g o y x'=D_sp c.g (Q+x'):=by intro x' hx';rw[hQ];exact qG1 c.g hy hx' have l2:(2:USize).toNat=2:=uk 2 ב have l3:(3:USize).toNat=3:=uk 3 ב have l4:(4:USize).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,D_B c.g y (x+k)=D_B c.g y x+k:=by intro k;unfold D_B;rw[st1];simp only[Nat.mul_one];omega have bB:=λk (hk:x+k< c.g.wo)=>qEe h.ok h.hC hy hk;have b0:=bB 3 א;rw[hB] at b0;have a1:(b+1).toNat=D_B c.g y (x+1):=by rw[hB,qAh _ _ (by rw[Щ];omega),hb0,Щ] have a2:(b+2).toNat=D_B c.g y (x+2):=by rw[hB,qAh _ _ (by rw[l2];omega),hb0,l2] have a3:(b+3).toNat=D_B c.g y (x+3):=by rw[hB,qAh _ _ (by rw[l3];omega),hb0,l3] obtain ⟨R0,R1,R2,R3,R4⟩:=qFu c.he c.hw c.hs (c.h4 b h4.2.2) rr hrr (λk p=>dv (D_xw 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:USize).toNat=0 from rfl,К];exact h.len.symm) (λj p hj=>by have h1:=h.rd j p hj;rw[show (0:USize).toNat=0 from rfl,К,h1.1];refine ⟨h1.2,λk hk=>?_⟩;rw[show b.toNat+D_off c.g.s (D_t0 c.g o+p)+k =D_B c.g y (x+k)+D_off c.g.s (D_t0 c.g o+p) by rw[hB,hb0];omega] exact (qCd h.ok h.hC hy א (qGh h hj)).2) have o0:D_fin c o y x b (L.foldl (λacc p=>acc+dv (D_xw c.g o y (x+0) p) * dv c.g.W[o*c.g.k+p]!) 0)=D_out c.g o y x :=qBo h hy א hb0 rfl have o1:=qBo h hy (א:x+1< c.g.wo) a1 (a:=(L.foldl (λacc p=>acc+dv (D_xw c.g o y (x+1) p) * dv c.g.W[o*c.g.k+p]!) 0)) rfl have o2:=qBo h hy (א:x+2< c.g.wo) a2 (a:=(L.foldl (λacc p=>acc+dv (D_xw c.g o y (x+2) p) * dv c.g.W[o*c.g.k+p]!) 0)) rfl have o3:=qBo h hy (א:x+3< c.g.wo) a3 (a:=(L.foldl (λacc p=>acc+dv (D_xw c.g o y (x+3) p) * dv c.g.W[o*c.g.k+p]!) 0)) rfl rw[D_cols,Р 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:=qEe h.ok h.hC hy hx;rw[hB] at hbb ⊢;rw[qAh _ _ (by rw[l4];omega),hb0,l4] . rw[qAh _ _ (by rw[l4,hq];omega),hq,l4];omega . have q1:(q+1).toNat=Q+x+1:=by rw[qAh _ _ (by rw[Щ,hq];omega),hq,Щ] have q2:(q+2).toNat=Q+x+1+1:=by rw[qAh _ _ (by rw[l2,hq];omega),hq,l2] have q3:(q+3).toNat=Q+x+1+1+1:=by rw[qAh _ _ (by rw[l3,hq];omega),hq,l3] have s1:D_out c.g o y (x+1)=D_sp c.g (Q+x+1):=by rw[з];exact hsp _ א have s2:D_out c.g o y (x+2)=D_sp c.g (Q+x+1+1):=by rw[show Q+x+1+1=Q+(x+2) by omega];exact hsp _ א have s3:D_out c.g o y (x+3)=D_sp 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 qAo sN (qAo sN (qAo sN (qAo sN H hq א (hsp _ א)) q1 א s1) q2 א s2) q3 א s3 . by_cases h1:x< c.g.wo . have hb0:=hb h1;rw[D_cols,Х h4,Л h1];refine ih (d-1) א (x+1) _ _ _ _ א ?_ ?_ hrr ?_ . intro hx;have hbb:=qEe h.ok h.hC hy hx;have e:D_B c.g y (x+1)=D_B c.g y x+c.g.s.stride:=by unfold D_B;rw[ф,ц];omega rw[e] at hbb ⊢;rw[qAh _ _ (by rw[hb0,h.ok.st];exact hbb),hb0,h.ok.st] . rw[qAh _ _ (by rw[Щ,hq];omega),hq,Щ];omega . rw[qCv h hy h1 hb0];exact qAo sN H hq א (hsp _ h1) . rw[D_cols,Х h4,Д h1];have e:x=c.g.wo :=א;rw[e] at H;exact H lemma qE5 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) (hN:(o*c.g.ho+c.g.ho)*c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:Nat) : ∀ (y:Nat) (q:USize) (out:Array Word),y+d=c.g.ho → q.toNat=(o*c.g.ho+y)*c.g.wo → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+y)*c.g.wo) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+c.g.ho)*c.g.wo) (D_rows c o y q out):=by induction d with | zero => intro y q out he _ H rw[D_rows,Д א] 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:=qAd _ א have eY:y.toUSize.toNat=y:=qAd _ א have bB:=qEe 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:=qHt 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:D_B c.g y 0=y*c.g.s.stride*c.g.wp:=by unfold D_B;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[qFi _ _ (by rw[eY,h.ok.st];omega),eY,h.ok.st] have eP:c.g.wp.toUSize.toNat=c.g.wp:=qAd _ h.ok.wp2 have ebase:(y.toUSize*c.g.step*c.g.wp.toUSize).toNat=D_B c.g y 0:=by rw[qFi _ _ (by rw[eS,eP];exact bB),eS,eP,e1] rw[D_rows,Л hy] refine ih (y+1) _ _ א ?_ ?_ . rw[qAh _ _ (by rw[eW,hq];omega),hq,eW,hA2] . rw[hA2];refine qE2 h hy rfl א c.g.wo 0 _ _ _ _ (К _) (λ_=>ebase) (by rw[Я];exact hq) ?_ (by rw[Я];exact H);rw[qD4 4 4 0 _ (by rw[show (0:USize).toNat=0 from rfl,К];exact (uk 4 ב).symm)];rfl lemma qD9 {g:D_C} {c:D_P} {o:Nat} {L:List Nat} (hg:c.g=g) (h:D_PO c o L) (hN:(o*g.ho+g.ho)*g.wo<=g.C*(g.ho*g.wo)) {q:USize} {out:Array Word} (hq:q.toNat=(o*g.ho+0)*g.wo) (H:C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p<(o*g.ho+0)*g.wo) out) : C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p<(o*g.ho+g.ho)*g.wo) (D_rows c o 0 q out):=by subst hg exact qE5 h hN c.g.ho 0 q out (К _) hq H lemma qGk (s:ConvShape) (o:Nat) {p:Nat} (hp:p< s.kh*s.kw) : D_off s (o*(s.kh*s.kw)+p) =o*((s.h+2*s.pad)*(s.w+2*s.pad))+D_off 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 D_off rw[(qBb o hp).1,с hp,e1,e2,Nat.add_comm (o*s.kh),Nat.add_mul_mod_self_right] ring lemma qH0 (a b:Nat):a.toUInt32+b.toUInt32=(a+b).toUInt32 :=(UInt32.ofNat_add a b).symm lemma qG4 {g:D_C} (ok:D_Ok g) (o:Nat) {p:Nat} (hp:p< g.k) {u:UInt32} (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 =(D_off g.s (D_t0 g o+p)).toUInt32:=by have hz:g.tab.size=g.k:=by rw[ok.tb];simp have e:g.tab[p]! = (D_off g.s p).toUInt32:=by rw[ok.tb,Е _ p (by simpa using hp),Array.getElem_ofFn] rw[qA0 _ _ (by have:=ok.nk;omega),Т,hu,e] have h1:=ok.hk unfold D_t0 cases hd:g.dw . simp only[Bool.false_eq_true,if_false,UInt32.zero_add,Nat.zero_add] . simp only[hd,if_true] at h1 ⊢;rw[h1,qGk g.s o (by rw[<-h1];exact hp),ok.wp,qH0] def D_tv (g:D_C) (o:Nat) (L:List Nat) (j:Nat):Word := (D_off g.s (D_t0 g o+L[j]?.getD 0)).toUInt32 lemma qFk {g:D_C} (ok:D_Ok g) (o:Nat) {ix:Array UInt32} {buf:FloatArray} {e:USize} {L:List Nat} {N:Nat} (wf:Nat->Float) (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:Nat,L[j]?=some p->(ix[j]! : UInt32).toNat=p∧buf.data[j]! = wf p) (d:Nat) : ∀ (i:USize) (off:Array UInt32) (a:Float),i.toNat+d=e.toNat → C_Inv N (D_tv g o L) (λq=>q< i.toNat) off → C_Inv N (D_tv g o L) (λq=>q< e.toNat) (D_tap g o ix buf e i off a).off ∧ (D_tap 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[D_tap,Х (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 (D_off g.s (D_t0 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 qAo hN H rfl א (by rw[D_tv,hp];rfl)) rw[D_tap,Р ht,qA0 _ _ hix,qG4 ok o (hk _ (List.getElem_mem hl)) h1,qGb _ _ hbuf א,h2,List.drop_eq_getElem_cons hl,List.foldl_cons] rw[usz i e ht] at hi2 exact hi2 lemma qE3 {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) (d:Nat) : ∀ (o:Nat) (q:USize) (ix off:Array UInt32) (buf:FloatArray) (out:Array 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)) (D_sp g) (λp=>p< o*(g.ho*g.wo)) out → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< g.C*(g.ho*g.wo)) (D_outs g hs o q ix off buf out):=by induction d with | zero => intro o q ix off buf out he _ _ _ _ H rw[D_outs,Д א] 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 qGa hC (le_refl _) have eo:(o*g.k).toUSize.toNat=o*g.k:=qAd _ א have ek:g.k.toUSize.toNat=g.k:=qAd _ 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:=qAd _ א have eOo:o.toUSize.toNat=o:=by have:o<=o*(g.ho*g.wo):=й _ hpos exact qAd _ א have D:=qHp (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[qA0 _ _ sW2,qAh _ _ (by rw[eo];omega),eo]) (λt _=>ok.HW _) g.k 0 ix buf 0 0 (by rw[show (0:USize).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[D_outs,Л hC] dsimp only have hlen:=qCi (λp=>g.W[o*g.k+p]!) g.k have T:=qFk 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:USize).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[qAh _ _ (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 qD9 (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[D_tv,hj] at e1;have e2:=(qCd ok hC (y:=0) (x:=0) (by rw[ok.ho];exact ز _) (by rw[ok.wo];exact ز _) hpk).1;have e3:D_off g.s (D_t0 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:USize).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[qA0 _ _ ok.sb,eOo] lemma qEt (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi = if 0x007FFFFF<=(D_scan A n i hn lo hi).lo∧(D_scan A n i hn lo hi).hi<0x7F800000 then (D_scan 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,D_scan] simp only[dite_eq_right h] | succ d ih => intro i lo hi he have h:i< n:=by rw[А];omega rw[scan,D_scan] simp only[dite_eq_left h] exact ih (i+1) _ _ (by rw[usz i n h];omega) lemma qG9 {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) : D_outs g hs 0 0 (Array.replicate g.k 0) (Array.replicate g.k 0) (pushZ g.k.toUSize 0 (FloatArray.emptyWithCapacity g.k)) (Array.replicate (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:=qAd g.k (lt_trans ok.nk ב) refine (qHg (qE3 ok hs g.C 0 0 _ _ _ _ (К _) (by rw[Nat.zero_mul];rfl) ג ג ?_ ⟨by simp,λq _ h=>?_⟩) (λq h=>h)).trans ?_ . rw[qD4 g.k.toUSize g.k 0 _ (by rw[e,show (0:USize).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 (Array.ofFn (n:=s.cout*(s.ho*s.wo))) (funext λq=>?_);simp[D_sp,D_out,D_xw,D_t0] . refine congrArg (Array.ofFn (n:=s.cin*(s.ho*s.wo))) (funext λq=>?_);simp[D_sp,D_out,D_xw,D_t0] lemma qFf (dw:Bool) (s:ConvShape) (relu:Bool) (X W b:Array UInt32) {k C:Nat} (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) : D_F 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 dotFn s relu X W b else conv2dWith dotFn s relu X W b) =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by rw[qAc];rfl unfold D_F dsimp only rw[<-hfb] generalize (if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn 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:=qEt X X.usize (qAk X) (X.usize.toNat) 0 0xFFFFFFFF 0 (by rw[show (0:USize).toNat=0 from rfl,К]);rw[Л ⟨g2,g3⟩] at e;have Hx:=qA1 X g1 (by rw[e];intro h;rw[h] at g3;exact absurd g3 (by decide));rw[e] at Hx;refine (qG9 ?_ _).trans hfb.symm exact ⟨hk,hC,rfl,rfl,rfl,qAd _ g8,rfl,λh=>beq_iff_eq.mp (у.mp h).1,g7,g9,g10,g11,g12, qE0 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,qGf (qA1 W g15 g19).2.2.1,qGf (qA1 b g16 g20).2.2.1, g5,g15,g16,g17,g14,rfl⟩ . rfl . rfl lemma qCk:convF=conv2dSpec:=by funext s relu X W b;exact qFf false s relu X W b rfl rfl lemma qDp:depthF=depthwiseSpec:=by funext s relu X W b;exact qFf true s relu X W b rfl rfl end @[inline] def sm (w:UInt32):Bool:=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 qG8 (s:Sign) (m:Nat) (e T:Int) (K r:Nat) (hT:Format.binary32.targetExponent (totalExponent m e)=T) (hK:T=e+(K+1:Nat)) (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 T_r (N:Nat):Nat := N/2^39+if 2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1) then 1 else 0 lemma qIj (N:Nat) : (ExtendedMantissa.ofMantissaAndAccuracy N .exact >>> (38+1)).roundedMantissa=T_r N:=by rw[qD0];have d:=Nat.div_add_mod N (2^39);have l:N%2^39<2^39:=Ъ _ ב;unfold T_r;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 qHf (neg:Bool) (N:Nat) (e':Int) (hN1:2^62<=N) (hN2:N<2^63) (h1:-148<=e'+39) (h2:e'+39+150<=253) : toWord (Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) N e')) =UInt32.ofNat ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by have hr1:2^23<=T_r N:=by unfold T_r;have:2^23<=N/2^39 :=א split<;>omega have hr2:T_r N<=2^24:=by unfold T_r;have:N/2^39<2^24 :=א split<;>omega have hL:N.log2=62:=qBi N 62 hN1 hN2 have hT:Format.binary32.targetExponent (totalExponent N e')=e'+39:=by simp only[totalExponent,hL,qBu];omega rw[qG8 _ N e' _ 38 _ hT (by push_cast;ring) (qIj N)] by_cases hc:T_r N=2^24 . rw[hc,show e'+39=e'+40-1 by ring,qD1 _ (e'+40) א (by norm_num:0<2^23)];simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits];congr 1;show (UnpackedFloat.pack Format.binary32 _).toNat=_;rw[qAy _ _ _ _ ב ב א א];have e:(e'+40+150).toNat=(e'+40-1+149).toNat+2 :=א;rw[e];cases neg<;>simp[Model.modelSign]<;>omega . have hp:0< T_r N :=א;rw[qCt _ _ _ hr1 א א hp];simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits];congr 1;show (UnpackedFloat.pack Format.binary32 _).toNat=_;rw[qAy _ _ _ _ hr1 א א א];have e:(e'+39+150).toNat=(e'+39+149).toNat+1 :=א;rw[e];cases neg<;>simp[Model.modelSign]<;>omega @[inline] def T_rd (neg:Bool) (mag e0 L:UInt64):UInt32 := let n:=mag <<< (62-L) let q:=n >>> 39 let rm:=n &&& 0x7FFFFFFFFF let w:=((e0+L-24) <<< 23)+q + (if rm > 0x4000000000 || (rm == 0x4000000000 && q &&& 1 == 1) then 1 else 0) (if neg then w ||| 0x80000000 else w).toUInt32 lemma qGp (neg:Bool) (mag e0:UInt64) (h0:mag.toNat≠0) (h1:mag.toNat<2^63) (h2:24< e0.toNat+mag.toNat.log2) (h3:e0.toNat+mag.toNat.log2<277) : T_rd neg mag e0 mag.log2=toWord (Model.roundDyadic Fmt ⟨neg,mag.toNat,(e0.toNat:Int)-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[qA5,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,qHf neg N _ hN1 hN2 א א];have eT:((e0.toNat:Int)-150-((62-L:Nat):Int)+39+149).toNat=e0.toNat+L-24:=by omega rw[eT];unfold T_rd;dsimp only;have e1:(62-mag.log2).toNat=62-L:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[eL];exact hL)),eL];rfl have a1:(mag <<< (62-mag.log2)).toNat=N:=by rw[UInt64.toNat_shiftLeft,e1,Г (by omega:62-L<64),т,<-hN,Г א] generalize mag <<< (62-mag.log2)=n at a1;have a2:(n >>> 39).toNat=N/2^39:=by rw[м,a1];simp[Nat.shiftRight_eq_div_pow] have a3:(n &&& 0x7FFFFFFFFF).toNat=N%2^39:=by rw[э,a1];exact В N 39 generalize n >>> 39=q at a2;generalize n &&& 0x7FFFFFFFFF=rm at a3;have a4:(q &&& 1).toNat=q.toNat%2:=by rw[э];exact В _ 1 have a5:(e0+mag.log2-24).toNat=e0.toNat+L-24:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[Ы,eL,Г א];show 24<=_;omega)), Ы,eL,Г א] rfl have a6:((e0+mag.log2-24) <<< 23).toNat=(e0.toNat+L-24)*2^23:=by rw[UInt64.toNat_shiftLeft,a5,т];show _*2^23%_=_;rw[Г א] generalize (e0+mag.log2-24) <<< 23=hi at a6;have hup:(decide (rm > 0x4000000000) || (rm == 0x4000000000 && q &&& 1 == 1)) =decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=by apply Bool.eq_iff_iff.2;simp only[Bool.or_eq_true,Bool.and_eq_true,decide_eq_true_eq,beq_iff_eq,gt_iff_lt,UInt64.lt_iff_toNat_lt,<-UInt64.toNat_inj,a3,a4,a2];rfl rw[hup] have hw:(hi+q+(if decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1))=true then (1:UInt64) else 0)).toNat=(e0.toNat+L-24)*2^23+T_r N:=by unfold T_r;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:UInt64) else 0))=w at hw have hw2:w.toNat<2^31:=by have:T_r N<=2^24:=by unfold T_r;have:N/2^39<2^24 :=א;split<;>omega omega apply И.mp;rw[е];cases neg;. simp only[Bool.false_eq_true,ite_false,UInt64.toNat_toUInt32,Nat.zero_add,hw] . simp only[ite_true,UInt64.toNat_toUInt32,UInt64.toNat_or];have h5:w.toNat ||| (2147483648:UInt64).toNat=2^31+w.toNat:=by have:=щ hw2 1;simp only[Nat.mul_one] at this;rw[this,Nat.or_comm];rfl rw[h5];congr 1;omega lemma qIg (w:UInt32) (h:(w &&& 0x7FFFFFFF)=0):rationalWord w=0:=by have h1:=qA3 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 T_e (w:UInt32):UInt64 := if w &&& 0x7FFFFFFF == 0 then 255 else (expOf w).toUInt64 @[inline] def T_m (w:UInt32):UInt64 := if w &&& 0x7FFFFFFF == 0 then 0 else (fracOf w+0x800000).toUInt64 @[inline] def T_s (e0:UInt64) (w:UInt32):UInt64:=T_m w <<< (T_e w-e0) @[inline] def T_ok (e0:UInt64) (w:UInt32):Bool := good w && (w &&& 0x7FFFFFFF == 0 || (e0<=T_e w && T_e w-e0<=37)) @[inline] def T_p (e0:UInt64) (w:UInt32) (p:UInt64):UInt64 := if signOf w then p else p+T_s e0 w @[inline] def T_n (e0:UInt64) (w:UInt32) (n:UInt64):UInt64 := if signOf w then n+T_s e0 w else n lemma qHy (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true) : (T_s e0 w).toNat<2^61∧rationalWord w = (if signOf w then -((T_s e0 w).toNat:ℚ) else ((T_s e0 w).toNat:ℚ)) * 2^((e0.toNat:Int)-150):=by unfold T_ok at h simp only[Bool.and_eq_true,Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h obtain ⟨hg,hz⟩:=h by_cases h0:w &&& 0x7FFFFFFF=0 . have hs:T_s e0 w=0:=by unfold T_s T_m;simp[h0] rw[hs,qIg w h0];simp . obtain ⟨h1,h2⟩:=hz.resolve_left h0;have g:=qBl w hg;have a0:(w &&& 0x7FFFFFFF).toNat≠0:=λe=>h0 (И.mp (by simpa using e));rw[qA3] at a0;have hx:=qBf w;have hf:(fracOf w).toNat=w.toNat%2^23:=qBg w;have hl:=w.toNat_lt;have x1:1<=(expOf w).toNat :=א;have x2:(expOf w).toNat<=254 :=א;have f1:(fracOf w).toNat<2^23 :=א;have ee:(T_e w).toNat=(expOf w).toNat:=by unfold T_e;simp[h0] have em:(T_m w).toNat=2^23+(fracOf w).toNat:=by unfold T_m;simp only[h0,beq_iff_eq,if_false,UInt32.toNat_toUInt64];rw[UInt32.toNat_add,Г (by show _+8388608< _;omega)];show _+8388608=_;omega have l1:=UInt64.le_iff_toNat_le.1 h1;have l2:=UInt64.le_iff_toNat_le.1 h2;rw[UInt64.toNat_sub_of_le _ _ h1,ee] at l2;rw[ee] at l1;have l3:(expOf w).toNat-e0.toNat<=37:=l2 have es:(T_s e0 w).toNat=(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat):=by unfold T_s;have sh:(T_e w-e0).toNat%64=(expOf w).toNat-e0.toNat:=by rw[UInt64.toNat_sub_of_le _ _ h1,ee];exact Г א rw[UInt64.toNat_shiftLeft,em,sh,т];apply Г;have:2^((expOf w).toNat-e0.toNat)<=2^37:=д ב 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:Int)-150) =2^((expOf w).toNat-e0.toNat)*2^((e0.toNat:Int)-150):=by rw[<-Ц,<-М С];congr 1;omega rw[es];simp only[rationalWord,decodeD,nz,ite_false,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand,ew];cases signOf w<;>simp<;>ring lemma qEm (e0:UInt64) (w:UInt32) (p n:UInt64) (V:ℚ) (B C:Nat) (hC:C=2^61-1) (hB:B+C<2^63) (hw:T_ok e0 w=true) (hp:p.toNat<=B) (hn:n.toNat<=B) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) : (T_p e0 w p).toNat<=B+C∧(T_n e0 w n).toNat<=B+C ∧ V+rationalWord w=(((T_p e0 w p).toNat:ℚ)-(T_n e0 w n).toNat) * 2^((e0.toNat:Int)-150):=by obtain ⟨h1,h2⟩:=qHy e0 w hw have h3:(T_s e0 w).toNat<=C :=א unfold T_p T_n rw[h2,hV] cases signOf w . simp only[Bool.false_eq_true,ite_false];have e:(p+T_s e0 w).toNat=p.toNat+(T_s e0 w).toNat:=by rw[Ы];exact Г א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring . simp only[ite_true];have e:(n+T_s e0 w).toNat=n.toNat+(T_s e0 w).toNat:=by rw[Ы];exact Г א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring @[inline] def T_fin (p n e0:UInt64) (f:Unit->UInt32):UInt32 := if p == n then f () else let mag:=if p< n then n-p else p-n let L:=mag.log2 if e0+L<=24 || e0+L ≥ 277 then f () else T_rd (decide (p< n)) mag e0 L lemma qC9 (p n e0:UInt64) (f:Unit->UInt32) (hp:p.toNat<2^63) (hn:n.toNat<2^63) (he:e0.toNat<=255) (R:UInt32) (hf:f ()=R) (hR:p≠n->∃ d:FloatLib.Numerics.Dyadic, d.toRat=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150) ∧ R=toWord (Model.roundDyadic Fmt d)):T_fin p n e0 f=R:=by unfold T_fin split . exact hf . rename_i hne;have hne':p≠n:=λe=>hne (by simp[e]);have hnn:p.toNat≠n.toNat:=λe=>hne' (UInt64.toNat_inj.mp e);obtain ⟨d,hd,hR⟩:=hR hne';dsimp only;obtain ⟨mag,hm⟩:∃ m,m=if p< n then n-p else p-n:=⟨_,rfl⟩;rw[<-hm] have hm1:mag.toNat≠0∧mag.toNat<2^63 ∧ (((if decide (p< n)=true then -(mag.toNat:ℚ) else (mag.toNat:ℚ))) =(p.toNat:ℚ)-n.toNat):=by by_cases c:p< n . have c':=р.1 c;have:mag.toNat=n.toNat-p.toNat:=by rw[hm,if_pos c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c'.le)] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'.le];simp[c] . have c':n.toNat<=p.toNat:=by have:=р.not.1 c;omega have:mag.toNat=p.toNat-n.toNat:=by rw[hm,if_neg c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c')] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'];simp[c] obtain ⟨m0,m1,m2⟩:=hm1;split;. exact hf . rename_i hb;simp only[Bool.or_eq_true,decide_eq_true_eq,not_or,ge_iff_le] at hb;obtain ⟨b1,b2⟩:=hb;have eL:mag.log2.toNat=mag.toNat.log2:=rfl;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt m0).2 m1;omega have eA:(e0+mag.log2).toNat=e0.toNat+mag.toNat.log2:=by rw[Ы,eL];exact Г א have b3:¬ (e0+mag.log2).toNat<=(24:UInt64).toNat:=λh=>b1 (UInt64.le_iff_toNat_le.2 h);have b4:¬ (277:UInt64).toNat<=(e0+mag.log2).toNat:=λh=>b2 (UInt64.le_iff_toNat_le.2 h);rw[eA] at b3 b4;have t1:(24:UInt64).toNat=24:=rfl;have t2:(277:UInt64).toNat=277:=rfl;rw[qGp _ _ _ 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 qEv (w:UInt32):(T_e w).toNat<=255:=by unfold T_e;split;. decide . have:=qBf w;simp only[UInt32.toNat_toUInt64];omega @[inline] def T_min (x y:UInt64):UInt64:=if x< y then x else y lemma qB6 (x y:UInt64):(T_min x y).toNat<=x.toNat:=by unfold T_min;split;. exact le_rfl . have:=р.not.1 ‹_›;omega lemma qDx (e0:UInt64) (w0 w1:UInt32) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 0)).toNat<=2*(2^61-1)∧(T_n e0 w1 (T_n e0 w0 0)).toNat<=2*(2^61-1) ∧ rationalWord w0+rationalWord w1=(((T_p e0 w1 (T_p e0 w0 0)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 0)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEm e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=qEm e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma qF2 (e0:UInt64) (w0 w1:UInt32) (p n:UInt64) (V:ℚ) (hp:p.toNat<=2*(2^61-1)) (hn:n.toNat<=2*(2^61-1)) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 p)).toNat<2^63∧(T_n e0 w1 (T_n e0 w0 n)).toNat<2^63 ∧ V+rationalWord w0+rationalWord w1=(((T_p e0 w1 (T_p e0 w0 p)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 n)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEm e0 w0 p n V _ _ rfl ב h0 hp hn hV obtain ⟨b1,b2,b3⟩:=qEm e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 exact ⟨by omega,by omega,b3⟩ lemma qBj (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true):expOf w≠255:=by unfold T_ok at h;simp only[Bool.and_eq_true] at h;exact qAf w h.1 end @[noinline] def addSlow (relu:Bool) (x y:Word):Word := act relu (toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat @[noinline] def addT (relu:Bool) (x y:Word):Word := let e0:=T_min (T_e x) (T_e y) if T_ok e0 x && T_ok e0 y then act relu (T_fin (T_p e0 y (T_p e0 x 0)) (T_n e0 y (T_n e0 x 0)) e0 λ_=>toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) else addSlow relu x y lemma qGr (relu:Bool) (x y:Word):addT relu x y=addSlow relu x y:=by unfold addT;dsimp only;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨hx,hy⟩:=hc;obtain ⟨p1,n1,v⟩:=qDx _ x y hx hy;have ex:=qBj _ x hx;have ey:=qBj _ y hy;unfold addSlow;congr 1 refine qC9 _ _ _ _ א א ((qB6 _ _).trans (qEv 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:Bool) (nf zr u:Float) (x y:Word):Word := if sm x && sm y then let q:=dv x*u let c:=dv y let r:=ziv ((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 qG5 (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠0xFFFFFFFF) : ∃ (S:Bool) (ex fr:Nat) (W:ℝ),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ V=(if S then -W else W) ∧ W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=qAs s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qAi _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qAi _ S ex fr h1 h2 h3 hb' rw[hva] at hhi rw[hvb] at hlo refine ⟨S,ex,fr,if S then -V else V,h1,h2,h3,hz,?_,?_,?_,?_⟩ all_goals cases S all_goals simp only[Bool.false_eq_true,ite_false,ite_true,neg_neg] at hhi hlo ⊢ . exact 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 qIh (ex:Nat) (h1:897<=ex) (h2:ex<1151):((ex:Int)-1046+150).toNat=ex-896:=by omega lemma qHs (S:Bool) (ex fr m:Nat) (q:Int) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (hm:m≠0) (hW1:(m:ℝ)*2^q<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<(m:ℝ)*2^q) (hW3:(2^53-2^28)*2^((ex:Int)-1076)<(m:ℝ)*2^q) : toWord (Model.roundDyadic Fmt ⟨S,m,q⟩) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-Ц,<-М С] congr 1;push_cast;ring have hword:=qFb S m q (2^23+fr) ((ex:Int)-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,qIh ex h1 h2,e2] lemma qF9 (a b:ℝ) (ha:0<=a) (h:a< b) : ∃ (m:Nat) (q:Int),m≠0∧a<(m:ℝ)*2^q∧(m:ℝ)*2^q<=b:=by obtain ⟨k,hk⟩:=exists_pow_lt_of_lt_one (ד: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:Int),?_,?_,?_⟩;. 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 qHo (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,Model.ofUInt32,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z lemma qIl (w:UInt32) (h0:expOf w≠0) (h1:expOf w≠255):wv w≠0:=by rw[qA8 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 qGq (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠0xFFFFFFFF) (M:Model Fmt) (hf:Model.isFinite M=true) (hM:Model.toReal M=Model.roundAt Fmt V):toWord M=ziv s e:=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=qG5 s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=ю ב _;have hA2:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076) :=Ь ב hb0.le obtain ⟨Lo,hLo0,hLoW,hLo1,hLo2⟩:∃ Lo:ℝ,0<=Lo∧Lo< W ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<=Lo) ∧ (2^53-2^28)*2^((ex:Int)-1076)<=Lo:=by by_cases hfr:fr=0;. exact ⟨_,hA2,hW3,λh=>absurd hfr h,le_rfl⟩ . have hfr1:(1:ℝ)<=fr:=by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hfr refine ⟨_,?_,hW2 hfr,λ_=>le_rfl,?_⟩;. exact Ь (by nlinarith) hb0.le . exact Б (by nlinarith) hb0.le have hW0:0< W:=в hLo0 hLoW;obtain ⟨m1,q1,hm1,hd1,hd1'⟩:=qF9 Lo W hLo0 hLoW;set B:ℝ:=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) with hB;obtain ⟨m2,q2,hm2,hd2,hd2'⟩:=qF9 W ((W+B)/2) hW0.le ד;have w1:=qHs S ex fr m1 q1 h1 h2 h3 hm1 ד (λh=>в (hLo1 h) hd1) (в hLo2 hd1) have w2:=qHs 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[qBf,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:Model.roundDyadic Fmt ⟨S,m1,q1⟩=ofW z:=by rw[<-w1,qHo] have r2:Model.roundDyadic Fmt ⟨S,m2,q2⟩=ofW z:=by rw[<-w2,qHo] have fz:Model.isFinite (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:Model.toReal (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:Model.toReal M≠0:=by rw[hM,<-hv];exact qIl 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 qIk:(2:ℝ)^(127:ℤ)<=Model.toReal (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,qAx] rw[h3,show (127:ℤ)=23+104 from rfl,М ב];apply Б _ (by positivity);norm_num lemma qHu (w:UInt32) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by unfold sm at h;simp only[Bool.and_eq_true,decide_eq_true_eq] at h;obtain ⟨hg,hl⟩:=h;refine ⟨hg,?_⟩;have h1:w.toNat%2^31<=0x70FFFFFF:=by have:=Ж.mp hl;rwa [qA3] at this have h2:=qBl w hg;rw[qB8 w hg];refine (qCe _ _ h1 א).trans ?_;have:A_mag 0x70FFFFFF=(16777215:ℝ)*2^(75:ℤ):=by unfold A_mag;norm_num rw[this];norm_num lemma qC5 (w:UInt32) (h:good w=true):Model.isFinite (ofW w)=true:=by have he:=qAf w h;simp[ofW,isFinite_ofUInt32,he] lemma qH6:wv oneW=1:=by rw[qA8 oneW (by decide),qEi];simp lemma qCy (relu:Bool) (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[Bool.and_eq_true] at hc;obtain ⟨gx,lx⟩:=qHu x hc.1;obtain ⟨gy,ly⟩:=qHu y hc.2;dsimp only;split;. exact qGr relu x y . rename_i hr;have hn:ziv ((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⟩:=qBk S E hn;obtain ⟨hlo,hhi⟩:=qBp 1 ב (λ_=>x) (λ_=>oneW) y (λ_ _=>gx) (λ_ _=>by decide) gy [0] ג ג (λt ht h=>absurd (by simp;omega) h) S E (by simp only[List.foldl_cons,List.foldl_nil];exact ⟨hs.symm,he.symm⟩) hbig;have hv:exactS 1 (λ_=>x) (λ_=>oneW) y=Model.toReal (ofW x)+Model.toReal (ofW y):=by simp only[exactS,Finset.sum_range_one,qH6,mul_one];rfl rw[hv] at hlo hhi;have hb : |Model.toReal (ofW x)| + |Model.toReal (ofW y)| ≤ Model.toReal (Model.posMaxFinite Fmt):=by have e:(2:ℝ)^99+2^99<=2^(127:ℤ) :=ב;exact (add_le_add lx ly).trans (e.trans qIk) have hf:=Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ rfl (qC5 x gx) (qC5 y gy) hb;have hM:=Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ rfl (qC5 x gx) (qC5 y gy) hb;have:=qGq S E _ hlo hhi hn _ hf hM;rw[this] . rfl lemma qE1 {L:USize->Array UInt32->Array UInt32} (f:USize->UInt32->UInt32) {e:USize} (hL:∀ i x,L i x=if i< e then L (i+1) (wrW x i (f i (rdW x i))) else x) (d:Nat) : ∀ (i:USize) (x:Array UInt32),i.toNat+d=e.toNat->e.toNat<=x.size->x.size<2^32 → (L i x).size=x.size∧∀ q,(L i x)[q]! =if i.toNat<=q∧q< e.toNat then f (USize.ofNat q) x[q]! else x[q]!:=by induction d with | zero => intro i x h1 _ _ rw[hL,if_neg (by rw[А];omega)] exact ⟨rfl,λq=>(Д א).symm⟩ | succ d ih => intro i x h1 h2 h3 have hi:i< e:=by rw[А];omega have hs:=qA2 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,qBc _ _ _ _ h3 א,usz i e hi,qA0 _ _ 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:Bool) (nf zr u:Float) (y:Array Word) (e i:USize) (x:Array Word) : Array 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 addF (relu:Bool) (a b:Array Word):Array 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 Array.ofFn (n:=a.size) λi=>addOne relu nf 0 (dv oneW) a[i] b[i.val]! lemma qFl:addF=addReluSpec:=by funext relu a b;unfold addF;split . rename_i h;obtain ⟨h1,h2⟩:=h;have hu:a.usize.toNat=a.size:=qAd _ h2;have hg:=qE1 (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:USize).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⟩,qCy,Е a i hi,qA0 _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,qCy] end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary @[noinline] def E_fma (relu:Bool) (w sw tw:UInt32):UInt32 := act relu (toWord (Model.fma (ofW w) (ofW sw) (ofW tw))) @[inline] def E_act (relu:Bool) (r:UInt32):UInt32 := if relu && signOf r then 0 else r @[inline] def E_el (relu g:Bool) (sw tw:UInt32) (y z nf zr:Float) (w:UInt32):UInt32 := if g && good w then let q:=dv w*y let r:=ziv ((zr+q)+z) (nf*((zr+q.abs)+z.abs)) if r == 0xFFFFFFFF then E_fma relu w sw tw else E_act relu r else E_fma relu w sw tw @[specialize] def E_map (f:UInt32->UInt32) (e i:USize) (x:Array UInt32):Array UInt32 := if h:i< e then E_map f e (i+1) (wrW x i (f (rdW x i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def E_chs (relu:Bool) (c hw:Nat) (s t:Array UInt32) (nf zr:Float) (ch:Nat) (x:Array UInt32):Array UInt32 := if ch< c then let sw:=s[ch]! let tw:=t[ch]! let y:=dv sw let z:=dv tw E_chs relu c hw s t nf zr (ch+1) (E_map (E_el 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 scaleF (relu:Bool) (c hw:Nat) (x s t:Array UInt32):Array UInt32 := if x.size=c*hw∧x.size<2^32 then E_chs relu c hw s t (nfOf 1) 0 0 x else scaleShiftSpec relu c hw x s t @[noinline] def E_dot (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := dotFn (k*k) (λt=>X[b.toNat+(t/k*w+t%k)]!) (λ_=>oneW) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open FloatLib.Floats.ExecFloat.Binary open Float.Model Float.Model.UnpackedFloat def E_dotT (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := if k=2 then let i:=b.toNat let a0:=X[i]! let a1:=X[i+1]! let a2:=X[i+w]! let a3:=X[i+(w+1)]! let e0:=T_min (T_min (T_e a0) (T_e a1)) (T_min (T_e a2) (T_e a3)) if T_ok e0 a0 && T_ok e0 a1 && T_ok e0 a2 && T_ok e0 a3 then T_fin (T_p e0 a3 (T_p e0 a2 (T_p e0 a1 (T_p e0 a0 0)))) (T_n e0 a3 (T_n e0 a2 (T_n e0 a1 (T_n e0 a0 0)))) e0 λ_=>E_dot X b k w else E_dot X b k w else E_dot X b k w lemma qEz (X:Array UInt32) (b:USize) (k w:Nat):E_dotT X b k w=E_dot X b k w:=by unfold E_dotT;split . rename_i hk;subst hk;dsimp only;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨⟨⟨h0,h1⟩,h2⟩,h3⟩:=hc;generalize hi:b.toNat=i at *;obtain ⟨p1,n1,v⟩:=qDx _ _ _ h0 h1;obtain ⟨p2,n2,v2⟩:=qF2 _ _ _ _ _ _ p1 n1 v h2 h3;refine qC9 _ _ _ _ p2 n2 ((qB6 _ _).trans ((qB6 _ _).trans (qEv _))) _ 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∈Finset.range (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,qEi,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 ⟨qBj _ _ h0,o⟩ . rw[q1];exact ⟨qBj _ _ h1,o⟩ . rw[q2];exact ⟨qBj _ _ h2,o⟩ . rw[q3];exact ⟨qBj _ _ h3,o⟩ obtain ⟨d,h4,h5⟩:=qC7 (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 E_dot;rw[qAc];exact h5 . rfl . rfl end @[noinline] def E_div (n:Nat) (s:UInt32):UInt32 := toWord (Model.div (ofW s) (ofW (natW n))) @[inline] def E_quo (n:Nat) (j s:UInt32):UInt32 := if j< expOf s∧expOf s≠255 then s-(j <<< (23:UInt32)) else E_div n s @[inline] def E_tl (X:Array UInt32) (b:USize) (k w n:Nat) (j z:UInt32) (nf zb s a:Float) (o:UInt32):UInt32 := if o=0 then z else let r:=ziv (s+zb) (nf*(a+zb.abs)) E_quo n j (if r == 0xFFFFFFFF then E_dotT X b k w else r) def E_win (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (u nf zb:Float) (b m t:USize) (s a:Float) (o:UInt32):UInt32 := if h:t< m then let v:=rdW X (b+(rdW off t).toUSize) let d:=dv v*u E_win X off k w n j z u nf zb b m (t+1) (s+d) (a+d.abs) (o ||| v) else E_tl X b k w n j z nf zb s a o termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega @[specialize] def G_x (f:USize->UInt32) (sx:USize) (W x:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if x< W then G_x f sx W (x+1) (b+sx) (q+1) (wrW out q (f b)) else out termination_by W-x @[specialize] def G_y (f:USize->UInt32) (sy sx:USize) (W H y:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if y< H then G_y f sy sx W H (y+1) (b+sy) (q+W.toUSize) (G_x f sx W 0 b q out) else out termination_by H-y @[specialize] def G_c (f:USize->UInt32) (sc sy sx:USize) (W H c ch:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if ch< c then G_c f sc sy sx W H c (ch+1) (b+sc) (q+(H*W).toUSize) (G_y f sy sx W H 0 b q out) else out termination_by c-ch def E_off (k w:Nat):Array UInt32 := Array.ofFn (n:=k*k) λt=>(t.val/k*w+t.val%k).toUInt32 @[noinline] def E_grid (c h w k n:Nat) (X off:Array UInt32) (j z:UInt32) (u nf zb zr:Float) : Array UInt32 := G_c (λb=>E_win X off k w n j z u nf zb b n.toUSize 0 zr zr 0) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (Array.replicate (c*(h/k*(w/k))) 0) def avgF (c h w k:Nat) (X:Array UInt32):Array UInt32 := if c*h*w<=X.size∧X.size<2^32∧k*k<2^20 ∧ scan X X.usize 0 (qAk X) 0xFFFFFFFF 0≠0xFFFFFFFF then let n:=k*k let off:=E_off k w let j:UInt32:=if n=4 then 2 else if n=64 then 6 else 255 let z:=E_div n (dotFn n (λ_=>0) (λ_=>oneW)) E_grid c h w k n X off j z (dv oneW) (nfOf n) (dv 0) 0 else avgpoolSpec c h w k X lemma qHm (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠0xFFFFFFFF):V≠0:=by obtain ⟨S,ex,fr,h1,h2,h3,-,ha,hb⟩:=qAs s e h;obtain ⟨-,va,hva,-,-,loa⟩:=qAi _ S ex fr h1 h2 h3 ha;obtain ⟨-,vb,hvb,-,-,lob⟩:=qAi _ S ex fr h1 h2 h3 hb;have h0:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076) :=Ь ב (zpow_nonneg ב _);rintro rfl;rw[hva] at hhi;rw[hvb] at hlo;cases S;. simp only[Bool.false_eq_true,ite_false] at hlo;linarith . simp only[ite_true] at hhi;linarith lemma qF5 (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:Float) (d:FloatLib.Numerics.Dyadic) (hd:(d.toRat:ℝ)=exactS n xw ww bw) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠0xFFFFFFFF):ziv s e=toWord (Model.roundDyadic Fmt d):=by have hV:=qHm s e _ hlo hhi h;have hsum:=qGe n xw ww bw hx hw hb have e0:∀ c:FloatLib.Numerics.Dyadic,(c.toRat:ℝ)=exactS n xw ww bw → c.significand≠0:=λc hc h0=>hV (by rw[<-hc,(а c).mpr h0,ع]) obtain ⟨d',hd',hw'⟩:=qC7 (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 ⟨qAf _ (hx j hj),qAf _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,qAf _ hb⟩) (λh0=>hV (by rw[<-hsum,h0,ع])) rw[qCr 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 E_d (x y z:UInt32):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 qF4 (x y z:UInt32) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) : Model.fma (ofW x) (ofW y) (ofW z)=Model.roundDyadic Fmt (E_d 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 (Model.ofUInt32 x:Model Fmt) (Model.ofUInt32 y) (Model.ofUInt32 z) =none:=(Model.chooseNaN3_eq_none_iff _ _ _).2 ⟨Model.isNaN_eq_false_of_toDyadic?_some e,Model.isNaN_eq_false_of_toDyadic?_some f,Model.isNaN_eq_false_of_toDyadic?_some 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,Bool.false_eq_true,ite_false,e,f,g];rfl lemma qGx (x y z:UInt32) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) : ((E_d 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[E_d,Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,j,qA8 x hx,qA8 y hy,qA8 z hz];simp only[rationalWord,Rat.cast_add,Rat.cast_mul] lemma qGm (relu:Bool) (r:UInt32) (hf:expOf r≠255):E_act 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 qFp (relu:Bool) (s e:Float) (h:ziv s e≠0xFFFFFFFF) : E_act relu (ziv s e)=act relu (ziv s e):=by apply qGm;obtain ⟨S,ex,fr,h1,h2,h3,hz,-,-⟩:=qAs s e h;intro h0;have h4:(expOf (ziv s e)).toNat=255:=by rw[h0];rfl rw[qBf,hz,е] at h4;split at h4<;>omega lemma qGv (relu g:Bool) (sw tw w:UInt32) (hg:g=true->good sw=true∧good tw=true) : E_el relu g sw tw (dv sw) (dv tw) (nfOf 1) 0 w=E_fma relu w sw tw:=by unfold E_el;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⟩:=qBk _ _ hr';obtain ⟨lo,hi⟩:=qBp 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:exactS 1 (λ_=>w) (λ_=>sw) tw=wv w*wv sw+wv tw:=by simp[exactS] have hf:=qAf _ h2;have hg1:=qAf _ h3;have hg2:=qAf _ h4;rw[qFp _ _ _ hr',qF5 1 ב (λ_=>w) (λ_=>sw) tw (λ_ _=>h2) (λ_ _=>h3) h4 _ _ (E_d w sw tw) (by rw[e1,qGx _ _ _ hf hg1 hg2]) lo hi hr',E_fma,qF4 _ _ _ hf hg1 hg2] . rfl lemma qEk (relu:Bool) (c hw:Nat) (s t:Array UInt32) (d:Nat) : ∀ (ch:Nat) (x:Array UInt32),ch+d=c->x.size=c*hw->x.size<2^32 → (E_chs relu c hw s t (nfOf 1) 0 ch x).size=x.size∧∀ q,q< c*hw → (E_chs relu c hw s t (nfOf 1) 0 ch x)[q]! =if ch*hw<=q then E_fma relu x[q]! s[q/hw]! t[q/hw]! else x[q]!:=by induction d with | zero => intro ch x h1 _ _ rw[E_chs,if_neg א] refine ⟨rfl,λq hq=>?_⟩ rw[if_neg (by rw[(א:ch=c)];omega)] | succ d ih => intro ch x h1 h2 h3 rw[E_chs,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:=qAd _ א have e0:(ch*hw).toUSize.toNat=ch*hw:=qAd _ א obtain ⟨h6,h7⟩:=qE1 (λ_=>E_el relu (good s[ch]! && good t[ch]!) s[ch]! t[ch]! (dv s[ch]!) (dv t[ch]!) (nfOf 1) 0) (L:=E_map _ ((ch+1)*hw).toUSize) (λ_ _=>by rw[E_map];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,qGv _ _ _ _ _ (λ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 qE8:scaleF=scaleShiftSpec:=by funext relu c hw x s t;unfold scaleF;split . rename_i h;obtain ⟨h1,h2⟩:=h;obtain ⟨h3,h4⟩:=qEk 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,E_fma] . rfl lemma qFx (w:UInt32):toWord (ofW w)=w:=by apply И.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact Г w.toBitVec.isLt lemma qCg (w:UInt32) (h:expOf w≠255) : Model.toReal (ofW w)=(sv w:ℝ)*2^((idxOf w:ℤ)-149):=by rw[show Model.toReal (ofW w)=wv w from rfl,qA8 w h,rationalWord,decodeD_toRat];push_cast;rfl lemma qH4 (s j:UInt32) (hj:j.toNat<=6) (a:j.toNat<(expOf s).toNat) : (expOf (s-(j <<< (23:UInt32)))).toNat=(expOf s).toNat-j.toNat ∧ fracOf (s-(j <<< (23:UInt32)))=fracOf s ∧ signOf (s-(j <<< (23:UInt32)))=signOf s:=by rw[qBf] at a have hs:=s.toNat_lt have e:(j <<< (23:UInt32)).toNat=j.toNat*2^23:=by rw[UInt32.toNat_shiftLeft,т] exact Г (by simp;omega) have f:j <<< (23:UInt32)<=s:=by rw[Ж,e];omega have d:(s-(j <<< (23:UInt32))).toNat=s.toNat-j.toNat*2^23:=by rw[س _ _ f,e] refine ⟨?_,?_,?_⟩ . rw[qBf,qBf,d];omega . apply И.mp;rw[fracOf,fracOf,qBg,qBg,d];omega . rw[qDk,qDk,d];simp;omega lemma qCw (s y j:UInt32) (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:UInt32)):=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⟩:=qH4 s j f b;generalize s-(j <<< (23:UInt32))=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:Model.isFinite (ofW s)=true:=by simp[ofW,isFinite_ofUInt32,a] have u:Model.isFinite (ofW y)=true:=by simp[ofW,isFinite_ofUInt32,d] have v:Model.isFinite (ofW r)=true:=by simp[ofW,isFinite_ofUInt32,l] have x:Model.toReal (ofW y)=2^j.toNat:=by rw[qCg _ d,A,k];push_cast;rw[show ((126:ℤ)+j.toNat-149)=(j.toNat:ℤ)+(-23) by ring,М С,Ц];field_simp;norm_num have I:Model.toReal (ofW r)=Model.toReal (ofW s)/2^j.toNat:=by rw[qCg _ l,qCg _ a,G,H,show ((idxOf s:ℤ)-j.toNat-149) =((idxOf s:ℤ)-149)+-(j.toNat:ℤ) by ring,М С,zpow_neg,Ц];field_simp have J : |Model.toReal (ofW s)/Model.toReal (ofW y)| ≤ Model.toReal (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:Model.isFinite (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:Model.toReal (Model.div (ofW s) (ofW y))≠0:=by rw[p,qCg _ 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,qFx] lemma qFw (n:Nat) (j s:UInt32) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) : E_quo n j s=E_div n s:=by unfold E_quo;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 (qCw s _ 2 (by decide) h2 h1 (by decide) (by decide) (by decide) (by decide)).symm . exact (qCw s _ 6 (by decide) h2 h1 (by decide) (by decide) (by decide) (by decide)).symm . rfl def E_f (xw:Nat->UInt32) (l:List Nat) (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 qF1 (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (nf zb:Float) (b m:USize) (xw:Nat->UInt32) (hx:∀ t:USize,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:Nat):∀ (t:USize) (s a:Float) (o:UInt32),t.toNat+d=m.toNat → E_win X off k w n j z (dv oneW) nf zb b m t s a o=E_tl X b k w n j z nf zb (E_f xw (List.range' t.toNat d) ⟨s,a⟩).s (E_f 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[E_win,Х (by rw[А];omega)] rfl | succ d ih => intro t s a o h have ht:t< m:=by rw[А];omega rw[E_win,Р 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 qIa (xw:Nat->UInt32) (l:List Nat):∀ o:UInt32,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 qHi (n:Nat) (hn:n<2^20) (xw:Nat->UInt32) (hg:∀ t,t< n->good (xw t)=true) (r:ziv ((E_f xw (List.range n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (List.range n) ⟨0,0⟩).t+(dv 0).abs))≠0xFFFFFFFF) : ziv ((E_f xw (List.range n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (List.range n) ⟨0,0⟩).t+(dv 0).abs))=dotOf n xw (λ_=>oneW):=by obtain ⟨-,-,hbig⟩:=qBk _ _ r obtain ⟨hlo,hhi⟩:=qBp n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) (List.range n) List.nodup_range ג (λt h1 h2=>absurd (н.mpr h1) h2) _ _ ⟨rfl,rfl⟩ hbig have hsum:((∑ j∈Finset.range n,rationalWord (xw j)*rationalWord oneW:ℚ):ℝ) =exactS n xw (λ_=>oneW) 0:=by rw[exactS,qBs 0 (by decide) (by decide),add_zero,Rat.cast_sum] refine ы rfl λt ht=>?_ rw[Rat.cast_mul,qA8 _ (qAf _ (hg t (Finset.mem_range.mp ht))),qA8 oneW (by decide)] obtain ⟨d,hd,hw⟩:=qC7 n xw (λ_=>oneW) (λj hj=>⟨qAf _ (hg j hj),by decide⟩) λh=>qHm _ _ _ hlo hhi r (by rw[<-hsum,h,ع]) rw[hw] exact qF5 n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) _ _ d (by rw[hd,hsum]) hlo hhi r lemma qGl (X:Array UInt32) (b:USize) (k w n:Nat) (hn:n<2^20) (j:UInt32) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) (xw:Nat->UInt32) (hg:∀ t,t< n->good (xw t)=true) (hs:E_dot X b k w=dotOf n xw (λ_=>oneW)) : E_tl X b k w n j (E_div n (dotFn n (λ_=>0) (λ_=>oneW))) (nfOf n) (dv 0) (E_f xw (List.range n) ⟨0,0⟩).s (E_f xw (List.range n) ⟨0,0⟩).t ((List.range n).foldl (λc i=>c ||| xw i) 0)=E_div n (dotOf n xw (λ_=>oneW)):=by unfold E_tl split . rename_i h1;obtain ⟨-,h5⟩:=qIa 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,qAc] . dsimp only;rw[qFw _ _ _ hj];congr 1;split;. rw[qEz];exact hs . rename_i hr;exact qHi _ hn xw hg (λh=>hr (beq_iff_eq.mpr h)) lemma qEc {c h w k ch y x t:Nat} (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:Nat) (sp:Nat->UInt32) (out:Array UInt32) (p:Nat):Prop := out.size=N∧N<2^32∧∀ i,i< p->out[i]! = sp i lemma qIi {N p:Nat} {sp:Nat->UInt32} {out:Array UInt32} (h:G_P N sp out p) (hp:p< N) (q:USize) (hq:q.toNat=p):G_P N sp (wrW out q (sp p)) (p+1):=by obtain ⟨h1,h2,h3⟩:=h;refine ⟨(qA2 _ _ _).trans h1,h2,λi hi=>?_⟩;rw[qBc _ _ _ _ א א,hq];by_cases e:i=p;. rw[if_pos e,e] . rw[if_neg e];exact h3 i א lemma qHn (a s i:Nat):(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 qD7 {L:Nat->USize->USize->Array UInt32->Array UInt32} {g:USize->USize->Array UInt32->Array UInt32} {sb S n N q0 b0:Nat} {sp:Nat->UInt32} (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:Nat) : ∀ 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 א,<-qHn,<-qHn] exact ih (i+1) _ א (hs i o א h2) lemma qHw {f:USize->UInt32} {sc sy sx W H c N:Nat} {sp:Nat->UInt32} (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:Array UInt32) (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:=qD7 (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:=qD7 (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:=qD7 (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 qIi h א _ (qAd _ (by have:=h.2.1;omega))) W 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) H 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) c 0 o א (by simpa using ho) simp only[Nat.zero_mul,Nat.zero_add,Nat.add_zero,hN] at this exact this lemma qHb {c H W q:Nat} (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 qHj (f:USize->UInt32) (c h w k:Nat) (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 (Array.replicate (c*(h/k*(w/k))) 0) = Array.ofFn (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:=qHw (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⟩:=qBn W (H*ch+y) t h3 obtain ⟨d3,d4⟩:=qBn 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) (Array.replicate (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 qGc {c h w k ch y x:Nat} {X:Array UInt32} (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:USize) (ht:t.toNat< k*k) : rdW X (((ch*h+y*k)*w+x*k).toUSize+(rdW (E_off k w) t).toUSize) =poolAt h w k X ch y x t.toNat:=by have h4:=qEc 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:(E_off k w).size<2^32:=by rw[E_off,Array.size_ofFn];exact h3 rw[qA0 _ _ hz,E_off,qAe (k*k) (λt=>(t/k*w+t%k).toUInt32) _ ht,qA0 _ _ 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 :=qAd _ א show _=X[(ch*h+y*k+t.toNat/k)*w+x*k+t.toNat%k]! rw[qAh _ _ (by rw[e1,e2];omega),e1,e2,e0] lemma qH8 {c h w k ch y x:Nat} {X:Array UInt32} (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:UInt32) (hj:j=255∨(j=2∧k*k=4)∨(j=6∧k*k=64)) : E_win X (E_off k w) k w (k*k) j (E_div (k*k) (dotFn (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 = E_div (k*k) (dotOf (k*k) (poolAt h w k X ch y x) (λ_=>oneW)):=by have hn:(k*k).toUSize.toNat=k*k:=qAd _ א 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=>qEc hc hy hx ht have e2:((ch*h+y*k)*w+x*k).toUSize.toNat=(ch*h+y*k)*w+x*k :=qAd _ (by have:=hI 0 hk;simp at this;omega) rw[qF1 X _ k w (k*k) j _ _ _ _ _ (poolAt h w k X ch y x) (λt ht=>qGc א h2 א hc hy hx t (by rw[А,hn] at ht;exact ht)) (k*k) 0 0 0 0 (by rw[hn,show (0:USize).toNat=0 from rfl,К]), show (0:USize).toNat=0 from rfl,<-List.range_eq_range'] refine qGl X _ k w _ h3 j hj _ (λt ht=>hg _ (by have:=hI t ht;omega)) ?_ unfold E_dot rw[qAc] congr 1 funext t rw[e2] show _=X[(ch*h+y*k+t/k)*w+x*k+t%k]! congr 1 ring lemma qGz:avgF=avgpoolSpec:=by funext c h w k X;unfold avgF;split . rename_i hc;obtain ⟨h1,h2,h3,h4⟩:=hc;simp only[avgpoolSpec,avgpoolWith];rw[E_grid,qHj _ c h w k א];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qHb hq;dsimp only;refine qH8 h1 h2 h3 (qA1 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 linR (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32):Array UInt32 := if rows=1 then convF ⟨inW,1,1,outW,1,1,1,0⟩ relu X W b else linearWith dotFn rows inW outW relu X W b lemma qIm (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) : 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,Nat.add_zero,Nat.sub_zero];congr 2 lemma qGn:linR=linearSpec:=by funext rows inW outW relu X W b;unfold linR;split;. subst_vars;rw[qIm,qCk] . rw[qAc];rfl end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats @[inline] def ordW (w:Word):UInt32 := let m:=w &&& 0x7FFFFFFF if w >>> (31:UInt32) == 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:Nat):Nat := if m/2^23=0 then m%2^23 else (2^23+m%2^23)*2^(m/2^23-1) lemma qB5: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 qC4 (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:UInt32).toNat=2^31-1:=rfl;have a2:(0xFF:UInt32).toNat=2^8-1:=rfl;have a3:(0x7FFFFF:UInt32).toNat=2^23-1:=rfl;have a4:(23:UInt32).toNat%32=23:=rfl;unfold expOf fracOf;rw[О,О,О,л,a1,a2,a3,a4,В,В,В,Ю];omega lemma qEy (w:Word):signOf w=true ↔ 2^31<=w.toNat:=by have h:w.toNat<2^32:=w.toNat_lt;have a1:(0x80000000:UInt32).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,UInt32.toNat_zero];omega . rw[decide_eq_false c];simp only[Bool.toNat_false,UInt32.toNat_zero];omega lemma qG7 (w:Word):mantOf w <<< idxOf w=M_K (w.toNat%2^31):=by obtain ⟨-,h1,h2⟩:=qC4 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 qGi (w:Word):keyOf w=if w.toNat<2^31 then (M_K (w.toNat%2^31):Int) else -(M_K (w.toNat%2^31):Int):=by unfold keyOf;rw[qG7];by_cases c:w.toNat<2^31;. rw[Л c,Д (λe=>by have:=(qEy w).mp e;omega)] . rw[Д c,Л ((qEy w).mpr א)] lemma qF6 (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:UInt32).toNat=2^31:=rfl;have a2:(31:UInt32).toNat%32=31:=rfl;have e:(w >>> (31:UInt32)).toNat=w.toNat/2^31:=by rw[л,a2,Ю] have hm:=(qC4 w).1;unfold ordW;dsimp only;by_cases c:w.toNat<2^31;. have e0:w >>> (31:UInt32)=0:=И.mp (by rw[e,UInt32.toNat_zero];omega);rw[e0,Л (show ((0:UInt32) == 0)=true from rfl),Л c,UInt32.toNat_add,hm,a1];omega . have e0:(w >>> (31:UInt32) == 0)=false:=by rw[beq_eq_false_iff_ne];intro h0;rw[h0,UInt32.toNat_zero] at e;omega rw[e0,Д ג,Д c,UInt32.toNat_sub,hm,a1];omega lemma qHd (a b:Word) (h:ordW a< ordW b):keyOf a< keyOf b:=by have h':=п.mp h;rw[qF6,qF6] at h';rw[qGi,qGi];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:=qB5 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:=qB5 (א: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:=qB5 (א:b.toNat%2^31< a.toNat%2^31);omega lemma qIc (x m n:Nat):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 qF8 (w:Word) (h:¬ (w &&& 0x7F800000 == 0x7F800000)=true):expOf w≠255:=by intro e;apply h;have e1:=(qC4 w).2.1;rw[e,show (255:UInt32).toNat=255 from rfl] at e1;rw[beq_iff_eq,<-И,О,show (0x7F800000:UInt32).toNat=255 <<< 23 from rfl,qIc,show (255:Nat)=2^8-1 from rfl,В,Ю];have:w.toNat<2^32:=w.toNat_lt;rw[(א:w.toNat/2^23%2^8=2^8-1)] lemma qHq (a:Word) (hz:Model.isZero (ofW a)=true):a=0∨a=0x80000000:=by obtain ⟨-,h1,h2⟩:=qC4 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[Bool.and_eq_true,beq_iff_eq] at hz';rw[h1,h2] at hz';by_cases c:a.toNat<2^31;. exact Or.inl (И.mp (by rw[UInt32.toNat_zero];omega)) . exact Or.inr (И.mp (by rw[show (0x80000000:UInt32).toNat=2^31 from rfl];omega)) lemma qHl (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 qHq a hz with rfl | rfl<;>decide . rw[Д (by simpa using hz)];exact qCl a lemma qGy (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[Bool.or_eq_true,not_or] at g;have ha:=qF8 a g.1;have hb:=qF8 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 (qHd a b p)] . rw[Д p];by_cases q:ordW b< ordW a;. rw[Л q];unfold maxW;rw[Д hn,compare_gt_iff_gt.mpr (qHd b a q)] . rw[Д q];by_cases e:(a == b)=true;. rw[Л e,eq_of_beq e,qHl b hb] . rw[Д e] def mpWin (X off:Array UInt32) (b m t:USize) (acc:UInt32):UInt32 := 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 maxpoolF (c h w k:Nat) (inp:Array Word):Array Word := if c*h*w<2^32∧inp.size<2^32∧k*k<2^32 then let off:=E_off 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 (Array.replicate (c*(h/k*(w/k))) 0) else maxpoolW c h w k inp lemma qFj (X off:Array UInt32) (b m:USize) (xw:Nat->UInt32) (hx:∀ t:USize,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:Nat) : ∀ (t:USize) (a:UInt32),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,List.foldl_cons] lemma qDu:maxpoolF=maxpoolSpec:=by rw[<-qDt];funext c h w k inp;unfold maxpoolF;split . rename_i hc;obtain ⟨h1,h2,h3⟩:=hc;rw[qHj _ c h w k h1];simp only[maxpoolW];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qHb hq;dsimp only;have hn:(k*k).toUSize.toNat=k*k:=qAd _ h3 have e:∀ {ch y x:Nat},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:=qEc (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,Nat.add_zero] at h4;rw[qA0 _ _ h2,qAd _ א];simp[poolAt] rw[e a1 a2 a3,qFj inp _ _ _ (poolAt h w k inp _ _ _) (λt ht=>qGc h1 h2 h3 a1 a2 a3 t (by rw[А,hn] at ht;exact ht)) (k*k) 0 _ (by rw[hn,show (0:USize).toNat=0 from rfl,К]),show (0:USize).toNat=0 from rfl,<-List.range_eq_range'];simp only[qGy] . rfl end def kernels:Kernels where core:=⟨sumX,dotX⟩ dense:=⟨linR,gemmWith dotFn⟩ conv:=⟨convF,depthF⟩ elem:=⟨avgF,maxpoolF,addF,scaleF⟩ norm:=⟨rmsnormWith dotFn,layernormWith dotFn⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specCore,specDense,specConv,specElem,specNorm,gemmSpec,avgpoolSpec,rmsnormSpec,layernormSpec,qGn,qCk,qDp,qFl,qE8,qGz,qDu,qAc,funext qDa,funext λa=>funext (qDd a)] end Submission