NumberHelperScientificRenderedSuffixSafeLength

Lean checked

Statement

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

Lemma · Scientific parsing from rendered digit values

Let an Aeneas byte slice of length at most (9223372036854120458) have byte values equal to the standard rendering of an optional exponent sign followed by a nonempty list of decimal digits. If every digit is below ten, then parse_scientific consumes the whole slice and returns an exponent in ([-655359,655359]). When at most five digits are rendered, the returned exponent lies in the sharper interval ([-99999,99999]). Unless the optional sign is a minus sign, the exponent is nonnegative.

Lean statement

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

import Tablet.NumberHelperScientificSuffixSharpSafeLength
import Tablet.NumberHelperScientificSignedSuffixSafeLength
import Tablet.NumberHelperSplitExponentSuffix

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificRenderedSuffixSafeLength]
theorem NumberHelperScientificRenderedSuffixSafeLength (s : Slice Std.U8)
    (sign : Option Bool) (digits : List Nat)
    (hne : digits != [])
    (hdigits : forall d, List.Mem d digits -> d < 10)
    (hs : List.map (fun b : Std.U8 => b.val) s.val =
      (match sign with
      | none => []
      | some negative => [if negative then 45 else 43]) ++
        List.map (fun d : Nat => 48 + d) digits)
    (hlen : s.val.length <= 9223372036854120458) :
    exists (exponent : Std.I64) (rest : Slice Std.U8),
      num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
      rest.val = [] /\
      -655359 <= exponent.val /\ exponent.val <= 655359 /\
      (digits.length <= 5 ->
        -99999 <= exponent.val /\ exponent.val <= 99999) /\
      (sign ≠ some true -> 0 <= exponent.val) := 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 NumberHelperSplitExponentSuffix to the assumed byte-value equality. It produces the exact optional ASCII sign prefix and a byte list whose values are the rendered digit values. Every such byte has value (48+d) for a stated digit (d<10), so it lies from 48 through 57. The digit-byte list is nonempty because equality of its mapped length with the nonempty source digit list rules out the empty list; the same length equality shows that the two digit counts coincide.
The exact byte decomposition, decimal-byte property, nonemptiness, and slice length bound meet all hypotheses of NumberHelperScientificSuffixSharpSafeLength. That lemma gives complete consumption and the coarse exponent interval. Its short-list conclusion applies exactly when the original digit list has length at most five, by the established equality of lengths, and gives the stated sharper interval. Applying NumberHelperScientificSignedSuffixSafeLength to the same exact byte decomposition gives nonnegativity in the no-sign and plus-sign cases. Both calls describe the result of the same deterministic parser invocation, so their returned exponents coincide, establishing the final conditional claim.

