Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Exact second carry-loop semantics
usize write cursor, and let c be a u64 carry satisfying
c<10^q. Suppose that, whenever j<q, j<768, and
c<10^{q-j-1}, cell a[j] is zero. If the extracted
left_shift_loop1 call on (b,a,q,c) returns (b',a'), then
there is a length-q list u of decimal digits such that
Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: DecimalLeftShiftSecondLoopSemantics]
theorem DecimalLeftShiftSecondLoopSemantics
(a a' : Array Std.U8 768#usize) (b b' : Bool)
(q : Std.Usize) (carry : Std.U64)
(hcarry : carry.val < 10 ^ q.val)
(hzero : forall j, j < q.val -> j < 768 ->
carry.val < 10 ^ (q.val - j - 1) -> a.val[j]? = some 0#u8)
(hloop :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b a q carry = ok (b', a')) :
exists outDigits : List Nat,
outDigits.length = q.val /\
(forall z, z ∈ outDigits -> z < 10) /\
carry.val = outDigits.foldl (fun acc z => 10 * acc + z) 0 /\
(forall j, j < outDigits.length -> j < 768 ->
(a'.val.map (fun z => z.val))[j]? = outDigits[j]?) /\
(forall j, j < 768 -> q.val <= j -> a'.val[j]? = a.val[j]?) /\
(b' = true <-> b = true \/
exists j, j < outDigits.length /\ 768 <= j /\
outDigits[j]? != some 0) := by
Proof
Natural-language proof
The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.
Explanatory prose status
Prose retained as authored. The explanatory prose is preserved as it stood when captured.
Proof
usize subtraction w-1 is legal.
Put
u64 division is legal because its divisor is ten.
The standard division bound 10t\le c_s<2^{64} discharges the checked
u64 multiplication, and it also discharges the following checked
subtraction. Euclidean division gives c_s=10t+r and r<10; hence the
u8 cast of r is exact. Moreover, if w_1=w-1, then
t<10^{w_1}, directly from c_s<10^w and
10^w=10\cdot10^{w_1}. Prepending r to v therefore preserves every
arithmetic clause of (1), including the exact fold equation.Lean proof
let F := fun ys : List Nat =>
ys.foldl (fun acc z => 10 * acc + z) 0
have foldlFrom (ys : List Nat) (acc : Nat) :
ys.foldl (fun n z => 10 * n + z) acc =
acc * 10 ^ ys.length + F ys := by
induction ys generalizing acc with
| nil => simp [F]
| cons y ys ih =>
simp only [List.foldl_cons, List.length_cons]
rw [ih, pow_succ]
change (10 * acc + y) * 10 ^ ys.length + F ys =
acc * (10 ^ ys.length * 10) +
(y :: ys).foldl (fun n z => 10 * n + z) 0
simp only [List.foldl_cons]
rw [ih]
ring
have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) :
F ys < 10 ^ ys.length := by
induction ys with
| nil => simp [F]
| cons y ys ih =>
have hy : y < 10 := hd y (by simp)
have hys : forall z, z ∈ ys -> z < 10 := by
intro z hz
exact hd z (by simp [hz])
have hi := ih hys
rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
Nat.mul_le_mul_right _ (by omega)
calc
y * 10 ^ ys.length + F ys <
y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
_ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
Nat.add_le_add_right hmul _
_ = 10 ^ ys.length * 10 := by ring
let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
let (bc, ac, wi, c) := x
wi.val <= q.val /\
c.val < 10 ^ wi.val /\
exists outs : List Nat,
outs.length = q.val - wi.val /\
(forall z, z ∈ outs -> z < 10) /\
carry.val = c.val * 10 ^ outs.length + F outs /\
(forall j, j < outs.length -> wi.val + j < 768 ->
(ac.val.map (fun z : Std.U8 => z.val))[wi.val + j]? = outs[j]?) /\
(forall j, j < 768 -> (j < wi.val \/ q.val <= j) ->
ac.val[j]? = a.val[j]?) /\
(bc = true <-> b = true \/
exists j, j < outs.length /\ 768 <= wi.val + j /\
outs[j]? != some 0)
let Post := fun (x : Bool × Array Std.U8 768#usize) =>
let (bc, ac) := x
exists outDigits : List Nat,
outDigits.length = q.val /\
(forall z, z ∈ outDigits -> z < 10) /\
carry.val = F outDigits /\
(forall j, j < outDigits.length -> j < 768 ->
(ac.val.map (fun z : Std.U8 => z.val))[j]? = outDigits[j]?) /\
(forall j, j < 768 -> q.val <= j -> ac.val[j]? = a.val[j]?) /\
(bc = true <-> b = true \/
exists j, j < outDigits.length /\ 768 <= j /\
outDigits[j]? != some 0)
let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
let (bc, ac, wi, c) := x
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body bc ac wi c
have hp : Post (b', a') := by
refine ResultLoopPartial Inv Post Body ?_ (b, a, q, carry) (b', a') ?_ ?_
· intro x hx r hr
rcases x with ⟨bc, ac, wi, c⟩
simp only [Inv] at hx
rcases hx with ⟨hwiq, hcfit, outs, houtslen, houtsdigit, hnum,
harr, houtside, hsticky⟩
simp only [Body,
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body] at hr
by_cases hc0 : c.val = 0
· have hcEq : c = 0#u64 := UScalar.eq_of_val_eq (by simpa using hc0)
have hnotgt : ¬ c > 0#u64 := by simp [hcEq]
simp [hnotgt] at hr
subst r
simp only [Post]
have hcarryOut : carry.val = F outs := by simpa [hc0] using hnum
have houtsBound : carry.val < 10 ^ outs.length := by
rw [hcarryOut]
exact foldBound outs houtsdigit
let zeros := List.replicate wi.val 0
let final := zeros ++ outs
refine ⟨final, ?_, ?_, ?_, ?_, ?_, ?_⟩
· simp [final, zeros, houtslen]
omega
· intro z hz
simp only [final, zeros, List.mem_append, List.mem_replicate] at hz
rcases hz with ⟨_, rfl⟩ | hz
· omega
· exact houtsdigit z hz
· rw [hcarryOut]
have hzF : F final = F outs := by
simp only [final, zeros, F]
rw [List.foldl_append]
have hrep :
(List.replicate wi.val 0).foldl
(fun acc z => 10 * acc + z) 0 = 0 := by
induction wi.val with
| zero => rfl
| succ n ih => simpa [List.replicate_succ, ih]
rw [hrep]
exact hzF.symm
· intro j hj hphys
by_cases hjwi : j < wi.val
· have hbase := houtside j hphys (Or.inl hjwi)
have hpowle : 10 ^ outs.length <= 10 ^ (q.val - j - 1) := by
apply Nat.pow_le_pow_right (by omega)
rw [houtslen]
omega
have hz := hzero j (by omega) hphys (lt_of_lt_of_le houtsBound hpowle)
have hac0 : ac.val[j]? = some 0#u8 := by simpa [hbase] using hz
rw [List.getElem?_map, hac0]
rw [List.getElem?_append_left (by simpa [zeros] using hjwi)]
rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hjwi)]
rfl
· have hjoff : exists t, j = wi.val + t /\ t < outs.length := by
refine ⟨j - wi.val, by omega, ?_⟩
simp [final, zeros] at hj
omega
rcases hjoff with ⟨t, rfl, ht⟩
have ha := harr t ht hphys
rw [List.getElem?_append_right (by simp [zeros])]
simpa [final, zeros] using ha
· intro j hj hqj
exact houtside j hj (Or.inr hqj)
· constructor
· intro htrue
rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
refine ⟨wi.val + j, ?_, hjcap, ?_⟩
· simp [final, zeros, houtslen]
omega
· rw [List.getElem?_append_right (by simp [zeros])]
simpa [zeros] using hjnz
· intro h
apply hsticky.mpr
rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
by_cases hji : j < wi.val
· have hzj : final[j]? = some 0 := by
rw [List.getElem?_append_left (by simpa [zeros] using hji)]
rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hji)]
rw [hzj] at hjnz
simp at hjnz
· refine ⟨j - wi.val, ?_, ?_, ?_⟩
· simp [final, zeros] at hj
omega
· omega
· rw [List.getElem?_append_right (by simpa [zeros] using hji)] at hjnz
simpa [zeros] using hjnz
· have hcpos : 0 < c.val := by omega
have hgt : c > 0#u64 := by
change 0 < c.val
exact hcpos
have hwipos : 0 < wi.val := by
by_contra hn
have hwi0 : wi.val = 0 := by omega
rw [hwi0] at hcfit
simp at hcfit
omega
obtain ⟨wi1, hwi1, hwi1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := wi) (y := 1#usize) (by simpa using hwipos))
norm_num at hwi1Val
simp only [hgt, if_true] at hr
rw [hwi1] at hr
simp only [bind_tc_ok] at hr
obtain ⟨quotient, hquotient, hquotientVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.div_spec c (y := 10#u64) (by norm_num))
rw [hquotient] at hr
simp only [bind_tc_ok] at hr
have hmulBound : 10 * quotient.val <= U64.max := by
rw [hquotientVal]
have hcLt : c.val < 2 ^ 64 := c.bv.isLt
have hqle : 10 * (c.val / 10) <= c.val := Nat.mul_div_le c.val 10
norm_num [U64.max, U64.numBits] at hcLt ⊢
omega
obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := quotient) (by
simpa [Nat.mul_comm] using hmulBound))
rw [htenq] at hr
simp only [bind_tc_ok] at hr
have htenqLe : tenq.val <= c.val := by
rw [htenqVal, hquotientVal]
exact Nat.mul_div_le c.val 10
obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := c) (y := tenq) htenqLe)
rw [hrem] at hr
simp only [bind_tc_ok] at hr
have hremFormula : rem.val = c.val % 10 := by
have htenqFormula : tenq.val = 10 * (c.val / 10) := by
rw [htenqVal, hquotientVal]
norm_num
have heuc := Nat.mod_add_div c.val 10
rw [hremVal.1, htenqFormula]
omega
have hremLt : rem.val < 10 := by
rw [hremFormula]
exact Nat.mod_lt _ (by omega)
have hquotientFormula : quotient.val = c.val / 10 := by
simpa using hquotientVal
have hquotientFit : quotient.val < 10 ^ wi1.val := by
rw [hquotientFormula]
apply (Nat.div_lt_iff_lt_mul (by omega)).2
rw [← pow_succ]
have hwiSucc : wi1.val + 1 = wi.val := by omega
rw [hwiSucc]
exact hcfit
let newOuts := rem.val :: outs
have hnewlen : newOuts.length = q.val - wi1.val := by
simp [newOuts, houtslen]
omega
have hnewdigit : forall z, z ∈ newOuts -> z < 10 := by
intro z hz
simp only [newOuts, List.mem_cons] at hz
rcases hz with rfl | hz
· exact hremLt
· exact houtsdigit z hz
have houtStep : F newOuts = rem.val * 10 ^ outs.length + F outs := by
simpa [newOuts, F] using foldlFrom outs rem.val
have hnumNew : carry.val =
quotient.val * 10 ^ newOuts.length + F newOuts := by
have heuc : c.val = 10 * quotient.val + rem.val := by
rw [hquotientFormula, hremFormula]
have hmoddiv := Nat.mod_add_div c.val 10
omega
have hlenSucc : q.val - wi1.val = outs.length + 1 := by
rw [houtslen]
omega
calc
carry.val = c.val * 10 ^ outs.length + F outs := hnum
_ = (10 * quotient.val + rem.val) * 10 ^ outs.length + F outs := by rw [heuc]
_ = quotient.val * 10 ^ newOuts.length + F newOuts := by
rw [houtStep, hnewlen, hlenSucc, pow_succ]
ring
by_cases hcap : wi1.val < 768
· have hcastRem : (UScalar.cast .U8 rem).val = rem.val := by
simp [UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : rem.val < 256)]
have hwibound : wi1.val < ac.val.length := by
have hacLen : ac.val.length = 768 := by simpa using ac.property
rw [hacLen]
exact hcap
obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec ac wi1 (UScalar.cast .U8 rem)
(by simpa [Array.length] using hwibound))
have hcapWord : wi1 <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hcap
simp only [hcapWord, if_true] at hr
simp only [lift] at hr
simp only [bind_tc_ok] at hr
rw [hac1] at hr
simp only [bind_tc_ok] at hr
cases hr
simp only [Inv]
refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
hnumNew, ?_, ?_, ?_⟩
· intro j hj hpos
rw [hac1Val]
simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
cases j with
| zero => simpa [newOuts, hcastRem] using hcap
| succ j =>
have hjold : j < outs.length := by
simp [newOuts] at hj
omega
have hidx : wi1.val + (j + 1) = wi.val + j := by omega
have hne : wi1.val ≠ wi.val + j := by omega
simp [newOuts, hidx, hne]
simpa using harr j hjold (by omega)
· intro j hj houtsideNew
rw [hac1Val]
simp only [Aeneas.Std.Array.set, List.getElem?_set]
have hne : wi1.val ≠ j := by omega
simp [hne]
apply houtside j hj
omega
· constructor
· intro htrue
rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
refine ⟨j + 1, ?_, ?_, ?_⟩
· simp [newOuts]
omega
· omega
· simpa [newOuts] using hjnz
· intro h
apply hsticky.mpr
rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
cases j with
| zero => omega
| succ j =>
refine ⟨j, ?_, ?_, ?_⟩
· simp [newOuts] at hj
omega
· omega
· simpa [newOuts] using hjnz
· have hcapWord : ¬ wi1 <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hcap
simp only [hcapWord, if_false] at hr
by_cases hrem0 : rem.val = 0
· have hremEq : rem = 0#u64 := UScalar.eq_of_val_eq (by simpa using hrem0)
have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
simp [hnotgt] at hr
subst r
simp only [Inv]
refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
hnumNew, ?_, ?_, ?_⟩
· intro j hj hpos
cases j with
| zero => omega
| succ j =>
have hjold : j < outs.length := by
simp [newOuts] at hj
omega
have hidx : wi1.val + (j + 1) = wi.val + j := by omega
simpa [newOuts, hidx] using harr j hjold (by omega)
· intro j hj houtsideNew
apply houtside j hj
omega
· constructor
· intro htrue
rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
refine ⟨j + 1, ?_, ?_, ?_⟩
· simp [newOuts]
omega
· omega
· simpa [newOuts] using hjnz
· intro h
apply hsticky.mpr
rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
cases j with
| zero => simp [newOuts, hrem0] at hjnz
| succ j =>
refine ⟨j, ?_, ?_, ?_⟩
· simp [newOuts] at hj
omega
· omega
· simpa [newOuts] using hjnz
· have hgtRem : rem > 0#u64 := by
change 0 < rem.val
omega
simp [hgtRem] at hr
subst r
simp only [Inv]
refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
hnumNew, ?_, ?_, ?_⟩
· intro j hj hpos
cases j with
| zero => omega
| succ j =>
have hjold : j < outs.length := by
simp [newOuts] at hj
omega
have hidx : wi1.val + (j + 1) = wi.val + j := by omega
simpa [newOuts, hidx] using harr j hjold (by omega)
· intro j hj houtsideNew
apply houtside j hj
omega
· constructor
· intro _
right
refine ⟨0, by simp [newOuts], by omega, ?_⟩
simp [newOuts, hrem0]
· exact fun _ => True.intro
· simp only [Inv]
refine ⟨le_rfl, hcarry, [], by simp, ?_, ?_, ?_, ?_, ?_⟩
· intro z hz
simp at hz
· simp [F]
· intro j hj
simp at hj
· intro j hj houtside
trivial
· simp
· simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1, Body]
using hloop
simpa [Post, F] using hp
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: DecimalLeftShiftSecondLoopSemantics]
theorem DecimalLeftShiftSecondLoopSemantics
(a a' : Array Std.U8 768#usize) (b b' : Bool)
(q : Std.Usize) (carry : Std.U64)
(hcarry : carry.val < 10 ^ q.val)
(hzero : forall j, j < q.val -> j < 768 ->
carry.val < 10 ^ (q.val - j - 1) -> a.val[j]? = some 0#u8)
(hloop :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b a q carry = ok (b', a')) :
exists outDigits : List Nat,
outDigits.length = q.val /\
(forall z, z ∈ outDigits -> z < 10) /\
carry.val = outDigits.foldl (fun acc z => 10 * acc + z) 0 /\
(forall j, j < outDigits.length -> j < 768 ->
(a'.val.map (fun z => z.val))[j]? = outDigits[j]?) /\
(forall j, j < 768 -> q.val <= j -> a'.val[j]? = a.val[j]?) /\
(b' = true <-> b = true \/
exists j, j < outDigits.length /\ 768 <= j /\
outDigits[j]? != some 0) := by
-- BODY
let F := fun ys : List Nat =>
ys.foldl (fun acc z => 10 * acc + z) 0
have foldlFrom (ys : List Nat) (acc : Nat) :
ys.foldl (fun n z => 10 * n + z) acc =
acc * 10 ^ ys.length + F ys := by
induction ys generalizing acc with
| nil => simp [F]
| cons y ys ih =>
simp only [List.foldl_cons, List.length_cons]
rw [ih, pow_succ]
change (10 * acc + y) * 10 ^ ys.length + F ys =
acc * (10 ^ ys.length * 10) +
(y :: ys).foldl (fun n z => 10 * n + z) 0
simp only [List.foldl_cons]
rw [ih]
ring
have foldBound (ys : List Nat) (hd : forall z, z ∈ ys -> z < 10) :
F ys < 10 ^ ys.length := by
induction ys with
| nil => simp [F]
| cons y ys ih =>
have hy : y < 10 := hd y (by simp)
have hys : forall z, z ∈ ys -> z < 10 := by
intro z hz
exact hd z (by simp [hz])
have hi := ih hys
rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
have hmul : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
Nat.mul_le_mul_right _ (by omega)
calc
y * 10 ^ ys.length + F ys <
y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
_ <= 9 * 10 ^ ys.length + 10 ^ ys.length :=
Nat.add_le_add_right hmul _
_ = 10 ^ ys.length * 10 := by ring
let Inv := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
let (bc, ac, wi, c) := x
wi.val <= q.val /\
c.val < 10 ^ wi.val /\
exists outs : List Nat,
outs.length = q.val - wi.val /\
(forall z, z ∈ outs -> z < 10) /\
carry.val = c.val * 10 ^ outs.length + F outs /\
(forall j, j < outs.length -> wi.val + j < 768 ->
(ac.val.map (fun z : Std.U8 => z.val))[wi.val + j]? = outs[j]?) /\
(forall j, j < 768 -> (j < wi.val \/ q.val <= j) ->
ac.val[j]? = a.val[j]?) /\
(bc = true <-> b = true \/
exists j, j < outs.length /\ 768 <= wi.val + j /\
outs[j]? != some 0)
let Post := fun (x : Bool × Array Std.U8 768#usize) =>
let (bc, ac) := x
exists outDigits : List Nat,
outDigits.length = q.val /\
(forall z, z ∈ outDigits -> z < 10) /\
carry.val = F outDigits /\
(forall j, j < outDigits.length -> j < 768 ->
(ac.val.map (fun z : Std.U8 => z.val))[j]? = outDigits[j]?) /\
(forall j, j < 768 -> q.val <= j -> ac.val[j]? = a.val[j]?) /\
(bc = true <-> b = true \/
exists j, j < outDigits.length /\ 768 <= j /\
outDigits[j]? != some 0)
let Body := fun (x : Bool × Array Std.U8 768#usize × Std.Usize × Std.U64) =>
let (bc, ac, wi, c) := x
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body bc ac wi c
have hp : Post (b', a') := by
refine ResultLoopPartial Inv Post Body ?_ (b, a, q, carry) (b', a') ?_ ?_
· intro x hx r hr
rcases x with ⟨bc, ac, wi, c⟩
simp only [Inv] at hx
rcases hx with ⟨hwiq, hcfit, outs, houtslen, houtsdigit, hnum,
harr, houtside, hsticky⟩
simp only [Body,
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body] at hr
by_cases hc0 : c.val = 0
· have hcEq : c = 0#u64 := UScalar.eq_of_val_eq (by simpa using hc0)
have hnotgt : ¬ c > 0#u64 := by simp [hcEq]
simp [hnotgt] at hr
subst r
simp only [Post]
have hcarryOut : carry.val = F outs := by simpa [hc0] using hnum
have houtsBound : carry.val < 10 ^ outs.length := by
rw [hcarryOut]
exact foldBound outs houtsdigit
let zeros := List.replicate wi.val 0
let final := zeros ++ outs
refine ⟨final, ?_, ?_, ?_, ?_, ?_, ?_⟩
· simp [final, zeros, houtslen]
omega
· intro z hz
simp only [final, zeros, List.mem_append, List.mem_replicate] at hz
rcases hz with ⟨_, rfl⟩ | hz
· omega
· exact houtsdigit z hz
· rw [hcarryOut]
have hzF : F final = F outs := by
simp only [final, zeros, F]
rw [List.foldl_append]
have hrep :
(List.replicate wi.val 0).foldl
(fun acc z => 10 * acc + z) 0 = 0 := by
induction wi.val with
| zero => rfl
| succ n ih => simpa [List.replicate_succ, ih]
rw [hrep]
exact hzF.symm
· intro j hj hphys
by_cases hjwi : j < wi.val
· have hbase := houtside j hphys (Or.inl hjwi)
have hpowle : 10 ^ outs.length <= 10 ^ (q.val - j - 1) := by
apply Nat.pow_le_pow_right (by omega)
rw [houtslen]
omega
have hz := hzero j (by omega) hphys (lt_of_lt_of_le houtsBound hpowle)
have hac0 : ac.val[j]? = some 0#u8 := by simpa [hbase] using hz
rw [List.getElem?_map, hac0]
rw [List.getElem?_append_left (by simpa [zeros] using hjwi)]
rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hjwi)]
rfl
· have hjoff : exists t, j = wi.val + t /\ t < outs.length := by
refine ⟨j - wi.val, by omega, ?_⟩
simp [final, zeros] at hj
omega
rcases hjoff with ⟨t, rfl, ht⟩
have ha := harr t ht hphys
rw [List.getElem?_append_right (by simp [zeros])]
simpa [final, zeros] using ha
· intro j hj hqj
exact houtside j hj (Or.inr hqj)
· constructor
· intro htrue
rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
refine ⟨wi.val + j, ?_, hjcap, ?_⟩
· simp [final, zeros, houtslen]
omega
· rw [List.getElem?_append_right (by simp [zeros])]
simpa [zeros] using hjnz
· intro h
apply hsticky.mpr
rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
by_cases hji : j < wi.val
· have hzj : final[j]? = some 0 := by
rw [List.getElem?_append_left (by simpa [zeros] using hji)]
rw [List.getElem?_replicate_of_lt (by simpa [zeros] using hji)]
rw [hzj] at hjnz
simp at hjnz
· refine ⟨j - wi.val, ?_, ?_, ?_⟩
· simp [final, zeros] at hj
omega
· omega
· rw [List.getElem?_append_right (by simpa [zeros] using hji)] at hjnz
simpa [zeros] using hjnz
· have hcpos : 0 < c.val := by omega
have hgt : c > 0#u64 := by
change 0 < c.val
exact hcpos
have hwipos : 0 < wi.val := by
by_contra hn
have hwi0 : wi.val = 0 := by omega
rw [hwi0] at hcfit
simp at hcfit
omega
obtain ⟨wi1, hwi1, hwi1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := wi) (y := 1#usize) (by simpa using hwipos))
norm_num at hwi1Val
simp only [hgt, if_true] at hr
rw [hwi1] at hr
simp only [bind_tc_ok] at hr
obtain ⟨quotient, hquotient, hquotientVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.div_spec c (y := 10#u64) (by norm_num))
rw [hquotient] at hr
simp only [bind_tc_ok] at hr
have hmulBound : 10 * quotient.val <= U64.max := by
rw [hquotientVal]
have hcLt : c.val < 2 ^ 64 := c.bv.isLt
have hqle : 10 * (c.val / 10) <= c.val := Nat.mul_div_le c.val 10
norm_num [U64.max, U64.numBits] at hcLt ⊢
omega
obtain ⟨tenq, htenq, htenqVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := quotient) (by
simpa [Nat.mul_comm] using hmulBound))
rw [htenq] at hr
simp only [bind_tc_ok] at hr
have htenqLe : tenq.val <= c.val := by
rw [htenqVal, hquotientVal]
exact Nat.mul_div_le c.val 10
obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := c) (y := tenq) htenqLe)
rw [hrem] at hr
simp only [bind_tc_ok] at hr
have hremFormula : rem.val = c.val % 10 := by
have htenqFormula : tenq.val = 10 * (c.val / 10) := by
rw [htenqVal, hquotientVal]
norm_num
have heuc := Nat.mod_add_div c.val 10
rw [hremVal.1, htenqFormula]
omega
have hremLt : rem.val < 10 := by
rw [hremFormula]
exact Nat.mod_lt _ (by omega)
have hquotientFormula : quotient.val = c.val / 10 := by
simpa using hquotientVal
have hquotientFit : quotient.val < 10 ^ wi1.val := by
rw [hquotientFormula]
apply (Nat.div_lt_iff_lt_mul (by omega)).2
rw [← pow_succ]
have hwiSucc : wi1.val + 1 = wi.val := by omega
rw [hwiSucc]
exact hcfit
let newOuts := rem.val :: outs
have hnewlen : newOuts.length = q.val - wi1.val := by
simp [newOuts, houtslen]
omega
have hnewdigit : forall z, z ∈ newOuts -> z < 10 := by
intro z hz
simp only [newOuts, List.mem_cons] at hz
rcases hz with rfl | hz
· exact hremLt
· exact houtsdigit z hz
have houtStep : F newOuts = rem.val * 10 ^ outs.length + F outs := by
simpa [newOuts, F] using foldlFrom outs rem.val
have hnumNew : carry.val =
quotient.val * 10 ^ newOuts.length + F newOuts := by
have heuc : c.val = 10 * quotient.val + rem.val := by
rw [hquotientFormula, hremFormula]
have hmoddiv := Nat.mod_add_div c.val 10
omega
have hlenSucc : q.val - wi1.val = outs.length + 1 := by
rw [houtslen]
omega
calc
carry.val = c.val * 10 ^ outs.length + F outs := hnum
_ = (10 * quotient.val + rem.val) * 10 ^ outs.length + F outs := by rw [heuc]
_ = quotient.val * 10 ^ newOuts.length + F newOuts := by
rw [houtStep, hnewlen, hlenSucc, pow_succ]
ring
by_cases hcap : wi1.val < 768
· have hcastRem : (UScalar.cast .U8 rem).val = rem.val := by
simp [UScalar.cast_val_eq, Nat.mod_eq_of_lt (by omega : rem.val < 256)]
have hwibound : wi1.val < ac.val.length := by
have hacLen : ac.val.length = 768 := by simpa using ac.property
rw [hacLen]
exact hcap
obtain ⟨ac1, hac1, hac1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec ac wi1 (UScalar.cast .U8 rem)
(by simpa [Array.length] using hwibound))
have hcapWord : wi1 <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hcap
simp only [hcapWord, if_true] at hr
simp only [lift] at hr
simp only [bind_tc_ok] at hr
rw [hac1] at hr
simp only [bind_tc_ok] at hr
cases hr
simp only [Inv]
refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
hnumNew, ?_, ?_, ?_⟩
· intro j hj hpos
rw [hac1Val]
simp only [Aeneas.Std.Array.set, List.map_set, List.getElem?_set]
cases j with
| zero => simpa [newOuts, hcastRem] using hcap
| succ j =>
have hjold : j < outs.length := by
simp [newOuts] at hj
omega
have hidx : wi1.val + (j + 1) = wi.val + j := by omega
have hne : wi1.val ≠ wi.val + j := by omega
simp [newOuts, hidx, hne]
simpa using harr j hjold (by omega)
· intro j hj houtsideNew
rw [hac1Val]
simp only [Aeneas.Std.Array.set, List.getElem?_set]
have hne : wi1.val ≠ j := by omega
simp [hne]
apply houtside j hj
omega
· constructor
· intro htrue
rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
refine ⟨j + 1, ?_, ?_, ?_⟩
· simp [newOuts]
omega
· omega
· simpa [newOuts] using hjnz
· intro h
apply hsticky.mpr
rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
cases j with
| zero => omega
| succ j =>
refine ⟨j, ?_, ?_, ?_⟩
· simp [newOuts] at hj
omega
· omega
· simpa [newOuts] using hjnz
· have hcapWord : ¬ wi1 <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hcap
simp only [hcapWord, if_false] at hr
by_cases hrem0 : rem.val = 0
· have hremEq : rem = 0#u64 := UScalar.eq_of_val_eq (by simpa using hrem0)
have hnotgt : ¬ rem > 0#u64 := by simp [hremEq]
simp [hnotgt] at hr
subst r
simp only [Inv]
refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
hnumNew, ?_, ?_, ?_⟩
· intro j hj hpos
cases j with
| zero => omega
| succ j =>
have hjold : j < outs.length := by
simp [newOuts] at hj
omega
have hidx : wi1.val + (j + 1) = wi.val + j := by omega
simpa [newOuts, hidx] using harr j hjold (by omega)
· intro j hj houtsideNew
apply houtside j hj
omega
· constructor
· intro htrue
rcases hsticky.mp htrue with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
refine ⟨j + 1, ?_, ?_, ?_⟩
· simp [newOuts]
omega
· omega
· simpa [newOuts] using hjnz
· intro h
apply hsticky.mpr
rcases h with hb | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hb
· right
cases j with
| zero => simp [newOuts, hrem0] at hjnz
| succ j =>
refine ⟨j, ?_, ?_, ?_⟩
· simp [newOuts] at hj
omega
· omega
· simpa [newOuts] using hjnz
· have hgtRem : rem > 0#u64 := by
change 0 < rem.val
omega
simp [hgtRem] at hr
subst r
simp only [Inv]
refine ⟨by omega, hquotientFit, newOuts, hnewlen, hnewdigit,
hnumNew, ?_, ?_, ?_⟩
· intro j hj hpos
cases j with
| zero => omega
| succ j =>
have hjold : j < outs.length := by
simp [newOuts] at hj
omega
have hidx : wi1.val + (j + 1) = wi.val + j := by omega
simpa [newOuts, hidx] using harr j hjold (by omega)
· intro j hj houtsideNew
apply houtside j hj
omega
· constructor
· intro _
right
refine ⟨0, by simp [newOuts], by omega, ?_⟩
simp [newOuts, hrem0]
· exact fun _ => True.intro
· simp only [Inv]
refine ⟨le_rfl, hcarry, [], by simp, ?_, ?_, ?_, ?_, ?_⟩
· intro z hz
simp at hz
· simp [F]
· intro j hj
simp at hj
· intro j hj houtside
trivial
· simp
· simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1, Body]
using hloop
simpa [Post, F] using hp
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Inspect related source (117)
- left_shift · dec2flt/decimal_seq.rs:130 Program Rust
- left_shift_loop0 · dec2flt/decimal_seq.rs:139 Program Rust
- body · dec2flt/decimal_seq.rs:139 Program Rust
More program dependencies (84)
- left_shift_loop1 · dec2flt/decimal_seq.rs:153 Program Rust
- body · dec2flt/decimal_seq.rs:153 Program Rust
- DecimalSeq · dec2flt/decimal_seq.rs:22 Program Rust
- MAX_DIGITS · dec2flt/decimal_seq.rs:64 Program Rust
- trim · dec2flt/decimal_seq.rs:83 Program Rust
- trim_loop · dec2flt/decimal_seq.rs:92 Program Rust
- body · dec2flt/decimal_seq.rs:92 Program Rust
- number_of_digits_decimal_left_shift · dec2flt/decimal_seq.rs:375 Program Rust
- number_of_digits_decimal_left_shift_loop · dec2flt/decimal_seq.rs:441 Program Rust
- body · dec2flt/decimal_seq.rs:441 Program Rust
- parse_long_mantissa · dec2flt/slow.rs:27 Program Rust
- parse_long_mantissa_loop0 · dec2flt/slow.rs:51 Program Rust
- body · dec2flt/slow.rs:51 Program Rust
- parse_long_mantissa_loop1 · dec2flt/slow.rs:69 Program Rust
- body · dec2flt/slow.rs:69 Program Rust
- parse_long_mantissa_loop2 · dec2flt/slow.rs:94 Program Rust
- body · dec2flt/slow.rs:94 Program Rust
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- BiasedFp · dec2flt/common.rs:56 Program Rust
- zero_pow2 · dec2flt/common.rs:66 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 Program Rust
- DECIMAL_POINT_RANGE · dec2flt/decimal_seq.rs:68 Program Rust
- right_shift · dec2flt/decimal_seq.rs:176 Program Rust
- right_shift_loop0 · dec2flt/decimal_seq.rs:181 Program Rust
- body · dec2flt/decimal_seq.rs:181 Program Rust
- right_shift_loop0_loop0 · dec2flt/decimal_seq.rs:191 Program Rust
- body · dec2flt/decimal_seq.rs:191 Program Rust
- right_shift_loop1 · dec2flt/decimal_seq.rs:210 Program Rust
- body · dec2flt/decimal_seq.rs:210 Program Rust
- right_shift_loop2 · dec2flt/decimal_seq.rs:217 Program Rust
- body · dec2flt/decimal_seq.rs:217 Program Rust
- round · dec2flt/decimal_seq.rs:97 Program Rust
- round_loop · dec2flt/decimal_seq.rs:107 Program Rust
- body · dec2flt/decimal_seq.rs:107 Program Rust
- TABLE · dec2flt/decimal_seq.rs:377 Program Rust
- TABLE_POW5 · dec2flt/decimal_seq.rs:386 Program Rust
- parse_decimal_seq · dec2flt/decimal_seq.rs:288 Program Rust
- parse_decimal_seq_loop0 · dec2flt/decimal_seq.rs:296 Program Rust
- body · dec2flt/decimal_seq.rs:296 Program Rust
- parse_decimal_seq_loop1 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- parse_decimal_seq_loop2 · dec2flt/decimal_seq.rs:309 Program Rust
- body · dec2flt/decimal_seq.rs:309 Program Rust
- parse_decimal_seq_loop3 · dec2flt/decimal_seq.rs:313 Program Rust
- body · dec2flt/decimal_seq.rs:313 Program Rust
- parse_decimal_seq_loop4 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- parse_decimal_seq_loop5 · dec2flt/decimal_seq.rs:313 Program Rust
- body · dec2flt/decimal_seq.rs:313 Program Rust
- parse_decimal_seq_loop6 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- parse_decimal_seq_loop7 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- MAX_SHIFT · dec2flt/slow.rs:28 Program Rust
- call · dec2flt/slow.rs:33 Program Rust
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
- read_u64 · dec2flt/common.rs:22 Program Rust
- write_u64 · dec2flt/common.rs:29 Program Rust
- is_8digits · dec2flt/common.rs:44 Program Rust
- default · dec2flt/decimal_seq.rs:34 Program Rust
- MAX_DIGITS_WITHOUT_OVERFLOW · dec2flt/decimal_seq.rs:67 Program Rust
- count_trailing_zeros · dec2flt/decimal_seq.rs:236 Program Rust
- count_trailing_zeros_loop · dec2flt/decimal_seq.rs:240 Program Rust
- body · dec2flt/decimal_seq.rs:240 Program Rust
- final_decimal_point · dec2flt/decimal_seq.rs:269 Program Rust
- parse_digits_into_seq · dec2flt/decimal_seq.rs:257 Program Rust
- parse_digits_into_seq_loop · dec2flt/decimal_seq.rs:259 Program Rust
- body · dec2flt/decimal_seq.rs:259 Program Rust
- parse_scientific · dec2flt/parse.rs:121 Program Rust
- parse_scientific_loop · dec2flt/parse.rs:145 Program Rust
- body · dec2flt/parse.rs:145 Program Rust
- NUM_POWERS · dec2flt/slow.rs:29 Program Rust
- POWERS · dec2flt/slow.rs:30 Program Rust
- closure · dec2flt/slow.rs:33 Program Rust
- try_add_digit · dec2flt/decimal_seq.rs:74 Program Rust
Standard-library dependencies (30)
- clone · core/src/clone.rs:698 Rust library
- div · core/src/ops/arith.rs:515 Rust library
- mul · core/src/ops/arith.rs:350 Rust library
- neg · core/src/ops/arith.rs:719 Rust library
- bitand · core/src/ops/bit.rs:178 Rust library
- bitor · core/src/ops/bit.rs:282 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- shr · core/src/ops/bit.rs:612 Rust library
- EXPONENT_MASK · core/src/num/f64.rs:615 Rust library
- INFINITY · core/src/num/f64.rs:512 Rust library
- MANTISSA_DIGITS · core/src/num/f64.rs:412 Rust library
- MANTISSA_MASK · core/src/num/f64.rs:637 Rust library
- NAN · core/src/num/f64.rs:509 Rust library
- NEG_INFINITY · core/src/num/f64.rs:515 Rust library
- classify · core/src/num/f64.rs:782 Rust library
- from_bits · core/src/num/f64.rs:1269 Rust library
- to_bits · core/src/num/f64.rs:1223 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 Rust library
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- first · core/src/slice/mod.rs:155 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
Related source and models come from the extraction records and declared dependencies.
Proof assumptions and review
Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.
Transitive assumptions
- Classical.choice
- Quot.sound
- propext
This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.
Helper theorems included
- DecimalLeftShiftSecondLoopSemantics
- ResultLoopPartial
- num imp dec2flt decimal seq DecimalSeq
- num imp dec2flt decimal seq DecimalSeq MAX DIGITS
- num imp dec2flt decimal seq DecimalSeq left shift
- num imp dec2flt decimal seq DecimalSeq trim
All 9 entries
- DecimalLeftShiftSecondLoopSemantics
- ResultLoopPartial
- num imp dec2flt decimal seq DecimalSeq
- num imp dec2flt decimal seq DecimalSeq MAX DIGITS
- num imp dec2flt decimal seq DecimalSeq left shift
- num imp dec2flt decimal seq DecimalSeq trim
- num imp dec2flt decimal seq number of digits decimal left shift
- num imp dec2flt decimal seq number of digits decimal left shift TABLE
- num imp dec2flt decimal seq number of digits decimal left shift TABLE POW5
Technical checking records
Local closure summary
{
"node": "DecimalLeftShiftSecondLoopSemantics",
"closure_version": "patch_c_v1",
"active_decl_hash": "3489afd071ea72e1b9c73468c58d87dfcb3a85d3aa27932fb7e43f896cc8dd7a",
"active_statement_hash": "85a1cbb42095415b0c0341aab50b16b892c6a123f810cb77df4fd06fb5c189b0",
"kernel_axioms": [
"Classical.choice",
"Quot.sound",
"propext"
],
"toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
"lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
"checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
"approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
"boundary_theorems": {},
"strict_theorem_deps": {
"ResultLoopPartial": "566d2aeb76ed9100",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
"num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "0eb6fb2627d0ff5c"
},
"strict_definition_deps": {
"ResultLoopPartial": "1b58e96ad00b23c2",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
"num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "4555009895b002bf"
}
}Prose assessment
{
"label": "Lean proof closed; predictive prose review no longer required",
"assessment_status": null,
"fingerprint_relation": "unknown",
"assessment_fingerprint": null,
"current_fingerprint": "sound-v2:565f8bc8321f317f90e996fd82e4c6c0a6fbc27baf3195ffff48426f6175ae07",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"substantiveness_status": "Pass"
}