P800_T1_ParserFactorization

Lean checked

Statement

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

Lemma · Branchwise parser factorization

For every byte slice s, the extracted call \texttt{parse\_decimal\_seq}(s) equals the following Result computation, including its failure and divergence outcomes. First execute the scan computation Q(s) below and obtain (u,a,f); then execute the finishing computation R(s,u,a,f) below.
The computation Q(s) first calls DecimalSeq's default constructor, obtaining d, calls \texttt{parse\_decimal\_seq\_loop0}(s), obtaining s_1, and calls \texttt{parse\_digits\_into\_seq}(s_1,d), obtaining (s_2,d_1). It calls \texttt{is\_empty}(s_2). If true, it returns (s_2,d_1,0\texttt{\#usize}). Otherwise it reads index zero of s_2. If that byte is not 46\texttt{\#u8}, it returns the same triple. If it is 46\texttt{\#u8}, it obtains s_3 by the extracted RangeFrom-Usize slice index with start one. When d_1.\mathit{num\_digits}=0, it calls loop 2 on s_3, then loop 3 on the returned slice with d_1.\mathit{num\_digits} and d_1.\mathit{digits}. Otherwise it calls loop 5 directly on s_3 with those same operands. Each loop 3 or 5 returns a triple (v,n,A). In either case it calls \texttt{parse\_digits\_into\_seq}(v, \{d_1\text{ with count }n\text{ and digits }A\}), obtaining (u,a); it computes the checked difference f=\texttt{len}(s_3)-\texttt{len}(u) and returns (u,a,f).
Let E(u) call \texttt{is\_empty}(u) and return I64 zero when true. Otherwise it reads index zero. For byte 101 or 69 it obtains the RangeFrom-Usize suffix with start one, calls \texttt{parse\_scientific}, and returns its optional exponent unwrapped with I64-zero default; for every other byte it returns I64 zero. The 101 test precedes the 69 test.
The computation R(s,u,a,f) first selects (r,b) as follows. If a.\mathit{num\_digits}\ne0, compute the checked difference c=\texttt{len}(s)-\texttt{len}(u), call \texttt{count\_trailing\_zeros}(s,c) to obtain z, and compute the checked difference k=a.\mathit{num\_digits}-z. If k>\texttt{MAX\_DIGITS}, select (\texttt{MAX\_DIGITS},\texttt{true}); otherwise select (k,a.\mathit{truncated}). When the scan count is zero, select (a.\mathit{num\_digits},a.\mathit{truncated}) without either difference or the reverse scan. Next call E(u), obtaining e; call \texttt{final\_decimal\_point}(a.\mathit{num\_digits},f,e), obtaining p; call loop 1 with range [r,\texttt{MAX\_DIGITS\_WITHOUT\_OVERFLOW}) and array a.\mathit{digits}, obtaining B; and return \{\mathit{num\_digits}=r,\mathit{decimal\_point}=p, \mathit{truncated}=b,\mathit{digits}=B\}. All computations above use sequential Result binds, aborting on failure or divergence. In particular the saved operand of the final point call is the untrimmed count of a, not r.

Lean statement

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

import Tablet.DecimalSeqDuplicateLoops

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

