Binary64ExactPositiveDivRounds

Lean checked

Statement

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

Lemma

Let two clear-sign finite binary64 words decode exactly to positive rationals x and y. If 2^{-1022}\le x/y<2^{128}, their modeled division returns a clear-sign word below 2^{63} which satisfies roundsMagnitude for the real value of x/y.

Lean statement

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

import Tablet.Binary64NormalEncoderRounds
import Tablet.Binary64ModelDefault
import Tablet.DecimalGoalVocabularyDefault
import Tablet.ModelF64Div

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

namespace dec2flt_model

-- [TABLET NODE: Binary64ExactPositiveDivRounds]
theorem Binary64ExactPositiveDivRounds :
    ∀ (a b : F64) (x y : Rat),
      a.val < 2047 * 2 ^ 52 → b.val < 2047 * 2 ^ 52 →
      (let bits := a.val
       let e := bits / (2 ^ 52 : Nat)
       let m := bits % (2 ^ 52 : Nat)
       if e = 0 then (m : Rat) / (2 : Rat) ^ (1074 : Nat)
       else if 1075 ≤ e then (((2 ^ 52 : Nat) + m : Nat) : Rat) * (2 : Rat) ^ (e - 1075)
       else (((2 ^ 52 : Nat) + m : Nat) : Rat) / (2 : Rat) ^ (1075 - e)) = x →
      (let bits := b.val
       let e := bits / (2 ^ 52 : Nat)
       let m := bits % (2 ^ 52 : Nat)
       if e = 0 then (m : Rat) / (2 : Rat) ^ (1074 : Nat)
       else if 1075 ≤ e then (((2 ^ 52 : Nat) + m : Nat) : Rat) * (2 : Rat) ^ (e - 1075)
       else (((2 ^ 52 : Nat) + m : Nat) : Rat) / (2 : Rat) ^ (1075 - e)) = y →
      0 < x → 0 < y → (2 : Rat) ^ (-1022 : Int) ≤ x / y →
      x / y < (2 : Rat) ^ (128 : Nat) →
      ∃ w : F64, ModelF64Div a b = ok w ∧ w.val < 2 ^ 63 ∧
        DecimalGoalVocabularyDefault.roundsMagnitude ((x / y : Rat) : Real) w := 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

The operand raw bounds put their exponent fields below 2047 and clear bit 63. Thus neither is NaN or infinity and the modeled result sign is positive. Positivity of the exact decoded values also excludes the zero category: if an operand had zero exponent and zero fraction fields, Euclidean division by 2^{52} would make its raw word zero and its stated exact decoding would be zero, contradicting positivity.
Unfold ModelF64Div and Binary64ModelDefault. The NaN, infinity, and zero special cases are consequently false, and the ordinary branch encodes the quotient of the two finite decodings. The exact-decoding hypotheses identify this quotient with x/y. Apply Binary64NormalEncoderRounds using the stated normal-range bounds. It supplies the required roundsMagnitude relation. That relation bounds the returned raw word by 2047\,2^{52}<2^{63}, proving the final clear-sign bound.

