Binary64NormalEncoderRounds

Lean checked

Statement

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

Lemma

Let x be a nonnegative rational in the normal, non-overflow range 2^{-1022}\leq x<2^{128}. Run the normal branch of Binary64ModelDefault's magnitude encoder: scan the binary64 normal exponent fields, scale x at the selected field, round the scaled rational to the nearest integer with ties to even, and form the corresponding raw magnitude word, carrying into the next exponent field when necessary. The resulting word satisfies the roundsMagnitude relation of DecimalGoalVocabularyDefault for the real image of x.

Lean statement

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

import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.Binary64EncoderCarryArithmetic
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64ModelDefault
import Tablet.DecimalGoalVocabularyDefault

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: Binary64NormalEncoderRounds]
theorem Binary64NormalEncoderRounds :
    ∀ x : Rat, (2 : Rat) ^ (-1022 : Int) ≤ x → x < (2 : Rat) ^ (128 : Nat) →
      let roundEvenNat := fun z : Rat =>
        let q := z.num.natAbs / z.den
        let r := z.num.natAbs % z.den
        if z.den < 2 * r ∨ (z.den = 2 * r ∧ q % 2 = 1) then q + 1 else q
      let normalExponent := fun z : Rat =>
        (List.range 2046).foldl (fun (old : Nat) (i : Nat) =>
          let e : Nat := i + 1
          if ((2 : Rat) ^ ((e : Int) - 1023)) ≤ z then e else old) 1
      let e := normalExponent x
      let significand := roundEvenNat (x * ((2 : Rat) ^ ((1075 : Int) - (e : Int))))
      let bits := if significand = 2 ^ 53 then (e + 1) * 2 ^ 52
        else e * 2 ^ 52 + (significand - 2 ^ 52)
      DecimalGoalVocabularyDefault.roundsMagnitude (x : Real)
        (⟨BitVec.ofNat 64 bits⟩ : F64) := 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

Write U=2^{52} and let e be the final exponent field selected by the scan. The initial field is 1, whose threshold is 2^{-1022}, so the lower hypothesis establishes the scan invariant 2^{e-1023}\leq x. At each scan step the field either advances to the tested field, preserving that invariant, or remains unchanged. The bound x<2^{128}<2^{1023} prevents the last tested field from being selected. Monotonicity of the scan index then shows that the next field was rejected, hence x<2^{e+1-1023}.
After scaling, z=x2^{1075-e} therefore satisfies U\leq z<2U. Put q=\lfloor z\rfloor and let r be the numerator remainder over the positive reduced denominator. Thus U\leq q<2U and z=q+r/\mathit{den}(z) with 0\leq r<\mathit{den}(z). The encoder selects q+1 exactly when 2r exceeds the denominator, or when equality holds and q is odd; otherwise it selects q. The two corresponding raw words are adjacent, including the case where the upper one carries to field e+1, by Binary64EncoderCarryArithmetic. Their decoded values are respectively q2^{e-1075} and (q+1)2^{e-1075} by Binary64AdjacentNormalValues, so the selected one is no farther from x than the other. At equality the selected integer significand is even, and because U and the exponent-field contribution are even, the selected raw word is even.
The selected raw word is below the infinity word 2047U. By Binary64FiniteMagnitudeStrictMono, every smaller finite competitor has decoded value at most the lower adjacent value, and every larger one has decoded value at least the upper adjacent value. Elementary absolute-value inequalities therefore make each such competitor no closer than the selected word. The real embedding of rationals preserves all powers, products, orders, and the remainder decomposition used above. Finally x<2^{128}<2^{1024}-2^{970}, so the finite rather than infinity clause of roundsMagnitude applies. These facts give its nonnegativity, finite-word bound, overflow-threshold, every-competitor minimization, and even-tie clauses.

