DecimalSeqFractionalIntegerState

Lean checked

Statement

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

Lemma · Fractional lexical state with a nonzero integer part

Let a source-valid slice render decimal digits I, a point, decimal digits F, and an optional well-formed exponent suffix whose accumulated signed value is E. Assume that deleting the leading zeroes of I leaves a nonempty list and that the extracted decimal-sequence parser succeeds with state d. If S is obtained from I F by deleting leading zeroes and R is obtained from S by deleting trailing zeroes, then d has logical count \min(|R|,768), its retained cells are exactly the corresponding prefix of R, and its truncation flag is true exactly when 768<|R|. Moreover, if z is the number of deleted leading zeroes, then for some integer k,
d.\mathit{decimal\_point}=|I|-z+E+k2^{32}.

Lean statement

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

import Tablet.DecimalDigitTrimDecomposition
import Tablet.DecimalSeqDigitRunAccumulate
import Tablet.DecimalSeqDuplicateLoops
import Tablet.DecimalSeqExponentResidualPointwise
import Tablet.DecimalSeqFinalizationExact
import Tablet.DecimalSeqFractionalParserCore
import Tablet.DecimalSeqLeadingZeroLoopExact
import Tablet.DecimalSeqRenderedSplit
import Tablet.DecimalSeqScalarScanExact
import Tablet.SourceValidByteSlice

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 3000000
set_option maxRecDepth 100000

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqFractionalIntegerState]
theorem DecimalSeqFractionalIntegerState (s : Slice Std.U8) (I F tail : List Nat) (E : Int)
    (exponent : Option (Bool × Option Bool × List Nat))
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (hsource : SourceValidByteSlice s)
    (hIdigits : forall z, z ∈ I -> z < 10)
    (hFdigits : forall z, z ∈ F -> z < 10)
    (hExpNe : forall upper sign digits,
      exponent = some (upper, sign, digits) -> digits ≠ [])
    (hExpDigits : forall upper sign digits,
      exponent = some (upper, sign, digits) ->
        forall z, z ∈ digits -> z < 10)
    (htailNone : exponent = none -> tail = [])
    (htailSome : forall upper sign digits,
      exponent = some (upper, sign, digits) -> tail =
        (if upper then 69 else 101) ::
          (match sign with
          | none => []
          | some negative => [if negative then 45 else 43]) ++
          digits.map (fun z => 48 + z))
    (hENone : exponent = none -> E = 0)
    (hESome : forall upper sign digits,
      exponent = some (upper, sign, digits) -> E =
        let n : Nat := digits.foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0
        match sign with | some true => -(n : Int) | _ => (n : Int))
    (hSigI : I.dropWhile (fun digit => digit == 0) ≠ [])
    (hbytes : s.val.map (fun b => b.val) =
      I.map (fun z => 48 + z) ++ 46 :: F.map (fun z => 48 + z) ++ tail)
    (hparse : num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d) :
    let significant := (I ++ F).dropWhile (fun digit => digit == 0)
    let trimmed :=
      (significant.reverse.dropWhile (fun digit => digit == 0)).reverse
    let z := (I ++ F).length - significant.length
    let P : Int := (I.length : Int) - z
    (d.num_digits.val = min trimmed.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trimmed.take d.num_digits.val /\
      (d.truncated = true <-> 768 < trimmed.length)) /\
      exists k : Int, d.decimal_point.val = P + E + k * 4294967296 := 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 DecimalDigitTrimDecomposition first to IF and then to I. Write z,q for the leading- and trailing-zero counts of IF, and z_I for the leading-zero count of I. Because the leading-zero deletion of I is nonempty, deletion commutes across the append with F and yields S=I'F, where I' is the nonzero-leading remainder of I. Comparing the two decompositions gives z=z_I. It also gives IF=0^z R0^q, S=R0^q, and the facts that R is nonempty and has nonzero last digit.
