Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma
from_u64 call with input at most 2^{53} succeeds
with a positive-sign finite, non-NaN binary64 word. For every
pow10_fast_path index from 0 through 22, the call succeeds
with a nonzero positive-sign finite, non-NaN word.Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.ModelF64FromU64
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_pow10_fast_path
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: Binary64NativeCallDomain]
theorem Binary64NativeCallDomain :
And
(forall v : Std.U64, v.val <= 2 ^ 53 ->
exists f : F64,
And (ModelF64FromU64 v = ok f)
(f.val < 0x7ff0000000000000))
(forall i : Std.Usize, i.val <= 22 ->
exists p : F64,
And
(F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.pow10_fast_path i = ok p)
(And (0 < p.val) (p.val < 0x7ff0000000000000))) := 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
from_bits operation on its corresponding
w_k. By ModelF64FromBits, that call succeeds and returns a value
whose inspected raw word is exactly w_k. Applying the two checked
inequalities to that returned word supplies the required nonzero positive-sign
finite, non-NaN result. Since the unchanged mask selects one of precisely
these 23 branches whenever the original index is at most 22, these are all
cases under the stated hypothesis.Lean proof
constructor
· intro v hv
unfold ModelF64FromU64 Binary64ModelDefault
have input_bound : (v.val : Nat) ≤ 2 ^ 53 := by exact hv
simp only [if_pos (by omega)]
refine ⟨_, rfl, ?_⟩
have rational_bound : (↑(v.val) : Rat) ≤ 2 ^ 53 := by exact_mod_cast input_bound
have nearest_bound : ∀ q : Rat, 0 ≤ q → q < (2 ^ 53 : Rat) →
(if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) ≤ 2 ^ 53 := by
intro q nonnegative small
have numerator_nonnegative : 0 ≤ q.num := Rat.num_nonneg.mpr nonnegative
have cross_rational := (Rat.lt_iff q (2 ^ 53 : Rat)).mp small
have cross_natural : q.num.natAbs < 2 ^ 53 * q.den := by
have absolute_value : (q.num.natAbs : Int) = q.num :=
Int.natAbs_of_nonneg numerator_nonnegative
have cross_absolute : (q.num.natAbs : Int) < (2 ^ 53 : Int) * q.den := by
simpa [absolute_value] using cross_rational
exact_mod_cast cross_absolute
have quotient_small : q.num.natAbs / q.den < 2 ^ 53 :=
(Nat.div_lt_iff_lt_mul q.den_pos).mpr cross_natural
split <;> omega
have overflow_false : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ ↑(v.val) := 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
simp only [if_neg overflow_false]
by_cases tiny : (↑(v.val) : Rat) < 2 ^ (-1022 : Int)
· simp only [if_pos tiny]
have q_nonnegative :
0 ≤ (↑(v.val) : Rat) * 2 ^ (1074 : Nat) := by positivity
have q_small :
(↑(v.val) : Rat) * 2 ^ (1074 : Nat) < (2 ^ 53 : Rat) := by
calc
(↑(v.val) : Rat) * 2 ^ (1074 : Nat) <
2 ^ (-1022 : Int) * 2 ^ (1074 : Nat) := by
exact mul_lt_mul_of_pos_right tiny (by positivity)
_ = 2 ^ 52 := by
norm_num [zpow_neg, div_eq_mul_inv]
rw [mul_comm, ← div_eq_mul_inv]
apply (div_eq_iff (by positivity : (2 ^ 1022 : Rat) ≠ 0)).mpr
set_option exponentiation.threshold 2048 in norm_num
_ < 2 ^ 53 := by norm_num [pow_succ]
have rounded_small := nearest_bound
((↑(v.val) : Rat) * 2 ^ (1074 : Nat)) q_nonnegative q_small
let q : Rat := (↑(v.val) : Rat) * 2 ^ (1074 : Nat)
let rounded : Nat := if q.den <
2 * (q.num.natAbs % q.den) ∨ q.den = 2 * (q.num.natAbs % q.den) ∧
q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den
have rounded_le : rounded ≤ 2 ^ 53 := by
dsimp [rounded, q]
exact rounded_small
change (BitVec.ofNat 64 rounded).toNat < _
rw [BitVec.toNat_ofNat]
rw [Nat.mod_eq_of_lt (by omega : rounded < 2 ^ 64)]
omega
· simp only [if_neg tiny]
let step : Nat → Nat → Nat := fun old i =>
if ((2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023)) ≤ (↑(v.val) : Rat) 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
let scan_monotone : Monotone scan := monotone_nat_of_le_succ scan_mono
have step_eq :
(fun old i => if (2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023) ≤ (↑(v.val) : Rat)
then i + 1 else old) = step := by
funext old i
simp only [step]
have exponent_eq :
List.foldl (fun old i => if (2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023) ≤ (↑(v.val) : Rat)
then i + 1 else old) 1 (List.range 2046) = scan 2046 := by
calc
_ = List.foldl step 1 (List.range 2046) :=
congrArg (fun f => List.foldl f 1 (List.range 2046)) step_eq
_ = scan 2046 := rfl
set_option maxHeartbeats 800000 in rw [exponent_eq]
have scan_bound : ∀ n : Nat, 1 ≤ n → scan n ≤ n := by
intro n
induction n with
| zero => omega
| succ n ih =>
rw [scan_formula]
by_cases zero_case : n = 0
· subst zero_case
simp [scan, step]
· dsimp [step]
split <;> omega
have exponent_lower : 1 ≤ scan 2046 := scan_lower 2046
have last_threshold : ¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤
(↑(v.val) : Rat)) := by
intro threshold
have impossible := threshold.trans rational_bound
set_option exponentiation.threshold 2048 in
norm_num at impossible
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) ≤
(↑(v.val) : Rat) then 2046 else scan 2045) ≤ 2045
rw [if_neg last_threshold]
exact scan_bound 2045 (by omega)
have input_positive : (1 : Rat) ≤ (↑(v.val) : Rat) := by
have nonzero : v.val ≠ 0 := by
intro zero_case
have zero_rational : (↑(v.val) : Rat) = 0 := by exact_mod_cast zero_case
rw [zero_rational] at tiny
exact tiny (by exact zpow_pos (by norm_num) _)
exact_mod_cast (Nat.one_le_iff_ne_zero.mpr nonzero)
have no_next_threshold :
scan 2046 < 2046 →
¬ ((2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤
(↑(v.val) : Rat)) := by
intro below
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) ≤
(↑(v.val) : Rat) 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 q : Rat := (↑(v.val) : Rat) *
2 ^ ((1075 : Int) - (scan 2046 : Int))
have q_nonnegative : 0 ≤ q := by
dsimp [q]
have scale_pos : 0 < (2 : Rat) ^ ((1075 : Int) - (scan 2046 : Int)) :=
zpow_pos (by norm_num) _
exact mul_nonneg (by exact_mod_cast (Nat.zero_le v.val)) scale_pos.le
have q_small : q < (2 ^ 53 : Rat) := by
by_cases top : scan 2046 = 2046
· dsimp [q]
rw [top]
calc
(↑(v.val) : Rat) * 2 ^ (-971 : Int) ≤
(2 ^ 53 : Rat) * 2 ^ (-971 : Int) := by
exact mul_le_mul_of_nonneg_right rational_bound
(zpow_pos (by norm_num) _).le
_ < 2 ^ 53 := by
have factor : (2 : Rat) ^ (-971 : Int) < 1 := by
rw [zpow_neg]
apply inv_lt_one_of_one_lt₀
set_option exponentiation.threshold 2048 in norm_num
have product := mul_lt_mul_of_pos_left factor (by positivity : (0 : Rat) < 2 ^ 53)
simpa using product
· have below : scan 2046 < 2046 := by omega
have threshold := no_next_threshold below
have strict : (↑(v.val) : Rat) <
(2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) :=
lt_of_not_ge threshold
dsimp [q]
calc
(↑(v.val) : Rat) * 2 ^ ((1075 : Int) - (scan 2046 : Int)) <
2 ^ (((scan 2046 + 1 : Nat) : Int) - 1023) *
2 ^ ((1075 : Int) - (scan 2046 : Int)) := by
exact mul_lt_mul_of_pos_right strict
(zpow_pos (by norm_num) _)
_ = 2 ^ 53 := by
set_option maxRecDepth 100000 in
rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)]
have exponent_identity :
(((scan 2046 + 1 : Nat) : Int) - 1023) +
((1075 : Int) - (scan 2046 : Int)) = 53 := by omega
rw [exponent_identity]
rfl
have rounded_small := nearest_bound q q_nonnegative q_small
let rounded : Nat := if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den
have rounded_le : rounded ≤ 2 ^ 53 := by
dsimp [rounded]
exact rounded_small
change (BitVec.ofNat 64 (if
(if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) = 2 ^ 53 then
(scan 2046 + 1) * 2 ^ 52
else scan 2046 * 2 ^ 52 +
((if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) - 2 ^ 52))).toNat < _
rw [show (if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) = rounded from rfl]
have magnitude_bound :
(if rounded = 2 ^ 53 then (scan 2046 + 1) * 2 ^ 52
else scan 2046 * 2 ^ 52 + (rounded - 2 ^ 52)) <
2047 * 2 ^ 52 := by
by_cases carry : rounded = 2 ^ 53
· rw [if_pos carry]
exact (Nat.mul_lt_mul_right (by positivity : 0 < 2 ^ 52)).mpr (by omega)
· have rounded_lt : rounded < 2 ^ 53 := by omega
rw [if_neg carry]
omega
rw [BitVec.toNat_ofNat]
rw [Nat.mod_eq_of_lt (by omega :
(if rounded = 2 ^ 53 then (scan 2046 + 1) * 2 ^ 52
else scan 2046 * 2 ^ 52 + (rounded - 2 ^ 52)) < 2 ^ 64)]
omega
· intro i hi
unfold F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.pow10_fast_path
have mask_eq : i &&& 31#usize = i := by
apply UScalar.eq_of_val_eq
simp only [UScalar.val_and]
change i.val &&& 31 = i.val
rw [show (31 : Nat) = 2 ^ 5 - 1 by norm_num, Nat.and_two_pow_sub_one_eq_mod]
exact Nat.mod_eq_of_lt (by omega)
have cases :
i.val = 0 ∨ i.val = 1 ∨ i.val = 2 ∨ i.val = 3 ∨ i.val = 4 ∨
i.val = 5 ∨ i.val = 6 ∨ i.val = 7 ∨ i.val = 8 ∨ i.val = 9 ∨
i.val = 10 ∨ i.val = 11 ∨ i.val = 12 ∨ i.val = 13 ∨ i.val = 14 ∨
i.val = 15 ∨ i.val = 16 ∨ i.val = 17 ∨ i.val = 18 ∨ i.val = 19 ∨
i.val = 20 ∨ i.val = 21 ∨ i.val = 22 := by omega
rcases cases with h | h | h | h | h | h | h | h | h | h | h | h | h | h | h |
h | h | h | h | h | h | h | h <;>
simp [h, Aeneas.Std.lift, mask_eq, core.f64.F64.from_bits,
ModelF64FromBits] <;>
refine ⟨_, rfl, ?_⟩ <;> norm_num
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.ModelF64FromU64
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_pow10_fast_path
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: Binary64NativeCallDomain]
theorem Binary64NativeCallDomain :
And
(forall v : Std.U64, v.val <= 2 ^ 53 ->
exists f : F64,
And (ModelF64FromU64 v = ok f)
(f.val < 0x7ff0000000000000))
(forall i : Std.Usize, i.val <= 22 ->
exists p : F64,
And
(F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.pow10_fast_path i = ok p)
(And (0 < p.val) (p.val < 0x7ff0000000000000))) := by
-- BODY
constructor
· intro v hv
unfold ModelF64FromU64 Binary64ModelDefault
have input_bound : (v.val : Nat) ≤ 2 ^ 53 := by exact hv
simp only [if_pos (by omega)]
refine ⟨_, rfl, ?_⟩
have rational_bound : (↑(v.val) : Rat) ≤ 2 ^ 53 := by exact_mod_cast input_bound
have nearest_bound : ∀ q : Rat, 0 ≤ q → q < (2 ^ 53 : Rat) →
(if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) ≤ 2 ^ 53 := by
intro q nonnegative small
have numerator_nonnegative : 0 ≤ q.num := Rat.num_nonneg.mpr nonnegative
have cross_rational := (Rat.lt_iff q (2 ^ 53 : Rat)).mp small
have cross_natural : q.num.natAbs < 2 ^ 53 * q.den := by
have absolute_value : (q.num.natAbs : Int) = q.num :=
Int.natAbs_of_nonneg numerator_nonnegative
have cross_absolute : (q.num.natAbs : Int) < (2 ^ 53 : Int) * q.den := by
simpa [absolute_value] using cross_rational
exact_mod_cast cross_absolute
have quotient_small : q.num.natAbs / q.den < 2 ^ 53 :=
(Nat.div_lt_iff_lt_mul q.den_pos).mpr cross_natural
split <;> omega
have overflow_false : ¬ (2 ^ 1024 - 2 ^ 970 : Rat) ≤ ↑(v.val) := 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
simp only [if_neg overflow_false]
by_cases tiny : (↑(v.val) : Rat) < 2 ^ (-1022 : Int)
· simp only [if_pos tiny]
have q_nonnegative :
0 ≤ (↑(v.val) : Rat) * 2 ^ (1074 : Nat) := by positivity
have q_small :
(↑(v.val) : Rat) * 2 ^ (1074 : Nat) < (2 ^ 53 : Rat) := by
calc
(↑(v.val) : Rat) * 2 ^ (1074 : Nat) <
2 ^ (-1022 : Int) * 2 ^ (1074 : Nat) := by
exact mul_lt_mul_of_pos_right tiny (by positivity)
_ = 2 ^ 52 := by
norm_num [zpow_neg, div_eq_mul_inv]
rw [mul_comm, ← div_eq_mul_inv]
apply (div_eq_iff (by positivity : (2 ^ 1022 : Rat) ≠ 0)).mpr
set_option exponentiation.threshold 2048 in norm_num
_ < 2 ^ 53 := by norm_num [pow_succ]
have rounded_small := nearest_bound
((↑(v.val) : Rat) * 2 ^ (1074 : Nat)) q_nonnegative q_small
let q : Rat := (↑(v.val) : Rat) * 2 ^ (1074 : Nat)
let rounded : Nat := if q.den <
2 * (q.num.natAbs % q.den) ∨ q.den = 2 * (q.num.natAbs % q.den) ∧
q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den
have rounded_le : rounded ≤ 2 ^ 53 := by
dsimp [rounded, q]
exact rounded_small
change (BitVec.ofNat 64 rounded).toNat < _
rw [BitVec.toNat_ofNat]
rw [Nat.mod_eq_of_lt (by omega : rounded < 2 ^ 64)]
omega
· simp only [if_neg tiny]
let step : Nat → Nat → Nat := fun old i =>
if ((2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023)) ≤ (↑(v.val) : Rat) 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
let scan_monotone : Monotone scan := monotone_nat_of_le_succ scan_mono
have step_eq :
(fun old i => if (2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023) ≤ (↑(v.val) : Rat)
then i + 1 else old) = step := by
funext old i
simp only [step]
have exponent_eq :
List.foldl (fun old i => if (2 : Rat) ^ (((i + 1 : Nat) : Int) - 1023) ≤ (↑(v.val) : Rat)
then i + 1 else old) 1 (List.range 2046) = scan 2046 := by
calc
_ = List.foldl step 1 (List.range 2046) :=
congrArg (fun f => List.foldl f 1 (List.range 2046)) step_eq
_ = scan 2046 := rfl
set_option maxHeartbeats 800000 in rw [exponent_eq]
have scan_bound : ∀ n : Nat, 1 ≤ n → scan n ≤ n := by
intro n
induction n with
| zero => omega
| succ n ih =>
rw [scan_formula]
by_cases zero_case : n = 0
· subst zero_case
simp [scan, step]
· dsimp [step]
split <;> omega
have exponent_lower : 1 ≤ scan 2046 := scan_lower 2046
have last_threshold : ¬ ((2 : Rat) ^ (((2045 + 1 : Nat) : Int) - 1023) ≤
(↑(v.val) : Rat)) := by
intro threshold
have impossible := threshold.trans rational_bound
set_option exponentiation.threshold 2048 in
norm_num at impossible
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) ≤
(↑(v.val) : Rat) then 2046 else scan 2045) ≤ 2045
rw [if_neg last_threshold]
exact scan_bound 2045 (by omega)
have input_positive : (1 : Rat) ≤ (↑(v.val) : Rat) := by
have nonzero : v.val ≠ 0 := by
intro zero_case
have zero_rational : (↑(v.val) : Rat) = 0 := by exact_mod_cast zero_case
rw [zero_rational] at tiny
exact tiny (by exact zpow_pos (by norm_num) _)
exact_mod_cast (Nat.one_le_iff_ne_zero.mpr nonzero)
have no_next_threshold :
scan 2046 < 2046 →
¬ ((2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) ≤
(↑(v.val) : Rat)) := by
intro below
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) ≤
(↑(v.val) : Rat) 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 q : Rat := (↑(v.val) : Rat) *
2 ^ ((1075 : Int) - (scan 2046 : Int))
have q_nonnegative : 0 ≤ q := by
dsimp [q]
have scale_pos : 0 < (2 : Rat) ^ ((1075 : Int) - (scan 2046 : Int)) :=
zpow_pos (by norm_num) _
exact mul_nonneg (by exact_mod_cast (Nat.zero_le v.val)) scale_pos.le
have q_small : q < (2 ^ 53 : Rat) := by
by_cases top : scan 2046 = 2046
· dsimp [q]
rw [top]
calc
(↑(v.val) : Rat) * 2 ^ (-971 : Int) ≤
(2 ^ 53 : Rat) * 2 ^ (-971 : Int) := by
exact mul_le_mul_of_nonneg_right rational_bound
(zpow_pos (by norm_num) _).le
_ < 2 ^ 53 := by
have factor : (2 : Rat) ^ (-971 : Int) < 1 := by
rw [zpow_neg]
apply inv_lt_one_of_one_lt₀
set_option exponentiation.threshold 2048 in norm_num
have product := mul_lt_mul_of_pos_left factor (by positivity : (0 : Rat) < 2 ^ 53)
simpa using product
· have below : scan 2046 < 2046 := by omega
have threshold := no_next_threshold below
have strict : (↑(v.val) : Rat) <
(2 : Rat) ^ (((scan 2046 + 1 : Nat) : Int) - 1023) :=
lt_of_not_ge threshold
dsimp [q]
calc
(↑(v.val) : Rat) * 2 ^ ((1075 : Int) - (scan 2046 : Int)) <
2 ^ (((scan 2046 + 1 : Nat) : Int) - 1023) *
2 ^ ((1075 : Int) - (scan 2046 : Int)) := by
exact mul_lt_mul_of_pos_right strict
(zpow_pos (by norm_num) _)
_ = 2 ^ 53 := by
set_option maxRecDepth 100000 in
rw [← zpow_add₀ (by norm_num : (2 : Rat) ≠ 0)]
have exponent_identity :
(((scan 2046 + 1 : Nat) : Int) - 1023) +
((1075 : Int) - (scan 2046 : Int)) = 53 := by omega
rw [exponent_identity]
rfl
have rounded_small := nearest_bound q q_nonnegative q_small
let rounded : Nat := if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den
have rounded_le : rounded ≤ 2 ^ 53 := by
dsimp [rounded]
exact rounded_small
change (BitVec.ofNat 64 (if
(if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) = 2 ^ 53 then
(scan 2046 + 1) * 2 ^ 52
else scan 2046 * 2 ^ 52 +
((if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) - 2 ^ 52))).toNat < _
rw [show (if q.den < 2 * (q.num.natAbs % q.den) ∨
q.den = 2 * (q.num.natAbs % q.den) ∧ q.num.natAbs / q.den % 2 = 1 then
q.num.natAbs / q.den + 1 else q.num.natAbs / q.den) = rounded from rfl]
have magnitude_bound :
(if rounded = 2 ^ 53 then (scan 2046 + 1) * 2 ^ 52
else scan 2046 * 2 ^ 52 + (rounded - 2 ^ 52)) <
2047 * 2 ^ 52 := by
by_cases carry : rounded = 2 ^ 53
· rw [if_pos carry]
exact (Nat.mul_lt_mul_right (by positivity : 0 < 2 ^ 52)).mpr (by omega)
· have rounded_lt : rounded < 2 ^ 53 := by omega
rw [if_neg carry]
omega
rw [BitVec.toNat_ofNat]
rw [Nat.mod_eq_of_lt (by omega :
(if rounded = 2 ^ 53 then (scan 2046 + 1) * 2 ^ 52
else scan 2046 * 2 ^ 52 + (rounded - 2 ^ 52)) < 2 ^ 64)]
omega
· intro i hi
unfold F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.pow10_fast_path
have mask_eq : i &&& 31#usize = i := by
apply UScalar.eq_of_val_eq
simp only [UScalar.val_and]
change i.val &&& 31 = i.val
rw [show (31 : Nat) = 2 ^ 5 - 1 by norm_num, Nat.and_two_pow_sub_one_eq_mod]
exact Nat.mod_eq_of_lt (by omega)
have cases :
i.val = 0 ∨ i.val = 1 ∨ i.val = 2 ∨ i.val = 3 ∨ i.val = 4 ∨
i.val = 5 ∨ i.val = 6 ∨ i.val = 7 ∨ i.val = 8 ∨ i.val = 9 ∨
i.val = 10 ∨ i.val = 11 ∨ i.val = 12 ∨ i.val = 13 ∨ i.val = 14 ∨
i.val = 15 ∨ i.val = 16 ∨ i.val = 17 ∨ i.val = 18 ∨ i.val = 19 ∨
i.val = 20 ∨ i.val = 21 ∨ i.val = 22 := by omega
rcases cases with h | h | h | h | h | h | h | h | h | h | h | h | h | h | h |
h | h | h | h | h | h | h | h <;>
simp [h, Aeneas.Std.lift, mask_eq, core.f64.F64.from_bits,
ModelF64FromBits] <;>
refine ⟨_, rfl, ?_⟩ <;> norm_num
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Inspect related source (45)
- from_u64 · dec2flt/mod.rs:263 Program Rust
- pow10_fast_path · dec2flt/mod.rs:222 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
More program dependencies (12)
- 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
- BiasedFp · dec2flt/common.rs:52 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 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
- Binary64Model
- Binary64ModelDefault
- Binary64NativeCallDomain
- F64
- F64 Insts Dec2flt modelNumImpDec2fltLemireU64 pow10 fast path
- ModelF64Classify
All 31 entries
- Binary64Model
- Binary64ModelDefault
- Binary64NativeCallDomain
- 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
Technical checking records
Local closure summary
{
"node": "Binary64NativeCallDomain",
"closure_version": "patch_c_v1",
"active_decl_hash": "7fcdc8c2eebf10a98c8e9f6dfb42066b266dc76fcb6724cd2fc90724fc008685",
"active_statement_hash": "886bf16c262b27a3bfa40445def796a4eb633c2d3147ca5dfff21d8852731232",
"kernel_axioms": [
"Classical.choice",
"Quot.sound",
"propext"
],
"toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
"lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
"checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
"approved_axioms_hash": "d5c04ffa333d9b33b238a527f5beaa2be82e021d3e9c0bbe01b1310280f7f55a",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-23",
"boundary_theorems": {},
"strict_theorem_deps": {
"Binary64ModelDefault": "c722a2c00805b545",
"F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_pow10_fast_path": "440c195c8d8c288d"
},
"strict_definition_deps": {
"Binary64Model": "d5cc94c8e69bf8c6",
"Binary64ModelDefault": "47013ebe5c83894c",
"F64": "ec4e25fbaf4e11d4",
"F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_pow10_fast_path": "7494bd35549bb33e",
"ModelF64FromBits": "ff88211b2f4e3b25",
"ModelF64FromU64": "97a26f5bc593ff7a",
"Preamble": "365e22a97907c302",
"core_num_FpCategory": "f500de642a5d5148"
}
}Prose assessment
{
"label": "Recorded Sound: VerifierPass",
"assessment_status": "VerifierPass",
"fingerprint_relation": "matches current fingerprint",
"assessment_fingerprint": "sound-v2:da61f97abd13df3d4ce46f6ae2145f6132bc69a3138e1b8713a3c73ebfb7f3d3",
"current_fingerprint": "sound-v2:da61f97abd13df3d4ce46f6ae2145f6132bc69a3138e1b8713a3c73ebfb7f3d3",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}