P367_T1_NumberHelperFractionalRetryA

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Lemma · Normal fractional retry with exact accounting

Let slices s,t have contents I\mathbin{++}[46]\mathbin{++}F\mathbin{++}R and [46]\mathbin{++}F\mathbin{++}R. Every byte of I and F is an ASCII digit; R is empty or has a nondigit head. Suppose |s|\le A and an explicit I64 exponent e has value in [-C,C], with A=3458764513820540928 and C=5764607523034234879. The actual extracted \texttt{parse\_many\_digits}(s,t,e) returns normally with a word m and exponent q. There exist byte lists P,Q such that
I\mathbin{++}F=P\mathbin{++}Q,\qquad m.\mathit{val}=\operatorname{foldl}(a,b\mapsto10a+(b.\mathit{val}-48),0,P),
m.\mathit{val}\le10^{19}-1,\qquad q.\mathit{val}=e.\mathit{val}-|F|+|Q|.
The digit lists may be empty. No success premise or significant-digit count premise is imposed.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.P367_T1_NumberHelperIntegralRetryA

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P367_T1_NumberHelperFractionalRetryA]
theorem P367_T1_NumberHelperFractionalRetryA
    (start intEnd : Slice Std.U8) (explicit : Std.I64)
    (integer fraction tail : List Std.U8)
    (hstart : start.val = integer ++ 46#u8 :: (fraction ++ tail))
    (hend : intEnd.val = 46#u8 :: (fraction ++ tail))
    (hinteger : forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57)
    (hfraction : forall b, b ∈ fraction -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57))
    (hlen : start.val.length <= 3458764513820540928)
    (hexpLo : -5764607523034234879 <= explicit.val)
    (hexpHi : explicit.val <= 5764607523034234879) :
    exists (mantissa : Std.U64) (exponent : Std.I64)
      (consumed remaining : List Std.U8),
      num.imp.dec2flt.parse.parse_many_digits start intEnd explicit =
        ok (mantissa, exponent) /\
      integer ++ fraction = consumed ++ remaining /\
      mantissa.val = consumed.foldl (fun a b => 10 * a + (b.val - 48)) 0 /\
      mantissa.val <= 9999999999999999999 /\
      exponent.val = explicit.val - (fraction.length : Int) +
        (remaining.length : Int) := 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

The point byte is not a digit. Apply NumberHelperTryParse19SafeLength to the first run I and tail [46]\mathbin{++}F\mathbin{++}R, starting at zero. Write its partition as I=P_1\mathbin{++}Q_1, its word as m_1, and its residual contents as Q_1\mathbin{++}[46]\mathbin{++}F\mathbin{++}R.
If m_1.\mathit{val}\ge10^{18}, the value of the whole integer run is at least 10^{18}: its fold extends the prefix fold by Q_1, and each natural update a\mapsto10a+(b.\mathit{val}-48) is at least a. Induction over Q_1 proves this monotonicity from every starting value. Apply NumberHelperIntegralRetryA with this point-headed tail. It gives a partition I=P\mathbin{++}Q' and a normal pair with exponent e.\mathit{val}+|Q'|. Set Q=Q'\mathbin{++}F. Then IF=PQ and e.\mathit{val}-|F|+|Q|=e.\mathit{val}+|Q'|; the other conclusions are unchanged.
Otherwise the low-result clause of NumberHelperTryParse19SafeLength gives Q_1=[], P_1=I, and first residual exactly [46]\mathbin{++}F\mathbin{++}R. Unfold num imp dec2flt parse parse many digits; the threshold from num imp dec2flt parse MIN 19DIGIT INT selects its low branch. The range-from-one operation is in bounds because that residual has a first point byte. It gives precisely the slice F\mathbin{++}R, whose length is at most |s| and whose Aeneas carrier bound follows from the original carrier. Apply NumberHelperTryParse19SafeLength again, now with initial word m_1, run F, and tail R. Its initial bound m_1.\mathit{val}\le10^{19}-1 is supplied by the first call. It returns normally with F=P_2\mathbin{++}Q_2, residual Q_2\mathbin{++}R, and word m equal to the fold of P_2 starting at the value of I, still bounded by 10^{19}-1.
Apply NumberHelperOffsetSourceLength to the whole fractional slice and its residual. Their lengths are at most A<2^{63}, and |Q_2|\le|F|. The normal signed offset is (|F|+|R|)-(|Q_2|+|R|)=|F|-|Q_2|\in[0,A]. Its checked negation is safe, since it is not the signed minimum, and has value -|F|+|Q_2|\in[-A,0]. Its signed-to-I64 cast is exact, since both endpoint values fit I64. NumberHelperExponentAddA makes the final addition normal with value e.\mathit{val}-|F|+|Q_2|. Take P=I\mathbin{++}P_2 and Q=Q_2. The fold-over-append identity identifies the returned word with the fold of P from zero. Substitution of the successful first scan, guarded slice, second scan, offset, negation, cast and addition into the actual monadic body gives the required normal pair in this branch also.

Lean proof


  have hfracBound : fraction.length <= 3458764513820540928 := by
    rw [hstart] at hlen
    simp only [List.length_append, List.length_cons] at hlen
    omega
  have hpointTail : forall b rest,
      46#u8 :: (fraction ++ tail) = b :: rest ->
      ¬ (48 <= b.val /\ b.val <= 57) := by
    intro b rest h
    cases h
    norm_num
  obtain ⟨rest1, value1, rem1, used1, hscan1, hpart1, hrest1,
      hvalue1, hremDigits1, hremLen1, hbound1, hlow1, hsharp1⟩ :=
    NumberHelperTryParse19SafeLength start 0#u64 integer
      (46#u8 :: (fraction ++ tail)) hstart hinteger hpointTail (by norm_num)
  by_cases hhigh : 1000000000000000000 <= value1.val
  · have fold_ge_start : forall (ds : List Std.U8) (a : Nat),
        a <= ds.foldl (fun x b => 10 * x + (b.val - 48)) a := by
      intro ds
      induction ds with
      | nil => intro a; rfl
      | cons b bs ih =>
        intro a
        have h := ih (10 * a + (b.val - 48))
        simpa only [List.foldl_cons] using (show
          a <= bs.foldl (fun x b => 10 * x + (b.val - 48))
            (10 * a + (b.val - 48)) by omega)
    have hfull : 1000000000000000000 <=
        integer.foldl (fun a b => 10 * a + (b.val - 48)) 0 := by
      rw [hpart1, List.foldl_append]
      have hg := fold_ge_start rem1 value1.val
      simpa [hvalue1] using (le_trans hhigh hg)
    obtain ⟨mantissa, exponent, consumed, remaining, hparse, hpart,
        hvalue, hmantissaLo, hmantissaHi, hexponent⟩ :=
      P367_T1_NumberHelperIntegralRetryA start intEnd explicit integer
        (46#u8 :: (fraction ++ tail)) hstart hend hinteger hpointTail
        hlen hfull hexpLo hexpHi
    refine ⟨mantissa, exponent, consumed, remaining ++ fraction,
      hparse, ?_, hvalue, hmantissaHi, ?_⟩
    · rw [hpart, List.append_assoc]
    · simp only [List.length_append, Nat.cast_add]
      omega
  · have hrem1nil : rem1 = [] := hlow1 (by omega)
    have hused1 : used1 = integer := by simpa [hrem1nil] using hpart1.symm
    have hrest1val : rest1.val = 46#u8 :: (fraction ++ tail) := by
      simpa [hrem1nil] using hrest1
    have hfracLen : (fraction ++ tail).length <= start.val.length := by
      rw [hstart]
      simp only [List.length_append, List.length_cons]
      omega
    let fractional : Slice Std.U8 :=
      ⟨fraction ++ tail, le_trans hfracLen start.property⟩
    have hdrop : core.slice.index.SliceIndexRangeFromUsizeSlice.index
        (T := Std.U8) { start := 1#usize } rest1 = ok fractional := by
      simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
        Slice.drop, hrest1val, fractional]
    obtain ⟨rest2, value2, rem2, used2, hscan2, hpart2, hrest2,
        hvalue2, hremDigits2, hremLen2, hbound2, hlow2, hsharp2⟩ :=
      NumberHelperTryParse19SafeLength fractional value1 fraction tail
        rfl hfraction htail hbound1
    have hrest2Len : rest2.val.length <= fractional.val.length := by
      have hp := congrArg List.length hpart2
      simp only [List.length_append] at hp
      simp only [hrest2, fractional, List.length_append]
      omega
    obtain ⟨offset, hoffset, hoffsetVal⟩ :=
      P364_T1_NumberHelperOffsetSourceLength fractional rest2
        (by dsimp [fractional]; omega) hrest2Len
    have hoffsetExact : offset.val =
        (fraction.length : Int) - (rem2.length : Int) := by
      simp only [fractional, hrest2, List.length_append, Nat.cast_add]
        at hoffsetVal
      omega
    have hnotmin : offset.val ≠ IScalar.min .Isize := by
      rw [hoffsetExact, IScalar.min]
      norm_num [IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits]
      omega
    obtain ⟨negOffset, hneg, hnegVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (HNeg.hNeg.step offset hnotmin)
    have hcastBounds : IScalar.min .I64 <= negOffset.val /\
        negOffset.val <= IScalar.max .I64 := by
      simp only [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
        IScalar.max_IScalarTy_I64_eq, I64.max_eq]
      rw [hnegVal, hoffsetExact]
      omega
    obtain ⟨base, hcast, hbase⟩ := Aeneas.Std.WP.spec_imp_exists
      (IScalar.cast_inBounds_spec .I64 negOffset hcastBounds)
    obtain ⟨exponent, hadd, hexponent⟩ :=
      P367_T1_NumberHelperExponentAddA base explicit
        (by rw [hbase, hnegVal, hoffsetExact]; omega)
        (by rw [hbase, hnegVal, hoffsetExact]; omega) hexpLo hexpHi
    refine ⟨value2, exponent, integer ++ used2, rem2, ?_, ?_, ?_,
      hbound2, ?_⟩
    · simp [num.imp.dec2flt.parse.parse_many_digits, hscan1,
        num.imp.dec2flt.parse.MIN_19DIGIT_INT, hhigh, hdrop, hscan2,
        hoffset, hneg, hcast, hadd]
    · rw [hpart2, List.append_assoc]
    · simp only [List.foldl_append]
      simpa [hused1, hvalue1] using hvalue2
    · rw [hexponent, hbase, hnegVal, hoffsetExact]
      omega

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P367_T1_NumberHelperIntegralRetryA

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P367_T1_NumberHelperFractionalRetryA]
theorem P367_T1_NumberHelperFractionalRetryA
    (start intEnd : Slice Std.U8) (explicit : Std.I64)
    (integer fraction tail : List Std.U8)
    (hstart : start.val = integer ++ 46#u8 :: (fraction ++ tail))
    (hend : intEnd.val = 46#u8 :: (fraction ++ tail))
    (hinteger : forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57)
    (hfraction : forall b, b ∈ fraction -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57))
    (hlen : start.val.length <= 3458764513820540928)
    (hexpLo : -5764607523034234879 <= explicit.val)
    (hexpHi : explicit.val <= 5764607523034234879) :
    exists (mantissa : Std.U64) (exponent : Std.I64)
      (consumed remaining : List Std.U8),
      num.imp.dec2flt.parse.parse_many_digits start intEnd explicit =
        ok (mantissa, exponent) /\
      integer ++ fraction = consumed ++ remaining /\
      mantissa.val = consumed.foldl (fun a b => 10 * a + (b.val - 48)) 0 /\
      mantissa.val <= 9999999999999999999 /\
      exponent.val = explicit.val - (fraction.length : Int) +
        (remaining.length : Int) := by
-- BODY
  have hfracBound : fraction.length <= 3458764513820540928 := by
    rw [hstart] at hlen
    simp only [List.length_append, List.length_cons] at hlen
    omega
  have hpointTail : forall b rest,
      46#u8 :: (fraction ++ tail) = b :: rest ->
      ¬ (48 <= b.val /\ b.val <= 57) := by
    intro b rest h
    cases h
    norm_num
  obtain ⟨rest1, value1, rem1, used1, hscan1, hpart1, hrest1,
      hvalue1, hremDigits1, hremLen1, hbound1, hlow1, hsharp1⟩ :=
    NumberHelperTryParse19SafeLength start 0#u64 integer
      (46#u8 :: (fraction ++ tail)) hstart hinteger hpointTail (by norm_num)
  by_cases hhigh : 1000000000000000000 <= value1.val
  · have fold_ge_start : forall (ds : List Std.U8) (a : Nat),
        a <= ds.foldl (fun x b => 10 * x + (b.val - 48)) a := by
      intro ds
      induction ds with
      | nil => intro a; rfl
      | cons b bs ih =>
        intro a
        have h := ih (10 * a + (b.val - 48))
        simpa only [List.foldl_cons] using (show
          a <= bs.foldl (fun x b => 10 * x + (b.val - 48))
            (10 * a + (b.val - 48)) by omega)
    have hfull : 1000000000000000000 <=
        integer.foldl (fun a b => 10 * a + (b.val - 48)) 0 := by
      rw [hpart1, List.foldl_append]
      have hg := fold_ge_start rem1 value1.val
      simpa [hvalue1] using (le_trans hhigh hg)
    obtain ⟨mantissa, exponent, consumed, remaining, hparse, hpart,
        hvalue, hmantissaLo, hmantissaHi, hexponent⟩ :=
      P367_T1_NumberHelperIntegralRetryA start intEnd explicit integer
        (46#u8 :: (fraction ++ tail)) hstart hend hinteger hpointTail
        hlen hfull hexpLo hexpHi
    refine ⟨mantissa, exponent, consumed, remaining ++ fraction,
      hparse, ?_, hvalue, hmantissaHi, ?_⟩
    · rw [hpart, List.append_assoc]
    · simp only [List.length_append, Nat.cast_add]
      omega
  · have hrem1nil : rem1 = [] := hlow1 (by omega)
    have hused1 : used1 = integer := by simpa [hrem1nil] using hpart1.symm
    have hrest1val : rest1.val = 46#u8 :: (fraction ++ tail) := by
      simpa [hrem1nil] using hrest1
    have hfracLen : (fraction ++ tail).length <= start.val.length := by
      rw [hstart]
      simp only [List.length_append, List.length_cons]
      omega
    let fractional : Slice Std.U8 :=
      ⟨fraction ++ tail, le_trans hfracLen start.property⟩
    have hdrop : core.slice.index.SliceIndexRangeFromUsizeSlice.index
        (T := Std.U8) { start := 1#usize } rest1 = ok fractional := by
      simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
        Slice.drop, hrest1val, fractional]
    obtain ⟨rest2, value2, rem2, used2, hscan2, hpart2, hrest2,
        hvalue2, hremDigits2, hremLen2, hbound2, hlow2, hsharp2⟩ :=
      NumberHelperTryParse19SafeLength fractional value1 fraction tail
        rfl hfraction htail hbound1
    have hrest2Len : rest2.val.length <= fractional.val.length := by
      have hp := congrArg List.length hpart2
      simp only [List.length_append] at hp
      simp only [hrest2, fractional, List.length_append]
      omega
    obtain ⟨offset, hoffset, hoffsetVal⟩ :=
      P364_T1_NumberHelperOffsetSourceLength fractional rest2
        (by dsimp [fractional]; omega) hrest2Len
    have hoffsetExact : offset.val =
        (fraction.length : Int) - (rem2.length : Int) := by
      simp only [fractional, hrest2, List.length_append, Nat.cast_add]
        at hoffsetVal
      omega
    have hnotmin : offset.val ≠ IScalar.min .Isize := by
      rw [hoffsetExact, IScalar.min]
      norm_num [IScalarTy.numBits, Dec2Flt.P340_T1_selected_target_numBits]
      omega
    obtain ⟨negOffset, hneg, hnegVal⟩ := Aeneas.Std.WP.spec_imp_exists
      (HNeg.hNeg.step offset hnotmin)
    have hcastBounds : IScalar.min .I64 <= negOffset.val /\
        negOffset.val <= IScalar.max .I64 := by
      simp only [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
        IScalar.max_IScalarTy_I64_eq, I64.max_eq]
      rw [hnegVal, hoffsetExact]
      omega
    obtain ⟨base, hcast, hbase⟩ := Aeneas.Std.WP.spec_imp_exists
      (IScalar.cast_inBounds_spec .I64 negOffset hcastBounds)
    obtain ⟨exponent, hadd, hexponent⟩ :=
      P367_T1_NumberHelperExponentAddA base explicit
        (by rw [hbase, hnegVal, hoffsetExact]; omega)
        (by rw [hbase, hnegVal, hoffsetExact]; omega) hexpLo hexpHi
    refine ⟨value2, exponent, integer ++ used2, rem2, ?_, ?_, ?_,
      hbound2, ?_⟩
    · simp [num.imp.dec2flt.parse.parse_many_digits, hscan1,
        num.imp.dec2flt.parse.MIN_19DIGIT_INT, hhigh, hdrop, hscan2,
        hoffset, hneg, hcast, hadd]
    · rw [hpart2, List.append_assoc]
    · simp only [List.foldl_append]
      simpa [hused1, hvalue1] using hvalue2
    · rw [hexponent, hbase, hnegVal, hoffsetExact]
      omega

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (50)
More program dependencies (17)
Standard-library dependencies (30)

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

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 38 entries
Technical checking records
Local closure summary
{
  "node": "P367_T1_NumberHelperFractionalRetryA",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "586023820c11f25e444cd95d62cdb2f986000c4f361d3b6ea59a58eabb3f745c",
  "active_statement_hash": "e5234d0b210573df8f18daa82af1c805f8e0cfe3f05e4e0ec36ecb96d9e87c84",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.P340_T1_selected_target_numBits",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperTryParse19SafeLength": "dd334c60b1e032a8",
    "P364_T1_NumberHelperOffsetSourceLength": "de0168dfeba3b802",
    "P367_T1_NumberHelperExponentAddA": "cd7f7391f1891168",
    "P367_T1_NumberHelperIntegralRetryA": "d66d59fc5c9acf83",
    "num_imp_dec2flt_parse_MIN_19DIGIT_INT": "de62bc3f1baba809",
    "num_imp_dec2flt_parse_parse_many_digits": "0f3e5cc6a65d812e"
  },
  "strict_definition_deps": {
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "num_imp_dec2flt_parse_MIN_19DIGIT_INT": "476edc5502ff7bfb",
    "num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
    "num_imp_dec2flt_parse_try_parse_19digits": "c9c74554a359a9c4"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:6404b5f6f988f2ee504742d55ab0830dd9d9a361979b7cc3b50763b51cbf3a18",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}