P529_T1_RoundArbitraryEndpoint

Lean checked

Statement

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

Lemma · Normal nearest-even extraction for an arbitrary endpoint

Let d have count at most 768 and decimal retained cells. Assume either d.n=0, false sticky and X=0, or positive exact-length retained shape and a nonzero last retained cell. Suppose p<19, 0\le X<2^{64}-1/2 and
X=V(d)+\eta,\quad Y=V(d)+u,\quad 0\le\eta<u,\quad d.\mathit{truncated}\iff\eta>0 .
If for every natural n, n+1/2\le V(d) or Y\le n+1/2, then the actual round call returns normally to a U64 m with
X\le m+\tfrac12,\quad m\le X+\tfrac12,\quad (X=m+\tfrac12\ \lor\ X=m-\tfrac12)\Longrightarrow m\bmod2=0.
The endpoint Y need not be a fresh decimal cutoff.

Lean statement

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

import Tablet.DecimalSeqRoundLoopValue
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.P472_T1_DecimalValueBounds
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_RoundArbitraryEndpoint]
theorem P529_T1_RoundArbitraryEndpoint (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (X Y : Real)
    (hcap : d.num_digits.val <= 768)
    (hdigits : forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10)
    (hshape : (d.num_digits.val = 0 /\ d.truncated = false /\ X = 0) \/
      ((exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val) /\
        d.digits.val[d.num_digits.val - 1]? ≠ some 0#u8))
    (hpoint : d.decimal_point.val < 19)
    (hXlo : 0 <= X) (hXhi : X < (2 : Real) ^ (64 : Nat) - 1 / 2) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    (exists eta u : Real, X = V d + eta /\ Y = V d + u /\
      0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) ->
    (forall n : Nat, (n : Real) + 1 / 2 <= V d \/ Y <= (n : Real) + 1 / 2) ->
    exists m : Std.U64,
      num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
      X <= (m.val : Real) + 1 / 2 /\ (m.val : Real) <= X + 1 / 2 /\
      (X = (m.val : Real) + 1 / 2 \/ X = (m.val : Real) - 1 / 2 ->
        m.val % 2 = 0) := 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 num imp dec2flt decimal seq DecimalSeq round. In the zero case it returns U64 zero, and X=0 proves the conclusions. In the other case the word is nonempty and its last mapped digit is nonzero: the physical last index is d.n-1<768, and exact retained length identifies the last mapped digit with that byte. The endpoint witnesses give X<Y.
If p<0, DecimalValueBounds gives V(d)<10^p\le1/10<1/2. Separation at n=0 must therefore give Y\le1/2, hence 0\le X<Y\le1/2. The negative-point branch returns zero, satisfies both distance bounds and has no possible midpoint equality.
For 0\le p<19, the literal threshold is 19 by num imp dec2flt decimal seq DecimalSeq MAX DIGITS WITHOUT OVERFLOW. Its cast to I32 is exact, the saturation guard is false, and the I32-to-Usize cast of p is exact with value q=p\le18. These casts use the ratified width in Assumptions. Apply DecimalSeqRoundLoopValue at this q, the given count and digit bounds. It constructs the actual normal range-loop result N: it is the first-q fold if q<d.n, and the full fold padded by 10^{q-d.n} otherwise. The normal loop and its value are supplied by that principal, so no termination of the enclosing round is assumed.
If d.n\le q, then N=V(d), the fractional-digit guard is false, and the result is N. Separation at natural N excludes N+1/2\le V(d)=N, hence gives Y\le N+1/2. Thus N\le X<Y\le N+1/2, proving both bounds without a tie.
If q<d.n, apply DecimalSeqFractionalDigitInterval to the mapped digit word, its established nonzero last digit, and index q. It gives N\le V(d)<N+1 and the exact trichotomy at N+1/2: a first fractional digit below five is strictly below; one above five is strictly above; a five followed by any remaining digit is strictly above because of terminal trimming; a terminal five is exactly equal. The array read is safe since q<d.n\le768. For a five, q+1\le19 makes its checked Usize addition safe.
Below the midpoint separation at N forces Y\le N+1/2, so N\le X<N+1/2 and the unchanged result has the required bounds. Above the midpoint, or at a terminal five with true sticky, X>N+1/2. Separation at N+1 and V(d)<N+1 force Y\le N+3/2, hence N+1/2<X<Y\le N+3/2. The code increments in exactly these cases. The two strict inequalities give the distance bounds for N+1 and exclude midpoint equality. The increment cannot overflow: N+1/2<X<2^{64}-1/2 gives N+1<2^{64}.
For a terminal five with false sticky, \eta\ge0 and the exact sticky equivalence imply \eta=0, so X=N+1/2. If q=0, the special branch keeps N=0, which is even. Otherwise q-1 is a safe Usize subtraction and the preceding byte read is in bounds. If that byte has value c, the fold identity is N=10P+c, so N\bmod2=c\bmod2. The unsigned-byte bitvector semantics gives \mathit{val}(1\mathbin{\&}c)=c\bmod2. The branch therefore keeps an even N and increments an odd N to an even N+1. In the latter case the same safe-range argument X=N+1/2<2^{64}-1/2 proves the increment legal. The selected result satisfies both nearest inequalities and is even at the only possible tie. These cases exhaust every guard and checked operation, establishing normality and all stated output laws.

Lean proof


  exact (set_option maxHeartbeats 40000000 in (by
      dsimp only
      intro hw hsep
      rcases hshape with hzero | hpos
      · rcases hzero with ⟨hn, ht, hX⟩
        subst X
        rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
        simp [hn] at hXe hYe
        have hY : Y = u := by linarith
        have hU : 0 < u := by linarith
        refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
        · simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hn]
        · simp
        · simp
        · intro hmid
          simp
      · rcases hpos with ⟨⟨first, tail, hmap, hfirstlo, hfirsthi, hlen⟩, hlast⟩
        let digits := (d.digits.val.take d.num_digits.val).map (fun b => b.val)
        have hdigitsLen : digits.length = d.num_digits.val := by
          simp [digits]
          have ha : d.digits.val.length = 768 := by simpa using d.digits.property
          omega
        have hword : digits = first :: tail := by simpa [digits] using hmap
        have hnpos : 0 < d.num_digits.val := by
          rw [← hlen]
          simp
        have hlastWord : digits.getLast? ≠ some 0 := by
          have hlastEq : digits.getLast? =
              (d.digits.val[d.num_digits.val - 1]?).map (fun b => b.val) := by
            rw [List.getLast?_eq_getElem?]
            simp only [digits, List.getElem?_map, List.getElem?_take]
            simp [hcap, Nat.sub_lt hnpos]
          intro heq
          rw [hlastEq] at heq
          rcases Option.map_eq_some_iff.mp heq with ⟨b, hb, hbval⟩
          have hbzero : b = 0#u8 := by scalar_tac
          apply hlast
          rw [hb, hbzero]
        have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
          intro z hz
          have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
              (fun b => b.val) := by simpa [hword, digits] using hz
          rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
          exact hbz ▸ hdigits b hb
        by_cases hnegative : d.decimal_point.val < 0
        · have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
            intro z hz
            have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
                (fun b => b.val) := by simpa [hword, digits] using hz
            rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
            exact hbz ▸ hdigits b hb
          have hvbound := P472_T1_DecimalValueBounds
            (first :: tail) first tail rfl hfirstlo hdigitNat d.decimal_point.val
          have hVlt :
              ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                (fun n z => 10 * n + z) 0 : Nat) : Real) *
                  (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
              (1 : Real) / 2 := by
            have hv :
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun n z => 10 * n + z) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                (10 : Real) ^ d.decimal_point.val := by
              have hv' := hvbound.2
              rw [← hword] at hv'
              rw [hdigitsLen] at hv'
              simpa [digits] using hv'
            have hp : (10 : Real) ^ d.decimal_point.val < 1 / 2 := by
              let k : Nat := Int.toNat (-d.decimal_point.val - 1)
              have hk : d.decimal_point.val = -(k : Int) - 1 := by
                have hp0 : 0 ≤ -d.decimal_point.val - 1 := by omega
                have ht : ((-d.decimal_point.val - 1).toNat : Int) =
                    -d.decimal_point.val - 1 := Int.toNat_of_nonneg hp0
                simp only [k]
                omega
              rw [hk, show -(k : Int) - 1 = -((k + 1 : Nat) : Int) by omega,
                zpow_neg, zpow_natCast]
              have hden : (10 : Real) ≤ (10 : Real) ^ (k + 1) := by
                calc
                  (10 : Real) = 10 ^ (1 : Nat) := by norm_num
                  _ ≤ 10 ^ (k + 1) := by
                    exact_mod_cast (Nat.pow_le_pow_right (by norm_num : 0 < 10)
                      (show 1 ≤ k + 1 by omega))
              have hinv : ((10 : Real) ^ (k + 1))⁻¹ ≤ (10 : Real)⁻¹ :=
                (inv_le_inv₀ (by positivity : (0 : Real) < (10 : Real) ^ (k + 1))
                  (by norm_num : (0 : Real) < 10)).2 hden
              norm_num at hinv ⊢
              linarith
            exact lt_trans hv hp
          rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
          have hsep0 := hsep 0
          have hYhalf : Y <= 1 / 2 := by
            rcases hsep0 with hbad | hgood
            · linarith
            · simpa using hgood
          have hXhalf : X < 1 / 2 := by
            have hXV : (0 : Real) <=
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun n z => 10 * n + z) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
              exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num) _))
            have hxle :
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun n z => 10 * n + z) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
              rw [hXe]
              linarith
            linarith
          refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
          · have hp : d.decimal_point < 0#i32 := by scalar_tac
            simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hp]
          · simpa using le_of_lt hXhalf
          · norm_num
            linarith
          · intro _
            norm_num
        · have hpLower : 0 <= d.decimal_point.val := by
            omega
          have hpUpper : d.decimal_point.val <= 18 := by
            exact Int.le_of_lt_add_one (by simpa using hpoint)
          have hpointNonneg : ¬ d.decimal_point < 0#i32 := by scalar_tac
          have hpointSmall : d.decimal_point < 19#i32 := by scalar_tac
          obtain ⟨q, hqCast, hqval⟩ := Aeneas.Std.WP.spec_imp_exists
            (IScalar.hcast_inBounds_spec .Usize d.decimal_point (by scalar_tac))
          change lift (IScalar.hcast .Usize d.decimal_point) = ok q at hqCast
          have hqBound : q.val <= 18 := by omega
          obtain ⟨n, hnLoop, hnPrefix, hnPadded⟩ :=
            DecimalSeqRoundLoopValue d q hqBound hcap hdigits
          let integer := (digits.foldl (fun z x => 10 * z + x) 0)
          have hnValue :
              (q.val < d.num_digits.val ->
                n.val = (digits.take q.val).foldl (fun z x => 10 * z + x) 0) ∧
              (d.num_digits.val <= q.val ->
                n.val = integer * 10 ^ (q.val - d.num_digits.val)) := by
            constructor
            · exact hnPrefix
            · exact hnPadded
          rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
          have hV :
              ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                (fun z x => 10 * z + x) 0 : Nat) : Real) *
                  (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
              (integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int)) := by
            rw [← hqval]
            have hexp : (q.val : Int) - (d.num_digits.val : Int) =
                (q.val : Int) - (digits.length : Int) := by rw [hdigitsLen]
            rw [hexp]
          have hVleX :
              ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                (fun z x => 10 * z + x) 0 : Nat) : Real) *
                  (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
            rw [hXe]
            linarith
          have hXVY : X < Y := by rw [hXe, hYe]; linarith
          by_cases hqLt : q.val < d.num_digits.val
          · have hFrac := DecimalSeqFractionalDigitInterval digits q.val
              (by rw [hdigitsLen]; exact hqLt)
              (by
                intro z hz
                have hz' : z ∈ first :: tail := by simpa [hword] using hz
                exact hdigitNat z hz')
              (by simp [bne_iff_ne, hlastWord])
            let N : Nat := (digits.take q.val).foldl (fun z x => 10 * z + x) 0
            let f : Nat := digits[q.val]
            have hnN : n.val = N := by exact hnValue.1 hqLt
            dsimp only at hFrac
            have hVfrac :
                ((integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int))) =
                  ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                    (fun z x => 10 * z + x) 0 : Nat) : Real) *
                      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
              exact hV.symm
            have hbelow :
                f < 5 ->
                  ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                    (fun z x => 10 * z + x) 0 : Nat) : Real) *
                      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                        (N : Real) + 1 / 2 := by
              intro hf
              have hb := hFrac.2.2.1 (by simpa [f] using hf)
              rw [← hVfrac]
              simpa [N, f] using hb
            have habove :
                5 < f ->
                  (N : Real) + 1 / 2 <
                    ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                      (fun z x => 10 * z + x) 0 : Nat) : Real) *
                        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
              intro hf
              have hb := hFrac.2.2.2.1 (by simpa [f] using hf)
              rw [← hVfrac]
              simpa [N, f] using hb
            have hqArray : q.val < d.digits.val.length := by
              have ha : d.digits.val.length = 768 := by simpa using d.digits.property
              omega
            obtain ⟨i1, hi1, hi1val⟩ := Aeneas.Std.WP.spec_imp_exists
              (Array.index_usize_spec d.digits q (by
                simpa [Array.length] using hqArray))
            have hreadVal : i1.val = f := by
              rw [hi1val]
              simp [f, digits, List.getElem_map, List.getElem_take]
            have hnumNe : d.num_digits ≠ 0#usize := by
              intro he
              have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
              have hz : d.num_digits.val = 0 := by simpa using hv
              omega
            obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
            have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
              rw [hi19]
              congr 1
              apply IScalar.eq_of_val_eq
              simpa using hi19val
            have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
              exact Result.ok.inj (by simpa [lift] using hcast19)
            have hqRaw : IScalar.hcast .Usize d.decimal_point = q :=
              Result.ok.inj (by simpa [lift] using hqCast)
            have hpointVal : d.decimal_point.val < 19 := by
              set_option maxHeartbeats 4000000 in scalar_tac
            have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
            have hread : d.digits.index_usize q = ok i1 := by
              simpa using hi1
            obtain ⟨qnext, hqnext, hqnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
              (Usize.add_spec (x := q) (y := 1#usize) (by scalar_tac))
            have hqnextAdd : q + 1#usize = ok qnext := by simpa using hqnext
            by_cases hlow : f < 5
            · have hroundKeep : d.round = ok n := by
                simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                  hnumNe, hpointNonneg,
                  num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                  hcast19Raw, hthreshold, hqRaw, hnLoop,
                  hqLt, hread, hlow, hreadVal]
              have hNleV : (N : Real) <=
                  ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                    (fun z x => 10 * z + x) 0 : Nat) : Real) *
                      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
                have hb := hFrac.1
                rw [hVfrac] at hb
                simpa [N] using hb
              have hYbelow : Y <= (N : Real) + 1 / 2 := by
                rcases hsep N with hbad | hgood
                · have := hbelow hlow
                  linarith
                · simpa using hgood
              have hNleX : (N : Real) <= X := hNleV.trans hVleX
              have hnleX : (n.val : Real) <= X := by simpa [hnN] using hNleX
              have hXstrict : X < (N : Real) + 1 / 2 :=
                lt_of_lt_of_le hXVY hYbelow
              refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
              · simpa [hnN] using le_of_lt hXstrict
              · have hbound : (N : Real) <= X + 1 / 2 :=
                  hNleX.trans (le_add_of_nonneg_right (by norm_num : (0 : Real) <= 1 / 2))
                simpa [hnN] using hbound
              · intro hmid
                rcases hmid with hu | hl
                · have hu' : X = (N : Real) + 1 / 2 := by simpa [hnN] using hu
                  exact False.elim ((ne_of_lt hXstrict) hu')
                · rw [hnN] at hl
                  have hltN : (N : Real) - 1 / 2 < (N : Real) := by
                    exact sub_lt_self _ (by norm_num)
                  have hstrict : X < (n.val : Real) := by
                    simpa [hnN] using (show X < (N : Real) by rw [hl]; exact hltN)
                  exact False.elim ((not_lt_of_ge hnleX) hstrict)
            · have hfge : 5 <= f := by
                exact Nat.le_of_not_lt hlow
              by_cases hfive : f = 5
              · have hFive := hFrac.2.2.2.2 (by simpa [f] using hfive)
                by_cases hterminal : q.val + 1 = digits.length
                · have hterminalCount : q.val + 1 = d.num_digits.val := by
                    simpa [hdigitsLen] using hterminal
                  have hqnextEq : qnext = d.num_digits := by
                    apply UScalar.eq_of_val_eq
                    have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
                    simpa [hqnv] using hterminalCount
                  have hVtie :
                      ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                        (fun z x => 10 * z + x) 0 : Nat) : Real) *
                        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
                        (N : Real) + 1 / 2 := by
                    have ht := hFive.1 hterminal
                    rw [hVfrac] at ht
                    simpa [N] using ht
                  by_cases htruncated : d.truncated = true
                  · have hetaPos : 0 < eta := htrunc.mp htruncated
                    have hXabove : (N : Real) + 1 / 2 < X := by
                      rw [hXe, hVtie]
                      linarith
                    have hYabove : Y <= (N : Real) + 3 / 2 := by
                      rcases hsep (N + 1) with hbad | hgood
                      · have hVup :
                            ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                              (fun z x => 10 * z + x) 0 : Nat) : Real) *
                                (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                            (N : Real) + 1 := by
                          have hu := hFrac.2.1
                          rw [hVfrac] at hu
                          simpa [N] using hu
                        exfalso
                        push_cast at hbad
                        linarith [hbad, hVup]
                      · have hgood' : Y <= (N : Real) + 3 / 2 := by
                          have hgood'' := hgood
                          push_cast at hgood''
                          convert hgood'' using 1 <;> ring
                        exact hgood'
                    have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
                      rw [hnN]
                    have hnAddBound : n.val + 1 <= U64.max := by
                      rw [U64.max_eq]
                      have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                        linarith [hNnext, hXabove, hXhi]
                      have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                      omega
                    obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
                      (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                    have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                    have hi1EqFive : i1 = 5#u8 := by
                      apply UScalar.eq_of_val_eq
                      simpa [hreadVal] using hfive
                    have hroundInc : d.round = ok nnext := by
                      simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                        hnumNe, hpointNonneg,
                        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                        hcast19Raw, hthreshold, hqRaw, hnLoop,
                        hqLt, hread, htruncated, hqnextEq, hi1EqFive,
                        hnAddEq, hqnextAdd]
                    have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                      rw [hnAddVal, hnN]
                      norm_num
                    refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                    · have hx := hXVY.trans_le hYabove
                      have hx' : X < (nnext.val : Real) + 1 / 2 := by
                        rw [hnnextReal]
                        convert hx using 1 <;> ring
                      exact le_of_lt hx'
                    · rw [hnnextReal]
                      have hx' : (N : Real) + 1 < X + 1 / 2 := by
                        convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
                      exact le_of_lt hx'
                    · intro hmid
                      have hXstrict : X < (N : Real) + 3 / 2 :=
                        lt_of_lt_of_le hXVY hYabove
                      rcases hmid with hhi | hlo
                      · have hhi' : X = (N : Real) + 3 / 2 := by
                          calc
                            X = (nnext.val : Real) + 1 / 2 := hhi
                            _ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
                        exact False.elim ((ne_of_lt hXstrict) hhi')
                      · have hlo' : X = (N : Real) + 1 / 2 := by
                          calc
                            X = (nnext.val : Real) - 1 / 2 := hlo
                            _ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
                        exact False.elim ((ne_of_gt hXabove) hlo')
                  · have htruncatedFalse : d.truncated = false := by
                      cases hflag : d.truncated with
                      | false => rfl
                      | true => exact False.elim (htruncated hflag)
                    have hetaZero : eta = 0 := by
                      have ht := htrunc
                      rw [htruncatedFalse] at ht
                      simp at ht
                      linarith
                    have hXtie : X = (N : Real) + 1 / 2 := by
                      rw [hXe, hVtie, hetaZero]
                      ring
                    by_cases hqzero : q.val = 0
                    · have hqZero : q = 0#usize :=
                        UScalar.eq_of_val_eq (by simpa using hqzero)
                      have hNzero : N = 0 := by simp [N, hqzero]
                      have hloop0 :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
                            { start := 0#usize, «end» := 0#usize }
                            d.num_digits d.digits 0#u64 = ok n := by
                        simpa [hqZero] using hnLoop
                      have hnumEqOne : d.num_digits = 1#usize := by
                        apply UScalar.eq_of_val_eq
                        have hcount : d.num_digits.val = 1 := by omega
                        simpa using hcount
                      have hloop01 := hloop0
                      rw [hnumEqOne] at hloop01
                      have hi1EqFive : i1 = 5#u8 := by
                        apply UScalar.eq_of_val_eq
                        simpa [hreadVal] using hfive
                      have hread0 := hread
                      rw [hqZero] at hread0
                      have hzeroAdd : 0#usize + 1#usize = ok 1#usize := by
                        simpa [hqZero, hqnextEq, hnumEqOne] using hqnextAdd
                      have hroundKeep : d.round = ok n := by
                        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                          hpointNonneg,
                          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                          hcast19Raw, hthreshold, hqRaw, hloop01,
                          hread0, hi1EqFive, htruncatedFalse, hqZero,
                          hnumEqOne, hzeroAdd]
                      have hnzero : n.val = 0 := by rw [hnN, hNzero]
                      refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
                      · rw [hXtie, hNzero]
                        simp
                      · rw [hnzero, hXtie, hNzero]
                        norm_num
                      · intro _
                        rw [hnzero]
                    · have hqpos : 0 < q.val := by omega
                      obtain ⟨qprev, hqprev, hqprevSpec⟩ :=
                        Aeneas.Std.WP.spec_imp_exists
                          (UScalar.sub_spec (x := q) (y := 1#usize) (by simpa using hqpos))
                      have hqprevVal : qprev.val = q.val - 1 := by
                        simpa using hqprevSpec.1
                      have hqprevAdd : qprev.val + 1 = q.val := by omega
                      have hqprevArray : qprev.val < d.digits.val.length := by
                        have ha : d.digits.val.length = 768 := by
                          simpa using d.digits.property
                        omega
                      obtain ⟨iprev, hiprev, hiprevVal⟩ :=
                        Aeneas.Std.WP.spec_imp_exists
                          (Array.index_usize_spec d.digits qprev (by
                            simpa [Array.length] using hqprevArray))
                      have hprevRead : d.digits.index_usize qprev = ok iprev := by
                        simpa using hiprev
                      let c := digits[qprev.val]
                      let P := (digits.take qprev.val).foldl (fun z x => 10 * z + x) 0
                      have hprevReadVal : iprev.val = c := by
                        rw [hiprevVal]
                        simp [c, digits, List.getElem_map, List.getElem_take]
                      have htake : digits.take q.val = digits.take qprev.val ++ [c] := by
                        have ht := List.take_succ_eq_append_getElem
                          (l := digits) (i := qprev.val) (by omega : qprev.val < digits.length)
                        simpa [c, hqprevAdd] using ht
                      have hNdecomp : N = 10 * P + c := by
                        dsimp [N, P]
                        rw [htake, List.foldl_append]
                        simp only [List.foldl_cons, List.foldl_nil]
                      have hNmod : N % 2 = c % 2 := by
                        rw [hNdecomp]
                        omega
                      have hbitVal : (1#u8 &&& iprev).val = c % 2 := by
                        rw [UScalar.val_and, hprevReadVal]
                        have hmask : (1#u8).val = 2 ^ 1 - 1 := by norm_num
                        rw [hmask]
                        calc
                          (2 ^ 1 - 1) &&& c = c &&& (2 ^ 1 - 1) := Nat.and_comm _ _
                          _ = c % 2 ^ 1 := Nat.and_two_pow_sub_one_eq_mod c 1
                          _ = c % 2 := by norm_num
                      have hi1EqFive : i1 = 5#u8 := by
                        apply UScalar.eq_of_val_eq
                        simpa [hreadVal] using hfive
                      have hqNeZero : q ≠ 0#usize := by
                        intro he
                        have hv := congrArg UScalar.val he
                        simp at hv
                        omega
                      have hqvalNeZero : q.val ≠ 0 := by omega
                      by_cases heven : N % 2 = 0
                      · have hcEven : c % 2 = 0 := by rw [← hNmod]; exact heven
                        have hbitZero : (1#u8 &&& iprev) = 0#u8 := by
                          apply UScalar.eq_of_val_eq
                          rw [hbitVal]
                          simp [hcEven]
                        have hroundKeep : d.round = ok n := by
                          simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                            hnumNe, hpointNonneg,
                            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                            hcast19Raw, hthreshold, hqRaw, hnLoop,
                            hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
                            hqvalNeZero, hqprev, hprevRead, hbitZero,
                            hqnextAdd]
                        have hnmod : n.val % 2 = 0 := by rw [hnN, heven]
                        refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
                        · rw [hnN, hXtie]
                        · rw [hnN, hXtie]
                          linarith
                        · intro _
                          exact hnmod
                      · have hNmodlt : N % 2 < 2 := Nat.mod_lt _ (by omega)
                        have hodd : N % 2 = 1 := by omega
                        have hcOdd : c % 2 = 1 := by rw [← hNmod]; exact hodd
                        have hbitNeZero : (1#u8 &&& iprev) ≠ 0#u8 := by
                          intro he
                          have hv := congrArg UScalar.val he
                          have hzero : (1#u8 &&& iprev).val = 0 := by simpa using hv
                          rw [hbitVal] at hzero
                          omega
                        have hnAddBound : n.val + 1 <= U64.max := by
                          rw [U64.max_eq]
                          have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                            rw [hnN]
                            rw [hXtie] at hXhi
                            linarith
                          have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                          omega
                        obtain ⟨nnext, hnAdd, hnAddVal⟩ :=
                          Aeneas.Std.WP.spec_imp_exists
                            (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                        have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                        have hroundInc : d.round = ok nnext := by
                          simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                            hnumNe, hpointNonneg,
                            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                            hcast19Raw, hthreshold, hqRaw, hnLoop,
                            hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
                            hqvalNeZero, hqprev, hprevRead, hbitNeZero, hnAddEq,
                            hqnextAdd]
                        have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                          rw [hnAddVal, hnN]
                          norm_num
                        refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                        · rw [hXtie, hnnextReal]
                          linarith
                        · rw [hXtie, hnnextReal]
                          linarith
                        · intro _
                          have hnnextValNat : nnext.val = n.val + 1 := by simpa using hnAddVal
                          rw [hnnextValNat, hnN]
                          omega
                · have hnonterminal : q.val + 1 < digits.length := by omega
                  have hNbelowV : (N : Real) + 1 / 2 <
                      ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                        (fun z x => 10 * z + x) 0 : Nat) : Real) *
                          (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
                    have ht := hFive.2 hnonterminal
                    rw [hVfrac] at ht
                    simpa [N] using ht
                  have hYabove : Y <= (N : Real) + 3 / 2 := by
                    rcases hsep (N + 1) with hbad | hgood
                    · have hVup :
                          ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                            (fun z x => 10 * z + x) 0 : Nat) : Real) *
                              (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                          (N : Real) + 1 := by
                        have hu := hFrac.2.1
                        rw [hVfrac] at hu
                        simpa [N] using hu
                      exfalso
                      push_cast at hbad
                      linarith [hbad, hVup]
                    · have hgood' : Y <= (N : Real) + 3 / 2 := by
                        have hgood'' := hgood
                        push_cast at hgood''
                        convert hgood'' using 1 <;> ring
                      exact hgood'
                  have hXabove : (N : Real) + 1 / 2 < X :=
                    lt_of_lt_of_le hNbelowV hVleX
                  have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by rw [hnN]
                  have hnAddBound : n.val + 1 <= U64.max := by
                    rw [U64.max_eq]
                    have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                      linarith [hNnext, hXabove, hXhi]
                    have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                    omega
                  obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
                    (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                  have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                  have hi1EqFive : i1 = 5#u8 := by
                    apply UScalar.eq_of_val_eq
                    simpa [hreadVal] using hfive
                  have hqnextNe : qnext ≠ d.num_digits := by
                    intro he
                    have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
                    have hv := congrArg UScalar.val he
                    change qnext.val = d.num_digits.val at hv
                    rw [hqnv] at hv
                    omega
                  have hroundInc : d.round = ok nnext := by
                    simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                      hnumNe, hpointNonneg,
                      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                      hcast19Raw, hthreshold, hqRaw, hnLoop,
                      hqLt, hread, hi1EqFive,
                      hqnextNe, hnAddEq, hqnextAdd]
                  have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                    rw [hnAddVal, hnN]
                    norm_num
                  refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                  · have hx := hXVY.trans_le hYabove
                    rw [hnnextReal]
                    exact le_of_lt (calc
                      X < (N : Real) + 3 / 2 := hx
                      _ = (N : Real) + 1 + 1 / 2 := by ring)
                  · rw [hnnextReal]
                    have hx' : (N : Real) + 1 < X + 1 / 2 := by
                      convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
                    exact le_of_lt hx'
                  · intro hmid
                    have hXstrict : X < (N : Real) + 3 / 2 :=
                      lt_of_lt_of_le hXVY hYabove
                    rcases hmid with hhi | hlo
                    · have hhi' : X = (N : Real) + 3 / 2 := by
                        calc
                          X = (nnext.val : Real) + 1 / 2 := hhi
                          _ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
                      exact False.elim ((ne_of_lt hXstrict) hhi')
                    · have hlo' : X = (N : Real) + 1 / 2 := by
                        calc
                          X = (nnext.val : Real) - 1 / 2 := hlo
                          _ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
                      exact False.elim ((ne_of_gt hXabove) hlo')
              · have hfabove : 5 < f := by omega
                have hi1NeFive : i1 ≠ 5#u8 := by
                  intro he
                  have hv := congrArg UScalar.val he
                  simp [hreadVal] at hv
                  omega
                have hi1AtLeastFive : 5 <= i1.val := by omega
                have hNbelowV : (N : Real) + 1 / 2 <
                    ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                      (fun z x => 10 * z + x) 0 : Nat) : Real) *
                        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) :=
                  habove hfabove
                have hYabove : Y <= (N : Real) + 3 / 2 := by
                  rcases hsep (N + 1) with hbad | hgood
                  · have hVup :
                        ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                          (fun z x => 10 * z + x) 0 : Nat) : Real) *
                            (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                        (N : Real) + 1 := by
                      have hu := hFrac.2.1
                      rw [hVfrac] at hu
                      simpa [N] using hu
                    exfalso
                    push_cast at hbad
                    linarith [hbad, hVup]
                  · have hgood' : Y <= (N : Real) + 3 / 2 := by
                      have hgood'' := hgood
                      push_cast at hgood''
                      convert hgood'' using 1 <;> ring
                    exact hgood'
                have hXabove : (N : Real) + 1 / 2 < X := by
                  exact lt_of_lt_of_le hNbelowV hVleX
                have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
                  rw [hnN]
                have hnAddBound : n.val + 1 <= U64.max := by
                  rw [U64.max_eq]
                  have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                    linarith [hNnext, hXabove, hXhi]
                  have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                  omega
                obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
                  (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                have hroundInc : d.round = ok nnext := by
                  simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                    hnumNe, hpointNonneg,
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                    hcast19Raw, hthreshold, hqRaw, hnLoop,
                    hqLt, hread, hreadVal, hi1NeFive, hnAddEq,
                    hqnextAdd]
                  all_goals omega
                have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                  rw [hnAddVal, hnN]
                  norm_num
                refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                · have hx := hXVY.trans_le hYabove
                  rw [hnnextReal]
                  linarith
                · rw [hnnextReal]
                  linarith
                · intro hmid
                  rcases hmid with hmid | hmid <;> rw [hnnextReal] at hmid <;> linarith
          · have hqGe : d.num_digits.val <= q.val := by omega
            have hNreal : (n.val : Real) =
                (integer : Real) * (10 : Real) ^ ((q.val : Int) - (d.num_digits.val : Int)) := by
              rw [hnValue.2 hqGe]
              rw [Nat.cast_mul, Nat.cast_pow]
              have hexp : (q.val : Int) - (d.num_digits.val : Int) =
                  ((q.val - d.num_digits.val : Nat) : Int) := by omega
              rw [hexp, zpow_natCast]
              norm_num
            have hVn :
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun z x => 10 * z + x) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
                  (n.val : Real) := by
              rw [hdigitsLen] at hV
              rw [hV, hNreal]
            have hSepN := hsep n.val
            have hYbound : Y <= (n.val : Real) + 1 / 2 := by
              rcases hSepN with hbad | hgood
              · rw [hVn] at hbad
                norm_num at hbad
              · simpa using hgood
            have hXbound : X < (n.val : Real) + 1 / 2 := by
              exact lt_of_lt_of_le hXVY hYbound
            have hnumNe : d.num_digits ≠ 0#usize := by
              intro he
              have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
              have hz : d.num_digits.val = 0 := by simpa using hv
              omega
            obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
            have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
              rw [hi19]
              congr 1
              apply IScalar.eq_of_val_eq
              simpa using hi19val
            have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
              exact Result.ok.inj (by simpa [lift] using hcast19)
            have hqRaw : IScalar.hcast .Usize d.decimal_point = q := by
              exact Result.ok.inj (by simpa [lift] using hqCast)
            have hpointVal : d.decimal_point.val < 19 := hpoint
            have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
            have hqNotLt : ¬ q.val < d.num_digits.val := Nat.not_lt_of_ge hqGe
            have hroundN : d.round = ok n := by
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              rw [if_neg hnumNe]
              rw [if_neg hpointNonneg]
              simp only [
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                hcast19Raw, lift]
              simp [hthreshold, hqRaw, hnLoop, hqNotLt]
            refine ⟨n, hroundN, ?_, ?_, ?_⟩
            · exact le_of_lt hXbound
            · have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
              exact le_trans hnX (le_add_of_nonneg_right (by norm_num))
            · intro hmid
              have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
              rcases hmid with hmid | hmid
              · exact False.elim ((ne_of_lt hXbound) hmid)
              · have hlt : (n.val : Real) - 1 / 2 < n.val :=
                  sub_lt_self _ (by norm_num)
                have hXlt : X < (n.val : Real) := by rw [hmid]; exact hlt
                exact False.elim (not_lt_of_ge hnX hXlt)
    ))

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqRoundLoopValue
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.P472_T1_DecimalValueBounds
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P529_T1_RoundArbitraryEndpoint]
theorem P529_T1_RoundArbitraryEndpoint (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (X Y : Real)
    (hcap : d.num_digits.val <= 768)
    (hdigits : forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10)
    (hshape : (d.num_digits.val = 0 /\ d.truncated = false /\ X = 0) \/
      ((exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val) /\
        d.digits.val[d.num_digits.val - 1]? ≠ some 0#u8))
    (hpoint : d.decimal_point.val < 19)
    (hXlo : 0 <= X) (hXhi : X < (2 : Real) ^ (64 : Nat) - 1 / 2) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    (exists eta u : Real, X = V d + eta /\ Y = V d + u /\
      0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) ->
    (forall n : Nat, (n : Real) + 1 / 2 <= V d \/ Y <= (n : Real) + 1 / 2) ->
    exists m : Std.U64,
      num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
      X <= (m.val : Real) + 1 / 2 /\ (m.val : Real) <= X + 1 / 2 /\
      (X = (m.val : Real) + 1 / 2 \/ X = (m.val : Real) - 1 / 2 ->
        m.val % 2 = 0) := by
-- BODY
  exact (set_option maxHeartbeats 40000000 in (by
      dsimp only
      intro hw hsep
      rcases hshape with hzero | hpos
      · rcases hzero with ⟨hn, ht, hX⟩
        subst X
        rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
        simp [hn] at hXe hYe
        have hY : Y = u := by linarith
        have hU : 0 < u := by linarith
        refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
        · simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hn]
        · simp
        · simp
        · intro hmid
          simp
      · rcases hpos with ⟨⟨first, tail, hmap, hfirstlo, hfirsthi, hlen⟩, hlast⟩
        let digits := (d.digits.val.take d.num_digits.val).map (fun b => b.val)
        have hdigitsLen : digits.length = d.num_digits.val := by
          simp [digits]
          have ha : d.digits.val.length = 768 := by simpa using d.digits.property
          omega
        have hword : digits = first :: tail := by simpa [digits] using hmap
        have hnpos : 0 < d.num_digits.val := by
          rw [← hlen]
          simp
        have hlastWord : digits.getLast? ≠ some 0 := by
          have hlastEq : digits.getLast? =
              (d.digits.val[d.num_digits.val - 1]?).map (fun b => b.val) := by
            rw [List.getLast?_eq_getElem?]
            simp only [digits, List.getElem?_map, List.getElem?_take]
            simp [hcap, Nat.sub_lt hnpos]
          intro heq
          rw [hlastEq] at heq
          rcases Option.map_eq_some_iff.mp heq with ⟨b, hb, hbval⟩
          have hbzero : b = 0#u8 := by scalar_tac
          apply hlast
          rw [hb, hbzero]
        have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
          intro z hz
          have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
              (fun b => b.val) := by simpa [hword, digits] using hz
          rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
          exact hbz ▸ hdigits b hb
        by_cases hnegative : d.decimal_point.val < 0
        · have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
            intro z hz
            have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
                (fun b => b.val) := by simpa [hword, digits] using hz
            rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
            exact hbz ▸ hdigits b hb
          have hvbound := P472_T1_DecimalValueBounds
            (first :: tail) first tail rfl hfirstlo hdigitNat d.decimal_point.val
          have hVlt :
              ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                (fun n z => 10 * n + z) 0 : Nat) : Real) *
                  (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
              (1 : Real) / 2 := by
            have hv :
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun n z => 10 * n + z) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                (10 : Real) ^ d.decimal_point.val := by
              have hv' := hvbound.2
              rw [← hword] at hv'
              rw [hdigitsLen] at hv'
              simpa [digits] using hv'
            have hp : (10 : Real) ^ d.decimal_point.val < 1 / 2 := by
              let k : Nat := Int.toNat (-d.decimal_point.val - 1)
              have hk : d.decimal_point.val = -(k : Int) - 1 := by
                have hp0 : 0 ≤ -d.decimal_point.val - 1 := by omega
                have ht : ((-d.decimal_point.val - 1).toNat : Int) =
                    -d.decimal_point.val - 1 := Int.toNat_of_nonneg hp0
                simp only [k]
                omega
              rw [hk, show -(k : Int) - 1 = -((k + 1 : Nat) : Int) by omega,
                zpow_neg, zpow_natCast]
              have hden : (10 : Real) ≤ (10 : Real) ^ (k + 1) := by
                calc
                  (10 : Real) = 10 ^ (1 : Nat) := by norm_num
                  _ ≤ 10 ^ (k + 1) := by
                    exact_mod_cast (Nat.pow_le_pow_right (by norm_num : 0 < 10)
                      (show 1 ≤ k + 1 by omega))
              have hinv : ((10 : Real) ^ (k + 1))⁻¹ ≤ (10 : Real)⁻¹ :=
                (inv_le_inv₀ (by positivity : (0 : Real) < (10 : Real) ^ (k + 1))
                  (by norm_num : (0 : Real) < 10)).2 hden
              norm_num at hinv ⊢
              linarith
            exact lt_trans hv hp
          rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
          have hsep0 := hsep 0
          have hYhalf : Y <= 1 / 2 := by
            rcases hsep0 with hbad | hgood
            · linarith
            · simpa using hgood
          have hXhalf : X < 1 / 2 := by
            have hXV : (0 : Real) <=
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun n z => 10 * n + z) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
              exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num) _))
            have hxle :
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun n z => 10 * n + z) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
              rw [hXe]
              linarith
            linarith
          refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
          · have hp : d.decimal_point < 0#i32 := by scalar_tac
            simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hp]
          · simpa using le_of_lt hXhalf
          · norm_num
            linarith
          · intro _
            norm_num
        · have hpLower : 0 <= d.decimal_point.val := by
            omega
          have hpUpper : d.decimal_point.val <= 18 := by
            exact Int.le_of_lt_add_one (by simpa using hpoint)
          have hpointNonneg : ¬ d.decimal_point < 0#i32 := by scalar_tac
          have hpointSmall : d.decimal_point < 19#i32 := by scalar_tac
          obtain ⟨q, hqCast, hqval⟩ := Aeneas.Std.WP.spec_imp_exists
            (IScalar.hcast_inBounds_spec .Usize d.decimal_point (by scalar_tac))
          change lift (IScalar.hcast .Usize d.decimal_point) = ok q at hqCast
          have hqBound : q.val <= 18 := by omega
          obtain ⟨n, hnLoop, hnPrefix, hnPadded⟩ :=
            DecimalSeqRoundLoopValue d q hqBound hcap hdigits
          let integer := (digits.foldl (fun z x => 10 * z + x) 0)
          have hnValue :
              (q.val < d.num_digits.val ->
                n.val = (digits.take q.val).foldl (fun z x => 10 * z + x) 0) ∧
              (d.num_digits.val <= q.val ->
                n.val = integer * 10 ^ (q.val - d.num_digits.val)) := by
            constructor
            · exact hnPrefix
            · exact hnPadded
          rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
          have hV :
              ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                (fun z x => 10 * z + x) 0 : Nat) : Real) *
                  (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
              (integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int)) := by
            rw [← hqval]
            have hexp : (q.val : Int) - (d.num_digits.val : Int) =
                (q.val : Int) - (digits.length : Int) := by rw [hdigitsLen]
            rw [hexp]
          have hVleX :
              ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                (fun z x => 10 * z + x) 0 : Nat) : Real) *
                  (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
            rw [hXe]
            linarith
          have hXVY : X < Y := by rw [hXe, hYe]; linarith
          by_cases hqLt : q.val < d.num_digits.val
          · have hFrac := DecimalSeqFractionalDigitInterval digits q.val
              (by rw [hdigitsLen]; exact hqLt)
              (by
                intro z hz
                have hz' : z ∈ first :: tail := by simpa [hword] using hz
                exact hdigitNat z hz')
              (by simp [bne_iff_ne, hlastWord])
            let N : Nat := (digits.take q.val).foldl (fun z x => 10 * z + x) 0
            let f : Nat := digits[q.val]
            have hnN : n.val = N := by exact hnValue.1 hqLt
            dsimp only at hFrac
            have hVfrac :
                ((integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int))) =
                  ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                    (fun z x => 10 * z + x) 0 : Nat) : Real) *
                      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
              exact hV.symm
            have hbelow :
                f < 5 ->
                  ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                    (fun z x => 10 * z + x) 0 : Nat) : Real) *
                      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                        (N : Real) + 1 / 2 := by
              intro hf
              have hb := hFrac.2.2.1 (by simpa [f] using hf)
              rw [← hVfrac]
              simpa [N, f] using hb
            have habove :
                5 < f ->
                  (N : Real) + 1 / 2 <
                    ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                      (fun z x => 10 * z + x) 0 : Nat) : Real) *
                        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
              intro hf
              have hb := hFrac.2.2.2.1 (by simpa [f] using hf)
              rw [← hVfrac]
              simpa [N, f] using hb
            have hqArray : q.val < d.digits.val.length := by
              have ha : d.digits.val.length = 768 := by simpa using d.digits.property
              omega
            obtain ⟨i1, hi1, hi1val⟩ := Aeneas.Std.WP.spec_imp_exists
              (Array.index_usize_spec d.digits q (by
                simpa [Array.length] using hqArray))
            have hreadVal : i1.val = f := by
              rw [hi1val]
              simp [f, digits, List.getElem_map, List.getElem_take]
            have hnumNe : d.num_digits ≠ 0#usize := by
              intro he
              have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
              have hz : d.num_digits.val = 0 := by simpa using hv
              omega
            obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
            have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
              rw [hi19]
              congr 1
              apply IScalar.eq_of_val_eq
              simpa using hi19val
            have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
              exact Result.ok.inj (by simpa [lift] using hcast19)
            have hqRaw : IScalar.hcast .Usize d.decimal_point = q :=
              Result.ok.inj (by simpa [lift] using hqCast)
            have hpointVal : d.decimal_point.val < 19 := by
              set_option maxHeartbeats 4000000 in scalar_tac
            have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
            have hread : d.digits.index_usize q = ok i1 := by
              simpa using hi1
            obtain ⟨qnext, hqnext, hqnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
              (Usize.add_spec (x := q) (y := 1#usize) (by scalar_tac))
            have hqnextAdd : q + 1#usize = ok qnext := by simpa using hqnext
            by_cases hlow : f < 5
            · have hroundKeep : d.round = ok n := by
                simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                  hnumNe, hpointNonneg,
                  num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                  hcast19Raw, hthreshold, hqRaw, hnLoop,
                  hqLt, hread, hlow, hreadVal]
              have hNleV : (N : Real) <=
                  ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                    (fun z x => 10 * z + x) 0 : Nat) : Real) *
                      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
                have hb := hFrac.1
                rw [hVfrac] at hb
                simpa [N] using hb
              have hYbelow : Y <= (N : Real) + 1 / 2 := by
                rcases hsep N with hbad | hgood
                · have := hbelow hlow
                  linarith
                · simpa using hgood
              have hNleX : (N : Real) <= X := hNleV.trans hVleX
              have hnleX : (n.val : Real) <= X := by simpa [hnN] using hNleX
              have hXstrict : X < (N : Real) + 1 / 2 :=
                lt_of_lt_of_le hXVY hYbelow
              refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
              · simpa [hnN] using le_of_lt hXstrict
              · have hbound : (N : Real) <= X + 1 / 2 :=
                  hNleX.trans (le_add_of_nonneg_right (by norm_num : (0 : Real) <= 1 / 2))
                simpa [hnN] using hbound
              · intro hmid
                rcases hmid with hu | hl
                · have hu' : X = (N : Real) + 1 / 2 := by simpa [hnN] using hu
                  exact False.elim ((ne_of_lt hXstrict) hu')
                · rw [hnN] at hl
                  have hltN : (N : Real) - 1 / 2 < (N : Real) := by
                    exact sub_lt_self _ (by norm_num)
                  have hstrict : X < (n.val : Real) := by
                    simpa [hnN] using (show X < (N : Real) by rw [hl]; exact hltN)
                  exact False.elim ((not_lt_of_ge hnleX) hstrict)
            · have hfge : 5 <= f := by
                exact Nat.le_of_not_lt hlow
              by_cases hfive : f = 5
              · have hFive := hFrac.2.2.2.2 (by simpa [f] using hfive)
                by_cases hterminal : q.val + 1 = digits.length
                · have hterminalCount : q.val + 1 = d.num_digits.val := by
                    simpa [hdigitsLen] using hterminal
                  have hqnextEq : qnext = d.num_digits := by
                    apply UScalar.eq_of_val_eq
                    have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
                    simpa [hqnv] using hterminalCount
                  have hVtie :
                      ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                        (fun z x => 10 * z + x) 0 : Nat) : Real) *
                        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
                        (N : Real) + 1 / 2 := by
                    have ht := hFive.1 hterminal
                    rw [hVfrac] at ht
                    simpa [N] using ht
                  by_cases htruncated : d.truncated = true
                  · have hetaPos : 0 < eta := htrunc.mp htruncated
                    have hXabove : (N : Real) + 1 / 2 < X := by
                      rw [hXe, hVtie]
                      linarith
                    have hYabove : Y <= (N : Real) + 3 / 2 := by
                      rcases hsep (N + 1) with hbad | hgood
                      · have hVup :
                            ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                              (fun z x => 10 * z + x) 0 : Nat) : Real) *
                                (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                            (N : Real) + 1 := by
                          have hu := hFrac.2.1
                          rw [hVfrac] at hu
                          simpa [N] using hu
                        exfalso
                        push_cast at hbad
                        linarith [hbad, hVup]
                      · have hgood' : Y <= (N : Real) + 3 / 2 := by
                          have hgood'' := hgood
                          push_cast at hgood''
                          convert hgood'' using 1 <;> ring
                        exact hgood'
                    have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
                      rw [hnN]
                    have hnAddBound : n.val + 1 <= U64.max := by
                      rw [U64.max_eq]
                      have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                        linarith [hNnext, hXabove, hXhi]
                      have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                      omega
                    obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
                      (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                    have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                    have hi1EqFive : i1 = 5#u8 := by
                      apply UScalar.eq_of_val_eq
                      simpa [hreadVal] using hfive
                    have hroundInc : d.round = ok nnext := by
                      simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                        hnumNe, hpointNonneg,
                        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                        hcast19Raw, hthreshold, hqRaw, hnLoop,
                        hqLt, hread, htruncated, hqnextEq, hi1EqFive,
                        hnAddEq, hqnextAdd]
                    have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                      rw [hnAddVal, hnN]
                      norm_num
                    refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                    · have hx := hXVY.trans_le hYabove
                      have hx' : X < (nnext.val : Real) + 1 / 2 := by
                        rw [hnnextReal]
                        convert hx using 1 <;> ring
                      exact le_of_lt hx'
                    · rw [hnnextReal]
                      have hx' : (N : Real) + 1 < X + 1 / 2 := by
                        convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
                      exact le_of_lt hx'
                    · intro hmid
                      have hXstrict : X < (N : Real) + 3 / 2 :=
                        lt_of_lt_of_le hXVY hYabove
                      rcases hmid with hhi | hlo
                      · have hhi' : X = (N : Real) + 3 / 2 := by
                          calc
                            X = (nnext.val : Real) + 1 / 2 := hhi
                            _ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
                        exact False.elim ((ne_of_lt hXstrict) hhi')
                      · have hlo' : X = (N : Real) + 1 / 2 := by
                          calc
                            X = (nnext.val : Real) - 1 / 2 := hlo
                            _ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
                        exact False.elim ((ne_of_gt hXabove) hlo')
                  · have htruncatedFalse : d.truncated = false := by
                      cases hflag : d.truncated with
                      | false => rfl
                      | true => exact False.elim (htruncated hflag)
                    have hetaZero : eta = 0 := by
                      have ht := htrunc
                      rw [htruncatedFalse] at ht
                      simp at ht
                      linarith
                    have hXtie : X = (N : Real) + 1 / 2 := by
                      rw [hXe, hVtie, hetaZero]
                      ring
                    by_cases hqzero : q.val = 0
                    · have hqZero : q = 0#usize :=
                        UScalar.eq_of_val_eq (by simpa using hqzero)
                      have hNzero : N = 0 := by simp [N, hqzero]
                      have hloop0 :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
                            { start := 0#usize, «end» := 0#usize }
                            d.num_digits d.digits 0#u64 = ok n := by
                        simpa [hqZero] using hnLoop
                      have hnumEqOne : d.num_digits = 1#usize := by
                        apply UScalar.eq_of_val_eq
                        have hcount : d.num_digits.val = 1 := by omega
                        simpa using hcount
                      have hloop01 := hloop0
                      rw [hnumEqOne] at hloop01
                      have hi1EqFive : i1 = 5#u8 := by
                        apply UScalar.eq_of_val_eq
                        simpa [hreadVal] using hfive
                      have hread0 := hread
                      rw [hqZero] at hread0
                      have hzeroAdd : 0#usize + 1#usize = ok 1#usize := by
                        simpa [hqZero, hqnextEq, hnumEqOne] using hqnextAdd
                      have hroundKeep : d.round = ok n := by
                        simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                          hpointNonneg,
                          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                          hcast19Raw, hthreshold, hqRaw, hloop01,
                          hread0, hi1EqFive, htruncatedFalse, hqZero,
                          hnumEqOne, hzeroAdd]
                      have hnzero : n.val = 0 := by rw [hnN, hNzero]
                      refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
                      · rw [hXtie, hNzero]
                        simp
                      · rw [hnzero, hXtie, hNzero]
                        norm_num
                      · intro _
                        rw [hnzero]
                    · have hqpos : 0 < q.val := by omega
                      obtain ⟨qprev, hqprev, hqprevSpec⟩ :=
                        Aeneas.Std.WP.spec_imp_exists
                          (UScalar.sub_spec (x := q) (y := 1#usize) (by simpa using hqpos))
                      have hqprevVal : qprev.val = q.val - 1 := by
                        simpa using hqprevSpec.1
                      have hqprevAdd : qprev.val + 1 = q.val := by omega
                      have hqprevArray : qprev.val < d.digits.val.length := by
                        have ha : d.digits.val.length = 768 := by
                          simpa using d.digits.property
                        omega
                      obtain ⟨iprev, hiprev, hiprevVal⟩ :=
                        Aeneas.Std.WP.spec_imp_exists
                          (Array.index_usize_spec d.digits qprev (by
                            simpa [Array.length] using hqprevArray))
                      have hprevRead : d.digits.index_usize qprev = ok iprev := by
                        simpa using hiprev
                      let c := digits[qprev.val]
                      let P := (digits.take qprev.val).foldl (fun z x => 10 * z + x) 0
                      have hprevReadVal : iprev.val = c := by
                        rw [hiprevVal]
                        simp [c, digits, List.getElem_map, List.getElem_take]
                      have htake : digits.take q.val = digits.take qprev.val ++ [c] := by
                        have ht := List.take_succ_eq_append_getElem
                          (l := digits) (i := qprev.val) (by omega : qprev.val < digits.length)
                        simpa [c, hqprevAdd] using ht
                      have hNdecomp : N = 10 * P + c := by
                        dsimp [N, P]
                        rw [htake, List.foldl_append]
                        simp only [List.foldl_cons, List.foldl_nil]
                      have hNmod : N % 2 = c % 2 := by
                        rw [hNdecomp]
                        omega
                      have hbitVal : (1#u8 &&& iprev).val = c % 2 := by
                        rw [UScalar.val_and, hprevReadVal]
                        have hmask : (1#u8).val = 2 ^ 1 - 1 := by norm_num
                        rw [hmask]
                        calc
                          (2 ^ 1 - 1) &&& c = c &&& (2 ^ 1 - 1) := Nat.and_comm _ _
                          _ = c % 2 ^ 1 := Nat.and_two_pow_sub_one_eq_mod c 1
                          _ = c % 2 := by norm_num
                      have hi1EqFive : i1 = 5#u8 := by
                        apply UScalar.eq_of_val_eq
                        simpa [hreadVal] using hfive
                      have hqNeZero : q ≠ 0#usize := by
                        intro he
                        have hv := congrArg UScalar.val he
                        simp at hv
                        omega
                      have hqvalNeZero : q.val ≠ 0 := by omega
                      by_cases heven : N % 2 = 0
                      · have hcEven : c % 2 = 0 := by rw [← hNmod]; exact heven
                        have hbitZero : (1#u8 &&& iprev) = 0#u8 := by
                          apply UScalar.eq_of_val_eq
                          rw [hbitVal]
                          simp [hcEven]
                        have hroundKeep : d.round = ok n := by
                          simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                            hnumNe, hpointNonneg,
                            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                            hcast19Raw, hthreshold, hqRaw, hnLoop,
                            hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
                            hqvalNeZero, hqprev, hprevRead, hbitZero,
                            hqnextAdd]
                        have hnmod : n.val % 2 = 0 := by rw [hnN, heven]
                        refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
                        · rw [hnN, hXtie]
                        · rw [hnN, hXtie]
                          linarith
                        · intro _
                          exact hnmod
                      · have hNmodlt : N % 2 < 2 := Nat.mod_lt _ (by omega)
                        have hodd : N % 2 = 1 := by omega
                        have hcOdd : c % 2 = 1 := by rw [← hNmod]; exact hodd
                        have hbitNeZero : (1#u8 &&& iprev) ≠ 0#u8 := by
                          intro he
                          have hv := congrArg UScalar.val he
                          have hzero : (1#u8 &&& iprev).val = 0 := by simpa using hv
                          rw [hbitVal] at hzero
                          omega
                        have hnAddBound : n.val + 1 <= U64.max := by
                          rw [U64.max_eq]
                          have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                            rw [hnN]
                            rw [hXtie] at hXhi
                            linarith
                          have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                          omega
                        obtain ⟨nnext, hnAdd, hnAddVal⟩ :=
                          Aeneas.Std.WP.spec_imp_exists
                            (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                        have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                        have hroundInc : d.round = ok nnext := by
                          simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                            hnumNe, hpointNonneg,
                            num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                            hcast19Raw, hthreshold, hqRaw, hnLoop,
                            hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
                            hqvalNeZero, hqprev, hprevRead, hbitNeZero, hnAddEq,
                            hqnextAdd]
                        have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                          rw [hnAddVal, hnN]
                          norm_num
                        refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                        · rw [hXtie, hnnextReal]
                          linarith
                        · rw [hXtie, hnnextReal]
                          linarith
                        · intro _
                          have hnnextValNat : nnext.val = n.val + 1 := by simpa using hnAddVal
                          rw [hnnextValNat, hnN]
                          omega
                · have hnonterminal : q.val + 1 < digits.length := by omega
                  have hNbelowV : (N : Real) + 1 / 2 <
                      ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                        (fun z x => 10 * z + x) 0 : Nat) : Real) *
                          (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
                    have ht := hFive.2 hnonterminal
                    rw [hVfrac] at ht
                    simpa [N] using ht
                  have hYabove : Y <= (N : Real) + 3 / 2 := by
                    rcases hsep (N + 1) with hbad | hgood
                    · have hVup :
                          ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                            (fun z x => 10 * z + x) 0 : Nat) : Real) *
                              (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                          (N : Real) + 1 := by
                        have hu := hFrac.2.1
                        rw [hVfrac] at hu
                        simpa [N] using hu
                      exfalso
                      push_cast at hbad
                      linarith [hbad, hVup]
                    · have hgood' : Y <= (N : Real) + 3 / 2 := by
                        have hgood'' := hgood
                        push_cast at hgood''
                        convert hgood'' using 1 <;> ring
                      exact hgood'
                  have hXabove : (N : Real) + 1 / 2 < X :=
                    lt_of_lt_of_le hNbelowV hVleX
                  have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by rw [hnN]
                  have hnAddBound : n.val + 1 <= U64.max := by
                    rw [U64.max_eq]
                    have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                      linarith [hNnext, hXabove, hXhi]
                    have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                    omega
                  obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
                    (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                  have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                  have hi1EqFive : i1 = 5#u8 := by
                    apply UScalar.eq_of_val_eq
                    simpa [hreadVal] using hfive
                  have hqnextNe : qnext ≠ d.num_digits := by
                    intro he
                    have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
                    have hv := congrArg UScalar.val he
                    change qnext.val = d.num_digits.val at hv
                    rw [hqnv] at hv
                    omega
                  have hroundInc : d.round = ok nnext := by
                    simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                      hnumNe, hpointNonneg,
                      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                      hcast19Raw, hthreshold, hqRaw, hnLoop,
                      hqLt, hread, hi1EqFive,
                      hqnextNe, hnAddEq, hqnextAdd]
                  have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                    rw [hnAddVal, hnN]
                    norm_num
                  refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                  · have hx := hXVY.trans_le hYabove
                    rw [hnnextReal]
                    exact le_of_lt (calc
                      X < (N : Real) + 3 / 2 := hx
                      _ = (N : Real) + 1 + 1 / 2 := by ring)
                  · rw [hnnextReal]
                    have hx' : (N : Real) + 1 < X + 1 / 2 := by
                      convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
                    exact le_of_lt hx'
                  · intro hmid
                    have hXstrict : X < (N : Real) + 3 / 2 :=
                      lt_of_lt_of_le hXVY hYabove
                    rcases hmid with hhi | hlo
                    · have hhi' : X = (N : Real) + 3 / 2 := by
                        calc
                          X = (nnext.val : Real) + 1 / 2 := hhi
                          _ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
                      exact False.elim ((ne_of_lt hXstrict) hhi')
                    · have hlo' : X = (N : Real) + 1 / 2 := by
                        calc
                          X = (nnext.val : Real) - 1 / 2 := hlo
                          _ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
                      exact False.elim ((ne_of_gt hXabove) hlo')
              · have hfabove : 5 < f := by omega
                have hi1NeFive : i1 ≠ 5#u8 := by
                  intro he
                  have hv := congrArg UScalar.val he
                  simp [hreadVal] at hv
                  omega
                have hi1AtLeastFive : 5 <= i1.val := by omega
                have hNbelowV : (N : Real) + 1 / 2 <
                    ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                      (fun z x => 10 * z + x) 0 : Nat) : Real) *
                        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) :=
                  habove hfabove
                have hYabove : Y <= (N : Real) + 3 / 2 := by
                  rcases hsep (N + 1) with hbad | hgood
                  · have hVup :
                        ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                          (fun z x => 10 * z + x) 0 : Nat) : Real) *
                            (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
                        (N : Real) + 1 := by
                      have hu := hFrac.2.1
                      rw [hVfrac] at hu
                      simpa [N] using hu
                    exfalso
                    push_cast at hbad
                    linarith [hbad, hVup]
                  · have hgood' : Y <= (N : Real) + 3 / 2 := by
                      have hgood'' := hgood
                      push_cast at hgood''
                      convert hgood'' using 1 <;> ring
                    exact hgood'
                have hXabove : (N : Real) + 1 / 2 < X := by
                  exact lt_of_lt_of_le hNbelowV hVleX
                have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
                  rw [hnN]
                have hnAddBound : n.val + 1 <= U64.max := by
                  rw [U64.max_eq]
                  have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
                    linarith [hNnext, hXabove, hXhi]
                  have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
                  omega
                obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
                  (U64.add_spec (x := n) (y := 1#u64) hnAddBound)
                have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
                have hroundInc : d.round = ok nnext := by
                  simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
                    hnumNe, hpointNonneg,
                    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                    hcast19Raw, hthreshold, hqRaw, hnLoop,
                    hqLt, hread, hreadVal, hi1NeFive, hnAddEq,
                    hqnextAdd]
                  all_goals omega
                have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
                  rw [hnAddVal, hnN]
                  norm_num
                refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
                · have hx := hXVY.trans_le hYabove
                  rw [hnnextReal]
                  linarith
                · rw [hnnextReal]
                  linarith
                · intro hmid
                  rcases hmid with hmid | hmid <;> rw [hnnextReal] at hmid <;> linarith
          · have hqGe : d.num_digits.val <= q.val := by omega
            have hNreal : (n.val : Real) =
                (integer : Real) * (10 : Real) ^ ((q.val : Int) - (d.num_digits.val : Int)) := by
              rw [hnValue.2 hqGe]
              rw [Nat.cast_mul, Nat.cast_pow]
              have hexp : (q.val : Int) - (d.num_digits.val : Int) =
                  ((q.val - d.num_digits.val : Nat) : Int) := by omega
              rw [hexp, zpow_natCast]
              norm_num
            have hVn :
                ((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
                  (fun z x => 10 * z + x) 0 : Nat) : Real) *
                    (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
                  (n.val : Real) := by
              rw [hdigitsLen] at hV
              rw [hV, hNreal]
            have hSepN := hsep n.val
            have hYbound : Y <= (n.val : Real) + 1 / 2 := by
              rcases hSepN with hbad | hgood
              · rw [hVn] at hbad
                norm_num at hbad
              · simpa using hgood
            have hXbound : X < (n.val : Real) + 1 / 2 := by
              exact lt_of_lt_of_le hXVY hYbound
            have hnumNe : d.num_digits ≠ 0#usize := by
              intro he
              have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
              have hz : d.num_digits.val = 0 := by simpa using hv
              omega
            obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
            have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
              rw [hi19]
              congr 1
              apply IScalar.eq_of_val_eq
              simpa using hi19val
            have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
              exact Result.ok.inj (by simpa [lift] using hcast19)
            have hqRaw : IScalar.hcast .Usize d.decimal_point = q := by
              exact Result.ok.inj (by simpa [lift] using hqCast)
            have hpointVal : d.decimal_point.val < 19 := hpoint
            have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
            have hqNotLt : ¬ q.val < d.num_digits.val := Nat.not_lt_of_ge hqGe
            have hroundN : d.round = ok n := by
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              rw [if_neg hnumNe]
              rw [if_neg hpointNonneg]
              simp only [
                num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
                hcast19Raw, lift]
              simp [hthreshold, hqRaw, hnLoop, hqNotLt]
            refine ⟨n, hroundN, ?_, ?_, ?_⟩
            · exact le_of_lt hXbound
            · have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
              exact le_trans hnX (le_add_of_nonneg_right (by norm_num))
            · intro hmid
              have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
              rcases hmid with hmid | hmid
              · exact False.elim ((ne_of_lt hXbound) hmid)
              · have hlt : (n.val : Real) - 1 / 2 < n.val :=
                  sub_lt_self _ (by norm_num)
                have hXlt : X < (n.val : Real) := by rw [hmid]; exact hlt
                exact False.elim (not_lt_of_ge hnX hXlt)
    ))

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
All 10 entries
Technical checking records
Local closure summary
{
  "node": "P529_T1_RoundArbitraryEndpoint",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "0cfe783033838b708c7f4832b6bac3577c27613cc2391071d2942db6e8075e50",
  "active_statement_hash": "822bb6a9e82825dd9534d1eabb5c89c0b5e35a6002ac5a3f94b0cc296bec1dbb",
  "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": {
    "DecimalSeqFractionalDigitInterval": "24315b479bbec2fd",
    "DecimalSeqRoundLoopValue": "89c751600a31491b",
    "P472_T1_DecimalValueBounds": "4b5c924b5d52bc9d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "606c25078d23ad9c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "cb99c951cfc3a86f"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "13e3cec939d02f51",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "65634ac95536dcca",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "74fa9a4c095ca758"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:d1aef3139c4dc7f7b691c0a3b5a7276bdf6ae1ab0f7458c1beb8fa1829aaae63",
  "current_fingerprint": "sound-v2:d1aef3139c4dc7f7b691c0a3b5a7276bdf6ae1ab0f7458c1beb8fa1829aaae63",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}