Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Normal left shift on the loop-one domain
Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.DecimalLeftShiftDigitGrowth
import Tablet.DecimalLeftShiftFirstLoopSemantics
import Tablet.DecimalLeftShiftSecondLoopSemantics
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalSeqTrimSemantics
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: DecimalLeftShiftLoop1Execution]
theorem DecimalLeftShiftLoop1Execution :
forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize),
shift.val <= 60 ->
-324 <= d.decimal_point.val -> d.decimal_point.val <= 0 ->
d.num_digits.val <= 768 /\
(forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
(exists z zs,
(d.digits.val.take d.num_digits.val).map (fun a => a.val) = z :: zs /\
1 <= z /\ z < 10 /\ (z :: zs).length = d.num_digits.val) ->
exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (q : Nat),
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
q <= 19 /\
d'.decimal_point.val = d.decimal_point.val + (q : Int) /\
-324 <= d'.decimal_point.val /\ d'.decimal_point.val <= 19 /\
d'.num_digits.val != 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
Lean proof
intro d shift hshift hpointLo hpointHi hinput
have loop0Total :
forall (b : Bool) (a : Array Std.U8 768#usize)
(read write : Std.Usize) (carry : Std.U64),
read.val <= write.val ->
read.val <= 768 ->
(forall j z, j < read.val -> a.val[j]? = some z -> z.val < 10) ->
carry.val < 2 ^ shift.val ->
exists (b' : Bool) (a' : Array Std.U8 768#usize)
(write' : Std.Usize) (carry' : Std.U64),
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
b a shift read write carry = ok (b', a', write', carry') := by
let B := 2 ^ shift.val
have hBle : B <= 2 ^ 60 :=
Nat.pow_le_pow_right (by omega) hshift
intro b a read
generalize hn : read.val = n
induction n using Nat.strong_induction_on generalizing read b a with
| h n ih =>
intro write carry hreadwrite hreadcap hdigit hcarry
by_cases hread0 : read.val = 0
· have hreadEq : read = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hread0)
subst read
refine ⟨b, a, write, carry, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
· have hreadpos : 0 < read.val := by omega
have hreadne : read != 0#usize := by
simp only [bne_iff_ne]
intro h
apply hread0
simpa [h]
obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := read) (y := 1#usize) (by simpa using hreadpos))
norm_num at hread1Val
have hwritepos : 0 < write.val := by omega
obtain ⟨write1, hwrite1, hwrite1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := write) (y := 1#usize) (by simpa using hwritepos))
norm_num at hwrite1Val
have hread1lt : read1.val < a.val.length := by
rw [a.property]
norm_num
omega
obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec a read1
(by simpa [Array.length] using hread1lt))
have hdigitGet : a.val[read1.val]? = some digit :=
getElem?_eq_some_iff.mpr ⟨hread1lt, hdigitReadVal.symm⟩
have hdigitLt : digit.val < 10 :=
hdigit read1.val digit (by omega) hdigitGet
let digit64 : Std.U64 := UScalar.cast .U64 digit
have hdigit64 : digit64.val = digit.val := by
simp [digit64, UScalar.cast_val_eq]
have hshiftSpec := UScalar.ShiftLeft_spec digit64 shift (2 ^ 64)
(by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq])
cases hshiftEq : digit64 <<< shift with
| fail e => simp [hshiftEq] at hshiftSpec
| div => simp [hshiftEq] at hshiftSpec
| ok shifted =>
rw [hshiftEq] at hshiftSpec
have hshiftVal : shifted.val = digit.val * B := by
have hmulLt : digit.val * B < 2 ^ 64 := by
calc
digit.val * B <= 9 * 2 ^ 60 := Nat.mul_le_mul (by omega) hBle
_ < 2 ^ 64 := by norm_num
have hs := hshiftSpec.1
simp only [WP.spec_ok] at hs
rw [Nat.shiftLeft_eq, hdigit64, Nat.mod_eq_of_lt (by
simpa [B] using hmulLt)] at hs
simpa [B] using hs
have haddBound : carry.val + shifted.val <= U64.max := by
rw [hshiftVal]
have hdigitMul : digit.val * B <= 9 * B :=
Nat.mul_le_mul_right B (by omega)
have : carry.val + digit.val * B < 2 ^ 64 := by
calc
carry.val + digit.val * B < B + 9 * B := by omega
_ = 10 * B := by ring
_ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
_ < 2 ^ 64 := by norm_num
norm_num [U64.max, U64.numBits]
omega
obtain ⟨total, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := carry) (y := shifted) haddBound)
obtain ⟨quotient, hquotient, hquotientVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.div_spec total (y := 10#u64) (by norm_num))
have hmulBound : 10 * quotient.val <= U64.max := by
rw [hquotientVal]
have htotalLt : total.val < 2 ^ 64 := total.bv.isLt
have hqle : 10 * (total.val / 10) <= total.val :=
Nat.mul_div_le total.val 10
norm_num [U64.max, U64.numBits] at htotalLt ⊢
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))
have htenqLe : tenq.val <= total.val := by
rw [htenqVal, hquotientVal]
exact Nat.mul_div_le total.val 10
obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := total) (y := tenq) htenqLe)
have htotalFormula : total.val = carry.val + digit.val * B := by
rw [htotalVal, hshiftVal]
have hquotientFormula : quotient.val = total.val / 10 := hquotientVal
have hremFormula : rem.val = total.val % 10 := by
have htenqFormula : tenq.val = 10 * (total.val / 10) := by
rw [htenqVal, hquotientVal]
norm_num
have heuc := Nat.mod_add_div total.val 10
rw [hremVal.1, htenqFormula]
omega
have hremLt : rem.val < 10 := by
rw [hremFormula]
exact Nat.mod_lt _ (by omega)
have hquotientB : quotient.val < B := by
rw [hquotientFormula]
apply (Nat.div_lt_iff_lt_mul (by omega)).2
rw [htotalFormula]
have hdigitMul : digit.val * B <= 9 * B :=
Nat.mul_le_mul_right B (by omega)
omega
by_cases hcap : write1.val < 768
· have hwibound : write1.val < a.val.length := by
rw [a.property]
norm_num
exact hcap
obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec a write1 (UScalar.cast .U8 rem)
(by simpa [Array.length] using hwibound))
have hreadwrite1 : read1.val <= write1.val := by omega
have hcapWord : write1 <
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
have hdigit1 : forall j z, j < read1.val ->
a1.val[j]? = some z -> z.val < 10 := by
intro j z hj hz
rw [ha1Val] at hz
simp only [Aeneas.Std.Array.set, List.getElem?_set] at hz
have hne : write1.val ≠ j := by omega
simp [hne] at hz
exact hdigit j z (by omega) hz
obtain ⟨b', a', write', carry', hrest⟩ :=
ih read1.val (by omega) b a1 read1 (by rfl)
write1 quotient (by omega) (by omega) hdigit1 hquotientB
refine ⟨b', a', write', carry', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
hdigitRead, lift]
rw [show UScalar.cast .U64 digit = digit64 by rfl]
simp only [
hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
hcapWord, ha1]
exact hrest
· have hcapWord : ¬ write1 <
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
have hdigit1 : forall j z, j < read1.val ->
a.val[j]? = some z -> z.val < 10 := by
intro j z hj hz
exact hdigit j z (by omega) hz
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]
obtain ⟨b', a', write', carry', hrest⟩ :=
ih read1.val (by omega) b a read1 (by rfl)
write1 quotient (by omega) (by omega) hdigit1 hquotientB
refine ⟨b', a', write', carry', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
hdigitRead, lift]
rw [show UScalar.cast .U64 digit = digit64 by rfl]
simp only [
hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
hcapWord, if_false, hnotgt]
exact hrest
· have hgt : rem > 0#u64 := by
change 0 < rem.val
omega
obtain ⟨b', a', write', carry', hrest⟩ :=
ih read1.val (by omega) true a read1 (by rfl)
write1 quotient (by omega) (by omega) hdigit1 hquotientB
refine ⟨b', a', write', carry', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
hdigitRead, lift]
rw [show UScalar.cast .U64 digit = digit64 by rfl]
simp only [
hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
hcapWord, if_false, hgt]
exact hrest
have loop1Total :
forall (b : Bool) (a : Array Std.U8 768#usize)
(write : Std.Usize) (carry : Std.U64),
carry.val < 10 ^ write.val ->
exists (b' : Bool) (a' : Array Std.U8 768#usize),
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b a write carry = ok (b', a') := by
intro b a write
generalize hn : write.val = n
induction n using Nat.strong_induction_on generalizing write b a with
| h n ih =>
intro carry hcarry
have hcarryWrite : carry.val < 10 ^ write.val := by
rw [hn]
exact hcarry
by_cases hcarry0 : carry.val = 0
· have hcarryEq : carry = 0#u64 :=
UScalar.eq_of_val_eq (by simpa using hcarry0)
subst carry
refine ⟨b, a, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body]
· have hcarrypos : 0 < carry.val := by omega
have hgt : carry > 0#u64 := by
change 0 < carry.val
exact hcarrypos
have hwritepos : 0 < write.val := by
by_contra hnpos
have hwrite0 : write.val = 0 := by omega
rw [hwrite0] at hcarryWrite
simp at hcarryWrite
omega
obtain ⟨write1, hwrite1, hwrite1Val⟩ :=
Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := write) (y := 1#usize)
(by simpa using hwritepos))
norm_num at hwrite1Val
obtain ⟨quotient, hquotient, hquotientVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.div_spec carry (y := 10#u64) (by norm_num))
have hmulBound : 10 * quotient.val <= U64.max := by
rw [hquotientVal]
have hcarryLt : carry.val < 2 ^ 64 := carry.bv.isLt
have hqle : 10 * (carry.val / 10) <= carry.val :=
Nat.mul_div_le carry.val 10
norm_num [U64.max, U64.numBits] at hcarryLt ⊢
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))
have htenqLe : tenq.val <= carry.val := by
rw [htenqVal, hquotientVal]
exact Nat.mul_div_le carry.val 10
obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := carry) (y := tenq) htenqLe)
have hremFormula : rem.val = carry.val % 10 := by
have htenqFormula : tenq.val = 10 * (carry.val / 10) := by
rw [htenqVal, hquotientVal]
norm_num
have heuc := Nat.mod_add_div carry.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 = carry.val / 10 := by
simpa using hquotientVal
have hquotientFit : quotient.val < 10 ^ write1.val := by
rw [hquotientFormula]
apply (Nat.div_lt_iff_lt_mul (by omega)).2
rw [← pow_succ]
have hwriteSucc : write1.val + 1 = write.val := by omega
rw [hwriteSucc]
exact hcarryWrite
by_cases hcap : write1.val < 768
· have hwibound : write1.val < a.val.length := by
rw [a.property]
norm_num
exact hcap
obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec a write1 (UScalar.cast .U8 rem)
(by simpa [Array.length] using hwibound))
have hcapWord : write1 <
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
obtain ⟨b', a', hrest⟩ :=
ih write1.val (by omega) b a1 write1 (by rfl)
quotient hquotientFit
refine ⟨b', a', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
hcapWord, lift, ha1]
exact hrest
· have hcapWord : ¬ write1 <
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
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]
obtain ⟨b', a', hrest⟩ :=
ih write1.val (by omega) b a write1 (by rfl)
quotient hquotientFit
refine ⟨b', a', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
hcapWord, if_false, hnotgt]
exact hrest
· have hgtRem : rem > 0#u64 := by
change 0 < rem.val
omega
obtain ⟨b', a', hrest⟩ :=
ih write1.val (by omega) true a write1 (by rfl)
quotient hquotientFit
refine ⟨b', a', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
hcapWord, if_false, hgtRem]
exact hrest
have zeroGrowthTotal (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
(sh : Std.Usize) (hsh : sh.val = 0) :
exists gr : Std.Usize,
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d0 sh = ok gr /\ gr.val = 0 := by
have hseq : sh = 0#usize := UScalar.eq_of_val_eq (by simpa using hsh)
subst sh
let kfin : Fin 61 := ⟨0, by omega⟩
have cert := DecimalLeftShiftTableCertificate kfin
dsimp only at cert
rcases cert with ⟨hloidx, hhiidx, hlenTable, _, hzeroTable, _⟩
have hz := hzeroTable (by simp [kfin])
rcases hz with ⟨hhiZero, hdsZero⟩
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let pow5Table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
let x := table.val.get ⟨0, by simp [table]⟩
let y := table.val.get ⟨1, by simp [table]⟩
let hi := x.val / 2048
let lo := x.val % 2048
let hiNext := y.val % 2048
let dsU := pow5Table.val.extract lo hiNext
let ds := dsU.map (fun q => q.val)
have hloidx' : lo <= hiNext := by
simpa [lo, hiNext, x, y, table, kfin] using hloidx
have hhiidx' : hiNext <= 1308 := by
simpa [hiNext, y, table, kfin] using hhiidx
have hlen' : ds.length = hiNext - lo := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hlenTable
have hhiZero' : hi = 0 := by
simpa [hi, x, table, kfin] using hhiZero
have hdsZero' : ds = [] := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hdsZero
have hloEq : lo = hiNext := by
rw [hdsZero'] at hlen'
simp at hlen'
omega
have hmask : (0#usize &&& 63#usize) = 0#usize := by decide
obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
0#usize (by simp [Array.length]))
have hxaEq : xa = x := by
apply UScalar.eq_of_val_eq
simpa [x, table] using congrArg UScalar.val hxaVal
subst xa
obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
norm_num at hnextVal
obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
next (by simp [Array.length]; omega))
have hybEq : yb = y := by
apply UScalar.eq_of_val_eq
simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
subst yb
obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
have hhigh16Eq : high16.val = hi := by
rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
norm_num [hi, Int.toNat]
let numNew : Std.Usize := UScalar.cast .Usize high16
have hnumNewVal : numNew.val = 0 := by
simp [numNew, hhigh16Eq, hhiZero']
let low16 := 2047#u16 &&& x
let pow5a : Std.Usize := UScalar.cast .Usize low16
have hpow5aVal : pow5a.val = lo := by
simp [pow5a, low16, lo, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
let next16 := 2047#u16 &&& y
let pow5b : Std.Usize := UScalar.cast .Usize next16
have hpow5bVal : pow5b.val = hiNext := by
simp [pow5b, next16, hiNext, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
have hlo1308 : pow5a.val <= 1308 := by
rw [hpow5aVal]
exact hloidx'.trans hhiidx'
let slice : Slice Std.U8 := pow5Table.to_slice.drop pow5a
have hslice :
core.array.Array.index
(core.ops.index.IndexSlice
(core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
pow5Table { start := pow5a } = ok slice := by
rw [Array.index_SliceIndexRangeFromUsizeSlice]
unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
simp [hlo1308, slice]
have hsubBound : pow5a.val <= pow5b.val := by
rw [hpow5aVal, hpow5bVal]
exact hloidx'
obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
have hspanZero : span.val = 0 := by
rw [hspanVal.1, hpow5aVal, hpow5bVal, ← hloEq]
simp
have hspanEq : span = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hspanZero)
subst span
refine ⟨numNew, ?_, hnumNewVal⟩
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
simp only [hmask, lift, Bind.bind, Aeneas.Std.bind, hxa, bind_tc_ok,
hnext, hyb, hhigh16]
rw [hxaEq, hybEq]
simp only [hhigh16, bind_tc_ok]
rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
rw [show UScalar.cast .Usize high16 = numNew by rfl]
rw [hslice]
simp only [bind_tc_ok]
unfold core.slice.Slice.iter
simp only [bind_tc_ok]
unfold core.slice.iter.IteratorSliceIter.enumerate
simp only [bind_tc_ok, hspan]
unfold core.iter.adapters.enumerate.IteratorEnumerate.take
simp only [bind_tc_ok]
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop
rw [Aeneas.Std.loop.eq_def]
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body
unfold core.iter.adapters.take.IteratorTake.next
simp
have trimLoopTotal :
forall (a : Array Std.U8 768#usize) (n : Std.Usize),
n.val <= a.val.length ->
exists j : Std.Usize,
num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop n a = ok j := by
intro a n
generalize hn : n.val = k
induction k using Nat.strong_induction_on generalizing n with
| h k ih =>
intro hbound
by_cases hn0 : n.val = 0
· have hnEq : n = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hn0)
subst n
refine ⟨0#usize, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
rw [Aeneas.Std.loop.eq_def]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body]
· have hnpos : 0 < n.val := by omega
have hnne : n != 0#usize := by
simp only [bne_iff_ne]
intro heq
apply hn0
simpa [heq]
obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := n) (y := 1#usize) (by simpa using hnpos))
norm_num at hn1Val
have hn1lt : n1.val < a.val.length := by omega
obtain ⟨digit, hdigit, hdigitVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec a n1
(by simpa [Array.length] using hn1lt))
by_cases hzero : digit = 0#u8
· obtain ⟨j, hrest⟩ :=
ih n1.val (by omega) n1 (by rfl) (by omega)
refine ⟨j, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
rw [Aeneas.Std.loop.eq_def]
simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_true]
exact hrest
· refine ⟨n, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
rw [Aeneas.Std.loop.eq_def]
simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_false]
simp
have trimTotal
(e : num.imp.dec2flt.decimal_seq.DecimalSeq)
(hbound : e.num_digits.val <= e.digits.val.length) :
exists e' : num.imp.dec2flt.decimal_seq.DecimalSeq,
num.imp.dec2flt.decimal_seq.DecimalSeq.trim e = ok e' := by
have hle : e.num_digits <=
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [UScalar.le_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
e.digits.property] using hbound
obtain ⟨j, hj⟩ := trimLoopTotal e.digits e.num_digits hbound
refine ⟨{ e with num_digits := j }, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim
simp only [hle, massert_True, bind_tc_ok, hj]
rcases hinput with ⟨hcap, hdigit, z, zs, hdigits, hzlo, hzhi, hlen⟩
let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
let I := F xs
let B := 2 ^ shift.val
have hxdigit : forall q, q ∈ xs -> q < 10 := by
intro q hq
simp only [xs, List.mem_map] at hq
rcases hq with ⟨a, ha, rfl⟩
exact hdigit a ha
have hxslen : xs.length = d.num_digits.val := by
rw [show xs = z :: zs by simpa [xs] using hdigits]
exact hlen
have hnpos : 0 < d.num_digits.val := by
rw [← hxslen]
simpa [xs, hdigits]
have hnne : d.num_digits ≠ 0#usize := by
intro heq
have : d.num_digits.val = 0 := by simpa [heq]
omega
have foldlFrom (ys : List Nat) (acc : Nat) :
ys.foldl (fun n q => 10 * n + q) acc =
acc * 10 ^ ys.length + F ys := by
induction ys generalizing acc with
| nil => simp [F]
| cons q qs ih =>
simp only [List.foldl_cons, List.length_cons]
rw [ih, pow_succ]
change (10 * acc + q) * 10 ^ qs.length + F qs =
acc * (10 ^ qs.length * 10) +
(q :: qs).foldl (fun n z => 10 * n + z) 0
simp only [List.foldl_cons]
rw [ih]
ring
have foldBound (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10) :
F ys < 10 ^ ys.length := by
induction ys with
| nil => simp [F]
| cons q qs ih =>
have hq : q < 10 := hy q (by simp)
have hqs : forall a, a ∈ qs -> a < 10 := by
intro a ha
exact hy a (by simp [ha])
have hi := ih hqs
rw [show F (q :: qs) = q * 10 ^ qs.length + F qs by
simpa [F] using foldlFrom qs q, List.length_cons, pow_succ]
have hm : q * 10 ^ qs.length <= 9 * 10 ^ qs.length :=
Nat.mul_le_mul_right _ (by omega)
calc
q * 10 ^ qs.length + F qs <
q * 10 ^ qs.length + 10 ^ qs.length := Nat.add_lt_add_left hi _
_ <= 9 * 10 ^ qs.length + 10 ^ qs.length := Nat.add_le_add_right hm _
_ = 10 ^ qs.length * 10 := by ring
have hIbound : I < 10 ^ d.num_digits.val := by
simpa [hxslen] using foldBound xs hxdigit
have hIlower : 10 ^ (d.num_digits.val - 1) <= I := by
have hIform : I = z * 10 ^ zs.length + F zs := by
simp only [I, xs, hdigits, List.foldl_cons]
simpa [F] using foldlFrom zs z
have hnlen : d.num_digits.val = zs.length + 1 := by
rw [← hxslen]
simp [xs, hdigits]
rw [hnlen]
simp only [Nat.add_sub_cancel]
rw [hIform]
have hm := Nat.mul_le_mul_right (10 ^ zs.length) hzlo
omega
have decimalUpper (c w t n : Nat) (hc : c < t) (hw : w < 10 ^ n) :
c * 10 ^ n + w < t * 10 ^ n := by
have hc1 : c + 1 <= t := by omega
have hm := Nat.mul_le_mul_right (10 ^ n) hc1
nlinarith
obtain ⟨growth, hgrowth, hgrowthLe, hproductLo, hproductHi⟩ :
exists growth : Std.Usize,
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d shift = ok growth /\
growth.val <= 19 /\
10 ^ (d.num_digits.val + growth.val - 1) <= B * I /\
B * I < 10 ^ (d.num_digits.val + growth.val) := by
by_cases hk0 : shift.val = 0
· obtain ⟨growth, hgrowth, hgrowthZero⟩ :=
zeroGrowthTotal d shift hk0
refine ⟨growth, hgrowth, by omega, ?_, ?_⟩
· simpa [B, hk0, hgrowthZero] using hIlower
· simpa [B, hk0, hgrowthZero] using hIbound
· have hkpos : 0 < shift.val := by omega
let kfin : Fin 61 := ⟨shift.val, by omega⟩
have cert := DecimalLeftShiftTableCertificate kfin
dsimp only at cert
rcases cert with ⟨_, _, _, _, _, hpositiveTable⟩
have hp := hpositiveTable (by simpa [kfin] using hkpos)
rcases hp with ⟨_, _, _, _, hlo, hhi⟩
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let tableEntry := table.val.get ⟨shift.val, by
have htlen : table.val.length = 65 := by simp [table]
omega⟩
let h := tableEntry.val / 2048
have hlo' : 10 ^ (h - 1) < B := by
simpa [h, tableEntry, table, kfin, B] using hlo
have hhi' : B < 10 ^ h := by
simpa [h, tableEntry, table, kfin, B] using hhi
have hhpos : 0 < h := by
by_contra hz
have : h = 0 := by omega
have hBpos : 0 < B := by simp [B]
rw [this] at hhi'
norm_num at hhi'
omega
have hhle : h <= 19 := by
by_contra hn
have hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) (by omega)
have hBle : B <= 2 ^ 60 :=
Nat.pow_le_pow_right (by omega) hshift
have hnum : 2 ^ 60 < 10 ^ 19 := by norm_num
exact (not_lt_of_ge hp19)
(lt_of_lt_of_le hlo' (le_trans hBle (Nat.le_of_lt hnum)))
obtain ⟨growth, hgrowth, hgrowthVal⟩ :=
DecimalLeftShiftDigitGrowth d shift h hshift hlo' hhi' hcap hdigit
have hgrowthFormula : growth.val =
if 10 ^ (d.num_digits.val + h - 1) <= I * B
then h else h - 1 := by
simpa [xs, I, F, B] using hgrowthVal
have hgrowthLe : growth.val <= 19 := by
rw [hgrowthFormula]
split <;> omega
refine ⟨growth, hgrowth, hgrowthLe, ?_, ?_⟩
· rw [hgrowthFormula]
split
next ht => simpa [Nat.mul_comm] using ht
next ht =>
have hmul1 : 10 ^ (h - 1) * 10 ^ (d.num_digits.val - 1) <
B * 10 ^ (d.num_digits.val - 1) :=
(Nat.mul_lt_mul_right (by positivity)).mpr hlo'
have hmul2 : B * 10 ^ (d.num_digits.val - 1) <= B * I :=
Nat.mul_le_mul_left B hIlower
have he : d.num_digits.val + (h - 1) - 1 =
(h - 1) + (d.num_digits.val - 1) := by omega
rw [he, Nat.pow_add]
exact Nat.le_of_lt (lt_of_lt_of_le hmul1 hmul2)
· rw [hgrowthFormula]
split
next ht =>
have hBpos : 0 < B := by simp [B]
have hpowpos : 0 < 10 ^ d.num_digits.val := by positivity
have hmul1 : B * I < B * 10 ^ d.num_digits.val :=
(Nat.mul_lt_mul_left hBpos).mpr hIbound
have hmul2 : B * 10 ^ d.num_digits.val <
10 ^ h * 10 ^ d.num_digits.val :=
(Nat.mul_lt_mul_right hpowpos).mpr hhi'
have hm := lt_trans hmul1 hmul2
rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hm
simpa using hm
next ht =>
have hlt : I * B <
10 ^ (d.num_digits.val + h - 1) := by omega
rw [Nat.mul_comm]
simpa only [show d.num_digits.val + h - 1 =
d.num_digits.val + (h - 1) by omega] using hlt
have haddBound : d.num_digits.val + growth.val <= UScalar.max .Usize := by
scalar_tac
obtain ⟨width, hwidth, hwidthVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := d.num_digits) (y := growth) haddBound)
have hcursorInput : d.num_digits.val <= width.val := by
rw [hwidthVal]
omega
have hdigitIndexed : forall j u, j < d.num_digits.val ->
d.digits.val[j]? = some u -> u.val < 10 := by
intro j u hj hu
apply hdigit u
rw [List.mem_take_iff_getElem]
rcases getElem?_eq_some_iff.mp hu with ⟨hjlen, hju⟩
exact ⟨j, by simp; omega, hju⟩
obtain ⟨b0, a0, cursor, carry, hloop0⟩ :=
loop0Total d.truncated d.digits d.num_digits
width 0#u64 hcursorInput hcap hdigitIndexed (by simp [B])
obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryBase,
hproduct, hlowArray, houtside0, hsticky0⟩ :=
DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
shift d.num_digits width cursor carry hshift hcap hcursorInput
hdigit hloop0
have hcursorVal : cursor.val = growth.val := by
rw [hwidthVal] at hcursorEq
omega
have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
simpa [hlowLen] using foldBound lowDigits hlowDigit
have hproduct' :
B * I = carry.val * 10 ^ d.num_digits.val + F lowDigits := by
simpa [B, I, F, xs, List.map_take] using hproduct
have hcarryWidth : carry.val < 10 ^ cursor.val := by
rw [hcursorVal]
have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
rw [hproduct']
omega
have hpow : 10 ^ growth.val * 10 ^ d.num_digits.val =
10 ^ (d.num_digits.val + growth.val) := by
rw [Nat.mul_comm, ← Nat.pow_add]
have hm : carry.val * 10 ^ d.num_digits.val <
10 ^ growth.val * 10 ^ d.num_digits.val := by
rw [hpow]
exact lt_of_le_of_lt hcle hproductHi
exact Nat.lt_of_mul_lt_mul_right hm
have hcarryLower : 0 < growth.val ->
10 ^ (growth.val - 1) <= carry.val := by
intro hgpos
by_contra hnot
have hcsmall : carry.val < 10 ^ (growth.val - 1) := by omega
have hu := decimalUpper carry.val (F lowDigits)
(10 ^ (growth.val - 1)) d.num_digits.val hcsmall hlowBound
rw [← hproduct'] at hu
rw [Nat.mul_comm (10 ^ (growth.val - 1)), ← Nat.pow_add] at hu
have he : d.num_digits.val + (growth.val - 1) =
d.num_digits.val + growth.val - 1 := by omega
rw [he] at hu
exact (not_lt_of_ge hproductLo) hu
have hpad : forall j, j < cursor.val -> j < 768 ->
carry.val < 10 ^ (cursor.val - j - 1) ->
a0.val[j]? = some 0#u8 := by
intro j hjcursor _ hjcarry
have hgpos : 0 < growth.val := by omega
have hlower := hcarryLower hgpos
have hexp : cursor.val - j - 1 <= growth.val - 1 := by
rw [hcursorVal]
omega
have hpow :
10 ^ (cursor.val - j - 1) <= 10 ^ (growth.val - 1) :=
Nat.pow_le_pow_right (by omega) hexp
omega
obtain ⟨b1, a1, hloop1⟩ :=
loop1Total b0 a0 cursor carry hcarryWidth
obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
hhighArray, houtside1, hsticky1⟩ :=
DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
hcarryWidth hpad hloop1
have headPositive (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10)
(hyspos : 0 < ys.length)
(hlower : 10 ^ (ys.length - 1) <= F ys) :
exists q qs, ys = q :: qs /\ 0 < q := by
cases hys : ys with
| nil =>
rw [hys] at hyspos
simp at hyspos
| cons q qs =>
refine ⟨q, qs, rfl, ?_⟩
by_contra hq
have hqzero : q = 0 := by omega
have htailDigit : forall a, a ∈ qs -> a < 10 := by
intro a ha
apply hy a
rw [hys]
simp [ha]
have htailBound := foldBound qs htailDigit
have htailLower : 10 ^ qs.length <= F qs := by
simpa [hys, F, hqzero] using hlower
omega
have hcellZero : a1.val[0]? != some 0#u8 := by
simp only [bne_iff_ne]
by_cases hgzero : growth.val = 0
· have hcursorZero : cursor.val = 0 := by rw [hcursorVal, hgzero]
have hcarryZero : carry.val = 0 := by
rw [hcursorZero] at hcarryWidth
simp at hcarryWidth
omega
have hlowLower : 10 ^ (lowDigits.length - 1) <= F lowDigits := by
rw [hlowLen]
rw [hgzero, Nat.add_zero] at hproductLo
rw [hcarryZero] at hproduct'
simp only [zero_mul, zero_add] at hproduct'
rw [hproduct'] at hproductLo
exact hproductLo
obtain ⟨q, qs, hlowEq, hqpos⟩ :=
headPositive lowDigits hlowDigit (by rw [hlowLen]; exact hnpos) hlowLower
have hlowCell := hlowArray 0 (by rw [hlowEq]; simp) (by
rw [hcursorZero]
omega)
have hout := houtside1 0 (by omega) (by omega)
have hcell :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
rw [List.getElem?_map, hout]
simpa [hcursorZero, hlowEq] using hlowCell
intro hz
have hzmap :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
rw [List.getElem?_map, hz]
rfl
rw [hcell] at hzmap
have : q = 0 := Option.some.inj hzmap
omega
· have hgpos : 0 < growth.val := by omega
have hhighLower :
10 ^ (highDigits.length - 1) <= F highDigits := by
rw [hhighLen, hcursorVal]
simpa [F, hcarryDigits] using hcarryLower hgpos
obtain ⟨q, qs, hhighEq, hqpos⟩ :=
headPositive highDigits hhighDigit
(by rw [hhighLen, hcursorVal]; exact hgpos) hhighLower
have hcell := hhighArray 0 (by rw [hhighEq]; simp) (by omega)
have hcell' :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
simpa [hhighEq] using hcell
intro hz
have hzmap :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
rw [List.getElem?_map, hz]
rfl
rw [hcell'] at hzmap
have : q = 0 := Option.some.inj hzmap
omega
let capped : Std.Usize :=
if width.val > 768 then
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else width
have hcappedVal : capped.val = min width.val 768 := by
simp only [capped]
split
· simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
omega
· simp
omega
have hcapEval :
(if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
else ok width) = ok capped := by
simp only [capped]
by_cases hc : width.val > 768
· have hc' : width >
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [gt_iff_lt, UScalar.lt_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hc
simp [hc', hc]
· have hc' : ¬ width >
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [gt_iff_lt, UScalar.lt_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hc
simp [hc', hc]
have hcappedPos : 0 < capped.val := by
rw [hcappedVal, hwidthVal]
simp only [lt_min_iff]
omega
let growthI : Std.I32 := UScalar.hcast .I32 growth
have hgrowthIVal : growthI.val = (growth.val : Int) := by
have hs := UScalar.hcast_inBounds_spec .I32 growth (by
rw [IScalar.max_I32_eq]
omega)
simp only [lift, WP.spec_ok] at hs
exact hs
have hpointBounds :
IScalar.min .I32 <= d.decimal_point.val + growthI.val /\
d.decimal_point.val + growthI.val <= IScalar.max .I32 := by
rw [hgrowthIVal]
constructor
· rw [IScalar.min_I32_eq]
omega
· rw [IScalar.max_I32_eq]
omega
obtain ⟨point, hpoint, hpointVal⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hpointBounds.1 hpointBounds.2)
have hpointVal' : point.val =
d.decimal_point.val + (growth.val : Int) := by
rw [hpointVal, hgrowthIVal]
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := capped, decimal_point := point,
truncated := b1, digits := a1 }
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, hcappedVal, a1.property]
obtain ⟨d', htrim⟩ := trimTotal pre hpreBound
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalue⟩
have hnumNonzero : d'.num_digits.val != 0 := by
simp only [bne_iff_ne]
intro hzero
have hz := hzeros 0 (by omega) (by simpa [pre] using hcappedPos)
have hz' : a1.val[0]? = some 0#u8 := by
simpa [pre] using hz
have hcellZero' : a1.val[0]? ≠ some 0#u8 := by
simpa only [bne_iff_ne] using hcellZero
exact hcellZero' hz'
have hcall :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift
simp only [hnne, if_false, hgrowth, bind_tc_ok, hwidth, hloop0]
change (do
let (bc, ac) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b0 a0 cursor carry
let count ←
if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
else ok width
let delta ← lift (UScalar.hcast .I32 growth)
let point' ← d.decimal_point + delta
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := count, decimal_point := point',
truncated := bc, digits := ac }) = ok d'
rw [hloop1]
simp only [bind_tc_ok]
rw [hcapEval]
simp only [bind_tc_ok, lift]
rw [show UScalar.hcast .I32 growth = growthI by rfl]
simp only [bind_tc_ok, hpoint]
simpa [pre] using htrim
have hpointOut : d'.decimal_point.val =
d.decimal_point.val + (growth.val : Int) := by
rw [hpointEq]
simpa [pre] using hpointVal'
refine ⟨d', growth.val, hcall, hgrowthLe, hpointOut, ?_, ?_,
hnumNonzero⟩
· rw [hpointOut]
omega
· rw [hpointOut]
omega
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalLeftShiftDigitGrowth
import Tablet.DecimalLeftShiftFirstLoopSemantics
import Tablet.DecimalLeftShiftSecondLoopSemantics
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalSeqTrimSemantics
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: DecimalLeftShiftLoop1Execution]
theorem DecimalLeftShiftLoop1Execution :
forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize),
shift.val <= 60 ->
-324 <= d.decimal_point.val -> d.decimal_point.val <= 0 ->
d.num_digits.val <= 768 /\
(forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
(exists z zs,
(d.digits.val.take d.num_digits.val).map (fun a => a.val) = z :: zs /\
1 <= z /\ z < 10 /\ (z :: zs).length = d.num_digits.val) ->
exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (q : Nat),
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
q <= 19 /\
d'.decimal_point.val = d.decimal_point.val + (q : Int) /\
-324 <= d'.decimal_point.val /\ d'.decimal_point.val <= 19 /\
d'.num_digits.val != 0 := by
-- BODY
intro d shift hshift hpointLo hpointHi hinput
have loop0Total :
forall (b : Bool) (a : Array Std.U8 768#usize)
(read write : Std.Usize) (carry : Std.U64),
read.val <= write.val ->
read.val <= 768 ->
(forall j z, j < read.val -> a.val[j]? = some z -> z.val < 10) ->
carry.val < 2 ^ shift.val ->
exists (b' : Bool) (a' : Array Std.U8 768#usize)
(write' : Std.Usize) (carry' : Std.U64),
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
b a shift read write carry = ok (b', a', write', carry') := by
let B := 2 ^ shift.val
have hBle : B <= 2 ^ 60 :=
Nat.pow_le_pow_right (by omega) hshift
intro b a read
generalize hn : read.val = n
induction n using Nat.strong_induction_on generalizing read b a with
| h n ih =>
intro write carry hreadwrite hreadcap hdigit hcarry
by_cases hread0 : read.val = 0
· have hreadEq : read = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hread0)
subst read
refine ⟨b, a, write, carry, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
· have hreadpos : 0 < read.val := by omega
have hreadne : read != 0#usize := by
simp only [bne_iff_ne]
intro h
apply hread0
simpa [h]
obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := read) (y := 1#usize) (by simpa using hreadpos))
norm_num at hread1Val
have hwritepos : 0 < write.val := by omega
obtain ⟨write1, hwrite1, hwrite1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := write) (y := 1#usize) (by simpa using hwritepos))
norm_num at hwrite1Val
have hread1lt : read1.val < a.val.length := by
rw [a.property]
norm_num
omega
obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec a read1
(by simpa [Array.length] using hread1lt))
have hdigitGet : a.val[read1.val]? = some digit :=
getElem?_eq_some_iff.mpr ⟨hread1lt, hdigitReadVal.symm⟩
have hdigitLt : digit.val < 10 :=
hdigit read1.val digit (by omega) hdigitGet
let digit64 : Std.U64 := UScalar.cast .U64 digit
have hdigit64 : digit64.val = digit.val := by
simp [digit64, UScalar.cast_val_eq]
have hshiftSpec := UScalar.ShiftLeft_spec digit64 shift (2 ^ 64)
(by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq])
cases hshiftEq : digit64 <<< shift with
| fail e => simp [hshiftEq] at hshiftSpec
| div => simp [hshiftEq] at hshiftSpec
| ok shifted =>
rw [hshiftEq] at hshiftSpec
have hshiftVal : shifted.val = digit.val * B := by
have hmulLt : digit.val * B < 2 ^ 64 := by
calc
digit.val * B <= 9 * 2 ^ 60 := Nat.mul_le_mul (by omega) hBle
_ < 2 ^ 64 := by norm_num
have hs := hshiftSpec.1
simp only [WP.spec_ok] at hs
rw [Nat.shiftLeft_eq, hdigit64, Nat.mod_eq_of_lt (by
simpa [B] using hmulLt)] at hs
simpa [B] using hs
have haddBound : carry.val + shifted.val <= U64.max := by
rw [hshiftVal]
have hdigitMul : digit.val * B <= 9 * B :=
Nat.mul_le_mul_right B (by omega)
have : carry.val + digit.val * B < 2 ^ 64 := by
calc
carry.val + digit.val * B < B + 9 * B := by omega
_ = 10 * B := by ring
_ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
_ < 2 ^ 64 := by norm_num
norm_num [U64.max, U64.numBits]
omega
obtain ⟨total, htotal, htotalVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := carry) (y := shifted) haddBound)
obtain ⟨quotient, hquotient, hquotientVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.div_spec total (y := 10#u64) (by norm_num))
have hmulBound : 10 * quotient.val <= U64.max := by
rw [hquotientVal]
have htotalLt : total.val < 2 ^ 64 := total.bv.isLt
have hqle : 10 * (total.val / 10) <= total.val :=
Nat.mul_div_le total.val 10
norm_num [U64.max, U64.numBits] at htotalLt ⊢
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))
have htenqLe : tenq.val <= total.val := by
rw [htenqVal, hquotientVal]
exact Nat.mul_div_le total.val 10
obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := total) (y := tenq) htenqLe)
have htotalFormula : total.val = carry.val + digit.val * B := by
rw [htotalVal, hshiftVal]
have hquotientFormula : quotient.val = total.val / 10 := hquotientVal
have hremFormula : rem.val = total.val % 10 := by
have htenqFormula : tenq.val = 10 * (total.val / 10) := by
rw [htenqVal, hquotientVal]
norm_num
have heuc := Nat.mod_add_div total.val 10
rw [hremVal.1, htenqFormula]
omega
have hremLt : rem.val < 10 := by
rw [hremFormula]
exact Nat.mod_lt _ (by omega)
have hquotientB : quotient.val < B := by
rw [hquotientFormula]
apply (Nat.div_lt_iff_lt_mul (by omega)).2
rw [htotalFormula]
have hdigitMul : digit.val * B <= 9 * B :=
Nat.mul_le_mul_right B (by omega)
omega
by_cases hcap : write1.val < 768
· have hwibound : write1.val < a.val.length := by
rw [a.property]
norm_num
exact hcap
obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec a write1 (UScalar.cast .U8 rem)
(by simpa [Array.length] using hwibound))
have hreadwrite1 : read1.val <= write1.val := by omega
have hcapWord : write1 <
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
have hdigit1 : forall j z, j < read1.val ->
a1.val[j]? = some z -> z.val < 10 := by
intro j z hj hz
rw [ha1Val] at hz
simp only [Aeneas.Std.Array.set, List.getElem?_set] at hz
have hne : write1.val ≠ j := by omega
simp [hne] at hz
exact hdigit j z (by omega) hz
obtain ⟨b', a', write', carry', hrest⟩ :=
ih read1.val (by omega) b a1 read1 (by rfl)
write1 quotient (by omega) (by omega) hdigit1 hquotientB
refine ⟨b', a', write', carry', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
hdigitRead, lift]
rw [show UScalar.cast .U64 digit = digit64 by rfl]
simp only [
hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
hcapWord, ha1]
exact hrest
· have hcapWord : ¬ write1 <
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
have hdigit1 : forall j z, j < read1.val ->
a.val[j]? = some z -> z.val < 10 := by
intro j z hj hz
exact hdigit j z (by omega) hz
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]
obtain ⟨b', a', write', carry', hrest⟩ :=
ih read1.val (by omega) b a read1 (by rfl)
write1 quotient (by omega) (by omega) hdigit1 hquotientB
refine ⟨b', a', write', carry', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
hdigitRead, lift]
rw [show UScalar.cast .U64 digit = digit64 by rfl]
simp only [
hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
hcapWord, if_false, hnotgt]
exact hrest
· have hgt : rem > 0#u64 := by
change 0 < rem.val
omega
obtain ⟨b', a', write', carry', hrest⟩ :=
ih read1.val (by omega) true a read1 (by rfl)
write1 quotient (by omega) (by omega) hdigit1 hquotientB
refine ⟨b', a', write', carry', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body]
simp only [hreadne, if_true, hread1, hwrite1, bind_tc_ok,
hdigitRead, lift]
rw [show UScalar.cast .U64 digit = digit64 by rfl]
simp only [
hshiftEq, bind_tc_ok, htotal, hquotient, htenq, hrem,
hcapWord, if_false, hgt]
exact hrest
have loop1Total :
forall (b : Bool) (a : Array Std.U8 768#usize)
(write : Std.Usize) (carry : Std.U64),
carry.val < 10 ^ write.val ->
exists (b' : Bool) (a' : Array Std.U8 768#usize),
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b a write carry = ok (b', a') := by
intro b a write
generalize hn : write.val = n
induction n using Nat.strong_induction_on generalizing write b a with
| h n ih =>
intro carry hcarry
have hcarryWrite : carry.val < 10 ^ write.val := by
rw [hn]
exact hcarry
by_cases hcarry0 : carry.val = 0
· have hcarryEq : carry = 0#u64 :=
UScalar.eq_of_val_eq (by simpa using hcarry0)
subst carry
refine ⟨b, a, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body]
· have hcarrypos : 0 < carry.val := by omega
have hgt : carry > 0#u64 := by
change 0 < carry.val
exact hcarrypos
have hwritepos : 0 < write.val := by
by_contra hnpos
have hwrite0 : write.val = 0 := by omega
rw [hwrite0] at hcarryWrite
simp at hcarryWrite
omega
obtain ⟨write1, hwrite1, hwrite1Val⟩ :=
Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := write) (y := 1#usize)
(by simpa using hwritepos))
norm_num at hwrite1Val
obtain ⟨quotient, hquotient, hquotientVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.div_spec carry (y := 10#u64) (by norm_num))
have hmulBound : 10 * quotient.val <= U64.max := by
rw [hquotientVal]
have hcarryLt : carry.val < 2 ^ 64 := carry.bv.isLt
have hqle : 10 * (carry.val / 10) <= carry.val :=
Nat.mul_div_le carry.val 10
norm_num [U64.max, U64.numBits] at hcarryLt ⊢
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))
have htenqLe : tenq.val <= carry.val := by
rw [htenqVal, hquotientVal]
exact Nat.mul_div_le carry.val 10
obtain ⟨rem, hrem, hremVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := carry) (y := tenq) htenqLe)
have hremFormula : rem.val = carry.val % 10 := by
have htenqFormula : tenq.val = 10 * (carry.val / 10) := by
rw [htenqVal, hquotientVal]
norm_num
have heuc := Nat.mod_add_div carry.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 = carry.val / 10 := by
simpa using hquotientVal
have hquotientFit : quotient.val < 10 ^ write1.val := by
rw [hquotientFormula]
apply (Nat.div_lt_iff_lt_mul (by omega)).2
rw [← pow_succ]
have hwriteSucc : write1.val + 1 = write.val := by omega
rw [hwriteSucc]
exact hcarryWrite
by_cases hcap : write1.val < 768
· have hwibound : write1.val < a.val.length := by
rw [a.property]
norm_num
exact hcap
obtain ⟨a1, ha1, ha1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec a write1 (UScalar.cast .U8 rem)
(by simpa [Array.length] using hwibound))
have hcapWord : write1 <
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
obtain ⟨b', a', hrest⟩ :=
ih write1.val (by omega) b a1 write1 (by rfl)
quotient hquotientFit
refine ⟨b', a', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
hcapWord, lift, ha1]
exact hrest
· have hcapWord : ¬ write1 <
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
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]
obtain ⟨b', a', hrest⟩ :=
ih write1.val (by omega) b a write1 (by rfl)
quotient hquotientFit
refine ⟨b', a', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
hcapWord, if_false, hnotgt]
exact hrest
· have hgtRem : rem > 0#u64 := by
change 0 < rem.val
omega
obtain ⟨b', a', hrest⟩ :=
ih write1.val (by omega) true a write1 (by rfl)
quotient hquotientFit
refine ⟨b', a', ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
rw [Aeneas.Std.loop.eq_def]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1.body,
hgt, if_true, hwrite1, bind_tc_ok, hquotient, htenq, hrem,
hcapWord, if_false, hgtRem]
exact hrest
have zeroGrowthTotal (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
(sh : Std.Usize) (hsh : sh.val = 0) :
exists gr : Std.Usize,
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d0 sh = ok gr /\ gr.val = 0 := by
have hseq : sh = 0#usize := UScalar.eq_of_val_eq (by simpa using hsh)
subst sh
let kfin : Fin 61 := ⟨0, by omega⟩
have cert := DecimalLeftShiftTableCertificate kfin
dsimp only at cert
rcases cert with ⟨hloidx, hhiidx, hlenTable, _, hzeroTable, _⟩
have hz := hzeroTable (by simp [kfin])
rcases hz with ⟨hhiZero, hdsZero⟩
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let pow5Table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
let x := table.val.get ⟨0, by simp [table]⟩
let y := table.val.get ⟨1, by simp [table]⟩
let hi := x.val / 2048
let lo := x.val % 2048
let hiNext := y.val % 2048
let dsU := pow5Table.val.extract lo hiNext
let ds := dsU.map (fun q => q.val)
have hloidx' : lo <= hiNext := by
simpa [lo, hiNext, x, y, table, kfin] using hloidx
have hhiidx' : hiNext <= 1308 := by
simpa [hiNext, y, table, kfin] using hhiidx
have hlen' : ds.length = hiNext - lo := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hlenTable
have hhiZero' : hi = 0 := by
simpa [hi, x, table, kfin] using hhiZero
have hdsZero' : ds = [] := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table, kfin] using hdsZero
have hloEq : lo = hiNext := by
rw [hdsZero'] at hlen'
simp at hlen'
omega
have hmask : (0#usize &&& 63#usize) = 0#usize := by decide
obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
0#usize (by simp [Array.length]))
have hxaEq : xa = x := by
apply UScalar.eq_of_val_eq
simpa [x, table] using congrArg UScalar.val hxaVal
subst xa
obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
norm_num at hnextVal
obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
next (by simp [Array.length]; omega))
have hybEq : yb = y := by
apply UScalar.eq_of_val_eq
simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
subst yb
obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
have hhigh16Eq : high16.val = hi := by
rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
norm_num [hi, Int.toNat]
let numNew : Std.Usize := UScalar.cast .Usize high16
have hnumNewVal : numNew.val = 0 := by
simp [numNew, hhigh16Eq, hhiZero']
let low16 := 2047#u16 &&& x
let pow5a : Std.Usize := UScalar.cast .Usize low16
have hpow5aVal : pow5a.val = lo := by
simp [pow5a, low16, lo, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
let next16 := 2047#u16 &&& y
let pow5b : Std.Usize := UScalar.cast .Usize next16
have hpow5bVal : pow5b.val = hiNext := by
simp [pow5b, next16, hiNext, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
have hlo1308 : pow5a.val <= 1308 := by
rw [hpow5aVal]
exact hloidx'.trans hhiidx'
let slice : Slice Std.U8 := pow5Table.to_slice.drop pow5a
have hslice :
core.array.Array.index
(core.ops.index.IndexSlice
(core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
pow5Table { start := pow5a } = ok slice := by
rw [Array.index_SliceIndexRangeFromUsizeSlice]
unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
simp [hlo1308, slice]
have hsubBound : pow5a.val <= pow5b.val := by
rw [hpow5aVal, hpow5bVal]
exact hloidx'
obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
have hspanZero : span.val = 0 := by
rw [hspanVal.1, hpow5aVal, hpow5bVal, ← hloEq]
simp
have hspanEq : span = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hspanZero)
subst span
refine ⟨numNew, ?_, hnumNewVal⟩
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
simp only [hmask, lift, Bind.bind, Aeneas.Std.bind, hxa, bind_tc_ok,
hnext, hyb, hhigh16]
rw [hxaEq, hybEq]
simp only [hhigh16, bind_tc_ok]
rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
rw [show UScalar.cast .Usize high16 = numNew by rfl]
rw [hslice]
simp only [bind_tc_ok]
unfold core.slice.Slice.iter
simp only [bind_tc_ok]
unfold core.slice.iter.IteratorSliceIter.enumerate
simp only [bind_tc_ok, hspan]
unfold core.iter.adapters.enumerate.IteratorEnumerate.take
simp only [bind_tc_ok]
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop
rw [Aeneas.Std.loop.eq_def]
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body
unfold core.iter.adapters.take.IteratorTake.next
simp
have trimLoopTotal :
forall (a : Array Std.U8 768#usize) (n : Std.Usize),
n.val <= a.val.length ->
exists j : Std.Usize,
num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop n a = ok j := by
intro a n
generalize hn : n.val = k
induction k using Nat.strong_induction_on generalizing n with
| h k ih =>
intro hbound
by_cases hn0 : n.val = 0
· have hnEq : n = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hn0)
subst n
refine ⟨0#usize, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
rw [Aeneas.Std.loop.eq_def]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body]
· have hnpos : 0 < n.val := by omega
have hnne : n != 0#usize := by
simp only [bne_iff_ne]
intro heq
apply hn0
simpa [heq]
obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := n) (y := 1#usize) (by simpa using hnpos))
norm_num at hn1Val
have hn1lt : n1.val < a.val.length := by omega
obtain ⟨digit, hdigit, hdigitVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec a n1
(by simpa [Array.length] using hn1lt))
by_cases hzero : digit = 0#u8
· obtain ⟨j, hrest⟩ :=
ih n1.val (by omega) n1 (by rfl) (by omega)
refine ⟨j, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
rw [Aeneas.Std.loop.eq_def]
simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_true]
exact hrest
· refine ⟨n, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
rw [Aeneas.Std.loop.eq_def]
simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body,
hnne, if_false, hn1, bind_tc_ok, hdigit, hzero, if_false]
simp
have trimTotal
(e : num.imp.dec2flt.decimal_seq.DecimalSeq)
(hbound : e.num_digits.val <= e.digits.val.length) :
exists e' : num.imp.dec2flt.decimal_seq.DecimalSeq,
num.imp.dec2flt.decimal_seq.DecimalSeq.trim e = ok e' := by
have hle : e.num_digits <=
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [UScalar.le_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
e.digits.property] using hbound
obtain ⟨j, hj⟩ := trimLoopTotal e.digits e.num_digits hbound
refine ⟨{ e with num_digits := j }, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim
simp only [hle, massert_True, bind_tc_ok, hj]
rcases hinput with ⟨hcap, hdigit, z, zs, hdigits, hzlo, hzhi, hlen⟩
let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
let F := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
let I := F xs
let B := 2 ^ shift.val
have hxdigit : forall q, q ∈ xs -> q < 10 := by
intro q hq
simp only [xs, List.mem_map] at hq
rcases hq with ⟨a, ha, rfl⟩
exact hdigit a ha
have hxslen : xs.length = d.num_digits.val := by
rw [show xs = z :: zs by simpa [xs] using hdigits]
exact hlen
have hnpos : 0 < d.num_digits.val := by
rw [← hxslen]
simpa [xs, hdigits]
have hnne : d.num_digits ≠ 0#usize := by
intro heq
have : d.num_digits.val = 0 := by simpa [heq]
omega
have foldlFrom (ys : List Nat) (acc : Nat) :
ys.foldl (fun n q => 10 * n + q) acc =
acc * 10 ^ ys.length + F ys := by
induction ys generalizing acc with
| nil => simp [F]
| cons q qs ih =>
simp only [List.foldl_cons, List.length_cons]
rw [ih, pow_succ]
change (10 * acc + q) * 10 ^ qs.length + F qs =
acc * (10 ^ qs.length * 10) +
(q :: qs).foldl (fun n z => 10 * n + z) 0
simp only [List.foldl_cons]
rw [ih]
ring
have foldBound (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10) :
F ys < 10 ^ ys.length := by
induction ys with
| nil => simp [F]
| cons q qs ih =>
have hq : q < 10 := hy q (by simp)
have hqs : forall a, a ∈ qs -> a < 10 := by
intro a ha
exact hy a (by simp [ha])
have hi := ih hqs
rw [show F (q :: qs) = q * 10 ^ qs.length + F qs by
simpa [F] using foldlFrom qs q, List.length_cons, pow_succ]
have hm : q * 10 ^ qs.length <= 9 * 10 ^ qs.length :=
Nat.mul_le_mul_right _ (by omega)
calc
q * 10 ^ qs.length + F qs <
q * 10 ^ qs.length + 10 ^ qs.length := Nat.add_lt_add_left hi _
_ <= 9 * 10 ^ qs.length + 10 ^ qs.length := Nat.add_le_add_right hm _
_ = 10 ^ qs.length * 10 := by ring
have hIbound : I < 10 ^ d.num_digits.val := by
simpa [hxslen] using foldBound xs hxdigit
have hIlower : 10 ^ (d.num_digits.val - 1) <= I := by
have hIform : I = z * 10 ^ zs.length + F zs := by
simp only [I, xs, hdigits, List.foldl_cons]
simpa [F] using foldlFrom zs z
have hnlen : d.num_digits.val = zs.length + 1 := by
rw [← hxslen]
simp [xs, hdigits]
rw [hnlen]
simp only [Nat.add_sub_cancel]
rw [hIform]
have hm := Nat.mul_le_mul_right (10 ^ zs.length) hzlo
omega
have decimalUpper (c w t n : Nat) (hc : c < t) (hw : w < 10 ^ n) :
c * 10 ^ n + w < t * 10 ^ n := by
have hc1 : c + 1 <= t := by omega
have hm := Nat.mul_le_mul_right (10 ^ n) hc1
nlinarith
obtain ⟨growth, hgrowth, hgrowthLe, hproductLo, hproductHi⟩ :
exists growth : Std.Usize,
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d shift = ok growth /\
growth.val <= 19 /\
10 ^ (d.num_digits.val + growth.val - 1) <= B * I /\
B * I < 10 ^ (d.num_digits.val + growth.val) := by
by_cases hk0 : shift.val = 0
· obtain ⟨growth, hgrowth, hgrowthZero⟩ :=
zeroGrowthTotal d shift hk0
refine ⟨growth, hgrowth, by omega, ?_, ?_⟩
· simpa [B, hk0, hgrowthZero] using hIlower
· simpa [B, hk0, hgrowthZero] using hIbound
· have hkpos : 0 < shift.val := by omega
let kfin : Fin 61 := ⟨shift.val, by omega⟩
have cert := DecimalLeftShiftTableCertificate kfin
dsimp only at cert
rcases cert with ⟨_, _, _, _, _, hpositiveTable⟩
have hp := hpositiveTable (by simpa [kfin] using hkpos)
rcases hp with ⟨_, _, _, _, hlo, hhi⟩
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let tableEntry := table.val.get ⟨shift.val, by
have htlen : table.val.length = 65 := by simp [table]
omega⟩
let h := tableEntry.val / 2048
have hlo' : 10 ^ (h - 1) < B := by
simpa [h, tableEntry, table, kfin, B] using hlo
have hhi' : B < 10 ^ h := by
simpa [h, tableEntry, table, kfin, B] using hhi
have hhpos : 0 < h := by
by_contra hz
have : h = 0 := by omega
have hBpos : 0 < B := by simp [B]
rw [this] at hhi'
norm_num at hhi'
omega
have hhle : h <= 19 := by
by_contra hn
have hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) (by omega)
have hBle : B <= 2 ^ 60 :=
Nat.pow_le_pow_right (by omega) hshift
have hnum : 2 ^ 60 < 10 ^ 19 := by norm_num
exact (not_lt_of_ge hp19)
(lt_of_lt_of_le hlo' (le_trans hBle (Nat.le_of_lt hnum)))
obtain ⟨growth, hgrowth, hgrowthVal⟩ :=
DecimalLeftShiftDigitGrowth d shift h hshift hlo' hhi' hcap hdigit
have hgrowthFormula : growth.val =
if 10 ^ (d.num_digits.val + h - 1) <= I * B
then h else h - 1 := by
simpa [xs, I, F, B] using hgrowthVal
have hgrowthLe : growth.val <= 19 := by
rw [hgrowthFormula]
split <;> omega
refine ⟨growth, hgrowth, hgrowthLe, ?_, ?_⟩
· rw [hgrowthFormula]
split
next ht => simpa [Nat.mul_comm] using ht
next ht =>
have hmul1 : 10 ^ (h - 1) * 10 ^ (d.num_digits.val - 1) <
B * 10 ^ (d.num_digits.val - 1) :=
(Nat.mul_lt_mul_right (by positivity)).mpr hlo'
have hmul2 : B * 10 ^ (d.num_digits.val - 1) <= B * I :=
Nat.mul_le_mul_left B hIlower
have he : d.num_digits.val + (h - 1) - 1 =
(h - 1) + (d.num_digits.val - 1) := by omega
rw [he, Nat.pow_add]
exact Nat.le_of_lt (lt_of_lt_of_le hmul1 hmul2)
· rw [hgrowthFormula]
split
next ht =>
have hBpos : 0 < B := by simp [B]
have hpowpos : 0 < 10 ^ d.num_digits.val := by positivity
have hmul1 : B * I < B * 10 ^ d.num_digits.val :=
(Nat.mul_lt_mul_left hBpos).mpr hIbound
have hmul2 : B * 10 ^ d.num_digits.val <
10 ^ h * 10 ^ d.num_digits.val :=
(Nat.mul_lt_mul_right hpowpos).mpr hhi'
have hm := lt_trans hmul1 hmul2
rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hm
simpa using hm
next ht =>
have hlt : I * B <
10 ^ (d.num_digits.val + h - 1) := by omega
rw [Nat.mul_comm]
simpa only [show d.num_digits.val + h - 1 =
d.num_digits.val + (h - 1) by omega] using hlt
have haddBound : d.num_digits.val + growth.val <= UScalar.max .Usize := by
scalar_tac
obtain ⟨width, hwidth, hwidthVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := d.num_digits) (y := growth) haddBound)
have hcursorInput : d.num_digits.val <= width.val := by
rw [hwidthVal]
omega
have hdigitIndexed : forall j u, j < d.num_digits.val ->
d.digits.val[j]? = some u -> u.val < 10 := by
intro j u hj hu
apply hdigit u
rw [List.mem_take_iff_getElem]
rcases getElem?_eq_some_iff.mp hu with ⟨hjlen, hju⟩
exact ⟨j, by simp; omega, hju⟩
obtain ⟨b0, a0, cursor, carry, hloop0⟩ :=
loop0Total d.truncated d.digits d.num_digits
width 0#u64 hcursorInput hcap hdigitIndexed (by simp [B])
obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryBase,
hproduct, hlowArray, houtside0, hsticky0⟩ :=
DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
shift d.num_digits width cursor carry hshift hcap hcursorInput
hdigit hloop0
have hcursorVal : cursor.val = growth.val := by
rw [hwidthVal] at hcursorEq
omega
have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
simpa [hlowLen] using foldBound lowDigits hlowDigit
have hproduct' :
B * I = carry.val * 10 ^ d.num_digits.val + F lowDigits := by
simpa [B, I, F, xs, List.map_take] using hproduct
have hcarryWidth : carry.val < 10 ^ cursor.val := by
rw [hcursorVal]
have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
rw [hproduct']
omega
have hpow : 10 ^ growth.val * 10 ^ d.num_digits.val =
10 ^ (d.num_digits.val + growth.val) := by
rw [Nat.mul_comm, ← Nat.pow_add]
have hm : carry.val * 10 ^ d.num_digits.val <
10 ^ growth.val * 10 ^ d.num_digits.val := by
rw [hpow]
exact lt_of_le_of_lt hcle hproductHi
exact Nat.lt_of_mul_lt_mul_right hm
have hcarryLower : 0 < growth.val ->
10 ^ (growth.val - 1) <= carry.val := by
intro hgpos
by_contra hnot
have hcsmall : carry.val < 10 ^ (growth.val - 1) := by omega
have hu := decimalUpper carry.val (F lowDigits)
(10 ^ (growth.val - 1)) d.num_digits.val hcsmall hlowBound
rw [← hproduct'] at hu
rw [Nat.mul_comm (10 ^ (growth.val - 1)), ← Nat.pow_add] at hu
have he : d.num_digits.val + (growth.val - 1) =
d.num_digits.val + growth.val - 1 := by omega
rw [he] at hu
exact (not_lt_of_ge hproductLo) hu
have hpad : forall j, j < cursor.val -> j < 768 ->
carry.val < 10 ^ (cursor.val - j - 1) ->
a0.val[j]? = some 0#u8 := by
intro j hjcursor _ hjcarry
have hgpos : 0 < growth.val := by omega
have hlower := hcarryLower hgpos
have hexp : cursor.val - j - 1 <= growth.val - 1 := by
rw [hcursorVal]
omega
have hpow :
10 ^ (cursor.val - j - 1) <= 10 ^ (growth.val - 1) :=
Nat.pow_le_pow_right (by omega) hexp
omega
obtain ⟨b1, a1, hloop1⟩ :=
loop1Total b0 a0 cursor carry hcarryWidth
obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
hhighArray, houtside1, hsticky1⟩ :=
DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
hcarryWidth hpad hloop1
have headPositive (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10)
(hyspos : 0 < ys.length)
(hlower : 10 ^ (ys.length - 1) <= F ys) :
exists q qs, ys = q :: qs /\ 0 < q := by
cases hys : ys with
| nil =>
rw [hys] at hyspos
simp at hyspos
| cons q qs =>
refine ⟨q, qs, rfl, ?_⟩
by_contra hq
have hqzero : q = 0 := by omega
have htailDigit : forall a, a ∈ qs -> a < 10 := by
intro a ha
apply hy a
rw [hys]
simp [ha]
have htailBound := foldBound qs htailDigit
have htailLower : 10 ^ qs.length <= F qs := by
simpa [hys, F, hqzero] using hlower
omega
have hcellZero : a1.val[0]? != some 0#u8 := by
simp only [bne_iff_ne]
by_cases hgzero : growth.val = 0
· have hcursorZero : cursor.val = 0 := by rw [hcursorVal, hgzero]
have hcarryZero : carry.val = 0 := by
rw [hcursorZero] at hcarryWidth
simp at hcarryWidth
omega
have hlowLower : 10 ^ (lowDigits.length - 1) <= F lowDigits := by
rw [hlowLen]
rw [hgzero, Nat.add_zero] at hproductLo
rw [hcarryZero] at hproduct'
simp only [zero_mul, zero_add] at hproduct'
rw [hproduct'] at hproductLo
exact hproductLo
obtain ⟨q, qs, hlowEq, hqpos⟩ :=
headPositive lowDigits hlowDigit (by rw [hlowLen]; exact hnpos) hlowLower
have hlowCell := hlowArray 0 (by rw [hlowEq]; simp) (by
rw [hcursorZero]
omega)
have hout := houtside1 0 (by omega) (by omega)
have hcell :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
rw [List.getElem?_map, hout]
simpa [hcursorZero, hlowEq] using hlowCell
intro hz
have hzmap :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
rw [List.getElem?_map, hz]
rfl
rw [hcell] at hzmap
have : q = 0 := Option.some.inj hzmap
omega
· have hgpos : 0 < growth.val := by omega
have hhighLower :
10 ^ (highDigits.length - 1) <= F highDigits := by
rw [hhighLen, hcursorVal]
simpa [F, hcarryDigits] using hcarryLower hgpos
obtain ⟨q, qs, hhighEq, hqpos⟩ :=
headPositive highDigits hhighDigit
(by rw [hhighLen, hcursorVal]; exact hgpos) hhighLower
have hcell := hhighArray 0 (by rw [hhighEq]; simp) (by omega)
have hcell' :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
simpa [hhighEq] using hcell
intro hz
have hzmap :
(a1.val.map (fun z : Std.U8 => z.val))[0]? = some 0 := by
rw [List.getElem?_map, hz]
rfl
rw [hcell'] at hzmap
have : q = 0 := Option.some.inj hzmap
omega
let capped : Std.Usize :=
if width.val > 768 then
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS else width
have hcappedVal : capped.val = min width.val 768 := by
simp only [capped]
split
· simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
omega
· simp
omega
have hcapEval :
(if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
else ok width) = ok capped := by
simp only [capped]
by_cases hc : width.val > 768
· have hc' : width >
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [gt_iff_lt, UScalar.lt_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hc
simp [hc', hc]
· have hc' : ¬ width >
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [gt_iff_lt, UScalar.lt_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using hc
simp [hc', hc]
have hcappedPos : 0 < capped.val := by
rw [hcappedVal, hwidthVal]
simp only [lt_min_iff]
omega
let growthI : Std.I32 := UScalar.hcast .I32 growth
have hgrowthIVal : growthI.val = (growth.val : Int) := by
have hs := UScalar.hcast_inBounds_spec .I32 growth (by
rw [IScalar.max_I32_eq]
omega)
simp only [lift, WP.spec_ok] at hs
exact hs
have hpointBounds :
IScalar.min .I32 <= d.decimal_point.val + growthI.val /\
d.decimal_point.val + growthI.val <= IScalar.max .I32 := by
rw [hgrowthIVal]
constructor
· rw [IScalar.min_I32_eq]
omega
· rw [IScalar.max_I32_eq]
omega
obtain ⟨point, hpoint, hpointVal⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hpointBounds.1 hpointBounds.2)
have hpointVal' : point.val =
d.decimal_point.val + (growth.val : Int) := by
rw [hpointVal, hgrowthIVal]
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := capped, decimal_point := point,
truncated := b1, digits := a1 }
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, hcappedVal, a1.property]
obtain ⟨d', htrim⟩ := trimTotal pre hpreBound
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalue⟩
have hnumNonzero : d'.num_digits.val != 0 := by
simp only [bne_iff_ne]
intro hzero
have hz := hzeros 0 (by omega) (by simpa [pre] using hcappedPos)
have hz' : a1.val[0]? = some 0#u8 := by
simpa [pre] using hz
have hcellZero' : a1.val[0]? ≠ some 0#u8 := by
simpa only [bne_iff_ne] using hcellZero
exact hcellZero' hz'
have hcall :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift
simp only [hnne, if_false, hgrowth, bind_tc_ok, hwidth, hloop0]
change (do
let (bc, ac) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b0 a0 cursor carry
let count ←
if width > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
then ok num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
else ok width
let delta ← lift (UScalar.hcast .I32 growth)
let point' ← d.decimal_point + delta
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := count, decimal_point := point',
truncated := bc, digits := ac }) = ok d'
rw [hloop1]
simp only [bind_tc_ok]
rw [hcapEval]
simp only [bind_tc_ok, lift]
rw [show UScalar.hcast .I32 growth = growthI by rfl]
simp only [bind_tc_ok, hpoint]
simpa [pre] using htrim
have hpointOut : d'.decimal_point.val =
d.decimal_point.val + (growth.val : Int) := by
rw [hpointEq]
simpa [pre] using hpointVal'
refine ⟨d', growth.val, hcall, hgrowthLe, hpointOut, ?_, ?_,
hnumNonzero⟩
· rw [hpointOut]
omega
· rw [hpointOut]
omega
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Inspect related source (116)
- left_shift · dec2flt/decimal_seq.rs:129 Program Rust
- left_shift_loop0 · dec2flt/decimal_seq.rs:138 Program Rust
- body · dec2flt/decimal_seq.rs:138 Program Rust
More program dependencies (83)
- left_shift_loop1 · dec2flt/decimal_seq.rs:152 Program Rust
- body · dec2flt/decimal_seq.rs:152 Program Rust
- DecimalSeq · dec2flt/decimal_seq.rs:21 Program Rust
- MAX_DIGITS · dec2flt/decimal_seq.rs:63 Program Rust
- trim · dec2flt/decimal_seq.rs:82 Program Rust
- trim_loop · dec2flt/decimal_seq.rs:91 Program Rust
- body · dec2flt/decimal_seq.rs:91 Program Rust
- number_of_digits_decimal_left_shift · dec2flt/decimal_seq.rs:368 Program Rust
- TABLE · dec2flt/decimal_seq.rs:370 Program Rust
- TABLE_POW5 · dec2flt/decimal_seq.rs:379 Program Rust
- number_of_digits_decimal_left_shift_loop · dec2flt/decimal_seq.rs:434 Program Rust
- body · dec2flt/decimal_seq.rs:434 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:52 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 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
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
- zero_pow2 · dec2flt/common.rs:62 Program Rust
- DECIMAL_POINT_RANGE · dec2flt/decimal_seq.rs:67 Program Rust
- right_shift · dec2flt/decimal_seq.rs:175 Program Rust
- right_shift_loop0 · dec2flt/decimal_seq.rs:180 Program Rust
- body · dec2flt/decimal_seq.rs:180 Program Rust
- right_shift_loop0_loop0 · dec2flt/decimal_seq.rs:190 Program Rust
- body · dec2flt/decimal_seq.rs:190 Program Rust
- right_shift_loop1 · dec2flt/decimal_seq.rs:209 Program Rust
- body · dec2flt/decimal_seq.rs:209 Program Rust
- right_shift_loop2 · dec2flt/decimal_seq.rs:216 Program Rust
- body · dec2flt/decimal_seq.rs:216 Program Rust
- round · dec2flt/decimal_seq.rs:96 Program Rust
- round_loop · dec2flt/decimal_seq.rs:106 Program Rust
- body · dec2flt/decimal_seq.rs:106 Program Rust
- parse_decimal_seq · dec2flt/decimal_seq.rs:283 Program Rust
- parse_decimal_seq_loop0 · dec2flt/decimal_seq.rs:290 Program Rust
- body · dec2flt/decimal_seq.rs:290 Program Rust
- parse_decimal_seq_loop1 · dec2flt/decimal_seq.rs:361 Program Rust
- body · dec2flt/decimal_seq.rs:361 Program Rust
- parse_decimal_seq_loop2 · dec2flt/decimal_seq.rs:303 Program Rust
- body · dec2flt/decimal_seq.rs:303 Program Rust
- parse_decimal_seq_loop3 · dec2flt/decimal_seq.rs:307 Program Rust
- body · dec2flt/decimal_seq.rs:307 Program Rust
- parse_decimal_seq_loop4 · dec2flt/decimal_seq.rs:361 Program Rust
- body · dec2flt/decimal_seq.rs:361 Program Rust
- parse_decimal_seq_loop5 · dec2flt/decimal_seq.rs:307 Program Rust
- body · dec2flt/decimal_seq.rs:307 Program Rust
- parse_decimal_seq_loop6 · dec2flt/decimal_seq.rs:361 Program Rust
- body · dec2flt/decimal_seq.rs:361 Program Rust
- parse_decimal_seq_loop7 · dec2flt/decimal_seq.rs:361 Program Rust
- body · dec2flt/decimal_seq.rs:361 Program Rust
- MAX_SHIFT · dec2flt/slow.rs:28 Program Rust
- call · dec2flt/slow.rs:33 Program Rust
- read_u64 · dec2flt/common.rs:18 Program Rust
- write_u64 · dec2flt/common.rs:25 Program Rust
- is_8digits · dec2flt/common.rs:40 Program Rust
- default · dec2flt/decimal_seq.rs:33 Program Rust
- MAX_DIGITS_WITHOUT_OVERFLOW · dec2flt/decimal_seq.rs:66 Program Rust
- count_trailing_zeros · dec2flt/decimal_seq.rs:235 Program Rust
- count_trailing_zeros_loop · dec2flt/decimal_seq.rs:239 Program Rust
- body · dec2flt/decimal_seq.rs:239 Program Rust
- parse_decimal_exponent · dec2flt/decimal_seq.rs:269 Program Rust
- parse_decimal_exponent_loop · dec2flt/decimal_seq.rs:272 Program Rust
- body · dec2flt/decimal_seq.rs:272 Program Rust
- parse_digits_into_seq · dec2flt/decimal_seq.rs:256 Program Rust
- parse_digits_into_seq_loop · dec2flt/decimal_seq.rs:258 Program Rust
- body · dec2flt/decimal_seq.rs:258 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:73 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
- DecimalDigitLexScale
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftFirstLoopSemantics
- DecimalLeftShiftLoop1Execution
- DecimalLeftShiftLoopLex
- DecimalLeftShiftSecondLoopSemantics
All 16 entries
- DecimalDigitLexScale
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftFirstLoopSemantics
- DecimalLeftShiftLoop1Execution
- DecimalLeftShiftLoopLex
- DecimalLeftShiftSecondLoopSemantics
- DecimalLeftShiftTableCertificate
- DecimalSeqTrimSemantics
- 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": "DecimalLeftShiftLoop1Execution",
"closure_version": "patch_c_v1",
"active_decl_hash": "89381916b4c8be60a99b4df09969d2397e4abe3a7f3fa96aae4f8778c23f5c64",
"active_statement_hash": "88a05276c3f3123a43e3bd7a70414d42c7c1394f5480709bdca02ff2c64b68c8",
"kernel_axioms": [
"Classical.choice",
"Quot.sound",
"propext"
],
"toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
"lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
"checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
"approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "revalidate-cycle-184",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalLeftShiftDigitGrowth": "6dcf8858df4713eb",
"DecimalLeftShiftFirstLoopSemantics": "d126fe9b83dfc5f3",
"DecimalLeftShiftSecondLoopSemantics": "ccbfee7a0aef7445",
"DecimalLeftShiftTableCertificate": "2f00a3779f444f8c",
"DecimalSeqTrimSemantics": "e4b2cbb62f7171af",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
"num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "0f3e5cc6a65d812e",
"num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "adfe9a4161cd7fd6",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "3703c132ca68c95c"
},
"strict_definition_deps": {
"num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
"num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
"num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "889854c120bac844",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE": "e68bec96fec99700",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE_POW5": "4dda92c1af5f2c9e"
}
}Prose assessment
{
"label": "Recorded Sound: VerifierPass",
"assessment_status": "VerifierPass",
"fingerprint_relation": "matches current fingerprint",
"assessment_fingerprint": "sound-v2:03705aed00402d46309b059027e523cfa0322e9ffb0f7fd2b7e37ef8de0ba58a",
"current_fingerprint": "sound-v2:03705aed00402d46309b059027e523cfa0322e9ffb0f7fd2b7e37ef8de0ba58a",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}