Lean proof


  intro a b x y ha hb hax hby hx hy hlo hhi
  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 / y)
  let significand := roundEvenNat ((x / y) * ((2 : Rat) ^ ((1075 : Int) - (e : Int))))
  let bits := if significand = 2 ^ 53 then (e + 1) * 2 ^ 52
    else e * 2 ^ 52 + (significand - 2 ^ 52)
  let w : F64 := ⟨BitVec.ofNat 64 bits⟩
  have hround : DecimalGoalVocabularyDefault.roundsMagnitude (((x / y : Rat) : Real)) w := by
    exact Binary64NormalEncoderRounds (x / y) hlo hhi
  have haexp : a.val / (2 ^ 52 : Nat) < 2047 := by
    apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < (2 ^ 52 : Nat))).2
    simpa [Nat.mul_comm] using ha
  have hbexp : b.val / (2 ^ 52 : Nat) < 2047 := by
    apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < (2 ^ 52 : Nat))).2
    simpa [Nat.mul_comm] using hb
  have hasign : a.val / (2 ^ 63 : Nat) = 0 := Nat.div_eq_of_lt (by omega)
  have hbsign : b.val / (2 ^ 63 : Nat) = 0 := Nat.div_eq_of_lt (by omega)
  have hamag : a.val % (2 ^ 63 : Nat) = a.val := Nat.mod_eq_of_lt (by omega)
  have hbmag : b.val % (2 ^ 63 : Nat) = b.val := Nat.mod_eq_of_lt (by omega)
  refine ⟨w, ?_, ?_, hround⟩
  · simp only [ModelF64Div, Binary64ModelDefault]
    have ha2047 : a.val / (2 ^ 52 : Nat) ≠ 2047 := by omega
    have hb2047 : b.val / (2 ^ 52 : Nat) ≠ 2047 := by omega
    simp only [hasign, hbsign, hamag, hbmag, ha2047, hb2047, if_false]
    let ca := if a.val / 4503599627370496 = 0 then
      if a.val % 4503599627370496 = 0 then 2 else 3 else 4
    let cb := if b.val / 4503599627370496 = 0 then
      if b.val % 4503599627370496 = 0 then 2 else 3 else 4
    have ca0 : ca ≠ 0 := by
      dsimp [ca]
      by_cases h : a.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have ca1 : ca ≠ 1 := by
      dsimp [ca]
      by_cases h : a.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have cb0 : cb ≠ 0 := by
      dsimp [cb]
      by_cases h : b.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have cb1 : cb ≠ 1 := by
      dsimp [cb]
      by_cases h : b.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have ca2 : ca ≠ 2 := by
      intro hca
      have he : a.val / 4503599627370496 = 0 := by
        dsimp [ca] at hca
        split at hca <;> omega
      have hm : a.val % 4503599627370496 = 0 := by
        dsimp [ca] at hca
        rw [if_pos he] at hca
        split at hca <;> omega
      have haz : a.val = 0 := by
        have hdecomp := Nat.mod_add_div a.val 4503599627370496
        omega
      simp [haz] at hax
      linarith
    have cb2 : cb ≠ 2 := by
      intro hcb
      have he : b.val / 4503599627370496 = 0 := by
        dsimp [cb] at hcb
        split at hcb <;> omega
      have hm : b.val % 4503599627370496 = 0 := by
        dsimp [cb] at hcb
        rw [if_pos he] at hcb
        split at hcb <;> omega
      have hbz : b.val = 0 := by
        have hdecomp := Nat.mod_add_div b.val 4503599627370496
        omega
      simp [hbz] at hby
      linarith
    change (if ca = 0 ∨ cb = 0 then _ else
      if (ca = 1 ∧ cb = 1) ∨ (ca = 2 ∧ cb = 2) then _ else
      if ca = 1 ∨ cb = 2 then _ else if cb = 1 ∨ ca = 2 then _ else _) = _
    simp only [ca0, ca1, ca2, cb0, cb1, cb2, false_or, false_and,
      and_false, if_false]
    rw [hax, hby]
    have hover : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ x / y := by
      intro h
      have impossible : (2 ^ 1024 - 2 ^ 970 : Rat) < 2 ^ 128 := h.trans_lt hhi
      set_option exponentiation.threshold 2048 in norm_num at impossible
    rw [if_neg hover, if_neg (not_lt_of_ge hlo)]
    have hneg : decide (((0 : Nat) != 0) = true) = false := rfl
    rw [hneg]
    simp only [Bool.false_eq_true, if_false, Nat.add_zero]
    change ok w = ok w
    rfl
  · dsimp [DecimalGoalVocabularyDefault] at hround
    exact lt_of_le_of_lt hround.2.1 (by norm_num)

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Binary64NormalEncoderRounds
import Tablet.Binary64ModelDefault
import Tablet.DecimalGoalVocabularyDefault
import Tablet.ModelF64Div

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

namespace dec2flt_model

-- [TABLET NODE: Binary64ExactPositiveDivRounds]
theorem Binary64ExactPositiveDivRounds :
    ∀ (a b : F64) (x y : Rat),
      a.val < 2047 * 2 ^ 52 → b.val < 2047 * 2 ^ 52 →
      (let bits := a.val
       let e := bits / (2 ^ 52 : Nat)
       let m := bits % (2 ^ 52 : Nat)
       if e = 0 then (m : Rat) / (2 : Rat) ^ (1074 : Nat)
       else if 1075 ≤ e then (((2 ^ 52 : Nat) + m : Nat) : Rat) * (2 : Rat) ^ (e - 1075)
       else (((2 ^ 52 : Nat) + m : Nat) : Rat) / (2 : Rat) ^ (1075 - e)) = x →
      (let bits := b.val
       let e := bits / (2 ^ 52 : Nat)
       let m := bits % (2 ^ 52 : Nat)
       if e = 0 then (m : Rat) / (2 : Rat) ^ (1074 : Nat)
       else if 1075 ≤ e then (((2 ^ 52 : Nat) + m : Nat) : Rat) * (2 : Rat) ^ (e - 1075)
       else (((2 ^ 52 : Nat) + m : Nat) : Rat) / (2 : Rat) ^ (1075 - e)) = y →
      0 < x → 0 < y → (2 : Rat) ^ (-1022 : Int) ≤ x / y →
      x / y < (2 : Rat) ^ (128 : Nat) →
      ∃ w : F64, ModelF64Div a b = ok w ∧ w.val < 2 ^ 63 ∧
        DecimalGoalVocabularyDefault.roundsMagnitude ((x / y : Rat) : Real) w := by
