Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Returned-record semantics of decimal left shift
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
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000
namespace dec2flt_model
-- [TABLET NODE: DecimalSeqLeftShiftRecordSemantics]
theorem DecimalSeqLeftShiftRecordSemantics :
let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
let digits :=
(d.digits.val.take d.num_digits.val).map (fun z => z.val)
let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
(integer : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))
forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
(shift : Std.Usize),
shift.val <= 60 ->
0 < value d ->
(forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' ->
exists rho : Real,
0 <= rho /\
rho < (10 : Real) ^ (d'.decimal_point.val - 768) /\
value d' = (2 : Real) ^ shift.val * value d - rho /\
(d'.truncated = true <->
d.truncated = true \/ 0 < rho) := 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
left_shift_loop0. Its first body step decrements the positive
read index n and then reads cell n-1. A failing decrement or array read
cannot lie on an execution whose enclosing call returns normally. The
digit array has exactly 768 cells by
num imp dec2flt decimal seq DecimalSeq, and the cap has value 768
by num imp dec2flt decimal seq DecimalSeq MAX DIGITS; hence the
successful read gives n-1<768, and therefore n\le768. This also covers
an unrestricted Aeneas state whose count initially exceeds its physical
array: such a state cannot meet the successful-call hypothesis.Lean proof
dsimp only
intro d d' shift hshift hvalue hdigit hcall
let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
let F := fun ys : List Nat => ys.foldl (fun acc z => 10 * acc + z) 0
let I := F xs
have hxdigit : forall z, z ∈ xs -> z < 10 := by
intro z hz
simp only [xs, List.mem_map] at hz
rcases hz with ⟨u, hu, rfl⟩
exact hdigit u hu
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
have foldLowerAt (ys : List Nat) (j : Nat) (hj : j < ys.length)
(hjpos : 0 < ys[j]) : 10 ^ (ys.length - j - 1) <= F ys := by
have hprefix : ys[j] <= F (ys.take (j + 1)) := by
simp only [F]
rw [List.take_succ, List.foldl_append]
have hget : ys[j]? = some ys[j] :=
getElem?_eq_some_iff.mpr ⟨hj, rfl⟩
simp [hget]
have hdecomp : F ys =
F (ys.take (j + 1)) * 10 ^ (ys.length - j - 1) +
F (ys.drop (j + 1)) := by
conv_lhs => rw [← List.take_append_drop (j + 1) ys]
simp only [F, List.foldl_append]
rw [foldlFrom]
congr 2
simp
omega
rw [hdecomp]
have hm := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hprefix
have hone : 1 <= ys[j] := by omega
have hp := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hone
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
have foldPos (ys : List Nat) :
0 < F ys <-> exists j, exists hj : j < ys.length, 0 < ys[j] := by
induction ys with
| nil => simp [F]
| cons y ys ih =>
have hform : F (y :: ys) = y * 10 ^ ys.length + F ys := by
simpa [F] using foldlFrom ys y
rw [hform]
constructor
· intro hp
by_cases hy : 0 < y
· exact ⟨0, by simp, hy⟩
· have htail : 0 < F ys := by
have : y = 0 := by omega
simpa [this] using hp
rcases ih.mp htail with ⟨j, hj, hjpos⟩
exact ⟨j + 1, by simp; omega, by simpa using hjpos⟩
· rintro ⟨j, hj, hjpos⟩
cases j with
| zero =>
simp only [List.getElem_cons_zero] at hjpos
have hpow : 0 < 10 ^ ys.length := by positivity
nlinarith
| succ j =>
have hjlt : j < ys.length := by simp at hj; omega
have hp : 0 < F ys := ih.mpr ⟨j, hjlt, by simpa using hjpos⟩
omega
have zeroGrowth (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
(sh gr : Std.Usize) (hsh : sh.val = 0)
(hc : num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d0 sh = ok gr) : gr.val = 0 := by
as_aux_lemma =>
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, hlen, _, hzero, _⟩
have hz := hzero (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 z => z.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 hlen
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
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift at hc
simp only [hmask, lift, Bind.bind, Aeneas.Std.bind] at hc
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]))
rw [hxa] at hc
simp only [bind_tc_ok] at hc
have hxaEq : xa = x := by
apply UScalar.eq_of_val_eq
simpa [x, table] using congrArg UScalar.val hxaVal
subst xa
rw [hxaEq] at hc
obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
rw [hnext] at hc
simp only [bind_tc_ok] at hc
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))
rw [hyb] at hc
simp only [bind_tc_ok] at hc
have hybEq : yb = y := by
apply UScalar.eq_of_val_eq
simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
subst yb
rw [hybEq] at hc
obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
rw [hhigh16] at hc
simp only [bind_tc_ok] at hc
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]
rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl] at hc
rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl] at hc
rw [show UScalar.cast .Usize high16 = numNew by rfl] at hc
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]
rw [hslice] at hc
simp only [bind_tc_ok] at hc
unfold core.slice.Slice.iter at hc
simp only [bind_tc_ok] at hc
unfold core.slice.iter.IteratorSliceIter.enumerate at hc
simp only [bind_tc_ok] at hc
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)
rw [hspan] at hc
simp only [bind_tc_ok] at hc
unfold core.iter.adapters.enumerate.IteratorEnumerate.take at hc
simp only [bind_tc_ok] at hc
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
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop at hc
rw [Aeneas.Std.loop.eq_def] at hc
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body at hc
unfold core.iter.adapters.take.IteratorTake.next at hc
simp at hc
have hgrEq : gr = numNew := hc.symm
rw [hgrEq]
exact hnumNewVal
have hIpos : 0 < I := by
by_contra hn
have hIz : I = 0 := by omega
simp only [xs, I, F] at hIz
rw [hIz] at hvalue
simp at hvalue
have hnpos : 0 < d.num_digits.val := by
by_contra hn
have hnz : d.num_digits.val = 0 := by omega
simp [xs, I, F, hnz] at hIpos
have hnne : d.num_digits ≠ 0#usize := by
intro h
have : d.num_digits.val = 0 := by simpa [h]
omega
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift at hcall
simp only [hnne, if_false] at hcall
cases hgrowth :
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift d shift with
| fail e => simp [hgrowth] at hcall
| div => simp [hgrowth] at hcall
| ok growth =>
simp only [hgrowth, bind_tc_ok] at hcall
cases hadd : d.num_digits + growth with
| fail e => simp [hadd] at hcall
| div => simp [hadd] at hcall
| ok width =>
simp only [hadd, bind_tc_ok] at hcall
cases hloop0 :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
d.truncated d.digits shift d.num_digits width 0#u64 with
| fail e => simp [hloop0] at hcall
| div => simp [hloop0] at hcall
| ok out0 =>
rcases out0 with ⟨b0, a0, cursor, carry⟩
simp only [hloop0, bind_tc_ok] at hcall
have hnle : d.num_digits.val <= 768 := by
by_contra hnle
have hbad : 768 < d.num_digits.val := by omega
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 at hloop0
simp only [Bind.bind, Aeneas.Std.bind] at hloop0
rw [Aeneas.Std.loop.eq_def] at hloop0
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body at hloop0
have hrne : d.num_digits != 0#usize := by
simp only [bne_iff_ne]
exact hnne
simp only [hrne, if_true] at hloop0
obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := d.num_digits) (y := 1#usize)
(by simpa using hnpos))
norm_num at hread1Val
rw [hread1] at hloop0
simp only [bind_tc_ok] at hloop0
cases hwrite : width - 1#usize with
| fail e => simp [hwrite] at hloop0
| div => simp [hwrite] at hloop0
| ok write1 =>
rw [hwrite] at hloop0
simp only [bind_tc_ok] at hloop0
have hread1bad : d.digits.val.length <= read1.val := by
have halen : d.digits.val.length = 768 := by
simpa using d.digits.property
rw [halen, hread1Val.1]
omega
have hnone : d.digits.val[read1.val]? = none :=
List.getElem?_eq_none hread1bad
unfold Array.index_usize at hloop0
simp [hnone] at hloop0
have hxslen : xs.length = d.num_digits.val := by
simp [xs, hnle, d.digits.property]
have hIbound : I < 10 ^ d.num_digits.val := by
simpa [I, hxslen] using foldBound xs hxdigit
cases hloop1 :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b0 a0 cursor carry with
| fail e => simp [hloop1] at hcall
| div => simp [hloop1] at hcall
| ok out1 =>
rcases out1 with ⟨b1, a1⟩
let B := 2 ^ shift.val
by_cases hk0 : shift.val = 0
· have hgrowthZero : growth.val = 0 :=
zeroGrowth d shift growth hk0 hgrowth
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)
rw [hadd] at hwidth'
cases hwidth'
have hwidthEq : width.val = d.num_digits.val := by
rw [hwidthVal, hgrowthZero]
simp
have hcursorInput : d.num_digits.val <= width.val := by
rw [hwidthEq]
obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
hproduct, hlowArray, houtside0, hsticky0⟩ :=
DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
shift d.num_digits width cursor carry hshift hnle hcursorInput
hdigit hloop0
have hcursorZero : cursor.val = 0 := by
rw [hwidthEq] at hcursorEq
omega
have hcarryZero : carry.val = 0 := by
rw [hk0] at hcarryB
norm_num at hcarryB
omega
have hcarryWidth : carry.val < 10 ^ cursor.val := by
rw [hcarryZero, hcursorZero]
norm_num
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 hj
rw [hcursorZero] at hj
omega
obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
hhighArray, houtside1, hsticky1⟩ :=
DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
hcarryWidth hpad hloop1
have hhighNil : highDigits = [] := by
apply List.eq_nil_of_length_eq_zero
rw [hhighLen, hcursorZero]
have hb10 : b1 = true <-> b0 = true := by
rw [hsticky1]
constructor
· intro h
rcases h with h | ⟨j, hj, _, _⟩
· exact h
· rw [hhighNil] at hj
simp at hj
· exact Or.inl
have hb0 : b0 = true <-> d.truncated = true := by
rw [hsticky0]
constructor
· intro h
rcases h with h | ⟨j, hj, hjcap, _⟩
· exact h
· rw [hlowLen] at hj
rw [hcursorZero] at hjcap
omega
· exact Or.inl
have hsticky : b1 = true <-> d.truncated = true := by
rw [hb10, hb0]
have hlowFold : F lowDigits = I := by
simpa [hk0, hcarryZero, F, I, xs] using hproduct.symm
have hlowArray1 : forall j, j < lowDigits.length -> j < 768 ->
(a1.val.map (fun z : Std.U8 => z.val))[j]? = lowDigits[j]? := by
intro j hj hjphys
have hout := houtside1 j hjphys (by
rw [hcursorZero]
exact Nat.zero_le j)
have hlo := hlowArray j hj (by
rw [hcursorZero]
simpa using hjphys)
rw [List.getElem?_map, hout]
simpa [hcursorZero] using hlo
have hwidthLe : width.val <= 768 := by
rw [hwidthEq]
exact hnle
have hcapFalse : ¬ 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 (show ¬ 768 < width.val by 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 width := by
simp [hcapFalse]
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' at hcall
rw [hloop1] at hcall
simp only [bind_tc_ok] at hcall
rw [hcapEval] at hcall
simp only [bind_tc_ok, lift] at hcall
let growthI : Std.I32 := UScalar.hcast .I32 growth
rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
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 hgrowthIZero : growthI.val = 0 := by
rw [hgrowthIVal, hgrowthZero]
norm_num
cases hpoint : d.decimal_point + growthI with
| fail e => simp [hpoint] at hcall
| div => simp [hpoint] at hcall
| ok point =>
rw [hpoint] at hcall
simp only [bind_tc_ok] at hcall
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := width, decimal_point := point,
truncated := b1, digits := a1 }
have htrim :
num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
simpa [pre] using hcall
have hpointVal : point.val = d.decimal_point.val := by
have hs := IScalar.add_equiv d.decimal_point growthI
rw [hpoint] at hs
rw [hgrowthIZero] at hs
omega
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, a1.property]
exact hwidthLe
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
have hpreDigits :
(a1.val.take width.val).map (fun z => z.val) = lowDigits := by
apply List.ext_getElem
· simp [hwidthEq, hlowLen, hnle, a1.property]
· intro i hleft hright
have hi : i < lowDigits.length := hright
have hi768 : i < 768 := by
rw [hlowLen] at hi
omega
have ha := hlowArray1 i hi hi768
have haSome :
(a1.val.map (fun z : Std.U8 => z.val))[i]? =
some lowDigits[i] := by
rw [ha, List.getElem?_eq_getElem hi]
have hmapBound :
i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
simp [a1.property]
exact hi768
rw [show ((a1.val.take width.val).map (fun z => z.val))[i] =
(a1.val.map (fun z => z.val))[i] by simp]
have hsome := List.getElem?_eq_getElem hmapBound
rw [haSome] at hsome
exact Option.some.inj hsome.symm
refine ⟨0, by norm_num, zpow_pos (by norm_num) _, ?_, ?_⟩
· rw [hvalueTrim]
simp only [pre, hpointVal]
rw [hpreDigits, hwidthEq]
change (F lowDigits : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) =
(2 : Real) ^ shift.val *
((I : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))) - 0
rw [hlowFold, hk0]
norm_num
· rw [htruncEq]
simpa using hsticky
· 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 ⟨_, _, _, _, _, hpositive⟩
have hp := hpositive (by simpa [kfin] using hkpos)
rcases hp with ⟨_, _, _, hsum, hlo, hhi⟩
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let x := table.val.get ⟨shift.val, by
have hlen : table.val.length = 65 := by simp [table]
omega⟩
let h := x.val / 2048
have hlo' : 10 ^ (h - 1) < B := by
simpa [h, x, table, kfin, B] using hlo
have hhi' : B < 10 ^ h := by
simpa [h, x, 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 h20 : 20 <= h := by omega
have hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) (by omega)
have hBle : B <= 2 ^ 60 := by
exact 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' hnle hdigit
rw [hgrowth] at hgrowth'
cases hgrowth'
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
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)
rw [hadd] at hwidth'
cases hwidth'
have hcursorInput : d.num_digits.val <= width.val := by
rw [hwidthVal]
omega
obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
hproduct, hlowArray, houtside0, hsticky0⟩ :=
DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
shift d.num_digits width cursor carry hshift hnle hcursorInput
hdigit hloop0
have hcursorVal : cursor.val = growth.val := by
rw [hwidthVal] at hcursorEq
omega
have hprodWidth : B * I <
10 ^ (d.num_digits.val + growth.val) := by
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 hmul := lt_trans hmul1 hmul2
rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hmul
simpa using hmul
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 hcarryWidth : carry.val < 10 ^ cursor.val := by
rw [hcursorVal]
have hmul : carry.val * 10 ^ d.num_digits.val <
10 ^ growth.val * 10 ^ d.num_digits.val := by
have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
rw [hproduct]
omega
rw [Nat.mul_comm (10 ^ growth.val) (10 ^ d.num_digits.val),
← Nat.pow_add]
exact lt_of_le_of_lt hcle hprodWidth
exact Nat.lt_of_mul_lt_mul_right hmul
have hpad : forall j, j < cursor.val -> j < 768 ->
carry.val < 10 ^ (cursor.val - j - 1) ->
a0.val[j]? = some 0#u8 := by
as_aux_lemma =>
intro j hjcursor hjphys hjcarry
have hbase : a0.val[j]? = d.digits.val[j]? :=
houtside0 j hjphys (Or.inl hjcursor)
rw [hgrowthFormula] at hcursorVal
split at hcursorVal
next ht =>
have hexp : cursor.val - j - 1 <= h - 1 := by omega
have hpow : 10 ^ (cursor.val - j - 1) <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) hexp
have hcTop : carry.val < 10 ^ (h - 1) :=
lt_of_lt_of_le hjcarry hpow
have hu := decimalUpper carry.val (F lowDigits)
(10 ^ (h - 1)) d.num_digits.val hcTop (by
simpa [hlowLen] using foldBound lowDigits hlowDigit)
rw [← hproduct] at hu
rw [Nat.mul_comm (10 ^ (h - 1)), ← Nat.pow_add] at hu
change B * I < 10 ^ (d.num_digits.val + (h - 1)) at hu
have hthreshold : 10 ^ (d.num_digits.val + h - 1) <= B * I := by
simpa [Nat.mul_comm] using ht
have he : d.num_digits.val + (h - 1) =
d.num_digits.val + h - 1 := by omega
rw [he] at hu
exact False.elim ((not_lt_of_ge hthreshold) hu)
next ht =>
have hcursorGrowth : cursor.val = h - 1 := hcursorVal
have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
simpa [hlowLen] using foldBound lowDigits hlowDigit
have hu := decimalUpper carry.val (F lowDigits)
(10 ^ (cursor.val - j - 1)) d.num_digits.val
hjcarry hlowBound
rw [← hproduct] at hu
rw [Nat.mul_comm (10 ^ (cursor.val - j - 1)),
← Nat.pow_add] at hu
change B * I <
10 ^ (d.num_digits.val + (cursor.val - j - 1)) at hu
have hjN : j < d.num_digits.val := by
by_contra hjN
have hNle : d.num_digits.val <= j := by omega
have hexp : d.num_digits.val + (cursor.val - j - 1) <=
cursor.val - 1 := by omega
have hpow : 10 ^ (d.num_digits.val + (cursor.val - j - 1)) <=
10 ^ (cursor.val - 1) :=
Nat.pow_le_pow_right (by omega) hexp
have hcursorPos : 0 < cursor.val := by omega
have hpowStep : 10 ^ (cursor.val - 1) < 10 ^ cursor.val := by
apply Nat.pow_lt_pow_right (by omega)
omega
have hBLower : 10 ^ cursor.val < B := by
simpa [hcursorGrowth] using hlo'
have hBI : B <= B * I := by
have hm := Nat.mul_le_mul_left B (show 1 <= I by omega)
simpa using hm
exact (not_lt_of_ge hBI)
(lt_of_lt_of_le hu (le_trans hpow
(le_trans (Nat.le_of_lt hpowStep) (Nat.le_of_lt hBLower))))
have hcellBound : j < d.digits.val.length := by
have hlen : d.digits.val.length = 768 := by
simpa using d.digits.property
rw [hlen]
exact hjphys
have hxval : xs[j] = (d.digits.val[j]'hcellBound).val := by
simp [xs, hjN]
have hxzero : xs[j] = 0 := by
by_contra hxzero
have hxpos : 0 < xs[j] := by omega
have hIlower := foldLowerAt xs j (by simpa [hxslen] using hjN) hxpos
have hIlower' : 10 ^ (d.num_digits.val - j - 1) <= I := by
simpa [hxslen, I] using hIlower
have hBLower : 10 ^ cursor.val < B := by
simpa [hcursorGrowth] using hlo'
have hmul1 : 10 ^ cursor.val *
10 ^ (d.num_digits.val - j - 1) <
B * 10 ^ (d.num_digits.val - j - 1) :=
(Nat.mul_lt_mul_right (by positivity)).mpr hBLower
have hmul2 : B * 10 ^ (d.num_digits.val - j - 1) <= B * I :=
Nat.mul_le_mul_left B hIlower'
have hlower : 10 ^ (cursor.val +
(d.num_digits.val - j - 1)) < B * I := by
rw [Nat.pow_add]
exact lt_of_lt_of_le hmul1 hmul2
have he : cursor.val + (d.num_digits.val - j - 1) =
d.num_digits.val + (cursor.val - j - 1) := by omega
rw [he] at hlower
exact False.elim ((not_lt_of_ge (Nat.le_of_lt hlower)) hu)
have hdigitZero : d.digits.val[j]'hcellBound = 0#u8 := by
apply UScalar.eq_of_val_eq
simpa [← hxval, hxzero]
have hbaseZero : d.digits.val[j]? = some 0#u8 := by
apply getElem?_eq_some_iff.mpr
exact ⟨hcellBound, hdigitZero⟩
simpa [hbase] using hbaseZero
obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
hhighArray, houtside1, hsticky1⟩ :=
DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
hcarryWidth hpad hloop1
let fullDigits := highDigits ++ lowDigits
have hfullLen : fullDigits.length = width.val := by
simp [fullDigits, hhighLen, hlowLen, hcursorEq]
have hfullDigit : forall z, z ∈ fullDigits -> z < 10 := by
intro z hz
simp only [fullDigits, List.mem_append] at hz
exact hz.elim (hhighDigit z) (hlowDigit z)
have hfullFold : F fullDigits = B * I := by
have happ : F fullDigits =
carry.val * 10 ^ lowDigits.length + F lowDigits := by
simp only [fullDigits, F, List.foldl_append]
rw [foldlFrom, hcarryDigits]
rw [happ, hlowLen]
simpa [B, I, F, xs] using hproduct.symm
have hfullArray : forall j, j < fullDigits.length -> j < 768 ->
(a1.val.map (fun z : Std.U8 => z.val))[j]? = fullDigits[j]? := by
intro j hj hjphys
by_cases hjhigh : j < cursor.val
· have hh := hhighArray j (by simpa [hhighLen] using hjhigh) hjphys
rw [List.getElem?_append_left (by simpa [fullDigits, hhighLen] using hjhigh)]
simpa [fullDigits] using hh
· have hcj : cursor.val <= j := by omega
let t := j - cursor.val
have ht : t < lowDigits.length := by
simp [fullDigits, hhighLen, hlowLen, hfullLen] at hj
simp [t]
omega
have hidx : cursor.val + t = j := by simp [t, hcj]
have hout := houtside1 j hjphys hcj
have hlo := hlowArray t ht (by omega)
rw [List.getElem?_map, hout]
rw [List.getElem?_append_right (by
simpa [fullDigits, hhighLen] using hjhigh)]
simpa [fullDigits, hhighLen, t, hidx] using hlo
have hb10 : b1 = true <-> b0 = true := by
rw [hsticky1]
constructor
· intro h
rcases h with h | ⟨j, hj, hjcap, _⟩
· exact h
· rw [hhighLen, hcursorVal] at hj
omega
· exact Or.inl
have hstickyFull : b1 = true <-> d.truncated = true \/
exists j, j < fullDigits.length /\ 768 <= j /\
fullDigits[j]? != some 0 := by
rw [hb10, hsticky0]
constructor
· intro h
rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hd
· right
refine ⟨cursor.val + j, ?_, hjcap, ?_⟩
· simp [fullDigits, hhighLen, hlowLen]
omega
· rw [List.getElem?_append_right (by simp [hhighLen])]
simpa [hhighLen] using hjnz
· intro h
rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hd
· right
have hcj : cursor.val <= j := by
by_contra hcj
rw [List.getElem?_append_left (by
simpa [fullDigits, hhighLen] using hcj)] at hjnz
have hjlt : j < highDigits.length := by simpa [hhighLen] using hcj
have hjcap' : j < 768 := by omega
have hz := hhighDigit highDigits[j] (List.getElem_mem _)
omega
refine ⟨j - cursor.val, ?_, ?_, ?_⟩
· simp [fullDigits, hhighLen] at hj
omega
· omega
· rw [List.getElem?_append_right (by
simpa [fullDigits, hhighLen] using hcj)] at hjnz
simpa [hhighLen] using hjnz
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]
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' at hcall
rw [hloop1] at hcall
simp only [bind_tc_ok] at hcall
rw [hcapEval] at hcall
simp only [bind_tc_ok, lift] at hcall
let growthI : Std.I32 := UScalar.hcast .I32 growth
rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
have hgrowthI : 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
cases hpoint : d.decimal_point + growthI with
| fail e => simp [hpoint] at hcall
| div => simp [hpoint] at hcall
| ok point =>
rw [hpoint] at hcall
simp only [bind_tc_ok] at hcall
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := capped, decimal_point := point,
truncated := b1, digits := a1 }
have htrim :
num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
simpa [pre] using hcall
have hpointVal : point.val = d.decimal_point.val + growth.val := by
have hs := IScalar.add_equiv d.decimal_point growthI
rw [hpoint] at hs
rw [hgrowthI] at hs
omega
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, hcappedVal, a1.property]
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
let m := capped.val
let pref := fullDigits.take m
let suff := fullDigits.drop m
let E := F suff
have hmWidth : m <= fullDigits.length := by
simp [m, hcappedVal, hfullLen]
have hm768 : m <= 768 := by
simp [m, hcappedVal]
have hprefixDigits :
(a1.val.take m).map (fun z => z.val) = pref := by
apply List.ext_getElem
· simp [pref, hmWidth, m, a1.property, hm768]
· intro i hleft hright
have him : i < m := by
simpa [m, a1.property, hm768] using hleft
have hifull : i < fullDigits.length := lt_of_lt_of_le him hmWidth
have hi768 : i < 768 := lt_of_lt_of_le him hm768
have ha := hfullArray i hifull hi768
have haSome :
(a1.val.map (fun z : Std.U8 => z.val))[i]? =
some fullDigits[i] := by
rw [ha, List.getElem?_eq_getElem hifull]
have hmapBound : i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
simp [a1.property]
exact hi768
rw [show ((a1.val.take m).map (fun z => z.val))[i] =
(a1.val.map (fun z => z.val))[i] by simp]
rw [show pref[i] = fullDigits[i] by simp [pref]]
have hsome := List.getElem?_eq_getElem hmapBound
rw [haSome] at hsome
exact Option.some.inj hsome.symm
have hsuffixLen : suff.length = fullDigits.length - m := by
simp [suff]
have hfoldSplit : F fullDigits =
F pref * 10 ^ suff.length + E := by
conv_lhs => rw [← List.take_append_drop m fullDigits]
simp only [pref, suff, E, F, List.foldl_append]
rw [foldlFrom]
have hEpos : 0 < E <->
exists j, j < fullDigits.length /\ 768 <= j /\
fullDigits[j]? != some 0 := by
constructor
· intro hE
rcases (foldPos suff).mp hE with ⟨t, ht, htpos⟩
have hmLt : m < fullDigits.length := by
simp [suff] at ht
omega
have hmEq : m = 768 := by
simp [m, hcappedVal] at hmLt ⊢
omega
refine ⟨m + t, ?_, by omega, ?_⟩
· simp [suff] at ht
omega
· have hidx : suff[t] = fullDigits[m + t] := by
exact List.getElem_drop
have hbound : m + t < fullDigits.length := by
simp [suff] at ht
omega
have hsome := List.getElem?_eq_getElem hbound
rw [hsome]
simp only [bne_iff_ne]
intro hz
have hzval : fullDigits[m + t] = 0 := Option.some.inj hz
rw [← hidx] at hzval
omega
· rintro ⟨j, hj, hjcap, hjnz⟩
have hmEq : m = 768 := by
simp [m, hcappedVal, hfullLen]
omega
have ht : j - m < suff.length := by
simp [suff]
omega
apply (foldPos suff).mpr
refine ⟨j - m, ht, ?_⟩
have hidx : suff[j - m] = fullDigits[j] := by
rw [List.getElem_drop]
congr
omega
rw [hidx]
have hsome := List.getElem?_eq_getElem hj
by_contra hz
have hz' : fullDigits[j] = 0 := by omega
rw [hsome, hz'] at hjnz
simp at hjnz
let rho : Real := (E : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))
have hrho0 : 0 <= rho := by
dsimp only [rho]
exact mul_nonneg (Nat.cast_nonneg E)
(le_of_lt (zpow_pos (by norm_num : (0 : Real) < 10) _))
have hrhopos : 0 < rho <-> 0 < E := by
dsimp only [rho]
have hscale : 0 < (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) :=
zpow_pos (by norm_num) _
constructor
· intro hr
have hcast : 0 < (E : Real) := by nlinarith
exact_mod_cast hcast
· intro hE
have hcast : 0 < (E : Real) := by exact_mod_cast hE
exact mul_pos hcast hscale
have hsuffDigit : forall z, z ∈ suff -> z < 10 := by
intro z hz
exact hfullDigit z (List.mem_of_mem_drop hz)
have hEbound : E < 10 ^ suff.length := by
simpa [E] using foldBound suff hsuffDigit
have hrhoCell :
rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by
by_cases hEz : E = 0
· simp only [rho, hEz, Nat.cast_zero, zero_mul]
exact zpow_pos (by norm_num) _
· have hEposNat : 0 < E := Nat.pos_of_ne_zero hEz
rcases (foldPos suff).mp hEposNat with ⟨t, ht, _⟩
have hmLt : m < fullDigits.length := by
simp [suff] at ht
omega
have hmEq : m = 768 := by
simp [m, hcappedVal, hfullLen] at hmLt ⊢
omega
have hexpCell :
d.decimal_point.val - (d.num_digits.val : Int) +
(suff.length : Int) =
d'.decimal_point.val - 768 := by
rw [hpointEq]
simp only [pre]
rw [hpointVal, hsuffixLen, hfullLen, hwidthVal]
have hmBound : m <= d.num_digits.val + growth.val := by
rw [← hwidthVal, ← hfullLen]
exact hmWidth
rw [Nat.cast_sub hmBound, hmEq]
omega
have hcastBound :
(E : Real) < (10 : Real) ^ suff.length := by
exact_mod_cast hEbound
have hscalePos : 0 < (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) :=
zpow_pos (by norm_num) _
calc
rho = (E : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) := rfl
_ < (10 : Real) ^ suff.length * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) :=
mul_lt_mul_of_pos_right hcastBound hscalePos
_ = (10 : Real) ^ (d'.decimal_point.val - 768) := by
norm_num only [← zpow_natCast]
rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
rw [add_comm, hexpCell]
refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩
· rw [hvalueTrim]
simp only [pre, hpointVal, m] at hprefixDigits ⊢
rw [hprefixDigits]
change (F pref : Real) * (10 : Real) ^
(d.decimal_point.val + (growth.val : Int) -
(capped.val : Int)) = _
have hexp : d.decimal_point.val + (growth.val : Int) -
(m : Int) =
(d.decimal_point.val - (d.num_digits.val : Int)) +
(suff.length : Int) := by
rw [hsuffixLen, hfullLen, hwidthVal]
have hmbound : m <= d.num_digits.val + growth.val := by
rw [← hwidthVal, ← hfullLen]
exact hmWidth
rw [Nat.cast_sub hmbound]
omega
rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
norm_num only [zpow_natCast]
have hcastSplit : (B * I : Real) =
(F pref : Real) * (10 : Real) ^ suff.length + E := by
norm_cast
rw [← hfullFold]
exact hfoldSplit
have hBcast : (2 : Real) ^ shift.val = (B : Real) := by
norm_cast
have hINat :
List.foldl (fun acc z => 10 * acc + z) 0
(List.map (fun z : Std.U8 => z.val)
(List.take d.num_digits.val d.digits.val)) = I := by
rfl
rw [hBcast, hINat]
change (F pref : Real) *
((10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) *
(10 : Real) ^ suff.length) =
(B : Real) * ((I : Real) *
(10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))) - rho
let scale : Real := (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))
calc
(F pref : Real) * (scale * (10 : Real) ^ suff.length) =
((F pref : Real) * (10 : Real) ^ suff.length + E) *
scale - (E : Real) * scale := by ring
_ = ((B : Real) * I) * scale - (E : Real) * scale := by
rw [hcastSplit]
_ = (B : Real) * ((I : Real) * scale) - rho := by
dsimp only [rho, scale]
ring
· rw [htruncEq]
change b1 = true <-> _
rw [hstickyFull, hrhopos, hEpos]
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
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000
namespace dec2flt_model
-- [TABLET NODE: DecimalSeqLeftShiftRecordSemantics]
theorem DecimalSeqLeftShiftRecordSemantics :
let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
let digits :=
(d.digits.val.take d.num_digits.val).map (fun z => z.val)
let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
(integer : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))
forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
(shift : Std.Usize),
shift.val <= 60 ->
0 < value d ->
(forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' ->
exists rho : Real,
0 <= rho /\
rho < (10 : Real) ^ (d'.decimal_point.val - 768) /\
value d' = (2 : Real) ^ shift.val * value d - rho /\
(d'.truncated = true <->
d.truncated = true \/ 0 < rho) := by
-- BODY
dsimp only
intro d d' shift hshift hvalue hdigit hcall
let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
let F := fun ys : List Nat => ys.foldl (fun acc z => 10 * acc + z) 0
let I := F xs
have hxdigit : forall z, z ∈ xs -> z < 10 := by
intro z hz
simp only [xs, List.mem_map] at hz
rcases hz with ⟨u, hu, rfl⟩
exact hdigit u hu
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
have foldLowerAt (ys : List Nat) (j : Nat) (hj : j < ys.length)
(hjpos : 0 < ys[j]) : 10 ^ (ys.length - j - 1) <= F ys := by
have hprefix : ys[j] <= F (ys.take (j + 1)) := by
simp only [F]
rw [List.take_succ, List.foldl_append]
have hget : ys[j]? = some ys[j] :=
getElem?_eq_some_iff.mpr ⟨hj, rfl⟩
simp [hget]
have hdecomp : F ys =
F (ys.take (j + 1)) * 10 ^ (ys.length - j - 1) +
F (ys.drop (j + 1)) := by
conv_lhs => rw [← List.take_append_drop (j + 1) ys]
simp only [F, List.foldl_append]
rw [foldlFrom]
congr 2
simp
omega
rw [hdecomp]
have hm := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hprefix
have hone : 1 <= ys[j] := by omega
have hp := Nat.mul_le_mul_right (10 ^ (ys.length - j - 1)) hone
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
have foldPos (ys : List Nat) :
0 < F ys <-> exists j, exists hj : j < ys.length, 0 < ys[j] := by
induction ys with
| nil => simp [F]
| cons y ys ih =>
have hform : F (y :: ys) = y * 10 ^ ys.length + F ys := by
simpa [F] using foldlFrom ys y
rw [hform]
constructor
· intro hp
by_cases hy : 0 < y
· exact ⟨0, by simp, hy⟩
· have htail : 0 < F ys := by
have : y = 0 := by omega
simpa [this] using hp
rcases ih.mp htail with ⟨j, hj, hjpos⟩
exact ⟨j + 1, by simp; omega, by simpa using hjpos⟩
· rintro ⟨j, hj, hjpos⟩
cases j with
| zero =>
simp only [List.getElem_cons_zero] at hjpos
have hpow : 0 < 10 ^ ys.length := by positivity
nlinarith
| succ j =>
have hjlt : j < ys.length := by simp at hj; omega
have hp : 0 < F ys := ih.mpr ⟨j, hjlt, by simpa using hjpos⟩
omega
have zeroGrowth (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
(sh gr : Std.Usize) (hsh : sh.val = 0)
(hc : num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d0 sh = ok gr) : gr.val = 0 := by
as_aux_lemma =>
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, hlen, _, hzero, _⟩
have hz := hzero (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 z => z.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 hlen
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
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift at hc
simp only [hmask, lift, Bind.bind, Aeneas.Std.bind] at hc
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]))
rw [hxa] at hc
simp only [bind_tc_ok] at hc
have hxaEq : xa = x := by
apply UScalar.eq_of_val_eq
simpa [x, table] using congrArg UScalar.val hxaVal
subst xa
rw [hxaEq] at hc
obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := 0#usize) (y := 1#usize) (by scalar_tac))
rw [hnext] at hc
simp only [bind_tc_ok] at hc
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))
rw [hyb] at hc
simp only [bind_tc_ok] at hc
have hybEq : yb = y := by
apply UScalar.eq_of_val_eq
simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
subst yb
rw [hybEq] at hc
obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
rw [hhigh16] at hc
simp only [bind_tc_ok] at hc
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]
rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl] at hc
rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl] at hc
rw [show UScalar.cast .Usize high16 = numNew by rfl] at hc
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]
rw [hslice] at hc
simp only [bind_tc_ok] at hc
unfold core.slice.Slice.iter at hc
simp only [bind_tc_ok] at hc
unfold core.slice.iter.IteratorSliceIter.enumerate at hc
simp only [bind_tc_ok] at hc
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)
rw [hspan] at hc
simp only [bind_tc_ok] at hc
unfold core.iter.adapters.enumerate.IteratorEnumerate.take at hc
simp only [bind_tc_ok] at hc
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
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop at hc
rw [Aeneas.Std.loop.eq_def] at hc
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift_loop.body at hc
unfold core.iter.adapters.take.IteratorTake.next at hc
simp at hc
have hgrEq : gr = numNew := hc.symm
rw [hgrEq]
exact hnumNewVal
have hIpos : 0 < I := by
by_contra hn
have hIz : I = 0 := by omega
simp only [xs, I, F] at hIz
rw [hIz] at hvalue
simp at hvalue
have hnpos : 0 < d.num_digits.val := by
by_contra hn
have hnz : d.num_digits.val = 0 := by omega
simp [xs, I, F, hnz] at hIpos
have hnne : d.num_digits ≠ 0#usize := by
intro h
have : d.num_digits.val = 0 := by simpa [h]
omega
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift at hcall
simp only [hnne, if_false] at hcall
cases hgrowth :
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift d shift with
| fail e => simp [hgrowth] at hcall
| div => simp [hgrowth] at hcall
| ok growth =>
simp only [hgrowth, bind_tc_ok] at hcall
cases hadd : d.num_digits + growth with
| fail e => simp [hadd] at hcall
| div => simp [hadd] at hcall
| ok width =>
simp only [hadd, bind_tc_ok] at hcall
cases hloop0 :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0
d.truncated d.digits shift d.num_digits width 0#u64 with
| fail e => simp [hloop0] at hcall
| div => simp [hloop0] at hcall
| ok out0 =>
rcases out0 with ⟨b0, a0, cursor, carry⟩
simp only [hloop0, bind_tc_ok] at hcall
have hnle : d.num_digits.val <= 768 := by
by_contra hnle
have hbad : 768 < d.num_digits.val := by omega
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0 at hloop0
simp only [Bind.bind, Aeneas.Std.bind] at hloop0
rw [Aeneas.Std.loop.eq_def] at hloop0
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop0.body at hloop0
have hrne : d.num_digits != 0#usize := by
simp only [bne_iff_ne]
exact hnne
simp only [hrne, if_true] at hloop0
obtain ⟨read1, hread1, hread1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := d.num_digits) (y := 1#usize)
(by simpa using hnpos))
norm_num at hread1Val
rw [hread1] at hloop0
simp only [bind_tc_ok] at hloop0
cases hwrite : width - 1#usize with
| fail e => simp [hwrite] at hloop0
| div => simp [hwrite] at hloop0
| ok write1 =>
rw [hwrite] at hloop0
simp only [bind_tc_ok] at hloop0
have hread1bad : d.digits.val.length <= read1.val := by
have halen : d.digits.val.length = 768 := by
simpa using d.digits.property
rw [halen, hread1Val.1]
omega
have hnone : d.digits.val[read1.val]? = none :=
List.getElem?_eq_none hread1bad
unfold Array.index_usize at hloop0
simp [hnone] at hloop0
have hxslen : xs.length = d.num_digits.val := by
simp [xs, hnle, d.digits.property]
have hIbound : I < 10 ^ d.num_digits.val := by
simpa [I, hxslen] using foldBound xs hxdigit
cases hloop1 :
num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift_loop1
b0 a0 cursor carry with
| fail e => simp [hloop1] at hcall
| div => simp [hloop1] at hcall
| ok out1 =>
rcases out1 with ⟨b1, a1⟩
let B := 2 ^ shift.val
by_cases hk0 : shift.val = 0
· have hgrowthZero : growth.val = 0 :=
zeroGrowth d shift growth hk0 hgrowth
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)
rw [hadd] at hwidth'
cases hwidth'
have hwidthEq : width.val = d.num_digits.val := by
rw [hwidthVal, hgrowthZero]
simp
have hcursorInput : d.num_digits.val <= width.val := by
rw [hwidthEq]
obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
hproduct, hlowArray, houtside0, hsticky0⟩ :=
DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
shift d.num_digits width cursor carry hshift hnle hcursorInput
hdigit hloop0
have hcursorZero : cursor.val = 0 := by
rw [hwidthEq] at hcursorEq
omega
have hcarryZero : carry.val = 0 := by
rw [hk0] at hcarryB
norm_num at hcarryB
omega
have hcarryWidth : carry.val < 10 ^ cursor.val := by
rw [hcarryZero, hcursorZero]
norm_num
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 hj
rw [hcursorZero] at hj
omega
obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
hhighArray, houtside1, hsticky1⟩ :=
DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
hcarryWidth hpad hloop1
have hhighNil : highDigits = [] := by
apply List.eq_nil_of_length_eq_zero
rw [hhighLen, hcursorZero]
have hb10 : b1 = true <-> b0 = true := by
rw [hsticky1]
constructor
· intro h
rcases h with h | ⟨j, hj, _, _⟩
· exact h
· rw [hhighNil] at hj
simp at hj
· exact Or.inl
have hb0 : b0 = true <-> d.truncated = true := by
rw [hsticky0]
constructor
· intro h
rcases h with h | ⟨j, hj, hjcap, _⟩
· exact h
· rw [hlowLen] at hj
rw [hcursorZero] at hjcap
omega
· exact Or.inl
have hsticky : b1 = true <-> d.truncated = true := by
rw [hb10, hb0]
have hlowFold : F lowDigits = I := by
simpa [hk0, hcarryZero, F, I, xs] using hproduct.symm
have hlowArray1 : forall j, j < lowDigits.length -> j < 768 ->
(a1.val.map (fun z : Std.U8 => z.val))[j]? = lowDigits[j]? := by
intro j hj hjphys
have hout := houtside1 j hjphys (by
rw [hcursorZero]
exact Nat.zero_le j)
have hlo := hlowArray j hj (by
rw [hcursorZero]
simpa using hjphys)
rw [List.getElem?_map, hout]
simpa [hcursorZero] using hlo
have hwidthLe : width.val <= 768 := by
rw [hwidthEq]
exact hnle
have hcapFalse : ¬ 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 (show ¬ 768 < width.val by 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 width := by
simp [hcapFalse]
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' at hcall
rw [hloop1] at hcall
simp only [bind_tc_ok] at hcall
rw [hcapEval] at hcall
simp only [bind_tc_ok, lift] at hcall
let growthI : Std.I32 := UScalar.hcast .I32 growth
rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
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 hgrowthIZero : growthI.val = 0 := by
rw [hgrowthIVal, hgrowthZero]
norm_num
cases hpoint : d.decimal_point + growthI with
| fail e => simp [hpoint] at hcall
| div => simp [hpoint] at hcall
| ok point =>
rw [hpoint] at hcall
simp only [bind_tc_ok] at hcall
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := width, decimal_point := point,
truncated := b1, digits := a1 }
have htrim :
num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
simpa [pre] using hcall
have hpointVal : point.val = d.decimal_point.val := by
have hs := IScalar.add_equiv d.decimal_point growthI
rw [hpoint] at hs
rw [hgrowthIZero] at hs
omega
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, a1.property]
exact hwidthLe
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
have hpreDigits :
(a1.val.take width.val).map (fun z => z.val) = lowDigits := by
apply List.ext_getElem
· simp [hwidthEq, hlowLen, hnle, a1.property]
· intro i hleft hright
have hi : i < lowDigits.length := hright
have hi768 : i < 768 := by
rw [hlowLen] at hi
omega
have ha := hlowArray1 i hi hi768
have haSome :
(a1.val.map (fun z : Std.U8 => z.val))[i]? =
some lowDigits[i] := by
rw [ha, List.getElem?_eq_getElem hi]
have hmapBound :
i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
simp [a1.property]
exact hi768
rw [show ((a1.val.take width.val).map (fun z => z.val))[i] =
(a1.val.map (fun z => z.val))[i] by simp]
have hsome := List.getElem?_eq_getElem hmapBound
rw [haSome] at hsome
exact Option.some.inj hsome.symm
refine ⟨0, by norm_num, zpow_pos (by norm_num) _, ?_, ?_⟩
· rw [hvalueTrim]
simp only [pre, hpointVal]
rw [hpreDigits, hwidthEq]
change (F lowDigits : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) =
(2 : Real) ^ shift.val *
((I : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))) - 0
rw [hlowFold, hk0]
norm_num
· rw [htruncEq]
simpa using hsticky
· 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 ⟨_, _, _, _, _, hpositive⟩
have hp := hpositive (by simpa [kfin] using hkpos)
rcases hp with ⟨_, _, _, hsum, hlo, hhi⟩
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let x := table.val.get ⟨shift.val, by
have hlen : table.val.length = 65 := by simp [table]
omega⟩
let h := x.val / 2048
have hlo' : 10 ^ (h - 1) < B := by
simpa [h, x, table, kfin, B] using hlo
have hhi' : B < 10 ^ h := by
simpa [h, x, 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 h20 : 20 <= h := by omega
have hp19 : 10 ^ 19 <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) (by omega)
have hBle : B <= 2 ^ 60 := by
exact 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' hnle hdigit
rw [hgrowth] at hgrowth'
cases hgrowth'
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
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)
rw [hadd] at hwidth'
cases hwidth'
have hcursorInput : d.num_digits.val <= width.val := by
rw [hwidthVal]
omega
obtain ⟨lowDigits, hlowLen, hlowDigit, hcursorEq, hcarryB,
hproduct, hlowArray, houtside0, hsticky0⟩ :=
DecimalLeftShiftFirstLoopSemantics d.digits a0 d.truncated b0
shift d.num_digits width cursor carry hshift hnle hcursorInput
hdigit hloop0
have hcursorVal : cursor.val = growth.val := by
rw [hwidthVal] at hcursorEq
omega
have hprodWidth : B * I <
10 ^ (d.num_digits.val + growth.val) := by
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 hmul := lt_trans hmul1 hmul2
rw [Nat.mul_comm (10 ^ h), ← Nat.pow_add] at hmul
simpa using hmul
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 hcarryWidth : carry.val < 10 ^ cursor.val := by
rw [hcursorVal]
have hmul : carry.val * 10 ^ d.num_digits.val <
10 ^ growth.val * 10 ^ d.num_digits.val := by
have hcle : carry.val * 10 ^ d.num_digits.val <= B * I := by
rw [hproduct]
omega
rw [Nat.mul_comm (10 ^ growth.val) (10 ^ d.num_digits.val),
← Nat.pow_add]
exact lt_of_le_of_lt hcle hprodWidth
exact Nat.lt_of_mul_lt_mul_right hmul
have hpad : forall j, j < cursor.val -> j < 768 ->
carry.val < 10 ^ (cursor.val - j - 1) ->
a0.val[j]? = some 0#u8 := by
as_aux_lemma =>
intro j hjcursor hjphys hjcarry
have hbase : a0.val[j]? = d.digits.val[j]? :=
houtside0 j hjphys (Or.inl hjcursor)
rw [hgrowthFormula] at hcursorVal
split at hcursorVal
next ht =>
have hexp : cursor.val - j - 1 <= h - 1 := by omega
have hpow : 10 ^ (cursor.val - j - 1) <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) hexp
have hcTop : carry.val < 10 ^ (h - 1) :=
lt_of_lt_of_le hjcarry hpow
have hu := decimalUpper carry.val (F lowDigits)
(10 ^ (h - 1)) d.num_digits.val hcTop (by
simpa [hlowLen] using foldBound lowDigits hlowDigit)
rw [← hproduct] at hu
rw [Nat.mul_comm (10 ^ (h - 1)), ← Nat.pow_add] at hu
change B * I < 10 ^ (d.num_digits.val + (h - 1)) at hu
have hthreshold : 10 ^ (d.num_digits.val + h - 1) <= B * I := by
simpa [Nat.mul_comm] using ht
have he : d.num_digits.val + (h - 1) =
d.num_digits.val + h - 1 := by omega
rw [he] at hu
exact False.elim ((not_lt_of_ge hthreshold) hu)
next ht =>
have hcursorGrowth : cursor.val = h - 1 := hcursorVal
have hlowBound : F lowDigits < 10 ^ d.num_digits.val := by
simpa [hlowLen] using foldBound lowDigits hlowDigit
have hu := decimalUpper carry.val (F lowDigits)
(10 ^ (cursor.val - j - 1)) d.num_digits.val
hjcarry hlowBound
rw [← hproduct] at hu
rw [Nat.mul_comm (10 ^ (cursor.val - j - 1)),
← Nat.pow_add] at hu
change B * I <
10 ^ (d.num_digits.val + (cursor.val - j - 1)) at hu
have hjN : j < d.num_digits.val := by
by_contra hjN
have hNle : d.num_digits.val <= j := by omega
have hexp : d.num_digits.val + (cursor.val - j - 1) <=
cursor.val - 1 := by omega
have hpow : 10 ^ (d.num_digits.val + (cursor.val - j - 1)) <=
10 ^ (cursor.val - 1) :=
Nat.pow_le_pow_right (by omega) hexp
have hcursorPos : 0 < cursor.val := by omega
have hpowStep : 10 ^ (cursor.val - 1) < 10 ^ cursor.val := by
apply Nat.pow_lt_pow_right (by omega)
omega
have hBLower : 10 ^ cursor.val < B := by
simpa [hcursorGrowth] using hlo'
have hBI : B <= B * I := by
have hm := Nat.mul_le_mul_left B (show 1 <= I by omega)
simpa using hm
exact (not_lt_of_ge hBI)
(lt_of_lt_of_le hu (le_trans hpow
(le_trans (Nat.le_of_lt hpowStep) (Nat.le_of_lt hBLower))))
have hcellBound : j < d.digits.val.length := by
have hlen : d.digits.val.length = 768 := by
simpa using d.digits.property
rw [hlen]
exact hjphys
have hxval : xs[j] = (d.digits.val[j]'hcellBound).val := by
simp [xs, hjN]
have hxzero : xs[j] = 0 := by
by_contra hxzero
have hxpos : 0 < xs[j] := by omega
have hIlower := foldLowerAt xs j (by simpa [hxslen] using hjN) hxpos
have hIlower' : 10 ^ (d.num_digits.val - j - 1) <= I := by
simpa [hxslen, I] using hIlower
have hBLower : 10 ^ cursor.val < B := by
simpa [hcursorGrowth] using hlo'
have hmul1 : 10 ^ cursor.val *
10 ^ (d.num_digits.val - j - 1) <
B * 10 ^ (d.num_digits.val - j - 1) :=
(Nat.mul_lt_mul_right (by positivity)).mpr hBLower
have hmul2 : B * 10 ^ (d.num_digits.val - j - 1) <= B * I :=
Nat.mul_le_mul_left B hIlower'
have hlower : 10 ^ (cursor.val +
(d.num_digits.val - j - 1)) < B * I := by
rw [Nat.pow_add]
exact lt_of_lt_of_le hmul1 hmul2
have he : cursor.val + (d.num_digits.val - j - 1) =
d.num_digits.val + (cursor.val - j - 1) := by omega
rw [he] at hlower
exact False.elim ((not_lt_of_ge (Nat.le_of_lt hlower)) hu)
have hdigitZero : d.digits.val[j]'hcellBound = 0#u8 := by
apply UScalar.eq_of_val_eq
simpa [← hxval, hxzero]
have hbaseZero : d.digits.val[j]? = some 0#u8 := by
apply getElem?_eq_some_iff.mpr
exact ⟨hcellBound, hdigitZero⟩
simpa [hbase] using hbaseZero
obtain ⟨highDigits, hhighLen, hhighDigit, hcarryDigits,
hhighArray, houtside1, hsticky1⟩ :=
DecimalLeftShiftSecondLoopSemantics a0 a1 b0 b1 cursor carry
hcarryWidth hpad hloop1
let fullDigits := highDigits ++ lowDigits
have hfullLen : fullDigits.length = width.val := by
simp [fullDigits, hhighLen, hlowLen, hcursorEq]
have hfullDigit : forall z, z ∈ fullDigits -> z < 10 := by
intro z hz
simp only [fullDigits, List.mem_append] at hz
exact hz.elim (hhighDigit z) (hlowDigit z)
have hfullFold : F fullDigits = B * I := by
have happ : F fullDigits =
carry.val * 10 ^ lowDigits.length + F lowDigits := by
simp only [fullDigits, F, List.foldl_append]
rw [foldlFrom, hcarryDigits]
rw [happ, hlowLen]
simpa [B, I, F, xs] using hproduct.symm
have hfullArray : forall j, j < fullDigits.length -> j < 768 ->
(a1.val.map (fun z : Std.U8 => z.val))[j]? = fullDigits[j]? := by
intro j hj hjphys
by_cases hjhigh : j < cursor.val
· have hh := hhighArray j (by simpa [hhighLen] using hjhigh) hjphys
rw [List.getElem?_append_left (by simpa [fullDigits, hhighLen] using hjhigh)]
simpa [fullDigits] using hh
· have hcj : cursor.val <= j := by omega
let t := j - cursor.val
have ht : t < lowDigits.length := by
simp [fullDigits, hhighLen, hlowLen, hfullLen] at hj
simp [t]
omega
have hidx : cursor.val + t = j := by simp [t, hcj]
have hout := houtside1 j hjphys hcj
have hlo := hlowArray t ht (by omega)
rw [List.getElem?_map, hout]
rw [List.getElem?_append_right (by
simpa [fullDigits, hhighLen] using hjhigh)]
simpa [fullDigits, hhighLen, t, hidx] using hlo
have hb10 : b1 = true <-> b0 = true := by
rw [hsticky1]
constructor
· intro h
rcases h with h | ⟨j, hj, hjcap, _⟩
· exact h
· rw [hhighLen, hcursorVal] at hj
omega
· exact Or.inl
have hstickyFull : b1 = true <-> d.truncated = true \/
exists j, j < fullDigits.length /\ 768 <= j /\
fullDigits[j]? != some 0 := by
rw [hb10, hsticky0]
constructor
· intro h
rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hd
· right
refine ⟨cursor.val + j, ?_, hjcap, ?_⟩
· simp [fullDigits, hhighLen, hlowLen]
omega
· rw [List.getElem?_append_right (by simp [hhighLen])]
simpa [hhighLen] using hjnz
· intro h
rcases h with hd | ⟨j, hj, hjcap, hjnz⟩
· exact Or.inl hd
· right
have hcj : cursor.val <= j := by
by_contra hcj
rw [List.getElem?_append_left (by
simpa [fullDigits, hhighLen] using hcj)] at hjnz
have hjlt : j < highDigits.length := by simpa [hhighLen] using hcj
have hjcap' : j < 768 := by omega
have hz := hhighDigit highDigits[j] (List.getElem_mem _)
omega
refine ⟨j - cursor.val, ?_, ?_, ?_⟩
· simp [fullDigits, hhighLen] at hj
omega
· omega
· rw [List.getElem?_append_right (by
simpa [fullDigits, hhighLen] using hcj)] at hjnz
simpa [hhighLen] using hjnz
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]
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' at hcall
rw [hloop1] at hcall
simp only [bind_tc_ok] at hcall
rw [hcapEval] at hcall
simp only [bind_tc_ok, lift] at hcall
let growthI : Std.I32 := UScalar.hcast .I32 growth
rw [show UScalar.hcast .I32 growth = growthI by rfl] at hcall
have hgrowthI : 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
cases hpoint : d.decimal_point + growthI with
| fail e => simp [hpoint] at hcall
| div => simp [hpoint] at hcall
| ok point =>
rw [hpoint] at hcall
simp only [bind_tc_ok] at hcall
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := capped, decimal_point := point,
truncated := b1, digits := a1 }
have htrim :
num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
simpa [pre] using hcall
have hpointVal : point.val = d.decimal_point.val + growth.val := by
have hs := IScalar.add_equiv d.decimal_point growthI
rw [hpoint] at hs
rw [hgrowthI] at hs
omega
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, hcappedVal, a1.property]
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, _, _, _, hvalueTrim⟩
let m := capped.val
let pref := fullDigits.take m
let suff := fullDigits.drop m
let E := F suff
have hmWidth : m <= fullDigits.length := by
simp [m, hcappedVal, hfullLen]
have hm768 : m <= 768 := by
simp [m, hcappedVal]
have hprefixDigits :
(a1.val.take m).map (fun z => z.val) = pref := by
apply List.ext_getElem
· simp [pref, hmWidth, m, a1.property, hm768]
· intro i hleft hright
have him : i < m := by
simpa [m, a1.property, hm768] using hleft
have hifull : i < fullDigits.length := lt_of_lt_of_le him hmWidth
have hi768 : i < 768 := lt_of_lt_of_le him hm768
have ha := hfullArray i hifull hi768
have haSome :
(a1.val.map (fun z : Std.U8 => z.val))[i]? =
some fullDigits[i] := by
rw [ha, List.getElem?_eq_getElem hifull]
have hmapBound : i < (a1.val.map (fun z : Std.U8 => z.val)).length := by
simp [a1.property]
exact hi768
rw [show ((a1.val.take m).map (fun z => z.val))[i] =
(a1.val.map (fun z => z.val))[i] by simp]
rw [show pref[i] = fullDigits[i] by simp [pref]]
have hsome := List.getElem?_eq_getElem hmapBound
rw [haSome] at hsome
exact Option.some.inj hsome.symm
have hsuffixLen : suff.length = fullDigits.length - m := by
simp [suff]
have hfoldSplit : F fullDigits =
F pref * 10 ^ suff.length + E := by
conv_lhs => rw [← List.take_append_drop m fullDigits]
simp only [pref, suff, E, F, List.foldl_append]
rw [foldlFrom]
have hEpos : 0 < E <->
exists j, j < fullDigits.length /\ 768 <= j /\
fullDigits[j]? != some 0 := by
constructor
· intro hE
rcases (foldPos suff).mp hE with ⟨t, ht, htpos⟩
have hmLt : m < fullDigits.length := by
simp [suff] at ht
omega
have hmEq : m = 768 := by
simp [m, hcappedVal] at hmLt ⊢
omega
refine ⟨m + t, ?_, by omega, ?_⟩
· simp [suff] at ht
omega
· have hidx : suff[t] = fullDigits[m + t] := by
exact List.getElem_drop
have hbound : m + t < fullDigits.length := by
simp [suff] at ht
omega
have hsome := List.getElem?_eq_getElem hbound
rw [hsome]
simp only [bne_iff_ne]
intro hz
have hzval : fullDigits[m + t] = 0 := Option.some.inj hz
rw [← hidx] at hzval
omega
· rintro ⟨j, hj, hjcap, hjnz⟩
have hmEq : m = 768 := by
simp [m, hcappedVal, hfullLen]
omega
have ht : j - m < suff.length := by
simp [suff]
omega
apply (foldPos suff).mpr
refine ⟨j - m, ht, ?_⟩
have hidx : suff[j - m] = fullDigits[j] := by
rw [List.getElem_drop]
congr
omega
rw [hidx]
have hsome := List.getElem?_eq_getElem hj
by_contra hz
have hz' : fullDigits[j] = 0 := by omega
rw [hsome, hz'] at hjnz
simp at hjnz
let rho : Real := (E : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))
have hrho0 : 0 <= rho := by
dsimp only [rho]
exact mul_nonneg (Nat.cast_nonneg E)
(le_of_lt (zpow_pos (by norm_num : (0 : Real) < 10) _))
have hrhopos : 0 < rho <-> 0 < E := by
dsimp only [rho]
have hscale : 0 < (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) :=
zpow_pos (by norm_num) _
constructor
· intro hr
have hcast : 0 < (E : Real) := by nlinarith
exact_mod_cast hcast
· intro hE
have hcast : 0 < (E : Real) := by exact_mod_cast hE
exact mul_pos hcast hscale
have hsuffDigit : forall z, z ∈ suff -> z < 10 := by
intro z hz
exact hfullDigit z (List.mem_of_mem_drop hz)
have hEbound : E < 10 ^ suff.length := by
simpa [E] using foldBound suff hsuffDigit
have hrhoCell :
rho < (10 : Real) ^ (d'.decimal_point.val - 768) := by
by_cases hEz : E = 0
· simp only [rho, hEz, Nat.cast_zero, zero_mul]
exact zpow_pos (by norm_num) _
· have hEposNat : 0 < E := Nat.pos_of_ne_zero hEz
rcases (foldPos suff).mp hEposNat with ⟨t, ht, _⟩
have hmLt : m < fullDigits.length := by
simp [suff] at ht
omega
have hmEq : m = 768 := by
simp [m, hcappedVal, hfullLen] at hmLt ⊢
omega
have hexpCell :
d.decimal_point.val - (d.num_digits.val : Int) +
(suff.length : Int) =
d'.decimal_point.val - 768 := by
rw [hpointEq]
simp only [pre]
rw [hpointVal, hsuffixLen, hfullLen, hwidthVal]
have hmBound : m <= d.num_digits.val + growth.val := by
rw [← hwidthVal, ← hfullLen]
exact hmWidth
rw [Nat.cast_sub hmBound, hmEq]
omega
have hcastBound :
(E : Real) < (10 : Real) ^ suff.length := by
exact_mod_cast hEbound
have hscalePos : 0 < (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) :=
zpow_pos (by norm_num) _
calc
rho = (E : Real) * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) := rfl
_ < (10 : Real) ^ suff.length * (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) :=
mul_lt_mul_of_pos_right hcastBound hscalePos
_ = (10 : Real) ^ (d'.decimal_point.val - 768) := by
norm_num only [← zpow_natCast]
rw [← zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
rw [add_comm, hexpCell]
refine ⟨rho, hrho0, hrhoCell, ?_, ?_⟩
· rw [hvalueTrim]
simp only [pre, hpointVal, m] at hprefixDigits ⊢
rw [hprefixDigits]
change (F pref : Real) * (10 : Real) ^
(d.decimal_point.val + (growth.val : Int) -
(capped.val : Int)) = _
have hexp : d.decimal_point.val + (growth.val : Int) -
(m : Int) =
(d.decimal_point.val - (d.num_digits.val : Int)) +
(suff.length : Int) := by
rw [hsuffixLen, hfullLen, hwidthVal]
have hmbound : m <= d.num_digits.val + growth.val := by
rw [← hwidthVal, ← hfullLen]
exact hmWidth
rw [Nat.cast_sub hmbound]
omega
rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
norm_num only [zpow_natCast]
have hcastSplit : (B * I : Real) =
(F pref : Real) * (10 : Real) ^ suff.length + E := by
norm_cast
rw [← hfullFold]
exact hfoldSplit
have hBcast : (2 : Real) ^ shift.val = (B : Real) := by
norm_cast
have hINat :
List.foldl (fun acc z => 10 * acc + z) 0
(List.map (fun z : Std.U8 => z.val)
(List.take d.num_digits.val d.digits.val)) = I := by
rfl
rw [hBcast, hINat]
change (F pref : Real) *
((10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int)) *
(10 : Real) ^ suff.length) =
(B : Real) * ((I : Real) *
(10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))) - rho
let scale : Real := (10 : Real) ^
(d.decimal_point.val - (d.num_digits.val : Int))
calc
(F pref : Real) * (scale * (10 : Real) ^ suff.length) =
((F pref : Real) * (10 : Real) ^ suff.length + E) *
scale - (E : Real) * scale := by ring
_ = ((B : Real) * I) * scale - (E : Real) * scale := by
rw [hcastSplit]
_ = (B : Real) * ((I : Real) * scale) - rho := by
dsimp only [rho, scale]
ring
· rw [htruncEq]
change b1 = true <-> _
rw [hstickyFull, hrhopos, hEpos]
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftFirstLoopSemantics
- DecimalLeftShiftSecondLoopSemantics
- DecimalLeftShiftTableCertificate
- DecimalSeqTrimSemantics
- num imp dec2flt decimal seq DecimalSeq
All 9 entries
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftFirstLoopSemantics
- DecimalLeftShiftSecondLoopSemantics
- DecimalLeftShiftTableCertificate
- DecimalSeqTrimSemantics
- 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 number of digits decimal left shift
Inspect related source (116)
- DecimalSeq · dec2flt/decimal_seq.rs:21 Program Rust
- MAX_DIGITS · dec2flt/decimal_seq.rs:63 Program Rust
- left_shift · dec2flt/decimal_seq.rs:129 Program Rust
More program dependencies (83)
- left_shift_loop0 · dec2flt/decimal_seq.rs:138 Program Rust
- body · dec2flt/decimal_seq.rs:138 Program Rust
- left_shift_loop1 · dec2flt/decimal_seq.rs:152 Program Rust
- body · dec2flt/decimal_seq.rs:152 Program Rust
- number_of_digits_decimal_left_shift · dec2flt/decimal_seq.rs:368 Program Rust
- number_of_digits_decimal_left_shift_loop · dec2flt/decimal_seq.rs:434 Program Rust
- body · dec2flt/decimal_seq.rs:434 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
- TABLE · dec2flt/decimal_seq.rs:370 Program Rust
- TABLE_POW5 · dec2flt/decimal_seq.rs:379 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
- DecimalLeftShiftLoopLex
- DecimalLeftShiftSecondLoopSemantics
- DecimalLeftShiftTableCertificate
All 16 entries
- DecimalDigitLexScale
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftFirstLoopSemantics
- DecimalLeftShiftLoopLex
- DecimalLeftShiftSecondLoopSemantics
- DecimalLeftShiftTableCertificate
- DecimalSeqLeftShiftRecordSemantics
- 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": "DecimalSeqLeftShiftRecordSemantics",
"closure_version": "patch_c_v1",
"active_decl_hash": "1f6315c09e604970d1b604e64fdeac8313f80c9b7f7b5b3779b57bc3bc99b5f9",
"active_statement_hash": "defee60c1ef0b5a2e318b78cb75d4781cf348a6fa5df296307c10f6fcc6b0cc4",
"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-188",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalLeftShiftDigitGrowth": "6dcf8858df4713eb",
"DecimalLeftShiftFirstLoopSemantics": "d126fe9b83dfc5f3",
"DecimalLeftShiftSecondLoopSemantics": "ccbfee7a0aef7445",
"DecimalLeftShiftTableCertificate": "3c85f76c60cebc6c",
"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_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": "bab51f6bda8660d2",
"num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061",
"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": "differs from current fingerprint",
"assessment_fingerprint": "sound-v2:f98e9e7e3f4420d74ca64b8fe01332bccc3246a862930fcb9a68025c96cfd58f",
"current_fingerprint": "sound-v2:2bac68c5d923d2769ce7860e7a3445f63fc800805e0a9f57a5d44f68f59d3976",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}