Lean proof


  intro x hxlow hxhigh
  let step : Nat → Nat → Nat := fun old i =>
    if ((2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023)) ≤ x then i + 1 else old
  let scan : Nat → Nat := fun n => List.foldl step 1 (List.range n)
  have scan_formula : ∀ n : Nat, scan (n + 1) = step (scan n) n := by
    intro n
    change List.foldl step 1 (List.range (n + 1)) =
      step (List.foldl step 1 (List.range n)) n
    rw [List.range_succ, List.foldl_append]
    rfl
  have scan_lower : ∀ n : Nat, 1 ≤ scan n := by
    intro n
    induction n with
    | zero => simp [scan]
    | succ n ih =>
        rw [scan_formula]
        dsimp [step]
        split <;> omega
  have scan_upper : ∀ n : Nat, scan n ≤ n + 1 := by
    intro n
    induction n with
    | zero => simp [scan]
    | succ n ih =>
        rw [scan_formula]
        dsimp [step]
        split <;> omega
  have scan_mono : ∀ n : Nat, scan n ≤ scan (n + 1) := by
    intro n
    rw [scan_formula]
    dsimp [step]
    split
    · exact scan_upper n
    · exact le_rfl
  have scan_monotone : Monotone scan := monotone_nat_of_le_succ scan_mono
  have scan_threshold : ∀ n : Nat,
      (2 : Rat) ^ ((scan n : Int) - 1023) ≤ x := by
    intro n
    induction n with
    | zero =>
        simp [scan]
        simpa using hxlow
    | succ n ih =>
        rw [scan_formula]
        dsimp [step]
        split
        · assumption
        · exact ih
  have last_threshold : ¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x) := by
    intro h
    have hlarge : (2 : Rat) ^ (128 : Nat) < (2 : Rat) ^ (1023 : Nat) := by
      exact pow_lt_pow_right₀ (by norm_num) (by omega)
    have : (2 : Rat) ^ (1023 : Nat) ≤ x := by simpa using h
    exact (not_lt_of_ge this) (hxhigh.trans hlarge)
  have scan_bound : ∀ n : Nat, 1 ≤ n → scan n ≤ n := by
    intro n hn
    induction n with
    | zero => omega
    | succ n ih =>
        rw [scan_formula]
        by_cases zero_case : n = 0
        · subst n
          simp [scan, step]
        · dsimp [step]
          split <;> omega
  have exponent_upper : scan 2046 ≤ 2045 := by
    rw [show 2046 = 2045 + 1 by norm_num, scan_formula]
    change (if (2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x
      then 2046 else scan 2045) ≤ 2045
    rw [if_neg last_threshold]
    exact scan_bound 2045 (by omega)
  have no_next_threshold :
      ¬ ((2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤ x) := by
    intro threshold
    have advanced : scan (scan 2046 + 1) = scan 2046 + 1 := by
      rw [scan_formula]
      change (if (2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤ x
        then scan 2046 + 1 else scan (scan 2046)) = scan 2046 + 1
      rw [if_pos threshold]
    have future : scan (scan 2046 + 1) ≤ scan 2046 := by
      apply scan_monotone
      omega
    omega
  let e := scan 2046
  let z : Rat := x * (2 : Rat) ^ ((1075 : Int) - (e : Int))
  have e_lower : 1 ≤ e := scan_lower 2046
  have e_upper : e ≤ 2045 := exponent_upper
  have current_threshold : (2 : Rat) ^ ((e : Int) - 1023) ≤ x :=
    scan_threshold 2046
  have next_threshold : x < (2 : Rat) ^ (((e + 1 : Nat) : Int) - 1023) :=
    lt_of_not_ge no_next_threshold
  have z_lower : (2 ^ 52 : Rat) ≤ z := by
    dsimp [z]
    calc
      (2 ^ 52 : Rat) = (2 : Rat) ^ (52 : Int) := by norm_num
      _ = (2 : Rat) ^ ((e : Int) - 1023) *
          (2 : Rat) ^ ((1075 : Int) - (e : Int)) := by
            rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)]
            congr 1
            omega
      _ ≤ x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) :=
        mul_le_mul_of_nonneg_right current_threshold (zpow_pos (by norm_num) _).le
  have z_upper : z < (2 ^ 53 : Rat) := by
    dsimp [z]
    calc
      x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) <
          (2 : Rat) ^ (((e + 1 : Nat) : Int) - 1023) *
            (2 : Rat) ^ ((1075 : Int) - (e : Int)) :=
        mul_lt_mul_of_pos_right next_threshold (zpow_pos (by norm_num) _)
      _ = (2 : Rat) ^ (53 : Int) := by
        rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)]
        congr 1
        push_cast
        omega
      _ = (2 ^ 53 : Rat) := by norm_num
  have z_nonnegative : 0 ≤ z := le_trans (by positivity) z_lower
  let q : Nat := z.num.natAbs / z.den
  let r : Nat := z.num.natAbs % z.den
  have numerator_nonnegative : 0 ≤ z.num := Rat.num_nonneg.mpr z_nonnegative
  have numerator_natAbs : (z.num.natAbs : Int) = z.num :=
    Int.natAbs_of_nonneg numerator_nonnegative
  have den_pos : 0 < z.den := z.den_pos
  have qr_decomp : q * z.den + r = z.num.natAbs := by
    dsimp [q, r]
    simpa [Nat.mul_comm] using Nat.div_add_mod z.num.natAbs z.den
  have r_lt : r < z.den := by
    dsimp [r]
    exact Nat.mod_lt _ den_pos
  have q_lower : 2 ^ 52 ≤ q := by
    have cross := (Rat.le_iff (2 ^ 52 : Rat) z).mp z_lower
    have cross_nat : 2 ^ 52 * z.den ≤ z.num.natAbs := by
      have : (2 ^ 52 : Int) * z.den ≤ z.num := by simpa using cross
      rw [← numerator_natAbs] at this
      exact_mod_cast this
    dsimp [q]
    exact (Nat.le_div_iff_mul_le den_pos).2 (by simpa [Nat.mul_comm] using cross_nat)
  have q_upper : q < 2 ^ 53 := by
    have cross := (Rat.lt_iff z (2 ^ 53 : Rat)).mp z_upper
    have cross_nat : z.num.natAbs < 2 ^ 53 * z.den := by
      have : z.num < (2 ^ 53 : Int) * z.den := by simpa using cross
      rw [← numerator_natAbs] at this
      exact_mod_cast this
    dsimp [q]
    exact (Nat.div_lt_iff_lt_mul den_pos).2 cross_nat
  let chooseHigh : Prop := z.den < 2 * r ∨ (z.den = 2 * r ∧ q % 2 = 1)
  let rounded : Nat := if chooseHigh then q + 1 else q
  let lowBits : Nat := e * 2 ^ 52 + (q - 2 ^ 52)
  let bits : Nat := if rounded = 2 ^ 53 then (e + 1) * 2 ^ 52
    else e * 2 ^ 52 + (rounded - 2 ^ 52)
  have rounded_bounds : 2 ^ 52 ≤ rounded ∧ rounded ≤ 2 ^ 53 := by
    change 2 ^ 52 ≤ (if chooseHigh then q + 1 else q) ∧
      (if chooseHigh then q + 1 else q) ≤ 2 ^ 53
    split <;> omega
  have low_bits_formula : lowBits = e * 2 ^ 52 + q - 2 ^ 52 := by
    change e * 2 ^ 52 + (q - 2 ^ 52) = e * 2 ^ 52 + q - 2 ^ 52
    omega
  have carry_arithmetic :=
    Binary64EncoderCarryArithmetic e q chooseHigh e_lower e_upper q_lower q_upper
  have bits_choice : bits = if chooseHigh then lowBits + 1 else lowBits :=
    carry_arithmetic.1
  have low_bits_bound : lowBits < 2047 * 2 ^ 52 := by
    change e * 2 ^ 52 + (q - 2 ^ 52) < 2047 * 2 ^ 52
    omega
  have bits_bound : bits < 2047 * 2 ^ 52 := carry_arithmetic.2.1
  have bits_word : (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) = bits := by
    change (BitVec.ofNat 64 bits).toNat = bits
    rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt]
    omega
  let scale : Real := (2 : Real) ^ ((e : Int) - 1075)
  have scale_pos : 0 < scale := by
    dsimp [scale]
    exact zpow_pos (by norm_num) _
  have z_real_decomp : (z : Real) = (q : Real) + (r : Real) / (z.den : Real) := by
    rw [Rat.cast_def, ← numerator_natAbs, ← qr_decomp]
    push_cast
    rw [add_div, mul_div_cancel_right₀ _ (by positivity : (z.den : Real) ≠ 0)]
  have x_scale : (x : Real) = (z : Real) * scale := by
    dsimp [z, scale]
    push_cast
    rw [mul_assoc, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
    have exponent_zero : ((1075 : Int) - (e : Int)) + ((e : Int) - 1075) = 0 := by
      ring
    rw [exponent_zero]
    norm_num
  let value : Nat → Real := fun n =>
    let field := n / (2 ^ 52 : Nat)
    let fraction := n % (2 ^ 52 : Nat)
    if field = 0 then (fraction : Real) / (2 : Real) ^ 1074
    else if 1075 ≤ field then
      (((2 ^ 52 : Nat) + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075)
    else (((2 ^ 52 : Nat) + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field)
  have adjacent_values :
      value lowBits = (q : Real) * scale ∧
      value (lowBits + 1) = ((q + 1 : Nat) : Real) * scale := by
    simpa only [value, lowBits, scale] using
      Binary64AdjacentNormalValues e q e_lower e_upper q_lower q_upper
  let frac : Real := (r : Real) / (z.den : Real)
  have den_pos_real : (0 : Real) < (z.den : Real) := by exact_mod_cast den_pos
  have frac_nonnegative : 0 ≤ frac := by
    dsimp [frac]
    exact div_nonneg (Nat.cast_nonneg r) den_pos_real.le
  have frac_lt_one : frac < 1 := by
    dsimp [frac]
    apply (div_lt_one den_pos_real).2
    exact_mod_cast r_lt
  have x_formula : (x : Real) = ((q : Real) + frac) * scale := by
    rw [x_scale, z_real_decomp]
  have low_le_x : value lowBits ≤ (x : Real) := by
    rw [adjacent_values.1, x_formula]
    exact mul_le_mul_of_nonneg_right (by linarith) scale_pos.le
  have x_le_high : (x : Real) ≤ value (lowBits + 1) := by
    rw [adjacent_values.2, x_formula]
    push_cast
    exact mul_le_mul_of_nonneg_right (by linarith) scale_pos.le
  have low_distance :
      abs ((x : Real) - value lowBits) = frac * scale := by
    rw [abs_of_nonneg (sub_nonneg.mpr low_le_x), x_formula, adjacent_values.1]
    ring
  have high_distance :
      abs ((x : Real) - value (lowBits + 1)) = (1 - frac) * scale := by
    rw [abs_of_nonpos (sub_nonpos.mpr x_le_high), x_formula, adjacent_values.2]
    push_cast
    ring
  have frac_ge_half : z.den ≤ 2 * r → (1 : Real) / 2 ≤ frac := by
    intro h
    dsimp [frac]
    apply (div_le_div_iff₀ (by norm_num : (0 : Real) < 2) den_pos_real).2
    have hc : (z.den : Real) ≤ 2 * (r : Real) := by exact_mod_cast h
    simpa [mul_comm] using hc
  have frac_le_half : 2 * r ≤ z.den → frac ≤ (1 : Real) / 2 := by
    intro h
    dsimp [frac]
    apply (div_le_div_iff₀ den_pos_real (by norm_num : (0 : Real) < 2)).2
    have hc : 2 * (r : Real) ≤ (z.den : Real) := by exact_mod_cast h
    simpa [mul_comm] using hc
  have high_near (h : chooseHigh) :
      abs ((x : Real) - value (lowBits + 1)) ≤
        abs ((x : Real) - value lowBits) := by
    rw [high_distance, low_distance]
    apply mul_le_mul_of_nonneg_right _ scale_pos.le
    apply sub_le_iff_le_add.mpr
    have hden : z.den ≤ 2 * r := by
      dsimp [chooseHigh] at h
      omega
    have := frac_ge_half hden
    linarith
  have low_near (h : ¬ chooseHigh) :
      abs ((x : Real) - value lowBits) ≤
        abs ((x : Real) - value (lowBits + 1)) := by
    rw [low_distance, high_distance]
    apply mul_le_mul_of_nonneg_right _ scale_pos.le
    have hden : 2 * r ≤ z.den := by
      dsimp [chooseHigh] at h
      omega
    have := frac_le_half hden
    linarith
  have tie_den (htie :
      abs ((x : Real) - value lowBits) =
        abs ((x : Real) - value (lowBits + 1))) : z.den = 2 * r := by
    rw [low_distance, high_distance] at htie
    have hfrac : frac = 1 - frac :=
      mul_right_cancel₀ scale_pos.ne' htie
    have hhalf : frac = (1 : Real) / 2 := by linarith
    have hdiv : (r : Real) / (z.den : Real) = (1 : Real) / 2 := by
      simpa only [frac] using hhalf
    have hcross := (div_eq_div_iff (ne_of_gt den_pos_real) (by norm_num : (2 : Real) ≠ 0)).mp hdiv
    have hnat : r * 2 = 1 * z.den := by exact_mod_cast hcross
    omega
  have value_mono : ∀ p q : Nat, p < q → q < 9218868437227405312 →
      value p < value q := by
    simpa only [value] using Binary64FiniteMagnitudeStrictMono
  have hround : DecimalGoalVocabularyDefault.roundsMagnitude (x : Real)
      (⟨BitVec.ofNat 64 bits⟩ : F64) := by
    dsimp [DecimalGoalVocabularyDefault]
    change 0 ≤ (x : Real) ∧
      (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) ≤ 9218868437227405312 ∧
      (if (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) = 9218868437227405312 then
        (2 : Real) ^ 1024 - (2 : Real) ^ 970 ≤ (x : Real)
       else (x : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 ∧
        ∀ v : F64, (↑v : Nat) < 9218868437227405312 →
          v ≠ (⟨BitVec.ofNat 64 bits⟩ : F64) →
          abs ((x : Real) - value (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat)) ≤
              abs ((x : Real) - value (↑v : Nat)) ∧
          (abs ((x : Real) - value (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat)) =
              abs ((x : Real) - value (↑v : Nat)) →
            (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) % 2 = 0))
    have x_nonnegative : (0 : Real) ≤ (x : Real) := by
      have hp : (0 : Rat) ≤ 2 ^ (-1022 : Int) :=
        (zpow_pos (by norm_num : (0 : Rat) < 2) _).le
      exact_mod_cast le_trans hp hxlow
    have x_high_real : (x : Real) < (2 : Real) ^ (128 : Nat) := by
      exact_mod_cast hxhigh
    have below_threshold : (x : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 := by
      apply x_high_real.trans
      set_option exponentiation.threshold 2048 in
        norm_num
    rw [bits_word]
    refine ⟨x_nonnegative, by omega, ?_⟩
    rw [if_neg (by omega)]
    refine ⟨below_threshold, ?_⟩
    intro v hv hne
    have raw_ne : (↑v : Nat) ≠ bits := by
      intro heq
      apply hne
      apply UScalar.eq_of_val_eq
      simpa [bits_word] using heq
    by_cases hhigh : chooseHigh
    · have bits_eq : bits = lowBits + 1 := by
        rw [bits_choice, if_pos hhigh]
      rw [bits_eq]
      have raw_ne_high : (↑v : Nat) ≠ lowBits + 1 := by
        simpa only [bits_eq] using raw_ne
      rcases lt_or_gt_of_ne raw_ne_high with raw_lt | raw_gt
      · have raw_le : (↑v : Nat) ≤ lowBits := by omega
        have vvalue_le_low : value (↑v : Nat) ≤ value lowBits := by
          rcases raw_le.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (value_mono _ _ hlt low_bits_bound).le
        have vvalue_le_x : value (↑v : Nat) ≤ (x : Real) :=
          vvalue_le_low.trans low_le_x
        have low_to_v :
            abs ((x : Real) - value lowBits) ≤
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonneg (sub_nonneg.mpr low_le_x),
            abs_of_nonneg (sub_nonneg.mpr vvalue_le_x)]
          linarith
        constructor
        · exact (high_near hhigh).trans low_to_v
        · intro heqdist
          have low_to_high :
              abs ((x : Real) - value lowBits) ≤
                abs ((x : Real) - value (lowBits + 1)) := by
            calc
              abs ((x : Real) - value lowBits) ≤
                  abs ((x : Real) - value (↑v : Nat)) := low_to_v
              _ = abs ((x : Real) - value (lowBits + 1)) := heqdist.symm
          have htie :
              abs ((x : Real) - value lowBits) =
                abs ((x : Real) - value (lowBits + 1)) :=
            le_antisymm low_to_high (high_near hhigh)
          have hden := tie_den htie
          have qodd : q % 2 = 1 := by
            dsimp [chooseHigh] at hhigh
            omega
          have qsucc_even : (q + 1) % 2 = 0 := by omega
          exact carry_arithmetic.2.2.2.trans qsucc_even
      · have high_value_lt_v : value (lowBits + 1) < value (↑v : Nat) :=
          value_mono _ _ raw_gt hv
        have x_le_v : (x : Real) ≤ value (↑v : Nat) :=
          x_le_high.trans high_value_lt_v.le
        have strict_distance :
            abs ((x : Real) - value (lowBits + 1)) <
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonpos (sub_nonpos.mpr x_le_high),
            abs_of_nonpos (sub_nonpos.mpr x_le_v)]
          linarith
        constructor
        · exact strict_distance.le
        · intro heqdist
          exact (ne_of_lt strict_distance heqdist).elim
    · have bits_eq : bits = lowBits := by
        rw [bits_choice, if_neg hhigh]
      rw [bits_eq]
      have raw_ne_low : (↑v : Nat) ≠ lowBits := by
        simpa only [bits_eq] using raw_ne
      rcases lt_or_gt_of_ne raw_ne_low with raw_lt | raw_gt
      · have vvalue_lt_low : value (↑v : Nat) < value lowBits :=
          value_mono _ _ raw_lt low_bits_bound
        have vvalue_le_x : value (↑v : Nat) ≤ (x : Real) :=
          vvalue_lt_low.le.trans low_le_x
        have strict_distance :
            abs ((x : Real) - value lowBits) <
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonneg (sub_nonneg.mpr low_le_x),
            abs_of_nonneg (sub_nonneg.mpr vvalue_le_x)]
          linarith
        constructor
        · exact strict_distance.le
        · intro heqdist
          exact (ne_of_lt strict_distance heqdist).elim
      · have high_le_raw : lowBits + 1 ≤ (↑v : Nat) := by omega
        have high_value_le_v : value (lowBits + 1) ≤ value (↑v : Nat) := by
          rcases high_le_raw.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (value_mono _ _ hlt hv).le
        have x_le_v : (x : Real) ≤ value (↑v : Nat) :=
          x_le_high.trans high_value_le_v
        have high_to_v :
            abs ((x : Real) - value (lowBits + 1)) ≤
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonpos (sub_nonpos.mpr x_le_high),
            abs_of_nonpos (sub_nonpos.mpr x_le_v)]
          linarith
        constructor
        · exact (low_near hhigh).trans high_to_v
        · intro heqdist
          have high_to_low :
              abs ((x : Real) - value (lowBits + 1)) ≤
                abs ((x : Real) - value lowBits) := by
            calc
              abs ((x : Real) - value (lowBits + 1)) ≤
                  abs ((x : Real) - value (↑v : Nat)) := high_to_v
              _ = abs ((x : Real) - value lowBits) := heqdist.symm
          have htie :
              abs ((x : Real) - value lowBits) =
                abs ((x : Real) - value (lowBits + 1)) :=
            le_antisymm (low_near hhigh) high_to_low
          have hden := tie_den htie
          have qeven : q % 2 = 0 := by
            have qmod : q % 2 < 2 := Nat.mod_lt _ (by omega)
            dsimp [chooseHigh] at hhigh
            omega
          exact carry_arithmetic.2.2.1.trans qeven
  simpa only [step, scan, e, z, q, r, chooseHigh, rounded, bits] using hround

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.Binary64EncoderCarryArithmetic
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64ModelDefault
import Tablet.DecimalGoalVocabularyDefault

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: Binary64NormalEncoderRounds]
theorem Binary64NormalEncoderRounds :
    ∀ x : Rat, (2 : Rat) ^ (-1022 : Int) ≤ x → x < (2 : Rat) ^ (128 : Nat) →
      let roundEvenNat := fun z : Rat =>
        let q := z.num.natAbs / z.den
        let r := z.num.natAbs % z.den
        if z.den < 2 * r ∨ (z.den = 2 * r ∧ q % 2 = 1) then q + 1 else q
      let normalExponent := fun z : Rat =>
        (List.range 2046).foldl (fun (old : Nat) (i : Nat) =>
          let e : Nat := i + 1
          if ((2 : Rat) ^ ((e : Int) - 1023)) ≤ z then e else old) 1
      let e := normalExponent x
      let significand := roundEvenNat (x * ((2 : Rat) ^ ((1075 : Int) - (e : Int))))
      let bits := if significand = 2 ^ 53 then (e + 1) * 2 ^ 52
        else e * 2 ^ 52 + (significand - 2 ^ 52)
      DecimalGoalVocabularyDefault.roundsMagnitude (x : Real)
        (⟨BitVec.ofNat 64 bits⟩ : F64) := by
