P367_T1_NumberHelperIntegralRetryA

Lean checked

Statement

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

Lemma · Normal integral retry with exact accounting

Let s,t:\texttt{Slice U8} have contents I\mathbin{++}R and R. Every member of I is an ASCII digit, and R is empty or has a nondigit head. Suppose |s|\le A=3458764513820540928 and the ordinary natural decimal fold of I is at least 10^{18}. Let an explicit exponent e:\texttt{I64} have value in [-C,C], where C=5764607523034234879. Then the actual extracted \texttt{parse\_many\_digits}(s,t,e) returns normally with mantissa m and exponent q. There are byte lists P,Q with I=P\mathbin{++}Q such that
m.\mathit{val}=\operatorname{foldl}(a,b\mapsto10a+(b.\mathit{val}-48),0,P), \quad 10^{18}\le m.\mathit{val}\le10^{19}-1, \quad q.\mathit{val}=e.\mathit{val}+|Q|.
Subtraction in the fold is natural subtraction. No normal-return premise or bound on the number of exponent digits is assumed.

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_NumberHelperExponentAddA
import Tablet.P364_T1_NumberHelperOffsetSourceLength
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.num_imp_dec2flt_parse_parse_many_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P367_T1_NumberHelperIntegralRetryA]
theorem P367_T1_NumberHelperIntegralRetryA
    (start intEnd : Slice Std.U8) (explicit : Std.I64)
    (integer tail : List Std.U8)
    (hstart : start.val = integer ++ tail) (hend : intEnd.val = tail)
    (hdigits : forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57))
    (hlen : start.val.length <= 3458764513820540928)
    (hvalue : 1000000000000000000 <=
      integer.foldl (fun a b => 10 * a + (b.val - 48)) 0)
    (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 = consumed ++ remaining /\
      mantissa.val = consumed.foldl (fun a b => 10 * a + (b.val - 48)) 0 /\
      1000000000000000000 <= mantissa.val /\
      mantissa.val <= 9999999999999999999 /\
      exponent.val = explicit.val + (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

Apply NumberHelperTryParse19SafeLength to s, initial word zero, digit list I, and tail R. Its premises are precisely the slice decomposition, digit ranges, nondigit-head condition, and 0\le10^{19}-1. It supplies a normal scan, I=P\mathbin{++}Q, residual contents Q\mathbin{++}R, the stated fold for its word m, and m.\mathit{val}\le10^{19}-1. If m.\mathit{val}<10^{18}, its low-result clause makes Q=[]; the fold equation would then identify m.\mathit{val} with the value of all of I, contradicting the value premise. Thus m.\mathit{val}\ge10^{18}.
Unfold num imp dec2flt parse parse many digits. The constant in num imp dec2flt parse MIN 19DIGIT INT is exactly 10^{18}, so the high branch is selected. The live scan residual has length |Q|+|R|\le|I|+|R|=|s|\le A, and the saved slice has length |R|. Apply NumberHelperOffsetSourceLength with the live residual as whole and the saved slice as rest: their lengths are at most 2^{63}-1, and the second is no longer than the first. It returns normally with signed offset (|Q|+|R|)-|R|=|Q|.
This offset belongs to [0,A], hence its checked signed-to-I64 cast is in bounds and exact. Apply NumberHelperExponentAddA to this cast value and e. All its base and explicit-exponent inequalities have just been proved. The final addition returns normally with value |Q|+e.\mathit{val}. Substituting these successful scan, offset, cast and addition results into the extracted monadic body gives the claimed successful pair. Thus failure and divergence have been excluded by actual successful operations, not by inversion of an assumed normal return.

Lean proof


  obtain ⟨rest, mantissa, remaining, consumed, hscan, hpartition,
      hrest, hmantissa, hremaining, hremLen, hbound, hlow, hsharp⟩ :=
    NumberHelperTryParse19SafeLength start 0#u64 integer tail
      hstart hdigits htail (by norm_num)
  have hhigh : 1000000000000000000 <= mantissa.val := by
    by_contra hnot
    have hrnil := hlow (by omega)
    have hc : consumed = integer := by simpa [hrnil] using hpartition.symm
    rw [hc] at hmantissa
    norm_num at hmantissa
    omega
  have hrestLen : rest.val.length <= start.val.length := by
    have hp := congrArg List.length hpartition
    simp only [List.length_append] at hp
    simp only [hrest, hstart, List.length_append]
    omega
  have hendLen : intEnd.val.length <= rest.val.length := by
    simp [hend, hrest]
  obtain ⟨offset, hoffset, hoffsetVal⟩ :=
    P364_T1_NumberHelperOffsetSourceLength rest intEnd
      (by omega) hendLen
  have hoffsetExact : offset.val = (remaining.length : Int) := by
    rw [hrest, hend] at hoffsetVal
    simp only [List.length_append, Nat.cast_add] at hoffsetVal
    omega
  have hcastBounds : IScalar.min .I64 <= offset.val /\
      offset.val <= IScalar.max .I64 := by
    simp only [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
      IScalar.max_IScalarTy_I64_eq, I64.max_eq]
    rw [hoffsetExact]
    omega
  obtain ⟨base, hcast, hbase⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.cast_inBounds_spec .I64 offset hcastBounds)
  obtain ⟨exponent, hadd, hexponent⟩ :=
    P367_T1_NumberHelperExponentAddA base explicit
      (by rw [hbase, hoffsetExact]; omega)
      (by rw [hbase, hoffsetExact]; omega) hexpLo hexpHi
  refine ⟨mantissa, exponent, consumed, remaining, ?_, hpartition,
    by simpa using hmantissa, hhigh, hbound, ?_⟩
  · simp [num.imp.dec2flt.parse.parse_many_digits, hscan,
      num.imp.dec2flt.parse.MIN_19DIGIT_INT, hhigh, hoffset, hcast, hadd]
  · rw [hexponent, hbase, hoffsetExact]
    omega

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P367_T1_NumberHelperExponentAddA
import Tablet.P364_T1_NumberHelperOffsetSourceLength
import Tablet.NumberHelperTryParse19SafeLength
import Tablet.num_imp_dec2flt_parse_parse_many_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P367_T1_NumberHelperIntegralRetryA]
theorem P367_T1_NumberHelperIntegralRetryA
    (start intEnd : Slice Std.U8) (explicit : Std.I64)
    (integer tail : List Std.U8)
    (hstart : start.val = integer ++ tail) (hend : intEnd.val = tail)
    (hdigits : forall b, b ∈ integer -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57))
    (hlen : start.val.length <= 3458764513820540928)
    (hvalue : 1000000000000000000 <=
      integer.foldl (fun a b => 10 * a + (b.val - 48)) 0)
    (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 = consumed ++ remaining /\
      mantissa.val = consumed.foldl (fun a b => 10 * a + (b.val - 48)) 0 /\
      1000000000000000000 <= mantissa.val /\
      mantissa.val <= 9999999999999999999 /\
      exponent.val = explicit.val + (remaining.length : Int) := by
-- BODY
  obtain ⟨rest, mantissa, remaining, consumed, hscan, hpartition,
      hrest, hmantissa, hremaining, hremLen, hbound, hlow, hsharp⟩ :=
    NumberHelperTryParse19SafeLength start 0#u64 integer tail
      hstart hdigits htail (by norm_num)
  have hhigh : 1000000000000000000 <= mantissa.val := by
    by_contra hnot
    have hrnil := hlow (by omega)
    have hc : consumed = integer := by simpa [hrnil] using hpartition.symm
    rw [hc] at hmantissa
    norm_num at hmantissa
    omega
  have hrestLen : rest.val.length <= start.val.length := by
    have hp := congrArg List.length hpartition
    simp only [List.length_append] at hp
    simp only [hrest, hstart, List.length_append]
    omega
  have hendLen : intEnd.val.length <= rest.val.length := by
    simp [hend, hrest]
  obtain ⟨offset, hoffset, hoffsetVal⟩ :=
    P364_T1_NumberHelperOffsetSourceLength rest intEnd
      (by omega) hendLen
  have hoffsetExact : offset.val = (remaining.length : Int) := by
    rw [hrest, hend] at hoffsetVal
    simp only [List.length_append, Nat.cast_add] at hoffsetVal
    omega
  have hcastBounds : IScalar.min .I64 <= offset.val /\
      offset.val <= IScalar.max .I64 := by
    simp only [IScalar.min_IScalarTy_I64_eq, I64.min_eq,
      IScalar.max_IScalarTy_I64_eq, I64.max_eq]
    rw [hoffsetExact]
    omega
  obtain ⟨base, hcast, hbase⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.cast_inBounds_spec .I64 offset hcastBounds)
  obtain ⟨exponent, hadd, hexponent⟩ :=
    P367_T1_NumberHelperExponentAddA base explicit
      (by rw [hbase, hoffsetExact]; omega)
      (by rw [hbase, hoffsetExact]; omega) hexpLo hexpHi
  refine ⟨mantissa, exponent, consumed, remaining, ?_, hpartition,
    by simpa using hmantissa, hhigh, hbound, ?_⟩
  · simp [num.imp.dec2flt.parse.parse_many_digits, hscan,
      num.imp.dec2flt.parse.MIN_19DIGIT_INT, hhigh, hoffset, hcast, hadd]
  · rw [hexponent, hbase, 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 37 entries
Technical checking records
Local closure summary
{
  "node": "P367_T1_NumberHelperIntegralRetryA",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "fe265b88197d0c4410b1aa168fa57aea1a849a87830aa0b9ab2e604cb9ac14bf",
  "active_statement_hash": "61f490e8c673138f0a59b9f0009f222683ec4c6cb24a9db7cebe833763acc932",
  "kernel_axioms": [
    "Classical.choice",
    "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",
    "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:ab1271fb1569b26935b2758458c749d312d13298a4b739fb418c47b6062ca5ef",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}