The byte equation therefore begins with exactly z_I ASCII zero bytes, followed by the rendering of I'. Its next byte cannot be zero: otherwise the maximality clause of the decomposition of I is contradicted. Apply DecimalSeqLeadingZeroLoopExact; the initial extracted loop consumes exactly those z_I bytes and leaves the rendering of I', the point, F, and the exponent suffix. Source validity supplies the machine-size bound. Apply DecimalSeqScalarScanExact to I'. It stops exactly at the point, returns logical count |I'|, leaves truncation false, writes the first \min(|I'|,768) digits, and preserves every other cell. The point slice is nonempty, index zero is byte 46, and range-indexing from one leaves exactly the rendering of F followed by the suffix.
The optional-exponent hypotheses imply that the residual suffix is either empty or begins with byte 69 or 101. The source length bounds |I'|+|F|+|\mathit{suffix}| by the machine maximum. Thus DecimalSeqDigitRunAccumulate, which uses the exact bulk and scalar digit-run semantics, consumes exactly F, leaves precisely the exponent suffix, returns logical count |S|, keeps truncation false, and stores the first \min(|S|,768) significant digits. The loop identity in DecimalSeqDuplicateLoops changes its bulk-loop equation into the loop~5 equation used by the fractional parser branch.
Render the identity IF=0^zR0^q with the point inserted after I. DecimalSeqRenderedSplit supplies a decomposition of the source prefix before the exponent into a prefix ending in a significant digit and a maximal suffix consisting only of point and zero bytes. That suffix contains exactly q zero bytes. The residual exponent slice remains source-valid. By DecimalSeqExponentResidualPointwise, evaluation of that exact suffix adds precisely E to any base point; this includes the absent suffix, where the stated hypothesis makes E=0.
All premises of DecimalSeqFractionalParserCore now hold. It exposes the checked branch base
b=\operatorname{cast}_{32}(|\mathit{suffix}|) -\operatorname{cast}_{32}(|F|+|\mathit{suffix}|)
and identifies the successful parse with the common finalization DecimalSeqFractionalFinish. Apply DecimalSeqI32HCastCongruence to both casts. Since S=R0^q and IF=0^zS, integer arithmetic gives
b+q+|R| \equiv -|F|+q+(|I|+|F|-z-q)=|I|-z \pmod {2^{32}}.
Finally apply DecimalSeqFinalizationExact. Its exact reverse scan removes the q trailing zeroes, its cap yields count \min(|R|,768) and the stated truncation equivalence, its fill loop preserves the retained prefix, and its exponent step adds E to the displayed congruence. These are exactly the four conclusions.

Lean proof


  dsimp only
  let sig := (I ++ F).dropWhile (fun digit => digit == 0)
  let trim := (sig.reverse.dropWhile (fun digit => digit == 0)).reverse
  suffices hgoal : (d.num_digits.val = min trim.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trim.take d.num_digits.val /\
      (d.truncated = true <-> 768 < trim.length)) /\ exists k : Int,
    d.decimal_point.val = (I.length : Int) -
      ((I ++ F).length - sig.length : Nat) + E + k * 4294967296 by
    simpa [sig, trim] using hgoal
  obtain ⟨z, q, hz, hq, hIF, hsigtrim, hsigstop, htrimstop⟩ :=
    DecimalDigitTrimDecomposition (I ++ F)
  obtain ⟨zi, qi, hzi, hqi, hI, hIsigTail, hIsigstop, hItrimstop⟩ :=
    DecimalDigitTrimDecomposition I
  let isig := I.dropWhile (fun digit => digit == 0)
  have hsig : sig = isig ++ F := by
    simp only [sig, isig, List.dropWhile_append]
    simp [hSigI]
  have hzzi : z = zi := by
    have hlenIF := congrArg List.length hIF
    have hlenI := congrArg List.length hI
    have hlenSig := congrArg List.length hsig
    simp at hlenIF hlenI hlenSig
    change (List.dropWhile (fun d => d == 0) (I ++ F)).length =
      (List.dropWhile (fun d => d == 0) I).length + F.length at hlenSig
    omega
  have hIbytes : I.map (fun n => 48 + n) =
      List.replicate zi 48 ++ isig.map (fun n => 48 + n) := by
    rw [hI, List.map_append]
    simp [isig]
  have hslead : s.val.map (fun b => b.val) =
      List.replicate zi 48 ++ isig.map (fun n => 48 + n) ++
        46 :: F.map (fun n => 48 + n) ++ tail := by
    rw [hbytes, hIbytes]
  have hleadstop :
      (isig.map (fun n => 48 + n) ++
        46 :: F.map (fun n => 48 + n) ++ tail)[0]? ≠ some 48 := by
    rcases hIsigstop with hempty | hstop
    · exact (hSigI (by simpa [isig] using hempty)).elim
    · intro heq
      apply hstop
      cases hisig : isig with
      | nil => exact (hSigI (by simpa [isig] using hisig)).elim
      | cons a restI =>
        have ha : a = 0 := by simp [hisig] at heq; omega
        subst a
        simp [isig, hisig]
  obtain ⟨s1, hlead, hs1⟩ := DecimalSeqLeadingZeroLoopExact s zi
    (isig.map (fun n => 48 + n) ++
      46 :: F.map (fun n => 48 + n) ++ tail)
    (by simpa [List.append_assoc] using hslead) (Or.inr hleadstop)
  let d0 : num.imp.dec2flt.decimal_seq.DecimalSeq := {
    num_digits := 0#usize
    decimal_point := 0#i32
    truncated := false
    digits := Array.repeat 768#usize 0#u8 }
  have hbound1 : isig.length <= UScalar.max .Usize := by
    unfold SourceValidByteSlice at hsource
    have hlen := congrArg List.length hslead
    simp at hlen
    simp [UScalar.max, Usize.max, Usize.numBits,
      Dec2Flt.configured_x86_64_numBits]
    omega
  obtain ⟨pointRest, d1, hscan1, hpointRest, hd1count, hd1point,
    hd1trunc, hd1old, hd1written, hd1outside⟩ :=
    DecimalSeqScalarScanExact s1 d0 isig
      (46 :: F.map (fun n => 48 + n) ++ tail)
      (by simpa [List.append_assoc] using hs1)
      (fun n hn => hIdigits n (by rw [hI]; exact List.mem_append_right _ hn))
      (by simp) (by simpa [d0] using hbound1)
  have hpointRestNe : pointRest.val ≠ [] := by
    intro he
    have hh := congrArg List.length hpointRest
    simp [he] at hh
  have hpointIndex : Slice.index_usize pointRest 0#usize = ok 46#u8 := by
    cases hr : pointRest.val with
    | nil => exact (hpointRestNe hr).elim
    | cons b bs =>
      have hbval : b.val = 46 := by
        have hh := congrArg List.head? hpointRest
        simp [hr] at hh
        exact hh
      have hb : b = 46#u8 := UScalar.eq_of_val_eq hbval
      simp [Slice.index_usize, hr, hb]
  let afterPoint := Slice.drop pointRest 1#usize
  have hdropPoint :
      core.slice.index.SliceIndexRangeFromUsizeSlice.index
        { start := 1#usize } pointRest = ok afterPoint := by
    simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
      Slice.length, afterPoint, hpointRestNe]
  have hafterPoint : afterPoint.val.map (fun b => b.val) =
      F.map (fun n => 48 + n) ++ tail := by
    simp [afterPoint, Slice.drop, List.map_drop, hpointRest]
  have hd1prefix :
      (d1.digits.val.take (min isig.length 768)).map (fun b => b.val) =
        isig.take (min isig.length 768) := by
    apply List.ext_getElem?
    intro j
    by_cases hj : j < min isig.length 768
    · rw [List.getElem?_map, List.getElem?_take, List.getElem?_take]
      simp only [hj, if_true]
      have hw := hd1written j (by simp [d0]) (by simpa [d0] using hj)
      simpa [d0] using hw
    · simp [List.getElem?_map, List.getElem?_take, hj]
  have htailForm : tail = [] \/ exists marker rest,
      tail = marker :: rest /\ (marker = 69 \/ marker = 101) := by
    cases he : exponent with
    | none => left; exact htailNone he
    | some data =>
      rcases data with ⟨upper, sign, digits⟩
      right
      refine ⟨if upper then 69 else 101,
        (match sign with
          | none => []
          | some negative => [if negative then 45 else 43]) ++
          digits.map (fun z => 48 + z), ?_, ?_⟩
      · exact htailSome upper sign digits he
      · cases upper <;> simp
  have hbound2 : d1.num_digits.val + F.length + tail.length <=
      UScalar.max .Usize := by
    have hsmax : s.val.length <= UScalar.max .Usize := by
      unfold SourceValidByteSlice at hsource
      simp [UScalar.max, Usize.max, Usize.numBits,
        Dec2Flt.configured_x86_64_numBits]
      omega
    have hlen := congrArg List.length hbytes
    simp only [List.length_map, List.length_append, List.length_cons] at hlen
    have hisigle : isig.length <= I.length := by
      simpa [isig] using
        (List.length_dropWhile_le (l := I) (p := fun digit => digit == 0))
    have hd10 : d1.num_digits.val = isig.length := by
      simpa [d0] using hd1count
    rw [hd10]
    omega
  obtain ⟨bulkRest, rest, bulkCount, bulkArray, d2,
    hbulk, hscan2, hrest, hd2count, hd2point, hd2trunc, hd2prefix⟩ :=
    DecimalSeqDigitRunAccumulate afterPoint d1 isig F tail hafterPoint
      hFdigits htailForm hbound2 (by simpa [d0] using hd1count) hd1prefix
  have hd2sigCount : d2.num_digits.val = sig.length := by
    rw [hd2count, hsig]
  have hd2sigPrefix :
      (d2.digits.val.take (min sig.length 768)).map (fun b => b.val) =
        sig.take (min sig.length 768) := by
    simpa [hsig] using hd2prefix
  have hrestTail : rest.val.map (fun b => b.val) = tail := hrest
  have hloop5 :
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop5 afterPoint
        d1.num_digits d1.digits = ok (bulkRest, bulkCount, bulkArray) := by
    rw [DecimalSeqDuplicateLoops.2.1]
    exact hbulk
  have hrun :
      (do
        let (br, count, arr) <-
          num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop5 afterPoint
            d1.num_digits d1.digits
        num.imp.dec2flt.decimal_seq.parse_digits_into_seq br
          { d1 with num_digits := count, digits := arr }) = ok (rest, d2) := by
    rw [hloop5]
    simpa using hscan2
  have hd1ne : d1.num_digits ≠ 0#usize := by
    intro heq
    have hv := congrArg UScalar.val heq
    simp [hd1count, d0, hSigI, isig] at hv
  have hsigNe : sig ≠ [] := by
    rw [hsig]
    simp [hSigI, isig]
  have hsigTrim : sig = trim ++ List.replicate q 0 := by
    simpa [sig, trim] using hsigtrim
  have htrimNe : trim ≠ [] := by
    intro ht
    have hszeros : sig = List.replicate q 0 := by
      simpa [ht] using hsigTrim
    rcases hsigstop with hs0 | hs0
    · exact hsigNe (by simpa [sig] using hs0)
    · cases q with
      | zero => exact hsigNe (by simpa using hszeros)
      | succ q =>
        apply hs0
        simp [sig, hszeros, List.replicate_succ]
  have htrimLast : trim.getLast? ≠ some 0 := by
    rcases htrimstop with ht | ht
    · exact (htrimNe (by simpa [trim] using ht)).elim
    · simpa [trim] using ht
  let A := List.replicate z 0 ++ trim
  have hIFA : I ++ F = A ++ List.replicate q 0 := by
    calc
      I ++ F = List.replicate z 0 ++ sig := by simpa [sig] using hIF
      _ = List.replicate z 0 ++ (trim ++ List.replicate q 0) := by
        rw [hsigTrim]
      _ = (List.replicate z 0 ++ trim) ++ List.replicate q 0 := by
        simp [List.append_assoc]
      _ = A ++ List.replicate q 0 := by rfl
  have hAne : A ≠ [] := by simp [A, htrimNe]
  have hAlast : A.getLast? ≠ some 0 := by
    simpa [A, htrimNe] using htrimLast
  obtain ⟨prefx, suffix, hrend, hprefx', hsuffix, hsuffixCount⟩ :=
    DecimalSeqRenderedSplit I F A q hIFA hAne hAlast
  have hprefx : prefx = [] \/ exists pre b,
      prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46 :=
    Or.inr hprefx'
  have hstart : s.val.map (fun b => b.val) =
      prefx ++ suffix ++ tail := by
    rw [hbytes, hrend]
  have hrestSource : SourceValidByteSlice rest := by
    unfold SourceValidByteSlice at hsource ⊢
    have hslen := congrArg List.length hbytes
    have hrlen := congrArg List.length hrestTail
    simp at hslen hrlen
    omega
  let suffixEval : Std.I32 -> Result Std.I32 :=
    DecimalSeqExponentEval rest
  have hsuffixValue : forall base result,
      suffixEval base = ok result -> result.val = base.val + E := by
    as_aux_lemma =>
    intro base result heval
    apply DecimalSeqExponentResidualPointwise rest exponent E hrestSource
      hExpNe hExpDigits
      (fun he => by simpa using hrestTail.trans (htailNone he))
      (fun upper sign digits he => by
        simpa using hrestTail.trans (htailSome upper sign digits he))
      hENone hESome base result
    simpa [suffixEval] using heval
  have hd2ne : d2.num_digits ≠ 0#usize := by
    intro heq
    have hv := congrArg UScalar.val heq
    simp [hd2sigCount, hsigNe] at hv
  have hd2valNe : d2.num_digits.val ≠ 0 := by
    rw [hd2sigCount]
    simp [hsigNe]
  obtain ⟨base, hb, hcore0⟩ :=
    DecimalSeqFractionalParserCore s s1 pointRest afterPoint bulkRest rest
      d0 d1 d2 d bulkCount bulkArray (by rfl) hlead hscan1 hpointRestNe
      hpointIndex hdropPoint hd1ne hloop5 hscan2 hd2valNe hparse
  let dbase : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { d2 with decimal_point := base }
  obtain ⟨kr, hkr⟩ :=
    DecimalSeqI32HCastCongruence (Slice.len rest)
  obtain ⟨kf, hkf⟩ :=
    DecimalSeqI32HCastCongruence (Slice.len afterPoint)
  have hbaseVal : base.val =
      (UScalar.hcast .I32 (Slice.len rest)).val -
        (UScalar.hcast .I32 (Slice.len afterPoint)).val := by
    have hh := IScalar.sub_equiv
      (UScalar.hcast .I32 (Slice.len rest) : Std.I32)
      (UScalar.hcast .I32 (Slice.len afterPoint) : Std.I32)
    rw [hb] at hh
    exact hh.2.1
  have hlenRest : (Slice.len rest).val = tail.length := by
    have hh := congrArg List.length hrestTail
    simpa [Slice.len] using hh
  have hlenAfter : (Slice.len afterPoint).val =
      F.length + tail.length := by
    have hh := congrArg List.length hafterPoint
    simpa [Slice.len] using hh
  have hscanPoint : exists k : Int,
      dbase.decimal_point.val + (q : Int) + (trim.length : Int) =
        ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) +
          k * 4294967296 := by
    refine ⟨kr - kf, ?_⟩
    simp only [dbase]
    rw [hbaseVal, hkr, hkf, hlenRest, hlenAfter]
    have hst := congrArg List.length hsigTrim
    simp at hst
    have hIFlen := congrArg List.length hIF
    simp at hIFlen
    change I.length + F.length = z + sig.length at hIFlen
    have hz' : (I ++ F).length - sig.length = z := by
      simpa [sig] using hz.symm
    rw [hz']
    push_cast
    ring_nf
    omega
  have hcore :
      (do
        let consumed <- Slice.len s - Slice.len rest
        let trailing <-
          num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed
        let p1 <- dbase.decimal_point + UScalar.hcast .I32 trailing
        let kept <- dbase.num_digits - trailing
        let point <- p1 + UScalar.hcast .I32 kept
        let (nDigits, flag) <-
          if kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
          then ok (num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS, true)
          else ok (kept, dbase.truncated)
        let finalPoint <- suffixEval point
        let a <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
          { start := nDigits,
            «end» :=
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
          dbase.digits
        ok (num.imp.dec2flt.decimal_seq.DecimalSeq.mk
          nDigits finalPoint flag a)) = ok d := by
    simpa only [DecimalSeqFractionalFinish, dbase, suffixEval] using hcore0
  have hfinal : d.num_digits.val = min trim.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trim.take d.num_digits.val /\
      (d.truncated = true <-> 768 < trim.length) /\
      exists k : Int, d.decimal_point.val =
        ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) +
          E + k * 4294967296 := by
    as_aux_lemma =>
    exact DecimalSeqFinalizationExact s rest dbase d sig trim
      prefx suffix tail q
      ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) E
      suffixEval hstart hrestTail hprefx hsuffix hsuffixCount
      (by simpa [dbase] using hd2sigCount) hscanPoint
      (by simpa [dbase, hd2trunc, hd1trunc, d0])
      hsigTrim (by simpa [dbase] using hd2sigPrefix)
      hsuffixValue hcore
  rcases hfinal with ⟨hcount, hdigits, htrunc, k, hk⟩
  exact ⟨⟨hcount, hdigits, htrunc⟩, k, hk⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalDigitTrimDecomposition
import Tablet.DecimalSeqDigitRunAccumulate
import Tablet.DecimalSeqDuplicateLoops
import Tablet.DecimalSeqExponentResidualPointwise
import Tablet.DecimalSeqFinalizationExact
import Tablet.DecimalSeqFractionalParserCore
import Tablet.DecimalSeqLeadingZeroLoopExact
import Tablet.DecimalSeqRenderedSplit
import Tablet.DecimalSeqScalarScanExact
import Tablet.SourceValidByteSlice

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 3000000
set_option maxRecDepth 100000

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqFractionalIntegerState]
theorem DecimalSeqFractionalIntegerState (s : Slice Std.U8) (I F tail : List Nat) (E : Int)
    (exponent : Option (Bool × Option Bool × List Nat))
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (hsource : SourceValidByteSlice s)
    (hIdigits : forall z, z ∈ I -> z < 10)
    (hFdigits : forall z, z ∈ F -> z < 10)
    (hExpNe : forall upper sign digits,
      exponent = some (upper, sign, digits) -> digits ≠ [])
    (hExpDigits : forall upper sign digits,
      exponent = some (upper, sign, digits) ->
        forall z, z ∈ digits -> z < 10)
    (htailNone : exponent = none -> tail = [])
    (htailSome : forall upper sign digits,
      exponent = some (upper, sign, digits) -> tail =
        (if upper then 69 else 101) ::
          (match sign with
          | none => []
          | some negative => [if negative then 45 else 43]) ++
          digits.map (fun z => 48 + z))
    (hENone : exponent = none -> E = 0)
    (hESome : forall upper sign digits,
      exponent = some (upper, sign, digits) -> E =
        let n : Nat := digits.foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0
        match sign with | some true => -(n : Int) | _ => (n : Int))
    (hSigI : I.dropWhile (fun digit => digit == 0) ≠ [])
    (hbytes : s.val.map (fun b => b.val) =
      I.map (fun z => 48 + z) ++ 46 :: F.map (fun z => 48 + z) ++ tail)
    (hparse : num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok d) :
    let significant := (I ++ F).dropWhile (fun digit => digit == 0)
    let trimmed :=
      (significant.reverse.dropWhile (fun digit => digit == 0)).reverse
    let z := (I ++ F).length - significant.length
    let P : Int := (I.length : Int) - z
    (d.num_digits.val = min trimmed.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trimmed.take d.num_digits.val /\
      (d.truncated = true <-> 768 < trimmed.length)) /\
      exists k : Int, d.decimal_point.val = P + E + k * 4294967296 := by
