Binary64ExactPositiveMulRounds

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 nonnegative rationals x and y. If 2^{-1022}\le xy<2^{128}, their modeled multiplication returns a clear-sign word below 2^{63} which satisfies roundsMagnitude for the real value of xy.

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

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

namespace dec2flt_model

-- [TABLET NODE: Binary64ExactPositiveMulRounds]
theorem Binary64ExactPositiveMulRounds :
    ∀ (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 →
      (2 : Rat) ^ (-1022 : Int) ≤ x * y → x * y < (2 : Rat) ^ (128 : Nat) →
      ∃ w : F64, ModelF64Mul 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 raw bounds put each operand's exponent field strictly below 2047, so neither operand has the NaN or infinity category. They also put bit 63 clear, so the modeled result sign is positive. Unfold ModelF64Mul and Binary64ModelDefault. All special category branches are therefore false and the remaining branch applies the model's magnitude encoder to the product of the two finite decodings. The two exact-decoding hypotheses identify that argument with xy.
The stated lower and upper bounds are exactly the hypotheses of Binary64NormalEncoderRounds; apply that lemma to the encoder word. It supplies roundsMagnitude for the embedded real product. The relation bounds the raw word by the infinity word 2047\,2^{52}, which is strictly below 2^{63}, giving the required clear-sign bound.

Lean proof


  intro a b x y ha hb hax hby 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 := by
    apply Nat.div_eq_of_lt
    omega
  have hbsign : b.val / (2 ^ 63 : Nat) = 0 := by
    apply Nat.div_eq_of_lt
    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 [ModelF64Mul, 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]
    have hca0 :
        (if a.val / (2 ^ 52 : Nat) = 0 then
          if a.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 0 := by
      by_cases h : a.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    have hca1 :
        (if a.val / (2 ^ 52 : Nat) = 0 then
          if a.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 1 := by
      by_cases h : a.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    have hcb0 :
        (if b.val / (2 ^ 52 : Nat) = 0 then
          if b.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 0 := by
      by_cases h : b.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    have hcb1 :
        (if b.val / (2 ^ 52 : Nat) = 0 then
          if b.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 1 := by
      by_cases h : b.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    simp only [hca0, hca1, hcb0, hcb1, 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.ModelF64Mul

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

namespace dec2flt_model

-- [TABLET NODE: Binary64ExactPositiveMulRounds]
theorem Binary64ExactPositiveMulRounds :
    ∀ (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 →
      (2 : Rat) ^ (-1022 : Int) ≤ x * y → x * y < (2 : Rat) ^ (128 : Nat) →
      ∃ w : F64, ModelF64Mul 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 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 := by
    apply Nat.div_eq_of_lt
    omega
  have hbsign : b.val / (2 ^ 63 : Nat) = 0 := by
    apply Nat.div_eq_of_lt
    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 [ModelF64Mul, 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]
    have hca0 :
        (if a.val / (2 ^ 52 : Nat) = 0 then
          if a.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 0 := by
      by_cases h : a.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    have hca1 :
        (if a.val / (2 ^ 52 : Nat) = 0 then
          if a.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 1 := by
      by_cases h : a.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    have hcb0 :
        (if b.val / (2 ^ 52 : Nat) = 0 then
          if b.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 0 := by
      by_cases h : b.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    have hcb1 :
        (if b.val / (2 ^ 52 : Nat) = 0 then
          if b.val % (2 ^ 52 : Nat) = 0 then 2 else 3 else 4) ≠ 1 := by
      by_cases h : b.val / (2 ^ 52 : Nat) = 0
      · rw [if_pos h]
        split <;> omega
      · rw [if_neg h]
        omega
    simp only [hca0, hca1, hcb0, hcb1, 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": "Binary64ExactPositiveMulRounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "493fcee3f38c208db1b11bd310b8b6127aae281dc3cdc3d6ce4a1c0201e080d2",
  "active_statement_hash": "67711f9d26c0972d4ec4d8d4fa3ad56e300bafad967996f132a80a6f67d45746",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64ModelDefault": "c722a2c00805b545",
    "Binary64NormalEncoderRounds": "e7e60775e2c3fcdc",
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "Binary64Model": "c98c2c511cf0ced6",
    "Binary64ModelDefault": "dc548d1e27696ba4",
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "F64": "ec4e25fbaf4e11d4",
    "ModelF64Mul": "ba97971db93c2ae2",
    "core_num_FpCategory": "c43614ff2149ecf2"
  }
}
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:22811133860d8fadf64680d66034438710d31e47037341fcb3ff0a6e163ee2e4",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}