\begin{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 \texttt{$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$.
\end{lemma}
\begin{proof}
All members of $S$ are below ten by validity in
\noderef{DecimalGoalVocabularyDefault}; deletion of prefixes or suffixes
preserves that property.  Since the stored-prefix equality identifies every
retained byte value with a member of \texttt{$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 \texttt{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
\texttt{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.
\end{proof}
