P835_T1_PartialRunReverseA

Lean checked

Statement

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

Lemma · Reverse on-normal lexical certificate

Let s be a nonempty byte slice with |s|\le3458764513820540928, and suppose the actual partial-number parser returns \texttt{ok}(o). Let x be an unsigned valid numeric lexeme in DecimalGoalVocabularyDefault, and R a byte list, with \operatorname{bytes}(s)=\operatorname{renderNumeric}(x)++ \operatorname{map}(\mathit{val},R). Require R to be empty or nondigit-headed. If x has no exponent, require every residual head to differ from 69 and 101, and also from 46 when x has no fraction. Then there exist d,k such that o=\texttt{some}(d,k) and k.\mathit{val}=|\operatorname{renderNumeric}(x)|.

Lean statement

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

import Tablet.P435_T1_RenderedDigitBlocks
import Tablet.P435_T1_DigitPrefixUnique
import Tablet.P364_T1_NumberHelperDigitPrefix
import Tablet.P835_T1_PartialContinuationA
import Tablet.P835_T1_FinishOnNormalA
import Tablet.NumberHelperScientificPrefixSafeLength
import Tablet.ModelOptionBranch

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P835_T1_PartialRunReverseA]
theorem P835_T1_PartialRunReverseA
    (s : Slice Std.U8) (hne : s.val ≠ [])
    (hlen : s.val.length <= 3458764513820540928)
    (result : Option (num.imp.dec2flt.decimal.Decimal × Std.Usize))
    (hnormal : num.imp.dec2flt.parse.parse_partial_number s = ok result)
    (x : DecimalLexeme) (r : List Std.U8)
    (hsign : x.leadingSign = none)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hrender : DecimalGoalVocabularyDefault.bytes s =
      DecimalGoalVocabularyDefault.renderNumeric x ++ r.map (fun b => b.val))
    (hrt : forall b bs, r = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57))
    (hguards : x.exponent = none -> forall b bs, r = b :: bs ->
      b ≠ 69#u8 /\ b ≠ 101#u8 /\ (x.fractionDigits = none -> b ≠ 46#u8)) :
    exists d consumed, result = some (d, consumed) /\
      consumed.val = (DecimalGoalVocabularyDefault.renderNumeric x).length := 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

Apply RenderedDigitBlocks to s.\mathit{val}, R,x, using unsignedness, validity and the rendering equation. Obtain byte lists I,F,J with the exact source decomposition into the integer block, optional point and fractional block, optional marker and sign and exponent block, and R. Its statement gives the value-map equations for all three blocks and their digit ranges. Let p be true exactly when the fraction option of x is present, and let T be its optional rendered exponent byte block followed by R. The decomposition becomes s.\mathit{val}=I++(\text{if }p\text{ then }[46]++F++T \text{ else }T). If the fraction is absent its value-map equation forces F=[]. If the exponent is absent, T=R and the assumed digit and point guards apply. If it is present, T begins with 69 or 101, each outside [48,57] and different from 46. Thus T is nondigit-headed, and when p is false it has no point head.
Use NumberHelperDigitPrefix on s.\mathit{val} to obtain its actual maximal integer run. The specified integer block is also maximal: its remainder begins with 46 when p is true, and otherwise is the nondigit-headed T just established. DigitPrefixUnique identifies the actual run and remainder with I and that specified remainder. Apply the digit-prefix theorem to F++T and its uniqueness theorem again to identify its digit run with F and remainder with T. When the point is present this is the actual fractional scan suffix; when absent F=[] and no fractional scan is requested. The forced point, or its exclusion by the absent-point guard, therefore agrees with the actual source branch.
The value-map equations preserve block lengths: |I| equals the number of integer digits, and |F| equals the number of fractional digits, or zero for absent fraction. The nonempty-significand validity clause in DecimalGoalVocabularyDefault consequently gives |I|+|F|>0. Apply PartialContinuationA with the identified blocks, point choice, all digit and residual guards, source nonemptiness and A bound. Its count n has this positive value, so the zero-count None exit is impossible. Substitute its computation equality in the given normal-result equation. The continuation after splitting its actual slice t, whose contents are T, therefore equals \texttt{ok}(o).
With no exponent, T=R. ModelSliceSplitFirst returns None when R=[], giving C(b,0,t). If R is nonempty it exposes its head, which differs from 101 and 69 by hypothesis, giving the same C(b,0,t). Its final residual is exactly R.
With exponent (u,\sigma,D), write c=69 for u true and 101 otherwise. The exact byte-block decomposition gives T=c::(\operatorname{signBytes}(\sigma)++J++R). ModelSliceSplitFirst on t thus exposes c and the actual suffix slice v with v.\mathit{val}=\operatorname{signBytes}(\sigma)++J++R. For c=101 the first comparison selects E(v); for c=69 the first comparison fails and the second selects the identical E(v). Exponent validity says D\ne[], and the exponent value-map equation has length |J|=|D|, so J\ne[]. Its digit ranges and the assumed residual nondigit guard are exactly the hypotheses of NumberHelperScientificPrefixSafeLength on this actual v, sign \sigma, block J and residual R. It supplies a normal scientific return (\texttt{some}(a),r) with r.\mathit{val}=R. No short-exponent premise is needed. Substitute this equation into the normal equation for E(v). By ModelOptionBranch, Some takes Continue with a. Inspect the checked addition b+a: failure or divergence would propagate to the whole result, contradicting ok(o). Its successful case supplies e, leaving C(e,a,r)=\texttt{ok}(o).
In the no-exponent case let P=I++(\text{if }p\text{ then }[46]++F\text{ else }[]). In the exponent case append [c], the sign bytes, and J to this P. The exact decompositions give s.\mathit{val}=P++r.\mathit{val} in each case, with r=t in the no-exponent case and always r.\mathit{val}=R. Apply FinishOnNormalA to the successful continuation to conclude o=\texttt{some}(d,k) and k.\mathit{val}=|P|. Taking lengths of the original rendering equation gives |s|=|\operatorname{renderNumeric}(x)|+|R|; taking lengths of the exact suffix equation gives |s|=|P|+|R|. Cancel |R| to get the required consumed length. The proof has used normality only as its given premise: it has not asserted termination, additional Decimal-field constraints, an exponent-digit bound, or any restriction on a nondigit residual after a present exponent.

Lean proof


  obtain ⟨integer, fraction, digits, hshape, him, hfm, hem, hd, hf, he⟩ :=
    P435_T1_RenderedDigitBlocks s.val r x hsign hvalid hrender
  let point := x.fractionDigits.isSome
  let tail : List Std.U8 :=
    (match (generalizing := false) x.exponent with
    | none => []
    | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) ::
      (match (generalizing := false) sign with
      | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits) ++ r
  have hshape' : s.val =
      integer ++ (if point then 46#u8 :: (fraction ++ tail) else tail) := by
    rw [hshape]
    cases hx : x.fractionDigits <;>
      simp [point, tail, hx, List.append_assoc]
    all_goals
      cases x.exponent with
      | none => rfl
      | some p =>
        rcases p with ⟨u, sg, ds⟩
        cases sg <;> rfl
  have hnoFraction : point = false -> fraction = [] := by
    intro hp
    cases hx : x.fractionDigits with
    | none => simpa [hx] using hfm
    | some ds => simp [point, hx] at hp
  have ht : forall b bs, tail = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57) := by
    cases hx : x.exponent with
    | none => simpa [tail, hx] using hrt
    | some p =>
      rcases p with ⟨upper, sign, ds⟩
      intro b bs hb
      have hhead : (if upper then 69#u8 else 101#u8) = b := by
        simpa [tail, hx] using congrArg List.head? hb
      rw [← hhead]
      cases upper <;> norm_num
  have hnoPoint : point = false -> forall b bs, tail = b :: bs -> b ≠ 46#u8 := by
    intro hp b bs hb
    cases hx : x.exponent with
    | none =>
      have hr : r = b :: bs := by simpa [tail, hx] using hb
      have hfrac : x.fractionDigits = none := by
        cases hf : x.fractionDigits <;> simp_all [point]
      exact (hguards hx b bs hr).2.2 hfrac
    | some p =>
      rcases p with ⟨upper, sign, ds⟩
      have hhead : (if upper then 69#u8 else 101#u8) = b := by
        simpa [tail, hx] using congrArg List.head? hb
      rw [← hhead]
      cases upper <;> decide
  have hpos : 0 < integer.length + fraction.length := by
    have hiLen := congrArg List.length him
    have hfLen := congrArg List.length hfm
    simp only [List.length_map] at hiLen hfLen
    have hv := hvalid
    change _ ∧ _ ∧ _ ∧ _ at hv
    rcases hv.2.2.1 with hi | hfr
    · have hnonzero := List.length_pos_iff.mpr hi
      omega
    · cases hx : x.fractionDigits with
      | none => simp [hx] at hfr
      | some ds =>
        have hnonzero : 0 < ds.length := List.length_pos_iff.mpr (by simpa [hx] using hfr)
        simp only [hx] at hfLen
        omega
  have htailInteger : forall b bs,
      (if point then 46#u8 :: (fraction ++ tail) else tail) = b :: bs ->
      ¬ (48 <= b.val /\ b.val <= 57) := by
    cases point with
    | false => exact ht
    | true => intro b bs hb; cases hb; norm_num
  obtain ⟨actualInteger, actualTail, hactual, haDigits, haTail⟩ :=
    P364_T1_NumberHelperDigitPrefix s.val
  have hunique := P435_T1_DigitPrefixUnique actualInteger actualTail integer
    (if point then 46#u8 :: (fraction ++ tail) else tail)
    (hactual.symm.trans hshape') haDigits haTail hd htailInteger
  rcases hunique with ⟨hiUnique, htUnique⟩
  subst actualInteger
  subst actualTail
  obtain ⟨actualFraction, actualFractionTail, hfActual, hfDigits, hfTail⟩ :=
    P364_T1_NumberHelperDigitPrefix (fraction ++ tail)
  have hfUnique := P435_T1_DigitPrefixUnique actualFraction actualFractionTail fraction tail
    hfActual.symm hfDigits hfTail hf ht
  rcases hfUnique with ⟨hfUnique, htUnique⟩
  subst actualFraction
  subst actualFractionTail
  obtain ⟨intEnd, sigEnd, word, total, base, hsigEnd, htotal, hparser⟩ :=
    P835_T1_PartialContinuationA s integer fraction tail point hne hlen
      hshape' hnoFraction hd hf ht hnoPoint
  let finish := fun (exponent explicit : Std.I64) (rest : Slice Std.U8) => do
    let offset ← SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest s
    let len ← lift (IScalar.hcast .Usize offset)
    if total <= 19#isize then
      ok (some (num.imp.dec2flt.decimal.Decimal.mk exponent word false false, len))
    else
      let more ← total - 19#isize
      let adjusted ← num.imp.dec2flt.parse.adjust_many_digit_count s more
      if adjusted > 0#isize then
        let (manyWord, manyExp) ← num.imp.dec2flt.parse.parse_many_digits s intEnd explicit
        ok (some (num.imp.dec2flt.decimal.Decimal.mk manyExp manyWord false true, len))
      else
        ok (some (num.imp.dec2flt.decimal.Decimal.mk exponent word false false, len))
  let scientific := fun (next : Slice Std.U8) => do
    let (value, rest) ← num.imp.dec2flt.parse.parse_scientific next
    let cf ← core.option.Option.Insts.CoreOpsTry_traitTry.branch value
    match cf with
    | core.ops.control_flow.ControlFlow.Continue explicit => do
      let exponent ← base + explicit
      finish exponent explicit rest
    | core.ops.control_flow.ControlFlow.Break residual =>
      core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual
        (num.imp.dec2flt.decimal.Decimal × Std.Usize) residual
  let afterSignificand : Result (Option (num.imp.dec2flt.decimal.Decimal × Std.Usize)) := do
    let o ← core.slice.Slice.split_first sigEnd
    match o with
    | none => finish base 0#i64 sigEnd
    | some (c, next) =>
      if c = 101#u8 then scientific next
      else if c = 69#u8 then scientific next
      else finish base 0#i64 sigEnd
  
  change num.imp.dec2flt.parse.parse_partial_number s =
    (if total = 0#isize then ok none else afterSignificand) at hparser
  have htne : total ≠ 0#isize := by
    intro hz
    have hzero : total.val = 0 := by rw [hz]; simp
    rw [htotal] at hzero
    omega
  have hafterNormal : afterSignificand = ok result := by
    rw [hparser] at hnormal
    simpa only [if_neg htne] using hnormal
  have finishDone : forall (exponent explicit : Std.I64) (rest : Slice Std.U8)
      (pref : List Std.U8), rest.val = r -> s.val = pref ++ rest.val ->
      finish exponent explicit rest = ok result ->
      exists d consumed, result = some (d, consumed) /\
        consumed.val = (DecimalGoalVocabularyDefault.renderNumeric x).length := by
    intro exponent explicit rest pref hr hp hn
    obtain ⟨d, consumed, hres, hk⟩ :=
      P835_T1_FinishOnNormalA s intEnd rest pref word total exponent explicit
        result hlen hp hn
    refine ⟨d, consumed, hres, ?_⟩
    have hlength := congrArg List.length hrender
    change (s.val.map (fun b => b.val)).length = _ at hlength
    simp only [List.length_map, List.length_append] at hlength
    have hlength' := congrArg List.length hp
    simp only [List.length_append, hr] at hlength'
    rw [hk]
    omega
  cases hx : x.exponent with
  | none =>
    have htail : tail = r := by simp [tail, hx]
    have hfinish : finish base 0#i64 sigEnd = ok result := by
      cases hr : r with
      | nil =>
        have hs : sigEnd = (⟨[], by simp⟩ : Slice Std.U8) :=
          (Slice.eq_iff _ _).2 (by rw [hsigEnd, htail, hr])
        have hsplit : core.slice.Slice.split_first sigEnd = ok none := by rw [hs]; rfl
        simpa [afterSignificand, hsplit] using hafterNormal
      | cons b bs =>
        let next : Slice Std.U8 := ⟨bs, by
          have hp := sigEnd.property
          rw [hsigEnd, htail, hr] at hp
          simp only [List.length_cons] at hp
          omega⟩
        have hs : sigEnd = (⟨b :: bs, by
          rw [← hr, ← htail, ← hsigEnd]
          exact sigEnd.property⟩ : Slice Std.U8) :=
          (Slice.eq_iff _ _).2 (hsigEnd.trans (htail.trans hr))
        have hsplit : core.slice.Slice.split_first sigEnd = ok (some (b, next)) := by
          rw [hs]; rfl
        have hg := hguards hx b bs hr
        simpa [afterSignificand, hsplit, hg.1, hg.2.1] using hafterNormal
    apply finishDone base 0#i64 sigEnd (integer ++ (if point then 46#u8 :: fraction else []))
    · exact hsigEnd.trans htail
    · rw [hshape', hsigEnd]
      cases point <;> simp [List.append_assoc]
    · exact hfinish
  | some p =>
    rcases p with ⟨upper, sign, ds⟩
    let signBytes : List Std.U8 :=
      match (generalizing := false) sign with
      | none => [] | some neg => [if neg then 45#u8 else 43#u8]
    let marker : Std.U8 := if upper then 69#u8 else 101#u8
    have htail : tail = marker :: (signBytes ++ digits ++ r) := by
      simp [tail, hx, marker, signBytes, List.append_assoc]
    let next : Slice Std.U8 := ⟨signBytes ++ digits ++ r, by
      have hp := sigEnd.property
      rw [hsigEnd, htail] at hp
      simp only [List.length_cons] at hp
      omega⟩
    have hs : sigEnd = (⟨marker :: next.val, by
      change (marker :: (signBytes ++ digits ++ r)).length <= Usize.max
      rw [← htail, ← hsigEnd]
      exact sigEnd.property⟩ : Slice Std.U8) :=
      (Slice.eq_iff _ _).2 (hsigEnd.trans htail)
    have hsplit : core.slice.Slice.split_first sigEnd = ok (some (marker, next)) := by
      rw [hs]; rfl
    have hscient : scientific next = ok result := by
      cases upper <;> simpa [afterSignificand, hsplit, marker] using hafterNormal
    have hdigitsNe : digits ≠ [] := by
      have hv := hvalid
      change _ ∧ _ ∧ _ ∧ _ at hv
      have hvexp : ds ≠ [] ∧ (forall d, d ∈ ds -> d < 10) := by
        simpa only [hx] using hv.2.2.2
      have hds := hvexp.1
      have hm : digits.map (fun b => b.val) = ds.map (fun d => 48 + d) := by
        simpa [hx] using hem
      intro hz
      simp [hz] at hm
      exact hds hm
    obtain ⟨explicit, rest, hparse, hrest, hlo, hhi, hshort, hpositive⟩ :=
      NumberHelperScientificPrefixSafeLength next sign digits r
        (by simpa only [bne_iff_ne] using hdigitsNe) he hrt rfl
    cases haddExp : base + explicit with
    | fail err =>
      simp [scientific, hparse,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] at hscient
    | div =>
      simp [scientific, hparse,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] at hscient
    | ok exponent =>
      have hfinish : finish exponent explicit rest = ok result := by
        simpa [scientific, hparse,
          core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] using hscient
      apply finishDone exponent explicit rest
        (integer ++ (if point then 46#u8 :: fraction else []) ++ [marker] ++ signBytes ++ digits)
        hrest
      · rw [hshape', htail, hrest]
        cases point <;> simp [List.append_assoc]
      · exact hfinish

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P435_T1_RenderedDigitBlocks
import Tablet.P435_T1_DigitPrefixUnique
import Tablet.P364_T1_NumberHelperDigitPrefix
import Tablet.P835_T1_PartialContinuationA
import Tablet.P835_T1_FinishOnNormalA
import Tablet.NumberHelperScientificPrefixSafeLength
import Tablet.ModelOptionBranch

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P835_T1_PartialRunReverseA]
theorem P835_T1_PartialRunReverseA
    (s : Slice Std.U8) (hne : s.val ≠ [])
    (hlen : s.val.length <= 3458764513820540928)
    (result : Option (num.imp.dec2flt.decimal.Decimal × Std.Usize))
    (hnormal : num.imp.dec2flt.parse.parse_partial_number s = ok result)
    (x : DecimalLexeme) (r : List Std.U8)
    (hsign : x.leadingSign = none)
    (hvalid : DecimalGoalVocabularyDefault.validNumeric x)
    (hrender : DecimalGoalVocabularyDefault.bytes s =
      DecimalGoalVocabularyDefault.renderNumeric x ++ r.map (fun b => b.val))
    (hrt : forall b bs, r = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57))
    (hguards : x.exponent = none -> forall b bs, r = b :: bs ->
      b ≠ 69#u8 /\ b ≠ 101#u8 /\ (x.fractionDigits = none -> b ≠ 46#u8)) :
    exists d consumed, result = some (d, consumed) /\
      consumed.val = (DecimalGoalVocabularyDefault.renderNumeric x).length := by
-- BODY
  obtain ⟨integer, fraction, digits, hshape, him, hfm, hem, hd, hf, he⟩ :=
    P435_T1_RenderedDigitBlocks s.val r x hsign hvalid hrender
  let point := x.fractionDigits.isSome
  let tail : List Std.U8 :=
    (match (generalizing := false) x.exponent with
    | none => []
    | some (upper, sign, _) => (if upper then 69#u8 else 101#u8) ::
      (match (generalizing := false) sign with
      | none => [] | some neg => [if neg then 45#u8 else 43#u8]) ++ digits) ++ r
  have hshape' : s.val =
      integer ++ (if point then 46#u8 :: (fraction ++ tail) else tail) := by
    rw [hshape]
    cases hx : x.fractionDigits <;>
      simp [point, tail, hx, List.append_assoc]
    all_goals
      cases x.exponent with
      | none => rfl
      | some p =>
        rcases p with ⟨u, sg, ds⟩
        cases sg <;> rfl
  have hnoFraction : point = false -> fraction = [] := by
    intro hp
    cases hx : x.fractionDigits with
    | none => simpa [hx] using hfm
    | some ds => simp [point, hx] at hp
  have ht : forall b bs, tail = b :: bs -> ¬ (48 <= b.val /\ b.val <= 57) := by
    cases hx : x.exponent with
    | none => simpa [tail, hx] using hrt
    | some p =>
      rcases p with ⟨upper, sign, ds⟩
      intro b bs hb
      have hhead : (if upper then 69#u8 else 101#u8) = b := by
        simpa [tail, hx] using congrArg List.head? hb
      rw [← hhead]
      cases upper <;> norm_num
  have hnoPoint : point = false -> forall b bs, tail = b :: bs -> b ≠ 46#u8 := by
    intro hp b bs hb
    cases hx : x.exponent with
    | none =>
      have hr : r = b :: bs := by simpa [tail, hx] using hb
      have hfrac : x.fractionDigits = none := by
        cases hf : x.fractionDigits <;> simp_all [point]
      exact (hguards hx b bs hr).2.2 hfrac
    | some p =>
      rcases p with ⟨upper, sign, ds⟩
      have hhead : (if upper then 69#u8 else 101#u8) = b := by
        simpa [tail, hx] using congrArg List.head? hb
      rw [← hhead]
      cases upper <;> decide
  have hpos : 0 < integer.length + fraction.length := by
    have hiLen := congrArg List.length him
    have hfLen := congrArg List.length hfm
    simp only [List.length_map] at hiLen hfLen
    have hv := hvalid
    change _ ∧ _ ∧ _ ∧ _ at hv
    rcases hv.2.2.1 with hi | hfr
    · have hnonzero := List.length_pos_iff.mpr hi
      omega
    · cases hx : x.fractionDigits with
      | none => simp [hx] at hfr
      | some ds =>
        have hnonzero : 0 < ds.length := List.length_pos_iff.mpr (by simpa [hx] using hfr)
        simp only [hx] at hfLen
        omega
  have htailInteger : forall b bs,
      (if point then 46#u8 :: (fraction ++ tail) else tail) = b :: bs ->
      ¬ (48 <= b.val /\ b.val <= 57) := by
    cases point with
    | false => exact ht
    | true => intro b bs hb; cases hb; norm_num
  obtain ⟨actualInteger, actualTail, hactual, haDigits, haTail⟩ :=
    P364_T1_NumberHelperDigitPrefix s.val
  have hunique := P435_T1_DigitPrefixUnique actualInteger actualTail integer
    (if point then 46#u8 :: (fraction ++ tail) else tail)
    (hactual.symm.trans hshape') haDigits haTail hd htailInteger
  rcases hunique with ⟨hiUnique, htUnique⟩
  subst actualInteger
  subst actualTail
  obtain ⟨actualFraction, actualFractionTail, hfActual, hfDigits, hfTail⟩ :=
    P364_T1_NumberHelperDigitPrefix (fraction ++ tail)
  have hfUnique := P435_T1_DigitPrefixUnique actualFraction actualFractionTail fraction tail
    hfActual.symm hfDigits hfTail hf ht
  rcases hfUnique with ⟨hfUnique, htUnique⟩
  subst actualFraction
  subst actualFractionTail
  obtain ⟨intEnd, sigEnd, word, total, base, hsigEnd, htotal, hparser⟩ :=
    P835_T1_PartialContinuationA s integer fraction tail point hne hlen
      hshape' hnoFraction hd hf ht hnoPoint
  let finish := fun (exponent explicit : Std.I64) (rest : Slice Std.U8) => do
    let offset ← SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from rest s
    let len ← lift (IScalar.hcast .Usize offset)
    if total <= 19#isize then
      ok (some (num.imp.dec2flt.decimal.Decimal.mk exponent word false false, len))
    else
      let more ← total - 19#isize
      let adjusted ← num.imp.dec2flt.parse.adjust_many_digit_count s more
      if adjusted > 0#isize then
        let (manyWord, manyExp) ← num.imp.dec2flt.parse.parse_many_digits s intEnd explicit
        ok (some (num.imp.dec2flt.decimal.Decimal.mk manyExp manyWord false true, len))
      else
        ok (some (num.imp.dec2flt.decimal.Decimal.mk exponent word false false, len))
  let scientific := fun (next : Slice Std.U8) => do
    let (value, rest) ← num.imp.dec2flt.parse.parse_scientific next
    let cf ← core.option.Option.Insts.CoreOpsTry_traitTry.branch value
    match cf with
    | core.ops.control_flow.ControlFlow.Continue explicit => do
      let exponent ← base + explicit
      finish exponent explicit rest
    | core.ops.control_flow.ControlFlow.Break residual =>
      core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual
        (num.imp.dec2flt.decimal.Decimal × Std.Usize) residual
  let afterSignificand : Result (Option (num.imp.dec2flt.decimal.Decimal × Std.Usize)) := do
    let o ← core.slice.Slice.split_first sigEnd
    match o with
    | none => finish base 0#i64 sigEnd
    | some (c, next) =>
      if c = 101#u8 then scientific next
      else if c = 69#u8 then scientific next
      else finish base 0#i64 sigEnd
  
  change num.imp.dec2flt.parse.parse_partial_number s =
    (if total = 0#isize then ok none else afterSignificand) at hparser
  have htne : total ≠ 0#isize := by
    intro hz
    have hzero : total.val = 0 := by rw [hz]; simp
    rw [htotal] at hzero
    omega
  have hafterNormal : afterSignificand = ok result := by
    rw [hparser] at hnormal
    simpa only [if_neg htne] using hnormal
  have finishDone : forall (exponent explicit : Std.I64) (rest : Slice Std.U8)
      (pref : List Std.U8), rest.val = r -> s.val = pref ++ rest.val ->
      finish exponent explicit rest = ok result ->
      exists d consumed, result = some (d, consumed) /\
        consumed.val = (DecimalGoalVocabularyDefault.renderNumeric x).length := by
    intro exponent explicit rest pref hr hp hn
    obtain ⟨d, consumed, hres, hk⟩ :=
      P835_T1_FinishOnNormalA s intEnd rest pref word total exponent explicit
        result hlen hp hn
    refine ⟨d, consumed, hres, ?_⟩
    have hlength := congrArg List.length hrender
    change (s.val.map (fun b => b.val)).length = _ at hlength
    simp only [List.length_map, List.length_append] at hlength
    have hlength' := congrArg List.length hp
    simp only [List.length_append, hr] at hlength'
    rw [hk]
    omega
  cases hx : x.exponent with
  | none =>
    have htail : tail = r := by simp [tail, hx]
    have hfinish : finish base 0#i64 sigEnd = ok result := by
      cases hr : r with
      | nil =>
        have hs : sigEnd = (⟨[], by simp⟩ : Slice Std.U8) :=
          (Slice.eq_iff _ _).2 (by rw [hsigEnd, htail, hr])
        have hsplit : core.slice.Slice.split_first sigEnd = ok none := by rw [hs]; rfl
        simpa [afterSignificand, hsplit] using hafterNormal
      | cons b bs =>
        let next : Slice Std.U8 := ⟨bs, by
          have hp := sigEnd.property
          rw [hsigEnd, htail, hr] at hp
          simp only [List.length_cons] at hp
          omega⟩
        have hs : sigEnd = (⟨b :: bs, by
          rw [← hr, ← htail, ← hsigEnd]
          exact sigEnd.property⟩ : Slice Std.U8) :=
          (Slice.eq_iff _ _).2 (hsigEnd.trans (htail.trans hr))
        have hsplit : core.slice.Slice.split_first sigEnd = ok (some (b, next)) := by
          rw [hs]; rfl
        have hg := hguards hx b bs hr
        simpa [afterSignificand, hsplit, hg.1, hg.2.1] using hafterNormal
    apply finishDone base 0#i64 sigEnd (integer ++ (if point then 46#u8 :: fraction else []))
    · exact hsigEnd.trans htail
    · rw [hshape', hsigEnd]
      cases point <;> simp [List.append_assoc]
    · exact hfinish
  | some p =>
    rcases p with ⟨upper, sign, ds⟩
    let signBytes : List Std.U8 :=
      match (generalizing := false) sign with
      | none => [] | some neg => [if neg then 45#u8 else 43#u8]
    let marker : Std.U8 := if upper then 69#u8 else 101#u8
    have htail : tail = marker :: (signBytes ++ digits ++ r) := by
      simp [tail, hx, marker, signBytes, List.append_assoc]
    let next : Slice Std.U8 := ⟨signBytes ++ digits ++ r, by
      have hp := sigEnd.property
      rw [hsigEnd, htail] at hp
      simp only [List.length_cons] at hp
      omega⟩
    have hs : sigEnd = (⟨marker :: next.val, by
      change (marker :: (signBytes ++ digits ++ r)).length <= Usize.max
      rw [← htail, ← hsigEnd]
      exact sigEnd.property⟩ : Slice Std.U8) :=
      (Slice.eq_iff _ _).2 (hsigEnd.trans htail)
    have hsplit : core.slice.Slice.split_first sigEnd = ok (some (marker, next)) := by
      rw [hs]; rfl
    have hscient : scientific next = ok result := by
      cases upper <;> simpa [afterSignificand, hsplit, marker] using hafterNormal
    have hdigitsNe : digits ≠ [] := by
      have hv := hvalid
      change _ ∧ _ ∧ _ ∧ _ at hv
      have hvexp : ds ≠ [] ∧ (forall d, d ∈ ds -> d < 10) := by
        simpa only [hx] using hv.2.2.2
      have hds := hvexp.1
      have hm : digits.map (fun b => b.val) = ds.map (fun d => 48 + d) := by
        simpa [hx] using hem
      intro hz
      simp [hz] at hm
      exact hds hm
    obtain ⟨explicit, rest, hparse, hrest, hlo, hhi, hshort, hpositive⟩ :=
      NumberHelperScientificPrefixSafeLength next sign digits r
        (by simpa only [bne_iff_ne] using hdigitsNe) he hrt rfl
    cases haddExp : base + explicit with
    | fail err =>
      simp [scientific, hparse,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] at hscient
    | div =>
      simp [scientific, hparse,
        core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] at hscient
    | ok exponent =>
      have hfinish : finish exponent explicit rest = ok result := by
        simpa [scientific, hparse,
          core.option.Option.Insts.CoreOpsTry_traitTry.branch, ModelOptionBranch, haddExp] using hscient
      apply finishDone exponent explicit rest
        (integer ++ (if point then 46#u8 :: fraction else []) ++ [marker] ++ signBytes ++ digits)
        hrest
      · rw [hshape', htail, hrest]
        cases point <;> simp [List.append_assoc]
      · exact hfinish

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (68)
More program dependencies (35)
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 63 entries
Technical checking records
Local closure summary
{
  "node": "P835_T1_PartialRunReverseA",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "85aec53da9febde64e330af4899d6dcbc22bcf95f8ca781241a98610f95a08fc",
  "active_statement_hash": "07f5edc8d16bb3dfb2146b3ba3eeba719ebc8554e8b4dee9098a24ad44f9e1a1",
  "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-194",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperScientificPrefixSafeLength": "2081afb9bfe32845",
    "P364_T1_NumberHelperDigitPrefix": "3426b4efc0d98c81",
    "P435_T1_DigitPrefixUnique": "1a8f5be111c56ca1",
    "P435_T1_RenderedDigitBlocks": "53c602364b85e70c",
    "P835_T1_FinishOnNormalA": "4f130f3375cbb311",
    "P835_T1_PartialContinuationA": "d026c1de5ccb78ec"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "55eb6bce0e8a4131",
    "DecimalLexeme": "8ad754cf2c272e73",
    "ModelOptionBranch": "c55e273482c8c0a1",
    "P435_T1_RenderedDigitBlocks": "3016c00221d29252",
    "P835_T1_PartialContinuationA": "5712e484f99eb7aa",
    "Preamble": "965f15fc36f80938",
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_offset_from": "7e7284dc9bc88830",
    "num_imp_dec2flt_decimal_Decimal": "4da4ccbe640ef8e6",
    "num_imp_dec2flt_parse_adjust_many_digit_count": "4e59e3776b6cfdaa",
    "num_imp_dec2flt_parse_parse_many_digits": "50c9ef13b3ed7fe8",
    "num_imp_dec2flt_parse_parse_partial_number": "94ed7b2ff71604ef",
    "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:053785d7eb9a5398898de1e41df96520f9b0ba244d11f861cb16b80b01a655df",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}