RoundingTotality

Lean checked

Statement

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

Theorem

Every nonnegative real number has an IEEE 754 binary64 magnitude rounding target under round-to-nearest, ties-to-even, where the target may be finite, subnormal, zero, or positive infinity.

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: RoundingTotality]
theorem RoundingTotality :
    ∀ x : Real, 0 <= x -> ∃ target : F64, DecimalGoalVocabularyDefault.roundsMagnitude x target := 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}.
Thus I=2^{63}-2^{52}<2^{63}<2^{64}. By the carrier definition in F64, there is consequently an F64 word with value field I, and there is an F64 word with value field n for every natural n<I. Moreover, two such words having the same value field are equal by extensionality of the unsigned-word structure.
Fix a real x and assume 0\leq x. Unfold the roundsMagnitude field of DecimalGoalVocabularyDefault. Its infinity word is exactly I, its overflow threshold is exactly T, and its finite-value function is
\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{1}
where e=n/F and m=n\bmod F. For n<I, Euclidean division gives n=eF+m, 0\leq m<F, and 0\leq e<2047, so (1) covers every finite candidate and only uses natural exponents.
First suppose T\leq x. Choose the word w whose value field is I. The three outer requirements of roundsMagnitude are then discharged as follows: 0\leq x is the hypothesis, w.\mathit{val}\leq I is equality, and the conditional selects its infinity branch because w.\mathit{val}=I, where its required conclusion is exactly T\leq x. This supplies a target in the overflow case.
It remains to treat x<T. We first establish explicitly that \phi is strictly increasing on the natural indices below I. Let n+1<I, and write n=eF+m with m<F.
If m+1<F, then n+1=eF+(m+1) has the same exponent field. Subtraction in the three respective branches of (1) 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}
Every displayed denominator is a positive power of two and every displayed power is positive, hence in each case \phi(n)<\phi(n+1).
If instead m=F-1, then n+1=(e+1)F has fraction field zero. The premise n+1<I gives e\leq2045. All possible boundary cases are the following. For e=0, the last subnormal value and first normal value differ by
{F\over 2^{1074}}-{F-1\over 2^{1074}}={1\over2^{1074}}>0.
For 1\leq e\leq1073, both exponent fields use the reciprocal branch and
{F\over2^{1074-e}}-{2F-1\over2^{1075-e}} ={2F-(2F-1)\over2^{1075-e}} ={1\over2^{1075-e}}>0.
For e=1074, the old word uses the reciprocal branch and the new exponent field 1075 uses the multiplicative branch, giving
F-{2F-1\over2}={1\over2}>0.
Finally, for 1075\leq e\leq2045, both values use the multiplicative branch and
F2^{e+1-1075}-(2F-1)2^{e-1075} =2^{e-1075}>0.
This includes the subnormal/first-normal transition and both sides of the formula change at exponent field 1075. Thus every consecutive step below I is strict. Iterating the consecutive inequality through the finite interval from p to q shows
p<q<I\quad\Longrightarrow\quad\phi(p)<\phi(q). \tag{2}
Now let S=\{n\in\mathbb N\mid n<I\}. It is finite and nonempty: I>0, so 0\in S. For n\in S put d(n)=|x-\phi(n)|. A real-valued function on a nonempty finite set attains its minimum; let \delta be that minimum and let
M=\{n\in S\mid d(n)=\delta\}
be the nonempty set of minimizing indices.
Any two distinct members of M are adjacent. Indeed, take p<q in M. If p+1<q, (2) puts
a:=\phi(p)<z:=\phi(p+1)<b:=\phi(q).
Because p and q minimize, |x-a|=|x-b|=\delta. The equality of the two endpoint distances 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|. Therefore |x-a|=x-a and |x-b|=b-x, so their equality says x-a=b-x. If z\leq x, then
|x-z|=x-z<x-a=\delta
because a<z; if x\leq z, then
|x-z|=z-x<b-x=\delta
because z<b. The total order on the reals gives one of these two cases, and either conclusion contradicts minimality of \delta, since p+1\in S. Hence p+1<q is impossible. As p<q already gives p+1\leq q, necessarily q=p+1.
It follows that M has at most two elements. Otherwise three of its members could be ordered p<q<r; applying the preceding conclusion to p,q and to p,r would give both q=p+1 and r=p+1, contrary to q<r. If M has one element, let r be that element. If M has two elements, write them as p<p+1 and choose for r the one whose residue modulo 2 is zero. Such a choice exists because consecutive natural numbers have residues 0 and 1 modulo 2. In either case
r\in S,\qquad d(r)\leq d(n)\quad(n\in S), \tag{3}
and, whenever M has two elements, r\bmod2=0.
Since r<I<2^{64}, F64 supplies a word w with w.\mathit{val}=r. We verify every clause of the finite branch of roundsMagnitude. The leading clause 0\leq x is the original hypothesis. The target bound is w.\mathit{val}=r<I, hence w.\mathit{val}\leq I. In particular w.\mathit{val}\ne I, so the conditional takes the finite branch, whose first clause is precisely the current assumption x<T.
Finally take any competing word v satisfying v.\mathit{val}<I and v\ne w, and put n=v.\mathit{val}. Then n\in S, so (3) gives
|x-\phi(w.\mathit{val})|=d(r)\leq d(n) =|x-\phi(v.\mathit{val})|,
which is the required absolute-distance inequality. Suppose equality holds. Then n\in M. If M is a singleton, it follows that n=r; equality of the two value fields and unsigned-word extensionality give v=w, contradicting the competitor hypothesis. Thus equality cannot occur in the singleton case. In the two-element case, the construction of r gives w.\mathit{val}\bmod2=r\bmod2=0, exactly the conclusion required by the tie implication. Hence the distance and ties-to-even clauses hold for every competing finite word. The word w is therefore a rounding target when x<T, completing the two exhaustive threshold cases.

