DecimalSeqRoundArbitraryEndpoint

Lean checked

Statement

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

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.

Lean statement

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

import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.DecimalGoalVocabularyDefault
import Tablet.DecimalSeqPrimitiveSemantics
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.DecimalSeqRoundLoopValue

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundArbitraryEndpoint]
theorem DecimalSeqRoundArbitraryEndpoint :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (X B : Real),
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
      let value : Real := integer * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      ((d.num_digits.val = 0 /\ digits = [] /\ d.truncated = false /\
          X = 0) \/
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val /\
          digits.getLast? != some 0)) /\
      (exists eta u : Real,
        X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\
        (d.truncated = true <-> 0 < eta)) ->
      d.decimal_point.val < 19 ->
      0 <= X -> X < (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) ->
      (forall n : Nat,
        (n : Real) + (1 / 2 : Real) <= value \/
          B <= (n : Real) + (1 / 2 : Real)) ->
      exists m : Std.U64,
        num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
        X <= (m.val : Real) + (1 / 2 : Real) /\
        (m.val : Real) <= X + (1 / 2 : Real) /\
        (X = (m.val : Real) + (1 / 2 : Real) \/
          X = (m.val : Real) - (1 / 2 : Real) -> 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.

Earlier prose review

An earlier review questioned the prose argument. Lean subsequently checked the formal proof. Review of the statement’s intended meaning is a separate judgment.

Proof

Unfold 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 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 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 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 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 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.

Lean proof


  dsimp
  intro d X B hshape hp19 hX0 hXbound hsep
  let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
  let value : Real := integer * (10 : Real) ^
    (d.decimal_point.val - (d.num_digits.val : Int))
  change d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      ((d.num_digits.val = 0 /\ digits = [] /\ d.truncated = false /\ X = 0) \/
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val /\ digits.getLast? != some 0)) /\
      (exists eta u : Real, X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) at hshape
  change forall n : Nat, (n : Real) + 1 / 2 <= value \/
    B <= (n : Real) + 1 / 2 at hsep
  rcases hshape with ⟨hnum, hdigit, hcanon,
    eta, u, hX, hB, heta0, hetau, hsticky⟩
  simp only [bne_iff_ne] at hcanon
  have hXB : X < B := by rw [hX, hB]; linarith
  have hdigits : forall z, z ∈ digits -> z < 10 := by
    intro z hz
    simp only [digits, List.mem_map] at hz
    rcases hz with ⟨w, hw, rfl⟩
    exact hdigit w hw
  by_cases hzero : d.num_digits.val = 0
  · have hword : d.num_digits = 0#usize :=
      UScalar.eq_of_val_eq (by simpa using hzero)
    have hXeq : X = 0 := by
      rcases hcanon with hz | hp
      · exact hz.2.2.2
      · rcases hp with ⟨z, zs, hds, hz1, hz10, hlen, hlast⟩
        rw [hzero] at hlen
        simp [hds] at hlen
    refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword]
    · norm_num [hXeq]
    · norm_num [hXeq]
    · norm_num [hXeq]
  have hword : d.num_digits ≠ 0#usize := by
    intro he
    apply hzero
    exact congrArg UScalar.val he
  rcases hcanon with hz | hp
  · exact (hzero hz.1).elim
  rcases hp with ⟨z, zs, hdigitsCons, hz1, hz10, hdigitsLen, hlast⟩
  by_cases hpneg : d.decimal_point.val < 0
  · have hpword : d.decimal_point < 0#i32 := by
      simpa [IScalar.lt_equiv] using hpneg
    have hsuffix := NumberHelperDecimalPrefixInterval [] digits hdigits
      (d.decimal_point.val - (digits.length : Int))
    dsimp only at hsuffix
    have hexp : d.decimal_point.val - (digits.length : Int) +
        (digits.length : Int) = d.decimal_point.val := by omega
    have hvaluePow : value < (10 : Real) ^ d.decimal_point.val := by
      rw [hexp] at hsuffix
      rw [hdigitsLen] at hsuffix
      simpa [value, integer] using hsuffix.2
    have hpLe : d.decimal_point.val <= (-1 : Int) := by omega
    have hpowLe : (10 : Real) ^ d.decimal_point.val <= 1 / 10 := by
      calc
        (10 : Real) ^ d.decimal_point.val <= (10 : Real) ^ (-1 : Int) :=
          zpow_le_zpow_right₀ (by norm_num) hpLe
        _ = 1 / 10 := by norm_num [zpow_neg]
    have hvalueHalf : value < 1 / 2 := by linarith
    have hBhalf : B <= 1 / 2 := by
      rcases hsep 0 with h | h
      · norm_num at h
        linarith
      · simpa using h
    have hXhalf : X < 1 / 2 := lt_of_lt_of_le hXB hBhalf
    refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpword]
    · norm_num
      exact hXhalf.le
    · norm_num
      linarith
    · norm_num
  have hp0 : 0 <= d.decimal_point.val := by omega
  have hpnotword : ¬ d.decimal_point < 0#i32 := by
    simpa [IScalar.lt_equiv] using (not_lt.mpr hp0)
  have hcap : lift (UScalar.hcast .I32
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW) =
      ok 19#i32 := by
    have hmaxVal := DecimalSeqPrimitiveSemantics.2.2.1
    have hbound :
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.val <=
          IScalar.max .I32 := by
      rw [hmaxVal]
      simp [I32.max_eq]
    obtain ⟨i, hi, hival⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.hcast_inBounds_spec .I32
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW hbound)
    have hieq : i = 19#i32 := by
      apply IScalar.eq_of_val_eq
      rw [hival, hmaxVal]
      norm_num
    subst i
    exact hi
  have hguard : ¬ d.decimal_point >= 19#i32 := by
    simpa [IScalar.le_equiv] using (not_le.mpr hp19)
  have hguardVal : ¬ (19 : Int) <= d.decimal_point.val := not_le.mpr hp19
  have hdpBounds : 0 <= d.decimal_point.val /\
      d.decimal_point.val <= UScalar.max .Usize := by
    constructor
    · exact hp0
    · rcases System.Platform.numBits_eq with hbits | hbits <;>
        simp [UScalar.max, hbits] <;> omega
  obtain ⟨dp, hdpCast0, hdpVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.hcast_inBounds_spec .Usize d.decimal_point hdpBounds)
  have hdpCast : lift (IScalar.hcast .Usize d.decimal_point) = ok dp := by
    simpa [lift] using hdpCast0
  have hpdp : d.decimal_point.val = (dp.val : Int) := hdpVal.symm
  have hdp18 : dp.val <= 18 := by omega
  obtain ⟨n, hloop, hnPrefix, hnAfter⟩ :=
    DecimalSeqRoundLoopValue d dp hdp18 hnum hdigit
  by_cases hfrac : dp.val < d.num_digits.val
  · have hnval := hnPrefix hfrac
    have hmath := DecimalSeqFractionalDigitInterval digits dp.val
      (by simpa [hdigitsLen] using hfrac) hdigits
      (by simpa [bne_iff_ne] using hlast)
    dsimp only at hmath
    have hN :
        (digits.take dp.val).foldl (fun a z => 10 * a + z) 0 = n.val :=
      hnval.symm
    have hV : (integer : Real) *
        (10 : Real) ^ ((dp.val : Int) - (digits.length : Int)) = value := by
      simp only [value]
      rw [hpdp, hdigitsLen]
    rw [hN, hV] at hmath
    have hvalueLower : (n.val : Real) <= value := hmath.1
    have hvalueUpper : value < (n.val : Real) + 1 := hmath.2.1
    have hXgeN : (n.val : Real) <= X := by rw [hX]; linarith
    have hnextB : B <= (n.val : Real) + 3 / 2 := by
      rcases hsep (n.val + 1) with h | h
      · norm_num at h
        push_cast at h
        linarith
      · push_cast at h
        norm_num at h ⊢
        linarith
    have hXnext : X < (n.val : Real) + 3 / 2 := lt_of_lt_of_le hXB hnextB
    have hdpWord : dp < d.num_digits := by
      simpa [UScalar.lt_equiv] using hfrac
    have hidx : dp.val < d.digits.val.length := by
      have ha : d.digits.val.length = 768 := by simpa using d.digits.property
      omega
    obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
      Aeneas.Std.WP.spec_imp_exists
        (Array.index_usize_spec d.digits dp
          (by simpa [Array.length] using hidx))
    have hdigitVal : digit.val = digits[dp.val] := by
      have hraw : digit = d.digits.val[dp.val]'hidx := hdigitReadVal
      have htake :
          (d.digits.val.take d.num_digits.val)[dp.val]'(by
            simp
            omega) = d.digits.val[dp.val]'hidx := by
        apply List.getElem_take
      simp only [digits, List.getElem_map]
      rw [htake, ← hraw]
    have hdigitLt : digit.val < 10 := by
      rw [hdigitVal]
      exact hdigits _ (List.getElem_mem _)
    have haddBound : dp.val + 1 <= UScalar.max .Usize := by
      rcases System.Platform.numBits_eq with hbits | hbits <;>
        simp [UScalar.max, hbits] <;> omega
    obtain ⟨dp1, hdpAdd, hdp1Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := dp) (y := 1#usize) (by simpa using haddBound))
    have finishDown (hmid : X < (n.val : Real) + 1 / 2)
        (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok n) :
        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
      refine ⟨n, hround, hmid.le, by linarith, ?_⟩
      intro htie
      exfalso
      rcases htie with h | h <;> linarith
    have increment (hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat)) :
        exists n1 : Std.U64, n + 1#u64 = ok n1 /\ n1.val = n.val + 1 := by
      have hnat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hsafe
      have hbound : n.val + 1 <= U64.max := by
        rw [U64.max_eq]
        omega
      obtain ⟨n1, hn1, hn1val⟩ := Aeneas.Std.WP.spec_imp_exists
        (U64.add_spec (x := n) (y := 1#u64) (by simpa using hbound))
      exact ⟨n1, hn1, by simpa using hn1val⟩
    have finishUp (habove : (n.val : Real) + 1 / 2 < X)
        (hcode : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = n + 1#u64) :
        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
      have hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat) := by
        linarith
      obtain ⟨n1, hn1, hn1val⟩ := increment hsafe
      rw [hn1] at hcode
      refine ⟨n1, hcode, ?_, ?_, ?_⟩
      · rw [hn1val]
        push_cast
        linarith
      · rw [hn1val]
        push_cast
        linarith
      · intro htie
        exfalso
        rw [hn1val] at htie
        push_cast at htie
        rcases htie with h | h <;> linarith
    have finishTieDown (htie : X = (n.val : Real) + 1 / 2)
        (heven : n.val % 2 = 0)
        (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok n) :
        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
      refine ⟨n, hround, htie.le, by rw [htie]; linarith, ?_⟩
      exact fun _ => heven
    have finishTieUp (htie : X = (n.val : Real) + 1 / 2)
        (hodd : n.val % 2 = 1)
        (hcode : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = n + 1#u64) :
        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
      have hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat) := by
        linarith
      obtain ⟨n1, hn1, hn1val⟩ := increment hsafe
      rw [hn1] at hcode
      refine ⟨n1, hcode, ?_, ?_, ?_⟩
      · rw [hn1val, htie]
        push_cast
        linarith
      · rw [hn1val, htie]
        push_cast
        linarith
      · intro _
        rw [hn1val]
        omega
    by_cases hbelow : digit.val < 5
    · have hvalueMid : value < (n.val : Real) + 1 / 2 :=
        hmath.2.2.1 (by simpa [hdigitVal] using hbelow)
      have hBmid : B <= (n.val : Real) + 1 / 2 := by
        rcases hsep n.val with h | h
        · linarith
        · exact h
      have hXmid : X < (n.val : Real) + 1 / 2 := lt_of_lt_of_le hXB hBmid
      apply finishDown hXmid
      have hdigitNe : digit ≠ 5#u8 := by
        intro he
        have := congrArg UScalar.val he
        norm_num at this
        omega
      have hnotge : ¬ digit >= 5#u8 := by
        simpa [UScalar.le_equiv] using (not_le.mpr hbelow)
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
        hfrac, hdigitRead, hdigitNe, hnotge]
      omega
    by_cases habove : 5 < digit.val
    · have hvalueMid : (n.val : Real) + 1 / 2 < value :=
        hmath.2.2.2.1 (by simpa [hdigitVal] using habove)
      have hXmid : (n.val : Real) + 1 / 2 < X := by
        rw [hX]
        linarith
      apply finishUp hXmid
      have hdigitNe : digit ≠ 5#u8 := by
        intro he
        have := congrArg UScalar.val he
        norm_num at this
        omega
      have hge : digit >= 5#u8 := by
        simpa [UScalar.le_equiv] using (by omega : 5 <= digit.val)
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
        hfrac, hdigitRead, hdigitNe, hge]
      omega
    · have hdigitVal5 : digit.val = 5 := by omega
      have hdigitEq : digit = 5#u8 :=
        UScalar.eq_of_val_eq (by simpa using hdigitVal5)
      have hfirst5 : digits[dp.val] = 5 := by
        rw [← hdigitVal]
        exact hdigitVal5
      have htieMath := hmath.2.2.2.2 hfirst5
      have hdp1Nat : dp1.val = dp.val + 1 := by
        simpa using hdp1Val
      by_cases hterminal : dp1 = d.num_digits
      · have hterminalNat : dp.val + 1 = digits.length := by
          rw [hdigitsLen, ← hdp1Nat]
          exact congrArg UScalar.val hterminal
        have hvalueTie : value = (n.val : Real) + 1 / 2 :=
          htieMath.1 hterminalNat
        by_cases htrunc : d.truncated = true
        · have hetaPos : 0 < eta := hsticky.mp htrunc
          have hXmid : (n.val : Real) + 1 / 2 < X := by
            rw [hX, hvalueTie]
            linarith
          apply finishUp hXmid
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
          simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
            hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc]
        · have hetaNotPos : ¬ 0 < eta := by
            intro he
            exact htrunc (hsticky.mpr he)
          have hetaEq : eta = 0 := by linarith
          have hXtie : X = (n.val : Real) + 1 / 2 := by
            rw [hX, hvalueTie, hetaEq]
            ring
          by_cases hdpZero : dp.val = 0
          · have hdpEq : dp = 0#usize :=
              UScalar.eq_of_val_eq (by simpa using hdpZero)
            subst dp
            apply finishTieDown hXtie
            · rw [hnval]
              simp
            · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hzero, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc]
          · have hdpPos : 0 < dp.val := by omega
            obtain ⟨dpm1, hdpSub, hdpm1Val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.sub_spec (x := dp) (y := 1#usize)
                (by simpa using hdpPos))
            norm_num at hdpm1Val
            have hdpm1Eq : dpm1.val = dp.val - 1 := hdpm1Val.1
            have hprevIdx : dpm1.val < d.digits.val.length := by
              have ha : d.digits.val.length = 768 := by
                simpa using d.digits.property
              omega
            obtain ⟨prev, hprevRead, hprevReadVal⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (Array.index_usize_spec d.digits dpm1
                  (by simpa [Array.length] using hprevIdx))
            have hprevVal : prev.val = digits[dp.val - 1] := by
              have hrawVal := congrArg UScalar.val hprevReadVal
              rw [hrawVal]
              simp [digits, List.getElem_map, List.getElem_take, hdpm1Eq]
            let pref := (digits.take (dp.val - 1)).foldl
              (fun a z => 10 * a + z) 0
            have htakeLast : digits.take dp.val =
                digits.take (dp.val - 1) ++ [digits[dp.val - 1]] := by
              conv_lhs => rw [show dp.val = (dp.val - 1) + 1 by omega]
              exact List.take_succ_eq_append_getElem
                (by omega : dp.val - 1 < digits.length)
            have hnLast : n.val = 10 * pref + digits[dp.val - 1] := by
              rw [hnval]
              rw [htakeLast, List.foldl_append]
              simp [pref]
            let bit : Std.U8 := 1#u8 &&& prev
            have hbitLift : lift (1#u8 &&& prev) = ok bit := by
              rfl
            have hbitVal : bit.val = prev.val % 2 := by
              change (BitVec.ofNat 8 1 &&& prev.bv).toNat =
                prev.bv.toNat % 2
              rw [BitVec.toNat_and]
              norm_num [Nat.one_and_eq_mod_two]
            have hnParity : n.val % 2 = prev.val % 2 := by
              rw [hnLast, hprevVal]
              omega
            have hdpNe : dp != 0#usize := by
              simp only [bne_iff_ne]
              intro he
              apply hdpZero
              exact congrArg UScalar.val he
            by_cases heven : n.val % 2 = 0
            · have hbitZeroVal : bit.val = 0 := by
                rw [hbitVal, ← hnParity, heven]
              have hbitZero : bit = 0#u8 :=
                UScalar.eq_of_val_eq (by simpa using hbitZeroVal)
              apply finishTieDown hXtie heven
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc,
                hdpNe, hdpZero, hdpSub, hprevRead, hbitLift, hbitZero]
            · have hodd : n.val % 2 = 1 := by
                have := Nat.mod_lt n.val (by omega : 0 < 2)
                omega
              have hbitOneVal : bit.val = 1 := by
                rw [hbitVal, ← hnParity, hodd]
              have hbitNe : bit != 0#u8 := by
                simp only [bne_iff_ne]
                intro he
                have := congrArg UScalar.val he
                norm_num [hbitOneVal] at this
              apply finishTieUp hXtie hodd
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc,
                hdpNe, hdpZero, hdpSub, hprevRead, hbitLift, hbitNe]
      · have hnotTerminalNat : dp.val + 1 ≠ digits.length := by
          intro he
          apply hterminal
          apply UScalar.eq_of_val_eq
          rw [hdp1Nat]
          simpa [hdigitsLen] using he
        have hnonterminalNat : dp.val + 1 < digits.length := by omega
        have hvalueMid : (n.val : Real) + 1 / 2 < value :=
          htieMath.2 hnonterminalNat
        have hXmid : (n.val : Real) + 1 / 2 < X := by
          rw [hX]
          linarith
        apply finishUp hXmid
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
        simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
          hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal]
  · have hle : d.num_digits.val <= dp.val := by omega
    have hnval := hnAfter hle
    have hvalueN : value = (n.val : Real) := by
      dsimp only [value, integer]
      rw [hnval, hpdp, ← Nat.cast_sub hle, zpow_natCast]
      push_cast
      ring
    have hXge : (n.val : Real) <= X := by
      rw [hX, hvalueN]
      linarith
    have hBmid : B <= (n.val : Real) + 1 / 2 := by
      rcases hsep n.val with h | h
      · rw [hvalueN] at h
        linarith
      · exact h
    have hXmid : X < (n.val : Real) + 1 / 2 := lt_of_lt_of_le hXB hBmid
    have hdpWord : ¬ dp < d.num_digits := by
      simpa [UScalar.lt_equiv] using hfrac
    refine ⟨n, ?_, hXmid.le, by linarith, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop, hfrac]
    · intro htie
      exfalso
      rcases htie with h | h <;> linarith

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.DecimalGoalVocabularyDefault
import Tablet.DecimalSeqPrimitiveSemantics
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.DecimalSeqRoundLoopValue

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundArbitraryEndpoint]
theorem DecimalSeqRoundArbitraryEndpoint :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (X B : Real),
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
      let value : Real := integer * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      ((d.num_digits.val = 0 /\ digits = [] /\ d.truncated = false /\
          X = 0) \/
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val /\
          digits.getLast? != some 0)) /\
      (exists eta u : Real,
        X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\
        (d.truncated = true <-> 0 < eta)) ->
      d.decimal_point.val < 19 ->
      0 <= X -> X < (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) ->
      (forall n : Nat,
        (n : Real) + (1 / 2 : Real) <= value \/
          B <= (n : Real) + (1 / 2 : Real)) ->
      exists m : Std.U64,
        num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
        X <= (m.val : Real) + (1 / 2 : Real) /\
        (m.val : Real) <= X + (1 / 2 : Real) /\
        (X = (m.val : Real) + (1 / 2 : Real) \/
          X = (m.val : Real) - (1 / 2 : Real) -> m.val % 2 = 0) := by
