P495_T1_FinalizationExact

Lean checked

Statement

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

Lemma · Saved-count finalization and exact fill

Let a be a scanned record, n=a.\mathit{num\_digits.val}=|C|, and let f:\texttt{Usize}, e:\texttt{I64}, c,z:\texttt{Usize}. Assume n,f.\mathit{val}\le2^{63}-1, false scan sticky flag, C=T\mathbin{++}0^{z.\mathit{val}}, and a normal length-difference equation \texttt{len}(s)-\texttt{len}(u)=\texttt{ok}\ c. When C\ne[], assume the actual reverse-count call on (s,c) returns \texttt{ok}\ z. Assume the first \min(|C|,768) scan cells have value list \operatorname{take}(\min(|C|,768),C), and all 768 scan cell values are below ten.
The following computation returns normally with d: if the scan count is nonzero, compute that length difference and reverse count, subtract the count from the scan count, then cap at 768, setting sticky true exactly when the uncapped result exceeds 768; if the scan count is zero, retain its count and flag. Call final_decimal_point with the ORIGINAL scan count, f,e, fill loop 1 from the selected count to 19, and construct the result from that count, point, flag, and array.
Its actual finalizer call equals \texttt{ok}\ d.\mathit{decimal\_point}. Its point value is zero when |C|=0 and otherwise \max(-2047,\min(2047,|C|-f.\mathit{val}+e.\mathit{val})). Its retained count is r=\min(|T|,768), retained value list is \operatorname{take}(r,T), and sticky is true iff 768<|T|. The first r actual U8 scan cells are preserved by filling. For every j<768, the output cell is U8 zero on r\le j<19, and the scan cell otherwise. All output cell values are below ten.

Lean statement

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

import Tablet.P495_T1_FillExact
import Tablet.P9_T1_FinalPointExact

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

