DecimalSeqRetainedValue

Lean checked

Statement

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

Lemma · Retained decimal value and exact sticky error

Let x be a valid unsigned numeric lexeme and let d be a decimal sequence. Form the significand word S, delete its leading zero run to obtain C, and delete the trailing zero run of C to obtain C' of length N. Put P=|x.\mathit{integerDigits}|-(|S|-|C|) and let E be the accumulated explicit exponent. Assume that
n:=d.\mathit{num\_digits}=\min(N,768),
that the first n stored byte values are exactly C'.take n, that d.\mathit{truncated} is true exactly when N>768, and that d.\mathit{decimal\_point}=P+E.
Then n\le768, every retained cell is below ten, and, writing I for the base-ten fold of those cells, there is a real \eta such that
\operatorname{accumulatedMagnitude}(x) =I10^{d.\mathit{decimal\_point}-n}+\eta, \quad 0\le\eta<10^{d.\mathit{decimal\_point}-768},
with d.\mathit{truncated}=\mathrm{true} exactly when 0<\eta.

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.NumberHelperDecimalPrefixInterval
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Mathlib.Data.List.DropRight

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRetainedValue]
theorem DecimalSeqRetainedValue :
    forall (x : DecimalLexeme)
      (d : num.imp.dec2flt.decimal_seq.DecimalSeq),
      x.leadingSign = none ->
      DecimalGoalVocabularyDefault.validNumeric x ->
      let significand := x.integerDigits ++
        match x.fractionDigits with | none => [] | some ds => ds
      let significant := significand.dropWhile (fun digit => digit == 0)
      let trimmed :=
        (significant.reverse.dropWhile (fun digit => digit == 0)).reverse
      let leadingZeros := significand.length - significant.length
      let P : Int := (x.integerDigits.length : Int) - leadingZeros
      let E : Int :=
        DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
      d.num_digits.val = min trimmed.length 768 ->
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trimmed.take d.num_digits.val ->
      (d.truncated = true <-> 768 < trimmed.length) ->
      d.decimal_point.val = P + E ->
      d.num_digits.val <= 768 /\
        (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
        let retainedDigits :=
          (d.digits.val.take d.num_digits.val).map (fun z => z.val)
        let retainedInteger : Nat :=
          retainedDigits.foldl (fun n digit => 10 * n + digit) 0
        let retainedValue : Real :=
          retainedInteger * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int))
        let cutoff : Real :=
          (10 : Real) ^ (d.decimal_point.val - (768 : Int))
        exists eta : Real,
          DecimalGoalVocabularyDefault.accumulatedMagnitude x =
              retainedValue + eta /\
          0 <= eta /\ eta < cutoff /\
          (d.truncated = true <-> 0 < eta) := 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

