Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Theorem
parse_number returns normally with
Some record d, then d.\mathit{negative}=\texttt{false} and
d.\mathit{mantissa}<10^{19}.Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.Assumptions
import Tablet.ModelOptionBranch
import Tablet.ModelOptionFromResidual
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManyExact
import Tablet.NumberHelperDigitScanExact
import Tablet.NumberHelperDigitScanSourceLength
import Tablet.NumberHelperOffsetSourceLength
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.SourceValidByteSlice
import Tablet.U64_Insts_CoreOpsBitBitAndU64U64
import Tablet.U64_Insts_CoreOpsBitBitOrU64U64
import Tablet.num_imp_dec2flt_decimal_Decimal
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: NumberHelperRecordBounds]
theorem NumberHelperRecordBounds :
∀ (s : Slice Std.U8) d, SourceValidByteSlice s -> s.val ≠ [] ->
num.imp.dec2flt.parse.parse_number s = ok (some d) ->
d.negative = false ∧ d.mantissa.val < 10 ^ 19 := 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
GOAL.md is 10^{19}. Put
L=|s|. By SourceValidByteSlice, L\leq2^{63}-1, and
Assumptions fixes both modeled pointer-sized widths at 64 bits;
the nonemptiness premise also discharges the initial nonempty assertion in the
partial parser. First unfold
num imp dec2flt parse parse number. Its only route to
ok (some d) is that
num imp dec2flt parse parse partial number returned
ok (some (d,\ell)) and that the final test established
\ell=L. The former definition constructs d, and the latter definition
only tests \ell and returns the same record. It therefore suffices to bound
every record literal in parse_partial_number.copy_from_slice both
have length eight, and from_le_bytes returns
0x4646464646464646, 0x3030303030303030, and
0x8080808080808080; its conjunction and disjunction have the bounded
word meanings in U64 Insts CoreOpsBitBitAndU64U64,
U64 Insts CoreOpsBitBitOrU64U64,
ModelU64BitAnd, and ModelU64BitOr. To rule out false
positives, let c_j be the carry into byte j of the wrapping addition by
0x46 in every byte, and let r_j be the borrow into byte j of the
wrapping subtraction by 0x30 in every byte. Both initial values are
zero. Acceptance says that every result byte of both words has zero high bit,
because the high-bit mask of their OR is zero. Inducting from the low byte in
the subtraction, if r_j=0 and b_j<48, its result is
b_j-48+256\in[208,255], a contradiction. Hence b_j\geq48, the outgoing
borrow is zero, and the zero high bit of b_j-48 also gives b_j\leq175.
Inducting through the addition next, if c_j=0 and b_j\geq58, then the
established upper bound gives b_j+70\in[128,245], again a contradiction.
Thus b_j\leq57, and b_j+70\leq127 gives zero outgoing carry. Consequently
the test returns true only if
u32 succeeds and
the final widening cast to u64 preserves it. We have proved that
parse_8digits returns the ordinary base-ten value of every guarded
accepted block, with all checked products, sums, shifts, and casts accounted
for.u64 cast is exact, and the new
ideal value is 10x+e. Every intermediate ideal prefix is no larger than
the final nonnegative value. Since
parse_partial_number. Let I be the consecutive initial digit
sequence, let F be the consecutive sequence after byte 46 if that byte
immediately follows I (and the empty sequence otherwise), and put
D=|I|+|F|. Here the optional-point test uses the exact empty-versus-head/tail
decomposition of ModelSliceSplitFirst. The two returned slices and
SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from
show that n_digits1 is exactly D: both suffix lengths are at most
L\leq2^{63}-1, and the 64-bit widths from Assumptions show that the
usize-to-isize casts and their
nonnegative differences are in range; the optional fractional negation and
cast to i64 are also in range. The two digit runs are disjoint
subranges of the input, so D\leq L and the checked addition forming
n_digits1 also succeeds. If D\leq19, the concatenation
IF has at most nineteen digit positions, so its ordinary value is at most
10^D-1\leq10^{19}-1. The scanner invariant proves
mantissa1<10^{19} in every such branch.mantissa1.
All accesses and increments are guarded by L. Since D>19, the initial
count is at least one and at most L-19; at most L zero bytes can be
subtracted, so every count is at least 1-L\geq1-(2^{63}-1) and at most
2^{63}-1. By the 64-bit signed range supplied by
Assumptions, every checked signed subtraction succeeds.mantissa1=0 immediately. Otherwise the exact formula above gives
D-z\leq19. Before the first nonzero digit, every scalar or eight-byte
update appends only zeros to accumulator zero. From that digit onward there
are at most nineteen positions, including internal and trailing zeros. Hence
the full ordinary value of IF is at most 10^{19}-1, and the same scanner
invariant gives mantissa1<10^{19} even though D itself exceeds
nineteen. Conversely, an adjusted-positive outcome is impossible in the
all-zero case; in the other case its exact value D-19-z>0 gives
D-z\geq20.isize-to-i64
cast is exact. The later checked addition of the explicit scientific
exponent may fail on very long inputs, but a record-producing execution is by
hypothesis successful; that addition changes only the exponent and cannot
change m'. Consequently either return route of
parse_many_digits has mantissa below 10^{19}, including the
initial scan, the one-byte-skip retry, and every offset, slice, cast, and
checked arithmetic operation relevant to reaching its result.some val to
successful Continue val, which can reach a record literal, whereas it
maps none to successful Break none; then
ModelOptionFromResidual converts that sole residual to successful
none. Thus the Break route cannot produce the hypothesized d, and
the continued value is used only in exponent arithmetic. In each of the four
cases the definition then
has exactly these three record-producing alternatives:
negative := false. By the extracted structure type
num imp dec2flt decimal Decimal, each displayed literal's
negative and mantissa fields are exactly the projections
d.\mathit{negative} and d.\mathit{mantissa} in the theorem. Final
consumed-length offsets and casts may
affect only the paired length, and scientific parsing and exponent additions
may affect only the exponent. Therefore the record d passed unchanged
through parse_number satisfies both asserted conjuncts.Lean proof
intro s d hsource hne hparse
have hlen : s.val.length <= 9223372036854775807 := by
simpa [SourceValidByteSlice] using hsource
have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha)
(f : alpha -> Result beta) y,
Bind.bind r f = ok y -> exists x, r = ok x /\ f x = ok y := by
intro alpha beta r f y h
cases r with
| fail err => simp [Bind.bind, Aeneas.Std.bind] at h
| div => simp [Bind.bind, Aeneas.Std.bind] at h
| ok x => exact ⟨x, rfl, h⟩
have split_digits : forall xs : List Std.U8,
exists digits tail,
xs = digits ++ tail /\
(forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) /\
(forall b rest, tail = b :: rest ->
Not (48 <= b.val /\ b.val <= 57)) := by
intro xs
induction xs with
| nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), (by
intro b rest h
cases h)⟩
| cons b bs ih =>
by_cases hb : 48 <= b.val /\ b.val <= 57
· obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := ih
refine ⟨b :: digits, tail, by simp [hsplit], ?_, htail⟩
intro c hc
rcases List.mem_cons.mp hc with rfl | hc
· exact hb
· exact hdigits c hc
· exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), (by
intro c rest h
cases h
exact hb)⟩
have parse_many_bound : forall (start intEnd : Slice Std.U8)
(exp outExp : Std.I64) (mantissa : Std.U64),
num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
ok (mantissa, outExp) ->
mantissa.val <= 9999999999999999999 := by
intro start intEnd exp outExp mantissa hmany
obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := split_digits start.val
obtain ⟨rest, first, remaining, consumed, hfirst, hpartition,
hrest, hfirstValue, hremainingDigits, hremainingLen, hfirstBound,
hfirstLow, hfirstSharp⟩ :=
NumberHelperTryParse19SafeLength start 0#u64 digits tail hsplit
hdigits htail (by norm_num)
unfold num.imp.dec2flt.parse.parse_many_digits at hmany
rw [hfirst] at hmany
by_cases hthreshold : first.val < 1000000000000000000
· have hnotHigh : ¬ 1000000000000000000 <= first.val := by omega
simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
hnotHigh] at hmany
cases hdrop :
core.slice.index.SliceIndexRangeFromUsizeSlice.index
{ start := 1#usize } rest with
| fail err =>
rw [hdrop] at hmany
simp at hmany
| div =>
rw [hdrop] at hmany
simp at hmany
| ok afterDrop =>
rw [hdrop] at hmany
simp at hmany
obtain ⟨digits2, tail2, hsplit2, hdigits2, htail2⟩ :=
split_digits afterDrop.val
obtain ⟨rest2, second, remaining2, consumed2, hsecond,
hpartition2, hrest2, hsecondValue, hremainingDigits2,
hremainingLen2, hsecondBound, hsecondLow, hsecondSharp⟩ :=
NumberHelperTryParse19SafeLength afterDrop first digits2 tail2
hsplit2 hdigits2 htail2 hfirstBound
rw [hsecond] at hmany
simp at hmany
cases hoff :
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
rest2 afterDrop with
| fail err => simp [hoff] at hmany
| div => simp [hoff] at hmany
| ok off =>
simp [hoff] at hmany
cases hneg : HNeg.hNeg off with
| fail err => simp [hneg] at hmany
| div => simp [hneg] at hmany
| ok negOff =>
simp [hneg] at hmany
cases hcast : lift (IScalar.cast .I64 negOff) with
| fail err => simp [hcast] at hmany
| div => simp [hcast] at hmany
| ok castOff =>
simp [hcast] at hmany
cases hadd : castOff + exp with
| fail err => simp [hadd] at hmany
| div => simp [hadd] at hmany
| ok finalExp =>
simp [hadd] at hmany
rcases hmany with ⟨rfl, rfl⟩
exact hsecondBound
· have hhigh : 1000000000000000000 <= first.val := by omega
simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
hhigh] at hmany
cases hoff :
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
intEnd rest with
| fail err => simp [hoff] at hmany
| div => simp [hoff] at hmany
| ok off =>
simp [hoff] at hmany
cases hcast : lift (IScalar.cast .I64 off) with
| fail err => simp [hcast] at hmany
| div => simp [hcast] at hmany
| ok castOff =>
simp [hcast] at hmany
cases hadd : castOff + exp with
| fail err => simp [hadd] at hmany
| div => simp [hadd] at hmany
| ok finalExp =>
simp [hadd] at hmany
rcases hmany with ⟨rfl, rfl⟩
exact hfirstBound
have record_branches : forall (total : Std.Isize)
(shortMantissa : Std.U64) (intEnd : Slice Std.U8)
(shortExponent retryExponent : Std.I64) (len outLen : Std.Usize),
(total.val <= 19 -> shortMantissa.val <= 9999999999999999999) ->
(forall (nMore adjusted : Std.Isize),
total - 19#isize = ok nMore ->
num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
Not (total.val <= 19) ->
Not (0 < adjusted.val) ->
shortMantissa.val <= 9999999999999999999) ->
(if total.val <= 19 then
ok (some
(num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
false false, len))
else do
let nMore <- total - 19#isize
let adjusted <-
num.imp.dec2flt.parse.adjust_many_digit_count s nMore
if 0 < adjusted.val then
let (manyMantissa, manyExponent) <-
num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent
ok (some
(num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa
false true, len))
else
ok (some
(num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
false false, len))) = ok (some (d, outLen)) ->
d.negative = false /\ d.mantissa.val < 10 ^ 19 := by
intro total shortMantissa intEnd shortExponent retryExponent len outLen
hshort hnonpositive hresult
by_cases hsmall : total.val <= 19
· simp [hsmall] at hresult
rcases hresult with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
have hb := hshort hsmall
norm_num
omega
· simp [hsmall] at hresult
obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult
obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult
by_cases hpositive : 0 < adjusted.val
· simp only [if_pos hpositive] at hresult
obtain ⟨manyResult, hmany, hresult⟩ :=
bind_ok_inv _ _ _ hresult
rcases manyResult with ⟨manyMantissa, manyExponent⟩
rcases hresult with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
have hb := parse_many_bound s intEnd retryExponent manyExponent
manyMantissa hmany
norm_num
omega
· simp only [if_neg hpositive] at hresult
rcases hresult with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
have hb := hnonpositive nMore adjusted hsub hadjust hsmall hpositive
norm_num
omega
have fold_upper : forall (digits : List Std.U8) (a : Nat),
(forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
digits.foldl (fun x b => 10 * x + (b.val - 48)) a <
(a + 1) * 10 ^ digits.length := by
intro digits
induction digits with
| nil => intro a h; simp
| cons b bs ih =>
intro a h
have hb := h b List.mem_cons_self
have hbs : forall c, List.Mem c bs ->
48 <= c.val /\ c.val <= 57 := by
intro c hc
exact h c (List.mem_cons_of_mem b hc)
simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
have hrec := ih (10 * a + (b.val - 48)) hbs
have hcoeff : 10 * a + (b.val - 48) + 1 <= 10 * (a + 1) := by
omega
have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff
exact hrec.trans_le (by
simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul)
have fold_ge_start : forall (digits : List Std.U8) (a : Nat),
a <= digits.foldl (fun x b => 10 * x + (b.val - 48)) a := by
intro digits
induction digits with
| nil => intro a; rfl
| cons b bs ih =>
intro a
apply le_trans (b := 10 * a + (b.val - 48))
· omega
· exact ih _
have zero_fold : forall digits : List Std.U8,
(forall b, List.Mem b digits -> b = 48#u8) ->
digits.foldl (fun x b => 10 * x + (b.val - 48)) 0 = 0 := by
intro digits hzero
induction digits with
| nil => rfl
| cons b bs ih =>
have hb := hzero b List.mem_cons_self
have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
intro c hc
exact hzero c (List.mem_cons_of_mem b hc)
simp [hb, ih hbs]
have split_zeros : forall digits : List Std.U8,
exists zeros tail,
digits = zeros ++ tail /\
(forall b, List.Mem b zeros -> b = 48#u8) /\
(tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by
intro digits
induction digits with
| nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), Or.inl rfl⟩
| cons b bs ih =>
by_cases hb : b = 48#u8
· obtain ⟨zeros, tail, hsplit, hzeros, htail⟩ := ih
refine ⟨b :: zeros, tail, by simp [hsplit], ?_, htail⟩
intro c hc
rcases List.mem_cons.mp hc with rfl | hc
· exact hb
· exact hzeros c hc
· exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc),
Or.inr ⟨b, bs, rfl, hb⟩⟩
have count_zeros : forall zeros : List Std.U8,
(forall b, List.Mem b zeros -> b = 48#u8) ->
zeros.count 48#u8 = zeros.length := by
intro zeros hzeros
induction zeros with
| nil => rfl
| cons b bs ih =>
have hb := hzeros b List.mem_cons_self
have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
intro c hc
exact hzeros c (List.mem_cons_of_mem b hc)
simp [hb, ih hbs]
have split_significand_zeros : forall
(integer fraction suffix : List Std.U8),
(forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
(forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) ->
Not (forall b, List.Mem b (integer ++ fraction) -> b = 48#u8) ->
exists rawZeros rawTail sigZeros sigTail,
integer ++ 46#u8 :: (fraction ++ suffix) = rawZeros ++ rawTail /\
(forall b, List.Mem b rawZeros -> b = 48#u8 \/ b = 46#u8) /\
(exists b rest, rawTail = b :: rest /\
b ≠ 48#u8 /\ b ≠ 46#u8) /\
integer ++ fraction = sigZeros ++ sigTail /\
(forall b, List.Mem b sigZeros -> b = 48#u8) /\
(exists b rest, sigTail = b :: rest /\ b ≠ 48#u8) /\
rawZeros.count 48#u8 = sigZeros.length := by
intro integer fraction suffix hinteger hfraction hnonzero
obtain ⟨intZeros, intSig, hintSplit, hintZeros, hintSig⟩ :=
split_zeros integer
rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩
· obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ :=
split_zeros fraction
rcases hfracSig with hfracNil | ⟨c, fracRest, hfracSigEq, hc⟩
· exfalso
apply hnonzero
intro x hx
rcases List.mem_append.mp hx with hx | hx
· rw [hintSplit, hintNil] at hx
exact hintZeros x (by simpa using hx)
· rw [hfracSplit, hfracNil] at hx
exact hfracZeros x (by simpa using hx)
· refine ⟨intZeros ++ 46#u8 :: fracZeros,
fracSig ++ suffix, intZeros ++ fracZeros, fracSig,
?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
· rw [hintSplit, hintNil, hfracSplit]
simp [List.append_assoc]
· intro x hx
rcases List.mem_append.mp hx with hx | hx
· exact Or.inl (hintZeros x hx)
· rcases List.mem_cons.mp hx with rfl | hx
· exact Or.inr rfl
· exact Or.inl (hfracZeros x hx)
· refine ⟨c, fracRest ++ suffix, by simp [hfracSigEq], hc, ?_⟩
have hcdigit := hfraction c (by
rw [hfracSplit, hfracSigEq]
exact List.mem_append_right fracZeros List.mem_cons_self)
intro hpoint
subst c
norm_num at hcdigit
· rw [hintSplit, hintNil, hfracSplit]
simp [List.append_assoc]
· intro x hx
rcases List.mem_append.mp hx with hx | hx
· exact hintZeros x hx
· exact hfracZeros x hx
· exact ⟨c, fracRest, hfracSigEq, hc⟩
· rw [List.count_append, List.count_cons,
count_zeros intZeros hintZeros,
count_zeros fracZeros hfracZeros]
norm_num
· refine ⟨intZeros, intSig ++ 46#u8 :: (fraction ++ suffix),
intZeros, intSig ++ fraction, ?_, ?_, ?_, ?_, hintZeros, ?_, ?_⟩
· rw [hintSplit]
simp [List.append_assoc]
· intro x hx
exact Or.inl (hintZeros x hx)
· refine ⟨b, intRest ++ 46#u8 :: (fraction ++ suffix),
by simp [hintSigEq], hb, ?_⟩
have hbdigit := hinteger b (by
rw [hintSplit, hintSigEq]
exact List.mem_append_right intZeros List.mem_cons_self)
intro hpoint
subst b
norm_num at hbdigit
· rw [hintSplit]
simp [List.append_assoc]
· exact ⟨b, intRest ++ fraction, by simp [hintSigEq], hb⟩
· exact count_zeros intZeros hintZeros
obtain ⟨intDigits, intTail, hsval, hintDigits, hintTail⟩ :=
split_digits s.val
have hintTailProp : intTail.length <= Usize.max := by
have hp := s.property
rw [hsval] at hp
simp only [List.length_append] at hp
omega
let intRest : Slice Std.U8 := ⟨intTail, hintTailProp⟩
obtain ⟨intMantissa, scannedIntRest, hscanInt, hscannedIntRest⟩ :=
NumberHelperDigitScanSourceLength s 0#u64 intDigits intTail hsval
hintDigits hintTail hlen
have hscannedIntRestEq : scannedIntRest = intRest :=
(Slice.eq_iff _ _).2 (by simpa [intRest] using hscannedIntRest)
subst scannedIntRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSourceLength s intRest hlen (by
rw [hsval]
simp [intRest])
have hnDigitsEq : nDigits.val = intDigits.length := by
rw [hnDigitsVal, hsval]
simp [intRest]
have hpartial : exists len,
num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := by
unfold num.imp.dec2flt.parse.parse_number at hparse
cases hp : num.imp.dec2flt.parse.parse_partial_number s with
| fail err => simp [hp] at hparse
| div => simp [hp] at hparse
| ok result =>
cases result with
| none => simp [hp] at hparse
| some pair =>
rcases pair with ⟨record, len⟩
simp [hp] at hparse
split at hparse
· have hrecord : record = d := by simpa using hparse
subst d
exact ⟨len, rfl⟩
· simp at hparse
obtain ⟨parsedLen, hpartial⟩ := hpartial
cases intTail with
| nil =>
have hsplitInt : core.slice.Slice.split_first intRest = ok none := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst, intRest]
have haddZero : nDigits + 0#isize = ok nDigits := by
have hb := IScalar.hBounds nDigits
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec (x := nDigits) (y := 0#isize) (by
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega) (by
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hb ⊢
omega))
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
simpa using hzval
have hfullOffset :
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
intRest s = ok nDigits := by
simpa [intRest] using hoffInt
have hcastLen : lift (IScalar.hcast .Usize nDigits) =
ok (Slice.len s) := by
have hbound : 0 <= nDigits.val /\
nDigits.val <= UScalar.max .Usize := by
rw [hnDigitsEq]
constructor
· omega
· have hp := s.property
rw [hsval] at hp
simpa [UScalar.max, Usize.max, Usize.numBits,
Dec2Flt.configured_x86_64_numBits] using hp
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize nDigits hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hnDigitsEq] at hzval
have hslen : s.val.length = intDigits.length := by simp [hsval]
simpa [Slice.len_val, hslen] using hzval
unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
simp [hne, hscanInt, hoffInt, hsplitInt, haddZero,
hcastLen] at hpartial
by_cases hnzero : nDigits = 0#isize
· simp [hnzero] at hpartial
· simp [hnzero] at hpartial
by_cases hsmall : nDigits.val <= 19
· simp [hsmall] at hpartial
rcases hpartial with ⟨rfl, rfl⟩
constructor
· rfl
· have hfoldLt := fold_upper intDigits 0 hintDigits
norm_num at hfoldLt
have hlen19 : intDigits.length <= 19 := by
omega
have hbound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
have hp := hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
norm_num at hp ⊢
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits []
(by simpa using hsval) hintDigits (by
intro b rest h
cases h) hlen hbound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
rw [hvalue]
norm_num
exact hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
· have hlarge : 19 < nDigits.val := by omega
have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\
nDigits.val - 19 <= IScalar.max .Isize := by
have hb := IScalar.hBounds nDigits
simp [IScalar.min, IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hb ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := nDigits) (y := 19#isize)
(by norm_num; omega) (by norm_num; omega))
norm_num at hnMoreVal
simp [hsmall, hnMore] at hpartial
obtain ⟨zeros, nonzeroTail, hzeroSplit, hzeros, hzeroTail⟩ :=
split_zeros intDigits
have hcount := count_zeros zeros hzeros
have hsAdjust : s.val = zeros ++ nonzeroTail := by
simpa [hzeroSplit] using hsval
have htailSharp : nonzeroTail = [] \/ exists b rest,
nonzeroTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
rcases hzeroTail with hnil | ⟨b, rest, htailEq, hb⟩
· exact Or.inl hnil
· right
refine ⟨b, rest, htailEq, hb, ?_⟩
have hbdigit := hintDigits b (by
rw [hzeroSplit, htailEq]
exact List.mem_append_right zeros List.mem_cons_self)
intro heq
subst b
norm_num at hbdigit
have hnMoreNonneg : 0 <= nMore.val := by
rw [hnMoreVal]
omega
have hnMoreLe : nMore.val <= s.val.length := by
rw [hnMoreVal, hnDigitsEq, hsval]
simp only [List.length_append, List.length_nil, Nat.add_zero]
omega
have hadjustLower : IScalar.min .Isize <=
nMore.val - (zeros.count 48#u8 : Int) := by
rw [hnMoreVal, hcount, hnDigitsEq]
have hzerosLen : zeros.length <= intDigits.length := by
rw [hzeroSplit]
simp
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨adjusted, hadjust, hadjustVal⟩ :=
NumberHelperAdjustManyExact s nMore zeros nonzeroTail hsAdjust
(by intro b hb; exact Or.inl (hzeros b hb)) htailSharp hlen
hnMoreNonneg hnMoreLe hadjustLower
rw [hadjust] at hpartial
by_cases hpositive : 0 < adjusted.val
· simp [hpositive] at hpartial
cases hmany :
num.imp.dec2flt.parse.parse_many_digits s intRest 0#i64 with
| fail err => simp [hmany] at hpartial
| div => simp [hmany] at hpartial
| ok result =>
rcases result with ⟨manyMantissa, manyExponent⟩
simp [hmany] at hpartial
rcases hpartial with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
change manyMantissa.val < 10 ^ 19
have hb := parse_many_bound s intRest 0#i64 manyExponent
manyMantissa hmany
omega
· simp [hpositive] at hpartial
have htailLen : nonzeroTail.length <= 19 := by
rw [hadjustVal, hnMoreVal, hcount, hnDigitsEq] at hpositive
have hlenEq := congrArg List.length hzeroSplit
simp only [List.length_append] at hlenEq
omega
have htailDigits : forall b, List.Mem b nonzeroTail ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
apply hintDigits b
rw [hzeroSplit]
exact List.mem_append_right zeros hb
have hzeroFold := zero_fold zeros hzeros
have hfoldLt : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
rcases hzeroTail with hnil | hcons
· subst nonzeroTail
rw [hzeroSplit, List.append_nil, hzeroFold]
norm_num
· rw [hzeroSplit, List.foldl_append, hzeroFold]
have htailLt := fold_upper nonzeroTail 0 htailDigits
norm_num at htailLt
exact htailLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
htailLen)
have hfoldBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits []
(by simpa using hsval) hintDigits (by
intro b rest h
cases h) hlen hfoldBound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
rcases hpartial with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
norm_num at hvalue ⊢
rw [hvalue]
exact hfoldLt
| cons first tail =>
have htailProp : tail.length <= Usize.max := by
have hp := intRest.property
simp [intRest] at hp ⊢
omega
let afterFirst : Slice Std.U8 := ⟨tail, htailProp⟩
have hsplitInt : core.slice.Slice.split_first intRest =
ok (some (first, afterFirst)) := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
intRest, afterFirst]
by_cases hpoint : first = 46#u8
· subst first
obtain ⟨fracDigits, fracTail, hfracSplit, hfracDigits, hfracTail⟩ :=
split_digits tail
have hfracTailProp : fracTail.length <= Usize.max := by
have hp := afterFirst.property
simp [afterFirst, hfracSplit] at hp ⊢
omega
let fracRest : Slice Std.U8 := ⟨fracTail, hfracTailProp⟩
have hlenAfter : afterFirst.val.length <= 9223372036854775807 := by
simp [afterFirst]
rw [hsval] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega
obtain ⟨fracMantissa, scannedFracRest, hscanFrac, hscannedFracRest⟩ :=
NumberHelperDigitScanSourceLength afterFirst intMantissa fracDigits
fracTail (by simp [afterFirst, hfracSplit]) hfracDigits hfracTail
hlenAfter
have hscannedFracRestEq : scannedFracRest = fracRest :=
(Slice.eq_iff _ _).2 (by
simpa [fracRest] using hscannedFracRest)
subst scannedFracRest
obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
NumberHelperOffsetSourceLength afterFirst fracRest hlenAfter (by
simp [afterFirst, fracRest, hfracSplit])
have hnAfterEq : nAfter.val = fracDigits.length := by
rw [hnAfterVal]
simp [afterFirst, fracRest, hfracSplit]
have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
rw [hnAfterEq, IScalar.min]
simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(HNeg.hNeg.step nAfter hnAfterNotMin)
have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
negAfter.val <= IScalar.max .I64 := by
rw [hnegAfterVal, hnAfterEq,
IScalar.min_IScalarTy_I64_eq, I64.min_eq,
IScalar.max_IScalarTy_I64_eq, I64.max_eq]
constructor <;> omega
obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
have htotalBounds : IScalar.min .Isize <=
nDigits.val + nAfter.val /\
nDigits.val + nAfter.val <= IScalar.max .Isize := by
rw [hnDigitsEq, hnAfterEq]
simp [IScalar.min, IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
rw [hsval, hfracSplit] at hlen
simp only [List.length_append, List.length_cons] at hlen
constructor <;> omega
obtain ⟨totalDigits, htotal, htotalVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec htotalBounds.1 htotalBounds.2)
have hsigDigits : forall b,
List.Mem b (intDigits ++ fracDigits) ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
rcases List.mem_append.mp hb with hb | hb
· exact hintDigits b hb
· exact hfracDigits b hb
have hexactScans :
(intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max ->
fracMantissa.val = (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 := by
intro hfullBound
have hprefixLe := fold_ge_start fracDigits
(intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0)
rw [← List.foldl_append] at hprefixLe
have hintBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max :=
hprefixLe.trans hfullBound
obtain ⟨intValue, intScanRest, hexactInt, hexactIntRest,
hintValue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits
(46#u8 :: tail) (by simpa using hsval) hintDigits hintTail
hlen hintBound
rw [hscanInt] at hexactInt
simp at hexactInt
rcases hexactInt with ⟨rfl, rfl⟩
have hfracBound : fracDigits.foldl
(fun x b => 10 * x + (b.val - 48)) intMantissa.val <=
U64.max := by
norm_num at hintValue
rw [hintValue, ← List.foldl_append]
exact hfullBound
obtain ⟨fracValue, fracScanRest, hexactFrac, hexactFracRest,
hfracValue⟩ :=
NumberHelperDigitScanExact afterFirst intMantissa fracDigits
fracTail (by simp [afterFirst, hfracSplit]) hfracDigits
hfracTail hlenAfter hfracBound
rw [hscanFrac] at hexactFrac
simp at hexactFrac
rcases hexactFrac with ⟨rfl, rfl⟩
norm_num at hintValue hfracValue ⊢
rw [hfracValue, hintValue]
have hshort : totalDigits.val <= 19 ->
fracMantissa.val <= 9999999999999999999 := by
intro hsmall
have hsigLen : intDigits.length + fracDigits.length <= 19 := by
rw [htotalVal, hnDigitsEq, hnAfterEq] at hsmall
omega
have hfoldLt := fold_upper (intDigits ++ fracDigits) 0 hsigDigits
norm_num at hfoldLt
have hfold19 := hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hsigLen)
have hfoldBound : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
norm_num
omega
rw [hexactScans hfoldBound]
norm_num
omega
have hnonpositive : forall (nMore adjusted : Std.Isize),
totalDigits - 19#isize = ok nMore ->
num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
Not (totalDigits.val <= 19) ->
Not (0 < adjusted.val) ->
fracMantissa.val <= 9999999999999999999 := by
intro nMore adjusted hsub hadjust hlarge hnonpos
by_cases hallzero : forall b,
List.Mem b (intDigits ++ fracDigits) -> b = 48#u8
· have hfoldZero := zero_fold (intDigits ++ fracDigits) hallzero
have hfoldBound : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [hfoldZero]
exact Nat.zero_le _
rw [hexactScans hfoldBound, hfoldZero]
norm_num
· obtain ⟨rawZeros, rawTail, sigZeros, sigTail, hrawSplit,
hrawZeros, hrawTail, hsigSplit, hsigZeros, hsigTail, hcount⟩ :=
split_significand_zeros intDigits fracDigits fracTail
hintDigits hfracDigits hallzero
have hsAdjust : s.val = rawZeros ++ rawTail := by
rw [hsval, hfracSplit]
exact hrawSplit
have hnMoreVal : nMore.val = totalDigits.val - 19 := by
have heq := IScalar.sub_equiv totalDigits 19#isize
rw [hsub] at heq
have hval := heq.2.1
norm_num at hval ⊢
exact hval
have hnMoreNonneg : 0 <= nMore.val := by
rw [hnMoreVal]
omega
have hnMoreLe : nMore.val <= s.val.length := by
rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq, hsval,
hfracSplit]
simp only [List.length_append, List.length_cons]
omega
have hadjustLower : IScalar.min .Isize <=
nMore.val - (rawZeros.count 48#u8 : Int) := by
rw [hnMoreVal, hcount, htotalVal, hnDigitsEq, hnAfterEq]
have hzlen : sigZeros.length <=
(intDigits ++ fracDigits).length := by
rw [hsigSplit]
simp
have hsigSource : (intDigits ++ fracDigits).length <=
s.val.length := by
rw [hsval, hfracSplit]
simp only [List.length_append, List.length_cons]
omega
have hzBound : sigZeros.length <= 9223372036854775807 :=
hzlen.trans (hsigSource.trans hlen)
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
NumberHelperAdjustManyExact s nMore rawZeros rawTail hsAdjust
hrawZeros (Or.inr hrawTail) hlen hnMoreNonneg hnMoreLe
hadjustLower
rw [hadjust] at hadjust2
simp at hadjust2
subst adjusted2
have hsigTailLen : sigTail.length <= 19 := by
rw [hadjust2Val, hnMoreVal, hcount, htotalVal, hnDigitsEq,
hnAfterEq] at hnonpos
have hlenEq := congrArg List.length hsigSplit
simp only [List.length_append] at hlenEq
omega
have hsigTailDigits : forall b, List.Mem b sigTail ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
apply hsigDigits b
rw [hsigSplit]
exact List.mem_append_right sigZeros hb
have hzeroFold := zero_fold sigZeros hsigZeros
have htailLt := fold_upper sigTail 0 hsigTailDigits
norm_num at htailLt
have hfoldLt : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
rw [hsigSplit, List.foldl_append, hzeroFold]
exact htailLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
hsigTailLen)
have hfoldBound : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
exact (Nat.le_of_lt hfoldLt).trans (by
norm_num [U64.max_eq])
rw [hexactScans hfoldBound]
norm_num at hfoldLt ⊢
omega
unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
simp [hne, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac,
hnegAfter, hfracExponent] at hpartial
simp only [UScalar.ofNatCore] at hpartial
simp [htotal] at hpartial
by_cases hzero : totalDigits = 0#isize
· simp [hzero] at hpartial
· simp [hzero] at hpartial
cases fracTail with
| nil =>
have hsplitFrac : core.slice.Slice.split_first fracRest =
ok none := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
fracRest]
simp [hsplitFrac] at hpartial
obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest fracExponent
0#i64 len parsedLen hshort hnonpositive hpartial
| cons marker suffix =>
have hsuffixProp : suffix.length <= Usize.max := by
have hp := fracRest.property
simp [fracRest] at hp ⊢
omega
let afterMarker : Slice Std.U8 := ⟨suffix, hsuffixProp⟩
have hsplitFrac : core.slice.Slice.split_first fracRest =
ok (some (marker, afterMarker)) := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
fracRest, afterMarker]
simp [hsplitFrac] at hpartial
by_cases hlower : marker = (⟨101#8⟩ : Std.U8)
· subst marker
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· by_cases hupper : marker = (⟨69#8⟩ : Std.U8)
· subst marker
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· simp [hlower, hupper] at hpartial
obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest
fracExponent 0#i64 len parsedLen hshort hnonpositive hpartial
· have haddZero : nDigits + 0#isize = ok nDigits := by
have hb := IScalar.hBounds nDigits
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec (x := nDigits) (y := 0#isize) (by
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega) (by
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hb ⊢
omega))
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
simpa using hzval
have hshort : nDigits.val <= 19 ->
intMantissa.val <= 9999999999999999999 := by
intro hsmall
have hlen19 : intDigits.length <= 19 := by omega
have hfoldLt := fold_upper intDigits 0 hintDigits
norm_num at hfoldLt
have hfold19 : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 :=
hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
have hfoldBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
(by simpa using hsval) hintDigits hintTail hlen hfoldBound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
norm_num at hvalue ⊢
rw [hvalue]
omega
have hnonpositive : forall (nMore adjusted : Std.Isize),
nDigits - 19#isize = ok nMore ->
num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
Not (nDigits.val <= 19) ->
Not (0 < adjusted.val) ->
intMantissa.val <= 9999999999999999999 := by
intro nMore adjusted hsub hadjust hlarge hnonpos
have hnMoreVal : nMore.val = nDigits.val - 19 := by
have heq := IScalar.sub_equiv nDigits 19#isize
rw [hsub] at heq
have hval := heq.2.1
norm_num at hval ⊢
exact hval
obtain ⟨zeros, nzTail, hzeroSplit, hzeros, hnzTail⟩ :=
split_zeros intDigits
have hcount := count_zeros zeros hzeros
let rawTail : List Std.U8 := nzTail ++ first :: tail
have hsAdjust : s.val = zeros ++ rawTail := by
rw [hsval, hzeroSplit]
simp [rawTail, List.append_assoc]
have hrawTail : rawTail = [] \/ exists b rest,
rawTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
right
rcases hnzTail with hnil | ⟨b, rest, heq, hb⟩
· subst nzTail
refine ⟨first, tail, by simp [rawTail], ?_, hpoint⟩
intro hzero
subst first
have hnot := hintTail 48#u8 tail rfl
norm_num at hnot
· refine ⟨b, rest ++ first :: tail, by simp [rawTail, heq], hb, ?_⟩
have hbdigit := hintDigits b (by
rw [hzeroSplit, heq]
exact List.mem_append_right zeros List.mem_cons_self)
intro heq46
subst b
norm_num at hbdigit
have hnMoreNonneg : 0 <= nMore.val := by
rw [hnMoreVal]
omega
have hnMoreLe : nMore.val <= s.val.length := by
rw [hnMoreVal, hnDigitsEq, hsval]
simp only [List.length_append, List.length_cons]
omega
have hadjustLower : IScalar.min .Isize <=
nMore.val - (zeros.count 48#u8 : Int) := by
rw [hnMoreVal, hcount, hnDigitsEq]
have hzlen : zeros.length <= intDigits.length := by
rw [hzeroSplit]
simp
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
NumberHelperAdjustManyExact s nMore zeros rawTail hsAdjust
(by intro b hb; exact Or.inl (hzeros b hb)) hrawTail hlen
hnMoreNonneg hnMoreLe hadjustLower
rw [hadjust] at hadjust2
simp at hadjust2
subst adjusted2
have hnzLen : nzTail.length <= 19 := by
rw [hadjust2Val, hnMoreVal, hcount, hnDigitsEq] at hnonpos
have hlenEq := congrArg List.length hzeroSplit
simp only [List.length_append] at hlenEq
omega
have hnzDigits : forall b, List.Mem b nzTail ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
apply hintDigits b
rw [hzeroSplit]
exact List.mem_append_right zeros hb
have hzeroFold := zero_fold zeros hzeros
have hfoldLt : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
rcases hnzTail with hnil | hcons
· subst nzTail
rw [hzeroSplit, List.append_nil, hzeroFold]
norm_num
· rw [hzeroSplit, List.foldl_append, hzeroFold]
have htailLt := fold_upper nzTail 0 hnzDigits
norm_num at htailLt
exact htailLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hnzLen)
have hfoldBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
(by simpa using hsval) hintDigits hintTail hlen hfoldBound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
norm_num at hvalue ⊢
rw [hvalue]
omega
have hpointVal : first.val ≠ 46 := by
intro hval
apply hpoint
exact UScalar.eq_of_val_eq hval
unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
simp [hne, hscanInt, hoffInt, hsplitInt] at hpartial
split at hpartial
all_goals try { exfalso; apply hpointVal; rfl }
simp [haddZero] at hpartial
by_cases hnzero : nDigits = 0#isize
· simp [hnzero] at hpartial
· simp [hnzero, hsplitInt] at hpartial
by_cases hlower : first = 101#u8
· subst first
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches nDigits intMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· by_cases hupper : first = 69#u8
· simp [hlower] at hpartial
subst first
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches nDigits intMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· simp [hlower, hupper] at hpartial
rw [hoffInt] at hpartial
simp at hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches nDigits intMantissa intRest 0#i64 0#i64
len parsedLen hshort hnonpositive hpartial
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.Assumptions
import Tablet.ModelOptionBranch
import Tablet.ModelOptionFromResidual
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManyExact
import Tablet.NumberHelperDigitScanExact
import Tablet.NumberHelperDigitScanSourceLength
import Tablet.NumberHelperOffsetSourceLength
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.SourceValidByteSlice
import Tablet.U64_Insts_CoreOpsBitBitAndU64U64
import Tablet.U64_Insts_CoreOpsBitBitOrU64U64
import Tablet.num_imp_dec2flt_decimal_Decimal
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: NumberHelperRecordBounds]
theorem NumberHelperRecordBounds :
∀ (s : Slice Std.U8) d, SourceValidByteSlice s -> s.val ≠ [] ->
num.imp.dec2flt.parse.parse_number s = ok (some d) ->
d.negative = false ∧ d.mantissa.val < 10 ^ 19 := by
-- BODY
intro s d hsource hne hparse
have hlen : s.val.length <= 9223372036854775807 := by
simpa [SourceValidByteSlice] using hsource
have bind_ok_inv : forall {alpha beta : Type} (r : Result alpha)
(f : alpha -> Result beta) y,
Bind.bind r f = ok y -> exists x, r = ok x /\ f x = ok y := by
intro alpha beta r f y h
cases r with
| fail err => simp [Bind.bind, Aeneas.Std.bind] at h
| div => simp [Bind.bind, Aeneas.Std.bind] at h
| ok x => exact ⟨x, rfl, h⟩
have split_digits : forall xs : List Std.U8,
exists digits tail,
xs = digits ++ tail /\
(forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) /\
(forall b rest, tail = b :: rest ->
Not (48 <= b.val /\ b.val <= 57)) := by
intro xs
induction xs with
| nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), (by
intro b rest h
cases h)⟩
| cons b bs ih =>
by_cases hb : 48 <= b.val /\ b.val <= 57
· obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := ih
refine ⟨b :: digits, tail, by simp [hsplit], ?_, htail⟩
intro c hc
rcases List.mem_cons.mp hc with rfl | hc
· exact hb
· exact hdigits c hc
· exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc), (by
intro c rest h
cases h
exact hb)⟩
have parse_many_bound : forall (start intEnd : Slice Std.U8)
(exp outExp : Std.I64) (mantissa : Std.U64),
num.imp.dec2flt.parse.parse_many_digits start intEnd exp =
ok (mantissa, outExp) ->
mantissa.val <= 9999999999999999999 := by
intro start intEnd exp outExp mantissa hmany
obtain ⟨digits, tail, hsplit, hdigits, htail⟩ := split_digits start.val
obtain ⟨rest, first, remaining, consumed, hfirst, hpartition,
hrest, hfirstValue, hremainingDigits, hremainingLen, hfirstBound,
hfirstLow, hfirstSharp⟩ :=
NumberHelperTryParse19SafeLength start 0#u64 digits tail hsplit
hdigits htail (by norm_num)
unfold num.imp.dec2flt.parse.parse_many_digits at hmany
rw [hfirst] at hmany
by_cases hthreshold : first.val < 1000000000000000000
· have hnotHigh : ¬ 1000000000000000000 <= first.val := by omega
simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
hnotHigh] at hmany
cases hdrop :
core.slice.index.SliceIndexRangeFromUsizeSlice.index
{ start := 1#usize } rest with
| fail err =>
rw [hdrop] at hmany
simp at hmany
| div =>
rw [hdrop] at hmany
simp at hmany
| ok afterDrop =>
rw [hdrop] at hmany
simp at hmany
obtain ⟨digits2, tail2, hsplit2, hdigits2, htail2⟩ :=
split_digits afterDrop.val
obtain ⟨rest2, second, remaining2, consumed2, hsecond,
hpartition2, hrest2, hsecondValue, hremainingDigits2,
hremainingLen2, hsecondBound, hsecondLow, hsecondSharp⟩ :=
NumberHelperTryParse19SafeLength afterDrop first digits2 tail2
hsplit2 hdigits2 htail2 hfirstBound
rw [hsecond] at hmany
simp at hmany
cases hoff :
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
rest2 afterDrop with
| fail err => simp [hoff] at hmany
| div => simp [hoff] at hmany
| ok off =>
simp [hoff] at hmany
cases hneg : HNeg.hNeg off with
| fail err => simp [hneg] at hmany
| div => simp [hneg] at hmany
| ok negOff =>
simp [hneg] at hmany
cases hcast : lift (IScalar.cast .I64 negOff) with
| fail err => simp [hcast] at hmany
| div => simp [hcast] at hmany
| ok castOff =>
simp [hcast] at hmany
cases hadd : castOff + exp with
| fail err => simp [hadd] at hmany
| div => simp [hadd] at hmany
| ok finalExp =>
simp [hadd] at hmany
rcases hmany with ⟨rfl, rfl⟩
exact hsecondBound
· have hhigh : 1000000000000000000 <= first.val := by omega
simp [num.imp.dec2flt.parse.MIN_19DIGIT_INT.eq_def,
hhigh] at hmany
cases hoff :
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
intEnd rest with
| fail err => simp [hoff] at hmany
| div => simp [hoff] at hmany
| ok off =>
simp [hoff] at hmany
cases hcast : lift (IScalar.cast .I64 off) with
| fail err => simp [hcast] at hmany
| div => simp [hcast] at hmany
| ok castOff =>
simp [hcast] at hmany
cases hadd : castOff + exp with
| fail err => simp [hadd] at hmany
| div => simp [hadd] at hmany
| ok finalExp =>
simp [hadd] at hmany
rcases hmany with ⟨rfl, rfl⟩
exact hfirstBound
have record_branches : forall (total : Std.Isize)
(shortMantissa : Std.U64) (intEnd : Slice Std.U8)
(shortExponent retryExponent : Std.I64) (len outLen : Std.Usize),
(total.val <= 19 -> shortMantissa.val <= 9999999999999999999) ->
(forall (nMore adjusted : Std.Isize),
total - 19#isize = ok nMore ->
num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
Not (total.val <= 19) ->
Not (0 < adjusted.val) ->
shortMantissa.val <= 9999999999999999999) ->
(if total.val <= 19 then
ok (some
(num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
false false, len))
else do
let nMore <- total - 19#isize
let adjusted <-
num.imp.dec2flt.parse.adjust_many_digit_count s nMore
if 0 < adjusted.val then
let (manyMantissa, manyExponent) <-
num.imp.dec2flt.parse.parse_many_digits s intEnd retryExponent
ok (some
(num.imp.dec2flt.decimal.Decimal.mk manyExponent manyMantissa
false true, len))
else
ok (some
(num.imp.dec2flt.decimal.Decimal.mk shortExponent shortMantissa
false false, len))) = ok (some (d, outLen)) ->
d.negative = false /\ d.mantissa.val < 10 ^ 19 := by
intro total shortMantissa intEnd shortExponent retryExponent len outLen
hshort hnonpositive hresult
by_cases hsmall : total.val <= 19
· simp [hsmall] at hresult
rcases hresult with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
have hb := hshort hsmall
norm_num
omega
· simp [hsmall] at hresult
obtain ⟨nMore, hsub, hresult⟩ := bind_ok_inv _ _ _ hresult
obtain ⟨adjusted, hadjust, hresult⟩ := bind_ok_inv _ _ _ hresult
by_cases hpositive : 0 < adjusted.val
· simp only [if_pos hpositive] at hresult
obtain ⟨manyResult, hmany, hresult⟩ :=
bind_ok_inv _ _ _ hresult
rcases manyResult with ⟨manyMantissa, manyExponent⟩
rcases hresult with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
have hb := parse_many_bound s intEnd retryExponent manyExponent
manyMantissa hmany
norm_num
omega
· simp only [if_neg hpositive] at hresult
rcases hresult with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
have hb := hnonpositive nMore adjusted hsub hadjust hsmall hpositive
norm_num
omega
have fold_upper : forall (digits : List Std.U8) (a : Nat),
(forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
digits.foldl (fun x b => 10 * x + (b.val - 48)) a <
(a + 1) * 10 ^ digits.length := by
intro digits
induction digits with
| nil => intro a h; simp
| cons b bs ih =>
intro a h
have hb := h b List.mem_cons_self
have hbs : forall c, List.Mem c bs ->
48 <= c.val /\ c.val <= 57 := by
intro c hc
exact h c (List.mem_cons_of_mem b hc)
simp only [List.foldl_cons, List.length_cons, Nat.pow_succ]
have hrec := ih (10 * a + (b.val - 48)) hbs
have hcoeff : 10 * a + (b.val - 48) + 1 <= 10 * (a + 1) := by
omega
have hmul := Nat.mul_le_mul_right (10 ^ bs.length) hcoeff
exact hrec.trans_le (by
simpa [Nat.mul_assoc, Nat.mul_comm, Nat.mul_left_comm] using hmul)
have fold_ge_start : forall (digits : List Std.U8) (a : Nat),
a <= digits.foldl (fun x b => 10 * x + (b.val - 48)) a := by
intro digits
induction digits with
| nil => intro a; rfl
| cons b bs ih =>
intro a
apply le_trans (b := 10 * a + (b.val - 48))
· omega
· exact ih _
have zero_fold : forall digits : List Std.U8,
(forall b, List.Mem b digits -> b = 48#u8) ->
digits.foldl (fun x b => 10 * x + (b.val - 48)) 0 = 0 := by
intro digits hzero
induction digits with
| nil => rfl
| cons b bs ih =>
have hb := hzero b List.mem_cons_self
have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
intro c hc
exact hzero c (List.mem_cons_of_mem b hc)
simp [hb, ih hbs]
have split_zeros : forall digits : List Std.U8,
exists zeros tail,
digits = zeros ++ tail /\
(forall b, List.Mem b zeros -> b = 48#u8) /\
(tail = [] \/ exists b rest, tail = b :: rest /\ b ≠ 48#u8) := by
intro digits
induction digits with
| nil => exact ⟨[], [], rfl, (by intro b hb; cases hb), Or.inl rfl⟩
| cons b bs ih =>
by_cases hb : b = 48#u8
· obtain ⟨zeros, tail, hsplit, hzeros, htail⟩ := ih
refine ⟨b :: zeros, tail, by simp [hsplit], ?_, htail⟩
intro c hc
rcases List.mem_cons.mp hc with rfl | hc
· exact hb
· exact hzeros c hc
· exact ⟨[], b :: bs, rfl, (by intro c hc; cases hc),
Or.inr ⟨b, bs, rfl, hb⟩⟩
have count_zeros : forall zeros : List Std.U8,
(forall b, List.Mem b zeros -> b = 48#u8) ->
zeros.count 48#u8 = zeros.length := by
intro zeros hzeros
induction zeros with
| nil => rfl
| cons b bs ih =>
have hb := hzeros b List.mem_cons_self
have hbs : forall c, List.Mem c bs -> c = 48#u8 := by
intro c hc
exact hzeros c (List.mem_cons_of_mem b hc)
simp [hb, ih hbs]
have split_significand_zeros : forall
(integer fraction suffix : List Std.U8),
(forall b, List.Mem b integer -> 48 <= b.val /\ b.val <= 57) ->
(forall b, List.Mem b fraction -> 48 <= b.val /\ b.val <= 57) ->
Not (forall b, List.Mem b (integer ++ fraction) -> b = 48#u8) ->
exists rawZeros rawTail sigZeros sigTail,
integer ++ 46#u8 :: (fraction ++ suffix) = rawZeros ++ rawTail /\
(forall b, List.Mem b rawZeros -> b = 48#u8 \/ b = 46#u8) /\
(exists b rest, rawTail = b :: rest /\
b ≠ 48#u8 /\ b ≠ 46#u8) /\
integer ++ fraction = sigZeros ++ sigTail /\
(forall b, List.Mem b sigZeros -> b = 48#u8) /\
(exists b rest, sigTail = b :: rest /\ b ≠ 48#u8) /\
rawZeros.count 48#u8 = sigZeros.length := by
intro integer fraction suffix hinteger hfraction hnonzero
obtain ⟨intZeros, intSig, hintSplit, hintZeros, hintSig⟩ :=
split_zeros integer
rcases hintSig with hintNil | ⟨b, intRest, hintSigEq, hb⟩
· obtain ⟨fracZeros, fracSig, hfracSplit, hfracZeros, hfracSig⟩ :=
split_zeros fraction
rcases hfracSig with hfracNil | ⟨c, fracRest, hfracSigEq, hc⟩
· exfalso
apply hnonzero
intro x hx
rcases List.mem_append.mp hx with hx | hx
· rw [hintSplit, hintNil] at hx
exact hintZeros x (by simpa using hx)
· rw [hfracSplit, hfracNil] at hx
exact hfracZeros x (by simpa using hx)
· refine ⟨intZeros ++ 46#u8 :: fracZeros,
fracSig ++ suffix, intZeros ++ fracZeros, fracSig,
?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
· rw [hintSplit, hintNil, hfracSplit]
simp [List.append_assoc]
· intro x hx
rcases List.mem_append.mp hx with hx | hx
· exact Or.inl (hintZeros x hx)
· rcases List.mem_cons.mp hx with rfl | hx
· exact Or.inr rfl
· exact Or.inl (hfracZeros x hx)
· refine ⟨c, fracRest ++ suffix, by simp [hfracSigEq], hc, ?_⟩
have hcdigit := hfraction c (by
rw [hfracSplit, hfracSigEq]
exact List.mem_append_right fracZeros List.mem_cons_self)
intro hpoint
subst c
norm_num at hcdigit
· rw [hintSplit, hintNil, hfracSplit]
simp [List.append_assoc]
· intro x hx
rcases List.mem_append.mp hx with hx | hx
· exact hintZeros x hx
· exact hfracZeros x hx
· exact ⟨c, fracRest, hfracSigEq, hc⟩
· rw [List.count_append, List.count_cons,
count_zeros intZeros hintZeros,
count_zeros fracZeros hfracZeros]
norm_num
· refine ⟨intZeros, intSig ++ 46#u8 :: (fraction ++ suffix),
intZeros, intSig ++ fraction, ?_, ?_, ?_, ?_, hintZeros, ?_, ?_⟩
· rw [hintSplit]
simp [List.append_assoc]
· intro x hx
exact Or.inl (hintZeros x hx)
· refine ⟨b, intRest ++ 46#u8 :: (fraction ++ suffix),
by simp [hintSigEq], hb, ?_⟩
have hbdigit := hinteger b (by
rw [hintSplit, hintSigEq]
exact List.mem_append_right intZeros List.mem_cons_self)
intro hpoint
subst b
norm_num at hbdigit
· rw [hintSplit]
simp [List.append_assoc]
· exact ⟨b, intRest ++ fraction, by simp [hintSigEq], hb⟩
· exact count_zeros intZeros hintZeros
obtain ⟨intDigits, intTail, hsval, hintDigits, hintTail⟩ :=
split_digits s.val
have hintTailProp : intTail.length <= Usize.max := by
have hp := s.property
rw [hsval] at hp
simp only [List.length_append] at hp
omega
let intRest : Slice Std.U8 := ⟨intTail, hintTailProp⟩
obtain ⟨intMantissa, scannedIntRest, hscanInt, hscannedIntRest⟩ :=
NumberHelperDigitScanSourceLength s 0#u64 intDigits intTail hsval
hintDigits hintTail hlen
have hscannedIntRestEq : scannedIntRest = intRest :=
(Slice.eq_iff _ _).2 (by simpa [intRest] using hscannedIntRest)
subst scannedIntRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSourceLength s intRest hlen (by
rw [hsval]
simp [intRest])
have hnDigitsEq : nDigits.val = intDigits.length := by
rw [hnDigitsVal, hsval]
simp [intRest]
have hpartial : exists len,
num.imp.dec2flt.parse.parse_partial_number s = ok (some (d, len)) := by
unfold num.imp.dec2flt.parse.parse_number at hparse
cases hp : num.imp.dec2flt.parse.parse_partial_number s with
| fail err => simp [hp] at hparse
| div => simp [hp] at hparse
| ok result =>
cases result with
| none => simp [hp] at hparse
| some pair =>
rcases pair with ⟨record, len⟩
simp [hp] at hparse
split at hparse
· have hrecord : record = d := by simpa using hparse
subst d
exact ⟨len, rfl⟩
· simp at hparse
obtain ⟨parsedLen, hpartial⟩ := hpartial
cases intTail with
| nil =>
have hsplitInt : core.slice.Slice.split_first intRest = ok none := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst, intRest]
have haddZero : nDigits + 0#isize = ok nDigits := by
have hb := IScalar.hBounds nDigits
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec (x := nDigits) (y := 0#isize) (by
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega) (by
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hb ⊢
omega))
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
simpa using hzval
have hfullOffset :
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
intRest s = ok nDigits := by
simpa [intRest] using hoffInt
have hcastLen : lift (IScalar.hcast .Usize nDigits) =
ok (Slice.len s) := by
have hbound : 0 <= nDigits.val /\
nDigits.val <= UScalar.max .Usize := by
rw [hnDigitsEq]
constructor
· omega
· have hp := s.property
rw [hsval] at hp
simpa [UScalar.max, Usize.max, Usize.numBits,
Dec2Flt.configured_x86_64_numBits] using hp
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize nDigits hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hnDigitsEq] at hzval
have hslen : s.val.length = intDigits.length := by simp [hsval]
simpa [Slice.len_val, hslen] using hzval
unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
simp [hne, hscanInt, hoffInt, hsplitInt, haddZero,
hcastLen] at hpartial
by_cases hnzero : nDigits = 0#isize
· simp [hnzero] at hpartial
· simp [hnzero] at hpartial
by_cases hsmall : nDigits.val <= 19
· simp [hsmall] at hpartial
rcases hpartial with ⟨rfl, rfl⟩
constructor
· rfl
· have hfoldLt := fold_upper intDigits 0 hintDigits
norm_num at hfoldLt
have hlen19 : intDigits.length <= 19 := by
omega
have hbound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
have hp := hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
norm_num at hp ⊢
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits []
(by simpa using hsval) hintDigits (by
intro b rest h
cases h) hlen hbound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
rw [hvalue]
norm_num
exact hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
· have hlarge : 19 < nDigits.val := by omega
have hsubBounds : IScalar.min .Isize <= nDigits.val - 19 /\
nDigits.val - 19 <= IScalar.max .Isize := by
have hb := IScalar.hBounds nDigits
simp [IScalar.min, IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hb ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := nDigits) (y := 19#isize)
(by norm_num; omega) (by norm_num; omega))
norm_num at hnMoreVal
simp [hsmall, hnMore] at hpartial
obtain ⟨zeros, nonzeroTail, hzeroSplit, hzeros, hzeroTail⟩ :=
split_zeros intDigits
have hcount := count_zeros zeros hzeros
have hsAdjust : s.val = zeros ++ nonzeroTail := by
simpa [hzeroSplit] using hsval
have htailSharp : nonzeroTail = [] \/ exists b rest,
nonzeroTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
rcases hzeroTail with hnil | ⟨b, rest, htailEq, hb⟩
· exact Or.inl hnil
· right
refine ⟨b, rest, htailEq, hb, ?_⟩
have hbdigit := hintDigits b (by
rw [hzeroSplit, htailEq]
exact List.mem_append_right zeros List.mem_cons_self)
intro heq
subst b
norm_num at hbdigit
have hnMoreNonneg : 0 <= nMore.val := by
rw [hnMoreVal]
omega
have hnMoreLe : nMore.val <= s.val.length := by
rw [hnMoreVal, hnDigitsEq, hsval]
simp only [List.length_append, List.length_nil, Nat.add_zero]
omega
have hadjustLower : IScalar.min .Isize <=
nMore.val - (zeros.count 48#u8 : Int) := by
rw [hnMoreVal, hcount, hnDigitsEq]
have hzerosLen : zeros.length <= intDigits.length := by
rw [hzeroSplit]
simp
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨adjusted, hadjust, hadjustVal⟩ :=
NumberHelperAdjustManyExact s nMore zeros nonzeroTail hsAdjust
(by intro b hb; exact Or.inl (hzeros b hb)) htailSharp hlen
hnMoreNonneg hnMoreLe hadjustLower
rw [hadjust] at hpartial
by_cases hpositive : 0 < adjusted.val
· simp [hpositive] at hpartial
cases hmany :
num.imp.dec2flt.parse.parse_many_digits s intRest 0#i64 with
| fail err => simp [hmany] at hpartial
| div => simp [hmany] at hpartial
| ok result =>
rcases result with ⟨manyMantissa, manyExponent⟩
simp [hmany] at hpartial
rcases hpartial with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
change manyMantissa.val < 10 ^ 19
have hb := parse_many_bound s intRest 0#i64 manyExponent
manyMantissa hmany
omega
· simp [hpositive] at hpartial
have htailLen : nonzeroTail.length <= 19 := by
rw [hadjustVal, hnMoreVal, hcount, hnDigitsEq] at hpositive
have hlenEq := congrArg List.length hzeroSplit
simp only [List.length_append] at hlenEq
omega
have htailDigits : forall b, List.Mem b nonzeroTail ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
apply hintDigits b
rw [hzeroSplit]
exact List.mem_append_right zeros hb
have hzeroFold := zero_fold zeros hzeros
have hfoldLt : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
rcases hzeroTail with hnil | hcons
· subst nonzeroTail
rw [hzeroSplit, List.append_nil, hzeroFold]
norm_num
· rw [hzeroSplit, List.foldl_append, hzeroFold]
have htailLt := fold_upper nonzeroTail 0 htailDigits
norm_num at htailLt
exact htailLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
htailLen)
have hfoldBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits []
(by simpa using hsval) hintDigits (by
intro b rest h
cases h) hlen hfoldBound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
rcases hpartial with ⟨rfl, rfl⟩
refine ⟨rfl, ?_⟩
norm_num at hvalue ⊢
rw [hvalue]
exact hfoldLt
| cons first tail =>
have htailProp : tail.length <= Usize.max := by
have hp := intRest.property
simp [intRest] at hp ⊢
omega
let afterFirst : Slice Std.U8 := ⟨tail, htailProp⟩
have hsplitInt : core.slice.Slice.split_first intRest =
ok (some (first, afterFirst)) := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
intRest, afterFirst]
by_cases hpoint : first = 46#u8
· subst first
obtain ⟨fracDigits, fracTail, hfracSplit, hfracDigits, hfracTail⟩ :=
split_digits tail
have hfracTailProp : fracTail.length <= Usize.max := by
have hp := afterFirst.property
simp [afterFirst, hfracSplit] at hp ⊢
omega
let fracRest : Slice Std.U8 := ⟨fracTail, hfracTailProp⟩
have hlenAfter : afterFirst.val.length <= 9223372036854775807 := by
simp [afterFirst]
rw [hsval] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega
obtain ⟨fracMantissa, scannedFracRest, hscanFrac, hscannedFracRest⟩ :=
NumberHelperDigitScanSourceLength afterFirst intMantissa fracDigits
fracTail (by simp [afterFirst, hfracSplit]) hfracDigits hfracTail
hlenAfter
have hscannedFracRestEq : scannedFracRest = fracRest :=
(Slice.eq_iff _ _).2 (by
simpa [fracRest] using hscannedFracRest)
subst scannedFracRest
obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
NumberHelperOffsetSourceLength afterFirst fracRest hlenAfter (by
simp [afterFirst, fracRest, hfracSplit])
have hnAfterEq : nAfter.val = fracDigits.length := by
rw [hnAfterVal]
simp [afterFirst, fracRest, hfracSplit]
have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
rw [hnAfterEq, IScalar.min]
simp [IScalarTy.numBits, Dec2Flt.configured_x86_64_numBits]
obtain ⟨negAfter, hnegAfter, hnegAfterVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(HNeg.hNeg.step nAfter hnAfterNotMin)
have hnegCastBounds : IScalar.min .I64 <= negAfter.val /\
negAfter.val <= IScalar.max .I64 := by
rw [hnegAfterVal, hnAfterEq,
IScalar.min_IScalarTy_I64_eq, I64.min_eq,
IScalar.max_IScalarTy_I64_eq, I64.max_eq]
constructor <;> omega
obtain ⟨fracExponent, hfracExponent, hfracExponentVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.cast_inBounds_spec .I64 negAfter hnegCastBounds)
have htotalBounds : IScalar.min .Isize <=
nDigits.val + nAfter.val /\
nDigits.val + nAfter.val <= IScalar.max .Isize := by
rw [hnDigitsEq, hnAfterEq]
simp [IScalar.min, IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
rw [hsval, hfracSplit] at hlen
simp only [List.length_append, List.length_cons] at hlen
constructor <;> omega
obtain ⟨totalDigits, htotal, htotalVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec htotalBounds.1 htotalBounds.2)
have hsigDigits : forall b,
List.Mem b (intDigits ++ fracDigits) ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
rcases List.mem_append.mp hb with hb | hb
· exact hintDigits b hb
· exact hfracDigits b hb
have hexactScans :
(intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max ->
fracMantissa.val = (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 := by
intro hfullBound
have hprefixLe := fold_ge_start fracDigits
(intDigits.foldl (fun x b => 10 * x + (b.val - 48)) 0)
rw [← List.foldl_append] at hprefixLe
have hintBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max :=
hprefixLe.trans hfullBound
obtain ⟨intValue, intScanRest, hexactInt, hexactIntRest,
hintValue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits
(46#u8 :: tail) (by simpa using hsval) hintDigits hintTail
hlen hintBound
rw [hscanInt] at hexactInt
simp at hexactInt
rcases hexactInt with ⟨rfl, rfl⟩
have hfracBound : fracDigits.foldl
(fun x b => 10 * x + (b.val - 48)) intMantissa.val <=
U64.max := by
norm_num at hintValue
rw [hintValue, ← List.foldl_append]
exact hfullBound
obtain ⟨fracValue, fracScanRest, hexactFrac, hexactFracRest,
hfracValue⟩ :=
NumberHelperDigitScanExact afterFirst intMantissa fracDigits
fracTail (by simp [afterFirst, hfracSplit]) hfracDigits
hfracTail hlenAfter hfracBound
rw [hscanFrac] at hexactFrac
simp at hexactFrac
rcases hexactFrac with ⟨rfl, rfl⟩
norm_num at hintValue hfracValue ⊢
rw [hfracValue, hintValue]
have hshort : totalDigits.val <= 19 ->
fracMantissa.val <= 9999999999999999999 := by
intro hsmall
have hsigLen : intDigits.length + fracDigits.length <= 19 := by
rw [htotalVal, hnDigitsEq, hnAfterEq] at hsmall
omega
have hfoldLt := fold_upper (intDigits ++ fracDigits) 0 hsigDigits
norm_num at hfoldLt
have hfold19 := hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hsigLen)
have hfoldBound : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
norm_num
omega
rw [hexactScans hfoldBound]
norm_num
omega
have hnonpositive : forall (nMore adjusted : Std.Isize),
totalDigits - 19#isize = ok nMore ->
num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
Not (totalDigits.val <= 19) ->
Not (0 < adjusted.val) ->
fracMantissa.val <= 9999999999999999999 := by
intro nMore adjusted hsub hadjust hlarge hnonpos
by_cases hallzero : forall b,
List.Mem b (intDigits ++ fracDigits) -> b = 48#u8
· have hfoldZero := zero_fold (intDigits ++ fracDigits) hallzero
have hfoldBound : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [hfoldZero]
exact Nat.zero_le _
rw [hexactScans hfoldBound, hfoldZero]
norm_num
· obtain ⟨rawZeros, rawTail, sigZeros, sigTail, hrawSplit,
hrawZeros, hrawTail, hsigSplit, hsigZeros, hsigTail, hcount⟩ :=
split_significand_zeros intDigits fracDigits fracTail
hintDigits hfracDigits hallzero
have hsAdjust : s.val = rawZeros ++ rawTail := by
rw [hsval, hfracSplit]
exact hrawSplit
have hnMoreVal : nMore.val = totalDigits.val - 19 := by
have heq := IScalar.sub_equiv totalDigits 19#isize
rw [hsub] at heq
have hval := heq.2.1
norm_num at hval ⊢
exact hval
have hnMoreNonneg : 0 <= nMore.val := by
rw [hnMoreVal]
omega
have hnMoreLe : nMore.val <= s.val.length := by
rw [hnMoreVal, htotalVal, hnDigitsEq, hnAfterEq, hsval,
hfracSplit]
simp only [List.length_append, List.length_cons]
omega
have hadjustLower : IScalar.min .Isize <=
nMore.val - (rawZeros.count 48#u8 : Int) := by
rw [hnMoreVal, hcount, htotalVal, hnDigitsEq, hnAfterEq]
have hzlen : sigZeros.length <=
(intDigits ++ fracDigits).length := by
rw [hsigSplit]
simp
have hsigSource : (intDigits ++ fracDigits).length <=
s.val.length := by
rw [hsval, hfracSplit]
simp only [List.length_append, List.length_cons]
omega
have hzBound : sigZeros.length <= 9223372036854775807 :=
hzlen.trans (hsigSource.trans hlen)
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
NumberHelperAdjustManyExact s nMore rawZeros rawTail hsAdjust
hrawZeros (Or.inr hrawTail) hlen hnMoreNonneg hnMoreLe
hadjustLower
rw [hadjust] at hadjust2
simp at hadjust2
subst adjusted2
have hsigTailLen : sigTail.length <= 19 := by
rw [hadjust2Val, hnMoreVal, hcount, htotalVal, hnDigitsEq,
hnAfterEq] at hnonpos
have hlenEq := congrArg List.length hsigSplit
simp only [List.length_append] at hlenEq
omega
have hsigTailDigits : forall b, List.Mem b sigTail ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
apply hsigDigits b
rw [hsigSplit]
exact List.mem_append_right sigZeros hb
have hzeroFold := zero_fold sigZeros hsigZeros
have htailLt := fold_upper sigTail 0 hsigTailDigits
norm_num at htailLt
have hfoldLt : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
rw [hsigSplit, List.foldl_append, hzeroFold]
exact htailLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat))
hsigTailLen)
have hfoldBound : (intDigits ++ fracDigits).foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
exact (Nat.le_of_lt hfoldLt).trans (by
norm_num [U64.max_eq])
rw [hexactScans hfoldBound]
norm_num at hfoldLt ⊢
omega
unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
simp [hne, hscanInt, hoffInt, hsplitInt, hscanFrac, hoffFrac,
hnegAfter, hfracExponent] at hpartial
simp only [UScalar.ofNatCore] at hpartial
simp [htotal] at hpartial
by_cases hzero : totalDigits = 0#isize
· simp [hzero] at hpartial
· simp [hzero] at hpartial
cases fracTail with
| nil =>
have hsplitFrac : core.slice.Slice.split_first fracRest =
ok none := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
fracRest]
simp [hsplitFrac] at hpartial
obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest fracExponent
0#i64 len parsedLen hshort hnonpositive hpartial
| cons marker suffix =>
have hsuffixProp : suffix.length <= Usize.max := by
have hp := fracRest.property
simp [fracRest] at hp ⊢
omega
let afterMarker : Slice Std.U8 := ⟨suffix, hsuffixProp⟩
have hsplitFrac : core.slice.Slice.split_first fracRest =
ok (some (marker, afterMarker)) := by
simp [core.slice.Slice.split_first, ModelSliceSplitFirst,
fracRest, afterMarker]
simp [hsplitFrac] at hpartial
by_cases hlower : marker = (⟨101#8⟩ : Std.U8)
· subst marker
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· by_cases hupper : marker = (⟨69#8⟩ : Std.U8)
· subst marker
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· simp [hlower, hupper] at hpartial
obtain ⟨off, hoff, hpartial⟩ := bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ := bind_ok_inv _ _ _ hpartial
exact record_branches totalDigits fracMantissa intRest
fracExponent 0#i64 len parsedLen hshort hnonpositive hpartial
· have haddZero : nDigits + 0#isize = ok nDigits := by
have hb := IScalar.hBounds nDigits
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec (x := nDigits) (y := 0#isize) (by
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega) (by
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hb ⊢
omega))
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
simpa using hzval
have hshort : nDigits.val <= 19 ->
intMantissa.val <= 9999999999999999999 := by
intro hsmall
have hlen19 : intDigits.length <= 19 := by omega
have hfoldLt := fold_upper intDigits 0 hintDigits
norm_num at hfoldLt
have hfold19 : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 :=
hfoldLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hlen19)
have hfoldBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
(by simpa using hsval) hintDigits hintTail hlen hfoldBound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
norm_num at hvalue ⊢
rw [hvalue]
omega
have hnonpositive : forall (nMore adjusted : Std.Isize),
nDigits - 19#isize = ok nMore ->
num.imp.dec2flt.parse.adjust_many_digit_count s nMore = ok adjusted ->
Not (nDigits.val <= 19) ->
Not (0 < adjusted.val) ->
intMantissa.val <= 9999999999999999999 := by
intro nMore adjusted hsub hadjust hlarge hnonpos
have hnMoreVal : nMore.val = nDigits.val - 19 := by
have heq := IScalar.sub_equiv nDigits 19#isize
rw [hsub] at heq
have hval := heq.2.1
norm_num at hval ⊢
exact hval
obtain ⟨zeros, nzTail, hzeroSplit, hzeros, hnzTail⟩ :=
split_zeros intDigits
have hcount := count_zeros zeros hzeros
let rawTail : List Std.U8 := nzTail ++ first :: tail
have hsAdjust : s.val = zeros ++ rawTail := by
rw [hsval, hzeroSplit]
simp [rawTail, List.append_assoc]
have hrawTail : rawTail = [] \/ exists b rest,
rawTail = b :: rest /\ b ≠ 48#u8 /\ b ≠ 46#u8 := by
right
rcases hnzTail with hnil | ⟨b, rest, heq, hb⟩
· subst nzTail
refine ⟨first, tail, by simp [rawTail], ?_, hpoint⟩
intro hzero
subst first
have hnot := hintTail 48#u8 tail rfl
norm_num at hnot
· refine ⟨b, rest ++ first :: tail, by simp [rawTail, heq], hb, ?_⟩
have hbdigit := hintDigits b (by
rw [hzeroSplit, heq]
exact List.mem_append_right zeros List.mem_cons_self)
intro heq46
subst b
norm_num at hbdigit
have hnMoreNonneg : 0 <= nMore.val := by
rw [hnMoreVal]
omega
have hnMoreLe : nMore.val <= s.val.length := by
rw [hnMoreVal, hnDigitsEq, hsval]
simp only [List.length_append, List.length_cons]
omega
have hadjustLower : IScalar.min .Isize <=
nMore.val - (zeros.count 48#u8 : Int) := by
rw [hnMoreVal, hcount, hnDigitsEq]
have hzlen : zeros.length <= intDigits.length := by
rw [hzeroSplit]
simp
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨adjusted2, hadjust2, hadjust2Val⟩ :=
NumberHelperAdjustManyExact s nMore zeros rawTail hsAdjust
(by intro b hb; exact Or.inl (hzeros b hb)) hrawTail hlen
hnMoreNonneg hnMoreLe hadjustLower
rw [hadjust] at hadjust2
simp at hadjust2
subst adjusted2
have hnzLen : nzTail.length <= 19 := by
rw [hadjust2Val, hnMoreVal, hcount, hnDigitsEq] at hnonpos
have hlenEq := congrArg List.length hzeroSplit
simp only [List.length_append] at hlenEq
omega
have hnzDigits : forall b, List.Mem b nzTail ->
48 <= b.val /\ b.val <= 57 := by
intro b hb
apply hintDigits b
rw [hzeroSplit]
exact List.mem_append_right zeros hb
have hzeroFold := zero_fold zeros hzeros
have hfoldLt : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 < 10 ^ 19 := by
rcases hnzTail with hnil | hcons
· subst nzTail
rw [hzeroSplit, List.append_nil, hzeroFold]
norm_num
· rw [hzeroSplit, List.foldl_append, hzeroFold]
have htailLt := fold_upper nzTail 0 hnzDigits
norm_num at htailLt
exact htailLt.trans_le
(Nat.pow_le_pow_right (by norm_num : 0 < (10 : Nat)) hnzLen)
have hfoldBound : intDigits.foldl
(fun x b => 10 * x + (b.val - 48)) 0 <= U64.max := by
rw [U64.max_eq]
omega
obtain ⟨value, rest, hexact, hrest, hvalue⟩ :=
NumberHelperDigitScanExact s 0#u64 intDigits (first :: tail)
(by simpa using hsval) hintDigits hintTail hlen hfoldBound
rw [hscanInt] at hexact
simp at hexact
rcases hexact with ⟨rfl, rfl⟩
norm_num at hvalue ⊢
rw [hvalue]
omega
have hpointVal : first.val ≠ 46 := by
intro hval
apply hpoint
exact UScalar.eq_of_val_eq hval
unfold num.imp.dec2flt.parse.parse_partial_number at hpartial
simp [hne, hscanInt, hoffInt, hsplitInt] at hpartial
split at hpartial
all_goals try { exfalso; apply hpointVal; rfl }
simp [haddZero] at hpartial
by_cases hnzero : nDigits = 0#isize
· simp [hnzero] at hpartial
· simp [hnzero, hsplitInt] at hpartial
by_cases hlower : first = 101#u8
· subst first
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches nDigits intMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· by_cases hupper : first = 69#u8
· simp [hlower] at hpartial
subst first
obtain ⟨sciResult, hsci, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
rcases sciResult with ⟨optExp, sciRest⟩
cases optExp with
| none =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch,
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual,
ModelOptionFromResidual] at hpartial
| some parsedExp =>
simp [core.option.Option.Insts.CoreOpsTry_traitTry.branch,
ModelOptionBranch] at hpartial
obtain ⟨combined, hsum, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨off, hoff, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches nDigits intMantissa intRest combined
parsedExp len parsedLen hshort hnonpositive hpartial
· simp [hlower, hupper] at hpartial
rw [hoffInt] at hpartial
simp at hpartial
obtain ⟨len, hcast, hpartial⟩ :=
bind_ok_inv _ _ _ hpartial
exact record_branches nDigits intMantissa intRest 0#i64 0#i64
len parsedLen hshort hnonpositive hpartial
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
- Assumptions
- ModelOptionBranch
- ModelOptionFromResidual
- ModelSliceSplitFirst
- NumberHelperAdjustManyExact
- NumberHelperDigitScanExact
All 17 entries
- Assumptions
- ModelOptionBranch
- ModelOptionFromResidual
- ModelSliceSplitFirst
- NumberHelperAdjustManyExact
- NumberHelperDigitScanExact
- NumberHelperDigitScanSourceLength
- NumberHelperOffsetSourceLength
- NumberHelperTryParse19SafeLength
- SourceValidByteSlice
- U64 Insts CoreOpsBitBitAndU64U64
- U64 Insts CoreOpsBitBitOrU64U64
- num imp dec2flt decimal Decimal
- num imp dec2flt parse parse many digits
- num imp dec2flt parse parse number
- num imp dec2flt parse parse partial number
- num imp dec2flt parse parse scientific
Used by
No other declaration in this version lists this one as a dependency.
Inspect related source (71)
- Decimal · dec2flt/decimal.rs:32 Program Rust
- parse_many_digits · dec2flt/parse.rs:104 Program Rust
- parse_number · dec2flt/parse.rs:231 Program Rust
More program dependencies (36)
- parse_partial_number · dec2flt/parse.rs:161 Program Rust
- parse_scientific · dec2flt/parse.rs:121 Program Rust
- parse_scientific_loop · dec2flt/parse.rs:142 Program Rust
- body · dec2flt/parse.rs:142 Program Rust
- offset_from · dec2flt/common.rs:30 Program Rust
- read_u64 · dec2flt/common.rs:18 Program Rust
- is_8digits · dec2flt/common.rs:40 Program Rust
- MIN_19DIGIT_INT · dec2flt/parse.rs:8 Program Rust
- adjust_many_digit_count · dec2flt/parse.rs:86 Program Rust
- adjust_many_digit_count_loop · dec2flt/parse.rs:89 Program Rust
- body · dec2flt/parse.rs:89 Program Rust
- parse_8digits · dec2flt/parse.rs:18 Program Rust
- try_parse_19digits · dec2flt/parse.rs:61 Program Rust
- try_parse_19digits_loop · dec2flt/parse.rs:64 Program Rust
- body · dec2flt/parse.rs:64 Program Rust
- try_parse_digits · dec2flt/parse.rs:30 Program Rust
- try_parse_digits_loop0 · dec2flt/parse.rs:33 Program Rust
- body · dec2flt/parse.rs:33 Program Rust
- try_parse_digits_loop1 · dec2flt/parse.rs:50 Program Rust
- body · dec2flt/parse.rs:50 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
- DecimalSeq · dec2flt/decimal_seq.rs:21 Program Rust
- MASK · dec2flt/parse.rs:19 Program Rust
- MUL1 · dec2flt/parse.rs:20 Program Rust
- MUL2 · dec2flt/parse.rs:21 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 (32)
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- 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
- first · core/src/slice/mod.rs:155 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- CoreOpsBitBitAndU64U64 · core/src/ops/bit.rs:174 Rust library
- CoreOpsBitBitOrU64U64 · core/src/ops/bit.rs:278 Rust library
- BitOr · core/src/ops/bit.rs:254 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
- 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
All 56 entries
- Binary64Model
- Binary64ModelDefault
- F64
- ModelF64Classify
- ModelF64Clone
- ModelF64Div
- ModelF64ExponentMask
- ModelF64FromBits
- ModelF64FromU64
- ModelF64Infinity
- ModelF64MantissaDigits
- ModelF64MantissaMask
- ModelF64Mul
- ModelF64Nan
- ModelF64Neg
- ModelF64NegInfinity
- ModelF64ToBits
- ModelOptionBranch
- ModelOptionFromResidual
- ModelSliceFirst
- ModelSliceSplitFirst
- ModelStrAsBytes
- ModelU64BitAnd
- ModelU64BitOr
- ModelU64Shl
- ModelU64Shr
- ModelU8IsAsciiDigit
- NumberHelperAdjustManyExact
- NumberHelperDigitScanExact
- NumberHelperDigitScanSourceLength
- NumberHelperEightDigitWord
- NumberHelperOffsetSourceLength
- NumberHelperParseEightDigitsExact
- NumberHelperParseEightDigitsSafe
- NumberHelperReadEightBytes
- NumberHelperRecordBounds
- NumberHelperTryParse19SafeLength
- Preamble
- SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from
- SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64
- SourceValidByteSlice
- core num FpCategory
- num imp dec2flt common is 8digits
- num imp dec2flt decimal Decimal
- num imp dec2flt parse MIN 19DIGIT INT
- num imp dec2flt parse adjust many digit count
- num imp dec2flt parse parse 8digits
- num imp dec2flt parse parse 8digits MASK
- num imp dec2flt parse parse 8digits MUL1
- num imp dec2flt parse parse 8digits MUL2
- num imp dec2flt parse parse many digits
- num imp dec2flt parse parse number
- num imp dec2flt parse parse partial number
- num imp dec2flt parse parse scientific
- num imp dec2flt parse try parse 19digits
- num imp dec2flt parse try parse digits
Technical checking records
Local closure summary
{
"node": "NumberHelperRecordBounds",
"closure_version": "patch_c_v1",
"active_decl_hash": "698d95ba07f02035f8d7078a273eb58fe7fcf975bbd30c3803a8957602efb8cb",
"active_statement_hash": "273d7ce693720aade46ebfbdca0ec4027ee04613fd6ec39e8e6b91072393eeb7",
"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-140",
"boundary_theorems": {},
"strict_theorem_deps": {
"ModelSliceSplitFirst": "f617ee05dd2925a9",
"NumberHelperAdjustManyExact": "8999fa99d4681219",
"NumberHelperDigitScanExact": "6bcf20c61d10dea3",
"NumberHelperDigitScanSourceLength": "8892b939bba0ecf9",
"NumberHelperOffsetSourceLength": "de0168dfeba3b802",
"NumberHelperTryParse19SafeLength": "dd334c60b1e032a8",
"num_imp_dec2flt_parse_MIN_19DIGIT_INT": "8d99761093f40bc7",
"num_imp_dec2flt_parse_parse_many_digits": "faf0081b4c91301b",
"num_imp_dec2flt_parse_parse_partial_number": "004e715471c389b8"
},
"strict_definition_deps": {
"ModelOptionBranch": "2b227898dbe15e59",
"ModelOptionFromResidual": "99bee9493878565f",
"Preamble": "7754ffaf671693bf",
"SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
"SourceValidByteSlice": "3dc42716a6378d26",
"num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2",
"num_imp_dec2flt_parse_MIN_19DIGIT_INT": "476edc5502ff7bfb",
"num_imp_dec2flt_parse_adjust_many_digit_count": "4e59e3776b6cfdaa",
"num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
"num_imp_dec2flt_parse_parse_number": "b8693ec58f01c43c",
"num_imp_dec2flt_parse_parse_partial_number": "359d30eefc709c2a",
"num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5",
"num_imp_dec2flt_parse_try_parse_19digits": "c9c74554a359a9c4",
"num_imp_dec2flt_parse_try_parse_digits": "238b4a73cd46fc56"
}
}Prose assessment
{
"label": "Recorded Sound: VerifierPass",
"assessment_status": "VerifierPass",
"fingerprint_relation": "matches current fingerprint",
"assessment_fingerprint": "sound-v2:b3ecdea1460097640039f56fe7799499544fbf26dee60f744e8a4b6e16337660",
"current_fingerprint": "sound-v2:b3ecdea1460097640039f56fe7799499544fbf26dee60f744e8a4b6e16337660",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}