import ContractExact namespace Submission open ExactRuntime FloatLib.Floats.Formats.BinaryInterchange FloatLib.Floats FloatLib.Floats.ExecFloat.Binary local notation"А"=>USize.lt_iff_toNat_lt local notation"Б"=>mul_le_mul_of_nonneg_right local notation"В"=>0x7FFFFFFF local notation"Г"=>mul_le_mul_of_nonneg_left local notation"Д"=>rationalWord local notation"Е"=>0xFFFFFFFF local notation"Ж"=>abs_of_nonneg local notation"З"=>Nat.mod_eq_of_lt local notation"И"=>Array.replicate local notation"Й"=>abs_nonneg local notation"К"=>List.range local notation"Л"=>Nat.zero_add local notation"М"=>UInt32.le_iff_toNat_le local notation"Н"=>mul_nonneg local notation"О"=>Nat.and_two_pow_sub_one_eq_mod local notation"П"=>Float.ofBits local notation"Р"=>two_ne_zero local notation"С"=>mul_le_mul local notation"Т"=>Nat.mul_le_mul_right local notation"У"=>dite_eq_left local notation"Ф"=>FloatArray.emptyWithCapacity local notation"Х"=>ite_eq_right local notation"Ц"=>0x7F800000 local notation"Ч"=>Model.roundDyadic local notation"Ш"=>zpow_le_zpow_right₀ local notation"Щ"=>UInt32.toNat_inj local notation"Ъ"=>dite_eq_right local notation"Ы"=>toDyadic_ofUInt32 local notation"Ь"=>abs_add_le local notation"Э"=>getElem!_pos local notation"Ю"=>Nat.cast_nonneg local notation"Я"=>Model.isFinite local notation"а"=>zpow_add₀ local notation"б"=>mul_lt_mul_of_pos_right local notation"в"=>Model.toReal local notation"г"=>abs_of_pos local notation"д"=>FloatLib.Numerics.Dyadic.cast_toRat local notation"е"=>abs_mul local notation"ж"=>Nat.mul_comm local notation"з"=>ite_eq_left local notation"и"=>abs_sub_comm local notation"й"=>List.mem_range local notation"к"=>zpow_natCast local notation"л"=>UInt64.toNat_add local notation"м"=>Nat.div_lt_of_lt_mul local notation"н"=>UInt32.toNat_toUSize local notation"о"=>Model.toReal_eq_unpackedToReal_toModel local notation"п"=>Real.sqrt local notation"р"=>Finset.range local notation"с"=>Nat.add_assoc local notation"т"=>Nat.shiftRight_eq_div_pow local notation"у"=>Nat.add_zero local notation"ф"=>Float.sqrt local notation"х"=>0x80000000 local notation"ц"=>lt_of_le_of_lt local notation"ч"=>lt_of_lt_of_le local notation"ш"=>Nat.div_eq_of_lt local notation"щ"=>Nat.shiftLeft_eq local notation"ъ"=>UInt64.toNat_shiftRight local notation"ы"=>zpow_pos local notation"ь"=>FloatLib.Numerics.binaryRadix local notation"э"=>Nat.mod_lt local notation"ю"=>UInt32.lt_iff_toNat_lt local notation"я"=>UInt64.lt_iff_toNat_lt local notation"ذ"=>Bool.and_eq_true_iff local notation"ر"=>Finset.sum_congr local notation"ز"=>UInt32.toNat_and local notation"س"=>UInt32.toNat_ofNat' local notation"ش"=>USize.toNat_one local notation"ص"=>Nat.pos_of_ne_zero local notation"ض"=>pow_le_pow_left₀ local notation"ط"=>0x3CE0000000000000 local notation"ظ"=>Model.modelSignBit local notation"ع"=>Nat.le_mul_of_pos_right local notation"غ"=>getElem!_neg local notation"ػ"=>Nat.succ_mul local notation"ؼ"=>Float.Model.UnpackedFloat.unpack local notation"ؽ"=>Finset.mem_range local notation"ؾ"=>le_trans local notation"ؿ"=>List.mem_of_getElem? local notation"ـ"=>Nat.mul_add_div local notation"ف"=>Nat.mul_add_mod local notation"ق"=>sq_nonneg local notation"ك"=>0x3370000000000000 local notation"ل"=>Model.toModel_ofFloatModel local notation"م"=>Nat.add_mul local notation"ن"=>zpow_sub₀ local notation"ه"=>Array.ext local notation"و"=>UInt64.toNat_and local notation"ى"=>FloatLib.Numerics.Dyadic.toRat_eq_zero_iff local notation"ي"=>UInt32.toNat_sub_of_le local notation"Ա"=>List.foldl_cons local notation"Բ"=>List.mem_cons_of_mem local notation"Գ"=>List.range_eq_range' local notation"Դ"=>Nat.one_mul local notation"Ե"=>abs_sub local notation"Զ"=>List.map_congr_left local notation"Է"=>List.mem_cons_self local notation"א"=>by omega local notation"ב"=>by norm_num local notation"ג"=>by simp local notation"ד"=>by linarith section open FloatLib.Numerics end lemma qAa (n:Nat) (f:Nat->UInt32) (k:Nat) (hk:k< n) : (Array.ofFn (n:=n) λi=>f i.val)[k]! = f k:=by rw[Э _ k (by simpa using hk),Array.getElem_ofFn] lemma qBo (w:Word):toWord (ofW w)=w:=by apply Щ.mp;simp only[toWord,ofW,Model.toNatBits,ofUInt32_bits,UInt32.toNat_ofNat'];exact З w.toBitVec.isLt lemma qBl (z:Model Fmt):ofW (toWord z)=z:=by have h:z.toNatBits<2^32:=Model.toNatBits_lt_two_pow z;simp only[ofW,toWord,Model.ofUInt32,UInt32.toNat_ofNat',Nat.mod_eq_of_lt h];exact Model.ofNatBits_toNatBits z @[inline] def keyOf (w:Word):Int := if signOf w then -((mantOf w <<< idxOf w:Nat):Int) else ((mantOf w <<< idxOf w:Nat):Int) lemma qGi (w:Word):keyOf w=sv w*2^idxOf w:=by unfold keyOf sv;split<;>simp[Nat.shiftLeft_eq] lemma qAg (w:Word) : (decodeD w).toReal=(keyOf w:ℝ)*2^(-149:Int):=by rw[<-д,decodeD_toRat,qGi];push_cast;rw[ن Р,к];ring @[inline] def maxW (a b:Word):Word := if expOf a=255∨expOf b=255 then toWord (Model.maximum (ofW a) (ofW b)) else match compare (keyOf a) (keyOf b) with | .lt=>b | .gt=>a | .eq=>toWord (if Model.isZero (ofW a) && Model.isZero (ofW b) then Model.zero Fmt (Model.signBit (ofW a) && Model.signBit (ofW b)) else ofW a) lemma qDy (a b:Word) : Model.cmpDyadic (decodeD a) (decodeD b)=compare (keyOf a) (keyOf b):=by have hs:(0:ℝ)<2^(-149:Int):=ы two_pos _;cases h:compare (keyOf a) (keyOf b);. rw[Model.cmpDyadic_lt_iff,qAg,qAg];exact б (by exact_mod_cast compare_lt_iff_lt.mp h) hs . rw[Model.cmpDyadic_eq_iff,qAg,qAg,compare_eq_iff_eq.mp h] . rw[Model.cmpDyadic_gt_iff,qAg,qAg];exact б (by exact_mod_cast compare_gt_iff_gt.mp h) hs lemma qFp (a b:Word):ofW (maxW a b)=Model.maximum (ofW a) (ofW b):=by unfold maxW;split;. exact qBl _ . rename_i hs;have ha:=Ы a (λh=>hs (Or.inl h));have hb:=Ы b (λh=>hs (Or.inr h));have hna:=Model.isNaN_eq_false_of_toDyadic?_some ha;have hnb:=Model.isNaN_eq_false_of_toDyadic?_some hb;have hc:Model.compare (ofW a) (ofW b)=some _ :=Model.compare_eq_some_cmpDyadic_of_toDyadic? _ _ ha hb;rw[qDy] at hc;have hnone:Model.chooseNaN2 (ofW a) (ofW b)=none :=(Model.chooseNaN2_eq_none_iff _ _).2 ⟨hna,hnb⟩;unfold Model.maximum;rw[Model.withNaNSelection_of_none _ _ hnone];dsimp only;rw[Model.compareNonNaN_eq_of_compare_eq_some _ _ _ _ hc];cases compare (keyOf a) (keyOf b)<;>simp only[qBl] def maxpoolW (c h w k:Nat) (inp:Array Word):Array Word := Array.ofFn (n:=c*(h/k*(w/k))) λq => let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) (К (k*k)).foldl (λacc t=>maxW acc (poolAt h w k inp ch y x t)) (poolAt h w k inp ch y x 0) lemma qEk (p:Nat->Word) (l:List Nat) (acc:Word) : l.foldl (λm t=>Model.maximum m (ofW (p t))) (ofW acc) =ofW (l.foldl (λa t=>maxW a (p t)) acc):=by induction l generalizing acc with | nil=>rfl | cons t l ih=>simp only[List.foldl_cons,<-qFp,ih] lemma qD0:maxpoolW=maxpoolSpec:=by funext c h w k inp;simp only[maxpoolW,maxpoolSpec,qEk,qBo] lemma qDs {w:UInt32} (h:expOf w≠255):Я (ofW w)=true:=by simp[ofW,isFinite_ofUInt32,h] lemma qEt (A B:Array UInt32) (hB:B.size=A.size) (hall:∀ k,k< A.size->expOf A[k]!≠255∧expOf B[k]!≠255) : finishWord (dsum A B 0 A.size) (-298) (A.size≠0 && dallNZ A B 0 A.size)=dotSpec A B:=by set L:=A.map (Model.ofUInt32 (fmt:=Fmt)) with hL;set R:=B.map (Model.ofUInt32 (fmt:=Fmt)) with hR;have hLR:L.size=R.size:=by simp[hL,hR,hB] have hS:L.size=A.size:=by simp[hL] have hLf:∀ k (hk:k< L.size),Я L[k]=true:=λk hk=>by simpa [hL,ofW] using qDs (getElem!_pos A k א ▸ (hall k א).1) have hRf:∀ k (hk:k< R.size),Я R[k]=true:=λk hk=>by simpa [hR,ofW] using qDs (getElem!_pos B k א ▸ (hall k א).2) have f k (_:0<=k) (h:k<0+L.size):=hall k א;have s:=(finiteDotSlice_eq A B 0 L.size (by simp[hL]) (by simp[hL,hB]) f).trans (by rw[hS]);have x:=(Model.Reduction.Internal.dotState_exact_toRat (destination:=Fmt) L R hLR hLf hRf).trans s;unfold dotSpec Model.dot;by_cases h0:dsum A B 0 A.size=0;. rw[Model.Reduction.dotWithStatus_eq_zero_of_finite Fmt L R .nearestEven hLR hLf hRf ((ى _).1 (by simp[x,h0])),h0,finishWord_zero];obtain ⟨h1,h2⟩:=dotStateLoop_flags A B {} 0 L.size (by simp[hL]) (by simp[hL,hB]) f;simp only[Except.map,Model.Reduction.Internal.dotState];rw[h1,h2];simp[hS] . rw[Model.Reduction.dotWithStatus_eq_round_of_finite_nonzero Fmt L R .nearestEven hLR hLf hRf (by rw[Model.Reduction.Semantics.finiteDot_toList_eq_slice L R hLR,s] simpa using h0)] exact finishWord_of_ne _ _ _ _ h0 x structure Acc where (pos neg:Array Nat) (special negZero:Bool) def Acc.total (acc:Acc):Int:=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList @[inline] def Acc.add (acc:Acc) (m idx:Nat) (neg:Bool):Acc := match acc with | ⟨pos,ng,sp,nz⟩ => if m=0 then ⟨pos,ng,sp,nz && neg⟩ else if neg then ⟨pos,addLimbs64 ng m.toUInt64 idx,sp,false⟩ else ⟨addLimbs64 pos m.toUInt64 idx,ng,sp,false⟩ lemma Acc.add_spec (acc:Acc) (m idx:Nat) (neg:Bool) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hm:m<2^64) (hi:idx/32+1<18) : (acc.add m idx neg).pos.size=18∧(acc.add m idx neg).neg.size=18 ∧ (acc.add m idx neg).special=acc.special ∧ (acc.add m idx neg).negZero=(acc.negZero && (decide (m=0) && neg)) ∧ (acc.add m idx neg).total=acc.total+(if neg then -(m:Int) else m)*2^idx:=by obtain ⟨pos,ng,sp,nz⟩:=acc;simp only at hp hn;by_cases hm0:m=0;. simp[Acc.add,Acc.total,hm0,hp,hn] . cases neg;. refine ⟨by simp[Acc.add,hm0,addLimbs64_size,hp],by simp[Acc.add,hm0,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,Bool.false_eq_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring . refine ⟨by simp[Acc.add,hm0,hp],by simp[Acc.add,hm0,addLimbs64_size,hn],by simp[Acc.add,hm0],by simp[Acc.add,hm0],?_⟩;simp only[Acc.add,hm0,ite_false,ite_true,addLimbs64_eq _ _ _ hm,Acc.total];rw[addLimbs_val _ _ _ א];push_cast;ring @[inline] def dotStep (acc:Acc) (wa wb:UInt32):Acc := if expOf wa=255∨expOf wb=255 then ⟨acc.pos,acc.neg,true,acc.negZero⟩ else acc.add (mantOf wa*mantOf wb) (idxOf wa+idxOf wb) (Bool.xor (signOf wa) (signOf wb)) @[specialize] def dotLoop (fa fb:Nat->UInt32) (pos neg:Array Nat) (sp nz:Bool) (index:Nat):Nat->Acc | 0=>⟨pos,neg,sp,nz⟩ | count+1 => match dotStep ⟨pos,neg,sp,nz⟩ (fa index) (fb index) with | ⟨p,n,s,z⟩=>dotLoop fa fb p n s z (index+1) count lemma qCf (fa fb:Nat->UInt32) (pos neg:Array Nat) (nz:Bool) (index count:Nat):(dotLoop fa fb pos neg true nz index count).special=true:=by induction count generalizing pos neg nz index with | zero=>rfl | succ count ih => rw[dotLoop];have hs:(dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)).special=true:=by unfold dotStep Acc.add;split_ifs<;>rfl generalize dotStep ⟨pos,neg,true,nz⟩ (fa index) (fb index)=r at hs ⊢;obtain ⟨p,q,s,z⟩:=r;simp only at hs;subst hs;exact ih .. lemma qDk (fa fb:Nat->UInt32) (A B:Array UInt32) (n:Nat) (hA:∀ k,k< n->A[k]! = fa k) (hB:∀ k,k< n->B[k]! = fb k) (acc:Acc) (index remaining:Nat) (h:index+remaining<=n) (hp:acc.pos.size=18) (hn:acc.neg.size=18) (hfin:(dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).special =false) : (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).pos.size=18 ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).neg.size=18 ∧ acc.special=false ∧ (∀ k,index<=k->k< index+remaining->expOf A[k]!≠255∧expOf B[k]!≠255) ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).total =acc.total+dsum A B index remaining ∧ (dotLoop fa fb acc.pos acc.neg acc.special acc.negZero index remaining).negZero =(acc.negZero && dallNZ A B index remaining):=by induction remaining generalizing acc index with | zero=>obtain ⟨p,n,s,z⟩:=acc;simp_all [dotLoop,dsum,dallNZ] | succ count ih => obtain ⟨p,q,s,z⟩:=acc simp only at hp hn have ha:=hA index א have hb:=hB index א by_cases he:expOf (fa index)=255∨expOf (fb index)=255 . exfalso;rw[dotLoop] at hfin;have hs:(dotStep ⟨p,q,s,z⟩ (fa index) (fb index)).special=true:=by simp[dotStep,he] generalize dotStep ⟨p,q,s,z⟩ (fa index) (fb index)=r at hs hfin;obtain ⟨p',q',s',z'⟩:=r;simp only at hs;subst hs;rw[qCf] at hfin;exact Bool.noConfusion hfin . have he':=not_or.mp he obtain ⟨h1,h2,h3,h4,h5⟩ := Acc.add_spec ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))) hp hn (mul_mant_lt _ _) (by have:=idxOf_le _ he'.1;have:=idxOf_le _ he'.2;omega) have hstep:dotStep ⟨p,q,s,z⟩ (fa index) (fb index) = Acc.add ⟨p,q,s,z⟩ (mantOf (fa index)*mantOf (fb index)) (idxOf (fa index)+idxOf (fb index)) (Bool.xor (signOf (fa index)) (signOf (fb index))):=by simp[dotStep,he] rw[dotLoop,hstep] at hfin ⊢;obtain ⟨hP,hN,hs,hall,hD,hz⟩:=ih _ (index+1) א h1 h2 hfin;refine ⟨hP,hN,by rw[<-h3,hs],?_,?_,?_⟩ . intro k k1 k2;rcases Nat.lt_or_ge k (index+1) with hk | hk;. have:k=index :=א;subst this;rw[ha,hb];exact he' . exact hall k hk א . have sv2:(if Bool.xor (signOf (fa index)) (signOf (fb index)) then -((mantOf (fa index)*mantOf (fb index):Nat):Int) else ((mantOf (fa index)*mantOf (fb index):Nat):Int)) =sv (fa index)*sv (fb index):=by unfold sv;cases signOf (fa index)<;>cases signOf (fb index)<;>simp rw[hD,h5,dsum,ha,hb,sv2];ring . rw[hz,h4,dallNZ,ha,hb];simp[Bool.and_assoc] def Y_low (p q:Array Nat) (k:Nat):Nat->Nat | 0=>k | f+1=>if p[k]! = 0 && q[k]! = 0 then Y_low p q (k+1) f else k lemma qEb (p q:Array Nat):∀ f k,(∀ j,j< k->p[j]! = 0∧q[j]! = 0) → ∀ j,j< Y_low p q k f->p[j]! = 0∧q[j]! = 0:=by intro f;induction f with | zero=>exact λk h j hj=>h j hj | succ f ih => intro k h j hj;unfold Y_low at hj;split at hj . rename_i hk;simp only[Bool.and_eq_true,decide_eq_true_eq] at hk;refine ih (k+1) (λi hi=>?_) j hj;rcases Nat.lt_or_ge i k with h1 | h1;. exact h i h1 . have:i=k :=א;subst this;exact hk . exact h j hj lemma qH1 (L:List Nat):∀ l,(∀ j,j< l->L[j]! = 0) → limbVal L=2^(32*l)*limbVal (L.drop l):=by induction L with | nil=>simp[limbVal] | cons x xs ih => intro l h;cases l with | zero=>simp | succ l => have h0:x=0:=by simpa using h 0 א have:=ih l (λj hj=>by simpa using h (j+1) א);simp only[limbVal,List.drop_succ_cons,h0,this];rw[Nat.mul_add 32 l 1,pow_add];ring lemma qGq (A:Array Nat) (l:Nat) (h:∀ j,j< l->A[j]! = 0) : limbVal A.toList=2^(32*l)*limbValA (A.extract l A.size):=by rw[limbValA_eq,Array.toList_extract,List.extract_eq_drop_take,<-Array.length_toList];rw[List.take_of_length_le ג];refine qH1 _ l λj hj=>?_;have:=h j hj;rw[getElem!_def] at this;simp only[Array.getElem!_toList,getElem!_def] at *;cases e:A[j]?<;>simp_all lemma qKh (S:Int) (l:Nat) (e:Int) (z:Bool) : finishWord S (e+32*l) z=finishWord (2^(32*l)*S) e z:=by by_cases hS:S=0;. simp[hS,finishWord] have h2:(2:Int)^(32*l)*S≠0:=mul_ne_zero (by positivity) hS rw[finishWord_of_ne (ofIntDyadic (2^(32*l)*S) e) S (e+32*l) z hS (by rw[toRat_ofIntDyadic,а ב,show (32:ℤ)*l=((32*l:ℕ):ℤ) by push_cast;ring,к];push_cast;ring)] simp[finishWord,h2] @[inline] def dotFn (n:Nat) (fa fb:Nat->UInt32):UInt32 := let acc:=dotLoop fa fb (И 18 0) (И 18 0) false true 0 n if acc.special then dotSpec (Array.ofFn (n:=n) λi=>fa i) (Array.ofFn (n:=n) λi=>fb i) else let l:=Y_low acc.pos acc.neg 0 18 finishWord ((limbValA (acc.pos.extract l 18):Int)-limbValA (acc.neg.extract l 18)) (-298+32*l) (n≠0 && acc.negZero) lemma qAe:dotFn=dotOf:=by funext n fa fb;unfold dotFn dotOf;set A:=Array.ofFn (n:=n) λi=>fa i with hA;set B:=Array.ofFn (n:=n) λi=>fb i with hB;set acc:=dotLoop fa fb (И 18 0) (И 18 0) false true 0 n with hacc;by_cases hsp:acc.special=true;. simp[hsp] have hsp':acc.special=false:=by simpa using hsp have hF:=qDk fa fb A B n (qAa n fa) (qAa n fb) ⟨И 18 0,И 18 0,false,true⟩ 0 n א ג ג hsp';obtain ⟨hP,hN,-,hall,hD,hz⟩:=hF;have hAs:A.size=n:=by simp[hA] have hBs:B.size=n:=by simp[hB] have hL:=qEb acc.pos acc.neg 18 0 (λj hj=>absurd hj (Nat.not_lt_zero _));simp only[hsp',Bool.false_eq_true,ite_false];have e0:(⟨И 18 0,И 18 0,false,true⟩:Acc).total=0:=by decide rw[e0,Int.zero_add] at hD;rw[Bool.true_and] at hz;generalize Y_low acc.pos acc.neg 0 18=l at hL ⊢;have h1:=qGq acc.pos l (λj hj=>(hL j hj).1);have h2:=qGq acc.neg l (λj hj=>(hL j hj).2);rw[hP] at h1;rw[hN] at h2;rw[qKh,<-qEt A B א (λk hk=>hall k א א),hAs,<-hD,<-hz];congr 1;have:acc.total=(limbVal acc.pos.toList:Int)-limbVal acc.neg.toList:=rfl;rw[this,h1,h2];push_cast;ring lemma usz (t n:USize) (h:t< n):(t+1).toNat=t.toNat+1:=by have h1:t.toNat< n.toNat:=А.mp h;have h2:n.toNat< USize.size:=n.toNat_lt_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add,ش];exact З א lemma qAo (a b:USize) (h:a.toNat+b.toNat<2^32):(a+b).toNat=a.toNat+b.toNat:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_add];exact З א lemma uk (k:Nat) (h:k<2^32):(USize.ofNat k).toNat=k:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_ofNat'];exact З א lemma qB6 (a:FloatArray) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[FloatArray.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega lemma qBg (a:Array UInt32) (i:USize) (h:i< a.usize):i.toNat< a.size:=by have h1:=А.mp h;have h2:a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ omega @[inline] def rdF (a:FloatArray) (i:USize):Float := if h:i< a.usize then a.uget i (qB6 a i h) else 0 @[inline] def rdW (a:Array UInt32) (i:USize):UInt32 := if h:i< a.usize then a.uget i (qBg 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 (qB6 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 (qBg a i h) else a def pwEntry (i:Nat):Float := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then П s else П (s ||| ((e+873) <<< (52:UInt64))) def pw:FloatArray := (К 512).foldl (λa i=>a.push (pwEntry i)) (Ф 512) lemma qD1:pw.data=(К 512).toArray.map pwEntry:=by have key:∀ (l:List Nat) (a:FloatArray),(l.foldl (λa i=>a.push (pwEntry i)) a).data=a.data ++ (l.toArray.map pwEntry):=by intro l;induction l with | nil=>intro a;simp | cons x l ih => intro a;simp only[List.foldl_cons,ih];simp[FloatArray.push] simp only[pw,key];simp[FloatArray.emptyWithCapacity] lemma qHk:pw.size=512:=by simp[FloatArray.size,qD1] lemma qDo (w:UInt32):(w >>> (23:UInt32)).toUSize.toNat< pw.size:=by rw[qHk];have h:=w.toNat_lt_size;simp only[UInt32.toNat_toUSize,UInt32.toNat_shiftRight];norm_num [Nat.shiftRight_eq_div_pow] at h ⊢;omega @[inline] def dv (w:UInt32):Float := ((w &&& 0x7FFFFF) ||| 0x800000).toFloat*pw.uget (w >>> (23:UInt32)).toUSize (qDo w) @[inline] def dvB (v:Float) (w:UInt32):Float := (v*pw.uget (w >>> (23:UInt32)).toUSize (qDo w))*((w &&& 0x7FFFFF) ||| 0x800000).toFloat @[inline] def good (w:UInt32):Bool := let v:=w &&& В v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32) def scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):UInt32 := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& В let u:=v-1 scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else if 0x007FFFFF<=lo∧hi<Ц then hi else Е termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];omega lemma qAf (a:Array UInt32):a.usize.toNat<=a.size:=by simp only[Array.usize,Nat.toUSize_eq,USize.toNat_ofNat'];exact Nat.mod_le _ _ def pushZ (n k:USize) (a:FloatArray):FloatArray := if h:k< n then pushZ n (k+1) (a.push 0) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega @[inline] def zivW (A B:UInt64):UInt32 := let x:UInt64:=(A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let y:UInt64:=(B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64) let ex:UInt64:=(x >>> (23:UInt64)) &&& (0x7FF:UInt64) if x == y && ex-(897:UInt64)<(254:UInt64) then (((x >>> (34:UInt64)) <<< (31:UInt64)) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< (23:UInt64)))).toUInt32 else Е @[inline] def zivS (s:Float):UInt32 := let a:=s.toBits zivW (a+2) (a-2) @[inline] def ziv (s e:Float):UInt32 := zivW (s+e).toBits (s-e).toBits @[noinline] def slowOut (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat):Word := act relu (dotFn (inW+1) (λt=>if t< inW then X[i*inW+t]! else oneW) (λt=>if t< inW then W[j*inW+t]! else b[j]!)) @[inline] def fin (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf t:Float) (bw:UInt32):UInt32 := let bv:=dv bw let r:=ziv (s+bv) (nf*(t+bv.abs)) if r == Е then slowOut inW relu X W b i j else act relu r structure F1 where (s t:Float) def tightGo (X W:Array UInt32) (xo wo n t:USize) (s a:Float):F1 := if h:t< n then let q:=dv (rdW X (xo+t))*dv (rdW W (wo+t)) tightGo X W xo wo n (t+1) (s+q) (a+q.abs) else ⟨s,a⟩ termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega @[inline] def fin8 (inW:Nat) (relu:Bool) (X W b:Array Word) (i j:Nat) (s nf a:Float) (hx bw:UInt32) (n:USize):UInt32 := let bv:=dv bw let r:=ziv (s+bv) (nf*(dv hx*a+bv.abs)) if r == Е then let q:=tightGo X W (i*inW).toUSize (j*inW).toUSize n 0 0 0 fin inW relu X W b i j q.s nf q.t bw else act relu r structure R1 where (ix:Array UInt32) (buf:FloatArray) (c:USize) (hi:UInt32) @[inline] def mx (a w:UInt32):UInt32 := let m:=w &&& В if m<=a then a else m def decR (X:Array UInt32) (o n t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hi:UInt32):R1 := if h:t< n then let w:=rdW X (o+t) if w &&& В == 0 then decR X o n (t+1) ix buf c hi else decR X o n (t+1) (wrW ix c t.toUInt32) (wrF buf c (dv w)) (c+1) (mx hi w) else ⟨ix,buf,c,hi⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];have:=А.mp h;omega) lemma wi (w i m:USize) (W:Array UInt32) (hw:w.toNat+m.toNat<=W.size) (hs:W.size<2^32) (hi:i.toNat< m.toNat):(w+i).toNat< W.size:=by rw[qAo _ _ א];omega structure F4 where (a b c d:Float) def go4B (x:FloatArray) (ix W:Array UInt32) (w0 w1 w2 w3 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) (a b c d:Float):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let q0:=v*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) let q1:=dvB v (W.uget (w1+i) (wi w1 i m W h1 hs him)) let q2:=v*dv (W.uget (w2+i) (wi w2 i m W h2 hs him)) let q3:=dvB v (W.uget (w3+i) (wi w3 i m W h3 hs him)) go4B x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 (a+q0) (b+q1) (c+q2) (d+q3) else go4B x ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 a b c d else ⟨a,b,c,d⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) def go1 (x:FloatArray) (ix W:Array UInt32) (w0 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (a:Float):Float := if h:t< n then have ht:=А.mp h let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm let q:=x.uget t א*dv (W.uget (w0+i) (wi w0 i m W h0 hs him)) go1 x ix W w0 m n (t+1) hx hi hs h0 (a+q) else go1 x ix W w0 m n (t+1) hx hi hs h0 a else a termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) def go4D (x wd:FloatArray) (ix W:Array UInt32) (w0 w1 w2 w3 m n t:USize) (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) (hd:wd.size=W.size) (a b c d:Float):F4 := if h:t< n then have ht:=А.mp h let v:=x.uget t א let i:=(ix.uget t א).toUSize if hm:i< m then have him:=А.mp hm go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd (a+v*wd.uget (w0+i) (hd ▸ wi w0 i m W h0 hs him)) (b+v*wd.uget (w1+i) (hd ▸ wi w1 i m W h1 hs him)) (c+v*wd.uget (w2+i) (hd ▸ wi w2 i m W h2 hs him)) (d+v*wd.uget (w3+i) (hd ▸ wi w3 i m W h3 hs him)) else go4D x wd ix W w0 w1 w2 w3 m n (t+1) hx hi hs h0 h1 h2 h3 hd a b c d else ⟨a,b,c,d⟩ termination_by n.toNat-t.toNat decreasing_by all_goals (rw[usz t n h];omega) def cols1 (inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (i:Nat) (ix:Array UInt32) (x wd:FloatArray) (nf a:Float) (hx:UInt32) (m n:USize) (j:Nat) (base:USize) (out:Array UInt32):Array UInt32 := if h4:j+4<=outW then let ju:=j.toUSize let w0:=ju*m let w1:=(ju+1)*m let w2:=(ju+2)*m let w3:=(ju+3)*m if hg:n.toNat<=x.size∧n.toNat<=ix.size∧W.size<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:=if hd:wd.size=W.size then go4D x wd 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 hd 0 0 0 0 else go4B 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 let o0:=fin8 inW relu X W b i j r.a nf a hx (rdW b ju) m let o1:=fin8 inW relu X W b i (j+1) r.b nf a hx (rdW b (ju+1)) m let o2:=fin8 inW relu X W b i (j+2) r.c nf a hx (rdW b (ju+2)) m let o3:=fin8 inW relu X W b i (j+3) r.d nf a hx (rdW b (ju+3)) m let out:=wrW out (base+ju) o0 let out:=wrW out (base+ju+1) o1 let out:=wrW out (base+ju+2) o2 let out:=wrW out (base+ju+3) o3 cols1 inW outW relu X W b i ix x wd nf a hx m n (j+4) base out else out else if h1:j< outW then let ju:=j.toUSize let w0:=ju*m if hg:n.toNat<=x.size∧n.toNat<=ix.size∧W.size<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 let o:=fin8 inW relu X W b i j r nf a hx (rdW b ju) m cols1 inW outW relu X W b i ix x wd nf a hx m n (j+1) base (wrW out (base+ju) o) else out else out termination_by outW-j def rows1 (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32) (wd:FloatArray) (nf a: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 0 let out:=cols1 inW outW relu X W b i r.ix r.buf wd nf a r.hi m r.c 0 (i*outW).toUSize out rows1 rows inW outW relu X W b wd nf a m (i+1) r.ix r.buf out else out termination_by rows-i def linF (rows inW outW:Nat) (relu:Bool) (X W b:Array UInt32):Array UInt32 := let hw:=scan W W.usize 0 (qAf W) Е 0 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 (qAf X) Е 0≠Е∧hw≠Е ∧ scan b b.usize 0 (qAf b) Е 0≠Е then let n:=inW.toUSize let nf:Float:=(inW.toUInt64+8).toFloat*П 0x3CC0000000000000 let out:Array UInt32:=И (rows*outW) 0 let wd:=if 8< rows then W.foldl (λa w=>a.push (dv w)) (Ф W.size) else Ф 0 rows1 rows inW outW relu X W b wd nf ((inW.toUInt64+1).toFloat*dv hw) n 0 (И inW 0) (pushZ n 0 (Ф inW)) out else linearWith dotFn rows inW outW relu X W b section local notation:max x:max "ᶠ"=>Float.isFinite x=true local notation "⟡"=>FloatFormat.binary64 local notation "⟠"=>Float.Model.Format.binary64 noncomputable def R (f:Float):ℝ:=в (toModel (ofFloat f)) noncomputable def wv (w:UInt32):ℝ:=в (ofW w) noncomputable def exactS (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32):ℝ := (р n).sum (λt=>wv (xw t)*wv (ww t))+wv bw def nfOf (n:Nat):Float:=(n.toUInt64+8).toFloat*П 0x3CC0000000000000 lemma qAm:⟡.isIEEE=true:=by decide lemma qA2 (a:Model ⟡) : Я a=(Model.toModel a).isFinite:=by rw[<-Model.toDyadic?_isSome_eq_isFinite,Model.toDyadic?_ieee_eq_model _ qAm];cases Model.toModel a<;>rfl abbrev A_M (x:Float):Model ⟡:=Model.ofFloatModel x.toModel lemma A_R (x:Float):R x=в (A_M x):=rfl lemma qAk (x:Float):Я (A_M x)=x.isFinite:=by rw[qA2,ل];rfl lemma qA5 {x:Float} (h:xᶠ):Я (A_M x)=true :=(qAk x).trans h lemma qA4 (e:ℤ):Formats.Flocq.bpow ь e=(2:ℝ)^e:=by simp[Formats.Flocq.bpow,FloatLib.Numerics.binaryRadix,FloatLib.Numerics.Radix.toReal] lemma qCl:(2:ℝ)^(1000:ℤ)<=в (Model.posMaxFinite ⟡):=by rw[Model.toReal_posMaxFinite,show Model.pow2 ⟡.fracWidth + ⟡.maxFiniteFracField=2^53-1 by decide +kernel, show ⟡.maxNormalExponent-Int.ofNat ⟡.fracWidth=971 by decide +kernel,Model.bpow,qA4,show (1000:ℤ)=29+971 from rfl,а Р] exact Б ב (by positivity) open Float.Model.UnpackedFloat in lemma qD3 {b:BitVec 64} {s m e h} (a:unpack ⟠ b=.finite s m e h) : 2^52<=m∨e=-1074:=by unfold Float.Model.UnpackedFloat.unpack at a;simp only at a;rw[show (⟠.exponentBias:ℤ)=1023 by decide] at a;split_ifs at a with h1 h2 h3;. injection a with _ _ he;rw[show (unpackExponent (spec:=⟠) b).toNat=0 by simp[h3]] at he;omega . injection a with _ hm;rw[<-hm,BitVec.toNat_append];exact .inl (ؾ (by simp[Nat.shiftLeft_eq]) Nat.left_le_or) open Float.Model.UnpackedFloat in lemma qJh {s₁ s₂ m₁ m₂ e₁ e₂ h₁ h₂} {f g:BitVec 64} (e:unpack ⟠ f=.finite s₁ m₁ e₁ h₁) (b:unpack ⟠ g=.finite s₂ m₂ e₂ h₂) : e₁+e₂<=⟠.targetExponent (Float.Model.totalExponent (m₁*m₂) (e₁+e₂)):=by have a:2^52<=m₁*m₂->52<=(m₁*m₂).log2:=(Nat.le_log2 (Nat.mul_ne_zero h₁.ne' h₂.ne')).2 simp only[Float.Model.Format.targetExponent,Float.Model.totalExponent,Float.Model.Format.mantissaBits,Float.Model.Format.minExponent] norm_num rcases qD3 e with c | c . have:=a (c.trans (ع _ h₂));omega rcases qD3 b with d | d . have:=a (d.trans (Nat.le_mul_of_pos_left _ h₁));omega omega lemma qB5 (x y:Float) (a:xᶠ) (b:yᶠ) : A_M (x*y)=Model.mul (A_M x) (A_M y):=by change Model.ofFloatModel (Float.Model.mul x.toModel y.toModel)=_;rw[Float.Model.mul,Model.ofFloatModel_pack,Model.Proof.mul_eq_spec,Model.Spec.mul];simp only[Model.toDyadic?_ieee_eq_model _ qAm,Model.toModel_ofFloatModel];change x.toModel.unpack.isFinite=true at a;change y.toModel.unpack.isFinite=true at b;have c:∀ s t:Float.Model.UnpackedFloat.Sign,Model.ofModel ⟡ (.zero (s*t)) =Model.zero _ (ظ s ^^ ظ t):=λs t=>by cases s<;>cases t<;>simp[Model.modelSignBit] <;>first | exact (Model.negZero_eq_ofModel_zero _).symm | exact (Model.posZero_eq_ofModel_zero _).symm cases hxm:x.toModel.unpack<;>cases hym:y.toModel.unpack <;>simp only[hxm,hym,Float.Model.UnpackedFloat.isFinite,Bool.false_eq_true] at a b case finite.finite s₁ m₁ e₁ h₁ s₂ m₂ e₂ h₂ => have d:(m₁ == 0 || m₂ == 0)=false:=by simp[h₁.ne',h₂.ne'] have e:Model.modelSign (ظ s₁ ^^ ظ s₂)=s₁*s₂:=by cases s₁<;>cases s₂<;>rfl simp only[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul];rw[Model.roundWithAccuracy_exact_eq_round_of_le_targetExponent _ _ _ _ (qJh hxm hym),d];simp only[Bool.false_eq_true,ite_false,Model.roundDyadic,qAm,ite_true,Model.ieeeRoundDyadic,e];rfl all_goals simp[Model.unpackedToDyadic?,Float.Model.UnpackedFloat.mul,c] lemma A_E (z:Float) {a:Model ⟡} {c:ℝ} (h:A_M z=a) (b:Я a=true) (d:в a=Model.roundAt ⟡ c) : zᶠ∧R z=Model.roundAt ⟡ c:=by subst h;exact ⟨(qAk z).symm.trans b,d⟩ lemma qC7 (x y:Float) (hx:xᶠ) (hy:yᶠ) (hb : |R x| * |R y| ≤ 2^(1000:ℤ)) : (x*y)ᶠ ∧ R (x*y)=Model.roundAt ⟡ (R x*R y) := A_E _ (qB5 x y hx hy) (Model.isFinite_mul_of_abs_mul_le_posMaxFinite _ _ qAm (qA5 hx) (qA5 hy) (hb.trans qCl)) (Model.toReal_mul_eq_roundAt_of_abs_mul_le_posMaxFinite _ _ qAm (qA5 hx) (qA5 hy) (hb.trans qCl)) noncomputable def A_z:ℝ:=2^(-1021:ℤ) noncomputable def A_B:ℝ:=2^(300:ℤ) lemma qAs:0< A_z:=by unfold A_z;positivity lemma A_p (a b c d:ℤ) (h:a+b<=c+d):(2:ℝ)^a*2^b<=2^c*2^d:=by rw[<-а Р,<-а Р];exact Ш one_le_two h lemma qAx:A_z<=1/2^200*(1/2^200):=by simpa [A_z] using A_p (-1021) 0 (-200) (-200) ב lemma qKo:0< A_B:=by unfold A_B;positivity lemma qBv:(2:ℝ)^256*2^42<=A_B:=by simpa [A_B] using A_p 256 42 300 0 ב lemma qAh:(2:ℝ)^128<=A_B :=(by norm_num:(2:ℝ)^128<=2^256*2^42).trans qBv lemma qGl {k:ℕ} (h:k<=2^128):(k:ℝ)<=A_B :=(show (k:ℝ)<=2^128 by exact_mod_cast h).trans qAh lemma qFa:A_B*2<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 1 1000 0 ב lemma qCb:A_B*A_B<=2^(1000:ℤ):=by simpa [A_B] using A_p 300 300 1000 0 ב lemma qGx (x:ℝ) : |Model.roundAt ⟡ x-x| ≤ 1/2^53*(|x| + A_z):=by rw[show (1/2^53*(|x| + A_z):ℝ)=2^(-53:ℤ) * |x| + 2^(-1074:ℤ) by rw[show (1/2^53:ℝ)=2^(-53:ℤ) by norm_num,show (-1074:ℤ)=-53+-1021 from rfl,а Р (-53),mul_add] rfl] by_cases a:x=0 . rw[a,Model.roundAt_zero,sub_self,abs_zero];positivity refine (Model.abs_roundAt_sub_le _ x).trans ?_ change Formats.Flocq.ulp _ (Model.fexpOf ⟡) x/2<=_ rw[Formats.Flocq.ulp.of_ne_zero _ _ x a] simp only[Formats.Flocq.cexp,Model.fexpOf,Formats.Flocq.fltExp] have b:=Formats.Flocq.bpow_magnitude_sub_one_le ь x a rw[qA4] at b rw[qA4,show ((⟡.fracWidth:ℤ)+1)=53 from rfl,show ⟡.minSubnormalExponent=-1074 from rfl] generalize Formats.Flocq.magnitude ь x=M at b ⊢ have d:(0:ℝ)<=2^(-53:ℤ) * |x|:=by positivity have c:(0:ℝ)<2^(-1074:ℤ):=by positivity rcases le_total (-1074) (M-53) with h | h . rw[max_eq_left h,show (2:ℝ)^(M-53)/2=2^(-53:ℤ)*2^(M-1) by rw[<-а Р,show (-53:ℤ)+(M-1)=M-53-1 by ring,ن Р _ 1,zpow_one]] linarith [Г b (show (0:ℝ)<=2^(-53:ℤ) by positivity)] . rw[max_eq_right h];exact (half_le_self c.le).trans (le_add_of_nonneg_left d) lemma qGg {z:Float} {c:ℝ} (h:zᶠ∧R z=Model.roundAt ⟡ c) : zᶠ∧|R z-c| ≤ 1/2^53*(|c| + A_z) :=⟨h.1,h.2 ▸ qGx c⟩ lemma qEa {a b:ℝ} (d : |a| ≤ A_B) (c : |b| ≤ A_B) : |a| + |b| ≤ в (Model.posMaxFinite ⟡):=by have:=qFa have:=qCl linarith lemma qBa (x y:Float) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x+y)ᶠ∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) := qGg (A_E _ (toModel_ofFloat_add_of_isFinite x y hx hy) (Model.isFinite_add_of_abs_add_le_posMaxFinite _ _ qAm (qA5 hx) (qA5 hy) (qEa h1 h2)) (Model.toReal_add_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAm (qA5 hx) (qA5 hy) (qEa h1 h2))) lemma qDc (x y:Float) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x-y)ᶠ∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) := qGg (A_E _ (toModel_ofFloat_sub_of_isFinite x y hx hy) (Model.isFinite_sub_of_abs_add_le_posMaxFinite _ _ qAm (qA5 hx) (qA5 hy) (qEa h1 h2)) (Model.toReal_sub_eq_roundAt_of_abs_add_le_posMaxFinite _ _ qAm (qA5 hx) (qA5 hy) (qEa h1 h2))) lemma qBw (x y:Float) (hx:xᶠ) (hy:yᶠ) (h1 : |R x| ≤ A_B) (h2 : |R y| ≤ A_B) : (x*y)ᶠ∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) :=qGg (qC7 x y hx hy ((С h1 h2 (Й _) qKo.le).trans qCb)) lemma qAu (m k:ℤ) (hm:m.natAbs<2^53) (hk:-1074<=k) : Model.roundAt ⟡ ((m:ℝ)*2^k)=(m:ℝ)*2^k:=by apply Formats.Flocq.round_preserves_generic;exact (Formats.Flocq.generic_format_FLT_iff (⟡.minSubnormalExponent) (⟡.fracWidth+1) (by decide) _).2 ⟨by decide,⟨m,k⟩,by simp[Formats.Flocq.toReal,qA4],hm,hk⟩ lemma qBm (x:Float):R x=Model.unpackedToReal x.toModel.unpack:=by rw[A_R,о qAm,ل] lemma qFf {f:Float} {s m e h} (c:f.toModel.unpack=.finite s m e h) : (Model.ofModel ⟡ (.finite .positive m e h)).toModel=.finite .positive m e h:=by have a:Model.toModel (A_M f)=.finite s m e h:=(ل _).trans c;cases s;. have b:=Model.toModel_neg_of_isFinite qAm (A_M f) (by rw[a];rfl);rw[a] at b;change _=Float.Model.UnpackedFloat.finite .positive m e h at b;rw[<-b];exact Model.toModel_canonicalizeModel _ . rw[<-a];exact Model.toModel_canonicalizeModel _ lemma qBk (f:Float) (hf:fᶠ):f.absᶠ∧R f.abs = |R f|:=by have a:A_M f.abs=Model.ofModel ⟡ f.toModel.unpack.abs :=Model.ofFloatModel_pack _;change f.toModel.unpack.isFinite=true at hf;cases hu:f.toModel.unpack with | notANumber | infinity _=>simp[hu,Float.Model.UnpackedFloat.isFinite] at hf | zero s => rw[hu] at a;refine ⟨by rw[<-qAk,a];exact Model.isFinite_ofModel_zero _ qAm _,?_⟩;rw[A_R f.abs,a,qBm f,hu];simp[Float.Model.UnpackedFloat.abs,Model.toReal_ofModel_zero _ qAm] | finite s m e h => have b:=qFf hu;rw[hu] at a;have c:A_M f.abs=Model.ofModel ⟡ (.finite .positive m e h):=a;refine ⟨by rw[<-qAk,c,qA2,b];rfl,?_⟩;rw[A_R f.abs,c,qBm f,hu,о qAm,b];simp only[Model.unpackedToReal_finite,qA4];cases s<;>simp[Model.modelSignBit,abs_of_nonneg (by positivity:(0:ℝ)<=(m:ℝ)*2^e)] lemma qCw (f:Float) (k:ℕ) (g:k<2^53) (e:f.toModel=Float.Model.pack (Float.Model.UnpackedFloat.ofNat ⟠ k)) : fᶠ∧R f=k:=by obtain ⟨d,b,c⟩:∃ d,A_M f=Ч ⟡ d∧d.toReal=k:=by refine ⟨.ofScaledInt k 0,?_,?_⟩;. change Model.ofFloatModel f.toModel=_;rw[e,Model.ofFloatModel_pack];exact Model.ofModel_ofInt_eq_roundDyadic _ qAm (k:ℤ) . rw[<-д,FloatLib.Numerics.Dyadic.ofScaledInt_toRat];simp have a:Я (A_M f)=true:=by rw[b];apply Model.isFinite_roundDyadic_of_isIEEE_of_abs_toReal_le_posMaxFinite _ qAm;rw[c,Ж (Ю _)];exact ((qGl (א:k<=2^128)).trans ((le_mul_of_one_le_right qKo.le one_le_two).trans qFa)).trans qCl refine ⟨(qAk f).symm.trans a,?_⟩;rw[A_R,b,Model.toReal_roundDyadic_eq_roundAt _ qAm _ (b ▸ a),c];simpa using qAu k 0 (by simpa using g) ב lemma qB0 (m:UInt64) (h:m.toNat<2^53):m.toFloatᶠ∧R m.toFloat=m.toNat :=qCw _ _ h rfl lemma qI0:(0:Float)ᶠ∧R 0=0:=by simpa using qCw 0 0 ב rfl def A_bits (i:ℕ):UInt64 := let e:=(i%256).toUInt64 let s:UInt64:=if i ≥ 256 then 0x8000000000000000 else 0 if e == 0 then s else s ||| ((e+873) <<< (52:UInt64)) lemma qC1 (i:ℕ):pwEntry i=П (A_bits i):=by unfold pwEntry A_bits;dsimp only;split<;>rfl def A_chk (i:ℕ):Bool := match ؼ ⟠ (A_bits i).toBitVec with | .finite s m e _=>(ظ s == decide (256<=i)) && m == 2^52 && e == ((i%256:ℕ):ℤ)-202 && (i%256 != 0) | .zero _=>i%256 == 0 | _=>false lemma qFg:∀ i,i<512->A_chk i=true:=by decide +kernel lemma qDa (i:ℕ) (b:i<512) : (pwEntry i)ᶠ∧R (pwEntry i) =if i%256=0 then 0 else (if 256<=i then -1 else 1)*2^(((i%256:ℕ):ℤ)-150):=by have a:=qFg i b unfold A_chk at a rw[qC1,qBm] change (П (A_bits i)).toModel.unpack.isFinite=true∧_ rw[show (П (A_bits i)).toModel.unpack =ؼ ⟠ (A_bits i).toBitVec from Model.unpack_pack_unpack _ _] generalize ؼ ⟠ (A_bits i).toBitVec=u at a cases u with | notANumber | infinity _=>simp at a | zero s => simp only[beq_iff_eq] at a exact ⟨rfl,by simp[a]⟩ | finite s m e h => simp only[Bool.and_eq_true,beq_iff_eq,bne_iff_ne] at a obtain ⟨⟨⟨g,d⟩,f⟩,c⟩:=a refine ⟨rfl,?_⟩ rw[Model.unpackedToReal_finite,qA4,f,g,d,mul_assoc, show ((2^52:ℕ):ℝ)*2^(((i%256:ℕ):ℤ)-202) =2^(((i%256:ℕ):ℤ)-150) by rw[Nat.cast_pow,Nat.cast_ofNat,<-к,<-а Р] congr 1 push_cast ring] simp only[decide_eq_true_eq,c,↓reduceIte] lemma qC4 (i:ℕ) (a:i<512):∃ σ:ℤ,σ.natAbs<=1 ∧ R (pwEntry i)=σ*2^(((i%256:ℕ):ℤ)-150):=by rw[(qDa i a).2];split_ifs;exacts [⟨0,by norm_num,by simp⟩,⟨-1,by norm_num,by simp⟩,⟨1,by norm_num,by simp⟩] lemma qCt (f:Float) (i:ℕ) (g:i<512) (q:fᶠ) (c k:ℤ) (r:c.natAbs<2^53) (s:-900<=k) (m : |R f| ≤ A_B) (l:R f=c*2^k) : (f*pwEntry i)ᶠ∧R (f*pwEntry i)=R f*R (pwEntry i):=by obtain ⟨b,a,d⟩:=qC4 i g;have o:((i%256:ℕ):ℤ)-150<=128∧-150<=((i%256:ℕ):ℤ)-150 :=א;generalize ((i%256:ℕ):ℤ)-150=E at o d;have p : |(b:ℝ)| ≤ 1:=by rw[<-Int.cast_abs,<-Nat.cast_natAbs];exact_mod_cast a have e:=ы (two_pos (α:=ℝ)) E;have h : |R (pwEntry i)| ≤ A_B:=by rw[d,е,г e,<-one_mul A_B];exact С p (Ш one_le_two א) e.le zero_le_one obtain ⟨j,n⟩:=qC7 f _ q (qDa i g).1 ((С m h (Й _) qKo.le).trans qCb);refine ⟨j,?_⟩ have:=qAu (c*b) (k+E) (by rw[Int.natAbs_mul];have:=Nat.mul_le_mul_left c.natAbs a omega) א rw[а Р] at this;push_cast at this;rw[n,d,l,show (c:ℝ)*2^k*(b*2^E)=c*b*(2^k*2^E) by ring];exact this lemma qF2 (i:USize) (h:i.toNat< pw.size):pw.uget i h=pwEntry i.toNat:=by simp[FloatArray.uget,qD1] lemma qAt (w:UInt32):(w &&& В).toNat=w.toNat%2^31:=by rw[ز];exact О w.toNat 31 lemma qBj (w:UInt32):(w &&& 0x7FFFFF).toNat=w.toNat%2^23:=by rw[ز];exact О w.toNat 23 lemma qGs (w:UInt32):(w >>> 23).toNat=w.toNat/2^23:=by rw[UInt32.toNat_shiftRight,т];rfl lemma qA0 (w:UInt32):(expOf w).toNat=w.toNat/2^23%2^8:=by unfold expOf;rw[ز,qGs];exact О _ 8 lemma qDl (w:UInt32):signOf w=decide (2^31<=w.toNat):=by have b:=w.toNat_lt;unfold signOf;have h:((w &&& х).toNat)=(w.toNat/2^31%2)*2^31:=by rw[ز];show w.toNat &&& 2^31=_;rw[Nat.and_two_pow,Nat.testBit_eq_decide_div_mod_eq];have a:w.toNat/2^31%2=0∨w.toNat/2^31%2=1 :=א;rcases a with a | a<;>rw[a]<;>rfl rw[Bool.eq_iff_iff];simp only[bne_iff_ne,ne_eq,decide_eq_true_eq];rw[<-Щ,h];simp only[UInt32.toNat_zero];omega lemma qIx (w:UInt32) : ((w &&& 0x7FFFFF) ||| 0x800000).toNat=2^23+w.toNat%2^23:=by rw[UInt32.toNat_or,qBj,Nat.or_comm];simpa using (Nat.two_pow_add_eq_or_of_lt (Nat.mod_lt w.toNat (by positivity:2^23 > 0)) 1).symm lemma qA3 (w:UInt32) (h:good w=true) : w.toNat%2^31=0∨(2^23<=w.toNat%2^31∧w.toNat%2^31<Ц):=by have a:=qAt w;have b:=(w &&& В).toNat_lt;unfold good at h;simp only[Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h;rcases h with h | h;. left;rw[<-a,h];rfl . right;rw[М,UInt32.toNat_sub] at h;rw[<-a];generalize (w &&& В).toNat=v at *;simp at h;omega lemma qAd (w:UInt32) (h:good w=true):expOf w≠255:=by intro he;have a:=qA0 w;rw[he] at a;change 255=_ at a;have:=qA3 w h;have:=w.toNat_lt;omega noncomputable def A_mag (v:ℕ):ℝ := if v=0 then 0 else ((2^23+v%2^23:ℕ):ℝ)*2^(((v/2^23:ℕ):ℤ)-150) lemma qAv (v:ℕ):0<=A_mag v:=by unfold A_mag;split_ifs<;>positivity lemma qAz (w:UInt32) (hg:good w=true) : wv w=(if 2^31<=w.toNat then -1 else 1)*A_mag (w.toNat%2^31):=by have g:=w.toNat_lt;have:=qA3 w hg;have a:=qA0 w;have d:(fracOf w).toNat=w.toNat%2^23:=qBj w;have f:wv w=(decodeD w).toReal:=by unfold wv ofW;rw[Model.toReal_eq,Ы w (qAd w hg)] rw[f];unfold decodeD A_mag;by_cases c:w.toNat%2^31=0;. have b:expOf w=0:=Щ.mp (by rw[a];show _=0;omega);have e:fracOf w=0:=Щ.mp (by rw[d];show _=0;omega);simp only[b,e,c,↓reduceIte];simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] . have b:expOf w≠0:=λh=>by rw[h] at a;change 0=_ at a;omega simp only[b,c,↓reduceIte];rw[show w.toNat%2^31%2^23=(fracOf w).toNat by rw[d];omega,show w.toNat%2^31/2^23=(expOf w).toNat by rw[a];omega];simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,qDl];simp;split_ifs<;>ring lemma qGf (v:ℕ) (h:v<Ц):A_mag v<=2^128:=by unfold A_mag;split_ifs with h0;. positivity . exact (С (show ((2^23+v%2^23:ℕ):ℝ)<=((2^24:ℕ):ℝ) from Nat.cast_le.2 א) (Ш one_le_two (by omega:_<=(104:ℤ))) (by positivity) (by positivity)).trans_eq ב lemma qA7 (a b:ℕ) (hab:a<=b) (ha:a=0∨2^23<=a):A_mag a<=A_mag b:=by rcases ha with rfl | ha;. simpa [A_mag] using qAv b unfold A_mag;simp only[show ¬ a=0 by omega,show ¬ b=0 by omega,↓reduceIte];have c:a/2^23<=b/2^23:=Nat.div_le_div_right hab;rcases c.eq_or_lt with d | g;. rw[d];exact Б (Nat.cast_le.2 א) (by positivity) . have e:(2:ℝ)^(((a/2^23:ℕ):ℤ)-150)*2<=2^(((b/2^23:ℕ):ℤ)-150):=by rw[<-zpow_add_one₀ Р];exact Ш one_le_two א have f:((2^23:ℕ):ℝ)<=((2^23+b%2^23:ℕ):ℝ):=Nat.cast_le.2 א calc _<=((2^23*2:ℕ):ℝ)*2^(((a/2^23:ℕ):ℤ)-150) := Б (Nat.cast_le.2 א) (by positivity) _=((2^23:ℕ):ℝ)*(2^(((a/2^23:ℕ):ℤ)-150)*2):=by push_cast;ring _<=_:=С f e (by positivity) (by positivity) abbrev A_mf (w:UInt32):Float:=((w &&& 0x7FFFFF) ||| 0x800000).toFloat lemma qEe (w:UInt32):pw.uget (w >>> 23).toUSize (qDo w) =pwEntry (w.toNat/2^23)∧w.toNat/2^23<512∧(A_mf w)ᶠ ∧ (A_mf w*pwEntry (w.toNat/2^23))ᶠ ∧ R (A_mf w*pwEntry (w.toNat/2^23))=R (A_mf w)*R (pwEntry (w.toNat/2^23)) ∧ R (A_mf w)=((2^23+w.toNat%2^23:ℕ):ℝ):=by have g:=w.toNat_lt;have c:=qIx w;obtain ⟨d,a⟩:=qCw (A_mf w) _ (by rw[c];omega) rfl;rw[c] at a;have b:w.toNat/2^23<512 :=א obtain ⟨f,e⟩:=qCt _ _ b d ((2^23+w.toNat%2^23:ℕ):ℤ) 0 א ב (by rw[a,Ж (Ю _)] exact qGl א) (by rw[a,zpow_zero,mul_one,Int.cast_natCast]) refine ⟨?_,b,d,f,e,a⟩;rw[qF2,н,qGs] lemma qAl (w:UInt32) (hg:good w=true):(dv w)ᶠ∧R (dv w)=wv w:=by obtain ⟨g,a,-,e,d,c⟩:=qEe w;have j:=w.toNat_lt;unfold dv;rw[g];refine ⟨e,?_⟩;rw[d,c,(qDa _ a).2,qAz w hg];have b:=qA3 w hg;generalize w.toNat=v at *;rcases b with h | ⟨l,k⟩;. rw[h];simp only[show v/2^23%256=0 by omega,↓reduceIte,mul_zero,show A_mag 0=0 by simp[A_mag]] . have f:(256<=v/2^23) ↔ 2^31<=v :=א;unfold A_mag;rw[(א:v%2^31/2^23=v/2^23%256),(א:v%2^31%2^23=v%2^23),if_neg (א:¬ v/2^23%256=0),if_neg (א:¬ v%2^31=0)];by_cases i:2^31<=v<;>simp only[f,i,↓reduceIte]<;>ring def A_fsum (l:List Float):Float:=l.foldl (.+·) 0 noncomputable def A_sa (l:List Float):ℝ:=(l.map λq=>|R q|).sum noncomputable def A_sr (l:List Float):ℝ:=(l.map R).sum lemma qDb {a:Type} (l:List a) (f:a->ℝ):0<=(l.map λq=>|f q|).sum :=List.sum_nonneg (by simp only[List.mem_map];rintro _ ⟨q,_,rfl⟩;exact Й _) lemma qC9 (l:List Float):0<=A_sa l:=qDb l R lemma qI4 (l:List Float) (q:Float):A_sa (l ++ [q])=A_sa l + |R q| ∧ A_sr (l ++ [q])=A_sr l+R q∧A_fsum (l ++ [q])=A_fsum l+q:=by simp[A_sa,A_sr,A_fsum] lemma qDd (l:List Float) : |A_sr l| ≤ A_sa l:=by induction l with | nil=>simp[A_sa,A_sr] | cons q l ih => simp only[A_sa,A_sr,List.map_cons,List.sum_cons] at ih ⊢;exact (Ь _ _).trans ד lemma qBx:A_z<=1:=qAx.trans ב lemma qBf (l:List Float) (hf:∀ q∈l,qᶠ) (hlen:l.length<=2^21) (hb:A_sa l<=A_B/4) : (A_fsum l)ᶠ ∧ |R (A_fsum l)-A_sr l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by induction l using List.reverseRecOn with | nil=>exact ⟨qI0.1,by simp[A_fsum,qI0.2,A_sr]⟩ | append_singleton l q ih => obtain ⟨e,f,n⟩:=qI4 l q rw[e] at hb rw[List.length_append,List.length_singleton] at hlen have t:=qC9 l have j:=Й (R q) obtain ⟨h,v⟩:=ih (λx i=>hf x (by simp[i])) א ד have u:(l.length:ℝ)+1<=2^21:=by exact_mod_cast hlen have d:(0:ℝ)<=l.length:=Ю _ have y:=qDd l have o:=qAs have m:=qBx have p:=qAh have s:=abs_sub_abs_le_abs_sub (R (A_fsum l)) (A_sr l) have w:=Б (show (l.length:ℝ)<=2^21 by linarith) (ד:0<=A_sa l+A_z) obtain ⟨b,z⟩:=qBa _ q h (hf q ג) ד ד rw[n,e,f,List.length_append,List.length_singleton] refine ⟨b,?_⟩ push_cast generalize R (A_fsum l)=r at * generalize R q=a at * generalize A_sa l=P at * generalize A_sr l=S at * generalize (l.length:ℝ)=k at * obtain ⟨W,g⟩:∃ W:ℝ,W=1/2^53*(P + |a| + A_z):=⟨_,rfl⟩ have c:0<=W :=ד rw[<-g] refine (abs_sub_le _ (r+a) (S+a)).trans ?_ rw[add_sub_add_right_eq_sub] linarith [Ь r a,Г (show 1/2^53*(P+A_z)<=W by linarith) d,Б (ד:k<=2^21) c] lemma qCj (l:List Float) (c:∀ q∈l,qᶠ) (d:l.length<=2^21) (f:A_sa l<=A_B/4) : (A_fsum (l.map Float.abs))ᶠ ∧ |R (A_fsum (l.map Float.abs))-A_sa l| ≤ 2*l.length*(1/2^53*(A_sa l+A_z)):=by have a:A_sa (l.map Float.abs)=A_sa l∧A_sr (l.map Float.abs)=A_sa l:=by constructor<;>simp only[A_sa,A_sr,List.map_map]<;>congr 1 <;>exact Զ λq b=>by simp[(qBk q (c q b)).2] have:=qBf (l.map Float.abs) (λq b=>by obtain ⟨x,e,rfl⟩:=List.mem_map.mp b exact (qBk x (c x e)).1) (by simpa) (a.1.symm ▸ f) rwa [a.1,a.2,List.length_map] at this lemma qKa (n:ℕ) (g:n<2^20) : (nfOf n)ᶠ∧R (nfOf n)=((n:ℝ)+8)/2^51:=by have b:(n.toUInt64+8).toNat=n+8:=by have c:(8:UInt64).toNat=8:=rfl;simp[UInt64.toNat_add,c];omega obtain ⟨f,a⟩:=qB0 (n.toUInt64+8) (by rw[b];omega);rw[b] at a obtain ⟨e,d⟩:=qCt _ 99 ב f ((n+8:ℕ):ℤ) 0 א ב (by rw[a,Ж (Ю _)] exact qGl א) (by rw[a];simp) push_cast at a;rw[show nfOf n=(n.toUInt64+8).toFloat*pwEntry 99 by rw[nfOf,qC1];rfl];refine ⟨e,?_⟩;rw[d,a,(qDa 99 ב).2];norm_num;ring lemma qHo {n:ℕ} (l:n<2^20) {s t:Float} (k:sᶠ) (m:tᶠ) {X Y:ℝ} (e:0<=Y) (v:Y<=A_B/8) (y : |R s-X| ≤ 4*((n:ℝ)+1)*(1/2^53*Y)) (u : |R t-Y| ≤ Y/2^30+1/2^250) (I : |R s| ≤ 2*Y) (c:(2:ℝ)^(-127:ℤ) ≤ |R (s+nfOf n*t)|) : R (s-nfOf n*t)< X∧X< R (s+nfOf n*t):=by have w:(8:ℝ)<=A_B:=(by norm_num:(8:ℝ)<=2^128).trans qAh;have A:(n:ℝ)+1<=2^20:=by exact_mod_cast l have o:(0:ℝ)<=n:=Ю n;obtain ⟨h,b⟩:=qKa n l;have E:=qAs;have B:=qAx;have a:0<=((n:ℝ)+8)/2^51:=by positivity have C:=abs_sub_abs_le_abs_sub (R t) Y;rw[Ж e] at C;have i : |R t| ≤ 2*Y+1/2^250 :=ד;have x : |R (nfOf n)*R t| ≤ 1/2^30*(2*Y+1/2^250):=by rw[е,b,Ж a];exact С ד i (Й _) ב obtain ⟨d,f⟩:=qBw _ _ h m (by rw[b,Ж a];linarith) ד;have D:=abs_sub_abs_le_abs_sub (R (nfOf n*t)) (R (nfOf n)*R t);obtain ⟨z,j⟩:=qBa s _ k d ד ד;obtain ⟨r,g⟩:=qDc s _ k d ד ד;have H:=abs_sub_abs_le_abs_sub (R (s+nfOf n*t)) (R s+R (nfOf n*t));have q:=Ь (R s) (R (nfOf n*t));have p:=Ե (R s) (R (nfOf n*t));rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at c;have G:=Г (abs_le.mp u).1 a;have M:=Б (show (n:ℝ)<=2^20 by linarith) e;rw[b] at f x D;obtain ⟨K,J⟩:=abs_le.mp y;obtain ⟨O,N⟩:=abs_le.mp j;obtain ⟨F,L⟩:=abs_le.mp g;obtain ⟨P,-⟩:=abs_le.mp f;constructor<;>linarith lemma qIl (g h:ℕ->Float) (l:List ℕ) (a b:Float) : l.foldl (λ(d:F1) t=>F1.mk (d.s+g t) (d.t+h t)) ⟨a,b⟩ =⟨l.foldl (λ(c:Float) t=>c+g t) a,l.foldl (λ(c:Float) t=>c+h t) b⟩:=by induction l generalizing a b with | nil=>rfl | cons x l ih=>simp only[List.foldl_cons,ih] lemma qB7 (a:List ℕ) (f:ℕ->ℝ) (M:ℝ) (h:∀ t∈a,f t<=M) : (a.map f).sum<=a.length*M:=by simpa using List.sum_le_sum h lemma qCa (w:UInt32) (hg:good w=true) : |wv w| = A_mag (w.toNat%2^31):=by rw[qAz w hg,е,Ж (qAv _)];split_ifs<;>simp lemma qAi (w:UInt32) (hg:good w=true) : |wv w| ≤ 2^128:=by rw[qCa w hg];apply qGf;rcases qA3 w hg with h | h<;>omega lemma qBc (w:UInt32) (hg:good w=true) (h:w &&& В=0):wv w=0:=by rw[qAz w hg,<-qAt w,h];simp[A_mag] lemma qFn (a b:UInt32) (ha:good a=true) (hb:good b=true) : (dv a*dv b)ᶠ ∧ |R (dv a*dv b)-wv a*wv b| ≤ 1/2^53*(|wv a*wv b| + A_z) ∧ |wv a*wv b| ≤ 2^256:=by obtain ⟨j,g⟩:=qAl a ha obtain ⟨i,d⟩:=qAl b hb have c:=qAi a ha have e:=qAi b hb obtain ⟨h,f⟩:=qBw _ _ j i (g ▸ c.trans qAh) (d ▸ e.trans qAh) rw[g,d] at f exact ⟨h,f,by rw[е] exact (С c e (Й _) (by positivity)).trans_eq ב⟩ lemma qBt {n:ℕ} {a b:ℕ->UInt32} {f:UInt32} (g:∀ t,t< n->good (a t)=true) (j:∀ t,t< n->good (b t)=true) {l:List ℕ} (k:l.Nodup) (d:∀ t∈l,t< n) (i:∀ t,t< n->t∉l->a t &&& В=0∨b t &&& В=0) : exactS n a b f=(l.map λt=>wv (a t)*wv (b t)).sum+wv f:=by unfold exactS;congr 1;rw[<-List.sum_toFinset _ k];refine (Finset.sum_subset (λt c=>by simpa using d t (by simpa using c)) λt c h=>?_).symm;have e:=ؽ.mp c;rcases i t e (by simpa using h) with h | h;. simp[qBc (a t) (g t e) h] . simp[qBc (b t) (j t e) h] lemma qEo {n:ℕ} {l:List ℕ} (b:l.Nodup) (a:∀ t∈l,t< n):l.length<=n:=by simpa using (b.subperm λt h=>List.mem_range.2 (a t h)).length_le abbrev A_G (ts:List ℕ) (g:ℕ->Float) (p:ℕ->ℝ):Prop := ∀ t∈ts,(g t)ᶠ∧|R (g t)-p t| ≤ 1/2^53*(|p t| + A_z)∧|p t| ≤ 2^256 lemma qGe {b c:ℕ->UInt32} {l:List ℕ} {n:ℕ} (a:∀ t∈l,t< n) (e:∀ t,t< n->good (b t)=true) (f:∀ t,t< n->good (c t)=true) : A_G l (λt=>dv (b t)*dv (c t)) λt=>wv (b t)*wv (c t) :=λt d=>qFn _ _ (e t (a t d)) (f t (a t d)) lemma qCs {a:List ℕ} {g:ℕ->Float} {p:ℕ->ℝ} (b:Float) (c:A_G a g p) : let A:=(a.map λt=>|p t|).sum let L:=a.map g ++ [b] |A_sr L-((a.map p).sum+R b)| ≤ 1/2^53*(A+a.length*A_z) ∧ A_sa L<=A + |R b| + 1/2^53*(A+a.length*A_z)∧A<=2*A_sa L+a.length*A_z:=by induction a with | nil=>simp[A_sa,A_sr] | cons x a ih => obtain ⟨d,i,j⟩:=ih (λt e=>c t (by simp[e])) have f:=(c x ג).2.1 simp only[A_sa,A_sr,List.map_cons,List.sum_cons,List.length_cons,List.cons_append] at d i j ⊢ have m:=qAs obtain ⟨o,r⟩:=abs_le.mp f obtain ⟨n,q⟩:=abs_le.mp d have k:=Й (p x) have h:=abs_sub_abs_le_abs_sub (p x) (R (g x)) have l:=abs_sub_abs_le_abs_sub (R (g x)) (p x) rw[и] at h refine ⟨abs_le.mpr ⟨?_,?_⟩,?_,?_⟩<;>push_cast<;>linarith lemma qH2 (l:List ℕ) (g:ℕ->Float) (b:Float) : ((l.map g ++ [b]).length:ℝ)=l.length+1:=by simp lemma qHx {a:List ℕ} {g:ℕ->Float} {p:ℕ->ℝ} {b:Float} (d:A_G a g p) (c:bᶠ):∀ q∈a.map g ++ [b],qᶠ:=by simp only[List.mem_append,List.mem_map,List.mem_singleton];rintro q (⟨t,e,rfl⟩ | rfl);. exact (d t e).1 . exact c lemma qB1 {a:UInt32} (d:good a=true) (h:a<=В) : 0<=wv a∧wv a<=2^128 ∧ ∀ w,good w=true->w &&& В<=a->|wv w| ≤ wv a:=by have g:a.toNat<=2147483647:=М.mp h have b:wv a=A_mag a.toNat:=by rw[qAz a d,З א] simp only[show ¬ 2^31<=a.toNat by omega,↓reduceIte,one_mul] refine ⟨b ▸ qAv _,(le_abs_self _).trans (qAi a d),λw c f=>?_⟩ have e:=М.mp f rw[qAt] at e rw[qCa _ c,b] exact qA7 _ _ e ((qA3 _ c).imp_right (·.1)) section variable {n:ℕ} {ts:List ℕ} {g:ℕ->Float} {p:ℕ->ℝ} {bw hh:UInt32} {X Y J:ℝ} {s t a e:Float} (A:n<2^20) (B:ts.length<=n) (C:A_G ts g p) (D:good bw=true) (E:X=(ts.map p).sum+wv bw) (F:s=ts.foldl (λ(c:Float) t=>c+g t) 0+dv bw) (G:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) include A B C D E F G lemma qFv (k:tᶠ) (b:Y<=A_B/8) (o:A_sa (ts.map g ++ [dv bw])+A_z<=3/2*Y) (u:2*((ts.length:ℝ)+1)*(A_sa (ts.map g ++ [dv bw])+A_z) + ((ts.map λt=>|p t|).sum+ts.length*A_z)<=4*((ts.length:ℝ)+1)*Y) (m : |R t-Y| ≤ Y/2^30+1/2^250) (h:e=nfOf n*t) : R (s-e)< X∧X< R (s+e):=by subst h obtain rfl:s=A_fsum (ts.map g ++ [dv bw]):=by simp[F,A_fsum,List.foldl_map] obtain ⟨c,d⟩:=qAl bw D obtain ⟨j,-,-⟩:=qCs (dv bw) C rw[d,<-E] at j have i:=qC9 (ts.map g ++ [dv bw]) have r:=qAs have l:(ts.length:ℝ)<=n:=by exact_mod_cast B have q:(n:ℝ)+1<=2^20:=by exact_mod_cast A have a:0<=Y :=ד obtain ⟨f,h⟩:=qBf _ (qHx C c) (by simp;omega) ד rw[qH2] at h generalize ts.map g ++ [dv bw]=L at * have x:=qDd L have y:=Б l a have v:=С (show (ts.length:ℝ)+1<=2^20 by linarith) o ד ב have w:=abs_sub_le (R (A_fsum L)) (A_sr L) X have z:=abs_sub_abs_le_abs_sub (R (A_fsum L)) (A_sr L) exact qHo A f k a b ד m ד G lemma qBu (P:good hh=true) (hp:hh<=В) (U:(ts.map λt=>|p t|).sum<=wv hh*J) (m:aᶠ) (d:0<=J) (z:J<=2^149) (v : |R a-J| ≤ (J+A_z)/2^32) (he:e=nfOf n*(dv hh*a+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by obtain ⟨b,h,-⟩:=qB1 P hp;obtain ⟨Q,N⟩:=qAl hh P;obtain ⟨q,j⟩:=qAl bw D;obtain ⟨-,K,-⟩:=qCs (dv bw) C;rw[j] at K;have S:=qAi bw D;generalize wv hh=H at *;have hk:(ts.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:ts.length+1<=2^20) have L:(0:ℝ)<=ts.length:=Ю _;have M:=qAs;have x:=qAx;have r:=qDb ts p;have hBl:=qBv;have T:=qAh;have u:=abs_sub_abs_le_abs_sub (R a) J;rw[Ж d] at u;obtain ⟨e1,W⟩:=abs_le.mp v;have e3:=Г e1 b;have e4:=Г W b;have e5:=С h x M.le ב;have e6 : |H*R a| ≤ H*(J+(J+A_z)/2^32):=by rw[е,Ж b];exact Г ד b have hW:=Н b d;have hHJ:H*J<=2^128*2^149:=С h z d ב;obtain ⟨o,i⟩:=qBw _ a Q m (by rw[N,Ж b];linarith) ד;rw[N] at i;have e7:=abs_sub_abs_le_abs_sub (R (dv hh*a)) (H*R a);obtain ⟨w,f⟩:=qBk (dv bw) q;rw[j] at f;obtain ⟨y,k⟩:=qBa _ _ o w ד (by rw[f,abs_abs];linarith);rw[f] at k;have e8:=Ь (R (dv hh*a)) |wv bw|;rw[abs_abs] at e8;obtain ⟨e9,Z⟩:=abs_le.mp i;obtain ⟨e11,V⟩:=abs_le.mp k;have hkz:=Б (show (ts.length:ℝ)<=2^20 by linarith) M.le;have O:=Й (wv bw);obtain ⟨Y,hY⟩:∃ Y,Y=H*J + |wv bw| + A_z:=⟨_,rfl⟩;have c:A_sa (ts.map g ++ [dv bw])+A_z<=3/2*Y :=ד;have I:(ts.map λt=>|p t|).sum+A_z<=Y :=ד;have l : |R (dv hh*a+(dv bw).abs)-Y| ≤ Y/2^30+1/2^250 :=abs_le.mpr ⟨by linarith,by linarith⟩;refine qFv A B C D E F G y ד c ?_ l he;have h3:=Г c (show 0<=(ts.length:ℝ)+1 by linarith);have h4:=Н L r;have h5:=Г I (show 0<=(ts.length:ℝ)+1 by linarith);linarith lemma qC6 {a:ℕ->UInt32} (u:good hh=true) (w:hh<=В) (i:∀ t∈ts,good (a t)=true) (o:∀ t∈ts,|p t| ≤ wv hh * |wv (a t)|) (r:e=nfOf n*(dv hh*ts.foldl (λ(c:Float) t=>c+(dv (a t)).abs) 0+(dv bw).abs)) : R (s-e)< X∧X< R (s+e):=by have f:(ts.length:ℝ)<=2^20:=by exact_mod_cast (by omega:ts.length<=2^20) have k:A_sa (ts.map λt=>dv (a t))=(ts.map λt=>|wv (a t)|).sum:=by simp only[A_sa,List.map_map];congr 1;exact Զ λt d=>by simp[(qAl _ (i t d)).2] have h:=(qB7 ts _ _ λt d=>qAi _ (i t d)).trans (Б f ב);rw[<-k] at h;have m:=qBv obtain ⟨j,b⟩:=qCj _ (λq v=>by obtain ⟨t,d,rfl⟩:=List.mem_map.mp v exact (qAl _ (i t d)).1) (by simp;omega) ד rw[List.length_map] at b;have g:=qC9 (ts.map λt=>dv (a t));have l:=qAs;refine qBu A B C D E F G u w ?_ j g (h.trans ב) ?_ (r.trans (by simp[A_fsum,List.foldl_map]));. rw[k,<-List.sum_map_mul_left];exact List.sum_le_sum o . refine b.trans ?_;have:=Б f (add_nonneg g l.le);linarith end lemma qB3 (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by simp only[qIl] at hs obtain ⟨hs,E⟩:=hs have a:=qGe hl2 hx hw have f:=qEo hl hl2 obtain ⟨h,i⟩:=qAl bw hb obtain ⟨-,m,y⟩:=qCs (dv bw) a rw[i] at m have b:=qC9 (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw]) have k:=qB7 l _ _ λt u=>(a t u).2.2 have v:(l.length:ℝ)+1<=2^20:=by exact_mod_cast (by omega:l.length+1<=2^20) have o:(0:ℝ)<=l.length:=Ю _ have d:=qAs have D:=qAx have w:=qAi bw hb have z:=qBv have A:=qDb l λt=>wv (xw t)*wv (ww t) have p:=k.trans (Б (show (l.length:ℝ)<=2^20 by linarith) ב) have C:=Б (show (l.length:ℝ)<=2^20 by linarith) d.le have g:A_sa (l.map (λt=>dv (xw t)*dv (ww t)) ++ [dv bw])+A_z<=A_B/8 :=ד obtain ⟨j,c⟩:=qCj _ (qHx a h) (by simp;omega) ד rw[qH2] at c have B:=Н o b have x:=Б v (add_nonneg b d.le) obtain ⟨G,F⟩:=abs_le.mp c exact qFv hn f a hb (qBt hx hw hl hl2 hz) hs hbig j g ד ד (abs_le.mpr ⟨by linarith,by linarith⟩) (E.trans (by simp[A_fsum,List.foldl_map])) lemma qEh (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (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) := qB3 n hn xw ww bw hx hw hb l hl hl2 (λt b a=>.inl (hz t b a)) s e hs hbig lemma qDv (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh hv: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) (hgv:good hv=true) (hm:∀ t,t< n->xw t &&& В<=hh) (hp:hh<=В) (hmv:∀ t,t< n->ww t &&& В<=hv) (hpv:hv<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*((n.toUInt64+1).toFloat*dv hv)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by obtain ⟨hs,B⟩:=hs obtain ⟨g,-,r⟩:=qB1 hg hp obtain ⟨a,u,j⟩:=qB1 hgv hpv obtain ⟨q,f⟩:=qAl hv hgv have w:(n.toUInt64+1).toNat=n+1:=by have i:(1:UInt64).toNat=1:=rfl simp[UInt64.toNat_add,i] omega obtain ⟨k,b⟩:=qB0 (n.toUInt64+1) (by rw[w];omega) rw[w] at b push_cast at b have m:(n:ℝ)+1<=2^20:=by exact_mod_cast hn have x:(0:ℝ)<=n:=Ю n have A:=qAs have v:=qAh obtain ⟨o,c⟩:=qBw _ _ k q (by rw[b,Ж ד];linarith) (by rw[f,Ж a];linarith) rw[b,f] at c have d:=Н (show 0<=(n:ℝ)+1 by linarith) a rw[Ж d] at c have p:=qEo hl hl2 have y:(l.length:ℝ)<=(n:ℝ)+1:=by exact_mod_cast (by omega:l.length<=n+1) refine qBu hn p (qGe hl2 hx hw) hb (qBt hx hw hl hl2 λt i z=>.inl (hz t i z)) hs hbig hg hp ?_ o d ((С m u a ב).trans ב) (c.trans ד) B refine (qB7 l _ (wv hh*wv hv) λt h=>?_).trans ((Б y (Н g a)).trans_eq (by ring)) rw[е] exact С (r _ (hx t (hl2 t h)) (hm t (hl2 t h))) (j _ (hw t (hl2 t h)) (hmv t (hl2 t h))) (Й _) g lemma qDg (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& В<=hh) (hp:hh<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:Float) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (hbig:(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|) : R (s-e)< exactS n xw ww bw∧exactS n xw ww bw< R (s+e):=by obtain ⟨hs,b⟩:=hs obtain ⟨-,-,c⟩:=qB1 hg hp exact qC6 hn (qEo hl hl2) (qGe hl2 hx hw) hb (qBt hx hw hl hl2 hz) hs hbig hg hp (λt a=>hw t (hl2 t a)) (λt a=>by rw[е] exact Б (c _ (hx t (hl2 t a)) (hm t (hl2 t a))) (Й _)) b end section local notation:max x:max "ᶠ"=>Float.isFinite x=true noncomputable def A_r (b:Bool):ℝ:=if b then -1 else 1 lemma qHd (s t:Bool):A_r (s ^^ t)=A_r s*A_r t:=by cases s<;>cases t<;>simp[A_r] lemma qHb (S T:Bool) (X Y:ℝ) (a:0< X) (b:0< Y) (h:A_r S*X=A_r T*Y) : S=T:=by cases S<;>cases T<;>simp[A_r] at h ⊢<;>linarith lemma qEc (d:FloatLib.Numerics.Dyadic) : d.toReal=A_r d.negative*((d.significand:ℝ)*2^d.exponent):=by obtain ⟨n,m,e⟩:=d;cases n<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,A_r] lemma A_s (d:FloatLib.Numerics.Dyadic):d.toReal=0 ↔ d.significand=0:=by have h:(2:ℝ)^d.exponent≠0:=zpow_ne_zero _ Р;rw[qEc];cases d.negative<;>simp[A_r,h] lemma qF8 (d:FloatLib.Numerics.Dyadic) (h:d.significand≠0) : 0<(d.significand:ℝ)*2^d.exponent :=mul_pos (Nat.cast_pos.mpr (ص h)) (ы two_pos _) lemma qJj (a b:Float) (h:A_M a=A_M b):a=b:=by have:=Model.ofFloatModel_injective h;cases a;cases b;simp_all lemma qDr (f:Float) (h:fᶠ) : ∃ d,Model.toDyadic? (A_M f)=some d∧d.toReal=R f:=by obtain ⟨d,a⟩:=Model.exists_toDyadic?_of_isFinite ((qAk f).trans h);exact ⟨d,a,by rw[A_R,Model.toReal_eq,a]⟩ lemma qCu {a b:Float} {x y:FloatLib.Numerics.Dyadic} (f:Model.toDyadic? (A_M a)=some x) (d:Model.toDyadic? (A_M b)=some y) (e:aᶠ) (c:bᶠ) : A_M (a*b)=if x.significand*y.significand=0 then Model.zero _ (x.negative ^^ y.negative) else Ч _ ⟨x.negative ^^ y.negative,x.significand*y.significand,x.exponent+y.exponent⟩:=by rw[qB5 a b e c,Model.Proof.mul_eq_spec,Model.Spec.mul,f,d] simp lemma qHy {a b:Float} {x y z:FloatLib.Numerics.Dyadic} (m:Model.toDyadic? (A_M a)=some x) (n:Model.toDyadic? (A_M b)=some y) (e:Model.toDyadic? (A_M (a*b))=some z) (k:aᶠ) (l:bᶠ) (c:z.toReal=x.toReal*y.toReal):z.negative=(x.negative ^^ y.negative):=by by_cases g:x.significand*y.significand=0;. have d:=qCu m n k l;simp only[g,↓reduceIte] at d;rw[d,Model.toDyadic?_zero] at e;cases e;show Model.signBit _=_;cases x.negative<;>cases y.negative<;>simp[Model.zero] . obtain ⟨f,i⟩:=Nat.mul_ne_zero_iff.mp g;have j:z.significand≠0:=λh=>by rw[(A_s z).2 h] at c;exact mul_ne_zero (mt (A_s x).1 f) (mt (A_s y).1 i) c.symm rw[qEc z,qEc x,qEc y,mul_mul_mul_comm,<-qHd] at c;exact qHb _ _ _ _ (qF8 z j) (mul_pos (qF8 x f) (qF8 y i)) c lemma qG8 (v m p:Float) (f:vᶠ) (i:mᶠ) (h:pᶠ) (u:(v*p)ᶠ) (s:(m*p)ᶠ) (q:R (v*p)=R v*R p) (r:R (m*p)=R m*R p):v*p*m=v*(m*p):=by obtain ⟨a,o,n⟩:=qDr v f;obtain ⟨b,y,k⟩:=qDr p h;obtain ⟨c,w,A⟩:=qDr m i;obtain ⟨d,t,B⟩:=qDr _ u;obtain ⟨e,l,x⟩:=qDr _ s;have D:=qHy o y t f h (by rw[B,n,k,q]);have E:=qHy w y l i h (by rw[x,A,k,r]);have C:(d.negative ^^ c.negative)=(a.negative ^^ e.negative):=by rw[D,E];cases a.negative<;>cases b.negative<;>cases c.negative<;>rfl have j:d.toReal*c.toReal=a.toReal*e.toReal:=by rw[B,A,n,x,q,r];ring have g:d.significand*c.significand=0 ↔ a.significand*e.significand=0:=by simp only[Nat.mul_eq_zero,<-A_s,<-mul_eq_zero,j] apply qJj;rw[qCu t w u i,qCu o l f s];by_cases z:d.significand*c.significand=0;. simp only[z,g.1 z,↓reduceIte,C] . simp only[z,mt g.2 z,↓reduceIte];refine ExactRuntime.roundDyadic_congr _ qAm _ _ z (mt g.2 z) (Rat.cast_injective (α:=ℝ) ?_);rw[д,д,Model.Dyadic.toReal_mul,Model.Dyadic.toReal_mul,j] lemma qJf (x w:UInt32) (hx:good x=true) : dvB (dv x) w=dv x*dv w:=by obtain ⟨e,o⟩:=qAl x hx;obtain ⟨p,f,j,q,g,-⟩:=qEe w;obtain ⟨d,a,-,-,l,b⟩:=qEe x;obtain ⟨m,c,k⟩:=qC4 _ a;have r:=x.toNat_lt;have h:R (dv x)=(((2^23+x.toNat%2^23:ℕ)*m:ℤ):ℝ) * 2^(((x.toNat/2^23%256:ℕ):ℤ)-150):=by unfold dv;rw[d,l,b,k,Int.cast_mul,Int.cast_natCast,mul_assoc] obtain ⟨n,i⟩:=qCt (dv x) _ f e _ _ (by rw[Int.natAbs_mul,Int.natAbs_natCast];have:=Nat.mul_le_mul_left (2^23+x.toNat%2^23) c omega) א (by rw[o];exact (qAi x hx).trans qAh) h show dv x*pw.uget _ _*_=dv x*(_*pw.uget _ _);rw[p];exact qG8 _ _ _ e j (qDa _ f).1 n q i g end section section open Float.Model Float.Model.UnpackedFloat lemma qDn (H a b:Nat) (hb:b< H):(H*a+b)/H=a∧(H*a+b)%H=b:=by have c:0< H :=א;constructor;. rw[ـ c,ш hb,у] . rw[ف,З hb] lemma qG6 (t:Int):Format.binary32.targetExponent t=max (t-24) (-149):=by simp[Format.targetExponent,Format.mantissaBits,Format.minExponent] lemma B_z (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,а Р,к] lemma qDu (m L:Nat) (h1:2^L<=m) (h2:m<2^(L+1)):m.log2=L :=(Nat.log2_eq_iff (by have:=Nat.two_pow_pos L;omega)).2 ⟨h1,h2⟩ lemma qEv (M:Nat) (e T:Int) (K r:Nat) (d:T=e+(K+1:Nat)) (c:0< r) (f:2*(r*2^K)< M+2^K) (g:M<2*(r*2^K)+2^K) : Model.roundMantissaAtExponentEven M e T=r:=by have b:r <<< (K+1)=2*(r*2^K):=by rw[щ,Nat.pow_succ];ring have a:Model.pow2 (K+1-1)=2^K:=Model.pow2_eq_two_pow K;rw[Model.roundMantissaAtExponentEven,з א,show (T-e).toNat=K+1 by omega];rcases le_total (2*(r*2^K)) M with h | h;. have:=Model.roundShiftRightEven_shiftLeft_add_of_lt_half r (M-2*(r*2^K)) (K+1) א (by rw[a];omega);rwa [b,Nat.add_sub_cancel' h] at this . have:=Model.roundShiftRightEven_shiftLeft_sub_of_lt_half r (2*(r*2^K)-M) (K+1) c א (by rw[a];omega);rwa [b,Nat.sub_sub_self h] at this lemma qEq (s:Sign) (P:Nat) (E:Int) (h1:2^23<=P) (h2:P<2^24) (hE:-149<=E) (hp:0< P):Model.finishRoundedMantissa Format.binary32 s (P,E)=.finite s P E hp:=by have:=Model.finishRoundedMantissa_normalized FloatFormat.binary32 s P (E+23) h1 h2 (by show -126<=E+23;omega) rwa [show E+23-(FloatFormat.binary32.fracWidth:ℤ)=E from Int.add_sub_cancel E 23] at this lemma qBb (neg:Bool) (m:Nat) (e:Int) : Ч Fmt ⟨neg,m,e⟩ =Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) m e):=by simp[Model.roundDyadic,Model.ieeeRoundDyadic] rfl lemma qBs (s:Sign) (M:Nat) (E:Int) (h:0< M) (hM1:2^23<=M) (hM2:M<2^24) (hb1:1<=E+150) (hb2:E+150<=254) : (UnpackedFloat.pack Format.binary32 (.finite s M E h)).toNat =(if s=.negative then 2^31 else 0)+(E+150).toNat*2^23+M%2^23:=by simp only[UnpackedFloat.pack,qDu M 23 hM1 hM2] rw[show (E+↑Format.binary32.exponentBias + ↑Format.binary32.mantissaBitsWithoutImplicit).toNat=(E+150).toNat by simp only[Format.exponentBias];norm_num;ring_nf] have c:¬ (2^Format.binary32.exponentBits<=(E+150).toNat+1):=by show ¬ (2^8<=(E+150).toNat+1);omega have a:(23+1=Format.binary32.mantissaBits):=rfl have b:s.toBitVec.toNat=if s=.negative then 1 else 0:=by cases s<;>rfl simp only[c,ite_false,a,ite_true,packComponents,BitVec.toNat_append,b,BitVec.toNat_ofNat] rw[З (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (by omega:(E+150).toNat<2^8), <-Nat.shiftLeft_add_eq_or_of_lt (э _ (by positivity):M%2^23<2^23)] split_ifs<;>simp[Nat.shiftLeft_eq] omega lemma qFe (neg:Bool) (m:Nat) (e:Int) (P:Nat) (E:Int) (hm:m≠0) (hP1:2^23<=P) (hP2:P<2^24) (hE:-149<=E) (hE2:E+150<=254) (hhi:2*((m:ℝ)*2^e)<(2*P+1)*2^E) (hlo1:P≠2^23->(2*P-1)*2^E<2*((m:ℝ)*2^e)) (hlo2:P=2^23->33554431*2^E<4*((m:ℝ)*2^e)) : toWord (Ч Fmt ⟨neg,m,e⟩) =UInt32.ofNat ((if neg then 2^31 else 0)+(E+150).toNat*2^23+P%2^23):=by have l:0< P :=א obtain ⟨k,F⟩:∃ k:Nat,e-E+2<=k:=⟨(e-E+2).toNat,by omega⟩ obtain ⟨g,z⟩:∃ g:Nat,(e-k)+(g+2:Nat)=E:=⟨(E-(e-k)-2).toNat,by omega⟩ have u:(0:ℝ)<2^(e-k):=ы two_pos _ have s:(m:ℝ)*2^e=((m*2^k:Nat):ℝ)*2^(e-k):=by push_cast rw[mul_assoc,<-B_z e _ k א] have i:(2:ℝ)^E=4*((2^g:Nat):ℝ)*2^(e-k):=by rw[B_z E (e-k) (g+2) א] push_cast ring rw[qBb,<-ExactRuntime.round_scale _ _ m e k hm] have p:m*2^k≠0:=Nat.mul_ne_zero hm (by positivity) rw[s,i] at hhi rw[s,i] at hlo1 hlo2 have H:0<2^g:=by positivity generalize m*2^k=M at * generalize e-k=q at * generalize (2:ℝ)^q=b at * have f:∀ A B:ℕ,(A:ℝ)*b< B*b->A< B :=λA B h=>by exact_mod_cast lt_of_mul_lt_mul_right h u.le push_cast at hhi hlo1 hlo2 have G:M<4*(P*2^g)+2*2^g:=f _ _ (by push_cast;linarith) have t:P≠2^23->4*(P*2^g)< M+2*2^g :=λh=>f _ _ (by push_cast;linarith [hlo1 h]) have y:P=2^23->33554431*2^g< M :=λh=>f _ _ (by push_cast;linarith [hlo2 h]) have C:P*2^g<=(2^24-1)*2^g:=Т _ א have r:2*(P*2^(g+1))=4*(P*2^g):=by rw[Nat.pow_succ];ring have n:2^(g+1)=2*2^g:=by rw[Nat.pow_succ];ring have w:UnpackedFloat.round Format.binary32 (Model.modelSign neg) M q =.finite (Model.modelSign neg) P E l:=by rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ p] have o:∀ (L:ℕ) (T:ℤ),2^L*2^g<=M->M<2^L*2^g*2 → max (((L+g:ℕ):ℤ)+1+q-24) (-149)=T → Format.binary32.targetExponent (totalExponent M q)=T:=λL T a c d=>by rw[<-Nat.pow_add] at a c simp only[totalExponent,qDu M _ a c,qG6];exact d by_cases v:2^25*2^g<=M . rw[o 25 E v א א,qEv M q E (g+1) P א l ?_ (by rw[r,n];omega)];. exact qEq _ P E hP1 hP2 hE l . rw[r,n];by_cases j:P=2^23;. subst j;omega . have:=t j;omega have j:P=2^23:=by by_contra hne have:=t hne have:(2^23+1)*2^g<=P*2^g:=Т _ א omega have D:=y j subst j by_cases x:-148<=E . rw[o 24 (E-1) א א א,qEv M q (E-1) g (2^24) א ב א א];have:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (Model.modelSign neg) (E-1+23) (by show -126<=E-1+23;omega);rw[show (FloatFormat.binary32.fracWidth:ℤ)=23 from rfl] at this;rwa [(א:E-1+23-23=E-1),(א:E-1+23+1-23=E)] at this . rw[o 24 E א א א,qEv M q E (g+1) (2^23) א ב (by rw[n];omega) (by rw[n];omega)];exact qEq _ _ E ב ב hE l rw[w] simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBs _ _ _ _ hP1 hP2 א א] cases neg<;>simp[Model.modelSign] end abbrev B_D:=ExecFloat.Binary.format (exponentBits:=11) (fractionBits:=52) lemma qG4 (f:Float):Model.signBit (toModel (ofFloat f))=decide (2^63<=f.toBits.toNat):=by rw[Model.signBit_eq_msb];show f.toBits.toBitVec.msb=_;rw[BitVec.msb_eq_decide];rfl lemma B_R (f:Float) (s:Bool) (E F:Nat) (hE1:1<=E) (hE:E<2047) (hF:F<2^52) (h:f.toBits.toNat=(if s then 2^63 else 0)+E*2^52+F) : f.isFinite=true ∧ R f=(if s then -1 else 1)*((2^52+F:Nat):ℝ)*2^((E:Int)-1075):=by have g:Model.expField (toModel (ofFloat f))=f.toBits.toNat/2^52%2^11:=by show ((f.toBits.toBitVec >>> 52) &&& BitVec.ofNat 64 2047).toNat=_ rw[BitVec.toNat_and,BitVec.toNat_ushiftRight,BitVec.toNat_ofNat,т] exact О _ 11 have e:Model.fracField (toModel (ofFloat f))=f.toBits.toNat%2^52:=by show (f.toBits.toBitVec &&& BitVec.ofNat 64 (2^52-1)).toNat=_ rw[BitVec.toNat_and,BitVec.toNat_ofNat] exact О _ 52 have i:Model.signBit (toModel (ofFloat f))=s :=(qG4 f).trans (by cases s<;>simp at h ⊢<;>omega) have c:=Model.ofFields_signBit_expField_fracField (toModel (ofFloat f)) rw[g,e,i,show f.toBits.toNat/2^52%2^11=E by split at h<;>omega,show f.toBits.toNat%2^52=F by split at h<;>omega] at c have b:E<2^11 :=א have a:Я (Model.ofFields B_D s E F)=true:=by rw[Model.isFinite_ofFields B_D _ _ _ b hF] show (E != 2047)=true simp;omega have d:=Model.toDyadic?_ofFields_of_isFinite B_D _ _ _ b hF a rw[c,if_neg (א:¬ E=0)] at d rw[c] at a refine ⟨(qAk f).symm.trans a,?_⟩ rw[R,Model.toReal_eq,d] simp only[show Model.pow2 B_D.fracWidth=2^52 from rfl, show Int.ofNat E-Int.ofNat B_D.exponentBias-Int.ofNat B_D.fracWidth=(E:ℤ)-1075 by show (E:ℤ)-1023-52=_;omega] cases s<;>simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] def B_near (A:Nat) (S:Bool) (ex fr:Nat):Prop := ∃ M,A=(if S then 2^63 else 0)+M∧(ex*2^23+fr)*2^29< M+2^28 ∧ M<=(ex*2^23+fr)*2^29+2^28 lemma B_x (A:UInt64) (x:UInt64) (hx:(A+0x0FFFFFFF) >>> 29=x) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151) : x.toNat<2^35∧B_near A.toNat (decide (2^34<=x.toNat)) (x.toNat/2^23%2^11) (x.toNat%2^23):=by have a:=A.toNat_lt have e:x.toNat=(A.toNat+268435455)%2^64/2^29:=by rw[<-hx,ъ,л] norm_num [Nat.shiftRight_eq_div_pow] refine ⟨by omega,?_⟩ unfold B_near by_cases h:2^34<=x.toNat . simp only[h,decide_true,ite_true];exact ⟨A.toNat-2^63,by omega,by omega,by omega⟩ . simp only[h,decide_false,Bool.false_eq_true,ite_false];exact ⟨A.toNat,by omega,by omega,by omega⟩ lemma qJ0 (x:UInt64) (hx:x.toNat<2^35) (h1:897<=x.toNat/2^23%2^11) (h2:x.toNat/2^23%2^11<1151) : (((x >>> 34) <<< 31) ||| ((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< 23))).toUInt32 = UInt32.ofNat ((if decide (2^34<=x.toNat) then 2^31 else 0) + (x.toNat/2^23%2^11-896)*2^23+x.toNat%2^23):=by have b:((x >>> 34) <<< 31).toNat=(x.toNat/2^34)*2^31:=by rw[UInt64.toNat_shiftLeft,ъ] norm_num [Nat.shiftRight_eq_div_pow,Nat.shiftLeft_eq] omega have a:(x &&& (0x3FFFFFFFF:UInt64)).toNat=x.toNat%2^34:=by rw[و] exact О _ 34 have d:((x &&& (0x3FFFFFFFF:UInt64))-((896:UInt64) <<< 23)).toNat =x.toNat%2^34-896*2^23:=by rw[show ((896:UInt64) <<< 23)=7516192768 by decide,UInt64.toNat_sub_of_le,a] . rfl . rw[UInt64.le_iff_toNat_le,a];norm_num;omega have c:(x.toNat/2^34)*2^31 ||| (x.toNat%2^34-896*2^23) =(x.toNat/2^34)*2^31+(x.toNat%2^34-896*2^23):=by rw[ж (x.toNat/2^34)] exact (Nat.two_pow_add_eq_or_of_lt א _).symm apply Щ.mp rw[UInt64.toNat_toUInt32,UInt64.toNat_or,b,d,c,س] simp only[decide_eq_true_eq] split_ifs<;>omega lemma qDq (A B:UInt64) (h:zivW A B≠Е) : ∃ (S:Bool) (ex fr:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ zivW A B=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near A.toNat S ex fr∧B_near B.toNat S ex fr:=by unfold zivW at h ⊢ dsimp only at h ⊢ generalize hx:(A+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=x at h ⊢ generalize hy:(B+(0x0FFFFFFF:UInt64)) >>> (29:UInt64)=y at h ⊢ have hex:((x >>> (23:UInt64)) &&& (0x7FF:UInt64)).toNat=x.toNat/2^23%2^11:=by rw[و,ъ] have:=О (x.toNat >>> 23) 11 norm_num [Nat.shiftRight_eq_div_pow] at this ⊢ exact this generalize (x >>> (23:UInt64)) &&& (0x7FF:UInt64)=ex at h hex ⊢ split at h . rename_i hc;have hc0:=hc;obtain ⟨hxy,hc⟩:=ذ.mp hc;have hxy:x=y:=by simpa using hxy subst hxy;have hc:ex-(897:UInt64)<(254:UInt64):=by simpa using hc rw[я,UInt64.toNat_sub] at hc;have hv:=ex.toNat_lt;norm_num at hc hv;have h1:897<=x.toNat/2^23%2^11 :=א;have h2:x.toNat/2^23%2^11<1151 :=א;simp only[hc0,ite_true];obtain ⟨hx35,ha⟩:=B_x A x hx h1 h2;exact ⟨_,_,_,h1,h2,by omega,qJ0 x hx35 h1 h2,ha,(B_x B x hy h1 h2).2⟩ . exact absurd rfl h lemma qCk (s e:Float) (h:ziv s e≠Е) : ∃ (S:Bool) (ex fr:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ B_near (s+e).toBits.toNat S ex fr∧B_near (s-e).toBits.toNat S ex fr := qDq (s+e).toBits (s-e).toBits h set_option maxRecDepth 8192 in lemma qA9 (f:Float) (S:Bool) (ex fr:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hn:B_near f.toBits.toNat S ex fr) : f.isFinite=true∧∃ v:ℝ,R f=(if S then -v else v) ∧ v<=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< v) ∧ (2^53-2^28)*2^((ex:Int)-1076)< v:=by obtain ⟨M,k,o,n⟩:=hn have d:M%2^52<2^52:=э _ ב have b:M=(M/2^52)*2^52+M%2^52 :=א generalize M/2^52=E at b generalize M%2^52=F at b d subst b have c:(0:ℝ)<2^((ex:Int)-1076):=ы ב _ have m:E=ex∨(E+1=ex∧fr=0) :=א obtain ⟨a,l⟩:=B_R f S E F א א d (by rw[k,с]) obtain ⟨w,j,i,e,g⟩:∃ w:ℕ,((2^52+F:Nat):ℝ)*2^((E:Int)-1075) =w*2^((ex:Int)-1076)∧w<=2^53+fr*2^30+2^29 ∧ (fr≠0->2^53+fr*2^30< w+2^29)∧2^53< w+2^28:=by rcases m with rfl | ⟨rfl,rfl⟩ . refine ⟨2*(2^52+F),?_,by omega,by omega,by omega⟩;rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,а Р,zpow_one];push_cast;ring . exact ⟨2^52+F,by congr 2;push_cast;ring,by omega,by omega,by omega⟩ refine ⟨a,(w:ℝ)*2^((ex:Int)-1076),?_,Б ?_ c.le,λh=>б ?_ c,б ?_ c⟩ . rw[l,mul_assoc,j];cases S<;>simp . exact_mod_cast i . rw[sub_lt_iff_lt_add];exact_mod_cast e h . rw[sub_lt_iff_lt_add];exact_mod_cast g lemma qBh (s e:Float) (h:ziv s e≠Е) : (s+e).isFinite=true∧(s-e).isFinite=true∧(2:ℝ)^(-127:ℤ) ≤ |R (s+e)|:=by obtain ⟨S,f,c,b,d,a,-,j,k⟩:=qCk s e h;obtain ⟨g,v,i,-,-,l⟩:=qA9 _ S f c b d a j;refine ⟨g,(qA9 _ S f c b d a k).1,?_⟩ exact (show (2:ℝ)^(-127:ℤ)<=(2^53-2^28)*2^(-179:ℤ) by norm_num).trans ((Г (Ш one_le_two א) ב).trans (l.le.trans (by rw[i];cases S<;>simp[le_abs_self]))) lemma qAq (w:UInt32) (h:expOf w≠255):wv w=(Д w:ℝ):=by rw[wv,Model.toReal_eq,ofW,Ы w h];simp only[rationalWord,FloatLib.Numerics.Dyadic.cast_toRat] lemma qFl (a b:Array UInt32) (start n:Nat) : (dsum a b start n:ℚ)*2^(-298:Int) =∑ j∈р n,Д a[start+j]! * Д b[start+j]!:=by have c:∀ a b:UInt32,((sv a:ℚ)*sv b*(2:ℚ)^(idxOf a+idxOf b))*2^(-298:Int) =Д a*Д b:=λa b=>by simp only[rationalWord,decodeD_toRat] rw[show (-298:Int)=-149+-149 by norm_num,а Р] rw[ن Р,ن Р,к,к,pow_add] norm_num [zpow_neg,zpow_natCast] ring induction n generalizing start with | zero=>simp[dsum] | succ n ih => rw[dsum,Int.cast_add,Int.cast_mul,Int.cast_mul,Int.cast_pow,Int.cast_ofNat,add_mul,ih,Finset.sum_range_succ',у,c,add_comm] congr 1 apply ر rfl intro j _ rw[с,Nat.add_comm 1 j] lemma qCe (n:Nat) (fa fb:Nat->UInt32) (hf:∀ j,j< n->expOf (fa j)≠255∧expOf (fb j)≠255) (hn:(∑ j∈р n,Д (fa j)*Д (fb j))≠0) : ∃ d:FloatLib.Numerics.Dyadic, d.toRat=(∑ j∈р n,Д (fa j)*Д (fb j)) ∧ dotOf n fa fb=toWord (Ч Fmt d):=by let a:=Array.ofFn (n:=n) λi=>fa i let b:=Array.ofFn (n:=n) λi=>fb i have e:a.size=n:=by simp[a] have g:b.size=n:=by simp[b] have c:(dsum a b 0 n:ℚ)*2^(-298:Int) =∑ j∈р n,Д (fa j)*Д (fb j):=by rw[qFl] refine ر rfl λj m=>?_ have h:=ؽ.mp m rw[Л,qAa n fa j h,qAa n fb j h] have l:dsum a b 0 n≠0:=λh=>hn (by rw[<-c,h];simp) have Z:=qEt a b (by rw[e,g]) λk f=>by rw[e] at f rw[qAa n fa k f,qAa n fb k f];exact hf k f rw[e] at Z refine ⟨ofIntDyadic (dsum a b 0 n) (-298),by rw[toRat_ofIntDyadic,c],?_⟩ show dotSpec a b=_ rw[<-Z];simp[finishWord,l] lemma qG2:Д oneW=1:=by norm_num [rationalWord,decodeD,show expOf oneW=127 by decide,show fracOf oneW=0 by decide,show signOf oneW=false by decide,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand] lemma qJy (n:Nat) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) : ((∑ j∈р (n+1),Д (if j< n then xw j else oneW) * Д (if j< n then ww j else bw):ℚ):ℝ)=exactS n xw ww bw:=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qG2,one_mul] rw[Rat.cast_add,Rat.cast_sum,exactS,qAq bw (qAd bw hb)] congr 1 refine ر rfl λt b=>?_ have a:=ؽ.mp b simp only[a,ite_true] rw[qAq _ (qAd _ (hx t a)),qAq _ (qAd _ (hw t a)),Rat.cast_mul] lemma qCd (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е) : ∃ (S:Bool) (ex fr:Nat) (W:ℝ),897<=ex∧ex<1151∧fr<2^23 ∧ ziv s e=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ V=(if S then -W else W) ∧ W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,ha,hb'⟩:=qCk s e h obtain ⟨-,va,hva,hia,loa1,loa⟩:=qA9 _ S ex fr h1 h2 h3 ha obtain ⟨-,vb,hvb,hib,lob1,lob⟩:=qA9 _ S ex fr h1 h2 h3 hb' rw[hva] at hhi rw[hvb] at hlo refine ⟨S,ex,fr,if S then -V else V,h1,h2,h3,hz,?_,?_,?_,?_⟩ all_goals cases S all_goals simp only[Bool.false_eq_true,ite_false,ite_true,neg_neg] at hhi hlo ⊢ . exact ч hhi hia . linarith . exact λh=>(lob1 h).trans hlo . exact λh=>by linarith [loa1 h] . exact lob.trans hlo . linarith lemma qF9 (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е):V≠0:=by obtain ⟨S,ex,fr,W,-,-,-,-,hV,-,-,hW3⟩:=qCd s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=ы ב _;have hA:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076):=Н ב hb0.le;intro h0;rw[h0] at hV;cases S;. simp only[Bool.false_eq_true,ite_false] at hV;linarith . simp only[ite_true] at hV;linarith lemma qE8 (d:FloatLib.Numerics.Dyadic) (S:Bool) (ex fr:Nat) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (W:ℝ) (hd:(d.toRat:ℝ)=if S then -W else W) (hW1:W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) (hW3:(2^53-2^28)*2^((ex:Int)-1076)< W) : toWord (Ч Fmt d) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hb0:(0:ℝ)<2^((ex:Int)-1076):=ы ב _ have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-к,<-а Р] congr 1;push_cast;ring have hW0:0< W:=ц (Н ב hb0.le) hW3 obtain ⟨neg,m,q⟩:=d have hdV:(FloatLib.Numerics.Dyadic.mk neg m q).toReal=if S then -W else W:=by rw[<-д,hd] have hq:(0:ℝ)<2^q:=ы ב _ have hm0:(0:ℝ)<=m:=Ю _ have hmq:=Н hm0 hq.le have hMW:(m:ℝ)*2^q=W∧neg=S:=by simp only[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand] at hdV cases neg<;>cases S <;> simp only[Bool.false_eq_true,ite_false,ite_true,Int.ofNat_eq_natCast,Int.cast_neg,Int.cast_natCast] at hdV . exact ⟨by linarith,rfl⟩ . exfalso;linarith . exfalso;linarith . exact ⟨by linarith,rfl⟩ obtain ⟨hMW,rfl⟩:=hMW have hm:m≠0:=by rintro rfl simp at hMW linarith have hword:=qFe neg m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hMW,hE];push_cast;linarith) (by intro hfr have hfr':fr≠0:=by intro h0;exact hfr א have:=hW2 hfr' rw[hMW,hE];push_cast;linarith) (by intro _ rw[hMW,hE];linarith) have e1:((ex:Int)-1046+150).toNat=ex-896 :=א have e2:(2^23+fr)%2^23=fr :=א rw[hword,e1,e2] lemma qIa (s e:Float) (d:FloatLib.Numerics.Dyadic) (hlo:R (s-e)<(d.toRat:ℝ)) (hhi:(d.toRat:ℝ)< R (s+e)) (h:ziv s e≠Е) : ziv s e=toWord (Ч Fmt d):=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=qCd s e _ hlo hhi h;rw[hz];exact (qE8 d S ex fr h1 h2 h3 W hV hW1 hW2 hW3).symm lemma qCq (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (s e:Float) (hlo:R (s-e)< exactS n xw ww bw) (hhi:exactS n xw ww bw< R (s+e)) (h:ziv s e≠Е) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by have hsum:=qJy n xw ww bw hx hw hb obtain ⟨d,hd,hw'⟩:=qCe (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw) (λj _=>by by_cases hj:j< n . simp only[hj,ite_true];exact ⟨qAd _ (hx j hj),qAd _ (hw j hj)⟩ . simp only[hj,ite_false] exact ⟨by decide,qAd _ hb⟩) λh0=>qF9 s e _ hlo hhi h (by rw[<-hsum,h0,Rat.cast_zero]) have q:(d.toRat:ℝ)=exactS n xw ww bw:=by rw[hd,hsum] rw[hw'] exact qIa s e d (by rw[q];exact hlo) (by rw[q];exact hhi) h end section lemma qB4 (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:Float) (hs:let r:=l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv (ww t) F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s+dv bw∧e=nfOf n*(r.t+(dv bw).abs)) (h:ziv s e≠Е) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBh s e h obtain ⟨hlo,hhi⟩:=qB3 n hn xw ww bw hx hw hb l hl hl2 hz s e hs hbig exact qCq n hn xw ww bw hx hw hb s e hlo hhi h lemma qDe (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh:UInt32) (hx:∀ t,t< n->good (xw t)=true) (hw:∀ t,t< n->good (ww t)=true) (hb:good bw=true) (hg:good hh=true) (hm:∀ t,t< n->xw t &&& В<=hh) (hp:hh<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0∨ww t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*(l.foldl (λ(acc:Float) t=>acc+(dv (ww t)).abs) 0)+(dv bw).abs)) (h:ziv s e≠Е) : 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⟩:=qBh s e h obtain ⟨hlo,hhi⟩:=qDg n hn xw ww bw hh hx hw hb hg hm hp l hl hl2 hz s e hs hbig exact qCq n hn xw ww bw hx hw hb s e hlo hhi h end section lemma qDt (n:Nat) (hn:n<2^20) (xw ww:Nat->UInt32) (bw hh hv: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) (hgv:good hv=true) (hm:∀ t,t< n->xw t &&& В<=hh) (hp:hh<=В) (hmv:∀ t,t< n->ww t &&& В<=hv) (hpv:hv<=В) (l:List Nat) (hl:l.Nodup) (hl2:∀ t∈l,t< n) (hz:∀ t,t< n->t∉l->xw t &&& В=0) (s e:Float) (hs:s=l.foldl (λ(acc:Float) t=>acc+dv (xw t)*dv (ww t)) 0+dv bw ∧ e=nfOf n*(dv hh*((n.toUInt64+1).toFloat*dv hv)+(dv bw).abs)) (h:ziv s e≠Е) : ziv s e=dotOf (n+1) (λt=>if t< n then xw t else oneW) (λt=>if t< n then ww t else bw):=by obtain ⟨-,-,hbig⟩:=qBh s e h obtain ⟨hlo,hhi⟩:=qDv n hn xw ww bw hh hv hx hw hb hg hgv hm hp hmv hpv l hl hl2 hz s e hs hbig exact qCq n hn xw ww bw hx hw hb s e hlo hhi h end section lemma C_z:(0:USize).toNat=0:=rfl lemma qAc (k:Nat) (h:k<2^32):k.toUSize.toNat=k:=by rw[Nat.toUSize_eq];exact uk k h lemma qDh (a b:USize) (h:a.toNat*b.toNat<2^32) : (a*b).toNat=a.toNat*b.toNat:=by have h1:2^32<=USize.size:=USize.le_size;have h3:USize.size=2^System.Platform.numBits:=rfl;rw[USize.toNat_mul];exact З א lemma qCy {α:Type} (a:Array α) (h:a.size<2^32):a.usize.toNat=a.size :=qAc _ h lemma qAj (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:=qBg a i h;rw[Э a i.toNat h2];rfl . rename_i h;have h':¬ i.toNat< a.size:=by intro h2;apply h;rw[А,qCy a hs];exact h2 rw[غ a i.toNat h'];rfl lemma qA8 (a:Array UInt32) (i:USize) (v:UInt32):(wrW a i v).size=a.size:=by unfold wrW;split<;>simp lemma qGp {α:Type} [Inhabited α] (a:Array α) (i q:Nat) (v:α) (hi:i< a.size) : (a.set i v hi)[q]! = if q=i then v else a[q]!:=by by_cases hq:q< a.size . rw[Э _ q (by simpa using hq),getElem!_pos a q hq,Array.getElem_set];by_cases e:q=i;. simp[e] . rw[Х e,Х (λh=>e h.symm)] . rw[غ _ q (by simpa using hq),getElem!_neg a q hq,ite_eq_right א] lemma qBd (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[А,qCy a hs];exact hi unfold wrW;rw[У h];exact qGp a _ q v hi def C_Inv (N:Nat) (sp:Nat->Word) (S:Nat->Prop) (out:Array Word):Prop := out.size=N∧∀ q,q< N->S q->out[q]! = sp q lemma qFz {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp S out) {u:USize} {p:Nat} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q=p∨S q) (wrW out u v):=by obtain ⟨h1,h2⟩:=h;refine ⟨by rw[qA8,h1],λq hq hS=>?_⟩;rw[qBd _ _ _ _ א א,hu];by_cases e:q=p;. rw[з e,e,hv] . rw[Х e];exact h2 q hq (hS.resolve_left e) lemma qEz {N:Nat} {sp:Nat->Word} {S S':Nat->Prop} {out:Array Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S' q->S q):C_Inv N sp S' out :=⟨h.1,λq hq h'=>h.2 q hq (hS q hq h')⟩ lemma qDp {N:Nat} {sp:Nat->Word} {S:Nat->Prop} {out:Array Word} (h:C_Inv N sp S out) (hS:∀ q,q< N->S q) : out=Array.ofFn (n:=N) λq=>sp q.val:=by obtain ⟨h1,h2⟩:=h;apply ه;. simp[h1] . intro q h3 h4;have h5:q< N :=א;have:=h2 q h5 (hS q h5);rw[Э out q h3] at this;simp[this] lemma qFi (v:UInt32) (h1:(0x7FFFFF:UInt32)<=v-1) (h2:v<Ц) : (v == 0 || v-(0x00800000:UInt32)<=(0x7EFFFFFF:UInt32))=true:=by have a1:(0x7FFFFF:UInt32).toNat=0x7FFFFF:=rfl;have a2:(Ц:UInt32).toNat=Ц:=rfl;have a3:(0x00800000:UInt32).toNat=0x00800000:=rfl;have a4:(0x7EFFFFFF:UInt32).toNat=0x7EFFFFFF:=rfl;have a5:(1:UInt32).toNat=1:=rfl;by_cases h0:v=0;. simp[h0] . have h3:(1:UInt32)<=v:=by rw[М];have:v.toNat≠0:=λh=>h0 (Щ.mp h);omega rw[М,ي _ _ h3] at h1;rw[ю] at h2;have h4:(0x00800000:UInt32)<=v:=by rw[М];omega simp only[Bool.or_eq_true,decide_eq_true_eq];right;rw[М,ي _ _ h4];omega lemma qJm (c u lo:UInt32) (h:c<=if u< lo then u else lo):c<=u∧c<=lo:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qJp (c v hi:UInt32) (h:(if hi< v then v else hi)< c):v< c∧hi< c:=by simp only[UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qB2 (w:UInt32) (h:good w=true):good (w &&& В)=true:=by have e:(w &&& В) &&& В=w &&& В:=by rw[UInt32.and_assoc,UInt32.and_self] unfold good at h ⊢;simpa only [e] using h lemma qFs {h:UInt32} (h1:h<Ц):h<=В:=by have a1:(Ц:UInt32).toNat=Ц:=rfl;have a2:(В:UInt32).toNat=В:=rfl;rw[ю] at h1;rw[М];omega def C_Hv (A:Array Word) (h:Word):Prop := good h=true∧h<=В∧(∀ k,k< A.size->good A[k]! = true) ∧ ∀ k,k< A.size->A[k]! &&& В<=h lemma qF3 (r v hi:UInt32) (h:(if hi< v then v else hi)<=r):v<=r∧hi<=r:=by simp only[UInt32.le_iff_toNat_le,UInt32.lt_iff_toNat_lt] at *;split at h<;>omega lemma qIk (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi≠Е → (0x7FFFFF:UInt32)<=lo∧hi<Ц∧hi<=scan A n i hn lo hi ∧ scan A n i hn lo hi<Ц∧(good hi=true->good (scan A n i hn lo hi)=true) ∧ ∀ k,i.toNat<=k->k< n.toNat → good A[k]! = true∧A[k]! &&& В<=scan A n i hn lo hi:=by induction d with | zero => intro i lo hi he h have e:scan A n i hn lo hi =if 0x007FFFFF<=lo∧hi<Ц then hi else Е:=by rw[scan,Ъ (by rw[А];omega)] rw[e] at h ⊢ by_cases c:0x007FFFFF<=lo∧hi<Ц . rw[з c];exact ⟨c.1,c.2,UInt32.le_refl _,c.2,id,λk h1 h2=>by omega⟩ . rw[Х c] at h;exact absurd rfl h | succ d ih => intro i lo hi he h have hi':i< n:=by rw[А];omega have hb:i.toNat< A.size :=א have e:scan A n i hn lo hi=scan A n (i+1) hn (if (A.uget i hb &&& В)-1< lo then (A.uget i hb &&& В)-1 else lo) (if hi< A.uget i hb &&& В then A.uget i hb &&& В else hi):=by rw[scan,У hi'] rw[e] at h ⊢ obtain ⟨h1,h2,h3,h4,h5,h6⟩:=ih (i+1) _ _ (by rw[usz i n hi'];omega) h obtain ⟨h7,h8⟩:=qJm _ _ _ h1 obtain ⟨h9,h10⟩:=qJp _ _ _ h2 obtain ⟨h11,h12⟩:=qF3 _ _ _ h3 have hg:good (A.uget i hb)=true:=qFi _ h7 h9 refine ⟨h8,h10,h12,h4,λg0=>h5 ?_,λk hk1 hk2=>?_⟩ . split;. exact qB2 _ hg . exact g0 . by_cases e2:k=i.toNat;. subst e2;rw[Э A i.toNat hb];exact ⟨hg,h11⟩ . exact h6 k (by rw[usz i n hi'];omega) hk2 lemma qAp (A:Array UInt32) (hs:A.size<2^32) (h:scan A A.usize 0 (qAf A) Е 0≠Е) : C_Hv A (scan A A.usize 0 (qAf A) Е 0):=by have e:=qCy A hs;have h0:(0:USize).toNat=0:=rfl;obtain ⟨-,-,-,h4,h5,h6⟩ :=qIk A A.usize (qAf A) A.size 0 _ _ (by rw[h0,e];omega) h;have h7:∀ k,k< A.size->good A[k]! = true ∧ A[k]! &&& В<=scan A A.usize 0 (qAf A) Е 0 :=λk hk=>h6 k א א;exact ⟨h5 rfl,qFs h4,λk hk=>(h7 k hk).1,λk hk=>(h7 k hk).2⟩ lemma qFk (w:UInt32):w &&& В<=В:=by rw[М,ز];exact Nat.and_le_right def C_hok (w:Nat->Word) (t:Nat) (h:Word):Prop := good h=true∧(∀ k,k< t->w k &&& В<=h)∧h<=В lemma qEn {w:Nat->Word} {t:Nat} {h:Word} (hg:good (w t)=true) (H:C_hok w t h):C_hok w (t+1) (mx h (w t)):=by obtain ⟨h1,h2,hp⟩:=H;have hm:=qB2 _ hg;unfold mx;dsimp only;by_cases e:w t &&& В<=h . rw[з e];refine ⟨h1,λk hk=>?_,hp⟩;by_cases e2:k=t;. rw[e2];exact e . exact h2 k א . rw[Х e];refine ⟨hm,λk hk=>?_,qFk _⟩;by_cases e2:k=t;. rw[e2];exact UInt32.le_refl _ . have h3:=h2 k א;simp only[UInt32.le_iff_toNat_le] at *;omega lemma qEp {w:Nat->Word} {t:Nat} {h:Word} (hz:w t &&& В=0) (H:C_hok w t h):C_hok w (t+1) h:=by obtain ⟨h1,h2,hp⟩:=H;refine ⟨h1,λk hk=>?_,hp⟩;by_cases e2:k=t;. rw[e2,hz,М];exact Nat.zero_le _ . exact h2 k א def C_out (I:Nat) (Y:Bool) (X W b:Array Word) (i j:Nat):Word := act Y (dotOf (I+1) (λt=>if t< I then X[i*I+t]! else oneW) (λt=>if t< I then W[j*I+t]! else b[j]!)) structure C_G (R I O:Nat) (X W b:Array Word):Prop where hX:R*I=X.size hW:O*I=W.size hb:O=b.size pi:0< I po:0< O ni:I<2^20 sX:X.size<2^32 sW:W.size<2^32 sb:b.size<2^32 sO:R*O<2^32 gX:∀ q,q< X.size->good X[q]! = true gW:∀ q,q< W.size->good W[q]! = true gb:∀ q,q< b.size->good b[q]! = true lemma qBe {R I i t:Nat} (hi:i< R) (ht:t< I):i*I+t< R*I:=by have h:(i+1)*I<=R*I:=Т _ hi;rw[م,Դ] at h;omega lemma qA1 {R I i t:Nat} (hi:i< R) (ht:t<=I) : i*I+t<=R*I:=by have h:(i+1)*I<=R*I:=Т _ hi;rw[م,Դ] at h;omega section variable {R I O:Nat} {X W b:Array Word} lemma C_G.gx (G:C_G R I O X W b) {i:Nat} (hi:i< R) : ∀ t,t< I->good X[i*I+t]! = true :=λ_ ht=>G.gX _ (by rw[<-G.hX];exact qBe hi ht) lemma C_G.gw (G:C_G R I O X W b) {j:Nat} (hj:j< O) : ∀ t,t< I->good W[j*I+t]! = true :=λ_ ht=>G.gW _ (by rw[<-G.hW];exact qBe hj ht) lemma qJ3 (G:C_G R I O X W b) (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) {s e:Float} (hs:let r:=(К I).foldl (λ(acc:F1) t => let q:=dv X[i*I+t]! * dv W[j*I+t]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨0,0⟩;s=r.s∧e=r.t) : fin I Y X W b i j s (nfOf I) e b[j]! = C_out I Y X W b i j:=by obtain ⟨h1,h2⟩:=hs unfold fin dsimp only split . unfold slowOut C_out;rw[qAe] . rename_i h;unfold C_out;congr 1;exact qB4 I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb];exact hj)) _ List.nodup_range (λt ht=>й.mp ht) (λt ht h3=>absurd (й.mpr ht) h3) _ _ ⟨by rw[h1],by rw[h2]⟩ (by simpa using h) lemma qFm {X W:Array UInt32} {xo wo n:USize} (hX:X.size<2^32) (hW:W.size<2^32) (h1:xo.toNat+n.toNat<=X.size) (h2:wo.toNat+n.toNat<=W.size) (d:Nat):∀ (t:USize) (s a:Float),t.toNat+d=n.toNat → tightGo X W xo wo n t s a=(List.range' t.toNat d).foldl (λ(acc:F1) k =>let q:=dv X[xo.toNat+k]! * dv W[wo.toNat+k]! F1.mk (acc.s+q) (acc.t+q.abs)) ⟨s,a⟩:=by induction d with | zero => intro t s a he rw[tightGo,Ъ (by rw[А];omega)] rfl | succ d ih => intro t s a he have ht:t< n:=by rw[А];omega rw[tightGo,У ht,List.range'_succ,Ա] dsimp only rw[qAj _ _ hX,qAj _ _ hW,qAo _ _ א,qAo _ _ א] rw[ih (t+1) _ _ (by rw[usz t n ht];omega),usz t n ht] lemma qHh (G:C_G R I O X W b) (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) {s a:Float} {hh:Word} (hc:ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs))≠Е → ziv (s+dv b[j]!) (nfOf I*(dv hh*a+(dv b[j]!).abs)) = dotOf (I+1) (λt=>if t< I then X[i*I+t]! else oneW) (λt=>if t< I then W[j*I+t]! else b[j]!)) : fin8 I Y X W b i j s (nfOf I) a hh b[j]! I.toUSize=C_out I Y X W b i j:=by have hin:I<2^32:=lt_trans G.ni ב have hX1:i*I+I<=X.size:=by rw[<-G.hX];exact qA1 hi (le_refl _) have hW1:j*I+I<=W.size:=by rw[<-G.hW];exact qA1 hj (le_refl _) have sX:=G.sX have sW:=G.sW have e1:(i*I).toUSize.toNat=i*I:=qAc _ א have e2:(j*I).toUSize.toNat=j*I:=qAc _ א have e3:I.toUSize.toNat=I:=qAc _ hin unfold fin8 dsimp only split . have e:=qFm (xo:=(i*I).toUSize) (wo:=(j*I).toUSize) (n:=I.toUSize) G.sX G.sW (by rw[e1,e3];exact hX1) (by rw[e2,e3];exact hW1) I 0 0 0 (by rw[e3,C_z,Л]);rw[C_z,e1,e2,<-Գ] at e;rw[qJ3 G Y hi hj (s:=(tightGo X W (i*I).toUSize (j*I).toUSize I.toUSize 0 0 0).s) (e:=(tightGo X W (i*I).toUSize (j*I).toUSize I.toUSize 0 0 0).t) ⟨by rw[e],by rw[e]⟩] . rename_i h;unfold C_out;congr 1;exact hc (by simpa using h) lemma qHj (G:C_G R I O X W b) (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) {s:Float} {hh hv:Word} (hg:good hh=true) (hm:∀ t,t< I->X[i*I+t]! &&& В<=hh) (hp:hh<=В) (Hv:C_Hv W hv) (L:List Nat) (hl:L.Nodup) (hl2:∀ t∈L,t< I) (hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0) (hs:s=L.foldl (λ(acc:Float) t=>acc+dv X[i*I+t]! * dv W[j*I+t]!) 0) : fin8 I Y X W b i j s (nfOf I) ((I.toUInt64+1).toFloat*dv hv) hh b[j]! I.toUSize=C_out I Y X W b i j:=by obtain ⟨g1,g2,-,g4⟩:=Hv have hW:∀ t,t< I->j*I+t< W.size :=λ_ ht=>by rw[<-G.hW];exact qBe hj ht exact qHh G Y hi hj λh => qDt I G.ni (λt=>X[i*I+t]!) (λt=>W[j*I+t]!) b[j]! hh hv (G.gx hi) (G.gw hj) (G.gb j (by rw[<-G.hb];exact hj)) hg g1 hm hp (λt ht=>g4 _ (hW t ht)) g2 L hl hl2 hz _ _ ⟨by rw[hs],rfl⟩ h end lemma qCn (x:FloatArray) (i:USize) (h:i.toNat< x.size) : x.uget i h=x.data[i.toNat]!:=by obtain ⟨ds⟩:=x;have h':i.toNat< ds.size:=h;rw[Э ds i.toNat h'];rfl lemma qDm (a:Array UInt32) (i:USize) (h:i.toNat< a.size) : a.uget i h=a[i.toNat]!:=by rw[Э a i.toNat h];rfl lemma qCg (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 qC5 (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 [qAc _ hs] unfold wrF;rw[У h];obtain ⟨ds⟩:=a;exact qGp ds _ q v hi def C_keep (xw:Nat->Word) (t:Nat):List Nat := (К t).filter (λp=>xw p &&& В != 0) lemma qBq (xw:Nat->Word) (t:Nat):(C_keep xw t).length<=t:=by have h:=List.length_filter_le (λp=>xw p &&& В != 0) (К t);simpa [C_keep] using h lemma qDw (xw:Nat->Word) (t:Nat) (h:xw t &&& В=0) : C_keep xw (t+1)=C_keep xw t:=by simp[C_keep,List.range_succ,List.filter_append,h] lemma qD9 (xw:Nat->Word) (t:Nat) (h:¬ xw t &&& В=0) : C_keep xw (t+1)=C_keep xw t ++ [t]:=by simp[C_keep,List.range_succ,List.filter_append,h] def C_Dec (xw:Nat->Word) (sz t:Nat) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh:Word):Prop := c.toNat=(C_keep xw t).length∧ix.size=sz∧buf.size=sz ∧ (∀ k p:Nat,(C_keep xw t)[k]?=some p → (ix[k]! : UInt32).toNat=p∧buf.data[k]! = dv (xw p))∧C_hok xw t hh lemma qHw {X:Array UInt32} {o n:USize} (xw:Nat->Word) (hn:n.toNat<2^32) (hxw:∀ t:USize,t< n->rdW X (o+t)=xw t.toNat) (hg:∀ t,t< n.toNat->good (xw t)=true) (d:Nat) : ∀ (t:USize) (ix:Array UInt32) (buf:FloatArray) (c:USize) (hh:Word),t.toNat+d=n.toNat->C_Dec xw n.toNat t.toNat ix buf c hh → C_Dec xw n.toNat n.toNat (decR X o n t ix buf c hh).ix (decR X o n t ix buf c hh).buf (decR X o n t ix buf c hh).c (decR X o n t ix buf c hh).hi:=by induction d with | zero => intro t ix buf c hh he H rw[decR,Ъ (by rw[А];omega)] have e:t.toNat=n.toNat :=א rw[e] at H;exact H | succ d ih => intro t ix buf c hh he H have ht:t< n:=by rw[А];omega have hu:=usz t n ht rw[decR,У ht] dsimp only rw[hxw t ht] obtain ⟨h1,h2,h3,h4,h5⟩:=H by_cases hz:xw t.toNat &&& В=0 . rw[з (by simpa using hz)];apply ih (t+1) _ _ _ _ א;rw[hu,C_Dec,qDw xw _ hz];exact ⟨h1,h2,h3,h4,qEp hz h5⟩ . rw[Х (by simpa using hz)];apply ih (t+1) _ _ _ _ א;have hk:=qBq xw t.toNat;have hc:c.toNat< t.toNat+1 :=א;rw[hu,C_Dec,qD9 xw _ hz];have hcn:c.toNat< n.toNat :=א;refine ⟨?_,by rw[qA8];exact h2,by rw[qCg];exact h3,λk p hk2=>?_,qEn (hg _ א) h5⟩;. rw[qAo _ _ (by rw[ش];omega),ش,h1];simp . rw[qBd _ _ _ _ א א,qC5 _ _ _ _ א א];rw[List.getElem?_append,<-h1] at hk2;by_cases e:k=c.toNat;. subst e;simp at hk2;subst hk2;simp;omega . rw[Х e,Х e];by_cases e2:k< c.toNat;. rw[з e2] at hk2;exact h4 k p hk2 . rw[Х e2] at hk2;obtain ⟨m,hm⟩:∃ m,k-c.toNat=m+1:=⟨k-c.toNat-1,by omega⟩;rw[hm] at hk2;simp at hk2 lemma qBz (a:FloatArray) (v:Float):(a.push v).size=a.size+1:=by obtain ⟨ds⟩:=a;exact Array.size_push _ lemma qCo (n:USize) (d:Nat):∀ (k:USize) (a:FloatArray),k.toNat+d=n.toNat → (pushZ n k a).size=a.size+d:=by induction d with | zero => intro k a he;rw[pushZ,Ъ (by rw[А];omega)];rfl | succ d ih => intro k a he;have ht:k< n:=by rw[А];omega rw[pushZ,У ht,ih (k+1) _ (by rw[usz k n ht];omega),qBz];omega lemma qJd {x:FloatArray} {ix W:Array UInt32} {w0 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (xf:Nat->Float) (ww:Nat->Word) (hww:∀ p,p< m.toNat->W[w0.toNat+p]! = ww p) (L:List Nat):∀ (t:USize) (a:Float),t.toNat+L.length=n.toNat → (∀ k p:Nat,L[k]?=some p->p< m.toNat∧(ix[t.toNat+k]! : UInt32).toNat=p ∧ x.data[t.toNat+k]! = xf p) → go1 x ix W w0 m n t hx hi hs h0 a =L.foldl (λ(acc:Float) p=>acc+xf p*dv (ww p)) a:=by induction L with | nil => intro t a he _ rw[go1,Ъ (by rw[А];simp at he;omega)] rfl | cons p L ih => intro t a he H simp only[List.length_cons] at he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א obtain ⟨h1,h2,h3⟩:=H 0 p rfl rw[у] at h2 h3 have hit:t.toNat< ix.size :=א have e0:(ix.uget t hit).toUSize.toNat=p:=by rw[н,qDm];exact h2 have hm:(ix.uget t hit).toUSize< m:=by rw[А,e0];exact h1 have e1:x.uget t א=xf p:=by rw[qCn,h3] have e2:∀ pf,W.uget (w0+(ix.uget t hit).toUSize) pf=ww p:=by intro pf rw[qDm,qAo _ _ (by rw[e0];omega),e0,hww p h1] rw[go1,У ht] dsimp only rw[У hm] rw[e1,e2,ih (t+1) _ (by rw[usz t n ht];omega),Ա] intro k q hq have:=H (k+1) q (by simpa using hq) rw[usz t n ht,show t.toNat+1+k=t.toNat+(k+1) by omega] exact this def C_Dw (wd:FloatArray) (W:Array Word):Prop := wd.size=W.size->∀ q,q< W.size->wd.data[q]! = dv W[q]! section variable {x:FloatArray} {ix W:Array UInt32} {w0 w1 w2 w3 m n:USize} (hx:n.toNat<=x.size) (hi:n.toNat<=ix.size) (hs:W.size<2^32) (h0:w0.toNat+m.toNat<=W.size) (h1:w1.toNat+m.toNat<=W.size) (h2:w2.toNat+m.toNat<=W.size) (h3:w3.toNat+m.toNat<=W.size) lemma qJn (hv:∀ j,j< n.toNat->∃ w,good w=true∧x.data[j]! = dv w) (k:Nat) : ∀ (t:USize) (a b c d:Float),t.toNat+k=n.toNat → go4B x ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4B go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א unfold go4B go1 simp only[dite_eq_left ht] obtain ⟨w,g,e⟩:=hv _ htn have B:∀ (u:UInt32) p0,dvB (x.uget t p0) u=x.uget t p0*dv u:=by intro u p0 rw[qCn,e] exact qJf w u g have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] lemma qI1 {wd:FloatArray} (hd:wd.size=W.size) (hv:C_Dw wd W) (k:Nat) : ∀ (t:USize) (a b c d:Float),t.toNat+k=n.toNat → go4D x wd ix W w0 w1 w2 w3 m n t hx hi hs h0 h1 h2 h3 hd a b c d = ⟨go1 x ix W w0 m n t hx hi hs h0 a,go1 x ix W w1 m n t hx hi hs h1 b,go1 x ix W w2 m n t hx hi hs h2 c,go1 x ix W w3 m n t hx hi hs h3 d⟩:=by induction k with | zero => intro t a b c d he have hn:¬ t< n:=by rw[А];omega unfold go4D go1 simp only[dite_eq_right hn] | succ k ih => intro t a b c d he have ht:t< n:=by rw[А];omega have htn:t.toNat< n.toNat :=א unfold go4D go1 simp only[dite_eq_left ht] have B:∀ (u:USize) p1,dv (W.uget u p1)=wd.uget u (hd ▸ p1):=λu p1=>by rw[qCn,hv hd _ p1,qDm] have J:=λa b c d=>ih (t+1) a b c d (by rw[usz t n ht];omega) split<;>simp only[B,J] end def C_sp (I O:Nat) (Y:Bool) (X W b:Array Word) (q:Nat):Word := C_out I Y X W b (q/O) (q%O) lemma qHi (I O:Nat) (Y:Bool) (X W b:Array Word) {i j:Nat} (hj:j< O) : C_sp I O Y X W b (i*O+j)=C_out I Y X W b i j:=by unfold C_sp;have h0:0< O :=א;rw[ж i O,ـ h0,ف,ш hj,З hj,у] lemma qAn {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:USize} {v:Word} (hu:u.toNat=p) (hp:p< N) (hv:v=sp p):C_Inv N sp (λq=>q< p+1) (wrW out u v) := qEz (qFz hN h hu hp hv) (λq _ (hq:q< p+1) =>(by omega:q=p∨q< p)) lemma qEy {u:USize} {p:Nat} (hu:u.toNat=p) (h:p+3<2^32) : (u+1).toNat=p+1∧(u+2).toNat=p+2∧(u+3).toNat=p+3 ∧ (p+4<2^32->(u+4).toNat=p+4):=by have l:∀ k,k<5->p+k<2^32->(u+USize.ofNat k).toNat=p+k:=λk hk h=>by rw[qAo _ _ (by rw[uk k א];omega),uk k א,hu] exact ⟨l 1 א א,l 2 א א,l 3 א א,l 4 א⟩ lemma qFb {N:Nat} {sp:Nat->Word} {p:Nat} {out:Array Word} (hN:N<2^32) (h:C_Inv N sp (λq=>q< p) out) {u:USize} {v0 v1 v2 v3:Word} (hu:u.toNat=p) (hp:p+4<=N) (h0:v0=sp p) (h1:v1=sp (p+1)) (h2:v2=sp (p+2)) (h3:v3=sp (p+3)):C_Inv N sp (λq=>q< p+4) (wrW (wrW (wrW (wrW out u v0) (u+1) v1) (u+2) v2) (u+3) v3):=by obtain ⟨a1,a2,a3,-⟩:=qEy hu א exact qAn hN (qAn hN (qAn hN (qAn hN h hu א h0) a1 א h1) a2 א h2) a3 א h3 lemma qFu {X:Array UInt32} {a n:Nat} (hX:X.size<2^32) (hn:a+n<=X.size) (hg:∀ t,t< n->good X[a+t]! = true) {ix:Array UInt32} {buf:FloatArray} (h1:ix.size=n) (h2:buf.size=n) : C_Dec (λp=>X[a+p]!) n n (decR X a.toUSize n.toUSize 0 ix buf 0 0).ix (decR X a.toUSize n.toUSize 0 ix buf 0 0).buf (decR X a.toUSize n.toUSize 0 ix buf 0 0).c (decR X a.toUSize n.toUSize 0 ix buf 0 0).hi:=by have eo:a.toUSize.toNat=a:=qAc _ א have ek:n.toUSize.toNat=n:=qAc _ א have D:=qHw (X:=X) (o:=a.toUSize) (n:=n.toUSize) (λp=>X[a+p]!) (by rw[ek];omega) (λt ht=>by have h4:=А.mp ht rw[ek] at h4 rw[qAj _ _ hX,qAo _ _ (by rw[eo];omega),eo]) (λt ht=>hg t (by rwa [ek] at ht)) n 0 ix buf 0 0 (by rw[C_z,ek,Л]) ⟨by simp[C_keep],by rw[ek];exact h1,by rw[ek];exact h2,λk p hk=>by simp[C_keep] at hk,rfl,λk hk=>absurd hk (Nat.not_lt_zero _),(by decide)⟩ rw[ek] at D exact D lemma qD2 (xw:Nat->Word) (n:Nat):(C_keep xw n).Nodup∧(∀ t∈C_keep xw n,t< n) ∧ ∀ t,t< n->t∉C_keep xw n->xw t &&& В=0 := ⟨List.nodup_range.filter _,λt ht=>й.mp (List.mem_filter.mp ht).1, λt ht hn=>by by_contra hc exact hn (List.mem_filter.mpr ⟨й.mpr ht,by simpa using hc⟩)⟩ structure C_Row (I:Nat) (X:Array Word) (i:Nat) (ix:Array UInt32) (x:FloatArray) (c:USize) (L:List Nat) (hh:Word):Prop where len:c.toNat=L.length cle:L.length<=I six:ix.size=I sx:x.size=I nd:L.Nodup lt:∀ t∈L,t< I hz:∀ t,t< I->t∉L->X[i*I+t]! &&& В=0 rd:∀ k p:Nat,L[k]?=some p → (ix[k]! : UInt32).toNat=p∧x.data[k]! = dv X[i*I+p]! ok:C_hok (λt=>X[i*I+t]!) I hh section variable {R I O:Nat} {X W b:Array Word} lemma C_G.nm (G:C_G R I O X W b):I.toUSize.toNat=I :=qAc _ (lt_trans G.ni ב) lemma C_G.wj (G:C_G R I O X W b) {j:Nat} {u:USize} (hu:u.toNat=j) (hj:j< O):(u*I.toUSize).toNat=j*I ∧ (u*I.toUSize).toNat+I.toUSize.toNat<=W.size:=by have h1:j*I+I<=O*I:=qA1 hj (le_refl _) have h2:=G.hW have h3:=G.sW have e:(u*I.toUSize).toNat=j*I:=by rw[qDh _ _ (by rw[hu,G.nm];omega),hu,G.nm] exact ⟨e,by rw[e,G.nm];omega⟩ lemma qCc (G:C_G R I O X W b) (Y:Bool) {i j:Nat} (hi:i< R) (hj:j< O) {hv:Word} (Hv:C_Hv W hv) {ix:Array UInt32} {x:FloatArray} {c:USize} {L:List Nat} {hh:Word} (Rw:C_Row I X i ix x c L hh) {u:USize} (hu:u.toNat=j) (hx:c.toNat<=x.size) (hi':c.toNat<=ix.size) (hs:W.size<2^32) (h0:(u*I.toUSize).toNat+I.toUSize.toNat<=W.size) : fin8 I Y X W b i j (go1 x ix W (u*I.toUSize) I.toUSize c 0 hx hi' hs h0 0) (nfOf I) ((I.toUInt64+1).toFloat*dv hv) hh (rdW b u) I.toUSize =C_out I Y X W b i j:=by have e:=qJd hx hi' hs h0 (λp=>dv X[i*I+p]!) (λp=>W[j*I+p]!) (λp _=>by rw[(G.wj hu hj).1]) L 0 0 (by rw[C_z,Л];exact Rw.len.symm) (λk p hk=>by have h1:=Rw.rd k p hk rw[C_z,Л,G.nm] exact ⟨Rw.lt p (ؿ hk),h1⟩) rw[qAj _ _ G.sb,hu] exact qHj G Y hi hj Rw.ok.1 Rw.ok.2.1 Rw.ok.2.2 Hv L Rw.nd Rw.lt Rw.hz e lemma qHl (G:C_G R I O X W b) (Y:Bool) {i:Nat} (hi:i< R) {hv:Word} (Hv:C_Hv W hv) {ix:Array UInt32} {x wd:FloatArray} {c:USize} {L:List Nat} {hh:Word} (Rw:C_Row I X i ix x c L hh) {base:USize} (hb:base.toNat=i*O) (Hd:C_Dw wd W) (d:Nat):∀ (j:Nat) (out:Array Word),j+d=O → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+j) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O+O) (cols1 I O Y X W b i ix x wd (nfOf I) ((I.toUInt64+1).toFloat*dv hv) hh I.toUSize c j base out):=by induction d using Nat.strong_induction_on with | _ d ih => intro j out he H have hN:=G.sO have sW:=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 hsp:∀ k,j+k< O->C_out I Y X W b i (j+k) =C_sp I O Y X W b (i*O+j+k):=λk hk=>by rw[с];exact (qHi I O Y X W b hk).symm by_cases h4:j+4<=O . have hP:=qBe hi (א:j+3< O);have e0:j.toUSize.toNat=j:=qAc _ א;obtain ⟨e1,e2,e3,-⟩:=qEy e0 א;have g0:=(G.wj e0 א).2;have g1:=(G.wj e1 א).2;have g2:=(G.wj e2 א).2;have g3:=(G.wj e3 א).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAo _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qCc G Y hi (א:j< O) Hv Rw e0 h1c h2c sW g0;have o1:=qCc G Y hi (א:j+1< O) Hv Rw e1 h1c h2c sW g1;have o2:=qCc G Y hi (א:j+2< O) Hv Rw e2 h1c h2c sW g2;have o3:=qCc G Y hi (א:j+3< O) Hv Rw e3 h1c h2c sW g3;rw[cols1,У h4];dsimp only;rw[У ⟨h1c,h2c,sW,g0,g1,g2,g3⟩];split;rename_i hd;rw[qI1 _ _ _ _ _ _ _ hd Hd c.toNat 0 _ _ _ _ (Л _)];swap rw[qJn _ _ _ _ _ _ _ (λj hj=>by rw[Rw.len] at hj;have hp:=List.getElem?_eq_getElem hj exact ⟨_,G.gx hi _ (Rw.lt _ (ؿ hp)),(Rw.rd j _ hp).2⟩) c.toNat 0 _ _ _ _ (Л _)] all_goals dsimp only;rw[o0,o1,o2,o3];refine ih (d-4) א (j+4) _ א ?_;rw[show i*O+(j+4)=i*O+j+4 by omega];exact qFb hN H a0 א (hsp 0 א) (hsp 1 א) (hsp 2 א) (hsp 3 א) . by_cases h1:j< O . have hP:=qBe hi h1;have e0:j.toUSize.toNat=j:=qAc _ א;have g0:=(G.wj e0 h1).2;have a0:(base+j.toUSize).toNat=i*O+j:=by rw[qAo _ _ (by rw[hb,e0];omega),hb,e0] have o0:=qCc G Y hi h1 Hv Rw e0 h1c h2c sW g0;rw[cols1,Ъ h4,У h1];dsimp only;rw[У ⟨h1c,h2c,sW,g0⟩];rw[o0];exact ih (d-1) א (j+1) _ א (qAn hN H a0 hP (hsp 0 א)) . rw[cols1,Ъ h4,Ъ h1];have e:j=O :=א;rw[e] at H;exact H lemma qHu (G:C_G R I O X W b) (Y:Bool) {hv:Word} (Hv:C_Hv W hv) {wd:FloatArray} (Hd:C_Dw wd W) (d:Nat):∀ (i:Nat) (ix:Array UInt32) (buf:FloatArray) (out:Array Word),i+d=R->ix.size=I->buf.size=I → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< i*O) out → C_Inv (R*O) (C_sp I O Y X W b) (λq=>q< R*O) (rows1 R I O Y X W b wd (nfOf I) ((I.toUInt64+1).toFloat*dv hv) I.toUSize i ix buf out):=by induction d with | zero => intro i ix buf out he _ _ H rw[rows1,Ъ א] have e:i=R :=א rw[e] at H;exact H | succ d ih => intro i ix buf out he h1 h2 H have hi:i< R :=א have hin:I<2^32:=lt_trans G.ni ב have hN:=G.sO have sX:=G.sX have hX:=G.hX have hP:i*I+I<=R*I:=qA1 hi (le_refl _) have hbs:(i*O).toUSize.toNat=i*O:=by have h3:i*O+O<=R*O:=qA1 hi (le_refl _) exact qAc _ א obtain ⟨d1,d2,d3,d4,d5⟩:=qFu sX א (G.gx hi) h1 h2 obtain ⟨k1,k2,k3⟩:=qD2 (λp=>X[i*I+p]!) I have Rw:=C_Row.mk d1 (qBq _ _) d2 d3 k1 k2 k3 d4 d5 rw[rows1,У hi] dsimp only refine ih (i+1) _ _ _ א d2 d3 ?_ have h5:=qHl G Y hi Hv Rw hbs Hd O 0 out א (by rw[у];exact H) rw[show (i+1)*O=i*O+O by rw[م,Դ]] exact h5 end lemma qJi (xs:FloatArray) (v:Float) (q:Nat) : (xs.push v).data[q]! = if q=xs.size then v else xs.data[q]!:=by obtain ⟨ds⟩:=xs;show (ds.push v)[q]! = if q=ds.size then v else ds[q]!;by_cases hq:q< ds.size . have h3:q<(ds.push v).size:=by simp;omega rw[Э _ q h3,Э ds q hq,Х א,Array.getElem_push_lt] . by_cases e:q=ds.size . subst e;have h3:ds.size<(ds.push v).size:=by simp rw[Э (ds.push v) ds.size h3,з rfl,Array.getElem_push_eq] . have h3:¬ q<(ds.push v).size:=by simp;omega rw[غ _ q h3,غ ds q hq,Х e] lemma qJ9 (l:List UInt32) (a:FloatArray) : (l.foldl (λa w=>a.push (dv w)) a).data=a.data ++ (l.map dv).toArray:=by induction l generalizing a with | nil=>simp | cons w l ih => rw[Ա,ih];simp[FloatArray.push] lemma qFd:linF=linearSpec:=by funext rows inW outW relu X W b;unfold linF;dsimp only;split . rename_i h;obtain ⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11,h12,h13⟩:=h;have Hv:=qAp W h8 h12;have G:C_G rows inW outW X W b:=⟨h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,(qAp X h7 h11).2.2.1,Hv.2.2.1,(qAp b h9 h13).2.2.1⟩;have e:linearSpec rows inW outW relu X W b=Array.ofFn (n:=rows*outW) λq =>C_sp inW outW relu X W b q.val:=rfl;have hnf:(inW.toUInt64+8).toFloat*П 0x3CC0000000000000=nfOf inW:=rfl;rw[e,hnf];have H0:C_Inv (rows*outW) (C_sp inW outW relu X W b) (λq=>q<0*outW) (И (rows*outW) 0) :=⟨by simp,λq _ h=>by rw[Nat.zero_mul] at h;exact absurd h (Nat.not_lt_zero _)⟩;have P0:(pushZ inW.toUSize 0 (Ф inW)).size=inW:=by rw[qCo inW.toUSize inW 0 _ (by rw[G.nm,C_z,Л])];exact Л _ refine qDp (qHu G relu Hv ?_ rows 0 _ _ _ א ג P0 H0) (λq hq=>hq);split;. intro _ q hq;rw[<-Array.foldl_toList,qJ9];simp[hq,FloatArray.emptyWithCapacity] . intro h q hq;have e:0=W.size:=h;omega . rw[qAe];rfl end lemma qD5:linF=linearSpec:=qFd section structure D_Sc where (lo hi:UInt32) def D_scan (A:Array UInt32) (n i:USize) (hn:n.toNat<=A.size) (lo hi:UInt32):D_Sc := if h:i< n then have ht:=А.mp h let v:=A.uget i א &&& В let u:=v-1 D_scan A n (i+1) hn (if u< lo then u else lo) (if hi< v then v else hi) else ⟨lo,hi⟩ termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];omega def D_fill (v:Float) (n k:USize) (a:FloatArray):FloatArray := if h:k< n then D_fill v n (k+1) (a.push v) else a termination_by n.toNat-k.toNat decreasing_by rw[usz k n h];have:=А.mp h;omega def D_copy (X:Array UInt32) (o n t:USize) (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (a:FloatArray):FloatArray := if h:t< n then D_copy X o n (t+1) hs ho (a.push (dv (X.uget (o+t) (wi o t n X ho hs (А.mp h))))) else a termination_by n.toNat-t.toNat decreasing_by rw[usz t n h];have:=А.mp h;omega def D_img (s:ConvShape) (X:Array UInt32) (hs:X.size<2^32) (z:Float) (R r:Nat) (a:FloatArray):FloatArray := if hr:r< R then let hp:=s.h+2*s.pad let yy:=r%hp let o:=((r/hp*s.h+(yy-s.pad))*s.w).toUSize let pu:=s.pad.toUSize if hg:s.pad<=yy∧yy-s.pad< s.h∧o.toNat+s.w.toUSize.toNat<=X.size then D_img s X hs z R (r+1) (D_fill z pu 0 (D_copy X o s.w.toUSize 0 hs hg.2.2 (D_fill z pu 0 a))) else D_img s X hs z R (r+1) (D_fill z (s.w+2*s.pad).toUSize 0 a) else a termination_by R-r def D_off (s:ConvShape) (t:Nat):Nat := (t/(s.kh*s.kw)*(s.h+2*s.pad)+t/s.kw%s.kh)*(s.w+2*s.pad)+t%s.kw structure D_C where (dw relu f4:Bool) (s:ConvShape) (X W b:Array UInt32) (k C ho wo wp:Nat) (step:USize) (hi:UInt32) (xd:FloatArray) (nf:Float) (tab:Array UInt32) structure D_P where (g:D_C) (wd:FloatArray) (off:Array UInt32) (e l1 l4:USize) (e0 bv ze:Float) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:g.xd.size<2^32) (h1:∀ b:USize,b< l1->∀ j (h:j< off.size),b.toNat+off[j].toNat+0< g.xd.size) (h4:∀ b:USize,b< l4->∀ j (h:j< off.size),b.toNat+off[j].toNat+3< g.xd.size) (hz:ze=0) lemma qHz {n d:Nat} (b:USize) (u:UInt32) (hs:n<2^32) (h:b.toNat+u.toNat+d< n):(b+u.toUSize).toNat+d< n:=by rw[qAo _ _ (by rw[н];omega),н];exact h lemma qGb {n:Nat} (p:USize) (k:Nat) (hs:n<2^32) (h:p.toNat+k< n) : (p+USize.ofNat k).toNat< n:=by rw[qAo _ _ (by rw[uk k א];omega),uk k א];exact h def D_go4R (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+3< xd.size) (a0 a1 a2 a3:Float) (r:FloatArray):FloatArray := if h:i< e then have ht:=А.mp h let p:=b+(off.uget i א).toUSize have hp:p.toNat+3< xd.size:=qHz _ _ hs (hb i.toNat א) let v:=wd.uget i א D_go4R xd wd off b e (i+1) he hw hs hb (a0+xd.uget p א*v) (a1+xd.uget (p+USize.ofNat 1) (qGb p 1 hs א)*v) (a2+xd.uget (p+USize.ofNat 2) (qGb p 2 hs א)*v) (a3+xd.uget (p+USize.ofNat 3) (qGb p 3 hs א)*v) r else wrF (wrF (wrF (wrF r 0 a0) 1 a1) 2 a2) 3 a3 termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega def D_goT (xd wd:FloatArray) (off:Array UInt32) (b e i:USize) (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+0< xd.size) (a t:Float):F1 := if h:i< e then have ht:=А.mp h let q:=xd.uget (b+(off.uget i א).toUSize) (qHz (d:=0) _ _ hs (hb i.toNat א))*wd.uget i א D_goT xd wd off b e (i+1) he hw hs hb (a+q) (t+q.abs) else ⟨a,t⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];omega @[noinline] def D_slow (g:D_C) (o y x:Nat):UInt32 := act g.relu (dotFn (g.k+1) (λt=>if t< g.k then windowAt g.s g.X y x ((if g.dw then o*g.k else 0)+t) else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) @[inline] def D_finT (c:D_P) (o y x:Nat) (b:USize):UInt32 := if hb:b< c.l1 then let q:=D_goT c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h1 b hb) c.ze c.ze let r:=ziv (q.s+c.bv) (c.g.nf*(q.t+c.bv.abs)) if r == Е then D_slow c.g o y x else act c.g.relu r else D_slow c.g o y x @[inline] def D_fin (c:D_P) (o y x:Nat) (b:USize) (a:Float):UInt32 := let r:=ziv (a+c.bv) c.e0 if r == Е then D_finT c o y x b else act c.g.relu r def D_cols (c:D_P) (o y x:Nat) (b q:USize) (rr:FloatArray) (out:Array UInt32) : Array UInt32 := if h4:x+4<=c.g.wo∧c.g.f4=true∧b< c.l4 then let r:=D_go4R c.g.xd c.wd c.off b c.e 0 c.he c.hw c.hs (c.h4 b h4.2.2) c.ze c.ze c.ze c.ze rr let o0:=D_fin c o y x b (rdF r 0) let o1:=D_fin c o y (x+1) (b+1) (rdF r 1) let o2:=D_fin c o y (x+2) (b+2) (rdF r 2) let o3:=D_fin c o y (x+3) (b+3) (rdF r 3) let out:=wrW out q o0 let out:=wrW out (q+1) o1 let out:=wrW out (q+2) o2 let out:=wrW out (q+3) o3 D_cols c o y (x+4) (b+4) (q+4) r out else if x< c.g.wo then D_cols c o y (x+1) (b+c.g.step) (q+1) rr (wrW out q (D_finT c o y x b)) else out termination_by c.g.wo-x def D_rows (c:D_P) (o y:Nat) (q:USize) (out:Array UInt32):Array UInt32 := if y< c.g.ho then D_rows c o (y+1) (q+c.g.wo.toUSize) (D_cols c o y 0 (y.toUSize*c.g.step*c.g.wp.toUSize) q (pushZ 4 0 (Ф 4)) out) else out termination_by c.g.ho-y structure D_T where (off:Array UInt32) (a:Float) def D_tap (g:D_C) (o:Nat) (ix:Array UInt32) (buf:FloatArray) (e i:USize) (off:Array UInt32) (a:Float):D_T := if h:i< e then D_tap g o ix buf e (i+1) (wrW off i ((if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0) + rdW g.tab (rdW ix i).toUSize)) (a+(rdF buf i).abs) else ⟨off,a⟩ termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega lemma qC3 (a:Array UInt32) (j:Nat) (h:j< a.size) : a[j].toNat<=a.foldl (λm v=>max m v.toNat) 0 := Array.foldl_induction (λn m=>∀ k (h:k< a.size),k< n->a[k].toNat<=m) nofun (λi b H k h hk=>by by_cases e:k=i . subst e;exact le_max_right .. exact (H k h א).trans (le_max_left ..)) j h h lemma qG0 {n m:Nat} (d:Nat) (off:Array UInt32) (hs:n<2^32) (hm:∀ j (h:j< off.size),off[j].toNat<=m) (b:USize) (hb:b<(n-(m+d)).toUSize) (j:Nat) (h:j< off.size):b.toNat+off[j].toNat+d< n:=by have h1:=А.1 hb;rw[qAc _ א] at h1;have:=hm j h;omega def D_outs (g:D_C) (hs:g.xd.size<2^32) (o:Nat) (q:USize) (ix off:Array UInt32) (buf:FloatArray) (out:Array UInt32):Array UInt32 := if o< g.C then let r:=decR g.W (o*g.k).toUSize g.k.toUSize 0 ix buf 0 0 let t:=D_tap g o r.ix r.buf r.c 0 off 0 let m:=t.off.foldl (λm v=>max m v.toNat) 0 if h:r.c.toNat<=t.off.size∧r.c.toNat<=r.buf.size then let c:D_P:=⟨g,r.buf,t.off,r.c,(g.xd.size-(m+0)).toUSize, (g.xd.size-(m+3)).toUSize,g.nf*(dv g.hi*t.a+(dv (rdW g.b o.toUSize)).abs), dv (rdW g.b o.toUSize),0,h.1,h.2,hs, qG0 0 t.off hs (qC3 t.off),qG0 3 t.off hs (qC3 t.off),rfl⟩ D_outs g hs (o+1) (q+(g.ho*g.wo).toUSize) r.ix t.off r.buf (D_rows c o 0 q out) else out else out termination_by g.C-o def D_F (dw:Bool) (s:ConvShape) (relu:Bool) (X W b:Array UInt32) (k C:Nat) : Array UInt32 := let hp:=s.h+2*s.pad let wp:=s.w+2*s.pad let rx:=D_scan X X.usize 0 (qAf X) Е 0 if hg:X.size<2^32∧0x007FFFFF<=rx.lo∧rx.hi<Ц∧X.size=s.cin*s.h*s.w ∧ W.size=C*k∧b.size=C∧0< s.stride∧s.stride<2^32∧0< k∧k<2^20 ∧ s.kh<=hp∧s.kw<=wp∧hp<2^32∧wp<2^32∧W.size<2^32∧b.size<2^32 ∧ C*(s.ho*s.wo)<2^32∧s.cin*hp*wp<2^32 ∧ scan W W.usize 0 (qAf W) Е 0≠Е ∧ scan b b.usize 0 (qAf b) Е 0≠Е then let tight:=(rx.hi >>> (23:UInt32))-((rx.lo+1) >>> (23:UInt32)) > 99 let R:=s.cin*hp let xd:=D_img s X hg.1 (dv 0) R 0 (Ф (R*wp)) if hs:xd.size<2^32 then let g:D_C:=⟨dw,relu,s.stride == 1 && !tight,s,X,W,b,k,C,s.ho,s.wo,wp, s.stride.toUSize,rx.hi,xd,(k.toUInt64+8).toFloat*П 0x3CC0000000000000, Array.ofFn (n:=k) λt=>(D_off s t.val).toUInt32⟩ D_outs g hs 0 0 (И k 0) (И k 0) (pushZ k.toUSize 0 (Ф k)) (И (C*(s.ho*s.wo)) 0) else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b else if dw then depthwiseWith dotFn s relu X W b else conv2dWith dotFn s relu X W b def convF (s:ConvShape) (relu:Bool) (X W b:Array UInt32):Array UInt32 := D_F false s relu X W b (s.cin*s.kh*s.kw) s.cout def depthF (s:ConvShape) (relu:Bool) (X W b:Array UInt32):Array UInt32 := D_F true s relu X W b (s.kh*s.kw) s.cin def D_px (s:ConvShape) (X:Array Word) (c yy xx:Nat):Word := if yy< s.pad∨xx< s.pad∨yy-s.pad ≥ s.h∨xx-s.pad ≥ s.w then 0 else X[(c*s.h+(yy-s.pad))*s.w+(xx-s.pad)]! def D_Fx (s:ConvShape) (X:Array Word) (q:Nat):Float := dv (D_px s X (q/(s.w+2*s.pad)/(s.h+2*s.pad)) (q/(s.w+2*s.pad)%(s.h+2*s.pad)) (q%(s.w+2*s.pad))) def D_Pre (F:Nat->Float) (m:Nat) (a:FloatArray):Prop := a.size=m∧∀ q,q< m->a.data[q]! = F q lemma qCi {F:Nat->Float} {m:Nat} {a:FloatArray} {v:Float} (h:D_Pre F m a) (hv:v=F m):D_Pre F (m+1) (a.push v):=by refine ⟨by rw[qBz,h.1],λq hq=>?_⟩;rw[qJi,h.1];split<;>rename_i e;. rw[e,hv] exact h.2 q א lemma qI3 (m d:Nat):m+(d+1)=m+1+d :=א lemma qCx {i e:USize} (h:e.toNat<=i.toNat):¬i< e :=mt А.1 (Nat.not_lt.2 h) lemma qB9 {i e:USize} {d:Nat} (h:i.toNat+(d+1)=e.toNat) : i< e∧(i+1).toNat+d=e.toNat:=by have t:=А.2 (א:i.toNat< e.toNat);exact ⟨t,by rw[usz i e t];omega⟩ lemma qEm {F:Nat->Float} {v:Float} {n:USize} (d:Nat) : ∀ (k:USize) (m:Nat) (a:FloatArray),k.toNat+d=n.toNat->D_Pre F m a → (∀ i,m<=i->i< m+d->F i=v)->D_Pre F (m+d) (D_fill v n k a):=by induction' d with d ih<;>intro k m a he H hv . rw[D_fill,Ъ (qCx he.ge)];exact H obtain ⟨ht,hu⟩:=qB9 he rw[D_fill,У ht,qI3] exact ih (k+1) (m+1) _ hu (qCi H (hv m א א).symm) λi h1 h2=>hv i א א lemma qEg {F:Nat->Float} {X:Array UInt32} {o n:USize} (hs:X.size<2^32) (ho:o.toNat+n.toNat<=X.size) (d:Nat) : ∀ (t:USize) (m:Nat) (a:FloatArray),t.toNat+d=n.toNat->D_Pre F m a → (∀ j,j< d->F (m+j)=dv X[o.toNat+t.toNat+j]!) → D_Pre F (m+d) (D_copy X o n t hs ho a):=by induction' d with d ih<;>intro t m a he H hv . rw[D_copy,Ъ (qCx he.ge)];exact H obtain ⟨ht,hu⟩:=qB9 he rw[D_copy,У ht,qI3] refine ih (t+1) (m+1) _ hu (qCi H ?_) λj hj=>?_ . rw[qDm,qAo _ _ א];exact (hv 0 א).symm simpa only [usz t n ht,Nat.add_assoc,Nat.add_comm 1] using hv (j+1) א lemma qCp {c:Nat} (a:Nat) {b:Nat} (h:b< c):(a*c+b)/c=a∧(a*c+b)%c=b:=by rw[ж a c,ـ א,ف,ш h,З h];exact ⟨rfl,rfl⟩ lemma qBr (s:ConvShape) (X:Array Word) (r:Nat) {i:Nat} (hi:i< s.w+2*s.pad) : D_Fx s X (r*(s.w+2*s.pad)+i) =dv (D_px s X (r/(s.h+2*s.pad)) (r%(s.h+2*s.pad)) i):=by simp only[D_Fx,qCp r hi] lemma qF7 {F:Nat->Float} {v:Float} {n:USize} {m d:Nat} {a:FloatArray} (hn:n.toNat=d) (H:D_Pre F m a) (hv:∀ u,u< d->F (m+u)=v):D_Pre F (m+d) (D_fill v n 0 a) := qEm d 0 m a (by rw[hn];exact Л d) H λi h1 h2=>by obtain ⟨u,rfl⟩:=Nat.exists_eq_add_of_le h1 exact hv u א lemma qKj (m p w:Nat):m+(w+2*p)=m+p+w+p :=א lemma qFw (s:ConvShape) (X:Array UInt32) (hs:X.size<2^32) (hX:X.size=s.cin*s.h*s.w) (hh:s.h+2*s.pad<2^32) (hw:s.w+2*s.pad<2^32) {R:Nat} (hR:R=s.cin*(s.h+2*s.pad)) (d:Nat) : ∀ (r:Nat) (a:FloatArray),r+d=R → D_Pre (D_Fx s X) (r*(s.w+2*s.pad)) a → D_Pre (D_Fx s X) (R*(s.w+2*s.pad)) (D_img s X hs (dv 0) R r a):=by induction' d with d ih<;>intro r a he H . rw[D_img,Ъ א,<-he];exact H have hr:r< R :=א have hc:=м (hr.trans_eq (hR.trans (ж _ _))) have ep:=qAc s.pad א have ew:=qAc s.w א have hO:=λg:r%(s.h+2*s.pad)-s.pad< s.h=>qA1 (qBe hc g) (Nat.le_refl s.w) rw[D_img,У hr] dsimp only split<;>rename_i g . obtain ⟨g1,g2,g3⟩:=g;have:=hO g2;apply ih (r+1) _ א;rw[ػ,qKj];refine qF7 ep (qEg hs g3 s.w 0 _ _ (by rw[ew];exact Л _) (qF7 ep H λu hu=>?_) λj hj=>?_) λu hu=>?_;. rw[qBr _ _ _ א,D_px,if_pos א] . rw[с,qBr _ _ _ א,D_px,if_neg א,Nat.add_sub_cancel_left,qAc _ א];rfl . rw[с,с,qBr _ _ _ א,D_px,if_pos א] apply ih (r+1) _ א rw[ػ] exact qF7 (qAc _ hw) H λu hu=>by rw[qBr _ _ _ hu,D_px,if_pos] by_contra n have:=hO א exact g ⟨by omega,by omega,by rw[qAc _ א,ew,hX];exact this⟩ def D_t0 (g:D_C) (o:Nat):Nat:=if g.dw then o*g.k else 0 def D_xw (g:D_C) (o y x t:Nat):Word:=windowAt g.s g.X y x (D_t0 g o+t) def D_out (g:D_C) (o y x:Nat):Word := act g.relu (dotOf (g.k+1) (λt=>if t< g.k then D_xw g o y x t else oneW) (λt=>if t< g.k then g.W[o*g.k+t]! else g.b[o]!)) lemma qC2 (g:D_C) (o y x:Nat):D_slow g o y x=D_out g o y x:=by unfold D_slow D_out D_xw D_t0;rw[qAe] def D_B (g:D_C) (y x:Nat):Nat:=y*g.s.stride*g.wp+x*g.s.stride structure D_Ok (g:D_C):Prop where hk:g.k=if g.dw then g.s.kh*g.s.kw else g.s.cin*g.s.kh*g.s.kw hC:g.C=if g.dw then g.s.cin else g.s.cout ho:g.ho=g.s.ho wo:g.wo=g.s.wo wp:g.wp=g.s.w+2*g.s.pad st:g.step.toNat=g.s.stride nf:g.nf=nfOf g.k f4:g.f4=true->g.s.stride=1 p0:0< g.s.stride pk:0< g.k nk:g.k<2^20 kh:g.s.kh<=g.s.h+2*g.s.pad kw:g.s.kw<=g.s.w+2*g.s.pad xd:D_Pre (D_Fx g.s g.X) (g.s.cin*(g.s.h+2*g.s.pad)*(g.s.w+2*g.s.pad)) g.xd xs:g.xd.size<2^32 Hx:C_Hv g.X g.hi HW:∀ q:Nat,good g.W[q]! = true Hb:∀ q:Nat,good g.b[q]! = true sW:g.W.size=g.C*g.k sW2:g.W.size<2^32 sb:g.b.size<2^32 sN:g.C*(g.ho*g.wo)<2^32 wp2:g.wp<2^32 tb:g.tab=Array.ofFn (n:=g.k) λt=>(D_off g.s t.val).toUInt32 lemma qE2 {A:Array Word} {h:Word}:good (0:Word)=true∧(0:Word) &&& В<=h :=⟨rfl,М.2 (Nat.zero_le _)⟩ lemma qGn {A:Array Word} (H:∀ k:Nat,k< A.size->good A[k]! = true) (q:Nat) : good A[q]! = true:=by by_cases h:q< A.size;. exact H q h rw[غ A q h];rfl lemma qCv {s:ConvShape} {X:Array Word} {hi:Word} (H:C_Hv X hi) (c yy xx:Nat) : good (D_px s X c yy xx)=true∧D_px s X c yy xx &&& В<=hi:=by unfold D_px;split;. exact qE2 (A:=X) generalize (c*s.h+(yy-s.pad))*s.w+(xx-s.pad)=n;by_cases h:n< X.size;. exact ⟨H.2.2.1 n h,H.2.2.2 n h⟩ rw[غ X n h];exact qE2 (A:=X) lemma qH6 {g:D_C} (ok:D_Ok g) {o t:Nat} (hC:o< g.C) (ht:t< g.k) : D_t0 g o+t< g.s.cin*(g.s.kh*g.s.kw):=by have h1:=ok.hk;have h2:=ok.hC;unfold D_t0;cases hd:g.dw<;>simp[hd] at h1 h2 ⊢;. rwa [<-Nat.mul_assoc,<-h1] exact h1 ▸ qBe (h2 ▸ hC) (h1 ▸ ht) lemma qH8 {n a b c:Nat} (h:n< a*(b*c)):0< b∧0< c:=by constructor<;>refine ص λe=>?_<;>simp[e] at h lemma qGo {a b st n m:Nat} (h0:0< st) (hk:b<=a) (hy:n<(a-b)/st+1) (hm:m< b) : n*st+m< a:=by have:=Т st (show n<=(a-b)/st by omega);have:=Nat.div_mul_le_self (a-b) st;omega lemma qKl (a p w v u y x:Nat) : v*w+u+((a*p+y)*w+x)=(a*p+(v+y))*w+(u+x):=by ring lemma qCm {g:D_C} (ok:D_Ok g) {o y x t:Nat} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo) (ht:t< g.k) : D_B g y x+D_off g.s (D_t0 g o+t)< g.xd.size ∧ g.xd.data[D_B g y x+D_off g.s (D_t0 g o+t)]! = dv (D_xw g o y x t):=by have hb:=qH6 ok hC ht have hk:=qH8 hb unfold D_xw generalize D_t0 g o+t=u at hb ⊢ have hyy:=qGo ok.p0 ok.kh (hy.trans_eq ok.ho) (э (u/g.s.kw) hk.1) have hxx:=qGo ok.p0 ok.kw (hx.trans_eq ok.wo) (э u hk.2) have hq:=qBe (qBe (м (hb.trans_eq (ж _ _))) hyy) hxx rw[D_B,D_off,ok.wp,qKl] refine ⟨ok.xd.1 ▸ hq,?_⟩ rw[ok.xd.2 _ hq] simp only[D_Fx,qCp _ hxx,qCp _ hyy] rfl lemma qCr {u v:USize} {a b:Nat} (hu:u.toNat=a) (hv:v.toNat=b) (h:a+b<2^32) : (u+v).toNat=a+b:=by rw[qAo _ _ א,hu,hv] lemma qH3 {n d:Nat} {b i:USize} {off:Array UInt32} (hs:n<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+d< n) (hi:i.toNat< off.size) : (b+(off.uget i hi).toUSize).toNat=b.toNat+(off[i.toNat]! : UInt32).toNat ∧ b.toNat+(off[i.toNat]! : UInt32).toNat+d< n:=by have h:b.toNat+(off.uget i hi).toNat+d< n:=hb _ hi rw[<-qDm _ _ hi] exact ⟨qCr rfl (н _) א,h⟩ lemma qGc {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+0< xd.size) (xf wf:Nat->Float) (L:List Nat):∀ (i:USize) (a t:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat]! = xf p ∧ wd.data[i.toNat+j]! = wf p) → D_goT xd wd off b e i he hw hs hb a t=L.foldl (λ(acc:F1) p =>let q:=xf p*wf p F1.mk (acc.s+q) (acc.t+q.abs)) ⟨a,t⟩:=by induction' L with p L ih<;>intro i a t h0 H . rw[D_goT,Ъ (qCx h0.ge)];rfl rw[List.length_cons] at h0 obtain ⟨ht,hu⟩:=qB9 h0 have h1:=H 0 p rfl rw[у] at h1 rw[D_goT,У ht] simp only[qCn] rw[(qH3 hs hb א).1,h1.1,h1.2,ih (i+1) _ _ hu,@List.foldl_cons] intro j q hq rw[usz i e ht,Nat.add_right_comm,с] exact H (j+1) q hq lemma qG5 (a:FloatArray) (i:USize) (hs:a.size<2^32) (hi:i.toNat< a.size) : rdF a i=a.data[i.toNat]!:=by rw[rdF,У (by rw[А,FloatArray.usize,qAc _ hs];exact hi),qCn] def D_R4 (R:FloatArray) (F:F4):Prop := R.size=4∧rdF R 0=F.a∧rdF R 1=F.b∧rdF R 2=F.c∧rdF R 3=F.d lemma qJo (r:FloatArray) (hr:r.size=4) (a b c d:Float) : D_R4 (wrF (wrF (wrF (wrF r 0 a) 1 b) 2 c) 3 d) ⟨a,b,c,d⟩:=by have u2:(2:USize).toNat=2:=uk 2 ב;have u3:(3:USize).toNat=3:=uk 3 ב;simp[D_R4,qG5,qC5,qCg,hr,u2,u3] lemma qGj {xd wd:FloatArray} {off:Array UInt32} {b e:USize} (he:e.toNat<=off.size) (hw:e.toNat<=wd.size) (hs:xd.size<2^32) (hb:∀ j (h:j< off.size),b.toNat+off[j].toNat+3< xd.size) (r:FloatArray) (hr:r.size=4) (xf:Nat->Nat->Float) (wf:Nat->Float) (L:List Nat):∀ (i:USize) (a0 a1 a2 a3:Float),i.toNat+L.length=e.toNat → (∀ j p:Nat,L[j]?=some p->wd.data[i.toNat+j]! = wf p∧∀ d,d<4 → xd.data[b.toNat+(off[i.toNat+j]! : UInt32).toNat+d]! = xf d p) → D_R4 (D_go4R xd wd off b e i he hw hs hb a0 a1 a2 a3 r) ⟨L.foldl (λacc p=>acc+xf 0 p*wf p) a0,L.foldl (λacc p=>acc+xf 1 p*wf p) a1,L.foldl (λacc p=>acc+xf 2 p*wf p) a2,L.foldl (λacc p=>acc+xf 3 p*wf p) a3⟩:=by induction' L with p L ih<;>intro i a0 a1 a2 a3 h0 H . rw[D_go4R,Ъ (qCx h0.ge)];exact qJo r hr _ _ _ _ rw[List.length_cons] at h0 obtain ⟨ht,hu⟩:=qB9 h0 obtain ⟨h1,h2⟩:=H 0 p rfl rw[у] at h1 h2 obtain ⟨ep,hbd⟩:=qH3 hs hb (א:i.toNat< off.size) have U:=λ(k:Nat) (hk:k<4)=>(qCr ep (uk k א) א).symm ▸ h2 k hk rw[D_go4R,У ht] simp only[qCn] rw[ep,h1,U 1 א,U 2 א,U 3 א,<-у (_+_),h2 0 א] refine ih (i+1) _ _ _ _ hu λj q hq=>?_ rw[usz i e ht,Nat.add_right_comm,с] exact H (j+1) q hq structure D_PO (c:D_P) (o:Nat) (L:List Nat):Prop where ok:D_Ok c.g hC:o< c.g.C len:c.e.toNat=L.length nd:L.Nodup lt:∀ t∈L,t< c.g.k hz:∀ t,t< c.g.k->t∉L->c.g.W[o*c.g.k+t]! &&& В=0 rd:∀ j p:Nat,L[j]?=some p → (c.off[j]! : UInt32).toNat=D_off c.g.s (D_t0 c.g o+p) ∧ c.wd.data[j]! = dv c.g.W[o*c.g.k+p]! e0:c.e0=nfOf c.g.k*(dv c.g.hi*L.foldl (λacc t=>acc+(dv c.g.W[o*c.g.k+t]!).abs) 0 + (dv c.g.b[o]!).abs) bv:c.bv=dv c.g.b[o]! lemma qC0 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=D_B c.g y x) : D_finT c o y x b=D_out c.g o y x:=by unfold D_finT;split<;>rename_i hl;swap;. exact qC2 .. have e:=qGc c.he c.hw c.hs (c.h1 b hl) (dv ∘ D_xw c.g o y x) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 ((Л _).trans h.len.symm) λj p hj=>by have h1:=h.rd j p hj;rw[C_z,Л,h1.1,hb];exact ⟨(qCm h.ok h.hC hy hx (h.lt p (ؿ hj))).2,h1.2⟩ simp only[c.hz,e,h.bv,h.ok.nf];split<;>rename_i hz;. exact qC2 .. exact congrArg (act _) (qB4 c.g.k h.ok.nk _ _ _ (λt _=>(qCv h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) L h.nd h.lt (λt a b=>.inr (h.hz t a b)) _ _ ⟨rfl,rfl⟩ (by simpa using hz)) lemma qGh {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y x:Nat} {b:USize} (hy:y< c.g.ho) (hx:x< c.g.wo) (hb:b.toNat=D_B c.g y x) {a:Float} (ha:a=L.foldl (λacc t=>acc+dv (D_xw c.g o y x t)*dv c.g.W[o*c.g.k+t]!) 0) : D_fin c o y x b a=D_out c.g o y x:=by subst ha;simp only[D_fin,h.bv,h.e0];split<;>rename_i hz;. exact qC0 h hy hx hb exact congrArg (act _) (qDe c.g.k h.ok.nk _ _ _ _ (λt _=>(qCv h.ok.Hx _ _ _).1) (λt _=>h.ok.HW _) (h.ok.Hb _) h.ok.Hx.1 (λt _=>(qCv h.ok.Hx _ _ _).2) h.ok.Hx.2.1 L h.nd h.lt (λt a b=>.inr (h.hz t a b)) _ _ ⟨rfl,rfl⟩ (by simpa using hz)) def D_sp (g:D_C) (q:Nat):Word := D_out g (q/(g.ho*g.wo)) (q/g.wo%g.ho) (q%g.wo) lemma qD6 (g:D_C) {o y x:Nat} (hy:y< g.ho) (hx:x< g.wo) : D_out g o y x=D_sp g ((o*g.ho+y)*g.wo+x):=by simp only[D_sp,Nat.mul_comm g.ho g.wo,<-Nat.div_div_eq_div_mul,qCp _ hx,qCp _ hy] lemma qEi {g:D_C} (ok:D_Ok g) {o y x:Nat} (hC:o< g.C) (hy:y< g.ho) (hx:x< g.wo):D_B g y x<2^32:=by have h:=(qCm ok hC hy hx ok.pk).1;have:=ok.xs;omega lemma qIc (g:D_C) (y x k:Nat):D_B g y (x+k)=D_B g y x+k*g.s.stride:=by unfold D_B;rw[م];omega lemma qE0 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) {y Q:Nat} (hy:y< c.g.ho) (hQ:Q=(o*c.g.ho+y)*c.g.wo) (hN:Q+c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:Nat) : ∀ (x:Nat) (b q:USize) (rr:FloatArray) (out:Array Word),x+d=c.g.wo → (x< c.g.wo->b.toNat=D_B c.g y x)->q.toNat=Q+x->rr.size=4 → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+x) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p< Q+c.g.wo) (D_cols c o y x b q rr out):=by subst hQ induction' d using Nat.strong_induction_on with d ih intro x b q rr out he hb hq hrr H have sN:=h.ok.sN rw[D_cols] split<;>rename_i h4 . have hb0:=hb א;have hB:=λk=>(qIc c.g y x k).trans (by rw[h.ok.f4 h4.2.1,Nat.mul_one]);obtain ⟨a1,a2,a3,a4⟩:=qEy hb0 (hB 3 ▸ qEi h.ok h.hC hy (א:x+3< _)) obtain ⟨R0,R1,R2,R3,R4⟩:=qGj c.he c.hw c.hs (c.h4 b h4.2.2) rr hrr (λk p=>dv (D_xw c.g o y (x+k) p)) (λp=>dv c.g.W[o*c.g.k+p]!) L 0 0 0 0 0 ((Л _).trans h.len.symm) λj p hj=>by have h1:=h.rd j p hj;rw[C_z,Л,h1.1];refine ⟨h1.2,λk hk=>?_⟩;rw[Nat.add_right_comm,hb0,<-hB];exact (qCm h.ok h.hC hy א (h.lt p (ؿ hj))).2 have W:=λk (hk:x+k< c.g.wo) {u:USize} {a:Float} (hu:u.toNat=D_B c.g y x+k) ha =>(qGh h hy hk (a:=a) (hu.trans (hB k).symm) ha).trans (qD6 c.g hy hk);simp only[c.hz];exact ih (d-4) א (x+4) _ _ _ _ א (λhx=>by rw[hB];exact a4 (hB 4 ▸ qEi h.ok h.hC hy hx)) ((qEy hq א).2.2.2 א) R0 (qFb sN H hq א (W 0 א hb0 R1) (W 1 א a1 R2) (W 2 א a2 R3) (W 3 א a3 R4)) split<;>rename_i h1 . have hb0:=hb h1;refine ih (d-1) א (x+1) _ _ _ _ א (λhx=>?_) ((qCr hq ش א).trans rfl) hrr (by rw[qC0 h hy h1 hb0];exact qAn sN H hq א (qD6 c.g hy h1));have:=qEi h.ok h.hC hy hx;rw[qIc,Դ] at this ⊢;exact qCr hb0 h.ok.st this rwa [(א:x=c.g.wo)] at H lemma qIy {u v:USize} {a b:Nat} (hu:u.toNat=a) (hv:v.toNat=b) (h:a*b<2^32) : (u*v).toNat=a*b:=by rw[qDh _ _ (by rw[hu,hv];exact h),hu,hv] lemma qE9 {c:D_P} {o:Nat} {L:List Nat} (h:D_PO c o L) (hN:(o*c.g.ho+c.g.ho)*c.g.wo<=c.g.C*(c.g.ho*c.g.wo)) (d:Nat) : ∀ (y:Nat) (q:USize) (out:Array Word),y+d=c.g.ho → q.toNat=(o*c.g.ho+y)*c.g.wo → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+y)*c.g.wo) out → C_Inv (c.g.C*(c.g.ho*c.g.wo)) (D_sp c.g) (λp=>p<(o*c.g.ho+c.g.ho)*c.g.wo) (D_rows c o y q out):=by induction' d with d ih<;>intro y q out he hq H . rw[D_rows,Х א];rwa [(א:y=c.g.ho)] at H have hy:y< c.g.ho :=א have sN:=h.ok.sN have hA:=qA1 (א:o*c.g.ho+y< o*c.g.ho+c.g.ho) (Nat.le_refl c.g.wo) have eW:=qAc c.g.wo א have bB:=qEi h.ok h.hC hy (x:=0) (h.ok.wo ▸ Nat.succ_pos _) have hwp:0< c.g.wp:=h.ok.wp ▸ (qH8 (qH6 h.ok h.hC h.ok.pk)).2.trans_le h.ok.kw simp[D_B] at bB have:=ع (y*c.g.s.stride) hwp have:=ع y h.ok.p0 rw[D_rows,з hy] refine ih (y+1) _ _ א ?_ ?_<;>rw[<-с,Nat.add_one_mul] . exact qCr hq eW א exact qE0 h hy rfl א c.g.wo 0 _ _ _ _ (Л _) (λ_ =>(qIy (qIy (qAc y א) h.ok.st א) (qAc _ h.ok.wp2) bB).trans (by simp[D_B])) hq (qCo 4 4 0 _ ((Л 4).trans (uk 4 ב).symm)) H lemma qHp (s:ConvShape) (o:Nat) {p:Nat} (hp:p< s.kh*s.kw) : D_off s (o*(s.kh*s.kw)+p) =o*((s.h+2*s.pad)*(s.w+2*s.pad))+D_off s p:=by unfold D_off rw[(qCp o hp).1,ш hp,<-Nat.mul_assoc,Nat.add_comm (o*s.kh*s.kw),Nat.add_mul_div_right _ _ (Nat.pos_of_mul_pos_left (Nat.zero_lt_of_lt hp)),Nat.add_mul_mod_self_right,Nat.add_mul_mod_self_right] ring lemma qJr (a b:Nat):a.toUInt32+b.toUInt32=(a+b).toUInt32 :=(UInt32.ofNat_add a b).symm lemma qIb {g:D_C} (ok:D_Ok g) (o:Nat) {p:Nat} (hp:p< g.k) {u:UInt32} (hu:u.toNat=p) : (if g.dw then (o*((g.s.h+2*g.s.pad)*g.wp)).toUInt32 else 0)+rdW g.tab u.toUSize =(D_off g.s (D_t0 g o+p)).toUInt32:=by have h2:=ok.nk rw[qAj _ _ (by simp[ok.tb];omega),н,hu,ok.tb,Э _ p (by simpa using hp),Array.getElem_ofFn] have h1:=ok.hk unfold D_t0 cases hd:g.dw . simp simp only[hd,if_true] at h1 ⊢ rw[h1,qHp g.s o (h1 ▸ hp),ok.wp,qJr] def D_tv (g:D_C) (o:Nat) (L:List Nat) (j:Nat):Word := (D_off g.s (D_t0 g o+L[j]?.getD 0)).toUInt32 lemma qGa {g:D_C} (ok:D_Ok g) (o:Nat) {ix:Array UInt32} {buf:FloatArray} {e:USize} {L:List Nat} {N:Nat} (wf:Nat->Float) (hk:∀ p∈L,p< g.k) (hN:N<2^32) (hix:ix.size<2^32) (hbuf:buf.size<2^32) (hL:e.toNat=L.length) (he:e.toNat<=buf.size) (heN:e.toNat<=N) (hr:∀ j p:Nat,L[j]?=some p->(ix[j]! : UInt32).toNat=p∧buf.data[j]! = wf p) (d:Nat) : ∀ (i:USize) (off:Array UInt32) (a:Float),i.toNat+d=e.toNat → C_Inv N (D_tv g o L) (λq=>q< i.toNat) off → C_Inv N (D_tv g o L) (λq=>q< e.toNat) (D_tap g o ix buf e i off a).off ∧ (D_tap g o ix buf e i off a).a =(L.drop i.toNat).foldl (λacc t=>acc+(wf t).abs) a:=by induction' d with d ih<;>intro i off a h0 H . rw[D_tap,Ъ (qCx h0.ge),(א:i.toNat=e.toNat)] at *;exact ⟨H,by rw[List.drop_eq_nil_of_le א];rfl⟩ obtain ⟨ht,hu⟩:=qB9 h0 have hl:i.toNat< L.length :=א have hp:=List.getElem?_eq_getElem hl obtain ⟨h1,h2⟩:=hr _ _ hp rw[D_tap,У ht,qAj _ _ hix,qIb ok o (hk _ (List.getElem_mem hl)) h1,qG5 _ _ hbuf א,h2,List.drop_eq_getElem_cons hl,@List.foldl_cons,<-usz i e ht] exact ih (i+1) _ _ hu (by rw[usz i e ht];exact qAn hN H rfl א (by rw[D_tv,hp];rfl)) lemma qE3 {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) (d:Nat) : ∀ (o:Nat) (q:USize) (ix off:Array UInt32) (buf:FloatArray) (out:Array Word),o+d=g.C->q.toNat=o*(g.ho*g.wo)->ix.size=g.k->off.size=g.k->buf.size=g.k → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< o*(g.ho*g.wo)) out → C_Inv (g.C*(g.ho*g.wo)) (D_sp g) (λp=>p< g.C*(g.ho*g.wo)) (D_outs g hs o q ix off buf out):=by induction' d with d ih<;>intro o q ix off buf out he hq h1 h2 h3 H . rw[D_outs,Х א];rwa [(א:o=g.C)] at H have hC:o< g.C :=א have hkn:=ok.nk have sW2:=ok.sW2 have sN:=ok.sN have hP:=(qA1 hC (Nat.le_refl g.k)).trans_eq ok.sW.symm have hO:=qA1 hC (Nat.le_refl (g.ho*g.wo)) have eB:rdW g.b o.toUSize=g.b[o]!:=by rw[qAj _ _ ok.sb,qAc _ (by have:=ع o ok.pk;omega)] obtain ⟨d1,d2,d3,d4,-⟩:=qFu sW2 hP (λt _=>ok.HW _) h1 h3 obtain ⟨k1,k2,k3⟩:=qD2 (λp=>g.W[o*g.k+p]!) g.k have hlen:=d1.trans_le (qBq _ _) obtain ⟨⟨T1,T3⟩,T2⟩:=qGa ok o (λp=>dv g.W[o*g.k+p]!) k2 א א א d1 א א d4 _ 0 off 0 ((Л _).trans d1.symm) ⟨h2,nofun⟩ rw[D_outs,з hC] dsimp only rw[У א,eB] refine ih (o+1) _ _ _ _ _ א (Nat.add_one_mul o _ ▸ qCr hq (qAc _ א) א) d2 T1 d3 ?_ have E:(o*g.ho+g.ho)*g.wo=o*(g.ho*g.wo)+g.ho*g.wo:=by ring rw[Nat.add_one_mul,<-E] rw[<-Nat.mul_assoc o] at hq H refine qE9 (L:=C_keep _ g.k) ⟨ok,hC,d1,k1,k2,k3,λj p hj=>?_,by dsimp only;rw[ok.nf,T2];rfl,rfl⟩ (by dsimp only;omega) g.ho 0 q out (Л _) hq H have hjl:=(List.getElem?_eq_some_iff.mp hj).1 refine ⟨?_,(d4 j p hj).2⟩ dsimp only rw[T3 j א א,D_tv,hj] simpa using Nat.mod_eq_of_lt ((Nat.lt_of_add_left_lt (qCm ok hC (y:=0) (x:=0) (ok.ho ▸ Nat.succ_pos _) (ok.wo ▸ Nat.succ_pos _) (k2 _ (List.mem_of_getElem? hj))).1).trans ok.xs) lemma qE6 (A:Array UInt32) (n:USize) (hn:n.toNat<=A.size) (d:Nat) : ∀ (i:USize) (lo hi:UInt32),i.toNat+d=n.toNat->scan A n i hn lo hi = if 0x007FFFFF<=(D_scan A n i hn lo hi).lo∧(D_scan A n i hn lo hi).hi<Ц then (D_scan A n i hn lo hi).hi else Е:=by induction' d with d ih<;>intro i lo hi he<;>rw[scan,D_scan] . simp only[dite_eq_right (qCx he.ge)] simp only[dite_eq_left (qB9 he).1] exact ih (i+1) _ _ (qB9 he).2 lemma qIq {g:D_C} (ok:D_Ok g) (hs:g.xd.size<2^32) : D_outs g hs 0 0 (И g.k 0) (И g.k 0) (pushZ g.k.toUSize 0 (Ф g.k)) (И (g.C*(g.ho*g.wo)) 0) =if g.dw then depthwiseSpec g.s g.relu g.X g.W g.b else conv2dSpec g.s g.relu g.X g.W g.b:=by refine (qDp (qE3 ok hs g.C 0 0 _ _ _ _ (Л _) ג ג ג ((qCo g.k.toUSize g.k 0 _ ((Л _).trans (qAc g.k (ok.nk.trans ב)).symm)).trans (Л _)) ⟨by simp,λq _ h=>א⟩) λq h=>h).trans ?_ obtain ⟨dw,_,_,s,_,_,_,k,C,ho,wo⟩:=g obtain ⟨h1,h2,h3,h4⟩:=ok cases dw<;>simp at h1 h2 h3 h4<;>subst h1 h2 h3 h4 <;>refine congrArg (Array.ofFn (n:=_)) (funext λq=>?_)<;>simp[D_sp,D_out,D_xw,D_t0] lemma qE1 (dw:Bool) (s:ConvShape) (relu:Bool) (X W b:Array UInt32) {k C:Nat} (hk:k=if dw then s.kh*s.kw else s.cin*s.kh*s.kw) (hC:C=if dw then s.cin else s.cout) : D_F dw s relu X W b k C =if dw then depthwiseSpec s relu X W b else conv2dSpec s relu X W b:=by unfold D_F dsimp only rw[qAe] split;swap;rfl split;swap;rfl rename_i hg hs obtain ⟨a,c,d,e,f,-,i,j,l,m,n,o,p,q,r,t,u,-,v,x⟩:=hg have y:=qE6 X X.usize (qAf X) X.usize.toNat 0 Е 0 (Л _) rw[з ⟨c,d⟩] at y have Hx:=y ▸ qAp X a (by rw[y];intro h;rw[h] at d;exact absurd d (by decide)) refine (qIq ?_ _).trans ?_ exact ⟨hk,hC,rfl,rfl,rfl,qAc _ j,rfl,λh=>beq_iff_eq.1 (ذ.1 h).1,i,l,m,n,o,qFw s X a e p q rfl _ 0 _ (Л _) ⟨by rw[Nat.zero_mul];rfl,λq hq=>א⟩,hs,Hx,qGn (qAp W r v).2.2.1,qGn (qAp b t x).2.2.1,f,r,t,u,q,rfl⟩ rfl lemma qEr:convF=conv2dSpec:=by funext s r X W b;exact qE1 false s r X W b rfl rfl lemma qDx:depthF=depthwiseSpec:=by funext s r X W b;exact qE1 true s r X W b rfl rfl end @[inline] def sm (w:UInt32):Bool:=good w && (w &&& В)<=0x70FFFFFF section open Float.Model Float.Model.UnpackedFloat def T_r (N:Nat):Nat := N/2^39+if 2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1) then 1 else 0 lemma qJl (N:Nat):FloatLib.Numerics.roundShiftRightEven N 39=T_r N:=by have a:Nat.shiftRight N 39=N/2^39:=т N 39;have b:N-Nat.shiftLeft (N/2^39) 39=N%2^39:=by rw[show Nat.shiftLeft (N/2^39) 39=N/2^39*2^39 from щ _ _];omega rw[FloatLib.Numerics.roundShiftRightEven_def];simp only[show (39 == 0)=false from rfl,Bool.false_eq_true,ite_false,a,b,beq_iff_eq];unfold T_r;split_ifs<;>omega set_option maxRecDepth 8192 in lemma qIf (neg:Bool) (N:Nat) (e':Int) (hN1:2^62<=N) (hN2:N<2^63) (h1:-148<=e'+39) (h2:e'+39+150<=253) : toWord (Model.ofModel Fmt (UnpackedFloat.round Format.binary32 (Model.modelSign neg) N e')) =UInt32.ofNat ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by have hr1:2^23<=T_r N:=by unfold T_r;split<;>omega have hr2:T_r N<=2^24:=by unfold T_r;split<;>omega have hT:Format.binary32.targetExponent (totalExponent N e')=e'+39:=by simp only[totalExponent,qDu N 62 hN1 hN2,qG6];omega have K:∀ (P:Nat) (E:Int) (hp:0< P),2^23<=P->P<2^24->-149<=E->E<=104 → (E+150).toNat*2^23+P%2^23=(e'+39+149).toNat*2^23+T_r N → toWord (Model.ofModel Fmt (.finite (Model.modelSign neg) P E hp)) =UInt32.ofNat ((if neg then 2^31 else 0)+(e'+39+149).toNat*2^23+T_r N):=by intro P E hp a b c d e simp only[toWord,Model.toNatBits,Model.ofModel,Model.ofModelBits] congr 1 show (UnpackedFloat.pack Format.binary32 _).toNat=_ rw[qBs _ _ _ _ a b א א] cases neg<;>simp[Model.modelSign]<;>omega rw[Model.round_exact_eq_finishRoundedMantissa _ _ _ _ א,hT,Model.roundMantissaAtExponentEven,з א,show (e'+39-e').toNat=39 by omega,qJl] by_cases hc:T_r N=2^24 . have h:=Model.finishRoundedMantissa_normalized_carry FloatFormat.binary32 (Model.modelSign neg) (e'+39+23) (by show -126<=e'+39+23;omega);rw[show e'+39+23-(FloatFormat.binary32.fracWidth:ℤ)=e'+39 by show e'+39+23-23=_;omega,show e'+39+23+1-(FloatFormat.binary32.fracWidth:ℤ)=e'+40 by show e'+39+23+1-23=_;omega] at h;rw[show Model.finishRoundedMantissa Format.binary32 (Model.modelSign neg) (T_r N,e'+39) =.finite (Model.modelSign neg) (2^23) (e'+40) ב by rw[hc];exact h] exact K (2^23) (e'+40) ב le_rfl ב א א (by rw[hc,show (e'+40+150).toNat=(e'+39+149).toNat+2 by omega];omega) . rw[qEq _ _ _ hr1 א א א] exact K _ _ _ hr1 א א א (by rw[show (e'+39+150).toNat=(e'+39+149).toNat+1 by omega];omega) @[inline] def T_rd (neg:Bool) (mag e0 L:UInt64):UInt32 := let n:=mag <<< (62-L) let q:=n >>> 39 let rm:=n &&& 0x7FFFFFFFFF let w:=((e0+L-24) <<< 23)+q + (if rm > 0x4000000000 || (rm == 0x4000000000 && q &&& 1 == 1) then 1 else 0) (if neg then w ||| х else w).toUInt32 lemma qHm (neg:Bool) (mag e0:UInt64) (h0:mag.toNat≠0) (h1:mag.toNat<2^63) (h2:24< e0.toNat+mag.toNat.log2) (h3:e0.toNat+mag.toNat.log2<277) : T_rd neg mag e0 mag.log2=toWord (Ч Fmt ⟨neg,mag.toNat,(e0.toNat:Int)-150⟩):=by have l0:=Nat.log2_self_le h0;have l1:mag.toNat<2^(mag.toNat.log2+1):=Nat.lt_log2_self;have hL:mag.toNat.log2<=62:=Nat.lt_succ_iff.1 ((Nat.log2_lt h0).2 h1);obtain ⟨L,hLd⟩:∃ L,L=mag.toNat.log2:=⟨_,rfl⟩;rw[<-hLd] at l0 l1 hL h2 h3;have eL:mag.log2.toNat=L:=hLd.symm;obtain ⟨N,hN⟩:∃ N,N=mag.toNat*2^(62-L):=⟨_,rfl⟩;have p:2^L*2^(62-L)=2^62:=by rw[<-Nat.pow_add];congr 1;omega have hN1:2^62<=N:=by rw[hN,<-p];exact Т _ l0 have hN2:N<2^63:=by have:=Nat.mul_lt_mul_of_pos_right l1 (Nat.two_pow_pos (62-L));rwa [<-hN,<-Nat.pow_add,show L+1+(62-L)=63 by omega] at this rw[qBb,<-ExactRuntime.round_scale _ _ _ _ (62-L) h0,<-hN,qIf neg N _ hN1 hN2 א א];have eT:((e0.toNat:Int)-150-((62-L:Nat):Int)+39+149).toNat=e0.toNat+L-24:=by omega rw[eT];unfold T_rd;dsimp only;have e1:(62-mag.log2).toNat=62-L:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[eL];exact hL)),eL];rfl have a1:(mag <<< (62-mag.log2)).toNat=N:=by rw[UInt64.toNat_shiftLeft,e1,З (by omega:62-L<64),щ,<-hN,З א] generalize mag <<< (62-mag.log2)=n at a1;have a2:(n >>> 39).toNat=N/2^39:=by rw[ъ,a1];simp[Nat.shiftRight_eq_div_pow] have a3:(n &&& 0x7FFFFFFFFF).toNat=N%2^39:=by rw[و,a1];exact О N 39 generalize n >>> 39=q at a2;generalize n &&& 0x7FFFFFFFFF=rm at a3;have a4:(q &&& 1).toNat=q.toNat%2:=by rw[و];exact О _ 1 have a5:(e0+mag.log2-24).toNat=e0.toNat+L-24:=by rw[UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 (by rw[л,eL,З א];show 24<=_;omega)), л,eL,З א] rfl have a6:((e0+mag.log2-24) <<< 23).toNat=(e0.toNat+L-24)*2^23:=by rw[UInt64.toNat_shiftLeft,a5,щ];show _*2^23%_=_;rw[З א] generalize (e0+mag.log2-24) <<< 23=hi at a6;have hup:(decide (rm > 0x4000000000) || (rm == 0x4000000000 && q &&& 1 == 1)) =decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=by apply Bool.eq_iff_iff.2;simp only[Bool.or_eq_true,Bool.and_eq_true,decide_eq_true_eq,beq_iff_eq,gt_iff_lt,UInt64.lt_iff_toNat_lt,<-UInt64.toNat_inj,a3,a4,a2];rfl rw[hup];obtain ⟨c,hc⟩:∃ c,c=decide (2^38< N%2^39∨(N%2^39=2^38∧(N/2^39)%2=1)):=⟨_,rfl⟩;rw[<-hc];have hr:T_r N=N/2^39+if c=true then 1 else 0:=by rw[hc,T_r];simp only[decide_eq_true_eq] have hq:N/2^39<2^24 :=א;have hw:(hi+q+(if c=true then (1:UInt64) else 0)).toNat=(e0.toNat+L-24)*2^23+T_r N:=by rw[л,л,a6,a2,hr];cases c<;>simp only[Bool.false_eq_true,ite_false,ite_true] <;>(try rw[show (1:UInt64).toNat=1 from rfl])<;>(try rw[show (0:UInt64).toNat=0 from rfl])<;>omega generalize hi+q+(if c=true then (1:UInt64) else 0)=w at hw;have hw2:w.toNat<2^31:=by rw[hw,hr];split<;>omega apply Щ.mp;rw[س];cases neg;. simp only[Bool.false_eq_true,ite_false,UInt64.toNat_toUInt32,Nat.zero_add,hw] . simp only[ite_true,UInt64.toNat_toUInt32,UInt64.toNat_or];have h5:w.toNat ||| (2147483648:UInt64).toNat=2^31+w.toNat:=by have:=Nat.two_pow_add_eq_or_of_lt hw2 1;simp only[Nat.mul_one] at this;rw[this,Nat.or_comm];rfl rw[h5];congr 1;omega lemma qKn (w:UInt32) (h:(w &&& В)=0):Д w=0:=by have h1:=qAt w;rw[h] at h1;have h2:=w.toNat_lt have h3:w=0∨w=х:=by have:w.toNat=0∨w.toNat=2147483648:=by simp at h1;omega rcases this with e | e;. exact Or.inl (Щ.mp (by simpa using e)) . exact Or.inr (Щ.mp (by simpa using e)) have h4:expOf w=0∧fracOf w=0:=by rcases h3 with rfl | rfl<;>decide simp[rationalWord,decodeD,h4,FloatLib.Numerics.Dyadic.toRat_eq_zero_iff] @[inline] def T_e (w:UInt32):UInt64 := if w &&& В == 0 then 255 else (expOf w).toUInt64 @[inline] def T_m (w:UInt32):UInt64 := if w &&& В == 0 then 0 else (fracOf w+0x800000).toUInt64 @[inline] def T_s (e0:UInt64) (w:UInt32):UInt64:=T_m w <<< (T_e w-e0) @[inline] def T_ok (e0:UInt64) (w:UInt32):Bool := good w && (w &&& В == 0 || (e0<=T_e w && T_e w-e0<=37)) @[inline] def T_p (e0:UInt64) (w:UInt32) (p:UInt64):UInt64 := if signOf w then p else p+T_s e0 w @[inline] def T_n (e0:UInt64) (w:UInt32) (n:UInt64):UInt64 := if signOf w then n+T_s e0 w else n lemma qJ1 (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true) : (T_s e0 w).toNat<2^61∧Д w = (if signOf w then -((T_s e0 w).toNat:ℚ) else ((T_s e0 w).toNat:ℚ)) * 2^((e0.toNat:Int)-150):=by unfold T_ok at h simp only[Bool.and_eq_true,Bool.or_eq_true,beq_iff_eq,decide_eq_true_eq] at h obtain ⟨hg,hz⟩:=h by_cases h0:w &&& В=0 . have hs:T_s e0 w=0:=by unfold T_s T_m;simp[h0] rw[hs,qKn w h0];simp . obtain ⟨h1,h2⟩:=hz.resolve_left h0;have g:=qA3 w hg;have a0:(w &&& В).toNat≠0:=λe=>h0 (Щ.mp (by simpa using e));rw[qAt] at a0;have hx:=qA0 w;have hf:(fracOf w).toNat=w.toNat%2^23:=qBj w;have hl:=w.toNat_lt;have x1:1<=(expOf w).toNat :=א;have x2:(expOf w).toNat<=254 :=א;have f1:(fracOf w).toNat<2^23 :=א;have ee:(T_e w).toNat=(expOf w).toNat:=by unfold T_e;simp[h0] have em:(T_m w).toNat=2^23+(fracOf w).toNat:=by unfold T_m;simp only[h0,beq_iff_eq,if_false,UInt32.toNat_toUInt64];rw[UInt32.toNat_add,З (by show _+8388608< _;omega)];show _+8388608=_;omega have l1:=UInt64.le_iff_toNat_le.1 h1;have l2:=UInt64.le_iff_toNat_le.1 h2;rw[UInt64.toNat_sub_of_le _ _ h1,ee] at l2;rw[ee] at l1;have l3:(expOf w).toNat-e0.toNat<=37:=l2 have es:(T_s e0 w).toNat=(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat):=by unfold T_s;have sh:(T_e w-e0).toNat%64=(expOf w).toNat-e0.toNat:=by rw[UInt64.toNat_sub_of_le _ _ h1,ee];exact З א rw[UInt64.toNat_shiftLeft,em,sh,щ];apply З;have:2^((expOf w).toNat-e0.toNat)<=2^37:=Nat.pow_le_pow_right ב l3;have:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat)<=2^24*2^37 :=Nat.mul_le_mul א this;omega refine ⟨?_,?_⟩;. have a:2^((expOf w).toNat-e0.toNat)<=2^37:=Nat.pow_le_pow_right ב l3;have b:(2^23+(fracOf w).toNat)*2^((expOf w).toNat-e0.toNat)<=(2^24-1)*2^37 :=Nat.mul_le_mul א a;rw[es];omega . have nz:expOf w≠0:=λe=>by rw[e] at x1;simp at x1 have ew:(2:ℚ)^(((expOf w).toNat:Int)-150) =2^((expOf w).toNat-e0.toNat)*2^((e0.toNat:Int)-150):=by rw[<-к,<-а Р];congr 1;omega rw[es];simp only[rationalWord,decodeD,nz,ite_false,FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand,ew];cases signOf w<;>simp<;>ring lemma qEu (e0:UInt64) (w:UInt32) (p n:UInt64) (V:ℚ) (B C:Nat) (hC:C=2^61-1) (hB:B+C<2^63) (hw:T_ok e0 w=true) (hp:p.toNat<=B) (hn:n.toNat<=B) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) : (T_p e0 w p).toNat<=B+C∧(T_n e0 w n).toNat<=B+C ∧ V+Д w=(((T_p e0 w p).toNat:ℚ)-(T_n e0 w n).toNat) * 2^((e0.toNat:Int)-150):=by obtain ⟨h1,h2⟩:=qJ1 e0 w hw have h3:(T_s e0 w).toNat<=C :=א unfold T_p T_n rw[h2,hV] cases signOf w . simp only[Bool.false_eq_true,ite_false];have e:(p+T_s e0 w).toNat=p.toNat+(T_s e0 w).toNat:=by rw[л];exact З א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring . simp only[ite_true];have e:(n+T_s e0 w).toNat=n.toNat+(T_s e0 w).toNat:=by rw[л];exact З א rw[e];refine ⟨by omega,by omega,?_⟩;push_cast;ring @[inline] def T_fin (p n e0:UInt64) (f:Unit->UInt32):UInt32 := if p == n then f () else let mag:=if p< n then n-p else p-n let L:=mag.log2 if e0+L<=24 || e0+L ≥ 277 then f () else T_rd (decide (p< n)) mag e0 L lemma qDi (p n e0:UInt64) (f:Unit->UInt32) (hp:p.toNat<2^63) (hn:n.toNat<2^63) (he:e0.toNat<=255) (R:UInt32) (hf:f ()=R) (hR:p≠n->∃ d:FloatLib.Numerics.Dyadic, d.toRat=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150) ∧ R=toWord (Ч Fmt d)):T_fin p n e0 f=R:=by unfold T_fin split . exact hf . rename_i hne;have hne':p≠n:=λe=>hne (by simp[e]);have hnn:p.toNat≠n.toNat:=λe=>hne' (UInt64.toNat_inj.mp e);obtain ⟨d,hd,hR⟩:=hR hne';dsimp only;obtain ⟨mag,hm⟩:∃ m,m=if p< n then n-p else p-n:=⟨_,rfl⟩;rw[<-hm] have hm1:mag.toNat≠0∧mag.toNat<2^63 ∧ (((if decide (p< n)=true then -(mag.toNat:ℚ) else (mag.toNat:ℚ))) =(p.toNat:ℚ)-n.toNat):=by by_cases c:p< n . have c':=я.1 c;have:mag.toNat=n.toNat-p.toNat:=by rw[hm,if_pos c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c'.le)] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'.le];simp[c] . have c':n.toNat<=p.toNat:=by have:=я.not.1 c;omega have:mag.toNat=p.toNat-n.toNat:=by rw[hm,if_neg c,UInt64.toNat_sub_of_le _ _ (UInt64.le_iff_toNat_le.2 c')] refine ⟨by omega,by omega,?_⟩;rw[this,Nat.cast_sub c'];simp[c] obtain ⟨m0,m1,m2⟩:=hm1;split;. exact hf . rename_i hb;simp only[Bool.or_eq_true,decide_eq_true_eq,not_or,ge_iff_le] at hb;obtain ⟨b1,b2⟩:=hb;have eL:mag.log2.toNat=mag.toNat.log2:=rfl;have hL:mag.toNat.log2<=62:=by have:=(Nat.log2_lt m0).2 m1;omega have eA:(e0+mag.log2).toNat=e0.toNat+mag.toNat.log2:=by rw[л,eL];exact З א have b3:¬ (e0+mag.log2).toNat<=(24:UInt64).toNat:=λh=>b1 (UInt64.le_iff_toNat_le.2 h);have b4:¬ (277:UInt64).toNat<=(e0+mag.log2).toNat:=λh=>b2 (UInt64.le_iff_toNat_le.2 h);rw[eA] at b3 b4;have t1:(24:UInt64).toNat=24:=rfl;have t2:(277:UInt64).toNat=277:=rfl;rw[qHm _ _ _ m0 m1 א א,hR];congr 1 have s0:d.significand≠0:=by intro h0;have:d.toRat=0:=(ى d).2 h0;rw[hd] at this;have q:(p.toNat:ℚ)-n.toNat≠0:=by rw[sub_ne_zero];exact_mod_cast hnn exact (mul_ne_zero q (zpow_ne_zero _ Р)) this refine roundDyadic_congr Fmt (by decide) _ _ m0 s0 ?_;rw[hd,<-m2];simp only[FloatLib.Numerics.Dyadic.toRat,FloatLib.Numerics.Dyadic.signedSignificand];cases decide (p< n)<;>simp lemma qFo (w:UInt32):(T_e w).toNat<=255:=by unfold T_e;split;. decide . have:=qA0 w;simp only[UInt32.toNat_toUInt64];omega @[inline] def T_min (x y:UInt64):UInt64:=if x< y then x else y lemma qB8 (x y:UInt64):(T_min x y).toNat<=x.toNat:=by unfold T_min;split;. exact le_rfl . have:=я.not.1 ‹_›;omega lemma qD7 (e0:UInt64) (w0 w1:UInt32) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 0)).toNat<=2*(2^61-1)∧(T_n e0 w1 (T_n e0 w0 0)).toNat<=2*(2^61-1) ∧ Д w0+Д w1=(((T_p e0 w1 (T_p e0 w0 0)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 0)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEu e0 w0 0 0 0 0 _ rfl ב h0 ג ג ג obtain ⟨b1,b2,b3⟩:=qEu e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 rw[zero_add] at b3 exact ⟨by omega,by omega,b3⟩ lemma qGm (e0:UInt64) (w0 w1:UInt32) (p n:UInt64) (V:ℚ) (hp:p.toNat<=2*(2^61-1)) (hn:n.toNat<=2*(2^61-1)) (hV:V=((p.toNat:ℚ)-n.toNat)*2^((e0.toNat:Int)-150)) (h0:T_ok e0 w0=true) (h1:T_ok e0 w1=true) : (T_p e0 w1 (T_p e0 w0 p)).toNat<2^63∧(T_n e0 w1 (T_n e0 w0 n)).toNat<2^63 ∧ V+Д w0+Д w1=(((T_p e0 w1 (T_p e0 w0 p)).toNat:ℚ) - (T_n e0 w1 (T_n e0 w0 n)).toNat)*2^((e0.toNat:Int)-150):=by obtain ⟨a1,a2,a3⟩:=qEu e0 w0 p n V _ _ rfl ב h0 hp hn hV obtain ⟨b1,b2,b3⟩:=qEu e0 w1 _ _ _ _ _ rfl ב h1 a1 a2 a3 exact ⟨by omega,by omega,b3⟩ lemma qBi (e0:UInt64) (w:UInt32) (h:T_ok e0 w=true):expOf w≠255:=by unfold T_ok at h;simp only[Bool.and_eq_true] at h;exact qAd w h.1 end @[noinline] def addSlow (relu:Bool) (x y:Word):Word := act relu (toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) section open Float.Model Float.Model.UnpackedFloat @[noinline] def addT (relu:Bool) (x y:Word):Word := let e0:=T_min (T_e x) (T_e y) if T_ok e0 x && T_ok e0 y then act relu (T_fin (T_p e0 y (T_p e0 x 0)) (T_n e0 y (T_n e0 x 0)) e0 λ_=>toWord (FloatLib.Floats.Formats.BinaryInterchange.Model.add (ofW x) (ofW y))) else addSlow relu x y lemma qHa (relu:Bool) (x y:Word):addT relu x y=addSlow relu x y:=by unfold addT;dsimp only;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨hx,hy⟩:=hc;obtain ⟨p1,n1,v⟩:=qD7 _ x y hx hy;have ex:=qBi _ x hx;have ey:=qBi _ y hy;unfold addSlow;congr 1 refine qDi _ _ _ _ א א ((qB8 _ _).trans (qFo x)) _ rfl λ_ =>⟨Model.addDyadic (decodeD x) (decodeD y),?_,?_⟩ . rw[Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat];exact v . rw[Model.Proof.add_eq_spec];have e:=Ы x ex;have f:=Ы y ey;simp only[Model.Spec.add,ofW,e,f] . rfl end @[inline] def addOne (relu:Bool) (x y:Word):Word := if sm x && sm y then let r:=zivS (dv x+dv y) if r == Е then addT relu x y else act relu r else addSlow relu x y section lemma qI6 (S:Bool) (ex fr m:Nat) (q:Int) (h1:897<=ex) (h2:ex<1151) (h3:fr<2^23) (hm:m≠0) (hW1:(m:ℝ)*2^q<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076)) (hW2:fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<(m:ℝ)*2^q) (hW3:(2^53-2^28)*2^((ex:Int)-1076)<(m:ℝ)*2^q) : toWord (Ч Fmt ⟨S,m,q⟩) =UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr):=by have hE:(2:ℝ)^((ex:Int)-1046)=2^30*2^((ex:Int)-1076):=by rw[<-к,<-а Р] congr 1;omega rw[qFe S m q (2^23+fr) ((ex:Int)-1046) hm א א א א (by rw[hE];push_cast;linarith) (λ_=>by rw[hE];push_cast;linarith [hW2 א]) (λ_=>by rw[hE];linarith),show ((ex:Int)-1046+150).toNat=ex-896 by omega,show (2^23+fr)%2^23=fr by omega] lemma qGv (a b:ℝ) (ha:0<=a) (h:a< b) : ∃ (m:Nat) (q:Int),m≠0∧a<(m:ℝ)*2^q∧(m:ℝ)*2^q<=b:=by obtain ⟨k,hk⟩:=exists_pow_lt_of_lt_one (sub_pos.2 h) one_half_lt_one;have h2:(0:ℝ)<2^k:=by positivity rw[one_div_pow,div_lt_iff₀ h2] at hk;refine ⟨⌊b*2^k⌋₊,-k,?_⟩;rw[zpow_neg,к,<-div_eq_mul_inv,lt_div_iff₀ h2,div_le_iff₀ h2];have h1:=Nat.lt_floor_add_one (b*2^k);have h5:a*2^k<⌊b*2^k⌋₊ :=ד;exact ⟨(Nat.cast_pos.1 ((Н ha h2.le).trans_lt h5)).ne',h5,Nat.floor_le (Н (ha.trans h.le) h2.le)⟩ lemma qIs (z:Model Fmt):ofW (toWord z)=z:=qBl z lemma qIz (w:UInt32) (h0:expOf w≠0) (h1:expOf w≠255):wv w≠0:=by have:sv w≠0:=by unfold sv mantOf;split<;>simp[h0]<;>omega simp[qAq w h1,rationalWord,decodeD_toRat,this,zpow_ne_zero] lemma qD8 (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h:ziv s e≠Е) (M:Model Fmt) (hf:Я M=true) (hM:в M=Model.roundAt Fmt V):toWord M=ziv s e:=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,hW2,hW3⟩:=qCd s e V hlo hhi h;have hb0:(0:ℝ)<2^((ex:Int)-1076):=ы ב _;have hA2:(0:ℝ)<=(2^53-2^28)*2^((ex:Int)-1076) :=Н ב hb0.le obtain ⟨Lo,hLo0,hLoW,hLo1,hLo2⟩:∃ Lo:ℝ,0<=Lo∧Lo< W ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)<=Lo) ∧ (2^53-2^28)*2^((ex:Int)-1076)<=Lo:=by by_cases hfr:fr=0;. exact ⟨_,hA2,hW3,λh=>absurd hfr h,le_rfl⟩ . have hfr1:(1:ℝ)<=fr:=by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hfr refine ⟨_,?_,hW2 hfr,λ_=>le_rfl,?_⟩;. exact Н (by nlinarith) hb0.le . exact Б (by nlinarith) hb0.le have hW0:0< W:=ц hLo0 hLoW;obtain ⟨m1,q1,hm1,hd1,hd1'⟩:=qGv Lo W hLo0 hLoW;set B:ℝ:=(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) with hB;obtain ⟨m2,q2,hm2,hd2,hd2'⟩:=qGv W ((W+B)/2) hW0.le ד;have w1:=qI6 S ex fr m1 q1 h1 h2 h3 hm1 ד (λh=>ц (hLo1 h) hd1) (ц hLo2 hd1) have w2:=qI6 S ex fr m2 q2 h1 h2 h3 hm2 ד (λh=>by exact ц (hLo1 h) ד) (ц hLo2 ד) rw[hz];set z:=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) with hzd;have hzn:z.toNat=(if S then 2^31 else 0)+(ex-896)*2^23+fr:=by rw[hzd,س,З];. split<;>omega have hexp:(expOf z).toNat=ex-896:=by rw[qA0,hzn];split<;>omega have he0:expOf z≠0:=λh=>by rw[h] at hexp;simp at hexp;omega have he1:expOf z≠255:=λh=>by rw[h] at hexp;simp at hexp;omega have r1:Ч Fmt ⟨S,m1,q1⟩=ofW z:=by rw[<-w1,qIs] have r2:Ч Fmt ⟨S,m2,q2⟩=ofW z:=by rw[<-w2,qIs] have fz:Я (ofW z)=true:=by simp[ofW,isFinite_ofUInt32,he1] have t1:=Model.toReal_roundDyadic_eq_roundAt Fmt (by decide) ⟨S,m1,q1⟩ (by rw[r1];exact fz);have t2:=Model.toReal_roundDyadic_eq_roundAt Fmt (by decide) ⟨S,m2,q2⟩ (by rw[r2];exact fz);rw[r1] at t1;rw[r2] at t2 have hv:в (ofW z)=Model.roundAt Fmt V:=by apply le_antisymm . cases S;. rw[t1];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . rw[t2];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . cases S;. rw[t2];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith . rw[t1];apply Model.roundAt_mono;simp[FloatLib.Numerics.Dyadic.toReal,FloatLib.Numerics.Dyadic.signedSignificand,hV];linarith have hnz:в M≠0:=by rw[hM,<-hv];exact qIz z he0 he1 have:=Model.eq_of_toReal_eq_of_nonzero hf fz (hM.trans hv.symm) hnz;rw[this];have hr:toWord (ofW z)=z:=by rw[<-r1,w1] exact hr lemma qKc:(2:ℝ)^(127:ℤ)<=в (Model.posMaxFinite Fmt):=by rw[Model.toReal_posMaxFinite,show Model.pow2 Fmt.fracWidth+Fmt.maxFiniteFracField=2^24-1 by decide +kernel,show Fmt.maxNormalExponent-Int.ofNat Fmt.fracWidth=104 by decide +kernel,Model.bpow,qA4];norm_num lemma qG1 (q:ℚ) : Model.signedScaledRatToReal (decide (q<0)) q.num.natAbs q.den 0=(q:ℝ):=by rw[Model.signedScaledRatToReal,Model.scaledRatToReal,Nat.cast_natAbs,Rat.cast_def];by_cases hq:q<0;. simp[hq,abs_of_neg (Rat.num_neg.2 hq)];ring . simp[hq,abs_of_nonneg (Rat.num_nonneg.2 (not_lt.1 hq))] lemma qJx (s e:Float) (q:ℚ) (hlo:R (s-e)<(q:ℝ)) (hhi:(q:ℝ)< R (s+e)) (h:ziv s e≠Е) (hb : |(q:ℝ)| ≤ 2^(127:ℤ)):roundRational q=ziv s e:=by have hf:=Model.isFinite_roundRatScaled_of_abs_le_posMaxFinite Fmt (decide (q<0)) q.num.natAbs q.den 0 (by decide) q.den_nz (qG1 q ▸ hb.trans qKc);have hM:=Model.toReal_roundRatScaled_eq_roundAt Fmt _ _ _ 0 (by decide) (by simpa using λh0=>qF9 s e q hlo hhi h (by simp[h0])) q.den_nz hf;exact qD8 s e q hlo hhi h _ hf (qG1 q ▸ hM) lemma qJb (N d:Nat) (x:Int) (hr:sqrtLower N d≠0) (hd:0< d) (hs:x<=(FloatFormat.toModel Fmt).targetExponent (Float.Model.totalExponent (sqrtLower N d) x)) (hb:п ((N:ℝ)/d)*FloatLib.Floats.Formats.Flocq.bpow ь x < 2^(127:ℤ)) : Я (Model.ofModel Fmt (Float.Model.UnpackedFloat.roundWithAccuracy (FloatFormat.toModel Fmt) .positive (sqrtLower N d) x (sqrtAccuracy N d)))=true := Model.isFinite_ofModel_roundWithAccuracy_of_lt_bpow Fmt rfl .positive _ _ _ (п ((N:ℝ)/d)) hr (sqrtAccuracy_correct N d hd) hs (hb.trans_eq (qA4 127).symm) lemma qJ5 (q:ℚ) (hq:0< q) (hb:п (q:ℝ)<2^(127:ℤ)) : Я (sqrtModel q)=true := qJb _ _ _ (ne_of_gt (sqrtRoot_pos q hq)) q.pos (sqrtStep_le_target q) (by rwa [sqrtScale_correct q hq]) lemma qI2 (s e:Float) (q:ℚ) (hq:0< q) (hlo:R (s-e)< п (q:ℝ)) (hhi:п (q:ℝ)< R (s+e)) (h:ziv s e≠Е) (hb:п (q:ℝ)<2^(127:ℤ)):sqrtRational q=ziv s e:=by have hf:=qJ5 q hq hb;exact (if_neg (not_le.2 hq)).trans (qD8 s e _ hlo hhi h _ hf (sqrtModel_correct q hq hf)) lemma qA6 (w:UInt32) (h:sm w=true):good w=true∧|wv w| ≤ 2^99:=by obtain ⟨hg,hl⟩:=ذ.1 h;have h1:=М.1 (of_decide_eq_true hl);rw[qAt] at h1;have h2:=qA3 w hg;rw[qCa w hg];refine ⟨hg,(qA7 _ _ h1 א).trans ?_⟩;unfold A_mag;norm_num lemma qJw (A P Q T g:Nat) (hg:g=0∨g=2^63) (ha:A<2^64) (e1:(A+2)%2^64=g+P) (e2:(2^64-2+A)%2^64=g+Q) (t1:2^61<=T) (t2:T<2^63-2^60) (l1:T< P+2^28) (u1:P<=T+2^28) (l2:T< Q+2^28) (u2:Q<=T+2^28) : A=g+(Q+2)∧T+2< Q+2+2^28∧Q+2+2<=T+2^28:=by omega lemma qJ7 (G f F:Nat) (hl:G+f+2< G+F+2^28) (hu:G+F+2<=G+f+2^28) : F+2<=f+2^28∧f+2< F+2^28:=by omega lemma qJa (s:Float) (h:zivS s≠Е) : ∃ (S:Bool) (ex fr M:Nat),897<=ex∧ex<1151∧fr<2^23 ∧ zivS s=UInt32.ofNat ((if S then 2^31 else 0)+(ex-896)*2^23+fr) ∧ s.toBits.toNat=(if S then 2^63 else 0)+M ∧ (ex*2^23+fr)*2^29+2< M+2^28 ∧ M+2<=(ex*2^23+fr)*2^29+2^28:=by obtain ⟨S,ex,fr,h1,h2,h3,hz,⟨P,e1,l1,u1⟩,⟨Q,e2,l2,u2⟩⟩:=qDq _ _ h rw[л] at e1 rw[UInt64.toNat_sub] at e2 obtain ⟨a,b,c⟩:=qJw _ P Q ((ex*2^23+fr)*2^29) _ (by cases S<;>simp) s.toBits.toNat_lt e1 e2 א א l1 u1 l2 u2 exact ⟨S,ex,fr,Q+2,h1,h2,h3,hz,a,b,c⟩ lemma qJg (v W z u:ℝ) (hu:0< u) (hz:z<=u) (h0:0<=v) (hv:v<=2^53*u) (h : |v-W| ≤ 1/2^53*(|W| + z)):v-2*u< W∧W< v+2*u:=by have h1 : |W| ≤ v + |v-W|:=by have:=Ь (v-(v-W)) (-(v-W)+(v-W));have e:W=v-(v-W):=by ring have a:=abs_sub_abs_le_abs_sub W v;rw[и W v,Ж h0] at a;linarith have h2:=abs_lt.mp (show |v-W| < 2*u by nlinarith);constructor<;>linarith [h2.1,h2.2] set_option maxRecDepth 8192 in lemma qKe (ex fr E F:Nat) (h1:897<=ex) (h2:ex<1151) (hfr:fr<2^23) (hF:F<2^52) (hl:(ex*2^23+fr)*2^29+2< E*2^52+F+2^28) (hu:E*2^52+F+2<=(ex*2^23+fr)*2^29+2^28) (W:ℝ) (h : |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z)) : W<(2^53+fr*2^30+2^29)*2^((ex:Int)-1076) ∧ (fr≠0->(2^53+fr*2^30-2^29)*2^((ex:Int)-1076)< W) ∧ (2^53-2^28)*2^((ex:Int)-1076)< W:=by have hE:E=ex∨(E+1=ex∧fr=0) :=א have hu0:(0:ℝ)<2^((E:Int)-1075):=ы ב _ have hz:A_z<=2^((E:Int)-1075):=by have e:A_z=2^(-1021:ℤ):=rfl rw[e] exact Ш ב א have hN:((2^52+F:Nat):ℝ)<=2^53:=by have:2^52+F<=2^53 :=א exact_mod_cast this have hN0:(0:ℝ)<=((2^52+F:Nat):ℝ):=Ю _ obtain ⟨a,b⟩:=qJg _ W A_z _ hu0 hz (Н hN0 hu0.le) (Б hN hu0.le) h push_cast at a b rcases hE with rfl | ⟨rfl,rfl⟩ . have e:(2:ℝ)^((E:Int)-1075)=2*2^((E:Int)-1076):=by rw[show (E:Int)-1075=1+((E:Int)-1076) by ring,а Р,zpow_one] rw[e] at a b;have hb:(0:ℝ)<2^((E:Int)-1076):=ы ב _;generalize (2:ℝ)^((E:Int)-1076)=c at a b hb ⊢;have e1:(E*2^23+fr)*2^29=E*2^52+fr*2^29:=by ring rw[e1] at hl hu;obtain ⟨n3,n4⟩:=qJ7 _ _ _ hl hu;have h3:(F:ℝ)+2<=fr*2^29+2^28:=by exact_mod_cast n3 have h4:(fr:ℝ)*2^29+2< F+2^28:=by exact_mod_cast n4 have h5:(0:ℝ)<=F:=Ю _;have h6:(0:ℝ)<=fr:=Ю _;have h7:=Б h3 hb.le;have h8:=б h4 hb;have h9:=Н h5 hb.le;have h10:=Н h6 hb.le;refine ⟨?_,?_,?_⟩;. nlinarith . intro _;nlinarith . nlinarith . have e:(2:ℝ)^(((E+1:Nat):Int)-1076)=2^((E:Int)-1075):=by congr 1;push_cast;ring rw[e];generalize (2:ℝ)^((E:Int)-1075)=c at a b hu0 ⊢;have h3:(F:ℝ)<=2^52:=by exact_mod_cast hF.le have h4:(2:ℝ)^52+2< F+2^28:=by exact_mod_cast (by omega:2^52+2< F+2^28) have h7:=Б h3 hu0.le;have h8:=б h4 hu0;refine ⟨?_,?_,?_⟩;. nlinarith . intro h0;exact absurd rfl h0 . nlinarith lemma qFj (s:Float) (d:FloatLib.Numerics.Dyadic) (hV : |R s-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z)) (h:zivS s≠Е):zivS s=toWord (Ч Fmt d):=by obtain ⟨S,ex,fr,M,h1,h2,h3,hz,hb,hl,hu⟩:=qJa s h;have hM:M=(M/2^52)*2^52+M%2^52 :=א;have hF:M%2^52<2^52:=э _ ב;generalize M/2^52=E at hM;generalize M%2^52=F at hM hF;subst hM;obtain ⟨-,hR⟩:=B_R s S E F א א hF (by rw[hb,с]);rw[hR] at hV obtain ⟨W,hVW,hW⟩:∃ W:ℝ,(d.toRat:ℝ)=(if S then -W else W) ∧ |((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-W| ≤ 1/2^53*(|W| + A_z):=by cases S . simp only[Bool.false_eq_true,ite_false,one_mul] at hV ⊢;exact ⟨_,rfl,hV⟩ . simp only[ite_true] at hV ⊢;refine ⟨-(d.toRat:ℝ),by simp,?_⟩;rw[abs_neg,show ((2^52+F:Nat):ℝ)*2^((E:Int)-1075) - -(d.toRat:ℝ) =-(-1*((2^52+F:Nat):ℝ)*2^((E:Int)-1075)-(d.toRat:ℝ)) by ring,abs_neg];exact hV obtain ⟨a,b,c⟩:=qKe ex fr E F h1 h2 h3 hF א א W hW;rw[hz];exact (qE8 d S ex fr h1 h2 h3 W hVW a b c).symm lemma qE4 (w:UInt32) (hg:good w=true) : ∃ (m k:ℤ),wv w=(m:ℝ)*2^k∧m.natAbs<2^25∧-160<=k:=by have hN:=w.toNat_lt;rw[qAz w hg];unfold A_mag;by_cases h0:w.toNat%2^31=0;. exact ⟨0,0,by rw[if_pos h0];simp,by norm_num,by norm_num⟩ . simp only[h0,ite_false];have hk:-160<=(((w.toNat%2^31/2^23:ℕ):ℤ)-150) :=א;have hM:2^23+w.toNat%2^31%2^23<2^25 :=א;generalize ((w.toNat%2^31/2^23:ℕ):ℤ)-150=k at hk ⊢;generalize 2^23+w.toNat%2^31%2^23=M at hM ⊢;by_cases hs:2^31<=w.toNat;. exact ⟨-(M:ℤ),k,by rw[if_pos hs];push_cast;ring,by simpa using hM,hk⟩ . exact ⟨(M:ℤ),k,by rw[if_neg hs];push_cast;ring,by simpa using hM,hk⟩ lemma qI7 (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:=by obtain ⟨fa,ra⟩:=qAl a ha;obtain ⟨fb,rb⟩:=qAl b hb;have la:=(qAi a ha).trans qAh;have lb:=(qAi b hb).trans qAh obtain ⟨h1,h2⟩:=qC7 _ _ fa fb (by rw[ra,rb] exact (С la lb (Й _) qKo.le).trans qCb) refine ⟨h1,?_⟩;obtain ⟨m,k,e1,h3,h4⟩:=qE4 a ha;obtain ⟨n,l,e2,h5,h6⟩:=qE4 b hb;have e:wv a*wv b=((m*n:ℤ):ℝ)*2^(k+l):=by rw[e1,e2,а Р];push_cast;ring rw[h2,ra,rb,e];refine qAu _ _ ?_ א;rw[Int.natAbs_mul];have:=Nat.mul_lt_mul'' h3 h5;omega lemma S_E (x y z:UInt32) (hx:good x=true) (hy:good y=true) (hz:good z=true) (d:FloatLib.Numerics.Dyadic) (hd:(d.toRat:ℝ)=wv x*wv y+wv z) : |R (dv x*dv y+dv z)-(d.toRat:ℝ)| ≤ 1/2^53*(|(d.toRat:ℝ)| + A_z):=by obtain ⟨f1,r1⟩:=qI7 x y hx hy obtain ⟨-,-,l⟩:=qFn x y hx hy obtain ⟨f2,r2⟩:=qAl z hz have h256:(2:ℝ)^256<=A_B:=by have:(2:ℝ)^256<=2^256*2^42 :=ב linarith [qBv] obtain ⟨-,he⟩:=qBa _ _ f1 f2 (by rw[r1];exact l.trans h256) (by rw[r2];exact (qAi z hz).trans qAh) rw[r1,r2] at he rw[hd] exact he lemma qCz (relu:Bool) (x y:Word) : addOne relu x y=act relu (toWord (Model.add (ofW x) (ofW y))):=by unfold addOne;split . rename_i hc;simp only[Bool.and_eq_true] at hc;obtain ⟨gx,-⟩:=qA6 x hc.1;obtain ⟨gy,-⟩:=qA6 y hc.2;dsimp only;split;. exact qHa relu x y . rename_i hr;have hn:zivS (dv x+dv y)≠Е:=λh=>hr (by simp[h]);have ex:=qAd _ gx;have ey:=qAd _ gy;have hd:((Model.addDyadic (decodeD x) (decodeD y)).toRat:ℝ)=wv x+wv y:=by rw[Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,qAq x ex,qAq y ey];simp only[rationalWord,Rat.cast_add] obtain ⟨fx,rx⟩:=qAl x gx;obtain ⟨fy,ry⟩:=qAl y gy;obtain ⟨-,he⟩:=qBa _ _ fx fy (by rw[rx];exact (qAi x gx).trans qAh) (by rw[ry];exact (qAi y gy).trans qAh);rw[rx,ry,<-hd] at he;rw[qFj _ _ he hn];congr 2;rw[Model.Proof.add_eq_spec];have e:=Ы x ex;have f:=Ы y ey;simp only[Model.Spec.add,ofW,e,f] . rfl lemma qFt {L:USize->Array UInt32->Array UInt32} (f:USize->UInt32->UInt32) {e:USize} (hL:∀ i x,L i x=if i< e then L (i+1) (wrW x i (f i (rdW x i))) else x) (d:Nat) : ∀ (i:USize) (x:Array UInt32),i.toNat+d=e.toNat->e.toNat<=x.size->x.size<2^32 → (L i x).size=x.size∧∀ q,(L i x)[q]! =if i.toNat<=q∧q< e.toNat then f (USize.ofNat q) x[q]! else x[q]!:=by induction d with | zero => intro i x h1 _ _ rw[hL,if_neg (by rw[А];omega)] exact ⟨rfl,λq=>(if_neg א).symm⟩ | succ d ih => intro i x h1 h2 h3 have hi:i< e:=А.2 א have hs:=qA8 x i (f i (rdW x i)) rw[hL,if_pos hi] obtain ⟨h4,h5⟩:=ih (i+1) (wrW x i (f i (rdW x i))) (by rw[usz i e hi];omega) א א refine ⟨h4.trans hs,λq=>?_⟩ rw[h5,qBd _ _ _ _ h3 א,usz i e hi,qAj _ _ h3] by_cases h6:q=i.toNat . subst h6;rw[if_neg א,if_pos rfl,if_pos א,USize.ofNat_toNat] . rw[if_neg h6];exact if_congr א rfl rfl def addGo (relu:Bool) (y:Array Word) (e i:USize) (x:Array Word) : Array Word := if h:i< e then addGo relu y e (i+1) (wrW x i (addOne relu (rdW x i) (rdW y i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def addF (relu:Bool) (a b:Array Word):Array Word := if a.size=b.size∧a.size<2^32 then addGo relu b a.usize 0 a else Array.ofFn (n:=a.size) λi=>addOne relu a[i] b[i.val]! lemma qF5:addF=addReluSpec:=by funext relu a b;unfold addF;split . rename_i h;obtain ⟨h1,h2⟩:=h;have hu:a.usize.toNat=a.size:=qAc _ h2;have hg:=qFt (L:=addGo relu b a.usize) (λi w=>addOne relu w (rdW b i)) (λ_ _=>by rw[addGo];rfl) a.size 0 a (by rw[hu,show (0:USize).toNat=0 from rfl,Л]) א h2;apply ه (by rw[hg.1];simp[addReluSpec]);intro i h3 h4;have hi:i< a.size:=by rwa [hg.1] at h3 rw[<-Э _ i h3,hg.2 i,if_pos ⟨Nat.zero_le _,by omega⟩,qCz,Э a i hi,qAj _ _ א,uk i א];simp[addReluSpec] . simp only[addReluSpec,qCz] end section @[noinline] def E_fma (relu:Bool) (w sw tw:UInt32):UInt32 := act relu (toWord (Model.fma (ofW w) (ofW sw) (ofW tw))) @[inline] def E_act (relu:Bool) (r:UInt32):UInt32 := if relu && signOf r then 0 else r @[inline] def E_el (relu g:Bool) (sw tw:UInt32) (y z:Float) (w:UInt32):UInt32 := if g && good w then let r:=zivS (dv w*y+z) if r == Е then E_fma relu w sw tw else E_act relu r else E_fma relu w sw tw @[specialize] def E_map (f:UInt32->UInt32) (e i:USize) (x:Array UInt32):Array UInt32 := if h:i< e then E_map f e (i+1) (wrW x i (f (rdW x i))) else x termination_by e.toNat-i.toNat decreasing_by rw[usz i e h];have:=А.mp h;omega def E_chs (relu:Bool) (c hw:Nat) (s t:Array UInt32) (ch:Nat) (x:Array UInt32):Array UInt32 := if ch< c then let sw:=s[ch]! let tw:=t[ch]! let y:=dv sw let z:=dv tw E_chs relu c hw s t (ch+1) (E_map (E_el relu (good sw && good tw) sw tw y z) ((ch+1)*hw).toUSize (ch*hw).toUSize x) else x termination_by c-ch def scaleF (relu:Bool) (c hw:Nat) (x s t:Array UInt32):Array UInt32 := if x.size=c*hw∧x.size<2^32 then E_chs relu c hw s t 0 x else scaleShiftSpec relu c hw x s t @[noinline] def E_dot (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := dotFn (k*k) (λt=>X[b.toNat+(t/k*w+t%k)]!) (λ_=>oneW) section open Float.Model Float.Model.UnpackedFloat def E_dotT (X:Array UInt32) (b:USize) (k w:Nat):UInt32 := if k=2 then let i:=b.toNat let a0:=X[i]! let a1:=X[i+1]! let a2:=X[i+w]! let a3:=X[i+(w+1)]! let e0:=T_min (T_min (T_e a0) (T_e a1)) (T_min (T_e a2) (T_e a3)) if T_ok e0 a0 && T_ok e0 a1 && T_ok e0 a2 && T_ok e0 a3 then T_fin (T_p e0 a3 (T_p e0 a2 (T_p e0 a1 (T_p e0 a0 0)))) (T_n e0 a3 (T_n e0 a2 (T_n e0 a1 (T_n e0 a0 0)))) e0 λ_=>E_dot X b k w else E_dot X b k w else E_dot X b k w lemma qE7 (X:Array UInt32) (b:USize) (k w:Nat):E_dotT X b k w=E_dot X b k w:=by unfold E_dotT;split;swap;rfl;subst_vars;dsimp only;split;swap;rfl;rename_i hc;simp at hc;obtain ⟨⟨⟨h0,h1⟩,h2⟩,h3⟩:=hc;obtain ⟨p1,n1,v⟩:=qD7 _ _ _ h0 h1;obtain ⟨p2,n2,v⟩:=qGm _ _ _ _ _ _ p1 n1 v h2 h3;refine qDi _ _ _ _ p2 n2 ((qB8 _ _).trans ((qB8 _ _).trans (qFo _))) _ rfl λe=>?_;have B:=qCe (2*2) (λj=>X[b.toNat+(j/2*w+j%2)]!) (λ_=>oneW) λj g =>⟨by interval_cases j<;>simp[qBi _ _ h0,qBi _ _ h1,qBi _ _ h2,qBi _ _ h3],by decide⟩;simp[Finset.sum_range_succ,qG2] at B;rw[v] at B;rw[E_dot,qAe];exact B (mul_ne_zero (sub_ne_zero.2 λh=>e (UInt64.toNat_inj.1 (Nat.cast_injective h))) (zpow_ne_zero _ Р)) end @[noinline] def E_div (n:Nat) (s:UInt32):UInt32 := toWord (Model.div (ofW s) (ofW (natW n))) @[inline] def E_quo (n:Nat) (j s:UInt32):UInt32 := if j< expOf s∧expOf s≠255 then s-(j <<< (23:UInt32)) else E_div n s @[inline] def E_tl (X:Array UInt32) (b:USize) (k w n:Nat) (j z:UInt32) (nf zb s a:Float) (o:UInt32):UInt32 := if o=0 then z else let r:=ziv (s+zb) (nf*(a+zb.abs)) E_quo n j (if r == Е then E_dotT X b k w else r) def E_win (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (u nf zb:Float) (b m t:USize) (s a:Float) (o:UInt32):UInt32 := if h:t< m then let v:=rdW X (b+(rdW off t).toUSize) let d:=dv v*u E_win X off k w n j z u nf zb b m (t+1) (s+d) (a+d.abs) (o ||| v) else E_tl X b k w n j z nf zb s a o termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega @[specialize] def G_x (f:USize->UInt32) (sx:USize) (W x:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if x< W then G_x f sx W (x+1) (b+sx) (q+1) (wrW out q (f b)) else out termination_by W-x @[specialize] def G_y (f:USize->UInt32) (sy sx:USize) (W H y:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if y< H then G_y f sy sx W H (y+1) (b+sy) (q+W.toUSize) (G_x f sx W 0 b q out) else out termination_by H-y @[specialize] def G_c (f:USize->UInt32) (sc sy sx:USize) (W H c ch:Nat) (b q:USize) (out:Array UInt32):Array UInt32 := if ch< c then G_c f sc sy sx W H c (ch+1) (b+sc) (q+(H*W).toUSize) (G_y f sy sx W H 0 b q out) else out termination_by c-ch def E_off (k w:Nat):Array UInt32 := Array.ofFn (n:=k*k) λt=>(t.val/k*w+t.val%k).toUInt32 @[noinline] def E_grid (c h w k n:Nat) (X off:Array UInt32) (j z:UInt32) (u nf zb zr:Float) : Array UInt32 := G_c (λb=>E_win X off k w n j z u nf zb b n.toUSize 0 zr zr 0) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (И (c*(h/k*(w/k))) 0) def avgF (c h w k:Nat) (X:Array UInt32):Array UInt32 := if c*h*w<=X.size∧X.size<2^32∧k*k<2^20 ∧ scan X X.usize 0 (qAf X) Е 0≠Е then let n:=k*k let off:=E_off k w let j:UInt32:=if n=4 then 2 else if n=64 then 6 else 255 let z:UInt32:=if 0< k∧k<17 then 0 else E_div n (dotFn n (λ_=>0) (λ_=>oneW)) E_grid c h w k n X off j z (dv oneW) (nfOf n) (dv 0) 0 else avgpoolSpec c h w k X def E_d (x y z:UInt32):FloatLib.Numerics.Dyadic := Model.addDyadic ⟨Bool.xor (decodeD x).negative (decodeD y).negative, (decodeD x).significand*(decodeD y).significand, (decodeD x).exponent+(decodeD y).exponent⟩ (decodeD z) lemma qGd (x y z:UInt32) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) : Model.fma (ofW x) (ofW y) (ofW z)=Ч Fmt (E_d x y z):=by rw[Model.Proof.fma_eq_spec];have e:=Ы x hx;have f:=Ы y hy;have g:=Ы z hz;simp[Model.Spec.fma,ofW,(Model.chooseNaN3_eq_none_iff _ _ _).2 ⟨Model.isNaN_eq_false_of_toDyadic?_some e,Model.isNaN_eq_false_of_toDyadic?_some f,Model.isNaN_eq_false_of_toDyadic?_some g⟩,Model.isInf_eq_false_of_toDyadic?_some e,Model.isInf_eq_false_of_toDyadic?_some f,Model.isInf_eq_false_of_toDyadic?_some g,e,f,g];rfl lemma qHq (x y z:UInt32) (hx:expOf x≠255) (hy:expOf y≠255) (hz:expOf z≠255) : ((E_d x y z).toRat:ℝ)=wv x*wv y+wv z:=by have j:=FloatLib.Numerics.Dyadic.mul_toRat (decodeD x) (decodeD y);simp only[FloatLib.Numerics.Dyadic.mul,FloatLib.Numerics.Dyadic.mulFields] at j;rw[E_d,Model.addDyadic,FloatLib.Numerics.Dyadic.add_toRat,j,qAq x hx,qAq y hy,qAq z hz];simp[rationalWord] lemma qHc (relu:Bool) (r:UInt32) (hf:expOf r≠255):E_act relu r=act relu r:=by cases relu<;>simp[E_act,act,reluW,hf,signOf] lemma qJ2 (relu:Bool) (s:Float) (h:zivS s≠Е) : E_act relu (zivS s)=act relu (zivS s):=by apply qHc;obtain ⟨S,ex,fr,h1,h2,h3,hz,-,-⟩:=qDq _ _ h;have hz':zivS s=_:=hz;intro h0;have h4:(expOf (zivS s)).toNat=255:=by rw[h0];rfl rw[qA0,hz',س] at h4;split at h4<;>omega lemma qHe (relu g:Bool) (sw tw w:UInt32) (hg:g=true->good sw=true∧good tw=true) : E_el relu g sw tw (dv sw) (dv tw) w=E_fma relu w sw tw:=by unfold E_el;split;swap;rfl;rename_i hc;obtain ⟨h1,h2⟩:=ذ.1 hc;obtain ⟨h3,h4⟩:=hg h1;dsimp only;split;rfl;rename_i hr;have r:=mt beq_iff_eq.2 hr;have hf:=qAd _ h2;have g1:=qAd _ h3;have g2:=qAd _ h4;rw[qJ2 _ _ r,qFj _ (E_d w sw tw) (S_E w sw tw h2 h3 h4 _ (qHq _ _ _ hf g1 g2)) r,E_fma,qGd _ _ _ hf g1 g2] lemma qEl (relu:Bool) (c hw:Nat) (s t:Array UInt32) (d:Nat) : ∀ (ch:Nat) (x:Array UInt32),ch+d=c->x.size=c*hw->x.size<2^32 → (E_chs relu c hw s t ch x).size=x.size∧∀ q,q< c*hw → (E_chs relu c hw s t ch x)[q]! =if ch*hw<=q then E_fma relu x[q]! s[q/hw]! t[q/hw]! else x[q]!:=by induction d with | zero => rintro ch x rfl _ _ rw[E_chs,if_neg א] exact ⟨rfl,λq hq=>(if_neg (by simpa using hq)).symm⟩ | succ d ih => intro ch x h1 h2 h3 rw[E_chs,if_pos א] dsimp only have h4:=Т hw (א:ch+1<=c) have h5:=Nat.add_one_mul ch hw have e1:=qAc ((ch+1)*hw) א have e0:=qAc (ch*hw) א obtain ⟨h6,h7⟩:=qFt (λ_=>E_el relu (good s[ch]! && good t[ch]!) s[ch]! t[ch]! (dv s[ch]!) (dv t[ch]!)) (L:=E_map _ ((ch+1)*hw).toUSize) (λ_ _=>by rw[E_map];rfl) hw (ch*hw).toUSize x א א h3 obtain ⟨h8,h9⟩:=ih (ch+1) _ א (h6.trans h2) א refine ⟨h8.trans h6,λq hq=>?_⟩ rw[h9 q hq,h7,e0,e1,qHe _ _ _ _ _ ذ.1] by_cases a:(ch+1)*hw<=q . rw[if_pos a,if_neg א,if_pos א] . rw[if_neg a];by_cases b:ch*hw<=q;. rw[if_pos א,if_pos b,Nat.div_eq_of_lt_le b א] . rw[if_neg א,if_neg b] lemma qFx:scaleF=scaleShiftSpec:=by funext relu c hw x s t;unfold scaleF;split . rename_i h;obtain ⟨h1,h2⟩:=h;obtain ⟨h3,h4⟩:=qEl relu c hw s t c 0 x א h1 h2;apply ه;. simp[scaleShiftSpec,h3,h1] . intro i hi hj;have hi':i< c*hw:=by rwa [h3,h1] at hi rw[<-Э _ i hi,h4 i hi',if_pos ג];simp[scaleShiftSpec,E_fma] . rfl lemma qDf (w:UInt32) (h:expOf w≠255) : в (ofW w)=(sv w:ℝ)*2^((idxOf w:ℤ)-149):=by simp[show Model.toReal (ofW w)=wv w from rfl,qAq w h,rationalWord,decodeD_toRat] lemma qJc (s j:UInt32) (hj:j.toNat<=6) (a:j.toNat<(expOf s).toNat) : (expOf (s-(j <<< (23:UInt32)))).toNat=(expOf s).toNat-j.toNat ∧ fracOf (s-(j <<< (23:UInt32)))=fracOf s ∧ signOf (s-(j <<< (23:UInt32)))=signOf s:=by rw[qA0] at a have hs:=s.toNat_lt have e:(j <<< (23:UInt32)).toNat=j.toNat*2^23:=by rw[UInt32.toNat_shiftLeft,щ] exact З (by simp;omega) generalize j <<< (23:UInt32)=J at e ⊢ have d:=ي s J (by rw[М,e];omega) rw[e] at d simp only[qA0,fracOf,<-UInt32.toNat_inj,qBj,qDl,d,decide_eq_decide] omega lemma qE5 (s y j:UInt32) (f:j.toNat<=6) (a:expOf s≠255) (b:j.toNat<(expOf s).toNat) (d:expOf y≠255) (g:signOf y=false) (e:mantOf y=2^23) (k:idxOf y=126+j.toNat) : toWord (Model.div (ofW s) (ofW y))=s-(j <<< (23:UInt32)):=by have B:Model.isZero (ofW y)=false:=by rw[ofW,Model.isZero_eq_beq_zero_of_toDyadic?_some (Ы y d),decodeD_sig_zero,e];decide obtain ⟨C,D,E⟩:=qJc s j f b;generalize s-(j <<< (23:UInt32))=r at C D E ⊢;have N:=expOf_toNat_lt s;have O:=mt Щ.1 a;simp at O;have l:expOf r≠255:=λh=>by simp[h] at C;omega have q:expOf r≠0:=λh=>by simp[h] at C;omega have t:expOf s≠0:=λh=>by simp[h] at b have x:в (ofW y)=2^j.toNat:=by rw[qDf _ d,sv,g,e,k];push_cast;rw[show (126+j.toNat-149:ℤ)=j.toNat-23 by ring,ن Р,к];field_simp;norm_num have I:в (ofW r)=в (ofW s)/в (ofW y):=by rw[x,qDf _ l,qDf _ a,show sv r=sv s by unfold sv mantOf;rw[E,D,if_neg q,if_neg t],show (idxOf r:ℤ)=idxOf s-j.toNat by unfold idxOf;rw[if_neg q,if_neg t,C];omega,sub_right_comm,ن Р,к,mul_div_assoc] have o:=qDs a;have u:=qDs d;have v:=qDs l;have z:=Model.isFinite_div_of_abs_div_le_posMaxFinite _ _ (by decide) o u B (I ▸ Model.abs_toReal_le_posMaxFinite_of_isIEEE_of_isFinite _ (by decide) v);have p:=Model.toReal_div_eq_roundAt _ _ (by decide) o u B z;rw[<-I,Model.roundAt_toReal_eq _ v] at p;rw[Model.eq_of_toReal_eq_of_nonzero z v p (p ▸ qIz r q l),qBo] lemma qF6 (n:Nat) (j s:UInt32) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) : E_quo n j s=E_div n s:=by unfold E_quo;split;swap;rfl;rename_i h;have h1:=ю.1 h.1;have h3:=expOf_toNat_lt s;rcases hj with rfl | ⟨rfl,rfl⟩ | ⟨rfl,rfl⟩;. simp at h1;omega all_goals exact (qE5 s _ _ (by decide) h.2 h1 (by decide) (by decide) (by decide) (by decide)).symm def E_f (xw:Nat->UInt32) (l:List Nat) (c:F1):F1 := l.foldl (λ(acc:F1) t=>let q:=dv (xw t)*dv oneW F1.mk (acc.s+q) (acc.t+q.abs)) c lemma qGk (X off:Array UInt32) (k w n:Nat) (j z:UInt32) (nf zb:Float) (b m:USize) (xw:Nat->UInt32) (hx:∀ t:USize,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:Nat):∀ (t:USize) (s a:Float) (o:UInt32),t.toNat+d=m.toNat → E_win X off k w n j z (dv oneW) nf zb b m t s a o=E_tl X b k w n j z nf zb (E_f xw (List.range' t.toNat d) ⟨s,a⟩).s (E_f xw (List.range' t.toNat d) ⟨s,a⟩).t ((List.range' t.toNat d).foldl (λc i=>c ||| xw i) o):=by induction d with | zero => intro t s a o h rw[E_win,dif_neg (by rw[А];omega)] rfl | succ d ih => intro t s a o h have ht:t< m:=А.2 א rw[E_win,dif_pos ht] dsimp only rw[hx t ht,ih (t+1) _ _ _ (by rw[usz t m ht];omega),usz t m ht,List.range'_succ] rfl lemma qJu (xw:Nat->UInt32) (l:List Nat):∀ o:UInt32,l.foldl (λc i=>c ||| xw i) o=0->o=0∧∀ i∈l,xw i=0:=by induction l with | nil=>simp | cons i l ih => intro o h;obtain ⟨h1,h2⟩:=ih _ h;obtain ⟨h3,h4⟩:=UInt32.or_eq_zero_iff.1 h1;exact ⟨h3,List.forall_mem_cons.2 ⟨h4,h2⟩⟩ lemma qIw (n:Nat) (hn:n<2^20) (xw:Nat->UInt32) (hg:∀ t,t< n->good (xw t)=true) (r:ziv ((E_f xw (К n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (К n) ⟨0,0⟩).t+(dv 0).abs))≠Е) : ziv ((E_f xw (К n) ⟨0,0⟩).s+dv 0) (nfOf n*((E_f xw (К n) ⟨0,0⟩).t+(dv 0).abs))=dotOf n xw (λ_=>oneW):=by obtain ⟨-,-,hbig⟩:=qBh _ _ r obtain ⟨hlo,hhi⟩:=qEh n hn xw (λ_=>oneW) 0 hg (λ_ _=>by decide) (by decide) (К n) List.nodup_range ג (λt h1 h2=>absurd (й.mpr h1) h2) _ _ ⟨rfl,rfl⟩ hbig have hsum:((∑ j∈р n,Д (xw j)*Д oneW:ℚ):ℝ) =exactS n xw (λ_=>oneW) 0:=by rw[exactS,qBc 0 (by decide) (by decide),add_zero,Rat.cast_sum] refine ر rfl λt ht=>?_ rw[Rat.cast_mul,qAq _ (qAd _ (hg t (ؽ.mp ht))),qAq oneW (by decide)] obtain ⟨d,hd,hw⟩:=qCe n xw (λ_=>oneW) (λj hj=>⟨qAd _ (hg j hj),by decide⟩) λh=>qF9 _ _ _ hlo hhi r (by rw[<-hsum,h,Rat.cast_zero]) have q:(d.toRat:ℝ)=exactS n xw (λ_=>oneW) 0:=by rw[hd,hsum] rw[hw] exact qIa _ _ d (by rw[q];exact hlo) (by rw[q];exact hhi) r lemma qHg (X:Array UInt32) (b:USize) (k w n:Nat) (hn:n<2^20) (j:UInt32) (hj:j=255∨(j=2∧n=4)∨(j=6∧n=64)) (xw:Nat->UInt32) (hg:∀ t,t< n->good (xw t)=true) (hs:E_dot X b k w=dotOf n xw (λ_=>oneW)) : E_tl X b k w n j (E_div n (dotFn n (λ_=>0) (λ_=>oneW))) (nfOf n) (dv 0) (E_f xw (К n) ⟨0,0⟩).s (E_f xw (К n) ⟨0,0⟩).t ((К n).foldl (λc i=>c ||| xw i) 0)=E_div n (dotOf n xw (λ_=>oneW)):=by unfold E_tl split next h1 => rw[qAe] unfold dotOf congr 3 funext i exact ((qJu xw _ _ h1).2 _ (й.2 i.2)).symm . dsimp only;rw[qF6 _ _ _ hj];congr 1;split;. rwa [qE7] next hr=>exact qIw _ hn xw hg (mt beq_iff_eq.2 hr) lemma qEd {c h w k ch y x t:Nat} (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (ht:t< k*k):(ch*h+y*k+t/k)*w+x*k+t%k< c*h*w:=by have hk:0< k:=ص (by rintro rfl;omega);have h1:=м ht;have h2:=э t hk;have h3:=(Nat.le_div_iff_mul_le hk).1 hy;have h4:=(Nat.le_div_iff_mul_le hk).1 hx;have h5:=Т h hc;rw[ػ] at h3 h4 h5;have h6:=Т w (א:ch*h+y*k+t/k+1<=c*h);rw[ػ] at h6;omega def G_P (N:Nat) (sp:Nat->UInt32) (out:Array UInt32) (p:Nat):Prop := out.size=N∧N<2^32∧∀ i,i< p->out[i]! = sp i lemma qKg {N p:Nat} {sp:Nat->UInt32} {out:Array UInt32} (h:G_P N sp out p) (hp:p< N) (q:USize) (hq:q.toNat=p):G_P N sp (wrW out q (sp p)) (p+1):=by obtain ⟨h1,h2,h3⟩:=h;refine ⟨(qA8 _ _ _).trans h1,h2,λi hi=>?_⟩;rw[qBd _ _ _ _ א א,hq];by_cases e:i=p;. rw[if_pos e,e] . rw[if_neg e];exact h3 i א lemma qH5 (a s i:Nat):(a+(i+1)*s).toUSize=(a+i*s).toUSize+s.toUSize:=by rw[show a+(i+1)*s=a+i*s+s by ring];simp only[Nat.toUSize_eq,USize.ofNat_add] lemma qEw {L:Nat->USize->USize->Array UInt32->Array UInt32} {g:USize->USize->Array UInt32->Array UInt32} {sb S n N q0 b0:Nat} {sp:Nat->UInt32} (hL:∀ i b q o,L i b q o =if i< n then L (i+1) (b+sb.toUSize) (q+S.toUSize) (g b q o) else o) (hs:∀ i o,i< n->G_P N sp o (q0+i*S) → G_P N sp (g (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+(i+1)*S)) (d:Nat) : ∀ i o,i+d=n->G_P N sp o (q0+i*S) → G_P N sp (L i (b0+i*sb).toUSize (q0+i*S).toUSize o) (q0+n*S):=by induction d with | zero => intro i o h1 h2 rw[hL,if_neg א,<-h1] exact h2 | succ d ih => intro i o h1 h2 rw[hL,if_pos א,<-qH5,<-qH5] exact ih (i+1) _ א (hs i o א h2) lemma qI5 {f:USize->UInt32} {sc sy sx W H c N:Nat} {sp:Nat->UInt32} (hN:c*(H*W)=N) (hs:∀ ch y t,ch< c->y< H->t< W → f (ch*sc+y*sy+t*sx).toUSize=sp (ch*(H*W)+y*W+t)) (o:Array UInt32) (ho:G_P N sp o 0) : G_P N sp (G_c f sc.toUSize sy.toUSize sx.toUSize W H c 0 0 0 o) N:=by have:=qEw (L:=G_c f sc.toUSize sy.toUSize sx.toUSize W H c) (sb:=sc) (S:=H*W) (n:=c) (N:=N) (q0:=0) (b0:=0) (sp:=sp) (λ_ _ _ _=>by rw[G_c]) (λch o a1 h=>by have h4:(ch+1)*(H*W)<=N:=hN ▸ Т _ a1 have:=qEw (L:=G_y f sy.toUSize sx.toUSize W H) (sb:=sy) (S:=W) (n:=H) (N:=N) (q0:=ch*(H*W)) (b0:=ch*sc) (sp:=sp) (λ_ _ _ _=>by rw[G_y]) (λy o a2 h=>by have h5:(y+1)*W<=H*W:=Т _ a2 rw[ػ] at h4 h5 have:=qEw (L:=G_x f sx.toUSize W) (g:=λb q o=>wrW o q (f b)) (sb:=sx) (S:=1) (n:=W) (N:=N) (q0:=ch*(H*W)+y*W) (b0:=ch*sc+y*sy) (sp:=sp) (λ_ _ _ _=>by rw[G_x];rfl) (λi o a3 h=>by have e1:ch*(H*W)+y*W+(i+1)*1 =ch*(H*W)+y*W+i*1+1 :=א have e2:ch*(H*W)+y*W+i=ch*(H*W)+y*W+i*1 :=א rw[e1,hs ch y i a1 a2 a3,e2] exact qKg h א _ (qAc _ (by have:=h.2.1;omega))) W 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) H 0 o א (by simpa using h) simpa [Nat.succ_mul,Nat.add_assoc] using this) c 0 o א (by simpa using ho) simp only[Nat.zero_mul,Nat.zero_add,Nat.add_zero,hN] at this exact this lemma qIi {c H W q:Nat} (hq:q< c*(H*W)) : q/(H*W)< c∧q/W%H< H∧q%W< W:=by have hp:0< H*W:=ص (by rintro e;simp[e] at hq);exact ⟨м (by rwa [ж]),э _ (Nat.pos_of_mul_pos_right hp),э _ (Nat.pos_of_mul_pos_left hp)⟩ lemma qIp (f:USize->UInt32) (c h w k:Nat) (hs:c*h*w<2^32) : G_c f (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (И (c*(h/k*(w/k))) 0) = Array.ofFn (n:=c*(h/k*(w/k))) λq =>let ch:=q.val/(h/k*(w/k)) let y:=q.val/(w/k)%(h/k) let x:=q.val%(w/k) f ((ch*h+y*k)*w+x*k).toUSize:=by have hN:c*(h/k*(w/k))<2^32:=by have h1:h/k*(w/k)<=h*w:=Nat.mul_le_mul (Nat.div_le_self _ _) (Nat.div_le_self _ _) exact ц ((Nat.mul_le_mul_left c h1).trans (Nat.mul_assoc c h w).symm.le) hs generalize hH:h/k=H at * generalize hW:w/k=W at * have hp:=qI5 (f:=f) (sc:=h*w) (sy:=k*w) (sx:=k) (W:=W) (H:=H) (c:=c) (sp:=λq=>f ((q/(H*W)*h+q/W%H*k)*w+q%W*k).toUSize) rfl (λch y t h1 h2 h3=>by obtain ⟨d1,d2⟩:=qDn W (H*ch+y) t h3 obtain ⟨d3,d4⟩:=qDn H ch y h2 have e:ch*(H*W)+y*W+t=W*(H*ch+y)+t:=by ring rw[e,d2,ж H W,<-Nat.div_div_eq_div_mul,d1,d3,d4] congr 2 ring) (И (c*(H*W)) 0) ⟨by simp,hN,by simp⟩ apply ه (by rw[hp.1,Array.size_ofFn]) intro i h1 h2 rw[<-Э _ i h1,hp.2.2 i (by simpa using h2),Array.getElem_ofFn] lemma qG7 {c h w k ch y x:Nat} {X:Array UInt32} (h1:c*h*w<2^32) (h2:X.size<2^32) (h3:k*k<2^32) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (t:USize) (ht:t.toNat< k*k) : rdW X (((ch*h+y*k)*w+x*k).toUSize+(rdW (E_off k w) t).toUSize) =poolAt h w k X ch y x t.toNat:=by have e0:=م (ch*h+y*k) (t.toNat/k) w have h4:=qEd hc hy hx ht rw[qAj _ _ h2,qAj _ _ (by rwa [E_off,Array.size_ofFn]),E_off,qAa _ (λt=>(t/k*w+t%k).toUInt32) _ ht,poolAt] generalize hu:t.toNat/k*w+t.toNat%k=u have e1:u.toUInt32.toUSize.toNat=u:=by rw[н,Nat.toUInt32_eq,س,З א] rw[qAo _ _ (by rw[e1,qAc _ א];omega),e1,qAc _ א] congr 1 omega lemma qJs {c h w k ch y x:Nat} {X:Array UInt32} (h1:c*h*w<=X.size) (h2:X.size<2^32) (h3:k*k<2^20) (hg:∀ i,i< X.size->good X[i]! = true) (hc:ch< c) (hy:y< h/k) (hx:x< w/k) (j:UInt32) (hj:j=255∨(j=2∧k*k=4)∨(j=6∧k*k=64)) : E_win X (E_off k w) k w (k*k) j (E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW))) (dv oneW) (nfOf (k*k)) (dv 0) ((ch*h+y*k)*w+x*k).toUSize (k*k).toUSize 0 0 0 0 = E_div (k*k) (dotOf (k*k) (poolAt h w k X ch y x) (λ_=>oneW)):=by have hn:=qAc (k*k) א have hk:0< k:=ص (by rintro rfl;simp at hy) have hI t (ht:t< k*k):=qEd hc hy hx ht have e2:=qAc ((ch*h+y*k)*w+x*k) (by have:=hI 0 (Nat.mul_pos hk hk);simp at this;omega) rw[qGk X _ k w (k*k) j _ _ _ _ _ (poolAt h w k X ch y x) (λt ht=>qG7 א h2 א hc hy hx t (hn ▸ А.1 ht)) (k*k) 0 0 0 0 ((Л _).trans hn.symm),USize.toNat_zero,<-Գ] refine qHg X _ k w _ h3 j hj _ (λt ht=>hg _ (by have:=hI t ht;omega)) ?_ rw[E_dot,qAe] congr 1 funext t rw[e2,poolAt] congr 1 ring lemma qJ4:∀ k,k<17->0< k->E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW))=0:=by decide +kernel lemma qHr:avgF=avgpoolSpec:=by funext c h w k X;unfold avgF;split . rename_i hc;obtain ⟨h1,h2,h3,h4⟩:=hc;simp only[avgpoolSpec,avgpoolWith] rw[show (if 0< k∧k<17 then (0:UInt32) else E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW)))=E_div (k*k) (dotFn (k*k) (λ_=>0) (λ_=>oneW)) by split;. rename_i e;exact (qJ4 k e.2 e.1).symm . rfl] rw[E_grid,qIp _ c h w k א];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qIi hq;dsimp only;refine qJs h1 h2 h3 (qAp X h2 h4).2.2.1 a1 a2 a3 _ ?_;split_ifs with e1 e2;. exact Or.inr (Or.inl ⟨rfl,e1⟩) . exact Or.inr (Or.inr ⟨rfl,e2⟩) . exact Or.inl rfl . rfl end section @[inline] def ordW (w:Word):UInt32 := let m:=w &&& В if w >>> (31:UInt32) == 0 then х+m else х-m @[inline] def maxF (a b:Word):Word := if a &&& Ц == Ц || b &&& Ц == Ц then maxW a b else let p:=ordW a let q:=ordW b if p< q then b else if q< p then a else if a == b then a else maxW a b def M_K (m:Nat):Nat := if m/2^23=0 then m%2^23 else (2^23+m%2^23)*2^(m/2^23-1) lemma qBp:StrictMono M_K:=by refine strictMono_nat_of_lt_succ λm=>?_;unfold M_K;by_cases hc:m%2^23+1<2^23 . rw[show (m+1)/2^23=m/2^23 by omega,show (m+1)%2^23=m%2^23+1 by omega];split;. omega . exact б א (Nat.two_pow_pos _) . rw[show (m+1)/2^23=m/2^23+1 by omega,show (m+1)%2^23=0 by omega,show m%2^23=2^23-1 by omega];rcases m/2^23 with _ | e;. norm_num . have:=Nat.two_pow_pos e;simp[pow_succ];omega lemma qGz (w:Word):(w &&& В).toNat=w.toNat%2^31 ∧ (expOf w).toNat=w.toNat%2^31/2^23∧(fracOf w).toNat=w.toNat%2^31%2^23:=by have:=w.toNat_lt;rw[qAt,qA0,fracOf,qBj];omega lemma qIe (w:Word):mantOf w <<< idxOf w=M_K (w.toNat%2^31):=by obtain ⟨-,h1,h2⟩:=qGz w;unfold mantOf idxOf M_K;rw[щ,<-h1,<-h2];simp only[<-UInt32.toNat_inj,UInt32.toNat_zero];split<;>simp[*] lemma qGw (w:Word):keyOf w=if w.toNat<2^31 then (M_K (w.toNat%2^31):Int) else -(M_K (w.toNat%2^31):Int):=by simp only[keyOf,qIe,qDl,decide_eq_true_eq];omega lemma qG3 (w:Word):(ordW w).toNat =if w.toNat<2^31 then 2^31+w.toNat%2^31 else 2^31-w.toNat%2^31:=by have h:=w.toNat_lt;have a:(х:UInt32).toNat=2^31:=rfl;simp[ordW,<-UInt32.toNat_inj,Nat.shiftRight_eq_div_pow];split<;>simp only[UInt32.toNat_add,UInt32.toNat_sub,qAt,a]<;>omega lemma qH4 (a b:Word) (h:ordW a< ordW b):keyOf a< keyOf b:=by rw[ю,qG3,qG3] at h;rw[qGw,qGw];generalize hA:a.toNat%2^31=A at *;generalize hB:b.toNat%2^31=B at *;have i:=@qBp A B;have j:=@qBp B A;have p:=@qBp 0 A;have q:=@qBp 0 B;split at h<;>split at h<;>omega lemma qJk (x m n:Nat):x &&& (m <<< n)=((x >>> n) &&& m) <<< n := Nat.eq_of_testBit_eq λi=>by by_cases h:n<=i<;>simp[h] lemma qGt (w:Word) (h:¬ (w &&& Ц == Ц)=true):expOf w≠255:=by intro e;apply h;have a:=qA0 w;rw[e] at a;rw[beq_iff_eq,<-Щ,ز,show (Ц:UInt32).toNat=255 <<< 23 from rfl,qJk,show (255:Nat)=2^8-1 from rfl,О,т,<-a];rfl lemma qIu (a:Word) (hz:Model.isZero (ofW a)=true):a=0∨a=х:=by obtain ⟨-,h1,h2⟩:=qGz a;have h:=a.toNat_lt;rw[ofW,ofUInt32_eq_ofFields,Model.isZero_ofFields Fmt _ _ _ (expOf_toNat_lt a) (fracOf_toNat_lt a)] at hz;simp[FloatFormat.binary32] at hz;rw[<-Щ,<-Щ];show a.toNat=0∨a.toNat=2^31;omega lemma qHv (a:Word) (ha:expOf a≠255):maxW a a=a:=by unfold maxW;rw[Х (by simpa using ha),compare_eq_iff_eq.2 rfl];dsimp only;by_cases hz:Model.isZero (ofW a)=true;. rcases qIu a hz with rfl | rfl<;>decide . rw[Х (by simpa using hz),qBo] lemma qHs (a b:Word):maxF a b=maxW a b:=by unfold maxF;by_cases g:(a &&& Ц == Ц || b &&& Ц == Ц)=true;. rw[з g] . rw[Х g];simp only[Bool.or_eq_true,not_or] at g;have ha:=qGt a g.1;have hb:=qGt b g.2;have hn:¬ (expOf a=255∨expOf b=255):=by simp[ha,hb] dsimp only;by_cases p:ordW a< ordW b;. rw[з p];unfold maxW;rw[Х hn,compare_lt_iff_lt.mpr (qH4 a b p)] . rw[Х p];by_cases q:ordW b< ordW a;. rw[з q];unfold maxW;rw[Х hn,compare_gt_iff_gt.mpr (qH4 b a q)] . rw[Х q];by_cases e:(a == b)=true;. rw[з e,eq_of_beq e,qHv b hb] . rw[Х e] def mpWin (X off:Array UInt32) (b m t:USize) (acc:UInt32):UInt32 := if h:t< m then mpWin X off b m (t+1) (maxF acc (rdW X (b+(rdW off t).toUSize))) else acc termination_by m.toNat-t.toNat decreasing_by rw[usz t m h];have:=А.mp h;omega def maxpoolF (c h w k:Nat) (inp:Array Word):Array Word := if c*h*w<2^32∧inp.size<2^32∧k*k<2^32 then let off:=E_off k w G_c (λb=>mpWin inp off b (k*k).toUSize 0 (rdW inp b)) (h*w).toUSize (k*w).toUSize k.toUSize (w/k) (h/k) c 0 0 0 (И (c*(h/k*(w/k))) 0) else maxpoolW c h w k inp lemma qFy (X off:Array UInt32) (b m:USize) (xw:Nat->UInt32) (hx:∀ t:USize,t< m->rdW X (b+(rdW off t).toUSize)=xw t.toNat) (d:Nat) : ∀ (t:USize) (a:UInt32),t.toNat+d=m.toNat → mpWin X off b m t a=(List.range' t.toNat d).foldl (λa i=>maxF a (xw i)) a:=by induction d with | zero => intro t a h rw[mpWin,Ъ (by rw[А];omega)] rfl | succ d ih => intro t a h have ht:t< m:=by rw[А];omega rw[mpWin,У ht,hx t ht,ih (t+1) _ (by rw[usz t m ht];omega),usz t m ht,List.range'_succ,Ա] lemma qDz:maxpoolF=maxpoolSpec:=by rw[<-qD0];funext c h w k inp;unfold maxpoolF;split . rename_i hc;obtain ⟨h1,h2,h3⟩:=hc;rw[qIp _ c h w k h1];simp only[maxpoolW];apply congrArg Array.ofFn;funext ⟨q,hq⟩;obtain ⟨a1,a2,a3⟩:=qIi hq;dsimp only;have hn:(k*k).toUSize.toNat=k*k:=qAc _ h3 have e:∀ {ch y x:Nat},ch< c->y< h/k->x< w/k → rdW inp ((ch*h+y*k)*w+x*k).toUSize=poolAt h w k inp ch y x 0:=by intro ch y x b1 b2 b3 have h4:=qEd (t:=0) b1 b2 b3 (by rcases Nat.eq_zero_or_pos k with rfl | h0;. simp at b2 . exact Nat.mul_pos h0 h0) simp only[Nat.zero_div,Nat.zero_mod,Nat.add_zero] at h4;rw[qAj _ _ h2,qAc _ א];simp[poolAt] rw[e a1 a2 a3,qFy inp _ _ _ (poolAt h w k inp _ _ _) (λt ht=>qG7 h1 h2 h3 a1 a2 a3 t (by rw[А,hn] at ht;exact ht)) (k*k) 0 _ (by rw[hn,show (0:USize).toNat=0 from rfl,Л]),show (0:USize).toNat=0 from rfl,<-Գ];simp only[qHs] . rfl end @[specialize] def fC {σ:Type} (f:σ->USize->USize->Word) (s:σ) (b j:USize) (o:Array Word):Nat->Array Word | 0=>o | k+1=>fC f s b (j+1) (wrW o (b+j) (f s (b+j) j)) k @[specialize] def fR {σ:Type} (h:Nat->σ) (f:σ->USize->USize->Word) (n r:Nat) (o:Array Word):Nat->Array Word | 0=>o | k+1=>fR h f n (r+1) (fC f (h r) (r*n).toUSize 0 o n) k def gemmF (batch m k n:Nat) (A B:Array Word):Array Word := if batch == 1 && A.size == m*k && B.size == k*n && decide (0< n) && decide (0< k) && A.all finiteWord && B.all finiteWord && decide (k*n<2^32) then let nu:=n.toUSize linF m k n false A (fR (λr=>r.toUSize) (λu _ t=>rdW B (t*nu+u)) k 0 (И (n*k) 0) n) (И n х) else gemmSpec batch m k n A B structure LnS where fast:Bool m:Float ir:Float em:Float den:UInt32 deriving Inhabited def lnSum (x:Array Word) (base n i:USize) (s t:Float):F1 := if h:i< n then let v:=dv (rdW x (base+i)) lnSum x base n (i+1) (s+v) (t+v.abs) else ⟨s,t⟩ termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];have:=А.mp h;omega def lnVar (x:Array Word) (base n i:USize) (m v:Float):Float := if h:i< n then let d:=dv (rdW x (base+i))-m lnVar x base n (i+1) m (v+d*d) else v termination_by n.toNat-i.toNat decreasing_by rw[usz i n h];have:=А.mp h;omega @[inline] def invOK (a b:Float):Bool := decide ((a.toBits >>> 52)-1<1222) && decide ((((a*b-П 0x3FF0000000000000)+П 0x3CD0000000000000).toBits >>> 52)-972<2) @[inline] def lnQ (b:Bool) (n:Nat) (x:Array Word) (r:Nat):ℚ:=if b then 0 else rowMean n x r def lnStat (b:Bool) (n:Nat) (eps:Word) (x:Array Word) (r:Nat):LnS := let base:=(r*n).toUSize let nu:=n.toUSize let nd:=n.toUInt64.toFloat let nf:=(n.toUInt64+8).toFloat*П ط let ind:=1.0/nd let a:=if b then F1.mk 0 0 else lnSum x base nu 0 0 0 let m:=if b then 0 else a.s*ind let v:=lnVar x base nu 0 m 0 let em:=(nf*a.t)*ind+П ك let tt:=v*ind+dv eps let d:=ф tt let z:=ziv d ((nf+nf)*d+em) let ir:=1.0/dv z if sm z && decide ((0x1F800000:UInt32)<=z) && decide (z<=(0x70FFFFFF:UInt32)) && decide ((tt.toBits >>> 52)-1<1222) && invOK ind nd && invOK ir (dv z) then ⟨true,m,ir,em,z⟩ else ⟨false,0,0,0,sqrtRational (rowVariance n x r (lnQ b n x r)+Д eps)⟩ @[noinline] def lnSlow (x g b den:Word) (mean:Unit->ℚ):Word := roundRational ((Д x-mean ())*Д g/Д den+Д b) @[inline] def lnOut (s:LnS) (w gw bw:Word) (mean:Unit->ℚ):Word := if s.fast then let bv:=dv bw let gd:=dv gw*s.ir let q:=(dv w-s.m)*gd let r:=ziv (q+bv) (П 0x3D10000000000000*(q.abs+bv.abs)+s.em*gd.abs) if 0x7EFFFFFF<(r &&& В) then lnSlow w gw bw s.den mean else r else lnSlow w gw bw s.den mean def lnF (rows n:Nat) (eps:Word) (x g b:Array Word):Array Word := if stableNormDomain rows n eps x g && b.size == n && b.all finiteWord && x.all sm && g.all sm && b.all sm && sm eps && decide (n<2^20) && decide (rows*n<2^32) then let st:=Array.ofFn (n:=rows) λr=>lnStat false n eps x r.val fR (λr=>(st[r]!,λ_:Unit=>rowMean n x r)) (λs p j=>lnOut s.1 (rdW x p) (rdW g j) (rdW b j) s.2) n 0 (И (rows*n) 0) rows else layernormSpec rows n eps x g b def rmsF (rows n:Nat) (eps:Word) (x g:Array Word):Array Word := if stableNormDomain rows n eps x g && x.all sm && g.all sm && sm eps && decide (n<2^20) && decide (rows*n<2^32) then let st:=Array.ofFn (n:=rows) λr=>lnStat true n eps x r.val fR (λr=>st[r]!) (λs p j => let w:=rdW x p if w &&& В == 0 then 0 else lnOut s w (rdW g j) 0 λ_=>0) n 0 (И (rows*n) 0) rows else rmsnormSpec rows n eps x g section lemma qGu (x:UInt64):(x >>> 52).toNat=x.toNat/2^52:=by rw[ъ,т];rfl lemma qEf (x k N:UInt64) (h:x-k< N) (hN:k.toNat+N.toNat<2^64) : k.toNat<=x.toNat∧x.toNat< k.toNat+N.toNat:=by have h1:=я.mp h;rw[UInt64.toNat_sub] at h1;have h2:=x.toNat_lt;have h3:=k.toNat_lt;omega lemma qEx (a b:ℤ) (n:ℕ) (h:a=n+b):(2:ℝ)^a=2^n*2^b:=by rw[h,а Р,к] lemma qC8 {f:Float} {E:Nat} (h:(f.toBits >>> 52).toNat=E) (hE1:1<=E) (hE:E<2047) : f.isFinite=true∧(2:ℝ)^((E:ℤ)-1023)<=R f∧R f<2^((E:ℤ)-1022):=by rw[qGu] at h;obtain ⟨F,hF,hM⟩:∃ F<2^52,f.toBits.toNat=E*2^52+F :=⟨f.toBits.toNat%2^52,by omega,by omega⟩;obtain ⟨h1,h2⟩:=B_R f false E F hE1 hE hF (by simpa using hM);have hb:(0:ℝ)<2^((E:ℤ)-1075):=ы ב _;have hF0:(0:ℝ)<=F:=Ю _;have hF2:(F:ℝ)<2^52:=by exact_mod_cast hF simp only[Bool.false_eq_true,ite_false,one_mul] at h2;push_cast at h2;rw[h2,qEx ((E:ℤ)-1023) (E-1075) 52 א,qEx ((E:ℤ)-1022) (E-1075) 53 א];exact ⟨h1,Б ד hb.le,б ד hb⟩ lemma L_k (c:UInt64) (E n:Nat) (hE1:1<=E) (hn:E+n=1023) (h:(П c).toBits.toNat=E*2^52) : (П c).isFinite=true∧R (П c)=1/2^n:=by obtain ⟨h1,h2⟩:=B_R (П c) false E 0 hE1 א ב (by simpa using h);refine ⟨h1,?_⟩;rw[h2,show (E:ℤ)-1075=-(52+(n:ℤ)) by omega,zpow_neg,а Р];norm_num;field_simp lemma qHt {f:Float} {E:Nat} (h:(f.toBits >>> 52).toNat=E) (hE1:1<=E) (hE:E<2047) : (ф f).isFinite=true∧0< R f ∧ |R (ф f)-п (R f)| ≤ 1/2^53*(п (R f)+A_z):=by obtain ⟨hf,hl,-⟩:=qC8 h hE1 hE have hp:0< R f:=ч (ы ב _) hl rw[qGu] at h have hs:Model.signBit (toModel (ofFloat f))=false:=by rw[qG4];simp only[decide_eq_false_iff_not,not_le];omega have hm:toModel (ofFloat f)=A_M f:=toModel_ofFloat f have hfm:Я (A_M f)=true:=by rw[qAk,hf] rw[hm] at hs have hr:=Model.toReal_sqrt_eq_roundAt (A_M f) (by decide) hfm (Or.inr hs) have hq:=Model.isFinite_sqrt_of_isFinite (A_M f) (by decide) hfm (Or.inr hs) have hd:A_M (ф f)=Model.canonicalizeModel (Model.sqrt (A_M f)):=by have h3:=toModel_ofFloat_sqrt f rw[hm,toModel_ofFloat] at h3 exact h3 have ht:Model.toModel (A_M (ф f))=Model.toModel (Model.sqrt (A_M f)):=by rw[hd,Model.toModel_canonicalizeModel] have hR:R (ф f)=Model.roundAt FloatFormat.binary64 (п (R f)):=by rw[A_R,о qAm,ht,<-о qAm] exact hr refine ⟨?_,hp,?_⟩ . rw[<-qAk,qA2,ht,<-qA2];exact hq . rw[hR];have:=qGx (п (R f));rwa [Ж (Real.sqrt_nonneg _)] at this lemma qAb (x y:ℝ) : |x| ≤ |y| + |x-y|:=by have:=abs_sub_abs_le_abs_sub x y;linarith lemma qAw {k:ℝ} (h:k<=2^256*2^44):k<=A_B :=h.trans_eq ((qEx 300 44 256 rfl).trans ב).symm lemma qBn (a:Array UInt32) (p:UInt32->Bool) (h:a.all p=true) (i:ℕ) (hi:i< a.size) : p a[i]! = true:=by rw[Э a i hi];simpa using Array.all_eq_true.1 h i hi lemma qF4 (w:UInt32) (g:good w=true) (h:w &&& В=0):Д w=0:=by exact_mod_cast (qAq w (qAd _ g)).symm.trans (qBc w g h) lemma qKm (s e:Float) (V:ℝ) (hlo:R (s-e)< V) (hhi:V< R (s+e)) (h0:ziv s e≠Е) (h:¬ (0x7EFFFFFF:UInt32)<(ziv s e &&& В)) : |V| ≤ 2^(127:ℤ):=by obtain ⟨S,ex,fr,W,h1,h2,h3,hz,hV,hW1,-,hW3⟩:=qCd s e V hlo hhi h0 have hb:(0:ℝ)<2^((ex:Int)-1076):=ы ב _ have hW0:0< W:=lt_trans (mul_pos ב hb) hW3 have hN:((if S then 2^31 else 0)+(ex-896)*2^23+fr)<2^32:=by split<;>omega have hex:ex<=1149:=by by_contra hc apply h apply ю.mpr rw[qAt,hz,UInt32.toNat_ofNat_of_lt' hN,(א:ex=1150)] show 2130706431< _ split<;>omega have hF:(fr:ℝ)+1<=2^23:=by exact_mod_cast (א:fr+1<=2^23) rw[hV] have hW:W<=2^54*2^(73:ℤ):=hW1.le.trans (С ד (Ш ב א) hb.le (by positivity)) rw[<-qEx 127 73 54 rfl] at hW split<;>simp only[abs_neg,abs_of_pos hW0,hW] lemma L_I {n I:ℝ} (hn:1<=n) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48):I<=2:=by have:=Г hn h3.le;linarith [(abs_le.mp h4).2] lemma qGy (n I v t Sb E T:ℝ) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (ht : |t| ≤ Sb) (hv : |v-n*t| ≤ n*E+T) (hE:0<=E) : |v*I-t| ≤ E*(1+1/2^48)+T*I+Sb*(1/2^48):=by have a1 : |(v-n*t)*I| ≤ (n*E+T)*I:=by rw[е,г h3];exact Б hv h3.le have a2:=Г (abs_le.mp h4).2 hE have a3 : |t*(I*n-1)| ≤ Sb*(1/2^48):=by rw[е];exact С ht h4 (Й _) ((Й _).trans ht) have a4:=Ь ((v-n*t)*I) (t*(I*n-1)) rw[show (v-n*t)*I+t*(I*n-1)=v*I-t by ring] at a4 linarith section variable {z:ℝ} (hz0:0<=z) (hz:z<=1/2^200*(1/2^200)) include hz0 hz lemma qEj {P rp rs rc:ℝ} (h1 : |rp-P| ≤ 1/2^53*(|P| + z)) (h2 : |rs-(rp-1)| ≤ 1/2^53*(|rp-1| + z)) (h3 : |rc-(rs+1/2^50)| ≤ 1/2^53*(|rs+1/2^50| + z)) (lo:1/2^51<=rc) (hi:rc<=1/2^49) : |P-1| ≤ 1/2^48:=by have a1:=qAb (rs+1/2^50) rc;rw[и (rs+1/2^50) rc] at a1;have a2 : |rc| = rc:=Ж (ؾ ב lo);obtain ⟨c1,c2⟩:=abs_le.mp h3;have a3 : |rs| ≤ 1/2^49:=by rw[abs_le];constructor<;>linarith [Й (rs+1/2^50)] have b1:=qAb (rp-1) rs;rw[и (rp-1) rs] at b1;have b2 : |rp-1| ≤ 1/2^48:=by linarith [Й (rp-1)] have c3:=qAb (P-1) (rp-1);rw[show P-1-(rp-1)=-(rp-P) by ring,abs_neg] at c3;have c4:=qAb P 1;rw[abs_one] at c4;linarith [Й (P-1)] lemma qIt {a Mr Rm Em g D b I RA Rg Rq RY RE Rsa Rcs Reg Hi Lo:ℝ} (hE0:0<=Em) (hE1:Em<=2^120) (hM:2 * |Mr-Rm| ≤ Em) (hD:1/2^64<=D) (ha : |a| ≤ 2^99) (hg : |g| ≤ 2^99) (hm : |Rm| ≤ 2^110) (h1 : |RA-(a-Rm)| ≤ 1/2^53*(|a-Rm| + z)) (h2 : |I*D-1| ≤ 1/2^48) (h3 : |Rg-g*I| ≤ 1/2^53*(|g*I| + z)) (h4 : |Rq-RA*Rg| ≤ 1/2^53*(|RA*Rg| + z)) (h5 : |RY-(Rq+b)| ≤ 1/2^53*(|Rq+b| + z)) (h6 : |Rsa-(|Rq| + |b|)| ≤ 1/2^53*(|(|Rq| + |b|)| + z)) (h7 : |Rcs-1/2^46*Rsa| ≤ 1/2^53*(|1/2^46*Rsa| + z)) (h8 : |Reg-Em*(|Rg|)| ≤ 1/2^53*(|Em*(|Rg|)| + z)) (h9 : |RE-(Rcs+Reg)| ≤ 1/2^53*(|Rcs+Reg| + z)) (h10 : |Hi-(RY+RE)| ≤ 1/2^53*(|RY+RE| + z)) (h11 : |Lo-(RY-RE)| ≤ 1/2^53*(|RY-RE| + z)) (hbig:1/2^127 ≤ |Hi|) : Lo<(a-Mr)*g/D+b∧(a-Mr)*g/D+b< Hi:=by have hDp:0< D:=ч (by positivity) hD;generalize hG:g/D=G;generalize hX:a-Rm=X at h1;rw[show (a-Mr)*g/D+b=X*G+(Rm-Mr)*G+b by rw[<-hG,<-hX];field_simp;ring];have hGb : |G| ≤ 2^163:=by rw[<-hG,abs_div,г hDp,div_le_iff₀ hDp];linarith have hXb : |X| ≤ 2^111:=hX ▸ (Ե _ _).trans ד;have hgI : |g*I-G| ≤ 1/2^48 * |G|:=by rw[show g*I-G=G*(I*D-1) by rw[<-hG];field_simp,е,mul_comm];exact Б h2 (Й _) generalize g*I=Y at h3 hgI;clear hG hX hDp hD ha hg hm h2;have nX:=Й X;have nG:=Й G;have nA:=Й RA;have nR:=Й Rg;have a1:=qAb X RA;rw[и X RA] at a1;have a3 : |X| ≤ (1+2/2^53) * |RA| + 2/2^53*z :=ד;have a4 : |X-RA| ≤ 2/2^53 * |RA| + 2/2^53*z:=by rw[и];linarith have a5 : |RA| ≤ 2^165:=by have:=qAb RA X;linarith have b1:=qAb Y G;have b2:=qAb G Rg;have b3:=abs_sub_le G Y Rg;rw[и G Y,и Y Rg] at b3;have b4 : |G| ≤ (1+1/2^47) * |Rg| + 2/2^53*z :=ד;have b5 : |G-Rg| ≤ 2/2^48 * |Rg| + 2/2^53*z :=ד;have c1 : |Rg| ≤ 2^165:=by have:=qAb Rg G;rw[и] at this;linarith have f1:=Ь ((X-RA)*G) (RA*(G-Rg));rw[е,е,show (X-RA)*G+RA*(G-Rg)=X*G-RA*Rg by ring] at f1;have f2:=С a4 b4 nG ד;have f3:=Г b5 nA;have zr:z * |RA| ≤ z*2^165:=Г a5 hz0;have zg:z * |Rg| ≤ z*2^165:=Г c1 hz0;have zz:z*z<=z*2^165:=Г ד hz0;have ze:z*Em<=z*2^165:=Г ד hz0;have nzr:=Н hz0 nA;have nzg:=Н hz0 nR;have nzz:=Н hz0 hz0;have nP:=Н nA nR;have f4 : |X*G-RA*Rg| ≤ 9/2^50*(|RA| * |Rg|)+1/2^230 :=ד;have p1 : |RA*Rg| ≤ (1+2/2^53) * |Rq| + 2/2^53*z:=by have:=qAb (RA*Rg) Rq;rw[и (RA*Rg) Rq] at this;have nq:=Й (RA*Rg);linarith rw[<-е] at f4;have q1 : |X*G-Rq| ≤ 5/2^49 * |Rq| + 1/2^229:=by have:=Ե (X*G-RA*Rg) (Rq-RA*Rg);rw[show X*G-RA*Rg-(Rq-RA*Rg)=X*G-Rq by ring] at this;have nq:=Й Rq;linarith generalize hT:Em * |Rg| = T at h8;have nT:0<=T:=hT ▸ Н hE0 nR;have e1 : |(Rm-Mr)*G| ≤ 5/2^3*T+1/2^230:=by rw[е,и];have h : |Mr-Rm| * |G| ≤ (Em/2)*((1+1/2^47) * |Rg| + 2/2^53*z) :=С ד b4 nG ד;have:=Н hz0 hE0;subst hT;linarith rw[Ж nT] at h8;have nq:=Й Rq;have nb:=Й b;have nS:0 ≤ |Rq| + |b| :=ד;rw[Ж nS] at h6;have s0 : |Rq+b| ≤ |Rq| + |b|:=Ь _ _;have s1 : |Rsa| ≤ 2*(|Rq| + |b|)+z:=by have:=qAb Rsa (|Rq| + |b|);rw[Ж nS] at this;linarith have h7' : |1/2^46*Rsa| = 1/2^46 * |Rsa|:=by rw[е,г (by positivity:(0:ℝ)<1/2^46)] rw[h7'] at h7;have s2 : |Rcs| ≤ 1/2^44*(|Rq| + |b|)+z:=by have:=qAb Rcs (1/2^46*Rsa);rw[h7'] at this;linarith have s3 : |Reg| ≤ 2*T+z:=by have:=qAb Reg T;rw[Ж nT] at this;linarith have s4 : |RY| ≤ 2*(|Rq| + |b|)+z:=by have:=qAb RY (Rq+b);linarith have s5:=Ь Rcs Reg;have s6 : |RE| ≤ 1/2^43*(|Rq| + |b|)+4*T+4*z:=by have:=qAb RE (Rcs+Reg);linarith have s7:=Ь RY RE;have s8:=Ե RY RE;have big:1/2^130 ≤ |Rq| + |b| + T:=by have:=qAb Hi (RY+RE);linarith obtain ⟨u1,u2⟩:=abs_le.mp q1;obtain ⟨v1,v2⟩:=abs_le.mp e1;obtain ⟨w1,w2⟩:=abs_le.mp h5;obtain ⟨x1,x2⟩:=abs_le.mp h6;obtain ⟨y1,y2⟩:=abs_le.mp h7;obtain ⟨z1,z2⟩:=abs_le.mp h8;obtain ⟨o1,o2⟩:=abs_le.mp h9;obtain ⟨r1,r2⟩:=abs_le.mp h10;obtain ⟨t1,t2⟩:=abs_le.mp h11;constructor<;>linarith lemma qIo {n Rs Rt I Rm N Y Em c:ℝ} (r Sb:ℝ) (hn1:1<=n) (hn2:n<=2^20) (hc:c=(n+8)/2^49) (hr : |r| ≤ Sb) (hS:Sb<=2^99) (h1 : |Rs-n*r| ≤ 2*n*(1/2^53*(n*Sb+z))) (h2 : |Rt-n*Sb| ≤ 2*n*(1/2^53*(n*Sb+z))) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (h5 : |Rm-Rs*I| ≤ 1/2^53*(|Rs*I| + z)) (h6 : |N-c*Rt| ≤ 1/2^53*(|c*Rt| + z)) (h7 : |Y-N*I| ≤ 1/2^53*(|N*I| + z)) (h8 : |Em-(Y+1/2^200)| ≤ 1/2^53*(|Y+1/2^200| + z)) : 2 * |r-Rm| ≤ Em∧Em<=2^120∧1/2^201<=Em∧|Rm| ≤ 2^110:=by have hS0:0<=Sb:=ؾ (Й _) hr;have nS:0<=n*Sb:=Н ד hS0;have nSb:n*Sb<=2^20*2^99:=С hn2 hS hS0 ב;generalize hW:n*Sb+z=W at h1 h2;have nW:0<=W :=ד;have hI2:=L_I hn1 h3 h4;generalize hE:201/100/2^53*W+Sb/2^48=E have key:∀ v t:ℝ,|t| ≤ Sb->|v-n*t| ≤ 2*n*(1/2^53*W)->|v*I-t| ≤ E := λv t ht hv=>by have:=qGy n I v t Sb (2*(1/2^53*W)) 0 h3 h4 ht ד ד;linarith have hr1:=key Rs r hr h1;have hA1:=key Rt Sb (by rw[Ж hS0]) h2;generalize hA:Rt*I=A at hA1;have hE0:0<=E:=by rw[<-hE];positivity have hEb:E<=2^80:=by rw[<-hE];norm_num at nSb hS ⊢;linarith have hc0:0<=c:=by rw[hc];positivity have hc1:c<=1/2^28:=by rw[hc];norm_num at hn2 ⊢;linarith have b0:=qAb A Sb;rw[Ж hS0] at b0;have b1 : |N*I-c*A| ≤ 1/2^53*(|c*A| + 2*z):=by rw[<-hA,show N*I-c*(Rt*I)=(N-c*Rt)*I by ring,е,г h3,show |c*(Rt*I)| = |c*Rt| * I by rw[е,е,е,г h3];ring];have:=Б h6 h3.le;have:=Г hI2 hz0;linarith have b2 : |c*A| ≤ 1/2^28*(Sb+E):=by rw[е,Ж hc0];exact С hc1 ד (Й _) ב have b3:c*Sb-1/2^28*E<=c*A:=by have h9 : |c*(A-Sb)| ≤ 1/2^28*E:=by rw[е,Ж hc0];exact С hc1 hA1 (Й _) ב linarith [(abs_le.mp h9).1] have b4:c*Sb=(n*Sb+8*Sb)/2^49:=by rw[hc];ring have c1:=qAb (N*I) (c*A);have c2:=qAb Y (N*I);have c3:=Ь Y (1/2^200);rw[г (by norm_num:(0:ℝ)<1/2^200)] at c3;have d1:=qAb Rm (Rs*I);have d2:=qAb (Rs*I) r;have d3:=abs_sub_le r (Rs*I) Rm;rw[и r (Rs*I),и (Rs*I) Rm] at d3;obtain ⟨e1,e2⟩:=abs_le.mp b1;obtain ⟨e3,e4⟩:=abs_le.mp h7;obtain ⟨e5,e6⟩:=abs_le.mp h8;rw[<-hE] at hr1 b2 b3 hEb;subst hW;have ha:=Й r;have g1:=le_abs_self (c*A);exact ⟨by linarith,by linarith,by linarith,by linarith⟩ lemma qH0 {s c Em dd Rn2 Rmu ee hi lo:ℝ} (hs0:0<=s) (hs1:s<=2^101) (hc0:9/2^49<=c) (hc1:c<=1/2^20) (hE:1/2^201<=Em) (hE1:Em<=2^120) (h1 : |dd-s| ≤ 1/2^53*(s+z)) (h2 : |Rn2-(c+c)| ≤ 1/2^53*(|c+c| + z)) (h3 : |Rmu-Rn2*dd| ≤ 1/2^53*(|Rn2*dd| + z)) (h4 : |ee-(Rmu+Em)| ≤ 1/2^53*(|Rmu+Em| + z)) (h5 : |hi-(dd+ee)| ≤ 1/2^53*(|dd+ee| + z)) (h6 : |lo-(dd-ee)| ≤ 1/2^53*(|dd-ee| + z)) : s*(1+3/2*c)+9/10*Em<=hi∧lo<=s*(1-3/2*c)-9/10*Em:=by have hcp:0< c:=ч ב hc0;have hcs:0<=c*s:=Н hcp.le hs0;have hcs2:=Б hc0 hs0;have hcc : |c+c| = c+c:=г ד;rw[hcc] at h2;have d1:=qAb dd s;rw[Ж hs0] at d1;have d2 : |dd| ≤ 3*s+z :=ד;have d3 : |dd| ≤ 2^103:=by norm_num at hs1 hz d2 ⊢;linarith have m1:=Ь ((Rn2-(c+c))*dd) ((c+c)*(dd-s));rw[е,е,hcc,show (Rn2-(c+c))*dd+(c+c)*(dd-s)=Rn2*dd-(c+c)*s by ring] at m1;have m2:=С h2 d2 (Й _) (by positivity);have m3:=Г h1 (by linarith:0<=c+c);have zs:=С hz hs1 hs0 ב;have zc:z*c<=z*1:=Г ד hz0;have zz:z*z<=z*1:=Г ד hz0;have nzs:=Н hz0 hs0;have nzc:=Н hz0 hcp.le;have nzz:=Н hz0 hz0;have m4 : |Rn2*dd-(c+c)*s| ≤ 1/2^49*(c*s)+1/2^230:=by norm_num at zs ⊢;linarith generalize Rn2*dd=M at h3 m4;rw[show (c+c)*s=2*(c*s) by ring] at m4;have n1:=qAb M (2*(c*s));rw[Ж (by linarith:0<=2*(c*s))] at n1;have n2:=qAb Rmu M;have n3:=Ь Rmu Em;rw[г (by linarith [(by norm_num:(0:ℝ)<1/2^201)]:0< Em)] at n3;have n4:=qAb ee (Rmu+Em);have n5:=Ь dd ee;have n6:=Ե dd ee;obtain ⟨a1,a2⟩:=abs_le.mp h1;obtain ⟨b1,b2⟩:=abs_le.mp m4;obtain ⟨c1,c2⟩:=abs_le.mp h3;obtain ⟨d4,d5⟩:=abs_le.mp h4;obtain ⟨e1,e2⟩:=abs_le.mp h5;obtain ⟨f1,f2⟩:=abs_le.mp h6;have hcs3:=Б hc1 hs0;constructor<;>linarith lemma qKf {x Rd RQ:ℝ} (hx : |x| ≤ 2^111) (h1 : |Rd-x| ≤ 1/2^53*(|x| + z)) (h2 : |RQ-Rd*Rd| ≤ 1/2^53*(|Rd*Rd| + z)) : |RQ-x^2| ≤ 4/2^53*x^2+1/2^250:=by have nA:=Й x have zA:=С hz hx nA ב have zz:z*z<=z*1:=Г ד hz0 have nzA:=Н hz0 nA have nzz:=Н hz0 hz0 have hsq : |x| ^ 2=x^2:=sq_abs x generalize he:Rd-x=e at h1 have q1:=ض (Й e) h1 2 have q3 : |2*x*e| ≤ 2 * |x| * (1/2^53*(|x| + z)):=by rw[е,е,abs_two] exact Г h1 (by positivity) have q6 : |Rd*Rd-x^2| ≤ |2*x*e| + |e| ^ 2:=by rw[show Rd*Rd-x^2=2*x*e+e^2 by rw[<-he];ring,<-abs_pow] exact Ь _ _ have q8:=qAb (Rd*Rd) (x^2) rw[Ж (ق x)] at q8 have q9:=qAb (RQ-x^2) (RQ-Rd*Rd) rw[show RQ-x^2-(RQ-Rd*Rd)=Rd*Rd-x^2 by ring] at q9 have nX:=ق x have s1 : |Rd*Rd-x^2| ≤ 201/100/2^53*x^2+1/2^254 :=ד linarith lemma qJz {n Rv I Rp Rt ep c:ℝ} (Ws tau:ℝ) (hn1:1<=n) (hn2:n<=2^20) (hc:c=(n+8)/2^49) (hWs:0<=Ws) (hep:1/2^126<=ep) (ht0:0<=tau) (ht:tau<=1/2^229) (h1 : |Rv-n*Ws| ≤ (2*n+6)/2^53*(n*Ws)+tau) (h3:0< I) (h4 : |I*n-1| ≤ 1/2^48) (h5 : |Rp-Rv*I| ≤ 1/2^53*(|Rv*I| + z)) (h6 : |Rt-(Rp+ep)| ≤ 1/2^53*(|Rp+ep| + z)) : |Rt-(Ws+ep)| ≤ c/2*(Ws+ep):=by have hep0:0< ep:=ч ב hep have hI2:=L_I hn1 h3 h4 have hX:0<=(2*n+6)/2^53*Ws:=Н ד hWs have b1:=qGy n I Rv Ws Ws _ tau h3 h4 (by rw[Ж hWs]) ד hX have a3:=Г hI2 ht0 have nnW:=Н (by linarith:0<=n) hWs have nnE:=Н (by linarith:0<=n) hep0.le have nnW2:=Б hn2 hWs have b2:=qAb (Rv*I) Ws rw[Ж hWs] at b2 have b3:=Ь Rp ep rw[г hep0] at b3 have b4:=qAb Rp (Rv*I) have b5:=Ь (Rt-(Rp+ep)) ((Rp-Rv*I)+(Rv*I-Ws)) have b6:=Ь (Rp-Rv*I) (Rv*I-Ws) rw[show Rt-(Rp+ep)+(Rp-Rv*I+(Rv*I-Ws))=Rt-(Ws+ep) by ring] at b5 rw[hc] linarith lemma qH9 {n W Rv Sq Si:ℝ} (hn1:1<=n) (hn2:n<=2^20) (hW:0<=W) (h1 : |Rv-Sq| ≤ 2*n*(1/2^53*(Si+z))) (h2 : |Sq-W| ≤ 4/2^53*W+n*(1/2^250)) (h3:Si<=(1+4/2^53)*W+n*(1/2^250)) : |Rv-W| ≤ (2*n+6)/2^53*W+1/2^229:=by have hn0:0<=n :=ד have a1:=Г h3 hn0 have a3:=Б hn2 hW have a4:=С hn2 hn2 hn0 ב have a5:=С hn2 hz hz0 ב have a6:=abs_sub_le Rv Sq W linarith end lemma qIn {T s c Em lo hi:ℝ} (hT:0< T) (hs:0< s) (hc0:0< c) (hc1:c<=1/2^20) (hE:0<=Em) (h1:T<=s^2*(1+c)) (h2:s^2*(1-c)-Em^2/4<=T) (h3:s*(1+3/2*c)+9/10*Em<=hi) (h4:lo<=s*(1-3/2*c)-9/10*Em) : lo< п T∧п T< hi:=by have hsT:=Real.sqrt_pos.mpr hT;have hq:0< s^2*c:=by positivity constructor . by_cases hl:lo<0;. linarith . rw[not_lt] at hl;rw[Real.lt_sqrt hl];generalize hk:s*(1-3/2*c)=k at h4;have hk0:0<=k-9/10*Em :=ד;have q1:=ض hl h4 2;have q3:=Н hE hk0 have q4:k^2<=s^2*(1-3/2*c):=by have h5:(1-3/2*c)^2<=1-3/2*c:=by nlinarith calc k^2=s^2*(1-3/2*c)^2:=by rw[<-hk];ring _<=_:=Г h5 (ق _) have q5:=ق Em;nlinarith . have hh:0< hi:=by have h5:0< s*(1+3/2*c):=by positivity linarith rw[Real.sqrt_lt' hh];have q1:=ض (by positivity) h3 2;have r1:0<=s^2*c^2:=by positivity have r2:0<=s*Em*(1+3/2*c):=by positivity have r3:=ق Em;nlinarith lemma qIv (l:List ℕ) (a:ℕ->ℝ) (c:ℝ) : (l.map λj=>(a j-c)^2).sum =(l.map λj=>a j^2).sum-2*c*(l.map a).sum+l.length*c^2:=by induction l with | nil=>simp | cons j l ih => simp only[List.map_cons,List.sum_cons,List.length_cons,ih] push_cast ring lemma qHf (l:List ℕ) (a:ℕ->ℝ) (m:ℝ) (hl:(l.length:ℝ)≠0) : (l.map λj=>(a j-m)^2).sum = (l.map λj=>(a j-(l.map a).sum/l.length)^2).sum + l.length*((l.map a).sum/l.length-m)^2:=by rw[qIv,qIv] field_simp ring lemma qIh (l:List ℕ) (f w:ℕ->ℝ) (u t:ℝ) (hw:∀ j∈l,0<=w j) (h:∀ j∈l,|f j-w j| ≤ u*w j+t) (hu:0<=u) (ht:0<=t) : |(l.map f).sum-(l.map w).sum| ≤ u*(l.map w).sum+l.length*t ∧ (l.map λj=>|f j|).sum<=(1+u)*(l.map w).sum+l.length*t∧0<=(l.map w).sum:=by induction l with | nil=>simp | cons j l ih => obtain ⟨i1,i2,i3⟩:=ih (λk hk=>hw k (Բ _ hk)) (λk hk=>h k (Բ _ hk)) have h0:=hw j Է have h1:=h j Է simp only[List.map_cons,List.sum_cons,List.length_cons] push_cast have a1:=Ь (f j-w j) ((l.map f).sum-(l.map w).sum) rw[show f j-w j+((l.map f).sum-(l.map w).sum)=f j+(l.map f).sum-(w j+(l.map w).sum) by ring] at a1 have a2:=qAb (f j) (w j) rw[Ж h0] at a2 exact ⟨by linarith,by linarith,by linarith⟩ lemma qIg (a b:Array UInt32) (hs:a.size=b.size) (hf:∀ j,j< a.size->expOf a[j]!≠255∧expOf b[j]!≠255) (hz:(dsum a b 0 a.size:ℚ)*2^(-298:Int)=0) : dotSpec a b=toWord (Model.zero Fmt (decide (a.size≠0) && dallNZ a b 0 a.size)):=by rw[<-qEt a b hs.symm hf,show dsum a b 0 a.size=0 by simpa using hz,finishWord_zero] lemma qIj (n:Nat) (a b:Nat->UInt32) (hf:∀ t,t< n->expOf (a t)≠255∧expOf (b t)≠255) (hS:∑ j∈р n,Д (a j)*Д (b j)=0) : dotOf n a b=toWord (Model.zero Fmt (decide (n≠0) && dallNZ (Array.ofFn (n:=n) λi=>a i.val) (Array.ofFn (n:=n) λi=>b i.val) 0 n)):=by have gA:=qAa n a have gB:=qAa n b have s:(Array.ofFn (n:=n) λi=>a i.val).size=n:=by simp have e:=qIg (Array.ofFn (n:=n) λi=>a i.val) (Array.ofFn (n:=n) λi=>b i.val) ג (λj hj=>by rw[s] at hj;rw[gA j hj,gB j hj];exact hf j hj) (by rw[s,qFl,<-hS] refine ر rfl λj hj=>?_ have h:=ؽ.mp hj rw[Л,gA j h,gB j h]) rw[s] at e exact e lemma qJ8 (a b:Array UInt32) (r i:Nat) : dallNZ a b i (r+1)=(dallNZ a b i r && (decide (mantOf a[i+r]! * mantOf b[i+r]! = 0) && Bool.xor (signOf a[i+r]!) (signOf b[i+r]!))):=by induction r generalizing i with | zero=>simp[dallNZ] | succ r ih => rw[dallNZ,ih (i+1),dallNZ] simp only[Bool.and_assoc,show i+1+r=i+(r+1) by omega] lemma qJt (a b u v:Array UInt32) (r i:Nat) (h:∀ j,i<=j->j< i+r->a[j]! = u[j]!∧b[j]! = v[j]!) : dallNZ a b i r=dallNZ u v i r:=by induction r generalizing i with | zero=>rfl | succ r ih => rw[dallNZ,dallNZ,ih (i+1) (λj h1 h2=>h j א א),(h i א א).1,(h i א א).2] lemma qJe (k:Nat) (a b:Nat->UInt32) (hk:0< k) (hf:∀ t,t< k->expOf (a t)≠255∧expOf (b t)≠255) : dotOf (k+1) (λt=>if t< k then a t else oneW) (λt=>if t< k then b t else х)=dotOf k a b:=by have hf':∀ j,j< k+1->expOf (if j< k then a j else oneW)≠255 ∧ expOf (if j< k then b j else х)≠255:=λj _=>by by_cases h:j< k . simpa [h] using hf j h . simp only[h,ite_false];exact ⟨by decide,by decide⟩ have hsum:(∑ j∈р (k+1),Д (if j< k then a j else oneW) * Д (if j< k then b j else х)) =∑ j∈р k,Д (a j)*Д (b j):=by rw[Finset.sum_range_succ] simp only[lt_self_iff_false,ite_false,qF4 0x80000000 (by decide) (by decide),mul_zero,add_zero] exact ر rfl λj hj=>by simp[Finset.mem_range.mp hj] by_cases hS:(∑ j∈р k,Д (a j)*Д (b j))=0 . have g1:=qAa (k+1) λt=>if t< k then a t else oneW;have g2:=qAa (k+1) λt=>if t< k then b t else х rw[qIj _ _ _ hf' (hsum.trans hS),qIj k a b hf hS,qJ8,Л,g1 k א,g2 k א, qJt _ _ (Array.ofFn (n:=k) λi=>a i.val) (Array.ofFn (n:=k) λi=>b i.val) k 0 (λj _ h2=>by rw[g1 j א,g2 j א,qAa k a j א,qAa k b j א] simp[show j< k by omega])] simp[show mantOf (0x80000000:UInt32)=0 by decide,show signOf oneW=false by decide,show signOf (0x80000000:UInt32)=true by decide,hk.ne'] . obtain ⟨d,hd,hw⟩:=qCe k a b hf hS;obtain ⟨e,he,hw'⟩:=qCe (k+1) _ _ hf' (by rw[hsum];exact hS);have t:=@FloatLib.Numerics.Dyadic.toRat_eq_zero_iff;have h3:e.toRat=d.toRat:=by rw[he,hd,hsum] rw[hw,hw',roundDyadic_congr Fmt (by decide) e d (λh=>hS (hd.symm.trans (h3.symm.trans ((t _).2 h)))) (λh=>hS (hd.symm.trans ((t _).2 h))) h3] lemma qI8 (A:Array UInt32) (h:A.all finiteWord=true) (t:Nat):expOf A[t]!≠255:=by by_cases ht:t< A.size;. simpa [finiteWord] using qBn A _ h t ht . rw[غ A t ht];decide lemma qKd {σ:Type} {N n c:Nat} {sp:Nat->Word} {f:σ->USize->USize->Word} {s:σ} {b:USize} (hN:N<2^32) (hb:b.toNat=c) (hn:c+n<=N) (hf:∀ p u:USize,p.toNat=c+u.toNat->u.toNat< n->f s p u=sp p.toNat) (k:Nat) : ∀ (j:USize) (o:Array Word),j.toNat+k=n->C_Inv N sp (λq=>q< c+j.toNat) o → C_Inv N sp (λq=>q< c+n) (fC f s b j o k):=by induction k with | zero => intro j o e h rw[fC,<-e];exact h | succ k ih => intro j o e h have a0:(b+j).toNat=c+j.toNat:=by rw[<-hb];exact qAo _ _ א have a1:(j+1).toNat=j.toNat+1:=by rw[qAo _ _ (by rw[ش];omega),ش] rw[fC] refine ih _ _ א ?_ rw[a1] exact qAn hN h a0 א (by rw[hf _ _ a0 א,a0]) lemma qDj {σ:Type} {R n:Nat} (F:Nat->Nat->Nat->Word) {h:Nat->σ} {f:σ->USize->USize->Word} (hN:R*n<2^32) (hn:0< n) (hf:∀ (r:Nat) (p u:USize),r< R->p.toNat=r*n+u.toNat->u.toNat< n → f (h r) p u=F r u.toNat p.toNat) : fR h f n 0 (И (R*n) 0) R =Array.ofFn (n:=R*n) λq=>F (q.val/n) (q.val%n) q.val:=by let sp q:=F (q/n) (q%n) q have K:∀ (d r:Nat) (o:Array Word),r+d=R->C_Inv (R*n) sp (λq=>q< r*n) o → C_Inv (R*n) sp (λq=>q< R*n) (fR h f n r o d):=λd=>by induction d with | zero => intro r o e H subst e exact H | succ d ih => intro r o e H have h1:r*n+n<=R*n:=qA1 (א:r< R) (le_refl _) rw[fR] refine ih _ _ א ?_ rw[ػ] exact qKd hN (qAc _ א) h1 (λp u h2 h3=>by rw[hf r p u א h2 h3] show F _ _ _=F _ _ _ rw[h2,ж r n,ـ hn,ف,ш h3,З h3,у]) n 0 o (Л _) H exact qDp (K R 0 _ א ⟨by simp,λq _ (h:q<0*n)=>by omega⟩) λq hq=>hq lemma qFc:gemmF=gemmSpec:=by funext batch m k n A B;unfold gemmF;split . rename_i hc;simp only[Bool.and_eq_true,beq_iff_eq,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨⟨⟨h1,h2⟩,h3⟩,h4⟩,h5⟩,h6⟩,h7⟩,h8⟩:=hc;subst h1;have hn:n<2^32:=ц (Nat.le_mul_of_pos_left n h5) h8;dsimp only rw[qD5,qDj (λr c _=>B[c*n+r]!) (by rwa [ж]) h5 λr p u hr hp hu=>by have a1:=qBe hu hr;have e1:=qAc n hn;have e2:=qAc r א;have e3:(u*n.toUSize).toNat=u.toNat*n:=by rw[qDh _ _ (by rw[e1];omega),e1] rw[qAj _ _ א,qAo _ _ (by rw[e3,e2];omega),e3,e2]] unfold linearSpec linearWith gemmSpec gemmWith;apply ه ג;intro q h8 h9;simp only[Array.getElem_ofFn,Array.size_ofFn] at h8 h9 ⊢;have hq:q< m*n:=by simpa using h9 have e1:q/(m*n)=0:=ш hq;have e2:q/n%m=q/n:=З (м (by rwa [ж]));have hj:q%n< n:=э _ h4;simp only[act,Bool.false_eq_true,ite_false];rw[<-qJe k (λt=>A[(q/(m*n)*m+q/n%m)*k+t]!) (λt=>B[(q/(m*n)*k+t)*n+q%n]!) h5 (λt _=>⟨qI8 A h6 _,qI8 B h7 _⟩)];congr 1 . funext t;by_cases ht:t< k;. simp[ht,e1,e2] . simp[ht] . funext t;by_cases ht:t< k . simp only[ht,if_true];have hi:q%n*k+t< n*k:=by have:=Т k (א:q%n+1<=n);rw[م,Դ] at this;omega rw[qAa (n*k) (λu=>B[u%k*n+u/k]!) _ hi];have i1:(q%n*k+t)%k=t:=by rw[ж,ف,З ht] have i2:(q%n*k+t)/k=q%n:=by rw[ж,ـ h5,ш ht,у] simp[i1,i2,e1] . simp only[ht,if_false];simp[hj] . rfl lemma qIr (x:Array UInt32) (hx:x.size<2^32) (base nu:USize) (hb:base.toNat+nu.toNat<2^32) (d:ℕ) (i:USize) (s t:Float) (h:i.toNat+d=nu.toNat) : lnSum x base nu i s t = ⟨((List.range' i.toNat d).map λj=>dv x[base.toNat+j]!).foldl (.+·) s,((List.range' i.toNat d).map λj=>(dv x[base.toNat+j]!).abs).foldl (.+·) t⟩:=by induction d generalizing i s t with | zero => rw[lnSum,dif_neg (by rw[А];omega)];simp | succ d ih => have hn:i< nu:=by rw[А];omega have e1:=usz i nu hn rw[lnSum,dif_pos hn,ih (i+1) _ _ (by rw[e1];omega),e1,qAj x _ hx,qAo base i א] simp[List.range'_succ] lemma qId (x:Array UInt32) (hx:x.size<2^32) (base nu:USize) (m:Float) (hb:base.toNat+nu.toNat<2^32) (d:ℕ) (i:USize) (v:Float) (h:i.toNat+d=nu.toNat) : lnVar x base nu i m v = ((List.range' i.toNat d).map λj =>(dv x[base.toNat+j]! - m)*(dv x[base.toNat+j]! - m)).foldl (.+·) v:=by induction d generalizing i v with | zero => rw[lnVar,dif_neg (by rw[А];omega)];simp | succ d ih => have hn:i< nu:=by rw[А];omega have e1:=usz i nu hn rw[lnVar,dif_pos hn,ih (i+1) _ (by rw[e1];omega),e1,qAj x _ hx,qAo base i א] simp[List.range'_succ] lemma qFr (l:List ℕ) (f:ℕ->ℚ) (u:ℕ->ℝ) (c:ℚ) (h:∀ j∈l,(f j:ℝ)=u j) : ((l.foldl (λt j=>t+f j) c:ℚ):ℝ)=c+(l.map u).sum:=by induction l generalizing c with | nil=>simp | cons j l ih => simp only[List.foldl_cons,List.map_cons,List.sum_cons];rw[ih _ λk hk=>h k (Բ _ hk),<-h j Է];push_cast;ring lemma qJq (w:UInt32) (hg:good w=true) (h1:(0x1F800000:UInt32)<=w) (h2:w<=(0x70FFFFFF:UInt32)):1/2^64<=wv w:=by have a1:528482304<=w.toNat:=М.mp h1;have a2:w.toNat<=1895825407:=М.mp h2;rw[qAz w hg,if_neg א,one_mul];have h3:=qA7 528482304 (w.toNat%2^31) א א;have:A_mag 528482304=1/2^64:=by unfold A_mag;norm_num rwa [this] at h3 lemma qJv (w:UInt32) (hg:good w=true) (h0:0< wv w):1/2^126<=wv w:=by have hn:=qA3 w hg;rw[qAz w hg] at h0 ⊢;rcases hn with hn | ⟨hn,-⟩;. rw[hn] at h0;simp[A_mag] at h0 . have hs:¬ 2^31<=w.toNat:=λhc=>by rw[if_pos hc] at h0;linarith [qAv (w.toNat%2^31)] rw[if_neg hs,one_mul];have h3:=qA7 (2^23) (w.toNat%2^31) hn א;have:A_mag (2^23)=1/2^126:=by unfold A_mag;norm_num rwa [this] at h3 lemma qKi (n:ℕ) (hn:n<2^20) : ((n.toUInt64+8).toFloat*П ط).isFinite=true ∧ R ((n.toUInt64+8).toFloat*П ط)=((n:ℝ)+8)/2^49:=by have ht:(n.toUInt64+8).toNat=n+8:=by have h8:(8:UInt64).toNat=8:=rfl simp[UInt64.toNat_add,h8] omega have h1:=qB0 (n.toUInt64+8) (by rw[ht];omega) obtain ⟨h2,h3⟩:=L_k ط 974 49 ב rfl (by rfl) have h4:(n:ℝ)+8<=2^21:=by exact_mod_cast (א:n+8<=2^21) obtain ⟨hf,he⟩:=qC7 _ _ h1.1 h2 (by rw[h1.2,h3,ht] refine ؾ (С (qAw ?_) (qAw (by rw[Ж (by positivity)];norm_num)) (Й _) qKo.le) qCb rw[Nat.cast_add,Ж (by positivity)] push_cast linarith) refine ⟨hf,?_⟩ rw[he,h1.2,h3,ht] have:=qAu ((n:ℤ)+8) (-49) א ב push_cast at this ⊢ rw[show (2:ℝ)^(-49:ℤ)=1/2^49 by norm_num] at this rw[this] ring lemma qEs {r x B:ℝ} (e : |r-x| ≤ 1/2^53*(|x| + A_z)) (h : |x| ≤ B) : |r| ≤ 9/8*B+A_z:=by linarith [qAb r x,Й x,qAs] lemma qAy (x y:Float) (B:ℝ) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ B) (h2 : |R y| ≤ B) (hB:B<=2^256*2^44:=by norm_num) : (x+y).isFinite=true∧|R (x+y)-(R x+R y)| ≤ 1/2^53*(|R x+R y| + A_z) ∧ |R (x+y)| ≤ 9/8*(B+B)+A_z:=by obtain ⟨f,e⟩:=qBa x y hx hy (h1.trans (qAw hB)) (h2.trans (qAw hB)) exact ⟨f,e,qEs e ((Ь _ _).trans (add_le_add h1 h2))⟩ lemma qCh (x y:Float) (B:ℝ) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ B) (h2 : |R y| ≤ B) (hB:B<=2^256*2^44:=by norm_num) : (x-y).isFinite=true∧|R (x-y)-(R x-R y)| ≤ 1/2^53*(|R x-R y| + A_z) ∧ |R (x-y)| ≤ 9/8*(B+B)+A_z:=by obtain ⟨f,e⟩:=qDc x y hx hy (h1.trans (qAw hB)) (h2.trans (qAw hB)) exact ⟨f,e,qEs e ((Ե _ _).trans (add_le_add h1 h2))⟩ lemma qAr (x y:Float) (B C:ℝ) (hx:x.isFinite=true) (hy:y.isFinite=true) (h1 : |R x| ≤ B) (h2 : |R y| ≤ C) (hB:B<=2^256*2^44:=by norm_num) (hC:C<=2^256*2^44:=by norm_num) : (x*y).isFinite=true∧|R (x*y)-R x*R y| ≤ 1/2^53*(|R x*R y| + A_z) ∧ |R (x*y)| ≤ 9/8*(B*C)+A_z:=by obtain ⟨f,e⟩:=qBw x y hx hy (h1.trans (qAw hB)) (h2.trans (qAw hC)) exact ⟨f,e,qEs e (е (R x) (R y) ▸ С h1 h2 (Й _) ((Й _).trans h1))⟩ lemma qGr (a b:Float) (hb:b.isFinite=true) (hb2 : |R b| ≤ 2^99) (h:invOK a b=true) : a.isFinite=true∧0< R a∧R a<=2^200∧|R a*R b-1| ≤ 1/2^48:=by simp only[invOK,Bool.and_eq_true,decide_eq_true_eq] at h;obtain ⟨r1,r2⟩:1<=_∧_<1+1222:=qEf _ 1 1222 h.1 (by decide);obtain ⟨r3,r4⟩:972<=_∧_<972+2:=qEf _ 972 2 h.2 (by decide);obtain ⟨f1,l1,u1⟩:=qC8 rfl r1 א;have hp:0< R a:=ч (ы ב _) l1;have ha2:R a<=2^200:=u1.le.trans ((Ш ב א).trans_eq (by norm_num:(2:ℝ)^(200:ℤ)=2^200));obtain ⟨k1,k2⟩:=L_k 0x3FF0000000000000 1023 0 ב rfl (by rfl);obtain ⟨k3,k4⟩:=L_k 0x3CD0000000000000 973 50 ב rfl (by rfl);rw[pow_zero,div_one] at k2;have hz1:=qBx;obtain ⟨f2,g1,q1⟩:=qAr a b (2^200) (2^99) f1 hb (by rwa [г hp]) hb2;obtain ⟨f3,g2⟩:=qDc _ _ f2 k1 (qAw ד) (qAw (by rw[k2];norm_num));rw[k2] at g2;have q3:=Ե (R (a*b)) 1;rw[abs_one] at q3;have q2:=qEs g2 q3;obtain ⟨f4,g3⟩:=qBa _ _ f3 k3 (qAw ד) (qAw (by rw[k4];norm_num));rw[k4] at g3;obtain ⟨-,l2,u2⟩:=qC8 rfl (ؾ ב r3) א;refine ⟨f1,hp,ha2,qEj qAs.le qAx g1 g2 g3 ?_ ?_⟩;. exact (by norm_num:(1/2^51:ℝ)=2^(-51:ℤ)).trans_le ((Ш ב א).trans l2) . exact u2.le.trans ((Ш ב א).trans_eq (by norm_num:(2:ℝ)^(-49:ℤ)=1/2^49)) lemma qD4 (n:ℕ) (f:ℕ->ℝ) (B:ℝ) (h:∀ j,j< n->f j<=B) : ((К n).map f).sum<=n*B:=by simpa using List.sum_le_length_nsmul ((List.range n).map f) B (by simpa using λj hj=>h j hj) lemma qF0 (n:ℕ) (hn:n<2^20) (f:ℕ->Float) (a b:ℕ->ℝ) (hf:∀ j,j< n->(f j).isFinite=true∧R (f j)=a j∧|a j| = b j) (hb:((К n).map b).sum<=A_B/4) : (A_fsum ((К n).map f)).isFinite=true ∧ |R (A_fsum ((К n).map f))-((К n).map a).sum| ≤ 2*n*(1/2^53*(((К n).map b).sum+A_z)):=by have s:∀ (u:Float->ℝ) (v:ℕ->ℝ),(∀ j,j< n->u (f j)=v j) → (((К n).map f).map u).sum=((К n).map v).sum:=λu v h=>by simp only[List.map_map] exact congrArg _ (Զ λj hj=>h j (й.1 hj)) have e1:=s (λq=>|R q|) b λj h=>by simp only[(hf j h).2.1,(hf j h).2.2] have e2:=s R a λj h=>(hf j h).2.1 have:=qBf ((К n).map f) (λq hq=>by obtain ⟨j,hj,rfl⟩:=List.mem_map.1 hq exact (hf j (й.1 hj)).1) (by simp;omega) (by rw[A_sa,e1];exact hb) rwa [A_sa,A_sr,e1,e2,List.length_map,List.length_range] at this lemma qIm (n:ℕ) (hn:n<2^20) (hn0:0< n) (g:ℕ->Float) (m v:Float) (W:ℝ) (hv:v=A_fsum ((К n).map λj=>(g j-m)*(g j-m))) (hW:W=((К n).map λj=>(R (g j)-R m)^2).sum) (hg:∀ j,j< n->(g j).isFinite=true∧|R (g j)| ≤ 2^99) (hm:m.isFinite=true) (hm2 : |R m| ≤ 2^110) : v.isFinite=true∧|R v-W| ≤ (2*n+6)/2^53*W+1/2^229∧W<=n*2^222∧0<=W:=by have hz0:=qAs.le;have hzl:=qAx;have hzb:=qBx have hq:∀ j,j< n->((g j-m)*(g j-m)).isFinite=true ∧ |R ((g j-m)*(g j-m))-(R (g j)-R m)^2| ≤ 4/2^53*(R (g j)-R m)^2+1/2^250∧(R (g j)-R m)^2<=2^222:=by intro j hj obtain ⟨f,b⟩:=hg j hj have B1 : |R (g j)| ≤ 2^110:=b.trans ב obtain ⟨f1,e1,b1⟩:=qCh (g j) m (2^110) f hm B1 hm2 have b2 : |R (g j-m)| ≤ 2^112 :=ד obtain ⟨f2,e2,-⟩:=qAr _ _ (2^112) (2^112) f1 f1 b2 b2 have hx : |R (g j)-R m| ≤ 2^111:=(Ե _ _).trans ד refine ⟨f2,qKf hz0 hzl hx e1 e2,?_⟩ have:=ض (Й _) hx 2 rw[sq_abs] at this exact this.trans ב obtain ⟨a1,a2,a3⟩:=qIh (К n) (λj=>R ((g j-m)*(g j-m))) (λj=>(R (g j)-R m)^2) (4/2^53) (1/2^250) (λj _=>ق _) (λj hj=>(hq j (й.1 hj)).2.1) ב ב;simp only[List.length_range] at a1 a2;rw[<-hW] at a1 a2 a3;have hW':W<=n*2^222:=hW ▸ qD4 n _ _ λj h=>(hq j h).2.2;have hnR:(n:ℝ)<=2^20:=by exact_mod_cast hn.le have hnR1:(1:ℝ)<=n:=by exact_mod_cast hn0 have h1:(n:ℝ)*2^222<=2^20*2^222:=by gcongr have h2:(n:ℝ)*(1/2^250)<=2^20*(1/2^250):=by gcongr obtain ⟨f2,e2⟩:=qF0 n hn _ _ (λj=>|R ((g j-m)*(g j-m))|) (λj h=>⟨(hq j h).1,rfl,rfl⟩) (by refine ؾ a2 ?_;have:=qAw (k:=2^256*2^44) le_rfl linarith) rw[<-hv] at e2 f2;exact ⟨f2,qH9 hz0 hzl hnR1 hnR a3 e2 a1 a2,hW',a3⟩ lemma qG9 (l:List ℕ) (f:ℕ->ℝ) : |(l.map f).sum| ≤ (l.map λj=>|f j|).sum:=by induction l with | nil=>simp | cons j l ih => simp only[List.map_cons,List.sum_cons];exact (Ь _ _).trans (add_le_add le_rfl ih) def L_oF (s:LnS) (M:ℝ):Prop := s.fast=true->s.m.isFinite=true∧s.ir.isFinite=true∧s.em.isFinite=true ∧ R s.em<=2^120∧1/2^201<=R s.em∧2 * |M-R s.m| ≤ R s.em ∧ |R s.m| ≤ 2^110∧good s.den=true∧1/2^64<=wv s.den ∧ |R s.ir*wv s.den-1| ≤ 1/2^48 def L_ok (b:Bool) (n:ℕ) (eps:Word) (x:Array Word) (r:ℕ) (s:LnS):Prop := s.den=sqrtRational (rowVariance n x r (lnQ b n x r)+Д eps) ∧ L_oF s (lnQ b n x r:ℚ) lemma qI9 (n r:ℕ) (x:Array Word) (g:ℕ->Float) (h:∀ j,j< n → R (g j)=(Д x[r*n+j]! : ℝ)) (q:ℚ) : ((rowMean n x r:ℚ):ℝ)=((К n).map λj=>R (g j)).sum/n ∧ ((rowVariance n x r q:ℚ):ℝ) =((К n).map λj=>(R (g j)-(q:ℝ))^2).sum/n:=by constructor . rw[rowMean,Rat.cast_div,qFr _ _ (λj=>R (g j)) 0 λj hj=>(h j (й.1 hj)).symm];simp . rw[rowVariance,Rat.cast_div,qFr _ _ (λj=>(R (g j)-(q:ℝ))^2) 0 λj hj=>by rw[h j (й.1 hj)];push_cast;rfl] simp lemma qH7 (n:ℕ) (hnR1:(1:ℝ)<=n) (hnR2:(n:ℝ)<=2^20) (Fs Ft ind nf m em:Float) (S Sa c:ℝ) (hc:c=((n:ℝ)+8)/2^49) (o1:Fs.isFinite=true) (o2:Ft.isFinite=true) (o3 : |S| ≤ Sa) (o4:Sa<=n*2^99) (o5 : |R Fs-S| ≤ 2*n*(1/2^53*(Sa+A_z))) (o6 : |R Ft-Sa| ≤ 2*n*(1/2^53*(Sa+A_z))) (fnf:nf.isFinite=true) (Rnf:R nf=c) (fi:ind.isFinite=true) (pi:0< R ind) (ei : |R ind*n-1| ≤ 1/2^48) (hm:Fs*ind=m) (hem:nf*Ft*ind+П ك=em) : m.isFinite=true∧em.isFinite=true∧R em<=2^120∧1/2^201<=R em ∧ 2 * |S/n-R m| ≤ R em∧|R m| ≤ 2^110:=by have hz0:=qAs.le have hzb:=qBx have hc0:0< c:=by rw[hc];positivity have hc1:c<=1/2^20:=by rw[hc];linarith have bI : |R ind| ≤ 2:=by rw[г pi];exact L_I hnR1 pi ei have hSa0:0<=Sa:=(Й _).trans o3 have hSb:=Б hnR2 (by norm_num:(0:ℝ)<=2^99) have o7:=С hnR2 (by linarith:Sa+A_z<=2^119+1) ד ב have bs : |R Fs| ≤ 2^120:=by linarith [qAb (R Fs) S] have bt : |R Ft| ≤ 2^120:=by have:=qAb (R Ft) Sa rw[Ж hSa0] at this linarith have bnf : |R nf| ≤ 1:=by rw[Rnf,г hc0];linarith obtain ⟨fm,em1,-⟩:=qAr Fs ind (2^120) 2 o1 fi bs bI rw[hm] at fm em1 obtain ⟨fN,emN,bN⟩:=qAr nf Ft 1 (2^120) fnf o2 bnf bt rw[Rnf] at emN obtain ⟨fY,emY,bY⟩:=qAr (nf*Ft) ind (2^122) 2 fN fi ד bI obtain ⟨k1,k2⟩:=L_k ك 823 200 ב rfl (by rfl) obtain ⟨fE,emE,-⟩:=qAy (nf*Ft*ind) _ (2^125) fY k1 ד (by rw[k2];norm_num) rw[k2,hem] at emE rw[hem] at fE have hnpos:(0:ℝ)< n :=ד have hnp:=hnpos.ne' have eS:(n:ℝ)*(S/n)=S:=by field_simp have eA:(n:ℝ)*(Sa/n)=Sa:=by field_simp obtain ⟨m1,m2,m3,m4⟩:=qIo hz0 qAx (S/n) (Sa/n) hnR1 hnR2 hc (by rw[abs_div,г hnpos];exact div_le_div_of_nonneg_right o3 hnpos.le) (by rw[div_le_iff₀ hnpos];linarith) (by rw[eS,eA];exact o5) (by rw[eA];exact o6) pi ei em1 emN emY emE exact ⟨fm,fE,m2,m3,m1,m4⟩ lemma qJ6 (n:ℕ) (hnR1:(1:ℝ)<=n) (hnR2:(n:ℝ)<=2^20) (v ind tt:Float) (eps:Word) (W c:ℝ) (hc:c=((n:ℝ)+8)/2^49) (p1:v.isFinite=true) (p2 : |R v-W| ≤ (2*n+6)/2^53*W+1/2^229) (p3:W<=n*2^222) (hW0:0<=W) (fi:ind.isFinite=true) (pi:0< R ind) (ei : |R ind*n-1| ≤ 1/2^48) (gE:good eps=true) (bE : |wv eps| ≤ 2^99) (hep0:0< wv eps) (htt:v*ind+dv eps=tt) : tt.isFinite=true∧|R tt-(W/n+wv eps)| ≤ c/2*(W/n+wv eps):=by have hnpos:(0:ℝ)< n :=ד;have hnp:=hnpos.ne';have hzb:=qBx;have bI : |R ind| ≤ 2:=by rw[г pi];exact L_I hnR1 pi ei have bW:=Б hnR2 (by norm_num:(0:ℝ)<=2^222);have bv : |R v| ≤ 2^250:=by have a1:=qAb (R v) W;rw[Ж hW0] at a1;have a3:=Б hnR2 hW0;linarith obtain ⟨fp,ep1,bp⟩:=qAr v ind (2^250) 2 p1 fi bv bI;obtain ⟨de1,de2⟩:=qAl eps gE;obtain ⟨ft,et,-⟩:=qAy (v*ind) (dv eps) (2^253) fp de1 ד (by rw[de2];exact bE.trans ב);rw[de2,htt] at et;rw[htt] at ft;exact ⟨ft,qJz qAs.le qAx (W/n) (1/2^229) hnR1 hnR2 hc (div_nonneg hW0 hnpos.le) (qJv eps gE hep0) ב le_rfl (by rw[show (n:ℝ)*(W/n)=W by field_simp];exact p2) pi ei ep1 et⟩ lemma qKk (tt nf em:Float) (E:ℕ) (hE1:1<=E) (hE:E<=1222) (hEe:(tt.toBits >>> 52).toNat=E) (c P δ T:ℝ) (hc1:c<=1/2^20) (hc2:9/2^49<=c) (fnf:nf.isFinite=true) (Rnf:R nf=c) (fE:em.isFinite=true) (m2:R em<=2^120) (m3:1/2^201<=R em) (hP0:0< P) (hq1 : |R tt-P| ≤ c/2*P) (m1:2 * |δ| ≤ R em) (hT:T=P-δ^2) (hT0:0< T) : R (ф tt-((nf+nf)*ф tt+em))< п T ∧ п T< R (ф tt+((nf+nf)*ф tt+em)) ∧ п T<2^(127:ℤ):=by have hzb:=qBx have hc0:0< c:=ч ב hc2 obtain ⟨-,-,u1⟩:=qC8 hEe hE1 א obtain ⟨fd,ptt,ed⟩:=qHt hEe hE1 א generalize ф tt=d at fd ed ⊢ have ht2:R tt<=2^200:=u1.le.trans ((Ш ב א).trans_eq (by norm_num:(2:ℝ)^(200:ℤ)=2^200)) generalize hs':п (R tt)=s' at ed have hs0:0< s':=by rw[<-hs'];exact Real.sqrt_pos.mpr ptt have hsq:s'^2=R tt:=by rw[<-hs'];exact Real.sq_sqrt ptt.le have hs1:s'<=2^100:=by rw[<-hs',Real.sqrt_le_iff] exact ⟨by norm_num,ht2.trans ב⟩ have bd : |R d| ≤ 2^102:=by have a1:=qAb (R d) s' rw[г hs0] at a1 linarith have bnf : |R nf| ≤ 1:=by rw[Rnf,г hc0];linarith obtain ⟨f2n,e2n,b2n⟩:=qAy nf nf 1 fnf fnf bnf bnf rw[Rnf] at e2n obtain ⟨fmu,emu,bmu⟩:=qAr (nf+nf) d 4 (2^102) f2n fd ד bd have hEm0:0<=R em:=ؾ ב m3 have bEm : |R em| ≤ 2^120:=by rw[Ж hEm0];exact m2 obtain ⟨fe,ee,be⟩:=qAy ((nf+nf)*d) em (2^120) fmu fE ד bEm obtain ⟨fh,eh,-⟩:=qAy d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨fl,el,-⟩:=qCh d _ (2^123) fd fe (bd.trans ב) ד obtain ⟨hhi,hlo⟩:=qH0 qAs.le qAx hs0.le (hs1.trans ב) hc2 hc1 m3 m2 ed e2n emu ee eh el obtain ⟨b1,b2⟩:=abs_le.mp hq1 have hδ2:δ^2<=R em^2/4:=by have a1:(2 * |δ|)^2<=R em^2:=ض (by positivity) m1 2 rw[mul_pow,sq_abs] at a1 linarith have nP:0<=c*P:=(mul_pos hc0 hP0).le have a3:=Б (by linarith:c<=1) nP have a4:=Н hc0.le nP have nd2:=ق δ have h1':T<=s'^2*(1+c):=by rw[hsq,hT] have a1:=Б (by linarith:P-c/2*P<=R tt) (by linarith:0<=1+c) linarith have h2':s'^2*(1-c)-R em^2/4<=T:=by rw[hsq,hT] have a1:=Б (by linarith:R tt<=P+c/2*P) (by linarith:0<=1-c) linarith obtain ⟨l1,l2⟩:=qIn hT0 hs0 hc0 hc1 hEm0 h1' h2' hhi hlo refine ⟨l1,l2,?_⟩ rw[Real.sqrt_lt' (by positivity),show ((2:ℝ)^(127:ℤ))^2=2^254 by norm_num] have a1:=С (hsq.trans_le ht2) (by linarith:1+c<=2) ד ב linarith lemma qFh (b:Bool) (rows n:ℕ) (eps:Word) (x:Array Word) (r:ℕ) (hr:r< rows) (hn0:0< n) (hn:n<2^20) (hs:x.size=rows*n) (hrn:rows*n<2^32) (hx:x.all sm=true) (he:sm eps=true) (he0:0< Д eps) : L_ok b n eps x r (lnStat b n eps x r):=by have hmul:r*n+n<=rows*n:=by have:=Т n hr;rwa [ػ] at this generalize hgd:(λj:ℕ=>dv x[r*n+j]!)=g;have hg0:∀ j,j< n->(g j).isFinite=true∧|R (g j)| ≤ 2^99 ∧ R (g j)=(Д x[r*n+j]! : ℝ):=λj h=>by obtain ⟨a1,a2⟩:=qA6 _ (qBn x sm hx (r*n+j) א);obtain ⟨a3,a4⟩:=qAl _ a1;rw[<-hgd];beta_reduce;rw[a4];exact ⟨a3,a2,qAq _ (qAd _ a1)⟩ have hnR1:(1:ℝ)<=n:=by exact_mod_cast hn0 have hnR2:(n:ℝ)<=2^20:=by exact_mod_cast hn.le have o4:((К n).map λj=>|R (g j)|).sum<=n*2^99 :=qD4 n _ _ λj h=>(hg0 j h).2.1;have hA2:((К n).map λj=>|R (g j)|).sum<=A_B/4:=by have:=qAw (k:=2^256*2^44) le_rfl;have:=Б hnR2 (by norm_num:(0:ℝ)<=2^99);linarith obtain ⟨o1,o5⟩:=qF0 n hn g (λj=>R (g j)) (λj=>|R (g j)|) (λj h=>⟨(hg0 j h).1,rfl,rfl⟩) hA2 obtain ⟨o2,o6⟩:=qF0 n hn (λj=>(g j).abs) (λj=>|R (g j)|) (λj=>|R (g j)|) (λj h=>by obtain ⟨p,q⟩:=qBk _ (hg0 j h).1 exact ⟨p,q,abs_abs _⟩) hA2 have o3:=qG9 (К n) λj=>R (g j);obtain ⟨hMc,hVc⟩:=qI9 n r x g (λj h=>(hg0 j h).2.2) (lnQ b n x r);generalize hQ:((lnQ b n x r:ℚ):ℝ)=Q at hVc;generalize hS:((К n).map λj=>R (g j)).sum=S at o3 o5 hMc;generalize ((К n).map λj=>|R (g j)|).sum=Sa at o3 o4 o5 o6;unfold lnStat;dsimp only;generalize hnu:n.toUSize=nu;generalize hbase:(r*n).toUSize=base;generalize hnd:n.toUInt64.toFloat=nd;generalize hnf:(n.toUInt64+8).toFloat*П ط=nf;generalize hind:(1.0:Float)/nd=ind;generalize ha:(if b=true then F1.mk 0 0 else lnSum x base nu 0 0 0)=a;generalize hm:(if b=true then (0:Float) else a.s*ind)=m;generalize hv:lnVar x base nu 0 m 0=v;generalize hem:nf*a.t*ind+П ك=em;generalize htt:v*ind+dv eps=tt;generalize hd:ф tt=d;generalize hz:ziv d ((nf+nf)*d+em)=z;generalize hir:(1.0:Float)/dv z=ir;split . rename_i hc;simp only[Bool.and_eq_true,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨c1,c2⟩,c3⟩,c4⟩,c5⟩,c6⟩:=hc;have hnp:(n:ℝ)≠0 :=ד;have hzb:=qBx;have hz0:=qAs.le;have hu:nu.toNat=n:=by rw[<-hnu];exact qAc n א have hb:base.toNat=r*n:=by rw[<-hbase];exact qAc _ א have hxs:x.size<2^32 :=א;obtain ⟨fnf,Rnf⟩:=hnf ▸ qKi n hn;obtain ⟨c,hc⟩:∃ c:ℝ,c=((n:ℝ)+8)/2^49:=⟨_,rfl⟩;rw[<-hc] at Rnf;have hc1:c<=1/2^20:=by rw[hc];linarith have hc2:9/2^49<=c:=by rw[hc];linarith obtain ⟨fnd,Rnd⟩:nd.isFinite=true∧R nd=n:=by rw[<-hnd];have e:n.toUInt64.toNat=n:=by simp;omega have:=qB0 n.toUInt64 א;rwa [e] at this obtain ⟨fi,pi,-,ei⟩:=qGr ind nd fnd (by rw[Rnd,Ж ד];exact hnR2.trans ב) c5 rw[Rnd] at ei;obtain ⟨z1,z2⟩:=qI0 obtain ⟨fm,fE,m2,m3,m1,m4⟩:m.isFinite=true∧em.isFinite=true∧R em<=2^120 ∧ 1/2^201<=R em∧2 * |Q-R m| ≤ R em∧|R m| ≤ 2^110:=by cases b with | true => have e1:a=F1.mk 0 0:=ha.symm;have e2:m=0:=hm.symm;have e3:Q=0:=by rw[<-hQ];simp[lnQ] subst e1;obtain ⟨-,fE,m2,m3,-,-⟩:=qH7 n hnR1 hnR2 0 0 ind nf _ em 0 0 c hc z1 z1 ג (by positivity) (by rw[z2];simp;positivity) (by rw[z2];simp;positivity) fnf Rnf fi pi ei rfl hem;rw[e2,e3,z2];refine ⟨z1,fE,m2,m3,?_,by norm_num⟩;simp only[sub_zero,abs_zero,mul_zero];exact ؾ ב m3 | false => replace ha:lnSum x base nu 0 0 0=a:=ha;replace hm:a.s*ind=m:=hm;have hA:a=⟨A_fsum ((К n).map g),A_fsum ((К n).map λj=>(g j).abs)⟩:=by rw[<-ha,qIr x hxs base nu א n 0 0 0 (by simp[hu])];simp only[USize.toNat_zero,hb,<-hgd,A_fsum,List.range_eq_range'] subst hA;rw[hQ.symm.trans hMc];exact qH7 n hnR1 hnR2 _ _ ind nf m em S Sa c hc o1 o2 o3 o4 o5 o6 fnf Rnf fi pi ei hm hem obtain ⟨p1,p2,p3,hW0⟩:=qIm n hn hn0 g m v _ (by rw[<-hv,qId x hxs base nu m א n 0 0 (by simp[hu])] simp only[USize.toNat_zero,hb,<-hgd,A_fsum,List.range_eq_range']) rfl (λj h=>⟨(hg0 j h).1,(hg0 j h).2.1⟩) fm m4 generalize hW:((К n).map λj=>(R (g j)-R m)^2).sum=W at p2 p3 hW0;generalize hV0:((К n).map λj=>(R (g j)-Q)^2).sum=V0 at hVc;generalize hδ:Q-R m=δ at m1;have hnpos:(0:ℝ)< n :=ד have hWs:W/n=V0/n+δ^2:=by cases b with | true => have e2:m=0:=hm.symm;have e3:Q=0:=by rw[<-hQ];simp[lnQ] rw[<-hδ,<-hW,<-hV0,e2,e3,z2];simp | false => have hs2:=qHf (К n) (λj=>R (g j)) (R m) (by simpa using hnp);simp only[List.length_range] at hs2;rw[hS,hW,show S/n=Q from (hQ.symm.trans hMc).symm,hV0,hδ] at hs2;rw[hs2];field_simp obtain ⟨gE,bE⟩:=qA6 eps he;have epsv:wv eps=(Д eps:ℝ):=qAq _ (qAd _ gE);have hep0:0< wv eps:=by rw[epsv];exact_mod_cast he0 obtain ⟨ft,hq1⟩:=qJ6 n hnR1 hnR2 v ind tt eps W c hc p1 p2 p3 hW0 fi pi ei gE bE hep0 htt;generalize hq:rowVariance n x r (lnQ b n x r)+Д eps=q;have hV1:0<=V0/n:=by rw[<-hV0];refine div_nonneg (List.sum_nonneg λy hy=>?_) hnpos.le;obtain ⟨j,-,rfl⟩:=List.mem_map.1 hy;exact ق _ have hT:(q:ℝ)=(W/n+wv eps)-δ^2:=by rw[<-hq,Rat.cast_add,hVc,epsv,hWs];ring have hT0:(0:ℝ)< q:=by rw[hT,hWs];linarith have hP0:0< W/n+wv eps:=by have:=div_nonneg hW0 hnpos.le;linarith obtain ⟨r1,r2⟩:1<=_∧_<1+1222:=qEf _ 1 1222 c4 (by decide);obtain ⟨l1,l2,hb127⟩:=qKk tt nf em _ r1 א rfl c _ δ (q:ℝ) hc1 hc2 fnf Rnf fE m2 m3 hP0 hq1 m1 hT hT0;rw[hd] at l1 l2;have hq0:0< q:=by exact_mod_cast hT0 have hzne:ziv d ((nf+nf)*d+em)≠Е:=by rw[hz];intro e0;rw[e0] at c3;exact absurd c3 (by decide) have hden:=((qI2 d _ q hq0 l1 l2 hzne hb127).trans hz).symm;rw[<-hq] at hden;obtain ⟨gZ,bZ⟩:=qA6 z c1;obtain ⟨dz1,dz2⟩:=qAl z gZ;obtain ⟨fi2,-,-,ei2⟩:=qGr ir (dv z) dz1 (by rw[dz2];exact bZ) c6;rw[dz2] at ei2;refine ⟨hden,λ_=>⟨fm,fi2,fE,m2,m3,?_,m4,gZ,qJq z gZ c2 c3,ei2⟩⟩;dsimp only;rw[hQ,hδ];exact m1 . exact ⟨rfl,by simp[L_oF]⟩ lemma qKb (m ir em:Float) (w gw bw:Word) (M D:ℝ) (fm:m.isFinite=true) (fi:ir.isFinite=true) (fe:em.isFinite=true) (e1:R em<=2^120) (e2:1/2^201<=R em) (e3:2 * |M-R m| ≤ R em) (e4 : |R m| ≤ 2^110) (hD:1/2^64<=D) (hI : |R ir*D-1| ≤ 1/2^48) (hw:sm w=true) (hg:sm gw=true) (hb:sm bw=true) (s e:Float) (hs:s=(dv w-m)*(dv gw*ir)+dv bw) (he:e=П 0x3D10000000000000*(((dv w-m)*(dv gw*ir)).abs+(dv bw).abs) + em*(dv gw*ir).abs) (hne:ziv s e≠Е) : R (s-e)<(wv w-M)*wv gw/D+wv bw∧(wv w-M)*wv gw/D+wv bw< R (s+e):=by have hzb:=qBx obtain ⟨gw1,bw1⟩:=qA6 w hw obtain ⟨gg1,bg1⟩:=qA6 gw hg obtain ⟨gb1,bb1⟩:=qA6 bw hb obtain ⟨fw,Rw⟩:=qAl w gw1 obtain ⟨fg,Rg⟩:=qAl gw gg1 obtain ⟨fb,Rb⟩:=qAl bw gb1 have hD0:0< D:=ч ב hD have bI : |R ir| ≤ 2^65:=by have a1:=qAb (R ir*D) 1 rw[abs_one,е,г hD0] at a1 have a2:=Г hD (Й (R ir)) linarith obtain ⟨f1,h1,b1⟩:=qCh (dv w) m (2^111) fw fm (by rw[Rw];exact bw1.trans ב) (e4.trans ב) rw[Rw] at h1 obtain ⟨f2,h3,b2⟩:=qAr (dv gw) ir (2^99) (2^65) fg fi (by rw[Rg];exact bg1) bI rw[Rg] at h3 obtain ⟨f3,h4,b3⟩:=qAr _ _ (2^113) (2^166) f1 f2 ד ד generalize hQ:(dv w-m)*(dv gw*ir)=Q at hs he f3 h4 b3 generalize dv gw*ir=G at he f2 h3 b2 h4 hQ obtain ⟨f4,h5,b4⟩:=qAy Q (dv bw) (2^250*2^31) f3 fb (by norm_num at b3 hzb ⊢;linarith) (by rw[Rb];exact bb1.trans ב) rw[Rb,<-hs] at h5 rw[<-hs] at f4 b4 obtain ⟨fa1,Ra1⟩:=qBk Q f3 obtain ⟨fa2,Ra2⟩:=qBk (dv bw) fb rw[Rb] at Ra2 obtain ⟨fa3,Ra3⟩:=qBk G f2 obtain ⟨f5,h6,b5⟩:=qAy Q.abs (dv bw).abs (2^250*2^31) fa1 fa2 (by rw[Ra1,abs_abs];norm_num at b3 hzb ⊢;linarith) (by rw[Ra2,abs_abs];exact bb1.trans ב) rw[Ra1,Ra2] at h6 obtain ⟨k1,k2⟩:=L_k 0x3D10000000000000 977 46 ב rfl (by rfl) obtain ⟨f6,h7,b6⟩:=qAr _ _ 1 (2^250*2^33) k1 f5 (by rw[k2];norm_num) (by norm_num at b5 hzb ⊢;linarith) rw[k2] at h7 have bE : |R em| ≤ 2^120:=by rw[Ж (ؾ ב e2)];exact e1 obtain ⟨f7,h8,b7⟩:=qAr em G.abs (2^120) (2^166) fe fa3 bE (by rw[Ra3,abs_abs];linarith) rw[Ra3] at h8 obtain ⟨f8,h9,b8⟩:=qAy _ _ (2^250*2^38) f6 f7 (by norm_num at b6 hzb ⊢;linarith) (by norm_num at b7 hzb ⊢;linarith) rw[<-he] at f8 h9 b8 obtain ⟨f9,h10,-⟩:=qAy s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨f10,h11,-⟩:=qCh s e (2^250*2^41) f4 f8 (by norm_num at b4 hzb ⊢;linarith) (by norm_num at b8 hzb ⊢;linarith) obtain ⟨-,-,big⟩:=qBh s e hne rw[show (2:ℝ)^(-127:ℤ)=1/2^127 by norm_num] at big exact qIt qAs.le qAx (ؾ ב e2) e1 e3 hD bw1 bg1 e4 h1 hI h3 h4 h5 h6 h7 h8 h9 h10 h11 big lemma qFq (s:LnS) (M:ℚ) (mean:Unit->ℚ) (hmean:mean ()=M) (w gw bw:Word) (hw:sm w=true) (hg:sm gw=true) (hb:sm bw=true) (h:L_oF s (M:ℝ)) : lnOut s w gw bw mean =roundRational ((Д w-M)*Д gw/Д s.den+Д bw):=by unfold lnOut split . rename_i hf;obtain ⟨fm,fi,fe,e1,e2,e3,e4,gd,hD,hI⟩:=h hf;dsimp only;split;. unfold lnSlow;rw[hmean] . rename_i hc have hne:ziv ((dv w-s.m)*(dv gw*s.ir)+dv bw) (П 0x3D10000000000000*(((dv w-s.m)*(dv gw*s.ir)).abs+(dv bw).abs) + s.em*(dv gw*s.ir).abs)≠Е:=λe0=>by rw[e0] at hc exact hc (by decide) obtain ⟨l1,l2⟩:=qKb s.m s.ir s.em w gw bw M (wv s.den) fm fi fe e1 e2 e3 e4 hD hI hw hg hb _ _ rfl rfl hne have ew:=qAq w (qAd _ (qA6 w hw).1);have eg:=qAq gw (qAd _ (qA6 gw hg).1);have eb:=qAq bw (qAd _ (qA6 bw hb).1);have ed:=qAq s.den (qAd _ gd);have hV:(((Д w-M)*Д gw/Д s.den+Д bw:ℚ):ℝ) =(wv w-M)*wv gw/wv s.den+wv bw:=by rw[ew,eg,eb,ed];push_cast;ring rw[<-hV] at l1 l2;exact (qJx _ _ _ l1 l2 hne (qKm _ _ _ l1 l2 hne hc)).symm . unfold lnSlow;rw[hmean] lemma qBy {α:Type} [Inhabited α] (rows k:ℕ) (f:ℕ->α) (hk:k< rows) : (Array.ofFn (n:=rows) λr=>f r.val)[k]! = f k:=by rw[Э _ k (by simpa using hk),Array.getElem_ofFn] lemma qHn:lnF=layernormSpec:=by funext rows n eps x g b;unfold lnF;split . rename_i hc;simp only[Bool.and_eq_true,beq_iff_eq,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨⟨⟨⟨hD,h2⟩,h3⟩,h4⟩,h5⟩,h6⟩,h7⟩,h8⟩,h9⟩:=hc;have hs:(stableNormDomain rows n eps x g && b.size == n && b.all finiteWord)=true:=by simp only[Bool.and_eq_true,beq_iff_eq];exact ⟨⟨hD,h2⟩,h3⟩ unfold layernormSpec layernormWith;rw[if_pos hs];unfold layernormStable;dsimp only;unfold stableNormDomain at hD;simp only[Bool.and_eq_true,beq_iff_eq,gt_iff_lt,decide_eq_true_eq] at hD;obtain ⟨⟨⟨⟨⟨⟨⟨h10,h11⟩,h12⟩,h13⟩,-⟩,-⟩,-⟩,h14⟩:=hD rw[qDj (λr c q=>lnOut (Array.ofFn (n:=rows) λr=>lnStat false n eps x r.val)[r]! x[q]! g[c]! b[c]! λ_=>rowMean n x r) h9 h11 λr p u hr hp hu=>by repeat rw[qAj _ _ א]] apply ه ג;intro q h1 h1';simp only[Array.getElem_ofFn,Array.size_ofFn] at h1 h1' ⊢;have hr:q/n< rows:=м (by rwa [ж]);have hj:q%n< n:=э _ h11;have e1:=qBy rows (q/n) (λr=>lnStat false n eps x r) hr;have e2:=qBy rows (q/n) (λr=>rowMean n x r) hr have e3:=qBy rows (q/n) (λr=>sqrtRational (rowVariance n x r ((Array.ofFn (n:=rows) λr=>rowMean n x r.val)[r]!)+Д eps)) hr beta_reduce at e1 e2 e3;rw[e1,e3,e2];obtain ⟨o1,o2⟩:=qFh false rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qFq _ (rowMean n x (q/n)) _ rfl _ _ _ (qBn x sm h4 q א) (qBn g sm h5 _ א) (qBn b sm h6 _ א) o2,o1];rfl . rfl lemma qF1:rmsF=rmsnormSpec:=by funext rows n eps x g;unfold rmsF;split . rename_i hc;simp only[Bool.and_eq_true,decide_eq_true_eq] at hc;obtain ⟨⟨⟨⟨⟨hD,h4⟩,h5⟩,h7⟩,h8⟩,h9⟩:=hc;unfold rmsnormSpec rmsnormWith;rw[if_pos hD];unfold rmsnormStable;dsimp only;unfold stableNormDomain at hD;simp only[Bool.and_eq_true,beq_iff_eq,gt_iff_lt,decide_eq_true_eq] at hD;obtain ⟨⟨⟨⟨⟨⟨⟨h10,h11⟩,h12⟩,h13⟩,-⟩,-⟩,-⟩,h14⟩:=hD rw[qDj (λr c q=>if x[q]! &&& В == 0 then 0 else lnOut (Array.ofFn (n:=rows) λr=>lnStat true n eps x r.val)[r]! x[q]! g[c]! 0 λ_=>0) h9 h11 λr p u hr hp hu=>by repeat rw[qAj _ _ א]] apply ه ג;intro q h1 h1';simp only[Array.getElem_ofFn,Array.size_ofFn] at h1 h1' ⊢;have hr:q/n< rows:=м (by rwa [ж]);have hj:q%n< n:=э _ h11;have e1:=qBy rows (q/n) (λr=>lnStat true n eps x r) hr;have e3:=qBy rows (q/n) (λr=>sqrtRational (rowVariance n x r 0+Д eps)) hr;beta_reduce at e1 e3;rw[e3];have hw:=qBn x sm h4 q א;split;. rename_i h0;rw[qF4 _ (qA6 _ hw).1 (by simpa using h0),zero_mul,zero_div];rfl . rw[e1];obtain ⟨o1,o2⟩:=qFh true rows n eps x (q/n) hr h11 h8 h12 h9 h4 h7 h14;rw[qFq _ 0 _ rfl _ _ _ hw (qBn g sm h5 _ א) (by decide) o2,o1,qF4 0 (by decide) rfl,sub_zero,add_zero];rfl . rfl end def kernels:Kernels where core:=specCore dense:=⟨linF,gemmF⟩ conv:=⟨convF,depthF⟩ elem:=⟨avgF,maxpoolF,addF,scaleF⟩ norm:=⟨rmsF,lnF⟩ theorem correct:Obligation kernels:=by simp only[Obligation,kernels,specKernels,specDense,specConv,specElem,specNorm,avgpoolSpec,qD5,qFc,qHn,qF1,qEr,qDx,qF5,qFx,qHr,qDz,qAe] end Submission