DecimalSeqRoundLoopPrefixCore

Lean checked

Statement

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

Helper · Checked decimal rounding loop

Let d be a decimal sequence whose count is at most 768 and whose retained cells are decimal digits. Let q be a usize with q\leq18. Define D_j to be the value of cell j when j is below the count, and zero otherwise, and put
F_k=\operatorname{foldl}(a,j\mapsto10a+D_j,0,[0,\ldots,k-1]).
Then the extracted rounding range loop from zero through q returns a U64 n normally, and n has value F_q.

Lean statement

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

import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.ResultLoopPartial
import Tablet.DecimalGoalVocabularyDefault

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundLoopPrefixCore]
theorem DecimalSeqRoundLoopPrefixCore (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 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
      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 /\
      n.val = F dp.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

Unfold the range loop and its body in num imp dec2flt decimal seq DecimalSeq round. Every D_j is strictly below 10: in the stored case this is the digit hypothesis, and in the other case it is zero. Induction on k gives
F_{k+1}=10F_k+D_k\qquad\hbox{and}\qquad F_k<10^k. \tag{1}
Use the invariant that the current range start k is at most q, the range endpoint is still q, and the U64 accumulator has value F_k. If the iterator finishes, k=q and the returned accumulator has the required value. Otherwise k<q\leq18. Thus k+1 fits usize. By (1), multiplying the accumulator by ten yields a value below 10^{k+1}\leq10^{18}<2^{64}, so the checked U64 multiplication succeeds. If k is below the digit count, then k<768 and the fixed array access is in bounds; its U8-to-U64 cast is exact, and adding the digit produces F_{k+1}<2^{64}, so the checked addition succeeds. If k is not below the count, the body adds zero and again produces F_{k+1}. In both cases the next state satisfies the invariant and the natural measure q-k strictly decreases.
The preservation-and-finish reasoning is the loop rule ResultLoopPartial; the displayed decreasing measure additionally establishes normal termination. Starting from k=0 and accumulator zero, where the invariant holds definitionally, therefore yields a normal return with value F_q.

Lean proof


  dsimp only
  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
  have hD (j : Nat) : D j < 10 := by
    simp only [D]
    split
    · rename_i hj
      apply hdigit
      have ht : j < (d.digits.val.take d.num_digits.val).length := by
        simp
        have ha : d.digits.val.length = 768 := by
          simpa using d.digits.property
        omega
      have hm := List.getElem_mem
        (l := d.digits.val.take d.num_digits.val) (n := j) ht
      simpa using hm
    · omega
  have hFstep (k : Nat) : F (k + 1) = 10 * F k + D k := by
    simp [F, List.range_succ, List.foldl_append]
  have hFbound (k : Nat) : F k < 10 ^ k := by
    induction k with
    | zero => simp [F]
    | succ k ih =>
      rw [show k + 1 = Nat.succ k by omega, hFstep, pow_succ]
      nlinarith [hD k]
  let Inv := fun p : core.ops.range.Range Std.Usize × Std.U64 =>
    p.1.start.val <= dp.val /\
    p.1.end = dp /\
    p.2.val = F p.1.start.val
  have hspec : Aeneas.Std.WP.spec
      (num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
        { start := 0#usize, «end» := dp } d.num_digits d.digits 0#u64)
      (fun n => n.val = F dp.val) := by
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : core.ops.range.Range Std.Usize × Std.U64 =>
        dp.val - p.1.start.val)
      Inv (fun n => n.val = F dp.val)
      (fun p => num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop.body
        d.num_digits d.digits p.1 p.2)
      ({ start := 0#usize, «end» := dp }, 0#u64)
    · intro p hp
      rcases p with ⟨iter, n⟩
      rcases iter with ⟨start, end_⟩
      rcases hp with ⟨hstart, hend, hn⟩
      simp only [Prod.fst, Prod.snd] at hstart hend hn ⊢
      rw [hend]
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop.body
      unfold core.iter.range.IteratorRange.next
      simp [core.iter.range.StepUsize, core.iter.range.UScalarStep,
        core.cmp.PartialOrdUsize, core.cmp.impls.PartialOrdUsize.lt,
        liftFun2]
      split
      · rename_i hlt
        have hmax : start.val + 1 <= UScalar.max .Usize := by
          rcases System.Platform.numBits_eq with hbits | hbits <;>
            simp [UScalar.max, UScalarTy.numBits, hbits] <;> omega
        simp [core.iter.range.UScalarStep.forward_checked, hmax]
        have hmul : n.val * 10 <= U64.max := by
          rw [hn]
          have hpow : 10 ^ (start.val + 1) <= 10 ^ 19 :=
            Nat.pow_le_pow_right (by norm_num) (by omega)
          have hlt : F start.val * 10 < 10 ^ (start.val + 1) := by
            rw [pow_succ]
            nlinarith [hFbound start.val]
          rw [U64.max_eq]
          norm_num at hpow ⊢
          omega
        obtain ⟨n10, hn10, hn10val⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := n) (y := 10#u64) (by simpa using hmul))
        rw [hn10]
        simp only [bind_tc_ok]
        by_cases hstored : start.val < d.num_digits.val
        · have hword : start < d.num_digits := by
            simpa [UScalar.lt_equiv] using hstored
          simp only [hstored, if_true]
          have hidx : start.val < d.digits.val.length := by
            have ha : d.digits.val.length = 768 := by
              simpa using d.digits.property
            omega
          obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (Array.index_usize_spec d.digits start
                (by simpa [Array.length] using hidx))
          rw [hdigitRead]
          simp only [bind_tc_ok, lift]
          let digit64 : Std.U64 := UScalar.cast .U64 digit
          rw [show UScalar.cast .U64 digit = digit64 by rfl]
          have hdigit64 : digit64.val = digit.val := by
            simp [digit64, UScalar.cast_val_eq]
          have hdigitVal : digit.val = D start.val := by
            rw [show D start.val =
              (d.digits.val[start.val]'hidx).val by
                simp [D, hstored]]
            exact congrArg UScalar.val hdigitReadVal
          have hadd : n10.val + digit64.val <= U64.max := by
            rw [hn10val, hdigit64, hdigitVal, hn]
            rw [U64.max_eq]
            have hb := hFbound (start.val + 1)
            have hpow : 10 ^ (start.val + 1) <= 10 ^ 19 :=
              Nat.pow_le_pow_right (by norm_num) (by omega)
            rw [hFstep] at hb
            norm_num at hpow ⊢
            omega
          obtain ⟨n1, hn1, hn1val⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.add_spec (x := n10) (y := digit64) hadd)
          rw [hn1]
          simp only [bind_tc_ok]
          refine ⟨⟨?_, rfl, ?_⟩, ?_⟩
          · change start.val + 1 <= dp.val
            omega
          · change n1.val = F (start.val + 1)
            rw [hn1val, hn10val, hn, hdigit64, hdigitVal, hFstep]
            norm_num
            omega
          · change dp.val - (start.val + 1) < dp.val - start.val
            omega
        · have hword : ¬ start < d.num_digits := by
            simpa [UScalar.lt_equiv] using hstored
          simp only [hstored, if_false, WP.spec_ok]
          refine ⟨⟨?_, rfl, ?_⟩, ?_⟩
          · change start.val + 1 <= dp.val
            omega
          · change n10.val = F (start.val + 1)
            rw [hn10val, hn, hFstep]
            simp [D, hstored]
            omega
          · change dp.val - (start.val + 1) < dp.val - start.val
            omega
      · rename_i hnotlt
        change n.val = F dp.val
        rw [hn]
        congr 1
        omega
    · simp [Inv, F]
  obtain ⟨n, hn, hnval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
  exact ⟨n, hn, hnval⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.ResultLoopPartial
import Tablet.DecimalGoalVocabularyDefault

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundLoopPrefixCore]
theorem DecimalSeqRoundLoopPrefixCore (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 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
      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 /\
      n.val = F dp.val := by
-- BODY
  dsimp only
  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
  have hD (j : Nat) : D j < 10 := by
    simp only [D]
    split
    · rename_i hj
      apply hdigit
      have ht : j < (d.digits.val.take d.num_digits.val).length := by
        simp
        have ha : d.digits.val.length = 768 := by
          simpa using d.digits.property
        omega
      have hm := List.getElem_mem
        (l := d.digits.val.take d.num_digits.val) (n := j) ht
      simpa using hm
    · omega
  have hFstep (k : Nat) : F (k + 1) = 10 * F k + D k := by
    simp [F, List.range_succ, List.foldl_append]
  have hFbound (k : Nat) : F k < 10 ^ k := by
    induction k with
    | zero => simp [F]
    | succ k ih =>
      rw [show k + 1 = Nat.succ k by omega, hFstep, pow_succ]
      nlinarith [hD k]
  let Inv := fun p : core.ops.range.Range Std.Usize × Std.U64 =>
    p.1.start.val <= dp.val /\
    p.1.end = dp /\
    p.2.val = F p.1.start.val
  have hspec : Aeneas.Std.WP.spec
      (num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
        { start := 0#usize, «end» := dp } d.num_digits d.digits 0#u64)
      (fun n => n.val = F dp.val) := by
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : core.ops.range.Range Std.Usize × Std.U64 =>
        dp.val - p.1.start.val)
      Inv (fun n => n.val = F dp.val)
      (fun p => num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop.body
        d.num_digits d.digits p.1 p.2)
      ({ start := 0#usize, «end» := dp }, 0#u64)
    · intro p hp
      rcases p with ⟨iter, n⟩
      rcases iter with ⟨start, end_⟩
      rcases hp with ⟨hstart, hend, hn⟩
      simp only [Prod.fst, Prod.snd] at hstart hend hn ⊢
      rw [hend]
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop.body
      unfold core.iter.range.IteratorRange.next
      simp [core.iter.range.StepUsize, core.iter.range.UScalarStep,
        core.cmp.PartialOrdUsize, core.cmp.impls.PartialOrdUsize.lt,
        liftFun2]
      split
      · rename_i hlt
        have hmax : start.val + 1 <= UScalar.max .Usize := by
          rcases System.Platform.numBits_eq with hbits | hbits <;>
            simp [UScalar.max, UScalarTy.numBits, hbits] <;> omega
        simp [core.iter.range.UScalarStep.forward_checked, hmax]
        have hmul : n.val * 10 <= U64.max := by
          rw [hn]
          have hpow : 10 ^ (start.val + 1) <= 10 ^ 19 :=
            Nat.pow_le_pow_right (by norm_num) (by omega)
          have hlt : F start.val * 10 < 10 ^ (start.val + 1) := by
            rw [pow_succ]
            nlinarith [hFbound start.val]
          rw [U64.max_eq]
          norm_num at hpow ⊢
          omega
        obtain ⟨n10, hn10, hn10val⟩ := Aeneas.Std.WP.spec_imp_exists
          (U64.mul_spec (x := n) (y := 10#u64) (by simpa using hmul))
        rw [hn10]
        simp only [bind_tc_ok]
        by_cases hstored : start.val < d.num_digits.val
        · have hword : start < d.num_digits := by
            simpa [UScalar.lt_equiv] using hstored
          simp only [hstored, if_true]
          have hidx : start.val < d.digits.val.length := by
            have ha : d.digits.val.length = 768 := by
              simpa using d.digits.property
            omega
          obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (Array.index_usize_spec d.digits start
                (by simpa [Array.length] using hidx))
          rw [hdigitRead]
          simp only [bind_tc_ok, lift]
          let digit64 : Std.U64 := UScalar.cast .U64 digit
          rw [show UScalar.cast .U64 digit = digit64 by rfl]
          have hdigit64 : digit64.val = digit.val := by
            simp [digit64, UScalar.cast_val_eq]
          have hdigitVal : digit.val = D start.val := by
            rw [show D start.val =
              (d.digits.val[start.val]'hidx).val by
                simp [D, hstored]]
            exact congrArg UScalar.val hdigitReadVal
          have hadd : n10.val + digit64.val <= U64.max := by
            rw [hn10val, hdigit64, hdigitVal, hn]
            rw [U64.max_eq]
            have hb := hFbound (start.val + 1)
            have hpow : 10 ^ (start.val + 1) <= 10 ^ 19 :=
              Nat.pow_le_pow_right (by norm_num) (by omega)
            rw [hFstep] at hb
            norm_num at hpow ⊢
            omega
          obtain ⟨n1, hn1, hn1val⟩ := Aeneas.Std.WP.spec_imp_exists
            (U64.add_spec (x := n10) (y := digit64) hadd)
          rw [hn1]
          simp only [bind_tc_ok]
          refine ⟨⟨?_, rfl, ?_⟩, ?_⟩
          · change start.val + 1 <= dp.val
            omega
          · change n1.val = F (start.val + 1)
            rw [hn1val, hn10val, hn, hdigit64, hdigitVal, hFstep]
            norm_num
            omega
          · change dp.val - (start.val + 1) < dp.val - start.val
            omega
        · have hword : ¬ start < d.num_digits := by
            simpa [UScalar.lt_equiv] using hstored
          simp only [hstored, if_false, WP.spec_ok]
          refine ⟨⟨?_, rfl, ?_⟩, ?_⟩
          · change start.val + 1 <= dp.val
            omega
          · change n10.val = F (start.val + 1)
            rw [hn10val, hn, hFstep]
            simp [D, hstored]
            omega
          · change dp.val - (start.val + 1) < dp.val - start.val
            omega
      · rename_i hnotlt
        change n.val = F dp.val
        rw [hn]
        congr 1
        omega
    · simp [Inv, F]
  obtain ⟨n, hn, hnval⟩ := Aeneas.Std.WP.spec_imp_exists hspec
  exact ⟨n, hn, hnval⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (117)
More program dependencies (84)
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": "DecimalSeqRoundLoopPrefixCore",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "767c01da8aadbe060d79a0a3012d6fb255f2bccbd324b1d87978f0a70a9483e4",
  "active_statement_hash": "fcb42bd40df5514861d7937c2c9021aab32ba4e03e469ad9b139cdf5844d5714",
  "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": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "827e500313be4668"
  }
}
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:3cfbb26605ae799c9d90f29dc1b89fe7eab3bfe0d3f445fe2ccd01b656af6584",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}