-- [TABLET NODE: P495_T1_FinalizationExact]
theorem P495_T1_FinalizationExact (start rest : Slice Std.U8)
    (scan : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (f : Std.Usize) (e : Std.I64) (consumed zeros : Std.Usize)
    (C T : List Nat)
    (hcount : scan.num_digits.val = C.length)
    (hn : scan.num_digits.val <= 9223372036854775807)
    (hf : f.val <= 9223372036854775807)
    (hflag : scan.truncated = false)
    (hsplit : C = T ++ List.replicate zeros.val 0)
    (hconsumed : Slice.len start - Slice.len rest = ok consumed)
    (hzeros : C ≠ [] ->
      num.imp.dec2flt.decimal_seq.count_trailing_zeros start consumed = ok zeros)
    (hstored : (scan.digits.val.take (min C.length 768)).map (fun b => b.val) =
      C.take (min C.length 768))
    (hdigits : forall b, b ∈ scan.digits.val -> b.val < 10) :
    let finish : Result num.imp.dec2flt.decimal_seq.DecimalSeq := do
      let (r, flag) <-
        if scan.num_digits != 0#usize then do
          let consumed <- Slice.len start - Slice.len rest
          let zeros <- num.imp.dec2flt.decimal_seq.count_trailing_zeros start 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 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 }
    exists d : num.imp.dec2flt.decimal_seq.DecimalSeq,
      finish = ok d /\
      num.imp.dec2flt.decimal_seq.final_decimal_point scan.num_digits f e =
        ok d.decimal_point /\
      d.decimal_point.val = (if C.length = 0 then 0 else
        max (-2047) (min 2047 ((C.length : Int) - f.val + e.val))) /\
      d.num_digits.val = min T.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun b => b.val) =
        T.take d.num_digits.val /\
      (d.truncated = true <-> 768 < T.length) /\
      d.digits.val.take d.num_digits.val = scan.digits.val.take d.num_digits.val /\
      (forall j, j < 768 -> d.digits.val[j]? =
        if d.num_digits.val <= j /\ j < 19 then some 0#u8 else scan.digits.val[j]?) /\
      (forall b, b ∈ d.digits.val -> b.val < 10) := 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

Use the finalization operations in num imp dec2flt decimal seq parse decimal seq. If C=[], the split implies T=[] and z.\mathit{val}=0. The scan count is Usize zero by value injectivity, so the branch retains count zero and false flag, exactly \min(|T|,768) and the required false equivalence. There is no reverse-count call in this branch; the conditional hypothesis about that call is not used.
If C\ne[], its length is positive and the source takes the nonzero arm. The two supplied actual call equations identify the length difference and reverse-count results. Taking lengths of C=T\mathbin{++}0^{z.\mathit{val}} gives |C|=|T|+z.\mathit{val}. Thus checked Usize subtraction is safe and returns a kept count of exactly |T|. By the pinned MAX_DIGITS constant num imp dec2flt decimal seq DecimalSeq MAX DIGITS, comparison against the cap is exactly comparison against 768. The cap branch yields count 768 and true flag when |T|>768; otherwise it yields |T| and the input false flag. Hence count r and the sticky equivalence hold.
In either branch, FinalPointExact applies to the original scan count and f, with their stated bounds, and to the arbitrary I64 e. It supplies the NORMAL actual finalizer result and exactly the point formula. No bound on n-f+e is assumed; that principal accounts for signed saturation before clamping. Its operand is n, not r or |T|.
Since r\le|T|\le|C| and r\le768, taking r entries of the stored-prefix equation gives the scan's first r values equal to \operatorname{take}(r,C)=\operatorname{take}(r,T), using the split. Apply FillExact to selected count r and the scan array. It supplies the normal fill result, preserves precisely the first r U8 cells, gives the displayed cell-by-cell equation, and propagates the all-cell digit bound. Assemble these normal results in the indicated monadic order to obtain d and every asserted equation. In particular, when r\ge19 the fill has no writes; it does not clear unused cells through 768 or assume the retained prefix has a nonzero last cell.

Lean proof


  dsimp only
  have hlength : C.length = T.length + zeros.val := by
    simpa using congrArg List.length hsplit
  let selected : Result (Std.Usize × Bool) := do
    if scan.num_digits != 0#usize then do
      let consumed <- Slice.len start - Slice.len rest
      let zeros <- num.imp.dec2flt.decimal_seq.count_trailing_zeros start 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)
  have hselected : exists (r : Std.Usize) (flag : Bool),
      selected = ok (r, flag) /\
      r.val = min T.length 768 /\
      (flag = true <-> 768 < T.length) := by
    by_cases hC : C = []
    · have hT : T.length = 0 := by
        have hsum : T.length + zeros.val = 0 := by
          simpa only [hC, List.length_nil] using hlength.symm
        omega
      have hnzero : scan.num_digits = 0#usize :=
        UScalar.eq_of_val_eq (by simpa [hC] using hcount)
      refine ⟨scan.num_digits, false, ?_, ?_, ?_⟩
      · simp [selected, hnzero, hflag]
      · simp [hnzero, hT]
      · simp [hT]
    · have hnnonzero : scan.num_digits != 0#usize := by
        simp only [bne_iff_ne]
        intro hz
        have hlenzero : C.length = 0 := by simpa [hz] using hcount.symm
        exact hC (List.eq_nil_of_length_eq_zero hlenzero)
      have hsubbound : zeros.val <= scan.num_digits.val := by omega
      obtain ⟨kept, hkept, hkeptval⟩ := WP.spec_imp_exists
        (UScalar.sub_spec (x := scan.num_digits) (y := zeros) hsubbound)
      have hkeptlen : kept.val = T.length := by omega
      by_cases hlarge : 768 < T.length
      · have hcap : kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
          simpa [UScalar.lt_equiv,
            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
            hkeptlen] using hlarge
        refine ⟨num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS,
          true, ?_, ?_, ?_⟩
        · simp only [selected, hnnonzero, if_true, hconsumed, bind_tc_ok,
            hzeros hC, hkept, hcap]
        · simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
          omega
        · simp [hlarge]
      · have hcap : ¬ kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
          simpa [UScalar.lt_equiv,
            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
            hkeptlen] using hlarge
        refine ⟨kept, false, ?_, ?_, ?_⟩
        · simp only [selected, hnnonzero, if_true, hconsumed, bind_tc_ok,
            hzeros hC, hkept, hcap, if_false, hflag]
        · rw [hkeptlen, Nat.min_eq_left (by omega)]
        · simp [hlarge]
  obtain ⟨r, flag, hselect, hr, hflagout⟩ := hselected
  obtain ⟨point, hpoint, hpointval⟩ := P9_T1_FinalPointExact scan.num_digits f e hn hf
  obtain ⟨out, hfill, hprefix, hcells, houtdigits⟩ :=
    P495_T1_FillExact r scan.digits
  have hrT : r.val <= T.length := by rw [hr]; exact Nat.min_le_left _ _
  have hr768 : r.val <= 768 := by rw [hr]; exact Nat.min_le_right _ _
  have hrC : r.val <= C.length := by omega
  have hrstored : r.val <= min C.length 768 := le_min hrC hr768
  have hstoredr : (scan.digits.val.take r.val).map (fun b => b.val) = C.take r.val := by
    have ht := congrArg (fun xs : List Nat => xs.take r.val) hstored
    simpa only [← List.map_take, List.take_take, Nat.min_eq_left hrstored] using ht
  have hCT : C.take r.val = T.take r.val := by
    rw [hsplit, List.take_append_of_le_length hrT]
  let d : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := r, decimal_point := point, truncated := flag, digits := out }
  refine ⟨d, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  · simp only [selected] at hselect
    simp only [hselect, bind_tc_ok, hpoint]
    change (do
      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 }) = ok d
    simp only [hfill, bind_tc_ok]
    rfl
  · exact hpoint
  · simpa only [d, hcount] using hpointval
  · exact hr
  · change (out.val.take r.val).map (fun b => b.val) = T.take r.val
    rw [hprefix, hstoredr, hCT]
  · exact hflagout
  · exact hprefix
  · exact hcells
  · exact houtdigits hdigits

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P495_T1_FillExact
import Tablet.P9_T1_FinalPointExact

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