All members of S are below ten by validity in DecimalGoalVocabularyDefault; deletion of prefixes or suffixes preserves that property. Since the stored-prefix equality identifies every retained byte value with a member of C'.take n, every retained cell is below ten. The count equality immediately gives n\le768.
Let q be the number of terminal zeroes deleted from C, and let V(W) denote the base-ten fold of a digit word W. Leading zero deletion does not change the fold, while terminal zero deletion gives
V(S)=V(C)=V(C')10^q. \tag{1}
The length identity N=|S|-(|S|-|C|)-q, together with |S|=|I_x|+|F_x| and the definition of P, gives
P+E-N=E-|F_x|+q. \tag{2}
Unfolding accumulatedMagnitude in the cited vocabulary and using (1)--(2) therefore yields
A:=\operatorname{accumulatedMagnitude}(x) =V(C')10^{P+E-N}. \tag{3}
This includes C'=[]: both folds are zero, so (3) holds independently of the exponent.
Split C' as its first n digits U followed by a suffix W. Let I=V(U) and J=V(W). Repeatedly applying the decimal-fold recurrence gives the exact integer identity
V(C')=I10^{N-n}+J,\qquad 0\le J<10^{N-n}. \tag{4}
The strict upper bound follows by induction from every suffix digit being in 0,\ldots,9. The stored-prefix hypothesis says that the Lean retainedInteger is exactly this I. Substitute (4) into (3), use d.\mathit{decimal\_point}=P+E, and define
\eta:=J10^{P+E-N}.
The laws of integer powers of the positive real number ten give
A=I10^{d.\mathit{decimal\_point}-n}+\eta. \tag{5}
If N\le768, then n=N, so W=[], J=0, and \eta=0. The cutoff is a power of the positive number ten and is therefore strictly positive; hence 0\le\eta<\textit{cutoff}. The assumed flag equivalence makes truncation false in this case.
If N>768, then n=768 and W is nonempty. Because C' was obtained by deleting all terminal zeroes, its last digit is nonzero. That digit belongs to W, so the decimal fold J is strictly positive. Positivity of powers of ten gives \eta>0. The strict bound in (4) is now J<10^{N-768}. Multiplying it by the positive 10^{P+E-N} yields
0<\eta<10^{N-768}10^{P+E-N} =10^{P+E-768} =10^{d.\mathit{decimal\_point}-768}. \tag{6}
The assumed flag equivalence makes truncation true exactly in this second case. The two cases are exhaustive, so (5)--(6) prove the value, error, cutoff, and exact sticky equivalence claimed in Lean.

Lean proof


  intro x d hsign hvalid
  dsimp only
  intro hnum hstored htruncated hpoint
  let S : List Nat := x.integerDigits ++
    match x.fractionDigits with | none => [] | some ds => ds
  let C : List Nat := S.dropWhile (fun digit => digit == 0)
  let T : List Nat := List.rdropWhile (fun digit => digit == 0) C
  let F : Nat := match x.fractionDigits with | none => 0 | some ds => ds.length
  let E : Int := DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
  change d.num_digits.val = min T.length 768 at hnum
  change (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
    T.take d.num_digits.val at hstored
  change (d.truncated = true ↔ 768 < T.length) at htruncated
  change d.decimal_point.val =
    (x.integerDigits.length : Int) - (S.length - C.length : Nat) + E at hpoint
  have hCS : C <:+ S := by
    exact List.dropWhile_suffix _
  have hTC : T <+: C := by
    exact List.rdropWhile_prefix _ _
  have hSdigits : forall digit, digit ∈ S -> digit < 10 := by
    intro digit hdigit
    rcases hvalid with ⟨hinteger, hfraction, hnonempty, hexponent⟩
    simp only [S, List.mem_append] at hdigit
    rcases hdigit with hdigit | hdigit
    · exact hinteger digit hdigit
    · cases hfrac : x.fractionDigits with
      | none => simp [hfrac] at hdigit
      | some ds =>
          simp only [hfrac] at hdigit hfraction
          exact hfraction digit hdigit
  have hTdigits : forall digit, digit ∈ T -> digit < 10 := by
    intro digit hdigit
    exact hSdigits digit (hCS.sublist.mem (hTC.sublist.mem hdigit))
  have hnle : d.num_digits.val ≤ 768 := by omega
  refine ⟨hnle, ?_, ?_⟩
  · intro z hz
    have hzmap : z.val ∈
        (d.digits.val.take d.num_digits.val).map (fun z => z.val) :=
      List.mem_map_of_mem hz
    rw [hstored] at hzmap
    exact hTdigits z.val (List.mem_of_mem_take hzmap)
  · dsimp only
    rw [hstored]
    let step : Nat -> Nat -> Nat := fun a digit => 10 * a + digit
    let n : Nat := d.num_digits.val
    let U : List Nat := T.take n
    let W : List Nat := T.drop n
    let m : Nat := U.foldl step 0
    let z : Nat := T.foldl step 0
    let full : Real := (z : Real) *
      (10 : Real) ^ (d.decimal_point.val - (T.length : Int))
    let retained : Real := (m : Real) *
      (10 : Real) ^ (d.decimal_point.val - (n : Int))
    let cutoff : Real :=
      (10 : Real) ^ (d.decimal_point.val - (768 : Int))
    have hn : n = min T.length 768 := by exact hnum
    have hnT : n ≤ T.length := by omega
    have hsplit : U ++ W = T := by
      exact List.take_append_drop n T
    have hWdigits : forall digit, digit ∈ W -> digit < 10 := by
      intro digit hdigit
      exact hTdigits digit (List.mem_of_mem_drop hdigit)
    have fold_shift : forall (ds : List Nat) (a : Nat),
        ds.foldl step a = a * 10 ^ ds.length + ds.foldl step 0 := by
      intro ds
      induction ds with
      | nil => intro a; simp
      | cons digit ds ih =>
          intro a
          simp only [List.foldl_cons, List.length_cons]
          rw [ih (step a digit), show step 0 digit = digit by simp [step], ih digit]
          simp only [step, Nat.pow_succ]
          ring
    have fold_leading : forall ds : List Nat,
        (ds.dropWhile (fun digit => digit == 0)).foldl step 0 =
          ds.foldl step 0 := by
      intro ds
      induction ds with
      | nil => rfl
      | cons digit ds ih =>
          by_cases hdigit : digit = 0
          · subst digit
            simpa [step] using ih
          · have hb : (digit == 0) = false :=
              beq_eq_false_iff_ne.mpr hdigit
            simp [List.dropWhile, hb]
    let Q : List Nat := List.rtakeWhile (fun digit => digit == 0) C
    have hTCQ : T ++ Q = C := by
      exact List.rdropWhile_append_rtakeWhile
    have hQzero : forall digit, digit ∈ Q -> digit = 0 := by
      intro digit hdigit
      have hb := List.mem_rtakeWhile_imp hdigit
      exact beq_iff_eq.mp hb
    have fold_zero : forall ds : List Nat,
        (forall digit, digit ∈ ds -> digit = 0) ->
        ds.foldl step 0 = 0 := by
      intro ds hzero
      induction ds with
      | nil => rfl
      | cons digit ds ih =>
          have hdigit := hzero digit List.mem_cons_self
          subst digit
          simp only [List.foldl_cons, step, Nat.mul_zero, Nat.zero_add]
          apply ih
          intro a ha
          exact hzero a (List.mem_cons_of_mem 0 ha)
    have hQfold : Q.foldl step 0 = 0 := fold_zero Q hQzero
    have hfoldS : C.foldl step 0 = S.foldl step 0 := by
      exact fold_leading S
    have hfoldC : C.foldl step 0 = T.foldl step 0 * 10 ^ Q.length := by
      rw [← hTCQ, List.foldl_append, fold_shift, hQfold]
      omega
    have hSlen : S.length = x.integerDigits.length + F := by
      cases hfrac : x.fractionDigits with
      | none => simp [S, F, hfrac]
      | some ds => simp [S, F, hfrac]
    have hCTlen : C.length = T.length + Q.length := by
      rw [← hTCQ, List.length_append]
    have hCLeS : C.length ≤ S.length := hCS.sublist.length_le
    have hexponent :
        (x.integerDigits.length : Int) - (S.length - C.length : Nat) + E -
            (T.length : Int) =
          (Q.length : Int) + (E - (F : Int)) := by
      rw [Nat.cast_sub hCLeS]
      push_cast at hSlen hCTlen ⊢
      omega
    have hmagnitude :
        DecimalGoalVocabularyDefault.accumulatedMagnitude x =
          ((S.foldl step 0 : Nat) : Real) *
            (10 : Real) ^ (E - (F : Int)) := by
      cases hfrac : x.fractionDigits <;>
        simp [DecimalGoalVocabularyDefault, S, step, E, F, hfrac]
    have hfull :
        DecimalGoalVocabularyDefault.accumulatedMagnitude x = full := by
      rw [hmagnitude]
      dsimp only [full, z]
      rw [← hfoldS, hfoldC]
      push_cast
      rw [hpoint, hexponent, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
      norm_num
      ring
    have hWlen : W.length = T.length - n := by
      simp [W]
    have hscale :
        d.decimal_point.val - (T.length : Int) + (W.length : Int) =
          d.decimal_point.val - (n : Int) := by
      rw [hWlen, Nat.cast_sub hnT]
      omega
    have hprefix := NumberHelperDecimalPrefixInterval U W hWdigits
      (d.decimal_point.val - (T.length : Int))
    dsimp only at hprefix
    rw [hsplit, hscale] at hprefix
    change retained ≤ full /\
      full < (((m + 1 : Nat) : Real)) *
        (10 : Real) ^ (d.decimal_point.val - (n : Int)) at hprefix
    have hfoldSplit : z = m * 10 ^ W.length + W.foldl step 0 := by
      dsimp only [z, m]
      rw [← hsplit, List.foldl_append]
      exact fold_shift W (U.foldl step 0)
    have hdecomp : full = retained +
        ((W.foldl step 0 : Nat) : Real) *
          (10 : Real) ^ (d.decimal_point.val - (T.length : Int)) := by
      dsimp only [full, retained]
      rw [hfoldSplit]
      push_cast
      rw [← hscale, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
      norm_num
      ring
    have hetaNonnegative : 0 ≤ full - retained := by
      linarith [hprefix.1]
    refine ⟨full - retained, ?_, hetaNonnegative, ?_, ?_⟩
    · rw [hfull]
      ring
    · by_cases hlong : 768 < T.length
      · have hn768 : n = 768 := by omega
        have hmcast : (((m + 1 : Nat) : Real)) = (m : Real) + 1 := by
          norm_num
        calc
          full - retained <
              (((m + 1 : Nat) : Real)) *
                  (10 : Real) ^ (d.decimal_point.val - (n : Int)) -
                retained := sub_lt_sub_right hprefix.2 retained
          _ = cutoff := by
            simp only [retained, cutoff, hn768, hmcast]
            ring_nf
      · have hnlength : n = T.length := by omega
        have hWnil : W = [] := by simp [W, hnlength]
        have heta : full - retained = 0 := by
          rw [hdecomp, hWnil]
          simp
        rw [heta]
        exact zpow_pos (by norm_num) _
    · by_cases hlong : 768 < T.length
      · have hn768 : n = 768 := by omega
        have hWlenPos : 0 < W.length := by
          rw [hWlen, hn768]
          omega
        have hWne : W ≠ [] := List.ne_nil_of_length_pos hWlenPos
        have hTne : T ≠ [] := by
          intro hnil
          rw [hnil] at hlong
          simp at hlong
        have hlastNotZero : T.getLast hTne ≠ 0 := by
          have hlast := List.rdropWhile_last_not
            (fun digit : Nat => digit == 0) C
            (by simpa only [T] using hTne)
          simpa only [Bool.not_eq_true, beq_eq_false_iff_ne] using hlast
        have hlastEq : T.getLast hTne = W.getLast hWne := by
          simpa only [← hsplit] using
            List.getLast_append_of_right_ne_nil U W hWne
        have hWlastPos : 0 < W.getLast hWne := by
          rw [← hlastEq]
          omega
        have hWfoldPos : 0 < W.foldl step 0 := by
          rw [← List.dropLast_append_getLast hWne, List.foldl_append]
          simp only [List.foldl_cons, List.foldl_nil, step]
          omega
        have hsuffixPos : 0 < ((W.foldl step 0 : Nat) : Real) *
            (10 : Real) ^ (d.decimal_point.val - (T.length : Int)) :=
          mul_pos (by exact_mod_cast hWfoldPos) (zpow_pos (by norm_num) _)
        have hetaPos : 0 < full - retained := by
          rw [hdecomp]
          linarith
        constructor
        · intro hflag
          exact hetaPos
        · intro heta
          exact htruncated.mpr hlong
      · have hnlength : n = T.length := by omega
        have hWnil : W = [] := by simp [W, hnlength]
        have heta : full - retained = 0 := by
          rw [hdecomp, hWnil]
          simp
        constructor
        · intro hflag
          have := htruncated.mp hflag
          omega
        · intro hpositive
          rw [heta] at hpositive
          linarith

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Mathlib.Data.List.DropRight

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRetainedValue]
theorem DecimalSeqRetainedValue :
    forall (x : DecimalLexeme)
      (d : num.imp.dec2flt.decimal_seq.DecimalSeq),
      x.leadingSign = none ->
      DecimalGoalVocabularyDefault.validNumeric x ->
      let significand := x.integerDigits ++
        match x.fractionDigits with | none => [] | some ds => ds
      let significant := significand.dropWhile (fun digit => digit == 0)
      let trimmed :=
        (significant.reverse.dropWhile (fun digit => digit == 0)).reverse
      let leadingZeros := significand.length - significant.length
      let P : Int := (x.integerDigits.length : Int) - leadingZeros
      let E : Int :=
        DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
      d.num_digits.val = min trimmed.length 768 ->
      (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
        trimmed.take d.num_digits.val ->
      (d.truncated = true <-> 768 < trimmed.length) ->
      d.decimal_point.val = P + E ->
      d.num_digits.val <= 768 /\
        (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
        let retainedDigits :=
          (d.digits.val.take d.num_digits.val).map (fun z => z.val)
        let retainedInteger : Nat :=
          retainedDigits.foldl (fun n digit => 10 * n + digit) 0
        let retainedValue : Real :=
          retainedInteger * (10 : Real) ^
            (d.decimal_point.val - (d.num_digits.val : Int))
        let cutoff : Real :=
          (10 : Real) ^ (d.decimal_point.val - (768 : Int))
        exists eta : Real,
          DecimalGoalVocabularyDefault.accumulatedMagnitude x =
              retainedValue + eta /\
          0 <= eta /\ eta < cutoff /\
          (d.truncated = true <-> 0 < eta) := by
-- BODY
  intro x d hsign hvalid
  dsimp only
  intro hnum hstored htruncated hpoint
  let S : List Nat := x.integerDigits ++
    match x.fractionDigits with | none => [] | some ds => ds
  let C : List Nat := S.dropWhile (fun digit => digit == 0)
  let T : List Nat := List.rdropWhile (fun digit => digit == 0) C
  let F : Nat := match x.fractionDigits with | none => 0 | some ds => ds.length
  let E : Int := DecimalGoalVocabularyDefault.accumulatedExplicitExponent x
  change d.num_digits.val = min T.length 768 at hnum
  change (d.digits.val.take d.num_digits.val).map (fun z => z.val) =
    T.take d.num_digits.val at hstored
  change (d.truncated = true ↔ 768 < T.length) at htruncated
  change d.decimal_point.val =
    (x.integerDigits.length : Int) - (S.length - C.length : Nat) + E at hpoint
  have hCS : C <:+ S := by
    exact List.dropWhile_suffix _
  have hTC : T <+: C := by
    exact List.rdropWhile_prefix _ _
  have hSdigits : forall digit, digit ∈ S -> digit < 10 := by
    intro digit hdigit
    rcases hvalid with ⟨hinteger, hfraction, hnonempty, hexponent⟩
    simp only [S, List.mem_append] at hdigit
    rcases hdigit with hdigit | hdigit
    · exact hinteger digit hdigit
    · cases hfrac : x.fractionDigits with
      | none => simp [hfrac] at hdigit
      | some ds =>
          simp only [hfrac] at hdigit hfraction
          exact hfraction digit hdigit
  have hTdigits : forall digit, digit ∈ T -> digit < 10 := by
    intro digit hdigit
    exact hSdigits digit (hCS.sublist.mem (hTC.sublist.mem hdigit))
  have hnle : d.num_digits.val ≤ 768 := by omega
  refine ⟨hnle, ?_, ?_⟩
  · intro z hz
    have hzmap : z.val ∈
        (d.digits.val.take d.num_digits.val).map (fun z => z.val) :=
      List.mem_map_of_mem hz
    rw [hstored] at hzmap
    exact hTdigits z.val (List.mem_of_mem_take hzmap)
  · dsimp only
    rw [hstored]
    let step : Nat -> Nat -> Nat := fun a digit => 10 * a + digit
    let n : Nat := d.num_digits.val
    let U : List Nat := T.take n
    let W : List Nat := T.drop n
    let m : Nat := U.foldl step 0
    let z : Nat := T.foldl step 0
    let full : Real := (z : Real) *
      (10 : Real) ^ (d.decimal_point.val - (T.length : Int))
    let retained : Real := (m : Real) *
      (10 : Real) ^ (d.decimal_point.val - (n : Int))
    let cutoff : Real :=
      (10 : Real) ^ (d.decimal_point.val - (768 : Int))
    have hn : n = min T.length 768 := by exact hnum
    have hnT : n ≤ T.length := by omega
    have hsplit : U ++ W = T := by
      exact List.take_append_drop n T
    have hWdigits : forall digit, digit ∈ W -> digit < 10 := by
      intro digit hdigit
      exact hTdigits digit (List.mem_of_mem_drop hdigit)
    have fold_shift : forall (ds : List Nat) (a : Nat),
        ds.foldl step a = a * 10 ^ ds.length + ds.foldl step 0 := by
      intro ds
      induction ds with
      | nil => intro a; simp
      | cons digit ds ih =>
          intro a
          simp only [List.foldl_cons, List.length_cons]
          rw [ih (step a digit), show step 0 digit = digit by simp [step], ih digit]
          simp only [step, Nat.pow_succ]
          ring
    have fold_leading : forall ds : List Nat,
        (ds.dropWhile (fun digit => digit == 0)).foldl step 0 =
          ds.foldl step 0 := by
      intro ds
      induction ds with
      | nil => rfl
      | cons digit ds ih =>
          by_cases hdigit : digit = 0
          · subst digit
            simpa [step] using ih
          · have hb : (digit == 0) = false :=
              beq_eq_false_iff_ne.mpr hdigit
            simp [List.dropWhile, hb]
    let Q : List Nat := List.rtakeWhile (fun digit => digit == 0) C
    have hTCQ : T ++ Q = C := by
      exact List.rdropWhile_append_rtakeWhile
    have hQzero : forall digit, digit ∈ Q -> digit = 0 := by
      intro digit hdigit
      have hb := List.mem_rtakeWhile_imp hdigit
      exact beq_iff_eq.mp hb
    have fold_zero : forall ds : List Nat,
        (forall digit, digit ∈ ds -> digit = 0) ->
        ds.foldl step 0 = 0 := by
      intro ds hzero
      induction ds with
      | nil => rfl
      | cons digit ds ih =>
          have hdigit := hzero digit List.mem_cons_self
          subst digit
          simp only [List.foldl_cons, step, Nat.mul_zero, Nat.zero_add]
          apply ih
          intro a ha
          exact hzero a (List.mem_cons_of_mem 0 ha)
    have hQfold : Q.foldl step 0 = 0 := fold_zero Q hQzero
    have hfoldS : C.foldl step 0 = S.foldl step 0 := by
      exact fold_leading S
    have hfoldC : C.foldl step 0 = T.foldl step 0 * 10 ^ Q.length := by
      rw [← hTCQ, List.foldl_append, fold_shift, hQfold]
      omega
    have hSlen : S.length = x.integerDigits.length + F := by
      cases hfrac : x.fractionDigits with
      | none => simp [S, F, hfrac]
      | some ds => simp [S, F, hfrac]
    have hCTlen : C.length = T.length + Q.length := by
      rw [← hTCQ, List.length_append]
    have hCLeS : C.length ≤ S.length := hCS.sublist.length_le
    have hexponent :
        (x.integerDigits.length : Int) - (S.length - C.length : Nat) + E -
            (T.length : Int) =
          (Q.length : Int) + (E - (F : Int)) := by
      rw [Nat.cast_sub hCLeS]
      push_cast at hSlen hCTlen ⊢
      omega
    have hmagnitude :
        DecimalGoalVocabularyDefault.accumulatedMagnitude x =
          ((S.foldl step 0 : Nat) : Real) *
            (10 : Real) ^ (E - (F : Int)) := by
      cases hfrac : x.fractionDigits <;>
        simp [DecimalGoalVocabularyDefault, S, step, E, F, hfrac]
    have hfull :
        DecimalGoalVocabularyDefault.accumulatedMagnitude x = full := by
      rw [hmagnitude]
      dsimp only [full, z]
      rw [← hfoldS, hfoldC]
      push_cast
      rw [hpoint, hexponent, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
      norm_num
      ring
    have hWlen : W.length = T.length - n := by
      simp [W]
    have hscale :
        d.decimal_point.val - (T.length : Int) + (W.length : Int) =
          d.decimal_point.val - (n : Int) := by
      rw [hWlen, Nat.cast_sub hnT]
      omega
    have hprefix := NumberHelperDecimalPrefixInterval U W hWdigits
      (d.decimal_point.val - (T.length : Int))
    dsimp only at hprefix
    rw [hsplit, hscale] at hprefix
    change retained ≤ full /\
      full < (((m + 1 : Nat) : Real)) *
        (10 : Real) ^ (d.decimal_point.val - (n : Int)) at hprefix
    have hfoldSplit : z = m * 10 ^ W.length + W.foldl step 0 := by
      dsimp only [z, m]
      rw [← hsplit, List.foldl_append]
      exact fold_shift W (U.foldl step 0)
    have hdecomp : full = retained +
        ((W.foldl step 0 : Nat) : Real) *
          (10 : Real) ^ (d.decimal_point.val - (T.length : Int)) := by
      dsimp only [full, retained]
      rw [hfoldSplit]
      push_cast
      rw [← hscale, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
      norm_num
      ring
    have hetaNonnegative : 0 ≤ full - retained := by
      linarith [hprefix.1]
    refine ⟨full - retained, ?_, hetaNonnegative, ?_, ?_⟩
    · rw [hfull]
      ring
    · by_cases hlong : 768 < T.length
      · have hn768 : n = 768 := by omega
        have hmcast : (((m + 1 : Nat) : Real)) = (m : Real) + 1 := by
          norm_num
        calc
          full - retained <
              (((m + 1 : Nat) : Real)) *
                  (10 : Real) ^ (d.decimal_point.val - (n : Int)) -
                retained := sub_lt_sub_right hprefix.2 retained
          _ = cutoff := by
            simp only [retained, cutoff, hn768, hmcast]
            ring_nf
      · have hnlength : n = T.length := by omega
        have hWnil : W = [] := by simp [W, hnlength]
        have heta : full - retained = 0 := by
          rw [hdecomp, hWnil]
          simp
        rw [heta]
        exact zpow_pos (by norm_num) _
    · by_cases hlong : 768 < T.length
      · have hn768 : n = 768 := by omega
        have hWlenPos : 0 < W.length := by
          rw [hWlen, hn768]
          omega
        have hWne : W ≠ [] := List.ne_nil_of_length_pos hWlenPos
        have hTne : T ≠ [] := by
          intro hnil
          rw [hnil] at hlong
          simp at hlong
        have hlastNotZero : T.getLast hTne ≠ 0 := by
          have hlast := List.rdropWhile_last_not
            (fun digit : Nat => digit == 0) C
            (by simpa only [T] using hTne)
          simpa only [Bool.not_eq_true, beq_eq_false_iff_ne] using hlast
        have hlastEq : T.getLast hTne = W.getLast hWne := by
          simpa only [← hsplit] using
            List.getLast_append_of_right_ne_nil U W hWne
        have hWlastPos : 0 < W.getLast hWne := by
          rw [← hlastEq]
          omega
        have hWfoldPos : 0 < W.foldl step 0 := by
          rw [← List.dropLast_append_getLast hWne, List.foldl_append]
          simp only [List.foldl_cons, List.foldl_nil, step]
          omega
        have hsuffixPos : 0 < ((W.foldl step 0 : Nat) : Real) *
            (10 : Real) ^ (d.decimal_point.val - (T.length : Int)) :=
          mul_pos (by exact_mod_cast hWfoldPos) (zpow_pos (by norm_num) _)
        have hetaPos : 0 < full - retained := by
          rw [hdecomp]
          linarith
        constructor
        · intro hflag
          exact hetaPos
        · intro heta
          exact htruncated.mpr hlong
      · have hnlength : n = T.length := by omega
        have hWnil : W = [] := by simp [W, hnlength]
        have heta : full - retained = 0 := by
          rw [hdecomp, hWnil]
          simp
        constructor
        · intro hflag
          have := htruncated.mp hflag
          omega
        · intro hpositive
          rw [heta] at hpositive
          linarith

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
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 9 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqRetainedValue",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "1c41fa07782280e7c2a192caa29aef773933b345222356e2e71b61963e28efcb",
  "active_statement_hash": "7c67dbbcda7652213d1bc0944c9431e47ae357c1e9c46643a49f05a40dc44085",
  "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-165",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
    "NumberHelperDecimalPrefixInterval": "5c32858414034b55"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "4634372c8f7a7f44",
    "DecimalLexeme": "250be645520aec86",
    "F64": "ec4e25fbaf4e11d4",
    "num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f",
    "num_imp_dec2flt_decimal_Decimal": "63fd2f021dd0ef41",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "c43614ff2149ecf2"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:4d117980194bd2fe48b38fef9e533639e2a37717c4230c5facf641b663b14c6b",
  "current_fingerprint": "sound-v2:4d117980194bd2fe48b38fef9e533639e2a37717c4230c5facf641b663b14c6b",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}