RoundingDeterminism

Lean checked

Statement

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

Theorem

For every nonnegative real number, any two IEEE 754 binary64 magnitude targets under round-to-nearest, ties-to-even are the same raw word.

Lean statement

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

import Tablet.DecimalGoalVocabularyDefault
import Tablet.F64
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: RoundingDeterminism]
theorem RoundingDeterminism :
    ∀ (x : Real) (a b : F64), 0 <= x -> DecimalGoalVocabularyDefault.roundsMagnitude x a ->
      DecimalGoalVocabularyDefault.roundsMagnitude x b -> a = b := 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

Put
F=2^{52},\qquad I=2047F,\qquad T=2^{1024}-2^{970}.
By F64, the carrier consists of unsigned 64-bit words. Since
I=(2^{11}-1)2^{52}=2^{63}-2^{52}<2^{64}, \tag{1}
every natural number at most I is the value field of a carrier word, and two carrier words with the same value field are equal by extensionality of the unsigned-word structure.
Fix x,a,b, assume 0\leq x, and assume that both a and b are rounding targets. Unfold the roundsMagnitude field of DecimalGoalVocabularyDefault. For a natural index n<I, write
e=n/F,\qquad m=n\bmod F.
Euclidean division gives
n=eF+m,\qquad 0\leq m<F,\qquad e<2047. \tag{2}
The finite magnitude represented at n is therefore
\phi(n)= \begin{cases} (m:\mathbb R)/2^{1074},&e=0,\\ (F+m:\mathbb R)/2^{1075-e},&0<e<1075,\\ (F+m:\mathbb R)2^{e-1075},&1075\leq e, \end{cases} \tag{3}
where all subtractions in exponents are natural-number subtractions under the displayed side conditions. Unfolding also says that every target w satisfies w.\mathit{val}\leq I. If w.\mathit{val}=I, its conditional branch requires T\leq x. If w.\mathit{val}\ne I, the bound implies w.\mathit{val}<I, and its finite branch requires x<T and, for every finite competitor v\ne w,
|x-\phi(w.\mathit{val})|\leq |x-\phi(v.\mathit{val})|, \tag{4}
with w.\mathit{val}\bmod2=0 whenever equality holds in (4).
We first prove the arithmetic fact needed below: \phi is strictly increasing on the indices below I. It is enough to prove
n+1<I\quad\Longrightarrow\quad \phi(n)<\phi(n+1), \tag{5}
because any p<q<I is joined by the finite chain p,p+1,\ldots,q, and transitivity of strict real order then gives
p<q<I\quad\Longrightarrow\quad \phi(p)<\phi(q). \tag{6}
For (5), use (2) for n=eF+m. If m+1<F, the quotient by F remains e and the remainder becomes m+1. Direct subtraction in every branch of (3) gives
\phi(n+1)-\phi(n)= \begin{cases} 1/2^{1074},&e=0,\\ 1/2^{1075-e},&1\leq e<1075,\\ 2^{e-1075},&1075\leq e<2047. \end{cases} \tag{7}
Every denominator in (7) is positive, and every power in (7) is positive, so the difference is positive.
It remains to check fraction overflow. If m+1 is not less than F, then m<F from (2) forces m=F-1; hence n+1=(e+1)F has remainder zero. The hypothesis n+1<I=2047F gives e+1<2047, so e\leq2045. There are four exhaustive cases. At the subnormal/first-normal boundary e=0,
\phi(n+1)-\phi(n) ={F\over2^{1074}}-{F-1\over2^{1074}} ={1\over2^{1074}}>0. \tag{8}
For 1\leq e\leq1073, both words use the reciprocal normal formula, and
\phi(n+1)-\phi(n) ={F\over2^{1074-e}}-{2F-1\over2^{1075-e}} ={2F-(2F-1)\over2^{1075-e}} ={1\over2^{1075-e}}>0. \tag{9}
At the formula change e=1074, the old word uses the reciprocal branch and the new word, whose exponent field is 1075, uses the multiplicative branch:
\phi(n+1)-\phi(n)=F-{2F-1\over2}={1\over2}>0. \tag{10}
Finally, for 1075\leq e\leq2045, both words use the multiplicative normal formula, and
\phi(n+1)-\phi(n) =F2^{e+1-1075}-(2F-1)2^{e-1075} =2^{e-1075}>0. \tag{11}
Equations (7)--(11) prove (5), including every within-bin increment, the subnormal boundary, and both sides of the formula change; hence (6) follows.
We now split at the overflow threshold. Suppose first that T\leq x. For either w=a or w=b, if w.\mathit{val}\ne I, the finite branch just unfolded would give x<T, contradicting T\leq x. Thus a.\mathit{val}=I=b.\mathit{val}, and carrier extensionality gives a=b.
Suppose instead that x<T. Neither target can have value field I, since the infinity branch would give T\leq x, contradicting x<T. Combining this inequality with each target's bound by I gives
a.\mathit{val}<I,\qquad b.\mathit{val}<I. \tag{12}
If their value fields are equal, carrier extensionality again gives a=b. It remains to rule out unequal value fields. Equal words have equal value fields, so unequal value fields imply a\ne b. Thus b is an admissible competitor in (4) for target a, by (12), and a is an admissible competitor for target b. Put
d_a=|x-\phi(a.\mathit{val})|, \qquad d_b=|x-\phi(b.\mathit{val})|.
The two applications of (4) give d_a\leq d_b and d_b\leq d_a, hence d_a=d_b. The equality implication in the application for target a gives a.\mathit{val}\bmod2=0, and the one for target b gives b.\mathit{val}\bmod2=0. (13)
We next show that the two unequal indices would have to be consecutive. Assume first
p:=a.\mathit{val}<q:=b.\mathit{val}.
Natural-number discreteness gives p+1\leq q. If p+1<q, then (12) gives p+1<q<I, so (1) and F64 supply a word c with c.\mathit{val}=p+1. Its value field is below I, and c\ne a because p+1\ne p. By (6), with
A=\phi(p),\qquad Z=\phi(p+1),\qquad B=\phi(q),
we have A<Z<B. The already proved equality d_a=d_b says |x-A|=|x-B|. This forces A<x<B: if x\leq A, then |x-B|=B-x>A-x=|x-A|, while if B\leq x, then |x-A|=x-A>x-B=|x-B|; each alternative contradicts equality.
The total order on the reals gives either Z\leq x or x\leq Z. In the first case,
|x-Z|=x-Z<x-A=|x-A|,
because A<Z; in the second case,
|x-Z|=Z-x<B-x=|x-B|=|x-A|,
because Z<B. These absolute-value identities use respectively the shown nonnegative differences. In both cases c is a finite competitor distinct from a, but its distance is strictly smaller than a's, contradicting (4). Therefore p+1<q is impossible. Together with p+1\leq q, this proves q=p+1.
The other possible strict order is q:=b.\mathit{val}<p:=a.\mathit{val}. Repeating the preceding argument with a,b interchanged gives p=q+1: if q+1<p, the carrier word with value field q+1 is below I, is distinct from b, has finite value strictly between \phi(q) and \phi(p) by (6), and the same two real-order cases make its distance strictly smaller than the common endpoint distance, contradicting (4) for target b. These two strict orders are exhaustive for unequal natural value fields.
Finally, consecutive natural numbers cannot both have remainder zero modulo 2: if r\bmod2=0, Euclidean division writes r=2k, so (r+1)\bmod2=1. In the first strict-order case, q=p+1 contradicts the two equalities in (13); in the second, p=q+1 does the same. Thus unequal value fields are impossible. Consequently a.\mathit{val}=b.\mathit{val}, and unsigned-word extensionality yields a=b. The threshold order is total, so the cases T\leq x and x<T complete the proof.

