lemmaeval_subtract10 (s : Store (Fin10)) :
∃ t ≤17*((s 0).length+(s 1).length)+2*(s 2).length+2*(s 3).length+2*(s 4).length+19,
Eval subtract10 s
(update (update (update (update (update s 0 (subtractWord (s 0) (s 1) false).1) 1 []) 2 [])
3 [(subtractWord (s 0) (s 1) false).2]) 4 []) t :=by
lemmaeval_reducePick (s : Store (Fin10)) (c : Bool) (hc : s 3 = [c]) (h9 : s 9 = []) :
∃ t ≤7*(s 8).length+2*(s 0).length+10,
Eval reducePick s
(update (update s 0 (if c then s 8else s 0)) 7 ((!c)::s 7)) t :=by
proof · 16
lines
cases c with
| false=>
refine ⟨2,by omega,?_⟩
simpa [reducePick] using (Eval.branchFalse (p :=Program.skip)
(r :=Program.seq (copy 809) (.push 7false)) hc (Eval.push (7 : Fin10) true s))
| true=>let u := update (update s 0 (s 8)) 9 []
have hc' := eval_copy (a := (8 : Fin10)) (b :=0) (tmp :=9) (by decide) (by decide) (by decide) s
havehp : Eval (Program.push 7false) u (update u 7 (false::s 7)) 1:=by
simpa [u] usingEval.push (7 : Fin10) false u
have hh :=Eval.branchTrue (p :=Program.skip) (q :=Program.push 7true) hc (Eval.seq hc' hp)
haveheq : update u 7 (false::s 7) = update (update s 0 (s 8)) 7 (false::s 7) :=by
funext k; by_cases hk0 : k=0 <;> by_cases hk7 : k=7 <;> by_cases hk9 : k=9 <;> simp_all [u]
rw [heq] at hh
refine ⟨7*(s 8).length+2*(s 0).length+10,le_refl _,?_⟩
convert hh using1 <;> simp [reducePick,h9,Nat.add_comm] ; omega
lemmaeval_reduceOnce (s : Store (Fin10)) (hs : divScratch s) :
∃ t ≤100*((s 0).length+(s 5).length+1),
Eval reduceOnce s (update (update s 0 (reduceWord (s 0) (s 5)).1)
7 ((reduceWord (s 0) (s 5)).2::s 7)) t :=by
proof · 37
lines
obtain ⟨h1,h2,h3,h4,h8,h9⟩ := hs
let s₁ := update (update s 8 (s 0)) 9 []
let s₂ := update (update s₁ 1 (s 5)) 9 []
havehc0 : Eval (copy 089) s s₁ (7*(s 0).length+7) :=by
simpa [s₁,h8,h9] using eval_copy (a := (0 : Fin10)) (b :=8) (tmp :=9) (by decide) (by decide) (by decide) s
havehc5 : Eval (copy 519) s₁ s₂ (7*(s 5).length+7) :=by
simpa [s₂,s₁,h1] using eval_copy (a := (5 : Fin10)) (b :=1) (tmp :=9) (by decide) (by decide) (by decide) s₁
obtain ⟨t,ht,hd⟩ := eval_subtract10 s₂
let r := subtractWord (s 0) (s 5) falselet s₃ := update (update (update (update (update s₂ 0 r.1) 1 []) 2 []) 3 [r.2]) 4 []
havehd' : Eval subtract10 s₂ s₃ t :=by simpa [s₃,r,s₂,s₁] using hd
obtain ⟨u,hu,hp⟩ := eval_reducePick s₃ r.2 (by simp [s₃]) (by simp [s₃,s₂])
let s₄ := update (update s₃ 0 (if r.2then s 0else r.1)) 7 ((!r.2)::s 7)
havehp' : Eval reducePick s₃ s₄ u :=by simpa [s₄,s₃,s₂,s₁] using hp
obtain ⟨v,hv,hn⟩ := eval_normalize (a := (0 : Fin10)) (tmp :=2) (flag :=4)
(by decide) (by decide) (by decide) s₄
let s₅ := update (update (update s₄ 0 (normalizeWord (s₄ 0))) 2 []) 4 []
havehn' : Eval (normalize 024) s₄ s₅ v := hn
let s₆ := update s₅ 3 []
havehclear3 : Eval (clear 3) s₅ s₆ 3:=by simpa [s₆,s₅,s₄,s₃] using eval_clear (3 : Fin10) s₅
havehclear8 : Eval (clear 8) s₆ (update s₆ 8 []) (2*(s 0).length+1) :=by
simpa [s₆,s₅,s₄,s₃,s₂,s₁] using eval_clear (8 : Fin10) s₆
have heval :=Eval.seq hc0 (Eval.seq hc5 (Eval.seq hd' (Eval.seq hp' (Eval.seq hn' (Eval.seq hclear3 hclear8)))))
haveheq : update s₆ 8 [] = update (update s 0 (reduceWord (s 0) (s 5)).1)
7 ((reduceWord (s 0) (s 5)).2::s 7) :=by
funext k
cases hr : r.2 <;> dsimp [r] at hr <;> fin_cases k <;>
simp [s₆,s₅,s₄,s₃,s₂,s₁,reduceWord,r,hr,h1,h2,h3,h4,h8,h9]
rw [heq] at heval
refine ⟨_,?_,heval⟩
havehlen : r.1.length = max (s 0).length (s 5).length := subtractWord_length ..
havehchosen : (if r.2then s 0else r.1).length ≤ (s 0).length+(s 5).length :=by
cases r.2 <;> simp [hlen]
simp [s₂,s₁,h2,h3,h4] at ht
simp [s₃,s₂,s₁] at hu
simp [s₄,s₃] at hv
omega
lemmaeval_divLoop (s : Store (Fin10)) (hs : divScratch s)
(hb : 0<wordValue (s 5)) (hr : wordValue (s 0)<wordValue (s 5))
(B : ℕ) (hB : (s 0).length ≤B) (hbB : (s 5).length ≤B) :
∃ t ≤ (s 6).length*(250*(B+1))+1,
Eval divLoop s
(update (update (update s 6 []) 0 (divideDigits (s 5) (s 6) (s 0) (s 7)).1)
7 (divideDigits (s 5) (s 6) (s 0) (s 7)).2) t :=by
proof · 30
lines
generalize h6 : s 6 = l
induction l generalizing s with
| nil =>haveheq : update (update (update s 6 []) 0 (s 0)) 7 (s 7) = s :=by
funext k; by_cases hk6 : k=6 <;> by_cases hk0 : k=0 <;> by_cases hk7 : k=7 <;> simp_all
refine ⟨1,by simp,?_⟩
simpa only [divideDigits,heq,divLoop] using (Eval.whileNil (p := divBody) h6)
| cons x l ih =>let r := reduceWord (x::s 0) (s 5)
let u := update (update (update s 6 l) 0 r.1) 7 (r.2::s 7)
havehc : wordValue (x::s 0) < 2*wordValue (s 5) :=byhave hx := bitNat_le_one x
rw [wordValue_bit]; omega
obtain ⟨hr',_,hlen⟩ := reduceWord_spec (x::s 0) (s 5) hb hc
obtain ⟨t,ht,he⟩ := eval_divBody s hs x l h6
havehs' : divScratch u :=by simpa [divScratch,u] using hs
obtain ⟨v,hv,hloop⟩ := ih u hs' (by simpa [u] using hb) (by simpa [u,r] using hr')
(by simpa [u,r] using hlen.trans hbB) (by simpa [u] using hbB) (by simp [u])
have h :=Eval.whileCons (by simp [h6]) he hloop
haveheq : update (update (update u 6 []) 0 (divideDigits (u 5) l (u 0) (u 7)).1)
7 (divideDigits (u 5) l (u 0) (u 7)).2 =
update (update (update s 6 []) 0 (divideDigits (s 5) (x::l) (s 0) (s 7)).1)
7 (divideDigits (s 5) (x::l) (s 0) (s 7)).2:=by
funext k; by_cases hk6 : k=6 <;> by_cases hk0 : k=0 <;> by_cases hk7 : k=7 <;>
simp_all [u,r,divideDigits]
rw [heq] at h
refine ⟨1+t+v,?_,h⟩
simp only [List.length_cons]
havehstep : 1+t ≤250*(B+1) :=by omega
nlinarith
Binary division starts with numerator in 0 and denominator in 5.
The original denominator is retained; quotient is placed in 7, remainder in 0.
Every other register is cleared.
lemmaeval_divide (s : Store (Fin10)) (hs : divScratch s) (h6 : s 6=[]) (h7 : s 7=[])
(hb : 0<wordValue (s 5)) :
∃ t ≤300*((s 0).length+1)*((s 5).length+1)+30,
Eval divide s (update (update s 0 (wordValue (s 0)%wordValue (s 5)).bits)
7 (wordValue (s 0)/wordValue (s 5)).bits) t :=by
proof · 44
lines
let u := update (update s 0 []) 6 (s 0).reverse
havehre : Eval (reverse 06) s u (2*(s 0).length+1) :=by
simpa [u,h6] using eval_reverse (a := (0 : Fin10)) (b :=6) (by decide) s
havehu : divScratch u :=by simpa [divScratch,u] using hs
obtain ⟨t,ht,he⟩ := eval_divLoop u hu (by simpa [u] using hb)
(by simpa [u,wordValue] using hb) (s 5).length (by simp [u]) (by simp [u])
let r := divideDigits (s 5) (s 0).reverse [] []
let v := update (update (update u 6 []) 0 r.1) 7 r.2havehe' : Eval divLoop u v t :=by simpa [v,r,u,h7] using he
obtain ⟨w,hw,hn⟩ := eval_normalize (a := (7 : Fin10)) (tmp :=2) (flag :=4)
(by decide) (by decide) (by decide) v
have h :=Eval.seq hre (Eval.seq he' hn)
obtain ⟨hr,hq⟩ := divideDigits_quotient_remainder (s 0) (s 5) hb
havehrnorm : normalizeWord r.1 = r.1:=by-- Each nonempty digit loop ends in a normalized one-step remainder.have aux (b l a q : ListBool) (ha : normalizeWord a=a) :
normalizeWord (divideDigits b l a q).1=(divideDigits b l a q).1:=by
induction l generalizing a q with
| nil => exact ha
| cons x l ih =>
apply ih
cases hh : (subtractWord (x::a) b false).2 <;>
simp [reduceWord,hh,normalizeWord_eq_bits]
exact aux _ _ _ _ (by rfl)
havehrbits : r.1=(wordValue (s 0)%wordValue (s 5)).bits :=by
rw [←hrnorm,normalizeWord_eq_bits]
exact congrArg Nat.bits hr
have h2 := hs.2.1have h4 := hs.2.2.2.1haveheq : update (update (update v 7 (normalizeWord (v 7))) 2 []) 4 [] =
update (update s 0 (wordValue (s 0)%wordValue (s 5)).bits)
7 (wordValue (s 0)/wordValue (s 5)).bits :=by
funext k; by_cases hk0 : k=0 <;> by_cases hk7 : k=7 <;> by_cases hk2 : k=2 <;>
by_cases hk4 : k=4 <;> by_cases hk6 : k=6 <;>
simp_all [v,u,hrbits,normalizeWord_eq_bits,r]
rw [heq] at h
refine ⟨_,?_,h⟩
obtain ⟨_,_,hlen,_⟩ := divideDigits_spec (s 5) (s 0).reverse [] [] hb (by simpa [wordValue] using hb)
simp [v,u,h2,h4] at hw
simp [u] at ht
simp at hlen
change r.2.length = (s 0).length at hlen
rw [hlen] at hw
nlinarith