Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Normal right shift on the loop-zero 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.DecimalRightShiftFinalLoopSemantics
import Tablet.DecimalRightShiftMainLoopSemantics
import Tablet.DecimalRightShiftPrefixLoopSemantics
import Tablet.DecimalSeqTrimSemantics
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: DecimalRightShiftLoop0Execution]
theorem DecimalRightShiftLoop0Execution :
forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize),
shift.val <= 60 ->
0 <= d.decimal_point.val -> d.decimal_point.val < 310 ->
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 (read : Std.Usize) (acc : Std.U64)
(d' : num.imp.dec2flt.decimal_seq.DecimalSeq),
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
d shift 0#usize 0#u64 false = ok (d, read, acc, false) /\
1 <= read.val /\ read.val <= d.num_digits.val + shift.val /\
2 ^ shift.val <= acc.val /\ acc.val < 10 * 2 ^ shift.val /\
(let prospective : Int :=
d.decimal_point.val - (read.val : Int) + 1
prospective >= -2047 /\ prospective <= 309 /\
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' /\
d'.decimal_point.val = prospective /\ 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
set_option maxHeartbeats 1600000 in
intro d shift hshift hpointLo hpointHi hinput
rcases hinput with ⟨hnum, hdigit, z, zs, hshape, hzlo, hzhi, hlen⟩
let xs := d.digits.val.map (fun z => z.val)
let F := fun r : Nat =>
(xs.take r).foldl (fun n z => 10 * n + z) 0
let I := F d.num_digits.val
let B := 2 ^ shift.val
have hBpos : 0 < B := by simp [B]
have hBle : B <= 2 ^ 60 := Nat.pow_le_pow_right (by omega) hshift
have hshift64 : shift.val < 64 := by omega
have hIpos : 0 < I := by
have hrest (ys : List Nat) (a : Nat) :
a <= ys.foldl (fun n digit => 10 * n + digit) a := by
induction ys generalizing a with
| nil => simp
| cons b bs ih =>
simp only [List.foldl_cons]
have hi := ih (10 * a + b)
omega
have hm := hrest zs z
have hzpos : 0 < z := by omega
have hp : 0 < (z :: zs).foldl (fun n digit => 10 * n + digit) 0 := by
simp only [List.foldl_cons]
exact lt_of_lt_of_le hzpos (by simpa using hm)
have heq := congrArg
(fun ys => ys.foldl (fun n digit => 10 * n + digit) 0) hshape
have heq' : I = (z :: zs).foldl
(fun n digit => 10 * n + digit) 0 := by
simpa [I, F, xs, List.map_take] using heq
rw [heq']
exact hp
have nestedSpec (start : Std.Usize) (c0 : Std.U64)
(hstart : start.val <= 768)
(hc0pos : 0 < c0.val) (hc0B : c0.val < B) :
Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
shift start c0) (fun _ => True) := by
let Inv := fun p : Std.Usize × Std.U64 =>
start.val <= p.1.val /\
p.1.val <= start.val + shift.val /\
0 < p.2.val /\ p.2.val < 10 * B /\
p.2.val = c0.val * 10 ^ (p.1.val - start.val)
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
apply Aeneas.Std.loop.spec_decr_nat
(fun p : Std.Usize × Std.U64 => start.val + shift.val - p.1.val)
Inv (fun _ => True)
(fun p =>
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
shift p.1 p.2) (start, c0)
· intro p hp
rcases p with ⟨r, n⟩
simp only [Inv, Prod.fst, Prod.snd] at hp
rcases hp with ⟨hsr, hrs, hnpos, hn10B, hnform⟩
simp only [Prod.fst, Prod.snd]
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok]
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqSpec.1
by_cases hq0 : q.val = 0
· have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
have hnB : n.val < B := by
rw [hqVal] at hq0
exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
simp only [hqEq, if_true]
have htlt : r.val - start.val < shift.val := by
by_contra hnot
have hkle : shift.val <= r.val - start.val := by omega
have hpowBase : 2 ^ shift.val <= 10 ^ shift.val :=
Nat.pow_le_pow_left (by omega) _
have hpowExp : 10 ^ shift.val <= 10 ^ (r.val - start.val) :=
Nat.pow_le_pow_right (by omega) hkle
have hc01 : 1 <= c0.val := by omega
have hmul : 10 ^ (r.val - start.val) <=
c0.val * 10 ^ (r.val - start.val) := by nlinarith
have hcontra : B <= n.val := by
rw [hnform]
exact le_trans hpowBase (le_trans hpowExp hmul)
omega
have hmulBound : n.val * 10 <= U64.max := by
have hlt : n.val * 10 < 2 ^ 64 := by calc
n.val * 10 < B * 10 := Nat.mul_lt_mul_of_pos_right hnB (by omega)
_ <= 2 ^ 60 * 10 := Nat.mul_le_mul_right 10 hBle
_ < 2 ^ 64 := by norm_num
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨n1, hn1, hn1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := n) (y := 10#u64) (by simpa using hmulBound))
rw [hn1]
simp only [bind_tc_ok]
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1]
have hr1Val : r1.val = r.val + 1 := by simpa using hr1Spec
have hn1Val : n1.val = n.val * 10 := by simpa using hn1Spec
constructor
· simp only [Inv, Prod.fst, Prod.snd]
refine ⟨by omega, by omega, by omega, ?_, ?_⟩
· rw [hn1Val]
omega
· rw [hn1Val, hnform, hr1Val]
have hsub : r.val + 1 - start.val =
(r.val - start.val) + 1 := by omega
rw [hsub, pow_succ]
ring
· dsimp only
omega
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp [hqne]
· simp only [Inv, Prod.fst, Prod.snd]
refine ⟨le_rfl, by omega, hc0pos, by omega, ?_⟩
norm_num
let Inv := fun p : Std.Usize × Std.U64 × Bool =>
p.2.2 = false /\ p.1.val <= d.num_digits.val /\ p.1.val <= 768 /\
p.2.1.val = F p.1.val /\ p.2.1.val < 10 * B
have hprefixSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
d shift 0#usize 0#u64 false) (fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
apply Aeneas.Std.loop.spec_decr_nat
(fun p : Std.Usize × Std.U64 × Bool => d.num_digits.val - p.1.val)
Inv (fun _ => True)
(fun p => num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
d shift p.1 p.2.1 p.2.2) (0#usize, 0#u64, false)
· intro p hp
rcases p with ⟨r, n, az⟩
dsimp only at hp ⊢
simp only [Inv] at hp
rcases hp with ⟨haz, hrn, hrcap, hnF, hn10⟩
rw [haz]
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok]
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqSpec.1
by_cases hq0 : q.val = 0
· have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
have hnB : n.val < B := by
rw [hqVal] at hq0
exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
simp only [hqEq, if_true, Bool.false_eq_true, if_false]
by_cases hrlt : r.val < d.num_digits.val
· have hrWord : r < d.num_digits := by
simpa [UScalar.lt_equiv] using hrlt
simp only [hrWord, if_true]
have hmulBound : 10 * n.val <= U64.max := by
have hlt : 10 * n.val < 2 ^ 64 := by calc
10 * n.val < 10 * B := Nat.mul_lt_mul_of_pos_left hnB (by omega)
_ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
_ < 2 ^ 64 := by norm_num
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨tenn, htenn, htennSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := n) (by simpa using hmulBound))
rw [htenn]
simp only [bind_tc_ok]
have hrArray : r.val < d.digits.val.length := by
have ha : d.digits.val.length = 768 := by simpa using d.digits.property
omega
obtain ⟨digit, hdigitRead, hdigitReadSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits r
(by simpa [Array.length] using hrArray))
rw [hdigitRead]
simp only [bind_tc_ok, lift]
let digit64 : Std.U64 := UScalar.cast .U64 digit
rw [show UScalar.cast .U64 digit = digit64 by rfl]
have hdigit64 : digit64.val = digit.val := by
simp [digit64, UScalar.cast_val_eq]
have htakeLen : r.val < (d.digits.val.take d.num_digits.val).length := by
rw [List.length_take]
exact lt_min hrlt hrArray
have htakeVal :
(d.digits.val.take d.num_digits.val)[r.val]'htakeLen = digit := by
have horig : d.digits.val[r.val]'hrArray = digit := hdigitReadSpec.symm
simpa using horig
have hdigitMem : digit ∈ d.digits.val.take d.num_digits.val := by
rw [← htakeVal]
exact List.getElem_mem _
have hdigitLt : digit.val < 10 := hdigit digit hdigitMem
have haddBound : tenn.val + digit64.val <= U64.max := by
have htennVal : tenn.val = 10 * n.val := by simpa using htennSpec
rw [htennVal, hdigit64]
norm_num [U64.max, U64.numBits]
omega
obtain ⟨n1, hn1, hn1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := tenn) (y := digit64) haddBound)
rw [hn1]
simp only [bind_tc_ok]
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1]
have hr1Val : r1.val = r.val + 1 := by simpa using hr1Spec
have htennVal : tenn.val = 10 * n.val := by simpa using htennSpec
have hn1Val : n1.val = 10 * n.val + digit.val := by
rw [hn1Spec, htennVal, hdigit64]
have hxsGet : xs[r.val]? = some digit.val := by
simp only [xs, List.getElem?_map]
have hsome : d.digits.val[r.val]? = some digit :=
getElem?_eq_some_iff.mpr ⟨hrArray, hdigitReadSpec.symm⟩
rw [hsome]
rfl
have hFstep : F (r.val + 1) = 10 * F r.val + digit.val := by
simp only [F]
rw [List.take_succ, List.foldl_append]
simp [hxsGet]
constructor
· simp only [Inv, Prod.fst, Prod.snd, true_and]
refine ⟨by omega, by omega, ?_, ?_⟩
· rw [hr1Val, hFstep, hn1Val, hnF]
· rw [hn1Val]
omega
· dsimp only
omega
· have hrEq : r.val = d.num_digits.val := by omega
have hrNotWord : ¬ r < d.num_digits := by
simpa [UScalar.lt_equiv] using hrlt
simp only [hrNotWord, if_false]
by_cases hn0 : n.val = 0
· have hnEq : n = 0#u64 := UScalar.eq_of_val_eq (by simpa using hn0)
simp only [hnEq, if_true]
exfalso
apply (Nat.ne_of_gt hIpos)
calc
I = F r.val := by simp [I, hrEq]
_ = n.val := hnF.symm
_ = 0 := hn0
· have hnNe : ¬ n = 0#u64 := by
intro heq
apply hn0
exact congrArg UScalar.val heq
simp only [hnNe, if_false]
obtain ⟨pair, hpair, _⟩ := Aeneas.Std.WP.spec_imp_exists
(nestedSpec r n hrcap (by omega) hnB)
rcases pair with ⟨r1, n1⟩
rw [hpair]
simp
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp [hqne, WP.spec_ok]
· simp [Inv, F]
exact hBpos
obtain ⟨out0, hloop0, _⟩ := Aeneas.Std.WP.spec_imp_exists hprefixSpec
rcases out0 with ⟨d0, read, acc, allZero⟩
have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc allZero
hshift (by simpa [I, F, xs, List.map_take] using hIpos) hdigit hloop0
dsimp only at hpref
rcases hpref with ⟨hd0, haz0, hreadpos, hBacc, hacc10, hphase⟩
subst d0
subst allZero
have hreadBound : read.val <= d.num_digits.val + shift.val := by
rcases hphase with hp | hp <;> omega
have hprosLo : -2047 <= d.decimal_point.val - (read.val : Int) + 1 := by
omega
have hprosHi : d.decimal_point.val - (read.val : Int) + 1 <= 309 := by
omega
have hread828 : read.val <= 828 := by omega
let readI : Std.I32 := UScalar.hcast .I32 read
have hreadIVal : readI.val = (read.val : Int) := by
have hs := UScalar.hcast_inBounds_spec .I32 read (by
rw [IScalar.max_I32_eq]
omega)
simp only [lift, WP.spec_ok] at hs
exact hs
obtain ⟨readMinus, hsub, hsubSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := readI) (y := 1#i32) (by
rw [hreadIVal]
scalar_tac) (by
rw [hreadIVal]
scalar_tac))
have hsubVal : readMinus.val = readI.val - 1 := by
simpa using hsubSpec
obtain ⟨point, hpoint, hpointSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := d.decimal_point) (y := readMinus) (by
rw [hsubVal, hreadIVal, IScalar.min_I32_eq]
omega) (by
rw [hsubVal, hreadIVal, IScalar.max_I32_eq]
omega))
have hpointVal : point.val = d.decimal_point.val - (read.val : Int) + 1 := by
rw [hpointSpec, hsubVal, hreadIVal]
omega
let negRange : Std.I32 := (-2047)#i32
have hnegRange :
-. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE =
ok negRange := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
change IScalar.neg (2047#i32 : Std.I32) = _
unfold IScalar.neg IScalar.tryMk IScalar.tryMkOpt
norm_num [IScalar.check_bounds, IScalarTy.numBits, negRange]
have hnegRangeVal : negRange.val = -2047 := by rfl
have hnotReset : ¬ point < negRange := by
simpa [UScalar.lt_equiv, hpointVal, hnegRangeVal] using hprosLo
obtain ⟨pow, hpow, hpowSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftLeft_spec 1#u64 shift (2 ^ 64) (by
norm_num [U64.numBits]
exact hshift64) (by norm_num [U64.size_eq]))
have hpowVal : pow.val = B := by
have hpLt : 2 ^ shift.val < 2 ^ 64 :=
Nat.pow_lt_pow_right (by omega) hshift64
norm_num [Nat.shiftLeft_eq] at hpowSpec
rw [show 18446744073709551616 = 2 ^ 64 by norm_num,
Nat.mod_eq_of_lt hpLt] at hpowSpec
simpa [B] using hpowSpec.1
obtain ⟨mask, hmask, hmaskSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := pow) (y := 1#u64) (by
rw [hpowVal]
exact hBpos))
have hmainExists : exists (a1 : Array Std.U8 768#usize)
(write1 : Std.Usize) (acc1 : Std.U64),
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask =
ok (a1, write1, acc1) := by
by_cases hrle : read.val <= d.num_digits.val
· let MainInv := fun p :
Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64 =>
read.val <= p.2.1.val /\ p.2.1.val <= d.num_digits.val /\
p.2.2.1.val = p.2.1.val - read.val /\
p.2.2.2.val < 10 * B /\
forall j, j < 768 -> p.2.1.val <= j ->
p.1.val[j]? = d.digits.val[j]?
have hmainSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask)
(fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
apply Aeneas.Std.loop.spec_decr_nat
(fun p : Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64 =>
d.num_digits.val - p.2.1.val)
MainInv (fun _ => True)
(fun p =>
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body
d.num_digits shift mask p.1 p.2.1 p.2.2.1 p.2.2.2)
(d.digits, read, 0#usize, acc)
· intro p hp
rcases p with ⟨ac, s, w, c⟩
dsimp only at hp ⊢
simp only [MainInv] at hp
rcases hp with ⟨hrs, hsi, hws, hc10, houtside⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body
by_cases hslt : s.val < d.num_digits.val
· have hsWord : s < d.num_digits := by
simpa [UScalar.lt_equiv] using hslt
simp only [hsWord, if_true]
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec c shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok, lift]
let newDigit : Std.U8 := UScalar.cast .U8 q
rw [show UScalar.cast .U8 q = newDigit by rfl]
let rem : Std.U64 := c &&& mask
rw [show c &&& mask = rem by rfl]
have hbridge := DecimalRightShiftU64DyadicBridge shift c pow mask q rem
hshift hpow hmask hq (by rfl)
simp only [B] at hbridge
rcases hbridge with
⟨_, _, hqVal, hremVal, heuc, hremB, _, hbound⟩
have hq10 : q.val < 10 := by
rw [hqVal]
exact (Nat.div_lt_iff_lt_mul hBpos).2 (by
simpa [Nat.mul_comm] using hc10)
have hmulBound : 10 * rem.val <= U64.max := by
have hlt := hbound 0 (by omega)
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨tenrem, htenrem, htenremSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := rem)
(by simpa using hmulBound))
rw [htenrem]
simp only [bind_tc_ok]
have hs768 : s.val < 768 := by omega
have hsArray : s.val < ac.val.length := by
rw [ac.property]
norm_num
exact hs768
obtain ⟨digit, hdigitRead, hdigitReadSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec ac s
(by simpa [Array.length] using hsArray))
rw [hdigitRead]
simp only [bind_tc_ok, lift]
let digit64 : Std.U64 := UScalar.cast .U64 digit
rw [show UScalar.cast .U64 digit = digit64 by rfl]
have hacOrig : ac.val[s.val]? = d.digits.val[s.val]? :=
houtside s.val hs768 (by omega)
have hacGet : ac.val[s.val]? = some digit :=
getElem?_eq_some_iff.mpr ⟨hsArray, hdigitReadSpec.symm⟩
have hdGet : d.digits.val[s.val]? = some digit := by
rw [← hacOrig]
exact hacGet
have hdBound : s.val < d.digits.val.length := by
rw [d.digits.property]
norm_num
exact hs768
have hdVal : d.digits.val[s.val]'hdBound = digit := by
have hh := List.getElem?_eq_getElem hdBound
rw [hdGet] at hh
exact Option.some.inj hh.symm
have htBound : s.val <
(d.digits.val.take d.num_digits.val).length := by
rw [List.length_take]
exact lt_min hslt hdBound
have htVal :
(d.digits.val.take d.num_digits.val)[s.val]'htBound = digit := by
simpa using hdVal
have hdigitLt : digit.val < 10 := hdigit digit (by
rw [← htVal]
exact List.getElem_mem _)
have hdigit64 : digit64.val = digit.val := by
simp [digit64, UScalar.cast_val_eq]
have haddBound : tenrem.val + digit64.val <= U64.max := by
have htenVal : tenrem.val = 10 * rem.val := by
simpa using htenremSpec
have hlt := hbound digit.val hdigitLt
rw [htenVal, hdigit64]
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨c1, hc1, hc1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := tenrem) (y := digit64) haddBound)
rw [hc1]
simp only [bind_tc_ok]
have hsBound : s.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨s1, hs1, hs1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := s) (y := 1#usize)
(by simpa using hsBound))
rw [hs1]
simp only [bind_tc_ok]
have hwArray : w.val < ac.val.length := by
rw [ac.property]
norm_num
omega
obtain ⟨ac1, hac1, hac1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec ac w newDigit
(by simpa [Array.length] using hwArray))
rw [hac1]
simp only [bind_tc_ok]
have hwBound : w.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨w1, hw1, hw1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := w) (y := 1#usize)
(by simpa using hwBound))
rw [hw1]
have hs1Val : s1.val = s.val + 1 := by simpa using hs1Spec
have hw1Val : w1.val = w.val + 1 := by simpa using hw1Spec
have htenVal : tenrem.val = 10 * rem.val := by
simpa using htenremSpec
have hc1Val : c1.val = 10 * rem.val + digit.val := by
rw [hc1Spec, htenVal, hdigit64]
have hc1ten : c1.val < 10 * B := by
rw [hc1Val]
omega
constructor
· simp only [MainInv, Prod.fst, Prod.snd, true_and]
refine ⟨by omega, by omega, by omega, hc1ten, ?_⟩
intro j hj hs1j
rw [hac1Spec]
simp only [Aeneas.Std.Array.set, List.getElem?_set]
have hne : w.val ≠ j := by omega
simp [hne]
exact houtside j hj (by omega)
· dsimp only
omega
· have hsNotWord : ¬ s < d.num_digits := by
simpa [UScalar.lt_equiv] using hslt
simp [hsNotWord]
· simp only [MainInv, Prod.fst, Prod.snd]
refine ⟨le_rfl, hrle, by simp, ?_, ?_⟩
· simpa [B] using hacc10
· intro j hj _
trivial
obtain ⟨out1, hloop1, _⟩ := Aeneas.Std.WP.spec_imp_exists hmainSpec
rcases out1 with ⟨a1, write1, acc1⟩
exact ⟨a1, write1, acc1, hloop1⟩
· have hrNotWord : ¬ read < d.num_digits := by
simp only [UScalar.lt_equiv]
omega
refine ⟨d.digits, 0#usize, acc, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
rw [Aeneas.Std.loop.eq_1]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body,
hrNotWord]
rcases hmainExists with ⟨a1, write1, acc1, hloop1⟩
have hmain : d.num_digits.val <= 768 /\
exists low : List Nat,
low.length = d.num_digits.val - read.val /\
(forall z, z ∈ low -> z < 10) /\
write1.val = d.num_digits.val - read.val /\
acc1.val < 10 * B /\
acc.val * 10 ^ (d.num_digits.val - read.val) +
(fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0)
((xs.take d.num_digits.val).drop read.val) =
B * (10 * (fun ys : List Nat =>
ys.foldl (fun n z => 10 * n + z) 0) low) + acc1.val /\
(forall j, j < low.length ->
(a1.val.map (fun z : Std.U8 => z.val))[j]? = low[j]?) /\
(forall j, j < 768 -> write1.val <= j ->
a1.val[j]? = d.digits.val[j]?) /\
(forall j, j < 768 -> d.num_digits.val <= j ->
a1.val[j]? = d.digits.val[j]?) := by
by_cases hrlt : read.val < d.num_digits.val
· have hm := DecimalRightShiftMainLoopSemantics
d.digits a1 shift read d.num_digits write1 pow mask acc acc1
hshift hreadpos (by omega) (by omega) hacc10 hdigit hpow hmask hloop1
simpa [xs, B] using hm
· have hrge : d.num_digits.val <= read.val := by omega
have hrNotWord : ¬ read < d.num_digits := by
simpa [UScalar.lt_equiv] using hrlt
have hdirect :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask =
ok (d.digits, 0#usize, acc) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
rw [Aeneas.Std.loop.eq_1]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body,
hrNotWord]
rw [hdirect] at hloop1
cases hloop1
refine ⟨hnum, [], by simp [Nat.sub_eq_zero_of_le hrge], by simp,
by simp [Nat.sub_eq_zero_of_le hrge], ?_, ?_, ?_, ?_, ?_⟩
· simpa [B] using hacc10
· have hdrop : (xs.take d.num_digits.val).drop read.val = [] :=
List.drop_eq_nil_of_le (by simp; omega)
simp [hdrop, Nat.sub_eq_zero_of_le hrge]
· intro j hj
simp at hj
· intro j hj _
rfl
· intro j hj _
rfl
rcases hmain with ⟨_, low, hlowLen, hlowDigit, hwrite1, hacc1,
hmainNum, hlowArray, houtside1, hhigh1⟩
have hwrite1Cap : write1.val <= 768 := by omega
have eventual (c : Nat) : exists k, B ∣ c * 10 ^ k := by
refine ⟨shift.val, ?_⟩
refine ⟨c * 5 ^ shift.val, ?_⟩
simp only [B]
rw [show 10 = 2 * 5 by norm_num, mul_pow]
ring
let fuel := fun c : Nat => Nat.find (eventual c)
have fuelSpec (c : Nat) : B ∣ c * 10 ^ fuel c := by
exact Nat.find_spec (eventual c)
have fuelStep (c rem c1 : Nat) (hrem : rem = c % B)
(hc1 : c1 = 10 * rem) (hremne : rem ≠ 0) : fuel c1 < fuel c := by
have hkpos : 0 < fuel c := by
by_contra hk
have hk0 : fuel c = 0 := by omega
have hd := fuelSpec c
rw [hk0] at hd
simp only [pow_zero, Nat.mul_one] at hd
have hz : c % B = 0 := Nat.dvd_iff_mod_eq_zero.mp hd
apply hremne
omega
have hm : c % B ≡ c [MOD B] := Nat.mod_modEq c B
have hm' := hm.mul_left (10 ^ fuel c)
have hd' : B ∣ (c % B) * 10 ^ fuel c := by
have hh : B ∣ 10 ^ fuel c * (c % B) := by
apply (hm'.dvd_iff (dvd_refl B)).2
simpa [Nat.mul_comm] using fuelSpec c
simpa [Nat.mul_comm] using hh
have hstep : B ∣ c1 * 10 ^ (fuel c - 1) := by
rw [hc1, hrem]
rw [show fuel c = (fuel c - 1) + 1 by omega, pow_succ] at hd'
convert hd' using 1 <;> ring
exact lt_of_le_of_lt (Nat.find_min' (eventual c1) hstep) (by omega)
let FinalInv := fun p :
Bool × Array Std.U8 768#usize × Std.Usize × Std.U64 =>
p.2.2.1.val <= 768 /\ p.2.2.2.val < 10 * B
let FinalMeasure := fun p :
Bool × Array Std.U8 768#usize × Std.Usize × Std.U64 =>
if p.2.2.2.val = 0 then 0 else fuel p.2.2.2.val + 1
have hfinalSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
d.truncated a1 shift write1 acc1 mask) (fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
apply Aeneas.Std.loop.spec_decr_nat FinalMeasure FinalInv (fun _ => True) (fun p =>
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body
shift mask p.1 p.2.1 p.2.2.1 p.2.2.2)
(d.truncated, a1, write1, acc1)
· intro p hp
rcases p with ⟨bc, ac, w, c⟩
dsimp only at hp ⊢
simp only [FinalInv] at hp
rcases hp with ⟨hwcap, hc10⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body
by_cases hc0 : c.val = 0
· have hcEq : c = 0#u64 := UScalar.eq_of_val_eq (by simpa using hc0)
have hnotgt : ¬ c > 0#u64 := by simp [hcEq]
simp [hnotgt]
· have hcgt : c > 0#u64 := by
change 0 < c.val
omega
simp only [hcgt, if_true]
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec c shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok, lift]
let newDigit : Std.U8 := UScalar.cast .U8 q
rw [show UScalar.cast .U8 q = newDigit by rfl]
let rem : Std.U64 := c &&& mask
rw [show c &&& mask = rem by rfl]
have hbridge := DecimalRightShiftU64DyadicBridge shift c pow mask q rem
hshift hpow hmask hq (by rfl)
simp only [B] at hbridge
rcases hbridge with
⟨_, _, hqVal, hremVal, heuc, hremB, _, hbound⟩
have hq10 : q.val < 10 := by
rw [hqVal]
exact (Nat.div_lt_iff_lt_mul hBpos).2 (by
simpa [Nat.mul_comm] using hc10)
have hnewDigitVal : newDigit.val = q.val := by
simp [newDigit, UScalar.cast_val_eq,
Nat.mod_eq_of_lt (by omega : q.val < 256)]
have hmulBound : 10 * rem.val <= U64.max := by
have hlt := hbound 0 (by omega)
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨c1, hc1, hc1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := rem)
(by simpa using hmulBound))
rw [hc1]
simp only [bind_tc_ok]
have hc1Val : c1.val = 10 * rem.val := by simpa using hc1Spec
have hc1ten : c1.val < 10 * B := by
rw [hc1Val]
omega
have hmeasure : FinalMeasure (bc, ac, w, c1) <
FinalMeasure (bc, ac, w, c) := by
simp only [FinalMeasure, Prod.fst, Prod.snd]
by_cases hrem0 : rem.val = 0
· have hc1zero : c1.val = 0 := by rw [hc1Val, hrem0]
simp [hc0, hc1zero]
· have hc1ne : c1.val ≠ 0 := by rw [hc1Val]; omega
simp [hc0, hc1ne]
exact fuelStep c.val rem.val c1.val hremVal hc1Val hrem0
by_cases hwlt : w.val < 768
· have hwWord : w <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hwlt
simp only [hwWord, if_true]
have hwArray : w.val < ac.val.length := by
rw [ac.property]
norm_num
exact hwlt
obtain ⟨ac1, hac1, hac1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec ac w newDigit
(by simpa [Array.length] using hwArray))
rw [hac1]
simp only [bind_tc_ok]
have hwBound : w.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨w1, hw1, hw1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := w) (y := 1#usize)
(by simpa using hwBound))
rw [hw1]
have hw1Val : w1.val = w.val + 1 := by simpa using hw1Spec
constructor
· simp only [FinalInv, Prod.fst, Prod.snd, true_and]
exact ⟨by omega, hc1ten⟩
· simpa [FinalMeasure] using hmeasure
· have hwWord : ¬ w <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hwlt
simp only [hwWord, if_false]
by_cases hnewgt : newDigit > 0#u8
· simp only [hnewgt, if_true]
constructor
· simp only [FinalInv, Prod.fst, Prod.snd]
exact ⟨hwcap, hc1ten⟩
· simpa [FinalMeasure] using hmeasure
· simp only [hnewgt, if_false]
constructor
· simp only [FinalInv, Prod.fst, Prod.snd]
exact ⟨hwcap, hc1ten⟩
· simpa [FinalMeasure] using hmeasure
· simp only [FinalInv, Prod.fst, Prod.snd]
exact ⟨hwrite1Cap, hacc1⟩
obtain ⟨out2, hloop2, _⟩ := Aeneas.Std.WP.spec_imp_exists hfinalSpec
rcases out2 with ⟨b2, a2, write2⟩
have hfinal := DecimalRightShiftFinalLoopSemantics
a1 a2 d.truncated b2 shift write1 write2 pow mask acc1
hshift hwrite1Cap hacc1 hpow hmask hloop2
dsimp only at hfinal
rcases hfinal with ⟨high, hhighDigit, hfinalNum, hwrite2,
hhighArray, hbelow2, houtside2, hsticky2⟩
let G := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
have foldFrom (ys : List Nat) (a : Nat) :
ys.foldl (fun n z => 10 * n + z) a = a * 10 ^ ys.length + G ys := by
induction ys generalizing a with
| nil => simp [G]
| cons y ys ih =>
simp only [List.foldl_cons, List.length_cons]
rw [ih, pow_succ]
change (10 * a + y) * 10 ^ ys.length + G ys =
a * (10 ^ ys.length * 10) +
(y :: ys).foldl (fun n z => 10 * n + z) 0
simp only [List.foldl_cons]
rw [ih]
ring
have foldLt (ys : List Nat) (hd : forall q, q ∈ ys -> q < 10) :
G ys < 10 ^ ys.length := by
induction ys with
| nil => simp [G]
| cons y ys ih =>
have hy : y < 10 := hd y (by simp)
have ht : forall q, q ∈ ys -> q < 10 := by
intro q hq
exact hd q (by simp [hq])
have hi := ih ht
have hf : G (y :: ys) = y * 10 ^ ys.length + G ys := by
simpa [G] using foldFrom ys y
rw [hf]
simp only [List.length_cons, pow_succ]
have hp : 0 < 10 ^ ys.length := by positivity
have hsum : y * 10 ^ ys.length + G ys + 1 <=
10 * 10 ^ ys.length := by
have hy9 : y <= 9 := by omega
calc
y * 10 ^ ys.length + G ys + 1 <=
y * 10 ^ ys.length + 10 ^ ys.length := by omega
_ <= 9 * 10 ^ ys.length + 10 ^ ys.length := by
exact Nat.add_le_add_right (Nat.mul_le_mul_right _ hy9) _
_ = 10 * 10 ^ ys.length := by ring
simpa [Nat.mul_comm] using Nat.lt_of_succ_le hsum
have headPos (q : Nat) (tail : List Nat) (A S R : Nat)
(hd : forall x, x ∈ tail -> x < 10)
(heq : A * 10 ^ (tail.length + 1) + S =
B * (10 * G (q :: tail)) + R)
(hA : B <= A) (hR : R < 10 * B) : 1 <= q := by
by_contra hq
have hq0 : q = 0 := by omega
have ht := foldLt tail hd
have htp : G tail + 1 <= 10 ^ tail.length := by omega
have hten : 10 * G tail + 10 <= 10 ^ (tail.length + 1) := by
have hh := Nat.mul_le_mul_left 10 htp
simpa [pow_succ, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_add] using hh
have hplus : R + 1 <= 10 * B := by omega
have hright : B * (10 * G tail) + R <
B * 10 ^ (tail.length + 1) := by
have hm : B * (10 * G tail) + R + 1 <=
B * 10 ^ (tail.length + 1) := by
calc
B * (10 * G tail) + R + 1 <=
B * (10 * G tail) + 10 * B := Nat.add_le_add_left hplus _
_ = B * (10 * G tail + 10) := by ring
_ <= B * 10 ^ (tail.length + 1) := Nat.mul_le_mul_left B hten
exact Nat.lt_of_succ_le hm
have hleft : B * 10 ^ (tail.length + 1) <=
A * 10 ^ (tail.length + 1) + S := by
have hh := Nat.mul_le_mul_right (10 ^ (tail.length + 1)) hA
omega
have heq0 : A * 10 ^ (tail.length + 1) + S =
B * (10 * G tail) + R := by
simpa [hq0, G] using heq
omega
have hfirstPre : exists q : Nat,
1 <= q /\ q < 10 /\ 0 < write2.val /\
(a2.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
cases low with
| nil =>
have hreadGe : d.num_digits.val <= read.val := by
have hh := hlowLen
simp at hh
omega
have hsubzero : d.num_digits.val - read.val = 0 :=
Nat.sub_eq_zero_of_le hreadGe
have hdrop : (xs.take d.num_digits.val).drop read.val = [] :=
List.drop_eq_nil_of_le (by simp; omega)
have haccEq : acc.val = acc1.val := by
have hm := hmainNum
simp [hsubzero, hdrop, G] at hm
omega
cases high with
| nil =>
have hz : acc1.val = 0 := by
simpa [G] using hfinalNum
omega
| cons q tail =>
have hqlo : 1 <= q := by
apply headPos q tail acc1.val 0 0
· intro x hx
exact hhighDigit x (by simp [hx])
· simpa [G] using hfinalNum
· rw [← haccEq]
exact hBacc
· omega
have hqhi : q < 10 := hhighDigit q (by simp)
have hw10 : write1.val = 0 := by omega
have hw2pos : 0 < write2.val := by
rw [hwrite2, hw10]
simp
have ha2 := hhighArray 0 (by simp) (by omega)
refine ⟨q, hqlo, hqhi, hw2pos, ?_⟩
simpa [hw10] using ha2
| cons q tail =>
have hlenEq : d.num_digits.val - read.val = tail.length + 1 := by
have hh := hlowLen
simp at hh
omega
have hqlo : 1 <= q := by
apply headPos q tail acc.val
(G ((xs.take d.num_digits.val).drop read.val)) acc1.val
· intro x hx
exact hlowDigit x (by simp [hx])
· rw [← hlenEq]
simpa [G] using hmainNum
· exact hBacc
· exact hacc1
have hqhi : q < 10 := hlowDigit q (by simp)
have hw1pos : 0 < write1.val := by
rw [hwrite1, hlenEq]
simp
have hw2pos : 0 < write2.val := by
rw [hwrite2]
omega
have ha1 := hlowArray 0 (by simp)
have ha2raw := hbelow2 0 (by omega) (by omega)
have ha2 : (a2.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
simp only [List.getElem?_map, ha2raw]
simpa [List.getElem?_map] using ha1
exact ⟨q, hqlo, hqhi, hw2pos, ha2⟩
rcases hfirstPre with ⟨first, hfirstLo, hfirstHi, hwrite2pos, hfirstGet⟩
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := write2, decimal_point := point,
truncated := b2, digits := a2 }
let TrimInv := fun j : Std.Usize =>
j.val <= write2.val /\ j.val <= a2.val.length
have htrimLoopSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop write2 a2)
(fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
apply Aeneas.Std.loop.spec_decr_nat (fun j : Std.Usize => j.val)
TrimInv (fun _ => True)
(fun j => num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body a2 j)
write2
· intro j hj
simp only [TrimInv] at hj
rcases hj with ⟨hjw, hjlen⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body
by_cases hj0 : j.val = 0
· have hjEq : j = 0#usize := UScalar.eq_of_val_eq (by simpa using hj0)
simp [hjEq]
· have hjne : j != 0#usize := by
simp only [bne_iff_ne]
intro heq
apply hj0
simpa [heq]
simp only [hjne, if_true]
obtain ⟨j1, hj1, hj1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.sub_spec (x := j) (y := 1#usize) (by
norm_num
omega))
rw [hj1]
simp only [bind_tc_ok]
have hj1Val : j1.val = j.val - 1 := by simpa using hj1Spec.1
have hj1Array : j1.val < a2.val.length := by omega
obtain ⟨digit, hdigitRead, hdigitReadSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec a2 j1
(by simpa [Array.length] using hj1Array))
rw [hdigitRead]
by_cases hdz : digit = 0#u8
· simp only [hdz, if_true]
constructor
· simp only [TrimInv]
exact ⟨by omega, by omega⟩
· omega
· simp [hdz]
· simp only [TrimInv]
exact ⟨le_rfl, by simp [a2.property]; omega⟩
obtain ⟨trimmed, htrimLoop, _⟩ :=
Aeneas.Std.WP.spec_imp_exists htrimLoopSpec
let d' : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ pre with num_digits := trimmed }
have htrim : num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim
have hle : pre.num_digits <=
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [pre, UScalar.le_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using
(show write2.val <= 768 by rw [hwrite2]; omega)
simp only [hle, massert_True, bind_tc_ok]
rw [show pre.num_digits = write2 by rfl,
show pre.digits = a2 by rfl, htrimLoop]
rfl
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, a2.property]
rw [hwrite2]
omega
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalueEq⟩
have hd'point : d'.decimal_point.val = point.val := by
simpa [pre] using congrArg IScalar.val hpointEq
have hd'nonzero : d'.num_digits.val != 0 := by
simp only [bne_iff_ne]
intro hz
have hzero := hzeros 0 (by omega) hwrite2pos
have ha2zero : a2.val[0]? = some 0#u8 := by simpa [pre] using hzero
have hfirstRaw : exists digit : Std.U8,
a2.val[0]? = some digit /\ digit.val = first := by
simpa [List.getElem?_map] using hfirstGet
rcases hfirstRaw with ⟨digit, hdigit0, hdigitVal⟩
rw [hdigit0] at ha2zero
have hdigitEq : digit = 0#u8 := Option.some.inj ha2zero
rw [hdigitEq] at hdigitVal
simp at hdigitVal
omega
let reset : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := 0#usize, decimal_point := 0#i32,
truncated := false, digits := d.digits }
have hright :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
rw [hloop0]
simp only [bind_tc_ok]
change (do
let i ← lift (UScalar.hcast .I32 read)
let i1 ← i - 1#i32
let i2 ← d.decimal_point - i1
let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
if i2 < i3 then
ok reset
else
let i4 ← 1#u64 <<< shift
let mask ← i4 - 1#u64
let (ac, wi, n1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask
let (bc, ac1, wi1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
d.truncated ac shift wi n1 mask
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := wi1, decimal_point := i2,
truncated := bc, digits := ac1 }) = ok d'
rw [show UScalar.hcast .I32 read = readI by rfl]
simp only [lift, bind_tc_ok]
rw [hsub]
simp only [bind_tc_ok]
rw [hpoint]
simp only [bind_tc_ok]
rw [hnegRange]
simp only [bind_tc_ok, hnotReset, if_false]
rw [hpow]
simp only [bind_tc_ok]
rw [hmask]
simp only [bind_tc_ok]
rw [hloop1]
simp only [bind_tc_ok]
change (do
let (bc, ac1, wi1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
d.truncated a1 shift write1 acc1 mask
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := wi1, decimal_point := point,
truncated := bc, digits := ac1 }) = ok d'
rw [hloop2]
simp only [bind_tc_ok]
change num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d'
exact htrim
refine ⟨read, acc, d', hloop0, hreadpos, hreadBound, hBacc, hacc10,
hprosLo, hprosHi, hright, ?_, hd'nonzero⟩
rw [hd'point, hpointVal]
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalRightShiftFinalLoopSemantics
import Tablet.DecimalRightShiftMainLoopSemantics
import Tablet.DecimalRightShiftPrefixLoopSemantics
import Tablet.DecimalSeqTrimSemantics
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: DecimalRightShiftLoop0Execution]
theorem DecimalRightShiftLoop0Execution :
forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (shift : Std.Usize),
shift.val <= 60 ->
0 <= d.decimal_point.val -> d.decimal_point.val < 310 ->
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 (read : Std.Usize) (acc : Std.U64)
(d' : num.imp.dec2flt.decimal_seq.DecimalSeq),
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
d shift 0#usize 0#u64 false = ok (d, read, acc, false) /\
1 <= read.val /\ read.val <= d.num_digits.val + shift.val /\
2 ^ shift.val <= acc.val /\ acc.val < 10 * 2 ^ shift.val /\
(let prospective : Int :=
d.decimal_point.val - (read.val : Int) + 1
prospective >= -2047 /\ prospective <= 309 /\
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' /\
d'.decimal_point.val = prospective /\ d'.num_digits.val != 0) := by
-- BODY
set_option maxHeartbeats 1600000 in
intro d shift hshift hpointLo hpointHi hinput
rcases hinput with ⟨hnum, hdigit, z, zs, hshape, hzlo, hzhi, hlen⟩
let xs := d.digits.val.map (fun z => z.val)
let F := fun r : Nat =>
(xs.take r).foldl (fun n z => 10 * n + z) 0
let I := F d.num_digits.val
let B := 2 ^ shift.val
have hBpos : 0 < B := by simp [B]
have hBle : B <= 2 ^ 60 := Nat.pow_le_pow_right (by omega) hshift
have hshift64 : shift.val < 64 := by omega
have hIpos : 0 < I := by
have hrest (ys : List Nat) (a : Nat) :
a <= ys.foldl (fun n digit => 10 * n + digit) a := by
induction ys generalizing a with
| nil => simp
| cons b bs ih =>
simp only [List.foldl_cons]
have hi := ih (10 * a + b)
omega
have hm := hrest zs z
have hzpos : 0 < z := by omega
have hp : 0 < (z :: zs).foldl (fun n digit => 10 * n + digit) 0 := by
simp only [List.foldl_cons]
exact lt_of_lt_of_le hzpos (by simpa using hm)
have heq := congrArg
(fun ys => ys.foldl (fun n digit => 10 * n + digit) 0) hshape
have heq' : I = (z :: zs).foldl
(fun n digit => 10 * n + digit) 0 := by
simpa [I, F, xs, List.map_take] using heq
rw [heq']
exact hp
have nestedSpec (start : Std.Usize) (c0 : Std.U64)
(hstart : start.val <= 768)
(hc0pos : 0 < c0.val) (hc0B : c0.val < B) :
Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
shift start c0) (fun _ => True) := by
let Inv := fun p : Std.Usize × Std.U64 =>
start.val <= p.1.val /\
p.1.val <= start.val + shift.val /\
0 < p.2.val /\ p.2.val < 10 * B /\
p.2.val = c0.val * 10 ^ (p.1.val - start.val)
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
apply Aeneas.Std.loop.spec_decr_nat
(fun p : Std.Usize × Std.U64 => start.val + shift.val - p.1.val)
Inv (fun _ => True)
(fun p =>
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
shift p.1 p.2) (start, c0)
· intro p hp
rcases p with ⟨r, n⟩
simp only [Inv, Prod.fst, Prod.snd] at hp
rcases hp with ⟨hsr, hrs, hnpos, hn10B, hnform⟩
simp only [Prod.fst, Prod.snd]
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok]
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqSpec.1
by_cases hq0 : q.val = 0
· have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
have hnB : n.val < B := by
rw [hqVal] at hq0
exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
simp only [hqEq, if_true]
have htlt : r.val - start.val < shift.val := by
by_contra hnot
have hkle : shift.val <= r.val - start.val := by omega
have hpowBase : 2 ^ shift.val <= 10 ^ shift.val :=
Nat.pow_le_pow_left (by omega) _
have hpowExp : 10 ^ shift.val <= 10 ^ (r.val - start.val) :=
Nat.pow_le_pow_right (by omega) hkle
have hc01 : 1 <= c0.val := by omega
have hmul : 10 ^ (r.val - start.val) <=
c0.val * 10 ^ (r.val - start.val) := by nlinarith
have hcontra : B <= n.val := by
rw [hnform]
exact le_trans hpowBase (le_trans hpowExp hmul)
omega
have hmulBound : n.val * 10 <= U64.max := by
have hlt : n.val * 10 < 2 ^ 64 := by calc
n.val * 10 < B * 10 := Nat.mul_lt_mul_of_pos_right hnB (by omega)
_ <= 2 ^ 60 * 10 := Nat.mul_le_mul_right 10 hBle
_ < 2 ^ 64 := by norm_num
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨n1, hn1, hn1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := n) (y := 10#u64) (by simpa using hmulBound))
rw [hn1]
simp only [bind_tc_ok]
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1]
have hr1Val : r1.val = r.val + 1 := by simpa using hr1Spec
have hn1Val : n1.val = n.val * 10 := by simpa using hn1Spec
constructor
· simp only [Inv, Prod.fst, Prod.snd]
refine ⟨by omega, by omega, by omega, ?_, ?_⟩
· rw [hn1Val]
omega
· rw [hn1Val, hnform, hr1Val]
have hsub : r.val + 1 - start.val =
(r.val - start.val) + 1 := by omega
rw [hsub, pow_succ]
ring
· dsimp only
omega
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp [hqne]
· simp only [Inv, Prod.fst, Prod.snd]
refine ⟨le_rfl, by omega, hc0pos, by omega, ?_⟩
norm_num
let Inv := fun p : Std.Usize × Std.U64 × Bool =>
p.2.2 = false /\ p.1.val <= d.num_digits.val /\ p.1.val <= 768 /\
p.2.1.val = F p.1.val /\ p.2.1.val < 10 * B
have hprefixSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
d shift 0#usize 0#u64 false) (fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
apply Aeneas.Std.loop.spec_decr_nat
(fun p : Std.Usize × Std.U64 × Bool => d.num_digits.val - p.1.val)
Inv (fun _ => True)
(fun p => num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
d shift p.1 p.2.1 p.2.2) (0#usize, 0#u64, false)
· intro p hp
rcases p with ⟨r, n, az⟩
dsimp only at hp ⊢
simp only [Inv] at hp
rcases hp with ⟨haz, hrn, hrcap, hnF, hn10⟩
rw [haz]
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok]
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqSpec.1
by_cases hq0 : q.val = 0
· have hqEq : q = 0#u64 := UScalar.eq_of_val_eq (by simpa using hq0)
have hnB : n.val < B := by
rw [hqVal] at hq0
exact (Nat.div_eq_zero_iff).mp hq0 |>.resolve_left (by omega)
simp only [hqEq, if_true, Bool.false_eq_true, if_false]
by_cases hrlt : r.val < d.num_digits.val
· have hrWord : r < d.num_digits := by
simpa [UScalar.lt_equiv] using hrlt
simp only [hrWord, if_true]
have hmulBound : 10 * n.val <= U64.max := by
have hlt : 10 * n.val < 2 ^ 64 := by calc
10 * n.val < 10 * B := Nat.mul_lt_mul_of_pos_left hnB (by omega)
_ <= 10 * 2 ^ 60 := Nat.mul_le_mul_left 10 hBle
_ < 2 ^ 64 := by norm_num
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨tenn, htenn, htennSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := n) (by simpa using hmulBound))
rw [htenn]
simp only [bind_tc_ok]
have hrArray : r.val < d.digits.val.length := by
have ha : d.digits.val.length = 768 := by simpa using d.digits.property
omega
obtain ⟨digit, hdigitRead, hdigitReadSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits r
(by simpa [Array.length] using hrArray))
rw [hdigitRead]
simp only [bind_tc_ok, lift]
let digit64 : Std.U64 := UScalar.cast .U64 digit
rw [show UScalar.cast .U64 digit = digit64 by rfl]
have hdigit64 : digit64.val = digit.val := by
simp [digit64, UScalar.cast_val_eq]
have htakeLen : r.val < (d.digits.val.take d.num_digits.val).length := by
rw [List.length_take]
exact lt_min hrlt hrArray
have htakeVal :
(d.digits.val.take d.num_digits.val)[r.val]'htakeLen = digit := by
have horig : d.digits.val[r.val]'hrArray = digit := hdigitReadSpec.symm
simpa using horig
have hdigitMem : digit ∈ d.digits.val.take d.num_digits.val := by
rw [← htakeVal]
exact List.getElem_mem _
have hdigitLt : digit.val < 10 := hdigit digit hdigitMem
have haddBound : tenn.val + digit64.val <= U64.max := by
have htennVal : tenn.val = 10 * n.val := by simpa using htennSpec
rw [htennVal, hdigit64]
norm_num [U64.max, U64.numBits]
omega
obtain ⟨n1, hn1, hn1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := tenn) (y := digit64) haddBound)
rw [hn1]
simp only [bind_tc_ok]
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1]
have hr1Val : r1.val = r.val + 1 := by simpa using hr1Spec
have htennVal : tenn.val = 10 * n.val := by simpa using htennSpec
have hn1Val : n1.val = 10 * n.val + digit.val := by
rw [hn1Spec, htennVal, hdigit64]
have hxsGet : xs[r.val]? = some digit.val := by
simp only [xs, List.getElem?_map]
have hsome : d.digits.val[r.val]? = some digit :=
getElem?_eq_some_iff.mpr ⟨hrArray, hdigitReadSpec.symm⟩
rw [hsome]
rfl
have hFstep : F (r.val + 1) = 10 * F r.val + digit.val := by
simp only [F]
rw [List.take_succ, List.foldl_append]
simp [hxsGet]
constructor
· simp only [Inv, Prod.fst, Prod.snd, true_and]
refine ⟨by omega, by omega, ?_, ?_⟩
· rw [hr1Val, hFstep, hn1Val, hnF]
· rw [hn1Val]
omega
· dsimp only
omega
· have hrEq : r.val = d.num_digits.val := by omega
have hrNotWord : ¬ r < d.num_digits := by
simpa [UScalar.lt_equiv] using hrlt
simp only [hrNotWord, if_false]
by_cases hn0 : n.val = 0
· have hnEq : n = 0#u64 := UScalar.eq_of_val_eq (by simpa using hn0)
simp only [hnEq, if_true]
exfalso
apply (Nat.ne_of_gt hIpos)
calc
I = F r.val := by simp [I, hrEq]
_ = n.val := hnF.symm
_ = 0 := hn0
· have hnNe : ¬ n = 0#u64 := by
intro heq
apply hn0
exact congrArg UScalar.val heq
simp only [hnNe, if_false]
obtain ⟨pair, hpair, _⟩ := Aeneas.Std.WP.spec_imp_exists
(nestedSpec r n hrcap (by omega) hnB)
rcases pair with ⟨r1, n1⟩
rw [hpair]
simp
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp [hqne, WP.spec_ok]
· simp [Inv, F]
exact hBpos
obtain ⟨out0, hloop0, _⟩ := Aeneas.Std.WP.spec_imp_exists hprefixSpec
rcases out0 with ⟨d0, read, acc, allZero⟩
have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc allZero
hshift (by simpa [I, F, xs, List.map_take] using hIpos) hdigit hloop0
dsimp only at hpref
rcases hpref with ⟨hd0, haz0, hreadpos, hBacc, hacc10, hphase⟩
subst d0
subst allZero
have hreadBound : read.val <= d.num_digits.val + shift.val := by
rcases hphase with hp | hp <;> omega
have hprosLo : -2047 <= d.decimal_point.val - (read.val : Int) + 1 := by
omega
have hprosHi : d.decimal_point.val - (read.val : Int) + 1 <= 309 := by
omega
have hread828 : read.val <= 828 := by omega
let readI : Std.I32 := UScalar.hcast .I32 read
have hreadIVal : readI.val = (read.val : Int) := by
have hs := UScalar.hcast_inBounds_spec .I32 read (by
rw [IScalar.max_I32_eq]
omega)
simp only [lift, WP.spec_ok] at hs
exact hs
obtain ⟨readMinus, hsub, hsubSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := readI) (y := 1#i32) (by
rw [hreadIVal]
scalar_tac) (by
rw [hreadIVal]
scalar_tac))
have hsubVal : readMinus.val = readI.val - 1 := by
simpa using hsubSpec
obtain ⟨point, hpoint, hpointSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := d.decimal_point) (y := readMinus) (by
rw [hsubVal, hreadIVal, IScalar.min_I32_eq]
omega) (by
rw [hsubVal, hreadIVal, IScalar.max_I32_eq]
omega))
have hpointVal : point.val = d.decimal_point.val - (read.val : Int) + 1 := by
rw [hpointSpec, hsubVal, hreadIVal]
omega
let negRange : Std.I32 := (-2047)#i32
have hnegRange :
-. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE =
ok negRange := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
change IScalar.neg (2047#i32 : Std.I32) = _
unfold IScalar.neg IScalar.tryMk IScalar.tryMkOpt
norm_num [IScalar.check_bounds, IScalarTy.numBits, negRange]
have hnegRangeVal : negRange.val = -2047 := by rfl
have hnotReset : ¬ point < negRange := by
simpa [UScalar.lt_equiv, hpointVal, hnegRangeVal] using hprosLo
obtain ⟨pow, hpow, hpowSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftLeft_spec 1#u64 shift (2 ^ 64) (by
norm_num [U64.numBits]
exact hshift64) (by norm_num [U64.size_eq]))
have hpowVal : pow.val = B := by
have hpLt : 2 ^ shift.val < 2 ^ 64 :=
Nat.pow_lt_pow_right (by omega) hshift64
norm_num [Nat.shiftLeft_eq] at hpowSpec
rw [show 18446744073709551616 = 2 ^ 64 by norm_num,
Nat.mod_eq_of_lt hpLt] at hpowSpec
simpa [B] using hpowSpec.1
obtain ⟨mask, hmask, hmaskSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.sub_spec (x := pow) (y := 1#u64) (by
rw [hpowVal]
exact hBpos))
have hmainExists : exists (a1 : Array Std.U8 768#usize)
(write1 : Std.Usize) (acc1 : Std.U64),
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask =
ok (a1, write1, acc1) := by
by_cases hrle : read.val <= d.num_digits.val
· let MainInv := fun p :
Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64 =>
read.val <= p.2.1.val /\ p.2.1.val <= d.num_digits.val /\
p.2.2.1.val = p.2.1.val - read.val /\
p.2.2.2.val < 10 * B /\
forall j, j < 768 -> p.2.1.val <= j ->
p.1.val[j]? = d.digits.val[j]?
have hmainSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask)
(fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
apply Aeneas.Std.loop.spec_decr_nat
(fun p : Array Std.U8 768#usize × Std.Usize × Std.Usize × Std.U64 =>
d.num_digits.val - p.2.1.val)
MainInv (fun _ => True)
(fun p =>
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body
d.num_digits shift mask p.1 p.2.1 p.2.2.1 p.2.2.2)
(d.digits, read, 0#usize, acc)
· intro p hp
rcases p with ⟨ac, s, w, c⟩
dsimp only at hp ⊢
simp only [MainInv] at hp
rcases hp with ⟨hrs, hsi, hws, hc10, houtside⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body
by_cases hslt : s.val < d.num_digits.val
· have hsWord : s < d.num_digits := by
simpa [UScalar.lt_equiv] using hslt
simp only [hsWord, if_true]
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec c shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok, lift]
let newDigit : Std.U8 := UScalar.cast .U8 q
rw [show UScalar.cast .U8 q = newDigit by rfl]
let rem : Std.U64 := c &&& mask
rw [show c &&& mask = rem by rfl]
have hbridge := DecimalRightShiftU64DyadicBridge shift c pow mask q rem
hshift hpow hmask hq (by rfl)
simp only [B] at hbridge
rcases hbridge with
⟨_, _, hqVal, hremVal, heuc, hremB, _, hbound⟩
have hq10 : q.val < 10 := by
rw [hqVal]
exact (Nat.div_lt_iff_lt_mul hBpos).2 (by
simpa [Nat.mul_comm] using hc10)
have hmulBound : 10 * rem.val <= U64.max := by
have hlt := hbound 0 (by omega)
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨tenrem, htenrem, htenremSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := rem)
(by simpa using hmulBound))
rw [htenrem]
simp only [bind_tc_ok]
have hs768 : s.val < 768 := by omega
have hsArray : s.val < ac.val.length := by
rw [ac.property]
norm_num
exact hs768
obtain ⟨digit, hdigitRead, hdigitReadSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec ac s
(by simpa [Array.length] using hsArray))
rw [hdigitRead]
simp only [bind_tc_ok, lift]
let digit64 : Std.U64 := UScalar.cast .U64 digit
rw [show UScalar.cast .U64 digit = digit64 by rfl]
have hacOrig : ac.val[s.val]? = d.digits.val[s.val]? :=
houtside s.val hs768 (by omega)
have hacGet : ac.val[s.val]? = some digit :=
getElem?_eq_some_iff.mpr ⟨hsArray, hdigitReadSpec.symm⟩
have hdGet : d.digits.val[s.val]? = some digit := by
rw [← hacOrig]
exact hacGet
have hdBound : s.val < d.digits.val.length := by
rw [d.digits.property]
norm_num
exact hs768
have hdVal : d.digits.val[s.val]'hdBound = digit := by
have hh := List.getElem?_eq_getElem hdBound
rw [hdGet] at hh
exact Option.some.inj hh.symm
have htBound : s.val <
(d.digits.val.take d.num_digits.val).length := by
rw [List.length_take]
exact lt_min hslt hdBound
have htVal :
(d.digits.val.take d.num_digits.val)[s.val]'htBound = digit := by
simpa using hdVal
have hdigitLt : digit.val < 10 := hdigit digit (by
rw [← htVal]
exact List.getElem_mem _)
have hdigit64 : digit64.val = digit.val := by
simp [digit64, UScalar.cast_val_eq]
have haddBound : tenrem.val + digit64.val <= U64.max := by
have htenVal : tenrem.val = 10 * rem.val := by
simpa using htenremSpec
have hlt := hbound digit.val hdigitLt
rw [htenVal, hdigit64]
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨c1, hc1, hc1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := tenrem) (y := digit64) haddBound)
rw [hc1]
simp only [bind_tc_ok]
have hsBound : s.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨s1, hs1, hs1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := s) (y := 1#usize)
(by simpa using hsBound))
rw [hs1]
simp only [bind_tc_ok]
have hwArray : w.val < ac.val.length := by
rw [ac.property]
norm_num
omega
obtain ⟨ac1, hac1, hac1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec ac w newDigit
(by simpa [Array.length] using hwArray))
rw [hac1]
simp only [bind_tc_ok]
have hwBound : w.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨w1, hw1, hw1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := w) (y := 1#usize)
(by simpa using hwBound))
rw [hw1]
have hs1Val : s1.val = s.val + 1 := by simpa using hs1Spec
have hw1Val : w1.val = w.val + 1 := by simpa using hw1Spec
have htenVal : tenrem.val = 10 * rem.val := by
simpa using htenremSpec
have hc1Val : c1.val = 10 * rem.val + digit.val := by
rw [hc1Spec, htenVal, hdigit64]
have hc1ten : c1.val < 10 * B := by
rw [hc1Val]
omega
constructor
· simp only [MainInv, Prod.fst, Prod.snd, true_and]
refine ⟨by omega, by omega, by omega, hc1ten, ?_⟩
intro j hj hs1j
rw [hac1Spec]
simp only [Aeneas.Std.Array.set, List.getElem?_set]
have hne : w.val ≠ j := by omega
simp [hne]
exact houtside j hj (by omega)
· dsimp only
omega
· have hsNotWord : ¬ s < d.num_digits := by
simpa [UScalar.lt_equiv] using hslt
simp [hsNotWord]
· simp only [MainInv, Prod.fst, Prod.snd]
refine ⟨le_rfl, hrle, by simp, ?_, ?_⟩
· simpa [B] using hacc10
· intro j hj _
trivial
obtain ⟨out1, hloop1, _⟩ := Aeneas.Std.WP.spec_imp_exists hmainSpec
rcases out1 with ⟨a1, write1, acc1⟩
exact ⟨a1, write1, acc1, hloop1⟩
· have hrNotWord : ¬ read < d.num_digits := by
simp only [UScalar.lt_equiv]
omega
refine ⟨d.digits, 0#usize, acc, ?_⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
rw [Aeneas.Std.loop.eq_1]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body,
hrNotWord]
rcases hmainExists with ⟨a1, write1, acc1, hloop1⟩
have hmain : d.num_digits.val <= 768 /\
exists low : List Nat,
low.length = d.num_digits.val - read.val /\
(forall z, z ∈ low -> z < 10) /\
write1.val = d.num_digits.val - read.val /\
acc1.val < 10 * B /\
acc.val * 10 ^ (d.num_digits.val - read.val) +
(fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0)
((xs.take d.num_digits.val).drop read.val) =
B * (10 * (fun ys : List Nat =>
ys.foldl (fun n z => 10 * n + z) 0) low) + acc1.val /\
(forall j, j < low.length ->
(a1.val.map (fun z : Std.U8 => z.val))[j]? = low[j]?) /\
(forall j, j < 768 -> write1.val <= j ->
a1.val[j]? = d.digits.val[j]?) /\
(forall j, j < 768 -> d.num_digits.val <= j ->
a1.val[j]? = d.digits.val[j]?) := by
by_cases hrlt : read.val < d.num_digits.val
· have hm := DecimalRightShiftMainLoopSemantics
d.digits a1 shift read d.num_digits write1 pow mask acc acc1
hshift hreadpos (by omega) (by omega) hacc10 hdigit hpow hmask hloop1
simpa [xs, B] using hm
· have hrge : d.num_digits.val <= read.val := by omega
have hrNotWord : ¬ read < d.num_digits := by
simpa [UScalar.lt_equiv] using hrlt
have hdirect :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask =
ok (d.digits, 0#usize, acc) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
rw [Aeneas.Std.loop.eq_1]
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1.body,
hrNotWord]
rw [hdirect] at hloop1
cases hloop1
refine ⟨hnum, [], by simp [Nat.sub_eq_zero_of_le hrge], by simp,
by simp [Nat.sub_eq_zero_of_le hrge], ?_, ?_, ?_, ?_, ?_⟩
· simpa [B] using hacc10
· have hdrop : (xs.take d.num_digits.val).drop read.val = [] :=
List.drop_eq_nil_of_le (by simp; omega)
simp [hdrop, Nat.sub_eq_zero_of_le hrge]
· intro j hj
simp at hj
· intro j hj _
rfl
· intro j hj _
rfl
rcases hmain with ⟨_, low, hlowLen, hlowDigit, hwrite1, hacc1,
hmainNum, hlowArray, houtside1, hhigh1⟩
have hwrite1Cap : write1.val <= 768 := by omega
have eventual (c : Nat) : exists k, B ∣ c * 10 ^ k := by
refine ⟨shift.val, ?_⟩
refine ⟨c * 5 ^ shift.val, ?_⟩
simp only [B]
rw [show 10 = 2 * 5 by norm_num, mul_pow]
ring
let fuel := fun c : Nat => Nat.find (eventual c)
have fuelSpec (c : Nat) : B ∣ c * 10 ^ fuel c := by
exact Nat.find_spec (eventual c)
have fuelStep (c rem c1 : Nat) (hrem : rem = c % B)
(hc1 : c1 = 10 * rem) (hremne : rem ≠ 0) : fuel c1 < fuel c := by
have hkpos : 0 < fuel c := by
by_contra hk
have hk0 : fuel c = 0 := by omega
have hd := fuelSpec c
rw [hk0] at hd
simp only [pow_zero, Nat.mul_one] at hd
have hz : c % B = 0 := Nat.dvd_iff_mod_eq_zero.mp hd
apply hremne
omega
have hm : c % B ≡ c [MOD B] := Nat.mod_modEq c B
have hm' := hm.mul_left (10 ^ fuel c)
have hd' : B ∣ (c % B) * 10 ^ fuel c := by
have hh : B ∣ 10 ^ fuel c * (c % B) := by
apply (hm'.dvd_iff (dvd_refl B)).2
simpa [Nat.mul_comm] using fuelSpec c
simpa [Nat.mul_comm] using hh
have hstep : B ∣ c1 * 10 ^ (fuel c - 1) := by
rw [hc1, hrem]
rw [show fuel c = (fuel c - 1) + 1 by omega, pow_succ] at hd'
convert hd' using 1 <;> ring
exact lt_of_le_of_lt (Nat.find_min' (eventual c1) hstep) (by omega)
let FinalInv := fun p :
Bool × Array Std.U8 768#usize × Std.Usize × Std.U64 =>
p.2.2.1.val <= 768 /\ p.2.2.2.val < 10 * B
let FinalMeasure := fun p :
Bool × Array Std.U8 768#usize × Std.Usize × Std.U64 =>
if p.2.2.2.val = 0 then 0 else fuel p.2.2.2.val + 1
have hfinalSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
d.truncated a1 shift write1 acc1 mask) (fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
apply Aeneas.Std.loop.spec_decr_nat FinalMeasure FinalInv (fun _ => True) (fun p =>
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body
shift mask p.1 p.2.1 p.2.2.1 p.2.2.2)
(d.truncated, a1, write1, acc1)
· intro p hp
rcases p with ⟨bc, ac, w, c⟩
dsimp only at hp ⊢
simp only [FinalInv] at hp
rcases hp with ⟨hwcap, hc10⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2.body
by_cases hc0 : c.val = 0
· have hcEq : c = 0#u64 := UScalar.eq_of_val_eq (by simpa using hc0)
have hnotgt : ¬ c > 0#u64 := by simp [hcEq]
simp [hnotgt]
· have hcgt : c > 0#u64 := by
change 0 < c.val
omega
simp only [hcgt, if_true]
obtain ⟨q, hq, hqSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec c shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq]
simp only [bind_tc_ok, lift]
let newDigit : Std.U8 := UScalar.cast .U8 q
rw [show UScalar.cast .U8 q = newDigit by rfl]
let rem : Std.U64 := c &&& mask
rw [show c &&& mask = rem by rfl]
have hbridge := DecimalRightShiftU64DyadicBridge shift c pow mask q rem
hshift hpow hmask hq (by rfl)
simp only [B] at hbridge
rcases hbridge with
⟨_, _, hqVal, hremVal, heuc, hremB, _, hbound⟩
have hq10 : q.val < 10 := by
rw [hqVal]
exact (Nat.div_lt_iff_lt_mul hBpos).2 (by
simpa [Nat.mul_comm] using hc10)
have hnewDigitVal : newDigit.val = q.val := by
simp [newDigit, UScalar.cast_val_eq,
Nat.mod_eq_of_lt (by omega : q.val < 256)]
have hmulBound : 10 * rem.val <= U64.max := by
have hlt := hbound 0 (by omega)
norm_num [U64.max, U64.numBits] at hlt ⊢
omega
obtain ⟨c1, hc1, hc1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := rem)
(by simpa using hmulBound))
rw [hc1]
simp only [bind_tc_ok]
have hc1Val : c1.val = 10 * rem.val := by simpa using hc1Spec
have hc1ten : c1.val < 10 * B := by
rw [hc1Val]
omega
have hmeasure : FinalMeasure (bc, ac, w, c1) <
FinalMeasure (bc, ac, w, c) := by
simp only [FinalMeasure, Prod.fst, Prod.snd]
by_cases hrem0 : rem.val = 0
· have hc1zero : c1.val = 0 := by rw [hc1Val, hrem0]
simp [hc0, hc1zero]
· have hc1ne : c1.val ≠ 0 := by rw [hc1Val]; omega
simp [hc0, hc1ne]
exact fuelStep c.val rem.val c1.val hremVal hc1Val hrem0
by_cases hwlt : w.val < 768
· have hwWord : w <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hwlt
simp only [hwWord, if_true]
have hwArray : w.val < ac.val.length := by
rw [ac.property]
norm_num
exact hwlt
obtain ⟨ac1, hac1, hac1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.update_spec ac w newDigit
(by simpa [Array.length] using hwArray))
rw [hac1]
simp only [bind_tc_ok]
have hwBound : w.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨w1, hw1, hw1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := w) (y := 1#usize)
(by simpa using hwBound))
rw [hw1]
have hw1Val : w1.val = w.val + 1 := by simpa using hw1Spec
constructor
· simp only [FinalInv, Prod.fst, Prod.snd, true_and]
exact ⟨by omega, hc1ten⟩
· simpa [FinalMeasure] using hmeasure
· have hwWord : ¬ w <
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
UScalar.lt_equiv] using hwlt
simp only [hwWord, if_false]
by_cases hnewgt : newDigit > 0#u8
· simp only [hnewgt, if_true]
constructor
· simp only [FinalInv, Prod.fst, Prod.snd]
exact ⟨hwcap, hc1ten⟩
· simpa [FinalMeasure] using hmeasure
· simp only [hnewgt, if_false]
constructor
· simp only [FinalInv, Prod.fst, Prod.snd]
exact ⟨hwcap, hc1ten⟩
· simpa [FinalMeasure] using hmeasure
· simp only [FinalInv, Prod.fst, Prod.snd]
exact ⟨hwrite1Cap, hacc1⟩
obtain ⟨out2, hloop2, _⟩ := Aeneas.Std.WP.spec_imp_exists hfinalSpec
rcases out2 with ⟨b2, a2, write2⟩
have hfinal := DecimalRightShiftFinalLoopSemantics
a1 a2 d.truncated b2 shift write1 write2 pow mask acc1
hshift hwrite1Cap hacc1 hpow hmask hloop2
dsimp only at hfinal
rcases hfinal with ⟨high, hhighDigit, hfinalNum, hwrite2,
hhighArray, hbelow2, houtside2, hsticky2⟩
let G := fun ys : List Nat => ys.foldl (fun n z => 10 * n + z) 0
have foldFrom (ys : List Nat) (a : Nat) :
ys.foldl (fun n z => 10 * n + z) a = a * 10 ^ ys.length + G ys := by
induction ys generalizing a with
| nil => simp [G]
| cons y ys ih =>
simp only [List.foldl_cons, List.length_cons]
rw [ih, pow_succ]
change (10 * a + y) * 10 ^ ys.length + G ys =
a * (10 ^ ys.length * 10) +
(y :: ys).foldl (fun n z => 10 * n + z) 0
simp only [List.foldl_cons]
rw [ih]
ring
have foldLt (ys : List Nat) (hd : forall q, q ∈ ys -> q < 10) :
G ys < 10 ^ ys.length := by
induction ys with
| nil => simp [G]
| cons y ys ih =>
have hy : y < 10 := hd y (by simp)
have ht : forall q, q ∈ ys -> q < 10 := by
intro q hq
exact hd q (by simp [hq])
have hi := ih ht
have hf : G (y :: ys) = y * 10 ^ ys.length + G ys := by
simpa [G] using foldFrom ys y
rw [hf]
simp only [List.length_cons, pow_succ]
have hp : 0 < 10 ^ ys.length := by positivity
have hsum : y * 10 ^ ys.length + G ys + 1 <=
10 * 10 ^ ys.length := by
have hy9 : y <= 9 := by omega
calc
y * 10 ^ ys.length + G ys + 1 <=
y * 10 ^ ys.length + 10 ^ ys.length := by omega
_ <= 9 * 10 ^ ys.length + 10 ^ ys.length := by
exact Nat.add_le_add_right (Nat.mul_le_mul_right _ hy9) _
_ = 10 * 10 ^ ys.length := by ring
simpa [Nat.mul_comm] using Nat.lt_of_succ_le hsum
have headPos (q : Nat) (tail : List Nat) (A S R : Nat)
(hd : forall x, x ∈ tail -> x < 10)
(heq : A * 10 ^ (tail.length + 1) + S =
B * (10 * G (q :: tail)) + R)
(hA : B <= A) (hR : R < 10 * B) : 1 <= q := by
by_contra hq
have hq0 : q = 0 := by omega
have ht := foldLt tail hd
have htp : G tail + 1 <= 10 ^ tail.length := by omega
have hten : 10 * G tail + 10 <= 10 ^ (tail.length + 1) := by
have hh := Nat.mul_le_mul_left 10 htp
simpa [pow_succ, Nat.mul_comm, Nat.mul_left_comm, Nat.mul_add] using hh
have hplus : R + 1 <= 10 * B := by omega
have hright : B * (10 * G tail) + R <
B * 10 ^ (tail.length + 1) := by
have hm : B * (10 * G tail) + R + 1 <=
B * 10 ^ (tail.length + 1) := by
calc
B * (10 * G tail) + R + 1 <=
B * (10 * G tail) + 10 * B := Nat.add_le_add_left hplus _
_ = B * (10 * G tail + 10) := by ring
_ <= B * 10 ^ (tail.length + 1) := Nat.mul_le_mul_left B hten
exact Nat.lt_of_succ_le hm
have hleft : B * 10 ^ (tail.length + 1) <=
A * 10 ^ (tail.length + 1) + S := by
have hh := Nat.mul_le_mul_right (10 ^ (tail.length + 1)) hA
omega
have heq0 : A * 10 ^ (tail.length + 1) + S =
B * (10 * G tail) + R := by
simpa [hq0, G] using heq
omega
have hfirstPre : exists q : Nat,
1 <= q /\ q < 10 /\ 0 < write2.val /\
(a2.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
cases low with
| nil =>
have hreadGe : d.num_digits.val <= read.val := by
have hh := hlowLen
simp at hh
omega
have hsubzero : d.num_digits.val - read.val = 0 :=
Nat.sub_eq_zero_of_le hreadGe
have hdrop : (xs.take d.num_digits.val).drop read.val = [] :=
List.drop_eq_nil_of_le (by simp; omega)
have haccEq : acc.val = acc1.val := by
have hm := hmainNum
simp [hsubzero, hdrop, G] at hm
omega
cases high with
| nil =>
have hz : acc1.val = 0 := by
simpa [G] using hfinalNum
omega
| cons q tail =>
have hqlo : 1 <= q := by
apply headPos q tail acc1.val 0 0
· intro x hx
exact hhighDigit x (by simp [hx])
· simpa [G] using hfinalNum
· rw [← haccEq]
exact hBacc
· omega
have hqhi : q < 10 := hhighDigit q (by simp)
have hw10 : write1.val = 0 := by omega
have hw2pos : 0 < write2.val := by
rw [hwrite2, hw10]
simp
have ha2 := hhighArray 0 (by simp) (by omega)
refine ⟨q, hqlo, hqhi, hw2pos, ?_⟩
simpa [hw10] using ha2
| cons q tail =>
have hlenEq : d.num_digits.val - read.val = tail.length + 1 := by
have hh := hlowLen
simp at hh
omega
have hqlo : 1 <= q := by
apply headPos q tail acc.val
(G ((xs.take d.num_digits.val).drop read.val)) acc1.val
· intro x hx
exact hlowDigit x (by simp [hx])
· rw [← hlenEq]
simpa [G] using hmainNum
· exact hBacc
· exact hacc1
have hqhi : q < 10 := hlowDigit q (by simp)
have hw1pos : 0 < write1.val := by
rw [hwrite1, hlenEq]
simp
have hw2pos : 0 < write2.val := by
rw [hwrite2]
omega
have ha1 := hlowArray 0 (by simp)
have ha2raw := hbelow2 0 (by omega) (by omega)
have ha2 : (a2.val.map (fun z : Std.U8 => z.val))[0]? = some q := by
simp only [List.getElem?_map, ha2raw]
simpa [List.getElem?_map] using ha1
exact ⟨q, hqlo, hqhi, hw2pos, ha2⟩
rcases hfirstPre with ⟨first, hfirstLo, hfirstHi, hwrite2pos, hfirstGet⟩
let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := write2, decimal_point := point,
truncated := b2, digits := a2 }
let TrimInv := fun j : Std.Usize =>
j.val <= write2.val /\ j.val <= a2.val.length
have htrimLoopSpec : Aeneas.Std.WP.spec
(num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop write2 a2)
(fun _ => True) := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
apply Aeneas.Std.loop.spec_decr_nat (fun j : Std.Usize => j.val)
TrimInv (fun _ => True)
(fun j => num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body a2 j)
write2
· intro j hj
simp only [TrimInv] at hj
rcases hj with ⟨hjw, hjlen⟩
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body
by_cases hj0 : j.val = 0
· have hjEq : j = 0#usize := UScalar.eq_of_val_eq (by simpa using hj0)
simp [hjEq]
· have hjne : j != 0#usize := by
simp only [bne_iff_ne]
intro heq
apply hj0
simpa [heq]
simp only [hjne, if_true]
obtain ⟨j1, hj1, hj1Spec⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.sub_spec (x := j) (y := 1#usize) (by
norm_num
omega))
rw [hj1]
simp only [bind_tc_ok]
have hj1Val : j1.val = j.val - 1 := by simpa using hj1Spec.1
have hj1Array : j1.val < a2.val.length := by omega
obtain ⟨digit, hdigitRead, hdigitReadSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec a2 j1
(by simpa [Array.length] using hj1Array))
rw [hdigitRead]
by_cases hdz : digit = 0#u8
· simp only [hdz, if_true]
constructor
· simp only [TrimInv]
exact ⟨by omega, by omega⟩
· omega
· simp [hdz]
· simp only [TrimInv]
exact ⟨le_rfl, by simp [a2.property]; omega⟩
obtain ⟨trimmed, htrimLoop, _⟩ :=
Aeneas.Std.WP.spec_imp_exists htrimLoopSpec
let d' : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ pre with num_digits := trimmed }
have htrim : num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim
have hle : pre.num_digits <=
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
simpa [pre, UScalar.le_equiv,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def] using
(show write2.val <= 768 by rw [hwrite2]; omega)
simp only [hle, massert_True, bind_tc_ok]
rw [show pre.num_digits = write2 by rfl,
show pre.digits = a2 by rfl, htrimLoop]
rfl
have hpreBound : pre.num_digits.val <= pre.digits.val.length := by
simp [pre, a2.property]
rw [hwrite2]
omega
have htrimSem := DecimalSeqTrimSemantics pre d' hpreBound htrim
dsimp only at htrimSem
rcases htrimSem with
⟨hdigitsEq, hpointEq, htruncEq, hnumLe, hzeros, hlast, hvalueEq⟩
have hd'point : d'.decimal_point.val = point.val := by
simpa [pre] using congrArg IScalar.val hpointEq
have hd'nonzero : d'.num_digits.val != 0 := by
simp only [bne_iff_ne]
intro hz
have hzero := hzeros 0 (by omega) hwrite2pos
have ha2zero : a2.val[0]? = some 0#u8 := by simpa [pre] using hzero
have hfirstRaw : exists digit : Std.U8,
a2.val[0]? = some digit /\ digit.val = first := by
simpa [List.getElem?_map] using hfirstGet
rcases hfirstRaw with ⟨digit, hdigit0, hdigitVal⟩
rw [hdigit0] at ha2zero
have hdigitEq : digit = 0#u8 := Option.some.inj ha2zero
rw [hdigitEq] at hdigitVal
simp at hdigitVal
omega
let reset : num.imp.dec2flt.decimal_seq.DecimalSeq :=
{ num_digits := 0#usize, decimal_point := 0#i32,
truncated := false, digits := d.digits }
have hright :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
rw [hloop0]
simp only [bind_tc_ok]
change (do
let i ← lift (UScalar.hcast .I32 read)
let i1 ← i - 1#i32
let i2 ← d.decimal_point - i1
let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
if i2 < i3 then
ok reset
else
let i4 ← 1#u64 <<< shift
let mask ← i4 - 1#u64
let (ac, wi, n1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
d.num_digits d.digits shift read 0#usize acc mask
let (bc, ac1, wi1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
d.truncated ac shift wi n1 mask
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := wi1, decimal_point := i2,
truncated := bc, digits := ac1 }) = ok d'
rw [show UScalar.hcast .I32 read = readI by rfl]
simp only [lift, bind_tc_ok]
rw [hsub]
simp only [bind_tc_ok]
rw [hpoint]
simp only [bind_tc_ok]
rw [hnegRange]
simp only [bind_tc_ok, hnotReset, if_false]
rw [hpow]
simp only [bind_tc_ok]
rw [hmask]
simp only [bind_tc_ok]
rw [hloop1]
simp only [bind_tc_ok]
change (do
let (bc, ac1, wi1) ←
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
d.truncated a1 shift write1 acc1 mask
num.imp.dec2flt.decimal_seq.DecimalSeq.trim
{ num_digits := wi1, decimal_point := point,
truncated := bc, digits := ac1 }) = ok d'
rw [hloop2]
simp only [bind_tc_ok]
change num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d'
exact htrim
refine ⟨read, acc, d', hloop0, hreadpos, hreadBound, hBacc, hacc10,
hprosLo, hprosHi, hright, ?_, hd'nonzero⟩
rw [hd'point, hpointVal]
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Inspect related source (116)
- 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
More program dependencies (83)
- 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
- 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
- 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
- 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
- left_shift_loop1 · dec2flt/decimal_seq.rs:152 Program Rust
- body · dec2flt/decimal_seq.rs:152 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
- 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
- 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
- TABLE · dec2flt/decimal_seq.rs:370 Program Rust
- TABLE_POW5 · dec2flt/decimal_seq.rs:379 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
- Dec2Flt.configured_x86_64_numBits
- 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
- DecimalRightShiftFinalLoopSemantics
- DecimalRightShiftLoop0Execution
- DecimalRightShiftMainLoopSemantics
- DecimalRightShiftPrefixLoopSemantics
- DecimalRightShiftU64DyadicBridge
- DecimalSeqTrimSemantics
All 12 entries
- DecimalRightShiftFinalLoopSemantics
- DecimalRightShiftLoop0Execution
- DecimalRightShiftMainLoopSemantics
- DecimalRightShiftPrefixLoopSemantics
- DecimalRightShiftU64DyadicBridge
- DecimalSeqTrimSemantics
- ResultLoopPartial
- num imp dec2flt decimal seq DecimalSeq
- num imp dec2flt decimal seq DecimalSeq DECIMAL POINT RANGE
- num imp dec2flt decimal seq DecimalSeq MAX DIGITS
- num imp dec2flt decimal seq DecimalSeq right shift
- num imp dec2flt decimal seq DecimalSeq trim
Technical checking records
Local closure summary
{
"node": "DecimalRightShiftLoop0Execution",
"closure_version": "patch_c_v1",
"active_decl_hash": "2d5a34e959150cdd357368048c46c71f39ef1890ac6c0602b95b5e337f312507",
"active_statement_hash": "adf1983941503e681b4d6eb23719551bd165af9c611da87c619b53e330e5f0e7",
"kernel_axioms": [
"Classical.choice",
"Dec2Flt.configured_x86_64_numBits",
"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-192",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalRightShiftFinalLoopSemantics": "79a214a5ee500eac",
"DecimalRightShiftMainLoopSemantics": "647cdcc98ff47e21",
"DecimalRightShiftPrefixLoopSemantics": "0d5cb25f18eedbfb",
"DecimalRightShiftU64DyadicBridge": "253d7e344ffc70a6",
"DecimalSeqTrimSemantics": "e4b2cbb62f7171af",
"num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "01b84a7a35e5bcf4",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
"num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "5f4488c177675662",
"num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "cb99c951cfc3a86f"
},
"strict_definition_deps": {
"num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
"num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
"num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1628b4985163d314",
"num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "0744fa1202a1cc02"
}
}Prose assessment
{
"label": "Recorded Sound: VerifierPass",
"assessment_status": "VerifierPass",
"fingerprint_relation": "differs from current fingerprint",
"assessment_fingerprint": "sound-v2:c47a61bc7ef3324e29b947e8408ba9fcca8904c9580bae4ef072d567de8f4461",
"current_fingerprint": "sound-v2:2b49f68d42188e121180042e44c99fa509f68f383b9c40ee0e3d2eefbaaa5ada",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}