Lean proof


  intro x a b hx ha hb
  dsimp [DecimalGoalVocabularyDefault] at ha hb
  let unit : Nat := 2 ^ 52
  let top : Nat := 2047 * unit
  let threshold : Real := (2 : Real) ^ 1024 - (2 : Real) ^ 970
  let value : Nat → Real := fun n =>
    let e := n / 4503599627370496
    let m := n % 4503599627370496
    if e = 0 then (m : Real) / (2 : Real) ^ 1074
    else if 1075 ≤ e then
      ((4503599627370496 + m : Nat) : Real) * (2 : Real) ^ (e - 1075)
    else ((4503599627370496 + m : Nat) : Real) / (2 : Real) ^ (1075 - e)
  have step : ∀ n : Nat, n + 1 < top → value n < value (n + 1) := by
    intro n hn
    let e := n / unit
    let m := n % unit
    have unit_pos : 0 < unit := by norm_num [unit]
    have rem_bound : m < unit := by
      exact Nat.mod_lt _ unit_pos
    have decomp : unit * e + m = n := by
      exact Nat.div_add_mod n unit
    have top_value : top = 9218868437227405312 := by norm_num [top, unit]
    have n_bound : n + 1 < 9218868437227405312 := by simpa [top_value] using hn
    norm_num [unit] at decomp rem_bound n_bound ⊢
    have decomp_symbol : unit * e + m = n := by exact decomp
    by_cases small : m + 1 < 4503599627370496
    · have next_decomp : 4503599627370496 * e + (m + 1) = n + 1 := by omega
      have next_quotient : (n + 1) / 4503599627370496 = e := by
        omega
      have next_remainder : (n + 1) % 4503599627370496 = m + 1 := by
        omega
      have value_n : value n = if e = 0 then (m : Real) / (2 : Real)^1074 else
          if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real)^(e-1075)
          else ((4503599627370496 + m : Nat) : Real) / (2 : Real)^(1075-e) := by
        dsimp [value]
        rw [← decomp]
        have quotient : (4503599627370496 * e + m) / 4503599627370496 = e := by
          rw [Nat.mul_add_div (by norm_num)]
          simp [Nat.div_eq_of_lt rem_bound]
        have remainder : (4503599627370496 * e + m) % 4503599627370496 = m := by
          rw [Nat.mul_add_mod]
          simp [Nat.mod_eq_of_lt rem_bound]
        simp [quotient, remainder] <;> ring
      have value_next : value (n+1) = if e = 0 then (m+1 : Real) / (2 : Real)^1074 else
          if 1075 ≤ e then ((4503599627370496 + m + 1 : Nat) : Real) * (2 : Real)^(e-1075)
          else ((4503599627370496 + m + 1 : Nat) : Real) / (2 : Real)^(1075-e) := by
        dsimp [value]
        rw [← next_decomp]
        have quotient : (4503599627370496 * e + (m + 1)) / 4503599627370496 = e := by
          rw [Nat.mul_add_div (by norm_num)]
          simp [Nat.div_eq_of_lt (by omega)]
        have remainder : (4503599627370496 * e + (m + 1)) % 4503599627370496 = m + 1 := by
          rw [Nat.mul_add_mod]
          simp [Nat.mod_eq_of_lt (by omega)]
        simp only [quotient, remainder, Nat.cast_add]
        by_cases z : e = 0
        · simp [z] <;> ring
        · by_cases h : 1075 ≤ e
          · simp [z, h] <;> ring
          · simp [z, h] <;> ring
      rw [value_n, value_next]
      by_cases zero : e = 0
      · simp [zero]
        apply (div_lt_div_iff_of_pos_right (by positivity)).2
        norm_num
      · by_cases high : 1075 ≤ e
        · simp [zero, high]
        · simp [zero, high]
          apply (div_lt_div_iff_of_pos_right (by positivity)).2
          norm_num
    · have edge : m + 1 = 4503599627370496 := by omega
      have next_decomp : 4503599627370496 * (e + 1) = n + 1 := by omega
      have next_quotient : (n + 1) / 4503599627370496 = e + 1 := by
        omega
      have next_remainder : (n + 1) % 4503599627370496 = 0 := by
        omega
      have value_n : value n = if e = 0 then (m : Real) / (2 : Real)^1074 else
          if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real)^(e-1075)
          else ((4503599627370496 + m : Nat) : Real) / (2 : Real)^(1075-e) := by
        dsimp [value]
        rw [← decomp]
        have quotient : (4503599627370496 * e + m) / 4503599627370496 = e := by
          rw [Nat.mul_add_div (by norm_num)]
          simp [Nat.div_eq_of_lt rem_bound]
        have remainder : (4503599627370496 * e + m) % 4503599627370496 = m := by
          rw [Nat.mul_add_mod]
          simp [Nat.mod_eq_of_lt rem_bound]
        simp [quotient, remainder]
      have value_next : value (n+1) = if e+1 = 0 then (0 : Real) / (2 : Real)^1074 else
          if 1075 ≤ e+1 then ((4503599627370496 : Nat) : Real) * (2 : Real)^((e+1)-1075)
          else ((4503599627370496 : Nat) : Real) / (2 : Real)^ (1075-(e+1)) := by
        dsimp [value]
        rw [← next_decomp]
        have quotient : (4503599627370496 * (e + 1)) / 4503599627370496 = e + 1 := by
          rw [show 4503599627370496 * (e + 1) = (e + 1) * 4503599627370496 by ring]
          exact Nat.mul_div_left _ (by norm_num)
        have remainder : (4503599627370496 * (e + 1)) % 4503599627370496 = 0 := by
          exact Nat.mul_mod_right _ _
        simp [quotient, remainder] <;> ring
      rw [value_n, value_next]
      by_cases zero : e = 0
      · have m_edge : m = 4503599627370495 := by omega
        have next_zero : ¬e + 1 = 0 := by omega
        have next_high : ¬1075 ≤ e + 1 := by omega
        simp only [if_pos zero, if_neg next_zero, if_neg next_high]
        rw [m_edge]
        rw [show 1075 - (e + 1) = 1074 by omega]
        apply (div_lt_div_iff_of_pos_right (by positivity)).2
        norm_num
      · by_cases boundary : e = 1074
        · have low : e < 1075 := by omega
          have not_high : ¬1075 ≤ e := by omega
          have next_high : 1075 ≤ e + 1 := by omega
          have next_zero : ¬e + 1 = 0 := by omega
          simp only [if_neg zero, if_neg not_high, if_pos boundary,
            if_neg next_zero, if_pos next_high]
          rw [show 1075 - e = 1 by omega, show e + 1 - 1075 = 0 by omega]
          have cast_bound : (m : Real) < 4503599627370496 := by exact_mod_cast rem_bound
          norm_num at ⊢
          linarith
        · by_cases high : 1075 ≤ e
          · have high2 : 1074 ≤ e := by omega
            have next_zero : ¬e + 1 = 0 := by omega
            have next_high : 1075 ≤ e + 1 := by omega
            simp only [if_neg zero, if_pos high, if_neg next_zero, if_pos next_high]
            rw [show e + 1 - 1075 = e - 1074 by omega]
            have pos : (0 : Real) < (2 : Real)^(e-1075) := by positivity
            have cast_bound : (m : Real) < 4503599627370496 := by exact_mod_cast rem_bound
            have cast_sum : ((4503599627370496 + m : Nat) : Real) =
                (4503599627370496 : Real) + m := by norm_num
            rw [cast_sum]
            have nextpow : (2 : Real)^(e-1074) = 2 * (2 : Real)^(e-1075) := by
              rw [show e-1074 = (e-1075)+1 by omega, pow_succ]
              ring
            rw [nextpow]
            have base : (4503599627370496 : Real) + m < 2 * 4503599627370496 := by
              linarith
            calc
              (4503599627370496 + (m : Real)) * (2 : Real)^(e-1075) <
                  (2 * 4503599627370496 : Real) * (2 : Real)^(e-1075) :=
                mul_lt_mul_of_pos_right base pos
              _ = 4503599627370496 * (2 * (2 : Real)^(e-1075)) := by ring
          · have low : e < 1075 := by omega
            have low2 : e < 1074 := by omega
            have not_high : ¬1074 ≤ e := by omega
            have next_zero : ¬e + 1 = 0 := by omega
            have next_high : ¬1075 ≤ e + 1 := by omega
            simp only [if_neg zero, if_neg high, if_neg next_zero, if_neg next_high]
            rw [show 1075 - (e + 1) = 1074 - e by omega]
            have cast_bound : (m : Real) < 4503599627370496 := by exact_mod_cast rem_bound
            have cast_sum : ((4503599627370496 + m : Nat) : Real) =
                (4503599627370496 : Real) + m := by norm_num
            rw [cast_sum]
            have denrel : (2 : Real)^(1075-e) = 2 * (2 : Real)^(1074-e) := by
              rw [show 1075-e = (1074-e)+1 by omega, pow_succ]
              ring
            have rhs : (4503599627370496 : Real) / (2 : Real)^(1074-e) =
                (2 * 4503599627370496 : Real) / (2 : Real)^(1075-e) := by
              rw [denrel]
              apply (div_eq_div_iff (by positivity) (by positivity)).2
              ring
            have base : (4503599627370496 : Real) + m < 2 * 4503599627370496 := by
              linarith
            exact ((div_lt_div_iff_of_pos_right (by positivity)).2 base).trans_eq rhs.symm
  have chain : ∀ p k : Nat, 0 < k → p + k < top → value p < value (p + k) := by
    intro p k hk hbound
    induction k with
    | zero => omega
    | succ k ih =>
        by_cases base : k = 0
        · subst k
          simpa using step p (by omega)
        · have prior : value p < value (p + k) := ih (by omega) (by omega)
          have next : value (p + k) < value (p + k + 1) := by
            apply step
            omega
          exact prior.trans next
  have monotone : ∀ p q : Nat, p < q → q < top → value p < value q := by
    intro p q hp hq
    obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le (Nat.le_of_lt hp)
    have hk : 0 < k := by omega
    exact chain p k hk hq
  change 0 ≤ x ∧ (↑a : Nat) ≤ top ∧
    (if (↑a : Nat) = top then threshold ≤ x else
      x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ a →
        abs (x - value (↑a : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
        (abs (x - value (↑a : Nat)) = abs (x - value (↑v : Nat)) →
          (↑a : Nat) % 2 = 0)) at ha
  change 0 ≤ x ∧ (↑b : Nat) ≤ top ∧
    (if (↑b : Nat) = top then threshold ≤ x else
      x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ b →
        abs (x - value (↑b : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
        (abs (x - value (↑b : Nat)) = abs (x - value (↑v : Nat)) →
          (↑b : Nat) % 2 = 0)) at hb
  have word_eq : ∀ u v : F64, (↑u : Nat) = ↑v → u = v := by
    intro u v h
    cases u with
    | mk ub =>
        cases v with
        | mk vb =>
            congr
            apply BitVec.eq_of_toNat_eq
            change ub.toNat = vb.toNat at h
            exact h
  have make_word : ∀ n : Nat, n < top → ∃ c : F64, (↑c : Nat) = n := by
    intro n hn
    have bound : n ≤ UScalar.cMax UScalarTy.U64 := by
      apply le_trans (Nat.le_of_lt hn)
      norm_num [top, unit, UScalar.cMax, UScalar.rMax, U64.rMax, UScalarTy.numBits]
    refine ⟨UScalar.ofNat n bound, ?_⟩
    rfl
  have closer : ∀ A Z B : Real, A < Z → Z < B →
      abs (x - A) = abs (x - B) → abs (x - Z) < abs (x - A) := by
    intro A Z B hAZ hZB hEq
    have between : A < x ∧ x < B := by
      constructor
      · by_contra h
        have hxA : x ≤ A := le_of_not_gt h
        have hxB : x ≤ B := le_trans hxA (le_of_lt (hAZ.trans hZB))
        rw [abs_of_nonpos (sub_nonpos.mpr hxA), abs_of_nonpos (sub_nonpos.mpr hxB)] at hEq
        linarith
      · by_contra h
        have hBx : B ≤ x := le_of_not_gt h
        have hAx : A ≤ x := le_trans (le_of_lt hAZ)
          (le_trans (le_of_lt hZB) hBx)
        rw [abs_of_nonneg (sub_nonneg.mpr hAx), abs_of_nonneg (sub_nonneg.mpr hBx)] at hEq
        linarith
    rcases le_total Z x with hZx | hxZ
    · rw [abs_of_nonneg (sub_nonneg.mpr hZx),
        abs_of_nonneg (sub_nonneg.mpr between.1.le)]
      linarith
    · have hAeqB : abs (x - A) = B - x := by
        rw [abs_of_nonpos (sub_nonpos.mpr between.2.le)] at hEq
        simpa [sub_eq_add_neg] using hEq
      calc
        abs (x - Z) = Z - x := by
          rw [abs_of_nonpos (sub_nonpos.mpr hxZ)]
          ring
        _ < B - x := by linarith
        _ = abs (x - A) := hAeqB.symm
  by_cases high : threshold ≤ x
  · have atop : (↑a : Nat) = top := by
      by_contra h
      have condition := (ha.2).2
      simp [h] at condition
      have hfinite : x < threshold := by
        exact condition.1
      exact (not_lt_of_ge high) hfinite
    have btop : (↑b : Nat) = top := by
      by_contra h
      have condition := (hb.2).2
      simp [h] at condition
      have hfinite : x < threshold := by
        exact condition.1
      exact (not_lt_of_ge high) hfinite
    apply word_eq
    omega
  · have low : x < threshold := lt_of_not_ge high
    have atop : (↑a : Nat) ≠ top := by
      intro h
      have hthreshold : threshold ≤ x := by simpa [h] using (ha.2).2
      exact (not_le_of_gt low) hthreshold
    have btop : (↑b : Nat) ≠ top := by
      intro h
      have hthreshold : threshold ≤ x := by simpa [h] using (hb.2).2
      exact (not_le_of_gt low) hthreshold
    have alt : (↑a : Nat) < top := by omega
    have blt : (↑b : Nat) < top := by omega
    have ha_finite := (ha.2).2
    have hb_finite := (hb.2).2
    simp [atop] at ha_finite
    simp [btop] at hb_finite
    have anear := ha_finite.2
    have bnear := hb_finite.2
    by_cases indices : (↑a : Nat) = ↑b
    · exact word_eq a b indices
    · have words : a ≠ b := by
        intro h
        apply indices
        simpa [h]
      have dab := anear b blt (by intro h; exact indices h.symm)
      have dba := bnear a alt (by intro h; exact indices h)
      have equal_distance : abs (x - value (↑a : Nat)) = abs (x - value (↑b : Nat)) :=
        le_antisymm dab.1 dba.1
      have even_a : (↑a : Nat) % 2 = 0 := dab.2 equal_distance
      have even_b : (↑b : Nat) % 2 = 0 := dba.2 equal_distance.symm
      by_cases order : (↑a : Nat) < ↑b
      · have no_gap : ¬(↑a : Nat) + 1 < ↑b := by
          intro gap
          obtain ⟨c, hc⟩ := make_word ((↑a : Nat) + 1) (by omega)
          have c_lt : (↑c : Nat) < top := by simpa [hc] using show (↑a : Nat) + 1 < top by omega
          have c_ne : (↑c : Nat) ≠ (↑a : Nat) := by
            intro h
            rw [hc] at h
            omega
          have competitor := anear c c_lt c_ne
          have left_step := monotone (↑a : Nat) ((↑a : Nat) + 1) (by omega) (by omega)
          have right_step := monotone ((↑a : Nat) + 1) (↑b : Nat) gap blt
          have middle : abs (x - value ((↑a : Nat) + 1)) < abs (x - value (↑a : Nat)) :=
            closer (value (↑a : Nat)) (value ((↑a : Nat) + 1)) (value (↑b : Nat))
              left_step right_step equal_distance
          have competitor' : abs (x - value (↑a : Nat)) ≤
              abs (x - value ((↑a : Nat) + 1)) := by
            simpa [hc] using competitor.1
          exact (not_lt_of_ge competitor') middle
        have consecutive : (↑b : Nat) = (↑a : Nat) + 1 := by omega
        have : False := by omega
        exact this.elim
      · have order' : (↑b : Nat) < (↑a : Nat) := by omega
        have no_gap : ¬(↑b : Nat) + 1 < ↑a := by
          intro gap
          obtain ⟨c, hc⟩ := make_word ((↑b : Nat) + 1) (by omega)
          have c_lt : (↑c : Nat) < top := by simpa [hc] using show (↑b : Nat) + 1 < top by omega
          have c_ne : (↑c : Nat) ≠ (↑b : Nat) := by
            intro h
            rw [hc] at h
            omega
          have competitor := bnear c c_lt c_ne
          have left_step := monotone (↑b : Nat) ((↑b : Nat) + 1) (by omega) (by omega)
          have right_step := monotone ((↑b : Nat) + 1) (↑a : Nat) gap alt
          have middle : abs (x - value ((↑b : Nat) + 1)) < abs (x - value (↑b : Nat)) :=
            closer (value (↑b : Nat)) (value ((↑b : Nat) + 1)) (value (↑a : Nat))
              left_step right_step equal_distance.symm
          have competitor' : abs (x - value (↑b : Nat)) ≤
              abs (x - value ((↑b : Nat) + 1)) := by
            simpa [hc] using competitor.1
          exact (not_lt_of_ge competitor') middle
        have consecutive : (↑a : Nat) = (↑b : Nat) + 1 := by omega
        have : False := by omega
        exact this.elim

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.F64
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: RoundingDeterminism]
theorem RoundingDeterminism :
    ∀ (x : Real) (a b : F64), 0 <= x -> DecimalGoalVocabularyDefault.roundsMagnitude x a ->
      DecimalGoalVocabularyDefault.roundsMagnitude x b -> a = b := by
-- BODY
  intro x a b hx ha hb
  dsimp [DecimalGoalVocabularyDefault] at ha hb
  let unit : Nat := 2 ^ 52
  let top : Nat := 2047 * unit
  let threshold : Real := (2 : Real) ^ 1024 - (2 : Real) ^ 970
  let value : Nat → Real := fun n =>
    let e := n / 4503599627370496
    let m := n % 4503599627370496
    if e = 0 then (m : Real) / (2 : Real) ^ 1074
    else if 1075 ≤ e then
      ((4503599627370496 + m : Nat) : Real) * (2 : Real) ^ (e - 1075)
    else ((4503599627370496 + m : Nat) : Real) / (2 : Real) ^ (1075 - e)
  have step : ∀ n : Nat, n + 1 < top → value n < value (n + 1) := by
    intro n hn
    let e := n / unit
    let m := n % unit
    have unit_pos : 0 < unit := by norm_num [unit]
    have rem_bound : m < unit := by
      exact Nat.mod_lt _ unit_pos
    have decomp : unit * e + m = n := by
      exact Nat.div_add_mod n unit
    have top_value : top = 9218868437227405312 := by norm_num [top, unit]
    have n_bound : n + 1 < 9218868437227405312 := by simpa [top_value] using hn
    norm_num [unit] at decomp rem_bound n_bound ⊢
    have decomp_symbol : unit * e + m = n := by exact decomp
    by_cases small : m + 1 < 4503599627370496
    · have next_decomp : 4503599627370496 * e + (m + 1) = n + 1 := by omega
      have next_quotient : (n + 1) / 4503599627370496 = e := by
        omega
      have next_remainder : (n + 1) % 4503599627370496 = m + 1 := by
        omega
      have value_n : value n = if e = 0 then (m : Real) / (2 : Real)^1074 else
          if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real)^(e-1075)
          else ((4503599627370496 + m : Nat) : Real) / (2 : Real)^(1075-e) := by
        dsimp [value]
        rw [← decomp]
        have quotient : (4503599627370496 * e + m) / 4503599627370496 = e := by
          rw [Nat.mul_add_div (by norm_num)]
          simp [Nat.div_eq_of_lt rem_bound]
        have remainder : (4503599627370496 * e + m) % 4503599627370496 = m := by
          rw [Nat.mul_add_mod]
          simp [Nat.mod_eq_of_lt rem_bound]
        simp [quotient, remainder] <;> ring
      have value_next : value (n+1) = if e = 0 then (m+1 : Real) / (2 : Real)^1074 else
          if 1075 ≤ e then ((4503599627370496 + m + 1 : Nat) : Real) * (2 : Real)^(e-1075)
          else ((4503599627370496 + m + 1 : Nat) : Real) / (2 : Real)^(1075-e) := by
        dsimp [value]
        rw [← next_decomp]
        have quotient : (4503599627370496 * e + (m + 1)) / 4503599627370496 = e := by
          rw [Nat.mul_add_div (by norm_num)]
          simp [Nat.div_eq_of_lt (by omega)]
        have remainder : (4503599627370496 * e + (m + 1)) % 4503599627370496 = m + 1 := by
          rw [Nat.mul_add_mod]
          simp [Nat.mod_eq_of_lt (by omega)]
        simp only [quotient, remainder, Nat.cast_add]
        by_cases z : e = 0
        · simp [z] <;> ring
        · by_cases h : 1075 ≤ e
          · simp [z, h] <;> ring
          · simp [z, h] <;> ring
      rw [value_n, value_next]
      by_cases zero : e = 0
      · simp [zero]
        apply (div_lt_div_iff_of_pos_right (by positivity)).2
        norm_num
      · by_cases high : 1075 ≤ e
        · simp [zero, high]
        · simp [zero, high]
          apply (div_lt_div_iff_of_pos_right (by positivity)).2
          norm_num
    · have edge : m + 1 = 4503599627370496 := by omega
      have next_decomp : 4503599627370496 * (e + 1) = n + 1 := by omega
      have next_quotient : (n + 1) / 4503599627370496 = e + 1 := by
        omega
      have next_remainder : (n + 1) % 4503599627370496 = 0 := by
        omega
      have value_n : value n = if e = 0 then (m : Real) / (2 : Real)^1074 else
          if 1075 ≤ e then ((4503599627370496 + m : Nat) : Real) * (2 : Real)^(e-1075)
          else ((4503599627370496 + m : Nat) : Real) / (2 : Real)^(1075-e) := by
        dsimp [value]
        rw [← decomp]
        have quotient : (4503599627370496 * e + m) / 4503599627370496 = e := by
          rw [Nat.mul_add_div (by norm_num)]
          simp [Nat.div_eq_of_lt rem_bound]
        have remainder : (4503599627370496 * e + m) % 4503599627370496 = m := by
          rw [Nat.mul_add_mod]
          simp [Nat.mod_eq_of_lt rem_bound]
        simp [quotient, remainder]
      have value_next : value (n+1) = if e+1 = 0 then (0 : Real) / (2 : Real)^1074 else
          if 1075 ≤ e+1 then ((4503599627370496 : Nat) : Real) * (2 : Real)^((e+1)-1075)
          else ((4503599627370496 : Nat) : Real) / (2 : Real)^ (1075-(e+1)) := by
        dsimp [value]
        rw [← next_decomp]
        have quotient : (4503599627370496 * (e + 1)) / 4503599627370496 = e + 1 := by
          rw [show 4503599627370496 * (e + 1) = (e + 1) * 4503599627370496 by ring]
          exact Nat.mul_div_left _ (by norm_num)
        have remainder : (4503599627370496 * (e + 1)) % 4503599627370496 = 0 := by
          exact Nat.mul_mod_right _ _
        simp [quotient, remainder] <;> ring
      rw [value_n, value_next]
      by_cases zero : e = 0
      · have m_edge : m = 4503599627370495 := by omega
        have next_zero : ¬e + 1 = 0 := by omega
        have next_high : ¬1075 ≤ e + 1 := by omega
        simp only [if_pos zero, if_neg next_zero, if_neg next_high]
        rw [m_edge]
        rw [show 1075 - (e + 1) = 1074 by omega]
        apply (div_lt_div_iff_of_pos_right (by positivity)).2
        norm_num
      · by_cases boundary : e = 1074
        · have low : e < 1075 := by omega
          have not_high : ¬1075 ≤ e := by omega
          have next_high : 1075 ≤ e + 1 := by omega
          have next_zero : ¬e + 1 = 0 := by omega
          simp only [if_neg zero, if_neg not_high, if_pos boundary,
            if_neg next_zero, if_pos next_high]
          rw [show 1075 - e = 1 by omega, show e + 1 - 1075 = 0 by omega]
          have cast_bound : (m : Real) < 4503599627370496 := by exact_mod_cast rem_bound
          norm_num at ⊢
          linarith
        · by_cases high : 1075 ≤ e
          · have high2 : 1074 ≤ e := by omega
            have next_zero : ¬e + 1 = 0 := by omega
            have next_high : 1075 ≤ e + 1 := by omega
            simp only [if_neg zero, if_pos high, if_neg next_zero, if_pos next_high]
            rw [show e + 1 - 1075 = e - 1074 by omega]
            have pos : (0 : Real) < (2 : Real)^(e-1075) := by positivity
            have cast_bound : (m : Real) < 4503599627370496 := by exact_mod_cast rem_bound
            have cast_sum : ((4503599627370496 + m : Nat) : Real) =
                (4503599627370496 : Real) + m := by norm_num
            rw [cast_sum]
            have nextpow : (2 : Real)^(e-1074) = 2 * (2 : Real)^(e-1075) := by
              rw [show e-1074 = (e-1075)+1 by omega, pow_succ]
              ring
            rw [nextpow]
            have base : (4503599627370496 : Real) + m < 2 * 4503599627370496 := by
              linarith
            calc
              (4503599627370496 + (m : Real)) * (2 : Real)^(e-1075) <
                  (2 * 4503599627370496 : Real) * (2 : Real)^(e-1075) :=
                mul_lt_mul_of_pos_right base pos
              _ = 4503599627370496 * (2 * (2 : Real)^(e-1075)) := by ring
          · have low : e < 1075 := by omega
            have low2 : e < 1074 := by omega
            have not_high : ¬1074 ≤ e := by omega
            have next_zero : ¬e + 1 = 0 := by omega
            have next_high : ¬1075 ≤ e + 1 := by omega
            simp only [if_neg zero, if_neg high, if_neg next_zero, if_neg next_high]
            rw [show 1075 - (e + 1) = 1074 - e by omega]
            have cast_bound : (m : Real) < 4503599627370496 := by exact_mod_cast rem_bound
            have cast_sum : ((4503599627370496 + m : Nat) : Real) =
                (4503599627370496 : Real) + m := by norm_num
            rw [cast_sum]
            have denrel : (2 : Real)^(1075-e) = 2 * (2 : Real)^(1074-e) := by
              rw [show 1075-e = (1074-e)+1 by omega, pow_succ]
              ring
            have rhs : (4503599627370496 : Real) / (2 : Real)^(1074-e) =
                (2 * 4503599627370496 : Real) / (2 : Real)^(1075-e) := by
              rw [denrel]
              apply (div_eq_div_iff (by positivity) (by positivity)).2
              ring
            have base : (4503599627370496 : Real) + m < 2 * 4503599627370496 := by
              linarith
            exact ((div_lt_div_iff_of_pos_right (by positivity)).2 base).trans_eq rhs.symm
  have chain : ∀ p k : Nat, 0 < k → p + k < top → value p < value (p + k) := by
    intro p k hk hbound
    induction k with
    | zero => omega
    | succ k ih =>
        by_cases base : k = 0
        · subst k
          simpa using step p (by omega)
        · have prior : value p < value (p + k) := ih (by omega) (by omega)
          have next : value (p + k) < value (p + k + 1) := by
            apply step
            omega
          exact prior.trans next
  have monotone : ∀ p q : Nat, p < q → q < top → value p < value q := by
    intro p q hp hq
    obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le (Nat.le_of_lt hp)
    have hk : 0 < k := by omega
    exact chain p k hk hq
  change 0 ≤ x ∧ (↑a : Nat) ≤ top ∧
    (if (↑a : Nat) = top then threshold ≤ x else
      x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ a →
        abs (x - value (↑a : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
        (abs (x - value (↑a : Nat)) = abs (x - value (↑v : Nat)) →
          (↑a : Nat) % 2 = 0)) at ha
  change 0 ≤ x ∧ (↑b : Nat) ≤ top ∧
    (if (↑b : Nat) = top then threshold ≤ x else
      x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ b →
        abs (x - value (↑b : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
        (abs (x - value (↑b : Nat)) = abs (x - value (↑v : Nat)) →
          (↑b : Nat) % 2 = 0)) at hb
  have word_eq : ∀ u v : F64, (↑u : Nat) = ↑v → u = v := by
    intro u v h
    cases u with
    | mk ub =>
        cases v with
        | mk vb =>
            congr
            apply BitVec.eq_of_toNat_eq
            change ub.toNat = vb.toNat at h
            exact h
  have make_word : ∀ n : Nat, n < top → ∃ c : F64, (↑c : Nat) = n := by
    intro n hn
    have bound : n ≤ UScalar.cMax UScalarTy.U64 := by
      apply le_trans (Nat.le_of_lt hn)
      norm_num [top, unit, UScalar.cMax, UScalar.rMax, U64.rMax, UScalarTy.numBits]
    refine ⟨UScalar.ofNat n bound, ?_⟩
    rfl
  have closer : ∀ A Z B : Real, A < Z → Z < B →
      abs (x - A) = abs (x - B) → abs (x - Z) < abs (x - A) := by
    intro A Z B hAZ hZB hEq
    have between : A < x ∧ x < B := by
      constructor
      · by_contra h
        have hxA : x ≤ A := le_of_not_gt h
        have hxB : x ≤ B := le_trans hxA (le_of_lt (hAZ.trans hZB))
        rw [abs_of_nonpos (sub_nonpos.mpr hxA), abs_of_nonpos (sub_nonpos.mpr hxB)] at hEq
        linarith
      · by_contra h
        have hBx : B ≤ x := le_of_not_gt h
        have hAx : A ≤ x := le_trans (le_of_lt hAZ)
          (le_trans (le_of_lt hZB) hBx)
        rw [abs_of_nonneg (sub_nonneg.mpr hAx), abs_of_nonneg (sub_nonneg.mpr hBx)] at hEq
        linarith
    rcases le_total Z x with hZx | hxZ
    · rw [abs_of_nonneg (sub_nonneg.mpr hZx),
        abs_of_nonneg (sub_nonneg.mpr between.1.le)]
      linarith
    · have hAeqB : abs (x - A) = B - x := by
        rw [abs_of_nonpos (sub_nonpos.mpr between.2.le)] at hEq
        simpa [sub_eq_add_neg] using hEq
      calc
        abs (x - Z) = Z - x := by
          rw [abs_of_nonpos (sub_nonpos.mpr hxZ)]
          ring
        _ < B - x := by linarith
        _ = abs (x - A) := hAeqB.symm
  by_cases high : threshold ≤ x
  · have atop : (↑a : Nat) = top := by
      by_contra h
      have condition := (ha.2).2
      simp [h] at condition
      have hfinite : x < threshold := by
        exact condition.1
      exact (not_lt_of_ge high) hfinite
    have btop : (↑b : Nat) = top := by
      by_contra h
      have condition := (hb.2).2
      simp [h] at condition
      have hfinite : x < threshold := by
        exact condition.1
      exact (not_lt_of_ge high) hfinite
    apply word_eq
    omega
  · have low : x < threshold := lt_of_not_ge high
    have atop : (↑a : Nat) ≠ top := by
      intro h
      have hthreshold : threshold ≤ x := by simpa [h] using (ha.2).2
      exact (not_le_of_gt low) hthreshold
    have btop : (↑b : Nat) ≠ top := by
      intro h
      have hthreshold : threshold ≤ x := by simpa [h] using (hb.2).2
      exact (not_le_of_gt low) hthreshold
    have alt : (↑a : Nat) < top := by omega
    have blt : (↑b : Nat) < top := by omega
    have ha_finite := (ha.2).2
    have hb_finite := (hb.2).2
    simp [atop] at ha_finite
    simp [btop] at hb_finite
    have anear := ha_finite.2
    have bnear := hb_finite.2
    by_cases indices : (↑a : Nat) = ↑b
    · exact word_eq a b indices
    · have words : a ≠ b := by
        intro h
        apply indices
        simpa [h]
      have dab := anear b blt (by intro h; exact indices h.symm)
      have dba := bnear a alt (by intro h; exact indices h)
      have equal_distance : abs (x - value (↑a : Nat)) = abs (x - value (↑b : Nat)) :=
        le_antisymm dab.1 dba.1
      have even_a : (↑a : Nat) % 2 = 0 := dab.2 equal_distance
      have even_b : (↑b : Nat) % 2 = 0 := dba.2 equal_distance.symm
      by_cases order : (↑a : Nat) < ↑b
      · have no_gap : ¬(↑a : Nat) + 1 < ↑b := by
          intro gap
          obtain ⟨c, hc⟩ := make_word ((↑a : Nat) + 1) (by omega)
          have c_lt : (↑c : Nat) < top := by simpa [hc] using show (↑a : Nat) + 1 < top by omega
          have c_ne : (↑c : Nat) ≠ (↑a : Nat) := by
            intro h
            rw [hc] at h
            omega
          have competitor := anear c c_lt c_ne
          have left_step := monotone (↑a : Nat) ((↑a : Nat) + 1) (by omega) (by omega)
          have right_step := monotone ((↑a : Nat) + 1) (↑b : Nat) gap blt
          have middle : abs (x - value ((↑a : Nat) + 1)) < abs (x - value (↑a : Nat)) :=
            closer (value (↑a : Nat)) (value ((↑a : Nat) + 1)) (value (↑b : Nat))
              left_step right_step equal_distance
          have competitor' : abs (x - value (↑a : Nat)) ≤
              abs (x - value ((↑a : Nat) + 1)) := by
            simpa [hc] using competitor.1
          exact (not_lt_of_ge competitor') middle
        have consecutive : (↑b : Nat) = (↑a : Nat) + 1 := by omega
        have : False := by omega
        exact this.elim
      · have order' : (↑b : Nat) < (↑a : Nat) := by omega
        have no_gap : ¬(↑b : Nat) + 1 < ↑a := by
          intro gap
          obtain ⟨c, hc⟩ := make_word ((↑b : Nat) + 1) (by omega)
          have c_lt : (↑c : Nat) < top := by simpa [hc] using show (↑b : Nat) + 1 < top by omega
          have c_ne : (↑c : Nat) ≠ (↑b : Nat) := by
            intro h
            rw [hc] at h
            omega
          have competitor := bnear c c_lt c_ne
          have left_step := monotone (↑b : Nat) ((↑b : Nat) + 1) (by omega) (by omega)
          have right_step := monotone ((↑b : Nat) + 1) (↑a : Nat) gap alt
          have middle : abs (x - value ((↑b : Nat) + 1)) < abs (x - value (↑b : Nat)) :=
            closer (value (↑b : Nat)) (value ((↑b : Nat) + 1)) (value (↑a : Nat))
              left_step right_step equal_distance.symm
          have competitor' : abs (x - value (↑b : Nat)) ≤
              abs (x - value ((↑b : Nat) + 1)) := by
            simpa [hc] using competitor.1
          exact (not_lt_of_ge competitor') middle
        have consecutive : (↑a : Nat) = (↑b : Nat) + 1 := by omega
        have : False := by omega
        exact this.elim

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (201)
More program dependencies (159)
Standard-library dependencies (39)

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 7 entries
Technical checking records
Local closure summary
{
  "node": "RoundingDeterminism",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "62475e43661140b69dbb2e54d11f7b915acc8eaf8b5834e0841bedfb451efb20",
  "active_statement_hash": "5cec914ad68e0e618733acb2a2e053b59512873a609b7e52fe7ece960b4a48f6",
  "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": "sidecar-sc-20260929-095806-2a30ca-RoundingDeterminism-cycle-57",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:5a519e17e3143622899a3b0ed66f4337e008d479840861f8a6c6f871366752aa",
  "current_fingerprint": "sound-v2:5a519e17e3143622899a3b0ed66f4337e008d479840861f8a6c6f871366752aa",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}