\begin{lemma}[Nearest-even rounding from an arbitrary open endpoint]
Let a terminally trimmed decimal record $d$ have at most 768 decimal cells
and canonical zero or positive-leading shape.  Put $V=V(d)$ and suppose
\[
 X=V+\eta,\qquad B=V+u,qquad0\leq\eta<u,qquad
 d.\mathit{truncated}\iff0<\eta.
\]
Assume $p<19$, $0\leq X<2^{64}-1/2$, and, for every natural $n$,
\[
 n+\tfrac12\leq V\quad\hbox{or}\quad B\leq n+\tfrac12. \tag{1}
\]
Then the extracted decimal-sequence round returns a U64 $m$ satisfying
\[
 X\leq m+\tfrac12,qquad m\leq X+\tfrac12,
\]
and equality at either adjacent midpoint implies that $m$ is even.  Negative
decimal points are permitted.
\end{lemma}
\begin{proof}
Unfold \noderef{num_imp_dec2flt_decimal_seq_DecimalSeq_round}.  If the digit
count is zero, canonicality gives $X=0$, and the first program branch returns
the U64 zero.  Its two distance inequalities and parity implication are
immediate.  Henceforth canonicality supplies a nonempty digit list with a
positive leading digit, the stated length, and nonzero final digit.  Also
$X<B$, because $X=V+\eta$, $B=V+u$, and $\eta<u$.

Suppose first that $p<0$.  Apply
\noderef{NumberHelperDecimalPrefixInterval} with empty prefix and the full
digit list as suffix.  It gives $V<10^p\leq10^{-1}<1/2$.  In (1) at $n=0$
the alternative $1/2\leq V$ is impossible, so $B\leq1/2$, and therefore
$0\leq X<B\leq1/2$.  The extracted negative-point branch returns zero.
The strict upper inequality excludes either midpoint equality, and all four
required conclusions follow.

It remains that $0\leq p<19$.  The capacity equality in
\noderef{DecimalSeqPrimitiveSemantics} says that the extracted overflow
threshold has value 19.  Thus its checked cast to I32 returns $19$, the guard
$p\geq19$ is false, and the checked I32-to-usize cast returns a value $q$
with $q=p$ and $q\leq18$.  In particular $q+1$ fits in usize.

For the extracted range loop define $D_j$ to be the stored digit when
$j<r$, where $r$ is the digit count, and zero otherwise, and define

\[
 F_k=\operatorname{foldl}(a,j\mapsto10a+D_j,0,[0,\ldots,k-1]).
\]

The loop invariant at range start $k$ is: $k\leq q$, the range endpoint is
$q$, and the U64 accumulator has value $F_k$.  Every $D_j<10$, so induction
gives $F_k<10^k$.  On a continuation $k<q\leq18$; consequently the checked
multiplication by ten, and the optional addition of a digit, produce
$F_{k+1}<10^{k+1}\leq10^{18}<2^{64}$ and cannot fail.  If $k<r$, then
$k<r\leq768$, so the guarded access is within the fixed 768-cell array and
the U8-to-U64 cast is exact; if $k\geq r$, the program adds zero.  The checked
usize successor is safe because $k+1\leq q\leq18$.  The measure $q-k$
strictly decreases.  The preservation part is the result-loop principle
\noderef{ResultLoopPartial}; the decreasing measure also proves normal
termination.  The loop therefore returns a U64 $N$ with value $F_q$.

If $r\leq q$, splitting the zero-padded fold after position $r$ yields
$N=I10^{q-r}$, where $I$ is the base-ten value of all retained digits.
Since $p=q$, this is exactly $V$.  The test $q<r$ is false, so the extracted
code sets \texttt{round\_up} to false and returns $N$.  We have
$N=V\leq X$.  Applying (1) at $n=N$ rules out $N+1/2\leq V=N$ and gives
$B\leq N+1/2$; hence $X<B\leq N+1/2$.  These inequalities prove the result,
and their strictness excludes a midpoint tie.

Now assume $q<r$.  The loop value is the base-ten value $N$ of the first
$q$ digits.  Applying \noderef{NumberHelperDecimalPrefixInterval} first at
that prefix and then at the prefix through digit $q$ gives

\[
 N\leq V<N+1.
\]

Writing $f$ for digit $q$, the same bounds give $V<N+1/2$ when $f<5$ and
$V>N+1/2$ when $f>5$.  If $f=5$ and $q+1=r$, direct evaluation gives
$V=N+1/2$.  If $f=5$ and $q+1<r$, terminal trimming says the remaining suffix
ends in a nonzero digit; its base-ten value is therefore positive, so
$V>N+1/2$.  The checked array read of $f$ is safe because $q<r\leq768$, and
the already established bound $q\leq18$ makes the checked computation of
$q+1$ safe.

In every below-midpoint case, (1) at $n=N$ forces $B\leq N+1/2$, whence
$X<B\leq N+1/2$ and the program correctly leaves $N$ unchanged.  In every
strictly above-midpoint case, $X=V+\eta\geq V>N+1/2$, and the program chooses
to increment.  For this latter branch one must also control the other side
of the nearest-integer interval.  Since $V<N+1$, (1) at $n=N+1$ cannot take
the alternative $N+3/2\leq V$; it gives $B\leq N+3/2$, and hence
$X<B\leq N+3/2$.  Together with $N+1/2<X$ this proves both distance bounds
for $N+1$ and excludes both adjacent midpoint equalities.  Moreover
$N+1/2<X<2^{64}-1/2$ implies $N+1<2^{64}$, so the checked U64 increment
succeeds.

It remains only an exact terminal-five case.  If the truncation flag is true,
the hypothesis on that flag gives $\eta>0$, so the preceding strict
above-midpoint argument applies.  If it is false, then $\eta=0$ and
$X=V=N+1/2$.  For $q=0$ the extracted special branch returns $N=0$, which is
even.  For $q>0$, the checked subtraction $q-1$ is safe and
$q-1<r\leq768$ makes the preceding-digit array read safe.  If that digit is
$c$, the fold identity is $N=10P+c$, so $N\bmod2=c\bmod2$.  Directly from the
U8 bit-vector semantics,

\[
 \operatorname{val}(1\mathbin{\&}c)=c\bmod2.
\]

Thus the extracted low-bit test is false exactly when $N$ is even.  It keeps
an even $N$ and increments an odd $N$; in the latter case $N+1$ is even and
the same $N+1<2^{64}$ argument proves the increment safe.  The returned
endpoint is therefore even at the sole possible tie, completing every branch
and every failure point of the extracted computation.
\end{proof}