Lean proof


  obtain ⟨digitBytes, hsval, hmap⟩ :=
    NumberHelperSplitExponentSuffix sign s.val digits hs
  have hbyteDigits : forall b, List.Mem b digitBytes ->
      48 <= b.val /\ b.val <= 57 := by
    intro b hb
    have hbmap : b.val ∈ List.map (fun c : Std.U8 => c.val) digitBytes :=
      List.mem_map_of_mem hb
    rw [hmap] at hbmap
    obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap
    have hdlt := hdigits d hd
    omega
  have hbytesNe : digitBytes != [] := by
    simp only [bne_iff_ne]
    intro hnil
    subst digitBytes
    cases digits with
    | nil => simp at hne
    | cons d ds => simp at hmap
  have hlength : digitBytes.length = digits.length := by
    have := congrArg List.length hmap
    simpa using this
  have finish : forall (sgn : Option Bool),
      s.val =
        (match sgn with
        | none => []
        | some negative => [if negative then 45#u8 else 43#u8]) ++
          digitBytes ->
      exists (exponent : Std.I64) (rest : Slice Std.U8),
        num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
        rest.val = [] /\
        -655359 <= exponent.val /\ exponent.val <= 655359 /\
        (digits.length <= 5 ->
          -99999 <= exponent.val /\ exponent.val <= 99999) /\
        (sgn ≠ some true -> 0 <= exponent.val) := by
    intro sgn hval
    obtain ⟨exponent, rest, hparse, hrest, hlo, hhi, hshort⟩ :=
      NumberHelperScientificSuffixSharpSafeLength s sgn digitBytes hbytesNe
        hbyteDigits hval hlen
    obtain ⟨signedExponent, signedRest, hsigned, _, _, _, hnonnegative⟩ :=
      NumberHelperScientificSignedSuffixSafeLength s sgn digitBytes hbytesNe
        hbyteDigits hval hlen
    rw [hparse] at hsigned
    have he : signedExponent = exponent := by
      injection hsigned with hp
      injection hp with ho _
      injection ho with hx
      exact hx.symm
    subst signedExponent
    exact ⟨exponent, rest, hparse, hrest, hlo, hhi,
      fun h => hshort (by omega), hnonnegative⟩
  cases sign with
  | none =>
    apply finish none
    simpa using hsval
  | some negative =>
    cases negative
    · apply finish (some false)
      simpa using hsval
    · apply finish (some true)
      simpa using hsval

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.NumberHelperScientificSuffixSharpSafeLength
import Tablet.NumberHelperScientificSignedSuffixSafeLength
import Tablet.NumberHelperSplitExponentSuffix

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificRenderedSuffixSafeLength]
theorem NumberHelperScientificRenderedSuffixSafeLength (s : Slice Std.U8)
    (sign : Option Bool) (digits : List Nat)
    (hne : digits != [])
    (hdigits : forall d, List.Mem d digits -> d < 10)
    (hs : List.map (fun b : Std.U8 => b.val) s.val =
      (match sign with
      | none => []
      | some negative => [if negative then 45 else 43]) ++
        List.map (fun d : Nat => 48 + d) digits)
    (hlen : s.val.length <= 9223372036854120458) :
    exists (exponent : Std.I64) (rest : Slice Std.U8),
      num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
      rest.val = [] /\
      -655359 <= exponent.val /\ exponent.val <= 655359 /\
      (digits.length <= 5 ->
        -99999 <= exponent.val /\ exponent.val <= 99999) /\
      (sign ≠ some true -> 0 <= exponent.val) := by
-- BODY
  obtain ⟨digitBytes, hsval, hmap⟩ :=
    NumberHelperSplitExponentSuffix sign s.val digits hs
  have hbyteDigits : forall b, List.Mem b digitBytes ->
      48 <= b.val /\ b.val <= 57 := by
    intro b hb
    have hbmap : b.val ∈ List.map (fun c : Std.U8 => c.val) digitBytes :=
      List.mem_map_of_mem hb
    rw [hmap] at hbmap
    obtain ⟨d, hd, hdval⟩ := List.mem_map.mp hbmap
    have hdlt := hdigits d hd
    omega
  have hbytesNe : digitBytes != [] := by
    simp only [bne_iff_ne]
    intro hnil
    subst digitBytes
    cases digits with
    | nil => simp at hne
    | cons d ds => simp at hmap
  have hlength : digitBytes.length = digits.length := by
    have := congrArg List.length hmap
    simpa using this
  have finish : forall (sgn : Option Bool),
      s.val =
        (match sgn with
        | none => []
        | some negative => [if negative then 45#u8 else 43#u8]) ++
          digitBytes ->
      exists (exponent : Std.I64) (rest : Slice Std.U8),
        num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
        rest.val = [] /\
        -655359 <= exponent.val /\ exponent.val <= 655359 /\
        (digits.length <= 5 ->
          -99999 <= exponent.val /\ exponent.val <= 99999) /\
        (sgn ≠ some true -> 0 <= exponent.val) := by
    intro sgn hval
    obtain ⟨exponent, rest, hparse, hrest, hlo, hhi, hshort⟩ :=
      NumberHelperScientificSuffixSharpSafeLength s sgn digitBytes hbytesNe
        hbyteDigits hval hlen
    obtain ⟨signedExponent, signedRest, hsigned, _, _, _, hnonnegative⟩ :=
      NumberHelperScientificSignedSuffixSafeLength s sgn digitBytes hbytesNe
        hbyteDigits hval hlen
    rw [hparse] at hsigned
    have he : signedExponent = exponent := by
      injection hsigned with hp
      injection hp with ho _
      injection ho with hx
      exact hx.symm
    subst signedExponent
    exact ⟨exponent, rest, hparse, hrest, hlo, hhi,
      fun h => hshort (by omega), hnonnegative⟩
  cases sign with
  | none =>
    apply finish none
    simpa using hsval
  | some negative =>
    cases negative
    · apply finish (some false)
      simpa using hsval
    · apply finish (some true)
      simpa using hsval

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (47)
More program dependencies (14)
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 36 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperScientificRenderedSuffixSafeLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "85079fe14d360a995c07b27f556811bb53c8b46737ab0744d68a071aff034f3b",
  "active_statement_hash": "a1b4fcc68039c92efedf6f60f38cc587939a778a71522620045a797cb40b8e4b",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-110",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperScientificSignedSuffixSafeLength": "59e1bedc9eb157ae",
    "NumberHelperScientificSuffixSharpSafeLength": "70ed9b39787701a0",
    "NumberHelperSplitExponentSuffix": "36f92cf2e9d9f766"
  },
  "strict_definition_deps": {
    "NumberHelperSplitExponentSuffix": "28ca0a8ac503ee01",
    "num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5"
  }
}
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:58dc6d410ff6f85241b0df146a000c5172b1d9c3c06c9b4449cb1e9cbac1824e",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}