NumberHelperScientificClassificationSourceLength

Lean checked

Statement

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

Lemma · Classification of a scientific prefix

Let a byte slice be an optional exponent sign followed by a maximal decimal digit prefix and a tail whose first byte, if present, is not a digit. When the sign is absent, additionally suppose that an empty digit prefix is not caused by a leading plus or minus byte. If the digit prefix is empty, the extracted scientific parser returns normally with no exponent and exactly the sign-stripped tail. Otherwise it returns an exponent and leaves exactly the stated tail, with value in [-5764607523034234879,5764607523034234879], value in [-99999,99999] for at most five digits, and nonnegative value unless the sign is minus.

Lean statement

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

import Tablet.NumberHelperScientificPrefixSafeLength
import Tablet.ModelSliceSplitFirst
import Tablet.ModelU8IsAsciiDigit

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificClassificationSourceLength]
theorem NumberHelperScientificClassificationSourceLength (s : Slice Std.U8) (sign : Option Bool)
    (digits tail : List Std.U8)
    (hdigits : forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest ->
      Not (48 <= b.val /\ b.val <= 57))
    (hs : s.val =
      (match sign with
      | none => []
      | some negative => [if negative then 45#u8 else 43#u8]) ++
        digits ++ tail)
    (hsign : digits = [] -> sign = none -> forall b rest, tail = b :: rest ->
      b ≠ 45#u8 /\ b ≠ 43#u8) :
    (digits = [] /\ exists rest,
      num.imp.dec2flt.parse.parse_scientific s = ok (none, rest) /\ rest.val = tail) \/
      (digits != [] /\ exists (exponent : Std.I64) (rest : Slice Std.U8),
      num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
      rest.val = tail /\
      -5764607523034234879 <= exponent.val /\ exponent.val <= 5764607523034234879 /\
      (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

Split on whether the digit prefix is empty. In the nonempty case all the hypotheses are exactly those of NumberHelperScientificPrefixSafeLength, which gives normal return, the exact tail, both numerical bounds, and the sign-dependent nonnegativity.
Suppose instead that the digit prefix is empty. Unfold num imp dec2flt parse parse scientific. If the optional sign is absent, the extra hypothesis says that a present first tail byte is neither 45 nor 43, so the sign test consumes nothing. If the sign is present, the first split consumes exactly its byte, 43 for plus or 45 for minus. Thus in all cases the slice inspected for a first exponent digit is exactly the tail. An empty tail triggers the parser's empty-slice branch and returns no exponent. For a nonempty tail, maximality says its first byte is outside the interval [48,57]. By ModelU8IsAsciiDigit, the extracted ASCII-digit test is therefore false, and the parser again returns normally with no exponent. The slice constructions used after removing a sign are legitimate because each is a suffix of the original Aeneas slice and hence respects its usize-length bound.

Lean proof


  by_cases hne : digits = []
  · left
    refine ⟨hne, ?_⟩
    subst digits
    by_cases hsnone : sign = none
    · subst sign
      have hsg := hsign rfl rfl
      clear hsign
      cases ht : tail with
      | nil =>
        have hsEq : s = (⟨[], by simp⟩ : Slice Std.U8) :=
          (Slice.eq_iff _ _).2 (by simpa [ht] using hs)
        subst s
        refine ⟨⟨[], by simp⟩, ?_, by simp [ht]⟩
        simp [num.imp.dec2flt.parse.parse_scientific,
          core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst]
      | cons b bs =>
        have hb := htail b bs ht
        have hsgb := hsg b bs ht
        have hp : (b :: bs).length <= Usize.max := by
          simpa [ht, hs] using s.property
        let t : Slice Std.U8 := ⟨b :: bs, hp⟩
        have hsEq : s = t := (Slice.eq_iff _ _).2 (by simpa [t, ht] using hs)
        refine ⟨t, ?_, by simp [t, ht]⟩
        rw [hsEq]
        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, t, hb, hsgb.1, hsgb.2]
    · have hsome : exists neg, sign = some neg := by
        cases sign with
        | none => exact False.elim (hsnone rfl)
        | some neg => exact ⟨neg, rfl⟩
      obtain ⟨neg, rfl⟩ := hsome
      clear hsign
      cases ht : tail with
      | nil =>
        cases neg with
        | false =>
          let empty : Slice Std.U8 := ⟨[], by simp⟩
          have hp : [43#u8].length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            exact hp
          have hsEq : s = (⟨[43#u8], hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [ht] using hs)
          refine ⟨empty, ?_, by simp [empty, ht]⟩
          rw [hsEq]
          simp [num.imp.dec2flt.parse.parse_scientific,
            core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
            empty]
        | true =>
          let empty : Slice Std.U8 := ⟨[], by simp⟩
          have hp : [45#u8].length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            exact hp
          have hsEq : s = (⟨[45#u8], hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [ht] using hs)
          refine ⟨empty, ?_, by simp [empty, ht]⟩
          rw [hsEq]
          simp [num.imp.dec2flt.parse.parse_scientific,
            core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
            empty]
      | cons b bs =>
        have hb := htail b bs ht
        cases neg with
        | false =>
          have htp : (b :: bs).length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            omega
          let t : Slice Std.U8 := ⟨b :: bs, htp⟩
          have hp : (43#u8 :: t.val).length <= Usize.max := by
            simpa [t, ht, hs] using s.property
          have hsEq : s = (⟨43#u8 :: t.val, hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [t, ht] using hs)
          refine ⟨t, ?_, by simp [t, ht]⟩
          rw [hsEq]
          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, t, hb]
        | true =>
          have htp : (b :: bs).length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            omega
          let t : Slice Std.U8 := ⟨b :: bs, htp⟩
          have hp : (45#u8 :: t.val).length <= Usize.max := by
            simpa [t, ht, hs] using s.property
          have hsEq : s = (⟨45#u8 :: t.val, hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [t, ht] using hs)
          refine ⟨t, ?_, by simp [t, ht]⟩
          rw [hsEq]
          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, t, hb]
  · right
    refine ⟨by simpa only [bne_iff_ne], ?_⟩
    exact NumberHelperScientificPrefixSafeLength s sign digits tail
      (by simpa only [bne_iff_ne]) hdigits htail hs

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.NumberHelperScientificPrefixSafeLength
import Tablet.ModelSliceSplitFirst
import Tablet.ModelU8IsAsciiDigit

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperScientificClassificationSourceLength]
theorem NumberHelperScientificClassificationSourceLength (s : Slice Std.U8) (sign : Option Bool)
    (digits tail : List Std.U8)
    (hdigits : forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest ->
      Not (48 <= b.val /\ b.val <= 57))
    (hs : s.val =
      (match sign with
      | none => []
      | some negative => [if negative then 45#u8 else 43#u8]) ++
        digits ++ tail)
    (hsign : digits = [] -> sign = none -> forall b rest, tail = b :: rest ->
      b ≠ 45#u8 /\ b ≠ 43#u8) :
    (digits = [] /\ exists rest,
      num.imp.dec2flt.parse.parse_scientific s = ok (none, rest) /\ rest.val = tail) \/
      (digits != [] /\ exists (exponent : Std.I64) (rest : Slice Std.U8),
      num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
      rest.val = tail /\
      -5764607523034234879 <= exponent.val /\ exponent.val <= 5764607523034234879 /\
      (digits.length <= 5 -> -99999 <= exponent.val /\ exponent.val <= 99999) /\
      (sign ≠ some true -> 0 <= exponent.val)) := by
-- BODY
  by_cases hne : digits = []
  · left
    refine ⟨hne, ?_⟩
    subst digits
    by_cases hsnone : sign = none
    · subst sign
      have hsg := hsign rfl rfl
      clear hsign
      cases ht : tail with
      | nil =>
        have hsEq : s = (⟨[], by simp⟩ : Slice Std.U8) :=
          (Slice.eq_iff _ _).2 (by simpa [ht] using hs)
        subst s
        refine ⟨⟨[], by simp⟩, ?_, by simp [ht]⟩
        simp [num.imp.dec2flt.parse.parse_scientific,
          core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst]
      | cons b bs =>
        have hb := htail b bs ht
        have hsgb := hsg b bs ht
        have hp : (b :: bs).length <= Usize.max := by
          simpa [ht, hs] using s.property
        let t : Slice Std.U8 := ⟨b :: bs, hp⟩
        have hsEq : s = t := (Slice.eq_iff _ _).2 (by simpa [t, ht] using hs)
        refine ⟨t, ?_, by simp [t, ht]⟩
        rw [hsEq]
        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, t, hb, hsgb.1, hsgb.2]
    · have hsome : exists neg, sign = some neg := by
        cases sign with
        | none => exact False.elim (hsnone rfl)
        | some neg => exact ⟨neg, rfl⟩
      obtain ⟨neg, rfl⟩ := hsome
      clear hsign
      cases ht : tail with
      | nil =>
        cases neg with
        | false =>
          let empty : Slice Std.U8 := ⟨[], by simp⟩
          have hp : [43#u8].length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            exact hp
          have hsEq : s = (⟨[43#u8], hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [ht] using hs)
          refine ⟨empty, ?_, by simp [empty, ht]⟩
          rw [hsEq]
          simp [num.imp.dec2flt.parse.parse_scientific,
            core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
            empty]
        | true =>
          let empty : Slice Std.U8 := ⟨[], by simp⟩
          have hp : [45#u8].length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            exact hp
          have hsEq : s = (⟨[45#u8], hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [ht] using hs)
          refine ⟨empty, ?_, by simp [empty, ht]⟩
          rw [hsEq]
          simp [num.imp.dec2flt.parse.parse_scientific,
            core.slice.Slice.split_first, dec2flt_model.ModelSliceSplitFirst,
            empty]
      | cons b bs =>
        have hb := htail b bs ht
        cases neg with
        | false =>
          have htp : (b :: bs).length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            omega
          let t : Slice Std.U8 := ⟨b :: bs, htp⟩
          have hp : (43#u8 :: t.val).length <= Usize.max := by
            simpa [t, ht, hs] using s.property
          have hsEq : s = (⟨43#u8 :: t.val, hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [t, ht] using hs)
          refine ⟨t, ?_, by simp [t, ht]⟩
          rw [hsEq]
          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, t, hb]
        | true =>
          have htp : (b :: bs).length <= Usize.max := by
            have hp := s.property
            rw [hs] at hp
            simp [ht] at hp ⊢
            omega
          let t : Slice Std.U8 := ⟨b :: bs, htp⟩
          have hp : (45#u8 :: t.val).length <= Usize.max := by
            simpa [t, ht, hs] using s.property
          have hsEq : s = (⟨45#u8 :: t.val, hp⟩ : Slice Std.U8) :=
            (Slice.eq_iff _ _).2 (by simpa [t, ht] using hs)
          refine ⟨t, ?_, by simp [t, ht]⟩
          rw [hsEq]
          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, t, hb]
  · right
    refine ⟨by simpa only [bne_iff_ne], ?_⟩
    exact NumberHelperScientificPrefixSafeLength s sign digits tail
      (by simpa only [bne_iff_ne]) hdigits htail hs

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 32 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperScientificClassificationSourceLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "6602cfd4f3b8cfcead77aa1375ced40a0be64fe81bc3320796fa8ac819b1e5f7",
  "active_statement_hash": "d2cc72806544e59e018a4381b815f5bcf3540bde42b3d4aaae09c35ae8865d54",
  "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": {
    "ModelSliceSplitFirst": "f617ee05dd2925a9",
    "NumberHelperScientificPrefixSafeLength": "2081afb9bfe32845",
    "num_imp_dec2flt_parse_parse_scientific": "ba81300a2601d639"
  },
  "strict_definition_deps": {
    "Preamble": "956f3baee2191107",
    "num_imp_dec2flt_parse_parse_scientific": "359d30eefc709c2a"
  }
}
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:839104d1ca066b49374856ca4d948010894d09fffbb67d2be0526e495b6b05b2",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}