Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Helper · Prefix-search semantics for decimal right shift
right_shift_loop0, started on d with shift k, read index zero,
zero accumulator, and false all-zero flag, returns (d',r,c,z), then
Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000
namespace dec2flt_model
-- [TABLET NODE: DecimalRightShiftPrefixLoopSemantics]
theorem DecimalRightShiftPrefixLoopSemantics
(d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
(shift read : Std.Usize) (acc : Std.U64) (allZero : Bool)
(hshift : shift.val <= 60)
(hpositive : 0 <
((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
(fun n z => 10 * n + z) 0)
(hdigit : forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10)
(hloop :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
d shift 0#usize 0#u64 false = ok (d', read, acc, allZero)) :
let xs := d.digits.val.map (fun z => z.val)
let I := (xs.take d.num_digits.val).foldl (fun n z => 10 * n + z) 0
let B := 2 ^ shift.val
d' = d /\
allZero = false /\
1 <= read.val /\
B <= acc.val /\
acc.val < 10 * B /\
((read.val <= d.num_digits.val /\
read.val <= 768 /\
acc.val = (xs.take read.val).foldl (fun n z => 10 * n + z) 0) \/
(d.num_digits.val < read.val /\
d.num_digits.val <= 768 /\
read.val <= d.num_digits.val + shift.val /\
acc.val = I * 10 ^ (read.val - d.num_digits.val))) := 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
right_shift_loop0. At a state
(u,C,\zeta) use
Lean proof
dsimp only
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 hIpos : 0 < I := by
simpa [I, F, xs] using hpositive
have hlen : xs.length = 768 := by
simp [xs, d.digits.property]
have hshift64 : shift.val < 64 := by omega
have shiftVal (x q : Std.U64) (hq : x >>> shift = ok q) :
q.val = x.val / B := by
have hs := UScalar.ShiftRight_spec x shift (by
norm_num [U64.numBits]
exact hshift64)
rw [hq] at hs
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hs.1
have nested (start : Std.Usize) (c0 : Std.U64)
(hstart : start.val <= 768)
(hc0pos : 0 < c0.val) (hc0B : c0.val < B)
(ri : Std.Usize) (c : Std.U64)
(hnested :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
shift start c0 = ok (ri, c)) :
start.val < ri.val /\
ri.val <= start.val + shift.val /\
B <= c.val /\
c.val < 10 * B /\
c.val = c0.val * 10 ^ (ri.val - start.val) := by
let Inv := fun (x : Std.Usize × Std.U64) =>
let (r, n) := x
start.val <= r.val /\
r.val <= start.val + shift.val /\
0 < n.val /\
n.val < 10 * B /\
n.val = c0.val * 10 ^ (r.val - start.val)
let Post := fun (x : Std.Usize × Std.U64) =>
let (r, n) := x
start.val < r.val /\
r.val <= start.val + shift.val /\
B <= n.val /\
n.val < 10 * B /\
n.val = c0.val * 10 ^ (r.val - start.val)
let Body := fun (x : Std.Usize × Std.U64) =>
let (r, n) := x
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
shift r n
have hp : Post (ri, c) := by
refine ResultLoopPartial Inv Post Body ?_ (start, c0) (ri, c) ?_ ?_
· intro x hx out hout
rcases x with ⟨r, n⟩
simp only [Inv] at hx
rcases hx with ⟨hsr, hrs, hnpos, hn10B, hnform⟩
simp only [Body,
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body]
at hout
obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq] at hout
simp only [bind_tc_ok] at hout
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.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] at hout
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 := by
exact Nat.pow_le_pow_left (by omega) _
have hpowExp : 10 ^ shift.val <= 10 ^ (r.val - start.val) := by
exact 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
rw [hnform] at hnB
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, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := n) (y := 10#u64) (by
simpa using hmulBound))
rw [hn1] at hout
simp only [bind_tc_ok] at hout
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1] at hout
simp only [bind_tc_ok] at hout
cases hout
simp only [Inv]
have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
have hn1Exact : n1.val = n.val * 10 := by simpa using hn1Val
refine ⟨by omega, by omega, by omega, ?_, ?_⟩
· rw [hn1Exact]
omega
· rw [hn1Exact, hnform, hr1Succ]
have hsub : r.val + 1 - start.val =
(r.val - start.val) + 1 := by omega
rw [hsub, pow_succ]
ring
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp only [hqne, if_false] at hout
cases hout
simp only [Post]
have hnB : B <= n.val := by
rw [hqVal] at hq0
exact (Nat.one_le_div_iff hBpos).mp (by omega)
have hrpos : start.val < r.val := by
by_contra hnot
have hre : r.val = start.val := by omega
rw [hre] at hnform
simp at hnform
rw [hnform] at hnB
omega
exact ⟨hrpos, hrs, hnB, hn10B, hnform⟩
· simp only [Inv]
refine ⟨by omega, ?_, hc0pos, ?_, ?_⟩
· omega
· omega
· norm_num
· exact hnested
simpa [Post] using hp
let Inv := fun (x : Std.Usize × Std.U64 × Bool) =>
let (r, n, z) := x
z = false /\
r.val <= d.num_digits.val /\
r.val <= 768 /\
n.val = F r.val /\
n.val < 10 * B
let Post := fun (x :
num.imp.dec2flt.decimal_seq.DecimalSeq × Std.Usize × Std.U64 × Bool) =>
let (dc, r, n, z) := x
dc = d /\
z = false /\
1 <= r.val /\
B <= n.val /\
n.val < 10 * B /\
((r.val <= d.num_digits.val /\
r.val <= 768 /\
n.val = F r.val) \/
(d.num_digits.val < r.val /\
d.num_digits.val <= 768 /\
r.val <= d.num_digits.val + shift.val /\
n.val = I * 10 ^ (r.val - d.num_digits.val)))
let Body := fun (x : Std.Usize × Std.U64 × Bool) =>
let (r, n, z) := x
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
d shift r n z
have hp : Post (d', read, acc, allZero) := by
refine ResultLoopPartial Inv Post Body ?_ (0#usize, 0#u64, false)
(d', read, acc, allZero) ?_ ?_
· intro x hx out hout
rcases x with ⟨r, n, z⟩
simp only [Inv] at hx
rcases hx with ⟨hz, hrn, hr768, hnF, hn10B⟩
subst z
simp only [Body,
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body]
at hout
obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq] at hout
simp only [bind_tc_ok] at hout
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.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] at hout
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] at hout
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, htennVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := n) (by simpa using hmulBound))
rw [htenn] at hout
simp only [bind_tc_ok] at hout
have hrArray : r.val < d.digits.val.length := by
by_contra hnot
have hnone : d.digits.val[r.val]? = none :=
List.getElem?_eq_none (by omega)
simp [Array.index_usize, Array.getElem?_Usize_eq, hnone] at hout
have harrayLen : d.digits.val.length = 768 := by
simpa using d.digits.property
have hr768lt : r.val < 768 := by omega
obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits r
(by simpa [Array.length] using hrArray))
rw [hdigitRead] at hout
simp only [bind_tc_ok, lift] at hout
let digit64 : Std.U64 := UScalar.cast .U64 digit
rw [show UScalar.cast .U64 digit = digit64 by rfl] at hout
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 := by
exact hdigitReadVal.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 htennExact : tenn.val = 10 * n.val := by
simpa using htennVal
rw [htennExact, hdigit64]
norm_num [U64.max, U64.numBits]
omega
obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := tenn) (y := digit64) haddBound)
rw [hn1] at hout
simp only [bind_tc_ok] at hout
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1] at hout
simp only [bind_tc_ok] at hout
cases hout
simp only [Inv]
have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
have htennExact : tenn.val = 10 * n.val := by simpa using htennVal
have hn1Exact : n1.val = 10 * n.val + digit.val := by
rw [hn1Val, htennExact, 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, hdigitReadVal.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]
refine ⟨by trivial, by omega, by omega, ?_, ?_⟩
· rw [hr1Succ, hFstep, hn1Exact, hnF]
· rw [hn1Exact]
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] at hout
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] at hout
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] at hout
cases hnestedCall :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
shift r n with
| fail e => simp [hnestedCall] at hout
| div => simp [hnestedCall] at hout
| ok pair =>
rcases pair with ⟨r1, n1⟩
simp only [hnestedCall, bind_tc_ok] at hout
cases hout
have hnpos : 0 < n.val := by omega
have hs := nested r n hr768 hnpos hnB r1 n1 hnestedCall
rcases hs with ⟨hrr1, hr1bound, hBn1, hn1ten, hn1form⟩
simp only [Post]
refine ⟨by trivial, by trivial, by omega, hBn1, hn1ten, Or.inr ?_⟩
refine ⟨by omega, ?_, ?_, ?_⟩
· omega
· simpa [hrEq] using hr1bound
· simpa [I, hrEq, hnF] using hn1form
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp only [hqne, if_false] at hout
cases hout
simp only [Post]
have hBn : B <= n.val := by
rw [hqVal] at hq0
exact (Nat.one_le_div_iff hBpos).mp (by omega)
have hrpos : 1 <= r.val := by
by_contra hnot
have hr0 : r.val = 0 := by omega
rw [hr0] at hnF
simp [F] at hnF
omega
exact ⟨by trivial, by trivial, hrpos, hBn, hn10B,
Or.inl ⟨hrn, hr768, hnF⟩⟩
· simp [Inv, F]
omega
· exact hloop
simpa [Post, I, F] using hp
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 800000
namespace dec2flt_model
-- [TABLET NODE: DecimalRightShiftPrefixLoopSemantics]
theorem DecimalRightShiftPrefixLoopSemantics
(d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
(shift read : Std.Usize) (acc : Std.U64) (allZero : Bool)
(hshift : shift.val <= 60)
(hpositive : 0 <
((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
(fun n z => 10 * n + z) 0)
(hdigit : forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10)
(hloop :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
d shift 0#usize 0#u64 false = ok (d', read, acc, allZero)) :
let xs := d.digits.val.map (fun z => z.val)
let I := (xs.take d.num_digits.val).foldl (fun n z => 10 * n + z) 0
let B := 2 ^ shift.val
d' = d /\
allZero = false /\
1 <= read.val /\
B <= acc.val /\
acc.val < 10 * B /\
((read.val <= d.num_digits.val /\
read.val <= 768 /\
acc.val = (xs.take read.val).foldl (fun n z => 10 * n + z) 0) \/
(d.num_digits.val < read.val /\
d.num_digits.val <= 768 /\
read.val <= d.num_digits.val + shift.val /\
acc.val = I * 10 ^ (read.val - d.num_digits.val))) := by
-- BODY
dsimp only
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 hIpos : 0 < I := by
simpa [I, F, xs] using hpositive
have hlen : xs.length = 768 := by
simp [xs, d.digits.property]
have hshift64 : shift.val < 64 := by omega
have shiftVal (x q : Std.U64) (hq : x >>> shift = ok q) :
q.val = x.val / B := by
have hs := UScalar.ShiftRight_spec x shift (by
norm_num [U64.numBits]
exact hshift64)
rw [hq] at hs
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hs.1
have nested (start : Std.Usize) (c0 : Std.U64)
(hstart : start.val <= 768)
(hc0pos : 0 < c0.val) (hc0B : c0.val < B)
(ri : Std.Usize) (c : Std.U64)
(hnested :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
shift start c0 = ok (ri, c)) :
start.val < ri.val /\
ri.val <= start.val + shift.val /\
B <= c.val /\
c.val < 10 * B /\
c.val = c0.val * 10 ^ (ri.val - start.val) := by
let Inv := fun (x : Std.Usize × Std.U64) =>
let (r, n) := x
start.val <= r.val /\
r.val <= start.val + shift.val /\
0 < n.val /\
n.val < 10 * B /\
n.val = c0.val * 10 ^ (r.val - start.val)
let Post := fun (x : Std.Usize × Std.U64) =>
let (r, n) := x
start.val < r.val /\
r.val <= start.val + shift.val /\
B <= n.val /\
n.val < 10 * B /\
n.val = c0.val * 10 ^ (r.val - start.val)
let Body := fun (x : Std.Usize × Std.U64) =>
let (r, n) := x
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body
shift r n
have hp : Post (ri, c) := by
refine ResultLoopPartial Inv Post Body ?_ (start, c0) (ri, c) ?_ ?_
· intro x hx out hout
rcases x with ⟨r, n⟩
simp only [Inv] at hx
rcases hx with ⟨hsr, hrs, hnpos, hn10B, hnform⟩
simp only [Body,
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0.body]
at hout
obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq] at hout
simp only [bind_tc_ok] at hout
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.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] at hout
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 := by
exact Nat.pow_le_pow_left (by omega) _
have hpowExp : 10 ^ shift.val <= 10 ^ (r.val - start.val) := by
exact 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
rw [hnform] at hnB
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, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := n) (y := 10#u64) (by
simpa using hmulBound))
rw [hn1] at hout
simp only [bind_tc_ok] at hout
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1] at hout
simp only [bind_tc_ok] at hout
cases hout
simp only [Inv]
have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
have hn1Exact : n1.val = n.val * 10 := by simpa using hn1Val
refine ⟨by omega, by omega, by omega, ?_, ?_⟩
· rw [hn1Exact]
omega
· rw [hn1Exact, hnform, hr1Succ]
have hsub : r.val + 1 - start.val =
(r.val - start.val) + 1 := by omega
rw [hsub, pow_succ]
ring
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp only [hqne, if_false] at hout
cases hout
simp only [Post]
have hnB : B <= n.val := by
rw [hqVal] at hq0
exact (Nat.one_le_div_iff hBpos).mp (by omega)
have hrpos : start.val < r.val := by
by_contra hnot
have hre : r.val = start.val := by omega
rw [hre] at hnform
simp at hnform
rw [hnform] at hnB
omega
exact ⟨hrpos, hrs, hnB, hn10B, hnform⟩
· simp only [Inv]
refine ⟨by omega, ?_, hc0pos, ?_, ?_⟩
· omega
· omega
· norm_num
· exact hnested
simpa [Post] using hp
let Inv := fun (x : Std.Usize × Std.U64 × Bool) =>
let (r, n, z) := x
z = false /\
r.val <= d.num_digits.val /\
r.val <= 768 /\
n.val = F r.val /\
n.val < 10 * B
let Post := fun (x :
num.imp.dec2flt.decimal_seq.DecimalSeq × Std.Usize × Std.U64 × Bool) =>
let (dc, r, n, z) := x
dc = d /\
z = false /\
1 <= r.val /\
B <= n.val /\
n.val < 10 * B /\
((r.val <= d.num_digits.val /\
r.val <= 768 /\
n.val = F r.val) \/
(d.num_digits.val < r.val /\
d.num_digits.val <= 768 /\
r.val <= d.num_digits.val + shift.val /\
n.val = I * 10 ^ (r.val - d.num_digits.val)))
let Body := fun (x : Std.Usize × Std.U64 × Bool) =>
let (r, n, z) := x
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body
d shift r n z
have hp : Post (d', read, acc, allZero) := by
refine ResultLoopPartial Inv Post Body ?_ (0#usize, 0#u64, false)
(d', read, acc, allZero) ?_ ?_
· intro x hx out hout
rcases x with ⟨r, n, z⟩
simp only [Inv] at hx
rcases hx with ⟨hz, hrn, hr768, hnF, hn10B⟩
subst z
simp only [Body,
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0.body]
at hout
obtain ⟨q, hq, hqValSpec⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.ShiftRight_spec n shift (by
norm_num [U64.numBits]
exact hshift64))
rw [hq] at hout
simp only [bind_tc_ok] at hout
have hqVal : q.val = n.val / B := by
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqValSpec.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] at hout
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] at hout
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, htennVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.mul_spec (x := 10#u64) (y := n) (by simpa using hmulBound))
rw [htenn] at hout
simp only [bind_tc_ok] at hout
have hrArray : r.val < d.digits.val.length := by
by_contra hnot
have hnone : d.digits.val[r.val]? = none :=
List.getElem?_eq_none (by omega)
simp [Array.index_usize, Array.getElem?_Usize_eq, hnone] at hout
have harrayLen : d.digits.val.length = 768 := by
simpa using d.digits.property
have hr768lt : r.val < 768 := by omega
obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits r
(by simpa [Array.length] using hrArray))
rw [hdigitRead] at hout
simp only [bind_tc_ok, lift] at hout
let digit64 : Std.U64 := UScalar.cast .U64 digit
rw [show UScalar.cast .U64 digit = digit64 by rfl] at hout
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 := by
exact hdigitReadVal.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 htennExact : tenn.val = 10 * n.val := by
simpa using htennVal
rw [htennExact, hdigit64]
norm_num [U64.max, U64.numBits]
omega
obtain ⟨n1, hn1, hn1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := tenn) (y := digit64) haddBound)
rw [hn1] at hout
simp only [bind_tc_ok] at hout
have hrBound : r.val + 1 <= Usize.max := by
simp [Usize.max, Usize.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨r1, hr1, hr1Val⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := r) (y := 1#usize) (by simpa using hrBound))
rw [hr1] at hout
simp only [bind_tc_ok] at hout
cases hout
simp only [Inv]
have hr1Succ : r1.val = r.val + 1 := by simpa using hr1Val
have htennExact : tenn.val = 10 * n.val := by simpa using htennVal
have hn1Exact : n1.val = 10 * n.val + digit.val := by
rw [hn1Val, htennExact, 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, hdigitReadVal.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]
refine ⟨by trivial, by omega, by omega, ?_, ?_⟩
· rw [hr1Succ, hFstep, hn1Exact, hnF]
· rw [hn1Exact]
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] at hout
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] at hout
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] at hout
cases hnestedCall :
num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0_loop0
shift r n with
| fail e => simp [hnestedCall] at hout
| div => simp [hnestedCall] at hout
| ok pair =>
rcases pair with ⟨r1, n1⟩
simp only [hnestedCall, bind_tc_ok] at hout
cases hout
have hnpos : 0 < n.val := by omega
have hs := nested r n hr768 hnpos hnB r1 n1 hnestedCall
rcases hs with ⟨hrr1, hr1bound, hBn1, hn1ten, hn1form⟩
simp only [Post]
refine ⟨by trivial, by trivial, by omega, hBn1, hn1ten, Or.inr ?_⟩
refine ⟨by omega, ?_, ?_, ?_⟩
· omega
· simpa [hrEq] using hr1bound
· simpa [I, hrEq, hnF] using hn1form
· have hqne : ¬ q = 0#u64 := by
intro heq
apply hq0
exact congrArg UScalar.val heq
simp only [hqne, if_false] at hout
cases hout
simp only [Post]
have hBn : B <= n.val := by
rw [hqVal] at hq0
exact (Nat.one_le_div_iff hBpos).mp (by omega)
have hrpos : 1 <= r.val := by
by_contra hnot
have hr0 : r.val = 0 := by omega
rw [hr0] at hnF
simp [F] at hnF
omega
exact ⟨by trivial, by trivial, hrpos, hBn, hn10B,
Or.inl ⟨hrn, hr768, hnF⟩⟩
· simp [Inv, F]
omega
· exact hloop
simpa [Post, I, F] using hp
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Inspect related source (116)
- 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
More program dependencies (83)
- 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
- DECIMAL_POINT_RANGE · dec2flt/decimal_seq.rs:67 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
- parse_long_mantissa · dec2flt/slow.rs:27 Program Rust
- parse_long_mantissa_loop0 · dec2flt/slow.rs:51 Program Rust
- body · dec2flt/slow.rs:51 Program Rust
- parse_long_mantissa_loop1 · dec2flt/slow.rs:69 Program Rust
- body · dec2flt/slow.rs:69 Program Rust
- parse_long_mantissa_loop2 · dec2flt/slow.rs:94 Program Rust
- body · dec2flt/slow.rs:94 Program Rust
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- BiasedFp · dec2flt/common.rs:52 Program Rust
- zero_pow2 · dec2flt/common.rs:62 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 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
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
- read_u64 · dec2flt/common.rs: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
- DecimalRightShiftPrefixLoopSemantics
- 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
All 7 entries
Technical checking records
Local closure summary
{
"node": "DecimalRightShiftPrefixLoopSemantics",
"closure_version": "patch_c_v1",
"active_decl_hash": "7aa5803726be0a18fe87aeacebb80b02b38d61e4e8044176833d39699a12b75e",
"active_statement_hash": "a8b5e59365838147449ccf5e4313513376e4b9d62634fc72806cf3017aad2f24",
"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": "worker-result-local-closure-issuance-cycle-159",
"boundary_theorems": {},
"strict_theorem_deps": {
"ResultLoopPartial": "566d2aeb76ed9100",
"num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "0eb6fb2627d0ff5c"
},
"strict_definition_deps": {
"ResultLoopPartial": "1b58e96ad00b23c2",
"num_imp_dec2flt_decimal_seq_DecimalSeq": "c43614ff2149ecf2",
"num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1628b4985163d314"
}
}Prose assessment
{
"label": "Lean proof closed; predictive prose review no longer required",
"assessment_status": null,
"fingerprint_relation": "unknown",
"assessment_fingerprint": null,
"current_fingerprint": "sound-v2:306bb996883d76c35dfe5e2e95646609dbd2b4528af19452eab4c07b4dcbae50",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"substantiveness_status": "Pass"
}