Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Binary64 grid certificate and assembly
assembledWord formula. Assume A\ge0 and that one of the following
three alternatives holds:
- r=T and H\le A;
- there is a natural number j such that\begin{gathered} j\le2^{53},\qquad r=j,\qquad A\le2^{53}2^{-1074},\\ A\le j2^{-1074}+2^{-1075},\qquad j2^{-1074}\le A+2^{-1075}, \end{gathered}and\bigl(A=(j+\tfrac12)2^{-1074}\ \lor\ A=(j-\tfrac12)2^{-1074}\bigr) \quad\Longrightarrow\quad j\bmod 2=0;
- there are natural numbers p,j such that2\le p\le2046,\qquad 2^{52}\le j\le2^{53},\qquad (p<2046\ \lor\ j<2^{53}),r=pU+(j-U),\qquad \Delta=2^{p-1075},U\Delta\le A,\qquad A\le2^{53}\Delta,\qquad A<H,A\le j\Delta+\frac{\Delta}{2},\qquad j\Delta\le A+\frac{\Delta}{2},and\bigl(A=(j+\tfrac12)\Delta\ \lor\ A=(j-\tfrac12)\Delta\bigr) \quad\Longrightarrow\quad j\bmod 2=0.
Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.NearestEvenOrderedGrid
import Tablet.num_imp_dec2flt_common_BiasedFp
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 1000000
namespace dec2flt_model
-- [TABLET NODE: Binary64NearestEvenAssembly]
theorem Binary64NearestEvenAssembly :
forall (A : Real) (fp : num.imp.dec2flt.common.BiasedFp) (w : F64),
let unit : Nat := 2 ^ 52
let infinityBits : Nat := 2047 * unit
let threshold : Real := (2 : Real) ^ (1024 : Nat) -
(2 : Real) ^ (970 : Nat)
let raw : Nat := Nat.lor fp.m.val
(((fp.p_biased.val.emod (2 ^ 64)).toNat * unit) % (2 ^ 64))
(0 <= A /\
((raw = infinityBits /\ threshold <= A) \/
(exists j : Nat,
j <= 2 ^ 53 /\ raw = j /\
A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\
A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) +
(2 : Real) ^ (-(1075 : Int)) /\
(j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A +
(2 : Real) ^ (-(1075 : Int)) /\
(A = ((j : Real) + (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) \/
A = ((j : Real) - (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/
(exists p j : Nat,
2 <= p /\ p <= 2046 /\
2 ^ 52 <= j /\ j <= 2 ^ 53 /\
(p < 2046 \/ j < 2 ^ 53) /\
raw = p * unit + (j - unit) /\
let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
(unit : Real) * delta <= A /\
A <= (2 ^ 53 : Nat) * delta /\
A < threshold /\
A <= (j : Real) * delta + delta / 2 /\
(j : Real) * delta <= A + delta / 2 /\
(A = ((j : Real) + (1 / 2 : Real)) * delta \/
A = ((j : Real) - (1 / 2 : Real)) * delta -> j % 2 = 0)))) ->
DecimalGoalVocabularyDefault.assembledWord fp w ->
DecimalGoalVocabularyDefault.roundsMagnitude A 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
roundsMagnitude.roundsMagnitude(A,w).Lean proof
intro A fp w
dsimp only
intro hcert hasm
let unitDef : Nat := 2 ^ 52
generalize hunitDef : unitDef = unit
have unit_eq : unit = 2 ^ 52 := by rw [← hunitDef]
have unit_pos : 0 < unit := by rw [unit_eq]; positivity
let top : Nat := 2047 * (2 ^ 52)
let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat)
let rawDef : Nat := Nat.lor fp.m.val
(((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64))
let valueDef : Nat → Real := fun n =>
let e := n / (2 ^ 52 : Nat)
let m := n % (2 ^ 52 : Nat)
if e = 0 then (m : Real) / (2 : Real) ^ 1074
else if 1075 ≤ e then
(((2 ^ 52 : Nat) + m : Nat) : Real) * (2 : Real) ^ (e - 1075)
else (((2 ^ 52 : Nat) + m : Nat) : Real) / (2 : Real) ^ (1075 - e)
have wraw : w.val = rawDef := by
dsimp [DecimalGoalVocabularyDefault] at hasm
simpa only [rawDef, unit_eq] using hasm
dsimp [DecimalGoalVocabularyDefault]
rw [wraw]
change 0 ≤ A ∧ rawDef ≤ top ∧
(if rawDef = top then threshold ≤ A
else A < threshold ∧ ∀ v : F64, v.val < top → v ≠ w →
abs (A - valueDef rawDef) ≤ abs (A - valueDef v.val) ∧
(abs (A - valueDef rawDef) = abs (A - valueDef v.val) → rawDef % 2 = 0))
generalize hrawDef : rawDef = raw at wraw ⊢
generalize hvalueDef : valueDef = value at ⊢
have value_mono : ∀ a b : Nat, a < b → b < top → value a < value b := by as_aux_lemma =>
rw [← hvalueDef]
simpa only [valueDef, top, unit_eq] using Binary64FiniteMagnitudeStrictMono
have low_threshold : ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ (-(1074 : Int)) <
threshold := by as_aux_lemma =>
have hsmall : ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ (-(1074 : Int)) < 1 := by
rw [show ((2 ^ 53 : Nat) : Real) = (2 : Real) ^ (53 : Int) by norm_num,
← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
apply inv_lt_one_of_one_lt₀
norm_num
have hone : (1 : Real) < threshold := by
have hp : (0 : Real) < 2 ^ 970 := by positivity
have hpow : (2 : Real) ^ 970 < 2 ^ 1024 :=
pow_lt_pow_right₀ (by norm_num) (by omega)
dsimp only [threshold]
set_option exponentiation.threshold 2048 in
have hone' : (1 : Real) ≤ 2 ^ 970 := by
exact_mod_cast (show (1 : Nat) ≤ 2 ^ 970 by omega)
nlinarith
exact hsmall.trans hone
rcases hcert with ⟨hA, hover | hfinite⟩
· rcases hover with ⟨hraw, hthreshold⟩
have hraw' : raw = top := by
rw [← hrawDef]
simpa only [rawDef, top, unit_eq] using hraw
refine ⟨hA, hraw'.le, ?_⟩
rw [if_pos hraw']
simpa only [threshold] using hthreshold
· rcases hfinite with hlow | hnormal
· rcases hlow with ⟨j, hj, hraw, hbound, hhi, hlo, htie⟩
have hraw' : raw = j := by
rw [← hrawDef]
simpa only [rawDef, unit_eq] using hraw
have hjtop : j < top := by
dsimp [top]
omega
have hbelow : A < threshold := hbound.trans_lt low_threshold
let d : Real := (2 : Real) ^ (-(1074 : Int))
have hd : 0 < d := by
dsimp [d]
exact zpow_pos (by norm_num) _
have half_d : d / 2 = (2 : Real) ^ (-(1075 : Int)) := by
calc
d / 2 = d * (2 : Real) ^ (-(1 : Int)) := by
rw [zpow_neg]
norm_num [div_eq_mul_inv]
_ = (2 : Real) ^ (-(1075 : Int)) := by
dsimp only [d]
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have double_d : 2 * d = (2 : Real) ^ (-(1073 : Int)) := by
calc
2 * d = (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d]
norm_num
_ = (2 : Real) ^ (-(1073 : Int)) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have sub_value : ∀ k : Nat, k < unit → value k = (k : Real) * d := by as_aux_lemma =>
intro k hk
rw [← hvalueDef]
have hk' : k < 4503599627370496 := by
norm_num [unit_eq] at hk ⊢
exact hk
have hdiv : k / 4503599627370496 = 0 := Nat.div_eq_of_lt hk'
have hmod : k % 4503599627370496 = k := Nat.mod_eq_of_lt hk'
dsimp [valueDef, d]
rw [hdiv, hmod, if_pos rfl, zpow_neg]
exact div_eq_mul_inv _ _
have one_adj : ∀ q : Nat, unit ≤ q → q < 2 ^ 53 →
value q = (q : Real) * d ∧ value (q + 1) = ((q + 1 : Nat) : Real) * d := by as_aux_lemma =>
intro q hq0 hq1
rw [← hvalueDef]
have hadj := Binary64AdjacentNormalValues 1 q (by omega) (by omega)
(by simpa only [unit_eq] using hq0) hq1
change valueDef (1 * (2 ^ 52) + (q - 2 ^ 52)) = (q : Real) * d ∧
valueDef (1 * (2 ^ 52) + (q - 2 ^ 52) + 1) = ((q + 1 : Nat) : Real) * d at hadj
have hbits : 1 * unit + (q - unit) = q := by omega
constructor
· calc
valueDef q = valueDef (1 * (2 ^ 52) + (q - 2 ^ 52)) := by
apply congrArg valueDef
simpa only [unit_eq] using hbits.symm
_ = (q : Real) * d := hadj.1
· calc
valueDef (q + 1) = valueDef (1 * (2 ^ 52) + (q - 2 ^ 52) + 1) := by
apply congrArg valueDef
simpa only [unit_eq] using congrArg (fun n : Nat => n + 1) hbits.symm
_ = ((q + 1 : Nat) : Real) * d := hadj.2
have hval' : value raw = (j : Real) * d := by as_aux_lemma =>
rw [hraw']
by_cases hsub : j < unit
· exact sub_value j hsub
· have hju : unit ≤ j := le_of_not_gt hsub
by_cases hend : j = 2 ^ 53
· have hadj := (one_adj (j - 1) (by omega) (by omega)).2
have hidx : j - 1 + 1 = j := by omega
rw [hidx] at hadj
exact hadj
· exact (one_adj j hju (by omega)).1
have hprev : raw = 0 ∨ value (raw - 1) = (j : Real) * d - d := by as_aux_lemma =>
by_cases hj0 : j = 0
· left
omega
· right
rw [hraw']
by_cases hjle : j ≤ unit
· rw [sub_value (j - 1) (by omega)]
rw [Nat.cast_sub (by omega)]
push_cast
ring
· have hadj := (one_adj (j - 1) (by omega) (by omega)).1
rw [hadj, Nat.cast_sub (by omega)]
push_cast
ring
have hrawtop : raw < top := by omega
by_cases hend : j = 2 ^ 53
· have hnext : raw + 1 = top ∨ value (raw + 1) = (j : Real) * d + 2 * d := by as_aux_lemma =>
right
rw [← hvalueDef]
rw [hraw']
rw [hend]
have hadj := (Binary64AdjacentNormalValues 2 (2 ^ 52)
(by omega) (by omega) (by omega) (by norm_num)).2
change valueDef (2 * (2 ^ 52) + (2 ^ 52 - 2 ^ 52) + 1) =
((2 ^ 52 + 1 : Nat) : Real) * (2 : Real) ^ (-(1073 : Int)) at hadj
have hbits : 2 * unit + (2 ^ 52 - unit) + 1 = 2 ^ 53 + 1 := by
norm_num [unit_eq]
rw [← hbits]
calc
valueDef (2 * unit + (2 ^ 52 - unit) + 1) =
((2 ^ 52 + 1 : Nat) : Real) * (2 : Real) ^ (-(1073 : Int)) := by
simpa only [unit_eq] using hadj
_ = ((2 ^ 53 : Nat) : Real) * d + 2 * d := by
rw [← double_d]
norm_num [unit_eq]
ring
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * d) d (2 * d)
value_mono hrawtop hval' hd (by positivity)
(by simpa only [half_d] using hlo)
(by have : A ≤ (j : Real) * d := by
rw [hend]
simpa only [d] using hbound
linarith)
hprev hnext
(by
intro hmid
have hcertmid :
A = ((j : Real) - (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d] at hmid ⊢
linarith
have hjeven := htie (Or.inr hcertmid)
simpa only [hraw'] using hjeven)
(by
intro hmid
have : A ≤ (j : Real) * d := by
rw [hend]
simpa only [d] using hbound
linarith)
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨hbelow, ?_⟩
intro v hv hne
have hvne : v.val ≠ raw := by
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
exact hordered v.val hv hvne
· have hjlt : j < 2 ^ 53 := by omega
have hnext : raw + 1 = top ∨ value (raw + 1) = (j : Real) * d + d := by as_aux_lemma =>
right
rw [hraw']
by_cases hnextsub : j + 1 < unit
· rw [sub_value (j + 1) hnextsub]
push_cast
ring
· by_cases hsub : j < unit
· have hju : j + 1 = unit := by omega
rw [hju]
rw [(one_adj unit (by omega) (by norm_num [unit_eq])).1]
rw [← hju]
push_cast
ring
· have hju : unit ≤ j := le_of_not_gt hsub
rw [(one_adj j hju hjlt).2]
push_cast
ring
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * d) d d
value_mono hrawtop hval' hd hd (by simpa only [half_d] using hlo)
(by simpa only [half_d] using hhi) hprev hnext
(by
intro hmid
have hcertmid :
A = ((j : Real) - (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d] at hmid ⊢
linarith
have hjeven := htie (Or.inr hcertmid)
simpa only [hraw'] using hjeven)
(by
intro hmid
have hcertmid :
A = ((j : Real) + (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d] at hmid ⊢
linarith
have hjeven := htie (Or.inl hcertmid)
simpa only [hraw'] using hjeven)
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨hbelow, ?_⟩
intro v hv hne
have hvne : v.val ≠ raw := by
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
exact hordered v.val hv hvne
· rcases hnormal with
⟨p, j, hp0, hp1, hj0, hj1, hedge, hraw, hbinlo, hbinhi, hbelow,
hhi, hlo, htie⟩
have hraw' : raw = p * unit + (j - unit) := by
rw [← hrawDef]
simpa only [rawDef, unit_eq] using hraw
let deltaDef : Real := (2 : Real) ^ ((p : Int) - 1075)
generalize hdeltaDef : deltaDef = delta
have delta_eq : delta = (2 : Real) ^ ((p : Int) - 1075) := by
rw [← hdeltaDef]
have hdelta : 0 < delta := by
rw [delta_eq]
exact zpow_pos (by norm_num) _
have normal_at : ∀ e q : Nat, 1 ≤ e → e ≤ 2046 → unit ≤ q → q < 2 ^ 53 →
value (e * unit + (q - unit)) =
(q : Real) * (2 : Real) ^ ((e : Int) - 1075) := by as_aux_lemma =>
intro e q he0 he1 hq0 hq1
rw [unit_eq] at hq0 ⊢
rw [← hvalueDef]
by_cases htop : e = 2046
· subst e
change valueDef (2046 * (2 ^ 52) + (q - 2 ^ 52)) =
(q : Real) * (2 : Real) ^ ((2046 : Int) - 1075)
have hrem : q - unit < unit := by omega
have hdiv : (2046 * unit + (q - unit)) / unit = 2046 := by
rw [Nat.mul_comm 2046 unit, Nat.mul_add_div unit_pos]
simp [Nat.div_eq_of_lt hrem]
have hmod : (2046 * unit + (q - unit)) % unit = q - unit := by
rw [Nat.mul_comm 2046 unit, Nat.mul_add_mod]
simp [Nat.mod_eq_of_lt hrem]
have hsum : unit + (q - unit) = q := by omega
dsimp [valueDef]
norm_num [unit_eq] at hdiv hmod
rw [hdiv, hmod, if_neg (by omega), if_pos (by omega)]
have hsumReal : (4503599627370496 : Real) +
((q - 4503599627370496 : Nat) : Real) = (q : Real) := by
norm_num [unit_eq] at hsum
exact_mod_cast hsum
push_cast
rw [hsumReal]
rfl
· have hadj := (Binary64AdjacentNormalValues e q he0 (by omega)
(by simpa only [unit_eq] using hq0) hq1).1
change valueDef (e * (2 ^ 52) + (q - 2 ^ 52)) =
(q : Real) * (2 : Real) ^ ((e : Int) - 1075) at hadj
simpa only [unit_eq] using hadj
have half_scale : (2 : Real) ^ (((p - 1 : Nat) : Int) - 1075) = delta / 2 := by
have hpcast : ((p - 1 : Nat) : Int) = (p : Int) - 1 := by omega
rw [hpcast]
calc
(2 : Real) ^ ((p : Int) - 1 - 1075) =
(2 : Real) ^ ((p : Int) - 1075) * (2 : Real) ^ (-(1 : Int)) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
congr 1
ring
_ = delta / 2 := by
rw [delta_eq]
rw [zpow_neg]
norm_num [div_eq_mul_inv]
have double_scale : (2 : Real) ^ (((p + 1 : Nat) : Int) - 1075) =
2 * delta := by
calc
(2 : Real) ^ (((p + 1 : Nat) : Int) - 1075) =
(2 : Real) ^ (1 : Int) * (2 : Real) ^ ((p : Int) - 1075) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
congr 1
push_cast
ring
_ = 2 * delta := by rw [delta_eq]; norm_num
have hrawtop : raw < top := by
rw [hraw']
dsimp [top]
rcases hedge with hpedge | hjedge <;> omega
have hparity : raw % 2 = j % 2 := by as_aux_lemma =>
rw [hraw']
norm_num [unit_eq]
omega
by_cases hjmin : j = unit
· have hval : value raw = (j : Real) * delta := by as_aux_lemma =>
rw [hraw']
simpa only [delta_eq] using normal_at p j (by omega) hp1
(by simpa only [unit_eq] using hj0) (by omega)
have hprev : raw = 0 ∨ value (raw - 1) =
(j : Real) * delta - (delta / 2) := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at (p - 1) (2 ^ 53 - 1) (by omega) (by omega)
(by norm_num [unit_eq]) (by omega)
have hbits : p * unit + (j - unit) - 1 =
(p - 1) * unit + ((2 ^ 53 - 1) - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, half_scale, hjmin]
push_cast
norm_num [unit_eq]
ring
have hnext : raw + 1 = top ∨ value (raw + 1) =
(j : Real) * delta + delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at p (unit + 1) (by omega) hp1 (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) + 1 =
p * unit + ((unit + 1) - unit) := by omega
rw [hbits, hv, hjmin]
rw [← delta_eq]
push_cast
ring
have hlo' : (j : Real) * delta ≤ A + (delta / 2) / 2 := by
have hy : (j : Real) * delta ≤ A := by
rw [hjmin]
simpa only [delta_eq, unit_eq] using hbinlo
linarith [hdelta]
have htieLow : A = (j : Real) * delta - (delta / 2) / 2 → raw % 2 = 0 := by
intro hmid
have hy : (j : Real) * delta ≤ A := by
rw [hjmin]
simpa only [delta_eq, unit_eq] using hbinlo
linarith [hdelta]
have htieHigh : A = (j : Real) * delta + delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
left
rw [delta_eq] at hmid
linarith
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * delta)
(delta / 2) delta value_mono hrawtop hval (by positivity) hdelta hlo'
(by simpa only [delta_eq] using hhi) hprev hnext htieLow htieHigh
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨by simpa only [threshold] using hbelow, ?_⟩
intro v hv hne
apply hordered v.val hv
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
· by_cases hjend : j = 2 ^ 53
· have hpnext : p + 1 ≤ 2046 := by omega
have hval : value raw = (j : Real) * delta := by as_aux_lemma =>
rw [hraw']
have hv := normal_at (p + 1) unit (by omega) hpnext (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) = (p + 1) * unit + (unit - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, double_scale, hjend]
norm_num [unit_eq]
ring
have hprev : raw = 0 ∨ value (raw - 1) =
(j : Real) * delta - delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at p (j - 1) (by omega) hp1 (by omega) (by omega)
have hbits : p * unit + (j - unit) - 1 =
p * unit + ((j - 1) - unit) := by omega
rw [hbits, hv, Nat.cast_sub (by omega)]
rw [← delta_eq]
push_cast
ring
have hnext : raw + 1 = top ∨ value (raw + 1) =
(j : Real) * delta + 2 * delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at (p + 1) (unit + 1) (by omega) hpnext (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) + 1 =
(p + 1) * unit + ((unit + 1) - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, double_scale, hjend]
push_cast
norm_num [unit_eq]
ring
have hhi' : A ≤ (j : Real) * delta + (2 * delta) / 2 := by
have hy : A ≤ (j : Real) * delta := by
rw [hjend]
simpa only [delta_eq] using hbinhi
linarith [hdelta]
have htieLow : A = (j : Real) * delta - delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
right
rw [delta_eq] at hmid
linarith
have htieHigh : A = (j : Real) * delta + (2 * delta) / 2 → raw % 2 = 0 := by
intro hmid
have hy : A ≤ (j : Real) * delta := by
rw [hjend]
simpa only [delta_eq] using hbinhi
linarith [hdelta]
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * delta)
delta (2 * delta) value_mono hrawtop hval hdelta (by positivity)
(by simpa only [delta_eq] using hlo) hhi' hprev hnext htieLow htieHigh
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨by simpa only [threshold] using hbelow, ?_⟩
intro v hv hne
apply hordered v.val hv
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
· have hval : value raw = (j : Real) * delta := by as_aux_lemma =>
rw [hraw']
simpa only [delta_eq] using normal_at p j (by omega) hp1
(by simpa only [unit_eq] using hj0) (by omega)
have hprev : raw = 0 ∨ value (raw - 1) =
(j : Real) * delta - delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at p (j - 1) (by omega) hp1 (by omega) (by omega)
have hbits : p * unit + (j - unit) - 1 =
p * unit + ((j - 1) - unit) := by omega
rw [hbits, hv, Nat.cast_sub (by omega)]
rw [← delta_eq]
push_cast
ring
have hnext : raw + 1 = top ∨ value (raw + 1) =
(j : Real) * delta + delta := by as_aux_lemma =>
by_cases hlast : raw + 1 = top
· exact Or.inl hlast
· right
by_cases hcarry : j + 1 = 2 ^ 53
· rw [hraw']
have hpnext : p + 1 ≤ 2046 := by
by_contra hn
have hp2046 : p = 2046 := by omega
apply hlast
rw [hraw', hp2046]
dsimp [top]
rw [unit_eq]
omega
have hv := normal_at (p + 1) unit (by omega) hpnext (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) + 1 =
(p + 1) * unit + (unit - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, double_scale]
have hjreal : (j : Real) = 2 * (unit : Real) - 1 := by
have := congrArg (fun n : Nat => (n : Real)) hcarry
push_cast at this
norm_num [unit_eq] at this ⊢
linarith
rw [hjreal]
ring
· rw [hraw']
have hv := normal_at p (j + 1) (by omega) hp1 (by omega) (by omega)
have hbits : p * unit + (j - unit) + 1 =
p * unit + ((j + 1) - unit) := by omega
rw [hbits, hv]
rw [← delta_eq]
push_cast
ring
have htieLow : A = (j : Real) * delta - delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
right
rw [delta_eq] at hmid
linarith
have htieHigh : A = (j : Real) * delta + delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
left
rw [delta_eq] at hmid
linarith
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * delta)
delta delta value_mono hrawtop hval hdelta hdelta
(by simpa only [delta_eq] using hlo) (by simpa only [delta_eq] using hhi)
hprev hnext htieLow htieHigh
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨by simpa only [threshold] using hbelow, ?_⟩
intro v hv hne
apply hordered v.val hv
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Binary64AdjacentNormalValues
import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.NearestEvenOrderedGrid
import Tablet.num_imp_dec2flt_common_BiasedFp
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 1000000
namespace dec2flt_model
-- [TABLET NODE: Binary64NearestEvenAssembly]
theorem Binary64NearestEvenAssembly :
forall (A : Real) (fp : num.imp.dec2flt.common.BiasedFp) (w : F64),
let unit : Nat := 2 ^ 52
let infinityBits : Nat := 2047 * unit
let threshold : Real := (2 : Real) ^ (1024 : Nat) -
(2 : Real) ^ (970 : Nat)
let raw : Nat := Nat.lor fp.m.val
(((fp.p_biased.val.emod (2 ^ 64)).toNat * unit) % (2 ^ 64))
(0 <= A /\
((raw = infinityBits /\ threshold <= A) \/
(exists j : Nat,
j <= 2 ^ 53 /\ raw = j /\
A <= (2 ^ 53 : Nat) * (2 : Real) ^ (-(1074 : Int)) /\
A <= (j : Real) * (2 : Real) ^ (-(1074 : Int)) +
(2 : Real) ^ (-(1075 : Int)) /\
(j : Real) * (2 : Real) ^ (-(1074 : Int)) <= A +
(2 : Real) ^ (-(1075 : Int)) /\
(A = ((j : Real) + (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) \/
A = ((j : Real) - (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) -> j % 2 = 0)) \/
(exists p j : Nat,
2 <= p /\ p <= 2046 /\
2 ^ 52 <= j /\ j <= 2 ^ 53 /\
(p < 2046 \/ j < 2 ^ 53) /\
raw = p * unit + (j - unit) /\
let delta : Real := (2 : Real) ^ ((p : Int) - 1075)
(unit : Real) * delta <= A /\
A <= (2 ^ 53 : Nat) * delta /\
A < threshold /\
A <= (j : Real) * delta + delta / 2 /\
(j : Real) * delta <= A + delta / 2 /\
(A = ((j : Real) + (1 / 2 : Real)) * delta \/
A = ((j : Real) - (1 / 2 : Real)) * delta -> j % 2 = 0)))) ->
DecimalGoalVocabularyDefault.assembledWord fp w ->
DecimalGoalVocabularyDefault.roundsMagnitude A w := by
-- BODY
intro A fp w
dsimp only
intro hcert hasm
let unitDef : Nat := 2 ^ 52
generalize hunitDef : unitDef = unit
have unit_eq : unit = 2 ^ 52 := by rw [← hunitDef]
have unit_pos : 0 < unit := by rw [unit_eq]; positivity
let top : Nat := 2047 * (2 ^ 52)
let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat)
let rawDef : Nat := Nat.lor fp.m.val
(((fp.p_biased.val.emod (2 ^ 64)).toNat * (2 ^ 52)) % (2 ^ 64))
let valueDef : Nat → Real := fun n =>
let e := n / (2 ^ 52 : Nat)
let m := n % (2 ^ 52 : Nat)
if e = 0 then (m : Real) / (2 : Real) ^ 1074
else if 1075 ≤ e then
(((2 ^ 52 : Nat) + m : Nat) : Real) * (2 : Real) ^ (e - 1075)
else (((2 ^ 52 : Nat) + m : Nat) : Real) / (2 : Real) ^ (1075 - e)
have wraw : w.val = rawDef := by
dsimp [DecimalGoalVocabularyDefault] at hasm
simpa only [rawDef, unit_eq] using hasm
dsimp [DecimalGoalVocabularyDefault]
rw [wraw]
change 0 ≤ A ∧ rawDef ≤ top ∧
(if rawDef = top then threshold ≤ A
else A < threshold ∧ ∀ v : F64, v.val < top → v ≠ w →
abs (A - valueDef rawDef) ≤ abs (A - valueDef v.val) ∧
(abs (A - valueDef rawDef) = abs (A - valueDef v.val) → rawDef % 2 = 0))
generalize hrawDef : rawDef = raw at wraw ⊢
generalize hvalueDef : valueDef = value at ⊢
have value_mono : ∀ a b : Nat, a < b → b < top → value a < value b := by as_aux_lemma =>
rw [← hvalueDef]
simpa only [valueDef, top, unit_eq] using Binary64FiniteMagnitudeStrictMono
have low_threshold : ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ (-(1074 : Int)) <
threshold := by as_aux_lemma =>
have hsmall : ((2 ^ 53 : Nat) : Real) * (2 : Real) ^ (-(1074 : Int)) < 1 := by
rw [show ((2 ^ 53 : Nat) : Real) = (2 : Real) ^ (53 : Int) by norm_num,
← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
apply inv_lt_one_of_one_lt₀
norm_num
have hone : (1 : Real) < threshold := by
have hp : (0 : Real) < 2 ^ 970 := by positivity
have hpow : (2 : Real) ^ 970 < 2 ^ 1024 :=
pow_lt_pow_right₀ (by norm_num) (by omega)
dsimp only [threshold]
set_option exponentiation.threshold 2048 in
have hone' : (1 : Real) ≤ 2 ^ 970 := by
exact_mod_cast (show (1 : Nat) ≤ 2 ^ 970 by omega)
nlinarith
exact hsmall.trans hone
rcases hcert with ⟨hA, hover | hfinite⟩
· rcases hover with ⟨hraw, hthreshold⟩
have hraw' : raw = top := by
rw [← hrawDef]
simpa only [rawDef, top, unit_eq] using hraw
refine ⟨hA, hraw'.le, ?_⟩
rw [if_pos hraw']
simpa only [threshold] using hthreshold
· rcases hfinite with hlow | hnormal
· rcases hlow with ⟨j, hj, hraw, hbound, hhi, hlo, htie⟩
have hraw' : raw = j := by
rw [← hrawDef]
simpa only [rawDef, unit_eq] using hraw
have hjtop : j < top := by
dsimp [top]
omega
have hbelow : A < threshold := hbound.trans_lt low_threshold
let d : Real := (2 : Real) ^ (-(1074 : Int))
have hd : 0 < d := by
dsimp [d]
exact zpow_pos (by norm_num) _
have half_d : d / 2 = (2 : Real) ^ (-(1075 : Int)) := by
calc
d / 2 = d * (2 : Real) ^ (-(1 : Int)) := by
rw [zpow_neg]
norm_num [div_eq_mul_inv]
_ = (2 : Real) ^ (-(1075 : Int)) := by
dsimp only [d]
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have double_d : 2 * d = (2 : Real) ^ (-(1073 : Int)) := by
calc
2 * d = (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d]
norm_num
_ = (2 : Real) ^ (-(1073 : Int)) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have sub_value : ∀ k : Nat, k < unit → value k = (k : Real) * d := by as_aux_lemma =>
intro k hk
rw [← hvalueDef]
have hk' : k < 4503599627370496 := by
norm_num [unit_eq] at hk ⊢
exact hk
have hdiv : k / 4503599627370496 = 0 := Nat.div_eq_of_lt hk'
have hmod : k % 4503599627370496 = k := Nat.mod_eq_of_lt hk'
dsimp [valueDef, d]
rw [hdiv, hmod, if_pos rfl, zpow_neg]
exact div_eq_mul_inv _ _
have one_adj : ∀ q : Nat, unit ≤ q → q < 2 ^ 53 →
value q = (q : Real) * d ∧ value (q + 1) = ((q + 1 : Nat) : Real) * d := by as_aux_lemma =>
intro q hq0 hq1
rw [← hvalueDef]
have hadj := Binary64AdjacentNormalValues 1 q (by omega) (by omega)
(by simpa only [unit_eq] using hq0) hq1
change valueDef (1 * (2 ^ 52) + (q - 2 ^ 52)) = (q : Real) * d ∧
valueDef (1 * (2 ^ 52) + (q - 2 ^ 52) + 1) = ((q + 1 : Nat) : Real) * d at hadj
have hbits : 1 * unit + (q - unit) = q := by omega
constructor
· calc
valueDef q = valueDef (1 * (2 ^ 52) + (q - 2 ^ 52)) := by
apply congrArg valueDef
simpa only [unit_eq] using hbits.symm
_ = (q : Real) * d := hadj.1
· calc
valueDef (q + 1) = valueDef (1 * (2 ^ 52) + (q - 2 ^ 52) + 1) := by
apply congrArg valueDef
simpa only [unit_eq] using congrArg (fun n : Nat => n + 1) hbits.symm
_ = ((q + 1 : Nat) : Real) * d := hadj.2
have hval' : value raw = (j : Real) * d := by as_aux_lemma =>
rw [hraw']
by_cases hsub : j < unit
· exact sub_value j hsub
· have hju : unit ≤ j := le_of_not_gt hsub
by_cases hend : j = 2 ^ 53
· have hadj := (one_adj (j - 1) (by omega) (by omega)).2
have hidx : j - 1 + 1 = j := by omega
rw [hidx] at hadj
exact hadj
· exact (one_adj j hju (by omega)).1
have hprev : raw = 0 ∨ value (raw - 1) = (j : Real) * d - d := by as_aux_lemma =>
by_cases hj0 : j = 0
· left
omega
· right
rw [hraw']
by_cases hjle : j ≤ unit
· rw [sub_value (j - 1) (by omega)]
rw [Nat.cast_sub (by omega)]
push_cast
ring
· have hadj := (one_adj (j - 1) (by omega) (by omega)).1
rw [hadj, Nat.cast_sub (by omega)]
push_cast
ring
have hrawtop : raw < top := by omega
by_cases hend : j = 2 ^ 53
· have hnext : raw + 1 = top ∨ value (raw + 1) = (j : Real) * d + 2 * d := by as_aux_lemma =>
right
rw [← hvalueDef]
rw [hraw']
rw [hend]
have hadj := (Binary64AdjacentNormalValues 2 (2 ^ 52)
(by omega) (by omega) (by omega) (by norm_num)).2
change valueDef (2 * (2 ^ 52) + (2 ^ 52 - 2 ^ 52) + 1) =
((2 ^ 52 + 1 : Nat) : Real) * (2 : Real) ^ (-(1073 : Int)) at hadj
have hbits : 2 * unit + (2 ^ 52 - unit) + 1 = 2 ^ 53 + 1 := by
norm_num [unit_eq]
rw [← hbits]
calc
valueDef (2 * unit + (2 ^ 52 - unit) + 1) =
((2 ^ 52 + 1 : Nat) : Real) * (2 : Real) ^ (-(1073 : Int)) := by
simpa only [unit_eq] using hadj
_ = ((2 ^ 53 : Nat) : Real) * d + 2 * d := by
rw [← double_d]
norm_num [unit_eq]
ring
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * d) d (2 * d)
value_mono hrawtop hval' hd (by positivity)
(by simpa only [half_d] using hlo)
(by have : A ≤ (j : Real) * d := by
rw [hend]
simpa only [d] using hbound
linarith)
hprev hnext
(by
intro hmid
have hcertmid :
A = ((j : Real) - (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d] at hmid ⊢
linarith
have hjeven := htie (Or.inr hcertmid)
simpa only [hraw'] using hjeven)
(by
intro hmid
have : A ≤ (j : Real) * d := by
rw [hend]
simpa only [d] using hbound
linarith)
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨hbelow, ?_⟩
intro v hv hne
have hvne : v.val ≠ raw := by
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
exact hordered v.val hv hvne
· have hjlt : j < 2 ^ 53 := by omega
have hnext : raw + 1 = top ∨ value (raw + 1) = (j : Real) * d + d := by as_aux_lemma =>
right
rw [hraw']
by_cases hnextsub : j + 1 < unit
· rw [sub_value (j + 1) hnextsub]
push_cast
ring
· by_cases hsub : j < unit
· have hju : j + 1 = unit := by omega
rw [hju]
rw [(one_adj unit (by omega) (by norm_num [unit_eq])).1]
rw [← hju]
push_cast
ring
· have hju : unit ≤ j := le_of_not_gt hsub
rw [(one_adj j hju hjlt).2]
push_cast
ring
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * d) d d
value_mono hrawtop hval' hd hd (by simpa only [half_d] using hlo)
(by simpa only [half_d] using hhi) hprev hnext
(by
intro hmid
have hcertmid :
A = ((j : Real) - (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d] at hmid ⊢
linarith
have hjeven := htie (Or.inr hcertmid)
simpa only [hraw'] using hjeven)
(by
intro hmid
have hcertmid :
A = ((j : Real) + (1 / 2 : Real)) *
(2 : Real) ^ (-(1074 : Int)) := by
dsimp only [d] at hmid ⊢
linarith
have hjeven := htie (Or.inl hcertmid)
simpa only [hraw'] using hjeven)
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨hbelow, ?_⟩
intro v hv hne
have hvne : v.val ≠ raw := by
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
exact hordered v.val hv hvne
· rcases hnormal with
⟨p, j, hp0, hp1, hj0, hj1, hedge, hraw, hbinlo, hbinhi, hbelow,
hhi, hlo, htie⟩
have hraw' : raw = p * unit + (j - unit) := by
rw [← hrawDef]
simpa only [rawDef, unit_eq] using hraw
let deltaDef : Real := (2 : Real) ^ ((p : Int) - 1075)
generalize hdeltaDef : deltaDef = delta
have delta_eq : delta = (2 : Real) ^ ((p : Int) - 1075) := by
rw [← hdeltaDef]
have hdelta : 0 < delta := by
rw [delta_eq]
exact zpow_pos (by norm_num) _
have normal_at : ∀ e q : Nat, 1 ≤ e → e ≤ 2046 → unit ≤ q → q < 2 ^ 53 →
value (e * unit + (q - unit)) =
(q : Real) * (2 : Real) ^ ((e : Int) - 1075) := by as_aux_lemma =>
intro e q he0 he1 hq0 hq1
rw [unit_eq] at hq0 ⊢
rw [← hvalueDef]
by_cases htop : e = 2046
· subst e
change valueDef (2046 * (2 ^ 52) + (q - 2 ^ 52)) =
(q : Real) * (2 : Real) ^ ((2046 : Int) - 1075)
have hrem : q - unit < unit := by omega
have hdiv : (2046 * unit + (q - unit)) / unit = 2046 := by
rw [Nat.mul_comm 2046 unit, Nat.mul_add_div unit_pos]
simp [Nat.div_eq_of_lt hrem]
have hmod : (2046 * unit + (q - unit)) % unit = q - unit := by
rw [Nat.mul_comm 2046 unit, Nat.mul_add_mod]
simp [Nat.mod_eq_of_lt hrem]
have hsum : unit + (q - unit) = q := by omega
dsimp [valueDef]
norm_num [unit_eq] at hdiv hmod
rw [hdiv, hmod, if_neg (by omega), if_pos (by omega)]
have hsumReal : (4503599627370496 : Real) +
((q - 4503599627370496 : Nat) : Real) = (q : Real) := by
norm_num [unit_eq] at hsum
exact_mod_cast hsum
push_cast
rw [hsumReal]
rfl
· have hadj := (Binary64AdjacentNormalValues e q he0 (by omega)
(by simpa only [unit_eq] using hq0) hq1).1
change valueDef (e * (2 ^ 52) + (q - 2 ^ 52)) =
(q : Real) * (2 : Real) ^ ((e : Int) - 1075) at hadj
simpa only [unit_eq] using hadj
have half_scale : (2 : Real) ^ (((p - 1 : Nat) : Int) - 1075) = delta / 2 := by
have hpcast : ((p - 1 : Nat) : Int) = (p : Int) - 1 := by omega
rw [hpcast]
calc
(2 : Real) ^ ((p : Int) - 1 - 1075) =
(2 : Real) ^ ((p : Int) - 1075) * (2 : Real) ^ (-(1 : Int)) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
congr 1
ring
_ = delta / 2 := by
rw [delta_eq]
rw [zpow_neg]
norm_num [div_eq_mul_inv]
have double_scale : (2 : Real) ^ (((p + 1 : Nat) : Int) - 1075) =
2 * delta := by
calc
(2 : Real) ^ (((p + 1 : Nat) : Int) - 1075) =
(2 : Real) ^ (1 : Int) * (2 : Real) ^ ((p : Int) - 1075) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
congr 1
push_cast
ring
_ = 2 * delta := by rw [delta_eq]; norm_num
have hrawtop : raw < top := by
rw [hraw']
dsimp [top]
rcases hedge with hpedge | hjedge <;> omega
have hparity : raw % 2 = j % 2 := by as_aux_lemma =>
rw [hraw']
norm_num [unit_eq]
omega
by_cases hjmin : j = unit
· have hval : value raw = (j : Real) * delta := by as_aux_lemma =>
rw [hraw']
simpa only [delta_eq] using normal_at p j (by omega) hp1
(by simpa only [unit_eq] using hj0) (by omega)
have hprev : raw = 0 ∨ value (raw - 1) =
(j : Real) * delta - (delta / 2) := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at (p - 1) (2 ^ 53 - 1) (by omega) (by omega)
(by norm_num [unit_eq]) (by omega)
have hbits : p * unit + (j - unit) - 1 =
(p - 1) * unit + ((2 ^ 53 - 1) - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, half_scale, hjmin]
push_cast
norm_num [unit_eq]
ring
have hnext : raw + 1 = top ∨ value (raw + 1) =
(j : Real) * delta + delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at p (unit + 1) (by omega) hp1 (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) + 1 =
p * unit + ((unit + 1) - unit) := by omega
rw [hbits, hv, hjmin]
rw [← delta_eq]
push_cast
ring
have hlo' : (j : Real) * delta ≤ A + (delta / 2) / 2 := by
have hy : (j : Real) * delta ≤ A := by
rw [hjmin]
simpa only [delta_eq, unit_eq] using hbinlo
linarith [hdelta]
have htieLow : A = (j : Real) * delta - (delta / 2) / 2 → raw % 2 = 0 := by
intro hmid
have hy : (j : Real) * delta ≤ A := by
rw [hjmin]
simpa only [delta_eq, unit_eq] using hbinlo
linarith [hdelta]
have htieHigh : A = (j : Real) * delta + delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
left
rw [delta_eq] at hmid
linarith
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * delta)
(delta / 2) delta value_mono hrawtop hval (by positivity) hdelta hlo'
(by simpa only [delta_eq] using hhi) hprev hnext htieLow htieHigh
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨by simpa only [threshold] using hbelow, ?_⟩
intro v hv hne
apply hordered v.val hv
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
· by_cases hjend : j = 2 ^ 53
· have hpnext : p + 1 ≤ 2046 := by omega
have hval : value raw = (j : Real) * delta := by as_aux_lemma =>
rw [hraw']
have hv := normal_at (p + 1) unit (by omega) hpnext (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) = (p + 1) * unit + (unit - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, double_scale, hjend]
norm_num [unit_eq]
ring
have hprev : raw = 0 ∨ value (raw - 1) =
(j : Real) * delta - delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at p (j - 1) (by omega) hp1 (by omega) (by omega)
have hbits : p * unit + (j - unit) - 1 =
p * unit + ((j - 1) - unit) := by omega
rw [hbits, hv, Nat.cast_sub (by omega)]
rw [← delta_eq]
push_cast
ring
have hnext : raw + 1 = top ∨ value (raw + 1) =
(j : Real) * delta + 2 * delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at (p + 1) (unit + 1) (by omega) hpnext (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) + 1 =
(p + 1) * unit + ((unit + 1) - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, double_scale, hjend]
push_cast
norm_num [unit_eq]
ring
have hhi' : A ≤ (j : Real) * delta + (2 * delta) / 2 := by
have hy : A ≤ (j : Real) * delta := by
rw [hjend]
simpa only [delta_eq] using hbinhi
linarith [hdelta]
have htieLow : A = (j : Real) * delta - delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
right
rw [delta_eq] at hmid
linarith
have htieHigh : A = (j : Real) * delta + (2 * delta) / 2 → raw % 2 = 0 := by
intro hmid
have hy : A ≤ (j : Real) * delta := by
rw [hjend]
simpa only [delta_eq] using hbinhi
linarith [hdelta]
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * delta)
delta (2 * delta) value_mono hrawtop hval hdelta (by positivity)
(by simpa only [delta_eq] using hlo) hhi' hprev hnext htieLow htieHigh
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨by simpa only [threshold] using hbelow, ?_⟩
intro v hv hne
apply hordered v.val hv
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
· have hval : value raw = (j : Real) * delta := by as_aux_lemma =>
rw [hraw']
simpa only [delta_eq] using normal_at p j (by omega) hp1
(by simpa only [unit_eq] using hj0) (by omega)
have hprev : raw = 0 ∨ value (raw - 1) =
(j : Real) * delta - delta := by as_aux_lemma =>
right
rw [hraw']
have hv := normal_at p (j - 1) (by omega) hp1 (by omega) (by omega)
have hbits : p * unit + (j - unit) - 1 =
p * unit + ((j - 1) - unit) := by omega
rw [hbits, hv, Nat.cast_sub (by omega)]
rw [← delta_eq]
push_cast
ring
have hnext : raw + 1 = top ∨ value (raw + 1) =
(j : Real) * delta + delta := by as_aux_lemma =>
by_cases hlast : raw + 1 = top
· exact Or.inl hlast
· right
by_cases hcarry : j + 1 = 2 ^ 53
· rw [hraw']
have hpnext : p + 1 ≤ 2046 := by
by_contra hn
have hp2046 : p = 2046 := by omega
apply hlast
rw [hraw', hp2046]
dsimp [top]
rw [unit_eq]
omega
have hv := normal_at (p + 1) unit (by omega) hpnext (by omega)
(by norm_num [unit_eq])
have hbits : p * unit + (j - unit) + 1 =
(p + 1) * unit + (unit - unit) := by
rw [unit_eq]
omega
rw [hbits, hv, double_scale]
have hjreal : (j : Real) = 2 * (unit : Real) - 1 := by
have := congrArg (fun n : Nat => (n : Real)) hcarry
push_cast at this
norm_num [unit_eq] at this ⊢
linarith
rw [hjreal]
ring
· rw [hraw']
have hv := normal_at p (j + 1) (by omega) hp1 (by omega) (by omega)
have hbits : p * unit + (j - unit) + 1 =
p * unit + ((j + 1) - unit) := by omega
rw [hbits, hv]
rw [← delta_eq]
push_cast
ring
have htieLow : A = (j : Real) * delta - delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
right
rw [delta_eq] at hmid
linarith
have htieHigh : A = (j : Real) * delta + delta / 2 → raw % 2 = 0 := by
intro hmid
rw [hparity]
apply htie
left
rw [delta_eq] at hmid
linarith
have hordered := NearestEvenOrderedGrid value top raw A ((j : Real) * delta)
delta delta value_mono hrawtop hval hdelta hdelta
(by simpa only [delta_eq] using hlo) (by simpa only [delta_eq] using hhi)
hprev hnext htieLow htieHigh
refine ⟨hA, hrawtop.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨by simpa only [threshold] using hbelow, ?_⟩
intro v hv hne
apply hordered v.val hv
intro heq
apply hne
apply UScalar.eq_of_val_eq
simpa only [wraw] using heq
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Inspect related source (44)
- BiasedFp · dec2flt/common.rs:52 Program Rust
- Decimal · dec2flt/decimal.rs:32 Program Rust
- from_u64 · dec2flt/mod.rs:263 Program Rust
More program dependencies (11)
- 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
Technical checking records
Local closure summary
{
"node": "Binary64NearestEvenAssembly",
"closure_version": "patch_c_v1",
"active_decl_hash": "7441342dda3be78f637905bba8af49dc045c0635291a03caba78dad1bf3bc994",
"active_statement_hash": "587b6ab853269d9aa8e8549f21b6b44b114adc369ca81d95fc77774c59440ce6",
"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-173",
"boundary_theorems": {},
"strict_theorem_deps": {
"Binary64AdjacentNormalValues": "8495001b4edafedb",
"Binary64FiniteMagnitudeStrictMono": "ea630fa702fb7b61",
"DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b",
"NearestEvenOrderedGrid": "f9128c4eca75845c"
},
"strict_definition_deps": {
"DecimalGoalVocabulary": "f76c8174a4d58eb5",
"DecimalGoalVocabularyDefault": "512f8d0aea894250",
"F64": "ec4e25fbaf4e11d4",
"num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2"
}
}Prose assessment
{
"label": "Recorded Sound: VerifierPass",
"assessment_status": "VerifierPass",
"fingerprint_relation": "matches current fingerprint",
"assessment_fingerprint": "sound-v2:fd05f5389bf18a18f4b3f2ab489f3ab4316289b82cbffd1abcbc8c290fb90dc8",
"current_fingerprint": "sound-v2:fd05f5389bf18a18f4b3f2ab489f3ab4316289b82cbffd1abcbc8c290fb90dc8",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}