Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Exact decimal digit growth under a left shift
if expression.Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalDigitLexScale
import Tablet.DecimalLeftShiftLoopLex
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: DecimalLeftShiftDigitGrowth]
theorem DecimalLeftShiftDigitGrowth :
forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
(shift : Std.Usize) (h : Nat),
shift.val <= 60 ->
10 ^ (h - 1) < 2 ^ shift.val ->
2 ^ shift.val < 10 ^ h ->
d.num_digits.val <= 768 ->
(forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
let digits :=
(d.digits.val.take d.num_digits.val).map (fun z => z.val)
let n := d.num_digits.val
let value := digits.foldl (fun a z => 10 * a + z) 0
exists out : Std.Usize,
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d shift = ok out /\
out.val = if 10 ^ (n + h - 1) <= value * 2 ^ shift.val
then h else h - 1 := 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.
Earlier prose review
An earlier review questioned the prose argument. Lean subsequently checked the formal proof. Review of the statement’s intended meaning is a separate judgment.
Proof
i\ge n returns h-1.
Thus the loop is exactly a lexicographic comparison of the first \ell
digits of the sequence after padding the sequence on the right with zeroes.
No array access fails: an input digit is read only in the branch i<n, and
n\le768 is the physical array length.Lean proof
intro d shift h hshift hlo hhi hn hdigit
simp only
by_cases hk0 : shift.val = 0
· simp [hk0] at hlo
· have hkpos : 0 < shift.val := by omega
let kfin : Fin 61 := ⟨shift.val, by omega⟩
have cert := DecimalLeftShiftTableCertificate kfin
dsimp only at cert
rcases cert with ⟨hloidx, hhiidx, hlen, hdsdigit, _, hpositive⟩
have hp := hpositive (by simpa [kfin] using hkpos)
rcases hp with ⟨hdsne, hhead, hfold, hsum, hclo, hchi⟩
simp only [kfin] at hclo hchi hfold hsum
have hhpos : 0 < h := by
by_contra hz
have : h = 0 := by omega
simp [this] at hhi
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let x := table.val.get ⟨shift.val, by
have hlen : table.val.length = 65 := by simp [table]
omega⟩
let hi := x.val / 2048
change 10 ^ (hi - 1) < 2 ^ shift.val at hclo
change 2 ^ shift.val < 10 ^ hi at hchi
have hhipos : 0 < hi := by
by_contra hz
have : hi = 0 := by omega
rw [this] at hchi
simp at hchi
have heq : hi = h := by
apply Nat.le_antisymm
· by_contra hnle
have hle : h <= hi - 1 := by omega
have hpows : 10 ^ h <= 10 ^ (hi - 1) :=
Nat.pow_le_pow_right (by omega) hle
omega
· by_contra hnle
have hle : hi <= h - 1 := by omega
have hpows : 10 ^ hi <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) hle
omega
simp only [kfin] at hloidx hhiidx hlen hdsdigit hdsne hhead
let pow5Table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
let y := table.val.get ⟨shift.val + 1, by
have hlen : table.val.length = 65 := by simp [table]
omega⟩
let lo := x.val % 2048
let hiNext := y.val % 2048
let dsU := pow5Table.val.extract lo hiNext
let ds := dsU.map (fun z => z.val)
have hloidx' : lo <= hiNext := by
simpa [lo, hiNext, x, y, table] using hloidx
have hhiidx' : hiNext <= 1308 := by
simpa [hiNext, y, table] using hhiidx
have hlen' : ds.length = hiNext - lo := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hlen
have hdsdigit' : forall z, z ∈ ds -> z < 10 := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsdigit
have hdsne' : 0 < ds.length := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsne
have hhead' : 0 < ds.headD 0 := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hhead
have hfold' : ds.foldl (fun n z => 10 * n + z) 0 = 5 ^ shift.val := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hfold
have hsum' : hi + ds.length = shift.val + 1 := by
simpa [hi, x, ds, dsU, pow5Table, lo, hiNext, y, table] using hsum
have hmask : shift &&& 63#usize = shift := by
apply UScalar.eq_of_val_eq
rw [UScalar.val_and]
norm_num
rw [show 63 = 2 ^ 6 - 1 by decide,
Nat.and_two_pow_sub_one_of_lt_two_pow (n := 6) (by omega)]
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
simp only [hmask, lift]
simp only [Bind.bind, Aeneas.Std.bind]
have htableBound : shift.val <
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
simp
omega
obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
shift (by simpa [Array.length] using htableBound))
rw [hxa]
simp only [Bind.bind, Aeneas.Std.bind]
have hxaEq : xa = x := by
apply UScalar.eq_of_val_eq
simpa [x, table] using congrArg UScalar.val hxaVal
subst xa
rw [hxaEq]
have hshiftAdd : shift.val + 1 <= UScalar.max .Usize := by scalar_tac
obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := shift) (y := 1#usize)
(by simpa using hshiftAdd))
rw [hnext]
simp only [Bind.bind, Aeneas.Std.bind]
norm_num at hnextVal
have hnextBound : next.val <
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
simp
omega
obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
next (by simpa [Array.length] using hnextBound))
rw [hyb]
simp only [Bind.bind, Aeneas.Std.bind]
have hybEq : yb = y := by
apply UScalar.eq_of_val_eq
simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
subst yb
rw [hybEq]
obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
rw [hhigh16]
simp only [Bind.bind, Aeneas.Std.bind]
have hhigh16Eq : high16.val = hi := by
rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
norm_num [hi, Int.toNat]
let numNew : Std.Usize := UScalar.cast .Usize high16
have hnumNewVal : numNew.val = h := by
simp [numNew, UScalar.cast_val_eq, hhigh16Eq, heq]
have hnumNewPos : 0 < numNew.val := by omega
let low16 := 2047#u16 &&& x
let pow5a : Std.Usize := UScalar.cast .Usize low16
have hpow5aVal : pow5a.val = lo := by
simp [pow5a, low16, lo, UScalar.cast_val_eq, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
let next16 := 2047#u16 &&& y
let pow5b : Std.Usize := UScalar.cast .Usize next16
have hpow5bVal : pow5b.val = hiNext := by
simp [pow5b, next16, hiNext, UScalar.cast_val_eq, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
rw [show UScalar.cast .Usize high16 = numNew by rfl]
have hloTable : pow5a.val <= pow5Table.val.length := by
simp [hpow5aVal, pow5Table]
omega
have hlo1308 : pow5a.val <= 1308 := by
rw [hpow5aVal]
omega
let suffix : Slice Std.U8 := pow5Table.to_slice.drop pow5a
have hsuffix :
core.array.Array.index
(core.ops.index.IndexSlice
(core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
pow5Table { start := pow5a } = ok suffix := by
rw [Array.index_SliceIndexRangeFromUsizeSlice]
unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
simp [hlo1308, suffix]
rw [hsuffix]
simp only [Bind.bind, Aeneas.Std.bind]
unfold core.slice.Slice.iter
simp only [Bind.bind, Aeneas.Std.bind]
unfold core.slice.iter.IteratorSliceIter.enumerate
simp only [Bind.bind, Aeneas.Std.bind]
have hsubBound : pow5a.val <= pow5b.val := by
rw [hpow5aVal, hpow5bVal]
exact hloidx'
obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
rw [hspan]
simp only [Bind.bind, Aeneas.Std.bind]
unfold core.iter.adapters.enumerate.IteratorEnumerate.take
simp only [Bind.bind, Aeneas.Std.bind]
have hspanDs : span.val = ds.length := by
rw [hspanVal.1, hpow5aVal, hpow5bVal]
omega
have hell : span.val <= suffix.val.length := by
simp [suffix, Slice.drop, Array.to_slice, pow5Table, hspanVal.1,
hpow5aVal, hpow5bVal, List.length_drop]
omega
have hdiffBound : hiNext - lo <= 1308 - lo :=
Nat.sub_le_sub_right hhiidx' lo
have hseq : (suffix.val.take span.val).map (fun z => z.val) = ds := by
simp [suffix, Slice.drop, Array.to_slice, hspanDs, ds, dsU,
pow5Table, hpow5aVal, Nat.min_eq_left hdiffBound]
have hfive : 5 ^ shift.val % 10 = 5 := by
have hs : forall q : Nat, 5 ^ (q + 1) % 10 = 5 := by
intro q
induction q with
| zero => decide
| succ q ih =>
rw [Nat.pow_succ, Nat.mul_mod, ih]
obtain ⟨q, hq⟩ := Nat.exists_eq_add_of_le hkpos
rw [hq]
simpa [Nat.add_comm] using hs q
have hdslast : forall hne : ds ≠ [], ds.getLast hne ≠ 0 := by
intro hne hz
have hreconstruct := List.dropLast_append_getLast hne
have hmod := congrArg (fun n : Nat => n % 10) hfold'
change List.foldl (fun n z => 10 * n + z) 0 ds % 10 =
5 ^ shift.val % 10 at hmod
rw [hfive] at hmod
rw [← hreconstruct] at hmod
simp [List.foldl_append, hz] at hmod
let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
have hdigits : forall z, z ∈ digits -> z < 10 := by
intro z hz
simp only [digits, List.mem_map] at hz
rcases hz with ⟨u, hu, rfl⟩
exact hdigit u hu
have hlex := DecimalDigitLexScale digits ds hdigits hdsdigit' hdslast
let value := digits.foldl (fun a z => 10 * a + z) 0
have hdigitsLen : digits.length = d.num_digits.val := by
simp [digits]
have hdlen : d.digits.val.length = 768 := d.digits.property
omega
have hkSplit : shift.val = (h - 1) + ds.length := by
omega
have hpowN : 10 ^ (d.num_digits.val + h - 1) =
10 ^ digits.length * 10 ^ (h - 1) := by
rw [show d.num_digits.val + h - 1 =
d.num_digits.val + (h - 1) by omega, Nat.pow_add, hdigitsLen]
have hpowK : 10 ^ shift.val = 10 ^ (h - 1) * 10 ^ ds.length := by
rw [hkSplit, Nat.pow_add]
have htwofive : 2 ^ shift.val * 5 ^ shift.val = 10 ^ shift.val := by
rw [← Nat.mul_pow]
have hthreshold :
10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val ↔
5 ^ shift.val * 10 ^ digits.length <= value * 10 ^ ds.length := by
constructor
· intro ht
have hm := Nat.mul_le_mul_left (5 ^ shift.val) ht
apply Nat.le_of_mul_le_mul_left (c := 10 ^ (h - 1)) ?_ (by positivity)
calc
10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) =
5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) := by
rw [hpowN]
ring
_ <= 5 ^ shift.val * (value * 2 ^ shift.val) := hm
_ = 10 ^ (h - 1) * (value * 10 ^ ds.length) := by
rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
value * (2 ^ shift.val * 5 ^ shift.val) by ring,
htwofive, hpowK]
ring
· intro ht
have hm := Nat.mul_le_mul_left (10 ^ (h - 1)) ht
apply Nat.le_of_mul_le_mul_left (c := 5 ^ shift.val) ?_ (by positivity)
calc
5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) =
10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) := by
rw [hpowN]
ring
_ <= 10 ^ (h - 1) * (value * 10 ^ ds.length) := hm
_ = 5 ^ shift.val * (value * 2 ^ shift.val) := by
rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
value * (2 ^ shift.val * 5 ^ shift.val) by ring,
htwofive, hpowK]
ring
have hlex' : List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds ↔
value * 10 ^ ds.length < 5 ^ shift.val * 10 ^ digits.length := by
simpa [value, hfold'] using hlex
obtain ⟨out, hout, houtVal⟩ := DecimalLeftShiftLoopLex
d suffix span numNew hell hnumNewPos hn
refine ⟨out, hout, ?_⟩
rw [hseq, hspanDs] at houtVal
rw [houtVal]
change (if List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds
then numNew.val - 1 else numNew.val) =
if 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
then h else h - 1
by_cases ht : 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
· have hs := hthreshold.mp ht
have hnlex : ¬ List.Lex (fun a b : Nat => a < b)
(digits.take ds.length) ds := by
intro hl
have hlt := hlex'.mp hl
omega
simp [ht, hnlex, hnumNewVal]
· have hns : ¬ 5 ^ shift.val * 10 ^ digits.length <=
value * 10 ^ ds.length := by
intro hs
exact ht (hthreshold.mpr hs)
have hlt : value * 10 ^ ds.length <
5 ^ shift.val * 10 ^ digits.length := by omega
have hl := hlex'.mpr hlt
simp [ht, hl, hnumNewVal]
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalLeftShiftTableCertificate
import Tablet.DecimalDigitLexScale
import Tablet.DecimalLeftShiftLoopLex
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: DecimalLeftShiftDigitGrowth]
theorem DecimalLeftShiftDigitGrowth :
forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
(shift : Std.Usize) (h : Nat),
shift.val <= 60 ->
10 ^ (h - 1) < 2 ^ shift.val ->
2 ^ shift.val < 10 ^ h ->
d.num_digits.val <= 768 ->
(forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
let digits :=
(d.digits.val.take d.num_digits.val).map (fun z => z.val)
let n := d.num_digits.val
let value := digits.foldl (fun a z => 10 * a + z) 0
exists out : Std.Usize,
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
d shift = ok out /\
out.val = if 10 ^ (n + h - 1) <= value * 2 ^ shift.val
then h else h - 1 := by
-- BODY
intro d shift h hshift hlo hhi hn hdigit
simp only
by_cases hk0 : shift.val = 0
· simp [hk0] at hlo
· have hkpos : 0 < shift.val := by omega
let kfin : Fin 61 := ⟨shift.val, by omega⟩
have cert := DecimalLeftShiftTableCertificate kfin
dsimp only at cert
rcases cert with ⟨hloidx, hhiidx, hlen, hdsdigit, _, hpositive⟩
have hp := hpositive (by simpa [kfin] using hkpos)
rcases hp with ⟨hdsne, hhead, hfold, hsum, hclo, hchi⟩
simp only [kfin] at hclo hchi hfold hsum
have hhpos : 0 < h := by
by_contra hz
have : h = 0 := by omega
simp [this] at hhi
let table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
let x := table.val.get ⟨shift.val, by
have hlen : table.val.length = 65 := by simp [table]
omega⟩
let hi := x.val / 2048
change 10 ^ (hi - 1) < 2 ^ shift.val at hclo
change 2 ^ shift.val < 10 ^ hi at hchi
have hhipos : 0 < hi := by
by_contra hz
have : hi = 0 := by omega
rw [this] at hchi
simp at hchi
have heq : hi = h := by
apply Nat.le_antisymm
· by_contra hnle
have hle : h <= hi - 1 := by omega
have hpows : 10 ^ h <= 10 ^ (hi - 1) :=
Nat.pow_le_pow_right (by omega) hle
omega
· by_contra hnle
have hle : hi <= h - 1 := by omega
have hpows : 10 ^ hi <= 10 ^ (h - 1) :=
Nat.pow_le_pow_right (by omega) hle
omega
simp only [kfin] at hloidx hhiidx hlen hdsdigit hdsne hhead
let pow5Table :=
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
let y := table.val.get ⟨shift.val + 1, by
have hlen : table.val.length = 65 := by simp [table]
omega⟩
let lo := x.val % 2048
let hiNext := y.val % 2048
let dsU := pow5Table.val.extract lo hiNext
let ds := dsU.map (fun z => z.val)
have hloidx' : lo <= hiNext := by
simpa [lo, hiNext, x, y, table] using hloidx
have hhiidx' : hiNext <= 1308 := by
simpa [hiNext, y, table] using hhiidx
have hlen' : ds.length = hiNext - lo := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hlen
have hdsdigit' : forall z, z ∈ ds -> z < 10 := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsdigit
have hdsne' : 0 < ds.length := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hdsne
have hhead' : 0 < ds.headD 0 := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hhead
have hfold' : ds.foldl (fun n z => 10 * n + z) 0 = 5 ^ shift.val := by
simpa [ds, dsU, pow5Table, lo, hiNext, x, y, table] using hfold
have hsum' : hi + ds.length = shift.val + 1 := by
simpa [hi, x, ds, dsU, pow5Table, lo, hiNext, y, table] using hsum
have hmask : shift &&& 63#usize = shift := by
apply UScalar.eq_of_val_eq
rw [UScalar.val_and]
norm_num
rw [show 63 = 2 ^ 6 - 1 by decide,
Nat.and_two_pow_sub_one_of_lt_two_pow (n := 6) (by omega)]
unfold num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
simp only [hmask, lift]
simp only [Bind.bind, Aeneas.Std.bind]
have htableBound : shift.val <
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
simp
omega
obtain ⟨xa, hxa, hxaVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
shift (by simpa [Array.length] using htableBound))
rw [hxa]
simp only [Bind.bind, Aeneas.Std.bind]
have hxaEq : xa = x := by
apply UScalar.eq_of_val_eq
simpa [x, table] using congrArg UScalar.val hxaVal
subst xa
rw [hxaEq]
have hshiftAdd : shift.val + 1 <= UScalar.max .Usize := by scalar_tac
obtain ⟨next, hnext, hnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.add_spec (x := shift) (y := 1#usize)
(by simpa using hshiftAdd))
rw [hnext]
simp only [Bind.bind, Aeneas.Std.bind]
norm_num at hnextVal
have hnextBound : next.val <
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.val.length := by
simp
omega
obtain ⟨yb, hyb, hybVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec
num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
next (by simpa [Array.length] using hnextBound))
rw [hyb]
simp only [Bind.bind, Aeneas.Std.bind]
have hybEq : yb = y := by
apply UScalar.eq_of_val_eq
simpa [y, table, hnextVal] using congrArg UScalar.val hybVal
subst yb
rw [hybEq]
obtain ⟨high16, hhigh16, hhigh16Val⟩ := Aeneas.Std.WP.spec_imp_exists
(U16.ShiftRight_IScalar_spec x 11#i32 (by norm_num) (by norm_num))
rw [hhigh16]
simp only [Bind.bind, Aeneas.Std.bind]
have hhigh16Eq : high16.val = hi := by
rw [hhigh16Val.1, Nat.shiftRight_eq_div_pow]
norm_num [hi, Int.toNat]
let numNew : Std.Usize := UScalar.cast .Usize high16
have hnumNewVal : numNew.val = h := by
simp [numNew, UScalar.cast_val_eq, hhigh16Eq, heq]
have hnumNewPos : 0 < numNew.val := by omega
let low16 := 2047#u16 &&& x
let pow5a : Std.Usize := UScalar.cast .Usize low16
have hpow5aVal : pow5a.val = lo := by
simp [pow5a, low16, lo, UScalar.cast_val_eq, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
let next16 := 2047#u16 &&& y
let pow5b : Std.Usize := UScalar.cast .Usize next16
have hpow5bVal : pow5b.val = hiNext := by
simp [pow5b, next16, hiNext, UScalar.cast_val_eq, UScalar.val_and]
rw [show 2047 = 2 ^ 11 - 1 by decide, Nat.and_comm,
Nat.and_two_pow_sub_one_eq_mod]
rw [show UScalar.cast .Usize (2047#u16 &&& x) = pow5a by rfl]
rw [show UScalar.cast .Usize (2047#u16 &&& y) = pow5b by rfl]
rw [show UScalar.cast .Usize high16 = numNew by rfl]
have hloTable : pow5a.val <= pow5Table.val.length := by
simp [hpow5aVal, pow5Table]
omega
have hlo1308 : pow5a.val <= 1308 := by
rw [hpow5aVal]
omega
let suffix : Slice Std.U8 := pow5Table.to_slice.drop pow5a
have hsuffix :
core.array.Array.index
(core.ops.index.IndexSlice
(core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8))
pow5Table { start := pow5a } = ok suffix := by
rw [Array.index_SliceIndexRangeFromUsizeSlice]
unfold core.slice.index.SliceIndexRangeFromUsizeSlice.index
simp [hlo1308, suffix]
rw [hsuffix]
simp only [Bind.bind, Aeneas.Std.bind]
unfold core.slice.Slice.iter
simp only [Bind.bind, Aeneas.Std.bind]
unfold core.slice.iter.IteratorSliceIter.enumerate
simp only [Bind.bind, Aeneas.Std.bind]
have hsubBound : pow5a.val <= pow5b.val := by
rw [hpow5aVal, hpow5bVal]
exact hloidx'
obtain ⟨span, hspan, hspanVal⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := pow5b) (y := pow5a) hsubBound)
rw [hspan]
simp only [Bind.bind, Aeneas.Std.bind]
unfold core.iter.adapters.enumerate.IteratorEnumerate.take
simp only [Bind.bind, Aeneas.Std.bind]
have hspanDs : span.val = ds.length := by
rw [hspanVal.1, hpow5aVal, hpow5bVal]
omega
have hell : span.val <= suffix.val.length := by
simp [suffix, Slice.drop, Array.to_slice, pow5Table, hspanVal.1,
hpow5aVal, hpow5bVal, List.length_drop]
omega
have hdiffBound : hiNext - lo <= 1308 - lo :=
Nat.sub_le_sub_right hhiidx' lo
have hseq : (suffix.val.take span.val).map (fun z => z.val) = ds := by
simp [suffix, Slice.drop, Array.to_slice, hspanDs, ds, dsU,
pow5Table, hpow5aVal, Nat.min_eq_left hdiffBound]
have hfive : 5 ^ shift.val % 10 = 5 := by
have hs : forall q : Nat, 5 ^ (q + 1) % 10 = 5 := by
intro q
induction q with
| zero => decide
| succ q ih =>
rw [Nat.pow_succ, Nat.mul_mod, ih]
obtain ⟨q, hq⟩ := Nat.exists_eq_add_of_le hkpos
rw [hq]
simpa [Nat.add_comm] using hs q
have hdslast : forall hne : ds ≠ [], ds.getLast hne ≠ 0 := by
intro hne hz
have hreconstruct := List.dropLast_append_getLast hne
have hmod := congrArg (fun n : Nat => n % 10) hfold'
change List.foldl (fun n z => 10 * n + z) 0 ds % 10 =
5 ^ shift.val % 10 at hmod
rw [hfive] at hmod
rw [← hreconstruct] at hmod
simp [List.foldl_append, hz] at hmod
let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
have hdigits : forall z, z ∈ digits -> z < 10 := by
intro z hz
simp only [digits, List.mem_map] at hz
rcases hz with ⟨u, hu, rfl⟩
exact hdigit u hu
have hlex := DecimalDigitLexScale digits ds hdigits hdsdigit' hdslast
let value := digits.foldl (fun a z => 10 * a + z) 0
have hdigitsLen : digits.length = d.num_digits.val := by
simp [digits]
have hdlen : d.digits.val.length = 768 := d.digits.property
omega
have hkSplit : shift.val = (h - 1) + ds.length := by
omega
have hpowN : 10 ^ (d.num_digits.val + h - 1) =
10 ^ digits.length * 10 ^ (h - 1) := by
rw [show d.num_digits.val + h - 1 =
d.num_digits.val + (h - 1) by omega, Nat.pow_add, hdigitsLen]
have hpowK : 10 ^ shift.val = 10 ^ (h - 1) * 10 ^ ds.length := by
rw [hkSplit, Nat.pow_add]
have htwofive : 2 ^ shift.val * 5 ^ shift.val = 10 ^ shift.val := by
rw [← Nat.mul_pow]
have hthreshold :
10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val ↔
5 ^ shift.val * 10 ^ digits.length <= value * 10 ^ ds.length := by
constructor
· intro ht
have hm := Nat.mul_le_mul_left (5 ^ shift.val) ht
apply Nat.le_of_mul_le_mul_left (c := 10 ^ (h - 1)) ?_ (by positivity)
calc
10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) =
5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) := by
rw [hpowN]
ring
_ <= 5 ^ shift.val * (value * 2 ^ shift.val) := hm
_ = 10 ^ (h - 1) * (value * 10 ^ ds.length) := by
rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
value * (2 ^ shift.val * 5 ^ shift.val) by ring,
htwofive, hpowK]
ring
· intro ht
have hm := Nat.mul_le_mul_left (10 ^ (h - 1)) ht
apply Nat.le_of_mul_le_mul_left (c := 5 ^ shift.val) ?_ (by positivity)
calc
5 ^ shift.val * 10 ^ (d.num_digits.val + h - 1) =
10 ^ (h - 1) * (5 ^ shift.val * 10 ^ digits.length) := by
rw [hpowN]
ring
_ <= 10 ^ (h - 1) * (value * 10 ^ ds.length) := hm
_ = 5 ^ shift.val * (value * 2 ^ shift.val) := by
rw [show 5 ^ shift.val * (value * 2 ^ shift.val) =
value * (2 ^ shift.val * 5 ^ shift.val) by ring,
htwofive, hpowK]
ring
have hlex' : List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds ↔
value * 10 ^ ds.length < 5 ^ shift.val * 10 ^ digits.length := by
simpa [value, hfold'] using hlex
obtain ⟨out, hout, houtVal⟩ := DecimalLeftShiftLoopLex
d suffix span numNew hell hnumNewPos hn
refine ⟨out, hout, ?_⟩
rw [hseq, hspanDs] at houtVal
rw [houtVal]
change (if List.Lex (fun a b : Nat => a < b) (digits.take ds.length) ds
then numNew.val - 1 else numNew.val) =
if 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
then h else h - 1
by_cases ht : 10 ^ (d.num_digits.val + h - 1) <= value * 2 ^ shift.val
· have hs := hthreshold.mp ht
have hnlex : ¬ List.Lex (fun a b : Nat => a < b)
(digits.take ds.length) ds := by
intro hl
have hlt := hlex'.mp hl
omega
simp [ht, hnlex, hnumNewVal]
· have hns : ¬ 5 ^ shift.val * 10 ^ digits.length <=
value * 10 ^ ds.length := by
intro hs
exact ht (hthreshold.mpr hs)
have hlt : value * 10 ^ ds.length <
5 ^ shift.val * 10 ^ digits.length := by omega
have hl := hlex'.mpr hlt
simp [ht, hl, hnumNewVal]
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Used by
Inspect related source (49)
More program dependencies (16)
- body · dec2flt/decimal_seq.rs:434 Program Rust
- TABLE · dec2flt/decimal_seq.rs:370 Program Rust
- TABLE_POW5 · dec2flt/decimal_seq.rs:379 Program Rust
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- BiasedFp · dec2flt/common.rs:52 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 Program Rust
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
Standard-library dependencies (30)
- clone · core/src/clone.rs:698 Rust library
- div · core/src/ops/arith.rs:515 Rust library
- mul · core/src/ops/arith.rs:350 Rust library
- neg · core/src/ops/arith.rs:719 Rust library
- bitand · core/src/ops/bit.rs:178 Rust library
- bitor · core/src/ops/bit.rs:282 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- shr · core/src/ops/bit.rs:612 Rust library
- EXPONENT_MASK · core/src/num/f64.rs:615 Rust library
- INFINITY · core/src/num/f64.rs:512 Rust library
- MANTISSA_DIGITS · core/src/num/f64.rs:412 Rust library
- MANTISSA_MASK · core/src/num/f64.rs:637 Rust library
- NAN · core/src/num/f64.rs:509 Rust library
- NEG_INFINITY · core/src/num/f64.rs:515 Rust library
- classify · core/src/num/f64.rs:782 Rust library
- from_bits · core/src/num/f64.rs:1269 Rust library
- to_bits · core/src/num/f64.rs:1223 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 Rust library
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- first · core/src/slice/mod.rs:155 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
Related source and models come from the extraction records and declared dependencies.
Proof assumptions and review
Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.
Transitive assumptions
- Classical.choice
- Quot.sound
- propext
This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.
Helper theorems included
- DecimalDigitLexScale
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftLoopLex
- DecimalLeftShiftTableCertificate
- num imp dec2flt decimal seq DecimalSeq
- num imp dec2flt decimal seq number of digits decimal left shift
All 8 entries
- DecimalDigitLexScale
- DecimalLeftShiftDigitGrowth
- DecimalLeftShiftLoopLex
- DecimalLeftShiftTableCertificate
- num imp dec2flt decimal seq DecimalSeq
- num imp dec2flt decimal seq number of digits decimal left shift
- num imp dec2flt decimal seq number of digits decimal left shift TABLE
- num imp dec2flt decimal seq number of digits decimal left shift TABLE POW5
Technical checking records
Local closure summary
{
"node": "DecimalLeftShiftDigitGrowth",
"closure_version": "patch_c_v1",
"active_decl_hash": "7153166e21d5312372f8071d8ff40068f022258fbfd0e8c750040a53a7fd9906",
"active_statement_hash": "372457485bbca360df9d83ca146a5a607beefa3e63fbd1e932a35b81552cfc9e",
"kernel_axioms": [
"Classical.choice",
"Quot.sound",
"propext"
],
"toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
"lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
"checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
"approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-154",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalDigitLexScale": "d2942644d8f7d862",
"DecimalLeftShiftLoopLex": "e280c31921852652",
"DecimalLeftShiftTableCertificate": "3c85f76c60cebc6c",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "3703c132ca68c95c"
},
"strict_definition_deps": {
"num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE": "e68bec96fec99700",
"num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE_POW5": "4dda92c1af5f2c9e"
}
}Prose assessment
{
"label": "Sound flag superseded by recorded Lean closure",
"assessment_status": "ReviewerPinnedFail",
"fingerprint_relation": "matches current fingerprint",
"assessment_fingerprint": "sound-v2:1223999f4f99cd0b23d792560fa4861f949a1bb73e7ea5d800bd223f38795060",
"current_fingerprint": "sound-v2:1223999f4f99cd0b23d792560fa4861f949a1bb73e7ea5d800bd223f38795060",
"superseded_for_theorem_closure": true
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Fail",
"substantiveness_status": "Pass"
}