-- [TABLET NODE: P495_T1_FinalizationExact]
theorem P495_T1_FinalizationExact (start rest : Slice Std.U8)
    (scan : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (f : Std.Usize) (e : Std.I64) (consumed zeros : Std.Usize)
    (C T : List Nat)
    (hcount : scan.num_digits.val = C.length)
    (hn : scan.num_digits.val <= 9223372036854775807)
    (hf : f.val <= 9223372036854775807)
    (hflag : scan.truncated = false)
    (hsplit : C = T ++ List.replicate zeros.val 0)
    (hconsumed : Slice.len start - Slice.len rest = ok consumed)
    (hzeros : C ≠ [] ->
      num.imp.dec2flt.decimal_seq.count_trailing_zeros start consumed = ok zeros)
    (hstored : (scan.digits.val.take (min C.length 768)).map (fun b => b.val) =
      C.take (min C.length 768))
    (hdigits : forall b, b ∈ scan.digits.val -> b.val < 10) :
    let finish : Result num.imp.dec2flt.decimal_seq.DecimalSeq := do
      let (r, flag) <-
        if scan.num_digits != 0#usize then do
          let consumed <- Slice.len start - Slice.len rest
          let zeros <- num.imp.dec2flt.decimal_seq.count_trailing_zeros start 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 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 }
    exists d : num.imp.dec2flt.decimal_seq.DecimalSeq,
      finish = ok d /\
      num.imp.dec2flt.decimal_seq.final_decimal_point scan.num_digits f e =
        ok d.decimal_point /\
      d.decimal_point.val = (if C.length = 0 then 0 else
        max (-2047) (min 2047 ((C.length : Int) - f.val + e.val))) /\
      d.num_digits.val = min T.length 768 /\
      (d.digits.val.take d.num_digits.val).map (fun b => b.val) =
        T.take d.num_digits.val /\
      (d.truncated = true <-> 768 < T.length) /\
      d.digits.val.take d.num_digits.val = scan.digits.val.take d.num_digits.val /\
      (forall j, j < 768 -> d.digits.val[j]? =
        if d.num_digits.val <= j /\ j < 19 then some 0#u8 else scan.digits.val[j]?) /\
      (forall b, b ∈ d.digits.val -> b.val < 10) := by
-- BODY
  dsimp only
  have hlength : C.length = T.length + zeros.val := by
    simpa using congrArg List.length hsplit
  let selected : Result (Std.Usize × Bool) := do
    if scan.num_digits != 0#usize then do
      let consumed <- Slice.len start - Slice.len rest
      let zeros <- num.imp.dec2flt.decimal_seq.count_trailing_zeros start 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)
  have hselected : exists (r : Std.Usize) (flag : Bool),
      selected = ok (r, flag) /\
      r.val = min T.length 768 /\
      (flag = true <-> 768 < T.length) := by
    by_cases hC : C = []
    · have hT : T.length = 0 := by
        have hsum : T.length + zeros.val = 0 := by
          simpa only [hC, List.length_nil] using hlength.symm
        omega
      have hnzero : scan.num_digits = 0#usize :=
        UScalar.eq_of_val_eq (by simpa [hC] using hcount)
      refine ⟨scan.num_digits, false, ?_, ?_, ?_⟩
      · simp [selected, hnzero, hflag]
      · simp [hnzero, hT]
      · simp [hT]
    · have hnnonzero : scan.num_digits != 0#usize := by
        simp only [bne_iff_ne]
        intro hz
        have hlenzero : C.length = 0 := by simpa [hz] using hcount.symm
        exact hC (List.eq_nil_of_length_eq_zero hlenzero)
      have hsubbound : zeros.val <= scan.num_digits.val := by omega
      obtain ⟨kept, hkept, hkeptval⟩ := WP.spec_imp_exists
        (UScalar.sub_spec (x := scan.num_digits) (y := zeros) hsubbound)
      have hkeptlen : kept.val = T.length := by omega
      by_cases hlarge : 768 < T.length
      · have hcap : kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
          simpa [UScalar.lt_equiv,
            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
            hkeptlen] using hlarge
        refine ⟨num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS,
          true, ?_, ?_, ?_⟩
        · simp only [selected, hnnonzero, if_true, hconsumed, bind_tc_ok,
            hzeros hC, hkept, hcap]
        · simp [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
          omega
        · simp [hlarge]
      · have hcap : ¬ kept > num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
          simpa [UScalar.lt_equiv,
            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
            hkeptlen] using hlarge
        refine ⟨kept, false, ?_, ?_, ?_⟩
        · simp only [selected, hnnonzero, if_true, hconsumed, bind_tc_ok,
            hzeros hC, hkept, hcap, if_false, hflag]
        · rw [hkeptlen, Nat.min_eq_left (by omega)]
        · simp [hlarge]
  obtain ⟨r, flag, hselect, hr, hflagout⟩ := hselected
  obtain ⟨point, hpoint, hpointval⟩ := P9_T1_FinalPointExact scan.num_digits f e hn hf
  obtain ⟨out, hfill, hprefix, hcells, houtdigits⟩ :=
    P495_T1_FillExact r scan.digits
  have hrT : r.val <= T.length := by rw [hr]; exact Nat.min_le_left _ _
  have hr768 : r.val <= 768 := by rw [hr]; exact Nat.min_le_right _ _
  have hrC : r.val <= C.length := by omega
  have hrstored : r.val <= min C.length 768 := le_min hrC hr768
  have hstoredr : (scan.digits.val.take r.val).map (fun b => b.val) = C.take r.val := by
    have ht := congrArg (fun xs : List Nat => xs.take r.val) hstored
    simpa only [← List.map_take, List.take_take, Nat.min_eq_left hrstored] using ht
  have hCT : C.take r.val = T.take r.val := by
    rw [hsplit, List.take_append_of_le_length hrT]
  let d : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := r, decimal_point := point, truncated := flag, digits := out }
  refine ⟨d, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
  · simp only [selected] at hselect
    simp only [hselect, bind_tc_ok, hpoint]
    change (do
      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 }) = ok d
    simp only [hfill, bind_tc_ok]
    rfl
  · exact hpoint
  · simpa only [d, hcount] using hpointval
  · exact hr
  · change (out.val.take r.val).map (fun b => b.val) = T.take r.val
    rw [hprefix, hstoredr, hCT]
  · exact hflagout
  · exact hprefix
  · exact hcells
  · exact houtdigits hdigits

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 47 entries
Technical checking records
Local closure summary
{
  "node": "P495_T1_FinalizationExact",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "a35ea7343312f97179d0b8546d7075915da089b51aad5f6c275e21478e84c8fc",
  "active_statement_hash": "735e65c62c90560163df9f99e853fb97499e9786fbcae85a15f54bc7ff06abcd",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P495_T1_FillExact": "68ceb8da9a285852",
    "P9_T1_FinalPointExact": "00b8def1a58eb426",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "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"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:9c761741c7bc52f01f2b07ff578429538f44dabc98ac448aab9b1caf140466bf",
  "current_fingerprint": "sound-v2:9c761741c7bc52f01f2b07ff578429538f44dabc98ac448aab9b1caf140466bf",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}