P789_T1_RenderedExponentContinuation

Lean checked

Statement

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

Lemma · Exact exponent continuation at the significand end

Let x be valid numeric in DecimalGoalVocabularyDefault, let J be a list of bytes in [48,57] whose value map is the digit encoding of x's exponent digits (empty when the exponent is absent), and let t:\texttt{Slice U8} have exactly the exponent suffix of x: empty if absent, otherwise its 69 or 101 marker followed by the optional 45 or 43 sign and J. There is an I64 word e with value exactly \texttt{accumulatedExplicitExponent}(x) and -5764607523034234879\le e.\mathit{val}\le5764607523034234879. Furthermore either t.\mathit{val}=[] and e=0, or there are a byte c and slices u,r such that split-first of t returns normally \texttt{Some}(c,u), c=101 or c=69, and the actual scientific parser on u returns normally (\texttt{Some}(e),r) with r.\mathit{val}=[].

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.NumberHelperScientificRenderedSuffixSafeLength
import Tablet.P435_T1_DigitDecoding
import Tablet.ModelSliceSplitFirst

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

-- [TABLET NODE: P789_T1_RenderedExponentContinuation]
theorem P789_T1_RenderedExponentContinuation
    (x : DecimalLexeme) (sigEnd : Slice Std.U8) (digits : List Std.U8)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hmap : digits.map (fun b => b.val) =
      (match x.exponent with | none => [] | some (_, _, ds) => ds).map (fun d => 48 + d))
    (hdigits : forall b, b ∈ digits -> 48 <= b.val /\ b.val <= 57)
    (hend : sigEnd.val = match x.exponent with
      | none => []
      | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) ::
        (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits) :
    exists explicit : Std.I64,
      explicit.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x /\
      -5764607523034234879 <= explicit.val /\ explicit.val <= 5764607523034234879 /\
      ((sigEnd.val = [] /\ explicit = 0#i64) \/
        exists (marker : Std.U8) (next rest : Slice Std.U8),
          core.slice.Slice.split_first sigEnd = ok (some (marker, next)) /\
          (marker = 101#u8 \/ marker = 69#u8) /\
          num.imp.dec2flt.parse.parse_scientific next = ok (some explicit, rest) /\
          rest.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

If the exponent is absent, take e=0 in I64. Its value and bounds are immediate, the vocabulary's accumulated exponent is zero, and the assumed suffix equation supplies the first alternative.
Otherwise write the exponent as (\mathit{upper},\mathit{sign},D). Validity in DecimalGoalVocabularyDefault gives D\ne[] and d<10 for all d\in D. The value-map equation and DigitDecoding give J.\texttt{map}(b\mapsto b.\mathit{val}-48)=D and |J|=|D|; in particular J\ne[]. The assumed equation for t has head c=69 or 101 and remainder U=\texttt{signBytes}(\mathit{sign})++J. The remainder has length strictly smaller than t, so it satisfies the carrier's Usize length bound. Form its slice u. ModelSliceSplitFirst gives exactly the normal split with head c and remainder u. Mapping values through U gives precisely the optional natural sign-byte list followed by D's digit encoding; this follows by the two sign cases and the value-map hypothesis.
Apply NumberHelperScientificRenderedSuffixSafeLength to u,\mathit{sign},D. All its hypotheses were just checked; it returns an I64 exponent e and empty residual r, with the stated two-sided bound. Independently apply ScientificVocabularyFold to u,\mathit{sign},J,[]. The digit range is assumed, J is nonempty, the empty-tail head condition is vacuous, and the slice decomposition is the definition of u. This gives another normal result of the same actual call, with value the sign multiplier times the natural guarded fold on the decoding of J. Inject equality of the two normal results, then the option and pair constructors: the exponents and residuals coincide. Substitute the decoding equation. The multiplier is -1 precisely for sign \texttt{some true} and 1 for both other signs, exactly as the accumulated exponent in DecimalGoalVocabularyDefault. Thus its value is the asserted accumulated exponent. This uses the pre-update 2^{59} guard and retains its possible last-update overshoot, not an unguarded exponent fold. The split, marker, scientific result and empty residual establish the second alternative.

Lean proof


  cases hx : x.exponent with
  | none =>
    refine ⟨0#i64, ?_, by norm_num, by norm_num, Or.inl ⟨?_, rfl⟩⟩
    · simp [DecimalGoalVocabularyDefault, hx]
    · simpa [hx] using hend
  | some tuple =>
    rcases tuple with ⟨upper, sign, ds⟩
    have hv : ds ≠ [] /\ forall d, d ∈ ds -> d < 10 := by
      have hv := (show _ ∧ _ ∧ _ ∧ _ from hvalid).2.2.2
      simpa [hx] using hv
    have hm : digits.map (fun b => b.val) = ds.map (fun d => 48 + d) := by
      simpa [hx] using hmap
    obtain ⟨hdecode, hlength, _⟩ := (P435_T1_DigitDecoding digits).2 ds hm
    have hne : digits != [] := by
      simp only [bne_iff_ne]
      intro he
      have : ds.length = 0 := by simpa [he] using hlength.symm
      exact hv.1 (List.eq_nil_of_length_eq_zero this)
    let suffix := (match sign with
      | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits
    have hs : sigEnd.val = (if upper then 69#u8 else 101#u8) :: suffix := by
      cases sign <;> simpa [hx, suffix] using hend
    have hlen : suffix.length <= Usize.max := by
      have h := sigEnd.property
      rw [hs] at h
      simp only [List.length_cons] at h
      omega
    let next : Slice Std.U8 := ⟨suffix, hlen⟩
    have hsplit : core.slice.Slice.split_first sigEnd =
        ok (some ((if upper then 69#u8 else 101#u8), next)) := by
      let shaped : Slice Std.U8 := ⟨(if upper then 69#u8 else 101#u8) :: suffix,
        by rw [← hs]; exact sigEnd.property⟩
      have hEq : sigEnd = shaped := (Slice.eq_iff _ _).2 hs
      rw [hEq]
      rfl
    have hmapNext : next.val.map (fun b => b.val) =
        (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
          ds.map (fun d => 48 + d) := by
      cases sign with
      | none => simpa [next, suffix] using hm
      | some neg => cases neg <;> simp [next, suffix, hm]
    obtain ⟨explicit, rest, hparse, hrest, hlo, hhi, _, _⟩ :=
      NumberHelperScientificRenderedSuffixSafeLength next sign ds
        (by simpa only [bne_iff_ne] using hv.1) hv.2 hmapNext
    obtain ⟨foldExponent, foldRest, hfoldParse, _, hfold⟩ :=
      P9_T1_ScientificVocabularyFold next sign digits [] hne hdigits
        (by simp) (by cases sign <;> simp [next, suffix])
    have heq : (some foldExponent, foldRest) = (some explicit, rest) :=
      Result.ok.inj (hfoldParse.symm.trans hparse)
    have he : foldExponent = explicit := Option.some.inj (congrArg Prod.fst heq)
    rw [he, hdecode] at hfold
    refine ⟨explicit, ?_, hlo, hhi, Or.inr ⟨_, next, rest, hsplit, ?_, hparse, hrest⟩⟩
    · cases sign with
      | none => simpa [DecimalGoalVocabularyDefault, hx] using hfold
      | some neg => cases neg <;> simpa [DecimalGoalVocabularyDefault, hx] using hfold
    · cases upper <;> simp

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P9_T1_ScientificVocabularyFold
import Tablet.NumberHelperScientificRenderedSuffixSafeLength
import Tablet.P435_T1_DigitDecoding
import Tablet.ModelSliceSplitFirst

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

-- [TABLET NODE: P789_T1_RenderedExponentContinuation]
theorem P789_T1_RenderedExponentContinuation
    (x : DecimalLexeme) (sigEnd : Slice Std.U8) (digits : List Std.U8)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hmap : digits.map (fun b => b.val) =
      (match x.exponent with | none => [] | some (_, _, ds) => ds).map (fun d => 48 + d))
    (hdigits : forall b, b ∈ digits -> 48 <= b.val /\ b.val <= 57)
    (hend : sigEnd.val = match x.exponent with
      | none => []
      | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) ::
        (match sign with | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits) :
    exists explicit : Std.I64,
      explicit.val = DecimalGoalVocabularyDefault.accumulatedExplicitExponent x /\
      -5764607523034234879 <= explicit.val /\ explicit.val <= 5764607523034234879 /\
      ((sigEnd.val = [] /\ explicit = 0#i64) \/
        exists (marker : Std.U8) (next rest : Slice Std.U8),
          core.slice.Slice.split_first sigEnd = ok (some (marker, next)) /\
          (marker = 101#u8 \/ marker = 69#u8) /\
          num.imp.dec2flt.parse.parse_scientific next = ok (some explicit, rest) /\
          rest.val = []) := by
-- BODY
  cases hx : x.exponent with
  | none =>
    refine ⟨0#i64, ?_, by norm_num, by norm_num, Or.inl ⟨?_, rfl⟩⟩
    · simp [DecimalGoalVocabularyDefault, hx]
    · simpa [hx] using hend
  | some tuple =>
    rcases tuple with ⟨upper, sign, ds⟩
    have hv : ds ≠ [] /\ forall d, d ∈ ds -> d < 10 := by
      have hv := (show _ ∧ _ ∧ _ ∧ _ from hvalid).2.2.2
      simpa [hx] using hv
    have hm : digits.map (fun b => b.val) = ds.map (fun d => 48 + d) := by
      simpa [hx] using hmap
    obtain ⟨hdecode, hlength, _⟩ := (P435_T1_DigitDecoding digits).2 ds hm
    have hne : digits != [] := by
      simp only [bne_iff_ne]
      intro he
      have : ds.length = 0 := by simpa [he] using hlength.symm
      exact hv.1 (List.eq_nil_of_length_eq_zero this)
    let suffix := (match sign with
      | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits
    have hs : sigEnd.val = (if upper then 69#u8 else 101#u8) :: suffix := by
      cases sign <;> simpa [hx, suffix] using hend
    have hlen : suffix.length <= Usize.max := by
      have h := sigEnd.property
      rw [hs] at h
      simp only [List.length_cons] at h
      omega
    let next : Slice Std.U8 := ⟨suffix, hlen⟩
    have hsplit : core.slice.Slice.split_first sigEnd =
        ok (some ((if upper then 69#u8 else 101#u8), next)) := by
      let shaped : Slice Std.U8 := ⟨(if upper then 69#u8 else 101#u8) :: suffix,
        by rw [← hs]; exact sigEnd.property⟩
      have hEq : sigEnd = shaped := (Slice.eq_iff _ _).2 hs
      rw [hEq]
      rfl
    have hmapNext : next.val.map (fun b => b.val) =
        (match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
          ds.map (fun d => 48 + d) := by
      cases sign with
      | none => simpa [next, suffix] using hm
      | some neg => cases neg <;> simp [next, suffix, hm]
    obtain ⟨explicit, rest, hparse, hrest, hlo, hhi, _, _⟩ :=
      NumberHelperScientificRenderedSuffixSafeLength next sign ds
        (by simpa only [bne_iff_ne] using hv.1) hv.2 hmapNext
    obtain ⟨foldExponent, foldRest, hfoldParse, _, hfold⟩ :=
      P9_T1_ScientificVocabularyFold next sign digits [] hne hdigits
        (by simp) (by cases sign <;> simp [next, suffix])
    have heq : (some foldExponent, foldRest) = (some explicit, rest) :=
      Result.ok.inj (hfoldParse.symm.trans hparse)
    have he : foldExponent = explicit := Option.some.inj (congrArg Prod.fst heq)
    rw [he, hdecode] at hfold
    refine ⟨explicit, ?_, hlo, hhi, Or.inr ⟨_, next, rest, hsplit, ?_, hparse, hrest⟩⟩
    · cases sign with
      | none => simpa [DecimalGoalVocabularyDefault, hx] using hfold
      | some neg => cases neg <;> simpa [DecimalGoalVocabularyDefault, hx] using hfold
    · cases upper <;> simp

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 43 entries
Technical checking records
Local closure summary
{
  "node": "P789_T1_RenderedExponentContinuation",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "1d33485a4b41e31c7d485a97e131c1f8e1d8c06648cca57d3142184cba52546e",
  "active_statement_hash": "29baa4a8ce4837d06acbf91ff05329f19e7cde08bc4df3a690946c969d7c199f",
  "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-182",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
    "NumberHelperScientificRenderedSuffixSafeLength": "e9bebcd7a1b030d2",
    "P435_T1_DigitDecoding": "0b69e5fe3215de46",
    "P9_T1_ScientificVocabularyFold": "b4e9d9d3618cb9a8"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "b9386c8d0fd53e6e",
    "DecimalLexeme": "8127b3eaeae00dd2",
    "F64": "ec4e25fbaf4e11d4",
    "P9_T1_ScientificVocabularyFold": "28ca0a8ac503ee01",
    "Preamble": "965f15fc36f80938",
    "num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f",
    "num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2",
    "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:520dded0002ded2ae546d8259aae0750ecd5cc7b7923058771ac12265a365bd5",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}