Lean proof


  set_option maxHeartbeats 800000 in
    exact (by
      intro x hx
      dsimp [DecimalGoalVocabularyDefault]
      let unit : Nat := 2 ^ 52
      let top : Nat := 9218868437227405312
      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 ⊢
        by_cases small : m + 1 < 4503599627370496
        · have next_decomp : 4503599627370496 * e + (m + 1) = n + 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 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 not_high : ¬1075 ≤ 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 not_high, 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 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 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)
        exact chain p k (by omega) hq
      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 bound : top ≤ UScalar.cMax UScalarTy.U64 := by
          have top_value : top = 9218868437227405312 := by norm_num [top, unit]
          rw [top_value]
          norm_num [UScalar.cMax, UScalar.rMax, U64.rMax, UScalarTy.numBits]
        let w : F64 := UScalar.ofNat top bound
        refine ⟨w, ?_⟩
        change 0 ≤ x ∧ (↑w : Nat) ≤ top ∧
          (if (↑w : Nat) = top then threshold ≤ x else
            x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ w →
              abs (x - value (↑w : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
              (abs (x - value (↑w : Nat)) = abs (x - value (↑v : Nat)) →
                (↑w : Nat) % 2 = 0))
        have hw : (↑w : Nat) = top := by rfl
        refine ⟨hx, ?_, ?_⟩
        · omega
        · rw [if_pos hw]
          exact high
      · have low : x < threshold := lt_of_not_ge high
        let dist : Nat → Real := fun n => abs (x - value n)
        let candidates : Set Nat := {n | n < top}
        have candidates_finite : candidates.Finite := by
          exact Set.toFinite candidates
        have candidates_nonempty : candidates.Nonempty := by
          refine ⟨0, ?_⟩
          change 0 < top
          norm_num [top, unit]
        obtain ⟨r0, hr0, hmin⟩ := Set.exists_min_image candidates dist candidates_finite candidates_nonempty
        have hmin_nat : ∀ n : Nat, n < top → dist r0 ≤ dist n := by
          intro n hn
          exact hmin n hn
        let property : Nat → Prop := fun n => n < top ∧ dist n = dist r0
        have property_exists : ∃ n, property n := ⟨r0, hr0, rfl⟩
        let r := Nat.find property_exists
        have rprop : property r := by exact Nat.find_spec property_exists
        have hr_lt : r < top := rprop.1
        have rbase : dist r = dist r0 := rprop.2
        have least_index : ∀ n : Nat, n < top → dist n = dist r0 → r ≤ n := by
          intro n hn heq
          exact Nat.find_min' property_exists ⟨hn, heq⟩
        set_option maxHeartbeats 800000 in
          have adjacent : ∀ p q : Nat, p < top → q < top →
              dist p = dist r → dist q = dist r → p < q → q = p + 1 := by
            intro p q hp hq pmin qmin hpq
            by_contra gap
            have left_step : value p < value (p + 1) := monotone p (p + 1) (by omega) (by omega)
            have right_step : value (p + 1) < value q := monotone (p + 1) q (by omega) hq
            have middle : dist (p + 1) < dist p := by
              exact closer (value p) (value (p + 1)) (value q) left_step right_step
                (pmin.trans qmin.symm)
            have min_p : dist p ≤ dist (p + 1) := by
              calc dist p = dist r0 := pmin.trans rbase
                   _ ≤ dist (p + 1) := hmin_nat (p + 1) (by omega)
            exact (not_lt_of_ge min_p) middle
        have minimum_property : ∀ n : Nat, n < top → dist r ≤ dist n := by
          intro n hn
          calc dist r = dist r0 := rbase
               _ ≤ dist n := hmin_nat n hn
        have finish : ∀ chosen : Nat, chosen < top →
            (∀ n : Nat, n < top → dist chosen ≤ dist n) →
            ((chosen % 2 = 0) ∨ (∀ n : Nat, n < top → dist chosen = dist n → n = chosen)) →
            ∃ target : F64,
              0 ≤ x ∧ (↑target : Nat) ≤ top ∧
              (if (↑target : Nat) = top then threshold ≤ x else
                x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ target →
                  abs (x - value (↑target : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
                  (abs (x - value (↑target : Nat)) = abs (x - value (↑v : Nat)) →
                    (↑target : Nat) % 2 = 0)) := by
          intro chosen chosen_lt chosen_min choice_rule
          obtain ⟨w, hw⟩ := make_word chosen chosen_lt
          refine ⟨w, ?_⟩
          change 0 ≤ x ∧ (↑w : Nat) ≤ top ∧
            (if (↑w : Nat) = top then threshold ≤ x else
              x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ w →
                abs (x - value (↑w : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
                (abs (x - value (↑w : Nat)) = abs (x - value (↑v : Nat)) →
                  (↑w : Nat) % 2 = 0))
          have wval : (↑w : Nat) = chosen := hw
          have wnot : (↑w : Nat) ≠ top := by omega
          refine ⟨hx, ?_, ?_⟩
          · omega
          · rw [if_neg wnot]
            refine ⟨low, ?_⟩
            intro v hv hne
            have vn : (↑v : Nat) < top := hv
            have bound_dist := chosen_min (↑v : Nat) vn
            constructor
            · simpa [dist, wval] using bound_dist
            · intro equal_dist
              rcases choice_rule with even | unique
              · simpa [wval] using even
              · have index_equal : (↑v : Nat) = chosen := by
                  apply unique (↑v : Nat) vn
                  simpa [dist, wval] using equal_dist
                have value_equal : (↑v : Nat) = (↑w : Nat) := index_equal.trans wval.symm
                have word_equal : v = w := word_eq v w value_equal
                exact (hne word_equal).elim
        by_cases even : r % 2 = 0
        · exact finish r hr_lt minimum_property (Or.inl even)
        · by_cases next : r + 1 < top ∧ dist r = dist (r + 1)
          · have next_min : ∀ n : Nat, n < top → dist (r + 1) ≤ dist n := by
              intro n hn
              rw [← next.2]
              exact minimum_property n hn
            have next_even : (r + 1) % 2 = 0 := by omega
            exact finish (r + 1) next.1 next_min (Or.inl next_even)
          · have unique : ∀ n : Nat, n < top → dist r = dist n → n = r := by
              intro n hn heq
              by_cases nr : n = r
              · exact nr
              · have nbase : dist n = dist r0 := by exact heq.symm.trans rbase
                have rle : r ≤ n := least_index n hn nbase
                have order : r < n := by omega
                have adjacent_right : n = r + 1 := by
                  set_option maxRecDepth 1000000 in
                    exact adjacent r n hr_lt hn (by rfl) heq.symm order
                exfalso
                apply next
                have next_value : dist r = dist (r + 1) := by
                  calc dist r = dist n := heq
                       _ = dist (r + 1) := by rw [adjacent_right]
                exact ⟨by omega, next_value⟩
            exact finish r hr_lt minimum_property (Or.inr unique)
    )

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: RoundingTotality]
theorem RoundingTotality :
    ∀ x : Real, 0 <= x -> ∃ target : F64, DecimalGoalVocabularyDefault.roundsMagnitude x target := by
-- BODY
  set_option maxHeartbeats 800000 in
    exact (by
      intro x hx
      dsimp [DecimalGoalVocabularyDefault]
      let unit : Nat := 2 ^ 52
      let top : Nat := 9218868437227405312
      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 ⊢
        by_cases small : m + 1 < 4503599627370496
        · have next_decomp : 4503599627370496 * e + (m + 1) = n + 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 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 not_high : ¬1075 ≤ 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 not_high, 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 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 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)
        exact chain p k (by omega) hq
      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 bound : top ≤ UScalar.cMax UScalarTy.U64 := by
          have top_value : top = 9218868437227405312 := by norm_num [top, unit]
          rw [top_value]
          norm_num [UScalar.cMax, UScalar.rMax, U64.rMax, UScalarTy.numBits]
        let w : F64 := UScalar.ofNat top bound
        refine ⟨w, ?_⟩
        change 0 ≤ x ∧ (↑w : Nat) ≤ top ∧
          (if (↑w : Nat) = top then threshold ≤ x else
            x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ w →
              abs (x - value (↑w : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
              (abs (x - value (↑w : Nat)) = abs (x - value (↑v : Nat)) →
                (↑w : Nat) % 2 = 0))
        have hw : (↑w : Nat) = top := by rfl
        refine ⟨hx, ?_, ?_⟩
        · omega
        · rw [if_pos hw]
          exact high
      · have low : x < threshold := lt_of_not_ge high
        let dist : Nat → Real := fun n => abs (x - value n)
        let candidates : Set Nat := {n | n < top}
        have candidates_finite : candidates.Finite := by
          exact Set.toFinite candidates
        have candidates_nonempty : candidates.Nonempty := by
          refine ⟨0, ?_⟩
          change 0 < top
          norm_num [top, unit]
        obtain ⟨r0, hr0, hmin⟩ := Set.exists_min_image candidates dist candidates_finite candidates_nonempty
        have hmin_nat : ∀ n : Nat, n < top → dist r0 ≤ dist n := by
          intro n hn
          exact hmin n hn
        let property : Nat → Prop := fun n => n < top ∧ dist n = dist r0
        have property_exists : ∃ n, property n := ⟨r0, hr0, rfl⟩
        let r := Nat.find property_exists
        have rprop : property r := by exact Nat.find_spec property_exists
        have hr_lt : r < top := rprop.1
        have rbase : dist r = dist r0 := rprop.2
        have least_index : ∀ n : Nat, n < top → dist n = dist r0 → r ≤ n := by
          intro n hn heq
          exact Nat.find_min' property_exists ⟨hn, heq⟩
        set_option maxHeartbeats 800000 in
          have adjacent : ∀ p q : Nat, p < top → q < top →
              dist p = dist r → dist q = dist r → p < q → q = p + 1 := by
            intro p q hp hq pmin qmin hpq
            by_contra gap
            have left_step : value p < value (p + 1) := monotone p (p + 1) (by omega) (by omega)
            have right_step : value (p + 1) < value q := monotone (p + 1) q (by omega) hq
            have middle : dist (p + 1) < dist p := by
              exact closer (value p) (value (p + 1)) (value q) left_step right_step
                (pmin.trans qmin.symm)
            have min_p : dist p ≤ dist (p + 1) := by
              calc dist p = dist r0 := pmin.trans rbase
                   _ ≤ dist (p + 1) := hmin_nat (p + 1) (by omega)
            exact (not_lt_of_ge min_p) middle
        have minimum_property : ∀ n : Nat, n < top → dist r ≤ dist n := by
          intro n hn
          calc dist r = dist r0 := rbase
               _ ≤ dist n := hmin_nat n hn
        have finish : ∀ chosen : Nat, chosen < top →
            (∀ n : Nat, n < top → dist chosen ≤ dist n) →
            ((chosen % 2 = 0) ∨ (∀ n : Nat, n < top → dist chosen = dist n → n = chosen)) →
            ∃ target : F64,
              0 ≤ x ∧ (↑target : Nat) ≤ top ∧
              (if (↑target : Nat) = top then threshold ≤ x else
                x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ target →
                  abs (x - value (↑target : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
                  (abs (x - value (↑target : Nat)) = abs (x - value (↑v : Nat)) →
                    (↑target : Nat) % 2 = 0)) := by
          intro chosen chosen_lt chosen_min choice_rule
          obtain ⟨w, hw⟩ := make_word chosen chosen_lt
          refine ⟨w, ?_⟩
          change 0 ≤ x ∧ (↑w : Nat) ≤ top ∧
            (if (↑w : Nat) = top then threshold ≤ x else
              x < threshold ∧ ∀ v : F64, (↑v : Nat) < top → v ≠ w →
                abs (x - value (↑w : Nat)) ≤ abs (x - value (↑v : Nat)) ∧
                (abs (x - value (↑w : Nat)) = abs (x - value (↑v : Nat)) →
                  (↑w : Nat) % 2 = 0))
          have wval : (↑w : Nat) = chosen := hw
          have wnot : (↑w : Nat) ≠ top := by omega
          refine ⟨hx, ?_, ?_⟩
          · omega
          · rw [if_neg wnot]
            refine ⟨low, ?_⟩
            intro v hv hne
            have vn : (↑v : Nat) < top := hv
            have bound_dist := chosen_min (↑v : Nat) vn
            constructor
            · simpa [dist, wval] using bound_dist
            · intro equal_dist
              rcases choice_rule with even | unique
              · simpa [wval] using even
              · have index_equal : (↑v : Nat) = chosen := by
                  apply unique (↑v : Nat) vn
                  simpa [dist, wval] using equal_dist
                have value_equal : (↑v : Nat) = (↑w : Nat) := index_equal.trans wval.symm
                have word_equal : v = w := word_eq v w value_equal
                exact (hne word_equal).elim
        by_cases even : r % 2 = 0
        · exact finish r hr_lt minimum_property (Or.inl even)
        · by_cases next : r + 1 < top ∧ dist r = dist (r + 1)
          · have next_min : ∀ n : Nat, n < top → dist (r + 1) ≤ dist n := by
              intro n hn
              rw [← next.2]
              exact minimum_property n hn
            have next_even : (r + 1) % 2 = 0 := by omega
            exact finish (r + 1) next.1 next_min (Or.inl next_even)
          · have unique : ∀ n : Nat, n < top → dist r = dist n → n = r := by
              intro n hn heq
              by_cases nr : n = r
              · exact nr
              · have nbase : dist n = dist r0 := by exact heq.symm.trans rbase
                have rle : r ≤ n := least_index n hn nbase
                have order : r < n := by omega
                have adjacent_right : n = r + 1 := by
                  set_option maxRecDepth 1000000 in
                    exact adjacent r n hr_lt hn (by rfl) heq.symm order
                exfalso
                apply next
                have next_value : dist r = dist (r + 1) := by
                  calc dist r = dist n := heq
                       _ = dist (r + 1) := by rw [adjacent_right]
                exact ⟨by omega, next_value⟩
            exact finish r hr_lt minimum_property (Or.inr unique)
    )

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": "RoundingTotality",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "c0b8a9b9c9d3b25686c2965069962cd76919be2f1ff2e3ce6c90f493d4ed59ce",
  "active_statement_hash": "e4cdd447c193134c272074d2ee12cc99a2a5627ffe149c2c6de90e8a0d2c15ac",
  "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-112508-efd8a3-RoundingTotality-cycle-60",
  "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:ad34df17ccc907c528630cebb39efcaa66cfb09b95e85e87b1a0abddabdce54a",
  "current_fingerprint": "sound-v2:ad34df17ccc907c528630cebb39efcaa66cfb09b95e85e87b1a0abddabdce54a",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}