Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma
roundsMagnitude for x in
DecimalGoalVocabularyDefault.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.Binary64FiniteMagnitudeStrictMono
import Tablet.ModelU64BitAnd
import Tablet.ModelU64Shl
import Mathlib.Tactic.FieldSimp
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 1500000
set_option maxRecDepth 1000000
namespace dec2flt_model
-- [TABLET NODE: P861_T1_IntegerSubnormalRounds]
theorem P861_T1_IntegerSubnormalRounds
(q : Std.I64) (w G64 : Std.U64) (p : Std.I32)
(G : Nat) (fp : num.imp.dec2flt.common.BiasedFp) (target : F64)
(hw : 0 < w.val) (hqlo : -342 ≤ q.val)
(hpLower : -114 ≤ p.val) (hp0 : p ≤ 0#i32)
(hGval : G64.val = G) (hGUpper : G < 2 ^ 54)
(hxcellLo : (G : Real) * (2 : Real) ^ (p.val - 1076) ≤
(w.val : Real) * (10 : Real) ^ q.val)
(hxcellHi : (w.val : Real) * (10 : Real) ^ q.val <
((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076))
(htargetAssembly : DecimalGoalVocabularyDefault.assembledWord fp target)
(hrun : (do
let negp ← -. p
let d32 ← negp + 1#i32
if d32 ≥ 64#i32 then ok { m := 0#u64, p_biased := 0#i32 }
else do
let d32' ← negp + 1#i32
let M1 ← G64 >>> d32'
let M2 ← M1 + (M1 &&& 1#u64)
let M3 ← M2 >>> 1#i32
let oneSub ← 1#u64 <<< 52#u32
ok { m := M3, p_biased :=
IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) }) = ok fp) :
DecimalGoalVocabularyDefault.roundsMagnitude
((w.val : Real) * (10 : Real) ^ q.val) target := 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.Lean proof
let x : Real := (w.val : Real) * (10 : Real) ^ q.val
have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by as_aux_lemma =>
intro α β r f y hr
cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
have andOne (z : Std.U64) : (z &&& 1#u64).val = z.val % 2 := by as_aux_lemma =>
change (z.bv &&& 1#64).toNat = z.bv.toNat % 2
rw [show (1#64) = 2#64 ^ 1 - 1#64 by decide,
BitVec.and_two_pow_sub_one_eq_mod]
simp
let unit : Nat := 2 ^ 52
let top : Nat := 2047 * unit
let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat)
let value : Nat → Real := fun n =>
let field := n / unit
let fraction := n % unit
if field = 0 then (fraction : Real) / (2 : Real) ^ (1074 : Nat)
else if 1075 ≤ field then
((unit + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075)
else ((unit + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field)
have hvalueMono : ∀ a b : Nat, a < b → b < top → value a < value b := by
simpa only [unit, top, value] using Binary64FiniteMagnitudeStrictMono
have hxpos : 0 < x := by as_aux_lemma =>
dsimp only [x]
exact mul_pos (by exact_mod_cast hw) (zpow_pos (by norm_num) _)
have honeThreshold : (1 : Real) < threshold := by as_aux_lemma =>
have hp : (0 : Real) < (2 : Real) ^ (970 : Nat) := by positivity
have heq : (2 : Real) ^ (1024 : Nat) =
(2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by
rw [← pow_add]
dsimp only [threshold]
rw [heq]
have h54 : (2 : Real) ^ (54 : Nat) = 18014398509481984 := by norm_num
rw [h54]
have hp1 : (1 : Real) ≤ (2 : Real) ^ (970 : Nat) := one_le_pow₀ (by norm_num)
rw [show 18014398509481984 * (2 : Real) ^ (970 : Nat) -
(2 : Real) ^ (970 : Nat) =
18014398509481983 * (2 : Real) ^ (970 : Nat) by ring]
calc
(1 : Real) < 18014398509481983 := by norm_num
_ ≤ 18014398509481983 * (2 : Real) ^ (970 : Nat) := by
simpa only [mul_one] using
mul_le_mul_of_nonneg_left hp1 (by norm_num : (0 : Real) ≤ 18014398509481983)
have cellRounds : ∀ (t : F64) (low bits : Nat),
t.val = bits → low + 1 < top →
value low ≤ x → x ≤ value (low + 1) →
(bits = low ∨ bits = low + 1) →
(bits = low →
abs (x - value low) ≤ abs (x - value (low + 1))) →
(bits = low + 1 →
abs (x - value (low + 1)) ≤ abs (x - value low)) →
(bits = low →
abs (x - value low) = abs (x - value (low + 1)) → bits % 2 = 0) →
(bits = low + 1 →
abs (x - value (low + 1)) = abs (x - value low) → bits % 2 = 0) →
x < threshold →
DecimalGoalVocabularyDefault.roundsMagnitude x t := by as_aux_lemma =>
intro t low bits ht hlow hxl hxh hchoice hnearLow hnearHigh
htieLow htieHigh hthreshold
dsimp only [DecimalGoalVocabularyDefault]
change 0 ≤ x ∧ t.val ≤ top ∧
(if t.val = top then threshold ≤ x else x < threshold ∧
∀ v : F64, v.val < top → v ≠ t →
abs (x - value t.val) ≤ abs (x - value v.val) ∧
(abs (x - value t.val) = abs (x - value v.val) → t.val % 2 = 0))
have hbits : bits < top := by rcases hchoice with h | h <;> omega
have htBound : t.val < top := by omega
refine ⟨hxpos.le, htBound.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨hthreshold, ?_⟩
intro v hv hne
have hvne : v.val ≠ bits := by
intro heq
apply hne
apply UScalar.eq_of_val_eq
omega
rcases hchoice with hbitsLow | hbitsHigh
· have htlow : t.val = low := ht.trans hbitsLow
rw [htlow]
have hnear := hnearLow hbitsLow
have htie := htieLow hbitsLow
by_cases hvBelow : v.val < low
· have hvValue : value v.val < value low :=
hvalueMono _ _ hvBelow (by omega)
have hvx : value v.val < x := hvValue.trans_le hxl
have hstrict : abs (x - value low) < abs (x - value v.val) := by
rw [abs_of_nonneg (sub_nonneg.mpr hxl),
abs_of_nonneg (sub_nonneg.mpr hvx.le)]
linarith
exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
· have hvAbove : low < v.val := by omega
have hhighLe : low + 1 ≤ v.val := by omega
have hhighValue : value (low + 1) ≤ value v.val := by
rcases hhighLe.eq_or_lt with heq | hlt
· exact le_of_eq (congrArg value heq)
· exact (hvalueMono _ _ hlt hv).le
have hxv : x ≤ value v.val := hxh.trans hhighValue
have hhighDist : abs (x - value (low + 1)) ≤
abs (x - value v.val) := by
rw [abs_of_nonpos (sub_nonpos.mpr hxh),
abs_of_nonpos (sub_nonpos.mpr hxv)]
linarith
constructor
· exact hnear.trans hhighDist
· intro heq
have hreverse : abs (x - value (low + 1)) ≤
abs (x - value low) := hhighDist.trans_eq heq.symm
have heqCell := le_antisymm hnear hreverse
simpa only [hbitsLow] using htie heqCell
· have hthigh : t.val = low + 1 := ht.trans hbitsHigh
rw [hthigh]
have hnear := hnearHigh hbitsHigh
have htie := htieHigh hbitsHigh
by_cases hvAbove : low + 1 < v.val
· have hvValue : value (low + 1) < value v.val :=
hvalueMono _ _ hvAbove hv
have hxv : x < value v.val := hxh.trans_lt hvValue
have hstrict : abs (x - value (low + 1)) < abs (x - value v.val) := by
rw [abs_of_nonpos (sub_nonpos.mpr hxh),
abs_of_nonpos (sub_nonpos.mpr hxv.le)]
linarith
exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
· have hvBelow : v.val < low + 1 := by omega
have hvLow : v.val ≤ low := by omega
have hvValue : value v.val ≤ value low := by
rcases hvLow.eq_or_lt with heq | hlt
· exact le_of_eq (congrArg value heq)
· exact (hvalueMono _ _ hlt (by omega)).le
have hvx : value v.val ≤ x := hvValue.trans hxl
have hlowDist : abs (x - value low) ≤ abs (x - value v.val) := by
rw [abs_of_nonneg (sub_nonneg.mpr hxl),
abs_of_nonneg (sub_nonneg.mpr hvx)]
linarith
constructor
· exact hnear.trans hlowDist
· intro heq
have hreverse : abs (x - value low) ≤
abs (x - value (low + 1)) := hlowDist.trans_eq heq.symm
have heqCell := le_antisymm hnear hreverse
simpa only [hbitsHigh] using htie heqCell
obtain ⟨negp, hnegp, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨d32, hd32, hrun⟩ := bind_ok_inv _ _ _ hrun
have hnegpVal : negp.val = -p.val := by
have hnotmin : p.val ≠ IScalar.min .I32 := by
rw [IScalar.min_I32_eq]
omega
have hs := IScalar.neg_step p hnotmin
change IScalar.neg p = ok negp at hnegp
rw [hnegp] at hs
simpa only [WP.spec_ok] using hs
have hdVal : d32.val = 1 - p.val := by
have hs := IScalar.add_equiv negp 1#i32
rw [hd32] at hs
norm_num at hs
omega
have hdPos : 0 < d32.val := by
have := (show p.val ≤ 0 by simpa only [IScalar.le_equiv] using hp0)
omega
have hpNonpos : p.val ≤ 0 := by
simpa only [IScalar.le_equiv] using hp0
have hxltOne : x < 1 := by
have hdeltaBound : (2 : Real) ^ (p.val - 1076) ≤
(2 : Real) ^ (-1076 : Int) :=
zpow_le_zpow_right₀ (by norm_num) (by omega)
have hG1 : (G + 1 : Nat) ≤ 2 ^ 54 := by omega
calc
x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := hxcellHi
_ ≤ (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (-1076 : Int) := by
exact mul_le_mul (by exact_mod_cast hG1) hdeltaBound
(zpow_pos (by norm_num) _).le (by positivity)
_ = (2 : Real) ^ (-1022 : Int) := by
rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
_ < 1 := by
rw [zpow_neg]
exact inv_lt_one_of_one_lt₀ (by norm_num)
have noOddHalf : ∀ odd : Nat, odd % 2 = 1 →
x ≠ (odd : Real) * (2 : Real) ^ (-1075 : Int) := by
intro odd hodd heq
have hqNeg : q.val < 0 := by
by_contra hn
have hq0 : 0 ≤ q.val := by omega
have hw1 : (1 : Real) ≤ w.val := by exact_mod_cast hw
have hpow1 : (1 : Real) ≤ (10 : Real) ^ q.val := by
rw [← zpow_zero (10 : Real)]
exact zpow_le_zpow_right₀ (by norm_num) hq0
have hx1 : 1 ≤ x := by
dsimp only [x]
nlinarith [mul_le_mul hw1 hpow1 (by norm_num : (0 : Real) ≤ 1)
(by positivity : (0 : Real) ≤ (w.val : Real))]
linarith
let n : Nat := (-q.val).toNat
have hqn : q.val = -(n : Int) := by
dsimp only [n]
rw [Int.toNat_of_nonneg (by omega)]
omega
have hn342 : n ≤ 342 := by omega
have hEq' : (w.val : Real) * (10 : Real) ^ (-(n : Int)) =
(odd : Real) * (2 : Real) ^ (-1075 : Int) := by
simpa only [x, hqn] using heq
rw [zpow_neg, zpow_neg] at hEq'
have hcross0 := hEq'
field_simp at hcross0
have hcross : (w.val : Real) * (2 : Real) ^ (1075 : Nat) =
(odd : Real) * (10 : Real) ^ n := by
simpa [mul_comm] using hcross0
have hcrossNat : w.val * 2 ^ 1075 = odd * 10 ^ n := by
exact_mod_cast hcross
have hten : 10 ^ n = 2 ^ n * 5 ^ n := by
rw [show (10 : Nat) = 2 * 5 by norm_num, mul_pow]
have htwo : 2 ^ 1075 = 2 ^ n * 2 ^ (1075 - n) := by
rw [← pow_add]
congr 1
omega
rw [hten, htwo] at hcrossNat
have hcancel : w.val * 2 ^ (1075 - n) = odd * 5 ^ n := by
have hc : 2 ^ n * (w.val * 2 ^ (1075 - n)) =
2 ^ n * (odd * 5 ^ n) := by
simpa [mul_assoc, mul_left_comm, mul_comm] using hcrossNat
exact Nat.eq_of_mul_eq_mul_left (by positivity) hc
have hleft : (w.val * 2 ^ (1075 - n)) % 2 = 0 := by
rw [Nat.mul_mod]
have heven : 2 ^ (1075 - n) % 2 = 0 := by
rw [show 1075 - n = (1075 - n - 1) + 1 by omega, pow_succ]
simp
simp [heven]
have hright : (odd * 5 ^ n) % 2 = 1 := by
rw [Nat.mul_mod, hodd]
have hfive : 5 ^ n % 2 = 1 := by
rw [Nat.pow_mod]
norm_num
simp [hfive]
rw [hcancel] at hleft
omega
by_cases hd64 : d32 ≥ 64#i32
· simp only [hd64, if_true] at hrun
have hfpEq : fp = { m := 0#u64, p_biased := 0#i32 } :=
(Result.ok.inj hrun).symm
have hdGe : 64 ≤ d32.val := by
simpa only [IScalar.le_equiv] using hd64
have hpLe : p.val ≤ -63 := by omega
have hdeltaBound : (2 : Real) ^ (p.val - 1076) ≤
(2 : Real) ^ (-1139 : Int) :=
zpow_le_zpow_right₀ (by norm_num) (by omega)
have hG1 : (G + 1 : Nat) ≤ 2 ^ 54 := by omega
have hxTiny : x < (2 : Real) ^ (-1085 : Int) := by
calc
x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := hxcellHi
_ ≤ (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (-1139 : Int) := by
exact mul_le_mul (by exact_mod_cast hG1) hdeltaBound
(zpow_pos (by norm_num) _).le (by positivity)
_ = (2 : Real) ^ (-1085 : Int) := by
rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have hxHalf : x < (2 : Real) ^ (-1075 : Int) :=
hxTiny.trans (zpow_lt_zpow_right₀ (by norm_num) (by omega))
subst fp
have htargetZero : target.val = 0 := by
have ha := htargetAssembly
dsimp only [DecimalGoalVocabularyDefault] at ha
norm_num [Int.toNat] at ha
exact ha
have hvalueZero : value 0 = 0 := by
dsimp only [value, unit]
norm_num
have hvalueOne : value 1 = (2 : Real) ^ (-1074 : Int) := by
dsimp only [value, unit]
norm_num
rfl
have htwiceHalf : 2 * (2 : Real) ^ (-1075 : Int) =
(2 : Real) ^ (-1074 : Int) := by
calc
2 * (2 : Real) ^ (-1075 : Int) =
(2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1075 : Int) := by norm_num
_ = (2 : Real) ^ (-1074 : Int) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have hxOne : x ≤ value 1 := by
rw [hvalueOne, ← htwiceHalf]
linarith [hxHalf]
have hxOne' : x ≤ (2 : Real) ^ (-1074 : Int) := by
simpa only [hvalueOne] using hxOne
apply cellRounds target 0 0 htargetZero
· dsimp only [top, unit]
norm_num
· rw [hvalueZero]
exact hxpos.le
· exact hxOne
· exact Or.inl rfl
· intro _
rw [hvalueZero, hvalueOne]
rw [sub_zero]
rw [abs_of_nonneg hxpos.le,
abs_of_nonpos (sub_nonpos.mpr hxOne')]
rw [← htwiceHalf]
linarith
· intro h
omega
· intro _ _
norm_num
· intro h
omega
· exact hxltOne.trans honeThreshold
· simp only [hd64, if_false] at hrun
obtain ⟨d32', hd32', hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨M1, hM1, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨M2, hM2, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨M3, hM3, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨oneSub, honeSub, hrun⟩ := bind_ok_inv _ _ _ hrun
have hdLt : d32.val < 64 := by
have hn : ¬64 ≤ d32.val := by
simpa only [IScalar.le_equiv] using hd64
omega
have hdEq : d32' = d32 := by
exact Result.ok.inj (hd32'.symm.trans hd32)
subst d32'
let dNat : Nat := d32.toNat
have hdNatCast : (dNat : Int) = d32.val := by
dsimp only [dNat]
exact Int.toNat_of_nonneg hdPos.le
have hdNatPos : 0 < dNat := by
have hi : (0 : Int) < (dNat : Int) := by
rw [hdNatCast]
exact hdPos
exact_mod_cast hi
have hM1spec := UScalar.ShiftRight_IScalar_spec G64 d32 hdPos.le hdLt
rw [hM1] at hM1spec
have hM1val : M1.val = G / 2 ^ dNat := by
rw [hGval] at hM1spec
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, dNat] using hM1spec.1
have hM2val : M2.val = M1.val + M1.val % 2 := by
have hs := UScalar.add_equiv M1 (M1 &&& 1#u64)
rw [hM2, andOne] at hs
exact hs.2.1
have hM3spec := UScalar.ShiftRight_IScalar_spec M2 (1#i32)
(by norm_num) (by norm_num)
rw [hM3] at hM3spec
have hM3val : M3.val = (M1.val + M1.val % 2) / 2 := by
have hs := hM3spec.1
rw [hM2val] at hs
simpa [Nat.shiftRight_eq_div_pow] using hs
have honeSpec := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64)
(by norm_num) (by norm_num [U64.size_eq])
rw [honeSub] at honeSpec
have honeVal : oneSub.val = unit := by
dsimp only [unit]
norm_num [WP.spec_ok, Nat.shiftLeft_eq] at honeSpec ⊢
exact honeSpec.1
have hpowDGe : 2 ≤ 2 ^ dNat := by
rw [show dNat = (dNat - 1) + 1 by omega, pow_succ]
have : 1 ≤ 2 ^ (dNat - 1) := Nat.one_le_iff_ne_zero.mpr (by positivity)
omega
have hM1Upper : M1.val < 2 ^ 53 := by
rw [hM1val]
apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ dNat)).2
calc
G < 2 ^ 54 := hGUpper
_ = 2 ^ 53 * 2 := by norm_num [pow_succ]
_ ≤ 2 ^ 53 * 2 ^ dNat := Nat.mul_le_mul_left _ hpowDGe
have hM1rem : M1.val % 2 ≤ 1 :=
Nat.le_pred_of_lt (Nat.mod_lt _ (by omega))
have hM3Upper : M3.val ≤ unit := by
rw [hM3val]
dsimp only [unit]
omega
let lowSub : Nat := M1.val / 2
let chooseSub : Prop := M1.val % 2 = 1
have hM3choice : M3.val = if chooseSub then lowSub + 1 else lowSub := by
rw [hM3val]
dsimp only [chooseSub, lowSub]
by_cases hc : M1.val % 2 = 1
· rw [if_pos hc, hc]
omega
· have hz : M1.val % 2 = 0 := by omega
rw [if_neg hc, hz]
omega
simp only [hM1, hM2, hM3, honeSub, bind_tc_ok] at hrun
have htargetM3 : target.val = M3.val := by
by_cases hc : M3 ≥ oneSub
· have hge : unit ≤ M3.val := by
rw [← honeVal]
simpa only [UScalar.le_equiv] using hc
have hM3Eq : M3.val = unit := by omega
have hpbi : IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) =
1#i32 := by
simp [hc, IScalar.cast_fromBool]
rfl
rw [hpbi] at hrun
have hfpEq : fp = { m := M3, p_biased := 1#i32 } :=
(Result.ok.inj hrun).symm
have ha := htargetAssembly
dsimp only [DecimalGoalVocabularyDefault] at ha
rw [hfpEq] at ha
norm_num [Int.toNat, hM3Eq, unit] at ha
omega
· have hpbi : IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) =
0#i32 := by
simp [hc, IScalar.cast_fromBool]
rfl
rw [hpbi] at hrun
have hfpEq : fp = { m := M3, p_biased := 0#i32 } :=
(Result.ok.inj hrun).symm
have ha := htargetAssembly
dsimp only [DecimalGoalVocabularyDefault] at ha
rw [hfpEq] at ha
norm_num [Int.toNat] at ha
exact ha
let base : Nat := 2 ^ dNat
let deltaSub : Real := (2 : Real) ^ (p.val - 1076)
let subUnit : Real := (2 : Real) ^ (-1074 : Int)
have hbasePos : 0 < base := by dsimp only [base]; positivity
have hM1LowerNat : M1.val * base ≤ G := by
rw [hM1val]
dsimp only [base]
exact Nat.div_mul_le_self G (2 ^ dNat)
have hM1UpperNat : G < (M1.val + 1) * base := by
rw [hM1val]
dsimp only [base]
simpa only [Nat.mul_comm] using
Nat.lt_mul_div_succ G (by positivity : 0 < 2 ^ dNat)
have hM1decomp : M1.val = 2 * lowSub + M1.val % 2 := by
dsimp only [lowSub]
have hd := Nat.mod_add_div M1.val 2
omega
have hscale : (base : Real) * deltaSub =
(2 : Real) ^ (-1075 : Int) := by
dsimp only [base, deltaSub]
push_cast
rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
congr 1
rw [hdNatCast, hdVal]
omega
have hsubUnitEq : subUnit = 2 * (base : Real) * deltaSub := by
have hp : (2 : Real) ^ (-1074 : Int) =
2 * (2 : Real) ^ (-1075 : Int) := by
calc
(2 : Real) ^ (-1074 : Int) =
(2 : Real) ^ (1 + (-1075 : Int)) := by norm_num
_ = (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1075 : Int) := by
rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
_ = 2 * (2 : Real) ^ (-1075 : Int) := by norm_num
dsimp only [subUnit]
rw [hp, ← hscale]
ring
have hlowSubUpper : lowSub < unit := by
dsimp only [lowSub, unit]
apply (Nat.div_lt_iff_lt_mul (by omega : 0 < 2)).2
norm_num at hM1Upper ⊢
exact hM1Upper
have hlowSucc : lowSub + 1 ≤ unit := by omega
have subValue : ∀ n : Nat, n ≤ unit → value n = (n : Real) * subUnit := by
intro n hn
by_cases hlt : n < unit
· dsimp only [value]
rw [Nat.div_eq_of_lt hlt, Nat.mod_eq_of_lt hlt, if_pos rfl]
dsimp only [subUnit]
rw [zpow_neg]
exact div_eq_mul_inv _ _
· have heq : n = unit := by omega
subst n
dsimp only [value]
rw [Nat.div_self (by dsimp only [unit]; positivity), Nat.mod_self,
if_neg (by omega), if_neg (by omega)]
dsimp only [subUnit]
rw [zpow_neg]
norm_num
exact div_eq_mul_inv _ _
have hvalueLowEq : value lowSub = (lowSub : Real) * subUnit :=
subValue lowSub hlowSubUpper.le
have hvalueHighEq : value (lowSub + 1) =
((lowSub + 1 : Nat) : Real) * subUnit :=
subValue (lowSub + 1) hlowSucc
have hxLowSub : (lowSub : Real) * subUnit ≤ x := by
have hc0 : 2 * lowSub * base ≤ G :=
(Nat.mul_le_mul_right base (by omega : 2 * lowSub ≤ M1.val)).trans
hM1LowerNat
have hc0R : ((2 * lowSub * base : Nat) : Real) ≤ (G : Real) := by
exact_mod_cast hc0
push_cast at hc0R
have hc : (2 : Real) * lowSub * base * deltaSub ≤
(G : Real) * deltaSub :=
mul_le_mul_of_nonneg_right hc0R (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)
have hxLo' : (G : Real) * deltaSub ≤ x := by
simpa only [deltaSub] using hxcellLo
calc
(lowSub : Real) * subUnit =
(2 : Real) * lowSub * base * deltaSub := by
rw [hsubUnitEq]
ring
_ ≤ (G : Real) * deltaSub := hc
_ ≤ x := hxLo'
have hxHighSub : x ≤ ((lowSub + 1 : Nat) : Real) * subUnit := by
have hcNat : G + 1 ≤ 2 * (lowSub + 1) * base := by
have h1 : G + 1 ≤ (M1.val + 1) * base := by omega
have h2 : M1.val + 1 ≤ 2 * (lowSub + 1) := by omega
exact h1.trans (Nat.mul_le_mul_right base h2)
have hc : ((G + 1 : Nat) : Real) * deltaSub ≤
((2 * (lowSub + 1) * base : Nat) : Real) * deltaSub := by
exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcNat) (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)
have hxHi' : x < ((G + 1 : Nat) : Real) * deltaSub := by
simpa only [deltaSub] using hxcellHi
calc
x ≤ ((G + 1 : Nat) : Real) * deltaSub := hxHi'.le
_ ≤ ((2 * (lowSub + 1) * base : Nat) : Real) * deltaSub := hc
_ = ((lowSub + 1 : Nat) : Real) * subUnit := by
rw [hsubUnitEq]
push_cast
ring
have hmidHigh (hc : chooseSub) :
((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub ≤ x := by
have hodd : M1.val % 2 = 1 := hc
have hEq : M1.val = 2 * lowSub + 1 := by omega
rw [← hEq]
have hcast : (M1.val : Real) * (base : Real) ≤ G := by
exact_mod_cast hM1LowerNat
have hxLo' : (G : Real) * deltaSub ≤ x := by
simpa only [deltaSub] using hxcellLo
exact (mul_le_mul_of_nonneg_right hcast (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)).trans hxLo'
have hmidLow (hc : ¬chooseSub) :
x < ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by
have hzero : M1.val % 2 = 0 := by
dsimp only [chooseSub] at hc
omega
have hEq : M1.val = 2 * lowSub := by omega
have hcNat : G + 1 ≤ (2 * lowSub + 1) * base := by
rw [← hEq]
omega
have hcReal : ((G + 1 : Nat) : Real) * deltaSub ≤
((2 * lowSub + 1) * base : Nat) * deltaSub := by
exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcNat) (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)
have hxHi' : x < ((G + 1 : Nat) : Real) * deltaSub := by
simpa only [deltaSub] using hxcellHi
calc
x < ((G + 1 : Nat) : Real) * deltaSub := hxHi'
_ ≤ (((2 * lowSub + 1) * base : Nat) : Real) * deltaSub := hcReal
_ = ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by
push_cast
ring
have hLowNearSub (hc : ¬chooseSub) :
abs (x - (lowSub : Real) * subUnit) ≤
abs (x - ((lowSub + 1 : Nat) : Real) * subUnit) := by
rw [abs_of_nonneg (sub_nonneg.mpr hxLowSub),
abs_of_nonpos (sub_nonpos.mpr hxHighSub)]
have hm := hmidLow hc
rw [hsubUnitEq]
push_cast at hm ⊢
nlinarith only [hm]
have hHighNearSub (hc : chooseSub) :
abs (x - ((lowSub + 1 : Nat) : Real) * subUnit) ≤
abs (x - (lowSub : Real) * subUnit) := by
rw [abs_of_nonpos (sub_nonpos.mpr hxHighSub),
abs_of_nonneg (sub_nonneg.mpr hxLowSub)]
have hm := hmidHigh hc
rw [hsubUnitEq]
push_cast at hm ⊢
nlinarith only [hm]
have hchoiceSub : M3.val = lowSub ∨ M3.val = lowSub + 1 := by
rw [hM3choice]
split <;> simp
apply cellRounds target lowSub M3.val htargetM3
· dsimp only [top, unit]
omega
· rw [hvalueLowEq]
exact hxLowSub
· rw [hvalueHighEq]
exact hxHighSub
· exact hchoiceSub
· intro hsel
rw [hvalueLowEq, hvalueHighEq]
apply hLowNearSub
by_contra hc
rw [hM3choice, if_pos hc] at hsel
omega
· intro hsel
rw [hvalueLowEq, hvalueHighEq]
apply hHighNearSub
by_contra hc
rw [hM3choice, if_neg hc] at hsel
omega
· intro hsel heq
rw [hvalueLowEq, hvalueHighEq] at heq
have hc : ¬chooseSub := by
by_contra hc
rw [hM3choice, if_pos hc] at hsel
omega
have hm := hmidLow hc
rw [abs_of_nonneg (sub_nonneg.mpr hxLowSub),
abs_of_nonpos (sub_nonpos.mpr hxHighSub)] at heq
rw [hsubUnitEq] at heq
push_cast at heq hm
nlinarith only [heq, hm]
· intro hsel heq
rw [hvalueLowEq, hvalueHighEq] at heq
have hc : chooseSub := by
by_contra hc
rw [hM3choice, if_neg hc] at hsel
omega
have hm := hmidHigh hc
rw [abs_of_nonpos (sub_nonpos.mpr hxHighSub),
abs_of_nonneg (sub_nonneg.mpr hxLowSub)] at heq
have hxmid : x = ((2 * lowSub + 1 : Nat) : Real) *
(base : Real) * deltaSub := by
have hxmidSub : 2 * x = ((2 * lowSub + 1 : Nat) : Real) * subUnit := by
push_cast at heq ⊢
linarith only [heq]
rw [hsubUnitEq] at hxmidSub
push_cast at hxmidSub ⊢
ring_nf at hxmidSub ⊢
linarith only [hxmidSub]
have hodd : M1.val % 2 = 1 := hc
have hM1Eq : M1.val = 2 * lowSub + 1 := by omega
exfalso
apply (noOddHalf M1.val hodd)
rw [hxmid, ← hM1Eq]
calc
(M1.val : Real) * (base : Real) * deltaSub =
(M1.val : Real) * ((base : Real) * deltaSub) := by ring
_ = (M1.val : Real) * (2 : Real) ^ (-1075 : Int) := by rw [hscale]
· exact hxltOne.trans honeThreshold
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Binary64FiniteMagnitudeStrictMono
import Tablet.ModelU64BitAnd
import Tablet.ModelU64Shl
import Mathlib.Tactic.FieldSimp
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 1500000
set_option maxRecDepth 1000000
namespace dec2flt_model
-- [TABLET NODE: P861_T1_IntegerSubnormalRounds]
theorem P861_T1_IntegerSubnormalRounds
(q : Std.I64) (w G64 : Std.U64) (p : Std.I32)
(G : Nat) (fp : num.imp.dec2flt.common.BiasedFp) (target : F64)
(hw : 0 < w.val) (hqlo : -342 ≤ q.val)
(hpLower : -114 ≤ p.val) (hp0 : p ≤ 0#i32)
(hGval : G64.val = G) (hGUpper : G < 2 ^ 54)
(hxcellLo : (G : Real) * (2 : Real) ^ (p.val - 1076) ≤
(w.val : Real) * (10 : Real) ^ q.val)
(hxcellHi : (w.val : Real) * (10 : Real) ^ q.val <
((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076))
(htargetAssembly : DecimalGoalVocabularyDefault.assembledWord fp target)
(hrun : (do
let negp ← -. p
let d32 ← negp + 1#i32
if d32 ≥ 64#i32 then ok { m := 0#u64, p_biased := 0#i32 }
else do
let d32' ← negp + 1#i32
let M1 ← G64 >>> d32'
let M2 ← M1 + (M1 &&& 1#u64)
let M3 ← M2 >>> 1#i32
let oneSub ← 1#u64 <<< 52#u32
ok { m := M3, p_biased :=
IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) }) = ok fp) :
DecimalGoalVocabularyDefault.roundsMagnitude
((w.val : Real) * (10 : Real) ^ q.val) target := by
-- BODY
let x : Real := (w.val : Real) * (10 : Real) ^ q.val
have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by as_aux_lemma =>
intro α β r f y hr
cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
have andOne (z : Std.U64) : (z &&& 1#u64).val = z.val % 2 := by as_aux_lemma =>
change (z.bv &&& 1#64).toNat = z.bv.toNat % 2
rw [show (1#64) = 2#64 ^ 1 - 1#64 by decide,
BitVec.and_two_pow_sub_one_eq_mod]
simp
let unit : Nat := 2 ^ 52
let top : Nat := 2047 * unit
let threshold : Real := (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat)
let value : Nat → Real := fun n =>
let field := n / unit
let fraction := n % unit
if field = 0 then (fraction : Real) / (2 : Real) ^ (1074 : Nat)
else if 1075 ≤ field then
((unit + fraction : Nat) : Real) * (2 : Real) ^ (field - 1075)
else ((unit + fraction : Nat) : Real) / (2 : Real) ^ (1075 - field)
have hvalueMono : ∀ a b : Nat, a < b → b < top → value a < value b := by
simpa only [unit, top, value] using Binary64FiniteMagnitudeStrictMono
have hxpos : 0 < x := by as_aux_lemma =>
dsimp only [x]
exact mul_pos (by exact_mod_cast hw) (zpow_pos (by norm_num) _)
have honeThreshold : (1 : Real) < threshold := by as_aux_lemma =>
have hp : (0 : Real) < (2 : Real) ^ (970 : Nat) := by positivity
have heq : (2 : Real) ^ (1024 : Nat) =
(2 : Real) ^ (54 : Nat) * (2 : Real) ^ (970 : Nat) := by
rw [← pow_add]
dsimp only [threshold]
rw [heq]
have h54 : (2 : Real) ^ (54 : Nat) = 18014398509481984 := by norm_num
rw [h54]
have hp1 : (1 : Real) ≤ (2 : Real) ^ (970 : Nat) := one_le_pow₀ (by norm_num)
rw [show 18014398509481984 * (2 : Real) ^ (970 : Nat) -
(2 : Real) ^ (970 : Nat) =
18014398509481983 * (2 : Real) ^ (970 : Nat) by ring]
calc
(1 : Real) < 18014398509481983 := by norm_num
_ ≤ 18014398509481983 * (2 : Real) ^ (970 : Nat) := by
simpa only [mul_one] using
mul_le_mul_of_nonneg_left hp1 (by norm_num : (0 : Real) ≤ 18014398509481983)
have cellRounds : ∀ (t : F64) (low bits : Nat),
t.val = bits → low + 1 < top →
value low ≤ x → x ≤ value (low + 1) →
(bits = low ∨ bits = low + 1) →
(bits = low →
abs (x - value low) ≤ abs (x - value (low + 1))) →
(bits = low + 1 →
abs (x - value (low + 1)) ≤ abs (x - value low)) →
(bits = low →
abs (x - value low) = abs (x - value (low + 1)) → bits % 2 = 0) →
(bits = low + 1 →
abs (x - value (low + 1)) = abs (x - value low) → bits % 2 = 0) →
x < threshold →
DecimalGoalVocabularyDefault.roundsMagnitude x t := by as_aux_lemma =>
intro t low bits ht hlow hxl hxh hchoice hnearLow hnearHigh
htieLow htieHigh hthreshold
dsimp only [DecimalGoalVocabularyDefault]
change 0 ≤ x ∧ t.val ≤ top ∧
(if t.val = top then threshold ≤ x else x < threshold ∧
∀ v : F64, v.val < top → v ≠ t →
abs (x - value t.val) ≤ abs (x - value v.val) ∧
(abs (x - value t.val) = abs (x - value v.val) → t.val % 2 = 0))
have hbits : bits < top := by rcases hchoice with h | h <;> omega
have htBound : t.val < top := by omega
refine ⟨hxpos.le, htBound.le, ?_⟩
rw [if_neg (by omega)]
refine ⟨hthreshold, ?_⟩
intro v hv hne
have hvne : v.val ≠ bits := by
intro heq
apply hne
apply UScalar.eq_of_val_eq
omega
rcases hchoice with hbitsLow | hbitsHigh
· have htlow : t.val = low := ht.trans hbitsLow
rw [htlow]
have hnear := hnearLow hbitsLow
have htie := htieLow hbitsLow
by_cases hvBelow : v.val < low
· have hvValue : value v.val < value low :=
hvalueMono _ _ hvBelow (by omega)
have hvx : value v.val < x := hvValue.trans_le hxl
have hstrict : abs (x - value low) < abs (x - value v.val) := by
rw [abs_of_nonneg (sub_nonneg.mpr hxl),
abs_of_nonneg (sub_nonneg.mpr hvx.le)]
linarith
exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
· have hvAbove : low < v.val := by omega
have hhighLe : low + 1 ≤ v.val := by omega
have hhighValue : value (low + 1) ≤ value v.val := by
rcases hhighLe.eq_or_lt with heq | hlt
· exact le_of_eq (congrArg value heq)
· exact (hvalueMono _ _ hlt hv).le
have hxv : x ≤ value v.val := hxh.trans hhighValue
have hhighDist : abs (x - value (low + 1)) ≤
abs (x - value v.val) := by
rw [abs_of_nonpos (sub_nonpos.mpr hxh),
abs_of_nonpos (sub_nonpos.mpr hxv)]
linarith
constructor
· exact hnear.trans hhighDist
· intro heq
have hreverse : abs (x - value (low + 1)) ≤
abs (x - value low) := hhighDist.trans_eq heq.symm
have heqCell := le_antisymm hnear hreverse
simpa only [hbitsLow] using htie heqCell
· have hthigh : t.val = low + 1 := ht.trans hbitsHigh
rw [hthigh]
have hnear := hnearHigh hbitsHigh
have htie := htieHigh hbitsHigh
by_cases hvAbove : low + 1 < v.val
· have hvValue : value (low + 1) < value v.val :=
hvalueMono _ _ hvAbove hv
have hxv : x < value v.val := hxh.trans_lt hvValue
have hstrict : abs (x - value (low + 1)) < abs (x - value v.val) := by
rw [abs_of_nonpos (sub_nonpos.mpr hxh),
abs_of_nonpos (sub_nonpos.mpr hxv.le)]
linarith
exact ⟨hstrict.le, fun heq => (ne_of_lt hstrict heq).elim⟩
· have hvBelow : v.val < low + 1 := by omega
have hvLow : v.val ≤ low := by omega
have hvValue : value v.val ≤ value low := by
rcases hvLow.eq_or_lt with heq | hlt
· exact le_of_eq (congrArg value heq)
· exact (hvalueMono _ _ hlt (by omega)).le
have hvx : value v.val ≤ x := hvValue.trans hxl
have hlowDist : abs (x - value low) ≤ abs (x - value v.val) := by
rw [abs_of_nonneg (sub_nonneg.mpr hxl),
abs_of_nonneg (sub_nonneg.mpr hvx)]
linarith
constructor
· exact hnear.trans hlowDist
· intro heq
have hreverse : abs (x - value low) ≤
abs (x - value (low + 1)) := hlowDist.trans_eq heq.symm
have heqCell := le_antisymm hnear hreverse
simpa only [hbitsHigh] using htie heqCell
obtain ⟨negp, hnegp, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨d32, hd32, hrun⟩ := bind_ok_inv _ _ _ hrun
have hnegpVal : negp.val = -p.val := by
have hnotmin : p.val ≠ IScalar.min .I32 := by
rw [IScalar.min_I32_eq]
omega
have hs := IScalar.neg_step p hnotmin
change IScalar.neg p = ok negp at hnegp
rw [hnegp] at hs
simpa only [WP.spec_ok] using hs
have hdVal : d32.val = 1 - p.val := by
have hs := IScalar.add_equiv negp 1#i32
rw [hd32] at hs
norm_num at hs
omega
have hdPos : 0 < d32.val := by
have := (show p.val ≤ 0 by simpa only [IScalar.le_equiv] using hp0)
omega
have hpNonpos : p.val ≤ 0 := by
simpa only [IScalar.le_equiv] using hp0
have hxltOne : x < 1 := by
have hdeltaBound : (2 : Real) ^ (p.val - 1076) ≤
(2 : Real) ^ (-1076 : Int) :=
zpow_le_zpow_right₀ (by norm_num) (by omega)
have hG1 : (G + 1 : Nat) ≤ 2 ^ 54 := by omega
calc
x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := hxcellHi
_ ≤ (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (-1076 : Int) := by
exact mul_le_mul (by exact_mod_cast hG1) hdeltaBound
(zpow_pos (by norm_num) _).le (by positivity)
_ = (2 : Real) ^ (-1022 : Int) := by
rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
_ < 1 := by
rw [zpow_neg]
exact inv_lt_one_of_one_lt₀ (by norm_num)
have noOddHalf : ∀ odd : Nat, odd % 2 = 1 →
x ≠ (odd : Real) * (2 : Real) ^ (-1075 : Int) := by
intro odd hodd heq
have hqNeg : q.val < 0 := by
by_contra hn
have hq0 : 0 ≤ q.val := by omega
have hw1 : (1 : Real) ≤ w.val := by exact_mod_cast hw
have hpow1 : (1 : Real) ≤ (10 : Real) ^ q.val := by
rw [← zpow_zero (10 : Real)]
exact zpow_le_zpow_right₀ (by norm_num) hq0
have hx1 : 1 ≤ x := by
dsimp only [x]
nlinarith [mul_le_mul hw1 hpow1 (by norm_num : (0 : Real) ≤ 1)
(by positivity : (0 : Real) ≤ (w.val : Real))]
linarith
let n : Nat := (-q.val).toNat
have hqn : q.val = -(n : Int) := by
dsimp only [n]
rw [Int.toNat_of_nonneg (by omega)]
omega
have hn342 : n ≤ 342 := by omega
have hEq' : (w.val : Real) * (10 : Real) ^ (-(n : Int)) =
(odd : Real) * (2 : Real) ^ (-1075 : Int) := by
simpa only [x, hqn] using heq
rw [zpow_neg, zpow_neg] at hEq'
have hcross0 := hEq'
field_simp at hcross0
have hcross : (w.val : Real) * (2 : Real) ^ (1075 : Nat) =
(odd : Real) * (10 : Real) ^ n := by
simpa [mul_comm] using hcross0
have hcrossNat : w.val * 2 ^ 1075 = odd * 10 ^ n := by
exact_mod_cast hcross
have hten : 10 ^ n = 2 ^ n * 5 ^ n := by
rw [show (10 : Nat) = 2 * 5 by norm_num, mul_pow]
have htwo : 2 ^ 1075 = 2 ^ n * 2 ^ (1075 - n) := by
rw [← pow_add]
congr 1
omega
rw [hten, htwo] at hcrossNat
have hcancel : w.val * 2 ^ (1075 - n) = odd * 5 ^ n := by
have hc : 2 ^ n * (w.val * 2 ^ (1075 - n)) =
2 ^ n * (odd * 5 ^ n) := by
simpa [mul_assoc, mul_left_comm, mul_comm] using hcrossNat
exact Nat.eq_of_mul_eq_mul_left (by positivity) hc
have hleft : (w.val * 2 ^ (1075 - n)) % 2 = 0 := by
rw [Nat.mul_mod]
have heven : 2 ^ (1075 - n) % 2 = 0 := by
rw [show 1075 - n = (1075 - n - 1) + 1 by omega, pow_succ]
simp
simp [heven]
have hright : (odd * 5 ^ n) % 2 = 1 := by
rw [Nat.mul_mod, hodd]
have hfive : 5 ^ n % 2 = 1 := by
rw [Nat.pow_mod]
norm_num
simp [hfive]
rw [hcancel] at hleft
omega
by_cases hd64 : d32 ≥ 64#i32
· simp only [hd64, if_true] at hrun
have hfpEq : fp = { m := 0#u64, p_biased := 0#i32 } :=
(Result.ok.inj hrun).symm
have hdGe : 64 ≤ d32.val := by
simpa only [IScalar.le_equiv] using hd64
have hpLe : p.val ≤ -63 := by omega
have hdeltaBound : (2 : Real) ^ (p.val - 1076) ≤
(2 : Real) ^ (-1139 : Int) :=
zpow_le_zpow_right₀ (by norm_num) (by omega)
have hG1 : (G + 1 : Nat) ≤ 2 ^ 54 := by omega
have hxTiny : x < (2 : Real) ^ (-1085 : Int) := by
calc
x < ((G + 1 : Nat) : Real) * (2 : Real) ^ (p.val - 1076) := hxcellHi
_ ≤ (2 : Real) ^ (54 : Nat) * (2 : Real) ^ (-1139 : Int) := by
exact mul_le_mul (by exact_mod_cast hG1) hdeltaBound
(zpow_pos (by norm_num) _).le (by positivity)
_ = (2 : Real) ^ (-1085 : Int) := by
rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have hxHalf : x < (2 : Real) ^ (-1075 : Int) :=
hxTiny.trans (zpow_lt_zpow_right₀ (by norm_num) (by omega))
subst fp
have htargetZero : target.val = 0 := by
have ha := htargetAssembly
dsimp only [DecimalGoalVocabularyDefault] at ha
norm_num [Int.toNat] at ha
exact ha
have hvalueZero : value 0 = 0 := by
dsimp only [value, unit]
norm_num
have hvalueOne : value 1 = (2 : Real) ^ (-1074 : Int) := by
dsimp only [value, unit]
norm_num
rfl
have htwiceHalf : 2 * (2 : Real) ^ (-1075 : Int) =
(2 : Real) ^ (-1074 : Int) := by
calc
2 * (2 : Real) ^ (-1075 : Int) =
(2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1075 : Int) := by norm_num
_ = (2 : Real) ^ (-1074 : Int) := by
rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
norm_num
have hxOne : x ≤ value 1 := by
rw [hvalueOne, ← htwiceHalf]
linarith [hxHalf]
have hxOne' : x ≤ (2 : Real) ^ (-1074 : Int) := by
simpa only [hvalueOne] using hxOne
apply cellRounds target 0 0 htargetZero
· dsimp only [top, unit]
norm_num
· rw [hvalueZero]
exact hxpos.le
· exact hxOne
· exact Or.inl rfl
· intro _
rw [hvalueZero, hvalueOne]
rw [sub_zero]
rw [abs_of_nonneg hxpos.le,
abs_of_nonpos (sub_nonpos.mpr hxOne')]
rw [← htwiceHalf]
linarith
· intro h
omega
· intro _ _
norm_num
· intro h
omega
· exact hxltOne.trans honeThreshold
· simp only [hd64, if_false] at hrun
obtain ⟨d32', hd32', hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨M1, hM1, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨M2, hM2, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨M3, hM3, hrun⟩ := bind_ok_inv _ _ _ hrun
obtain ⟨oneSub, honeSub, hrun⟩ := bind_ok_inv _ _ _ hrun
have hdLt : d32.val < 64 := by
have hn : ¬64 ≤ d32.val := by
simpa only [IScalar.le_equiv] using hd64
omega
have hdEq : d32' = d32 := by
exact Result.ok.inj (hd32'.symm.trans hd32)
subst d32'
let dNat : Nat := d32.toNat
have hdNatCast : (dNat : Int) = d32.val := by
dsimp only [dNat]
exact Int.toNat_of_nonneg hdPos.le
have hdNatPos : 0 < dNat := by
have hi : (0 : Int) < (dNat : Int) := by
rw [hdNatCast]
exact hdPos
exact_mod_cast hi
have hM1spec := UScalar.ShiftRight_IScalar_spec G64 d32 hdPos.le hdLt
rw [hM1] at hM1spec
have hM1val : M1.val = G / 2 ^ dNat := by
rw [hGval] at hM1spec
simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, dNat] using hM1spec.1
have hM2val : M2.val = M1.val + M1.val % 2 := by
have hs := UScalar.add_equiv M1 (M1 &&& 1#u64)
rw [hM2, andOne] at hs
exact hs.2.1
have hM3spec := UScalar.ShiftRight_IScalar_spec M2 (1#i32)
(by norm_num) (by norm_num)
rw [hM3] at hM3spec
have hM3val : M3.val = (M1.val + M1.val % 2) / 2 := by
have hs := hM3spec.1
rw [hM2val] at hs
simpa [Nat.shiftRight_eq_div_pow] using hs
have honeSpec := UScalar.ShiftLeft_spec (1#u64) (52#u32) (2 ^ 64)
(by norm_num) (by norm_num [U64.size_eq])
rw [honeSub] at honeSpec
have honeVal : oneSub.val = unit := by
dsimp only [unit]
norm_num [WP.spec_ok, Nat.shiftLeft_eq] at honeSpec ⊢
exact honeSpec.1
have hpowDGe : 2 ≤ 2 ^ dNat := by
rw [show dNat = (dNat - 1) + 1 by omega, pow_succ]
have : 1 ≤ 2 ^ (dNat - 1) := Nat.one_le_iff_ne_zero.mpr (by positivity)
omega
have hM1Upper : M1.val < 2 ^ 53 := by
rw [hM1val]
apply (Nat.div_lt_iff_lt_mul (by positivity : 0 < 2 ^ dNat)).2
calc
G < 2 ^ 54 := hGUpper
_ = 2 ^ 53 * 2 := by norm_num [pow_succ]
_ ≤ 2 ^ 53 * 2 ^ dNat := Nat.mul_le_mul_left _ hpowDGe
have hM1rem : M1.val % 2 ≤ 1 :=
Nat.le_pred_of_lt (Nat.mod_lt _ (by omega))
have hM3Upper : M3.val ≤ unit := by
rw [hM3val]
dsimp only [unit]
omega
let lowSub : Nat := M1.val / 2
let chooseSub : Prop := M1.val % 2 = 1
have hM3choice : M3.val = if chooseSub then lowSub + 1 else lowSub := by
rw [hM3val]
dsimp only [chooseSub, lowSub]
by_cases hc : M1.val % 2 = 1
· rw [if_pos hc, hc]
omega
· have hz : M1.val % 2 = 0 := by omega
rw [if_neg hc, hz]
omega
simp only [hM1, hM2, hM3, honeSub, bind_tc_ok] at hrun
have htargetM3 : target.val = M3.val := by
by_cases hc : M3 ≥ oneSub
· have hge : unit ≤ M3.val := by
rw [← honeVal]
simpa only [UScalar.le_equiv] using hc
have hM3Eq : M3.val = unit := by omega
have hpbi : IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) =
1#i32 := by
simp [hc, IScalar.cast_fromBool]
rfl
rw [hpbi] at hrun
have hfpEq : fp = { m := M3, p_biased := 1#i32 } :=
(Result.ok.inj hrun).symm
have ha := htargetAssembly
dsimp only [DecimalGoalVocabularyDefault] at ha
rw [hfpEq] at ha
norm_num [Int.toNat, hM3Eq, unit] at ha
omega
· have hpbi : IScalar.cast_fromBool .I32 (decide (M3 ≥ oneSub)) =
0#i32 := by
simp [hc, IScalar.cast_fromBool]
rfl
rw [hpbi] at hrun
have hfpEq : fp = { m := M3, p_biased := 0#i32 } :=
(Result.ok.inj hrun).symm
have ha := htargetAssembly
dsimp only [DecimalGoalVocabularyDefault] at ha
rw [hfpEq] at ha
norm_num [Int.toNat] at ha
exact ha
let base : Nat := 2 ^ dNat
let deltaSub : Real := (2 : Real) ^ (p.val - 1076)
let subUnit : Real := (2 : Real) ^ (-1074 : Int)
have hbasePos : 0 < base := by dsimp only [base]; positivity
have hM1LowerNat : M1.val * base ≤ G := by
rw [hM1val]
dsimp only [base]
exact Nat.div_mul_le_self G (2 ^ dNat)
have hM1UpperNat : G < (M1.val + 1) * base := by
rw [hM1val]
dsimp only [base]
simpa only [Nat.mul_comm] using
Nat.lt_mul_div_succ G (by positivity : 0 < 2 ^ dNat)
have hM1decomp : M1.val = 2 * lowSub + M1.val % 2 := by
dsimp only [lowSub]
have hd := Nat.mod_add_div M1.val 2
omega
have hscale : (base : Real) * deltaSub =
(2 : Real) ^ (-1075 : Int) := by
dsimp only [base, deltaSub]
push_cast
rw [← zpow_natCast, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
congr 1
rw [hdNatCast, hdVal]
omega
have hsubUnitEq : subUnit = 2 * (base : Real) * deltaSub := by
have hp : (2 : Real) ^ (-1074 : Int) =
2 * (2 : Real) ^ (-1075 : Int) := by
calc
(2 : Real) ^ (-1074 : Int) =
(2 : Real) ^ (1 + (-1075 : Int)) := by norm_num
_ = (2 : Real) ^ (1 : Int) * (2 : Real) ^ (-1075 : Int) := by
rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
_ = 2 * (2 : Real) ^ (-1075 : Int) := by norm_num
dsimp only [subUnit]
rw [hp, ← hscale]
ring
have hlowSubUpper : lowSub < unit := by
dsimp only [lowSub, unit]
apply (Nat.div_lt_iff_lt_mul (by omega : 0 < 2)).2
norm_num at hM1Upper ⊢
exact hM1Upper
have hlowSucc : lowSub + 1 ≤ unit := by omega
have subValue : ∀ n : Nat, n ≤ unit → value n = (n : Real) * subUnit := by
intro n hn
by_cases hlt : n < unit
· dsimp only [value]
rw [Nat.div_eq_of_lt hlt, Nat.mod_eq_of_lt hlt, if_pos rfl]
dsimp only [subUnit]
rw [zpow_neg]
exact div_eq_mul_inv _ _
· have heq : n = unit := by omega
subst n
dsimp only [value]
rw [Nat.div_self (by dsimp only [unit]; positivity), Nat.mod_self,
if_neg (by omega), if_neg (by omega)]
dsimp only [subUnit]
rw [zpow_neg]
norm_num
exact div_eq_mul_inv _ _
have hvalueLowEq : value lowSub = (lowSub : Real) * subUnit :=
subValue lowSub hlowSubUpper.le
have hvalueHighEq : value (lowSub + 1) =
((lowSub + 1 : Nat) : Real) * subUnit :=
subValue (lowSub + 1) hlowSucc
have hxLowSub : (lowSub : Real) * subUnit ≤ x := by
have hc0 : 2 * lowSub * base ≤ G :=
(Nat.mul_le_mul_right base (by omega : 2 * lowSub ≤ M1.val)).trans
hM1LowerNat
have hc0R : ((2 * lowSub * base : Nat) : Real) ≤ (G : Real) := by
exact_mod_cast hc0
push_cast at hc0R
have hc : (2 : Real) * lowSub * base * deltaSub ≤
(G : Real) * deltaSub :=
mul_le_mul_of_nonneg_right hc0R (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)
have hxLo' : (G : Real) * deltaSub ≤ x := by
simpa only [deltaSub] using hxcellLo
calc
(lowSub : Real) * subUnit =
(2 : Real) * lowSub * base * deltaSub := by
rw [hsubUnitEq]
ring
_ ≤ (G : Real) * deltaSub := hc
_ ≤ x := hxLo'
have hxHighSub : x ≤ ((lowSub + 1 : Nat) : Real) * subUnit := by
have hcNat : G + 1 ≤ 2 * (lowSub + 1) * base := by
have h1 : G + 1 ≤ (M1.val + 1) * base := by omega
have h2 : M1.val + 1 ≤ 2 * (lowSub + 1) := by omega
exact h1.trans (Nat.mul_le_mul_right base h2)
have hc : ((G + 1 : Nat) : Real) * deltaSub ≤
((2 * (lowSub + 1) * base : Nat) : Real) * deltaSub := by
exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcNat) (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)
have hxHi' : x < ((G + 1 : Nat) : Real) * deltaSub := by
simpa only [deltaSub] using hxcellHi
calc
x ≤ ((G + 1 : Nat) : Real) * deltaSub := hxHi'.le
_ ≤ ((2 * (lowSub + 1) * base : Nat) : Real) * deltaSub := hc
_ = ((lowSub + 1 : Nat) : Real) * subUnit := by
rw [hsubUnitEq]
push_cast
ring
have hmidHigh (hc : chooseSub) :
((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub ≤ x := by
have hodd : M1.val % 2 = 1 := hc
have hEq : M1.val = 2 * lowSub + 1 := by omega
rw [← hEq]
have hcast : (M1.val : Real) * (base : Real) ≤ G := by
exact_mod_cast hM1LowerNat
have hxLo' : (G : Real) * deltaSub ≤ x := by
simpa only [deltaSub] using hxcellLo
exact (mul_le_mul_of_nonneg_right hcast (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)).trans hxLo'
have hmidLow (hc : ¬chooseSub) :
x < ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by
have hzero : M1.val % 2 = 0 := by
dsimp only [chooseSub] at hc
omega
have hEq : M1.val = 2 * lowSub := by omega
have hcNat : G + 1 ≤ (2 * lowSub + 1) * base := by
rw [← hEq]
omega
have hcReal : ((G + 1 : Nat) : Real) * deltaSub ≤
((2 * lowSub + 1) * base : Nat) * deltaSub := by
exact mul_le_mul_of_nonneg_right (by exact_mod_cast hcNat) (by
dsimp only [deltaSub]
exact (zpow_pos (by norm_num) _).le)
have hxHi' : x < ((G + 1 : Nat) : Real) * deltaSub := by
simpa only [deltaSub] using hxcellHi
calc
x < ((G + 1 : Nat) : Real) * deltaSub := hxHi'
_ ≤ (((2 * lowSub + 1) * base : Nat) : Real) * deltaSub := hcReal
_ = ((2 * lowSub + 1 : Nat) : Real) * (base : Real) * deltaSub := by
push_cast
ring
have hLowNearSub (hc : ¬chooseSub) :
abs (x - (lowSub : Real) * subUnit) ≤
abs (x - ((lowSub + 1 : Nat) : Real) * subUnit) := by
rw [abs_of_nonneg (sub_nonneg.mpr hxLowSub),
abs_of_nonpos (sub_nonpos.mpr hxHighSub)]
have hm := hmidLow hc
rw [hsubUnitEq]
push_cast at hm ⊢
nlinarith only [hm]
have hHighNearSub (hc : chooseSub) :
abs (x - ((lowSub + 1 : Nat) : Real) * subUnit) ≤
abs (x - (lowSub : Real) * subUnit) := by
rw [abs_of_nonpos (sub_nonpos.mpr hxHighSub),
abs_of_nonneg (sub_nonneg.mpr hxLowSub)]
have hm := hmidHigh hc
rw [hsubUnitEq]
push_cast at hm ⊢
nlinarith only [hm]
have hchoiceSub : M3.val = lowSub ∨ M3.val = lowSub + 1 := by
rw [hM3choice]
split <;> simp
apply cellRounds target lowSub M3.val htargetM3
· dsimp only [top, unit]
omega
· rw [hvalueLowEq]
exact hxLowSub
· rw [hvalueHighEq]
exact hxHighSub
· exact hchoiceSub
· intro hsel
rw [hvalueLowEq, hvalueHighEq]
apply hLowNearSub
by_contra hc
rw [hM3choice, if_pos hc] at hsel
omega
· intro hsel
rw [hvalueLowEq, hvalueHighEq]
apply hHighNearSub
by_contra hc
rw [hM3choice, if_neg hc] at hsel
omega
· intro hsel heq
rw [hvalueLowEq, hvalueHighEq] at heq
have hc : ¬chooseSub := by
by_contra hc
rw [hM3choice, if_pos hc] at hsel
omega
have hm := hmidLow hc
rw [abs_of_nonneg (sub_nonneg.mpr hxLowSub),
abs_of_nonpos (sub_nonpos.mpr hxHighSub)] at heq
rw [hsubUnitEq] at heq
push_cast at heq hm
nlinarith only [heq, hm]
· intro hsel heq
rw [hvalueLowEq, hvalueHighEq] at heq
have hc : chooseSub := by
by_contra hc
rw [hM3choice, if_neg hc] at hsel
omega
have hm := hmidHigh hc
rw [abs_of_nonpos (sub_nonpos.mpr hxHighSub),
abs_of_nonneg (sub_nonneg.mpr hxLowSub)] at heq
have hxmid : x = ((2 * lowSub + 1 : Nat) : Real) *
(base : Real) * deltaSub := by
have hxmidSub : 2 * x = ((2 * lowSub + 1 : Nat) : Real) * subUnit := by
push_cast at heq ⊢
linarith only [heq]
rw [hsubUnitEq] at hxmidSub
push_cast at hxmidSub ⊢
ring_nf at hxmidSub ⊢
linarith only [hxmidSub]
have hodd : M1.val % 2 = 1 := hc
have hM1Eq : M1.val = 2 * lowSub + 1 := by omega
exfalso
apply (noOddHalf M1.val hodd)
rw [hxmid, ← hM1Eq]
calc
(M1.val : Real) * (base : Real) * deltaSub =
(M1.val : Real) * ((base : Real) * deltaSub) := by ring
_ = (M1.val : Real) * (2 : Real) ^ (-1075 : Int) := by rw [hscale]
· exact hxltOne.trans honeThreshold
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Used by
Inspect related source (44)
- BiasedFp · dec2flt/common.rs:56 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:22 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)
- bitand · core/src/ops/bit.rs:178 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- 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
- bitor · core/src/ops/bit.rs:282 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": "P861_T1_IntegerSubnormalRounds",
"closure_version": "patch_c_v1",
"active_decl_hash": "9ad739494b045ac9e4e708b5836c8c1bfe1f66e768a98ac2ce8e0500b68f166c",
"active_statement_hash": "ad9a4c657ae068905d9c66197dd4cbc58a33a666efbb098b405b4aef24337cd3",
"kernel_axioms": [
"Classical.choice",
"Quot.sound",
"propext"
],
"toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
"lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
"checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
"approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-201",
"boundary_theorems": {},
"strict_theorem_deps": {
"Binary64FiniteMagnitudeStrictMono": "ea630fa702fb7b61",
"DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
},
"strict_definition_deps": {
"DecimalGoalVocabulary": "f76c8174a4d58eb5",
"DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
"F64": "ec4e25fbaf4e11d4",
"num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2"
}
}Prose assessment
{
"label": "Lean proof closed; predictive prose review no longer required",
"assessment_status": null,
"fingerprint_relation": "unknown",
"assessment_fingerprint": null,
"current_fingerprint": "sound-v2:8de4f20b3a4c3227e17a3b751235e8537d840b263c291747924ad478f2d5196d",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass"
}