P495_T1_ExponentSuffix

Lean checked

Statement

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

Lemma · Actual optional exponent operand

Let x be a valid numeric lexeme and let a U8 slice u render exactly its optional exponent suffix: empty if absent, otherwise its e/E marker, optional exponent sign, and full exponent digit list. Consider the extracted decimal-sequence exponent computation: test empty and return I64 zero if empty; otherwise index the first byte, and for 101 or 69 drop it, call parse_scientific, and return the option's value or I64 zero; return I64 zero for other heads. This is the monadic expression in the paired statement. It returns normally with e satisfying e.\mathit{val}=\texttt{accumulatedExplicitExponent}(x). There is no source-length, A/B, or nonzero-significand hypothesis.

Lean statement

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

import Tablet.P9_T1_ScientificVocabularyFold
import Tablet.NumberHelperSplitExponentSuffix

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P495_T1_ExponentSuffix]
theorem P495_T1_ExponentSuffix (s : Slice Std.U8) (x : DecimalLexeme)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hs : s.val.map (fun b => b.val) = match x.exponent with
      | none => []
      | some (upper, sign, ds) =>
        (if upper then 69 else 101) ::
          (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
          ds.map (fun z => 48 + z)) :
    let explicitCall : Result Std.I64 := do
      let empty <- core.slice.Slice.is_empty s
      if empty then ok 0#i64 else
        let ch <- Slice.index_usize s 0#usize
        if ch = 101#u8 then do
          let suffix <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
            { start := 1#usize }
          let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
          ok (core.option.Option.unwrap_or o 0#i64)
        else if ch = 69#u8 then do
          let suffix <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
            { start := 1#usize }
          let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
          ok (core.option.Option.unwrap_or o 0#i64)
        else ok 0#i64
    exists e : Std.I64, explicitCall = ok e /\
      e.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x := 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

Expand validity and the exponent fold in DecimalGoalVocabularyDefault. If the exponent is absent, map equality gives an empty actual slice (mapping preserves length). The empty test returns true, and the result is I64 zero, exactly the vocabulary's absent-exponent value.
If the exponent is present, write it (u,\sigma,D). Validity gives D\ne[] and every d\in D below ten. The slice is nonempty and its first value is 69 for an uppercase marker or 101 for a lowercase marker. The checked index is in bounds; U8 value injectivity identifies the corresponding actual marker. The appropriate marker branch drops one byte in bounds. Split the resulting actual byte list after the optional sign byte. Its second part is a nonempty list D_8 with values \operatorname{map}(d\mapsto48+d,D), hence all byte values lie in [48,57]. Its first part is empty, U8 43, or U8 45 as specified by \sigma. Equality of U8 value lists implies equality of byte lists, so this is the precise slice equation required by ScientificVocabularyFold, with empty tail.
That principal supplies a normal result Some e, consumes all of D_8, and gives e.\mathit{val} equal to the signed guarded natural digit fold of \operatorname{map}(b\mapsto b.\mathit{val}-48,D_8). Natural subtraction of 48 from 48+d gives d, so this digit list is exactly D. The signed fold is precisely the vocabulary's accumulated explicit exponent, for all three exponent-sign alternatives. Unwrapping Some returns this same e, never the default. These are all possible rendered suffixes, so the other-head branch is not reached. The proof does not inspect the significand: a present exponent after an all-zero significand is still scanned completely by the same subcall.

Lean proof


  let explicitCall : Result Std.I64 := do
    let empty <- core.slice.Slice.is_empty s
    if empty then ok 0#i64 else
      let ch <- Slice.index_usize s 0#usize
      if ch = 101#u8 then do
        let suffix <- core.slice.index.Slice.index
          (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
          { start := 1#usize }
        let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
        ok (core.option.Option.unwrap_or o 0#i64)
      else if ch = 69#u8 then do
        let suffix <- core.slice.index.Slice.index
          (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
          { start := 1#usize }
        let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
        ok (core.option.Option.unwrap_or o 0#i64)
      else ok 0#i64
  change ∃ e : Std.I64, explicitCall = ok e ∧
    e.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
  cases hx : x.exponent with
  | none =>
      have hs0 : s.val.map (fun b => b.val) = [] := by simpa [hx] using hs
      cases hsv : s.val with
      | nil =>
          have hlen : s.val.length = 0 := by simp [hsv]
          have hempty : core.slice.Slice.is_empty s = ok true := by
            simp [core.slice.Slice.is_empty, hlen]
          refine ⟨0#i64, ?_, ?_⟩
          · simp [explicitCall, hempty, hsv]
          · simp [DecimalGoalVocabularyDefault, hx]
      | cons b bs => simp [hsv] at hs0
  | some ex =>
      rcases ex with ⟨upper, sign, ds⟩
      simp [DecimalGoalVocabularyDefault, hx] at hvalid
      rcases hvalid with ⟨_, _, _, hne, hd⟩
      have hmap : s.val.map (fun b => b.val) =
          (if upper then 69 else 101) ::
            ((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
              ds.map (fun z => 48 + z)) := by
        simpa [hx] using hs
      cases hsv : s.val with
      | nil => simp [hsv] at hmap
      | cons b bs =>
          obtain ⟨b', bs', hcons, hbmap, hbsmap⟩ :=
            List.map_eq_cons_iff.mp hmap
          have hbb : b = b' := by
            rw [hsv] at hcons
            injection hcons
          have hbs' : bs = bs' := by
            rw [hsv] at hcons
            injection hcons with hhead htail
          have hb : b = (if upper then 69#u8 else 101#u8) := by
            apply UScalar.eq_of_val_eq
            have hnum : (if upper then 69#u8 else 101#u8).val =
                (if upper then 69 else 101) := by cases upper <;> rfl
            rw [hbb, hnum]
            exact hbmap
          have hbsmap' : bs'.map (fun c => c.val) =
              (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
                ds.map (fun z => 48 + z) := by simpa using hbsmap
          obtain ⟨dbs, hsplit, hdbmap⟩ :=
            NumberHelperSplitExponentSuffix sign bs' ds hbsmap'
          have hdsne : dbs ≠ [] := by
            intro he
            subst dbs
            simp at hdbmap
            exact hne (by simpa using hdbmap)
          have hdbytes : ∀ c, c ∈ dbs → 48 ≤ c.val ∧ c.val ≤ 57 := by
            intro c hc
            have hmemb : c.val ∈ (ds.map (fun z => 48 + z)) := by
              rw [← hdbmap]
              exact List.mem_map.mpr ⟨c, hc, rfl⟩
            rcases List.mem_map.mp hmemb with ⟨d, hdmem, hcv⟩
            have := hd d hdmem
            omega
          have hbound : (b' :: bs').length ≤ Usize.max := by
            rw [← hcons]
            exact s.property
          let s' : Slice Std.U8 := ⟨b' :: bs', hbound⟩
          have hs' : s = s' := (Slice.eq_iff _ _).2 (by simp [s', hcons])
          subst s
          let u : Slice Std.U8 := ⟨bs', by
            have := hbound
            simp only [List.length_cons] at this
            omega⟩
          have hsplit' : bs' =
              (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ dbs := by
            cases sign <;> simp_all [hsplit]
          have huval : u.val =
              (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ dbs := by
            simpa [u] using hsplit'
          have hnonempty : core.slice.Slice.is_empty s' = ok false := by
            simp [core.slice.Slice.is_empty, s']
          have hread : Slice.index_usize s' 0#usize = ok (if upper then 69#u8 else 101#u8) := by
            have hb' : b' = (if upper then 69#u8 else 101#u8) := hbb.symm.trans hb
            simp [Slice.index_usize, s', hb']
          have hdrop : core.slice.index.Slice.index
              (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s'
              { start := 1#usize } = ok u := by
            simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop, s', u]
          obtain ⟨e, rest, hparse, hrest, hval⟩ :=
            P9_T1_ScientificVocabularyFold u sign dbs []
              (by simpa only [bne_iff_ne] using hdsne)
              hdbytes (by intro c cs heq; simp at heq)
              (by simpa using huval)
          have hdigit : (dbs.map (fun c => c.val - 48)) = ds := by
            calc
              _ = (dbs.map (fun c => c.val)).map (fun n => n - 48) := by
                simp [List.map_map, Function.comp_def]
              _ = (ds.map (fun z => 48 + z)).map (fun n => n - 48) :=
                congrArg (List.map (fun n => n - 48)) hdbmap
              _ = ds := by simp [List.map_map, Function.comp_def]
          have hexponent : e.val =
              DecimalGoalVocabularyDefault.accumulatedExplicitExponent x := by
            rw [hval, hdigit]
            cases sign with
            | none => simp [DecimalGoalVocabularyDefault, hx]
            | some neg => cases neg <;> simp [DecimalGoalVocabularyDefault, hx]
          have hresult : explicitCall = ok e := by
            unfold explicitCall
            cases upper <;>
              simp [hnonempty, hread, hdrop, hparse, hb]
          exact ⟨e, hresult, hexponent⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P9_T1_ScientificVocabularyFold
import Tablet.NumberHelperSplitExponentSuffix

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P495_T1_ExponentSuffix]
theorem P495_T1_ExponentSuffix (s : Slice Std.U8) (x : DecimalLexeme)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hs : s.val.map (fun b => b.val) = match x.exponent with
      | none => []
      | some (upper, sign, ds) =>
        (if upper then 69 else 101) ::
          (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
          ds.map (fun z => 48 + z)) :
    let explicitCall : Result Std.I64 := do
      let empty <- core.slice.Slice.is_empty s
      if empty then ok 0#i64 else
        let ch <- Slice.index_usize s 0#usize
        if ch = 101#u8 then do
          let suffix <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
            { start := 1#usize }
          let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
          ok (core.option.Option.unwrap_or o 0#i64)
        else if ch = 69#u8 then do
          let suffix <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
            { start := 1#usize }
          let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
          ok (core.option.Option.unwrap_or o 0#i64)
        else ok 0#i64
    exists e : Std.I64, explicitCall = ok e /\
      e.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x := by
-- BODY
  let explicitCall : Result Std.I64 := do
    let empty <- core.slice.Slice.is_empty s
    if empty then ok 0#i64 else
      let ch <- Slice.index_usize s 0#usize
      if ch = 101#u8 then do
        let suffix <- core.slice.index.Slice.index
          (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
          { start := 1#usize }
        let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
        ok (core.option.Option.unwrap_or o 0#i64)
      else if ch = 69#u8 then do
        let suffix <- core.slice.index.Slice.index
          (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
          { start := 1#usize }
        let (o, _) <- num.imp.dec2flt.parse.parse_scientific suffix
        ok (core.option.Option.unwrap_or o 0#i64)
      else ok 0#i64
  change ∃ e : Std.I64, explicitCall = ok e ∧
    e.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
  cases hx : x.exponent with
  | none =>
      have hs0 : s.val.map (fun b => b.val) = [] := by simpa [hx] using hs
      cases hsv : s.val with
      | nil =>
          have hlen : s.val.length = 0 := by simp [hsv]
          have hempty : core.slice.Slice.is_empty s = ok true := by
            simp [core.slice.Slice.is_empty, hlen]
          refine ⟨0#i64, ?_, ?_⟩
          · simp [explicitCall, hempty, hsv]
          · simp [DecimalGoalVocabularyDefault, hx]
      | cons b bs => simp [hsv] at hs0
  | some ex =>
      rcases ex with ⟨upper, sign, ds⟩
      simp [DecimalGoalVocabularyDefault, hx] at hvalid
      rcases hvalid with ⟨_, _, _, hne, hd⟩
      have hmap : s.val.map (fun b => b.val) =
          (if upper then 69 else 101) ::
            ((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
              ds.map (fun z => 48 + z)) := by
        simpa [hx] using hs
      cases hsv : s.val with
      | nil => simp [hsv] at hmap
      | cons b bs =>
          obtain ⟨b', bs', hcons, hbmap, hbsmap⟩ :=
            List.map_eq_cons_iff.mp hmap
          have hbb : b = b' := by
            rw [hsv] at hcons
            injection hcons
          have hbs' : bs = bs' := by
            rw [hsv] at hcons
            injection hcons with hhead htail
          have hb : b = (if upper then 69#u8 else 101#u8) := by
            apply UScalar.eq_of_val_eq
            have hnum : (if upper then 69#u8 else 101#u8).val =
                (if upper then 69 else 101) := by cases upper <;> rfl
            rw [hbb, hnum]
            exact hbmap
          have hbsmap' : bs'.map (fun c => c.val) =
              (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
                ds.map (fun z => 48 + z) := by simpa using hbsmap
          obtain ⟨dbs, hsplit, hdbmap⟩ :=
            NumberHelperSplitExponentSuffix sign bs' ds hbsmap'
          have hdsne : dbs ≠ [] := by
            intro he
            subst dbs
            simp at hdbmap
            exact hne (by simpa using hdbmap)
          have hdbytes : ∀ c, c ∈ dbs → 48 ≤ c.val ∧ c.val ≤ 57 := by
            intro c hc
            have hmemb : c.val ∈ (ds.map (fun z => 48 + z)) := by
              rw [← hdbmap]
              exact List.mem_map.mpr ⟨c, hc, rfl⟩
            rcases List.mem_map.mp hmemb with ⟨d, hdmem, hcv⟩
            have := hd d hdmem
            omega
          have hbound : (b' :: bs').length ≤ Usize.max := by
            rw [← hcons]
            exact s.property
          let s' : Slice Std.U8 := ⟨b' :: bs', hbound⟩
          have hs' : s = s' := (Slice.eq_iff _ _).2 (by simp [s', hcons])
          subst s
          let u : Slice Std.U8 := ⟨bs', by
            have := hbound
            simp only [List.length_cons] at this
            omega⟩
          have hsplit' : bs' =
              (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ dbs := by
            cases sign <;> simp_all [hsplit]
          have huval : u.val =
              (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ dbs := by
            simpa [u] using hsplit'
          have hnonempty : core.slice.Slice.is_empty s' = ok false := by
            simp [core.slice.Slice.is_empty, s']
          have hread : Slice.index_usize s' 0#usize = ok (if upper then 69#u8 else 101#u8) := by
            have hb' : b' = (if upper then 69#u8 else 101#u8) := hbb.symm.trans hb
            simp [Slice.index_usize, s', hb']
          have hdrop : core.slice.index.Slice.index
              (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s'
              { start := 1#usize } = ok u := by
            simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop, s', u]
          obtain ⟨e, rest, hparse, hrest, hval⟩ :=
            P9_T1_ScientificVocabularyFold u sign dbs []
              (by simpa only [bne_iff_ne] using hdsne)
              hdbytes (by intro c cs heq; simp at heq)
              (by simpa using huval)
          have hdigit : (dbs.map (fun c => c.val - 48)) = ds := by
            calc
              _ = (dbs.map (fun c => c.val)).map (fun n => n - 48) := by
                simp [List.map_map, Function.comp_def]
              _ = (ds.map (fun z => 48 + z)).map (fun n => n - 48) :=
                congrArg (List.map (fun n => n - 48)) hdbmap
              _ = ds := by simp [List.map_map, Function.comp_def]
          have hexponent : e.val =
              DecimalGoalVocabularyDefault.accumulatedExplicitExponent x := by
            rw [hval, hdigit]
            cases sign with
            | none => simp [DecimalGoalVocabularyDefault, hx]
            | some neg => cases neg <;> simp [DecimalGoalVocabularyDefault, hx]
          have hresult : explicitCall = ok e := by
            unfold explicitCall
            cases upper <;>
              simp [hnonempty, hread, hdrop, hparse, hb]
          exact ⟨e, hresult, hexponent⟩

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 40 entries
Technical checking records
Local closure summary
{
  "node": "P495_T1_ExponentSuffix",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "a66e14ddbde18270ea7c8171155b6c5f165d26edd4b6ab1183d5214da493b80d",
  "active_statement_hash": "907c42bbf16fbe429c2d2f2cc9f4d66cfa6cad1b84bf8e25c4fcf0e845f345e9",
  "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": {
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
    "NumberHelperSplitExponentSuffix": "36f92cf2e9d9f766",
    "P9_T1_ScientificVocabularyFold": "b4e9d9d3618cb9a8"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "b9386c8d0fd53e6e",
    "DecimalLexeme": "303e59678842cea4",
    "F64": "ec4e25fbaf4e11d4",
    "NumberHelperSplitExponentSuffix": "3a789d080d66f335",
    "P9_T1_ScientificVocabularyFold": "28ca0a8ac503ee01",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_decimal_Decimal": "63fd2f021dd0ef41",
    "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:083ab7b1947b668b972c2c43b61a7011a569a5e89d9c16d197a5a2731245df88",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}