DecimalSeqRoundLoopValue

Lean checked

Statement

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

Helper · Value computed by the rounding loop

Let d be a decimal sequence with count at most 768 and decimal retained cells, and let q\leq18 be a usize. Let L be the list of retained digit values and let I be its base-ten value. The extracted rounding loop from zero through q returns a U64 n. If q is below the count, then n is the base-ten value of the first q digits of L. If the count is at most q, then n=I10^{q-\mathrm{count}}.

Lean statement

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

import Tablet.DecimalSeqRoundLoopPrefixCore

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundLoopValue]
theorem DecimalSeqRoundLoopValue (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (dp : Std.Usize) (hdp : dp.val <= 18)
    (hnum : d.num_digits.val <= 768)
    (hdigit : forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) :
    let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    let integer := digits.foldl (fun n z => 10 * n + z) 0
    exists n : Std.U64,
      num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
        { start := 0#usize, «end» := dp } d.num_digits d.digits 0#u64 = ok n /\
      (dp.val < d.num_digits.val ->
        n.val = (digits.take dp.val).foldl (fun a z => 10 * a + z) 0) /\
      (d.num_digits.val <= dp.val ->
        n.val = integer * 10 ^ (dp.val - d.num_digits.val)) := by
Proof

Natural-language proof

The status above describes the Lean proof. Its explanatory prose may remain a sketch after that proof closes.

Explanatory prose status

Prose retained as authored. The explanatory prose is preserved as it stood when captured.

Proof

Apply DecimalSeqRoundLoopPrefixCore. It returns a U64 whose value is the fold F_q over the zero-padded digit function D from that lemma. The retained list has length equal to the count, because the backing array has length 768 and the assumed count is at most 768.
For every k at most the count, induction on k shows that F_k is the base-ten fold of the first k retained digits. At the successor step the definition of D selects exactly digit k, and both folds perform the same update a\mapsto10a+L_k. This gives the first conditional conclusion at k=q.
At the count itself, the same identity gives F_{\mathrm{count}}=I. Beyond the count, D is zero, so each additional step multiplies the fold by ten. A second induction gives F_{\mathrm{count}+t}=I10^t for every t. When the count is at most q, write q=\mathrm{count}+(q-\mathrm{count}) and substitute t=q-\mathrm{count}. This is the second conditional conclusion.

Lean proof


  dsimp only
  let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let integer := digits.foldl (fun n z => 10 * n + z) 0
  let D := fun j : Nat =>
    if h : j < d.num_digits.val then
      (d.digits.val[j]'(by
        have ha : d.digits.val.length = 768 := by simpa using d.digits.property
        omega)).val
    else 0
  let F := fun k : Nat =>
    (List.range k).foldl (fun n j => 10 * n + D j) 0
  obtain ⟨n, hn, hnF⟩ := DecimalSeqRoundLoopPrefixCore d dp hdp hnum hdigit
  change n.val = F dp.val at hnF
  have hdigitsLen : digits.length = d.num_digits.val := by
    simp [digits]
    have ha : d.digits.val.length = 768 := by simpa using d.digits.property
    omega
  have hDstored (j : Nat) (hj : j < d.num_digits.val) : D j = digits[j] := by
    have hjArray : j < d.digits.val.length := by
      have ha : d.digits.val.length = 768 := by simpa using d.digits.property
      omega
    simp only [D]
    rw [dif_pos hj]
    simp [digits, List.getElem_map, List.getElem_take]
  have hFstep (k : Nat) : F (k + 1) = 10 * F k + D k := by
    simp [F, List.range_succ, List.foldl_append]
  have hFprefix (k : Nat) (hk : k <= d.num_digits.val) :
      F k = (digits.take k).foldl (fun a z => 10 * a + z) 0 := by
    induction k with
    | zero => simp [F]
    | succ k ih =>
      have hklt : k < d.num_digits.val := by omega
      rw [hFstep,
        List.take_succ_eq_append_getElem (by omega : k < digits.length),
        List.foldl_append]
      simp only [List.foldl_cons, List.foldl_nil]
      rw [ih (by omega), hDstored k hklt]
  have hFafter (t : Nat) :
      F (d.num_digits.val + t) = integer * 10 ^ t := by
    induction t with
    | zero =>
      simp only [Nat.add_zero, pow_zero, mul_one]
      rw [hFprefix d.num_digits.val (le_refl _)]
      dsimp only [integer]
      simp [← hdigitsLen]
    | succ t ih =>
      rw [show d.num_digits.val + Nat.succ t =
          (d.num_digits.val + t) + 1 by omega,
        hFstep, ih, pow_succ]
      simp [D]
      ring
  refine ⟨n, hn, ?_, ?_⟩
  · intro hlt
    rw [hnF, hFprefix dp.val (by omega)]
  · intro hle
    rw [hnF]
    calc
      F dp.val = F (d.num_digits.val + (dp.val - d.num_digits.val)) := by
        rw [Nat.add_sub_of_le hle]
      _ = integer * 10 ^ (dp.val - d.num_digits.val) :=
        hFafter (dp.val - d.num_digits.val)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqRoundLoopPrefixCore

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundLoopValue]
theorem DecimalSeqRoundLoopValue (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (dp : Std.Usize) (hdp : dp.val <= 18)
    (hnum : d.num_digits.val <= 768)
    (hdigit : forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) :
    let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    let integer := digits.foldl (fun n z => 10 * n + z) 0
    exists n : Std.U64,
      num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
        { start := 0#usize, «end» := dp } d.num_digits d.digits 0#u64 = ok n /\
      (dp.val < d.num_digits.val ->
        n.val = (digits.take dp.val).foldl (fun a z => 10 * a + z) 0) /\
      (d.num_digits.val <= dp.val ->
        n.val = integer * 10 ^ (dp.val - d.num_digits.val)) := by
-- BODY
  dsimp only
  let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let integer := digits.foldl (fun n z => 10 * n + z) 0
  let D := fun j : Nat =>
    if h : j < d.num_digits.val then
      (d.digits.val[j]'(by
        have ha : d.digits.val.length = 768 := by simpa using d.digits.property
        omega)).val
    else 0
  let F := fun k : Nat =>
    (List.range k).foldl (fun n j => 10 * n + D j) 0
  obtain ⟨n, hn, hnF⟩ := DecimalSeqRoundLoopPrefixCore d dp hdp hnum hdigit
  change n.val = F dp.val at hnF
  have hdigitsLen : digits.length = d.num_digits.val := by
    simp [digits]
    have ha : d.digits.val.length = 768 := by simpa using d.digits.property
    omega
  have hDstored (j : Nat) (hj : j < d.num_digits.val) : D j = digits[j] := by
    have hjArray : j < d.digits.val.length := by
      have ha : d.digits.val.length = 768 := by simpa using d.digits.property
      omega
    simp only [D]
    rw [dif_pos hj]
    simp [digits, List.getElem_map, List.getElem_take]
  have hFstep (k : Nat) : F (k + 1) = 10 * F k + D k := by
    simp [F, List.range_succ, List.foldl_append]
  have hFprefix (k : Nat) (hk : k <= d.num_digits.val) :
      F k = (digits.take k).foldl (fun a z => 10 * a + z) 0 := by
    induction k with
    | zero => simp [F]
    | succ k ih =>
      have hklt : k < d.num_digits.val := by omega
      rw [hFstep,
        List.take_succ_eq_append_getElem (by omega : k < digits.length),
        List.foldl_append]
      simp only [List.foldl_cons, List.foldl_nil]
      rw [ih (by omega), hDstored k hklt]
  have hFafter (t : Nat) :
      F (d.num_digits.val + t) = integer * 10 ^ t := by
    induction t with
    | zero =>
      simp only [Nat.add_zero, pow_zero, mul_one]
      rw [hFprefix d.num_digits.val (le_refl _)]
      dsimp only [integer]
      simp [← hdigitsLen]
    | succ t ih =>
      rw [show d.num_digits.val + Nat.succ t =
          (d.num_digits.val + t) + 1 by omega,
        hFstep, ih, pow_succ]
      simp [D]
      ring
  refine ⟨n, hn, ?_, ?_⟩
  · intro hlt
    rw [hnF, hFprefix dp.val (by omega)]
  · intro hle
    rw [hnF]
    calc
      F dp.val = F (d.num_digits.val + (dp.val - d.num_digits.val)) := by
        rw [Nat.add_sub_of_le hle]
      _ = integer * 10 ^ (dp.val - d.num_digits.val) :=
        hFafter (dp.val - d.num_digits.val)

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (116)
More program dependencies (83)
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
Technical checking records
Local closure summary
{
  "node": "DecimalSeqRoundLoopValue",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "bd8227b4804fe2c473e49aedc9aba4bdc19b4b853f074522f906dc208327e105",
  "active_statement_hash": "068559973f3171258d60c57816ec8a622535a81e44a43b6b6309d809846bd08c",
  "kernel_axioms": [
    "Classical.choice",
    "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-182",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalSeqRoundLoopPrefixCore": "e07d14283be85014"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "74fa9a4c095ca758"
  }
}
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:4922f08d54a9f7bfa71741df7052ee936af5914bdb2b4bdadb98754310a3dcc6",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}