NumberHelperScientificSuffixSafeLength

Lean checked

Statement

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

Lemma · Safe complete scientific-exponent parsing

Let a byte slice of length at most 9223372036854120458 consist of an optional ASCII plus or minus followed by a nonempty list of ASCII decimal digits. The extracted parse_scientific returns normally with Some exponent, consumes the complete slice, and the returned signed exponent lies between -655359 and 655359.

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.ModelSliceSplitFirst
import Tablet.ModelU8IsAsciiDigit
import Tablet.num_imp_dec2flt_parse_parse_scientific

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificSuffixSafeLength]
theorem NumberHelperScientificSuffixSafeLength :
    forall (s : Slice Std.U8) (sign : Option Bool) (digits : List Std.U8),
      digits != [] ->
      (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
      s.val =
        (match sign with
        | none => []
        | some negative => [if negative then 45#u8 else 43#u8]) ++ digits ->
      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 := 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

Let q\geq1 be the number of digit bytes. The rendering hypothesis gives exactly three cases for the first split in num imp dec2flt parse parse scientific: no sign, a plus, or a minus. By ModelSliceSplitFirst, the guarded split_first either retains the whole slice or removes its unique sign byte. In every case the resulting slice consists of exactly the q digits and is nonempty. Its index at zero is therefore in bounds, and ModelU8IsAsciiDigit makes its first-byte ASCII test true.
Induct over the scientific loop with visited count j, where 0\leq j\leq q, and accumulator a satisfying 0\leq a\leq655359. If j<q, the length guard justifies reading digit j. Wrapping subtraction by 48 gives its value d\in[0,9]. When a<65536, the checked operations compute 10a+d\leq10\cdot65535+9=655359<2^{63}; when a\geq65536, the guarded branch leaves it unchanged. Thus the accumulator invariant is preserved. The checked count increment is at most q, which is at most the whole input length. By Assumptions and the stated bound, this is strictly below the 64-bit usize limit. Hence all q iterations succeed, including those after the accumulator stops changing.
At count q the loop's length guard is false. Slicing the digit-only suffix at that in-bounds count returns the empty slice. Without a minus the nonnegative accumulator is already the returned exponent. With a minus, checked negation is safe because 0\leq a\leq655359<2^{63} and returns -a. Consequently the function returns Some, consumes the whole input, and its result lies in the claimed closed interval.

Lean proof


  intro s sign digits hne hdigits hs hlen
  have loop_full : forall (t : Slice Std.U8) (a : Std.I64) (k : Std.Usize),
      k.val <= t.length ->
      0 <= a.val -> a.val <= 655359 ->
      (forall b, List.Mem b t.val -> 48 <= b.val /\ b.val <= 57) ->
      num.imp.dec2flt.parse.parse_scientific_loop a t k
        ⦃ p => p.2.val = t.length /\ 0 <= p.1.val /\ p.1.val <= 655359 ⦄ := by
    intro t a k hk ha hb htdigits
    unfold num.imp.dec2flt.parse.parse_scientific_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.I64 × Std.Usize => t.length - p.2.val)
      (fun p => p.2.val <= t.length /\ 0 <= p.1.val /\ p.1.val <= 655359)
      (fun p => p.2.val = t.length /\ 0 <= p.1.val /\ p.1.val <= 655359)
      (fun p => num.imp.dec2flt.parse.parse_scientific_loop.body t p.1 p.2)
      (a, k)
    · intro p hp
      simp [num.imp.dec2flt.parse.parse_scientific_loop.body, WP.spec_ok]
      split
      · step
        have hi_mem : List.Mem i1 t.val := by
          rw [i1_post]
          exact List.getElem_mem _
        have hi_digit := htdigits i1 hi_mem
        step
        split
        · step
          split
          · step
            step
            have hd : digit.val < 10 := by
              calc
                digit.val = i2.val := by simp [digit_post, i2_post]
                _ < 10 := by assumption
            have hc : (UScalar.hcast IScalarTy.I64 digit).val = digit.val := by
              have q := UScalar.hcast_inBounds_spec IScalarTy.I64 digit (by grind)
              simpa [WP.spec_ok] using q
            have hx : x.val = digit.val := by rw [x_post, hc]
            have hmax : exponent1.val + x.val <= I64.max := by
              rw [I64.max_eq]
              omega
            step with IScalar.add_spec
            step
            omega
          · step
            simp_all <;> grind
        · have hi2 : i2.val = i1.val - 48 := by
            rw [i2_post]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            omega
          simp [WP.spec_ok, Slice.length]
          omega
      · simp [WP.spec_ok, Slice.length] at *
        omega
    · exact ⟨hk, ha, hb⟩
  cases sign with
  | none =>
    simp only [List.nil_append] at hs
    cases hval : s.val with
    | nil =>
      simp_all
    | cons c xs =>
      have ht : s = (⟨c :: xs, by simpa [hval] using s.property⟩ : Slice Std.U8) := by
        exact (Slice.eq_iff _ _).2 hval
      have hc_mem : List.Mem c digits := by
        rw [← hs, hval]
        exact List.mem_cons_self
      have hc := hdigits c hc_mem
      have hc45 : c ≠ 45#u8 := by scalar_tac
      have hc43 : c ≠ 43#u8 := by scalar_tac
      have hall : forall b, List.Mem b (c :: xs) -> 48 <= b.val /\ b.val <= 57 := by
        intro b hb
        apply hdigits b
        rw [← hs, hval]
        exact hb
      have lp := loop_full
        (⟨c :: xs, by simpa [hval] using s.property⟩ : Slice Std.U8)
        0#i64 0#usize (by simp) (by norm_num) (by norm_num) hall
      rw [ht]
      simp [num.imp.dec2flt.parse.parse_scientific,
        core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
        hc45, hc43, core.num.U8.is_ascii_digit,
        dec2flt_model.ModelU8IsAsciiDigit, Slice.index_usize]
      simp only [if_pos hc]
      obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp
      rcases p with ⟨e, count⟩
      simp only [Prod.fst, Prod.snd] at hp
      rw [hloop]
      simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
        Slice.drop, hp]
      omega
  | some negative =>
    cases negative with
    | false =>
      simp at hs
      cases hd : digits with
      | nil => simp_all
      | cons c xs =>
        have hsval : s.val = 43#u8 :: c :: xs := by simpa [hd] using hs
        have ht : s = (⟨43#u8 :: c :: xs, by simpa [hsval] using s.property⟩ : Slice Std.U8) := by
          exact (Slice.eq_iff _ _).2 hsval
        have htailprop : (c :: xs).length <= Usize.max := by
          have p := s.property
          rw [hsval] at p
          simp only [List.length_cons] at p ⊢
          omega
        have hc_mem : List.Mem c digits := by rw [hd]; exact List.mem_cons_self
        have hc := hdigits c hc_mem
        have hall : forall b, List.Mem b (c :: xs) -> 48 <= b.val /\ b.val <= 57 := by
          intro b hb
          apply hdigits b
          rw [hd]
          exact hb
        have lp := loop_full (⟨c :: xs, htailprop⟩ : Slice Std.U8)
          0#i64 0#usize (by simp) (by norm_num) (by norm_num) hall
        rw [ht]
        simp [num.imp.dec2flt.parse.parse_scientific,
          core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit,
          Slice.index_usize]
        simp only [if_pos hc]
        obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp
        rcases p with ⟨e, count⟩
        simp only [Prod.fst, Prod.snd] at hp
        rw [hloop]
        simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, hp]
        omega
    | true =>
      simp at hs
      cases hd : digits with
      | nil => simp_all
      | cons c xs =>
        have hsval : s.val = 45#u8 :: c :: xs := by simpa [hd] using hs
        have ht : s = (⟨45#u8 :: c :: xs, by simpa [hsval] using s.property⟩ : Slice Std.U8) := by
          exact (Slice.eq_iff _ _).2 hsval
        have htailprop : (c :: xs).length <= Usize.max := by
          have p := s.property
          rw [hsval] at p
          simp only [List.length_cons] at p ⊢
          omega
        have hc_mem : List.Mem c digits := by rw [hd]; exact List.mem_cons_self
        have hc := hdigits c hc_mem
        have hall : forall b, List.Mem b (c :: xs) -> 48 <= b.val /\ b.val <= 57 := by
          intro b hb
          apply hdigits b
          rw [hd]
          exact hb
        have lp := loop_full (⟨c :: xs, htailprop⟩ : Slice Std.U8)
          0#i64 0#usize (by simp) (by norm_num) (by norm_num) hall
        rw [ht]
        simp [num.imp.dec2flt.parse.parse_scientific,
          core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit,
          Slice.index_usize]
        simp only [if_pos hc]
        obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp
        rcases p with ⟨e, count⟩
        simp only [Prod.fst, Prod.snd] at hp
        have hnotmin : e.val ≠ IScalar.min IScalarTy.I64 := by
          rw [IScalar.min]
          norm_num [IScalarTy.numBits]
          omega
        have hn := HNeg.hNeg.step e hnotmin
        obtain ⟨ne, hneg, hne⟩ := Aeneas.Std.WP.spec_imp_exists hn
        rw [hloop]
        simp [hneg, core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, hp]
        omega

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Assumptions
import Tablet.ModelSliceSplitFirst
import Tablet.ModelU8IsAsciiDigit
import Tablet.num_imp_dec2flt_parse_parse_scientific

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificSuffixSafeLength]
theorem NumberHelperScientificSuffixSafeLength :
    forall (s : Slice Std.U8) (sign : Option Bool) (digits : List Std.U8),
      digits != [] ->
      (forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57) ->
      s.val =
        (match sign with
        | none => []
        | some negative => [if negative then 45#u8 else 43#u8]) ++ digits ->
      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 := by
-- BODY
  intro s sign digits hne hdigits hs hlen
  have loop_full : forall (t : Slice Std.U8) (a : Std.I64) (k : Std.Usize),
      k.val <= t.length ->
      0 <= a.val -> a.val <= 655359 ->
      (forall b, List.Mem b t.val -> 48 <= b.val /\ b.val <= 57) ->
      num.imp.dec2flt.parse.parse_scientific_loop a t k
        ⦃ p => p.2.val = t.length /\ 0 <= p.1.val /\ p.1.val <= 655359 ⦄ := by
    intro t a k hk ha hb htdigits
    unfold num.imp.dec2flt.parse.parse_scientific_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.I64 × Std.Usize => t.length - p.2.val)
      (fun p => p.2.val <= t.length /\ 0 <= p.1.val /\ p.1.val <= 655359)
      (fun p => p.2.val = t.length /\ 0 <= p.1.val /\ p.1.val <= 655359)
      (fun p => num.imp.dec2flt.parse.parse_scientific_loop.body t p.1 p.2)
      (a, k)
    · intro p hp
      simp [num.imp.dec2flt.parse.parse_scientific_loop.body, WP.spec_ok]
      split
      · step
        have hi_mem : List.Mem i1 t.val := by
          rw [i1_post]
          exact List.getElem_mem _
        have hi_digit := htdigits i1 hi_mem
        step
        split
        · step
          split
          · step
            step
            have hd : digit.val < 10 := by
              calc
                digit.val = i2.val := by simp [digit_post, i2_post]
                _ < 10 := by assumption
            have hc : (UScalar.hcast IScalarTy.I64 digit).val = digit.val := by
              have q := UScalar.hcast_inBounds_spec IScalarTy.I64 digit (by grind)
              simpa [WP.spec_ok] using q
            have hx : x.val = digit.val := by rw [x_post, hc]
            have hmax : exponent1.val + x.val <= I64.max := by
              rw [I64.max_eq]
              omega
            step with IScalar.add_spec
            step
            omega
          · step
            simp_all <;> grind
        · have hi2 : i2.val = i1.val - 48 := by
            rw [i2_post]
            rw [core.num.U8.wrapping_sub_val_eq]
            simp only [UScalar.size_UScalarTyU8, U8.size_eq]
            norm_num
            omega
          simp [WP.spec_ok, Slice.length]
          omega
      · simp [WP.spec_ok, Slice.length] at *
        omega
    · exact ⟨hk, ha, hb⟩
  cases sign with
  | none =>
    simp only [List.nil_append] at hs
    cases hval : s.val with
    | nil =>
      simp_all
    | cons c xs =>
      have ht : s = (⟨c :: xs, by simpa [hval] using s.property⟩ : Slice Std.U8) := by
        exact (Slice.eq_iff _ _).2 hval
      have hc_mem : List.Mem c digits := by
        rw [← hs, hval]
        exact List.mem_cons_self
      have hc := hdigits c hc_mem
      have hc45 : c ≠ 45#u8 := by scalar_tac
      have hc43 : c ≠ 43#u8 := by scalar_tac
      have hall : forall b, List.Mem b (c :: xs) -> 48 <= b.val /\ b.val <= 57 := by
        intro b hb
        apply hdigits b
        rw [← hs, hval]
        exact hb
      have lp := loop_full
        (⟨c :: xs, by simpa [hval] using s.property⟩ : Slice Std.U8)
        0#i64 0#usize (by simp) (by norm_num) (by norm_num) hall
      rw [ht]
      simp [num.imp.dec2flt.parse.parse_scientific,
        core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
        hc45, hc43, core.num.U8.is_ascii_digit,
        dec2flt_model.ModelU8IsAsciiDigit, Slice.index_usize]
      simp only [if_pos hc]
      obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp
      rcases p with ⟨e, count⟩
      simp only [Prod.fst, Prod.snd] at hp
      rw [hloop]
      simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
        Slice.drop, hp]
      omega
  | some negative =>
    cases negative with
    | false =>
      simp at hs
      cases hd : digits with
      | nil => simp_all
      | cons c xs =>
        have hsval : s.val = 43#u8 :: c :: xs := by simpa [hd] using hs
        have ht : s = (⟨43#u8 :: c :: xs, by simpa [hsval] using s.property⟩ : Slice Std.U8) := by
          exact (Slice.eq_iff _ _).2 hsval
        have htailprop : (c :: xs).length <= Usize.max := by
          have p := s.property
          rw [hsval] at p
          simp only [List.length_cons] at p ⊢
          omega
        have hc_mem : List.Mem c digits := by rw [hd]; exact List.mem_cons_self
        have hc := hdigits c hc_mem
        have hall : forall b, List.Mem b (c :: xs) -> 48 <= b.val /\ b.val <= 57 := by
          intro b hb
          apply hdigits b
          rw [hd]
          exact hb
        have lp := loop_full (⟨c :: xs, htailprop⟩ : Slice Std.U8)
          0#i64 0#usize (by simp) (by norm_num) (by norm_num) hall
        rw [ht]
        simp [num.imp.dec2flt.parse.parse_scientific,
          core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit,
          Slice.index_usize]
        simp only [if_pos hc]
        obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp
        rcases p with ⟨e, count⟩
        simp only [Prod.fst, Prod.snd] at hp
        rw [hloop]
        simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, hp]
        omega
    | true =>
      simp at hs
      cases hd : digits with
      | nil => simp_all
      | cons c xs =>
        have hsval : s.val = 45#u8 :: c :: xs := by simpa [hd] using hs
        have ht : s = (⟨45#u8 :: c :: xs, by simpa [hsval] using s.property⟩ : Slice Std.U8) := by
          exact (Slice.eq_iff _ _).2 hsval
        have htailprop : (c :: xs).length <= Usize.max := by
          have p := s.property
          rw [hsval] at p
          simp only [List.length_cons] at p ⊢
          omega
        have hc_mem : List.Mem c digits := by rw [hd]; exact List.mem_cons_self
        have hc := hdigits c hc_mem
        have hall : forall b, List.Mem b (c :: xs) -> 48 <= b.val /\ b.val <= 57 := by
          intro b hb
          apply hdigits b
          rw [hd]
          exact hb
        have lp := loop_full (⟨c :: xs, htailprop⟩ : Slice Std.U8)
          0#i64 0#usize (by simp) (by norm_num) (by norm_num) hall
        rw [ht]
        simp [num.imp.dec2flt.parse.parse_scientific,
          core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
          core.num.U8.is_ascii_digit, dec2flt_model.ModelU8IsAsciiDigit,
          Slice.index_usize]
        simp only [if_pos hc]
        obtain ⟨p, hloop, hp⟩ := Aeneas.Std.WP.spec_imp_exists lp
        rcases p with ⟨e, count⟩
        simp only [Prod.fst, Prod.snd] at hp
        have hnotmin : e.val ≠ IScalar.min IScalarTy.I64 := by
          rw [IScalar.min]
          norm_num [IScalarTy.numBits]
          omega
        have hn := HNeg.hNeg.step e hnotmin
        obtain ⟨ne, hneg, hne⟩ := Aeneas.Std.WP.spec_imp_exists hn
        rw [hloop]
        simp [hneg, core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, hp]
        omega

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 31 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperScientificSuffixSafeLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "64b1bbb648916ac1081f081fb14443dacc609e8ca586f835d4957e661c65c4f3",
  "active_statement_hash": "20015e73fce113469616c6ff886beb0f8cc1c708062d8ed5f1ddd7e03d5e759a",
  "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-107",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ModelSliceSplitFirst": "f617ee05dd2925a9",
    "num_imp_dec2flt_parse_parse_scientific": "ba81300a2601d639"
  },
  "strict_definition_deps": {
    "ModelSliceSplitFirst": "4ed9c51a5bb9fb58",
    "ModelU8IsAsciiDigit": "67d39fc6c37cfc41",
    "Preamble": "956f3baee2191107",
    "num_imp_dec2flt_parse_parse_scientific": "76b01b9377828a5f"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:bb7d5a47951dd9cbd25e1843c9968b979324984343463183e9b50c7c4d7b7094",
  "current_fingerprint": "sound-v2:bb7d5a47951dd9cbd25e1843c9968b979324984343463183e9b50c7c4d7b7094",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}