-- BODY
  intro a b x y ha hb hax hby hx hy hlo hhi
  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 / y)
  let significand := roundEvenNat ((x / y) * ((2 : Rat) ^ ((1075 : Int) - (e : Int))))
  let bits := if significand = 2 ^ 53 then (e + 1) * 2 ^ 52
    else e * 2 ^ 52 + (significand - 2 ^ 52)
  let w : F64 := ⟨BitVec.ofNat 64 bits⟩
  have hround : DecimalGoalVocabularyDefault.roundsMagnitude (((x / y : Rat) : Real)) w := by
    exact Binary64NormalEncoderRounds (x / y) hlo hhi
  have haexp : a.val / (2 ^ 52 : Nat) < 2047 := by
    apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < (2 ^ 52 : Nat))).2
    simpa [Nat.mul_comm] using ha
  have hbexp : b.val / (2 ^ 52 : Nat) < 2047 := by
    apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < (2 ^ 52 : Nat))).2
    simpa [Nat.mul_comm] using hb
  have hasign : a.val / (2 ^ 63 : Nat) = 0 := Nat.div_eq_of_lt (by omega)
  have hbsign : b.val / (2 ^ 63 : Nat) = 0 := Nat.div_eq_of_lt (by omega)
  have hamag : a.val % (2 ^ 63 : Nat) = a.val := Nat.mod_eq_of_lt (by omega)
  have hbmag : b.val % (2 ^ 63 : Nat) = b.val := Nat.mod_eq_of_lt (by omega)
  refine ⟨w, ?_, ?_, hround⟩
  · simp only [ModelF64Div, Binary64ModelDefault]
    have ha2047 : a.val / (2 ^ 52 : Nat) ≠ 2047 := by omega
    have hb2047 : b.val / (2 ^ 52 : Nat) ≠ 2047 := by omega
    simp only [hasign, hbsign, hamag, hbmag, ha2047, hb2047, if_false]
    let ca := if a.val / 4503599627370496 = 0 then
      if a.val % 4503599627370496 = 0 then 2 else 3 else 4
    let cb := if b.val / 4503599627370496 = 0 then
      if b.val % 4503599627370496 = 0 then 2 else 3 else 4
    have ca0 : ca ≠ 0 := by
      dsimp [ca]
      by_cases h : a.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have ca1 : ca ≠ 1 := by
      dsimp [ca]
      by_cases h : a.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have cb0 : cb ≠ 0 := by
      dsimp [cb]
      by_cases h : b.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have cb1 : cb ≠ 1 := by
      dsimp [cb]
      by_cases h : b.val / 4503599627370496 = 0
      · rw [if_pos h]; split <;> omega
      · rw [if_neg h]; omega
    have ca2 : ca ≠ 2 := by
      intro hca
      have he : a.val / 4503599627370496 = 0 := by
        dsimp [ca] at hca
        split at hca <;> omega
      have hm : a.val % 4503599627370496 = 0 := by
        dsimp [ca] at hca
        rw [if_pos he] at hca
        split at hca <;> omega
      have haz : a.val = 0 := by
        have hdecomp := Nat.mod_add_div a.val 4503599627370496
        omega
      simp [haz] at hax
      linarith
    have cb2 : cb ≠ 2 := by
      intro hcb
      have he : b.val / 4503599627370496 = 0 := by
        dsimp [cb] at hcb
        split at hcb <;> omega
      have hm : b.val % 4503599627370496 = 0 := by
        dsimp [cb] at hcb
        rw [if_pos he] at hcb
        split at hcb <;> omega
      have hbz : b.val = 0 := by
        have hdecomp := Nat.mod_add_div b.val 4503599627370496
        omega
      simp [hbz] at hby
      linarith
    change (if ca = 0 ∨ cb = 0 then _ else
      if (ca = 1 ∧ cb = 1) ∨ (ca = 2 ∧ cb = 2) then _ else
      if ca = 1 ∨ cb = 2 then _ else if cb = 1 ∨ ca = 2 then _ else _) = _
    simp only [ca0, ca1, ca2, cb0, cb1, cb2, false_or, false_and,
      and_false, if_false]
    rw [hax, hby]
    have hover : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ x / y := by
      intro h
      have impossible : (2 ^ 1024 - 2 ^ 970 : Rat) < 2 ^ 128 := h.trans_lt hhi
      set_option exponentiation.threshold 2048 in norm_num at impossible
    rw [if_neg hover, if_neg (not_lt_of_ge hlo)]
    have hneg : decide (((0 : Nat) != 0) = true) = false := rfl
    rw [hneg]
    simp only [Bool.false_eq_true, if_false, Nat.add_zero]
    change ok w = ok w
    rfl
  · dsimp [DecimalGoalVocabularyDefault] at hround
    exact lt_of_le_of_lt hround.2.1 (by norm_num)

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 15 entries
Technical checking records
Local closure summary
{
  "node": "Binary64ExactPositiveDivRounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "ed7fea7204c1cb1f6ec7b0175b4eaaa852e63f5fc2a4a86e4f443dc98a1e91ba",
  "active_statement_hash": "b661b8b9f88fc89e2ad71a8443b9e6bc2e627f4cea22c73911f997f8cbd86849",
  "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": {
    "Binary64ModelDefault": "c722a2c00805b545",
    "Binary64NormalEncoderRounds": "e7e60775e2c3fcdc",
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "Binary64Model": "c98c2c511cf0ced6",
    "Binary64ModelDefault": "dc548d1e27696ba4",
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4",
    "ModelF64Div": "f8da2e585a4f4183",
    "core_num_FpCategory": "f500de642a5d5148"
  }
}
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:2905db8bc13490ceb75ee7b0124103294bfc419facca050cb4155593503072d0",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}