DecimalSeqTrimSemantics

Lean checked

Statement

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

Lemma · Exact semantics of decimal-sequence trimming

Let d be a decimal sequence whose stored count is at most the length of its 768-cell digit array, and suppose the extracted trim operation returns d'. Then the digit array, decimal point, and truncation Boolean of d' equal those of d. Its count n' is at most the old count n; every cell with index n'\le q<n is zero; and either n'=0 or cell n'-1 is nonzero. Finally, for
V(e)=\operatorname{fold}_{10}(e.\mathit{digits}[0:e.\mathit{num\_digits}]) 10^{e.\mathit{decimal\_point}-e.\mathit{num\_digits}},
one has V(d')=V(d).

Lean statement

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

import Tablet.DecimalGoalVocabularyDefault
import Tablet.ResultLoopPartial
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_trim

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqTrimSemantics]
theorem DecimalSeqTrimSemantics
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (hbound : d.num_digits.val <= d.digits.val.length)
    (htrim : num.imp.dec2flt.decimal_seq.DecimalSeq.trim d = ok d') :
    let value := fun e : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      let digits := (e.digits.val.take e.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun acc z => 10 * acc + z) 0
      (integer : Real) * (10 : Real) ^
        (e.decimal_point.val - (e.num_digits.val : Int))
    d'.digits = d.digits /\
      d'.decimal_point = d.decimal_point /\
      d'.truncated = d.truncated /\
      d'.num_digits.val <= d.num_digits.val /\
      (forall q, d'.num_digits.val <= q -> q < d.num_digits.val ->
        d.digits.val[q]? = some 0#u8) /\
      (d'.num_digits.val = 0 \/
        d.digits.val[d'.num_digits.val - 1]? != some 0#u8) /\
      value d' = value d := 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

Write n for the initial count, a for the digit array, and
F(i)=\operatorname{fold}_{10}(a[0:i]).
Apply the result-loop induction principle ResultLoopPartial to the extracted trim loop with invariant
i\le n,\qquad i\le |a|,\qquad (\forall q,\ i\le q<n\Rightarrow a[q]=0),\qquad F(n)=F(i)10^{n-i}. \tag{1}
At the initial state i=n, both bounds are immediate, the quantified index interval is empty, and the fold equation is F(n)=F(n)10^0.
Consider one normally returning loop-body step. If i=0, the body finishes with zero. If i>0, unsigned subtraction of one is in range and returns i-1. The invariant gives i\le|a|, so i-1<|a| and the array read at i-1 is also in range. If that cell is nonzero, the body finishes with i. Thus (1) still holds and the returned count has the required nonzero last cell.
If instead a[i-1]=0, the body continues at i-1. The two bounds in (1) are preserved by decrementing. The old zero-suffix assertion already covers all q\ge i; its one new index q=i-1 is zero by the branch test, so it now covers every q\ge i-1. For the fold equation, the last-cell decomposition of the base-ten fold gives
F(i)=10F(i-1)+a[i-1]=10F(i-1).
Substituting this into (1), and using n-(i-1)=(n-i)+1, gives
F(n)=10F(i-1)10^{n-i}=F(i-1)10^{n-(i-1)}.
This proves preservation of the entire invariant. The cited loop-induction principle therefore shows that a normal terminal count n' satisfies (1), and the body's only terminal cases show additionally that n'=0 or the last retained cell is nonzero.
The outer extracted operation first asserts n\le768. The stated array bound proves this because the fixed array length is 768. It then runs the loop and updates only num_digits; hence the returned digit array, decimal point, and truncation Boolean are unchanged. Equation (1) at n' is
F(n)=F(n')10^{n-n'}. \tag{2}
Since n'\le n, integer exponent arithmetic gives
p-n'=(p-n)+(n-n'). \tag{3}
Cast (2) to the reals, expand 10^{p-n'} using (3), and reassociate the products. The factor 10^{n-n'} introduced into the retained fold is exactly cancelled by the reduction of the stored count in the decimal exponent. Therefore
F(n')10^{p-n'}=F(n)10^{p-n},
which is precisely V(d')=V(d).

Lean proof


  dsimp only
  have foldTakeSuccZero
      (xs : List Nat) (n : Nat) (hn : n < xs.length) (hz : xs[n] = 0) :
      (xs.take (n + 1)).foldl (fun acc z => 10 * acc + z) 0 =
        10 * (xs.take n).foldl (fun acc z => 10 * acc + z) 0 := by
    rw [List.take_succ, List.foldl_append]
    have hget : xs[n]? = some xs[n] :=
      getElem?_eq_some_iff.mpr ⟨hn, rfl⟩
    simp [hget, hz]
  have decimalScaleCancel
      (A B N n : Nat) (p : Int) (hnN : n <= N)
      (hAB : A = B * 10 ^ (N - n)) :
      (A : Real) * (10 : Real) ^ (p - (N : Int)) =
        (B : Real) * (10 : Real) ^ (p - (n : Int)) := by
    have hcast : (A : Real) = (B : Real) * (10 : Real) ^ (N - n) := by
      norm_cast
    have hexp : p - (n : Int) =
        (p - (N : Int)) + ((N - n : Nat) : Int) := by
      rw [Nat.cast_sub hnN]
      omega
    rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
    norm_num only [zpow_natCast]
    rw [hcast]
    ring
  have trimLoopFull
      (a : Array Std.U8 768#usize) (N j : Std.Usize)
      (hNLen : N.val <= a.val.length)
      (hloop : num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop N a = ok j) :
      let xs := a.val.map (fun z => z.val)
      j.val <= N.val /\
        (forall q, j.val <= q -> q < N.val -> a.val[q]? = some 0#u8) /\
        (j.val = 0 \/ a.val[j.val - 1]? != some 0#u8) /\
        (xs.take N.val).foldl (fun acc z => 10 * acc + z) 0 =
          (xs.take j.val).foldl (fun acc z => 10 * acc + z) 0 *
            10 ^ (N.val - j.val) := by
    dsimp only
    let xs := a.val.map (fun z => z.val)
    let F := fun n : Nat =>
      (xs.take n).foldl (fun acc z => 10 * acc + z) 0
    let Inv := fun x : Std.Usize =>
      x.val <= N.val /\ x.val <= a.val.length /\
        (forall q, x.val <= q -> q < N.val -> a.val[q]? = some 0#u8) /\
        F N.val = F x.val * 10 ^ (N.val - x.val)
    let Post := fun x : Std.Usize =>
      Inv x /\ (x.val = 0 \/ a.val[x.val - 1]? != some 0#u8)
    let Body := fun x =>
      num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body a x
    have hp : Post j := by
      refine ResultLoopPartial Inv Post Body ?_ N j ?_ ?_
      · intro x hx r hr
        rcases hx with ⟨hxN, hxLen, hxzero, hxF⟩
        simp only [Body,
          num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body] at hr
        by_cases hx0 : x.val = 0
        · have hxEq : x = 0#usize :=
            UScalar.eq_of_val_eq (by simpa using hx0)
          subst x
          simp at hr
          subst r
          exact ⟨⟨hxN, hxLen, hxzero, hxF⟩, Or.inl rfl⟩
        · have hxpos : 0 < x.val := by omega
          have hxne : x != 0#usize := by
            simp only [bne_iff_ne]
            intro h
            apply hx0
            simpa [h]
          obtain ⟨x1, hx1, hx1val⟩ := Aeneas.Std.WP.spec_imp_exists
            (UScalar.sub_spec (x := x) (y := 1#usize)
              (by simpa using hxpos))
          norm_num at hx1val
          simp [hxne, hx1] at hr
          have hx1lt : x1.val < a.val.length := by omega
          obtain ⟨digit, hdigit, hdigitval⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.index_usize_spec a x1
              (by simpa [Array.length] using hx1lt))
          rw [hdigit] at hr
          by_cases hdz : digit = 0#u8
          · simp [hdz] at hr
            subst r
            simp only [Inv]
            refine ⟨?_, ?_, ?_, ?_⟩
            · omega
            · omega
            · intro q hqj hqN
              by_cases hqx : x.val <= q
              · exact hxzero q hqx hqN
              · have hq : q = x1.val := by omega
                subst q
                apply getElem?_eq_some_iff.mpr
                refine ⟨hx1lt, ?_⟩
                rw [← hdigitval, hdz]
            · have hxslen : xs.length = a.val.length := by simp [xs]
              have hxdigit : xs[x1.val] = 0 := by
                rw [show xs[x1.val] = digit.val by simp [xs, hdigitval]]
                simp [hdz]
              have hstep := foldTakeSuccZero xs x1.val (by omega) hxdigit
              have hstepF : F x.val = 10 * F x1.val := by
                simp only [F]
                simpa [show x.val = x1.val + 1 by omega] using hstep
              rw [hxF, hstepF]
              rw [show N.val - x1.val = (N.val - x.val) + 1 by omega,
                pow_succ]
              ring
          · simp [hdz] at hr
            subst r
            exact ⟨⟨hxN, hxLen, hxzero, hxF⟩, Or.inr (by
              simp only [bne_iff_ne]
              intro hz
              rcases getElem?_eq_some_iff.mp hz with ⟨_, hzval⟩
              apply hdz
              rw [hdigitval]
              simpa [hx1val] using hzval)⟩
      · refine ⟨le_rfl, hNLen, ?_, ?_⟩
        · intro q hq hq'
          omega
        · simp [F]
      · simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop, Body]
          using hloop
    rcases hp with ⟨⟨hjN, _, hjzero, hjF⟩, hjlast⟩
    exact ⟨hjN, hjzero, hjlast, hjF⟩
  have hle : d.num_digits <=
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
    simpa [UScalar.le_equiv,
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
      d.digits.property] using hbound
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim at htrim
  simp only [hle, massert_True, bind_tc_ok] at htrim
  cases hloopEq : num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
      d.num_digits d.digits with
  | fail e => simp [hloopEq] at htrim
  | div => simp [hloopEq] at htrim
  | ok j =>
    simp only [hloopEq, bind_tc_ok] at htrim
    cases htrim
    have hloop := trimLoopFull d.digits d.num_digits j hbound hloopEq
    rcases hloop with ⟨hjN, hjzero, hjlast, hjF⟩
    refine ⟨rfl, rfl, rfl, hjN, hjzero, hjlast, ?_⟩
    simpa [List.map_take] using
      (decimalScaleCancel _ _ _ _ d.decimal_point.val hjN hjF).symm

end dec2flt_model

Download Lean source · Download TeX source

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

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqTrimSemantics]
theorem DecimalSeqTrimSemantics
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (hbound : d.num_digits.val <= d.digits.val.length)
    (htrim : num.imp.dec2flt.decimal_seq.DecimalSeq.trim d = ok d') :
    let value := fun e : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      let digits := (e.digits.val.take e.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun acc z => 10 * acc + z) 0
      (integer : Real) * (10 : Real) ^
        (e.decimal_point.val - (e.num_digits.val : Int))
    d'.digits = d.digits /\
      d'.decimal_point = d.decimal_point /\
      d'.truncated = d.truncated /\
      d'.num_digits.val <= d.num_digits.val /\
      (forall q, d'.num_digits.val <= q -> q < d.num_digits.val ->
        d.digits.val[q]? = some 0#u8) /\
      (d'.num_digits.val = 0 \/
        d.digits.val[d'.num_digits.val - 1]? != some 0#u8) /\
      value d' = value d := by
-- BODY
  dsimp only
  have foldTakeSuccZero
      (xs : List Nat) (n : Nat) (hn : n < xs.length) (hz : xs[n] = 0) :
      (xs.take (n + 1)).foldl (fun acc z => 10 * acc + z) 0 =
        10 * (xs.take n).foldl (fun acc z => 10 * acc + z) 0 := by
    rw [List.take_succ, List.foldl_append]
    have hget : xs[n]? = some xs[n] :=
      getElem?_eq_some_iff.mpr ⟨hn, rfl⟩
    simp [hget, hz]
  have decimalScaleCancel
      (A B N n : Nat) (p : Int) (hnN : n <= N)
      (hAB : A = B * 10 ^ (N - n)) :
      (A : Real) * (10 : Real) ^ (p - (N : Int)) =
        (B : Real) * (10 : Real) ^ (p - (n : Int)) := by
    have hcast : (A : Real) = (B : Real) * (10 : Real) ^ (N - n) := by
      norm_cast
    have hexp : p - (n : Int) =
        (p - (N : Int)) + ((N - n : Nat) : Int) := by
      rw [Nat.cast_sub hnN]
      omega
    rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
    norm_num only [zpow_natCast]
    rw [hcast]
    ring
  have trimLoopFull
      (a : Array Std.U8 768#usize) (N j : Std.Usize)
      (hNLen : N.val <= a.val.length)
      (hloop : num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop N a = ok j) :
      let xs := a.val.map (fun z => z.val)
      j.val <= N.val /\
        (forall q, j.val <= q -> q < N.val -> a.val[q]? = some 0#u8) /\
        (j.val = 0 \/ a.val[j.val - 1]? != some 0#u8) /\
        (xs.take N.val).foldl (fun acc z => 10 * acc + z) 0 =
          (xs.take j.val).foldl (fun acc z => 10 * acc + z) 0 *
            10 ^ (N.val - j.val) := by
    dsimp only
    let xs := a.val.map (fun z => z.val)
    let F := fun n : Nat =>
      (xs.take n).foldl (fun acc z => 10 * acc + z) 0
    let Inv := fun x : Std.Usize =>
      x.val <= N.val /\ x.val <= a.val.length /\
        (forall q, x.val <= q -> q < N.val -> a.val[q]? = some 0#u8) /\
        F N.val = F x.val * 10 ^ (N.val - x.val)
    let Post := fun x : Std.Usize =>
      Inv x /\ (x.val = 0 \/ a.val[x.val - 1]? != some 0#u8)
    let Body := fun x =>
      num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body a x
    have hp : Post j := by
      refine ResultLoopPartial Inv Post Body ?_ N j ?_ ?_
      · intro x hx r hr
        rcases hx with ⟨hxN, hxLen, hxzero, hxF⟩
        simp only [Body,
          num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop.body] at hr
        by_cases hx0 : x.val = 0
        · have hxEq : x = 0#usize :=
            UScalar.eq_of_val_eq (by simpa using hx0)
          subst x
          simp at hr
          subst r
          exact ⟨⟨hxN, hxLen, hxzero, hxF⟩, Or.inl rfl⟩
        · have hxpos : 0 < x.val := by omega
          have hxne : x != 0#usize := by
            simp only [bne_iff_ne]
            intro h
            apply hx0
            simpa [h]
          obtain ⟨x1, hx1, hx1val⟩ := Aeneas.Std.WP.spec_imp_exists
            (UScalar.sub_spec (x := x) (y := 1#usize)
              (by simpa using hxpos))
          norm_num at hx1val
          simp [hxne, hx1] at hr
          have hx1lt : x1.val < a.val.length := by omega
          obtain ⟨digit, hdigit, hdigitval⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.index_usize_spec a x1
              (by simpa [Array.length] using hx1lt))
          rw [hdigit] at hr
          by_cases hdz : digit = 0#u8
          · simp [hdz] at hr
            subst r
            simp only [Inv]
            refine ⟨?_, ?_, ?_, ?_⟩
            · omega
            · omega
            · intro q hqj hqN
              by_cases hqx : x.val <= q
              · exact hxzero q hqx hqN
              · have hq : q = x1.val := by omega
                subst q
                apply getElem?_eq_some_iff.mpr
                refine ⟨hx1lt, ?_⟩
                rw [← hdigitval, hdz]
            · have hxslen : xs.length = a.val.length := by simp [xs]
              have hxdigit : xs[x1.val] = 0 := by
                rw [show xs[x1.val] = digit.val by simp [xs, hdigitval]]
                simp [hdz]
              have hstep := foldTakeSuccZero xs x1.val (by omega) hxdigit
              have hstepF : F x.val = 10 * F x1.val := by
                simp only [F]
                simpa [show x.val = x1.val + 1 by omega] using hstep
              rw [hxF, hstepF]
              rw [show N.val - x1.val = (N.val - x.val) + 1 by omega,
                pow_succ]
              ring
          · simp [hdz] at hr
            subst r
            exact ⟨⟨hxN, hxLen, hxzero, hxF⟩, Or.inr (by
              simp only [bne_iff_ne]
              intro hz
              rcases getElem?_eq_some_iff.mp hz with ⟨_, hzval⟩
              apply hdz
              rw [hdigitval]
              simpa [hx1val] using hzval)⟩
      · refine ⟨le_rfl, hNLen, ?_, ?_⟩
        · intro q hq hq'
          omega
        · simp [F]
      · simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop, Body]
          using hloop
    rcases hp with ⟨⟨hjN, _, hjzero, hjF⟩, hjlast⟩
    exact ⟨hjN, hjzero, hjlast, hjF⟩
  have hle : d.num_digits <=
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
    simpa [UScalar.le_equiv,
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
      d.digits.property] using hbound
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim at htrim
  simp only [hle, massert_True, bind_tc_ok] at htrim
  cases hloopEq : num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
      d.num_digits d.digits with
  | fail e => simp [hloopEq] at htrim
  | div => simp [hloopEq] at htrim
  | ok j =>
    simp only [hloopEq, bind_tc_ok] at htrim
    cases htrim
    have hloop := trimLoopFull d.digits d.num_digits j hbound hloopEq
    rcases hloop with ⟨hjN, hjzero, hjlast, hjF⟩
    refine ⟨rfl, rfl, rfl, hjN, hjzero, hjlast, ?_⟩
    simpa [List.map_take] using
      (decimalScaleCancel _ _ _ _ d.decimal_point.val hjN hjF).symm

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": "DecimalSeqTrimSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "ca53ca800d27f55e1042ba6a4ada0e2b4bc4926ed402c75c4314d7a97e12af27",
  "active_statement_hash": "7ac1691828949e50206cfa7f85081716d98e14cfa27df47c9c222f8326d08f42",
  "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-155",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ResultLoopPartial": "566d2aeb76ed9100",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "cb99c951cfc3a86f"
  },
  "strict_definition_deps": {
    "ResultLoopPartial": "1b58e96ad00b23c2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "2214a6a2f57b8665"
  }
}
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:f9697f3256d3f172b5d38f08981156b6ba9cca2b7128bb28e160d34d1fd43796",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}