Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Safe complete partial-number parsing
parse_partial_number return normally with a decimal record and a
consumed length equal to the full slice length.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.DecimalGoalVocabularyDefault
import Tablet.NumberHelperDigitScanSafeLength
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManySafeLength
import Tablet.NumberHelperFractionalManyDigitsSafeLength
import Tablet.NumberHelperIntegralAdjustment
import Tablet.NumberHelperIntegralManyDigitsSafeLength
import Tablet.NumberHelperOffsetSafeLength
import Tablet.NumberHelperScientificRenderedSuffixSafeLength
import Tablet.NumberHelperScientificSuffixSafeLength
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_parse_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
import Tablet.num_imp_dec2flt_parse_try_parse_19digits
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: NumberHelperPartialAcceptanceSafeLength]
theorem NumberHelperPartialAcceptanceSafeLength :
forall (s : Slice Std.U8) (x : DecimalLexeme),
SourceValidByteSlice s -> s.val != [] -> x.leadingSign = none ->
DecimalGoalVocabularyDefault.validNumeric x ->
DecimalGoalVocabularyDefault.bytes s =
DecimalGoalVocabularyDefault.renderNumeric x ->
s.val.length <= 9223372036854120458 ->
exists d,
num.imp.dec2flt.parse.parse_partial_number s =
ok (some (d, Slice.len s)) := 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
usize, isize, and i64 ranges.isize
and subtracts. Both lengths are between zero and L<M, so the casts and
nonnegative difference succeed and give the integer digit count I.i64 is exact on this 64-bit target, and the checked addition
I+F is the total significand count D\leq L. Validity gives D>0, so
the extracted zero-digit rejection in
num imp dec2flt parse parse partial number is not taken.Some result E, empty remainder, and |E|\leq655359. Let q be
the exponent digit count. The guarded decimal recurrence in
num imp dec2flt parse parse scientific also gives
|E|\leq10^q-1 when q\leq5. We verify the checked addition -F+E.
For E\geq0 it lies between -L and 655359. For E<0,F=0 it is at
least -655359. For E<0,F>0, the spelling includes the point, marker,
mandatory minus, and q digits, hence F\leq L-(q+3). Therefore
Slice.len s.isize.
If z is the number of leading zero significand digits, ignoring the point,
induction through this scan shows its final adjusted count is D-19-z and
all intermediate counts are at least -19 and at most L. Thus all
checked signed subtractions succeed. A positive result implies
D-z\geq20, so a nonzero significand digit exists and at least twenty
digits remain from it; a nonpositive result causes no retry.Some of its
constructed decimal record with consumed length exactly
Slice.len s, as claimed.Lean proof
intro s x hsource hne hsign hvalid hbytes hlen
rcases x with ⟨sign, ints, frac, expo⟩
simp only at hsign
subst sign
simp [DecimalGoalVocabularyDefault] at hvalid hbytes
have byte_digits : forall (bs : List Std.U8) (ds : List Nat),
List.map (fun b : Std.U8 => b.val) bs =
List.map (fun d : Nat => 48 + d) ds ->
(forall d, List.Mem d ds -> d < 10) ->
forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57 := by
intro bs ds hmap hds b hb
have hbmap : b.val ∈ List.map (fun b : Std.U8 => b.val) bs :=
List.mem_map_of_mem hb
rw [hmap] at hbmap
obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap
have := hds d hd
omega
have offset_safe : forall (whole rest : Slice Std.U8),
whole.val.length <= 9223372036854120458 ->
rest.val.length <= whole.val.length ->
exists z : Std.Isize,
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
rest whole = ok z /\
z.val = whole.val.length - rest.val.length := by
intro whole rest hw hr
have hbits := Dec2Flt.configured_x86_64_numBits
have hwcast : (Slice.len whole).val <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits, hbits]
omega
have hrcast : (Slice.len rest).val <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits, hbits]
omega
have hwsp := UScalar.hcast_inBounds_spec .Isize (Slice.len whole) hwcast
have hrsp := UScalar.hcast_inBounds_spec .Isize (Slice.len rest) hrcast
obtain ⟨wi, hwi, hwival⟩ := Aeneas.Std.WP.spec_imp_exists hwsp
obtain ⟨ri, hri, hrival⟩ := Aeneas.Std.WP.spec_imp_exists hrsp
simp at hwival hrival
have hsublo : IScalar.min .Isize <= wi.val - ri.val := by
simp [IScalar.min, IScalarTy.numBits, hbits]
omega
have hsubhi : wi.val - ri.val <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits, hbits]
omega
have hsub := IScalar.sub_spec hsublo hsubhi
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hsub
refine ⟨z, ?_, ?_⟩
· simp [SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from,
hwi, hri, hz]
· omega
cases frac with
| none =>
simp at hvalid hbytes
rcases hvalid with ⟨hints, hintsne, hexp⟩
cases expo with
| none =>
simp at hbytes
have hsdigits := byte_digits s.val ints hbytes hints
obtain ⟨mantissa, rest, hscan, hrest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 s.val [] (by simp) hsdigits
(by simp) hlen
have hrestnil : rest.val = [] := by simpa using hrest
let empty : Slice Std.U8 := ⟨[], by simp⟩
have hresteq : rest = empty := (Slice.eq_iff _ _).2 hrestnil
subst rest
have hrestle : empty.val.length <= s.val.length := by simp [empty]
obtain ⟨nDigits, hoff, hnval⟩ := offset_safe s empty hlen hrestle
have hnpos : nDigits.val > 0 := by
have hslen : s.val.length = ints.length := by
have := congrArg List.length hbytes
simpa using this
have hintlen : 0 < ints.length := by
cases ints <;> simp_all
simp [empty] at hnval
omega
have hcastlen : lift (IScalar.hcast .Usize nDigits) = ok (Slice.len s) := by
have hbound : 0 <= nDigits.val /\
nDigits.val <= UScalar.max .Usize := by
simp [empty] at hnval
simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
have hspec := IScalar.hcast_inBounds_spec .Usize nDigits hbound
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
simp [empty] at hnval
simpa [Slice.len_val] using (show z.val = s.val.length by omega)
have haddzero : (nDigits + 0#isize) = ok nDigits := by
have hspec := 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]
omega)
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
simpa using hzval
have hnotempty : Not (s.val = []) := by simpa using hne
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscan, hoff, haddzero, hnpos, hcastlen, empty,
core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst]
split
· rename_i hsmall
exact ⟨{
exponent := 0#i64
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
· rename_i hlarge
have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := nDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
NumberHelperIntegralAdjustment s nMore s.val [] (by simp)
hsdigits (by simp) (by simp) hlen (by
simp [empty] at hnval
norm_num at hnMoreVal
omega)
rw [hadjusted]
simp
split
· rename_i hpositive
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperIntegralManyDigitsSafeLength s empty 0#i64
nMore adjusted s.val [] (by simp) (by simp [empty])
hsdigits (by simp) (by simp) hlen (by
simp [empty] at hnval
norm_num at hnMoreVal
omega) hadjusted (by omega) (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
norm_num
norm_num at hnMoreVal
simp [empty] at hnval
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· exact ⟨{
exponent := 0#i64
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
| some e =>
simp at hbytes hexp
rcases e with ⟨upper, sign, expds⟩
simp only [Prod.fst, Prod.snd] at hbytes hexp
obtain ⟨hexpne, hexpdigits⟩ := hexp
obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
List.map_eq_append_iff.mp hbytes
obtain ⟨marker, suffixBytes, hafterVal, hmarkerVal, hsuffixMap⟩ :=
List.map_eq_cons_iff.mp hafterMap
have hmarker : marker = if upper then 69#u8 else 101#u8 := by
apply UScalar.eq_of_val_eq
cases upper <;> simp_all
subst marker
have hsdecomp : s.val = intBytes ++
(if upper then 69#u8 else 101#u8) :: suffixBytes := by
rw [hsval, hafterVal]
have hintDigits := byte_digits intBytes ints hintMap hints
have hintTailProp :
((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
Usize.max := by
have := s.property
rw [hsdecomp] at this
simp only [List.length_append] at this
omega
let intTail : Slice Std.U8 :=
⟨(if upper then 69#u8 else 101#u8) :: suffixBytes, hintTailProp⟩
obtain ⟨mantissa, intRest, hscan, hintRest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 intBytes
((if upper then 69#u8 else 101#u8) :: suffixBytes)
hsdecomp hintDigits (by
intro b rest h
simp only [List.cons.injEq] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num) hlen
have hintRestVal : intRest.val =
(if upper then 69#u8 else 101#u8) :: suffixBytes := by
simpa using hintRest
have hintRestEq : intRest = intTail := (Slice.eq_iff _ _).2 hintRestVal
subst intRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSafeLength s intTail hlen (by
rw [hsdecomp]
simp [intTail]
)
have hnDigitsEq : nDigits.val = intBytes.length := by
rw [hnDigitsVal, hsdecomp]
simp [intTail]
have hsuffixProp : suffixBytes.length <= Usize.max := by
have := intTail.property
simp [intTail] at this
omega
let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
have hsplitMarker : core.slice.Slice.split_first intTail =
ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
intTail, suffix]
have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
change suffixBytes.length <= 9223372036854120458
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega
obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
hexpshort, _⟩ :=
NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
(by simpa only [bne_iff_ne] using hexpne) hexpdigits
(by simpa [suffix] using hsuffixMap) hsuffixLen
have haddExp : (0#i64 + parsedExp) = ok parsedExp := by
have hb : IScalar.min .I64 <= (0#i64).val + parsedExp.val /\
(0#i64).val + parsedExp.val <= IScalar.max .I64 := by
have hp := IScalar.hBounds parsedExp
rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
IScalar.max_IScalarTy_I64_eq, I64.max_eq]
norm_num at hp ⊢
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hb.1 hb.2)
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
norm_num at hzval
exact hzval
have hemptyLen : empty.val.length <= s.val.length := by
simp [hempty]
obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
NumberHelperOffsetSafeLength s empty hlen hemptyLen
have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
ok (Slice.len s) := by
have hbound : 0 <= fullOffset.val /\
fullOffset.val <= UScalar.max .Usize := by
rw [hfullOffsetVal, hempty]
simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hfullOffsetVal, hempty] at hzval
simpa [Slice.len_val] using hzval
have haddzero : (nDigits + 0#isize) = ok nDigits := by
have hb : IScalar.min .Isize <= nDigits.val + (0#isize).val /\
nDigits.val + (0#isize).val <= IScalar.max .Isize := by
have hn := IScalar.hBounds nDigits
simp [IScalar.min, IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hn ⊢
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hb.1 hb.2)
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
norm_num at hzval
exact hzval
have hnpos : 0 < nDigits.val := by
have : 0 < ints.length := List.length_pos_of_ne_nil hintsne
have hlenEq := congrArg List.length hintMap
simp only [List.length_map] at hlenEq
omega
have hstartIntTail : s.val = intBytes ++ intTail.val := by
simpa [intTail] using hsdecomp
have hintTailNotDigit : forall b rest, intTail.val = b :: rest ->
Not (48 <= b.val /\ b.val <= 57) := by
intro b rest h
simp [intTail] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num
have hintTailNotPoint : forall b rest, intTail.val = b :: rest ->
b ≠ 46#u8 := by
intro b rest h
simp [intTail] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num
have hnotempty : Not (s.val = []) := by simpa using hne
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscan, hoffInt]
cases upper <;> simp [haddzero, hnpos, hsplitMarker, hscientific,
haddExp, hoffFull, hfullCast, UScalar.ofNatCore,
core.option.Option.Insts.CoreOpsTry_traitTry.branch,
dec2flt_model.ModelOptionBranch]
case false | true =>
split
· exact ⟨{
exponent := parsedExp
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
· rename_i hlarge
have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
have hn := IScalar.hBounds nDigits
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hn ⊢
omega
have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
have hn := IScalar.hBounds nDigits
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hn ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := nDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
NumberHelperIntegralAdjustment s nMore intBytes
intTail.val hstartIntTail hintDigits hintTailNotDigit
hintTailNotPoint hlen (by
norm_num at hnMoreVal
omega)
rw [hadjusted]
simp
split
· rename_i hpositive
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperIntegralManyDigitsSafeLength s intTail parsedExp
nMore adjusted intBytes intTail.val hstartIntTail rfl
hintDigits hintTailNotDigit hintTailNotPoint hlen (by
norm_num at hnMoreVal
omega) hadjusted hpositive (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· exact ⟨{
exponent := parsedExp
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
| some fracs =>
simp at hvalid hbytes
rcases hvalid with ⟨hints, hfracs, hsome, hexp⟩
cases expo with
| none =>
simp at hbytes
obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
List.map_eq_append_iff.mp hbytes
obtain ⟨point, fracBytes, hafterVal, hpointVal, hfracMap⟩ :=
List.map_eq_cons_iff.mp hafterMap
have hpoint : point = 46#u8 := by
apply UScalar.eq_of_val_eq
simpa using hpointVal
subst point
have hsdecomp : s.val = intBytes ++ 46#u8 :: fracBytes := by
rw [hsval, hafterVal]
have hintDigits := byte_digits intBytes ints hintMap hints
have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
have hintTailProp : (46#u8 :: fracBytes).length <= Usize.max := by
have := s.property
rw [hsdecomp] at this
simp only [List.length_append] at this
omega
let intTail : Slice Std.U8 := ⟨46#u8 :: fracBytes, hintTailProp⟩
obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 intBytes (46#u8 :: fracBytes)
hsdecomp hintDigits (by
intro b rest h
simp only [List.cons.injEq] at h
rcases h with ⟨rfl, _⟩
norm_num) hlen
have hintRestEq : intRest = intTail :=
(Slice.eq_iff _ _).2 (by simpa [intTail] using hintRest)
subst intRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSafeLength s intTail hlen (by
simp [intTail, hsdecomp])
have hnDigitsEq : nDigits.val = intBytes.length := by
rw [hnDigitsVal, hsdecomp]
simp [intTail]
have hfracProp : fracBytes.length <= Usize.max := by
have := intTail.property
simp [intTail] at this ⊢
omega
let fracSlice : Slice Std.U8 := ⟨fracBytes, hfracProp⟩
have hsplitPoint : core.slice.Slice.split_first intTail =
ok (some (46#u8, fracSlice)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
intTail, fracSlice]
obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes []
(by simp [fracSlice]) hfracDigits (by simp) (by
simp [fracSlice]
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
let empty : Slice Std.U8 := ⟨[], by simp⟩
have hfracRestEq : fracRest = empty :=
(Slice.eq_iff _ _).2 (by simpa [empty] using hfracRest)
subst fracRest
obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
NumberHelperOffsetSafeLength fracSlice empty (by
simp [fracSlice]
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega) (by simp [empty])
have hnAfterEq : nAfter.val = fracBytes.length := by
rw [hnAfterVal]
simp [fracSlice, empty]
have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
rw [hnAfterVal]
simp [empty, IScalar.min, 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
have hlenParts := hlen
rw [hsdecomp] at hlenParts
simp only [List.length_append, List.length_cons] at hlenParts
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 [hsdecomp] 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 hintLen : intBytes.length = ints.length := by
have := congrArg List.length hintMap
simpa using this
have hfracLen : fracBytes.length = fracs.length := by
have := congrArg List.length hfracMap
simpa using this
have htotalPos : 0 < totalDigits.val := by
rw [htotalVal, hnDigitsEq, hnAfterEq]
rcases hsome with hneInt | hneFrac
· have : 0 < ints.length := List.length_pos_of_ne_nil hneInt
omega
· have : 0 < fracs.length := List.length_pos_of_ne_nil hneFrac
omega
have htotalLen : totalDigits.val + 1 = s.val.length := by
rw [htotalVal, hnDigitsEq, hnAfterEq, hsdecomp]
simp
omega
obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
NumberHelperOffsetSafeLength s empty hlen (by simp [empty])
have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
ok (Slice.len s) := by
have hbound : 0 <= fullOffset.val /\
fullOffset.val <= UScalar.max .Usize := by
rw [hfullOffsetVal]
simp [empty, UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hfullOffsetVal] at hzval
simpa [empty, Slice.len_val] using hzval
have hnotempty : Not (s.val = []) := by simpa using hne
have hsplitEmpty : core.slice.Slice.split_first empty = ok none := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
empty]
have hpointMatch :
(match (46#u8 : Std.U8) with
| 46#uscalar => ok (empty, mantissa1, nAfter, fracExponent)
| _ => ok (intTail, mantissa, 0#isize, 0#i64)) =
ok (empty, mantissa1, nAfter, fracExponent) := by
rfl
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitEmpty,
hoffFull, hfullCast]
norm_num
simp only [UScalar.ofNat, UScalar.ofNatCore]
simp [htotal, htotalPos, hsplitEmpty, hoffFull, hfullCast]
split
· exact ⟨{
exponent := fracExponent
mantissa := mantissa1
negative := false
many_digits := false
}, rfl⟩
· rename_i hlarge
have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := totalDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted⟩ :=
NumberHelperAdjustManySafeLength s nMore hlen (by
norm_num at hnMoreVal
omega) (by
norm_num at hnMoreVal
rw [hnMoreVal]
omega)
rw [hadjusted]
simp
split
· rename_i hpositive
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperFractionalManyDigitsSafeLength s intTail 0#i64
intBytes fracBytes [] (by simpa [List.append_assoc] using hsdecomp)
(by simp [intTail]) hintDigits hfracDigits (by simp) hlen
(by
rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq]
norm_num
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega) (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
norm_num
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· exact ⟨{
exponent := fracExponent
mantissa := mantissa1
negative := false
many_digits := false
}, rfl⟩
| some e =>
simp at hbytes hexp
rcases e with ⟨upper, sign, expds⟩
simp only [Prod.fst, Prod.snd] at hbytes hexp
obtain ⟨hexpne, hexpdigits⟩ := hexp
obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
List.map_eq_append_iff.mp hbytes
obtain ⟨point, afterPoint, hafterVal, hpointVal, hafterPointMap⟩ :=
List.map_eq_cons_iff.mp hafterMap
have hpoint : point = 46#u8 := by
apply UScalar.eq_of_val_eq
simpa using hpointVal
subst point
obtain ⟨fracBytes, afterFrac, hafterPointVal, hfracMap,
hafterFracMap⟩ := List.map_eq_append_iff.mp hafterPointMap
obtain ⟨marker, suffixBytes, hafterFracVal, hmarkerVal, hsuffixMap⟩ :=
List.map_eq_cons_iff.mp hafterFracMap
have hmarker : marker = if upper then 69#u8 else 101#u8 := by
apply UScalar.eq_of_val_eq
cases upper <;> simp_all
subst marker
have hsdecomp : s.val = intBytes ++ 46#u8 ::
(fracBytes ++ (if upper then 69#u8 else 101#u8) :: suffixBytes) := by
rw [hsval, hafterVal, hafterPointVal, hafterFracVal]
have hintDigits := byte_digits intBytes ints hintMap hints
have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
have hmarkerTailProp :
((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
Usize.max := by
have := s.property
rw [hsdecomp] at this
simp only [List.length_append, List.length_cons] at this ⊢
omega
let markerTail : Slice Std.U8 :=
⟨(if upper then 69#u8 else 101#u8) :: suffixBytes,
hmarkerTailProp⟩
have hmarkerTailNotDigit : forall b rest,
markerTail.val = b :: rest ->
Not (48 <= b.val /\ b.val <= 57) := by
intro b rest h
simp [markerTail] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num
have hintTailProp :
(46#u8 :: (fracBytes ++ markerTail.val)).length <= Usize.max := by
have := s.property
rw [hsdecomp] at this
simp [markerTail] at this ⊢
omega
let intTail : Slice Std.U8 :=
⟨46#u8 :: (fracBytes ++ markerTail.val), hintTailProp⟩
have hstart : s.val = intBytes ++ 46#u8 ::
(fracBytes ++ markerTail.val) := by
simpa [markerTail] using hsdecomp
obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 intBytes intTail.val
hstart hintDigits (by
intro b rest h
simp [intTail] at h
rcases h with ⟨rfl, _⟩
norm_num) hlen
have hintRestEq : intRest = intTail :=
(Slice.eq_iff _ _).2 (by simpa using hintRest)
subst intRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSafeLength s intTail hlen (by
rw [hstart]
simp [intTail])
have hnDigitsEq : nDigits.val = intBytes.length := by
rw [hnDigitsVal, hstart]
simp [intTail]
have hfracSliceProp : (fracBytes ++ markerTail.val).length <=
Usize.max := by
have := intTail.property
simp [intTail] at this ⊢
omega
let fracSlice : Slice Std.U8 :=
⟨fracBytes ++ markerTail.val, hfracSliceProp⟩
have hsplitPoint : core.slice.Slice.split_first intTail =
ok (some (46#u8, fracSlice)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
intTail, fracSlice]
obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes
markerTail.val (by simp [fracSlice]) hfracDigits hmarkerTailNotDigit
(by
simp [fracSlice]
rw [hstart] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
have hfracRestEq : fracRest = markerTail :=
(Slice.eq_iff _ _).2 (by simpa using hfracRest)
subst fracRest
obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
NumberHelperOffsetSafeLength fracSlice markerTail (by
simp [fracSlice]
rw [hstart] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega) (by simp [fracSlice])
have hnAfterEq : nAfter.val = fracBytes.length := by
rw [hnAfterVal]
simp [fracSlice]
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
have hparts := hlen
rw [hstart] at hparts
simp only [List.length_append, List.length_cons] at hparts
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 [hstart] 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 htotalPos : 0 < totalDigits.val := by
rw [htotalVal, hnDigitsEq, hnAfterEq]
have hintLen : intBytes.length = ints.length := by
have := congrArg List.length hintMap
simpa using this
have hfracLen : fracBytes.length = fracs.length := by
have := congrArg List.length hfracMap
simpa using this
rcases hsome with hneInt | hneFrac
· have := List.length_pos_of_ne_nil hneInt
omega
· have := List.length_pos_of_ne_nil hneFrac
omega
have hsuffixProp : suffixBytes.length <= Usize.max := by
have := markerTail.property
simp [markerTail] at this
omega
let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
have hsplitMarker : core.slice.Slice.split_first markerTail =
ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
markerTail, suffix]
have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
change suffixBytes.length <= 9223372036854120458
rw [hstart] at hlen
simp [markerTail, intTail] at hlen
omega
obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
hexpshort, hexpnonnegative⟩ :=
NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
(by simpa only [bne_iff_ne] using hexpne) hexpdigits
(by simpa [suffix] using hsuffixMap) hsuffixLen
have hsuffixLenEq : suffixBytes.length =
(match sign with
| none => []
| some negative => [if negative then 45 else 43]).length +
expds.length := by
have := congrArg List.length hsuffixMap
simpa using this
have hcombinedBounds : IScalar.min .I64 <=
fracExponent.val + parsedExp.val /\
fracExponent.val + parsedExp.val <= IScalar.max .I64 := by
have hparts := hlen
rw [hstart] at hparts
simp [markerTail, intTail] at hparts
rw [hfracExponentVal, hnegAfterVal, hnAfterEq,
IScalar.min_IScalarTy_I64_eq, I64.min_eq,
IScalar.max_IScalarTy_I64_eq, I64.max_eq]
constructor
· by_cases hshort : expds.length <= 5
· have hsharp := hexpshort hshort
omega
· have hq : 6 <= expds.length := by omega
by_cases hnegative : sign = some true
· subst sign
simp at hsuffixLenEq
omega
· have hnonnegative := hexpnonnegative hnegative
omega
· omega
obtain ⟨combinedExponent, hcombinedExponent,
hcombinedExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hcombinedBounds.1 hcombinedBounds.2)
have h101Target : (101#u8 : Std.U8) = 101#uscalar := by
apply UScalar.eq_of_val_eq
rfl
have h69Target : (69#u8 : Std.U8) = 69#uscalar := by
apply UScalar.eq_of_val_eq
rfl
obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
NumberHelperOffsetSafeLength s empty hlen (by simp [hempty])
have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
ok (Slice.len s) := by
have hbound : 0 <= fullOffset.val /\
fullOffset.val <= UScalar.max .Usize := by
rw [hfullOffsetVal, hempty]
simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hfullOffsetVal, hempty] at hzval
simpa [Slice.len_val] using hzval
have hnotempty : Not (s.val = []) := by simpa using hne
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
hoffFrac, hnegAfter, hfracExponent]
norm_num
simp only [UScalar.ofNat, UScalar.ofNatCore]
cases upper <;> simp [htotal, htotalPos, hsplitMarker, hscientific,
hcombinedExponent, hoffFull, hfullCast,
core.option.Option.Insts.CoreOpsTry_traitTry.branch,
dec2flt_model.ModelOptionBranch, UScalar.ofNatCore,
h101Target, h69Target]
case false | true =>
by_cases hsmall : totalDigits.val <= 19
· exact ⟨{
exponent := combinedExponent
mantissa := mantissa1
negative := false
many_digits := false
}, by simp [hsmall]⟩
· simp [hsmall]
have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := totalDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted⟩ :=
NumberHelperAdjustManySafeLength s nMore hlen (by
norm_num at hnMoreVal
omega) (by
norm_num at hnMoreVal
rw [hnMoreVal]
rw [htotalVal, hnDigitsEq, hnAfterEq]
rw [hstart]
simp
omega)
rw [hadjusted]
by_cases hpositive : 0 < adjusted.val
· simp [hpositive]
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperFractionalManyDigitsSafeLength s intTail parsedExp
intBytes fracBytes markerTail.val hstart (by simp [intTail])
hintDigits hfracDigits hmarkerTailNotDigit hlen (by
have hb := hcombinedBounds.1
rw [hfracExponentVal, hnegAfterVal, hnAfterEq] at hb
simpa [IScalar.min_IScalarTy_I64_eq, I64.min_eq] using hb) (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
rw [hstart] at hlen
simp at hlen
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· simp [hpositive]
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.Assumptions
import Tablet.DecimalGoalVocabularyDefault
import Tablet.NumberHelperDigitScanSafeLength
import Tablet.ModelSliceSplitFirst
import Tablet.NumberHelperAdjustManySafeLength
import Tablet.NumberHelperFractionalManyDigitsSafeLength
import Tablet.NumberHelperIntegralAdjustment
import Tablet.NumberHelperIntegralManyDigitsSafeLength
import Tablet.NumberHelperOffsetSafeLength
import Tablet.NumberHelperScientificRenderedSuffixSafeLength
import Tablet.NumberHelperScientificSuffixSafeLength
import Tablet.SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_parse_MIN_19DIGIT_INT
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count
import Tablet.num_imp_dec2flt_parse_parse_many_digits
import Tablet.num_imp_dec2flt_parse_parse_partial_number
import Tablet.num_imp_dec2flt_parse_parse_scientific
import Tablet.num_imp_dec2flt_parse_try_parse_19digits
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: NumberHelperPartialAcceptanceSafeLength]
theorem NumberHelperPartialAcceptanceSafeLength :
forall (s : Slice Std.U8) (x : DecimalLexeme),
SourceValidByteSlice s -> s.val != [] -> x.leadingSign = none ->
DecimalGoalVocabularyDefault.validNumeric x ->
DecimalGoalVocabularyDefault.bytes s =
DecimalGoalVocabularyDefault.renderNumeric x ->
s.val.length <= 9223372036854120458 ->
exists d,
num.imp.dec2flt.parse.parse_partial_number s =
ok (some (d, Slice.len s)) := by
-- BODY
intro s x hsource hne hsign hvalid hbytes hlen
rcases x with ⟨sign, ints, frac, expo⟩
simp only at hsign
subst sign
simp [DecimalGoalVocabularyDefault] at hvalid hbytes
have byte_digits : forall (bs : List Std.U8) (ds : List Nat),
List.map (fun b : Std.U8 => b.val) bs =
List.map (fun d : Nat => 48 + d) ds ->
(forall d, List.Mem d ds -> d < 10) ->
forall b, List.Mem b bs -> 48 <= b.val /\ b.val <= 57 := by
intro bs ds hmap hds b hb
have hbmap : b.val ∈ List.map (fun b : Std.U8 => b.val) bs :=
List.mem_map_of_mem hb
rw [hmap] at hbmap
obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap
have := hds d hd
omega
have offset_safe : forall (whole rest : Slice Std.U8),
whole.val.length <= 9223372036854120458 ->
rest.val.length <= whole.val.length ->
exists z : Std.Isize,
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
rest whole = ok z /\
z.val = whole.val.length - rest.val.length := by
intro whole rest hw hr
have hbits := Dec2Flt.configured_x86_64_numBits
have hwcast : (Slice.len whole).val <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits, hbits]
omega
have hrcast : (Slice.len rest).val <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits, hbits]
omega
have hwsp := UScalar.hcast_inBounds_spec .Isize (Slice.len whole) hwcast
have hrsp := UScalar.hcast_inBounds_spec .Isize (Slice.len rest) hrcast
obtain ⟨wi, hwi, hwival⟩ := Aeneas.Std.WP.spec_imp_exists hwsp
obtain ⟨ri, hri, hrival⟩ := Aeneas.Std.WP.spec_imp_exists hrsp
simp at hwival hrival
have hsublo : IScalar.min .Isize <= wi.val - ri.val := by
simp [IScalar.min, IScalarTy.numBits, hbits]
omega
have hsubhi : wi.val - ri.val <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits, hbits]
omega
have hsub := IScalar.sub_spec hsublo hsubhi
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hsub
refine ⟨z, ?_, ?_⟩
· simp [SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from,
hwi, hri, hz]
· omega
cases frac with
| none =>
simp at hvalid hbytes
rcases hvalid with ⟨hints, hintsne, hexp⟩
cases expo with
| none =>
simp at hbytes
have hsdigits := byte_digits s.val ints hbytes hints
obtain ⟨mantissa, rest, hscan, hrest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 s.val [] (by simp) hsdigits
(by simp) hlen
have hrestnil : rest.val = [] := by simpa using hrest
let empty : Slice Std.U8 := ⟨[], by simp⟩
have hresteq : rest = empty := (Slice.eq_iff _ _).2 hrestnil
subst rest
have hrestle : empty.val.length <= s.val.length := by simp [empty]
obtain ⟨nDigits, hoff, hnval⟩ := offset_safe s empty hlen hrestle
have hnpos : nDigits.val > 0 := by
have hslen : s.val.length = ints.length := by
have := congrArg List.length hbytes
simpa using this
have hintlen : 0 < ints.length := by
cases ints <;> simp_all
simp [empty] at hnval
omega
have hcastlen : lift (IScalar.hcast .Usize nDigits) = ok (Slice.len s) := by
have hbound : 0 <= nDigits.val /\
nDigits.val <= UScalar.max .Usize := by
simp [empty] at hnval
simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
have hspec := IScalar.hcast_inBounds_spec .Usize nDigits hbound
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
simp [empty] at hnval
simpa [Slice.len_val] using (show z.val = s.val.length by omega)
have haddzero : (nDigits + 0#isize) = ok nDigits := by
have hspec := 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]
omega)
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
simpa using hzval
have hnotempty : Not (s.val = []) := by simpa using hne
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscan, hoff, haddzero, hnpos, hcastlen, empty,
core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst]
split
· rename_i hsmall
exact ⟨{
exponent := 0#i64
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
· rename_i hlarge
have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := nDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
NumberHelperIntegralAdjustment s nMore s.val [] (by simp)
hsdigits (by simp) (by simp) hlen (by
simp [empty] at hnval
norm_num at hnMoreVal
omega)
rw [hadjusted]
simp
split
· rename_i hpositive
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperIntegralManyDigitsSafeLength s empty 0#i64
nMore adjusted s.val [] (by simp) (by simp [empty])
hsdigits (by simp) (by simp) hlen (by
simp [empty] at hnval
norm_num at hnMoreVal
omega) hadjusted (by omega) (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
norm_num
norm_num at hnMoreVal
simp [empty] at hnval
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· exact ⟨{
exponent := 0#i64
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
| some e =>
simp at hbytes hexp
rcases e with ⟨upper, sign, expds⟩
simp only [Prod.fst, Prod.snd] at hbytes hexp
obtain ⟨hexpne, hexpdigits⟩ := hexp
obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
List.map_eq_append_iff.mp hbytes
obtain ⟨marker, suffixBytes, hafterVal, hmarkerVal, hsuffixMap⟩ :=
List.map_eq_cons_iff.mp hafterMap
have hmarker : marker = if upper then 69#u8 else 101#u8 := by
apply UScalar.eq_of_val_eq
cases upper <;> simp_all
subst marker
have hsdecomp : s.val = intBytes ++
(if upper then 69#u8 else 101#u8) :: suffixBytes := by
rw [hsval, hafterVal]
have hintDigits := byte_digits intBytes ints hintMap hints
have hintTailProp :
((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
Usize.max := by
have := s.property
rw [hsdecomp] at this
simp only [List.length_append] at this
omega
let intTail : Slice Std.U8 :=
⟨(if upper then 69#u8 else 101#u8) :: suffixBytes, hintTailProp⟩
obtain ⟨mantissa, intRest, hscan, hintRest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 intBytes
((if upper then 69#u8 else 101#u8) :: suffixBytes)
hsdecomp hintDigits (by
intro b rest h
simp only [List.cons.injEq] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num) hlen
have hintRestVal : intRest.val =
(if upper then 69#u8 else 101#u8) :: suffixBytes := by
simpa using hintRest
have hintRestEq : intRest = intTail := (Slice.eq_iff _ _).2 hintRestVal
subst intRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSafeLength s intTail hlen (by
rw [hsdecomp]
simp [intTail]
)
have hnDigitsEq : nDigits.val = intBytes.length := by
rw [hnDigitsVal, hsdecomp]
simp [intTail]
have hsuffixProp : suffixBytes.length <= Usize.max := by
have := intTail.property
simp [intTail] at this
omega
let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
have hsplitMarker : core.slice.Slice.split_first intTail =
ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
intTail, suffix]
have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
change suffixBytes.length <= 9223372036854120458
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega
obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
hexpshort, _⟩ :=
NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
(by simpa only [bne_iff_ne] using hexpne) hexpdigits
(by simpa [suffix] using hsuffixMap) hsuffixLen
have haddExp : (0#i64 + parsedExp) = ok parsedExp := by
have hb : IScalar.min .I64 <= (0#i64).val + parsedExp.val /\
(0#i64).val + parsedExp.val <= IScalar.max .I64 := by
have hp := IScalar.hBounds parsedExp
rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
IScalar.max_IScalarTy_I64_eq, I64.max_eq]
norm_num at hp ⊢
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hb.1 hb.2)
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
norm_num at hzval
exact hzval
have hemptyLen : empty.val.length <= s.val.length := by
simp [hempty]
obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
NumberHelperOffsetSafeLength s empty hlen hemptyLen
have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
ok (Slice.len s) := by
have hbound : 0 <= fullOffset.val /\
fullOffset.val <= UScalar.max .Usize := by
rw [hfullOffsetVal, hempty]
simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hfullOffsetVal, hempty] at hzval
simpa [Slice.len_val] using hzval
have haddzero : (nDigits + 0#isize) = ok nDigits := by
have hb : IScalar.min .Isize <= nDigits.val + (0#isize).val /\
nDigits.val + (0#isize).val <= IScalar.max .Isize := by
have hn := IScalar.hBounds nDigits
simp [IScalar.min, IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hn ⊢
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hb.1 hb.2)
rw [hz]
congr 2
apply IScalar.eq_of_val_eq
norm_num at hzval
exact hzval
have hnpos : 0 < nDigits.val := by
have : 0 < ints.length := List.length_pos_of_ne_nil hintsne
have hlenEq := congrArg List.length hintMap
simp only [List.length_map] at hlenEq
omega
have hstartIntTail : s.val = intBytes ++ intTail.val := by
simpa [intTail] using hsdecomp
have hintTailNotDigit : forall b rest, intTail.val = b :: rest ->
Not (48 <= b.val /\ b.val <= 57) := by
intro b rest h
simp [intTail] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num
have hintTailNotPoint : forall b rest, intTail.val = b :: rest ->
b ≠ 46#u8 := by
intro b rest h
simp [intTail] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num
have hnotempty : Not (s.val = []) := by simpa using hne
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscan, hoffInt]
cases upper <;> simp [haddzero, hnpos, hsplitMarker, hscientific,
haddExp, hoffFull, hfullCast, UScalar.ofNatCore,
core.option.Option.Insts.CoreOpsTry_traitTry.branch,
dec2flt_model.ModelOptionBranch]
case false | true =>
split
· exact ⟨{
exponent := parsedExp
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
· rename_i hlarge
have hsublo : IScalar.min .Isize <= nDigits.val - 19 := by
have hn := IScalar.hBounds nDigits
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hn ⊢
omega
have hsubhi : nDigits.val - 19 <= IScalar.max .Isize := by
have hn := IScalar.hBounds nDigits
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at hn ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := nDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted, hadjustedPositive⟩ :=
NumberHelperIntegralAdjustment s nMore intBytes
intTail.val hstartIntTail hintDigits hintTailNotDigit
hintTailNotPoint hlen (by
norm_num at hnMoreVal
omega)
rw [hadjusted]
simp
split
· rename_i hpositive
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperIntegralManyDigitsSafeLength s intTail parsedExp
nMore adjusted intBytes intTail.val hstartIntTail rfl
hintDigits hintTailNotDigit hintTailNotPoint hlen (by
norm_num at hnMoreVal
omega) hadjusted hpositive (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· exact ⟨{
exponent := parsedExp
mantissa := mantissa
negative := false
many_digits := false
}, rfl⟩
| some fracs =>
simp at hvalid hbytes
rcases hvalid with ⟨hints, hfracs, hsome, hexp⟩
cases expo with
| none =>
simp at hbytes
obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
List.map_eq_append_iff.mp hbytes
obtain ⟨point, fracBytes, hafterVal, hpointVal, hfracMap⟩ :=
List.map_eq_cons_iff.mp hafterMap
have hpoint : point = 46#u8 := by
apply UScalar.eq_of_val_eq
simpa using hpointVal
subst point
have hsdecomp : s.val = intBytes ++ 46#u8 :: fracBytes := by
rw [hsval, hafterVal]
have hintDigits := byte_digits intBytes ints hintMap hints
have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
have hintTailProp : (46#u8 :: fracBytes).length <= Usize.max := by
have := s.property
rw [hsdecomp] at this
simp only [List.length_append] at this
omega
let intTail : Slice Std.U8 := ⟨46#u8 :: fracBytes, hintTailProp⟩
obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 intBytes (46#u8 :: fracBytes)
hsdecomp hintDigits (by
intro b rest h
simp only [List.cons.injEq] at h
rcases h with ⟨rfl, _⟩
norm_num) hlen
have hintRestEq : intRest = intTail :=
(Slice.eq_iff _ _).2 (by simpa [intTail] using hintRest)
subst intRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSafeLength s intTail hlen (by
simp [intTail, hsdecomp])
have hnDigitsEq : nDigits.val = intBytes.length := by
rw [hnDigitsVal, hsdecomp]
simp [intTail]
have hfracProp : fracBytes.length <= Usize.max := by
have := intTail.property
simp [intTail] at this ⊢
omega
let fracSlice : Slice Std.U8 := ⟨fracBytes, hfracProp⟩
have hsplitPoint : core.slice.Slice.split_first intTail =
ok (some (46#u8, fracSlice)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
intTail, fracSlice]
obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes []
(by simp [fracSlice]) hfracDigits (by simp) (by
simp [fracSlice]
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
let empty : Slice Std.U8 := ⟨[], by simp⟩
have hfracRestEq : fracRest = empty :=
(Slice.eq_iff _ _).2 (by simpa [empty] using hfracRest)
subst fracRest
obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
NumberHelperOffsetSafeLength fracSlice empty (by
simp [fracSlice]
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega) (by simp [empty])
have hnAfterEq : nAfter.val = fracBytes.length := by
rw [hnAfterVal]
simp [fracSlice, empty]
have hnAfterNotMin : nAfter.val ≠ IScalar.min .Isize := by
rw [hnAfterVal]
simp [empty, IScalar.min, 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
have hlenParts := hlen
rw [hsdecomp] at hlenParts
simp only [List.length_append, List.length_cons] at hlenParts
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 [hsdecomp] 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 hintLen : intBytes.length = ints.length := by
have := congrArg List.length hintMap
simpa using this
have hfracLen : fracBytes.length = fracs.length := by
have := congrArg List.length hfracMap
simpa using this
have htotalPos : 0 < totalDigits.val := by
rw [htotalVal, hnDigitsEq, hnAfterEq]
rcases hsome with hneInt | hneFrac
· have : 0 < ints.length := List.length_pos_of_ne_nil hneInt
omega
· have : 0 < fracs.length := List.length_pos_of_ne_nil hneFrac
omega
have htotalLen : totalDigits.val + 1 = s.val.length := by
rw [htotalVal, hnDigitsEq, hnAfterEq, hsdecomp]
simp
omega
obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
NumberHelperOffsetSafeLength s empty hlen (by simp [empty])
have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
ok (Slice.len s) := by
have hbound : 0 <= fullOffset.val /\
fullOffset.val <= UScalar.max .Usize := by
rw [hfullOffsetVal]
simp [empty, UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hfullOffsetVal] at hzval
simpa [empty, Slice.len_val] using hzval
have hnotempty : Not (s.val = []) := by simpa using hne
have hsplitEmpty : core.slice.Slice.split_first empty = ok none := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
empty]
have hpointMatch :
(match (46#u8 : Std.U8) with
| 46#uscalar => ok (empty, mantissa1, nAfter, fracExponent)
| _ => ok (intTail, mantissa, 0#isize, 0#i64)) =
ok (empty, mantissa1, nAfter, fracExponent) := by
rfl
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
hoffFrac, hnegAfter, hfracExponent, htotal, htotalPos, hsplitEmpty,
hoffFull, hfullCast]
norm_num
simp only [UScalar.ofNat, UScalar.ofNatCore]
simp [htotal, htotalPos, hsplitEmpty, hoffFull, hfullCast]
split
· exact ⟨{
exponent := fracExponent
mantissa := mantissa1
negative := false
many_digits := false
}, rfl⟩
· rename_i hlarge
have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := totalDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted⟩ :=
NumberHelperAdjustManySafeLength s nMore hlen (by
norm_num at hnMoreVal
omega) (by
norm_num at hnMoreVal
rw [hnMoreVal]
omega)
rw [hadjusted]
simp
split
· rename_i hpositive
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperFractionalManyDigitsSafeLength s intTail 0#i64
intBytes fracBytes [] (by simpa [List.append_assoc] using hsdecomp)
(by simp [intTail]) hintDigits hfracDigits (by simp) hlen
(by
rw [IScalar.min_IScalarTy_I64_eq, I64.min_eq]
norm_num
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega) (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
norm_num
rw [hsdecomp] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· exact ⟨{
exponent := fracExponent
mantissa := mantissa1
negative := false
many_digits := false
}, rfl⟩
| some e =>
simp at hbytes hexp
rcases e with ⟨upper, sign, expds⟩
simp only [Prod.fst, Prod.snd] at hbytes hexp
obtain ⟨hexpne, hexpdigits⟩ := hexp
obtain ⟨intBytes, afterInt, hsval, hintMap, hafterMap⟩ :=
List.map_eq_append_iff.mp hbytes
obtain ⟨point, afterPoint, hafterVal, hpointVal, hafterPointMap⟩ :=
List.map_eq_cons_iff.mp hafterMap
have hpoint : point = 46#u8 := by
apply UScalar.eq_of_val_eq
simpa using hpointVal
subst point
obtain ⟨fracBytes, afterFrac, hafterPointVal, hfracMap,
hafterFracMap⟩ := List.map_eq_append_iff.mp hafterPointMap
obtain ⟨marker, suffixBytes, hafterFracVal, hmarkerVal, hsuffixMap⟩ :=
List.map_eq_cons_iff.mp hafterFracMap
have hmarker : marker = if upper then 69#u8 else 101#u8 := by
apply UScalar.eq_of_val_eq
cases upper <;> simp_all
subst marker
have hsdecomp : s.val = intBytes ++ 46#u8 ::
(fracBytes ++ (if upper then 69#u8 else 101#u8) :: suffixBytes) := by
rw [hsval, hafterVal, hafterPointVal, hafterFracVal]
have hintDigits := byte_digits intBytes ints hintMap hints
have hfracDigits := byte_digits fracBytes fracs hfracMap hfracs
have hmarkerTailProp :
((if upper then 69#u8 else 101#u8) :: suffixBytes).length <=
Usize.max := by
have := s.property
rw [hsdecomp] at this
simp only [List.length_append, List.length_cons] at this ⊢
omega
let markerTail : Slice Std.U8 :=
⟨(if upper then 69#u8 else 101#u8) :: suffixBytes,
hmarkerTailProp⟩
have hmarkerTailNotDigit : forall b rest,
markerTail.val = b :: rest ->
Not (48 <= b.val /\ b.val <= 57) := by
intro b rest h
simp [markerTail] at h
rcases h with ⟨rfl, _⟩
cases upper <;> norm_num
have hintTailProp :
(46#u8 :: (fracBytes ++ markerTail.val)).length <= Usize.max := by
have := s.property
rw [hsdecomp] at this
simp [markerTail] at this ⊢
omega
let intTail : Slice Std.U8 :=
⟨46#u8 :: (fracBytes ++ markerTail.val), hintTailProp⟩
have hstart : s.val = intBytes ++ 46#u8 ::
(fracBytes ++ markerTail.val) := by
simpa [markerTail] using hsdecomp
obtain ⟨mantissa, intRest, hscanInt, hintRest⟩ :=
NumberHelperDigitScanSafeLength s 0#u64 intBytes intTail.val
hstart hintDigits (by
intro b rest h
simp [intTail] at h
rcases h with ⟨rfl, _⟩
norm_num) hlen
have hintRestEq : intRest = intTail :=
(Slice.eq_iff _ _).2 (by simpa using hintRest)
subst intRest
obtain ⟨nDigits, hoffInt, hnDigitsVal⟩ :=
NumberHelperOffsetSafeLength s intTail hlen (by
rw [hstart]
simp [intTail])
have hnDigitsEq : nDigits.val = intBytes.length := by
rw [hnDigitsVal, hstart]
simp [intTail]
have hfracSliceProp : (fracBytes ++ markerTail.val).length <=
Usize.max := by
have := intTail.property
simp [intTail] at this ⊢
omega
let fracSlice : Slice Std.U8 :=
⟨fracBytes ++ markerTail.val, hfracSliceProp⟩
have hsplitPoint : core.slice.Slice.split_first intTail =
ok (some (46#u8, fracSlice)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
intTail, fracSlice]
obtain ⟨mantissa1, fracRest, hscanFrac, hfracRest⟩ :=
NumberHelperDigitScanSafeLength fracSlice mantissa fracBytes
markerTail.val (by simp [fracSlice]) hfracDigits hmarkerTailNotDigit
(by
simp [fracSlice]
rw [hstart] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega)
have hfracRestEq : fracRest = markerTail :=
(Slice.eq_iff _ _).2 (by simpa using hfracRest)
subst fracRest
obtain ⟨nAfter, hoffFrac, hnAfterVal⟩ :=
NumberHelperOffsetSafeLength fracSlice markerTail (by
simp [fracSlice]
rw [hstart] at hlen
simp only [List.length_append, List.length_cons] at hlen
omega) (by simp [fracSlice])
have hnAfterEq : nAfter.val = fracBytes.length := by
rw [hnAfterVal]
simp [fracSlice]
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
have hparts := hlen
rw [hstart] at hparts
simp only [List.length_append, List.length_cons] at hparts
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 [hstart] 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 htotalPos : 0 < totalDigits.val := by
rw [htotalVal, hnDigitsEq, hnAfterEq]
have hintLen : intBytes.length = ints.length := by
have := congrArg List.length hintMap
simpa using this
have hfracLen : fracBytes.length = fracs.length := by
have := congrArg List.length hfracMap
simpa using this
rcases hsome with hneInt | hneFrac
· have := List.length_pos_of_ne_nil hneInt
omega
· have := List.length_pos_of_ne_nil hneFrac
omega
have hsuffixProp : suffixBytes.length <= Usize.max := by
have := markerTail.property
simp [markerTail] at this
omega
let suffix : Slice Std.U8 := ⟨suffixBytes, hsuffixProp⟩
have hsplitMarker : core.slice.Slice.split_first markerTail =
ok (some (if upper then 69#u8 else 101#u8, suffix)) := by
simp [core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
markerTail, suffix]
have hsuffixLen : suffix.val.length <= 9223372036854120458 := by
change suffixBytes.length <= 9223372036854120458
rw [hstart] at hlen
simp [markerTail, intTail] at hlen
omega
obtain ⟨parsedExp, empty, hscientific, hempty, hexplo, hexphi,
hexpshort, hexpnonnegative⟩ :=
NumberHelperScientificRenderedSuffixSafeLength suffix sign expds
(by simpa only [bne_iff_ne] using hexpne) hexpdigits
(by simpa [suffix] using hsuffixMap) hsuffixLen
have hsuffixLenEq : suffixBytes.length =
(match sign with
| none => []
| some negative => [if negative then 45 else 43]).length +
expds.length := by
have := congrArg List.length hsuffixMap
simpa using this
have hcombinedBounds : IScalar.min .I64 <=
fracExponent.val + parsedExp.val /\
fracExponent.val + parsedExp.val <= IScalar.max .I64 := by
have hparts := hlen
rw [hstart] at hparts
simp [markerTail, intTail] at hparts
rw [hfracExponentVal, hnegAfterVal, hnAfterEq,
IScalar.min_IScalarTy_I64_eq, I64.min_eq,
IScalar.max_IScalarTy_I64_eq, I64.max_eq]
constructor
· by_cases hshort : expds.length <= 5
· have hsharp := hexpshort hshort
omega
· have hq : 6 <= expds.length := by omega
by_cases hnegative : sign = some true
· subst sign
simp at hsuffixLenEq
omega
· have hnonnegative := hexpnonnegative hnegative
omega
· omega
obtain ⟨combinedExponent, hcombinedExponent,
hcombinedExponentVal⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.add_spec hcombinedBounds.1 hcombinedBounds.2)
have h101Target : (101#u8 : Std.U8) = 101#uscalar := by
apply UScalar.eq_of_val_eq
rfl
have h69Target : (69#u8 : Std.U8) = 69#uscalar := by
apply UScalar.eq_of_val_eq
rfl
obtain ⟨fullOffset, hoffFull, hfullOffsetVal⟩ :=
NumberHelperOffsetSafeLength s empty hlen (by simp [hempty])
have hfullCast : lift (IScalar.hcast .Usize fullOffset) =
ok (Slice.len s) := by
have hbound : 0 <= fullOffset.val /\
fullOffset.val <= UScalar.max .Usize := by
rw [hfullOffsetVal, hempty]
simp [UScalar.max, Dec2Flt.configured_x86_64_numBits]
omega
obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize fullOffset hbound)
rw [hz]
congr 2
apply UScalar.eq_of_val_eq
rw [hfullOffsetVal, hempty] at hzval
simpa [Slice.len_val] using hzval
have hnotempty : Not (s.val = []) := by simpa using hne
unfold num.imp.dec2flt.parse.parse_partial_number
simp [hnotempty, hscanInt, hoffInt, hsplitPoint, hscanFrac,
hoffFrac, hnegAfter, hfracExponent]
norm_num
simp only [UScalar.ofNat, UScalar.ofNatCore]
cases upper <;> simp [htotal, htotalPos, hsplitMarker, hscientific,
hcombinedExponent, hoffFull, hfullCast,
core.option.Option.Insts.CoreOpsTry_traitTry.branch,
dec2flt_model.ModelOptionBranch, UScalar.ofNatCore,
h101Target, h69Target]
case false | true =>
by_cases hsmall : totalDigits.val <= 19
· exact ⟨{
exponent := combinedExponent
mantissa := mantissa1
negative := false
many_digits := false
}, by simp [hsmall]⟩
· simp [hsmall]
have hsublo : IScalar.min .Isize <= totalDigits.val - 19 := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.min, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
have hsubhi : totalDigits.val - 19 <= IScalar.max .Isize := by
have ht := IScalar.hBounds totalDigits
simp [IScalar.max, IScalarTy.numBits,
Dec2Flt.configured_x86_64_numBits] at ht ⊢
omega
obtain ⟨nMore, hnMore, hnMoreVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(IScalar.sub_spec (x := totalDigits) (y := 19#isize)
(by simpa using hsublo) (by simpa using hsubhi))
rw [hnMore]
simp
obtain ⟨adjusted, hadjusted⟩ :=
NumberHelperAdjustManySafeLength s nMore hlen (by
norm_num at hnMoreVal
omega) (by
norm_num at hnMoreVal
rw [hnMoreVal]
rw [htotalVal, hnDigitsEq, hnAfterEq]
rw [hstart]
simp
omega)
rw [hadjusted]
by_cases hpositive : 0 < adjusted.val
· simp [hpositive]
obtain ⟨mantissa2, exponent2, hmany⟩ :=
NumberHelperFractionalManyDigitsSafeLength s intTail parsedExp
intBytes fracBytes markerTail.val hstart (by simp [intTail])
hintDigits hfracDigits hmarkerTailNotDigit hlen (by
have hb := hcombinedBounds.1
rw [hfracExponentVal, hnegAfterVal, hnAfterEq] at hb
simpa [IScalar.min_IScalarTy_I64_eq, I64.min_eq] using hb) (by
rw [IScalar.max_IScalarTy_I64_eq, I64.max_eq]
rw [hstart] at hlen
simp at hlen
omega)
rw [hmany]
exact ⟨{
exponent := exponent2
mantissa := mantissa2
negative := false
many_digits := true
}, rfl⟩
· simp [hpositive]
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
- Assumptions
- DecimalGoalVocabularyDefault
- ModelSliceSplitFirst
- NumberHelperAdjustManySafeLength
- NumberHelperDigitScanSafeLength
- NumberHelperFractionalManyDigitsSafeLength
All 19 entries
- Assumptions
- DecimalGoalVocabularyDefault
- ModelSliceSplitFirst
- NumberHelperAdjustManySafeLength
- NumberHelperDigitScanSafeLength
- NumberHelperFractionalManyDigitsSafeLength
- NumberHelperIntegralAdjustment
- NumberHelperIntegralManyDigitsSafeLength
- NumberHelperOffsetSafeLength
- NumberHelperScientificRenderedSuffixSafeLength
- NumberHelperScientificSuffixSafeLength
- SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from
- SourceValidByteSlice
- num imp dec2flt parse MIN 19DIGIT INT
- num imp dec2flt parse adjust many digit count
- num imp dec2flt parse parse many digits
- num imp dec2flt parse parse partial number
- num imp dec2flt parse parse scientific
- num imp dec2flt parse try parse 19digits
Inspect related source (68)
- offset_from · dec2flt/common.rs:30 Program Rust
- MIN_19DIGIT_INT · dec2flt/parse.rs:8 Program Rust
- adjust_many_digit_count · dec2flt/parse.rs:86 Program Rust
More program dependencies (35)
- adjust_many_digit_count_loop · dec2flt/parse.rs:89 Program Rust
- body · dec2flt/parse.rs:89 Program Rust
- parse_many_digits · dec2flt/parse.rs:104 Program Rust
- 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
- 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
- read_u64 · dec2flt/common.rs:18 Program Rust
- is_8digits · dec2flt/common.rs:40 Program Rust
- Decimal · dec2flt/decimal.rs:32 Program Rust
- parse_8digits · dec2flt/parse.rs:18 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 (30)
- split_first · core/src/slice/mod.rs:198 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 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
- 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
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
Related source and models come from the extraction records and declared dependencies.
Proof assumptions and review
Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.
Transitive assumptions
- Classical.choice
- Dec2Flt.configured_x86_64_numBits
- Quot.sound
- propext
This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.
Helper theorems included
All 66 entries
- Binary64Model
- Binary64ModelDefault
- DecimalGoalVocabulary
- DecimalGoalVocabularyDefault
- DecimalLexeme
- 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
- NumberHelperAdjustManySafeLength
- NumberHelperDigitScanSafeLength
- NumberHelperEightDigitWord
- NumberHelperFractionalManyDigitsSafeLength
- NumberHelperIntegralAdjustment
- NumberHelperIntegralManyDigitsSafeLength
- NumberHelperOffsetSafeLength
- NumberHelperParseEightDigitsSafe
- NumberHelperPartialAcceptanceSafeLength
- NumberHelperReadEightBytes
- NumberHelperScientificRenderedSuffixSafeLength
- NumberHelperScientificShortSuffixSafeLength
- NumberHelperScientificSignedSuffixSafeLength
- NumberHelperScientificSuffixSafeLength
- NumberHelperScientificSuffixSharpSafeLength
- NumberHelperSplitExponentSuffix
- NumberHelperTryParse19SafeLength
- Preamble
- SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice offset from
- SliceU8 Insts Dec2flt modelNumImpDec2fltCommonByteSlice read u64
- SourceValidByteSlice
- core num FpCategory
- num imp dec2flt common BiasedFp
- 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 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": "NumberHelperPartialAcceptanceSafeLength",
"closure_version": "patch_c_v1",
"active_decl_hash": "166a6523f952970f3b6136dc7d0d7cd1399c2bf03b0aac7b75c35f7a9461a8c1",
"active_statement_hash": "4b1862a7360198fb98b9c140294d0f02a9d91bb787f4775d6cce012b35df4dae",
"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-110",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b",
"ModelSliceSplitFirst": "f617ee05dd2925a9",
"NumberHelperAdjustManySafeLength": "95baec0cbdffaa61",
"NumberHelperDigitScanSafeLength": "890b3a333811314c",
"NumberHelperFractionalManyDigitsSafeLength": "a5629aa775e4c63f",
"NumberHelperIntegralAdjustment": "87a0993d47acb7bb",
"NumberHelperIntegralManyDigitsSafeLength": "189c7a8b78fa907f",
"NumberHelperOffsetSafeLength": "20aaaa4dc95189e4",
"NumberHelperScientificRenderedSuffixSafeLength": "ab2dc4e68839f3a1",
"num_imp_dec2flt_parse_parse_partial_number": "0f3e5cc6a65d812e"
},
"strict_definition_deps": {
"DecimalGoalVocabulary": "c08dd4b4aea4abb4",
"DecimalGoalVocabularyDefault": "4634372c8f7a7f44",
"DecimalLexeme": "18938585e7684823",
"F64": "ec4e25fbaf4e11d4",
"ModelOptionBranch": "2b227898dbe15e59",
"ModelSliceSplitFirst": "4ed9c51a5bb9fb58",
"NumberHelperScientificRenderedSuffixSafeLength": "28ca0a8ac503ee01",
"Preamble": "4adf4172ae751d7d",
"SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
"SourceValidByteSlice": "3dc42716a6378d26",
"num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f",
"num_imp_dec2flt_decimal_Decimal": "4c41a56bf44cb31e",
"num_imp_dec2flt_parse_adjust_many_digit_count": "4e59e3776b6cfdaa",
"num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
"num_imp_dec2flt_parse_parse_partial_number": "359d30eefc709c2a",
"num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5",
"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:98f29485017e6821844f2cab9cb051ca15ecdd87d45203755b5d0954f2eebab7",
"current_fingerprint": "sound-v2:98f29485017e6821844f2cab9cb051ca15ecdd87d45203755b5d0954f2eebab7",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}