Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Normal nearest-even extraction for an arbitrary endpoint
Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.DecimalSeqRoundLoopValue
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.P472_T1_DecimalValueBounds
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P529_T1_RoundArbitraryEndpoint]
theorem P529_T1_RoundArbitraryEndpoint (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
(X Y : Real)
(hcap : d.num_digits.val <= 768)
(hdigits : forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10)
(hshape : (d.num_digits.val = 0 /\ d.truncated = false /\ X = 0) \/
((exists h tail,
(d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val) /\
d.digits.val[d.num_digits.val - 1]? ≠ some 0#u8))
(hpoint : d.decimal_point.val < 19)
(hXlo : 0 <= X) (hXhi : X < (2 : Real) ^ (64 : Nat) - 1 / 2) :
let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
(exists eta u : Real, X = V d + eta /\ Y = V d + u /\
0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) ->
(forall n : Nat, (n : Real) + 1 / 2 <= V d \/ Y <= (n : Real) + 1 / 2) ->
exists m : Std.U64,
num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
X <= (m.val : Real) + 1 / 2 /\ (m.val : Real) <= X + 1 / 2 /\
(X = (m.val : Real) + 1 / 2 \/ X = (m.val : Real) - 1 / 2 ->
m.val % 2 = 0) := 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
Lean proof
exact (set_option maxHeartbeats 40000000 in (by
dsimp only
intro hw hsep
rcases hshape with hzero | hpos
· rcases hzero with ⟨hn, ht, hX⟩
subst X
rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
simp [hn] at hXe hYe
have hY : Y = u := by linarith
have hU : 0 < u := by linarith
refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
· simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hn]
· simp
· simp
· intro hmid
simp
· rcases hpos with ⟨⟨first, tail, hmap, hfirstlo, hfirsthi, hlen⟩, hlast⟩
let digits := (d.digits.val.take d.num_digits.val).map (fun b => b.val)
have hdigitsLen : digits.length = d.num_digits.val := by
simp [digits]
have ha : d.digits.val.length = 768 := by simpa using d.digits.property
omega
have hword : digits = first :: tail := by simpa [digits] using hmap
have hnpos : 0 < d.num_digits.val := by
rw [← hlen]
simp
have hlastWord : digits.getLast? ≠ some 0 := by
have hlastEq : digits.getLast? =
(d.digits.val[d.num_digits.val - 1]?).map (fun b => b.val) := by
rw [List.getLast?_eq_getElem?]
simp only [digits, List.getElem?_map, List.getElem?_take]
simp [hcap, Nat.sub_lt hnpos]
intro heq
rw [hlastEq] at heq
rcases Option.map_eq_some_iff.mp heq with ⟨b, hb, hbval⟩
have hbzero : b = 0#u8 := by scalar_tac
apply hlast
rw [hb, hbzero]
have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
intro z hz
have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
(fun b => b.val) := by simpa [hword, digits] using hz
rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
exact hbz ▸ hdigits b hb
by_cases hnegative : d.decimal_point.val < 0
· have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
intro z hz
have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
(fun b => b.val) := by simpa [hword, digits] using hz
rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
exact hbz ▸ hdigits b hb
have hvbound := P472_T1_DecimalValueBounds
(first :: tail) first tail rfl hfirstlo hdigitNat d.decimal_point.val
have hVlt :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(1 : Real) / 2 := by
have hv :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(10 : Real) ^ d.decimal_point.val := by
have hv' := hvbound.2
rw [← hword] at hv'
rw [hdigitsLen] at hv'
simpa [digits] using hv'
have hp : (10 : Real) ^ d.decimal_point.val < 1 / 2 := by
let k : Nat := Int.toNat (-d.decimal_point.val - 1)
have hk : d.decimal_point.val = -(k : Int) - 1 := by
have hp0 : 0 ≤ -d.decimal_point.val - 1 := by omega
have ht : ((-d.decimal_point.val - 1).toNat : Int) =
-d.decimal_point.val - 1 := Int.toNat_of_nonneg hp0
simp only [k]
omega
rw [hk, show -(k : Int) - 1 = -((k + 1 : Nat) : Int) by omega,
zpow_neg, zpow_natCast]
have hden : (10 : Real) ≤ (10 : Real) ^ (k + 1) := by
calc
(10 : Real) = 10 ^ (1 : Nat) := by norm_num
_ ≤ 10 ^ (k + 1) := by
exact_mod_cast (Nat.pow_le_pow_right (by norm_num : 0 < 10)
(show 1 ≤ k + 1 by omega))
have hinv : ((10 : Real) ^ (k + 1))⁻¹ ≤ (10 : Real)⁻¹ :=
(inv_le_inv₀ (by positivity : (0 : Real) < (10 : Real) ^ (k + 1))
(by norm_num : (0 : Real) < 10)).2 hden
norm_num at hinv ⊢
linarith
exact lt_trans hv hp
rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
have hsep0 := hsep 0
have hYhalf : Y <= 1 / 2 := by
rcases hsep0 with hbad | hgood
· linarith
· simpa using hgood
have hXhalf : X < 1 / 2 := by
have hXV : (0 : Real) <=
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num) _))
have hxle :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
rw [hXe]
linarith
linarith
refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
· have hp : d.decimal_point < 0#i32 := by scalar_tac
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hp]
· simpa using le_of_lt hXhalf
· norm_num
linarith
· intro _
norm_num
· have hpLower : 0 <= d.decimal_point.val := by
omega
have hpUpper : d.decimal_point.val <= 18 := by
exact Int.le_of_lt_add_one (by simpa using hpoint)
have hpointNonneg : ¬ d.decimal_point < 0#i32 := by scalar_tac
have hpointSmall : d.decimal_point < 19#i32 := by scalar_tac
obtain ⟨q, hqCast, hqval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize d.decimal_point (by scalar_tac))
change lift (IScalar.hcast .Usize d.decimal_point) = ok q at hqCast
have hqBound : q.val <= 18 := by omega
obtain ⟨n, hnLoop, hnPrefix, hnPadded⟩ :=
DecimalSeqRoundLoopValue d q hqBound hcap hdigits
let integer := (digits.foldl (fun z x => 10 * z + x) 0)
have hnValue :
(q.val < d.num_digits.val ->
n.val = (digits.take q.val).foldl (fun z x => 10 * z + x) 0) ∧
(d.num_digits.val <= q.val ->
n.val = integer * 10 ^ (q.val - d.num_digits.val)) := by
constructor
· exact hnPrefix
· exact hnPadded
rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
have hV :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
(integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int)) := by
rw [← hqval]
have hexp : (q.val : Int) - (d.num_digits.val : Int) =
(q.val : Int) - (digits.length : Int) := by rw [hdigitsLen]
rw [hexp]
have hVleX :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
rw [hXe]
linarith
have hXVY : X < Y := by rw [hXe, hYe]; linarith
by_cases hqLt : q.val < d.num_digits.val
· have hFrac := DecimalSeqFractionalDigitInterval digits q.val
(by rw [hdigitsLen]; exact hqLt)
(by
intro z hz
have hz' : z ∈ first :: tail := by simpa [hword] using hz
exact hdigitNat z hz')
(by simp [bne_iff_ne, hlastWord])
let N : Nat := (digits.take q.val).foldl (fun z x => 10 * z + x) 0
let f : Nat := digits[q.val]
have hnN : n.val = N := by exact hnValue.1 hqLt
dsimp only at hFrac
have hVfrac :
((integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int))) =
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
exact hV.symm
have hbelow :
f < 5 ->
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 / 2 := by
intro hf
have hb := hFrac.2.2.1 (by simpa [f] using hf)
rw [← hVfrac]
simpa [N, f] using hb
have habove :
5 < f ->
(N : Real) + 1 / 2 <
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
intro hf
have hb := hFrac.2.2.2.1 (by simpa [f] using hf)
rw [← hVfrac]
simpa [N, f] using hb
have hqArray : q.val < d.digits.val.length := by
have ha : d.digits.val.length = 768 := by simpa using d.digits.property
omega
obtain ⟨i1, hi1, hi1val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits q (by
simpa [Array.length] using hqArray))
have hreadVal : i1.val = f := by
rw [hi1val]
simp [f, digits, List.getElem_map, List.getElem_take]
have hnumNe : d.num_digits ≠ 0#usize := by
intro he
have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
have hz : d.num_digits.val = 0 := by simpa using hv
omega
obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
rw [hi19]
congr 1
apply IScalar.eq_of_val_eq
simpa using hi19val
have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
exact Result.ok.inj (by simpa [lift] using hcast19)
have hqRaw : IScalar.hcast .Usize d.decimal_point = q :=
Result.ok.inj (by simpa [lift] using hqCast)
have hpointVal : d.decimal_point.val < 19 := by
set_option maxHeartbeats 4000000 in scalar_tac
have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
have hread : d.digits.index_usize q = ok i1 := by
simpa using hi1
obtain ⟨qnext, hqnext, hqnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := q) (y := 1#usize) (by scalar_tac))
have hqnextAdd : q + 1#usize = ok qnext := by simpa using hqnext
by_cases hlow : f < 5
· have hroundKeep : d.round = ok n := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hlow, hreadVal]
have hNleV : (N : Real) <=
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
have hb := hFrac.1
rw [hVfrac] at hb
simpa [N] using hb
have hYbelow : Y <= (N : Real) + 1 / 2 := by
rcases hsep N with hbad | hgood
· have := hbelow hlow
linarith
· simpa using hgood
have hNleX : (N : Real) <= X := hNleV.trans hVleX
have hnleX : (n.val : Real) <= X := by simpa [hnN] using hNleX
have hXstrict : X < (N : Real) + 1 / 2 :=
lt_of_lt_of_le hXVY hYbelow
refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
· simpa [hnN] using le_of_lt hXstrict
· have hbound : (N : Real) <= X + 1 / 2 :=
hNleX.trans (le_add_of_nonneg_right (by norm_num : (0 : Real) <= 1 / 2))
simpa [hnN] using hbound
· intro hmid
rcases hmid with hu | hl
· have hu' : X = (N : Real) + 1 / 2 := by simpa [hnN] using hu
exact False.elim ((ne_of_lt hXstrict) hu')
· rw [hnN] at hl
have hltN : (N : Real) - 1 / 2 < (N : Real) := by
exact sub_lt_self _ (by norm_num)
have hstrict : X < (n.val : Real) := by
simpa [hnN] using (show X < (N : Real) by rw [hl]; exact hltN)
exact False.elim ((not_lt_of_ge hnleX) hstrict)
· have hfge : 5 <= f := by
exact Nat.le_of_not_lt hlow
by_cases hfive : f = 5
· have hFive := hFrac.2.2.2.2 (by simpa [f] using hfive)
by_cases hterminal : q.val + 1 = digits.length
· have hterminalCount : q.val + 1 = d.num_digits.val := by
simpa [hdigitsLen] using hterminal
have hqnextEq : qnext = d.num_digits := by
apply UScalar.eq_of_val_eq
have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
simpa [hqnv] using hterminalCount
have hVtie :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
(N : Real) + 1 / 2 := by
have ht := hFive.1 hterminal
rw [hVfrac] at ht
simpa [N] using ht
by_cases htruncated : d.truncated = true
· have hetaPos : 0 < eta := htrunc.mp htruncated
have hXabove : (N : Real) + 1 / 2 < X := by
rw [hXe, hVtie]
linarith
have hYabove : Y <= (N : Real) + 3 / 2 := by
rcases hsep (N + 1) with hbad | hgood
· have hVup :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 := by
have hu := hFrac.2.1
rw [hVfrac] at hu
simpa [N] using hu
exfalso
push_cast at hbad
linarith [hbad, hVup]
· have hgood' : Y <= (N : Real) + 3 / 2 := by
have hgood'' := hgood
push_cast at hgood''
convert hgood'' using 1 <;> ring
exact hgood'
have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
rw [hnN]
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
linarith [hNnext, hXabove, hXhi]
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, htruncated, hqnextEq, hi1EqFive,
hnAddEq, hqnextAdd]
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· have hx := hXVY.trans_le hYabove
have hx' : X < (nnext.val : Real) + 1 / 2 := by
rw [hnnextReal]
convert hx using 1 <;> ring
exact le_of_lt hx'
· rw [hnnextReal]
have hx' : (N : Real) + 1 < X + 1 / 2 := by
convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
exact le_of_lt hx'
· intro hmid
have hXstrict : X < (N : Real) + 3 / 2 :=
lt_of_lt_of_le hXVY hYabove
rcases hmid with hhi | hlo
· have hhi' : X = (N : Real) + 3 / 2 := by
calc
X = (nnext.val : Real) + 1 / 2 := hhi
_ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_lt hXstrict) hhi')
· have hlo' : X = (N : Real) + 1 / 2 := by
calc
X = (nnext.val : Real) - 1 / 2 := hlo
_ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_gt hXabove) hlo')
· have htruncatedFalse : d.truncated = false := by
cases hflag : d.truncated with
| false => rfl
| true => exact False.elim (htruncated hflag)
have hetaZero : eta = 0 := by
have ht := htrunc
rw [htruncatedFalse] at ht
simp at ht
linarith
have hXtie : X = (N : Real) + 1 / 2 := by
rw [hXe, hVtie, hetaZero]
ring
by_cases hqzero : q.val = 0
· have hqZero : q = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hqzero)
have hNzero : N = 0 := by simp [N, hqzero]
have hloop0 :
num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
{ start := 0#usize, «end» := 0#usize }
d.num_digits d.digits 0#u64 = ok n := by
simpa [hqZero] using hnLoop
have hnumEqOne : d.num_digits = 1#usize := by
apply UScalar.eq_of_val_eq
have hcount : d.num_digits.val = 1 := by omega
simpa using hcount
have hloop01 := hloop0
rw [hnumEqOne] at hloop01
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hread0 := hread
rw [hqZero] at hread0
have hzeroAdd : 0#usize + 1#usize = ok 1#usize := by
simpa [hqZero, hqnextEq, hnumEqOne] using hqnextAdd
have hroundKeep : d.round = ok n := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hloop01,
hread0, hi1EqFive, htruncatedFalse, hqZero,
hnumEqOne, hzeroAdd]
have hnzero : n.val = 0 := by rw [hnN, hNzero]
refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
· rw [hXtie, hNzero]
simp
· rw [hnzero, hXtie, hNzero]
norm_num
· intro _
rw [hnzero]
· have hqpos : 0 < q.val := by omega
obtain ⟨qprev, hqprev, hqprevSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := q) (y := 1#usize) (by simpa using hqpos))
have hqprevVal : qprev.val = q.val - 1 := by
simpa using hqprevSpec.1
have hqprevAdd : qprev.val + 1 = q.val := by omega
have hqprevArray : qprev.val < d.digits.val.length := by
have ha : d.digits.val.length = 768 := by
simpa using d.digits.property
omega
obtain ⟨iprev, hiprev, hiprevVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits qprev (by
simpa [Array.length] using hqprevArray))
have hprevRead : d.digits.index_usize qprev = ok iprev := by
simpa using hiprev
let c := digits[qprev.val]
let P := (digits.take qprev.val).foldl (fun z x => 10 * z + x) 0
have hprevReadVal : iprev.val = c := by
rw [hiprevVal]
simp [c, digits, List.getElem_map, List.getElem_take]
have htake : digits.take q.val = digits.take qprev.val ++ [c] := by
have ht := List.take_succ_eq_append_getElem
(l := digits) (i := qprev.val) (by omega : qprev.val < digits.length)
simpa [c, hqprevAdd] using ht
have hNdecomp : N = 10 * P + c := by
dsimp [N, P]
rw [htake, List.foldl_append]
simp only [List.foldl_cons, List.foldl_nil]
have hNmod : N % 2 = c % 2 := by
rw [hNdecomp]
omega
have hbitVal : (1#u8 &&& iprev).val = c % 2 := by
rw [UScalar.val_and, hprevReadVal]
have hmask : (1#u8).val = 2 ^ 1 - 1 := by norm_num
rw [hmask]
calc
(2 ^ 1 - 1) &&& c = c &&& (2 ^ 1 - 1) := Nat.and_comm _ _
_ = c % 2 ^ 1 := Nat.and_two_pow_sub_one_eq_mod c 1
_ = c % 2 := by norm_num
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hqNeZero : q ≠ 0#usize := by
intro he
have hv := congrArg UScalar.val he
simp at hv
omega
have hqvalNeZero : q.val ≠ 0 := by omega
by_cases heven : N % 2 = 0
· have hcEven : c % 2 = 0 := by rw [← hNmod]; exact heven
have hbitZero : (1#u8 &&& iprev) = 0#u8 := by
apply UScalar.eq_of_val_eq
rw [hbitVal]
simp [hcEven]
have hroundKeep : d.round = ok n := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
hqvalNeZero, hqprev, hprevRead, hbitZero,
hqnextAdd]
have hnmod : n.val % 2 = 0 := by rw [hnN, heven]
refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
· rw [hnN, hXtie]
· rw [hnN, hXtie]
linarith
· intro _
exact hnmod
· have hNmodlt : N % 2 < 2 := Nat.mod_lt _ (by omega)
have hodd : N % 2 = 1 := by omega
have hcOdd : c % 2 = 1 := by rw [← hNmod]; exact hodd
have hbitNeZero : (1#u8 &&& iprev) ≠ 0#u8 := by
intro he
have hv := congrArg UScalar.val he
have hzero : (1#u8 &&& iprev).val = 0 := by simpa using hv
rw [hbitVal] at hzero
omega
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
rw [hnN]
rw [hXtie] at hXhi
linarith
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
hqvalNeZero, hqprev, hprevRead, hbitNeZero, hnAddEq,
hqnextAdd]
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· rw [hXtie, hnnextReal]
linarith
· rw [hXtie, hnnextReal]
linarith
· intro _
have hnnextValNat : nnext.val = n.val + 1 := by simpa using hnAddVal
rw [hnnextValNat, hnN]
omega
· have hnonterminal : q.val + 1 < digits.length := by omega
have hNbelowV : (N : Real) + 1 / 2 <
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
have ht := hFive.2 hnonterminal
rw [hVfrac] at ht
simpa [N] using ht
have hYabove : Y <= (N : Real) + 3 / 2 := by
rcases hsep (N + 1) with hbad | hgood
· have hVup :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 := by
have hu := hFrac.2.1
rw [hVfrac] at hu
simpa [N] using hu
exfalso
push_cast at hbad
linarith [hbad, hVup]
· have hgood' : Y <= (N : Real) + 3 / 2 := by
have hgood'' := hgood
push_cast at hgood''
convert hgood'' using 1 <;> ring
exact hgood'
have hXabove : (N : Real) + 1 / 2 < X :=
lt_of_lt_of_le hNbelowV hVleX
have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by rw [hnN]
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
linarith [hNnext, hXabove, hXhi]
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hqnextNe : qnext ≠ d.num_digits := by
intro he
have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
have hv := congrArg UScalar.val he
change qnext.val = d.num_digits.val at hv
rw [hqnv] at hv
omega
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hi1EqFive,
hqnextNe, hnAddEq, hqnextAdd]
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· have hx := hXVY.trans_le hYabove
rw [hnnextReal]
exact le_of_lt (calc
X < (N : Real) + 3 / 2 := hx
_ = (N : Real) + 1 + 1 / 2 := by ring)
· rw [hnnextReal]
have hx' : (N : Real) + 1 < X + 1 / 2 := by
convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
exact le_of_lt hx'
· intro hmid
have hXstrict : X < (N : Real) + 3 / 2 :=
lt_of_lt_of_le hXVY hYabove
rcases hmid with hhi | hlo
· have hhi' : X = (N : Real) + 3 / 2 := by
calc
X = (nnext.val : Real) + 1 / 2 := hhi
_ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_lt hXstrict) hhi')
· have hlo' : X = (N : Real) + 1 / 2 := by
calc
X = (nnext.val : Real) - 1 / 2 := hlo
_ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_gt hXabove) hlo')
· have hfabove : 5 < f := by omega
have hi1NeFive : i1 ≠ 5#u8 := by
intro he
have hv := congrArg UScalar.val he
simp [hreadVal] at hv
omega
have hi1AtLeastFive : 5 <= i1.val := by omega
have hNbelowV : (N : Real) + 1 / 2 <
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) :=
habove hfabove
have hYabove : Y <= (N : Real) + 3 / 2 := by
rcases hsep (N + 1) with hbad | hgood
· have hVup :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 := by
have hu := hFrac.2.1
rw [hVfrac] at hu
simpa [N] using hu
exfalso
push_cast at hbad
linarith [hbad, hVup]
· have hgood' : Y <= (N : Real) + 3 / 2 := by
have hgood'' := hgood
push_cast at hgood''
convert hgood'' using 1 <;> ring
exact hgood'
have hXabove : (N : Real) + 1 / 2 < X := by
exact lt_of_lt_of_le hNbelowV hVleX
have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
rw [hnN]
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
linarith [hNnext, hXabove, hXhi]
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hreadVal, hi1NeFive, hnAddEq,
hqnextAdd]
all_goals omega
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· have hx := hXVY.trans_le hYabove
rw [hnnextReal]
linarith
· rw [hnnextReal]
linarith
· intro hmid
rcases hmid with hmid | hmid <;> rw [hnnextReal] at hmid <;> linarith
· have hqGe : d.num_digits.val <= q.val := by omega
have hNreal : (n.val : Real) =
(integer : Real) * (10 : Real) ^ ((q.val : Int) - (d.num_digits.val : Int)) := by
rw [hnValue.2 hqGe]
rw [Nat.cast_mul, Nat.cast_pow]
have hexp : (q.val : Int) - (d.num_digits.val : Int) =
((q.val - d.num_digits.val : Nat) : Int) := by omega
rw [hexp, zpow_natCast]
norm_num
have hVn :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
(n.val : Real) := by
rw [hdigitsLen] at hV
rw [hV, hNreal]
have hSepN := hsep n.val
have hYbound : Y <= (n.val : Real) + 1 / 2 := by
rcases hSepN with hbad | hgood
· rw [hVn] at hbad
norm_num at hbad
· simpa using hgood
have hXbound : X < (n.val : Real) + 1 / 2 := by
exact lt_of_lt_of_le hXVY hYbound
have hnumNe : d.num_digits ≠ 0#usize := by
intro he
have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
have hz : d.num_digits.val = 0 := by simpa using hv
omega
obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
rw [hi19]
congr 1
apply IScalar.eq_of_val_eq
simpa using hi19val
have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
exact Result.ok.inj (by simpa [lift] using hcast19)
have hqRaw : IScalar.hcast .Usize d.decimal_point = q := by
exact Result.ok.inj (by simpa [lift] using hqCast)
have hpointVal : d.decimal_point.val < 19 := hpoint
have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
have hqNotLt : ¬ q.val < d.num_digits.val := Nat.not_lt_of_ge hqGe
have hroundN : d.round = ok n := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
rw [if_neg hnumNe]
rw [if_neg hpointNonneg]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, lift]
simp [hthreshold, hqRaw, hnLoop, hqNotLt]
refine ⟨n, hroundN, ?_, ?_, ?_⟩
· exact le_of_lt hXbound
· have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
exact le_trans hnX (le_add_of_nonneg_right (by norm_num))
· intro hmid
have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
rcases hmid with hmid | hmid
· exact False.elim ((ne_of_lt hXbound) hmid)
· have hlt : (n.val : Real) - 1 / 2 < n.val :=
sub_lt_self _ (by norm_num)
have hXlt : X < (n.val : Real) := by rw [hmid]; exact hlt
exact False.elim (not_lt_of_ge hnX hXlt)
))
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalSeqRoundLoopValue
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.P472_T1_DecimalValueBounds
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P529_T1_RoundArbitraryEndpoint]
theorem P529_T1_RoundArbitraryEndpoint (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
(X Y : Real)
(hcap : d.num_digits.val <= 768)
(hdigits : forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10)
(hshape : (d.num_digits.val = 0 /\ d.truncated = false /\ X = 0) \/
((exists h tail,
(d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val) /\
d.digits.val[d.num_digits.val - 1]? ≠ some 0#u8))
(hpoint : d.decimal_point.val < 19)
(hXlo : 0 <= X) (hXhi : X < (2 : Real) ^ (64 : Nat) - 1 / 2) :
let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
(exists eta u : Real, X = V d + eta /\ Y = V d + u /\
0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) ->
(forall n : Nat, (n : Real) + 1 / 2 <= V d \/ Y <= (n : Real) + 1 / 2) ->
exists m : Std.U64,
num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
X <= (m.val : Real) + 1 / 2 /\ (m.val : Real) <= X + 1 / 2 /\
(X = (m.val : Real) + 1 / 2 \/ X = (m.val : Real) - 1 / 2 ->
m.val % 2 = 0) := by
-- BODY
exact (set_option maxHeartbeats 40000000 in (by
dsimp only
intro hw hsep
rcases hshape with hzero | hpos
· rcases hzero with ⟨hn, ht, hX⟩
subst X
rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
simp [hn] at hXe hYe
have hY : Y = u := by linarith
have hU : 0 < u := by linarith
refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
· simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hn]
· simp
· simp
· intro hmid
simp
· rcases hpos with ⟨⟨first, tail, hmap, hfirstlo, hfirsthi, hlen⟩, hlast⟩
let digits := (d.digits.val.take d.num_digits.val).map (fun b => b.val)
have hdigitsLen : digits.length = d.num_digits.val := by
simp [digits]
have ha : d.digits.val.length = 768 := by simpa using d.digits.property
omega
have hword : digits = first :: tail := by simpa [digits] using hmap
have hnpos : 0 < d.num_digits.val := by
rw [← hlen]
simp
have hlastWord : digits.getLast? ≠ some 0 := by
have hlastEq : digits.getLast? =
(d.digits.val[d.num_digits.val - 1]?).map (fun b => b.val) := by
rw [List.getLast?_eq_getElem?]
simp only [digits, List.getElem?_map, List.getElem?_take]
simp [hcap, Nat.sub_lt hnpos]
intro heq
rw [hlastEq] at heq
rcases Option.map_eq_some_iff.mp heq with ⟨b, hb, hbval⟩
have hbzero : b = 0#u8 := by scalar_tac
apply hlast
rw [hb, hbzero]
have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
intro z hz
have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
(fun b => b.val) := by simpa [hword, digits] using hz
rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
exact hbz ▸ hdigits b hb
by_cases hnegative : d.decimal_point.val < 0
· have hdigitNat : forall z, z ∈ first :: tail -> z < 10 := by
intro z hz
have hz' : z ∈ (d.digits.val.take d.num_digits.val).map
(fun b => b.val) := by simpa [hword, digits] using hz
rcases List.mem_map.mp hz' with ⟨b, hb, hbz⟩
exact hbz ▸ hdigits b hb
have hvbound := P472_T1_DecimalValueBounds
(first :: tail) first tail rfl hfirstlo hdigitNat d.decimal_point.val
have hVlt :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(1 : Real) / 2 := by
have hv :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(10 : Real) ^ d.decimal_point.val := by
have hv' := hvbound.2
rw [← hword] at hv'
rw [hdigitsLen] at hv'
simpa [digits] using hv'
have hp : (10 : Real) ^ d.decimal_point.val < 1 / 2 := by
let k : Nat := Int.toNat (-d.decimal_point.val - 1)
have hk : d.decimal_point.val = -(k : Int) - 1 := by
have hp0 : 0 ≤ -d.decimal_point.val - 1 := by omega
have ht : ((-d.decimal_point.val - 1).toNat : Int) =
-d.decimal_point.val - 1 := Int.toNat_of_nonneg hp0
simp only [k]
omega
rw [hk, show -(k : Int) - 1 = -((k + 1 : Nat) : Int) by omega,
zpow_neg, zpow_natCast]
have hden : (10 : Real) ≤ (10 : Real) ^ (k + 1) := by
calc
(10 : Real) = 10 ^ (1 : Nat) := by norm_num
_ ≤ 10 ^ (k + 1) := by
exact_mod_cast (Nat.pow_le_pow_right (by norm_num : 0 < 10)
(show 1 ≤ k + 1 by omega))
have hinv : ((10 : Real) ^ (k + 1))⁻¹ ≤ (10 : Real)⁻¹ :=
(inv_le_inv₀ (by positivity : (0 : Real) < (10 : Real) ^ (k + 1))
(by norm_num : (0 : Real) < 10)).2 hden
norm_num at hinv ⊢
linarith
exact lt_trans hv hp
rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
have hsep0 := hsep 0
have hYhalf : Y <= 1 / 2 := by
rcases hsep0 with hbad | hgood
· linarith
· simpa using hgood
have hXhalf : X < 1 / 2 := by
have hXV : (0 : Real) <=
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
exact mul_nonneg (Nat.cast_nonneg _) (le_of_lt (zpow_pos (by norm_num) _))
have hxle :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun n z => 10 * n + z) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
rw [hXe]
linarith
linarith
refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
· have hp : d.decimal_point < 0#i32 := by scalar_tac
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, hp]
· simpa using le_of_lt hXhalf
· norm_num
linarith
· intro _
norm_num
· have hpLower : 0 <= d.decimal_point.val := by
omega
have hpUpper : d.decimal_point.val <= 18 := by
exact Int.le_of_lt_add_one (by simpa using hpoint)
have hpointNonneg : ¬ d.decimal_point < 0#i32 := by scalar_tac
have hpointSmall : d.decimal_point < 19#i32 := by scalar_tac
obtain ⟨q, hqCast, hqval⟩ := Aeneas.Std.WP.spec_imp_exists
(IScalar.hcast_inBounds_spec .Usize d.decimal_point (by scalar_tac))
change lift (IScalar.hcast .Usize d.decimal_point) = ok q at hqCast
have hqBound : q.val <= 18 := by omega
obtain ⟨n, hnLoop, hnPrefix, hnPadded⟩ :=
DecimalSeqRoundLoopValue d q hqBound hcap hdigits
let integer := (digits.foldl (fun z x => 10 * z + x) 0)
have hnValue :
(q.val < d.num_digits.val ->
n.val = (digits.take q.val).foldl (fun z x => 10 * z + x) 0) ∧
(d.num_digits.val <= q.val ->
n.val = integer * 10 ^ (q.val - d.num_digits.val)) := by
constructor
· exact hnPrefix
· exact hnPadded
rcases hw with ⟨eta, u, hXe, hYe, heta, hlt, htrunc⟩
have hV :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
(integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int)) := by
rw [← hqval]
have hexp : (q.val : Int) - (d.num_digits.val : Int) =
(q.val : Int) - (digits.length : Int) := by rw [hdigitsLen]
rw [hexp]
have hVleX :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <= X := by
rw [hXe]
linarith
have hXVY : X < Y := by rw [hXe, hYe]; linarith
by_cases hqLt : q.val < d.num_digits.val
· have hFrac := DecimalSeqFractionalDigitInterval digits q.val
(by rw [hdigitsLen]; exact hqLt)
(by
intro z hz
have hz' : z ∈ first :: tail := by simpa [hword] using hz
exact hdigitNat z hz')
(by simp [bne_iff_ne, hlastWord])
let N : Nat := (digits.take q.val).foldl (fun z x => 10 * z + x) 0
let f : Nat := digits[q.val]
have hnN : n.val = N := by exact hnValue.1 hqLt
dsimp only at hFrac
have hVfrac :
((integer : Real) * (10 : Real) ^ ((q.val : Int) - (digits.length : Int))) =
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
exact hV.symm
have hbelow :
f < 5 ->
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 / 2 := by
intro hf
have hb := hFrac.2.2.1 (by simpa [f] using hf)
rw [← hVfrac]
simpa [N, f] using hb
have habove :
5 < f ->
(N : Real) + 1 / 2 <
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
intro hf
have hb := hFrac.2.2.2.1 (by simpa [f] using hf)
rw [← hVfrac]
simpa [N, f] using hb
have hqArray : q.val < d.digits.val.length := by
have ha : d.digits.val.length = 768 := by simpa using d.digits.property
omega
obtain ⟨i1, hi1, hi1val⟩ := Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits q (by
simpa [Array.length] using hqArray))
have hreadVal : i1.val = f := by
rw [hi1val]
simp [f, digits, List.getElem_map, List.getElem_take]
have hnumNe : d.num_digits ≠ 0#usize := by
intro he
have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
have hz : d.num_digits.val = 0 := by simpa using hv
omega
obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
rw [hi19]
congr 1
apply IScalar.eq_of_val_eq
simpa using hi19val
have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
exact Result.ok.inj (by simpa [lift] using hcast19)
have hqRaw : IScalar.hcast .Usize d.decimal_point = q :=
Result.ok.inj (by simpa [lift] using hqCast)
have hpointVal : d.decimal_point.val < 19 := by
set_option maxHeartbeats 4000000 in scalar_tac
have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
have hread : d.digits.index_usize q = ok i1 := by
simpa using hi1
obtain ⟨qnext, hqnext, hqnextVal⟩ := Aeneas.Std.WP.spec_imp_exists
(Usize.add_spec (x := q) (y := 1#usize) (by scalar_tac))
have hqnextAdd : q + 1#usize = ok qnext := by simpa using hqnext
by_cases hlow : f < 5
· have hroundKeep : d.round = ok n := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hlow, hreadVal]
have hNleV : (N : Real) <=
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
have hb := hFrac.1
rw [hVfrac] at hb
simpa [N] using hb
have hYbelow : Y <= (N : Real) + 1 / 2 := by
rcases hsep N with hbad | hgood
· have := hbelow hlow
linarith
· simpa using hgood
have hNleX : (N : Real) <= X := hNleV.trans hVleX
have hnleX : (n.val : Real) <= X := by simpa [hnN] using hNleX
have hXstrict : X < (N : Real) + 1 / 2 :=
lt_of_lt_of_le hXVY hYbelow
refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
· simpa [hnN] using le_of_lt hXstrict
· have hbound : (N : Real) <= X + 1 / 2 :=
hNleX.trans (le_add_of_nonneg_right (by norm_num : (0 : Real) <= 1 / 2))
simpa [hnN] using hbound
· intro hmid
rcases hmid with hu | hl
· have hu' : X = (N : Real) + 1 / 2 := by simpa [hnN] using hu
exact False.elim ((ne_of_lt hXstrict) hu')
· rw [hnN] at hl
have hltN : (N : Real) - 1 / 2 < (N : Real) := by
exact sub_lt_self _ (by norm_num)
have hstrict : X < (n.val : Real) := by
simpa [hnN] using (show X < (N : Real) by rw [hl]; exact hltN)
exact False.elim ((not_lt_of_ge hnleX) hstrict)
· have hfge : 5 <= f := by
exact Nat.le_of_not_lt hlow
by_cases hfive : f = 5
· have hFive := hFrac.2.2.2.2 (by simpa [f] using hfive)
by_cases hterminal : q.val + 1 = digits.length
· have hterminalCount : q.val + 1 = d.num_digits.val := by
simpa [hdigitsLen] using hterminal
have hqnextEq : qnext = d.num_digits := by
apply UScalar.eq_of_val_eq
have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
simpa [hqnv] using hterminalCount
have hVtie :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
(N : Real) + 1 / 2 := by
have ht := hFive.1 hterminal
rw [hVfrac] at ht
simpa [N] using ht
by_cases htruncated : d.truncated = true
· have hetaPos : 0 < eta := htrunc.mp htruncated
have hXabove : (N : Real) + 1 / 2 < X := by
rw [hXe, hVtie]
linarith
have hYabove : Y <= (N : Real) + 3 / 2 := by
rcases hsep (N + 1) with hbad | hgood
· have hVup :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 := by
have hu := hFrac.2.1
rw [hVfrac] at hu
simpa [N] using hu
exfalso
push_cast at hbad
linarith [hbad, hVup]
· have hgood' : Y <= (N : Real) + 3 / 2 := by
have hgood'' := hgood
push_cast at hgood''
convert hgood'' using 1 <;> ring
exact hgood'
have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
rw [hnN]
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
linarith [hNnext, hXabove, hXhi]
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, htruncated, hqnextEq, hi1EqFive,
hnAddEq, hqnextAdd]
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· have hx := hXVY.trans_le hYabove
have hx' : X < (nnext.val : Real) + 1 / 2 := by
rw [hnnextReal]
convert hx using 1 <;> ring
exact le_of_lt hx'
· rw [hnnextReal]
have hx' : (N : Real) + 1 < X + 1 / 2 := by
convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
exact le_of_lt hx'
· intro hmid
have hXstrict : X < (N : Real) + 3 / 2 :=
lt_of_lt_of_le hXVY hYabove
rcases hmid with hhi | hlo
· have hhi' : X = (N : Real) + 3 / 2 := by
calc
X = (nnext.val : Real) + 1 / 2 := hhi
_ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_lt hXstrict) hhi')
· have hlo' : X = (N : Real) + 1 / 2 := by
calc
X = (nnext.val : Real) - 1 / 2 := hlo
_ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_gt hXabove) hlo')
· have htruncatedFalse : d.truncated = false := by
cases hflag : d.truncated with
| false => rfl
| true => exact False.elim (htruncated hflag)
have hetaZero : eta = 0 := by
have ht := htrunc
rw [htruncatedFalse] at ht
simp at ht
linarith
have hXtie : X = (N : Real) + 1 / 2 := by
rw [hXe, hVtie, hetaZero]
ring
by_cases hqzero : q.val = 0
· have hqZero : q = 0#usize :=
UScalar.eq_of_val_eq (by simpa using hqzero)
have hNzero : N = 0 := by simp [N, hqzero]
have hloop0 :
num.imp.dec2flt.decimal_seq.DecimalSeq.round_loop
{ start := 0#usize, «end» := 0#usize }
d.num_digits d.digits 0#u64 = ok n := by
simpa [hqZero] using hnLoop
have hnumEqOne : d.num_digits = 1#usize := by
apply UScalar.eq_of_val_eq
have hcount : d.num_digits.val = 1 := by omega
simpa using hcount
have hloop01 := hloop0
rw [hnumEqOne] at hloop01
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hread0 := hread
rw [hqZero] at hread0
have hzeroAdd : 0#usize + 1#usize = ok 1#usize := by
simpa [hqZero, hqnextEq, hnumEqOne] using hqnextAdd
have hroundKeep : d.round = ok n := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hloop01,
hread0, hi1EqFive, htruncatedFalse, hqZero,
hnumEqOne, hzeroAdd]
have hnzero : n.val = 0 := by rw [hnN, hNzero]
refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
· rw [hXtie, hNzero]
simp
· rw [hnzero, hXtie, hNzero]
norm_num
· intro _
rw [hnzero]
· have hqpos : 0 < q.val := by omega
obtain ⟨qprev, hqprev, hqprevSpec⟩ :=
Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := q) (y := 1#usize) (by simpa using hqpos))
have hqprevVal : qprev.val = q.val - 1 := by
simpa using hqprevSpec.1
have hqprevAdd : qprev.val + 1 = q.val := by omega
have hqprevArray : qprev.val < d.digits.val.length := by
have ha : d.digits.val.length = 768 := by
simpa using d.digits.property
omega
obtain ⟨iprev, hiprev, hiprevVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(Array.index_usize_spec d.digits qprev (by
simpa [Array.length] using hqprevArray))
have hprevRead : d.digits.index_usize qprev = ok iprev := by
simpa using hiprev
let c := digits[qprev.val]
let P := (digits.take qprev.val).foldl (fun z x => 10 * z + x) 0
have hprevReadVal : iprev.val = c := by
rw [hiprevVal]
simp [c, digits, List.getElem_map, List.getElem_take]
have htake : digits.take q.val = digits.take qprev.val ++ [c] := by
have ht := List.take_succ_eq_append_getElem
(l := digits) (i := qprev.val) (by omega : qprev.val < digits.length)
simpa [c, hqprevAdd] using ht
have hNdecomp : N = 10 * P + c := by
dsimp [N, P]
rw [htake, List.foldl_append]
simp only [List.foldl_cons, List.foldl_nil]
have hNmod : N % 2 = c % 2 := by
rw [hNdecomp]
omega
have hbitVal : (1#u8 &&& iprev).val = c % 2 := by
rw [UScalar.val_and, hprevReadVal]
have hmask : (1#u8).val = 2 ^ 1 - 1 := by norm_num
rw [hmask]
calc
(2 ^ 1 - 1) &&& c = c &&& (2 ^ 1 - 1) := Nat.and_comm _ _
_ = c % 2 ^ 1 := Nat.and_two_pow_sub_one_eq_mod c 1
_ = c % 2 := by norm_num
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hqNeZero : q ≠ 0#usize := by
intro he
have hv := congrArg UScalar.val he
simp at hv
omega
have hqvalNeZero : q.val ≠ 0 := by omega
by_cases heven : N % 2 = 0
· have hcEven : c % 2 = 0 := by rw [← hNmod]; exact heven
have hbitZero : (1#u8 &&& iprev) = 0#u8 := by
apply UScalar.eq_of_val_eq
rw [hbitVal]
simp [hcEven]
have hroundKeep : d.round = ok n := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
hqvalNeZero, hqprev, hprevRead, hbitZero,
hqnextAdd]
have hnmod : n.val % 2 = 0 := by rw [hnN, heven]
refine ⟨n, hroundKeep, ?_, ?_, ?_⟩
· rw [hnN, hXtie]
· rw [hnN, hXtie]
linarith
· intro _
exact hnmod
· have hNmodlt : N % 2 < 2 := Nat.mod_lt _ (by omega)
have hodd : N % 2 = 1 := by omega
have hcOdd : c % 2 = 1 := by rw [← hNmod]; exact hodd
have hbitNeZero : (1#u8 &&& iprev) ≠ 0#u8 := by
intro he
have hv := congrArg UScalar.val he
have hzero : (1#u8 &&& iprev).val = 0 := by simpa using hv
rw [hbitVal] at hzero
omega
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
rw [hnN]
rw [hXtie] at hXhi
linarith
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ :=
Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hi1EqFive, htruncatedFalse, hqnextEq,
hqvalNeZero, hqprev, hprevRead, hbitNeZero, hnAddEq,
hqnextAdd]
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· rw [hXtie, hnnextReal]
linarith
· rw [hXtie, hnnextReal]
linarith
· intro _
have hnnextValNat : nnext.val = n.val + 1 := by simpa using hnAddVal
rw [hnnextValNat, hnN]
omega
· have hnonterminal : q.val + 1 < digits.length := by omega
have hNbelowV : (N : Real) + 1 / 2 <
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) := by
have ht := hFive.2 hnonterminal
rw [hVfrac] at ht
simpa [N] using ht
have hYabove : Y <= (N : Real) + 3 / 2 := by
rcases hsep (N + 1) with hbad | hgood
· have hVup :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 := by
have hu := hFrac.2.1
rw [hVfrac] at hu
simpa [N] using hu
exfalso
push_cast at hbad
linarith [hbad, hVup]
· have hgood' : Y <= (N : Real) + 3 / 2 := by
have hgood'' := hgood
push_cast at hgood''
convert hgood'' using 1 <;> ring
exact hgood'
have hXabove : (N : Real) + 1 / 2 < X :=
lt_of_lt_of_le hNbelowV hVleX
have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by rw [hnN]
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
linarith [hNnext, hXabove, hXhi]
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hi1EqFive : i1 = 5#u8 := by
apply UScalar.eq_of_val_eq
simpa [hreadVal] using hfive
have hqnextNe : qnext ≠ d.num_digits := by
intro he
have hqnv : qnext.val = q.val + 1 := by simpa using hqnextVal
have hv := congrArg UScalar.val he
change qnext.val = d.num_digits.val at hv
rw [hqnv] at hv
omega
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hi1EqFive,
hqnextNe, hnAddEq, hqnextAdd]
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· have hx := hXVY.trans_le hYabove
rw [hnnextReal]
exact le_of_lt (calc
X < (N : Real) + 3 / 2 := hx
_ = (N : Real) + 1 + 1 / 2 := by ring)
· rw [hnnextReal]
have hx' : (N : Real) + 1 < X + 1 / 2 := by
convert add_lt_add_right hXabove (1 / 2 : Real) using 1 <;> ring
exact le_of_lt hx'
· intro hmid
have hXstrict : X < (N : Real) + 3 / 2 :=
lt_of_lt_of_le hXVY hYabove
rcases hmid with hhi | hlo
· have hhi' : X = (N : Real) + 3 / 2 := by
calc
X = (nnext.val : Real) + 1 / 2 := hhi
_ = (N : Real) + 3 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_lt hXstrict) hhi')
· have hlo' : X = (N : Real) + 1 / 2 := by
calc
X = (nnext.val : Real) - 1 / 2 := hlo
_ = (N : Real) + 1 / 2 := by rw [hnnextReal]; ring
exact False.elim ((ne_of_gt hXabove) hlo')
· have hfabove : 5 < f := by omega
have hi1NeFive : i1 ≠ 5#u8 := by
intro he
have hv := congrArg UScalar.val he
simp [hreadVal] at hv
omega
have hi1AtLeastFive : 5 <= i1.val := by omega
have hNbelowV : (N : Real) + 1 / 2 <
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) :=
habove hfabove
have hYabove : Y <= (N : Real) + 3 / 2 := by
rcases hsep (N + 1) with hbad | hgood
· have hVup :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) <
(N : Real) + 1 := by
have hu := hFrac.2.1
rw [hVfrac] at hu
simpa [N] using hu
exfalso
push_cast at hbad
linarith [hbad, hVup]
· have hgood' : Y <= (N : Real) + 3 / 2 := by
have hgood'' := hgood
push_cast at hgood''
convert hgood'' using 1 <;> ring
exact hgood'
have hXabove : (N : Real) + 1 / 2 < X := by
exact lt_of_lt_of_le hNbelowV hVleX
have hNnext : (n.val : Real) + 1 = (N : Real) + 1 := by
rw [hnN]
have hnAddBound : n.val + 1 <= U64.max := by
rw [U64.max_eq]
have hlt2 : (n.val : Real) + 1 < (2 : Real) ^ 64 := by
linarith [hNnext, hXabove, hXhi]
have hltNat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hlt2
omega
obtain ⟨nnext, hnAdd, hnAddVal⟩ := Aeneas.Std.WP.spec_imp_exists
(U64.add_spec (x := n) (y := 1#u64) hnAddBound)
have hnAddEq : n + 1#u64 = ok nnext := by simpa using hnAdd
have hroundInc : d.round = ok nnext := by
simp [num.imp.dec2flt.decimal_seq.DecimalSeq.round, lift,
hnumNe, hpointNonneg,
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, hthreshold, hqRaw, hnLoop,
hqLt, hread, hreadVal, hi1NeFive, hnAddEq,
hqnextAdd]
all_goals omega
have hnnextReal : (nnext.val : Real) = (N : Real) + 1 := by
rw [hnAddVal, hnN]
norm_num
refine ⟨nnext, hroundInc, ?_, ?_, ?_⟩
· have hx := hXVY.trans_le hYabove
rw [hnnextReal]
linarith
· rw [hnnextReal]
linarith
· intro hmid
rcases hmid with hmid | hmid <;> rw [hnnextReal] at hmid <;> linarith
· have hqGe : d.num_digits.val <= q.val := by omega
have hNreal : (n.val : Real) =
(integer : Real) * (10 : Real) ^ ((q.val : Int) - (d.num_digits.val : Int)) := by
rw [hnValue.2 hqGe]
rw [Nat.cast_mul, Nat.cast_pow]
have hexp : (q.val : Int) - (d.num_digits.val : Int) =
((q.val - d.num_digits.val : Nat) : Int) := by omega
rw [hexp, zpow_natCast]
norm_num
have hVn :
((((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
(fun z x => 10 * z + x) 0 : Nat) : Real) *
(10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int)) =
(n.val : Real) := by
rw [hdigitsLen] at hV
rw [hV, hNreal]
have hSepN := hsep n.val
have hYbound : Y <= (n.val : Real) + 1 / 2 := by
rcases hSepN with hbad | hgood
· rw [hVn] at hbad
norm_num at hbad
· simpa using hgood
have hXbound : X < (n.val : Real) + 1 / 2 := by
exact lt_of_lt_of_le hXVY hYbound
have hnumNe : d.num_digits ≠ 0#usize := by
intro he
have hv := congrArg (fun x : UScalar UScalarTy.Usize => x.val) he
have hz : d.num_digits.val = 0 := by simpa using hv
omega
obtain ⟨i19, hi19, hi19val⟩ := Aeneas.Std.WP.spec_imp_exists
(UScalar.hcast_inBounds_spec .I32 19#usize (by scalar_tac))
have hcast19 : lift (UScalar.hcast .I32 19#usize) = ok 19#i32 := by
rw [hi19]
congr 1
apply IScalar.eq_of_val_eq
simpa using hi19val
have hcast19Raw : UScalar.hcast .I32 19#usize = 19#i32 := by
exact Result.ok.inj (by simpa [lift] using hcast19)
have hqRaw : IScalar.hcast .Usize d.decimal_point = q := by
exact Result.ok.inj (by simpa [lift] using hqCast)
have hpointVal : d.decimal_point.val < 19 := hpoint
have hthreshold : ¬ (19 : Int) <= d.decimal_point.val := by omega
have hqNotLt : ¬ q.val < d.num_digits.val := Nat.not_lt_of_ge hqGe
have hroundN : d.round = ok n := by
unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
rw [if_neg hnumNe]
rw [if_neg hpointNonneg]
simp only [
num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW,
hcast19Raw, lift]
simp [hthreshold, hqRaw, hnLoop, hqNotLt]
refine ⟨n, hroundN, ?_, ?_, ?_⟩
· exact le_of_lt hXbound
· have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
exact le_trans hnX (le_add_of_nonneg_right (by norm_num))
· intro hmid
have hnX : (n.val : Real) <= X := by rw [← hVn]; exact hVleX
rcases hmid with hmid | hmid
· exact False.elim ((ne_of_lt hXbound) hmid)
· have hlt : (n.val : Real) - 1 / 2 < n.val :=
sub_lt_self _ (by norm_num)
have hXlt : X < (n.val : Real) := by rw [hmid]; exact hlt
exact False.elim (not_lt_of_ge hnX hXlt)
))
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Inspect related source (117)
- MAX_DIGITS_WITHOUT_OVERFLOW · dec2flt/decimal_seq.rs:67 Program Rust
- round · dec2flt/decimal_seq.rs:97 Program Rust
- round_loop · dec2flt/decimal_seq.rs:107 Program Rust
More program dependencies (84)
- body · dec2flt/decimal_seq.rs:107 Program Rust
- DecimalSeq · dec2flt/decimal_seq.rs:22 Program Rust
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- BiasedFp · dec2flt/common.rs:56 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 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
- parse_long_mantissa · dec2flt/slow.rs:27 Program Rust
- parse_long_mantissa_loop0 · dec2flt/slow.rs:51 Program Rust
- body · dec2flt/slow.rs:51 Program Rust
- parse_long_mantissa_loop1 · dec2flt/slow.rs:69 Program Rust
- body · dec2flt/slow.rs:69 Program Rust
- parse_long_mantissa_loop2 · dec2flt/slow.rs:94 Program Rust
- body · dec2flt/slow.rs:94 Program Rust
- zero_pow2 · dec2flt/common.rs:66 Program Rust
- DECIMAL_POINT_RANGE · dec2flt/decimal_seq.rs:68 Program Rust
- left_shift · dec2flt/decimal_seq.rs:130 Program Rust
- left_shift_loop0 · dec2flt/decimal_seq.rs:139 Program Rust
- body · dec2flt/decimal_seq.rs:139 Program Rust
- left_shift_loop1 · dec2flt/decimal_seq.rs:153 Program Rust
- body · dec2flt/decimal_seq.rs:153 Program Rust
- right_shift · dec2flt/decimal_seq.rs:176 Program Rust
- right_shift_loop0 · dec2flt/decimal_seq.rs:181 Program Rust
- body · dec2flt/decimal_seq.rs:181 Program Rust
- right_shift_loop0_loop0 · dec2flt/decimal_seq.rs:191 Program Rust
- body · dec2flt/decimal_seq.rs:191 Program Rust
- right_shift_loop1 · dec2flt/decimal_seq.rs:210 Program Rust
- body · dec2flt/decimal_seq.rs:210 Program Rust
- right_shift_loop2 · dec2flt/decimal_seq.rs:217 Program Rust
- body · dec2flt/decimal_seq.rs:217 Program Rust
- parse_decimal_seq · dec2flt/decimal_seq.rs:288 Program Rust
- parse_decimal_seq_loop0 · dec2flt/decimal_seq.rs:296 Program Rust
- body · dec2flt/decimal_seq.rs:296 Program Rust
- parse_decimal_seq_loop1 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- parse_decimal_seq_loop2 · dec2flt/decimal_seq.rs:309 Program Rust
- body · dec2flt/decimal_seq.rs:309 Program Rust
- parse_decimal_seq_loop3 · dec2flt/decimal_seq.rs:313 Program Rust
- body · dec2flt/decimal_seq.rs:313 Program Rust
- parse_decimal_seq_loop4 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- parse_decimal_seq_loop5 · dec2flt/decimal_seq.rs:313 Program Rust
- body · dec2flt/decimal_seq.rs:313 Program Rust
- parse_decimal_seq_loop6 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- parse_decimal_seq_loop7 · dec2flt/decimal_seq.rs:368 Program Rust
- body · dec2flt/decimal_seq.rs:368 Program Rust
- MAX_SHIFT · dec2flt/slow.rs:28 Program Rust
- call · dec2flt/slow.rs:33 Program Rust
- read_u64 · dec2flt/common.rs:22 Program Rust
- write_u64 · dec2flt/common.rs:29 Program Rust
- is_8digits · dec2flt/common.rs:44 Program Rust
- default · dec2flt/decimal_seq.rs:34 Program Rust
- MAX_DIGITS · dec2flt/decimal_seq.rs:64 Program Rust
- trim · dec2flt/decimal_seq.rs:83 Program Rust
- trim_loop · dec2flt/decimal_seq.rs:92 Program Rust
- body · dec2flt/decimal_seq.rs:92 Program Rust
- count_trailing_zeros · dec2flt/decimal_seq.rs:236 Program Rust
- count_trailing_zeros_loop · dec2flt/decimal_seq.rs:240 Program Rust
- body · dec2flt/decimal_seq.rs:240 Program Rust
- final_decimal_point · dec2flt/decimal_seq.rs:269 Program Rust
- number_of_digits_decimal_left_shift · dec2flt/decimal_seq.rs:375 Program Rust
- number_of_digits_decimal_left_shift_loop · dec2flt/decimal_seq.rs:441 Program Rust
- body · dec2flt/decimal_seq.rs:441 Program Rust
- parse_digits_into_seq · dec2flt/decimal_seq.rs:257 Program Rust
- parse_digits_into_seq_loop · dec2flt/decimal_seq.rs:259 Program Rust
- body · dec2flt/decimal_seq.rs:259 Program Rust
- parse_scientific · dec2flt/parse.rs:121 Program Rust
- parse_scientific_loop · dec2flt/parse.rs:145 Program Rust
- body · dec2flt/parse.rs:145 Program Rust
- NUM_POWERS · dec2flt/slow.rs:29 Program Rust
- POWERS · dec2flt/slow.rs:30 Program Rust
- closure · dec2flt/slow.rs:33 Program Rust
- try_add_digit · dec2flt/decimal_seq.rs:74 Program Rust
- TABLE · dec2flt/decimal_seq.rs:377 Program Rust
- TABLE_POW5 · dec2flt/decimal_seq.rs:386 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
- DecimalSeqFractionalDigitInterval
- DecimalSeqRoundLoopPrefixCore
- DecimalSeqRoundLoopValue
- NumberHelperDecimalPrefixInterval
- DecimalValueBounds
- RoundArbitraryEndpoint
All 10 entries
- DecimalSeqFractionalDigitInterval
- DecimalSeqRoundLoopPrefixCore
- DecimalSeqRoundLoopValue
- NumberHelperDecimalPrefixInterval
- DecimalValueBounds
- RoundArbitraryEndpoint
- DecimalFoldBounds
- num imp dec2flt decimal seq DecimalSeq
- num imp dec2flt decimal seq DecimalSeq MAX DIGITS WITHOUT OVERFLOW
- num imp dec2flt decimal seq DecimalSeq round
Technical checking records
Local closure summary
{
"node": "P529_T1_RoundArbitraryEndpoint",
"closure_version": "patch_c_v1",
"active_decl_hash": "0cfe783033838b708c7f4832b6bac3577c27613cc2391071d2942db6e8075e50",
"active_statement_hash": "822bb6a9e82825dd9534d1eabb5c89c0b5e35a6002ac5a3f94b0cc296bec1dbb",
"kernel_axioms": [
"Classical.choice",
"Quot.sound",
"propext"
],
"toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
"lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
"checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
"approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
"axcheck_status": "Agreed",
"accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalSeqFractionalDigitInterval": "24315b479bbec2fd",
"DecimalSeqRoundLoopValue": "89c751600a31491b",
"P472_T1_DecimalValueBounds": "4b5c924b5d52bc9d",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "606c25078d23ad9c",
"num_imp_dec2flt_decimal_seq_DecimalSeq_round": "cb99c951cfc3a86f"
},
"strict_definition_deps": {
"num_imp_dec2flt_decimal_seq_DecimalSeq": "13e3cec939d02f51",
"num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "65634ac95536dcca",
"num_imp_dec2flt_decimal_seq_DecimalSeq_round": "74fa9a4c095ca758"
}
}Prose assessment
{
"label": "Recorded Sound: VerifierPass",
"assessment_status": "VerifierPass",
"fingerprint_relation": "matches current fingerprint",
"assessment_fingerprint": "sound-v2:d1aef3139c4dc7f7b691c0a3b5a7276bdf6ae1ab0f7458c1beb8fa1829aaae63",
"current_fingerprint": "sound-v2:d1aef3139c4dc7f7b691c0a3b5a7276bdf6ae1ab0f7458c1beb8fa1829aaae63",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"sound_status": "Pass",
"substantiveness_status": "Pass"
}