-- [TABLET NODE: P800_T1_ParserFactorization]
theorem P800_T1_ParserFactorization (s : Slice Std.U8) :
    let scanCall : Result (Slice Std.U8 ×
        num.imp.dec2flt.decimal_seq.DecimalSeq × Std.Usize) := do
      let d <- num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default
      let s1 <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s
      let (s2, d1) <- num.imp.dec2flt.decimal_seq.parse_digits_into_seq s1 d
      let empty <- core.slice.Slice.is_empty s2
      if empty then ok (s2, d1, 0#usize) else
        let ch <- Slice.index_usize s2 0#usize
        if ch = 46#u8 then
          let s3 <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s2
            { start := 1#usize }
          let (rest, scan) <-
            if d1.num_digits = 0#usize then do
              let s4 <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop2 s3
              let (s5, n, a) <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop3
                s4 d1.num_digits d1.digits
              num.imp.dec2flt.decimal_seq.parse_digits_into_seq s5
                { d1 with num_digits := n, digits := a }
            else do
              let (s4, n, a) <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop5
                s3 d1.num_digits d1.digits
              num.imp.dec2flt.decimal_seq.parse_digits_into_seq s4
                { d1 with num_digits := n, digits := a }
          let f <- Slice.len s3 - Slice.len rest
          ok (rest, scan, f)
        else ok (s2, d1, 0#usize)
    let explicitCall (rest : Slice Std.U8) : Result Std.I64 := do
      let empty <- core.slice.Slice.is_empty rest
      if empty then ok 0#i64 else
        let ch <- Slice.index_usize rest 0#usize
        if ch = 101#u8 then do
          let suffix <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) rest
            { 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) rest
            { 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
    let finish (rest : Slice Std.U8)
        (scan : num.imp.dec2flt.decimal_seq.DecimalSeq) (f : Std.Usize) :
        Result num.imp.dec2flt.decimal_seq.DecimalSeq := do
      let (r, flag) <-
        if scan.num_digits != 0#usize then do
          let consumed <- Slice.len s - Slice.len rest
          let zeros <- num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed
          let kept <- scan.num_digits - zeros
          if kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS then
            ok (num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS, true)
          else ok (kept, scan.truncated)
        else ok (scan.num_digits, scan.truncated)
      let e <- explicitCall rest
      let point <- num.imp.dec2flt.decimal_seq.final_decimal_point scan.num_digits f e
      let a <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := r, «end» :=
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        scan.digits
      ok { num_digits := r, decimal_point := point, truncated := flag, digits := a }
    num.imp.dec2flt.decimal_seq.parse_decimal_seq s = (do
      let (rest, scan, f) <- scanCall
      finish rest scan f) := 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

Unfold num imp dec2flt decimal seq parse decimal seq. Associate Result binds to the right. This identity holds for all Results: for \texttt{ok}\ v both sides substitute v into the first continuation, for \texttt{fail}\ t both sides return that same failure, and for \texttt{div} both sides diverge. A bind of an explicitly returned tuple substitutes its components into its continuation. These identities let us compare the continuations after the common default, loop 0, and integer digit-scan calls without assuming those calls terminate normally. If any fails or diverges, both whole computations already have that identical result. The shared Aeneas slice primitives are also explicit: the empty test returns \texttt{ok}(|u.\mathit{val}|=0), and index-Usize matches the optional list entry, returning its value when present and array-out-of-bounds failure when absent. Thus the empty and head observations below require no parser correctness lemma or additional input premise.
Fix an arbitrary returned pair (s_2,d_1). There are two possible list shapes for s_2.\mathit{val}. If it is empty, the primitive empty test normally returns true. The source's first arm selects the trim/cap pair for (s,s_2,d_1) and its explicit-exponent conditional immediately returns I64 zero. The scan computation returns (s_2,d_1,0). In R, the repeated empty test on exactly s_2 also normally returns true and E(s_2) returns that same zero. Substituting these values leaves exactly the source's point call, loop 1, and record construction, with identical operands and sequencing.
If s_2.\mathit{val}=h::t, the primitive empty test normally returns false and primitive index zero normally returns h. These are direct consequences of the slice primitives' list meanings, with index bound 0<|h::t|. Split on h=46\texttt{\#u8}. If false, Q returns (s_2,d_1,0). The source's last arm selects exactly the trim/cap pair in R, then tests the already read h for 101 and 69. The fresh empty and index calls of E(s_2) normally return false and the same h. After those substitutions its marker tests, suffix indexing, scientific call, and unwrap are identical to the source's exponent computation, including every possible abnormal result of those calls. The loop 7 equality in DecimalSeqDuplicateLoops replaces the source fill by loop 1 on the identical range and array. The point operands and resulting record therefore agree.
If h=46\texttt{\#u8}, both sides next execute exactly the same RangeFrom-Usize index call yielding s_3. Again bind congruence handles abnormal results without a normality assumption. For a normal result, split on d_1.\mathit{num\_digits}=0. In the zero case both sides execute loop 2, loop 3, and the scalar digit scan with the same updated record. Associate binds past these calls and substitute the components of their returned triples and pairs. Both sides then compute the same full fractional difference \texttt{len}(s_3)-\texttt{len}(u), followed by the same trim/cap selection and the same fresh empty test and exponent computation on u. The loop 4 equality in DecimalSeqDuplicateLoops identifies the fill with loop 1. All other operands and the record agree literally. In the nonzero case both sides instead execute loop 5 and the scalar digit scan, followed by that same difference and finishing sequence. The loop 6 equality in DecimalSeqDuplicateLoops identifies the remaining fill. Tuple substitution and bind associativity give the same record here as well.
No call has been moved across another possibly abnormal call: in each arm trim/cap still precedes exponent parsing, which precedes the saved-count point call and fill. Only repeated primitive observations with the proved normal values were eliminated. The equalities in DecimalSeqDuplicateLoops also hold for every Result, not just normal arrays. Thus the four branch comparisons prove the claimed unconditional Result equality, including failure and divergence.

Lean proof


  dsimp only
  unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq
  simp only [bind_assoc_eq]
  congr 1
  funext d
  congr 1
  funext s1
  congr 1
  funext p
  rcases p with ⟨s2, d1⟩
  simp only [uncurry_apply_pair]
  cases hs2 : s2.val with
  | nil =>
      have hempty : core.slice.Slice.is_empty s2 = ok true := by
        simp [core.slice.Slice.is_empty, hs2]
      simp only [hempty, bind_tc_ok, uncurry_apply_pair, if_true]
  | cons ch cs =>
      have hempty : core.slice.Slice.is_empty s2 = ok false := by
        simp [core.slice.Slice.is_empty, hs2]
      have hhead : Slice.index_usize s2 0#usize = ok ch := by
        simp [Slice.index_usize, hs2]
      simp only [hempty, hhead, bind_tc_ok, Bool.false_eq_true, if_false]
      by_cases hpoint : ch = 46#u8
      · simp only [hpoint, if_true, bind_assoc_eq]
        congr 1
        funext s3
        by_cases hzero : d1.num_digits = 0#usize
        · simp only [hzero, if_true, bind_assoc_eq]
          apply bind_congr
          intro s4
          apply bind_congr
          rintro ⟨s5, n, a⟩
          simp only [uncurry_apply_pair]
          apply bind_congr
          rintro ⟨rest, scan⟩
          simp only [uncurry_apply_pair, bind_assoc_eq, bind_tc_ok,
            DecimalSeqDuplicateLoops.2.2.1]
        · simp only [hzero, if_false, bind_assoc_eq]
          apply bind_congr
          rintro ⟨s4, n, a⟩
          simp only [uncurry_apply_pair]
          apply bind_congr
          rintro ⟨rest, scan⟩
          simp only [uncurry_apply_pair, bind_assoc_eq, bind_tc_ok,
            DecimalSeqDuplicateLoops.2.2.2.1]
      · simp only [hpoint, if_false, bind_tc_ok, uncurry_apply_pair, hempty, hhead,
          Bool.false_eq_true,
          DecimalSeqDuplicateLoops.2.2.2.2]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqDuplicateLoops

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

-- [TABLET NODE: P800_T1_ParserFactorization]
theorem P800_T1_ParserFactorization (s : Slice Std.U8) :
    let scanCall : Result (Slice Std.U8 ×
        num.imp.dec2flt.decimal_seq.DecimalSeq × Std.Usize) := do
      let d <- num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default
      let s1 <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s
      let (s2, d1) <- num.imp.dec2flt.decimal_seq.parse_digits_into_seq s1 d
      let empty <- core.slice.Slice.is_empty s2
      if empty then ok (s2, d1, 0#usize) else
        let ch <- Slice.index_usize s2 0#usize
        if ch = 46#u8 then
          let s3 <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s2
            { start := 1#usize }
          let (rest, scan) <-
            if d1.num_digits = 0#usize then do
              let s4 <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop2 s3
              let (s5, n, a) <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop3
                s4 d1.num_digits d1.digits
              num.imp.dec2flt.decimal_seq.parse_digits_into_seq s5
                { d1 with num_digits := n, digits := a }
            else do
              let (s4, n, a) <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop5
                s3 d1.num_digits d1.digits
              num.imp.dec2flt.decimal_seq.parse_digits_into_seq s4
                { d1 with num_digits := n, digits := a }
          let f <- Slice.len s3 - Slice.len rest
          ok (rest, scan, f)
        else ok (s2, d1, 0#usize)
    let explicitCall (rest : Slice Std.U8) : Result Std.I64 := do
      let empty <- core.slice.Slice.is_empty rest
      if empty then ok 0#i64 else
        let ch <- Slice.index_usize rest 0#usize
        if ch = 101#u8 then do
          let suffix <- core.slice.index.Slice.index
            (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) rest
            { 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) rest
            { 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
    let finish (rest : Slice Std.U8)
        (scan : num.imp.dec2flt.decimal_seq.DecimalSeq) (f : Std.Usize) :
        Result num.imp.dec2flt.decimal_seq.DecimalSeq := do
      let (r, flag) <-
        if scan.num_digits != 0#usize then do
          let consumed <- Slice.len s - Slice.len rest
          let zeros <- num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed
          let kept <- scan.num_digits - zeros
          if kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS then
            ok (num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS, true)
          else ok (kept, scan.truncated)
        else ok (scan.num_digits, scan.truncated)
      let e <- explicitCall rest
      let point <- num.imp.dec2flt.decimal_seq.final_decimal_point scan.num_digits f e
      let a <- num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := r, «end» :=
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        scan.digits
      ok { num_digits := r, decimal_point := point, truncated := flag, digits := a }
    num.imp.dec2flt.decimal_seq.parse_decimal_seq s = (do
      let (rest, scan, f) <- scanCall
      finish rest scan f) := by
-- BODY
  dsimp only
  unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq
  simp only [bind_assoc_eq]
  congr 1
  funext d
  congr 1
  funext s1
  congr 1
  funext p
  rcases p with ⟨s2, d1⟩
  simp only [uncurry_apply_pair]
  cases hs2 : s2.val with
  | nil =>
      have hempty : core.slice.Slice.is_empty s2 = ok true := by
        simp [core.slice.Slice.is_empty, hs2]
      simp only [hempty, bind_tc_ok, uncurry_apply_pair, if_true]
  | cons ch cs =>
      have hempty : core.slice.Slice.is_empty s2 = ok false := by
        simp [core.slice.Slice.is_empty, hs2]
      have hhead : Slice.index_usize s2 0#usize = ok ch := by
        simp [Slice.index_usize, hs2]
      simp only [hempty, hhead, bind_tc_ok, Bool.false_eq_true, if_false]
      by_cases hpoint : ch = 46#u8
      · simp only [hpoint, if_true, bind_assoc_eq]
        congr 1
        funext s3
        by_cases hzero : d1.num_digits = 0#usize
        · simp only [hzero, if_true, bind_assoc_eq]
          apply bind_congr
          intro s4
          apply bind_congr
          rintro ⟨s5, n, a⟩
          simp only [uncurry_apply_pair]
          apply bind_congr
          rintro ⟨rest, scan⟩
          simp only [uncurry_apply_pair, bind_assoc_eq, bind_tc_ok,
            DecimalSeqDuplicateLoops.2.2.1]
        · simp only [hzero, if_false, bind_assoc_eq]
          apply bind_congr
          rintro ⟨s4, n, a⟩
          simp only [uncurry_apply_pair]
          apply bind_congr
          rintro ⟨rest, scan⟩
          simp only [uncurry_apply_pair, bind_assoc_eq, bind_tc_ok,
            DecimalSeqDuplicateLoops.2.2.2.1]
      · simp only [hpoint, if_false, bind_tc_ok, uncurry_apply_pair, hempty, hhead,
          Bool.false_eq_true,
          DecimalSeqDuplicateLoops.2.2.2.2]

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (79)
More program dependencies (46)
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 45 entries
Technical checking records
Local closure summary
{
  "node": "P800_T1_ParserFactorization",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "bf8b60ffe44794917abf2dd53caca88901dab0218cbfce9a3de005ac5ae2c626",
  "active_statement_hash": "a663ce84702b8e21f15e2c3009fe4dbc97f98ea813ea28728ca8dc8cea041899",
  "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-189",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalSeqDuplicateLoops": "4a7630ac67b4834a",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "004e715471c389b8"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "13e3cec939d02f51",
    "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_final_decimal_point": "4f8088dec06d629a",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "9f2c4f5675cad683",
    "num_imp_dec2flt_decimal_seq_parse_digits_into_seq": "8b2cc3a350f40ce1",
    "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:3c19d581519121f081a9f0d6b5851252afb384999004d3ce4188a2b036a9ecd5",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}