-- BODY
  intro x hxlow hxhigh
  let step : Nat → Nat → Nat := fun old i =>
    if ((2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023)) ≤ x then i + 1 else old
  let scan : Nat → Nat := fun n => List.foldl step 1 (List.range n)
  have scan_formula : ∀ n : Nat, scan (n + 1) = step (scan n) n := by
    intro n
    change List.foldl step 1 (List.range (n + 1)) =
      step (List.foldl step 1 (List.range n)) n
    rw [List.range_succ, List.foldl_append]
    rfl
  have scan_lower : ∀ n : Nat, 1 ≤ scan n := by
    intro n
    induction n with
    | zero => simp [scan]
    | succ n ih =>
        rw [scan_formula]
        dsimp [step]
        split <;> omega
  have scan_upper : ∀ n : Nat, scan n ≤ n + 1 := by
    intro n
    induction n with
    | zero => simp [scan]
    | succ n ih =>
        rw [scan_formula]
        dsimp [step]
        split <;> omega
  have scan_mono : ∀ n : Nat, scan n ≤ scan (n + 1) := by
    intro n
    rw [scan_formula]
    dsimp [step]
    split
    · exact scan_upper n
    · exact le_rfl
  have scan_monotone : Monotone scan := monotone_nat_of_le_succ scan_mono
  have scan_threshold : ∀ n : Nat,
      (2 : Rat) ^ ((scan n : Int) - 1023) ≤ x := by
    intro n
    induction n with
    | zero =>
        simp [scan]
        simpa using hxlow
    | succ n ih =>
        rw [scan_formula]
        dsimp [step]
        split
        · assumption
        · exact ih
  have last_threshold : ¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x) := by
    intro h
    have hlarge : (2 : Rat) ^ (128 : Nat) < (2 : Rat) ^ (1023 : Nat) := by
      exact pow_lt_pow_right₀ (by norm_num) (by omega)
    have : (2 : Rat) ^ (1023 : Nat) ≤ x := by simpa using h
    exact (not_lt_of_ge this) (hxhigh.trans hlarge)
  have scan_bound : ∀ n : Nat, 1 ≤ n → scan n ≤ n := by
    intro n hn
    induction n with
    | zero => omega
    | succ n ih =>
        rw [scan_formula]
        by_cases zero_case : n = 0
        · subst n
          simp [scan, step]
        · dsimp [step]
          split <;> omega
  have exponent_upper : scan 2046 ≤ 2045 := by
    rw [show 2046 = 2045 + 1 by norm_num, scan_formula]
    change (if (2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x
      then 2046 else scan 2045) ≤ 2045
    rw [if_neg last_threshold]
    exact scan_bound 2045 (by omega)
  have no_next_threshold :
      ¬ ((2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤ x) := by
    intro threshold
    have advanced : scan (scan 2046 + 1) = scan 2046 + 1 := by
      rw [scan_formula]
      change (if (2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤ x
        then scan 2046 + 1 else scan (scan 2046)) = scan 2046 + 1
      rw [if_pos threshold]
    have future : scan (scan 2046 + 1) ≤ scan 2046 := by
      apply scan_monotone
      omega
    omega
  let e := scan 2046
  let z : Rat := x * (2 : Rat) ^ ((1075 : Int) - (e : Int))
  have e_lower : 1 ≤ e := scan_lower 2046
  have e_upper : e ≤ 2045 := exponent_upper
  have current_threshold : (2 : Rat) ^ ((e : Int) - 1023) ≤ x :=
    scan_threshold 2046
  have next_threshold : x < (2 : Rat) ^ (((e + 1 : Nat) : Int) - 1023) :=
    lt_of_not_ge no_next_threshold
  have z_lower : (2 ^ 52 : Rat) ≤ z := by
    dsimp [z]
    calc
      (2 ^ 52 : Rat) = (2 : Rat) ^ (52 : Int) := by norm_num
      _ = (2 : Rat) ^ ((e : Int) - 1023) *
          (2 : Rat) ^ ((1075 : Int) - (e : Int)) := by
            rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)]
            congr 1
            omega
      _ ≤ x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) :=
        mul_le_mul_of_nonneg_right current_threshold (zpow_pos (by norm_num) _).le
  have z_upper : z < (2 ^ 53 : Rat) := by
    dsimp [z]
    calc
      x * (2 : Rat) ^ ((1075 : Int) - (e : Int)) <
          (2 : Rat) ^ (((e + 1 : Nat) : Int) - 1023) *
            (2 : Rat) ^ ((1075 : Int) - (e : Int)) :=
        mul_lt_mul_of_pos_right next_threshold (zpow_pos (by norm_num) _)
      _ = (2 : Rat) ^ (53 : Int) := by
        rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)]
        congr 1
        push_cast
        omega
      _ = (2 ^ 53 : Rat) := by norm_num
  have z_nonnegative : 0 ≤ z := le_trans (by positivity) z_lower
  let q : Nat := z.num.natAbs / z.den
  let r : Nat := z.num.natAbs % z.den
  have numerator_nonnegative : 0 ≤ z.num := Rat.num_nonneg.mpr z_nonnegative
  have numerator_natAbs : (z.num.natAbs : Int) = z.num :=
    Int.natAbs_of_nonneg numerator_nonnegative
  have den_pos : 0 < z.den := z.den_pos
  have qr_decomp : q * z.den + r = z.num.natAbs := by
    dsimp [q, r]
    simpa [Nat.mul_comm] using Nat.div_add_mod z.num.natAbs z.den
  have r_lt : r < z.den := by
    dsimp [r]
    exact Nat.mod_lt _ den_pos
  have q_lower : 2 ^ 52 ≤ q := by
    have cross := (Rat.le_iff (2 ^ 52 : Rat) z).mp z_lower
    have cross_nat : 2 ^ 52 * z.den ≤ z.num.natAbs := by
      have : (2 ^ 52 : Int) * z.den ≤ z.num := by simpa using cross
      rw [← numerator_natAbs] at this
      exact_mod_cast this
    dsimp [q]
    exact (Nat.le_div_iff_mul_le den_pos).2 (by simpa [Nat.mul_comm] using cross_nat)
  have q_upper : q < 2 ^ 53 := by
    have cross := (Rat.lt_iff z (2 ^ 53 : Rat)).mp z_upper
    have cross_nat : z.num.natAbs < 2 ^ 53 * z.den := by
      have : z.num < (2 ^ 53 : Int) * z.den := by simpa using cross
      rw [← numerator_natAbs] at this
      exact_mod_cast this
    dsimp [q]
    exact (Nat.div_lt_iff_lt_mul den_pos).2 cross_nat
  let chooseHigh : Prop := z.den < 2 * r ∨ (z.den = 2 * r ∧ q % 2 = 1)
  let rounded : Nat := if chooseHigh then q + 1 else q
  let lowBits : Nat := e * 2 ^ 52 + (q - 2 ^ 52)
  let bits : Nat := if rounded = 2 ^ 53 then (e + 1) * 2 ^ 52
    else e * 2 ^ 52 + (rounded - 2 ^ 52)
  have rounded_bounds : 2 ^ 52 ≤ rounded ∧ rounded ≤ 2 ^ 53 := by
    change 2 ^ 52 ≤ (if chooseHigh then q + 1 else q) ∧
      (if chooseHigh then q + 1 else q) ≤ 2 ^ 53
    split <;> omega
  have low_bits_formula : lowBits = e * 2 ^ 52 + q - 2 ^ 52 := by
    change e * 2 ^ 52 + (q - 2 ^ 52) = e * 2 ^ 52 + q - 2 ^ 52
    omega
  have carry_arithmetic :=
    Binary64EncoderCarryArithmetic e q chooseHigh e_lower e_upper q_lower q_upper
  have bits_choice : bits = if chooseHigh then lowBits + 1 else lowBits :=
    carry_arithmetic.1
  have low_bits_bound : lowBits < 2047 * 2 ^ 52 := by
    change e * 2 ^ 52 + (q - 2 ^ 52) < 2047 * 2 ^ 52
    omega
  have bits_bound : bits < 2047 * 2 ^ 52 := carry_arithmetic.2.1
  have bits_word : (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) = bits := by
    change (BitVec.ofNat 64 bits).toNat = bits
    rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt]
    omega
  let scale : Real := (2 : Real) ^ ((e : Int) - 1075)
  have scale_pos : 0 < scale := by
    dsimp [scale]
    exact zpow_pos (by norm_num) _
  have z_real_decomp : (z : Real) = (q : Real) + (r : Real) / (z.den : Real) := by
    rw [Rat.cast_def, ← numerator_natAbs, ← qr_decomp]
    push_cast
    rw [add_div, mul_div_cancel_right₀ _ (by positivity : (z.den : Real) ≠ 0)]
  have x_scale : (x : Real) = (z : Real) * scale := by
    dsimp [z, scale]
    push_cast
    rw [mul_assoc, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
    have exponent_zero : ((1075 : Int) - (e : Int)) + ((e : Int) - 1075) = 0 := by
      ring
    rw [exponent_zero]
    norm_num
  let value : Nat → Real := fun n =>
    let field := n / (2 ^ 52 : Nat)
    let fraction := n % (2 ^ 52 : Nat)
    if field = 0 then (fraction : Real) / (2 : Real) ^ 1074
    else if 1075 ≤ field then
      (((2 ^ 52 : Nat) + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075)
    else (((2 ^ 52 : Nat) + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field)
  have adjacent_values :
      value lowBits = (q : Real) * scale ∧
      value (lowBits + 1) = ((q + 1 : Nat) : Real) * scale := by
    simpa only [value, lowBits, scale] using
      Binary64AdjacentNormalValues e q e_lower e_upper q_lower q_upper
  let frac : Real := (r : Real) / (z.den : Real)
  have den_pos_real : (0 : Real) < (z.den : Real) := by exact_mod_cast den_pos
  have frac_nonnegative : 0 ≤ frac := by
    dsimp [frac]
    exact div_nonneg (Nat.cast_nonneg r) den_pos_real.le
  have frac_lt_one : frac < 1 := by
    dsimp [frac]
    apply (div_lt_one den_pos_real).2
    exact_mod_cast r_lt
  have x_formula : (x : Real) = ((q : Real) + frac) * scale := by
    rw [x_scale, z_real_decomp]
  have low_le_x : value lowBits ≤ (x : Real) := by
    rw [adjacent_values.1, x_formula]
    exact mul_le_mul_of_nonneg_right (by linarith) scale_pos.le
  have x_le_high : (x : Real) ≤ value (lowBits + 1) := by
    rw [adjacent_values.2, x_formula]
    push_cast
    exact mul_le_mul_of_nonneg_right (by linarith) scale_pos.le
  have low_distance :
      abs ((x : Real) - value lowBits) = frac * scale := by
    rw [abs_of_nonneg (sub_nonneg.mpr low_le_x), x_formula, adjacent_values.1]
    ring
  have high_distance :
      abs ((x : Real) - value (lowBits + 1)) = (1 - frac) * scale := by
    rw [abs_of_nonpos (sub_nonpos.mpr x_le_high), x_formula, adjacent_values.2]
    push_cast
    ring
  have frac_ge_half : z.den ≤ 2 * r → (1 : Real) / 2 ≤ frac := by
    intro h
    dsimp [frac]
    apply (div_le_div_iff₀ (by norm_num : (0 : Real) < 2) den_pos_real).2
    have hc : (z.den : Real) ≤ 2 * (r : Real) := by exact_mod_cast h
    simpa [mul_comm] using hc
  have frac_le_half : 2 * r ≤ z.den → frac ≤ (1 : Real) / 2 := by
    intro h
    dsimp [frac]
    apply (div_le_div_iff₀ den_pos_real (by norm_num : (0 : Real) < 2)).2
    have hc : 2 * (r : Real) ≤ (z.den : Real) := by exact_mod_cast h
    simpa [mul_comm] using hc
  have high_near (h : chooseHigh) :
      abs ((x : Real) - value (lowBits + 1)) ≤
        abs ((x : Real) - value lowBits) := by
    rw [high_distance, low_distance]
    apply mul_le_mul_of_nonneg_right _ scale_pos.le
    apply sub_le_iff_le_add.mpr
    have hden : z.den ≤ 2 * r := by
      dsimp [chooseHigh] at h
      omega
    have := frac_ge_half hden
    linarith
  have low_near (h : ¬ chooseHigh) :
      abs ((x : Real) - value lowBits) ≤
        abs ((x : Real) - value (lowBits + 1)) := by
    rw [low_distance, high_distance]
    apply mul_le_mul_of_nonneg_right _ scale_pos.le
    have hden : 2 * r ≤ z.den := by
      dsimp [chooseHigh] at h
      omega
    have := frac_le_half hden
    linarith
  have tie_den (htie :
      abs ((x : Real) - value lowBits) =
        abs ((x : Real) - value (lowBits + 1))) : z.den = 2 * r := by
    rw [low_distance, high_distance] at htie
    have hfrac : frac = 1 - frac :=
      mul_right_cancel₀ scale_pos.ne' htie
    have hhalf : frac = (1 : Real) / 2 := by linarith
    have hdiv : (r : Real) / (z.den : Real) = (1 : Real) / 2 := by
      simpa only [frac] using hhalf
    have hcross := (div_eq_div_iff (ne_of_gt den_pos_real) (by norm_num : (2 : Real) ≠ 0)).mp hdiv
    have hnat : r * 2 = 1 * z.den := by exact_mod_cast hcross
    omega
  have value_mono : ∀ p q : Nat, p < q → q < 9218868437227405312 →
      value p < value q := by
    simpa only [value] using Binary64FiniteMagnitudeStrictMono
  have hround : DecimalGoalVocabularyDefault.roundsMagnitude (x : Real)
      (⟨BitVec.ofNat 64 bits⟩ : F64) := by
    dsimp [DecimalGoalVocabularyDefault]
    change 0 ≤ (x : Real) ∧
      (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) ≤ 9218868437227405312 ∧
      (if (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) = 9218868437227405312 then
        (2 : Real) ^ 1024 - (2 : Real) ^ 970 ≤ (x : Real)
       else (x : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 ∧
        ∀ v : F64, (↑v : Nat) < 9218868437227405312 →
          v ≠ (⟨BitVec.ofNat 64 bits⟩ : F64) →
          abs ((x : Real) - value (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat)) ≤
              abs ((x : Real) - value (↑v : Nat)) ∧
          (abs ((x : Real) - value (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat)) =
              abs ((x : Real) - value (↑v : Nat)) →
            (↑(⟨BitVec.ofNat 64 bits⟩ : F64) : Nat) % 2 = 0))
    have x_nonnegative : (0 : Real) ≤ (x : Real) := by
      have hp : (0 : Rat) ≤ 2 ^ (-1022 : Int) :=
        (zpow_pos (by norm_num : (0 : Rat) < 2) _).le
      exact_mod_cast le_trans hp hxlow
    have x_high_real : (x : Real) < (2 : Real) ^ (128 : Nat) := by
      exact_mod_cast hxhigh
    have below_threshold : (x : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 := by
      apply x_high_real.trans
      set_option exponentiation.threshold 2048 in
        norm_num
    rw [bits_word]
    refine ⟨x_nonnegative, by omega, ?_⟩
    rw [if_neg (by omega)]
    refine ⟨below_threshold, ?_⟩
    intro v hv hne
    have raw_ne : (↑v : Nat) ≠ bits := by
      intro heq
      apply hne
      apply UScalar.eq_of_val_eq
      simpa [bits_word] using heq
    by_cases hhigh : chooseHigh
    · have bits_eq : bits = lowBits + 1 := by
        rw [bits_choice, if_pos hhigh]
      rw [bits_eq]
      have raw_ne_high : (↑v : Nat) ≠ lowBits + 1 := by
        simpa only [bits_eq] using raw_ne
      rcases lt_or_gt_of_ne raw_ne_high with raw_lt | raw_gt
      · have raw_le : (↑v : Nat) ≤ lowBits := by omega
        have vvalue_le_low : value (↑v : Nat) ≤ value lowBits := by
          rcases raw_le.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (value_mono _ _ hlt low_bits_bound).le
        have vvalue_le_x : value (↑v : Nat) ≤ (x : Real) :=
          vvalue_le_low.trans low_le_x
        have low_to_v :
            abs ((x : Real) - value lowBits) ≤
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonneg (sub_nonneg.mpr low_le_x),
            abs_of_nonneg (sub_nonneg.mpr vvalue_le_x)]
          linarith
        constructor
        · exact (high_near hhigh).trans low_to_v
        · intro heqdist
          have low_to_high :
              abs ((x : Real) - value lowBits) ≤
                abs ((x : Real) - value (lowBits + 1)) := by
            calc
              abs ((x : Real) - value lowBits) ≤
                  abs ((x : Real) - value (↑v : Nat)) := low_to_v
              _ = abs ((x : Real) - value (lowBits + 1)) := heqdist.symm
          have htie :
              abs ((x : Real) - value lowBits) =
                abs ((x : Real) - value (lowBits + 1)) :=
            le_antisymm low_to_high (high_near hhigh)
          have hden := tie_den htie
          have qodd : q % 2 = 1 := by
            dsimp [chooseHigh] at hhigh
            omega
          have qsucc_even : (q + 1) % 2 = 0 := by omega
          exact carry_arithmetic.2.2.2.trans qsucc_even
      · have high_value_lt_v : value (lowBits + 1) < value (↑v : Nat) :=
          value_mono _ _ raw_gt hv
        have x_le_v : (x : Real) ≤ value (↑v : Nat) :=
          x_le_high.trans high_value_lt_v.le
        have strict_distance :
            abs ((x : Real) - value (lowBits + 1)) <
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonpos (sub_nonpos.mpr x_le_high),
            abs_of_nonpos (sub_nonpos.mpr x_le_v)]
          linarith
        constructor
        · exact strict_distance.le
        · intro heqdist
          exact (ne_of_lt strict_distance heqdist).elim
    · have bits_eq : bits = lowBits := by
        rw [bits_choice, if_neg hhigh]
      rw [bits_eq]
      have raw_ne_low : (↑v : Nat) ≠ lowBits := by
        simpa only [bits_eq] using raw_ne
      rcases lt_or_gt_of_ne raw_ne_low with raw_lt | raw_gt
      · have vvalue_lt_low : value (↑v : Nat) < value lowBits :=
          value_mono _ _ raw_lt low_bits_bound
        have vvalue_le_x : value (↑v : Nat) ≤ (x : Real) :=
          vvalue_lt_low.le.trans low_le_x
        have strict_distance :
            abs ((x : Real) - value lowBits) <
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonneg (sub_nonneg.mpr low_le_x),
            abs_of_nonneg (sub_nonneg.mpr vvalue_le_x)]
          linarith
        constructor
        · exact strict_distance.le
        · intro heqdist
          exact (ne_of_lt strict_distance heqdist).elim
      · have high_le_raw : lowBits + 1 ≤ (↑v : Nat) := by omega
        have high_value_le_v : value (lowBits + 1) ≤ value (↑v : Nat) := by
          rcases high_le_raw.eq_or_lt with heq | hlt
          · exact le_of_eq (congrArg value heq)
          · exact (value_mono _ _ hlt hv).le
        have x_le_v : (x : Real) ≤ value (↑v : Nat) :=
          x_le_high.trans high_value_le_v
        have high_to_v :
            abs ((x : Real) - value (lowBits + 1)) ≤
              abs ((x : Real) - value (↑v : Nat)) := by
          rw [abs_of_nonpos (sub_nonpos.mpr x_le_high),
            abs_of_nonpos (sub_nonpos.mpr x_le_v)]
          linarith
        constructor
        · exact (low_near hhigh).trans high_to_v
        · intro heqdist
          have high_to_low :
              abs ((x : Real) - value (lowBits + 1)) ≤
                abs ((x : Real) - value lowBits) := by
            calc
              abs ((x : Real) - value (lowBits + 1)) ≤
                  abs ((x : Real) - value (↑v : Nat)) := high_to_v
              _ = abs ((x : Real) - value lowBits) := heqdist.symm
          have htie :
              abs ((x : Real) - value lowBits) =
                abs ((x : Real) - value (lowBits + 1)) :=
            le_antisymm (low_near hhigh) high_to_low
          have hden := tie_den htie
          have qeven : q % 2 = 0 := by
            have qmod : q % 2 < 2 := Nat.mod_lt _ (by omega)
            dsimp [chooseHigh] at hhigh
            omega
          exact carry_arithmetic.2.2.1.trans qeven
  simpa only [step, scan, e, z, q, r, chooseHigh, rounded, bits] using hround

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
Standard-library dependencies (30)

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

Proof assumptions and review

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

Transitive assumptions

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

Helper theorems included
All 10 entries
Technical checking records
Local closure summary
{
  "node": "Binary64NormalEncoderRounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "e92c0208d73fb8b12e9a60301257aee89b030e68ae0383b0fce95f1efdeb225f",
  "active_statement_hash": "6f00f074a50892811fde224cfa5e1a85b66ba8ce98a8592a94715623c1a7c8a9",
  "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-131",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64AdjacentNormalValues": "8495001b4edafedb",
    "Binary64EncoderCarryArithmetic": "c5544515a9685c24",
    "Binary64FiniteMagnitudeStrictMono": "ea630fa702fb7b61",
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:2c137be416a5629f65645320bd9f96eb7d4e6062b49f59f079f12eb8b7a62de6",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}