-- BODY
  dsimp
  intro d X B hshape hp19 hX0 hXbound hsep
  let digits := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
  let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
  let value : Real := integer * (10 : Real) ^
    (d.decimal_point.val - (d.num_digits.val : Int))
  change d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      ((d.num_digits.val = 0 /\ digits = [] /\ d.truncated = false /\ X = 0) \/
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val /\ digits.getLast? != some 0)) /\
      (exists eta u : Real, X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) at hshape
  change forall n : Nat, (n : Real) + 1 / 2 <= value \/
    B <= (n : Real) + 1 / 2 at hsep
  rcases hshape with ⟨hnum, hdigit, hcanon,
    eta, u, hX, hB, heta0, hetau, hsticky⟩
  simp only [bne_iff_ne] at hcanon
  have hXB : X < B := by rw [hX, hB]; linarith
  have hdigits : forall z, z ∈ digits -> z < 10 := by
    intro z hz
    simp only [digits, List.mem_map] at hz
    rcases hz with ⟨w, hw, rfl⟩
    exact hdigit w hw
  by_cases hzero : d.num_digits.val = 0
  · have hword : d.num_digits = 0#usize :=
      UScalar.eq_of_val_eq (by simpa using hzero)
    have hXeq : X = 0 := by
      rcases hcanon with hz | hp
      · exact hz.2.2.2
      · rcases hp with ⟨z, zs, hds, hz1, hz10, hlen, hlast⟩
        rw [hzero] at hlen
        simp [hds] at hlen
    refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword]
    · norm_num [hXeq]
    · norm_num [hXeq]
    · norm_num [hXeq]
  have hword : d.num_digits ≠ 0#usize := by
    intro he
    apply hzero
    exact congrArg UScalar.val he
  rcases hcanon with hz | hp
  · exact (hzero hz.1).elim
  rcases hp with ⟨z, zs, hdigitsCons, hz1, hz10, hdigitsLen, hlast⟩
  by_cases hpneg : d.decimal_point.val < 0
  · have hpword : d.decimal_point < 0#i32 := by
      simpa [IScalar.lt_equiv] using hpneg
    have hsuffix := NumberHelperDecimalPrefixInterval [] digits hdigits
      (d.decimal_point.val - (digits.length : Int))
    dsimp only at hsuffix
    have hexp : d.decimal_point.val - (digits.length : Int) +
        (digits.length : Int) = d.decimal_point.val := by omega
    have hvaluePow : value < (10 : Real) ^ d.decimal_point.val := by
      rw [hexp] at hsuffix
      rw [hdigitsLen] at hsuffix
      simpa [value, integer] using hsuffix.2
    have hpLe : d.decimal_point.val <= (-1 : Int) := by omega
    have hpowLe : (10 : Real) ^ d.decimal_point.val <= 1 / 10 := by
      calc
        (10 : Real) ^ d.decimal_point.val <= (10 : Real) ^ (-1 : Int) :=
          zpow_le_zpow_right₀ (by norm_num) hpLe
        _ = 1 / 10 := by norm_num [zpow_neg]
    have hvalueHalf : value < 1 / 2 := by linarith
    have hBhalf : B <= 1 / 2 := by
      rcases hsep 0 with h | h
      · norm_num at h
        linarith
      · simpa using h
    have hXhalf : X < 1 / 2 := lt_of_lt_of_le hXB hBhalf
    refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpword]
    · norm_num
      exact hXhalf.le
    · norm_num
      linarith
    · norm_num
  have hp0 : 0 <= d.decimal_point.val := by omega
  have hpnotword : ¬ d.decimal_point < 0#i32 := by
    simpa [IScalar.lt_equiv] using (not_lt.mpr hp0)
  have hcap : lift (UScalar.hcast .I32
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW) =
      ok 19#i32 := by
    have hmaxVal := DecimalSeqPrimitiveSemantics.2.2.1
    have hbound :
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.val <=
          IScalar.max .I32 := by
      rw [hmaxVal]
      simp [I32.max_eq]
    obtain ⟨i, hi, hival⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.hcast_inBounds_spec .I32
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW hbound)
    have hieq : i = 19#i32 := by
      apply IScalar.eq_of_val_eq
      rw [hival, hmaxVal]
      norm_num
    subst i
    exact hi
  have hguard : ¬ d.decimal_point >= 19#i32 := by
    simpa [IScalar.le_equiv] using (not_le.mpr hp19)
  have hguardVal : ¬ (19 : Int) <= d.decimal_point.val := not_le.mpr hp19
  have hdpBounds : 0 <= d.decimal_point.val /\
      d.decimal_point.val <= UScalar.max .Usize := by
    constructor
    · exact hp0
    · rcases System.Platform.numBits_eq with hbits | hbits <;>
        simp [UScalar.max, hbits] <;> omega
  obtain ⟨dp, hdpCast0, hdpVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.hcast_inBounds_spec .Usize d.decimal_point hdpBounds)
  have hdpCast : lift (IScalar.hcast .Usize d.decimal_point) = ok dp := by
    simpa [lift] using hdpCast0
  have hpdp : d.decimal_point.val = (dp.val : Int) := hdpVal.symm
  have hdp18 : dp.val <= 18 := by omega
  obtain ⟨n, hloop, hnPrefix, hnAfter⟩ :=
    DecimalSeqRoundLoopValue d dp hdp18 hnum hdigit
  by_cases hfrac : dp.val < d.num_digits.val
  · have hnval := hnPrefix hfrac
    have hmath := DecimalSeqFractionalDigitInterval digits dp.val
      (by simpa [hdigitsLen] using hfrac) hdigits
      (by simpa [bne_iff_ne] using hlast)
    dsimp only at hmath
    have hN :
        (digits.take dp.val).foldl (fun a z => 10 * a + z) 0 = n.val :=
      hnval.symm
    have hV : (integer : Real) *
        (10 : Real) ^ ((dp.val : Int) - (digits.length : Int)) = value := by
      simp only [value]
      rw [hpdp, hdigitsLen]
    rw [hN, hV] at hmath
    have hvalueLower : (n.val : Real) <= value := hmath.1
    have hvalueUpper : value < (n.val : Real) + 1 := hmath.2.1
    have hXgeN : (n.val : Real) <= X := by rw [hX]; linarith
    have hnextB : B <= (n.val : Real) + 3 / 2 := by
      rcases hsep (n.val + 1) with h | h
      · norm_num at h
        push_cast at h
        linarith
      · push_cast at h
        norm_num at h ⊢
        linarith
    have hXnext : X < (n.val : Real) + 3 / 2 := lt_of_lt_of_le hXB hnextB
    have hdpWord : dp < d.num_digits := by
      simpa [UScalar.lt_equiv] using hfrac
    have hidx : dp.val < d.digits.val.length := by
      have ha : d.digits.val.length = 768 := by simpa using d.digits.property
      omega
    obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
      Aeneas.Std.WP.spec_imp_exists
        (Array.index_usize_spec d.digits dp
          (by simpa [Array.length] using hidx))
    have hdigitVal : digit.val = digits[dp.val] := by
      have hraw : digit = d.digits.val[dp.val]'hidx := hdigitReadVal
      have htake :
          (d.digits.val.take d.num_digits.val)[dp.val]'(by
            simp
            omega) = d.digits.val[dp.val]'hidx := by
        apply List.getElem_take
      simp only [digits, List.getElem_map]
      rw [htake, ← hraw]
    have hdigitLt : digit.val < 10 := by
      rw [hdigitVal]
      exact hdigits _ (List.getElem_mem _)
    have haddBound : dp.val + 1 <= UScalar.max .Usize := by
      rcases System.Platform.numBits_eq with hbits | hbits <;>
        simp [UScalar.max, hbits] <;> omega
    obtain ⟨dp1, hdpAdd, hdp1Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := dp) (y := 1#usize) (by simpa using haddBound))
    have finishDown (hmid : X < (n.val : Real) + 1 / 2)
        (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok n) :
        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
      refine ⟨n, hround, hmid.le, by linarith, ?_⟩
      intro htie
      exfalso
      rcases htie with h | h <;> linarith
    have increment (hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat)) :
        exists n1 : Std.U64, n + 1#u64 = ok n1 /\ n1.val = n.val + 1 := by
      have hnat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hsafe
      have hbound : n.val + 1 <= U64.max := by
        rw [U64.max_eq]
        omega
      obtain ⟨n1, hn1, hn1val⟩ := Aeneas.Std.WP.spec_imp_exists
        (U64.add_spec (x := n) (y := 1#u64) (by simpa using hbound))
      exact ⟨n1, hn1, by simpa using hn1val⟩
    have finishUp (habove : (n.val : Real) + 1 / 2 < X)
        (hcode : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = n + 1#u64) :
        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
      have hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat) := by
        linarith
      obtain ⟨n1, hn1, hn1val⟩ := increment hsafe
      rw [hn1] at hcode
      refine ⟨n1, hcode, ?_, ?_, ?_⟩
      · rw [hn1val]
        push_cast
        linarith
      · rw [hn1val]
        push_cast
        linarith
      · intro htie
        exfalso
        rw [hn1val] at htie
        push_cast at htie
        rcases htie with h | h <;> linarith
    have finishTieDown (htie : X = (n.val : Real) + 1 / 2)
        (heven : n.val % 2 = 0)
        (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok n) :
        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
      refine ⟨n, hround, htie.le, by rw [htie]; linarith, ?_⟩
      exact fun _ => heven
    have finishTieUp (htie : X = (n.val : Real) + 1 / 2)
        (hodd : n.val % 2 = 1)
        (hcode : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = n + 1#u64) :
        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
      have hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat) := by
        linarith
      obtain ⟨n1, hn1, hn1val⟩ := increment hsafe
      rw [hn1] at hcode
      refine ⟨n1, hcode, ?_, ?_, ?_⟩
      · rw [hn1val, htie]
        push_cast
        linarith
      · rw [hn1val, htie]
        push_cast
        linarith
      · intro _
        rw [hn1val]
        omega
    by_cases hbelow : digit.val < 5
    · have hvalueMid : value < (n.val : Real) + 1 / 2 :=
        hmath.2.2.1 (by simpa [hdigitVal] using hbelow)
      have hBmid : B <= (n.val : Real) + 1 / 2 := by
        rcases hsep n.val with h | h
        · linarith
        · exact h
      have hXmid : X < (n.val : Real) + 1 / 2 := lt_of_lt_of_le hXB hBmid
      apply finishDown hXmid
      have hdigitNe : digit ≠ 5#u8 := by
        intro he
        have := congrArg UScalar.val he
        norm_num at this
        omega
      have hnotge : ¬ digit >= 5#u8 := by
        simpa [UScalar.le_equiv] using (not_le.mpr hbelow)
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
        hfrac, hdigitRead, hdigitNe, hnotge]
      omega
    by_cases habove : 5 < digit.val
    · have hvalueMid : (n.val : Real) + 1 / 2 < value :=
        hmath.2.2.2.1 (by simpa [hdigitVal] using habove)
      have hXmid : (n.val : Real) + 1 / 2 < X := by
        rw [hX]
        linarith
      apply finishUp hXmid
      have hdigitNe : digit ≠ 5#u8 := by
        intro he
        have := congrArg UScalar.val he
        norm_num at this
        omega
      have hge : digit >= 5#u8 := by
        simpa [UScalar.le_equiv] using (by omega : 5 <= digit.val)
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
        hfrac, hdigitRead, hdigitNe, hge]
      omega
    · have hdigitVal5 : digit.val = 5 := by omega
      have hdigitEq : digit = 5#u8 :=
        UScalar.eq_of_val_eq (by simpa using hdigitVal5)
      have hfirst5 : digits[dp.val] = 5 := by
        rw [← hdigitVal]
        exact hdigitVal5
      have htieMath := hmath.2.2.2.2 hfirst5
      have hdp1Nat : dp1.val = dp.val + 1 := by
        simpa using hdp1Val
      by_cases hterminal : dp1 = d.num_digits
      · have hterminalNat : dp.val + 1 = digits.length := by
          rw [hdigitsLen, ← hdp1Nat]
          exact congrArg UScalar.val hterminal
        have hvalueTie : value = (n.val : Real) + 1 / 2 :=
          htieMath.1 hterminalNat
        by_cases htrunc : d.truncated = true
        · have hetaPos : 0 < eta := hsticky.mp htrunc
          have hXmid : (n.val : Real) + 1 / 2 < X := by
            rw [hX, hvalueTie]
            linarith
          apply finishUp hXmid
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
          simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
            hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc]
        · have hetaNotPos : ¬ 0 < eta := by
            intro he
            exact htrunc (hsticky.mpr he)
          have hetaEq : eta = 0 := by linarith
          have hXtie : X = (n.val : Real) + 1 / 2 := by
            rw [hX, hvalueTie, hetaEq]
            ring
          by_cases hdpZero : dp.val = 0
          · have hdpEq : dp = 0#usize :=
              UScalar.eq_of_val_eq (by simpa using hdpZero)
            subst dp
            apply finishTieDown hXtie
            · rw [hnval]
              simp
            · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hzero, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc]
          · have hdpPos : 0 < dp.val := by omega
            obtain ⟨dpm1, hdpSub, hdpm1Val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.sub_spec (x := dp) (y := 1#usize)
                (by simpa using hdpPos))
            norm_num at hdpm1Val
            have hdpm1Eq : dpm1.val = dp.val - 1 := hdpm1Val.1
            have hprevIdx : dpm1.val < d.digits.val.length := by
              have ha : d.digits.val.length = 768 := by
                simpa using d.digits.property
              omega
            obtain ⟨prev, hprevRead, hprevReadVal⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (Array.index_usize_spec d.digits dpm1
                  (by simpa [Array.length] using hprevIdx))
            have hprevVal : prev.val = digits[dp.val - 1] := by
              have hrawVal := congrArg UScalar.val hprevReadVal
              rw [hrawVal]
              simp [digits, List.getElem_map, List.getElem_take, hdpm1Eq]
            let pref := (digits.take (dp.val - 1)).foldl
              (fun a z => 10 * a + z) 0
            have htakeLast : digits.take dp.val =
                digits.take (dp.val - 1) ++ [digits[dp.val - 1]] := by
              conv_lhs => rw [show dp.val = (dp.val - 1) + 1 by omega]
              exact List.take_succ_eq_append_getElem
                (by omega : dp.val - 1 < digits.length)
            have hnLast : n.val = 10 * pref + digits[dp.val - 1] := by
              rw [hnval]
              rw [htakeLast, List.foldl_append]
              simp [pref]
            let bit : Std.U8 := 1#u8 &&& prev
            have hbitLift : lift (1#u8 &&& prev) = ok bit := by
              rfl
            have hbitVal : bit.val = prev.val % 2 := by
              change (BitVec.ofNat 8 1 &&& prev.bv).toNat =
                prev.bv.toNat % 2
              rw [BitVec.toNat_and]
              norm_num [Nat.one_and_eq_mod_two]
            have hnParity : n.val % 2 = prev.val % 2 := by
              rw [hnLast, hprevVal]
              omega
            have hdpNe : dp != 0#usize := by
              simp only [bne_iff_ne]
              intro he
              apply hdpZero
              exact congrArg UScalar.val he
            by_cases heven : n.val % 2 = 0
            · have hbitZeroVal : bit.val = 0 := by
                rw [hbitVal, ← hnParity, heven]
              have hbitZero : bit = 0#u8 :=
                UScalar.eq_of_val_eq (by simpa using hbitZeroVal)
              apply finishTieDown hXtie heven
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc,
                hdpNe, hdpZero, hdpSub, hprevRead, hbitLift, hbitZero]
            · have hodd : n.val % 2 = 1 := by
                have := Nat.mod_lt n.val (by omega : 0 < 2)
                omega
              have hbitOneVal : bit.val = 1 := by
                rw [hbitVal, ← hnParity, hodd]
              have hbitNe : bit != 0#u8 := by
                simp only [bne_iff_ne]
                intro he
                have := congrArg UScalar.val he
                norm_num [hbitOneVal] at this
              apply finishTieUp hXtie hodd
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc,
                hdpNe, hdpZero, hdpSub, hprevRead, hbitLift, hbitNe]
      · have hnotTerminalNat : dp.val + 1 ≠ digits.length := by
          intro he
          apply hterminal
          apply UScalar.eq_of_val_eq
          rw [hdp1Nat]
          simpa [hdigitsLen] using he
        have hnonterminalNat : dp.val + 1 < digits.length := by omega
        have hvalueMid : (n.val : Real) + 1 / 2 < value :=
          htieMath.2 hnonterminalNat
        have hXmid : (n.val : Real) + 1 / 2 < X := by
          rw [hX]
          linarith
        apply finishUp hXmid
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
        simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
          hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal]
  · have hle : d.num_digits.val <= dp.val := by omega
    have hnval := hnAfter hle
    have hvalueN : value = (n.val : Real) := by
      dsimp only [value, integer]
      rw [hnval, hpdp, ← Nat.cast_sub hle, zpow_natCast]
      push_cast
      ring
    have hXge : (n.val : Real) <= X := by
      rw [hX, hvalueN]
      linarith
    have hBmid : B <= (n.val : Real) + 1 / 2 := by
      rcases hsep n.val with h | h
      · rw [hvalueN] at h
        linarith
      · exact h
    have hXmid : X < (n.val : Real) + 1 / 2 := lt_of_lt_of_le hXB hBmid
    have hdpWord : ¬ dp < d.num_digits := by
      simpa [UScalar.lt_equiv] using hfrac
    refine ⟨n, ?_, hXmid.le, by linarith, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop, hfrac]
    · intro htie
      exfalso
      rcases htie with h | h <;> linarith

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (116)
More program dependencies (83)
Standard-library dependencies (30)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 12 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqRoundArbitraryEndpoint",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "8e29f93f9af73405e9a2862808ee5e73749c58c8cbde5db22ebddcdb4822a9c0",
  "active_statement_hash": "694f7ab43baaae6ead6bb46ab84174b48d8ae6294cad509327f4a34edb241d79",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-182",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalSeqFractionalDigitInterval": "24315b479bbec2fd",
    "DecimalSeqPrimitiveSemantics": "d1871c68600bfbcd",
    "DecimalSeqRoundLoopValue": "89c751600a31491b",
    "NumberHelperDecimalPrefixInterval": "5c32858414034b55",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "cb99c951cfc3a86f"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_Insts_CoreDefaultDefault_default": "7b9921904495bc2a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "65634ac95536dcca",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_round": "74fa9a4c095ca758",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_try_add_digit": "69a4869a11307175"
  }
}
Prose assessment
{
  "label": "Sound flag superseded by recorded Lean closure",
  "assessment_status": "ReviewerPinnedFail",
  "fingerprint_relation": "differs from current fingerprint",
  "assessment_fingerprint": "sound-v2:7841d27da62fb03c99ad2ed5586438dd704c59e91ce0b3685ea73da290796758",
  "current_fingerprint": "sound-v2:070e509914c7827e64b7e00d6ef2869296b4eef7f4df012d635674bf7f4fbc33",
  "superseded_for_theorem_closure": true
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Fail",
  "substantiveness_status": "Pass"
}