import ContractExact namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open ExactRuntime renaming idxOf->γ,Word->δ,mantOf->ε,signOf->η,expOf->θ,Fmt->κ,toWord->π,finishWord->ρ,rationalWord->ς,dotOf->Γ,limbVal->ϋ,sqrtLower->Ϗ,decodeD_toRat->ϙ,mantOf_lt_24->Ϛ,addLimbs64->ϛ,rowVariance->ϫ,finishWord_scale->Aa,sqrtRational->Ac open Nat renaming mod_eq_of_lt->ν,shiftRight_eq_div_pow->ψ,mul_le_mul_right->ω,shiftLeft_eq->Α,zero_add->Β,two_pow_pos->Δ,mod_lt->Ε,mul_comm->Θ,pow_le_pow_right->Ι,pow_add->Κ,pos_of_ne_zero->Ο,lt_of_le_of_lt->Ρ,div_eq_of_lt->Υ,mul_pos->ϊ,le_add_right->ώ,add_zero->ϑ,add_mul_div_right->ϔ,div_lt_of_lt_mul->ϕ,cast_sub->ϗ,one_mul->Ϙ,add_comm->Ϟ,add_mul->ϟ,and_two_pow_sub_one_eq_mod->ϡ,mul_assoc->ϣ,div_div_eq_div_mul->ϧ,log2_self_le->ϰ,add_mul_mod_self_right->ϲ,div_add_mod'->϶,zero_mul->ϸ,log2_lt->Ϻ,eq_zero_or_pos->Ae,log2_eq_iff->Af,lt_of_lt_of_le->Ag,succ_mul->Ah,sub_zero->Ai,mul_lt_mul_of_pos_right->Aj,lt_log2_self->Ak,le_div_iff_mul_le->Am,div_lt_iff_lt_mul->An,lt_succ_iff->Ao,lt_or_eq_of_le->Ap,div_add_mod->Aq,mul_le_mul->Ar,le_sub_of_add_le->Au,div_mul_le_self->Av,mul_eq_zero->Ax,shiftLeft_add_eq_or_of_lt->Bq,one_le_two_pow->BE,testBit_eq_decide_div_mod_eq->BJ,mul_lt_mul''->BP,add_assoc->BU,reduceMod->BY,mul_add_mod->Ce,mul_add_div->Cf,mul_le_mul_left->Ci,mul_zero->Co,le_trans->Cp,sub_right_comm->Cu,add_right_comm->Cv,sub_add_eq->Cx,add_mul_mod_self_left->CF,le_log2->CH,cast_zero->CP,add_sub_cancel_left->CT,one_le_iff_ne_zero->Dd,add_one_mul->Dp,cast_ofNat->DF,add_mul_div_left->DG,mod_mul->DH,pow_lt_pow_right->DJ,mod_le->DY,not_lt->DZ,cast_pow->Ei,add_sub_of_le->Ej,zero_lt_of_lt->Ek,le_of_not_le->Ev,mod_add_div->Ez,cast_natAbs->EA,sub_le->EI,cast_add->ER,cast_mul->ES,lt_asymm->ET,cast_one->EU,mod_one->Fb open UInt64 renaming toNat_toUInt32->AA,toNat_add->ξ,toNat_inj->σ,toNat_sub_of_le->Η,toNat_shiftRight->Ξ,toNat_sub->Τ,lt_iff_toNat_lt->Ψ,toNat_shiftLeft->ό,le_iff_toNat_le->ϐ,toNat_zero->Ϝ,toNat_mul->ϯ,reduceToNat->Bc,toNat_and->BR,toNat_ofNat_of_lt'->CG,toNat_one->Da,zero_shiftRight->Dm,toNat_or->Du,toNat->Er,not_lt->EO,size->Fd open USize renaming le_size->τ,lt_iff_toNat_lt->υ,size->Λ,toNat_one->Φ,toNat_mul->Dv open Bool renaming false_eq_true->φ,xor->Ζ,and_eq_true->Ω,not_eq_true'->ϵ,or_eq_true->Ϸ,eq_iff_iff->AB,and_assoc->EK open Array renaming replicate->Χ,ext'->ϻ,getElem_ofFn->AD,ofFn->AF,mkEmpty->AY,emptyWithCapacity->Ba,size_push->Dt,getElem_push_lt->Dy,getElem_push_eq->Dz,getElem_toList->DL open UInt32 renaming toNat_inj->ύ,toNat_shiftRight->Ϣ,toNat_toUInt64->AG,toNat_and->AH,toNat_toUSize->AI,le_iff_toNat_le->AJ,toNat_ofNat'->AL,toNat_sub->CX,toNat_sub_of_le->Dj,lt_iff_toNat_lt->Dl,ofNat_toNat->Ee open List renaming range->ϒ,range'_succ->Ϩ,ext_getElem->ϩ,foldl_cons->AO,range'->Bd,drop_of_length_le->BL,mem_range->BM,range_eq_range'->Ca,map_congr_left->Ck,size_toArray->CK,append_assoc->CL,length_range->CM,map_cons->CR,map_map->Df,sum_map_mul_right->Dg,length_nil->Dr,map_add_range'->DS,sum_cons->DV,length_cons->Ex,mem_map->EZ open FloatFormat renaming toModel->ϓ,binary32->AZ,subnormalAlignExp->Cb,ieeeMinNormalExponent->Ef,ieeeMaxNormalExponent->Eg open Model.NativeBinary32 renaming mkBits->ϥ,roundRatScaled->AW,ofUInt32_roundRatScaled->Bf,ofUInt32->Bj,ofUInt32_mkBits->BB open Int renaming ofNat->BG,toNat_of_nonneg->Cj,add_mul_emod_self_left->CA,negSucc->CJ,shiftLeft_eq->CV,ofNat_eq_natCast->DK,zero_add->EW,toNat->Fe open FloatLib.Numerics renaming roundQuotientEven->AP,roundShiftRightEven->AQ,shiftRightJam->AR,FixedWord.roundShiftRightEven_toNat->Bg,roundShiftRightEven_shiftRightJam->Bh,FixedWord.roundShiftRightEven->Bm,roundShiftRightEven_le_succ->Bn,roundShiftRightEven_eq_roundQuotientEven->BQ,div_two_pow_le_roundShiftRightEven->Cq,shiftRightJam_ne_zero->DC,Radix.toReal->Fa open Model renaming roundDyadicGeneral->AT,roundDyadicGeneral_eq_roundDyadic->AV,ofModel_finite_normalized_eq_ofFields->Bo,roundDyadic->Bt,isFinite->BI,ofModel->BT,finishRoundedMantissa->Cn,pow2->Ct,toNatBits->Dn,ofUInt32->DD,fma->Eu,ofFields->EH,ofBits->EQ open Or renaming inl->Ea,inr->EY open FloatLib.Numerics.Dyadic renaming toRat_eq_zero_iff->Di open Float.Model renaming totalExponent->Bb open Real renaming sqrt->Db open Float.Model.UnpackedFloat renaming roundWithAccuracy->Bp,accuracyOfFraction->Dc open Float.Model.Format renaming targetExponent->BH open FloatLib.Numerics.RationalBinary renaming lessThanPowerOfTwo->Bi,atLeastPowerOfTwo->Bk,scaleByPowerOfTwo->Bl,floorLog2->Br local notation:max x:max "˧"=>UInt64.toNat (x :) local notation:max x:max "؛"=>USize.toNat (x :) local notation:max x:max "؎"=>UInt32.toNat (x :) local notation:max x:max "۞"=>Nat.toUSize (x :) local notation:max x:max "܀"=>Nat.log2 (x :) local notation:max x:max "܂"=>Fin.val (x :) local notation:max x:max "܃"=>Array.size (x :) local notation:max x:max "܅"=>UInt32.toUInt64 (x :) local notation:max x:max "܉"=>UInt64.toNat_lt (x :) local notation:max x:max "܍"=>Array.toList (x :) local notation:max x:max "߷"=>FloatLib.Floats.Formats.BinaryInterchange.FloatFormat.fracWidth (x :) local notation:max x:max "߸"=>Nat.toUInt64 (x :) local notation:max x:max "՜"=>UInt64.toUInt32 (x :) local notation:max x:max "׆"=>Int.toNat (x :) local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by decide local notation"ה"=>by positivity local notation"ו"=>by simp;omega local notation"ז"=>by grind local notation"ח"=>by simp_all local notation"߾"=>0x80000000 local notation"߿"=>0x100000000 local notation"ק"=>0x7FFFFFFF local notation"ר"=>ite_true local notation"ש"=>ite_false local notation"ת"=>decide_eq_true_eq local notation"ؠ"=>zpow_natCast local notation"ة"=>decide_eq_true_iff local notation"ح"=>getElem!_pos local notation"ض"=>Fmt.minSubnormalExponent local notation"ػ"=>UInt64 local notation"ؼ"=>UInt32 local notation"ؽ"=>Array local notation"ؾ"=>USize local notation"ف"=>Bool local notation"ٳ"=>Nat local notation"ٴ"=>Int local postfix:max"ٽ"=>Array.size local postfix:max"پ"=>UInt64.toNat local postfix:max"پ"=>USize.toNat local postfix:max"پ"=>UInt32.toNat local postfix:max"پ"=>Int.toNat local postfix:max"ٿ"=>Nat.toUSize local postfix:max"ٿ"=>UInt64.toUSize local postfix:max"ٿ"=>UInt32.toUSize local notation:max"ڀ"k:max=>Array.ofFn (n:=k) local notation"ހ"=>4294967295 local notation"ށ"=>1048576 local notation"ނ"=>16777216 local notation"ރ"=>8388608 local notation"ބ"=>4611686018427387904 local notation"ޅ"=>0xFFFF structure Acc where pos:ؽ ٳ neg:ؽ ٳ special:ف negZero:ف def Acc.empty:Acc:=⟨Χ 18 0,Χ 18 0,false,true⟩ def Acc.total (acc:Acc):ٴ:=(ϋ acc.pos.toList:ٴ)-ϋ acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:ٳ) (neg:ف):Acc:= match acc with | ⟨pos,ng,sp,nz⟩ => if m=0 then ⟨pos,ng,sp,nz&&neg⟩ else if neg then ⟨pos,ϛ ng m.toUInt64 idx,sp,false⟩ else ⟨ϛ 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 (a:Acc) (m d:ٳ) (b:ف) (e:a.posٽ=18) (f:a.negٽ=18) (g:m<2^64) (h:d/32+1<18):(a.add m d b).posٽ=18/\(a.add m d b).negٽ=18 /\ (a.add m d b).special=a.special /\ (a.add m d b).negZero=(a.negZero&&(decide (m=0)&&b)) /\ (a.add m d b).total=a.total+(if b then -(m:ٴ) else m)*2^d:=by obtain⟨pos,k,l,n⟩:=a;simp only at e f;by_cases j:m=0 . simp[add,total,j,e,f] . cases b<;>refine⟨by simp[add,j,addLimbs64_size,e],by simp[add,j,addLimbs64_size,f],by simp[add,j],by simp[add,j],?_⟩<;>simp only[add,j,ש,φ,ר,addLimbs64_eq _ _ _ g,total]<;>rw[addLimbs_val _ _ _ א]<;>push_cast<;>ring lemma Acc.total_empty:Acc.empty.total=0:=ד section open FloatLib.Numerics lemma ZbF (s:ف) (m:ٳ) (e:ٴ) (j:ٳ) (h:j+25<=m܀):Bt κ ⟨s,shiftRightJam m j,e+j⟩=Bt κ ⟨s,m,e⟩:=by obtain⟨l,b,d⟩:j<=m܀/\23<=m܀-j/\23<=m܀:=א;have f:m≠0:=by rintro rfl;simp at h have n:=ϰ f;have g:2^(j+1)<=m:=Cp (Ι ב ((ώ (j+1) 24).trans h)) n;have o:=log2_shiftRightJam m j g;have k:AR m j≠0:=DC m j (Cp (Ι ב (ώ j 1)) g);rw[<-AV,<-AV];unfold AT;have p:κ߷=23:=rfl;have q:ض=-149:=rfl;have r:κ.minNormalExponent=-126:=rfl;simp only[beq_iff_eq,o,k,f,ϗ l,sub_add_add_cancel,p,q,r,b,d,↓reduceIte];clear f n g o k l b d p q r;split_ifs;rfl;congr 1;obtain⟨a,b⟩:=Int.eq_negSucc_of_lt_zero (א:e+j- -149<0);obtain⟨d,k⟩:e- -149=CJ (j+a)/\0 ٳ | 0=>0 | k+1=>a[i]! + 2^32*Yg a (i+1) k lemma Zv (a:ؽ ٳ) (i k m:ٳ):Yg a i (k+m)=Yg a i k+2^(32*k)*Yg a (i+k) m:=by induction k generalizing i;simp[Yg];next k b=>rw[Cv _ 1,Yg,Yg,b,BU i 1 k,Ϟ 1 k];ring lemma ZbG (a b:ؽ ٳ) (i j k:ٳ) (h:∀t,ta[i+t]! = b[j+t]!):Yg a i k=Yg b j k:=by induction k generalizing i j with |zero=>rfl |succ k d=>rw[Yg,Yg,d (i+1) (j+1) (λt e=>by rw[BU,BU,Ϟ 1];exact h _ א),show a[i]! =b[j]! by simpa using h 0 א] lemma Zw (a:ؽ ٳ) (i k:ٳ):Yg a i k=0↔∀t,ta[i+t]! = 0:=by induction k generalizing i with |zero=>simp[Yg] |succ k d=>rw[Yg,Nat.add_eq_zero_iff,Ax,d (i+1)];have e:(2:ٳ)^32≠0:=ג;constructor;rintro⟨f,b|b⟩ t g;exact absurd b e;rcases t with _|t;simpa using f;rw[(א:i+(t+1)=i+1+t)];exact b t א;intro h;refine⟨by simpa using h 0 א,EY λt g=>?_⟩;rw[(א:i+1+t=i+(t+1))];exact h (t+1) א lemma ZbH (a:ؽ ٳ) (i k:ٳ) (h:∀t,ta[i+t]! < 2^32):Yg a i k<2^(32*k):=by induction k generalizing i with |zero=>simp[Yg] |succ k b=>have d:=h 0 א;have e:=b (i+1) (λt f=>by rw[(א:i+1+t=i+(t+1))];exact h (t+1) א);simp at d;rw[Yg,(א:32*(k+1)=32+32*k),Κ];linarith lemma O0 (a:ؽ ٳ) (t:ٳ) (h:a܃<=t):a[t]! = 0:=ח lemma ZbI (a:ؽ ٳ) (i k:ٳ) (h:a܃<=i+k):Yg a i k=ϋ (a܍.drop i):=by induction k generalizing i;simp[Yg,BL (by simpa using h:a܍.length<=i),ϋ];rename_i b;rw[Yg,b (i+1) א];by_cases d:ia[t]! = 0) (e:∀t,b+n<=t->a[t]! = 0):ϋ a܍=2^(32*b)*Yg a b n:=by rw[<-List.drop_zero (l:=a܍),<-ZbI a 0 (b+(n+a܃)) א,Zv,Zv,(Zw a 0 b).2 (λt f=>by simpa using d t f),(Zw a (0+b+n) a܃).2 (λt _=>e _ א)];simp def YP (a b:ؽ ٳ) (i ca cb br:ٳ) (out:ؽ ٳ) : ٳ -> ؽ ٳ × ٳ × ٳ × ٳ | 0=>(out,ca,cb,br) | k+1 => let ta:=a[i]! + ca;let tb:=b[i]! + cb;let t:=ta%߿+߿-tb%߿-br;YP a b (i+1) (ta/߿) (tb/߿) (1-t/߿) (out.push (t%߿)) k lemma N9 (a b:ؽ ٳ) (i f g e:ٳ) (d:ؽ ٳ) (k:ٳ) (j:e<=1) (h:∀t,td[t]! < 2^32) : let r:=YP a b i f g e d k r.1ٽ=d܃+k/\(∀t,tr.1[t]! <2^32)/\(Yg r.1 0 r.1ٽ:ٴ)+2^(32*(d܃+k))*(r.2.1-r.2.2.1-r.2.2.2:ٴ)=Yg d 0 d܃+2^(32*d܃)*((Yg a i k:ٴ)-Yg b i k+f-g-e):=by induction k generalizing i f g e d with |zero=>exact⟨by simp[YP],by simpa[YP]using h,by simp[YP,Yg]⟩ |succ k q=>simp only[YP];set n:=a[i]!+f;set o:=b[i]!+g;set t:=n%߿+߿-o%߿-e;obtain⟨w,x,p⟩:=q (i+1) (n/߿) (o/߿) (1-t/߿) (d.push (t%߿)) א (by intro s l;simp only[Dt]at l;rcases Ap (Ao.1 l)with l|rfl;rw[ح _ s ו,Dy l,<-ح d s l];exact h s l;rw[ح _ _ ג,Dz];omega);refine⟨by simp[w];omega,x,?_⟩;simp only[Dt]at p⊢;rw[(א:d܃+(k+1)=d܃+1+k),p,(by rw[Zv,ZbG _ d 0 0 d܃ (λs l=>by rw[ح _ _ ו,Dy א,<-ح d _ א])];simp only[Yg,Β,Co,ϑ];rw[ح _ _ ג,Dz]:Yg (d.push (t%߿)) 0 (d܃+1)=Yg d 0 d܃+2^(32*d܃)*(t%߿)),Yg,Yg];simp only[ER,ES,Ei,DF];linear_combination (2:ٴ)^(32*d܃)*(א:((t%߿:ٳ):ٴ)+߿*((n/߿:ٳ)-(o/߿:ٳ)-(1-t/߿:ٳ))=(a[i]!:ٴ)-b[i]!+f-g-e) def YW (r:ؽ ٳ):ٳ -> Option ٳ | 0=>none | k+1=>if r[k]! != 0 then some k else YW r k def YX (r:ؽ ٳ):ٳ -> ف | 0=>false | k+1=>r[k]! != 0||YX r k lemma ZbK (r:ؽ ٳ) (k:ٳ) (h:YW r k=none):∀t,tr[t]! = 0:=by induction k<;>intro t a ·omega rename_i b;simp only[YW]at h;split at h;cases h;rename_i d;rcases Ap (Ao.1 a)with a|rfl;exact b h t a;simpa using d lemma N8 (r:ؽ ٳ) (k h:ٳ) (d:YW r k=some h):htr[t]! = 0:=by induction k;simp[YW]at d;rename_i j;simp only[YW]at d;split at d<;>rename_i f;cases d;exact⟨א,by simpa using f,λt g e=>א⟩;obtain⟨a,b,c⟩:=j d;refine⟨א,b,λt g e=>?_⟩;rcases Ap (Ao.mp e)with e|rfl;exact c t g e;simpa using f lemma N7 (r:ؽ ٳ) (k:ٳ):YX r k=false↔∀t,tr[t]! = 0:=by induction k with |zero=>simp[YX] |succ k b=>simp only[YX,Bool.or_eq_false_iff,bne_eq_false_iff_eq,b];exact⟨λ⟨d,e⟩ t a=>(Ap (Ao.mp a)).elim (e t) (·▸d),λh=>⟨h k א,λt a=>h t א⟩⟩ lemma ZbL (r:ؽ ٳ) (k:ٳ):YX r k=(Yg r 0 k != 0):=by have a:=Zw r 0 k;simp at a;cases h:YX r k;rw[a.2 ((N7 r k).1 h)];rfl;simp;exact λb=>by have:=(N7 r k).2 (a.1 b);simp_all lemma ZbM (a x y h:ٳ) (b:a<2^(32*h)):shiftRightJam (a+2^(32*h)*(x+2^32*y)) (32*h+2)=if x%4 != 0||a != 0 then (y*1073741824+x/4)|||1 else y*1073741824+x/4:=by have k:2^(32*h+2)=4*2^(32*h):=ז;have q:x%4<4:=Ε x ב;have f:a+2^(32*h)*(x%4)<4*2^(32*h):=by nlinarith have l:a+2^(32*h)*(x+2^32*y)=(a+2^(32*h)*(x%4))+4*2^(32*h)*(y*1073741824+x/4):=by nth_rw 1[<-Ez x 4];ring have g:a+2^(32*h)*(x%4)=0↔¬(x%4≠0\/a≠0):=by constructor;intro d;have o:=Nat.eq_zero_of_add_eq_zero d;obtain p|p:=Ax.1 o.2<;>omega;intro d;simp at d;simp[d.1,d.2] unfold AR;dsimp only;rw[k,l,DG _ _ א,Υ f,Β,CF,ν f];by_cases j:x%4≠0\/a≠0;have:¬(a+2^(32*h)*(x%4)=0):=λd=>g.1 d j;have e:(x%4 !=0||a !=0)=true:=ז;simp only[beq_iff_eq,this,ש,e,ר];have:=g.2 j;have e:(x%4 !=0||a !=0)=false:=ז;simp only[beq_iff_eq,this,ר,e];rfl def Yas (neg:ف) (r:ؽ ٳ) (e:ٴ) (negZero:ف):ؼ:= match YW r rٽ with | none=>ρ 0 e negZero | some 0=>ρ (if neg then -(r[0]! : ٴ) 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||YX r h then v|||1 else v;π (Bt κ ⟨neg,m,e+((32*h+2:ٳ):ٴ)⟩) lemma F9 (b:ف) (R:ٳ) (e:ٴ) (a:ف) (d:R≠0):ρ (if b then -(R:ٴ) else R) e a=π (Bt κ ⟨b,R,e⟩):=by have f:(0:ٴ)simp[ρ,ofIntDyadic,d,not_lt.2 f.le,Ο d] lemma N6 (b:ف) (r:ؽ ٳ) (e:ٴ) (a:ف) (d:∀t,tr[t]! < 2^32):Yas b r e a=ρ (if b then -(Yg r 0 r܃:ٴ) else Yg r 0 r܃) e a:=by unfold Yas;split;rename_i o;rw[(Zw r 0 r܃).2 (λt f=>by simpa using ZbK r _ o t f)];simp;rename_i g;obtain⟨p,j,k⟩:=N8 r _ 0 g;rw[(א:r܃=1+(r܃-1)),Zv,(Zw r (0+1) (r܃-1)).2 (λt f=>k _ א א)];simp[Yg] rename_i h g;obtain⟨p,j,k⟩:=N8 r _ (h+1) g;have l:Yg r 0 r܃=Yg r 0 h+2^(32*h)*(r[h]!+2^32*r[h+1]!):=by rw[(א:r܃=h+(2+(r܃-h-2))),Zv,Zv,(Zw r (0+h+2) _).2 (λt f=>k _ א א)];simp[Yg] have q:Yg r 0 h<2^(32*h):=ZbH r 0 h (λt f=>d _ א);have n:Yg r 0 r܃≠0:=by rw[l];have:0<2^(32*h)*(r[h]!+2^32*r[h+1]!):=(by have:=Ο j;positivity);omega have s:32*h+2+25<=(Yg r 0 r܃)܀:=by rw[CH n,l];have:=Ο j;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;linarith _<=_:=א rw[F9 b _ e a n,<-ZbF b _ e _ s,l,ZbM _ _ _ h q,ZbL] def Yab (pos neg:ؽ ٳ):ٳ -> ٳ | 0=>0 | k+1=>if pos[k]! != 0||neg[k]! != 0 then k+1 else Yab pos neg k lemma ZbO (a b:ؽ ٳ) (k:ٳ):∀t,Yab a b k<=t->ta[t]! = 0/\b[t]! = 0:=by induction k<;>intro t d e ·omega rename_i f;simp only[Yab]at d;split at d;omega;rename_i g;rcases Ap (Ao.1 e)with e|rfl;exact f t d e;simpa[Ϸ,bne_iff_ne,not_or]using g def YaL (pos neg:ؽ ٳ) (e:ٴ) (negZero:ف):ؼ:= let lo:=lowLimb pos neg posٽ 0;let n:=Yab pos neg (max posٽ negٽ)+1-lo;let e':=e+((32*lo:ٳ):ٴ);let d:=YP pos neg lo 0 0 0 (Ba n) n;if d.2.1=0/\d.2.2.1=0/\d.2.2.2=0 then Yas false d.1 e' negZero else let d':=YP neg pos lo 0 0 0 (Ba n) n;if d'.2.1=0/\d'.2.2.1=0/\d'.2.2.2=0 then Yas true d'.1 e' negZero else finishLimbs pos neg e negZero lemma Q2 (b d:ؽ ٳ) (e:ٴ) (f:ف):YaL b d e f=ρ ((ϋ b܍:ٴ)-ϋ d܍) e f:=by unfold YaL;dsimp only;set g:=lowLimb b d b܃ 0;set n:=Yab b d (max b܃ d܃)+1-g;have o:=lowLimb_zero b d b܃ 0;have p:=ZbO b d (max b܃ d܃);have q (a:ؽ ٳ) (h:a=b\/a=d):ϋ a܍=2^(32*g)*Yg a g n:=by apply ZbJ a g n<;>intro t j;rcases h with rfl|rfl;exact(o t א j).1;exact(o t א j).2;by_cases r:t< max b܃ d܃;rcases h with rfl|rfl;exact(p t א r).1;exact(p t א r).2;rcases h with rfl|rfl<;>exact O0 _ t א have s:=q b (Ea rfl);have u:=q d (EY rfl);rw[show(Ba n:ؽ ٳ)=#[]from rfl];have v:=N9 b d g 0 0 0 #[] n א ג;have w:=N9 d b g 0 0 0 #[] n א ג;have z:Yg (#[]:ؽ ٳ) 0 0=0:=rfl;simp only[CK,Dr,Co,pow_zero,one_mul,z,CP,zero_add]at v w;obtain⟨-,A,x⟩:=v;obtain⟨-,B,y⟩:=w;split_ifs with k l;rw[N6 false _ _ _ A,Aa,s,u];simp;congr 1;rw[k.1,k.2.1,k.2.2]at x;linear_combination (2:ٴ)^(32*g)*x;rw[N6 true _ _ _ B,Aa,s,u];simp;congr 1;rw[l.1,l.2.1,l.2.2]at y;linear_combination (-(2:ٴ)^(32*g))*y;exact finishLimbs_eq b d e f end @[inline] def Yy (acc:Acc) (wa wb:ؼ):Acc:= if θ wa=255\/θ wb=255 then acc.markSpecial else acc.add (ε wa*ε wb) (γ wa+γ wb) (Ζ (η wa) (η wb)) @[specialize] def Yw (fa fb:ٳ -> ؼ) (pos neg:ؽ ٳ) (sp nz:ف) (index:ٳ):ٳ -> Acc | 0=>⟨pos,neg,sp,nz⟩ | count+1 => match Yy ⟨pos,neg,sp,nz⟩ (fa index) (fb index) with | ⟨p,n,s,z⟩=>Yw fa fb p n s z (index+1) count @[inline] def Yn (n:ٳ) (fa fb:ٳ -> ؼ):ؼ:= let acc:=Yw fa fb (Χ 18 0) (Χ 18 0) false true 0 n;if acc.special then dotSpec (ڀ n λi=>fa i) (ڀ n λi=>fb i) else YaL acc.pos acc.neg (-298) (n≠0&&acc.negZero) lemma Zn (a b:ؼ):(if Ζ (η a) (η b) then -((ε a*ε b:ٳ):ٴ) else ((ε a*ε b:ٳ):ٴ))=sv a*sv b:=by unfold sv;cases η a<;>cases η b<;>simp lemma ZbP (g h:ٳ->ؼ) (d e:ؽ ٳ) (f:ف) (a b:ٳ):(Yw g h d e true f a b).special=true:=by induction b generalizing d e f a ·rfl rename_i k;rw[Yw];have j:(Yy ⟨d,e,true,f⟩ (g a) (h a)).special=true:=by unfold Yy Acc.markSpecial Acc.add;split_ifs<;>rfl generalize Yy ⟨d,e,true,f⟩ (g a) (h a)=r at j⊢;obtain⟨p,q,s,z⟩:=r;subst j;exact k .. lemma Q1 (g j:ٳ->ؼ) (A B:ؽ ؼ) (n:ٳ) (o:∀k,kA[k]! = g k) (r:∀k,kB[k]! = j k) (f:Acc) (e d:ٳ) (h:e+d<=n) (t:f.posٽ=18) (u:f.negٽ=18) (l:(Yw g j f.pos f.neg f.special f.negZero e d).special =false):f.special=false /\ (∀k,e<=k->k < e+d->θ A[k]! ≠ 255/\θ B[k]! ≠ 255) /\ (Yw g j f.pos f.neg f.special f.negZero e d).total=f.total+dsum A B e d /\ (Yw g j f.pos f.neg f.special f.negZero e d).negZero=(f.negZero&&dallNZ A B e d):=by induction d generalizing f e with |zero=>cases f;simp_all[Yw,dsum,dallNZ] |succ _ C=>obtain⟨p,q,s,z⟩:=f;have v:=o e א;have w:=r e א;rw[Yw,Yy]at l⊢;generalize g e=a,j e=b at*;by_cases x:θ a=255\/θ b=255;simp only[if_pos x,Acc.markSpecial,ZbP,Bool.true_eq_false]at l;have y:=not_or.mp x;obtain⟨D,E,F,G,H⟩:=Acc.add_spec ⟨p,q,s,z⟩ (ε a*ε b) (γ a+γ b) (Ζ (η a) (η b)) t u (mul_mant_lt _ _) (by have:=idxOf_le _ y.1;have:=idxOf_le _ y.2;omega);rw[if_neg x]at l⊢;obtain⟨I,J,K,L⟩:=C _ (e+1) א D E l;refine⟨F▸I,?_,?_,?_⟩;intro k N O;by_cases M:k=e;subst M;rw[v,w];exact y;exact J k א א;rw[K,H,dsum,v,w,Zn];ring;rw[L,G,dallNZ,v,w];simp[EK] lemma Ze (n:ٳ) (f:ٳ->ؼ) (k:ٳ) (a:kf i܂)[k]! = f k:=ח lemma Z5 (n:ٳ) (a b:ٳ->ؼ):Yn n a b=dotSpec (ڀ n λi=>a i) (ڀ n λi=>b i):=by unfold Yn;set A:=ڀ n λ i=>a i with y;set B:=ڀ n λ i=>b i with z;set d:=Yw a b (Χ 18 0) (Χ 18 0) false true 0 n;cases l:d.special;swap;simp[l];obtain⟨-,f,g,o⟩:=Q1 a b A B n (Ze n a) (Ze n b) Acc.empty 0 n א rfl rfl l;have g:d.total=dsum A B 0 n:=by rwa[Acc.total_empty,EW]at g have o:d.negZero=dallNZ A B 0 n:=o;simp only[l,φ,ש];rw[Q2,<-Acc.total];obtain⟨p,u⟩:Aٽ=n/\Bٽ=n:=by simp[y,z] obtain⟨C,D⟩:=dotStateLoop_flags A B {} 0 n (by simp[p]) (by simp[u]) f;set L:=A.map (DD (fmt:=κ)) with h;set R:=B.map (DD (fmt:=κ)) with v;obtain⟨e,w⟩:Lٽ=Rٽ/\Lٽ=n:=ז;have q:∀ k (r:kby have:=(f k א א).1;rw[ح A k א]at this;simp[h,isFinite_ofUInt32,this] have s:∀ k (r:kby have:=(f k א א).2;rw[ح B k א]at this;simp[v,isFinite_ofUInt32,this] have t:=(finiteDotSlice_eq A B 0 Lٽ (by simp[h]) (by simp[h,p,u]) (λk E _=>f k E א)).trans (by rw[w,g]:_=(d.total:ℚ)*2^(-298:ℤ));have x:=Model.Reduction.Internal.dotState_exact_toRat (destination:=κ) L R e q s;unfold dotSpec Model.dot;by_cases j:d.total=0 . rw[Model.Reduction.dotWithStatus_eq_zero_of_finite κ L R .nearestEven e q s (by rw[<-Di,x,t,j];simp),j,finishWord_zero];simp[Model.Reduction.Internal.dotState,Except.map,h,v,p,C,D,o] rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero κ L R .nearestEven e q s (by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L R e,t];simpa using j)];exact finishWord_of_ne _ _ _ _ j (by rw[x,t]) lemma Zi:Yn=Γ:=funext λn=>funext λa=>funext λb=>Z5 n a b @[inline] def YH (x r d:ػ):ػ:=if x>>>(r+d) != 0 then r+d else r @[inline] def Yj (x:ػ):ػ:= YH x (YH x (YH x (YH x (YH x (YH x 0 32) 16) 8) 4) 2) 1 lemma Zz (x r d:ػ) (D:ٳ) (a:d˧=D) (f:1<=D) (g:r˧+2*D<=64) (b:2^r˧<=x˧) (e:x˧<2^(r˧+2*D)):(YH x r d)˧+D<=64/\2^(YH x r d)˧<=x˧ /\ x˧<2^((YH x r d)˧+D):=by subst a;have k:(r+d)˧=r˧+d˧:=by rw[ξ];omega have l:(x>>>(r+d))˧=x˧/2^(r˧+d˧):=by rw[Ξ,k,ν א,ψ] unfold YH;by_cases j:x>>>(r+d) !=0;simp only[j,ר,k];have:x˧/2^(r˧+d˧)≠0:=(by intro h;apply(bne_iff_ne.mp j);apply σ.mp;rw[l,h];rfl);exact⟨א,(Am (Δ _)).mp (Dd.mpr this)|>.trans' ג,by rwa[(א:r˧+d˧+d˧=r˧+2*d˧)]⟩;simp only[j,ש,φ];have n:x>>>(r+d)=0:=ז;exact⟨א,b,(Nat.div_eq_zero_iff_lt (Δ _)).mp (by rw[<-l,n];rfl)⟩ lemma Zj (x:ػ) (a:x≠0):(Yj x)˧=x˧܀:=by have b:1<=x˧:=Dd.mpr λh=>a (σ.mp (by simpa using h));have d:=x܉;obtain⟨t,e,f⟩:=Zz x 0 32 32 rfl ד ד (by simpa using b) (by simpa using d);obtain⟨u,g,j⟩:=Zz x _ 16 16 rfl ד א e (by simpa using f);obtain⟨v,k,l⟩:=Zz x _ 8 8 rfl ד א g (by simpa using j);obtain⟨w,n,o⟩:=Zz x _ 4 4 rfl ד א k (by simpa using l);obtain⟨y,p,q⟩:=Zz x _ 2 2 rfl ד א n (by simpa using o);obtain⟨z,r,s⟩:=Zz x _ 1 1 rfl ד א p (by simpa using q);exact((Af א).mpr ⟨r,s⟩).symm @[inline] def YK (m L:ػ):ػ:= if 23<=L then Bm m (L-23).toNat else m<<<(23-L) lemma Z7 (m:ػ) (a:m≠0):(YK m (Yj m))˧=(if κ߷<=m˧܀ then AQ m˧ (m˧܀-κ߷) else m˧<<<(κ߷-m˧܀)) /\ 2^23<=(YK m (Yj m))˧/\(YK m (Yj m))˧<=2^24:=by have k:m˧≠0:=λh=>a (σ.mp (by simpa using h));have b:=Zj m a;have f:=ϰ k;have g:m˧<2^(m˧܀+1):=Ak;rw[show κ߷=23 from rfl];unfold YK;generalize Yj m=L at b⊢;generalize q:m˧܀=N at*;by_cases h:23<=N;have d:(23:ػ)<=L:=by rw[ϐ,b];exact h simp only[d,ר,h,(ח:(L-23)˧=N-23),Bg];exact⟨trivial,(by rw[Am (Δ _),<-Κ,Ej h];exact f:2^23<=m˧/2^(N-23)).trans (Cq _ _),(Bn _ _).trans (by rw[An (Δ _),<-Κ,(by clear*-h;omega:24+(N-23)=N+1)];exact g:m˧/2^(N-23)<2^24)⟩;simp only[(by rw[ϐ,b];exact h:¬(23:ػ)<=L),ש,h];have j:m˧*2^(23-N)<2^24:=(Aj g (Δ _)).trans_eq (by rw[<-Κ];congr 1;clear*-h;omega);rw[(by rw[ό,(by rw[Η _ _ (by rw[ϐ,b];exact Ev h),b];rfl:(23-L)˧=23-N),ν (Ρ (EI 23 N) ד),Α];exact ν (j.trans ד):(m<<<(23-L))˧=m˧*2^(23-N)),Α];exact⟨rfl,(by rw[<-Κ,Ej (Ev h)]:2^23=2^N*2^(23-N)).trans_le (ω _ f),j.le⟩ @[inline] def Yf (w:ؼ):ػ:= if θ w=0 then (fracOf w).toUInt64 else (fracOf w).toUInt64+0x800000 @[inline] def Yd (w:ؼ):ػ:= if θ w=0 then 0 else (θ w).toUInt64-1 lemma Zk (w:ؼ):(Yf w)˧=ε w:=by have:=fracOf_toNat_lt w;unfold Yf ε;split_ifs;simp;simp only[ξ];show((fracOf w)؎+ރ)%2^64=2^23+(fracOf w)؎;omega lemma Zf (w:ؼ):(Yd w)˧=γ w:=by unfold Yd γ;split_ifs with h;. rfl . have b:1<=(θ w)؎:=by rcases Ae (θ w)؎with a|a;. exact absurd (ύ.mp (by simpa using a)) h . exact a rw[Η _ _ (by rw[ϐ];simp;omega)];simp lemma Zg (w:ؼ):γ w<=254:=by have:=expOf_toNat_lt w;unfold γ;split_ifs<;>omega @[specialize] def Yaj (hw:ٳ) (f:ٳ -> ٳ -> δ) (ch j:ٳ) (acc:ؽ δ):ؽ δ:= if j ٳ -> δ) (ch:ٳ) (acc:ؽ δ):ؽ δ:= if ch ٳ -> δ):ؽ δ:=Yak c hw f 0 (AY (c*hw)) lemma D3 (a:ٳ) (f:ٳ->ٳ->δ) (b:ٳ):∀k j (d:ؽ δ),a-j=k->(Yaj a f b j d)܍=d܍++(Bd j (a-j)).map (f b):=by intro k;induction k<;>intro j d h<;>unfold Yaj;rw[if_neg א];simp[h];next k e=>rw[if_pos א,e (j+1) _ א,Cx,h,Ϩ];simp lemma W5 (c a:ٳ) (f:ٳ->ٳ->δ):∀k b (d:ؽ δ),c-b=k->(Yak c a f b d)܍=d܍++(Bd (b*a) ((c-b)*a)).map (λi=>f (i/a) (i%a)):=by intro k;induction k<;>intro b d h<;>unfold Yak ·rw[if_neg א];simp[h] rename_i e;rw[if_pos א,e (b+1) _ א,D3 a f b _ 0 _ rfl,show(c-b)*a=a+(c-(b+1))*a by rw[(א:c-b=c-(b+1)+1),Dp,Ϟ],<-List.range'_append,List.map_append,Ah];simp only[CL,Ai];congr 2;rw[show Bd (b*a) a=(Bd 0 a).map (b*a+·) by rw[DS];simp,Df];apply Ck;intro j g;rw[List.mem_range'_1]at g;have q:jٳ->δ):YT c a f=ڀ N λi=>f (i܂/a) (i܂%a):=by subst b;apply ϻ;rw[YT,W5 c a f _ 0 _ rfl];apply ϩ<;>simp @[inline] def QPb (x:ؼ) (m:ػ) (s:ف):ٳ:= let M:=Yf x<<<(Yd x-m);(if η x != s then ބ-M else ބ+M).toNat @[specialize] def QPD (n:ٳ) (f:ٳ -> ؼ) (m:ػ) (t:ٳ) (P:ؽ ٳ):ؽ ٳ:= if tlinarith[bpow_le_bpow_of_le h] lemma cB {n d:ٳ} (e:ٴ) (a:n≠0) (b:d≠0):bpow (floorLog2 n d+e)<=n/d*bpow e/\n/d*bpow eAP (Bl n d x).1 (Bl n d x).2=AP (Bl a b r).1 (Bl a b r).2:=λx r h=>by apply roundQuotientEven_eq_of_rat_eq _ _ _ _ (scaleByPowerOfTwo_snd_ne_zero _ _ _ k) (scaleByPowerOfTwo_snd_ne_zero _ _ _ l);rw[scaleByPowerOfTwo_real,scaleByPowerOfTwo_real];rwa[mul_assoc,<-bpow_add,mul_assoc,<-bpow_add,add_sub_cancel,add_sub_cancel]at h rw[roundRatScaled_false_eq_of_isIEEE f n d e o p k,roundRatScaled_false_eq_of_isIEEE f a b g o q l];simp only;rw[u _ (g+BG (Cb f)) (by rw[j];ring_nf),u _ (BG f߷-Br a b) (by rw[j,(א:BG f߷-Br n d-e=BG f߷-Br a b-g)]),y] lemma N3 (Q:ℚ) (s:ف) (N D:ٳ) (E:ٴ) (b:00simp_all);obtain rfl:Q=0:=(by simp[a]);simp[roundRatScaled_num_zero _ _ _ _ b.ne'];rfl;have g:(0:ℚ)simp[neg_div] have h:Q.num.natAbs≠0:=by simpa using (by rw[a];cases s<;>simp[g.ne']:Q≠0) have k:(0:ℝ)push_cast<;>simp[abs_of_pos k] rw[(by rw[a];cases s<;>simp[g,not_lt.mpr g.le]:decide (Q<0)=s)];cases s;exact cV _ e _ _ _ _ _ _ h Q.den_nz f b.ne' j;rw[roundRatScaled_true_eq_neg_false _ _ _ _ e Q.den_nz,roundRatScaled_true_eq_neg_false _ _ _ _ e b.ne',cV _ e _ _ _ _ _ _ h Q.den_nz f b.ne' j] lemma Zc1 (q u D r:ٳ) (d:2<=u) (f:u%2=0) (e:0.trans ג;rw[bpow_natCast];push_cast;rw[pow_succ];linarith;rw[show((q܀+1:ℕ):ٴ)+1=((q܀+2:ℕ):ٴ) by push_cast;ring,bpow_natCast];push_cast;rw[show q܀+2=q܀+1+1 from rfl,pow_succ];linarith lemma Zc3 (s:ف) (N D:ٳ) (E:ٴ) (a:0simp[n,j]<;>omega . have:¬((q܀:ٴ)+Esimp[j]<;>omega end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Floats.Formats.Flocq open Float.Model.UnpackedFloat open FloatLib.Numerics (binaryRadix Radix.toReal) lemma Zc4 (q:ℚ) (b a:ٳ) (e:ٴ) (d:0(x:ℝ)) g;push_cast at this;rw[this,(ב:(4:ℝ)=2*2),mul_zpow,sq] rw[t,Real.sqrt_mul (by exact_mod_cast d.le),Real.sqrt_sq ה];simp only[bpow,binaryRadix,Fa];push_cast;rw[mul_assoc,<-zpow_add₀ (ב:(2:ℝ)≠0)];simp rw[s]at n o;have u:(nearestEven (scaledMantissa binaryRadix (Model.fexpOf κ) (Db (q:ℝ))))׆=(shiftToTargetExponent (ϓ κ) (Ϗ b a) e (sqrtAccuracy b a)).1.roundedMantissa:=by rw[o,<-r];simp simp only[sqrtMathWord,not_le.mpr d,ש,n,u];rw[Model.roundWithAccuracy_eq_finishRoundedMantissa,Model.shiftToTargetExponent_eq_of_le_targetExponent κ _ e _ l] end @[inline] def Y2 (x:ؽ δ) (base n:ٳ):ٳ:= (ϒ n).foldl (λb j => let w:=x[base+j]!;if ε w=0 then b else min b (γ w)) 255 @[inline] def Y6 (x:ؽ δ) (base n b0:ٳ):ؽ ٳ:= (ϒ n).foldl (λa j => let w:=x[base+j]!;ϛ a (ε w*ε w)߸ (2*(γ w-b0))) (Χ 18 0) @[inline] def Y7 (x:ؽ δ) (base n b0:ٳ):ؽ ٳ × ؽ ٳ:= (ϒ n).foldl (λpq j => let w:=x[base+j]!;if η w then (pq.1,ϛ pq.2 (ε w)߸ (γ w-b0)) else (ϛ pq.1 (ε w)߸ (γ w-b0),pq.2)) (Χ 18 0,Χ 18 0) @[inline] def sqA (num den root:ٳ):ٳ:= if num=root*root*den then 0 else if 4*num δ):δ:= let t:ٴ:=(47-(num0܀:ٴ)+m܀+1)/2;let num:=if 0<=t then num0<<<(2*tپ) else num0;let den:=if 0<=t then m else m<<<(2*(-t)׆);let root:=Ϗ num den;if ރ<=root then sqW num den root (h-t) else fallback () @[inline] def YF (b0 ie:ٳ):ٳ:=min (2*b0) (ie+149)/2*2 noncomputable def YaE (x:ؽ δ) (n:ٳ) (eps:δ) (r:ٳ):δ:= let b0:=Y2 x (r*n) n;let c:=YF b0 (γ eps);YaD (limbValA (Y6 x (r*n) n b0)<<<(2*b0-c) + (n*ε eps)<<<(γ eps+149-c)) n (((c:ٴ)-298)/2) λ_=>Ac (ϫ n x r 0+ς eps) noncomputable def Y8 (x:ؽ δ) (n:ٳ) (eps:δ) (r:ٳ):ٳ × ٴ × δ:= let b0:=Y2 x (r*n) n;let pq:=Y7 x (r*n) n b0;let S1:ٴ:=(limbValA pq.1:ٴ)-limbValA pq.2;let T:ٴ:=(n:ٴ)*limbValA (Y6 x (r*n) n b0)-S1*S1;let c:=YF b0 (γ eps);(b0,S1,YaD (Tپ<<<(2*b0-c)+(n*n*ε eps)<<<(γ eps+149-c)) (n*n) (((c:ٴ)-298)/2) λ_ => Ac (ϫ n x r (rowMean n x r)+ς eps)) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open Float.Model.UnpackedFloat lemma iB (w:δ):γ w<=254:=Zg w lemma lS (m k:ٳ->ٳ) (d:∀j,m j<2^64) (e:∀j,k j/32+1<18) (l:List ٳ) (a:ؽ ٳ) (b:a܃=18):(l.foldl (λa j=>ϛ a (m j)߸ (k j)) a)܃=18 /\ ϋ (l.foldl (λa j=>ϛ a (m j)߸ (k j)) a)܍=ϋ a܍+(l.map λj=>m j*2^k j).sum:=by induction l generalizing a with|nil=>simp[b]|cons j l f=>simp;obtain⟨g,h⟩:=f (ϛ a (m j)߸ (k j)) (by rwa[addLimbs64_size]);refine⟨g,?_⟩;rw[h,addLimbs64_eq _ _ _ (d j),addLimbs_val _ _ _ (by rw[b];exact e j)];ring lemma lP (x:ؽ δ) (a b:ٳ) (f:ؽ ٳ × ؽ ٳ->ٳ->ؽ ٳ × ؽ ٳ) (d:∀e j,f e j=if η x[a+j]! then (e.1,ϛ e.2 (ε x[a+j]!)߸ (γ x[a+j]! - b)) else (ϛ e.1 (ε x[a+j]!)߸ (γ x[a+j]! - b),e.2)) (l:List ٳ):∀p q,p܃=18->q܃=18->(l.foldl f (p,q)).1ٽ=18 /\ (l.foldl f (p,q)).2ٽ=18/\((ϋ (l.foldl f (p,q)).1.toList:ٴ) - ϋ (l.foldl f (p,q)).2.toList)=(ϋ p܍:ٴ)-ϋ q܍+(l.map λj=>sv x[a+j]! * 2^(γ x[a+j]! - b)).sum:=by induction l<;>intro p q g h;simp[g,h];rename_i j l s;simp only[AO,CR,DV,d];have t:=Ϛ x[a+j]!;have u:=iB x[a+j]!;by_cases k:η x[a+j]! =true;simp only[k,ר];obtain⟨n,o,r⟩:=s p (ϛ q (ε x[a+j]!)߸ (γ x[a+j]!-b)) g (by rw[addLimbs64_size];exact h);refine⟨n,o,?_⟩;rw[r,addLimbs64_eq _ _ _ א,addLimbs_val _ _ _ (by rw[h];omega)];simp only[sv,k,ר];push_cast;ring;simp only[k,φ,ש];obtain⟨n,o,r⟩:=s (ϛ p (ε x[a+j]!)߸ (γ x[a+j]!-b)) q (by rw[addLimbs64_size];exact g) h;refine⟨n,o,?_⟩;rw[r,addLimbs64_eq _ _ _ א,addLimbs_val _ _ _ (by rw[g];omega)];simp only[sv,k,φ,ש];push_cast;ring lemma fS (f:ٳ->ℚ) (l:List ٳ) (a:ℚ):l.foldl (λt j=>t+f j) a=a+(l.map f).sum:=by induction l generalizing a;simp;next j l b=>simp[AO,b,add_assoc] lemma A3 (a b:ٳ):sqrtAccuracy a b=Dc (sqA a b (Ϗ a b)) 4:=by unfold sqrtAccuracy sqA;dsimp only;simp only[sq];split_ifs<;>rfl lemma J9 (N d:ٳ):N/d<=AP N d /\ AP N d<=N/d+1:=by unfold AP;dsimp only;split_ifs<;>omega lemma J5:κ߷=23/\Ct κ߷=ރ/\Ct (κ߷+1)=ނ/\Ef κ = -126/\ BG (Eg κ)=127/\BG κ.bias=127:=ד lemma QW (E F:ٳ):π (Bj (ϥ false E F))=ϥ false E F:=cW _ lemma J4 (r:ٳ) (k:ٴ) (b:ރ<=r) (d:r<ނ) (e:-126<=k) (a:k<=127):π (BT κ (Cn (ϓ κ) .positive (r,k-23)))=ϥ false (k+127)׆ (r-ރ):=by obtain⟨j,g,f,m,M,o⟩:=J5;rw[show(k-23:ٴ)=k-(κ߷:ٴ) by rw[j];rfl,Model.finishRoundedMantissa_normalized κ .positive r k (by rw[g];exact b) (by rw[f];exact d) (by rw[m];exact e)];erw[Bo κ false r k (by rw[g];exact b) (by rw[f];exact d) (by rw[m];exact e) (by rw[M];exact a)];have h:=QW (k+127)׆ (r-ރ);rwa[BB]at h lemma J6 (k:ٴ) (b:-126<=k) (a:k+1<=127):π (BT κ (Cn (ϓ κ) .positive (ނ,k-23)))=ϥ false (k+1+127)׆ 0:=by obtain⟨f,g,d,c,i,l⟩:=J5;rw[show(k-23:ٴ)=k-(Fmt߷:ٴ) by rw[f];rfl,show(ނ:ٳ)=Ct (κ߷+1)from d.symm,Model.finishRoundedMantissa_normalized_carry κ .positive k (by rwa[c])];erw[Bo κ false (Ct κ߷) (k+1) le_rfl (by rw[g,d];decide) (by rw[c];omega) (by rwa[i])];have e:=QW (k+1+127)׆ 0;rwa[BB]at e theorem A8 (b d:ٳ) (e:ٴ):sqW b d (Ϗ b d) e=π (BT κ (Bp (ϓ κ) .positive (Ϗ b d) e (sqrtAccuracy b d))):=by have l:=A3 b d;unfold sqW;generalize Ϗ b d=c at l⊢;have j:(ϓ κ).mantissaBits=24:=ד;have k:(ϓ κ).minExponent= -149:=ד;by_cases u:c<ނ<;>simp only[u,ר,ש,CP,add_zero,EU] all_goals split;swap;rfl;rename_i v;obtain⟨w,x,A,y,z⟩:=v;have n:sqA b d c<4:=by unfold sqA;split_ifs<;>omega generalize sqA b d c=a at l n⊢;rw[l];have g:=Model.roundWithAccuracy_quotient_eq_finishRoundedMantissa (ϓ κ) .positive (4*c+a) 4 e ב;rw[show(4*c+a)/4=c by omega,show(4*c+a)%4=a by omega]at g;have f:c≠0:=א . have o:23<=c܀:=(CH f).2 א;have p:c܀<24:=(Ϻ f).2 א;have h:((ϓ κ).targetExponent (Bb c e)-e)׆=0:=by simp only[BH,Bb,j,k];omega rw[g];dsimp only;rw[h];simp only[pow_zero,mul_one,CP,add_zero];obtain⟨q,s⟩:=J9 (4*c+a) 4;generalize AP (4*c+a) 4=r at q s⊢;rw[show e=(e+23)-23 by ring];split;next t=>subst t;rw[J6 (e+23) א א];congr 2;omega ·rw[J4 r (e+23) א א א א];congr 2;omega . have o:24<=c܀:=(CH f).2 א;have p:c܀<25:=(Ϻ f).2 א;have h:((ϓ κ).targetExponent (Bb c e)-e)׆=1:=by simp only[BH,Bb,j,k];omega rw[g];dsimp only;rw[h];simp only[pow_one,EU];obtain⟨q,s⟩:=J9 (4*c+a) (4*2);generalize AP (4*c+a) (4*2)=r at q s⊢;rw[show e+1=(e+24)-23 by ring];split;next t=>subst t;rw[J6 (e+24) א א];congr 2;omega ·rw[J4 r (e+24) א א א א];congr 2;omega lemma N1 (a m:ٳ) (h:ٴ) (q:ℚ) (d:0Ac q)=Ac q:=by unfold YaD;simp only[A8];generalize (47-((a܀:ٳ):ٴ)+(m܀:ٳ)+1)/2=t;have e:∀k:ٳ,(2:ℚ)^(2*k)=4^(k:ٴ):=λk=>by rw[pow_mul,ؠ];norm_num by_cases g:0<=t<;>simp only[g,ר,ש]<;>split_ifs with j<;>try rfl all_goals refine Zc4 q _ _ _ d (by first|exact b|rw[Α];exact ϊ b ה) ?_ (by rw[(ב:(2:ٳ)^23=ރ)];exact j);rw[<-f,(by ring: -(h-t)=t-h),zpow_sub₀ ב];simp only[Α];push_cast;rw[e,Cj א];field_simp[zpow_neg] all_goals rw[mul_assoc,<-zpow_add₀ ב];simp lemma Zc7 (w:δ):(sv w:ℚ)^2=(ε w:ℚ)^2:=by unfold sv;split_ifs<;>push_cast<;>ring lemma N0 (a:δ) (b:0<ς a):ς a=(ε a:ℚ)*2^((γ a:ℤ)-149):=by simp only[ς,ϙ]at b⊢;unfold sv at b⊢;split_ifs at b⊢with d;have:(0:ℚ)<=2^((γ a:ٴ)-149):=ה;push_cast at b;nlinarith[(ה:(0:ℚ)<=ε a)];push_cast;ring lemma M9 (x:ؽ δ) (n r a:ٳ) (b:∀ja<=γ x[r*n+j]!):(limbValA (Y6 x (r*n) n a):ℚ)*2^((2*a:ℤ)-298)=((ϒ n).map λj=>(ς x[r*n+j]!)^2).sum:=by unfold Y6;rw[limbValA_eq,(lS (λj=>ε x[r*n+j]! *ε x[r*n+j]!) (λj=>2*(γ x[r*n+j]! -a)) (λj=>by have:=Ϛ x[r*n+j]!;nlinarith) (λj=>by have:=iB x[r*n+j]!;omega) _ _ ג).2,show ϋ (Χ 18 0)܍=0 from ד,Β,Nat.cast_list_sum,Df,<-Dg];congr 1;apply Ck;intro j f;simp only[Function.comp,ς,ϙ];by_cases d:ε x[r*n+j]! =0;. simp[d,sv] . have k:((2:ℚ)^((γ x[r*n+j]!:ٴ)-149))^2=(2:ℚ)^(2*(γ x[r*n+j]! -a))*2^((2*a:ٴ)-298):=by rw[<-ؠ (2:ℚ) (2*(γ x[r*n+j]! -a)),<-zpow_add₀ ב,<-ؠ _ 2,<-zpow_mul];congr 1;push_cast[b j (BM.1 f) d];ring rw[mul_pow,Zc7,k];push_cast;ring lemma M8 (x:ؽ δ) (n r a:ٳ) (b:∀ja<=γ x[r*n+j]!):(((limbValA (Y7 x (r*n) n a).1:ℤ)-limbValA (Y7 x (r*n) n a).2:ℤ):ℚ)*2^((a:ℤ)-149)=((ϒ n).map λj=>ς x[r*n+j]!).sum:=by unfold Y7;rw[limbValA_eq,limbValA_eq,(lP x (r*n) a _ (λ_ _=>rfl) (ϒ n) _ _ ג ג).2.2,show ϋ (Χ 18 0)܍=0 from ד];push_cast;rw[zero_add,Df,<-Dg];congr 1;apply Ck;intro j f;simp only[Function.comp,ς,ϙ];push_cast;by_cases d:ε x[r*n+j]! =0;. simp[d,sv] . rw[mul_assoc,<-ؠ,<-zpow_add₀ ב];push_cast[b j (BM.1 f) d];ring lemma sm (f:ٳ->ℚ) (μ:ℚ) (l:List ٳ):(l.map λj=>(f j-μ)^2).sum=(l.map λj=>f j^2).sum-2*μ*(l.map f).sum+l.length*μ^2:=by induction l;simp;next j l a=>simp only[CR,DV,Ex,a];push_cast;ring lemma Z2 (x:ؽ δ) (d n j:ٳ) (e:jif ε x[d+j]! =0 then b else min b (γ x[d+j]!)) b<=b/\∀k∈l,ε x[d+k]!≠0->l.foldl (λb j=>if ε x[d+j]! =0 then b else min b (γ x[d+j]!)) b<=γ x[d+k]! from(H _ 255).2 j (BM.2 e) f intro l;induction l with|nil=>intro b;simp|cons a l p=>intro b;simp only[AO];obtain⟨h,q⟩:=p (if ε x[d+a]! =0 then b else min b (γ x[d+a]!));refine⟨Cp h (by split_ifs<;>omega),λk g o=>?_⟩;rcases List.mem_cons.1 g with rfl|g;exacts[Cp h (by simp[o]),q k g o] lemma Zc9 (a b:ٳ):YF a b<=2*a/\YF a b<=b+149 /\ (4:ℚ)^(((YF a b:ℤ)-298)/2)=2^((YF a b:ℤ)-298):=by unfold YF;refine⟨א,א,?_⟩;rw[show(4:ℚ)=2^(2:ٴ)from ב,<-zpow_mul];congr 1;omega lemma M7 (A n a b d:ٳ) (e:0(ς x[r*n+j]! -0)^2)];simp;rw[g] have o:0<ϫ n x r 0+ς a:=by rw[h];positivity refine N1 _ _ _ _ o b ?_;rw[h,N0 a d];exact M7 A n e (γ a) (ε a) b lemma M6 (n:ٳ) (x:ؽ δ) (r:ٳ) (μ:ℚ):0<=ϫ n x r μ:=by unfold ϫ;rw[fS,zero_add];exact div_nonneg (List.sum_nonneg λa b=>by obtain⟨j,_,rfl⟩:=EZ.1 b;positivity) ה lemma V1 (x:ؽ δ) (n:ٳ) (a:δ) (r:ٳ) (b:0(ς x[r*n+j]!-rowMean n x r)^2),zero_add,sm,CM]:ϫ n x r (rowMean n x r)=(((ϒ n).map λj=>(ς x[r*n+j]!)^2).sum-2*rowMean n x r*((ϒ n).map λj=>ς x[r*n+j]!).sum+n*rowMean n x r^2)/n),(by unfold rowMean;rw[fS (λj=>ς x[r*n+j]!),zero_add]:rowMean n x r=((ϒ n).map λj=>ς x[r*n+j]!).sum/n),<-f,<-g,show(2:ℚ)^((2*e:ٴ)-298)=(2^((e:ٴ)-149))^2 by rw[<-ؠ _ 2,<-zpow_mul];congr 1;ring];push_cast;field_simp;ring rw[M7 _ (n*n) e (γ a) (ε a) (ϊ b b),h,show((((n:ٴ)*A-S1*S1)׆:ٳ):ℚ)=(((n:ٴ)*A-S1*S1:ٴ):ℚ) by exact_mod_cast Cj (by have:=M6 n x r (rowMean n x r);rw[h]at this;exact_mod_cast nonneg_of_mul_nonneg_left this (div_pos ה (by exact_mod_cast ϊ b b)):0<=(n:ٴ)*A-S1*S1)];push_cast;ring lemma V0 (x:ؽ δ) (n:ٳ) (a:δ) (r:ٳ) (b:0 ℚ):δ:= if sv dw<=0 then roundRational ((ς w-mean ())*ς gw/ς dw+ς bw) else let c2:=min (b0+γ gw) (γ bw+γ dw) let P:=((n:ٴ)*YaF w b0-S1)*sv gw<<<(b0+γ gw-c2) + (n:ٴ)*sv bw*sv dw<<<(γ bw+γ dw-c2) AW (P<0) P.natAbs (n*ε dw) ((c2:ٴ)-149-γ dw) section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats lemma eD (d:δ) (h:¬sv d<=0):sv d=ε d/\0 < ε d:=by have a:η d=false:=by by_contra c;simp at c;exact h (by simp[sv,c]) have:sv d=ε d:=by simp[sv,a] exact⟨this,by omega⟩ lemma eP (P:ℤ):(if decide (P<0)=true then -((P.natAbs:ℕ):ℚ) else (P.natAbs:ℚ))=P:=by rw[EA];split_ifs with c<;>simp_all[abs_of_neg,abs_of_nonneg] lemma U9 (w a b:δ):elR w a b=roundRational (ς w*ς a/ς b):=by unfold elR;split_ifs with h;rfl;obtain⟨e,f⟩:=eD b h;refine(N3 _ _ _ _ _ f (λp=>by simp at p;omega) ?_).symm;simp only[ς,ϙ,e];rw[eP];have d:(2:ℚ)≠0:=ג;rw[show((γ w:ٴ)+γ a-γ b-149)=((γ w:ٴ)-149)+((γ a:ٴ)-149)-((γ b:ٴ)-149) by ring,zpow_sub₀ d (((γ w:ٴ)-149)+((γ a:ٴ)-149)) ((γ b:ٴ)-149),zpow_add₀ d ((γ w:ٴ)-149) ((γ a:ٴ)-149)];push_cast;field_simp lemma U8 (n:ٳ) (w e f:δ) (b:ٳ) (g:ٴ) (d:δ) (a:Unit->ℚ) (j:0b<=γ w) (l:a ()=(g:ℚ)*2^((b:ٴ)-149)/n) : elL n w e f b g d a=roundRational ((ς w-a ())*ς e/ς d+ς f):=by unfold elL;split_ifs with h;·rfl obtain⟨r,s⟩:=eD d h;refine(N3 _ _ _ _ _ (ϊ j s) (λp=>by simp at p;omega) ?_).symm;simp only[ς,ϙ,l];rw[eP];generalize q:min (b+γ e) (γ f+γ d)=c;have t:=q▸min_le_left _ _;have u:=q▸min_le_right _ _;simp only[YaF,CV,r];push_cast;have o:(2:ℚ)≠0:=ג;have v:(sv w:ℚ)*2^((γ w:ٴ)-149)=sv w*2^(γ w-b)*2^((b:ٴ)-149):=by by_cases m:ε w=0;·simp[sv,m] rw[mul_assoc,<-ؠ,<-zpow_add₀ o];push_cast[k m];ring have x:(2:ℚ)^((b:ٴ)-149)*2^((γ e:ٴ)-149)=2^((c:ٴ)-149-γ d)*2^((γ d:ٴ)-149)*2^(b+γ e-c):=by rw[<-ؠ,<-zpow_add₀ o,<-zpow_add₀ o,<-zpow_add₀ o];push_cast[t];ring have y:(2:ℚ)^((γ f:ٴ)-149)=2^((c:ٴ)-149-γ d)*2^(γ f+γ d-c):=by rw[<-ؠ,<-zpow_add₀ o];congr 1;push_cast[u];ring rw[v,y];field_simp;linear_combination (((n:ℚ)*sv w*2^(γ w-b)-g)*sv e)*x end @[inline] def p2 (k:ٳ):ٳ:=if k<63 then ((1:ػ)<< ف | .ofNat s=>aa*p2 (s+1) ف | .ofNat s=>a>=b*p2 s | .negSucc s=>a*p2 (s+1)>=b @[inline] def fl2 (a b:ٳ):ٴ:= let i:ٴ:=BG (lg a)-BG (lg b);let l:=if lt2 a b i then i-1 else i;if ge2 a b (l+1) then l+1 else l @[inline] def sc2 (a b:ٳ):ٴ -> ٳ × ٳ | .ofNat s=>(a*p2 s,b) | .negSucc s=>(a,b*p2 (s+1)) @[inline] def YN (s:ف) (N D:ٳ) (E:ٴ):ؼ:= if D==0 then 0x7fc00000 else if N==0 then if s then ߾ else 0 else let r:=fl2 N D;let t:=r+E;if t>127 then if s then 0xff800000 else 0x7f800000 else if t < -150 then if s then ߾ else 0 else if t < -126 then let c:=sc2 N D (E+149);let f:=AP c.1 c.2;if f==0 then if s then ߾ else 0 else match Nat.decLe ރ f with | isTrue _=>ϥ s 1 0 | isFalse _=>ϥ s 0 f else let c:=sc2 N D (23-r);let m:=AP c.1 c.2;let k:=m==ނ;let x:=if k then t+1 else t;let y:=if k then ރ else m;if x>127 then if s then 0xff800000 else 0x7f800000 else ϥ s (Fe (x+127)) (y-ރ) @[inline] def Yap (sa:ف) (a:ٳ) (sb:ف) (b:ٳ):ف × ٳ:= if sa==sb then (sa,a+b) else if b<=a then (sa,a-b) else (sb,b-a) @[inline] def Ya7 (w gw dw:δ):δ:= if sv dw<=0 then roundRational (ς w*ς gw/ς dw) else let Pn:=ε w*ε gw;YN (η w != η gw&&Pn != 0) Pn (ε dw) ((γ w:ٴ)+γ gw-γ dw-149) @[inline] def Y3 (s1:ف) (q1 r1:ٳ) (s2:ف) (B D:ٳ):ف × ٳ × ٳ:= if s1==s2 then (s1,q1+B,r1) else if B<=q1 then (s1,q1-B,r1) else if r1=0 then (s2,B-q1,0) else (s2,B-q1-1,D-r1) @[inline] def Ya8 (s:ف) (M:ػ) (E:ٴ):ؼ:= let L:=Yj M;let t:=(Lپ:ٴ)+E;if M=0\/L<24\/t < -126 then YN s Mپ 1 E else let m:=(YK M L)˧;let k:=m==ނ;let x:=if k then t+1 else t;let y:=if k then ރ else m;if x>127 then if s then 0xff800000 else 0x7f800000 else ϥ s (Fe (x+127)) (y-ރ) @[inline] def Ya9 (s:ف) (M:ٳ) (E:ٴ):ؼ:=if M>>>62=0 then Ya8 s M߸ E else YN s M 1 E lemma ZE (k:ٳ):p2 k=2^k:=by unfold p2;split;next h=>have a:k߸˧=k:=CG (by unfold Fd;omega);rw[ό,a,ν (א:k<64),Da,Nat.one_shiftLeft];exact ν (DJ ב א) rfl lemma U7 (n:ٳ):lg n=n܀:=by unfold lg;dsimp only;split;rename_i h;by_cases a:n߸=0;have:n=0:=by rw[<-h,a];rfl subst this;decide;rw[Zj _ a,h];rfl lemma U6 (a b:ٳ) (e:ٴ):lt2 a b e=Bi a b e:=by cases e<;>simp[lt2,Bi,ZE,Α] lemma U5 (a b:ٳ) (e:ٴ):ge2 a b e=Bk a b e:=by cases e<;>simp[ge2,Bk,ZE,Α] lemma U4:sc2=Bl:=by funext a b e;cases e<;>simp[sc2,Bl,ZE,Α] lemma M5:fl2=Br:=by funext a b;simp only[fl2,Br,U7,U6,U5] lemma G2 (s:ف) (N D:ٳ) (E:ٴ):YN s N D E=AW s N D E:=by unfold YN;rw[M5,U4];rfl lemma U3 (s:ف) (M:ػ) (E:ٴ):Ya8 s M E=YN s M˧ 1 E:=by unfold Ya8;dsimp only;split;rfl;rename_i h;simp only[not_or,EO]at h;obtain⟨a,k,o⟩:=h;have d:M˧≠0:=λe=>a (σ.mp (by simpa using e));have f:=Zj M a;have p:24<=M˧܀:=by rw[<-f];exact ϐ.1 k obtain⟨g,_,_⟩:=Z7 M a;rw[if_pos (by show 23<=_;omega:κ߷<=M˧܀)]at g;rw[g,BQ,f,show κ߷=23 from rfl];unfold YN;rw[M5,Model.floorLog2_den_one _ d];simp only[show((1:ٳ)==0)=false from rfl,show(M˧==0)=false by simpa using d,(א:(23:ٴ)-(M˧܀:ٴ)=CJ (M˧܀-24)),sc2,ZE,φ,ש,Ϙ,(א:M˧܀-24+1=M˧܀-23)];by_cases j:(M˧܀:ٴ)+E>127;simp only[j,(by intro b;split<;>omega:∀b:ف,(if b=true then (M˧܀:ٴ)+E+1 else (M˧܀:ٴ)+E)>127),ר];simp only[j,ש,show¬(M˧܀:ٴ)+E< -150 by omega,show¬(M˧܀:ٴ)+E< -126 by omega] lemma U2 (s:ف) (M:ٳ) (E:ٴ):Ya9 s M E=YN s M 1 E:=by unfold Ya9;split;next h=>rw[U3];congr 1;simp[Nat.toUInt64];omega rfl def Yh (s:ف) (m:ٳ):ٴ:=if s then -(m:ٴ) else m lemma U1 (d:ف) (a:ٳ) (e:ف) (b:ٳ):Yh (Yap d a e b).1 (Yap d a e b).2=Yh d a+Yh e b:=by unfold Yap;by_cases g:d=e;subst g;cases d<;>simp[Yh];omega;rw[if_neg (by simpa using g)];by_cases f:b<=a;rw[if_pos f];cases d<;>cases e<;>simp_all[Yh,ϗ f]<;>omega;rw[if_neg f];have h:a<=b:=א;cases d<;>cases e<;>simp_all[Yh,ϗ h]<;>omega lemma M4 (s:ف) (m:ٳ):(Yh s m).natAbs=m:=by cases s<;>simp[Yh] lemma M3 (s:ف) (m:ٳ):decide (Yh s m<0)=(s&&m != 0):=by cases s;simp[Yh];by_cases h:m=0;simp[Yh,h];have:(0:ٴ)< m:=Nat.cast_pos.2 (Ο h);simp only[Yh,ר,Left.neg_neg_iff,this,decide_true,Bool.true_and];simpa using h lemma ZF (w:δ):sv w=Yh (η w) (ε w):=rfl lemma U0 (s:ف) (a b:ٳ):Yh s (a*b)=a*Yh s b:=by cases s<;>simp[Yh] lemma Z3 (s:ف) (a b:ٳ):Yh s (a*b)=Yh s a*b:=by cases s<;>simp[Yh] lemma M2 (s t:ف) (a b:ٳ):Yh (s != t) (a*b)=Yh s a*Yh t b:=by cases s<;>cases t<;>simp[Yh] lemma T9 (S:ٴ):Yh (!decide (S<0)) S.natAbs = -S:=by unfold Yh;by_cases h:S<0<;>simp[h]<;>omega lemma T8 (w a b:δ):Ya7 w a b=elR w a b:=by unfold Ya7 elR;split;rfl;simp only[G2,show sv w*sv a=Yh (η w !=η a) (ε w*ε a) by rw[M2,ZF,ZF],M4,M3] lemma T7 (a:ف) (b d:ٳ) (f:ف) (B D:ٳ) (g:dsimp only[Yh]<;>simp<;>ring;obtain rfl:f=!a:=by cases a<;>cases f<;>simp_all rw[if_neg (by cases a<;>simp)];by_cases h:B<=b;rw[if_pos h];refine⟨?_,g⟩;cases a<;>simp only[Yh]<;>simp<;>push_cast[ϗ h]<;>ring;rw[if_neg h];by_cases j:d=0;rw[if_pos j];have k:b<=B:=א;refine⟨?_,by dsimp only;omega⟩;cases a<;>simp only[Yh,j]<;>simp<;>push_cast[ϗ k]<;>ring;rw[if_neg j];have k:b+1<=B:=א;have n:d<=D:=א;refine⟨?_,by dsimp only;omega⟩;cases a<;>simp only[Yh]<;>simp<;>push_cast[ϗ k,ϗ n,Nat.sub_sub]<;>ring lemma T6 (t k D:ٳ):(t/D*k+t%D*k/D)*D+t%D*k%D=t*k:=by rw[ϟ,BU,϶,Nat.mul_right_comm,<-ϟ,϶] lemma J8 (a m D:ٳ):a*m/D=a/D*m+a%D*m/D/\a*m%D=a%D*m%D:=by rcases Ae D with rfl|b;simp;rw[show a*m=a%D*m+D*(a/D*m) by rw[<-ϣ,<-ϟ,Ez],DG _ _ b,CF];exact⟨Ϟ _ _,rfl⟩ lemma gO {α:Type} [Inhabited α] (n:ٳ) (f:ٳ->α) (k:ٳ) (a:kf i܂)[k]! = f k:=ח lemma M1 (a n:ٳ) (b:δ) (x g:ؽ δ) (h:stableNormDomain a n b x g=true):0ؼ) (m:ػ):∀k t (P:ؽ ٳ),n-t=k->(QPD n f m t P)܍=P܍ ++ (Bd (2*t) (2*(n-t))).map λu=>QPb (f (u/2)) m (u%2=1):=by intro k;induction k<;>intro t P h<;>unfold QPD ·rw[if_neg א];simp[h] rename_i k a;rw[if_pos א];dsimp only;have e:n-(t+1)=k:=א;rw[a (t+1) _ e,h,e,Nat.mul_succ 2 k,Ϩ,Ϩ];simp[Cf,(א:2*(t+1)=2*t+1+1)] lemma D2 (n c:ٳ) (f:ٳ->ؼ) (m:ػ):QPD n f m 0 (Ba c)=ڀ (2*n) λu=>QPb (f (u܂/2)) m (u܂%2=1):=by apply ϻ;rw[J1 n f m _ 0 _ rfl];apply ϩ<;>simp noncomputable def MYba (n:ٳ) (w gw bw:δ) (b0:ٳ) (S1:ٴ) (dw:δ) (mean:Unit -> ℚ):δ := if sv dw <= 0 then roundRational ((ς w-mean ())*ς gw/ς dw+ς bw) else let c2:=min (b0+γ gw) (γ bw+γ dw) let c:=Yap (η w) (n*(ε w*p2 (γ w-b0))) (!(S1 < 0)) S1.natAbs let D:=n*ε dw let t1:=c.2*ε gw let k1:=p2 (b0+γ gw-c2) let ra:=t1%D*k1 let Q:=Y3 (c.1 != η gw) (t1/D*k1+ra/D) (ra%D) (η bw) (ε bw*p2 (γ bw+γ dw-c2)) D if 16777216 <= Q.2.1 then Ya9 Q.1 (2*Q.2.1+(if Q.2.2=0 then 0 else 1)) ((c2:ٴ)-150-γ dw) else let N:=Q.2.1*D+Q.2.2 YN (Q.1&&N != 0) N D ((c2:ٴ)-149-γ dw) lemma Zdf (n:ٳ):lg n=n܀:=by unfold lg;dsimp only;split;rename_i h;by_cases h0:n߸=0;have:n=0:=by rw[<-h,h0];rfl subst this;decide;rw[Zj _ h0,h];rfl lemma Zbo:fl2=Br:=by funext a b;simp only[fl2,Br,Zdf,U6,U5] lemma Zdr (n:ٳ) (w gw bw:δ) (b0:ٳ) (S1:ٴ) (dw:δ) (mean:Unit->ℚ) (hn:0 < n) : MYba n w gw bw b0 S1 dw mean=elL n w gw bw b0 S1 dw mean:=by unfold MYba elL;split;rfl;rename_i d;have a:η dw=false:=by by_contra c;simp at c;exact d (by simp[sv,c]) have b:0<ε dw:=by by_contra c;exact d (by simp[sv,a];omega) simp only[U2];generalize min (b0+γ gw) (γ bw+γ dw)=z;generalize f:Yap (η w) (n*(ε w*p2 (γ w-b0))) (!decide (S1<0)) S1.natAbs=c;have p:∀e,(2:ٴ)^e=((p2 e:ٳ):ٴ):=λe=>by rw[ZE];push_cast;rfl rw[(by rw[<-f,U1,U0,Z3,T9,YaF,CV,ZF,ZE];push_cast;ring:(n:ٴ)*YaF w b0-S1=Yh c.1 c.2),(by simp[sv,a]:sv dw=ε dw),ZF gw,ZF bw,CV,CV,p,p];generalize p2 (b0+γ gw-z)=k;generalize p2 (γ bw+γ dw-z)=l;have g:=ϊ hn b;rw[show Yh c.1 c.2*(Yh (η gw) (ε gw)*(k:ٴ))+n*Yh (η bw) (ε bw)*(ε dw*(l:ٴ))=Yh (c.1 !=η gw) (c.2*ε gw*k)+Yh (η bw) (ε bw*l*(n*ε dw)) by rw[Z3,M2,Z3,Z3];push_cast;ring];generalize c.2*ε gw=t;generalize ε bw*l=B;generalize n*ε dw=D at g⊢;obtain⟨v,r⟩:=T7 (c.1 !=η gw) (t/D*k+t%D*k/D) (t%D*k%D) (η bw) B D (Ε _ g);rw[T6]at v;rw[<-v,M4,M3];generalize Y3 (c.1 !=η gw) (t/D*k+t%D*k/D) (t%D*k%D) (η bw) B D=Q at r⊢;split;next q=>have h:(Q.2.1*D+Q.2.2)/D=Q.2.1:=(by rw[Ϟ,ϔ _ _ g,Υ r,Β]);have i:=Zc3 Q.1 (Q.2.1*D+Q.2.2) D ((z:ٴ)-149-γ dw) g (by rw[h];exact q);rw[h,(by rw[Ϟ,ϲ,ν r]:(Q.2.1*D+Q.2.2)%D=Q.2.2),(א:(z:ٴ)-149-γ dw-1=(z:ٴ)-150-γ dw)]at i;rw[G2,i,(ו:(Q.2.1*D+Q.2.2 !=0)=true),Bool.and_true] rw[G2] noncomputable def elS (n:ٳ) (w gw bw:δ) (b0:ٳ) (S1:ٴ) (dw:δ) (mean:Unit -> ℚ):δ := if sv dw <= 0 then roundRational ((ς w-mean ())*ς gw/ς dw+ς bw) else let c2:=min (b0+γ gw) (γ bw+γ dw) let c:=Yap (η w) (n*(ε w*p2 (γ w-b0))) (!(S1 < 0)) S1.natAbs let D:=n*ε dw let r0:=c.2%D*ε gw let k1:=p2 (b0+γ gw-c2) let ra:=r0%D*k1 let Q:=Y3 (c.1 != η gw) ((c.2/D*ε gw+r0/D)*k1+ra/D) (ra%D) (η bw) (ε bw*p2 (γ bw+γ dw-c2)) D if 16777216 <= Q.2.1 then Ya9 Q.1 (2*Q.2.1+(if Q.2.2=0 then 0 else 1)) ((c2:ٴ)-150-γ dw) else let N:=Q.2.1*D+Q.2.2 YN (Q.1&&N != 0) N D ((c2:ٴ)-149-γ dw) lemma elS_eq (n:ٳ) (w gw bw:δ) (b0:ٳ) (S1:ٴ) (dw:δ) (mean:Unit->ℚ): elS n w gw bw b0 S1 dw mean=MYba n w gw bw b0 S1 dw mean:=by unfold elS MYba;dsimp only;generalize Yap (η w) (n*(ε w*p2 (γ w-b0))) (!decide (S1<0)) S1.natAbs=c;generalize n*ε dw=D;rw[(J8 c.2 (ε gw) D).1,(J8 c.2 (ε gw) D).2] @[inline] def u2a (h l t:ػ):ػ×ػ:=let l':=l+t;(h+(if l' < l then 1 else 0),l') lemma u2a_v (h l t:ػ) (hb:h˧*18446744073709551616+l˧+t˧ < 340282366920938463463374607431768211456) : (u2a h l t).1˧*18446744073709551616+(u2a h l t).2˧=h˧*18446744073709551616+l˧+t˧:=by unfold u2a;dsimp only;have hl:=l܉;have ht:=t܉;have hh:=h܉;by_cases c:l˧+t˧< 18446744073709551616 . have e:(l+t)˧=l˧+t˧:=by rw[ξ];exact ν c have nc:¬(l+t< l):=by rw[Ψ,e];omega simp only[nc,if_false,e];rw[ξ];simp;omega . have e:(l+t)˧=l˧+t˧-18446744073709551616:=by rw[ξ];omega have cc:l+t< l:=by rw[Ψ,e];omega simp only[cc,if_true,e];rw[ξ];simp;omega lemma qsplit (m s:ٳ) (hm:m < 2^24) (hs:s <= 36) : (if 2*s߸ < 64 then ((m߸*m߸)>>>1)>>>(63-2*s߸) else (m߸*m߸)<<<(2*s߸-64))˧*18446744073709551616+(if 2*s߸ < 64 then (m߸*m߸)<<<(2*s߸) else 0)˧=m*m*2^(2*s):=by have hq:m*m<2^48:=by nlinarith have eq:(m߸*m߸)˧=m*m:=by rw[ϯ,CG (by unfold Fd;omega)];exact ν א have es:(2*s߸)˧=2*s:=by rw[ϯ,CG (by unfold Fd;omega)];simp;omega have h64:(64:ػ)˧=64:=rfl;split_ifs with c<;>rw[Ψ,es,h64]at c . have e63:(63-2*s߸)˧=63-2*s:=by rw[Η _ _ (by rw[ϐ,es];show 2*s<=63;omega),es];rfl rw[Ξ,Ξ,ό,eq,es,e63];simp only[ψ,Α,show (1:ػ)˧=1 from rfl];rw[ν (א:63-2*s<64),ν c,ϧ,show m*m/(2^(1%64)*2^(63-2*s))=m*m*2^(2*s)/2^64 by rw[show (2:ٳ)^64=(2^(1%64)*2^(63-2*s))*2^(2*s) by rw[<-Κ,<-Κ];congr 1;omega,Nat.mul_div_mul_right _ _ ה]];omega . rw[ό,eq,show(2*s߸-64)˧=2*s-64 by rw[Η _ _ (by rw[ϐ,es,h64];omega),es,h64],Α,ν (א:2*s-64<64),ν (show m*m*2^(2*s-64)<2^64 by have:=Ι (ב:0<2) (א:2*s-64<=8);have:=Aj hq (Δ (2*s-64));omega),show (2:ٳ)^(2*s)=2^(2*s-64)*2^64 by rw[<-Κ];congr 1;omega];norm_num;ring noncomputable def rsU (x:ؽ δ) (base n b0 j:ٳ) (ph pl nh nl qh ql:ػ) (ok:ف):ف×ٳ×ٳ×ٳ:= if j < n then let w:=x[base+j]! let m:=(ε w)߸ let s:=(γ w-b0)߸ let t:=m<<>>1)>>>(63-2*s) else q<<<(2*s-64)) let ok':=ok&&(ε w=0||γ w-b0 <= 36) if η w then let p:=u2a nh nl t; rsU x base n b0 (j+1) ph pl p.1 p.2 qh' a.2 ok' else let p:=u2a ph pl t; rsU x base n b0 (j+1) p.1 p.2 nh nl qh' a.2 ok' else (ok&&n<256,ph˧<<<64+pl˧,nh˧<<<64+nl˧,qh˧<<<64+ql˧) termination_by n-j lemma rsU_ok (x:ؽ δ) (base n b0:ٳ):∀k j ph pl nh nl qh ql ok, n-j=k -> (rsU x base n b0 j ph pl nh nl qh ql ok).1=true->ok=true /\ n < 256 /\ ∀t,j <= t->t < n->(ε x[base+t]! = 0 \/ γ x[base+t]!-b0 <= 36):=by intro k;induction k with |zero=>intro j _ _ _ _ _ _ _ _ h;unfold rsU at h;rw[if_neg א]at h;simp at h;exact⟨h.1,h.2,λt h1 h2=>א⟩ |succ k ih=> intro j _ _ _ _ _ _ _ _ h;unfold rsU at h;rw[if_pos א]at h;dsimp only at h split at h all_goals obtain⟨o1,o2,o3⟩:=ih _ _ _ _ _ _ _ _ א h;simp only[Ω,Ϸ,ת]at o1;refine⟨o1.1,o2,λt h1 h2=>?_⟩;rcases Ap h1 with h1|rfl;exact o3 t א h2;exact o1.2 lemma rsU_val (x:ؽ δ) (base n b0:ٳ):∀k j ph pl nh nl qh ql ok, n-j=k -> (∀t,j <= t->t < n->(ε x[base+t]! = 0 \/ γ x[base+t]!-b0 <= 36)) -> ph˧*18446744073709551616+pl˧+((Bd j (n-j)).map λt=>if η x[base+t]! then 0 else ε x[base+t]!*2^(γ x[base+t]!-b0)).sum < 340282366920938463463374607431768211456 -> nh˧*18446744073709551616+nl˧+((Bd j (n-j)).map λt=>if η x[base+t]! then ε x[base+t]!*2^(γ x[base+t]!-b0) else 0).sum < 340282366920938463463374607431768211456 -> qh˧*18446744073709551616+ql˧+((Bd j (n-j)).map λt=>ε x[base+t]!*ε x[base+t]!*2^(2*(γ x[base+t]!-b0))).sum < 340282366920938463463374607431768211456 -> (rsU x base n b0 j ph pl nh nl qh ql ok).2.1=ph˧*18446744073709551616+pl˧+((Bd j (n-j)).map λt=>if η x[base+t]! then 0 else ε x[base+t]!*2^(γ x[base+t]!-b0)).sum /\ (rsU x base n b0 j ph pl nh nl qh ql ok).2.2.1=nh˧*18446744073709551616+nl˧+((Bd j (n-j)).map λt=>if η x[base+t]! then ε x[base+t]!*2^(γ x[base+t]!-b0) else 0).sum /\ (rsU x base n b0 j ph pl nh nl qh ql ok).2.2.2=qh˧*18446744073709551616+ql˧+((Bd j (n-j)).map λt=>ε x[base+t]!*ε x[base+t]!*2^(2*(γ x[base+t]!-b0))).sum:=by intro k;induction k<;>intro j ph pl nh nl qh ql ok hk hg bP bN bQ ·unfold rsU;rw[if_neg א];simp[hk,Α] rename_i k ih;simp only[hk,Ϩ,CR,DV]at bP bN bQ⊢;obtain rfl:n-(j+1)=k:=א;unfold rsU;rw[if_pos א];dsimp only;have hgj:=hg j le_rfl א;generalize x[base+j]! =w at hgj bP bN bQ⊢;have hm:=Ϛ w;have:=iB w;have em:(ε w)߸˧=ε w:=CG (by unfold Fd;omega);have es:(γ w-b0)߸˧=γ w-b0:=CG (by unfold Fd;omega);have et:((ε w)߸<<<(γ w-b0)߸)˧=ε w*2^(γ w-b0):=hgj.elim (λh0=>by rw[h0];simp) λh=>by rw[ό,em,es,Α,ν (א:γ w-b0<64)];exact ν (Ag (Nat.mul_lt_mul_of_lt_of_le hm (Ι ב h) ה) ב) have eq2:=hgj.elim (λh0=>by rw[h0];simp) (qsplit _ _ hm);generalize (ε w)߸=E,(γ w-b0)߸=S at et eq2⊢;generalize (if 2*S<64 then (E*E)<<<(2*S)else 0)=LO,(if 2*S<64 then ((E*E)>>>1)>>>(63-2*S)else (E*E)<<<(2*S-64))=HI at eq2⊢;have ea:=u2a_v qh ql LO (by clear bP bN;omega);generalize u2a qh ql LO=A at ea⊢;have eqh:(A.1+HI)˧=A.1˧+HI˧:=by rw[ξ];apply ν;clear bP bN;omega by_cases hs:η w=true<;>simp only[hs,φ,ר,ש,Β]at bP bN⊢;. have ep:=u2a_v nh nl (E<<hg t א) bP (by rw[ep,et,BU];exact bN) (by rw[eqh];linarith);rw[r1,r2,r3];clear bP bN;omega . have ep:=u2a_v ph pl (E<<hg t א) (by rw[ep,et,BU];exact bP) bN (by rw[eqh];linarith);rw[r1,r2,r3];clear bP bN;omega lemma sumB (l:List ٳ) (f:ٳ->ٳ) (B:ٳ) (h:∀t ∈ l,f t <= B):(l.map f).sum <= l.length*B:=by have:=List.sum_le_card_nsmul (l.map f) B (λy hy=>by obtain⟨t,ht,rfl⟩:=EZ.1 hy;exact h t ht);simpa using this lemma sdif (l:List ٳ) (f g:ٳ->ٳ) (F:ٳ->ٴ) (h:∀t,(f t:ٴ)-g t=F t):((l.map f).sum:ٴ)-(l.map g).sum=(l.map F).sum:=by induction l with |nil=>simp |cons a l ih=>simp only[CR,DV];push_cast;have:=h a;push_cast at ih;linarith lemma rsU_lim (x:ؽ δ) (base n b0:ٳ) (h:(rsU x base n b0 0 0 0 0 0 0 0 true).1=true) : ((rsU x base n b0 0 0 0 0 0 0 0 true).2.1:ٴ)-(rsU x base n b0 0 0 0 0 0 0 0 true).2.2.1=(limbValA (Y7 x base n b0).1:ٴ)-limbValA (Y7 x base n b0).2 /\ (rsU x base n b0 0 0 0 0 0 0 0 true).2.2.2=limbValA (Y6 x base n b0):=by obtain⟨-,hn,g⟩:=rsU_ok x base n b0 _ 0 0 0 0 0 0 0 true rfl h;have P:∀t∈Bd 0 (n-0),ε x[base+t]!*2^(γ x[base+t]!-b0)<=2^60:=λt e=>by have:=List.mem_range'_1.1 e;rcases g t א א with e|e;simp[e];exact(Ar (Ϛ _).le (Ι ב e)).trans ב have A:=sumB _ (λt=>if η x[base+t]! then 0 else ε x[base+t]!*2^(γ x[base+t]!-b0)) (2^60) (λt e=>by split;exact Nat.zero_le _;exact P t e);have B:=sumB _ (λt=>if η x[base+t]! then ε x[base+t]!*2^(γ x[base+t]!-b0)else 0) (2^60) (λt e=>by split;exact P t e;exact Nat.zero_le _);have C:=sumB _ (λt=>ε x[base+t]!*ε x[base+t]!*2^(2*(γ x[base+t]!-b0))) (2^120) (λt e=>by have:=Ar (P t e) (P t e);rw[two_mul,pow_add];linarith);norm_num at A B C;obtain⟨a,b,c⟩:=rsU_val x base n b0 _ 0 0 0 0 0 0 0 true rfl (λt _ e=>g t א e) ו ו ו;have z:ϋ (Χ 18 0).toList=0:=ד;simp only[Ϝ,ϸ,Β,Ai,<-Ca]at a b c;constructor;rw[a,b];unfold Y7;rw[limbValA_eq,limbValA_eq,(lP x base b0 _ (λ_ _=>rfl) (ϒ n) _ _ ג ג).2.2,z];simp only[CP,sub_zero,zero_add];exact sdif _ _ _ _ (λt=>by simp only[sv];split_ifs<;>push_cast<;>ring);rw[c];unfold Y6;rw[limbValA_eq,(lS (λj=>ε x[base+j]! *ε x[base+j]!) (λj=>2*(γ x[base+j]! -b0)) (λj=>by have:=Ϛ x[base+j]!;nlinarith) (λj=>by have:=iB x[base+j]!;omega) _ _ ג).2,z,zero_add] def Y2L (x:ؽ δ) (base n j b:ٳ):ٳ:= if jY2L x base n j b=(Bd j d).foldl (λb j=>let w:=x[base+j]!;if ε w=0 then b else min b (γ w)) b by rw[H n 0 255 א,Ca] intro d;induction d<;>intro j b h;rw[Y2L,if_neg א];rfl;rename_i d i;rw[Y2L,if_pos א,i (j+1) _ א,Ϩ];rfl lemma Tn (n X:ٳ) (S:ٴ):((n:ٴ)*X-S*S)پ=n*X-S.natAbs*S.natAbs:=by rw[<-Int.natAbs_mul_self' S,<-Int.natCast_mul,<-Int.natCast_mul,Int.toNat_sub] @[inline] def m64 (a b:ػ):ػ×ػ:= let a0:=a&&&ހ;let a1:=a>>>32;let b0:=b&&&ހ;let b1:=b>>>32 let p00:=a0*b0;let p01:=a0*b1;let p10:=a1*b0;let p11:=a1*b1 let mid:=(p00>>>32)+(p01&&&ހ)+(p10&&&ހ) (p11+(p01>>>32)+(p10>>>32)+(mid>>>32),(mid<<<32)|||(p00&&&ހ)) @[inline] def lg128 (h l:ػ):ػ:=if h != 0 then 64+Yj h else Yj l @[inline] def shl128 (h l s:ػ):ػ×ػ:= (if s==0 then h else if s<64 then (h<<>>(64-s)) else l<<<(s-64),if s==0 then l else if s<64 then l<<>>s else 0,if s==0 then l else if s<64 then (l>>>s)|||(h<<<(64-s)) else h>>>(s-64)) @[inline] def lowz128 (h l s:ػ):ف:= if s==0 then true else if s<64 then l&&&((1<<>>32);let q1:=x1/m;let r:=x1%m let x0:=(r<<<32)|||(l&&&ހ);let q0:=x0/m;let r:=x0%m (qh,(q1<<<32)|||q0,r) noncomputable def absd (ph pl nh nl:ػ):ػ×ػ:= (if nh>>62 != 0 then ߿ else FY DQ.2.1˧ DQ.2.2˧ (((shr128 shh sl (2*k-2)߸).2&&&3)˧) (lowz128 shh sl (2*k-2)߸) m c t def FW (shh sl:ػ) (m c:ٳ):ػ:= let t:ٴ:=(47-((lg128 shh sl)˧:ٴ)+m܀+1)/2 if 0<=t||64<=(-t)׆ then ߿ else FX shh sl m c t (-t)׆ @[inline] def FTk {β:Type} (ph pl nh nl qh ql:ػ) (n:ٳ) (k:ػ -> ػ -> ف -> β):β:= let ge:=nh ػ -> ف -> β):β:= let bt:=if th==0&&tl==0 then 0 else lg128 th tl+1 let be:=if E==0 then 0 else Yj E+1 let T2:=shl128 th tl d1߸ let E2:=shl128 0 E d2߸ let sl:=T2.2+E2.2 let sh0:=T2.1+E2.1 k (sh0+(if sl if ok then let c:=YF b0 (γ eps) FNk th tl (2*b0-c) (γ eps+149-c) (n*n*ε eps)߸ λshh sl ok2=> if ok2 then FW shh sl (n*n) c else ߿ else ߿ def FFr (qh ql:ػ) (n b0:ٳ) (eps:δ):ػ:= if 0 if ok2 then FW shh sl n c else ߿ else ߿ @[specialize] def rsV {β:Type} (kont:ف -> ػ -> ػ -> ػ -> ػ -> ػ -> ػ -> β) (x:ؽ δ) (base n b0 j:ٳ) (ph pl nh nl qh ql:ػ) (ok:ف):β:= if j < n then let w:=x[base+j]! let m:=(ε w)߸ let s:=(γ w-b0)߸ let t:=m<<>>1)>>>(63-2*s) else q<<<(2*s-64)) let ok':=ok&&(ε w=0||γ w-b0 <= 36) if η w then let p:=u2a nh nl t; rsV kont x base n b0 (j+1) ph pl p.1 p.2 qh' a.2 ok' else let p:=u2a ph pl t; rsV kont x base n b0 (j+1) p.1 p.2 nh nl qh' a.2 ok' else kont (ok&&n<256) ph pl nh nl qh ql termination_by n-j theorem lo32 (x:ػ):(x&&&(ހ:ػ))˧=x˧%2^32:=by rw[BR];exact ϡ x˧ 32 theorem hi32 (x:ػ):(x>>>(32:ػ))˧=x˧/2^32:=by rw[Ξ];simp[ψ] theorem sh32 (x:ػ):(x<<<(32:ػ))˧=x˧%2^32*2^32:=by rw[ό,show(32:ػ)˧%64=32 from rfl,Α];have:=x܉;omega theorem or32 (x y:ػ) (h:y˧<2^32):((x<<<(32:ػ))|||y)˧=x˧%2^32*2^32+y˧:=by rw[Du,sh32,<-Α,Bq h] theorem mul32 (x y:ػ) (hx:x˧<2^32) (hy:y˧<2^32):(x*y)˧=x˧*y˧:=by rw[ϯ];exact ν (by have:=BP hx hy;omega) theorem mulsplit (a b:ٳ):a*b=a/2^32*(b/2^32)*2^64+(a%2^32*(b/2^32)+a/2^32*(b%2^32))*2^32+a%2^32*(b%2^32):=by nth_rw 1[<-Aq a (2^32),<-Aq b (2^32)];ring theorem m64N (p00 p01 p10 p11:ٳ):(p11+p01/2^32+p10/2^32+(p00/2^32+p01%2^32+p10%2^32)/2^32)*2^64+((p00/2^32+p01%2^32+p10%2^32)%2^32*2^32+p00%2^32)=p11*2^64+(p01+p10)*2^32+p00:=א theorem m64_v (a b:ػ):(m64 a b).1˧*2^64+(m64 a b).2˧=a˧*b˧:=by have ha:=a܉;have hb:=b܉;have hlt:=BP ha hb;have la0:=lo32 a;have la1:=hi32 a;have lb0:=lo32 b;have lb1:=hi32 b;unfold m64;dsimp only;generalize a&&&(ހ:ػ)=a0 at la0⊢;generalize a>>>32=a1 at la1⊢;generalize b&&&(ހ:ػ)=b0 at lb0⊢;generalize b>>>32=b1 at lb1⊢;have e00:=mul32 a0 b0 א א;have e01:=mul32 a0 b1 א א;have e10:=mul32 a1 b0 א א;have e11:=mul32 a1 b1 א א;have hAB:a˧*b˧=a1˧*b1˧*2^64+(a0˧*b1˧+a1˧*b0˧)*2^32+a0˧*b0˧:=by rw[la0,lb0,la1,lb1];exact mulsplit _ _ generalize a0*b0=P00 at e00⊢;generalize a0*b1=P01 at e01⊢;generalize a1*b0=P10 at e10⊢;generalize a1*b1=P11 at e11⊢;have b00:=P00܉;rw[<-e00,<-e01,<-e10,<-e11]at hAB;have mid:(P00>>>32+(P01&&&(ހ:ػ))+(P10&&&(ހ:ػ)))˧=P00˧/2^32+P01˧%2^32+P10˧%2^32:=by rw[ξ,ξ,hi32,lo32,lo32];clear* -b00;omega generalize P00>>>32+(P01&&&(ހ:ػ))+(P10&&&(ހ:ػ))=MD at mid⊢;rw[or32 _ _ (by rw[lo32];omega),lo32,mid];have K:=m64N P00˧ P01˧ P10˧ P11˧;have hh:(P11+P01>>>32+P10>>>32+MD>>>32)˧=P11˧+P01˧/2^32+P10˧/2^32+(P00˧/2^32+P01˧%2^32+P10˧%2^32)/2^32:=by rw[ξ,ξ,ξ,hi32,hi32,hi32,mid];clear* -K hAB hlt;omega rw[hh,K,hAB] theorem lg_step (n k:ٳ) (h:2^k<=n):n܀=k+(n/2^k)܀:=by have p:=Δ k;rw[Af (א:n≠0)];exact⟨by rw[Ϟ,Κ];exact (Am p).1 (ϰ (Nat.div_pos h p).ne'),by rw[Ϟ k,Cv,Κ];exact (An p).1 Ak⟩ theorem shl_v (x s:ػ) (h:s˧<64):(x<<>>s)˧=x˧/2^s˧:=by rw[Ξ,ν h,ψ] theorem orpw (a b k:ٳ) (hb:b<2^k):a*2^k|||b=a*2^k+b:=by rw[<-Α,Bq hb] theorem shl128_v (h l s:ػ) (hs:s˧<128) (hv:(h˧*2^64+l˧)*2^s˧<2^128): (shl128 h l s).1˧*2^64+(shl128 h l s).2˧=(h˧*2^64+l˧)*2^s˧:=by have hl:=l܉;unfold shl128;by_cases cA:s==0;. simp at cA;subst cA;simp by_cases cB:s<64 . simp only[cA,cB,φ,ש,ר];have s0:s˧≠0:=by intro e;apply cA;simp;exact σ.1 (by rw[e];rfl) have s64:s˧<64:=by simpa[Ψ]using cB have t64:(64-s)˧=64-s˧:=by rw[Η _ _ (by rw[ϐ];simp;omega)];simp rw[Du,shl_v _ _ s64,shr_v _ _ א,shl_v _ _ s64,t64];generalize s˧=k at s0 s64 hv⊢;have p64:2^64=2^(64-k)*2^k:=by rw[<-Κ];congr 1;omega have hhk:h˧*2^k<2^64:=by by_contra c;have:2^64*2^64<=(h˧*2^64+l˧)*2^k:=Cp (ω (2^64) (DZ.1 c)) (by rw[Nat.mul_right_comm];exact ω _ (ώ _ _));omega rw[ν hhk];have ldiv:l˧/2^(64-k)<2^k:=by rw[An (Δ _),Θ,<-p64];exact hl rw[orpw _ _ _ ldiv];have lsplit:l˧=l˧/2^(64-k)*2^(64-k)+l˧%2^(64-k):=(϶ _ _).symm;generalize l˧/2^(64-k)=a at lsplit ldiv⊢;generalize gb:l˧%2^(64-k)=b at lsplit;have bl:b<2^(64-k):=by rw[<-gb];exact Ε _ (Δ _) have lm:l˧*2^k%2^64=b*2^k:=by rw[lsplit,ϟ,ϣ,<-p64,Ϟ,ϲ];exact ν (by rw[p64];exact Aj bl (Δ _)) rw[lm,lsplit,p64];ring . simp only[cA,cB,φ,ש];have s64:64<=s˧:=by simpa[Ψ]using cB have t64:(s-64)˧=s˧-64:=by rw[Η _ _ (by rw[ϐ];simp;omega)];simp rw[shl_v _ _ א,t64];generalize s˧=k at s64 hs hv⊢;have pk:2^k=2^(k-64)*2^64:=by rw[<-Κ];congr 1;omega have h0:h˧=0:=by by_contra c;have:2^64*2^64<=(h˧*2^64+l˧)*2^k:=Ar (Cp (Nat.le_mul_of_pos_left _ א) (ώ _ _)) (Ι ב s64);omega simp only[h0,ϸ,Β]at hv⊢;have lt:l˧*2^(k-64)<2^64:=by by_contra c;rw[pk,<-ϣ]at hv;have:2^64*2^64<=l˧*2^(k-64)*2^64:=ω _ א;omega rw[ν lt,pk];simp;ring theorem shr128_v (h l s:ػ) (hs:s˧<128): (shr128 h l s).1˧*2^64+(shr128 h l s).2˧=(h˧*2^64+l˧)/2^s˧:=by have hl:=l܉;unfold shr128;by_cases cA:s==0;. simp at cA;subst cA;simp by_cases cB:s<64 . simp only[cA,cB,φ,ש,ר];have s0:s˧≠0:=by intro e;apply cA;simp;exact σ.1 (by rw[e];rfl) have s64:s˧<64:=by simpa[Ψ]using cB have t64:(64-s)˧=64-s˧:=by rw[Η _ _ (by rw[ϐ];simp;omega)];simp rw[Du,shr_v _ _ s64,shr_v _ _ s64,shl_v _ _ א,t64];generalize s˧=k at s0 s64⊢;have pk:0<2^k:=Δ k;have p64:2^64=2^(64-k)*2^k:=by rw[<-Κ];congr 1;omega have hsplit:h˧=h˧/2^k*2^k+h˧%2^k:=(϶ _ _).symm;generalize h˧/2^k=a at hsplit⊢;generalize gb:h˧%2^k=b at hsplit;have bl:b<2^k:=by rw[<-gb];exact Ε _ pk have hm:h˧*2^(64-k)%2^64=b*2^(64-k):=by rw[hsplit,ϟ,ϣ,Θ (2^k),<-p64,Ϟ,ϲ];exact ν (by rw[p64,Θ (2^(64-k))];exact Aj bl (Δ _)) have ld:l˧/2^k<2^(64-k):=by rw[An pk,<-p64];exact hl rw[hm,Nat.or_comm,orpw _ _ _ ld,show(h˧*2^64+l˧)/2^k=h˧*2^(64-k)+l˧/2^k by rw[p64,<-ϣ,Ϟ,ϔ _ _ pk,Ϟ],hsplit,p64];ring . simp only[cA,cB,φ,ש];have s64:64<=s˧:=by simpa[Ψ]using cB have t64:(s-64)˧=s˧-64:=by rw[Η _ _ (by rw[ϐ];simp;omega)];simp rw[shr_v _ _ א,t64];generalize s˧=k at s64 hs⊢;have pk:2^k=2^64*2^(k-64):=by rw[<-Κ];congr 1;omega rw[pk,<-ϧ,show(h˧*2^64+l˧)/2^64=h˧ by rw[Ϟ,ϔ _ _ (Δ 64),Υ hl];simp];simp theorem bzU (x:ػ):(x==0)=decide (x˧=0):=by rw[AB];simp only[beq_iff_eq,ת];exact⟨λe=>by rw[e];rfl,λe=>σ.1 (by rw[e];rfl)⟩ theorem msk_v (x s:ػ) (hs:s˧<64):(x&&&((1<<c3 (by simp;exact σ.1 (by rw[e];rfl));simp[Ψ]at c2;omega have t64:(s-64)˧=s˧-64:=by rw[Η _ _ (by rw[ϐ];simp;omega)];simp rw[bzU,bzU,msk_v _ _ (by rw[t64];omega),t64,show 2^s˧=2^64*2^(s˧-64) by rw[<-Κ];congr 1;omega,DH,show(h˧*2^64+l˧)/2^64=h˧ by rw[Ϟ,ϔ _ _ (Δ 64),Υ hl];simp,m64e,AB];simp only[Ω,ת];generalize h˧%2^(s˧-64)=w;omega theorem orpw2 (a b k:ٳ) (hb:b < 2^k):a*2^k|||b=a*2^k+b:=by rw[<-Α,Bq hb] theorem dv128_v (h l m:ػ) (m0:0 < m˧) (m32:m˧ < 2^32): (dv128 h l m).1˧*2^64+(dv128 h l m).2.1˧=(h˧*2^64+l˧)/m˧ /\ (dv128 h l m).2.2˧=(h˧*2^64+l˧)%m˧:=by have:=l܉;unfold dv128;dsimp only;have r:=Ε h˧ m0;generalize e:(h%m)<<<(32:ػ)|||l>>>(32:ػ)=x;have i:x˧=h˧%m˧*2^32+l˧/2^32:=by simp only[<-e,Du,ό,Ξ,UInt64.toNat_mod,Bc,BY,Α,ψ];rw[ν א];exact orpw2 _ _ _ א have s:=Ε x˧ m0;generalize f:(x%m)<<<(32:ػ)|||l&&&(ހ:ػ)=y;have j:y˧=x˧%m˧*2^32+l˧%2^32:=by simp only[<-f,Du,ό,BR,UInt64.toNat_mod,Bc,BY,Α];rw[ν א,show ހ=2^32-1 from rfl,ϡ];exact orpw2 _ _ _ (Ε _ ד) have a:x˧/m˧<2^32:=by rw[An m0,i];omega have b:y˧/m˧<2^32:=by rw[An m0,j];omega have c:((x/m)<<<(32:ػ)|||y/m)˧=x˧/m˧*2^32+y˧/m˧:=by simp only[Du,ό,UInt64.toNat_div,Bc,BY,Α];rw[ν א];exact orpw2 _ _ _ b rw[c,UInt64.toNat_div,UInt64.toNat_mod];have d:=Aq h˧ m˧;have e:=Aq x˧ m˧;have f:=Aq y˧ m˧;exact((Nat.div_mod_unique m0).2 ⟨(by simp only[Nat.mul_add,<-ϣ];have:=Aq l˧ (2^32);clear* -d e f i j this;omega:y˧%m˧+m˧*(h˧/m˧*2^64+(x˧/m˧*2^32+y˧/m˧))=h˧*2^64+l˧),Ε _ m0⟩).imp Eq.symm Eq.symm theorem qE1 (q R X D:ٳ) (hD:0 < D) (hX:X < D):(q*D+X=R*D) ↔ (q=R/\X=0):=by refine⟨λe=>?_,by rintro⟨a,b⟩;subst a b;omega⟩;obtain h|rfl|h:=lt_trichotomy q R;. have:(q+1)*D<=R*D:=ω _ h;rw[ϟ]at this;omega . omega . have:(R+1)*D<=q*D:=ω _ h;rw[ϟ]at this;omega theorem qE2 (q S X D:ٳ) (hD:0 < D) (hX:X < D):(4*(q*D+X) < D*(4*S+1)) ↔ (q < S\/(q=S/\4*X < D)):=by rw[(by ring:D*(4*S+1)=4*(S*D)+D)];obtain h|rfl|h:=lt_trichotomy q S;. have:(q+1)*D<=S*D:=ω _ h;rw[ϟ]at this;constructor<;>intro<;>omega . constructor<;>intro<;>omega have:(S+1)*D<=q*D:=ω _ h;rw[ϟ]at this;constructor<;>intro<;>omega theorem qE3 (q S X D:ٳ) (hD:0 < D) (hX:X < D):(4*(q*D+X)=D*(4*S+1)) ↔ (q=S/\4*X=D):=by have e:D*(4*S+1)=4*(S*D)+D:=by ring rw[e];rcases lt_trichotomy q S with h|h|h;. have:(q+1)*D<=S*D:=ω _ h;rw[ϟ]at this;constructor<;>intro a<;>omega . subst h;constructor<;>intro a<;>omega . have:(S+1)*D<=q*D:=ω _ h;rw[ϟ]at this;constructor<;>intro a<;>omega theorem qX (r1 b2 rest K m:ٳ) (hK:0 < K) (hrest:rest < K): ((r1*(4*K)+b2*K+rest=0) ↔ (r1=0/\b2=0/\rest=0)) /\ ((4*(r1*(4*K)+b2*K+rest) < m*(4*K)) ↔ (4*r1+b2 < m)) /\ ((4*(r1*(4*K)+b2*K+rest)=m*(4*K)) ↔ (4*r1+b2=m/\rest=0)):=by have e1:4*(r1*(4*K)+b2*K+rest)=4*((4*r1+b2)*K+rest):=by ring have e2:m*(4*K)=4*(m*K):=by ring refine⟨by have:=qE1 (4*r1+b2) 0 rest K hK hrest;omega,?_,?_⟩<;>rw[e1,e2]<;>generalize 4*r1+b2=t;. exact⟨λh=>by by_contra c;have:m*K<=t*K:=ω _ א;omega,λh=>by have:(t+1)*K<=m*K:=ω _ h;rw[ϟ]at this;omega⟩ . have:=qE1 t m rest K hK hrest;omega theorem sqA_q (num den root q r1 b2 rest K m:ٳ) (hK:0 < K) (hm:0 < m) (hden:den=m*(4*K)) (hnum:num=q*(m*(4*K))+(r1*(4*K)+b2*K+rest)) (hr1:r1 < m) (hb2:b2 < 4) (hrest:rest < K) : sqA num den root=if q=root*root&&r1==0&&b2==0&&rest==0 then 0 else if q < root*root+root||(q=root*root+root&&4*r1+b2 < m) then 1 else if q=root*root+root&&4*r1+b2==m&&rest==0 then 2 else 3:=by have a:0< m*(4*K):=ה;have b:r1*(4*K)+b2*K+rest< m*(4*K):=by have:=ω K (א:b2<=3);have:=ω (4*K) hr1;rw[Ah]at this;omega obtain⟨x,y,z⟩:=qX r1 b2 rest K m hK hrest;unfold sqA;rw[hden,hnum,(by ring:(2*root+1)*(2*root+1)=4*(root*root+root)+1)];simp only[qE1 _ _ _ _ a b,qE2 _ _ _ _ a b,qE3 _ _ _ _ a b,x,y,z,Ω,beq_iff_eq,ת,Ϸ,and_assoc] theorem Yjv (x:ػ):(Yj x)˧=x˧܀:=by by_cases a:x=0;subst a;rfl;exact Zj x a theorem lg128_v (h l:ػ):(lg128 h l)˧=(h˧*2^64+l˧)܀:=by have hl:=l܉;unfold lg128;split . rename_i c;have c0:h≠0:=by simpa using c have c1:h˧≠0:=λe=>c0 (σ.1 (by rw[e];rfl)) have g:2^64<=h˧*2^64+l˧:=Cp (Nat.le_mul_of_pos_left _ (Ο c1)) (ώ _ _);rw[lg_step _ 64 g,show(h˧*2^64+l˧)/2^64=h˧ by rw[Ϟ,ϔ _ _ (Δ 64),Υ hl];simp,ξ,Yjv,show (64:ػ)˧=64 from rfl];have:=(Ϻ c1).2 h܉;omega . rename_i c;simp at c;subst c;simp[Yjv] theorem sqW_V (num den root:ٳ) (e:ٴ) (hd:0 < den) (h:sqV (sqA num den root) root e < ߿):(sqV (sqA num den root) root e)՜=sqW num den root e:=by unfold sqV at h⊢;unfold sqW;dsimp only at h⊢;by_cases hC:ރ<=root/\root<33554432/\1<=e+(if root<ނ then 0 else 1:ٳ)+150/\e+(if root<ނ then 0 else 1:ٳ)+150<=253;. rw[if_pos hC];simp only[hC.1,hC.2.1,hd,hC.2.2.1,hC.2.2.2,and_self,ite_true];simp . rw[if_neg hC]at h;simp at h theorem FY_v (num den q r1 b2 rest K m c:ٳ) (t:ٴ) (z:ف) (hK:0 < K) (hm:0 < m) (hden:den=m*(4*K)) (hnum:num=q*(m*(4*K))+(r1*(4*K)+b2*K+rest)) (hr1:r1 < m) (hb2:b2 < 4) (hrest:rest < K) (hz:z=(rest==0)) (hf:FY q r1 b2 z m c t < ߿) : (FY q r1 b2 z m c t)՜=sqW num den (Nat.sqrt q) (((c:ٴ)-298)/2-t):=by have sa:=sqA_q num den (Nat.sqrt q) q r1 b2 rest K m hK hm hden hnum hr1 hb2 hrest;have d0:0< den:=by rw[hden];positivity subst hz;unfold FY at hf⊢;dsimp only at hf⊢;rw[<-sa]at hf⊢;exact sqW_V _ _ _ _ d0 hf theorem FY_root (q r1 b2:ٳ) (z:ف) (m c:ٳ) (t:ٴ) (hf:FY q r1 b2 z m c t < ߿):8388608<=Nat.sqrt q:=by unfold FY sqV at hf;dsimp only at hf;by_contra hc;rw[if_neg (by intro h;exact hc h.1)]at hf;simp at hf theorem nsplit (N m K:ٳ):N=N/(m*(4*K))*(m*(4*K))+(N/(4*K)%m*(4*K)+N/K%4*K+N%K):=by have a1:=Aq N (4*K);have a2:=Aq (N/(4*K)) m;have a3:N%(4*K)=N%K+K*(N/K%4):=by rw[Θ 4 K];exact DH rw[ϧ,Θ (4*K)]at a2;rw[a3,<-a2]at a1;conv_lhs=>rw[<-a1] ring theorem FX_v (shh sl:ػ) (m c k:ٳ) (t:ٴ) (hk1:1<=k) (hk:k < 64) (hm:0 < m) (hm32:m < 2^32) (hf:FX shh sl m c t k < ߿) : (FX shh sl m c t k)՜=sqW (shh˧*2^64+sl˧) (m*4^k) (Nat.sqrt ((shh˧*2^64+sl˧)/(m*4^k))) (((c:ٴ)-298)/2-t) /\ 8388608<=Nat.sqrt ((shh˧*2^64+sl˧)/(m*4^k)):=by have mu:m߸˧=m:=ו have s1:(2*k)߸˧=2*k:=ו have s2:(2*k-2)߸˧=2*k-2:=ו unfold FX at hf⊢;dsimp only at hf⊢;have Av:=shr128_v shh sl (2*k)߸ ו;generalize shr128 shh sl (2*k)߸=A at hf Av⊢;rw[s1]at Av;have Dv:=dv128_v A.1 A.2 m߸ ו ו;generalize dv128 A.1 A.2 m߸=DQ at hf Dv⊢;rw[mu,Av]at Dv;split at hf;. simp at hf rename_i cq;rw[if_neg cq];simp only[Ϸ,bne_iff_ne,ne_eq,not_or,Decidable.not_not]at cq;rw[cq.1,Ϝ,ϸ,Β]at Dv;obtain⟨N,num0⟩:∃N,shh˧*2^64+sl˧=N:=⟨_,rfl⟩;rw[num0]at Av Dv⊢;have K0:0<4^(k-1):=ה;have p4:4^k=4*4^(k-1):=by rw[<-pow_succ'];congr 1;omega have p2:2^(2*k)=4^k:=by rw[pow_mul];norm_num have p2':2^(2*k-2)=4^(k-1):=by rw[show 2*k-2=2*(k-1) by omega,pow_mul];norm_num rw[p2]at Dv;have B:((shr128 shh sl (2*k-2)߸).2&&&3)˧=N/4^(k-1)%4:=by have v:=shr128_v shh sl (2*k-2)߸ ו;rw[s2,num0,p2']at v;rw[BR,show (3:ػ)˧=2^2-1 from rfl,ϡ,<-v];omega have Z:lowz128 shh sl (2*k-2)߸=(N%4^(k-1)==0):=by rw[lowz128_v _ _ _ ו,s2,num0,p2',AB];simp rw[B,Z]at hf⊢;have qv:DQ.2.1˧=N/(m*4^k):=by rw[Dv.1,ϧ,Θ] rw[qv]at hf⊢;exact⟨FY_v N (m*4^k) (N/(m*4^k)) DQ.2.2˧ (N/4^(k-1)%4) (N%4^(k-1)) (4^(k-1)) m c t (N%4^(k-1)==0) K0 hm (by rw[p4]) (by rw[Dv.2,p4];exact nsplit N m _) (by rw[Dv.2];exact Ε _ hm) (Ε _ ד) (Ε _ K0) rfl hf,FY_root _ _ _ _ _ _ _ hf⟩ theorem FW_v (shh sl:ػ) (m c:ٳ) (fb:Unit->δ) (hm:0 < m) (hm32:m < 2^32) (hf:FW shh sl m c < ߿) : (FW shh sl m c)՜=YaD (shh˧*2^64+sl˧) m (((c:ٴ)-298)/2) fb:=by unfold FW at hf⊢;dsimp only at hf⊢;rw[lg128_v]at hf⊢;split at hf;. simp at hf rename_i ct;simp only[Ϸ,decide_eq_true_eq,not_or,not_le]at ct;rw[if_neg (by simp only[Ϸ,decide_eq_true_eq,not_or,not_le];exact ct)];generalize ht:(47-((shh˧*2^64+sl˧)܀:ٴ)+m܀+1)/2=t at ct hf⊢;have k1:1<=(-t)׆:=א;obtain⟨fx,rt⟩:=FX_v shh sl m c (-t)׆ t k1 ct.2 hm hm32 hf;rw[fx];unfold YaD;dsimp only;rw[ht];have nt:¬(0<=t):=א;simp only[nt,if_false];have dn:m<<<(2*(-t)׆)=m*4^(-t)׆:=by rw[Α,pow_mul];norm_num rw[dn];have sr:Ϗ (shh˧*2^64+sl˧) (m*4^(-t)׆)=Nat.sqrt ((shh˧*2^64+sl˧)/(m*4^(-t)׆)):=rfl;rw[sr,if_pos rt] theorem sub128 (ah al bh bl:ػ) (hge:bh˧*2^64+bl˧<=ah˧*2^64+al˧) : (ah-bh-(if al < bl then 1 else 0))˧*2^64+(al-bl)˧=(ah˧*2^64+al˧)-(bh˧*2^64+bl˧):=by have ha:=ah܉;have hl:=al܉;have hb:=bh܉;have hbl:=bl܉;split . rename_i c;have c':al˧< bl˧:=Ψ.1 c;have g:bh˧< ah˧:=by by_contra hc;have:ah˧*2^64<=bh˧*2^64:=ω _ א;omega rw[Η _ _ (by rw[ϐ];simp;rw[Η _ _ (ϐ.2 א)];omega), Η _ _ (ϐ.2 א),Τ];simp omega . rename_i c;have c':bl˧<=al˧:=by have:=Ψ.not.1 c;omega have g:bh˧<=ah˧:=by by_contra hc;have:(ah˧+1)*2^64<=bh˧*2^64:=ω _ א;rw[ϟ]at this;omega simp only[UInt64.sub_zero];rw[Η _ _ (ϐ.2 g),Η _ _ (ϐ.2 c'),Nat.sub_mul];omega theorem absd_v (ph pl nh nl:ػ):(absd ph pl nh nl).1˧*2^64+(absd ph pl nh nl).2˧=(((ph˧*2^64+pl˧:ٳ):ٴ)-((nh˧*2^64+nl˧:ٳ):ٴ)).natAbs:=by have a1:=ph܉;have a2:=pl܉;have a3:=nh܉;have a4:=nl܉;unfold absd;by_cases cc:(nhsimp only[cc,φ,ר,ש]<;>simp only[Ϸ,Ω,beq_iff_eq,ת,Ψ,ϐ,<-σ,not_or,not_and]at cc<;>rw[sub128 _ _ _ _ א]<;>omega theorem mulq_v (qh ql:ػ) (n:ٳ) (hn:n < 2^64) (h:(mulq qh ql n).2.2=true):(mulq qh ql n).1˧*2^64+(mulq qh ql n).2.1˧=n*(qh˧*2^64+ql˧):=by have nu:n߸˧=n:=ו;unfold mulq at h⊢;dsimp only at h⊢;have e0:=m64_v ql n߸;have e1:=m64_v qh n߸;rw[nu]at e0 e1;generalize m64 ql n߸=Q0 at e0 h⊢;generalize m64 qh n߸=Q1 at e1 h⊢;simp only[Ω,beq_iff_eq,ת]at h;obtain⟨h1,h2⟩:=h;have b1:=Q1.2܉;have b0:=Q0.1܉;have nw:Q1.2˧+Q0.1˧<2^64:=by have:=ϐ.1 h2;rw[ξ]at this;by_contra hc;rw[Nat.mod_eq_sub_mod א,ν א]at this;omega rw[ξ,ν nw];rw[h1,Ϝ,ϸ,Β]at e1;rw[show n*(qh˧*2^64+ql˧)=qh˧*n*2^64+ql˧*n by ring,<-e1,<-e0];ring theorem FT_v (ph pl nh nl qh ql:ػ) (n:ٳ) (h:(FT ph pl nh nl qh ql n).2.2=true) : (FT ph pl nh nl qh ql n).1˧*2^64+(FT ph pl nh nl qh ql n).2.1˧=n*(qh˧*2^64+ql˧)-(((ph˧*2^64+pl˧:ٳ):ٴ)-((nh˧*2^64+nl˧:ٳ):ٴ)).natAbs*(((ph˧*2^64+pl˧:ٳ):ٴ)-((nh˧*2^64+nl˧:ٳ):ٴ)).natAbs:=by unfold FT at h⊢;dsimp only at h⊢;have av:=absd_v ph pl nh nl;generalize absd ph pl nh nl=D at av h⊢;simp only[Ω,beq_iff_eq,ת,Ϸ]at h;obtain⟨⟨⟨h1,h2⟩,h3⟩,h4⟩:=h;rw[h1,Ϝ,ϸ,Β]at av;have nq:=mulq_v qh ql n א h3;generalize mulq qh ql n=NQ at nq h4⊢;have d2:=m64_v D.2 D.2;rw[av]at d2;generalize m64 D.2 D.2=D2 at d2 h4⊢;have ge:D2.1˧*2^64+D2.2˧<=NQ.1˧*2^64+NQ.2.1˧:=by rcases h4 with c|⟨c,d⟩;have:D2.1˧c (by simp only[Ω,beq_iff_eq];constructor<;>apply σ.1<;>rw[Ϝ]<;>omega) have:=(Ϻ v).2 (by have:=th܉;have:=tl܉;omega:_<2^128);rw[ξ,lg128_v,Da,ν א]at h;exact Ag (Aj Ak (Δ d)) (by rw[<-Κ];exact Ι ד א) theorem add128 (ah al bh bl:ػ) (h3:ah<=ah+bh) (h4:ah+bh<=ah+bh+(if al+blrename_i c<;>rw[Ψ,ξ]at c<;>split<;>first|rfl|omega rw[ϐ,ξ]at h3;rw[ϐ,ξ (ah+bh),ξ ah bh,cy]at h4;rw[ξ (ah+bh),ξ ah bh,cy,ξ al bl];by_cases c:2^64<=al˧+bl˧<;>simp only[c,ite_true,ite_false]at h4⊢<;>omega theorem FN_v (th tl:ػ) (d1 d2:ٳ) (E:ػ) (h:(FN th tl d1 d2 E).2.2=true) : (FN th tl d1 d2 E).1˧*2^64+(FN th tl d1 d2 E).2.1˧=(th˧*2^64+tl˧)*2^d1+E˧*2^d2:=by unfold FN at h⊢;dsimp only at h⊢;obtain⟨h123,h4⟩:=(Ω _ _).mp h;obtain⟨h12,h3⟩:=(Ω _ _).mp h123;obtain⟨h1,h2⟩:=(Ω _ _).mp h12;replace h1:=of_decide_eq_true h1;replace h2:=of_decide_eq_true h2;replace h3:=of_decide_eq_true h3;replace h4:=of_decide_eq_true h4;have u1:d1߸˧=d1:=by simp;omega have u2:d2߸˧=d2:=by simp;omega have bE:(0*2^64+E˧)*2^d2< 2^128:=by have:=bitlen_v 0 E d2 (by have e:(if (0:ػ)==0&&E==0 then 0 else lg128 0 E+1)=(if E==0 then 0 else Yj E+1):=by split<;>split<;>simp_all[lg128] rw[e];exact h2) simpa using this have T2v:=shl128_v th tl d1߸ (by rw[u1];omega) (by rw[u1];exact bitlen_v th tl d1 h1);rw[u1]at T2v;have E2v:=shl128_v 0 E d2߸ (by rw[u2];omega) (by rw[u2];simpa using bE);rw[u2]at E2v;simp only[Ϝ,zero_mul,zero_add]at E2v;rw[add128 _ _ _ _ h3 h4,T2v,E2v] theorem FT_n (ph pl nh nl qh ql:ػ) (n:ٳ) (h:(FT ph pl nh nl qh ql n).2.2=true):n < 256:=by unfold FT at h;dsimp only at h;exact of_decide_eq_true ((Ω _ _).mp ((Ω _ _).mp ((Ω _ _).mp h).1).1).2 theorem FTk_eq {β:Type} (ph pl nh nl qh ql:ػ) (n:ٳ) (k:ػ->ػ->ف->β):FTk ph pl nh nl qh ql n k=k (FT ph pl nh nl qh ql n).1 (FT ph pl nh nl qh ql n).2.1 (FT ph pl nh nl qh ql n).2.2:=by unfold FTk FT absd mulq;rfl theorem FNk_eq {β:Type} (th tl:ػ) (d1 d2:ٳ) (E:ػ) (k:ػ->ػ->ف->β):FNk th tl d1 d2 E k=k (FN th tl d1 d2 E).1 (FN th tl d1 d2 E).2.1 (FN th tl d1 d2 E).2.2:=rfl theorem FF_v (ph pl nh nl qh ql:ػ) (n b0:ٳ) (eps:δ) (fb:Unit->δ) (hn:0 < n) (hf:FF ph pl nh nl qh ql n b0 eps < ߿) : (FF ph pl nh nl qh ql n b0 eps)՜=YaD ((n*(qh˧<<<64+ql˧)-(((ph˧<<<64+pl˧:ٳ):ٴ)-((nh˧<<<64+nl˧:ٳ):ٴ)).natAbs*(((ph˧<<<64+pl˧:ٳ):ٴ)-((nh˧<<<64+nl˧:ٳ):ٴ)).natAbs)<<<(2*b0-YF b0 (γ eps))+(n*n*ε eps)<<<(γ eps+149-YF b0 (γ eps))) (n*n) (((YF b0 (γ eps):ٴ)-298)/2) fb:=by simp only[Α];unfold FF at hf⊢;rw[FTk_eq]at hf⊢;dsimp only at hf⊢;split at hf;swap;simp at hf;rename_i hT;rw[if_pos hT];have n256:=FT_n _ _ _ _ _ _ _ hT;have tv:=FT_v _ _ _ _ _ _ _ hT;rw[FNk_eq]at hf⊢;split at hf;swap;simp at hf;rename_i hN;rw[if_pos hN];have Eu:(n*n*ε eps)߸˧=n*n*ε eps:=by have:=BP (BP n256 n256) (Ϛ eps);simp;omega have nv:=FN_v _ _ _ _ _ hN;rw[Eu,tv]at nv;rw[FW_v _ _ _ _ fb ה (by have:=BP n256 n256;omega) hf,nv] theorem FFr_v (qh ql:ػ) (n b0:ٳ) (eps:δ) (fb:Unit->δ) (hf:FFr qh ql n b0 eps < ߿) : (FFr qh ql n b0 eps)՜=YaD ((qh˧<<<64+ql˧)<<<(2*b0-YF b0 (γ eps))+(n*ε eps)<<<(γ eps+149-YF b0 (γ eps))) n (((YF b0 (γ eps):ٴ)-298)/2) fb:=by simp only[Α];unfold FFr at hf⊢;dsimp only at hf⊢;by_cases h:(0∃ ok' ph' pl' nh' nl' qh' ql',(∀ {β:Type} (kont:ف->ػ->ػ->ػ->ػ->ػ->ػ->β),rsV kont x base n b0 j ph pl nh nl qh ql ok=kont ok' ph' pl' nh' nl' qh' ql') /\ rsU x base n b0 j ph pl nh nl qh ql ok=(ok',ph'˧<<<64+pl'˧,nh'˧<<<64+nl'˧,qh'˧<<<64+ql'˧):=by intro k;induction k with |zero=>intro j ph pl nh nl qh ql ok hk;have c:¬jby rw[rsV,if_neg c],by rw[rsU,if_neg c]⟩ |succ k ih=>intro j ph pl nh nl qh ql ok hk;have c:jobtain⟨A,B,C,D,E,F,G,V,U⟩:=ih (j+1) _ _ _ _ _ _ _ א<;>exact⟨A,B,C,D,E,F,G,λk=>by rw[rsV,if_pos c];simp only[hs,φ,ר,ש];exact V k,by rw[rsU,if_pos c];simp only[hs,φ,ר,ש];exact U⟩ @[inline] def Y8f (x:ؽ δ) (n:ٳ) (eps:δ) (r:ٳ):ٳ × ٴ × δ := let b0:=Y2L x (r*n) n 0 255 rsV (λok ph pl nh nl qh ql=> let S1:ٴ:=if ok then ((ph˧<<<64+pl˧:ٳ):ٴ)-((nh˧<<<64+nl˧:ٳ):ٴ) else (limbValA (Y7 x (r*n) n b0).1:ٴ)-limbValA (Y7 x (r*n) n b0).2 let f:=if ok&&0 Ac (ϫ n x r (rowMean n x r)+ς eps))) x (r*n) n b0 0 0 0 0 0 0 0 true lemma Y8f_eq (x:ؽ δ) (n:ٳ) (eps:δ) (r:ٳ):Y8f x n eps r=Y8 x n eps r:=by unfold Y8f Y8;rw[Y2L_eq];dsimp only;obtain⟨o,a,b,c,d,e,f,V,U⟩:=rsV_ex x (r*n) n (Y2 x (r*n) n) _ 0 0 0 0 0 0 0 true rfl;rw[V,Tn];cases o;. simp only[Bool.false_and,φ,ש];rw[if_neg ד] obtain⟨g,h⟩:=rsU_lim x (r*n) n (Y2 x (r*n) n) (by rw[U]);rw[U]at g h;dsimp only at g h;simp only[ר,Bool.true_and];by_cases i:0 ℚ):δ := if sv dw <= 0 then roundRational ((ς w-mean ())*ς gw/ς dw+ς bw) else MYa7b n w gw bw b0 S1 dw lemma shm (w:UInt32) (m:ػ) (k:ٳ) (hk:k <= 38) (h:ε w=0\/(m˧ <= γ w/\γ w-m˧ <= k)) : (Yf w<<<(Yd w-m))˧=ε w*2^(γ w-m˧) /\ ε w*2^(γ w-m˧) < 2^(24+k):=by rcases h with h0|⟨h1,h2⟩;. rw[(σ.1 (by rw[Zk,h0];rfl):Yf w=0),h0];simp . have hp:ε w*2^(γ w-m˧)<2^(24+k):=by rw[Κ];exact Ag (Aj (Ϛ w) (Δ _)) (Ci _ (Ι ב h2)) refine⟨?_,hp⟩;rw[ό,Η _ _ (ϐ.2 (by rw[Zf];exact h1)),Zf,Zk,ν (א:γ w-m˧<64),Α];exact ν (Ag hp (Ι ב א)) end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats open Float.Model.UnpackedFloat @[inline] def rqU (a b:ػ):ػ := let q:=a/b let tw:=2*(a%b) if tw < b then q else if b < tw then q+1 else if q%2==0 then q else q+1 @[inline] def rrsG (s:ف) (N D U V:ػ) (fb:Unit -> δ):δ := let i:=Yj N-Yj D let R:=if N < D<<>>k=0):x < 2^k:=by rwa[ψ,Nat.div_eq_zero_iff_lt ה]at h end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Numerics open Model open RationalBinary (floorLog2 scaleByPowerOfTwo scaleByPowerOfTwo_snd_ne_zero) open FloatLib.Numerics (binaryRadix Radix.toReal) lemma flr (P Dm r:ٳ) (hD:Dm≠0) (hP:P≠0) (h1:2^r*Dm <= P) (h2:P < 2^(r+1)*Dm):fl2 P Dm = (r:ٴ):=by rw[Zbo];obtain⟨a,b⟩:=floorLog2_bounds P Dm hP hD;have c:(0:ℝ) omega rw[if_neg א,if_neg א,if_neg א];by_cases hk2:m=ނ;. simp only[hk2,beq_self_eq_true,ite_true]at hx⊢;rw[if_neg א] . have:(m==ނ)=false:=by simpa using hk2 simp only[hk2,this,ite_false,φ]at hx⊢;rw[if_neg א];simp lemma rqUe2 (a b:ػ) (hb0:0 < b˧) (hb:b˧ < 2^63) (hq:a˧/b˧ < 2^63) : (rqU a b)˧ = AP a˧ b˧:=by unfold rqU AP;dsimp only;have hq0:=UInt64.toNat_div a b;have hr:=UInt64.toNat_mod a b;have hrl:=Ε a˧ hb0;have htw:(2*(a%b))˧=2*(a˧%b˧):=by rw[ϯ,hr];simp;omega have hq1:(a/b+1)˧=a˧/b˧+1:=by rw[ξ,hq0];simp;omega have hm2:((a/b)%2)˧=(a˧/b˧)%2:=by rw[UInt64.toNat_mod,hq0];rfl have e3:((a/b)%2==0)=true↔(a˧/b˧)%2=0:=by simp only[beq_iff_eq];rw[<-hm2];exact⟨λh=>by rw[h];rfl,λh=>σ.mp (by rw[h];rfl)⟩ by_cases c1:2*(a˧%b˧)c3 (e3.mp h)),if_neg (by simpa using c3),hq1] lemma p2le (a b:ٳ) (h:a <= b):2^a <= 2^b:=Ι ב h lemma rRe (N D:ػ) (hD0:0 < D˧) (hND:D˧ <= N˧) (hD:D˧ < 2^40) : let R:=if Nby rw[h]at hND;simp at hND;omega);have hld:=Zj D (λh=>by rw[h]at hD0;simp at hD0) have bN:=(Af (א:N˧≠0)).mp rfl;have bD:=(Af (א:D˧≠0)).mp rfl;generalize N˧܀=ln at bN hln;generalize D˧܀=ld at bD hld have hle:ld<=ln:=Ao.1 ((Nat.pow_lt_pow_iff_right (ב:1<2)).1 (Ρ (Cp bD.1 hND) bN.2)) have hl63:ln<=63:=by by_contra h;have:=p2le 64 ln א;have:N˧<2^64:=N܉;omega have hi:(Yj N-Yj D)˧=ln-ld:=by rw[Η _ _ (by rw[ϐ,hln,hld];exact hle),hln,hld] have h1:D˧*2^(ln-ld)<2^(ln+1):=by rw[<-show 2^(ld+1)*2^(ln-ld)=2^(ln+1) by rw[<-Κ,(א:ld+1+(ln-ld)=ln+1)]];exact Aj bD.2 ה have hsh:(D<<<(Yj N-Yj D))˧=D˧*2^(ln-ld):=by rw[ό,hi,ν (א:ln-ld<64),Α];exact ν (Ag h1 (p2le _ _ א)) have up:N˧<2^(ln-ld+1)*D˧:=by have:=Ci (2^(ln-ld+1)) bD.1;rw[<-Κ,(א:ln-ld+1+ld=ln+1)]at this;omega by_cases c:NX) = X:=by unfold rrsG;dsimp only;obtain⟨a,b,c⟩:=rRe N D hD0 hND hD;generalize (if Nomega obtain⟨j,k⟩:(if M==ނ then (1:ػ)else 0)˧=(if M˧=ނ then 1 else 0)/\((if (if M==ނ then (1:ػ)else 0)==1 then ރ else M)-ރ)˧=(if M˧=ނ then ރ else M˧)-ރ:=by by_cases h:M˧=ނ;. simp[σ.1 (by rw[h];rfl:M˧=(ނ:ػ)˧)] . have:¬M=ނ:=λe=>h (by rw[e];rfl) refine⟨by simp[this,h],?_⟩;simp only[this,h,beq_iff_eq,ש,(ד:¬(0:ػ)=1)];rw[Η _ _ (by rw[ϐ];simp;omega)];simp generalize (if M==ނ then (1:ػ)else 0)=K at j k⊢;have l:(if M˧=ނ then 1 else 0)<=1:=by split<;>omega have m:(R+U)˧=R˧+U˧:=by rw[ξ];omega have n:(R+U+K)˧=R˧+U˧+(if M˧=ނ then 1 else 0):=by rw[ξ,m,j];omega by_cases h:(decide (Vsimp_all<;>omega),x,k];congr 1;split<;>simp_all<;>omega . rw[if_neg h] lemma eok (w:Word) (h:(expOf w).toUInt64-1 < 254):1 <= (expOf w)پ ∧ (expOf w)پ <= 254:=by have h0:=expOf_toNat_lt w;rw[Ψ]at h;by_cases hz:(θ w)پ=0;have:(θ w)܅=0:=σ.mp (by simp[AG,hz]);simp[this]at h;rw[Η _ _ (by rw[ϐ];simp[AG];omega)]at h;simp[AG]at h;omega lemma mok (w:Word) (h:1 <= (expOf w)پ):mantOf w = 2^23+(fracOf w)پ ∧ idxOf w = (expOf w)پ-1:=by have e:θ w≠0:=(λe=>by simp[e]at h);unfold ε γ;exact⟨if_neg e,if_neg e⟩ end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats @[inline] def Ya7G (n:ٳ) (w gw bw:δ) (b0:ٳ) (S1:ٴ) (dw:δ) (mean:Unit -> ℚ) (nU b0U:ػ) (S1n:ف) (S1a:ػ):δ := let ed:=(expOf dw)܅ if ed-1 < 254 && !η dw && 0 < nU && nU < 4096 && S1a < 0x1000000000000000 && expOf w != 255 && expOf gw != 255 && expOf bw != 255 then let iw:=Yd w let ig:=Yd gw let ib:=Yd bw let idd:=ed-1 let mw:=Yf w if mw == 0 || (b0U <= iw && iw-b0U <= 24) then let x:=nU*(mw<<<(iw-b0U)) let cs:ف:=if η w == S1n then η w else if S1a <= x then η w else S1n let cv:=if η w == S1n then x+S1a else if S1a <= x then x-S1a else S1a-x let D:=nU*Yf dw let c2:=if b0U+ig <= ib+idd then b0U+ig else ib+idd let ks:=b0U+ig-c2 let bs:=ib+idd-c2 let q0:=cv/D let r0:=cv%D*Yf gw let qq:=q0*Yf gw+r0/D if ks <= 8 && qq < (1:ػ)<<<(61-ks) && bs <= 37 then let ra:=(r0%D)<<MYa7 n w gw bw b0 S1 dw mean) else let N:=qi*D+qr if N == 0 then 0 else if N < D then MYa7 n w gw bw b0 S1 dw mean else rrsG qs N D (c2+1) (idd+23) (λ_=>MYa7 n w gw bw b0 S1 dw mean) else MYa7 n w gw bw b0 S1 dw mean else MYa7 n w gw bw b0 S1 dw mean else MYa7 n w gw bw b0 S1 dw mean end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Numerics open Model open RationalBinary (floorLog2 scaleByPowerOfTwo scaleByPowerOfTwo_snd_ne_zero) open FloatLib.Numerics (binaryRadix Radix.toReal) lemma u64m2 (a b:ػ) (h:a˧*b˧ < 2^64):(a*b)˧=a˧*b˧:=by rw[ϯ,ν h] lemma u64a2 (a b:ػ) (h:a˧+b˧ < 2^64):(a+b)˧=a˧+b˧:=by rw[ξ,ν h] lemma u64s2 (a b:ػ) (h:b˧ <= a˧):(a-b)˧=a˧-b˧:=by rw[Η _ _ (ϐ.mpr h)] lemma u64sh (a s:ػ) (hs:s˧ < 64) (h:a˧*2^s˧ < 2^64):(a<<f (ϐ.mp h);simp only[f,f',ש];by_cases g:rr˧=0;have g':rr=0:=σ.mp (by rw[g];rfl);simp only[g,g',beq_self_eq_true,ר];rw[u64s2 _ _ א];simp;have g':(rr==0)=false:=by simp only[beq_eq_false_iff_ne];intro h;apply g;rw[h];rfl simp only[g,g',ש,φ];rw[u64s2 _ _ (by rw[u64s2 _ _ א];simp;omega),u64s2 _ _ א,u64s2 _ _ א];simp lemma Ya7Ge (n:Nat) (w gw bw:Word) (b0:Nat) (S1:Int) (dw:Word) (mean:Unit->ℚ) (nU b0U:UInt64) (S1n:Bool) (S1a:UInt64) (hn:0 < nU.toNat → nU.toNat=n) (hb:b0U.toNat=b0) (hb255:b0 < 256) (hs:S1n=!(decide (S1 < 0))) (ha:S1a.toNat < 2^60 → S1a.toNat=S1.natAbs) : Ya7G n w gw bw b0 S1 dw mean nU b0U S1n S1a = MYa7 n w gw bw b0 S1 dw mean:=by simp only[Ya7G];rw[ite_eq_right_iff];intro c1;simp only[EK,Ω,ϵ,ת,bne_iff_ne,ne_eq,Ψ,Bc]at c1;obtain⟨ca,cb,cc,cd,ce,_⟩:=c1;replace hn:=hn cc;have hed:=eok dw (Ψ.2 ca);obtain⟨hmd,hid⟩:=mok dw hed.1;have hn0:0(first|rw[u64a2 _ _ א]|rw[u64s2 _ _ א])<;>exact⟨rfl,א⟩ have hDb:2^23<=n*ε dw/\n*ε dw<2^36:=⟨Cp (by rw[hmd];omega) (Nat.le_mul_of_pos_left _ hn0),(BP hn1 (Ϛ dw)).trans_eq ב⟩;generalize hd:nU*Yf dw=D;have hD:D˧=n*ε dw:=by rw[<-hd,u64m2 _ _ (by rw[hn,Zk];omega),hn,Zk] have hbg:(b0U+Yd gw)˧=b0+γ gw:=by rw[u64a2 _ _ (by rw[hb,Zf];omega),hb,Zf] have hbd:(Yd bw+idd)˧=γ bw+γ dw:=by rw[u64a2 _ _ (by rw[Zf,hidd];omega),Zf,hidd] generalize hc:(if b0U+Yd gw<=Yd bw+idd then b0U+Yd gw else Yd bw+idd)=c2;have hc2:c2˧=min (b0+γ gw) (γ bw+γ dw):=by subst hc;split<;>rename_i h<;>rw[ϐ]at h<;>omega generalize hk:b0U+Yd gw-c2=ks;have hks:ks˧=b0+γ gw-min (b0+γ gw) (γ bw+γ dw):=by rw[<-hk,u64s2 _ _ א];omega generalize hk:Yd bw+idd-c2=bs;have hbs:bs˧=γ bw+γ dw-min (b0+γ gw) (γ bw+γ dw):=by rw[<-hk,u64s2 _ _ א];omega have hDp:0simp at hY1<;>omega have hY7:MYa7 n w gw bw b0 S1 dw mean=if ނ<=qi˧then YN qs (2*qi˧+(if qr˧=0 then 0 else 1)) 1 (c2˧-150-γ dw)else YN (qs&&qi˧*D˧+qr˧ !=0) (qi˧*D˧+qr˧) D˧ (c2˧-149-γ dw):=by simp only[MYa7,MYa7b,if_neg hsv,hyah];rw[<-hD,ZE,ZE,<-hks,<-hbs,<-hc2,<-hqq,<-hra,<-hqa,<-hrr,<-hB,hY1] have hV:(idd+23)˧=γ dw+23:=by rw[u64a2 _ _ ו,hidd];rfl split<;>rename_i h16<;>simp only[ϐ,Bc]at h16;clear* -hc2 hV hY7 h16 hqib i3 i4;have hM:(2*qi+(if qr==0 then 0 else 1))˧=2*qi˧+(if qr˧=0 then 0 else 1):=by have h2q:(2*qi)˧=2*qi˧:=(by rw[ϯ];simp;omega);by_cases e:qr˧=0;simp only[(σ.mp (by rw[e];rfl):qr=0),e,beq_self_eq_true,ר];rw[u64a2 _ _ (by rw[h2q];simp;omega),h2q];simp;simp only[(by simp only[beq_eq_false_iff_ne];intro h;apply e;rw[h];rfl:(qr==0)=false),e,φ,ש];rw[u64a2 _ _ (by rw[h2q];simp;omega),h2q];simp exact rrsGe _ _ _ _ _ (c2˧-150-γ dw) _ ג (by rw[hM];simp;omega) ג א א א (by rw[hY7,if_pos h16,hM];rfl);have hqD:qi˧*D˧<2^60:=(BP (show qi˧<2^24 by omega) (show D˧<2^36 by omega)).trans_eq ב;have hNv:(qi*D+qr)˧=qi˧*D˧+qr˧:=by rw[u64a2 _ _ (by rw[u64m2 _ _ א];omega),u64m2 _ _ א] split;rename_i hN0;rw[hY7,if_neg h16,<-hNv,eq_of_beq hN0];unfold YN;simp[show D˧≠0 by omega];rename_i hN0;simp only[beq_iff_eq,<-σ,hNv,Bc]at hN0;rw[ite_eq_left_iff];intro hND;rw[Ψ,hNv]at hND;clear* -hDp hD hDb hNv hND hc2 hV hY7 h16 hN0 i3 i4;have hU:(c2+1)˧=c2˧+1:=u64a2 _ _ ו;exact rrsGe _ _ _ _ _ (c2˧-149-γ dw) _ hDp א א א א א (by rw[hY7,if_neg h16,hNv,(by simpa using hN0:(qi˧*D˧+qr˧ !=0)=true),Bool.and_true]) end section open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats lemma YTle (x:ؽ δ) (base n:ٳ):Y2 x base n <= 255:=by unfold Y2;suffices∀(l:List ٳ) b,b<=255->l.foldl (λb j=>let w:=x[base+j]!;if ε w=0 then b else min b (γ w)) b<=255 from this _ _ le_rfl intro l;induction l<;>intro b h;exact h;rename_i ih;simp only[AO];apply ih;split<;>omega end section lemma Ya7_eq (n:ٳ) (w gw bw:δ) (b0:ٳ) (S1:ٴ) (dw:δ) (mean:Unit->ℚ): MYa7 n w gw bw b0 S1 dw mean=MYba n w gw bw b0 S1 dw mean:=by rw[<-elS_eq];unfold MYa7 MYa7b elS;simp only[U2] end def MYbc (rows n:ٳ) (eps:δ) (x g b:ؽ δ):ؽ δ := if stableNormDomain rows n eps x g&&b.size==n&&b.all finiteWord then let st:=AF (n:=rows) λr=>Y8f x n eps r.val let aux:=AF (n:=rows) λr=> let s:=st[r.val]! let a:=s.2.1.natAbs ((!(s.2.1 < 0)),(if a>>>60 == 0 then a߸ else 0x1000000000000000),s.1߸) let nU:=if n < 4096 then n߸ else 0 YT rows n λr j=>let s:=st[r]!;let u:=aux[r]!;Ya7G n x[r*n+j]! g[j]! b[j]! s.1 s.2.1 s.2.2 (λ_=>rowMean n x r) nU u.2.2 u.1 u.2.1 else layernormLegacyWith Yn rows n eps x g b lemma Zdt:MYbc=layernormSpec:=by funext rows n eps x g b;unfold MYbc layernormSpec layernormWith;split . rename_i hd;simp only[Ω]at hd;obtain⟨⟨h0,-⟩,-⟩:=hd;obtain⟨hn,he⟩:=M1 rows n eps x g h0;unfold layernormStable;rw[ZB _ _ _ rfl];dsimp only;congr 1;funext q;rw[϶];have hr:q܂/n(let s:=(AF (n:=rows) λr=>Y8f x n eps r.val)[r]!;let a:=s.2.1.natAbs ((!(s.2.1<0)),(if a>>>60==0 then a߸else 0x1000000000000000),s.1߸))) _ hr];dsimp only;rw[gO rows (λr=>Y8f x n eps r) _ hr,Y8f_eq,Ya7Ge n _ _ _ _ _ _ _ _ _ _ _ (by intro h;split at h<;>simp_all[Nat.toUInt64_eq];omega) (by unfold Y8;simp[Nat.toUInt64_eq];omega) (by unfold Y8;simp;omega) rfl (by split;(rename_i h0;intro h;simp only[beq_iff_eq]at h0;have:=sh0 _ _ h0;simp[Nat.toUInt64_eq];omega);intro h;simp at h),Ya7_eq,gO rows (λr=>Ac (ϫ n x r (AF (n:=rows) λi=>rowMean n x i.val)[r]! +ς eps)) _ hr,gO rows (λr=>rowMean n x r) _ hr,Zdr _ _ _ _ _ _ _ _ hn,U8 _ _ _ _ _ _ _ _ hn (λhm=>by have:=Z2 x (q܂/n*n) n (q܂%n) (Ε _ hn) (by rwa[hq]);rwa[hq]at this) (V0 x n eps _ hn),V1 x n eps _ hn he] . simp only[Zi] @[inline] def YaEf (x:ؽ δ) (n:ٳ) (eps:δ) (r:ٳ):δ:= let b0:=Y2L x (r*n) n 0 255 rsV (λok _ _ _ _ qh ql=> let f:=if ok then FFr qh ql n b0 eps else ߿ if f<߿ then f՜ else let c:=YF b0 (γ eps) YaD ((if ok then qh˧<<<64+ql˧ else limbValA (Y6 x (r*n) n b0))<<<(2*b0-c)+(n*ε eps)<<<(γ eps+149-c)) n (((c:ٴ)-298)/2) λ_=>Ac (ϫ n x r 0+ς eps)) x (r*n) n b0 0 0 0 0 0 0 0 true lemma YaEf_eq (x:ؽ δ) (n:ٳ) (eps:δ) (r:ٳ):YaEf x n eps r=YaE x n eps r:=by unfold YaEf YaE;rw[Y2L_eq];dsimp only;obtain⟨o,a,b,c,d,e,f,V,U⟩:=rsV_ex x (r*n) n (Y2 x (r*n) n) _ 0 0 0 0 0 0 0 true rfl;rw[V];cases o;. simp only[φ,ש];rw[if_neg ד] obtain⟨-,h⟩:=rsU_lim x (r*n) n (Y2 x (r*n) n) (by rw[U]);rw[U]at h;dsimp only at h;simp only[ר];split;rename_i j;rw[FFr_v _ _ _ _ _ _ j,h];rw[h] def Ybb2 (rows n:ٳ) (eps:δ) (x g:ؽ δ):ؽ δ:= if stableNormDomain rows n eps x g then let den:=AF (n:=rows) λr=>YaEf x n eps r.val;YT rows n λr j=>Ya7 x[r*n+j]! g[j]! den[r]! else rmsnormLegacyWith Yn rows n eps x g lemma T4b:Ybb2=rmsnormSpec:=by funext c n a x g;unfold Ybb2 rmsnormSpec rmsnormWith;split;rename_i d;obtain⟨e,f⟩:=M1 c n a x g d;unfold rmsnormStable;rw[ZB _ _ _ rfl];congr 1;funext q;rw[϶];have b:q܂/nYaEf x n a r) _ b,gO c (λr=>Ac (ϫ n x r 0+ς a)) _ b,YaEf_eq,V2 x n a _ e f,T8,U9];simp only[Zi] lemma getElem!_ofFn (n:Nat) (f:Nat->UInt32) (k:Nat) (hk:k< n) : (AF (n:=n) λi=>f i.val)[k]! = f k:=by rw[getElem!_pos _ k (by simpa using hk),AD] 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< Λ:=n.toNat_lt_size have h3:Λ=2^System.Platform.numBits:=rfl rw[USize.toNat_add,Φ] exact ν א lemma uadd (a b:USize) (h:a.toNat+b.toNat<2^32):(a+b).toNat=a.toNat+b.toNat:=by have h1:2^32<=Λ:=τ have h3:Λ=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<=Λ:=τ have h3:Λ=2^System.Platform.numBits:=rfl rw[USize.toNat_ofNat']; exact ν א lemma lt_of_usz_f (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 DY _ _ omega lemma lt_of_usz_w (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 DY _ _ omega @[inline] def rdF (a:FloatArray) (i:USize):Float := if h:i< a.usize then a.uget i (lt_of_usz_f a i h) else 0 @[inline] def rdW (a:Array UInt32) (i:USize):UInt32 := if h:i< a.usize then a.uget i (lt_of_usz_w 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 (lt_of_usz_f 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 (lt_of_usz_w 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 := (ϒ 512).foldl (λa i=>a.push (pwEntry i)) (FloatArray.emptyWithCapacity 512) lemma pw_data:pw.data=(ϒ 512).toArray.map pwEntry:=by have key:∀ (l:List Nat) (a:FloatArray), (l.foldl (λa i=>a.push (pwEntry i)) a).data=a.data ++ (l.toArray.map pwEntry):=by intro l induction l with | nil=>intro a; simp | cons x l ih => intro a simp only[AO,ih] simp[FloatArray.push] simp only[pw,key] simp[FloatArray.emptyWithCapacity] lemma pw_size:pw.size=512:=by simp[FloatArray.size,pw_data] lemma pw_idx (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pw.size:=by rw[pw_size] have h:=w.toNat_lt_size simp only[AI,Ϣ] norm_num [ψ] at h ⊢ omega @[inline] def dv (w:UInt32):Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:UInt32)).toUSize (pw_idx 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):Bool := 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 (0x007FFFFF<=lo) && (hi<0x7F800000) termination_by n.toNat-i.toNat decreasing_by rw[usz i n h]; omega lemma usize_le (a:Array UInt32):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'] exact DY _ _ 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 @[noinline] def slowOut (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat):Word := act relu (Yn (inW+1) (λt=>if t< inW then X[i*inW+t]! else oneW) (λt=>if t< inW then W[j*inW+t]! else b[j]!)) @[inline] def fin (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf t:Float) (bw:UInt32):UInt32 := let bv:=dv bw let r:=ziv (s+bv) (nf*(t+bv.abs)) if r == 0xFFFFFFFF then slowOut inW relu X W b i j else act relu r structure F1 where (s t:Float) def tightGo (X W:Array UInt32) (xo wo n t:USize) (s a:Float):F1 := if h:t< n then let q:=dv (rdW X (xo+t))*dv (rdW W (wo+t)) tightGo X W xo wo n (t+1) (s+q) (a+q.abs) else ⟨s,a⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h]; have:=υ.mp h; omega @[inline] def fin8 (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf a:Float) (hx bw:UInt32) (n:USize):UInt32 := let bv:=dv bw let r:=ziv (s+bv) (nf*(dv hx*a+bv.abs)) if r == 0xFFFFFFFF then let q:=tightGo X W (i*inW).toUSize (j*inW).toUSize n 0 0 0 fin inW relu X W b i j q.s nf q.t bw else act relu r structure R1 where (ix:Array UInt32) (buf:FloatArray) (c:USize) def decR (X:Array UInt32) (o n t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize):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 else decR X o n (t+1) (wrW ix c t.toUInt32) (wrF buf c (dv w)) (c+1) else ⟨ix,buf,c⟩ 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[uadd _ _ א]; omega structure F4 where (a b c d t0 t1 t2 t3:Float) def go4 (x:FloatArray) (ix W:Array UInt32) (w0 w1 w2 w3 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) (a b c d t0 t1 t2 t3:Float):F4 := if h:t< n then have ht:=υ.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=υ.mp hm let q0:=v*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) let q1:=v*dv (W.uget (w1+i) (wi w1 i m W h1 hs him)) let q2:=v*dv (W.uget (w2+i) (wi w2 i m W h2 hs him)) let q3:=v*dv (W.uget (w3+i) (wi w3 i m W h3 hs him)) go4 x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 (a+q0) (b+q1) (c+q2) (d+q3) (t0+q0.abs) (t1+q1.abs) (t2+q2.abs) (t3+q3.abs) else go4 x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 a b c d t0 t1 t2 t3 else ⟨a,b,c,d,t0,t1,t2,t3⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h]; omega) def go1 (x:FloatArray) (ix W:Array UInt32) (w0 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (a t0:Float):F1 := if h:t< n then have ht:=υ.mp h let i:=(ix.uget t א).toUSize if hm:i< m then have him:=υ.mp hm let q:=x.uget t א*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) go1 x ix W w0 m n (t+1) hx hi hs h0 (a+q) (t0+q.abs) else go1 x ix W w0 m n (t+1) hx hi hs h0 a t0 else ⟨a,t0⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h]; omega) def cols1 (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (i:Nat) (ix:Array UInt32) (x:FloatArray) (nf:Float) (m n:USize) (j:Nat) (base:USize) (out:Array UInt32):Array UInt32 := if h4:j+4<=outW then let ju:=j.toUSize let w0:=ju*m let w1:=(ju+1)*m let w2:=(ju+2)*m let w3:=(ju+3)*m if hg:n.toNat<=x.size∧n.toNat<=ix.size∧W.size<2^32∧w0.toNat+m.toNat<=W.size ∧ w1.toNat+m.toNat<=W.size∧w2.toNat+m.toNat<=W.size∧w3.toNat+m.toNat<=W.size then let r:=go4 x ix W w0 w1 w2 w3 m n 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2.1 hg.2.2.2.2.1 hg.2.2.2.2.2.1 hg.2.2.2.2.2.2 0 0 0 0 0 0 0 0 let o0:=fin inW relu X W b i j r.a nf r.t0 (rdW b ju) let o1:=fin inW relu X W b i (j+1) r.b nf r.t1 (rdW b (ju+1)) let o2:=fin inW relu X W b i (j+2) r.c nf r.t2 (rdW b (ju+2)) let o3:=fin inW relu X W b i (j+3) r.d nf r.t3 (rdW b (ju+3)) let out:=wrW out (base+ju) o0 let out:=wrW out (base+ju+1) o1 let out:=wrW out (base+ju+2) o2 let out:=wrW out (base+ju+3) o3 cols1 inW outW relu X W b i ix x nf m n (j+4) base out else out else if h1:j< outW then let ju:=j.toUSize let w0:=ju*m if hg:n.toNat<=x.size∧n.toNat<=ix.size∧W.size<2^32∧w0.toNat+m.toNat<=W.size then let r:=go1 x ix W w0 m n 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2 0 0 let o:=fin inW relu X W b i j r.s nf r.t (rdW b ju) cols1 inW outW relu X W b i ix x nf m n (j+1) base (wrW out (base+ju) o) else out else out termination_by outW-j def rows1 (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (nf:Float) (m:USize) (i:Nat) (ix:Array UInt32) (buf:FloatArray) (out:Array UInt32):Array UInt32 := if h:i< rows then let r:=decR X (i*inW).toUSize m 0 ix buf 0 let out:=cols1 inW outW relu X W b i r.ix r.buf nf m r.c 0 (i*outW).toUSize out rows1 rows inW outW relu X W b nf m (i+1) r.ix r.buf out else out termination_by rows-i structure F8 where (s0 s1 s2 s3 s4 s5 s6 s7 t:Float) lemma xi (xs:FloatArray) (i t n:USize) (k:Nat) (hk:k<8) (ht:t.toNat< n.toNat) (hi:i.toNat=8*t.toNat) (hx:8*n.toNat<=xs.size) (hs:xs.size<2^32) : (i+USize.ofNat k).toNat< xs.size:=by rw[uadd _ _ (by rw[uk k א]; omega),uk k א]; omega def go8 (xs:FloatArray) (W:Array UInt32) (wo n t i:USize) (hx:8*n.toNat<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:wo.toNat+n.toNat<=W.size) (hi:i.toNat=8*t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a:Float):F8 := if h:t< n then have ht:=υ.mp h let v:=dv (W.uget (wo+t) (wi wo t n W hw hs ht)) go8 xs W wo n (t+1) (i+USize.ofNat 8) hx hsx hs hw (by rw[usz t n h,uadd _ _ (by rw[uk 8 א]; omega),uk 8 א]; omega) (s0+v*xs.uget (i+USize.ofNat 0) (xi xs i t n 0 א ht hi hx hsx)) (s1+v*xs.uget (i+USize.ofNat 1) (xi xs i t n 1 א ht hi hx hsx)) (s2+v*xs.uget (i+USize.ofNat 2) (xi xs i t n 2 א ht hi hx hsx)) (s3+v*xs.uget (i+USize.ofNat 3) (xi xs i t n 3 א ht hi hx hsx)) (s4+v*xs.uget (i+USize.ofNat 4) (xi xs i t n 4 א ht hi hx hsx)) (s5+v*xs.uget (i+USize.ofNat 5) (xi xs i t n 5 א ht hi hx hsx)) (s6+v*xs.uget (i+USize.ofNat 6) (xi xs i t n 6 א ht hi hx hsx)) (s7+v*xs.uget (i+USize.ofNat 7) (xi xs i t n 7 א ht hi hx hsx)) (a+v.abs) else ⟨s0,s1,s2,s3,s4,s5,s6,s7,a⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h]; omega structure H8 where (xs:FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7:UInt32) @[inline] def mx (a w:UInt32):UInt32 := let m:=w &&& 0x7FFFFFFF if m<=a then a else m def dec8 (X:Array UInt32) (n t:USize) (xs:FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7:UInt32):H8 := if h:t< n then let w0:=rdW X t let w1:=rdW X (n+t) let w2:=rdW X (2*n+t) let w3:=rdW X (3*n+t) let w4:=rdW X (4*n+t) let w5:=rdW X (5*n+t) let w6:=rdW X (6*n+t) let w7:=rdW X (7*n+t) dec8 X n (t+1) ((((((((xs.push (dv w0)).push (dv w1)).push (dv w2)).push (dv w3)).push (dv w4)).push (dv w5)).push (dv w6)).push (dv w7)) (mx h0 w0) (mx h1 w1) (mx h2 w2) (mx h3 w3) (mx h4 w4) (mx h5 w5) (mx h6 w6) (mx h7 w7) else ⟨xs,h0,h1,h2,h3,h4,h5,h6,h7⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h]; have:=υ.mp h; omega def cols8 (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (d:H8) (nf:Float) (n:USize) (j:Nat) (out:Array UInt32):Array UInt32 := if hj:j< outW then let ju:=j.toUSize let wo:=ju*n if hg:8*n.toNat<=d.xs.size∧d.xs.size<2^32∧W.size<2^32∧wo.toNat+n.toNat<=W.size then let r:=go8 d.xs W wo n 0 0 hg.1 hg.2.1 hg.2.2.1 hg.2.2.2 (by rw[show (0:USize).toNat=0 from rfl]) 0 0 0 0 0 0 0 0 0 let bw:=rdW b ju let ow:=outW.toUSize let out:=wrW out ju (fin8 inW relu X W b 0 j r.s0 nf r.t d.h0 bw n) let out:=wrW out (ow+ju) (fin8 inW relu X W b 1 j r.s1 nf r.t d.h1 bw n) let out:=wrW out (2*ow+ju) (fin8 inW relu X W b 2 j r.s2 nf r.t d.h2 bw n) let out:=wrW out (3*ow+ju) (fin8 inW relu X W b 3 j r.s3 nf r.t d.h3 bw n) let out:=wrW out (4*ow+ju) (fin8 inW relu X W b 4 j r.s4 nf r.t d.h4 bw n) let out:=wrW out (5*ow+ju) (fin8 inW relu X W b 5 j r.s5 nf r.t d.h5 bw n) let out:=wrW out (6*ow+ju) (fin8 inW relu X W b 6 j r.s6 nf r.t d.h6 bw n) let out:=wrW out (7*ow+ju) (fin8 inW relu X W b 7 j r.s7 nf r.t d.h7 bw n) cols8 inW outW relu X W b d nf n (j+1) out else out else out termination_by outW-j def linF (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32):Array UInt32 := if rows*inW=X.size∧outW*inW=W.size∧outW=b.size∧0< inW∧0< outW∧inW<2^20 ∧ X.size<2^32∧W.size<2^32∧b.size<2^32∧rows*outW<2^32 ∧ scan X X.usize 0 (usize_le X) 0xFFFFFFFF 0∧scan W W.usize 0 (usize_le W) 0xFFFFFFFF 0 ∧ scan b b.usize 0 (usize_le b) 0xFFFFFFFF 0 then let n:=inW.toUSize let nf:Float:=(inW.toUInt64+8).toFloat*Float.ofBits 0x3CC0000000000000 let out:Array UInt32:=Χ (rows*outW) 0 if rows == 8 then cols8 inW outW relu X W b (dec8 X n 0 (FloatArray.emptyWithCapacity (8*inW)) 0 0 0 0 0 0 0 0) nf n 0 out else rows1 rows inW outW relu X W b nf n 0 (Χ inW 0) (pushZ n 0 (FloatArray.emptyWithCapacity inW)) out else linearWith Yn rows inW outW relu X W b 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 qEw:FloatFormat.binary64.isIEEE=true:=by decide lemma qAl (a:Model FloatFormat.binary64) : BI a=(Model.toModel a).isFinite:=by have hd:=Model.toDyadic?_ieee_eq_model _ qEw a cases hm:Model.toModel a with | notANumber => rw[hm] at hd cases h:BI 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:BI 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 qFc (x:Float):Model FloatFormat.binary64:=Model.ofFloatModel x.toModel lemma qE6 (x:Float):R x=Model.toReal (qFc x):=rfl lemma qBk (x:Float):BI (qFc x)=x.isFinite:=by rw[qAl,Model.toModel_ofFloatModel] rfl lemma qCp (e:ℤ):Formats.Flocq.bpow FloatLib.Numerics.binaryRadix e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,Fa] lemma qCx:(2:ℝ)^(1000:ℤ)<=Model.toReal (Model.posMaxFinite FloatFormat.binary64):=by rw[Model.toReal_posMaxFinite] have h1:Ct FloatFormat.binary64.fracWidth+FloatFormat.binary64.maxFiniteFracField =2^53-1:=by decide +kernel have h2:FloatFormat.binary64.maxNormalExponent-BG FloatFormat.binary64.fracWidth =971:=by decide +kernel rw[h1,h2] have h3:Model.bpow 971=(2:ℝ)^(971:ℤ):=by simp only[Model.bpow,qCp] rw[h3,show (1000:ℤ)=29+971 from rfl,zpow_add₀ (by norm_num)] apply mul_le_mul_of_nonneg_right _ (by positivity) norm_num lemma qEa (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:qFc (x+y)=Model.add (qFc x) (qFc y) := toModel_ofFloat_add_of_isFinite x y hx hy have hfx:BI (qFc x)=true:=by rw[qBk,hx] have hfy:BI (qFc y)=true:=by rw[qBk,hy] constructor . rw[<-qBk,hm] exact Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qEw hfx hfy (hb.trans qCx) . rw[qE6,hm] exact Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qEw hfx hfy (hb.trans qCx) lemma qEg (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:qFc (x-y)=Model.sub (qFc x) (qFc y) := toModel_ofFloat_sub_of_isFinite x y hx hy have hfx:BI (qFc x)=true:=by rw[qBk,hx] have hfy:BI (qFc y)=true:=by rw[qBk,hy] constructor . rw[<-qBk,hm] exact Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qEw hfx hfy (hb.trans qCx) . rw[qE6,hm] exact Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qEw hfx hfy (hb.trans qCx) open Float.Model.UnpackedFloat in lemma qB2 (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 le_trans ?_ Nat.left_le_or simp[Α] . omega open Float.Model.UnpackedFloat in lemma qA7 (spec:Float.Model.Format) (s:Sign) (m:ℕ) (e:ℤ) (h:e<=spec.targetExponent (Bb 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 (Bb m e)).toNat=0 :=א simp[h0] open Float.Model.UnpackedFloat in lemma qEe (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 (Bb (m₁*m₂) (e₁+e₂)):=by have c₁:=qB2 _ _ _ _ _ hu₁ have c₂:=qB2 _ _ _ _ _ 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=>(CH hm0).2 h simp only[BH,Bb, Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] rcases c₁ with ⟨c₁,_⟩ | c₁ . have:=hlog (le_trans c₁ (Nat.le_mul_of_pos_right _ h₂)) apply le_max_of_le_left norm_num omega . rcases c₂ with ⟨c₂,_⟩ | c₂ . have:=hlog (le_trans 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 qAo (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) : qFc (x*y)=Model.mul (qFc x) (qFc y):=by have hX:Model.toModel (qFc x)=x.toModel.unpack:=Model.toModel_ofFloatModel _ have hY:Model.toModel (qFc 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 _ qEw (qFc x),Model.toDyadic?_ieee_eq_model _ qEw (qFc 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,φ] 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:=qEe s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂ _ _ hxm hym have h0:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] rw[<-qA7 _ _ _ _ hle,h0] have hs:Model.modelSign (Model.modelSignBit s₁ ^^ Model.modelSignBit s₂)=s₁*s₂:=by cases s₁ <;> cases s₂ <;> rfl simp only[φ,ite_false,Bt,qEw,ite_true, Model.ieeeRoundDyadic,hs] rfl lemma qD3 (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:qFc (x*y)=Model.mul (qFc x) (qFc y):=qAo x y hx hy have hfx:BI (qFc x)=true:=by rw[qBk,hx] have hfy:BI (qFc y)=true:=by rw[qBk,hy] constructor . rw[<-qBk,hm] exact Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qEw hfx hfy (hb.trans qCx) . rw[qE6,hm] exact Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qEw hfx hfy (hb.trans qCx) noncomputable def qFa:ℝ:=2^(-1021:ℤ) noncomputable def qE5:ℝ:=2^(300:ℤ) lemma qCg:0< qFa:=by unfold qFa; positivity lemma qC1:qFa<=1/2^200*(1/2^200):=by have:(1/2^200*(1/2^200):ℝ)=2^(-400:ℤ):=by rw[show (-400:ℤ)=-200+-200 from rfl,zpow_add₀ (by norm_num)] norm_num rw[this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) lemma qCa:0< qE5:=by unfold qE5; positivity lemma qDj:(2:ℝ)^256*2^42<=qE5:=by have:((2:ℝ)^256*2^42)=2^(298:ℤ):=by rw[show (298:ℤ)=256+42 from rfl,zpow_add₀ (by norm_num)] norm_num rw[this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) lemma qC5:qE5*2<=2^(1000:ℤ):=by have:qE5*2=2^(301:ℤ):=by unfold qE5 rw[show (301:ℤ)=300+1 from rfl,zpow_add₀ (by norm_num)] norm_num rw[this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) lemma qDf:qE5*qE5<=2^(1000:ℤ):=by have:qE5*qE5=2^(600:ℤ):=by unfold qE5 rw[show (600:ℤ)=300+300 from rfl,zpow_add₀ (by norm_num)] rw[this] exact zpow_le_zpow_right₀ (by norm_num) (by norm_num) lemma qDp (x:ℝ) : |Model.roundAt FloatFormat.binary64 x-x| ≤ 1/2^53*(|x| + qFa):=by have ez:(1/2^53*(|x| + qFa):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ):=by have e1:(1/2^53:ℝ)=2^(-53:ℤ):=by norm_num rw[e1,show (-1074:ℤ)=-53+-1021 from rfl,zpow_add₀ (by norm_num) (-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[qCp] at hl rw[qCp,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[<-zpow_add₀ (by norm_num),show (-53:ℤ)+(M-1)=M-53-1 by ring, zpow_sub₀ (by norm_num) _ 1,zpow_one] rw[e] have:=mul_le_mul_of_nonneg_left 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 qCs (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ qE5) (h2 : |R y| ≤ qE5) : (x+y).isFinite=true∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + qFa):=by obtain ⟨hf,he⟩:=qEa x y hx hy (by have:=qC5; linarith) exact ⟨hf,he ▸ qDp _⟩ lemma qCu (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ qE5) (h2 : |R y| ≤ qE5) : (x-y).isFinite=true∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + qFa):=by obtain ⟨hf,he⟩:=qEg x y hx hy (by have:=qC5; linarith) exact ⟨hf,he ▸ qDp _⟩ lemma qDb (x y:Float) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ qE5) (h2 : |R y| ≤ qE5) : (x*y).isFinite=true∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + qFa):=by have hb : |R x| * |R y| ≤ 2^(1000:ℤ) := (mul_le_mul h1 h2 (abs_nonneg _) qCa.le).trans qDf obtain ⟨hf,he⟩:=qD3 x y hx hy hb exact ⟨hf,he ▸ qDp _⟩ lemma qAe (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,qCp] . exact hm . exact hk lemma qA5 (x:Float):R x=Model.unpackedToReal x.toModel.unpack:=by rw[qE6,Model.toReal_eq_unpackedToReal_toModel qEw,Model.toModel_ofFloatModel] lemma qBj (f:Float) (s m e h) (hu:f.toModel.unpack=.finite s m e h) : (BT FloatFormat.binary64 (.finite .positive m e h)).toModel=.finite .positive m e h:=by have hm:Model.toModel (qFc 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 (qFc f)).isFinite=true:=by rw[hm]; rfl have hn:=Model.toModel_neg_of_isFinite qEw (qFc 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 qD7 (f:Float) (hf:f.isFinite=true):f.abs.isFinite=true∧R f.abs = |R f|:=by have hA:qFc f.abs=BT 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[<-qBk,hA] exact Model.isFinite_ofModel_zero _ qEw _ . rw[qE6 f.abs,hA,qA5 f,hu] simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qEw] | finite s m e h => rw[hu] at hA have hk:=qBj f s m e h hu change Model.toModel (BT FloatFormat.binary64 (.finite .positive m e h))=_ at hk constructor . rw[<-qBk,hA,qAl] change (Model.toModel (BT FloatFormat.binary64 (.finite .positive m e h))).isFinite=_ rw[hk]; rfl . rw[qE6 f.abs,hA,qA5 f,hu] change Model.toReal (BT FloatFormat.binary64 (.finite .positive m e h))=_ rw[Model.toReal_eq_unpackedToReal_toModel qEw,hk] simp only[Model.unpackedToReal_finite,qCp] have h0:(0:ℝ)<=(m:ℝ)*2^e:=by positivity cases s <;> simp[Model.modelSignBit,abs_of_nonneg h0] lemma qB0 (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:qFc f=Bt 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 _ qEw (k:ℤ) have hd:(FloatLib.Numerics.Dyadic.ofScaledInt k 0).toReal=k:=by rw[<-FloatLib.Numerics.Dyadic.cast_toRat,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:=by norm_num calc (k:ℝ)<=2^53:=h1 _<=2^256*2^42:=h2 _<=qE5:=qDj _<=qE5*2:=by linarith [qCa] _<=_:=qC5 have hfin:BI (qFc f)=true:=by rw[hA] apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qEw rw[hd,abs_of_nonneg (Nat.cast_nonneg _)] exact hkR.trans qCx refine ⟨by rw[<-qBk]; exact hfin,?_⟩ rw[qE6,hA,Model.toReal_roundDyadic_eq_roundAt _ qEw _ (hA ▸ hfin),hd] have:=qAe k 0 (by simpa using hk) (by norm_num) simpa using this lemma qCl (m:UInt32) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat := qB0 _ _ h rfl lemma qB1 (m:UInt64) (h:m.toNat<2^53):m.toFloat.isFinite=true∧R m.toFloat=m.toNat := qB0 _ _ h rfl lemma qAn:(0:Float).toModel.unpack=.zero .positive:=by rfl lemma qDc:(0:Float).isFinite=true∧R 0=0:=by refine ⟨?_,?_⟩ . change (0:Float).toModel.unpack.isFinite=true rw[qAn]; rfl . rw[qA5,qAn]; simp def qC8 (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 qBm (i:ℕ):pwEntry i=Float.ofBits (qC8 i):=by unfold pwEntry qC8 dsimp only split <;> rfl def qDn (i:ℕ):Bool := match Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (qC8 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 qBl:∀ i,i<512->qDn i=true:=by decide +kernel lemma qEq (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:=qBl i hi rw[qBm] have hU:(Float.ofBits (qC8 i)).toModel.unpack = Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (qC8 i).toBitVec := Model.unpack_pack_unpack _ _ unfold qDn at hc rw[qA5,hU] change (Float.ofBits (qC8 i)).toModel.unpack.isFinite=true∧_ rw[hU] cases hu:Float.Model.UnpackedFloat.unpack Float.Model.Format.binary64 (qC8 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[Ω,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[Ei,DF,<-zpow_natCast,<-zpow_add₀ (by norm_num)] congr 1 push_cast ring rw[Model.unpackedToReal_finite,qCp,h3,h1,h2,mul_assoc,e] simp only[decide_eq_true_eq,h4,↓reduceIte] lemma qBv (i:USize) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,pw_data] lemma qCn (w:UInt32):(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[AH] exact ϡ w.toNat 31 lemma qB8 (w:UInt32):(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[AH] exact ϡ w.toNat 23 lemma qDx (w:UInt32):(w >>> (23:UInt32)).toNat=w.toNat/2^23:=by rw[Ϣ,ψ] rfl lemma qEb (w:UInt32):(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf rw[AH,qDx] exact ϡ _ 8 lemma qC4 (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[AH] show w.toNat &&& 2^31=_ rw[Nat.and_two_pow,BJ] have h2:w.toNat/2^31%2=0∨w.toNat/2^31%2=1 :=א rcases h2 with h2 | h2 <;> rw[h2] <;> rfl rw[AB] simp only[bne_iff_ne,ne_eq,decide_eq_true_eq] rw[<-ύ,h] simp only[UInt32.toNat_zero] omega lemma qCz (w:UInt32) : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,qB8,Nat.or_comm] have h:w.toNat%2^23<2^23:=Ε _ (by positivity) have:=Nat.two_pow_add_eq_or_of_lt h 1 simpa using this.symm lemma qC9 (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:=qCn w have hlt:=(w &&& 0x7FFFFFFF).toNat_lt unfold good at h simp only[Ϸ,beq_iff_eq,decide_eq_true_eq] at h rcases h with h | h . left rw[<-hv,h]; rfl . right rw[AJ,CX] at h rw[<-hv] generalize (w &&& 0x7FFFFFFF).toNat=v at * simp at h omega noncomputable def qD6 (v:ℕ):ℝ := if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma qAh (v:ℕ):0<=qD6 v:=by unfold qD6; split_ifs <;> positivity lemma qB5 (w:UInt32) (hg:good w=true) : wv w=(if 2^31<=w.toNat then -1 else 1)*qD6 (w.toNat%2^31):=by have hN:=w.toNat_lt have hgd:=qC9 w hg have hE:=qEb w have hF:(fracOf w).toNat=w.toNat%2^23:=qB8 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 qD6 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,qC4] simp split_ifs <;> ring lemma qBy (v:ℕ) (h:v<0x7F800000):qD6 v<=2^128:=by unfold qD6 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:ℤ) := zpow_le_zpow_right₀ (by norm_num) א have h3:(2:ℝ)^(104:ℤ)=2^104:=by norm_num rw[h3] at h2 calc _<=(2:ℝ)^24*2^104:=mul_le_mul h1 h2 (by positivity) (by positivity) _=2^128:=by norm_num lemma qAv (a b:ℕ) (hab:a<=b) (ha:a=0∨2^23<=a):qD6 a<=qD6 b:=by rcases ha with rfl | ha . simpa [qD6] using qAh b unfold qD6 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 mul_le_mul_of_nonneg_right _ (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₀ (by norm_num)] exact zpow_le_zpow_right₀ (by norm_num) א 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) := mul_le_mul_of_nonneg_right h1 (by positivity) _=2^23*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by ring _<=_:=mul_le_mul h3 h2 (by positivity) (by positivity) lemma qEo (w:UInt32) (hg:good w=true):(dv w).isFinite=true∧R (dv w)=wv w:=by have hN:=w.toNat_lt have hgd:=qC9 w hg have hM:=qCz w have hm:=qCl ((w &&& 0x7FFFFF) ||| 0x800000) (by rw[hM]; omega) have hI:(w >>> (23:UInt32)).toUSize.toNat=w.toNat/2^23:=by rw[AI,qDx] have hp:=qEq (w.toNat/2^23) א unfold dv rw[qBv,hI] have hR1 : |R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| ≤ 2^24:=by rw[hm.2,hM,abs_of_nonneg (Nat.cast_nonneg _)] 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:ℤ) := zpow_le_zpow_right₀ (by norm_num) א simpa using this have hb : |R ((w &&& 0x7FFFFF) ||| 0x800000).toFloat| * |R (pwEntry (w.toNat/2^23))| ≤ 2^(1000:ℤ):=by have h1:=qDj have e1:(2:ℝ)^24<=2^256*2^42:=by norm_num have e2:(2:ℝ)^106<=2^256*2^42:=by norm_num exact (mul_le_mul (hR1.trans (e1.trans h1)) (hR2.trans (e2.trans h1)) (abs_nonneg _) qCa.le).trans qDf obtain ⟨hf,he⟩:=qD3 _ _ hm.1 hp.1 hb refine ⟨hf,?_⟩ rw[he,hm.2,hp.2,qB5 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[qD6] . 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 qD6 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:=qAe (-(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:=qAe (M:ℤ) k (by simpa using hMl) hk simp only[hs,↓reduceIte,one_mul] push_cast at this exact this def qCt (l:List Float):Float:=l.foldl (.+·) 0 noncomputable def qEm (l:List Float):ℝ:=(l.map λq=>|R q|).sum noncomputable def qEs (l:List Float):ℝ:=(l.map R).sum lemma qAq (l:List Float):0<=qEm l:=by induction l with | nil=>simp[qEm] | cons q l ih => simp only[qEm,CR,DV] at ih ⊢ positivity lemma qBb (l:List Float) (q:Float):qEm (l ++ [q])=qEm l + |R q|:=by simp[qEm] lemma qBa (l:List Float) (q:Float):qEs (l ++ [q])=qEs l+R q:=by simp[qEs] lemma qAm (l:List Float) (q:Float):qCt (l ++ [q])=qCt l+q:=by simp[qCt] lemma qBp (l:List Float) : |qEs l| ≤ qEm l:=by induction l with | nil=>simp[qEm,qEs] | cons q l ih => simp only[qEm,qEs,CR,DV] at ih ⊢ exact (abs_add_le _ _).trans (by linarith) lemma qAr (l:List Float) (hf:∀ q∈l,q.isFinite=true) (hlen:l.length<=2^21) (hb:qEm l<=qE5/4) : (qCt l).isFinite=true ∧ |R (qCt l)-qEs l| ≤ 2*l.length*(1/2^53*(qEm l+qFa)):=by induction l using List.reverseRecOn with | nil => simp only[qCt,List.foldl_nil,Dr] refine ⟨qDc.1,?_⟩ simp[qDc.2,qEs] | append_singleton l q ih => rw[qBb] at hb have hP:=qAq l have hq0:=abs_nonneg (R q) obtain ⟨h1,h2⟩:=ih (λx hx=>hf x (by simp[hx])) (by simp at hlen; omega) (by linarith) have hk:(l.length:ℝ)+1<=2^21:=by simp at hlen exact_mod_cast hlen have hk0:(0:ℝ)<=l.length:=Nat.cast_nonneg _ have hS:=qBp l have hz:=qCg have hz1:=qC1 have hB:=qCa have hBl:=qDj rw[qAm,qBb,qBa,List.length_append,List.length_singleton] have hB1:(8:ℝ)<=qE5:=by have:(8:ℝ)<=2^256*2^42:=by norm_num linarith have hz':qFa<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=by norm_num linarith push_cast set r:=R (qCt l) set a:=R q set P:=qEm l set S:=qEs l set k:ℝ:=(l.length:ℝ) set W:=(1/2^53:ℝ)*(P + |a| + qFa) with hWdef have hW:(1/2^53:ℝ)*(P+qFa)<=W:=by rw[hWdef] apply mul_le_mul_of_nonneg_left _ (by norm_num) linarith have hW0:0<=W:=by rw[hWdef]; positivity have hD : |r-S| ≤ 2*k*W:=h2.trans (mul_le_mul_of_nonneg_left 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) := mul_le_mul_of_nonneg_left hD (by norm_num) _=(2*k*(1/2^53))*W:=by ring _<=1*W:=mul_le_mul_of_nonneg_right hk2 hW0 _=W:=one_mul _ have hDP : |r-S| ≤ P+1 := calc |r-S| ≤ 2*k*(1/2^53*(P+qFa)):=h2 _=(2*k*(1/2^53))*(P+qFa):=by ring _<=1*(P+qFa):=mul_le_mul_of_nonneg_right hk2 (by positivity) _<=P+1:=by linarith have hr : |r| ≤ qE5:=by have e:r=(r-S)+S:=by ring have:=abs_add_le (r-S) S rw[<-e] at this linarith have ha : |a| ≤ qE5:=by linarith obtain ⟨hf3,he⟩:=qCs (qCt l) q h1 (hf q (by simp)) hr ha refine ⟨hf3,?_⟩ have t1 : |R (qCt l+q)-(S+a)| ≤ |R (qCt l+q)-(r+a)| + |r-S|:=by have e:R (qCt l+q)-(S+a)=(R (qCt l+q)-(r+a))+(r-S):=by ring rw[e] exact abs_add_le _ _ have t2 : |r+a| ≤ |r-S| + P + |a|:=by have e:r+a=(r-S)+S+a:=by ring rw[e] have h3:=abs_add_le ((r-S)+S) a have h4:=abs_add_le (r-S) S linarith have t3:(1/2^53:ℝ)*(|r+a| + qFa)<=1/2^53 * |r-S| + W:=by rw[hWdef,<-mul_add] apply mul_le_mul_of_nonneg_left _ (by norm_num) linarith calc |R (qCt l+q)-(S+a)| ≤ W+W+2*k*W:=by linarith _=2*(k+1)*W:=by ring lemma qEr (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[ξ,h8] omega have h1:=qB1 (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[qA5,hu,Model.unpackedToReal_finite,qCp] simp norm_num have h4:(n:ℝ)+8<=2^21:=by have:n+8<=2^21 :=א exact_mod_cast this obtain ⟨hf,he⟩:=qD3 _ _ h1.1 h2 (by rw[h1.2,h3,ht] have h5:(1:ℝ)<=2^256*2^42:=by norm_num have h6:=qDj have h7:(2:ℝ)^21<=2^256*2^42:=by norm_num refine le_trans (mul_le_mul ?_ ?_ (abs_nonneg _) qCa.le) qDf . rw[ER,abs_of_nonneg (by positivity)] push_cast linarith . rw[abs_of_nonneg (by positivity)] have:(1:ℝ)/2^51<=1:=by norm_num linarith) unfold nfOf refine ⟨hf,?_⟩ rw[he,h1.2,h3,ht] have:=qAe ((n:ℤ)+8) (-51) א (by norm_num) push_cast at this ⊢ have e:(2:ℝ)^(-51:ℤ)=1/2^51:=by norm_num rw[e] at this rw[this] ring lemma qC2 (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:=by linarith 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:=by norm_num calc c/2^51<=2^21/2^51:=by gcongr; linarith _=1/2^30:=by norm_num 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) := mul_le_mul hcG hT' (abs_nonneg _) (by positivity) _=_:=by ring have hE1 : |E| ≤ |E-G| + |G|:=by have e:E=(E-G)+G:=by ring have:=abs_add_le (E-G) G rwa [<-e] at this have hSE:=abs_add_le S E have hG0:=abs_nonneg G have hE0:=abs_nonneg E have hYl:1/2^129<=Y:=by linarith have hT3:Y-Y/2^30-1/2^250<=T:=by linarith have hT4:0<=Y-Y/2^30-1/2^250:=by linarith have hT5:0<=T:=by linarith have hG1:0<=G:=by rw[hGdef]; positivity rw[abs_of_nonneg 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:=by norm_num linarith exact mul_le_mul_of_nonneg_right 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:=by norm_num linarith exact mul_le_mul_of_nonneg_right 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 qCk (n:ℕ) (hn:n<2^20) (s t:Float) (hs:s.isFinite=true) (ht:t.isFinite=true) (X Y:ℝ) (hY0:0<=Y) (hYB:Y<=qE5/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:=qCa have hB8:(8:ℝ)<=qE5:=by have h:(8:ℝ)<=2^256*2^42:=by norm_num linarith [qDj] have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hn0:(0:ℝ)<=n:=Nat.cast_nonneg n obtain ⟨hnf,hnv⟩:=qEr n hn have hT : |R t| ≤ 3/8*qE5:=by have e:R t=(R t-Y)+Y:=by ring have:=abs_add_le (R t-Y) Y rw[<-e,abs_of_nonneg hY0] at this have h:(1:ℝ)/2^250<=1:=by norm_num have h':Y/2^30<=Y:=by apply div_le_self hY0; norm_num linarith have hT3 : |R t| ≤ qE5:=by linarith have hN : |R (nfOf n)| ≤ qE5:=by rw[hnv,abs_of_nonneg (by positivity)] have:((n:ℝ)+8)/2^51<=1:=by rw[div_le_one (by positivity)] norm_num at hnR ⊢ linarith linarith obtain ⟨hef,heE⟩:=qDb _ _ hnf ht hN hT3 have hNT : |R (nfOf n)*R t| ≤ |R t|:=by rw[abs_mul,hnv,abs_of_nonneg (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 [abs_nonneg (R t)] have hz:=qCg have hz1:=qC1 have hz2:qFa<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=by norm_num linarith have hE : |R (nfOf n*t)| ≤ qE5:=by have e:R (nfOf n*t)=(R (nfOf n*t)-R (nfOf n)*R t)+R (nfOf n)*R t:=by ring have:=abs_add_le (R (nfOf n*t)-R (nfOf n)*R t) (R (nfOf n)*R t) rw[<-e] at this linarith have hSB : |R s| ≤ qE5:=by linarith obtain ⟨hpf,hpE⟩:=qCs s _ hs hef hSB hE obtain ⟨hmf,hmE⟩:=qCu s _ hs hef hSB hE have hSb1 : |R s+R (nfOf n*t)| ≤ |R s| + |R (nfOf n*t)|:=abs_add_le _ _ 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:=abs_add_le (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)| + qFa):=by have e:(2:ℝ)^(-127:ℤ)=1/2^127:=by norm_num rw[e] at hbig linarith have key:=qC2 ((n:ℝ)+8) Y (R s) (R t) (R (nfOf n*t)) qFa (by linarith) (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 qDd (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[AO,CR,ih] lemma qBw (l:List Float) (hf:∀ q∈l,q.isFinite=true) : qEm (l.map Float.abs)=qEm l∧qEs (l.map Float.abs)=qEm l:=by induction l with | nil=>simp[qEm,qEs] | cons q l ih => obtain ⟨h1,h2⟩:=ih (λx hx=>hf x (by simp[hx])) have h3:=(qD7 q (hf q (by simp))).2 simp only[qEm,qEs,CR,DV,Df] at h1 h2 ⊢ simp only[Function.comp_def,h3,abs_abs] simp only[Function.comp_def] at h1 h2 rw[h1,h2] simp lemma qB7 (ts:List ℕ) (q:ℕ->Float) (p:ℕ->ℝ) (hq:∀ t∈ts,|R (q t)-p t| ≤ 1/2^53*(|p t| + qFa)) : |qEs (ts.map q)-(ts.map p).sum| ≤ 1/2^53*((ts.map λt=>|p t|).sum+ts.length*qFa) ∧ qEm (ts.map q)<=(ts.map λt=>|p t|).sum + 1/2^53*((ts.map λt=>|p t|).sum+ts.length*qFa) ∧ (ts.map λt=>|p t|).sum<=2*qEm (ts.map q)+ts.length*qFa:=by induction ts with | nil=>simp[qEm,qEs] | cons x ts ih => obtain ⟨h1,h2,h3⟩:=ih (λt ht=>hq t (by simp[ht])) have h4:=hq x (by simp) simp only[qEm,qEs,CR,DV,Df,Ex] at h1 h2 h3 ⊢ simp only[Function.comp_def] at h1 h2 h3 ⊢ have hz:=qCg 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| + qFa):=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| + qFa):=by have e:R (q x)=p x+(R (q x)-p x):=by ring have:=abs_add_le (p x) (R (q x)-p x) rw[<-e] at this linarith have hp0:=abs_nonneg (p x) refine ⟨abs_le.mpr ⟨?_,?_⟩,?_,?_⟩ <;> push_cast <;> nlinarith lemma qBr (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 (by simp) simp only[CR,DV,Ex] push_cast linarith lemma qBz (w:UInt32) (hg:good w=true) : |wv w| = qD6 (w.toNat%2^31):=by rw[qB5 w hg,abs_mul,abs_of_nonneg (qAh _)] split_ifs <;> simp lemma qCi (w:UInt32) (hg:good w=true) : |wv w| ≤ 2^128:=by rw[qBz w hg] apply qBy rcases qC9 w hg with h | h <;> omega lemma qBg (w:UInt32) (hg:good w=true) (h:w &&& 0x7FFFFFFF=0):wv w=0:=by rw[qB5 w hg,<-qCn w,h] simp[qD6] lemma qCj:(2:ℝ)^128<=qE5:=by have h:(2:ℝ)^128<=2^256*2^42:=by norm_num linarith [qDj] lemma qDi (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| + qFa) ∧ |wv a*wv b| ≤ 2^256:=by obtain ⟨fa,ra⟩:=qEo a ha obtain ⟨fb,rb⟩:=qEo b hb have la:=qCi a ha have lb:=qCi b hb have hab : |wv a*wv b| ≤ 2^256:=by rw[abs_mul] calc |wv a| * |wv b| ≤ 2^128*2^128:=mul_le_mul la lb (abs_nonneg _) (by positivity) _=2^256:=by norm_num obtain ⟨h1,h2⟩:=qDb _ _ fa fb (by rw[ra]; exact la.trans qCj) (by rw[rb]; exact lb.trans qCj) rw[ra,rb] at h2 exact ⟨h1,h2,hab⟩ lemma qAf (n:ℕ) (xw ww:ℕ->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (l:List ℕ) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& 0x7FFFFFFF=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 have h1:t< n:=by simpa using ht have h2:t∉l:=by simpa using hnl simp[qBg (xw t) (hx t h1) (hz t h1 h2)] lemma qDr (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 qAk (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| + qFa) ∧ |p t| ≤ 2^256) (hbf:b.isFinite=true) (hbv:R b=bv) (hbl : |bv| ≤ 2^128) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (qCt (ts.map g ++ [b]) + nfOf n*qCt ((ts.map g ++ [b]).map Float.abs))|) : R (qCt (ts.map g ++ [b])-nfOf n*qCt ((ts.map g ++ [b]).map Float.abs)) < (ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (qCt (ts.map g ++ [b]) + nfOf n*qCt ((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,EZ,List.mem_singleton] at hq rcases hq with ⟨t,ht,rfl⟩ | rfl . exact (hg t ht).1 . exact hbf obtain ⟨hA1,hA2,hA3⟩:=qB7 ts g p (λt ht=>(hg t ht).2.1) have hAle:=qBr ts (λt=>|p t|) _ (λt ht=>(hg t ht).2.2) have hsa:=qBb (ts.map g) b have hsr:=qBa (ts.map g) b have hsag:=qAq (ts.map g) have hsa0:=qAq (ts.map g ++ [b]) rw[hbv] at hsa hsr have hk:(ts.length:ℝ)<=n:=by exact_mod_cast hlen have hk0:(0:ℝ)<=ts.length:=Nat.cast_nonneg _ have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hz:=qCg have hz1:=qC1 have hz2:qFa<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=by norm_num linarith have hA0:0<=(ts.map λt=>|p t|).sum := List.sum_nonneg (by simp only[EZ]; rintro _ ⟨t,_,rfl⟩; exact abs_nonneg _) set A:=(ts.map λt=>|p t|).sum set k:ℝ:=(ts.length:ℝ) have hkz:k*qFa<=2^20:=by nlinarith have hAl:A<=2^20*2^256:=by refine hAle.trans (mul_le_mul_of_nonneg_right ?_ (by positivity)) linarith have hBl:=qDj have hnum:(2:ℝ)^20*2^256*3+2^128+2^20+2<=2^256*2^42/8:=by norm_num have hsaB:qEm (ts.map g ++ [b])+qFa<=qE5/8:=by have hU:(1/2^53:ℝ)*(A+k*qFa)<=A+k*qFa:=by have:0<=A+k*qFa:=by positivity nlinarith linarith have hB8:(1:ℝ)<=qE5/8:=by have:(1:ℝ)<=2^256*2^42/8:=by norm_num linarith have hlen1:(ts.map g ++ [b]).length<=2^21:=by simp; omega obtain ⟨hf1,he1⟩:=qAr _ hfq hlen1 (by linarith) obtain ⟨habs1,habs2⟩:=qBw _ hfq obtain ⟨hf2,he2⟩:=qAr ((ts.map g ++ [b]).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=EZ.mp hq exact (qD7 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:qEm (ts.map g ++ [b])=sa at * set Y:=sa+qFa 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:=by linarith have:=mul_le_mul_of_nonneg_right h hUY norm_num at this ⊢ linarith have hS1 : |qEs (ts.map g ++ [b])| ≤ sa:=by rw[<-hsaD]; exact qBp _ obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨he2a,he2b⟩:=abs_le.mp he2 refine qCk n hn _ _ hf1 hf2 _ Y hY0 (by linarith) ?_ ?_ ?_ hbig . have hKY:sa+k*qFa<=(k+1)*Y:=by rw[hYdef] nlinarith [mul_nonneg hk0 hsa0] have hU2:(1/2^53:ℝ)*(A+k*qFa)<=2*(k+1)*(1/2^53*Y):=by have h1:A+k*qFa<=2*(sa+k*qFa):=by linarith [abs_nonneg bv] have h2:A+k*qFa<=2*((k+1)*Y):=by linarith calc (1/2^53:ℝ)*(A+k*qFa)<=1/2^53*(2*((k+1)*Y)) := mul_le_mul_of_nonneg_left h2 (by norm_num) _=_:=by ring have hn4:4*(k+1)*(1/2^53*Y)<=4*((n:ℝ)+1)*(1/2^53*Y) := mul_le_mul_of_nonneg_right (by linarith) hUY obtain ⟨hA1a,hA1b⟩:=abs_le.mp hA1 apply abs_le.mpr constructor <;> linarith . apply abs_le.mpr constructor . have:(0:ℝ)<=1/2^250:=by norm_num have:qFa<=1/2^250:=by have:(1/2^200*(1/2^200):ℝ)<=1/2^250:=by norm_num linarith linarith . linarith . have e:R (qCt (ts.map g ++ [b])) = (R (qCt (ts.map g ++ [b]))-qEs (ts.map g ++ [b]))+qEs (ts.map g ++ [b]):=by ring have h1:=abs_add_le (R (qCt (ts.map g ++ [b]))-qEs (ts.map g ++ [b])) (qEs (ts.map g ++ [b])) rw[<-e] at h1 have h2:Y/2^30<=Y:=by apply div_le_self hY0; norm_num linarith lemma encl_tight (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):=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⟩ := qDd g (λt=>(g t).abs) l 0 0 obtain ⟨hs1,hs2⟩:=hs rw[hfold] at hs1 hs2 have hlen:=qDr n l hl hl2 have hgt:∀ t∈l,(g t).isFinite=true∧|R (g t)-p t| ≤ 1/2^53*(|p t| + qFa) ∧ |p t| ≤ 2^256:=λt ht=>qDi _ _ (hx t (hl2 t ht)) (hw t (hl2 t ht)) obtain ⟨hbf,hbv⟩:=qEo bw hb have hS:s=qCt (l.map g ++ [dv bw]):=by rw[hs1,qAm]; rfl have hE:e=nfOf n*qCt ((l.map g ++ [dv bw]).map Float.abs):=by rw[hs2] congr 1 rw[List.map_append,Df,List.map_singleton,qAm] rfl rw[hS,hE] at hbig ⊢ rw[qAf n xw ww bw hx l hl hl2 hz] exact qAk n hn l hlen g p (dv bw) (wv bw) hgt hbf hbv (qCi bw hb) hbig lemma qB3 (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):=mul_nonneg (by norm_num) (add_nonneg hJ hz0) have:=mul_le_mul_of_nonneg_right 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,abs_of_nonneg hH0] calc H * |Rj-J| ≤ H*((J+z)/2^32):=mul_le_mul_of_nonneg_left e1 hH0 _=_:=by ring have e3:H*z<=1/2^250:=by calc H*z<=2^128*(1/2^200*(1/2^200)) := mul_le_mul hH hz1 hz0 (by norm_num) _<=1/2^250:=by norm_num have hW:0<=H*J:=mul_nonneg hH0 hJ have hZ0:0<=H*z:=mul_nonneg 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:=abs_add_le Rg b rw[abs_of_nonneg hb] at this linarith apply abs_le.mpr constructor <;> linarith lemma qA9 (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 (by simp) simp only[CR,DV,mul_add] linarith lemma qAb (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| + qFa) ∧ |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 (qCt (ts.map g ++ [b]) + nfOf n*(f*qCt (ts.map λt=>(v t).abs)+b.abs))|) : R (qCt (ts.map g ++ [b])-nfOf n*(f*qCt (ts.map λt=>(v t).abs)+b.abs)) < (ts.map p).sum+bv ∧ (ts.map p).sum+bv< R (qCt (ts.map g ++ [b]) + nfOf n*(f*qCt (ts.map λt=>(v t).abs)+b.abs)):=by have hfq:∀ q∈ts.map g ++ [b],q.isFinite=true:=by intro q hq simp only[List.mem_append,EZ,List.mem_singleton] at hq rcases hq with ⟨t,ht,rfl⟩ | rfl . exact (hg t ht).1 . exact hbf obtain ⟨hA1,hA2,hA3⟩:=qB7 ts g p (λt ht=>(hg t ht).2.1) have hsa:=qBb (ts.map g) b have hsr:=qBa (ts.map g) b have hsa0:=qAq (ts.map g ++ [b]) rw[hbv] at hsa hsr have hk:(ts.length:ℝ)<=n:=by exact_mod_cast hlen have hk0:(0:ℝ)<=ts.length:=Nat.cast_nonneg _ have hnR:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have hz:=qCg have hz1:=qC1 have hz2:qFa<=1:=by have:(1/2^200*(1/2^200):ℝ)<=1:=by norm_num linarith have hA0:0<=(ts.map λt=>|p t|).sum := List.sum_nonneg (by simp only[EZ]; rintro _ ⟨t,_,rfl⟩; exact abs_nonneg _) have hJ0:0<=(ts.map λt=>|a t|).sum := List.sum_nonneg (by simp only[EZ]; rintro _ ⟨t,_,rfl⟩; exact abs_nonneg _) have hJl:=qBr ts (λt=>|a t|) _ (λt ht=>(hv t ht).2.2) have hAJ:=qA9 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[Df,Function.comp_def] have hvf:∀ q∈ts.map v,q.isFinite=true:=by intro q hq obtain ⟨t,ht,rfl⟩:=EZ.mp hq exact (hv t ht).1 obtain ⟨hv1,hv2⟩:=qBw (ts.map v) hvf have hJ1:qEm (ts.map v)=(ts.map λt=>|a t|).sum:=by simp only[qEm,Df] congr 1 apply Ck intro t ht simp[(hv t ht).2.1] rw[hvl] at hbig ⊢ rw[hJ1] at hv1 hv2 set A:=(ts.map λt=>|p t|).sum set J:=(ts.map λt=>|a t|).sum set k:ℝ:=(ts.length:ℝ) have hBl:=qDj have hB8:(1:ℝ)<=qE5/8:=by have:(1:ℝ)<=2^256*2^42/8:=by norm_num linarith have hkR:k<=2^20:=by linarith have hJ2:J<=2^20*2^128 := hJl.trans (mul_le_mul_of_nonneg_right hkR (by norm_num)) have hlenv:((ts.map v).map Float.abs).length<=2^21:=by simp; omega obtain ⟨hjf,hje⟩:=qAr ((ts.map v).map Float.abs) (by intro q hq obtain ⟨x,hx,rfl⟩:=EZ.mp hq exact (qD7 x (hvf x hx)).1) hlenv (by rw[hv1] have:(2:ℝ)^20*2^128<=2^256*2^42/4:=by norm_num linarith) rw[hv1,hv2] at hje have hl3:(((ts.map v).map Float.abs).length:ℝ)=k:=by simp[k] rw[hl3] at hje generalize hjD:qCt ((ts.map v).map Float.abs)=j at * have hk2:2*k*(1/2^53)<=1:=by norm_num at hkR ⊢ linarith have hje2 : |R j-J| ≤ J+1 := calc |R j-J| ≤ 2*k*(1/2^53*(J+qFa)):=hje _=(2*k*(1/2^53))*(J+qFa):=by ring _<=1*(J+qFa):=mul_le_mul_of_nonneg_right hk2 (by linarith) _<=J+1:=by linarith have hRj : |R j| ≤ 2^150:=by have e:R j=(R j-J)+J:=by ring have h1:=abs_add_le (R j-J) J rw[<-e,abs_of_nonneg hJ0] at h1 have:(2:ℝ)*(2^20*2^128)+1<=2^150:=by norm_num linarith have h150:(2:ℝ)^150<=qE5:=by have:(2:ℝ)^150<=2^256*2^42:=by norm_num linarith obtain ⟨hgf,hge⟩:=qDb f j hf hjf (by rw[hH,abs_of_nonneg hH0]; linarith [qCj]) (hRj.trans h150) rw[hH] at hge have hHj : |H*R j| ≤ 2^128*2^150:=by rw[abs_mul,abs_of_nonneg hH0] exact mul_le_mul hHl hRj (abs_nonneg _) (by norm_num) have hgb : |R (f*j)| ≤ qE5:=by have e:R (f*j)=(R (f*j)-H*R j)+H*R j:=by ring have h1:=abs_add_le (R (f*j)-H*R j) (H*R j) rw[<-e] at h1 have:(2:ℝ)^128*2^150*2+1<=2^256*2^42:=by norm_num have h2:(1/2^53:ℝ)*(|H*R j| + qFa) ≤ |H*R j| + 1:=by have:=abs_nonneg (H*R j) linarith linarith obtain ⟨haf,hav⟩:=qD7 b hbf rw[hbv] at hav obtain ⟨htf,hte⟩:=qCs (f*j) b.abs hgf haf hgb (by rw[hav,abs_abs]; linarith [qCj]) rw[hav] at hte have key2:=qB3 H J (R j) (R (f*j)) (R (f*j+b.abs)) |bv| qFa k hH0 hHl hJ0 (abs_nonneg _) hz.le hz1 hkR hje hge hte have hHJ:H*J<=2^128*(2^20*2^128):=mul_le_mul hHl hJ2 hJ0 (by norm_num) have hHJ0:0<=H*J:=mul_nonneg hH0 hJ0 have hbv0:=abs_nonneg bv have hkz:k*qFa<=2^20*qFa:=mul_le_mul_of_nonneg_right hkR hz.le have hYB:H*J + |bv| + qFa<=qE5/8:=by have:(2:ℝ)^128*(2^20*2^128)+2^128+1<=2^256*2^42/8:=by norm_num linarith have hsaY:qEm (ts.map g ++ [b])+qFa<=3/2*(H*J + |bv| + qFa):=by linarith have hlen1:(ts.map g ++ [b]).length<=2^21:=by simp; omega obtain ⟨hf1,he1⟩:=qAr _ hfq hlen1 (by linarith) have hl1:(((ts.map g ++ [b]).length:ℕ):ℝ)=k+1:=by simp[k] rw[hl1] at he1 have hS1:=qBp (ts.map g ++ [b]) set Y:=H*J + |bv| + qFa with hYdef have hY0:0<=Y:=by linarith have hUY:0<=(1/2^53:ℝ)*Y:=mul_nonneg (by norm_num) hY0 have hD1:2*(k+1)*(1/2^53*(qEm (ts.map g ++ [b])+qFa)) ≤ 3*(k+1)*(1/2^53*Y) := calc 2*(k+1)*(1/2^53*(qEm (ts.map g ++ [b])+qFa)) <=2*(k+1)*(1/2^53*(3/2*Y)):=by apply mul_le_mul_of_nonneg_left _ (by linarith) exact mul_le_mul_of_nonneg_left hsaY (by norm_num) _=_:=by ring have hkY:A+k*qFa<=(k+1)*Y:=by have h1:A+qFa<=Y:=by linarith have h2:=mul_le_mul_of_nonneg_left h1 (show 0<=k+1 by linarith) have h3:=mul_nonneg hk0 hA0 linarith have hD2:(1/2^53:ℝ)*(A+k*qFa)<=(k+1)*(1/2^53*Y) := calc (1/2^53:ℝ)*(A+k*qFa)<=1/2^53*((k+1)*Y) := mul_le_mul_of_nonneg_left hkY (by norm_num) _=_:=by ring have hn4:4*(k+1)*(1/2^53*Y)<=4*((n:ℝ)+1)*(1/2^53*Y) := mul_le_mul_of_nonneg_right (by linarith) hUY have hK3:3*(k+1)*(1/2^53*Y)<=Y/2:=by have h:k+1<=2^20:=by linarith have:=mul_le_mul_of_nonneg_right h hUY norm_num at this ⊢ linarith obtain ⟨he1a,he1b⟩:=abs_le.mp he1 obtain ⟨hA1a,hA1b⟩:=abs_le.mp hA1 refine qCk n hn _ _ hf1 htf _ Y hY0 hYB ?_ key2 ?_ hbig . apply abs_le.mpr constructor <;> linarith . have e:R (qCt (ts.map g ++ [b])) = (R (qCt (ts.map g ++ [b]))-qEs (ts.map g ++ [b]))+qEs (ts.map g ++ [b]):=by ring have h1:=abs_add_le (R (qCt (ts.map g ++ [b]))-qEs (ts.map g ++ [b])) (qEs (ts.map g ++ [b])) rw[<-e] at h1 linarith lemma qAd (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) (s e:Float) (hs:let r:=(ϒ n).foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*dv (xw t)) (acc.t+v.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(dv hh*r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by set g:ℕ->Float:=λt=>dv (ww t)*dv (xw t) with hgd set p:ℕ->ℝ:=λt=>wv (ww t)*wv (xw t) with hpd set v:ℕ->Float:=λt=>dv (ww t) with hvd have hfold:(ϒ n).foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*dv (xw t)) (acc.t+v.abs)) ⟨0,0⟩ = ⟨((ϒ n).map g).foldl (.+·) 0, ((ϒ n).map λt=>(v t).abs).foldl (.+·) 0⟩ := qDd g (λt=>(v t).abs) (ϒ n) 0 0 obtain ⟨hs1,hs2⟩:=hs rw[hfold] at hs1 hs2 have hhN:hh.toNat<2^31:=by have:=AJ.mp hp change hh.toNat<=2147483647 at this omega have hHeq:wv hh=qD6 hh.toNat:=by rw[qB5 hh hg,ν hhN] simp only[show ¬ 2^31<=hh.toNat by omega,↓reduceIte,one_mul] have hH0:0<=wv hh:=by rw[hHeq]; exact qAh _ have hHl:wv hh<=2^128:=le_trans (le_abs_self _) (qCi hh hg) have hle:∀ t,t< n->|wv (xw t)| ≤ wv hh:=by intro t ht have hg1:=hx t ht have h1:=AJ.mp (hm t ht) rw[qCn] at h1 rw[qBz _ hg1,hHeq] apply qAv _ _ h1 rcases qC9 _ hg1 with h | h . exact Ea h . exact EY h.1 have hgt:∀ t∈ϒ n,(g t).isFinite=true ∧ |R (g t)-p t| ≤ 1/2^53*(|p t| + qFa)∧|p t| ≤ wv hh * |wv (ww t)|:=by intro t ht have ht':=BM.mp ht obtain ⟨h1,h2,-⟩:=qDi _ _ (hw t ht') (hx t ht') refine ⟨h1,h2,?_⟩ simp only[hpd,abs_mul] rw[mul_comm] exact mul_le_mul_of_nonneg_right (hle t ht') (abs_nonneg _) have hvt:∀ t∈ϒ n,(v t).isFinite=true∧R (v t)=wv (ww t) ∧ |wv (ww t)| ≤ 2^128:=by intro t ht have ht':=BM.mp ht obtain ⟨h1,h2⟩:=qEo _ (hw t ht') exact ⟨h1,h2,qCi _ (hw t ht')⟩ obtain ⟨hbf,hbv⟩:=qEo bw hb obtain ⟨hhf,hhv⟩:=qEo hh hg have hS:s=qCt ((ϒ n).map g ++ [dv bw]):=by rw[hs1,qAm]; rfl have hE:e=nfOf n*(dv hh*qCt ((ϒ n).map λt=>(v t).abs)+(dv bw).abs):=by rw[hs2]; rfl rw[hS,hE] at hbig ⊢ have hX:exactS n xw ww bw=((ϒ n).map p).sum+wv bw:=by rw[qAf n xw ww bw hx (ϒ n) List.nodup_range (λt ht=>BM.mp ht) (λt ht h=>absurd (BM.mpr ht) h)] simp only[hpd,mul_comm] rw[hX] exact qAb n hn (ϒ n) (by simp) g v p (λt=>wv (ww t)) (dv bw) (dv hh) (wv bw) (wv hh) hgt hvt hbf hbv (qCi 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 qB6 (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, Fb] | 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 qBs (H a b:Nat) (hb:b< H):(H*a+b)/H=a∧(H*a+b)%H=b:=by have hH:0< H :=א constructor . rw[Cf hH,Υ hb,ϑ] . rw[Ce,ν hb] lemma qBf (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[qB6,Nat.pow_succ] have hH:0<2^K:=by positivity generalize 2^K=H at * have hdm:=Aq 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 Ea 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⟩:=qBs 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⟩:=qBs 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 qDv (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma qDh (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:=(Ϻ hne).2 h2; omega . exact (CH hne).2 h1 lemma qAg (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,qBf m K r h1 h2] lemma qCd (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:=qDh r 23 h1 h2 unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,qDv] 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 B_final' (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:=qDh _ 24 (by norm_num) (by norm_num) unfold shiftToTargetExponent shiftToExponent simp only[totalExponent,hl,qDv] have h3:max (((24:Nat):Int)+1+T-24) (-149)-T=1 :=א rw[h3] rfl lemma qB4 (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[qCd P E hP1 hP2 hE,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false] lemma qB9 (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[B_final' (E-1) א,ExtendedMantissa.ofMantissaAndAccuracy,hne,dite_false, sub_add_cancel] lemma qAj (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 . -- `m` is in the binade of `P . 2^E`: target `E`,mantissa `P`. have hL:m.log2=g+25:=by apply qDh 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,qDv]; omega rw[qAg s m e E (g+1) P hT א ?_ ?_] . exact qB4 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 . -- Just below a power of two. 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 qDh 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,qDv]; omega rw[qAg s m e (E-1) g (2^24) hT א א א] exact qB9 s E hE' hp . have hE2:E=-149 :=א have hT:Format.binary32.targetExponent (totalExponent m e)=E:=by simp only[totalExponent,hL,qDv]; omega rw[qAg s m e E (g+1) (2^23) hT א (by rw[e2]; omega) (by rw[e2]; omega)] exact qB4 s _ E (by norm_num) (by norm_num) hE hp lemma qAa (neg:Bool) (m:Nat) (e:Int) : Bt Fmt ⟨neg,m,e⟩ = BT Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e):=by simp[Bt,Model.ieeeRoundDyadic] rfl lemma qA0 (s:Sign):s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s <;> rfl lemma qAc (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:=qDh 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,qA0,BitVec.toNat_ofNat] have h8:(E+150).toNat%2^8=(E+150).toNat:=ν א rw[h8,<-Bq (by omega:(E+150).toNat<2^8), <-Bq (Ε _ (by positivity):M%2^23<2^23)] split_ifs <;> simp[Α] omega lemma qDa (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 (Bt 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):=zpow_pos (by norm_num) _ have hV:(m:ℝ)*2^e=((m*2^k:Nat):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-zpow_natCast,<-zpow_add₀ two_ne_zero] congr 2; ring have hEE:(2:ℝ)^E=((2^(g+2):Nat):ℝ)*2^(e-k):=by push_cast rw[<-zpow_natCast,<-zpow_add₀ two_ne_zero,<-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):ℝ) := lt_of_mul_lt_mul_right (by linarith) 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):ℝ) := lt_of_mul_lt_mul_right (by linarith) 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):ℝ) := lt_of_mul_lt_mul_right (by linarith) hb.le exact_mod_cast this rw[qAa,<-ExactRuntime.round_scale _ _ m e k hm, qAj _ (m*2^k) (e-k) P E g hp hP1 hP2 hE hg h1 h2 h3] simp only[toWord,Dn,BT,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qAc _ _ _ _ hP1 hP2 א א] cases neg <;> simp[Model.modelSign] end abbrev qFb:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qD2 (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 qC3 (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 qD1 (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 qEv (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:=qD2 f have hf:=qC3 f have hs:=qD1 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[φ,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:BI (EH qFb s E F)=true:=by rw[Model.isFinite_ofFields qFb _ _ _ (by show E<2^11; omega) hF] show (E != 2047)=true simp; omega rw[Model.toDyadic?_ofFields_of_isFinite qFb _ _ _ (by show E<2^11; omega) hF hfin] have hE0:E≠0 :=א simp only[hE0,ite_false,show qFb.exponentBias=1023 from rfl, show qFb.fracWidth=52 from rfl] simp only[show Ct 52=2^52 from rfl] congr 2 simp only[DK] push_cast ring lemma qAx (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 qE8 (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:=qEv f s E F hE1 hE hF h refine ⟨qAx f _ hd,?_⟩ rw[R,Model.toReal_eq,hd] cases s <;> simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def qCv (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 qE7 (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) : qCv A (decide (2^34<=x)) (x/2^23%2^11) (x%2^23):=by unfold qCv 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,φ,ite_false] exact ⟨A,by omega,by omega,by omega⟩ lemma qA8 (A:UInt64) : ((A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)).toNat=(A.toNat+268435455)%2^64/2^29:=by rw[Ξ,ξ] norm_num [ψ] lemma qA4 (x:UInt64) : ((x >>> (23:UInt64)) &&& (0x7FF:UInt64)).toNat=x.toNat/2^23%2^11:=by rw[BR,Ξ] have:=ϡ (x.toNat >>> 23) 11 norm_num [ψ] at this ⊢ exact this lemma qDg (v:UInt64) (h:v-(897:UInt64)<(254:UInt64)) : 897<=v.toNat∧v.toNat<1151:=by rw[Ψ,Τ] at h have hv:=v.toNat_lt norm_num at h hv omega lemma qDm (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[ό,Ξ] norm_num [ψ,Α] 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[BR] exact ϡ _ 34 have h3:((896:UInt64) <<< (23:UInt64))=7516192768:=by decide rw[h3,Η] . rw[h2]; rfl . rw[ϐ,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 (Nat.two_pow_add_eq_or_of_lt א _).symm apply ύ.mp rw[AA,Du,ha,hb,hc,AL] by_cases h:2^34<=x.toNat . simp only[h,decide_true,ite_true] omega . simp only[h,decide_false,φ,ite_false] omega lemma qAz (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) ∧ qCv (s+e).toBits.toNat S ex fr∧qCv (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:=qA8 A have hBn:=qA8 B generalize (A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=x at h hAn ⊢ generalize (B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=y at h hBn ⊢ have hex:=qA4 x generalize (x >>> (23:UInt64)) &&& (0x7FF:UInt64)=ex at h hex ⊢ split at h . rename_i hc have hc0:=hc obtain ⟨hxy,hc⟩:=Bool.and_eq_true_iff.mp hc have hxy:x=y:=by simpa using hxy obtain ⟨h1,h2⟩:=qDg 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,qDm x hx35 h1 h2,qE7 A.toNat x.toNat hA hAn h1 h2, qE7 B.toNat x.toNat hB hBn h1 h2⟩ . exact absurd rfl h set_option maxRecDepth 8192 in lemma qAy (f:Float) (S:Bool) (ex fr:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hn:qCv 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:=Ε _ (by norm_num) 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):=zpow_pos (by norm_num) _ have hE:E=ex∨(E+1=ex∧fr=0) :=א obtain ⟨hfin,hR⟩:=qE8 f S E F א א hF (by rw[hA,BU]) 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_add₀ two_ne_zero,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[BU] at hl; exact hl) have h4:(fr:ℝ)*2^29< F+2^28:=by exact_mod_cast h4' have h5:(0:ℝ)<=F:=Nat.cast_nonneg _ have h6:(0:ℝ)<=fr:=Nat.cast_nonneg _ have h7:=mul_le_mul_of_nonneg_right h3 hb.le have h8:=mul_lt_mul_of_pos_right h4 hb have h9:=mul_nonneg h5 hb.le have h10:=mul_nonneg 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:=mul_le_mul_of_nonneg_right h3 hb.le have h8:=mul_lt_mul_of_pos_right h4 hb push_cast refine ⟨?_,?_,?_⟩ . linarith . intro h0; exact absurd rfl h0 . linarith lemma ziv_big (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⟩:=qAz s e h obtain ⟨f1,v,hv,-,-,h5⟩:=qAy _ S ex fr h1 h2 h3 ha obtain ⟨f2,-⟩:=qAy _ S ex fr h1 h2 h3 hb refine ⟨f1,f2,?_⟩ have hb0:(2:ℝ)^(-179:ℤ)<=2^((ex:Int)-1076) := zpow_le_zpow_right₀ (by norm_num) א have hpos:(0:ℝ)<2^(-179:ℤ):=zpow_pos (by norm_num) _ have e1:(2:ℝ)^(-127:ℤ)=2^52*2^(-179:ℤ):=by rw[<-zpow_natCast,<-zpow_add₀ two_ne_zero]; norm_num have hv1:(2:ℝ)^(-127:ℤ)<=v:=by rw[e1] nlinarith have hv0:0< v:=lt_of_lt_of_le (zpow_pos (by norm_num) _) hv1 have : |R (s+e)| = v:=by rw[hv]; cases S <;> simp[abs_of_pos hv0] rwa [this] lemma qEk (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 qCr (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,AH,Ϣ, show (255:UInt32).toNat=2^8-1 from rfl,ϡ] at this simpa [ψ] using this have hv:(w &&& 0x7FFFFFFF).toNat=w.toNat%2^31:=by rw[AH,show (0x7FFFFFFF:UInt32).toNat=2^31-1 from rfl, ϡ] simp only[good,Ϸ,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[AJ,CX,hv] at h norm_num at h omega lemma qBq (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,zpow_add₀ two_ne_zero] rw[zpow_sub₀ two_ne_zero,zpow_sub₀ two_ne_zero,zpow_natCast,zpow_natCast,pow_add] norm_num [zpow_neg,zpow_natCast] ring lemma qCq (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[ϑ] rw[qBq] 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 Finset.sum_congr rfl intro j _ simp[Ϟ,Nat.add_left_comm] rw[ht] ring lemma qBn (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 (Bt Fmt d):=by let L:=a.map (DD (fmt:=Fmt)) let K:=b.map (DD (fmt:=Fmt)) have hLK:L.size=K.size:=by simp[L,K,hs] have hLf:∀ j (hj:j< L.size),BI L[j]=true:=by intro j hj have hj':j< a.size:=by simpa [L] using hj have he:=(hf j hj').1 rw[getElem!_pos a j hj'] at he simp[L,isFinite_ofUInt32,he] have hKf:∀ j (hj:j< K.size),BI K[j]=true:=by intro j hj have hj':j< b.size:=by simpa [K] using hj have he:=(hf j א).2 rw[getElem!_pos 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) (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) (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 qDy (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 (Bt Fmt d):=by have hsum:(dsum (AF (n:=n) λi=>fa i) (AF (n:=n) λi=>fb i) 0 n:ℚ) *2^(-298:Int)=∑ j∈Finset.range n,rationalWord (fa j)*rationalWord (fb j):=by rw[qCq] apply Finset.sum_congr rfl intro j hj have hj':=Finset.mem_range.mp hj rw[Β,getElem!_ofFn n fa j hj',getElem!_ofFn n fb j hj'] have hs:(AF (n:=n) λi=>fa i).size=n:=by simp obtain ⟨d,hd,hw⟩:=qBn (AF (n:=n) λi=>fa i) (AF (n:=n) λi=>fb i) (by simp) (by intro j hj have hj':j< n:=by simpa using hj rw[getElem!_ofFn n fa j hj',getElem!_ofFn n fb j hj'] exact hf j hj') (by rw[hs,hsum]; exact hn) rw[hs,hsum] at hd exact ⟨d,hd,hw⟩ lemma qDw: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 qD4 (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,qDw,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qEk bw (qCr bw hb)] congr 1 apply Finset.sum_congr rfl intro t ht have ht':=Finset.mem_range.mp ht simp only[ht',ite_true] rw[qEk _ (qCr _ (hx t ht')),qEk _ (qCr _ (hw t ht')),Rat.cast_mul] lemma ziv_dot (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'⟩:=qAz s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qAy _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qAy _ S ex fr h1 h2 h3 hb' have hsum:=qD4 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):=zpow_pos (by norm_num) _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-zpow_natCast,<-zpow_add₀ two_ne_zero] 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[φ,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 lt_of_le_of_lt ?_ hW3 exact mul_nonneg (by norm_num) 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,Rat.cast_zero] at hsum rw[<-hsum] at hVW cases S . simp only[φ,ite_false] at hVW; linarith . simp only[ite_true] at hVW; linarith obtain ⟨⟨neg,m,q⟩,hd,hw'⟩:=qDy (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 ⟨qCr _ (hx j hj),qCr _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨hone,qCr _ hb⟩) hne have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=V:=by rw[<-FloatLib.Numerics.Dyadic.cast_toRat,hd,hsum] have hq:(0:ℝ)<2^q:=zpow_pos (by norm_num) _ have hm0:(0:ℝ)<=m:=Nat.cast_nonneg _ have hmq:=mul_nonneg 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[φ,ite_false,ite_true,DK,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:=qDa 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 fast_tight (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)) (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⟩:=ziv_big s e h obtain ⟨hlo,hhi⟩:=encl_tight n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact ziv_dot n hn xw ww bw hx hw hb s e hlo hhi h lemma fast_coarse (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) (s e:Float) (hs:let r:=(ϒ n).foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*dv (xw t)) (acc.t+v.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(dv hh*r.t+(dv bw).abs)) (h:ziv s e≠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⟩:=ziv_big s e h obtain ⟨hlo,hhi⟩:=qAd n hn xw ww bw hh hx hw hb hg hm hp s e hs hbig exact ziv_dot 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 qEi (k:Nat) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq]; exact uk k h lemma qCw (a b:USize) (h:a.toNat*b.toNat<2^32) : (a*b).toNat=a.toNat*b.toNat:=by have h1:2^32<=Λ:=τ have h3:Λ=2^System.Platform.numBits:=rfl rw[Dv]; exact ν א lemma qCb {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size := qEi _ h lemma qDz (k:Nat) (h:k<2^32):(USize.ofNat k).toNat=k:=uk k h lemma qD0 (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:=lt_of_usz_w a i h rw[getElem!_pos a i.toNat h2]; rfl . rename_i h have h':¬ i.toNat< a.size:=by intro h2; apply h; rw[υ,qCb a hs]; exact h2 rw[getElem!_neg a i.toNat h']; rfl lemma qA3 (a:Array UInt32) (i:USize) (v:UInt32):(wrW a i v).size=a.size:=by unfold wrW; split <;> simp lemma qBe (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[υ,qCb a hs]; exact hi unfold wrW; rw[dite_eq_left h] by_cases hq:q< a.size . have h3:q<(a.uset i v (lt_of_usz_w a i h)).size:=by simpa using hq rw[getElem!_pos _ q h3,getElem!_pos 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 (lt_of_usz_w a i h)).size:=by simpa using hq rw[getElem!_neg _ q h3,getElem!_neg a q hq,ite_eq_right א] def qD5 (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 qBx {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (hN:N<2^32) (h:qD5 N sp S out) {u:USize} {p:Nat} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):qD5 N sp (λq=>q=p∨S q) (wrW out u v):=by obtain ⟨h1,h2⟩:=h refine ⟨by rw[qA3,h1],λq hq hS=>?_⟩ rw[qBe _ _ _ _ א א,hu] by_cases e:q=p . rw[ite_eq_left e,e,hv] . rw[ite_eq_right e]; exact h2 q hq (hS.resolve_left e) lemma qA1 {N:Nat} {sp:Nat->Word} {S S':Nat->Prop} {out:Array Word} (h:qD5 N sp S out) (hS:∀ q,q< N->S' q->S q):qD5 N sp S' out := ⟨h.1,λq hq h'=>h.2 q hq (hS q hq h')⟩ lemma qDk {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (h:qD5 N sp S out) (hS:∀ q,q< N->S q) : out=AF (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h apply Array.ext . simp[h1] . intro q h3 h4 have h5:q< N :=א have:=h2 q h5 (hS q h5) rw[getElem!_pos out q h3] at this simp[this] lemma qBi (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[AJ] have:v.toNat≠0:=λh=>h0 (ύ.mp h) omega rw[AJ,Dj _ _ h3] at h1 rw[Dl] at h2 have h4:(0x00800000:UInt32)<=v:=by rw[AJ]; omega simp only[Ϸ,decide_eq_true_eq] right rw[AJ,Dj _ _ h4] omega lemma qD9 (c u lo:UInt32) (h:c<=if u< lo then u else lo):c<=u∧c<=lo:=by simp only[AJ,Dl] at * split at h <;> omega lemma qDt (c v hi:UInt32) (h:(if hi< v then v else hi)< c):v< c∧hi< c:=by simp only[Dl] at * split at h <;> omega lemma qDe (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=true → (0x7FFFFF:UInt32)<=lo∧hi<0x7F800000 ∧ ∀ k,i.toNat<=k->k< n.toNat->good A[k]! = true:=by induction d with | zero => intro i lo hi he h rw[scan,dite_eq_right (by rw[υ]; omega)] at h simp only[Ω,decide_eq_true_eq] at h exact ⟨h.1,h.2,λk h1 h2=>by omega⟩ | succ d ih => intro i lo hi he h have hi':i< n:=by rw[υ]; omega rw[scan,dite_eq_left hi'] at h obtain ⟨h1,h2,h3⟩:=ih (i+1) _ _ (by rw[usz i n hi']; omega) h obtain ⟨h4,h5⟩:=qD9 _ _ _ h1 obtain ⟨h6,h7⟩:=qDt _ _ _ h2 refine ⟨h5,h7,λk hk1 hk2=>?_⟩ by_cases e:k=i.toNat . subst e have hb:i.toNat< A.size :=א rw[getElem!_pos A i.toNat hb] exact qBi _ h4 h6 . exact h3 k (by rw[usz i n hi']; omega) hk2 lemma qAw (A:Array UInt32) (hs:A.size<2^32) (h:scan A A.usize 0 (usize_le A) 0xFFFFFFFF 0=true) : ∀ k,k< A.size->good A[k]! = true:=by have e:=qCb A hs have h0:(0:USize).toNat=0:=rfl obtain ⟨-,-,h3⟩:=qDe A A.usize (usize_le A) A.size 0 _ _ (by rw[h0,e]; omega) h intro k hk exact h3 k א א lemma qAp (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 qBc (w:UInt32):w &&& 0x7FFFFFFF<=0x7FFFFFFF:=by rw[AJ,AH] exact Nat.and_le_right def qDs (w:Nat->Word) (t:Nat) (h:Word):Prop := good h=true∧(∀ k,k< t->w k &&& 0x7FFFFFFF<=h)∧h<=0x7FFFFFFF lemma qAt {w:Nat->Word} {t:Nat} {h:Word} (hg:good (w t)=true) (H:qDs w t h):qDs w (t+1) (mx h (w t)):=by obtain ⟨h1,h2,hp⟩:=H have hm:=qAp _ hg unfold mx dsimp only by_cases e:w t &&& 0x7FFFFFFF<=h . rw[ite_eq_left e] refine ⟨h1,λk hk=>?_,hp⟩ by_cases e2:k=t . rw[e2]; exact e . exact h2 k א . rw[ite_eq_right e] refine ⟨hm,λk hk=>?_,qBc _⟩ by_cases e2:k=t . rw[e2]; exact UInt32.le_refl _ . have h3:=h2 k א simp only[AJ] at * omega def qDl (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat):Word := act relu (dotOf (inW+1) (λt=>if t< inW then X[i*inW+t]! else oneW) (λt=>if t< inW then W[j*inW+t]! else b[j]!)) structure qE9 (rows inW outW:Nat) (X W b:Array Word):Prop where hX:rows*inW=X.size hW:outW*inW=W.size hb:outW=b.size pi:0< inW po:0< outW ni:inW<2^20 sX:X.size<2^32 sW:W.size<2^32 sb:b.size<2^32 sO:rows*outW<2^32 gX:∀ q,q< X.size->good X[q]! = true gW:∀ q,q< W.size->good W[q]! = true gb:∀ q,q< b.size->good b[q]! = true lemma qEn {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 C_ib' {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 section variable {rows inW outW:Nat} {X W b:Array Word} lemma qE9.gx (G:qE9 rows inW outW X W b) {i:Nat} (hi:i< rows) : ∀ t,t< inW->good X[i*inW+t]! = true := λ_ ht=>G.gX _ (by rw[<-G.hX]; exact qEn hi ht) lemma qE9.gw (G:qE9 rows inW outW X W b) {j:Nat} (hj:j< outW) : ∀ t,t< inW->good W[j*inW+t]! = true := λ_ ht=>G.gW _ (by rw[<-G.hW]; exact qEn hj ht) lemma qEc (G:qE9 rows inW outW X W b) (relu:Bool) {i j:Nat} (hi:i< rows) (hj:j< outW) (L:List Nat) (hl:L.Nodup) (hl2:∀ t∈L,t< inW) (hz:∀ t,t< inW->t∉L->X[i*inW+t]! &&& 0x7FFFFFFF=0) {s e:Float} (hs:let r:=L.foldl (λ(acc:F1) t=>let q:=dv X[i*inW+t]! * dv W[j*inW+t]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s∧e=r.t) : fin inW relu X W b i j s (nfOf inW) e b[j]! = qDl inW relu X W b i j:=by obtain ⟨h1,h2⟩:=hs unfold fin dsimp only by_cases h:ziv (s+dv b[j]!) (nfOf inW*(e+(dv b[j]!).abs))=0xFFFFFFFF . rw[ite_eq_left (by simpa using h)] unfold slowOut qDl rw[Zi] . rw[ite_eq_right (by simpa using h)] unfold qDl congr 1 exact fast_tight inW G.ni (λt=>X[i*inW+t]!) (λt=>W[j*inW+t]!) b[j]! (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb]; exact hj)) L hl hl2 hz _ _ ⟨by rw[h1],by rw[h2]⟩ h lemma qBd {X W:Array UInt32} {xo wo n:USize} (hX:X.size<2^32) (hW:W.size<2^32) (h1:xo.toNat+n.toNat<=X.size) (h2:wo.toNat+n.toNat<=W.size) (d:Nat):∀ (t:USize) (s a:Float),t.toNat+d=n.toNat → tightGo X W xo wo n t s a=(Bd t.toNat d).foldl (λ(acc:F1) k => let q:=dv X[xo.toNat+k]! * dv W[wo.toNat+k]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨s,a⟩:=by induction d with | zero => intro t s a he rw[tightGo,dite_eq_right (by rw[υ]; omega)] rfl | succ d ih => intro t s a he have ht:t< n:=by rw[υ]; omega rw[tightGo,dite_eq_left ht,Ϩ,AO] dsimp only rw[qD0 _ _ hX,qD0 _ _ hW,uadd _ _ א,uadd _ _ א] rw[ih (t+1) _ _ (by rw[usz t n ht]; omega),usz t n ht] lemma qC7 (G:qE9 rows inW outW X W b) (relu:Bool) {i j:Nat} (hi:i< rows) (hj:j< outW) {s a:Float} {hh:Word} (hg:good hh=true) (hm:∀ t,t< inW->X[i*inW+t]! &&& 0x7FFFFFFF<=hh) (hp:hh<=0x7FFFFFFF) (hs:let r:=(ϒ inW).foldl (λ(acc:F1) t=>let v:=dv W[j*inW+t]! F1.mk (acc.s+v*dv X[i*inW+t]!) (acc.t+v.abs)) ⟨0,0⟩;s=r.s∧a=r.t) : fin8 inW relu X W b i j s (nfOf inW) a hh b[j]! inW.toUSize=qDl inW relu X W b i j:=by obtain ⟨h1,h2⟩:=hs have hb:good b[j]! = true:=G.gb j (by rw[<-G.hb]; exact hj) have hin:inW<2^32:=lt_trans G.ni (by norm_num) have hX1:i*inW+inW<=X.size:=by rw[<-G.hX]; exact C_ib' hi (le_refl _) have hW1:j*inW+inW<=W.size:=by rw[<-G.hW]; exact C_ib' hj (le_refl _) have sX:=G.sX have sW:=G.sW have e1:(i*inW).toUSize.toNat=i*inW:=qEi _ א have e2:(j*inW).toUSize.toNat=j*inW:=qEi _ א have e3:inW.toUSize.toNat=inW:=qEi _ hin unfold fin8 dsimp only by_cases h:ziv (s+dv b[j]!) (nfOf inW*(dv hh*a+(dv b[j]!).abs))=0xFFFFFFFF . rw[ite_eq_left (by simpa using h)] have e:=qBd (xo:=(i*inW).toUSize) (wo:=(j*inW).toUSize) (n:=inW.toUSize) G.sX G.sW (by rw[e1,e3]; exact hX1) (by rw[e2,e3]; exact hW1) inW 0 0 0 (by rw[e3,show (0:USize).toNat=0 from rfl,Β]) rw[show (0:USize).toNat=0 from rfl,e1,e2,<-Ca] at e rw[qEc G relu hi hj (ϒ inW) List.nodup_range (λt ht=>BM.mp ht) (λt ht h3=>absurd (BM.mpr ht) h3) (s:=(tightGo X W (i*inW).toUSize (j*inW).toUSize inW.toUSize 0 0 0).s) (e:=(tightGo X W (i*inW).toUSize (j*inW).toUSize inW.toUSize 0 0 0).t) ⟨by rw[e],by rw[e]⟩] . rw[ite_eq_right (by simpa using h)] unfold qDl congr 1 exact fast_coarse inW G.ni (λt=>X[i*inW+t]!) (λt=>W[j*inW+t]!) b[j]! hh (G.gx hi) (G.gw hj) hb hg hm hp _ _ ⟨by rw[h1],by rw[h2]⟩ h end lemma qEj (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[getElem!_pos ds i.toNat h']; rfl lemma qEt (a:Array UInt32) (i:USize) (h:i.toNat< a.size) : a.uget i h=a[i.toNat]!:=by rw[getElem!_pos a i.toNat h]; rfl lemma qA2 (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 qBo (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 [qEi _ hs] unfold wrF; rw[dite_eq_left 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[getElem!_pos _ q h3,getElem!_pos 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[getElem!_neg _ q h3,getElem!_neg ds q hq,ite_eq_right א] def qCy (xw:Nat->Word) (t:Nat):List Nat := (ϒ t).filter (λp=>xw p &&& 0x7FFFFFFF != 0) lemma qA6 (xw:Nat->Word) (t:Nat):(qCy xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& 0x7FFFFFFF != 0) (ϒ t) simpa [qCy] using h lemma qAs (xw:Nat->Word) (t:Nat) (h:xw t &&& 0x7FFFFFFF=0) : qCy xw (t+1)=qCy xw t:=by simp[qCy,List.range_succ,List.filter_append,h] lemma qAu (xw:Nat->Word) (t:Nat) (h:¬ xw t &&& 0x7FFFFFFF=0) : qCy xw (t+1)=qCy xw t ++ [t]:=by simp[qCy,List.range_succ,List.filter_append,h] def qD8 (xw:Nat->Word) (sz t:Nat) (ix:Array UInt32) (buf:FloatArray) (c:USize):Prop := c.toNat=(qCy xw t).length∧ix.size=sz∧buf.size=sz ∧ ∀ k p:Nat,(qCy xw t)[k]?=some p->(ix[k]! : UInt32).toNat=p∧buf.data[k]! = dv (xw p) lemma qC0 {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) (d:Nat) : ∀ (t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize),t.toNat+d=n.toNat → qD8 xw n.toNat t.toNat ix buf c → qD8 xw n.toNat n.toNat (decR X o n t ix buf c).ix (decR X o n t ix buf c).buf (decR X o n t ix buf c).c:=by induction d with | zero => intro t ix buf c he H rw[decR,dite_eq_right (by rw[υ]; omega)] have e:t.toNat=n.toNat :=א rw[e] at H; exact H | succ d ih => intro t ix buf c he H have ht:t< n:=by rw[υ]; omega have hu:=usz t n ht rw[decR,dite_eq_left ht] dsimp only rw[hxw t ht] obtain ⟨h1,h2,h3,h4⟩:=H by_cases hz:xw t.toNat &&& 0x7FFFFFFF=0 . rw[ite_eq_left (by simpa using hz)] apply ih (t+1) _ _ _ א rw[hu,qD8,qAs xw _ hz] exact ⟨h1,h2,h3,h4⟩ . rw[ite_eq_right (by simpa using hz)] apply ih (t+1) _ _ _ א have hk:=qA6 xw t.toNat have hc:c.toNat< t.toNat+1 :=א rw[hu,qD8,qAu xw _ hz] have hcn:c.toNat< n.toNat :=א refine ⟨?_,by rw[qA3]; exact h2,by rw[qA2]; exact h3,λk p hk2=>?_⟩ . rw[uadd _ _ (by rw[Φ]; omega),Φ,h1]; simp . rw[qBe _ _ _ _ א א,qBo _ _ _ _ א א] rw[List.getElem?_append,<-h1] at hk2 by_cases e:k=c.toNat . subst e simp at hk2 subst hk2 simp omega . rw[ite_eq_right e,ite_eq_right e] by_cases e2:k< c.toNat . rw[ite_eq_left e2] at hk2; exact h4 k p hk2 . rw[ite_eq_right 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 qAi (a:FloatArray) (v:Float):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a exact Dt _ lemma qCc (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,dite_eq_right (by rw[υ]; omega)]; rfl | succ d ih => intro k a he have ht:k< n:=by rw[υ]; omega rw[pushZ,dite_eq_left ht,ih (k+1) _ (by rw[usz k n ht]; omega),qAi] omega lemma qDu {x:FloatArray} {ix W:Array UInt32} {w0 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (xf:Nat->Float) (ww:Nat->Word) (hww:∀ p,p< m.toNat->W[w0.toNat+p]! = ww p) (L:List Nat):∀ (t:USize) (a t0:Float),t.toNat+L.length=n.toNat → (∀ k p:Nat,L[k]?=some p->p< m.toNat∧(ix[t.toNat+k]! : UInt32).toNat=p ∧ x.data[t.toNat+k]! = xf p) → go1 x ix W w0 m n t hx hi hs h0 a t0=L.foldl (λ(acc:F1) p => let q:=xf p*dv (ww p) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨a,t0⟩:=by induction L with | nil => intro t a t0 he _ rw[go1,dite_eq_right (by rw[υ]; simp at he; omega)] rfl | cons p L ih => intro t a t0 he H simp only[Ex] at he have ht:t< n:=by rw[υ]; omega have htn:t.toNat< n.toNat :=א obtain ⟨h1,h2,h3⟩:=H 0 p rfl rw[ϑ] at h2 h3 have hit:t.toNat< ix.size :=א have e0:(ix.uget t hit).toUSize.toNat=p:=by rw[AI,qEt]; exact h2 have hm:(ix.uget t hit).toUSize< m:=by rw[υ,e0]; exact h1 have e1:x.uget t א=xf p:=by rw[qEj,h3] have e2:∀ pf,W.uget (w0+(ix.uget t hit).toUSize) pf=ww p:=by intro pf rw[qEt,uadd _ _ (by rw[e0]; omega),e0,hww p h1] rw[go1,dite_eq_left ht] dsimp only rw[dite_eq_left hm] rw[e1,e2,ih (t+1) _ _ (by rw[usz t n ht]; omega),AO] intro k q hq have:=H (k+1) q (by simpa using hq) rw[usz t n ht,show t.toNat+1+k=t.toNat+(k+1) by omega] exact this lemma qEh {x:FloatArray} {ix W:Array UInt32} {w0 w1 w2 w3 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) (k:Nat) : ∀ (t:USize) (a b c d t0 t1 t2 t3:Float),t.toNat+k=n.toNat → go4 x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d t0 t1 t2 t3 = ⟨(go1 x ix W w0 m n t hx hi hs h0 a t0).s,(go1 x ix W w1 m n t hx hi hs h1 b t1).s, (go1 x ix W w2 m n t hx hi hs h2 c t2).s,(go1 x ix W w3 m n t hx hi hs h3 d t3).s, (go1 x ix W w0 m n t hx hi hs h0 a t0).t,(go1 x ix W w1 m n t hx hi hs h1 b t1).t, (go1 x ix W w2 m n t hx hi hs h2 c t2).t,(go1 x ix W w3 m n t hx hi hs h3 d t3).t⟩:=by induction k with | zero => intro t a b c d t0 t1 t2 t3 he have hn:¬ t< n:=by rw[υ]; omega rw[go4,go1.eq_1 x ix W w0,go1.eq_1 x ix W w1,go1.eq_1 x ix W w2,go1.eq_1 x ix W w3] simp only[dite_eq_right hn] | succ k ih => intro t a b c d t0 t1 t2 t3 he have ht:t< n:=by rw[υ]; omega have htn:t.toNat< n.toNat :=א rw[go4,go1.eq_1 x ix W w0,go1.eq_1 x ix W w1,go1.eq_1 x ix W w2,go1.eq_1 x ix W w3] simp only[dite_eq_left ht] by_cases hm:(ix.uget t א).toUSize< m . simp only[dite_eq_left hm] exact ih (t+1) _ _ _ _ _ _ _ _ (by rw[usz t n ht]; omega) . simp only[dite_eq_right hm] exact ih (t+1) _ _ _ _ _ _ _ _ (by rw[usz t n ht]; omega) def qEy (inW outW:Nat) (relu:Bool) (X W b:Array Word) (q:Nat):Word := qDl inW relu X W b (q/outW) (q%outW) lemma qCf (inW outW:Nat) (relu:Bool) (X W b:Array Word) {i j:Nat} (hj:j< outW) : qEy inW outW relu X W b (i*outW+j)=qDl inW relu X W b i j:=by unfold qEy have h0:0< outW :=א rw[Θ i outW,Cf h0,Ce,Υ hj, ν hj,ϑ] lemma qBh {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:qD5 N sp (λq=>q< p) out) {u:USize} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):qD5 N sp (λq=>q< p+1) (wrW out u v) := qA1 (qBx hN h hu hp hv) (λq _ (hq:q< p+1) => (by omega:q=p∨q< p)) structure qEf (inW:Nat) (X:Array Word) (i:Nat) (ix:Array UInt32) (x:FloatArray) (c:USize) (L:List Nat):Prop where len:c.toNat=L.length cle:L.length<=inW six:ix.size=inW sx:x.size=inW nd:L.Nodup lt:∀ t∈L,t< inW hz:∀ t,t< inW->t∉L->X[i*inW+t]! &&& 0x7FFFFFFF=0 rd:∀ k p:Nat,L[k]?=some p → (ix[k]! : UInt32).toNat=p∧x.data[k]! = dv X[i*inW+p]! section variable {rows inW outW:Nat} {X W b:Array Word} lemma qE9.nm (G:qE9 rows inW outW X W b):inW.toUSize.toNat=inW := qEi _ (lt_trans G.ni (by norm_num)) lemma qE9.wj (G:qE9 rows inW outW X W b) {j:Nat} {u:USize} (hu:u.toNat=j) (hj:j< outW):(u*inW.toUSize).toNat=j*inW ∧ (u*inW.toUSize).toNat+inW.toUSize.toNat<=W.size:=by have h1:j*inW+inW<=outW*inW:=C_ib' hj (le_refl _) have h2:=G.hW have h3:=G.sW have e:(u*inW.toUSize).toNat=j*inW:=by rw[qCw _ _ (by rw[hu,G.nm]; omega),hu,G.nm] exact ⟨e,by rw[e,G.nm]; omega⟩ lemma qDq (G:qE9 rows inW outW X W b) (relu:Bool) {i j:Nat} (hi:i< rows) (hj:j< outW) {ix:Array UInt32} {x:FloatArray} {c:USize} {L:List Nat} (Rw:qEf inW X i ix x c L) {u:USize} (hu:u.toNat=j) (hx:c.toNat<=x.size) (hi':c.toNat<=ix.size) (hs:W.size<2^32) (h0:(u*inW.toUSize).toNat+inW.toUSize.toNat<=W.size) : fin inW relu X W b i j (go1 x ix W (u*inW.toUSize) inW.toUSize c 0 hx hi' hs h0 0 0).s (nfOf inW) (go1 x ix W (u*inW.toUSize) inW.toUSize c 0 hx hi' hs h0 0 0).t (rdW b u) = qDl inW relu X W b i j:=by have e:=qDu hx hi' hs h0 (λp=>dv X[i*inW+p]!) (λp=>W[j*inW+p]!) (λp _=>by rw[(G.wj hu hj).1]) L 0 0 0 (by rw[show (0:USize).toNat=0 from rfl,Β]; exact Rw.len.symm) (λk p hk=>by have h1:=Rw.rd k p hk rw[show (0:USize).toNat=0 from rfl,Β,G.nm] exact ⟨Rw.lt p (List.mem_of_getElem? hk),h1⟩) rw[qD0 _ _ G.sb,hu] exact qEc G relu hi hj L Rw.nd Rw.lt Rw.hz ⟨by rw[e],by rw[e]⟩ lemma qCe (G:qE9 rows inW outW X W b) (relu:Bool) {i:Nat} (hi:i< rows) {ix:Array UInt32} {x:FloatArray} {c:USize} {L:List Nat} (Rw:qEf inW X i ix x c L) {base:USize} (hb:base.toNat=i*outW) (d:Nat) : ∀ (j:Nat) (out:Array Word),j+d=outW → qD5 (rows*outW) (qEy inW outW relu X W b) (λq=>q< i*outW+j) out → qD5 (rows*outW) (qEy inW outW relu X W b) (λq=>q< i*outW+outW) (cols1 inW outW relu X W b i ix x (nfOf inW) inW.toUSize c j base out):=by induction d using Nat.strong_induction_on with | _ d ih => intro j out he H have hN:=G.sO have sW:=G.sW have hbs:=G.sb have hob:=G.hb have hle:=Rw.cle have h1c:c.toNat<=x.size:=by rw[Rw.sx,Rw.len]; exact hle have h2c:c.toNat<=ix.size:=by rw[Rw.six,Rw.len]; exact hle have l2:(2:USize).toNat=2:=uk 2 (by norm_num) have l3:(3:USize).toNat=3:=uk 3 (by norm_num) have hsp:=λ{i' j':Nat} (h:j'< outW) => (qCf inW outW relu X W b (i:=i') (j:=j') h).symm by_cases h4:j+4<=outW . have hP:=qEn hi (show j+3< outW by omega) have e0:j.toUSize.toNat=j:=qEi _ א have e1:(j.toUSize+1).toNat=j+1:=by rw[uadd _ _ (by rw[e0,Φ]; omega),e0,Φ] have e2:(j.toUSize+2).toNat=j+2:=by rw[uadd _ _ (by rw[e0,l2]; omega),e0,l2] have e3:(j.toUSize+3).toNat=j+3:=by rw[uadd _ _ (by rw[e0,l3]; omega),e0,l3] have g0:=(G.wj e0 א).2 have g1:=(G.wj e1 א).2 have g2:=(G.wj e2 א).2 have g3:=(G.wj e3 א).2 have a0:(base+j.toUSize).toNat=i*outW+j:=by rw[uadd _ _ (by rw[hb,e0]; omega),hb,e0] have a1:(base+j.toUSize+1).toNat=i*outW+j+1:=by rw[uadd _ _ (by rw[a0,Φ]; omega),a0,Φ] have a2:(base+j.toUSize+2).toNat=i*outW+j+1+1:=by rw[uadd _ _ (by rw[a0,l2]; omega),a0,l2] have a3:(base+j.toUSize+3).toNat=i*outW+j+1+1+1:=by rw[uadd _ _ (by rw[a0,l3]; omega),a0,l3] have o0:=qDq G relu hi (show j< outW by omega) Rw e0 h1c h2c sW g0 have o1:=qDq G relu hi (show j+1< outW by omega) Rw e1 h1c h2c sW g1 have o2:=qDq G relu hi (show j+2< outW by omega) Rw e2 h1c h2c sW g2 have o3:=qDq G relu hi (show j+3< outW by omega) Rw e3 h1c h2c sW g3 rw[cols1,dite_eq_left h4] dsimp only rw[dite_eq_left ⟨h1c,h2c,sW,g0,g1,g2,g3⟩] rw[qEh _ _ _ _ _ _ _ c.toNat 0 _ _ _ _ _ _ _ _ (by rw[show (0:USize).toNat=0 from rfl,Β])] dsimp only rw[o0,o1,o2,o3] refine ih (d-4) א (j+4) _ א ?_ have s1:qDl inW relu X W b i (j+1)=qEy inW outW relu X W b (i*outW+j+1):=by rw[BU]; exact hsp א have s2:qDl inW relu X W b i (j+2) = qEy inW outW relu X W b (i*outW+j+1+1):=by rw[show i*outW+j+1+1=i*outW+(j+2) by omega]; exact hsp א have s3:qDl inW relu X W b i (j+3) = qEy inW outW relu X W b (i*outW+j+1+1+1):=by rw[show i*outW+j+1+1+1=i*outW+(j+3) by omega]; exact hsp א rw[show i*outW+(j+4)=i*outW+j+1+1+1+1 by omega] exact qBh hN (qBh hN (qBh hN (qBh hN H a0 א (hsp א)) a1 א s1) a2 א s2) a3 א s3 . by_cases h1:j< outW . have hP:=qEn hi h1 have e0:j.toUSize.toNat=j:=qEi _ א have g0:=(G.wj e0 h1).2 have a0:(base+j.toUSize).toNat=i*outW+j:=by rw[uadd _ _ (by rw[hb,e0]; omega),hb,e0] have o0:=qDq G relu hi h1 Rw e0 h1c h2c sW g0 rw[cols1,dite_eq_right h4,dite_eq_left h1] dsimp only rw[dite_eq_left ⟨h1c,h2c,sW,g0⟩] rw[o0] exact ih (d-1) א (j+1) _ א (qBh hN H a0 hP (hsp h1)) . rw[cols1,dite_eq_right h4,dite_eq_right h1] have e:j=outW :=א rw[e] at H; exact H lemma qCm (G:qE9 rows inW outW X W b) (relu:Bool) (d:Nat) : ∀ (i:Nat) (ix:Array UInt32) (buf:FloatArray) (out:Array Word),i+d=rows → ix.size=inW->buf.size=inW → qD5 (rows*outW) (qEy inW outW relu X W b) (λq=>q< i*outW) out → qD5 (rows*outW) (qEy inW outW relu X W b) (λq=>q< rows*outW) (rows1 rows inW outW relu X W b (nfOf inW) inW.toUSize i ix buf out):=by induction d with | zero => intro i ix buf out he _ _ H rw[rows1,dite_eq_right א] have e:i=rows :=א rw[e] at H; exact H | succ d ih => intro i ix buf out he h1 h2 H have hi:i< rows :=א have hin:inW<2^32:=lt_trans G.ni (by norm_num) have hN:=G.sO have sX:=G.sX have hX:=G.hX have hP:i*inW+inW<=rows*inW:=C_ib' hi (le_refl _) have ho:(i*inW).toUSize.toNat=i*inW:=qEi _ א have hbs:(i*outW).toUSize.toNat=i*outW:=by have h3:i*outW+outW<=rows*outW:=C_ib' hi (le_refl _) exact qEi _ א have D:=qC0 (X:=X) (o:=(i*inW).toUSize) (n:=inW.toUSize) (λp=>X[i*inW+p]!) (by rw[G.nm]; exact hin) (λt ht=>by have h4:=υ.mp ht rw[G.nm] at h4 rw[qD0 _ _ sX,uadd _ _ (by rw[ho]; omega),ho]) inW 0 ix buf 0 (by rw[show (0:USize).toNat=0 from rfl,G.nm,Β]) ⟨by simp[qCy],by rw[G.nm]; exact h1,by rw[G.nm]; exact h2, λk p hk=>by simp[qCy] at hk⟩ rw[G.nm] at D obtain ⟨d1,d2,d3,d4⟩:=D have Rw:qEf inW X i (decR X (i*inW).toUSize inW.toUSize 0 ix buf 0).ix (decR X (i*inW).toUSize inW.toUSize 0 ix buf 0).buf (decR X (i*inW).toUSize inW.toUSize 0 ix buf 0).c (qCy (λp=>X[i*inW+p]!) inW) := ⟨d1,qA6 _ _,d2,d3,(List.nodup_range).filter _, λt ht=>BM.mp (List.mem_filter.mp ht).1, λt ht hn=>by by_contra hc exact hn (List.mem_filter.mpr ⟨BM.mpr ht,by simpa using hc⟩),d4⟩ rw[rows1,dite_eq_left hi] dsimp only refine ih (i+1) _ _ _ א d2 d3 ?_ have h5:=qCe G relu hi Rw hbs outW 0 out א (by rw[ϑ]; exact H) rw[show (i+1)*outW=i*outW+outW by rw[ϟ,Ϙ]] exact h5 end lemma qEd (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[getElem!_pos _ q h3,getElem!_pos ds q hq,ite_eq_right א,Dy] . by_cases e:q=ds.size . subst e have h3:ds.size<(ds.push v).size:=by simp rw[getElem!_pos (ds.push v) ds.size h3,ite_eq_left rfl,Dz] . have h3:¬ q<(ds.push v).size:=by simp; omega rw[getElem!_neg _ q h3,getElem!_neg ds q hq,ite_eq_right e] lemma qBt (xs:FloatArray) (a:Nat->Float) (r:Nat) (hr:r<8) : (((((((((xs.push (a 0)).push (a 1)).push (a 2)).push (a 3)).push (a 4)).push (a 5)).push (a 6)).push (a 7))).data[xs.size+r]! = a r:=by simp only[qEd,qAi] interval_cases r <;> repeat (first | rw[ite_eq_left א] | rw[ite_eq_right א]) lemma qBu (xs:FloatArray) (a0 a1 a2 a3 a4 a5 a6 a7:Float) (q:Nat) (hq:q< xs.size) : (((((((((xs.push a0).push a1).push a2).push a3).push a4).push a5).push a6).push a7)).data[q]! = xs.data[q]!:=by simp only[qEd,qAi] rw[ite_eq_right א,ite_eq_right א,ite_eq_right א,ite_eq_right א, ite_eq_right א,ite_eq_right א,ite_eq_right א,ite_eq_right א] def qEx (d:H8):Nat->UInt32 | 0=>d.h0 | 1=>d.h1 | 2=>d.h2 | 3=>d.h3 | 4=>d.h4 | 5=>d.h5 | 6=>d.h6 | _=>d.h7 def qEz (d:F8):Nat->Float | 0=>d.s0 | 1=>d.s1 | 2=>d.s2 | 3=>d.s3 | 4=>d.s4 | 5=>d.s5 | 6=>d.s6 | _=>d.s7 def qE0 (X:Array Word) (m t:Nat) (d:H8):Prop := d.xs.size=8*t∧(∀ k r:Nat,k< t->r<8->d.xs.data[8*k+r]! = dv X[r*m+k]!) ∧ ∀ r:Nat,r<8->qDs (λk=>X[r*m+k]!) t (qEx d r) lemma qE4 {n t c:USize} {k:Nat} (hc:c.toNat=k) (h:k*n.toNat+t.toNat<2^32) : (c*n+t).toNat=k*n.toNat+t.toNat:=by have h0:k*n.toNat<2^32 :=א rw[uadd _ _ (by rw[qCw _ _ (by rw[hc]; exact h0),hc]; exact h), qCw _ _ (by rw[hc]; exact h0),hc] lemma qEl {X:Array Word} {n t c:USize} {k:Nat} (hs:X.size<2^32) (hc:c.toNat=k) (h:k*n.toNat+t.toNat<2^32) : rdW X (c*n+t)=X[k*n.toNat+t.toNat]!:=by rw[qD0 _ _ hs,qE4 hc h] lemma qC6 {X:Array Word} {n:USize} (hs:X.size<2^32) (h8:8*n.toNat<=X.size) (hg:∀ q,q< X.size->good X[q]! = true) (d:Nat) : ∀ (t:USize) (xs:FloatArray) (h0 h1 h2 h3 h4 h5 h6 h7:UInt32),t.toNat+d=n.toNat → qE0 X n.toNat t.toNat ⟨xs,h0,h1,h2,h3,h4,h5,h6,h7⟩ → qE0 X n.toNat n.toNat (dec8 X n t xs h0 h1 h2 h3 h4 h5 h6 h7):=by induction d with | zero => intro t xs h0 h1 h2 h3 h4 h5 h6 h7 he H rw[dec8,dite_eq_right (by rw[υ]; omega)] have e:t.toNat=n.toNat :=א rw[e] at H; exact H | succ d ih => intro t xs h0 h1 h2 h3 h4 h5 h6 h7 he H have ht:t< n:=by rw[υ]; omega have hu:=usz t n ht have l2:(2:USize).toNat=2:=uk 2 (by norm_num) have l3:(3:USize).toNat=3:=uk 3 (by norm_num) have l4:(4:USize).toNat=4:=uk 4 (by norm_num) have l5:(5:USize).toNat=5:=uk 5 (by norm_num) have l6:(6:USize).toNat=6:=uk 6 (by norm_num) have l7:(7:USize).toNat=7:=uk 7 (by norm_num) have r0:rdW X t=X[0*n.toNat+t.toNat]!:=by rw[qD0 _ _ hs,ϸ,Β] have r1:rdW X (n+t)=X[1*n.toNat+t.toNat]!:=by rw[qD0 _ _ hs,uadd _ _ א,Ϙ] rw[dec8,dite_eq_left ht] dsimp only rw[r0,r1,qEl hs l2 א,qEl hs l3 א,qEl hs l4 א, qEl hs l5 א,qEl hs l6 א,qEl hs l7 א] obtain ⟨H1,H2,H3⟩:=H have hgood:∀ r,r<8->good X[r*n.toNat+t.toNat]! = true:=by intro r hr apply hg interval_cases r <;> omega dsimp only at H1 H2 refine ih (t+1) _ _ _ _ _ _ _ _ _ א ⟨?_,?_,?_⟩ . simp only[qAi,H1,hu]; omega . intro k r hk hr rw[hu] at hk by_cases e:k=t.toNat . subst e have h9:=qBt xs (λr=>dv X[r*n.toNat+t.toNat]!) r hr rw[H1] at h9 exact h9 . have h10:8*k+r< xs.size :=א rw[qBu _ _ _ _ _ _ _ _ _ _ h10] exact H2 k r א hr . intro r hr have h9:=H3 r hr have h10:=hgood r hr rw[hu] interval_cases r <;> exact qAt h10 h9 def qEp (xf:Nat->Float) (ww:Nat->Word) (l:List Nat) (acc:F1):F1 := l.foldl (λ(acc:F1) t=>let v:=dv (ww t) F1.mk (acc.s+v*xf t) (acc.t+v.abs)) acc lemma qEu {xs:FloatArray} {i t n:USize} {xf:Nat->Nat->Float} (hxf:∀ r k:Nat,r<8->k< n.toNat->xs.data[8*k+r]! = xf r k) (hi:i.toNat=8*t.toNat) (ht:t.toNat< n.toNat) (hsx:xs.size<2^32) (hx:8*n.toNat<=xs.size) (r:Nat) (hr:r<8) (pf:(i+USize.ofNat r).toNat< xs.size) : xs.uget (i+USize.ofNat r) pf=xf r t.toNat:=by rw[qEj,uadd _ _ (by rw[uk r א]; omega),uk r א,hi,hxf r _ hr ht] lemma qDo {xs:FloatArray} {W:Array UInt32} {wo n:USize} (hx:8*n.toNat<=xs.size) (hsx:xs.size<2^32) (hs:W.size<2^32) (hw:wo.toNat+n.toNat<=W.size) (xf:Nat->Nat->Float) (ww:Nat->Word) (hxf:∀ r k:Nat,r<8->k< n.toNat->xs.data[8*k+r]! = xf r k) (hww:∀ k,k< n.toNat->W[wo.toNat+k]! = ww k) (d:Nat) : ∀ (t i:USize) (hi:i.toNat=8*t.toNat) (s0 s1 s2 s3 s4 s5 s6 s7 a:Float), t.toNat+d=n.toNat->∀ r,r<8 → F1.mk (qEz (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a) r) (go8 xs W wo n t i hx hsx hs hw hi s0 s1 s2 s3 s4 s5 s6 s7 a).t = qEp (xf r) ww (Bd t.toNat d) ⟨qEz ⟨s0,s1,s2,s3,s4,s5,s6,s7,a⟩ r,a⟩:=by induction d with | zero => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a he r hr rw[go8,dite_eq_right (by rw[υ]; omega)] rfl | succ d ih => intro t i hi s0 s1 s2 s3 s4 s5 s6 s7 a he r hr have ht:t< n:=by rw[υ]; omega have htn:t.toNat< n.toNat :=א have hu:=usz t n ht have e:dv (W.uget (wo+t) (wi wo t n W hw hs htn))=dv (ww t.toNat):=by rw[qEt,uadd _ _ א,hww _ htn] rw[go8,dite_eq_left ht] dsimp only rw[e,qEu hxf hi htn hsx hx 0 א,qEu hxf hi htn hsx hx 1 א, qEu hxf hi htn hsx hx 2 א,qEu hxf hi htn hsx hx 3 א, qEu hxf hi htn hsx hx 4 א,qEu hxf hi htn hsx hx 5 א, qEu hxf hi htn hsx hx 6 א,qEu hxf hi htn hsx hx 7 א] rw[ih (t+1) _ _ _ _ _ _ _ _ _ _ _ א r hr,hu,Ϩ] unfold qEp rw[AO] interval_cases r <;> rfl lemma qCo {outW q j:Nat} (hj:j< outW) (hq:q<8*outW) (h:q%outW< j+1) : q=7*outW+j∨q=6*outW+j∨q=5*outW+j∨q=4*outW+j ∨ q=3*outW+j∨q=2*outW+j∨q=1*outW+j∨q=0*outW+j ∨ q%outW< j:=by by_cases e:q%outW< j . simp only[e,or_true] . have e2:q%outW=j :=א have e3:=Aq q outW have e4:q/outW<8:=ϕ (by rwa [Θ]) rw[e2,Θ] at e3 generalize q/outW=a at e3 e4 interval_cases a <;> omega section variable {inW outW:Nat} {X W b:Array Word} lemma qCh (G:qE9 8 inW outW X W b) (relu:Bool) {d:H8} (D:qE0 X inW inW d) (k:Nat):∀ (j:Nat) (out:Array Word),j+k=outW → qD5 (8*outW) (qEy inW outW relu X W b) (λq=>q%outW< j) out → qD5 (8*outW) (qEy inW outW relu X W b) (λq=>q%outW< outW) (cols8 inW outW relu X W b d (nfOf inW) inW.toUSize j out):=by induction k with | zero => intro j out he H rw[cols8,dite_eq_right א] have e:j=outW :=א rw[e] at H; exact H | succ k ih => intro j out he H have hj:j< outW :=א have hN:=G.sO have hbs:=G.sb have hob:=G.hb have hnm:=G.nm have hin:=G.ni have sxs:8*inW<=8*2^20 :=א have e0:j.toUSize.toNat=j:=qEi _ א have eo:outW.toUSize.toNat=outW:=qEi _ א have g0:=G.wj e0 hj have hx:8*inW.toUSize.toNat<=d.xs.size:=by rw[hnm,D.1] have hsx:d.xs.size<2^32:=by rw[D.1]; omega have sW:=G.sW have hg:=g0.2 have h8:8*outW<2^32:=hN have l2:(2:USize).toNat=2:=uk 2 (by norm_num) have l3:(3:USize).toNat=3:=uk 3 (by norm_num) have l4:(4:USize).toNat=4:=uk 4 (by norm_num) have l5:(5:USize).toNat=5:=uk 5 (by norm_num) have l6:(6:USize).toNat=6:=uk 6 (by norm_num) have l7:(7:USize).toNat=7:=uk 7 (by norm_num) have hi0:(0:USize).toNat=8*(0:USize).toNat:=by rw[show (0:USize).toNat=0 from rfl] have key:=qDo hx hsx sW hg (λr k=>dv X[r*inW+k]!) (λk=>W[j*inW+k]!) (λr k hr hk=>by rw[hnm] at hk; exact D.2.1 k r hk hr) (λk _=>by rw[g0.1]) inW 0 0 hi0 0 0 0 0 0 0 0 0 0 (by rw[hnm,show (0:USize).toNat=0 from rfl,Β]) rw[show (0:USize).toNat=0 from rfl,<-Ca] at key rw[cols8,dite_eq_left hj] dsimp only rw[dite_eq_left ⟨hx,hsx,sW,hg⟩] generalize go8 d.xs W (j.toUSize*inW.toUSize) inW.toUSize 0 0 hx hsx sW hg hi0 0 0 0 0 0 0 0 0 0=R at key ⊢ have O:∀ r,r<8->fin8 inW relu X W b r j (qEz R r) (nfOf inW) R.t (qEx d r) b[j]! inW.toUSize=qEy inW outW relu X W b (r*outW+j):=by intro r hr rw[qCf _ _ _ _ _ _ hj] have k1:=key r hr have e1:qEz ⟨0,0,0,0,0,0,0,0,0⟩ r=0:=by interval_cases r <;> rfl rw[e1] at k1 have H1:=D.2.2 r hr exact qC7 G relu hr hj H1.1 H1.2.1 H1.2.2 ⟨(congrArg F1.s k1 :),(congrArg F1.t k1 :)⟩ have o0:fin8 inW relu X W b 0 j R.s0 (nfOf inW) R.t d.h0 b[j]! inW.toUSize=_ := O 0 א have o1:fin8 inW relu X W b 1 j R.s1 (nfOf inW) R.t d.h1 b[j]! inW.toUSize=_ := O 1 א have o2:fin8 inW relu X W b 2 j R.s2 (nfOf inW) R.t d.h2 b[j]! inW.toUSize=_ := O 2 א have o3:fin8 inW relu X W b 3 j R.s3 (nfOf inW) R.t d.h3 b[j]! inW.toUSize=_ := O 3 א have o4:fin8 inW relu X W b 4 j R.s4 (nfOf inW) R.t d.h4 b[j]! inW.toUSize=_ := O 4 א have o5:fin8 inW relu X W b 5 j R.s5 (nfOf inW) R.t d.h5 b[j]! inW.toUSize=_ := O 5 א have o6:fin8 inW relu X W b 6 j R.s6 (nfOf inW) R.t d.h6 b[j]! inW.toUSize=_ := O 6 א have o7:fin8 inW relu X W b 7 j R.s7 (nfOf inW) R.t d.h7 b[j]! inW.toUSize=_ := O 7 א rw[qD0 _ _ hbs,e0,o0,o1,o2,o3,o4,o5,o6,o7] have u0:j.toUSize.toNat=0*outW+j:=by rw[e0]; omega have u1:(outW.toUSize+j.toUSize).toNat=1*outW+j:=by rw[uadd _ _ (by rw[eo,e0]; omega),eo,e0]; omega have a2:=qE4 (n:=outW.toUSize) (t:=j.toUSize) l2 (by rw[eo,e0]; omega) have a3:=qE4 (n:=outW.toUSize) (t:=j.toUSize) l3 (by rw[eo,e0]; omega) have a4:=qE4 (n:=outW.toUSize) (t:=j.toUSize) l4 (by rw[eo,e0]; omega) have a5:=qE4 (n:=outW.toUSize) (t:=j.toUSize) l5 (by rw[eo,e0]; omega) have a6:=qE4 (n:=outW.toUSize) (t:=j.toUSize) l6 (by rw[eo,e0]; omega) have a7:=qE4 (n:=outW.toUSize) (t:=j.toUSize) l7 (by rw[eo,e0]; omega) rw[eo,e0] at a2 a3 a4 a5 a6 a7 have I0:=qBx h8 H u0 א rfl have I1:=qBx h8 I0 u1 א rfl have I2:=qBx h8 I1 a2 א rfl have I3:=qBx h8 I2 a3 א rfl have I4:=qBx h8 I3 a4 א rfl have I5:=qBx h8 I4 a5 א rfl have I6:=qBx h8 I5 a6 א rfl have I7:=qBx h8 I6 a7 א rfl exact ih (j+1) _ א (qA1 I7 (λq hq hq'=>qCo hj hq hq')) end lemma C_linF_eq:linF=linearSpec:=by funext rows inW outW relu X W b unfold linF split . rename_i h obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13⟩:=h have G:qE9 rows inW outW X W b:=⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10, qAw X h7 h11,qAw W h8 h12,qAw b h9 h13⟩ have e:linearSpec rows inW outW relu X W b=AF (n:=rows*outW) λq => qEy inW outW relu X W b q.val:=rfl have hnf:(inW.toUInt64+8).toFloat*Float.ofBits 0x3CC0000000000000=nfOf inW:=rfl rw[e] dsimp only rw[hnf] split . rename_i hr have hr8:rows=8:=by simpa using hr subst hr8 have H0:qD5 (8*outW) (qEy inW outW relu X W b) (λq=>q%outW<0) (Χ (8*outW) 0) := ⟨by simp,λq _ h=>absurd h (Nat.not_lt_zero _)⟩ have D0:qE0 X inW.toUSize.toNat (0:USize).toNat ⟨FloatArray.emptyWithCapacity (8*inW),0,0,0,0,0,0,0,0⟩:=by have e0:(0:USize).toNat=0:=rfl rw[e0] refine ⟨rfl,λk r hk=>absurd hk (Nat.not_lt_zero _),λr hr=>?_⟩ have g0:good 0=true:=rfl interval_cases r <;> exact ⟨g0,λk hk=>absurd hk (Nat.not_lt_zero _),(show (0:UInt32)<=0x7FFFFFFF from by decide)⟩ have D:=qC6 (n:=inW.toUSize) G.sX (by rw[G.nm]; exact G.hX.le) G.gX inW 0 _ 0 0 0 0 0 0 0 0 (by rw[G.nm,show (0:USize).toNat=0 from rfl,Β]) D0 rw[G.nm] at D exact qDk (qCh G relu D outW 0 _ א H0) (λq _=>Ε _ G.po) . have H0:qD5 (rows*outW) (qEy inW outW relu X W b) (λq=>q<0*outW) (Χ (rows*outW) 0) := ⟨by simp,λq _ h=>by rw[ϸ] at h; exact absurd h (Nat.not_lt_zero _)⟩ have P0:(pushZ inW.toUSize 0 (FloatArray.emptyWithCapacity inW)).size=inW:=by rw[qCc inW.toUSize inW 0 _ (by rw[G.nm,show (0:USize).toNat=0 from rfl, Β])] exact Β _ exact qDk (qCm G relu rows 0 _ _ _ א (by simp) P0 H0) (λq hq=>hq) . rw[Zi]; rfl end lemma linF_eq:linF=linearSpec:=C_linF_eq def kernels:Kernels:=⟨specCore,⟨linF,gemmSpec⟩,specConv,specElem,⟨Ybb2,MYbc⟩⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specDense,specNorm,linF_eq,T4b,Zdt] end Submission