-- BODY
  dsimp only
  let sig := (I ++ F).dropWhile (fun digit => digit == 0)
  let trim := (sig.reverse.dropWhile (fun digit => digit == 0)).reverse
  suffices hgoal : (d.num_digits.val = min trim.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trim.take d.num_digits.val /\
      (d.truncated = true <-> 768 < trim.length)) /\ exists k : Int,
    d.decimal_point.val = (I.length : Int) -
      ((I ++ F).length - sig.length : Nat) + E + k * 4294967296 by
    simpa [sig, trim] using hgoal
  obtain ⟨z, q, hz, hq, hIF, hsigtrim, hsigstop, htrimstop⟩ :=
    DecimalDigitTrimDecomposition (I ++ F)
  obtain ⟨zi, qi, hzi, hqi, hI, hIsigTail, hIsigstop, hItrimstop⟩ :=
    DecimalDigitTrimDecomposition I
  let isig := I.dropWhile (fun digit => digit == 0)
  have hsig : sig = isig ++ F := by
    simp only [sig, isig, List.dropWhile_append]
    simp [hSigI]
  have hzzi : z = zi := by
    have hlenIF := congrArg List.length hIF
    have hlenI := congrArg List.length hI
    have hlenSig := congrArg List.length hsig
    simp at hlenIF hlenI hlenSig
    change (List.dropWhile (fun d => d == 0) (I ++ F)).length =
      (List.dropWhile (fun d => d == 0) I).length + F.length at hlenSig
    omega
  have hIbytes : I.map (fun n => 48 + n) =
      List.replicate zi 48 ++ isig.map (fun n => 48 + n) := by
    rw [hI, List.map_append]
    simp [isig]
  have hslead : s.val.map (fun b => b.val) =
      List.replicate zi 48 ++ isig.map (fun n => 48 + n) ++
        46 :: F.map (fun n => 48 + n) ++ tail := by
    rw [hbytes, hIbytes]
  have hleadstop :
      (isig.map (fun n => 48 + n) ++
        46 :: F.map (fun n => 48 + n) ++ tail)[0]? ≠ some 48 := by
    rcases hIsigstop with hempty | hstop
    · exact (hSigI (by simpa [isig] using hempty)).elim
    · intro heq
      apply hstop
      cases hisig : isig with
      | nil => exact (hSigI (by simpa [isig] using hisig)).elim
      | cons a restI =>
        have ha : a = 0 := by simp [hisig] at heq; omega
        subst a
        simp [isig, hisig]
  obtain ⟨s1, hlead, hs1⟩ := DecimalSeqLeadingZeroLoopExact s zi
    (isig.map (fun n => 48 + n) ++
      46 :: F.map (fun n => 48 + n) ++ tail)
    (by simpa [List.append_assoc] using hslead) (Or.inr hleadstop)
  let d0 : num.imp.dec2flt.decimal_seq.DecimalSeq := {
    num_digits := 0#usize
    decimal_point := 0#i32
    truncated := false
    digits := Array.repeat 768#usize 0#u8 }
  have hbound1 : isig.length <= UScalar.max .Usize := by
    unfold SourceValidByteSlice at hsource
    have hlen := congrArg List.length hslead
    simp at hlen
    simp [UScalar.max, Usize.max, Usize.numBits,
      Dec2Flt.configured_x86_64_numBits]
    omega
  obtain ⟨pointRest, d1, hscan1, hpointRest, hd1count, hd1point,
    hd1trunc, hd1old, hd1written, hd1outside⟩ :=
    DecimalSeqScalarScanExact s1 d0 isig
      (46 :: F.map (fun n => 48 + n) ++ tail)
      (by simpa [List.append_assoc] using hs1)
      (fun n hn => hIdigits n (by rw [hI]; exact List.mem_append_right _ hn))
      (by simp) (by simpa [d0] using hbound1)
  have hpointRestNe : pointRest.val ≠ [] := by
    intro he
    have hh := congrArg List.length hpointRest
    simp [he] at hh
  have hpointIndex : Slice.index_usize pointRest 0#usize = ok 46#u8 := by
    cases hr : pointRest.val with
    | nil => exact (hpointRestNe hr).elim
    | cons b bs =>
      have hbval : b.val = 46 := by
        have hh := congrArg List.head? hpointRest
        simp [hr] at hh
        exact hh
      have hb : b = 46#u8 := UScalar.eq_of_val_eq hbval
      simp [Slice.index_usize, hr, hb]
  let afterPoint := Slice.drop pointRest 1#usize
  have hdropPoint :
      core.slice.index.SliceIndexRangeFromUsizeSlice.index
        { start := 1#usize } pointRest = ok afterPoint := by
    simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
      Slice.length, afterPoint, hpointRestNe]
  have hafterPoint : afterPoint.val.map (fun b => b.val) =
      F.map (fun n => 48 + n) ++ tail := by
    simp [afterPoint, Slice.drop, List.map_drop, hpointRest]
  have hd1prefix :
      (d1.digits.val.take (min isig.length 768)).map (fun b => b.val) =
        isig.take (min isig.length 768) := by
    apply List.ext_getElem?
    intro j
    by_cases hj : j < min isig.length 768
    · rw [List.getElem?_map, List.getElem?_take, List.getElem?_take]
      simp only [hj, if_true]
      have hw := hd1written j (by simp [d0]) (by simpa [d0] using hj)
      simpa [d0] using hw
    · simp [List.getElem?_map, List.getElem?_take, hj]
  have htailForm : tail = [] \/ exists marker rest,
      tail = marker :: rest /\ (marker = 69 \/ marker = 101) := by
    cases he : exponent with
    | none => left; exact htailNone he
    | some data =>
      rcases data with ⟨upper, sign, digits⟩
      right
      refine ⟨if upper then 69 else 101,
        (match sign with
          | none => []
          | some negative => [if negative then 45 else 43]) ++
          digits.map (fun z => 48 + z), ?_, ?_⟩
      · exact htailSome upper sign digits he
      · cases upper <;> simp
  have hbound2 : d1.num_digits.val + F.length + tail.length <=
      UScalar.max .Usize := by
    have hsmax : s.val.length <= UScalar.max .Usize := by
      unfold SourceValidByteSlice at hsource
      simp [UScalar.max, Usize.max, Usize.numBits,
        Dec2Flt.configured_x86_64_numBits]
      omega
    have hlen := congrArg List.length hbytes
    simp only [List.length_map, List.length_append, List.length_cons] at hlen
    have hisigle : isig.length <= I.length := by
      simpa [isig] using
        (List.length_dropWhile_le (l := I) (p := fun digit => digit == 0))
    have hd10 : d1.num_digits.val = isig.length := by
      simpa [d0] using hd1count
    rw [hd10]
    omega
  obtain ⟨bulkRest, rest, bulkCount, bulkArray, d2,
    hbulk, hscan2, hrest, hd2count, hd2point, hd2trunc, hd2prefix⟩ :=
    DecimalSeqDigitRunAccumulate afterPoint d1 isig F tail hafterPoint
      hFdigits htailForm hbound2 (by simpa [d0] using hd1count) hd1prefix
  have hd2sigCount : d2.num_digits.val = sig.length := by
    rw [hd2count, hsig]
  have hd2sigPrefix :
      (d2.digits.val.take (min sig.length 768)).map (fun b => b.val) =
        sig.take (min sig.length 768) := by
    simpa [hsig] using hd2prefix
  have hrestTail : rest.val.map (fun b => b.val) = tail := hrest
  have hloop5 :
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop5 afterPoint
        d1.num_digits d1.digits = ok (bulkRest, bulkCount, bulkArray) := by
    rw [DecimalSeqDuplicateLoops.2.1]
    exact hbulk
  have hrun :
      (do
        let (br, count, arr) <-
          num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop5 afterPoint
            d1.num_digits d1.digits
        num.imp.dec2flt.decimal_seq.parse_digits_into_seq br
          { d1 with num_digits := count, digits := arr }) = ok (rest, d2) := by
    rw [hloop5]
    simpa using hscan2
  have hd1ne : d1.num_digits ≠ 0#usize := by
    intro heq
    have hv := congrArg UScalar.val heq
    simp [hd1count, d0, hSigI, isig] at hv
  have hsigNe : sig ≠ [] := by
    rw [hsig]
    simp [hSigI, isig]
  have hsigTrim : sig = trim ++ List.replicate q 0 := by
    simpa [sig, trim] using hsigtrim
  have htrimNe : trim ≠ [] := by
    intro ht
    have hszeros : sig = List.replicate q 0 := by
      simpa [ht] using hsigTrim
    rcases hsigstop with hs0 | hs0
    · exact hsigNe (by simpa [sig] using hs0)
    · cases q with
      | zero => exact hsigNe (by simpa using hszeros)
      | succ q =>
        apply hs0
        simp [sig, hszeros, List.replicate_succ]
  have htrimLast : trim.getLast? ≠ some 0 := by
    rcases htrimstop with ht | ht
    · exact (htrimNe (by simpa [trim] using ht)).elim
    · simpa [trim] using ht
  let A := List.replicate z 0 ++ trim
  have hIFA : I ++ F = A ++ List.replicate q 0 := by
    calc
      I ++ F = List.replicate z 0 ++ sig := by simpa [sig] using hIF
      _ = List.replicate z 0 ++ (trim ++ List.replicate q 0) := by
        rw [hsigTrim]
      _ = (List.replicate z 0 ++ trim) ++ List.replicate q 0 := by
        simp [List.append_assoc]
      _ = A ++ List.replicate q 0 := by rfl
  have hAne : A ≠ [] := by simp [A, htrimNe]
  have hAlast : A.getLast? ≠ some 0 := by
    simpa [A, htrimNe] using htrimLast
  obtain ⟨prefx, suffix, hrend, hprefx', hsuffix, hsuffixCount⟩ :=
    DecimalSeqRenderedSplit I F A q hIFA hAne hAlast
  have hprefx : prefx = [] \/ exists pre b,
      prefx = pre ++ [b] /\ b ≠ 48 /\ b ≠ 46 :=
    Or.inr hprefx'
  have hstart : s.val.map (fun b => b.val) =
      prefx ++ suffix ++ tail := by
    rw [hbytes, hrend]
  have hrestSource : SourceValidByteSlice rest := by
    unfold SourceValidByteSlice at hsource ⊢
    have hslen := congrArg List.length hbytes
    have hrlen := congrArg List.length hrestTail
    simp at hslen hrlen
    omega
  let suffixEval : Std.I32 -> Result Std.I32 :=
    DecimalSeqExponentEval rest
  have hsuffixValue : forall base result,
      suffixEval base = ok result -> result.val = base.val + E := by
    as_aux_lemma =>
    intro base result heval
    apply DecimalSeqExponentResidualPointwise rest exponent E hrestSource
      hExpNe hExpDigits
      (fun he => by simpa using hrestTail.trans (htailNone he))
      (fun upper sign digits he => by
        simpa using hrestTail.trans (htailSome upper sign digits he))
      hENone hESome base result
    simpa [suffixEval] using heval
  have hd2ne : d2.num_digits ≠ 0#usize := by
    intro heq
    have hv := congrArg UScalar.val heq
    simp [hd2sigCount, hsigNe] at hv
  have hd2valNe : d2.num_digits.val ≠ 0 := by
    rw [hd2sigCount]
    simp [hsigNe]
  obtain ⟨base, hb, hcore0⟩ :=
    DecimalSeqFractionalParserCore s s1 pointRest afterPoint bulkRest rest
      d0 d1 d2 d bulkCount bulkArray (by rfl) hlead hscan1 hpointRestNe
      hpointIndex hdropPoint hd1ne hloop5 hscan2 hd2valNe hparse
  let dbase : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { d2 with decimal_point := base }
  obtain ⟨kr, hkr⟩ :=
    DecimalSeqI32HCastCongruence (Slice.len rest)
  obtain ⟨kf, hkf⟩ :=
    DecimalSeqI32HCastCongruence (Slice.len afterPoint)
  have hbaseVal : base.val =
      (UScalar.hcast .I32 (Slice.len rest)).val -
        (UScalar.hcast .I32 (Slice.len afterPoint)).val := by
    have hh := IScalar.sub_equiv
      (UScalar.hcast .I32 (Slice.len rest) : Std.I32)
      (UScalar.hcast .I32 (Slice.len afterPoint) : Std.I32)
    rw [hb] at hh
    exact hh.2.1
  have hlenRest : (Slice.len rest).val = tail.length := by
    have hh := congrArg List.length hrestTail
    simpa [Slice.len] using hh
  have hlenAfter : (Slice.len afterPoint).val =
      F.length + tail.length := by
    have hh := congrArg List.length hafterPoint
    simpa [Slice.len] using hh
  have hscanPoint : exists k : Int,
      dbase.decimal_point.val + (q : Int) + (trim.length : Int) =
        ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) +
          k * 4294967296 := by
    refine ⟨kr - kf, ?_⟩
    simp only [dbase]
    rw [hbaseVal, hkr, hkf, hlenRest, hlenAfter]
    have hst := congrArg List.length hsigTrim
    simp at hst
    have hIFlen := congrArg List.length hIF
    simp at hIFlen
    change I.length + F.length = z + sig.length at hIFlen
    have hz' : (I ++ F).length - sig.length = z := by
      simpa [sig] using hz.symm
    rw [hz']
    push_cast
    ring_nf
    omega
  have hcore :
      (do
        let consumed <- Slice.len s - Slice.len rest
        let trailing <-
          num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed
        let p1 <- dbase.decimal_point + UScalar.hcast .I32 trailing
        let kept <- dbase.num_digits - trailing
        let point <- p1 + UScalar.hcast .I32 kept
        let (nDigits, flag) <-
          if kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
          then ok (num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS, true)
          else ok (kept, dbase.truncated)
        let finalPoint <- suffixEval point
        let a <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
          { start := nDigits,
            «end» :=
              num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
          dbase.digits
        ok (num.imp.dec2flt.decimal_seq.DecimalSeq.mk
          nDigits finalPoint flag a)) = ok d := by
    simpa only [DecimalSeqFractionalFinish, dbase, suffixEval] using hcore0
  have hfinal : d.num_digits.val = min trim.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trim.take d.num_digits.val /\
      (d.truncated = true <-> 768 < trim.length) /\
      exists k : Int, d.decimal_point.val =
        ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) +
          E + k * 4294967296 := by
    as_aux_lemma =>
    exact DecimalSeqFinalizationExact s rest dbase d sig trim
      prefx suffix tail q
      ((I.length : Int) - ((I ++ F).length - sig.length : Nat)) E
      suffixEval hstart hrestTail hprefx hsuffix hsuffixCount
      (by simpa [dbase] using hd2sigCount) hscanPoint
      (by simpa [dbase, hd2trunc, hd1trunc, d0])
      hsigTrim (by simpa [dbase] using hd2sigPrefix)
      hsuffixValue hcore
  rcases hfinal with ⟨hcount, hdigits, htrunc, k, hk⟩
  exact ⟨⟨hcount, hdigits, htrunc⟩, k, hk⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (77)
More program dependencies (44)
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 49 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqFractionalIntegerState",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "0e88ff5471fe7e048f0a22bdb26acc651a3f6c626881940f1f309aa91132ddd3",
  "active_statement_hash": "ce1ec71c1bf5aa4e6df5b5b19a1d63f33f2b9b2ade2be9d3e9a9a80276db92fb",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.configured_x86_64_numBits",
    "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-170",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalDigitTrimDecomposition": "74d5424585de54f0",
    "DecimalSeqDigitRunAccumulate": "6a6ae3cd643a92d6",
    "DecimalSeqDuplicateLoops": "4a7630ac67b4834a",
    "DecimalSeqExponentResidualPointwise": "d932a7306198e523",
    "DecimalSeqFinalizationExact": "d3b883652a1f053b",
    "DecimalSeqFractionalFinish": "0eb6fb2627d0ff5c",
    "DecimalSeqFractionalParserCore": "c4aee42c861a8ecf",
    "DecimalSeqI32HCastCongruence": "bedcc032e9d40104",
    "DecimalSeqLeadingZeroLoopExact": "afa2b1ae59f7e9a8",
    "DecimalSeqRenderedSplit": "8d4db985a4bb4317",
    "DecimalSeqScalarScanExact": "a9ff88a04accf0ed"
  },
  "strict_definition_deps": {
    "DecimalSeqExponentEval": "b51b6389c17b6c1f",
    "DecimalSeqExponentResidualPointwise": "28ca0a8ac503ee01",
    "DecimalSeqFractionalFinish": "691437a277bc5cda",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "6799fe6525ddb761",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_Insts_CoreDefaultDefault_default": "7b9921904495bc2a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "65634ac95536dcca",
    "num_imp_dec2flt_decimal_seq_count_trailing_zeros": "7a77bc9e01c1a09b",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "ec91b296e225e006",
    "num_imp_dec2flt_decimal_seq_parse_digits_into_seq": "8b2cc3a350f40ce1"
  }
}
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:17cae2bb601ab4a0df8e641a9605725c184ba6a77f2678ccbb914597f24eae33",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}