Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma
roundsMagnitude for the real number v.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.Binary64AdjacentNormalValues
import Tablet.Binary64ModelDefault
import Tablet.Binary64NativeCallDomain
import Tablet.DecimalGoalVocabularyDefault
import Tablet.ModelF64FromU64
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 2000000
namespace dec2flt_model
-- [TABLET NODE: Binary64FromU64Exact]
theorem Binary64FromU64Exact :
∀ v : Std.U64, v.val ≤ 2 ^ 53 →
∃ f : F64, ModelF64FromU64 v = ok f ∧ f.val < 2047 * 2 ^ 52 ∧
(let bits := f.val
let e := bits / (2 ^ 52 : Nat)
let m := bits % (2 ^ 52 : Nat)
let decoded : Rat := 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)
decoded = (v.val : Rat)) ∧
DecimalGoalVocabularyDefault.roundsMagnitude (v.val : Real) f := 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
roundsMagnitude fact.Lean proof
intro v hv
obtain ⟨fd, hfd, hfdFinite⟩ := Binary64NativeCallDomain.1 v hv
have hfdFinite' : fd.val < 2047 * 2 ^ 52 := by
norm_num at hfdFinite ⊢
exact hfdFinite
by_cases hv0 : v.val = 0
· have veq : v = 0#u64 := UScalar.eq_of_val_eq hv0
subst v
let fz : F64 := ⟨BitVec.ofNat 64 0⟩
have fzval : fz.val = 0 := rfl
have hmodel : ModelF64FromU64 0#u64 = ok fz := by
set_option exponentiation.threshold 2048 in
norm_num [ModelF64FromU64, Binary64ModelDefault, zpow_neg]
apply UScalar.eq_of_val_eq
change (BitVec.ofNat 64 0).toNat = 0
simp
have hfdEq : fd = fz := by
have hEq : ok fz = ok fd := hmodel.symm.trans hfd
exact (Result.ok.inj hEq).symm
refine ⟨fz, hmodel, ?_, ?_, ?_⟩
· rw [← hfdEq]
exact hfdFinite'
· simp [fzval]
dsimp [DecimalGoalVocabularyDefault]
rw [hv0, fzval]
simp only [Nat.zero_div, Nat.zero_mod, Nat.cast_zero, zero_div, if_true]
change 0 ≤ (0 : Real) ∧ 0 ≤ 9218868437227405312 ∧
(if (0 : Nat) = 9218868437227405312 then
(2 : Real) ^ 1024 - (2 : Real) ^ 970 ≤ 0
else (0 : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 ∧
∀ w : F64, w.val < 9218868437227405312 → w ≠ fz →
abs (0 - 0) ≤
abs (0 - (let e := w.val / 4503599627370496
let m := w.val % 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))) ∧
(abs (0 - 0) =
abs (0 - (let e := w.val / 4503599627370496
let m := w.val % 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))) →
True))
refine ⟨by norm_num, by norm_num, ?_⟩
rw [if_neg (by norm_num)]
refine ⟨?_, ?_⟩
· set_option exponentiation.threshold 2048 in
norm_num
· intro w hw hne
have value_nonnegative :
0 ≤ (let e := w.val / 4503599627370496
let m := w.val % 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)) := by
dsimp only
split <;> positivity
constructor
· simpa [abs_of_nonneg value_nonnegative] using value_nonnegative
· intro _
trivial
· let x : Rat := v.val
have hxlow : (2 : Rat) ^ (-1022 : Int) ≤ x := by
have hone : (1 : Rat) ≤ x := by
change (1 : Rat) ≤ (v.val : Rat)
exact_mod_cast (Nat.one_le_iff_ne_zero.mpr hv0)
apply le_trans _ hone
rw [zpow_neg]
exact (inv_le_one₀ (pow_pos (by norm_num : (0 : Rat) < 2) _)).2
(one_le_pow₀ (by norm_num))
have hxhigh : x < (2 : Rat) ^ (128 : Nat) := by
have hcast : x ≤ (2 ^ 53 : Rat) := by
change (v.val : Rat) ≤ (2 ^ 53 : Rat)
exact_mod_cast hv
apply hcast.trans_lt
exact pow_lt_pow_right₀ (by norm_num) (by omega)
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 => simpa [scan] using hxlow
| succ n ih =>
rw [scan_formula]
dsimp [step]
split
· assumption
· exact ih
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 hn0 : n = 0
· subst n
simp [scan, step]
· dsimp [step]
split <;> omega
have last_threshold :
¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x) := by
intro h
have hlarge : (2 : Rat) ^ (128 : Nat) < (2 : Rat) ^ (1023 : Nat) :=
pow_lt_pow_right₀ (by norm_num) (by omega)
exact (not_lt_of_ge h) (hxhigh.trans hlarge)
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 e_small : e ≤ 1076 := by
by_contra h
have hp : (2 : Rat) ^ (54 : Int) ≤ (2 : Rat) ^ ((e : Int) - 1023) :=
zpow_le_zpow_right₀ (by norm_num) (by omega)
have hm : (2 : Rat) ^ (54 : Int) ≤ x := hp.trans current_threshold
have hxle : x ≤ (2 : Rat) ^ (53 : Nat) := by
change (v.val : Rat) ≤ (2 ^ 53 : Rat)
exact_mod_cast hv
have himpossible : (2 : Rat) ^ (54 : Nat) ≤ (2 : Rat) ^ (53 : Nat) :=
hm.trans hxle
norm_num [pow_succ] at himpossible
have z_exact : ∃ n : Nat, z = (n : Rat) := by
by_cases he : e ≤ 1075
· refine ⟨v.val * 2 ^ (1075 - e), ?_⟩
dsimp [z, x]
rw [show (1075 : Int) - (e : Int) = ((1075 - e : Nat) : Int) by omega,
zpow_natCast]
norm_num [Nat.cast_mul, Nat.cast_pow]
· have ee : e = 1076 := by omega
have hm : v.val = 2 ^ 53 := by
have lower : (2 ^ 53 : Rat) ≤ x := by simpa [ee] using current_threshold
have lower_nat : 2 ^ 53 ≤ v.val := by
change (2 ^ 53 : Rat) ≤ (v.val : Rat) at lower
exact_mod_cast lower
exact le_antisymm hv lower_nat
refine ⟨2 ^ 52, ?_⟩
dsimp [z, x]
rw [ee, hm]
norm_num [zpow_neg, div_eq_mul_inv]
obtain ⟨n, hn⟩ := z_exact
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 n_lower : 2 ^ 52 ≤ n := by
rw [hn] at z_lower
exact_mod_cast z_lower
have n_upper : n < 2 ^ 53 := by
rw [hn] at z_upper
exact_mod_cast z_upper
let lowBits : Nat := e * 2 ^ 52 + (n - 2 ^ 52)
let f : F64 := ⟨BitVec.ofNat 64 lowBits⟩
have flow : f.val = lowBits := by
dsimp [f]
change (BitVec.ofNat 64 lowBits).toNat = lowBits
rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt]
dsimp [lowBits]
omega
have adjacent := Binary64AdjacentNormalValues e n e_lower e_upper n_lower n_upper
have decoded_real :
(let bits := lowBits
let ef := bits / (2 ^ 52 : Nat)
let mf := bits % (2 ^ 52 : Nat)
if ef = 0 then (mf : Real) / (2 : Real) ^ (1074 : Nat)
else if 1075 ≤ ef then (((2 ^ 52 : Nat) + mf : Nat) : Real) * (2 : Real) ^ (ef - 1075)
else (((2 ^ 52 : Nat) + mf : Nat) : Real) / (2 : Real) ^ (1075 - ef)) =
(x : Real) := by
calc
_ = (n : Real) * (2 : Real) ^ ((e : Int) - 1075) := adjacent.1
_ = (z : Real) * (2 : Real) ^ ((e : Int) - 1075) := by
rw [hn]
norm_num
_ = (x : Real) := by
dsimp [z]
push_cast
rw [mul_assoc, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
have hz : ((1075 : Int) - (e : Int)) + ((e : Int) - 1075) = 0 := by ring
rw [hz]
norm_num
have decoded :
(let bits := f.val
let ef := bits / (2 ^ 52 : Nat)
let mf := bits % (2 ^ 52 : Nat)
if ef = 0 then (mf : Rat) / (2 : Rat) ^ (1074 : Nat)
else if 1075 ≤ ef then (((2 ^ 52 : Nat) + mf : Nat) : Rat) * (2 : Rat) ^ (ef - 1075)
else (((2 ^ 52 : Nat) + mf : Nat) : Rat) / (2 : Rat) ^ (1075 - ef)) = x := by
apply Rat.cast_injective (α := Real)
rw [flow]
by_cases hez : lowBits / (2 ^ 52 : Nat) = 0
· rw [if_pos hez] at decoded_real ⊢
push_cast
simpa only [Rat.cast_natCast] using decoded_real
· rw [if_neg hez] at decoded_real ⊢
by_cases heh : 1075 ≤ lowBits / (2 ^ 52 : Nat)
· rw [if_pos heh] at decoded_real ⊢
push_cast
simpa only [Rat.cast_natCast, Nat.cast_add] using decoded_real
· rw [if_neg heh] at decoded_real ⊢
push_cast
simpa only [Rat.cast_natCast, Nat.cast_add] using decoded_real
have hround := Binary64NormalEncoderRounds x hxlow hxhigh
have hroundf : DecimalGoalVocabularyDefault.roundsMagnitude (v.val : Real) f := by
change DecimalGoalVocabularyDefault.roundsMagnitude (x : Real)
(⟨BitVec.ofNat 64
(if (if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) = 2 ^ 53
then (e + 1) * 2 ^ 52
else e * 2 ^ 52 +
((if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) - 2 ^ 52))⟩)
at hround
rw [hn] at hround
have nne : n ≠ 2 ^ 53 := by omega
simp only [Rat.num_natCast, Rat.den_natCast, Int.natAbs_natCast,
Nat.div_one, Nat.mod_one, Nat.mul_zero, Nat.not_lt_zero, Nat.one_ne_zero,
false_and, false_or, if_false, nne] at hround
have fw : (⟨BitVec.ofNat 64 (e * 2 ^ 52 + (n - 2 ^ 52))⟩ : F64) = f := rfl
rw [fw] at hround
change DecimalGoalVocabularyDefault.roundsMagnitude (x : Real) f
exact hround
have hmodel : ModelF64FromU64 v = ok f := by
simp only [ModelF64FromU64, Binary64ModelDefault]
rw [if_pos hv]
have rational_bound : (x : Rat) ≤ 2 ^ 53 := by
dsimp [x]
exact_mod_cast hv
have overflow_false : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ x := by
intro h
have impossible : (2 ^ 1024 - 2 ^ 970 : Rat) ≤ 2 ^ 53 :=
h.trans rational_bound
set_option exponentiation.threshold 2048 in
set_option maxRecDepth 100000 in norm_num at impossible
rw [if_neg overflow_false]
rw [if_neg (not_lt_of_ge hxlow)]
change ok (⟨BitVec.ofNat 64
(if (if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) = 2 ^ 53
then (e + 1) * 2 ^ 52
else e * 2 ^ 52 +
((if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) - 2 ^ 52))⟩ : F64) =
ok f
rw [hn]
have nne' : n ≠ 2 ^ 53 := by omega
simp only [Rat.num_natCast, Rat.den_natCast, Int.natAbs_natCast,
Nat.div_one, Nat.mod_one, Nat.mul_zero, Nat.not_lt_zero, Nat.one_ne_zero,
false_and, false_or, if_false, nne']
apply congrArg ok
change (⟨BitVec.ofNat 64 (e * 2 ^ 52 + (n - 2 ^ 52))⟩ : F64) = f
rfl
have hfdEq : fd = f := by
have hEq : ok f = ok fd := hmodel.symm.trans hfd
exact (Result.ok.inj hEq).symm
refine ⟨f, hmodel, ?_, ?_, hroundf⟩
· rw [← hfdEq]
exact hfdFinite'
· simpa only [x] using decoded
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.Binary64NormalEncoderRounds
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64ModelDefault
import Tablet.Binary64NativeCallDomain
import Tablet.DecimalGoalVocabularyDefault
import Tablet.ModelF64FromU64
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 2000000
namespace dec2flt_model
-- [TABLET NODE: Binary64FromU64Exact]
theorem Binary64FromU64Exact :
∀ v : Std.U64, v.val ≤ 2 ^ 53 →
∃ f : F64, ModelF64FromU64 v = ok f ∧ f.val < 2047 * 2 ^ 52 ∧
(let bits := f.val
let e := bits / (2 ^ 52 : Nat)
let m := bits % (2 ^ 52 : Nat)
let decoded : Rat := 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)
decoded = (v.val : Rat)) ∧
DecimalGoalVocabularyDefault.roundsMagnitude (v.val : Real) f := by
-- BODY
intro v hv
obtain ⟨fd, hfd, hfdFinite⟩ := Binary64NativeCallDomain.1 v hv
have hfdFinite' : fd.val < 2047 * 2 ^ 52 := by
norm_num at hfdFinite ⊢
exact hfdFinite
by_cases hv0 : v.val = 0
· have veq : v = 0#u64 := UScalar.eq_of_val_eq hv0
subst v
let fz : F64 := ⟨BitVec.ofNat 64 0⟩
have fzval : fz.val = 0 := rfl
have hmodel : ModelF64FromU64 0#u64 = ok fz := by
set_option exponentiation.threshold 2048 in
norm_num [ModelF64FromU64, Binary64ModelDefault, zpow_neg]
apply UScalar.eq_of_val_eq
change (BitVec.ofNat 64 0).toNat = 0
simp
have hfdEq : fd = fz := by
have hEq : ok fz = ok fd := hmodel.symm.trans hfd
exact (Result.ok.inj hEq).symm
refine ⟨fz, hmodel, ?_, ?_, ?_⟩
· rw [← hfdEq]
exact hfdFinite'
· simp [fzval]
dsimp [DecimalGoalVocabularyDefault]
rw [hv0, fzval]
simp only [Nat.zero_div, Nat.zero_mod, Nat.cast_zero, zero_div, if_true]
change 0 ≤ (0 : Real) ∧ 0 ≤ 9218868437227405312 ∧
(if (0 : Nat) = 9218868437227405312 then
(2 : Real) ^ 1024 - (2 : Real) ^ 970 ≤ 0
else (0 : Real) < (2 : Real) ^ 1024 - (2 : Real) ^ 970 ∧
∀ w : F64, w.val < 9218868437227405312 → w ≠ fz →
abs (0 - 0) ≤
abs (0 - (let e := w.val / 4503599627370496
let m := w.val % 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))) ∧
(abs (0 - 0) =
abs (0 - (let e := w.val / 4503599627370496
let m := w.val % 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))) →
True))
refine ⟨by norm_num, by norm_num, ?_⟩
rw [if_neg (by norm_num)]
refine ⟨?_, ?_⟩
· set_option exponentiation.threshold 2048 in
norm_num
· intro w hw hne
have value_nonnegative :
0 ≤ (let e := w.val / 4503599627370496
let m := w.val % 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)) := by
dsimp only
split <;> positivity
constructor
· simpa [abs_of_nonneg value_nonnegative] using value_nonnegative
· intro _
trivial
· let x : Rat := v.val
have hxlow : (2 : Rat) ^ (-1022 : Int) ≤ x := by
have hone : (1 : Rat) ≤ x := by
change (1 : Rat) ≤ (v.val : Rat)
exact_mod_cast (Nat.one_le_iff_ne_zero.mpr hv0)
apply le_trans _ hone
rw [zpow_neg]
exact (inv_le_one₀ (pow_pos (by norm_num : (0 : Rat) < 2) _)).2
(one_le_pow₀ (by norm_num))
have hxhigh : x < (2 : Rat) ^ (128 : Nat) := by
have hcast : x ≤ (2 ^ 53 : Rat) := by
change (v.val : Rat) ≤ (2 ^ 53 : Rat)
exact_mod_cast hv
apply hcast.trans_lt
exact pow_lt_pow_right₀ (by norm_num) (by omega)
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 => simpa [scan] using hxlow
| succ n ih =>
rw [scan_formula]
dsimp [step]
split
· assumption
· exact ih
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 hn0 : n = 0
· subst n
simp [scan, step]
· dsimp [step]
split <;> omega
have last_threshold :
¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤ x) := by
intro h
have hlarge : (2 : Rat) ^ (128 : Nat) < (2 : Rat) ^ (1023 : Nat) :=
pow_lt_pow_right₀ (by norm_num) (by omega)
exact (not_lt_of_ge h) (hxhigh.trans hlarge)
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 e_small : e ≤ 1076 := by
by_contra h
have hp : (2 : Rat) ^ (54 : Int) ≤ (2 : Rat) ^ ((e : Int) - 1023) :=
zpow_le_zpow_right₀ (by norm_num) (by omega)
have hm : (2 : Rat) ^ (54 : Int) ≤ x := hp.trans current_threshold
have hxle : x ≤ (2 : Rat) ^ (53 : Nat) := by
change (v.val : Rat) ≤ (2 ^ 53 : Rat)
exact_mod_cast hv
have himpossible : (2 : Rat) ^ (54 : Nat) ≤ (2 : Rat) ^ (53 : Nat) :=
hm.trans hxle
norm_num [pow_succ] at himpossible
have z_exact : ∃ n : Nat, z = (n : Rat) := by
by_cases he : e ≤ 1075
· refine ⟨v.val * 2 ^ (1075 - e), ?_⟩
dsimp [z, x]
rw [show (1075 : Int) - (e : Int) = ((1075 - e : Nat) : Int) by omega,
zpow_natCast]
norm_num [Nat.cast_mul, Nat.cast_pow]
· have ee : e = 1076 := by omega
have hm : v.val = 2 ^ 53 := by
have lower : (2 ^ 53 : Rat) ≤ x := by simpa [ee] using current_threshold
have lower_nat : 2 ^ 53 ≤ v.val := by
change (2 ^ 53 : Rat) ≤ (v.val : Rat) at lower
exact_mod_cast lower
exact le_antisymm hv lower_nat
refine ⟨2 ^ 52, ?_⟩
dsimp [z, x]
rw [ee, hm]
norm_num [zpow_neg, div_eq_mul_inv]
obtain ⟨n, hn⟩ := z_exact
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 n_lower : 2 ^ 52 ≤ n := by
rw [hn] at z_lower
exact_mod_cast z_lower
have n_upper : n < 2 ^ 53 := by
rw [hn] at z_upper
exact_mod_cast z_upper
let lowBits : Nat := e * 2 ^ 52 + (n - 2 ^ 52)
let f : F64 := ⟨BitVec.ofNat 64 lowBits⟩
have flow : f.val = lowBits := by
dsimp [f]
change (BitVec.ofNat 64 lowBits).toNat = lowBits
rw [BitVec.toNat_ofNat, Nat.mod_eq_of_lt]
dsimp [lowBits]
omega
have adjacent := Binary64AdjacentNormalValues e n e_lower e_upper n_lower n_upper
have decoded_real :
(let bits := lowBits
let ef := bits / (2 ^ 52 : Nat)
let mf := bits % (2 ^ 52 : Nat)
if ef = 0 then (mf : Real) / (2 : Real) ^ (1074 : Nat)
else if 1075 ≤ ef then (((2 ^ 52 : Nat) + mf : Nat) : Real) * (2 : Real) ^ (ef - 1075)
else (((2 ^ 52 : Nat) + mf : Nat) : Real) / (2 : Real) ^ (1075 - ef)) =
(x : Real) := by
calc
_ = (n : Real) * (2 : Real) ^ ((e : Int) - 1075) := adjacent.1
_ = (z : Real) * (2 : Real) ^ ((e : Int) - 1075) := by
rw [hn]
norm_num
_ = (x : Real) := by
dsimp [z]
push_cast
rw [mul_assoc, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
have hz : ((1075 : Int) - (e : Int)) + ((e : Int) - 1075) = 0 := by ring
rw [hz]
norm_num
have decoded :
(let bits := f.val
let ef := bits / (2 ^ 52 : Nat)
let mf := bits % (2 ^ 52 : Nat)
if ef = 0 then (mf : Rat) / (2 : Rat) ^ (1074 : Nat)
else if 1075 ≤ ef then (((2 ^ 52 : Nat) + mf : Nat) : Rat) * (2 : Rat) ^ (ef - 1075)
else (((2 ^ 52 : Nat) + mf : Nat) : Rat) / (2 : Rat) ^ (1075 - ef)) = x := by
apply Rat.cast_injective (α := Real)
rw [flow]
by_cases hez : lowBits / (2 ^ 52 : Nat) = 0
· rw [if_pos hez] at decoded_real ⊢
push_cast
simpa only [Rat.cast_natCast] using decoded_real
· rw [if_neg hez] at decoded_real ⊢
by_cases heh : 1075 ≤ lowBits / (2 ^ 52 : Nat)
· rw [if_pos heh] at decoded_real ⊢
push_cast
simpa only [Rat.cast_natCast, Nat.cast_add] using decoded_real
· rw [if_neg heh] at decoded_real ⊢
push_cast
simpa only [Rat.cast_natCast, Nat.cast_add] using decoded_real
have hround := Binary64NormalEncoderRounds x hxlow hxhigh
have hroundf : DecimalGoalVocabularyDefault.roundsMagnitude (v.val : Real) f := by
change DecimalGoalVocabularyDefault.roundsMagnitude (x : Real)
(⟨BitVec.ofNat 64
(if (if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) = 2 ^ 53
then (e + 1) * 2 ^ 52
else e * 2 ^ 52 +
((if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) - 2 ^ 52))⟩)
at hround
rw [hn] at hround
have nne : n ≠ 2 ^ 53 := by omega
simp only [Rat.num_natCast, Rat.den_natCast, Int.natAbs_natCast,
Nat.div_one, Nat.mod_one, Nat.mul_zero, Nat.not_lt_zero, Nat.one_ne_zero,
false_and, false_or, if_false, nne] at hround
have fw : (⟨BitVec.ofNat 64 (e * 2 ^ 52 + (n - 2 ^ 52))⟩ : F64) = f := rfl
rw [fw] at hround
change DecimalGoalVocabularyDefault.roundsMagnitude (x : Real) f
exact hround
have hmodel : ModelF64FromU64 v = ok f := by
simp only [ModelF64FromU64, Binary64ModelDefault]
rw [if_pos hv]
have rational_bound : (x : Rat) ≤ 2 ^ 53 := by
dsimp [x]
exact_mod_cast hv
have overflow_false : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ x := by
intro h
have impossible : (2 ^ 1024 - 2 ^ 970 : Rat) ≤ 2 ^ 53 :=
h.trans rational_bound
set_option exponentiation.threshold 2048 in
set_option maxRecDepth 100000 in norm_num at impossible
rw [if_neg overflow_false]
rw [if_neg (not_lt_of_ge hxlow)]
change ok (⟨BitVec.ofNat 64
(if (if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) = 2 ^ 53
then (e + 1) * 2 ^ 52
else e * 2 ^ 52 +
((if z.den < 2 * (z.num.natAbs % z.den) ∨
(z.den = 2 * (z.num.natAbs % z.den) ∧
(z.num.natAbs / z.den) % 2 = 1)
then z.num.natAbs / z.den + 1 else z.num.natAbs / z.den) - 2 ^ 52))⟩ : F64) =
ok f
rw [hn]
have nne' : n ≠ 2 ^ 53 := by omega
simp only [Rat.num_natCast, Rat.den_natCast, Int.natAbs_natCast,
Nat.div_one, Nat.mod_one, Nat.mul_zero, Nat.not_lt_zero, Nat.one_ne_zero,
false_and, false_or, if_false, nne']
apply congrArg ok
change (⟨BitVec.ofNat 64 (e * 2 ^ 52 + (n - 2 ^ 52))⟩ : F64) = f
rfl
have hfdEq : fd = f := by
have hEq : ok f = ok fd := hmodel.symm.trans hfd
exact (Result.ok.inj hEq).symm
refine ⟨f, hmodel, ?_, ?_, hroundf⟩
· rw [← hfdEq]
exact hfdFinite'
· simpa only [x] using decoded
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Inspect related source (45)
- from_u64 · dec2flt/mod.rs:263 Program Rust
- pow10_fast_path · dec2flt/mod.rs:222 Program Rust
- BiasedFp · dec2flt/common.rs:52 Program Rust
More program dependencies (12)
- Decimal · dec2flt/decimal.rs:32 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- DecimalSeq · dec2flt/decimal_seq.rs:21 Program Rust
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
Standard-library dependencies (30)
- clone · core/src/clone.rs:698 Rust library
- div · core/src/ops/arith.rs:515 Rust library
- mul · core/src/ops/arith.rs:350 Rust library
- neg · core/src/ops/arith.rs:719 Rust library
- bitand · core/src/ops/bit.rs:178 Rust library
- bitor · core/src/ops/bit.rs:282 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- shr · core/src/ops/bit.rs:612 Rust library
- EXPONENT_MASK · core/src/num/f64.rs:615 Rust library
- INFINITY · core/src/num/f64.rs:512 Rust library
- MANTISSA_DIGITS · core/src/num/f64.rs:412 Rust library
- MANTISSA_MASK · core/src/num/f64.rs:637 Rust library
- NAN · core/src/num/f64.rs:509 Rust library
- NEG_INFINITY · core/src/num/f64.rs:515 Rust library
- classify · core/src/num/f64.rs:782 Rust library
- from_bits · core/src/num/f64.rs:1269 Rust library
- to_bits · core/src/num/f64.rs:1223 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 Rust library
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- first · core/src/slice/mod.rs:155 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
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
- Classical.choice
- Quot.sound
- propext
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
- Binary64AdjacentNormalValues
- Binary64EncoderCarryArithmetic
- Binary64FiniteMagnitudeStrictMono
- Binary64FromU64Exact
- Binary64Model
- Binary64ModelDefault
All 41 entries
- Binary64AdjacentNormalValues
- Binary64EncoderCarryArithmetic
- Binary64FiniteMagnitudeStrictMono
- Binary64FromU64Exact
- Binary64Model
- Binary64ModelDefault
- Binary64NativeCallDomain
- Binary64NormalEncoderRounds
- DecimalGoalVocabulary
- DecimalGoalVocabularyDefault
- DecimalLexeme
- F64
- F64 Insts Dec2flt modelNumImpDec2fltLemireU64 pow10 fast path
- ModelF64Classify
- ModelF64Clone
- ModelF64Div
- ModelF64ExponentMask
- ModelF64FromBits
- ModelF64FromU64
- ModelF64Infinity
- ModelF64MantissaDigits
- ModelF64MantissaMask
- ModelF64Mul
- ModelF64Nan
- ModelF64Neg
- ModelF64NegInfinity
- ModelF64ToBits
- ModelOptionBranch
- ModelOptionFromResidual
- ModelSliceFirst
- ModelSliceSplitFirst
- ModelStrAsBytes
- ModelU64BitAnd
- ModelU64BitOr
- ModelU64Shl
- ModelU64Shr
- ModelU8IsAsciiDigit
- Preamble
- core num FpCategory
- num imp dec2flt common BiasedFp
- num imp dec2flt decimal Decimal
Technical checking records
Local closure summary
{
"node": "Binary64FromU64Exact",
"closure_version": "patch_c_v1",
"active_decl_hash": "1d496a0136a92d511085e2779648f7a7394fb9908a0bc5edda1787b3296cf7b1",
"active_statement_hash": "d700989c64e1d91e7f7587d093a727cfcdbca1f55d0ce02db43f67c938a9807a",
"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",
"Binary64ModelDefault": "c722a2c00805b545",
"Binary64NativeCallDomain": "bb13d93a3c739925",
"Binary64NormalEncoderRounds": "e7e60775e2c3fcdc",
"DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
},
"strict_definition_deps": {
"Binary64Model": "c98c2c511cf0ced6",
"Binary64ModelDefault": "dc548d1e27696ba4",
"DecimalGoalVocabulary": "f76c8174a4d58eb5",
"DecimalGoalVocabularyDefault": "512f8d0aea894250",
"F64": "ec4e25fbaf4e11d4",
"F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_pow10_fast_path": "c0ce6fdd5caeca22",
"ModelF64FromU64": "97a26f5bc593ff7a",
"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:c31d719156b99265aa946e27c795d91411c35d4586405857ab51b7ed4c60bd88",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"substantiveness_status": "Pass"
}