NumberHelperScientificSignedSuffixSafeLength

Lean checked

Statement

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

Lemma · Sign-sensitive scientific-suffix bound

Let a byte slice of length at most (9223372036854120458) consist of an optional ASCII plus or minus sign followed by a nonempty list of ASCII decimal digits. The extracted parse_scientific call consumes the complete slice and returns an exponent in ([-655359,655359]). Unless the optional sign is a minus sign, the returned 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.Assumptions
import Tablet.ModelSliceSplitFirst
import Tablet.ModelU8IsAsciiDigit
import Tablet.NumberHelperScientificSuffixSafeLength
import Tablet.num_imp_dec2flt_parse_parse_scientific

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificSignedSuffixSafeLength]
theorem NumberHelperScientificSignedSuffixSafeLength :
    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 /\
        (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

Follow the digit-loop argument of NumberHelperScientificSuffixSafeLength. More explicitly, unfold the extracted loop in num imp dec2flt parse parse scientific and maintain that the accumulator is between zero and (655359) while the consumed count advances through the exact decimal-byte list. Each accepted digit is between zero and nine after subtraction of 48. Below the extracted threshold, checked multiplication and addition produce at most (655359); at or above the threshold the guarded branch leaves the accumulator unchanged. The stated length bound keeps every count increment and final slice operation in range. Thus the loop consumes every digit, leaves the empty suffix, and produces a nonnegative accumulator at most (655359).
With no sign or with an ASCII plus, parse_scientific returns this accumulator itself, proving the additional nonnegativity assertion. With an ASCII minus, checked negation is safe on the established interval and returns its negative, which is at least (-655359); in this case the conditional nonnegativity assertion has a false antecedent. The three exact-rendering cases use the exact first-byte split from ModelSliceSplitFirst; the decimal test is justified by ModelU8IsAsciiDigit. They establish complete consumption, both coarse bounds, and the stated sign-sensitive conclusion.

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

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificSignedSuffixSafeLength]
theorem NumberHelperScientificSignedSuffixSafeLength :
    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 /\
        (sign ≠ some true -> 0 <= exponent.val) := 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": "NumberHelperScientificSignedSuffixSafeLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "b7ed88b5767a4157c7c06cf976a0b7c79f58fbf89cc57469245b8cf5b6cd1dda",
  "active_statement_hash": "fc9d1f3e7abc27748162cbee537baa68f42bc811579dd1ea966fdab0e3648ee6",
  "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-109",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ModelSliceSplitFirst": "f617ee05dd2925a9",
    "num_imp_dec2flt_parse_parse_scientific": "ba81300a2601d639"
  },
  "strict_definition_deps": {
    "Preamble": "956f3baee2191107",
    "num_imp_dec2flt_parse_parse_scientific": "aec5af3e7eb6fca1"
  }
}
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:ee2c9dad88019ac2ae15a82c1b5863d13e9fe5ea37fd947437449290ef661efb",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}