Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Reverse count on the actual consumed significand
Lean statement
Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.
import Tablet.DecimalDigitTrimDecomposition
import Tablet.DecimalSeqTrailingZeroValues
import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.DecimalSeqRenderedSplit
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P495_T1_TrailingCount]
theorem P495_T1_TrailingCount (s rest : Slice Std.U8) (x : DecimalLexeme)
(hsource : SourceValidByteSlice s) (hsign : x.leadingSign = none)
(hvalid : DecimalGoalVocabularyDefault.validNumeric x)
(hbytes : DecimalGoalVocabularyDefault.renderNumeric x =
DecimalGoalVocabularyDefault.bytes s)
(hrest : rest.val.map (fun b => b.val) = match x.exponent with
| none => []
| some (upper, sign, ds) =>
(if upper then 69 else 101) ::
(match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
ds.map (fun z => 48 + z)) :
let S := x.integerDigits ++
match x.fractionDigits with | none => [] | some ds => ds
let C := S.dropWhile (fun z => z == 0)
let T := (C.reverse.dropWhile (fun z => z == 0)).reverse
C ≠ [] ->
exists consumed zeros : Std.Usize,
Slice.len s - Slice.len rest = ok consumed /\
num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed = ok zeros /\
zeros.val = C.length - T.length /\ zeros.val < C.length /\
consumed.val = x.integerDigits.length +
(match x.fractionDigits with | none => 0 | some ds => 1 + ds.length) := 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
dsimp only
intro hCne
let F := match x.fractionDigits with | none => [] | some ds => ds
let S := x.integerDigits ++ F
let C := S.dropWhile (fun z => z == 0)
let T := (C.reverse.dropWhile (fun z => z == 0)).reverse
have hdecomp := DecimalDigitTrimDecomposition S
dsimp only [S, C, T] at hdecomp
obtain ⟨ell, q, hell, hq, hS, hC, hChead, hTlast⟩ := hdecomp
change C = T ++ List.replicate q 0 at hC
change C = [] ∨ C.head? ≠ some 0 at hChead
change T = [] ∨ T.getLast? ≠ some 0 at hTlast
have hTne : T ≠ [] := by
intro h
have : C = List.replicate q 0 := by simpa [h] using hC
have hhead := hChead.resolve_left hCne
have hqpos : q > 0 := by
by_contra hn
have hqzero : q = 0 := by omega
have hcempty : C = [] := by simpa [hqzero] using this
exact hCne hcempty
have hhead0 : C.head? = some 0 := by
rw [this]
cases q with
| zero => simp at hqpos
| succ q => rfl
exact hhead hhead0
have hq_lt : q < C.length := by
have htpos : 0 < T.length := by
cases hT : T with
| nil => exact False.elim (hTne hT)
| cons a tail => simp
have hlen : C.length = T.length + q := by rw [hC]; simp
omega
have hboundDigits : S.length ≤ s.val.length := by
have hlen0 := congrArg List.length hbytes
have hlen : (DecimalGoalVocabularyDefault.renderNumeric x).length = s.val.length := by
simpa [DecimalGoalVocabularyDefault] using hlen0
cases hf : x.fractionDigits <;> cases he : x.exponent <;>
simp [DecimalGoalVocabularyDefault, DecimalGoalVocabulary, hsign, hf, he, S, F]
at hlen ⊢ <;> omega
have hTlast' : T.getLast? ≠ some 0 := by
rcases hTlast with h | h
· exact False.elim (hTne h)
· exact h
let A := List.replicate ell 0 ++ T
have hAne : A ≠ [] := by simp [A, hTne]
have hAlast : A.getLast? ≠ some 0 := by
change (List.replicate ell 0 ++ T).getLast? ≠ some 0
rw [List.getLast?_append_of_ne_nil _ hTne]
exact hTlast'
have hS' : S = List.replicate ell 0 ++ C := by simpa [S] using hS
have hsig : S = A ++ List.replicate q 0 := by
rw [hS', hC]
simp [A, List.append_assoc]
have hIF : x.integerDigits ++ F = A ++ List.replicate q 0 := by
simpa [S] using hsig
cases hf : x.fractionDigits with
| none =>
have hif : x.integerDigits ++ [] = A ++ List.replicate q 0 := by
simpa [F, hf] using hIF
obtain ⟨prefx, suffix, hrender, hprefix, hsuffix, hcount⟩ :=
DecimalSeqRenderedSplit x.integerDigits [] A q hif hAne hAlast
rcases hprefix with ⟨pre, b, hpre, hb48, hb46⟩
have hsuffne : suffix ≠ [] := by
intro hsuff
subst suffix
have heq : x.integerDigits.map (fun n => 48 + n) ++ [46] = pre ++ [b] := by
simpa [hpre] using hrender
have hlast := congrArg List.getLast? heq
rw [List.getLast?_append_of_ne_nil _ (by simp)] at hlast
rw [List.getLast?_append_of_ne_nil _ (by simp)] at hlast
simp at hlast
exact hb46 hlast
have hsufflast : suffix.getLast? = some 46 := by
have hh := congrArg List.getLast? hrender
rw [List.getLast?_append_of_ne_nil _ hsuffne] at hh
rw [List.getLast?_append_of_ne_nil _ (by simp)] at hh
simpa using hh.symm
have hsufflastVal : suffix.getLast hsuffne = 46 := by
have hh := List.getLast?_eq_getLast hsuffne
rw [hsufflast] at hh
exact Option.some.inj hh.symm
have hdrop : suffix.dropLast ++ [46] = suffix := by
simpa [hsufflastVal] using List.dropLast_append_getLast hsuffne
let suffix' := suffix.dropLast
have hrenderNoDot : x.integerDigits.map (fun n => 48 + n) = prefx ++ suffix' := by
have hh : x.integerDigits.map (fun n => 48 + n) ++ [46] =
(prefx ++ suffix') ++ [46] := by
rw [← hdrop] at hrender
simpa [suffix', List.append_assoc] using hrender
exact List.append_cancel_right hh
have hsuffix' : ∀ b, b ∈ suffix' → b = 48 ∨ b = 46 := by
intro c hc
apply hsuffix c
rw [← hdrop]
exact List.mem_append_left _ hc
have hcount' : suffix'.count 48 = q := by
have hh := congrArg (List.count 48) hdrop
simp only [List.count_append, List.count_singleton] at hh
have hc : suffix'.count 48 = suffix.count 48 := by
simpa [suffix'] using hh
simpa [hcount] using hc
let E : List Nat := match x.exponent with
| none => []
| some (upper, sign, es) =>
(if upper then 69 else 101) ::
((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
es.map (fun n => 48 + n))
have hrest' : rest.val.map (fun b => b.val) = E := by
simpa [E] using hrest
have hbytesSig : s.val.map (fun b => b.val) =
x.integerDigits.map (fun n => 48 + n) ++ E := by
simpa [DecimalGoalVocabularyDefault, hsign, hf, E, List.append_assoc] using hbytes.symm
have hrenderE : (x.integerDigits.map (fun n => 48 + n)) ++ E =
(prefx ++ suffix') ++ E := congrArg (fun z : List Nat => z ++ E) hrenderNoDot
have hfull : s.val.map (fun b => b.val) = prefx ++ suffix' ++
rest.val.map (fun b => b.val) := by
calc
_ = x.integerDigits.map (fun n => 48 + n) ++ E := hbytesSig
_ = prefx ++ suffix' ++ rest.val.map (fun b => b.val) := by
rw [hrenderE, hrest']
have hlength := congrArg List.length hfull
simp only [List.length_map, List.length_append] at hlength
have hcursorExists := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := Slice.len s) (y := Slice.len rest) (by
change rest.val.length ≤ s.val.length
omega))
obtain ⟨consumed, hsub, hsubval⟩ := hcursorExists
rcases hsubval with ⟨hsubval, hsuble⟩
change consumed.val = (Slice.len s).val - (Slice.len rest).val at hsubval
have hcursor : consumed.val = (prefx ++ suffix').length := by
rw [Slice.len_val, Slice.len_val] at hsubval
change consumed.val = s.val.length - rest.val.length at hsubval
have hlen' : s.val.length = (prefx ++ suffix').length + rest.val.length := by
simpa [List.length_append, Nat.add_assoc] using hlength
rw [hlen'] at hsubval
omega
have hCS : C.length ≤ S.length := by
have hh := congrArg List.length hS'
simp only [List.length_append, List.length_replicate] at hh
omega
have hqSource : q ≤ s.val.length := by
have := hboundDigits
omega
have hwidth := Dec2Flt.P340_T1_selected_target_numBits
have hbound : (0#usize).val + suffix'.count 48 ≤ UScalar.max .Usize := by
rw [hcount']
have hs : s.val.length ≤ 9223372036854775807 := hsource
have hm : 9223372036854775807 ≤ UScalar.max .Usize := by
rw [UScalar.max_USize_eq]
have hp := Usize.max_succ_eq_pow
rw [hwidth] at hp
norm_num at hp
omega
have hz : (0#usize).val = 0 := rfl
rw [hz]
omega
have hprefix' : prefx = [] ∨ ∃ pre b, prefx = pre ++ [b] ∧
b ≠ 48 ∧ b ≠ 46 := Or.inr ⟨pre, b, hpre, hb48, hb46⟩
obtain ⟨zeros, hloop, hzeros⟩ := DecimalSeqTrailingZeroValues s prefx suffix'
(rest.val.map (fun b => b.val)) (0#usize) consumed hfull hprefix'
hsuffix' hcursor hbound
have hcall : num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed =
ok zeros := by
simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros, hloop]
have hqvalue : q = C.length - T.length := by simpa [C, T] using hq
have hqvalue' : q = (List.dropWhile (fun z => z == 0)
(x.integerDigits ++ [])).length - (List.dropWhile (fun z => z == 0)
(List.dropWhile (fun z => z == 0) (x.integerDigits ++ [])).reverse).reverse.length := by
simpa [F, hf] using hq
have hsiglen : (prefx ++ suffix').length = x.integerDigits.length := by
have hh := congrArg List.length hrenderNoDot
simpa only [List.length_append, List.length_map] using hh.symm
have hzq : zeros.val = q := by simpa [hcount'] using hzeros
refine ⟨consumed, zeros, hsub, hcall, ?_, ?_, ?_⟩
· simpa [F, hf] using hzq.trans hqvalue'
· have hq_lt' : q < (List.dropWhile (fun z => z == 0)
(x.integerDigits ++ [])).length := by simpa [F, hf, C, S] using hq_lt
simpa [hzq, F, hf] using hq_lt'
· rw [hcursor]
simp only [hf]
exact hsiglen
| some ds =>
have hif : x.integerDigits ++ ds = A ++ List.replicate q 0 := by
simpa [F, hf] using hIF
obtain ⟨prefx, suffix, hrender, hprefix, hsuffix, hcount⟩ :=
DecimalSeqRenderedSplit x.integerDigits ds A q hif hAne hAlast
let E : List Nat := match x.exponent with
| none => []
| some (upper, sign, es) =>
(if upper then 69 else 101) ::
((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
es.map (fun n => 48 + n))
have hrest' : rest.val.map (fun b => b.val) = E := by
simpa [E] using hrest
have hbytesSig :
s.val.map (fun b => b.val) =
x.integerDigits.map (fun n => 48 + n) ++
(46 :: ds.map (fun n => 48 + n)) ++ E := by
simpa [DecimalGoalVocabularyDefault, hsign, hf, E, List.append_assoc] using hbytes.symm
have hrenderE :
(x.integerDigits.map (fun n => 48 + n) ++
(46 :: ds.map (fun n => 48 + n))) ++ E =
(prefx ++ suffix) ++ E := congrArg (fun z : List Nat => z ++ E) hrender
have hfull : s.val.map (fun b => b.val) =
prefx ++ suffix ++ rest.val.map (fun b => b.val) := by
calc
_ = (x.integerDigits.map (fun n => 48 + n) ++
(46 :: ds.map (fun n => 48 + n))) ++ E := by
simpa [List.append_assoc] using hbytesSig
_ = prefx ++ suffix ++ rest.val.map (fun b => b.val) := by
rw [hrenderE, hrest']
have hlength := congrArg List.length hfull
simp only [List.length_map, List.length_append] at hlength
have hcursorExists := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := Slice.len s) (y := Slice.len rest) (by
change rest.val.length ≤ s.val.length
omega))
obtain ⟨consumed, hsub, hsubval⟩ := hcursorExists
rcases hsubval with ⟨hsubval, hsuble⟩
change consumed.val = (Slice.len s).val - (Slice.len rest).val at hsubval
have hcursor : consumed.val = (prefx ++ suffix).length := by
rw [Slice.len_val, Slice.len_val] at hsubval
change consumed.val = s.val.length - rest.val.length at hsubval
have hlen' : s.val.length = (prefx ++ suffix).length + rest.val.length := by
simpa [List.length_append, Nat.add_assoc] using hlength
rw [hlen'] at hsubval
omega
have hCS : C.length ≤ S.length := by
have hh := congrArg List.length hS'
simp only [List.length_append, List.length_replicate] at hh
omega
have hqSource : q ≤ s.val.length := by
have := hboundDigits
omega
have hwidth := Dec2Flt.P340_T1_selected_target_numBits
have hbound : (0#usize).val + suffix.count 48 ≤ UScalar.max .Usize := by
rw [hcount]
have hs : s.val.length ≤ 9223372036854775807 := hsource
have hm : 9223372036854775807 ≤ UScalar.max .Usize := by
rw [UScalar.max_USize_eq]
have hp := Usize.max_succ_eq_pow
rw [hwidth] at hp
norm_num at hp
omega
have hz : (0#usize).val = 0 := rfl
rw [hz]
omega
obtain ⟨zeros, hloop, hzeros⟩ := DecimalSeqTrailingZeroValues s prefx suffix
(rest.val.map (fun b => b.val)) (0#usize) consumed hfull (Or.inr hprefix) hsuffix
hcursor hbound
have hcall : num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed =
ok zeros := by
simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros, hloop]
have hqvalue : q = C.length - T.length := by simpa [C, T] using hq
have hqvalue' : q =
(List.dropWhile (fun z => z == 0) (x.integerDigits ++ ds)).length -
(List.dropWhile (fun z => z == 0)
(List.dropWhile (fun z => z == 0) (x.integerDigits ++ ds)).reverse).reverse.length := by
simpa [F, hf] using hq
have hsiglen : (prefx ++ suffix).length =
x.integerDigits.length + (1 + ds.length) := by
have hh := congrArg List.length hrender
have hh' : (prefx ++ suffix).length =
x.integerDigits.length + (ds.length + 1) := by
simpa only [List.length_append, List.length_cons, List.length_map] using hh.symm
omega
have hzq : zeros.val = q := by simpa [hcount] using hzeros
have hsiglen' : (prefx ++ suffix).length =
x.integerDigits.length +
(match x.fractionDigits with | none => 0 | some ds => 1 + ds.length) := by
simpa [hf] using hsiglen
refine ⟨consumed, zeros, hsub, hcall, ?_, ?_, ?_⟩
· simpa [F, hf] using hzq.trans hqvalue'
· have hq_lt' : q < (List.dropWhile (fun z => z == 0)
(x.integerDigits ++ ds)).length := by simpa [F, hf, C, S] using hq_lt
simpa [hzq, F, hf] using hq_lt'
· rw [hcursor]
simp only [hf]
omega
end dec2flt_model
Download Lean source · Download TeX source
Complete Lean module
import Tablet.DecimalDigitTrimDecomposition
import Tablet.DecimalSeqTrailingZeroValues
import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.DecimalSeqRenderedSplit
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P495_T1_TrailingCount]
theorem P495_T1_TrailingCount (s rest : Slice Std.U8) (x : DecimalLexeme)
(hsource : SourceValidByteSlice s) (hsign : x.leadingSign = none)
(hvalid : DecimalGoalVocabularyDefault.validNumeric x)
(hbytes : DecimalGoalVocabularyDefault.renderNumeric x =
DecimalGoalVocabularyDefault.bytes s)
(hrest : rest.val.map (fun b => b.val) = match x.exponent with
| none => []
| some (upper, sign, ds) =>
(if upper then 69 else 101) ::
(match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
ds.map (fun z => 48 + z)) :
let S := x.integerDigits ++
match x.fractionDigits with | none => [] | some ds => ds
let C := S.dropWhile (fun z => z == 0)
let T := (C.reverse.dropWhile (fun z => z == 0)).reverse
C ≠ [] ->
exists consumed zeros : Std.Usize,
Slice.len s - Slice.len rest = ok consumed /\
num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed = ok zeros /\
zeros.val = C.length - T.length /\ zeros.val < C.length /\
consumed.val = x.integerDigits.length +
(match x.fractionDigits with | none => 0 | some ds => 1 + ds.length) := by
-- BODY
dsimp only
intro hCne
let F := match x.fractionDigits with | none => [] | some ds => ds
let S := x.integerDigits ++ F
let C := S.dropWhile (fun z => z == 0)
let T := (C.reverse.dropWhile (fun z => z == 0)).reverse
have hdecomp := DecimalDigitTrimDecomposition S
dsimp only [S, C, T] at hdecomp
obtain ⟨ell, q, hell, hq, hS, hC, hChead, hTlast⟩ := hdecomp
change C = T ++ List.replicate q 0 at hC
change C = [] ∨ C.head? ≠ some 0 at hChead
change T = [] ∨ T.getLast? ≠ some 0 at hTlast
have hTne : T ≠ [] := by
intro h
have : C = List.replicate q 0 := by simpa [h] using hC
have hhead := hChead.resolve_left hCne
have hqpos : q > 0 := by
by_contra hn
have hqzero : q = 0 := by omega
have hcempty : C = [] := by simpa [hqzero] using this
exact hCne hcempty
have hhead0 : C.head? = some 0 := by
rw [this]
cases q with
| zero => simp at hqpos
| succ q => rfl
exact hhead hhead0
have hq_lt : q < C.length := by
have htpos : 0 < T.length := by
cases hT : T with
| nil => exact False.elim (hTne hT)
| cons a tail => simp
have hlen : C.length = T.length + q := by rw [hC]; simp
omega
have hboundDigits : S.length ≤ s.val.length := by
have hlen0 := congrArg List.length hbytes
have hlen : (DecimalGoalVocabularyDefault.renderNumeric x).length = s.val.length := by
simpa [DecimalGoalVocabularyDefault] using hlen0
cases hf : x.fractionDigits <;> cases he : x.exponent <;>
simp [DecimalGoalVocabularyDefault, DecimalGoalVocabulary, hsign, hf, he, S, F]
at hlen ⊢ <;> omega
have hTlast' : T.getLast? ≠ some 0 := by
rcases hTlast with h | h
· exact False.elim (hTne h)
· exact h
let A := List.replicate ell 0 ++ T
have hAne : A ≠ [] := by simp [A, hTne]
have hAlast : A.getLast? ≠ some 0 := by
change (List.replicate ell 0 ++ T).getLast? ≠ some 0
rw [List.getLast?_append_of_ne_nil _ hTne]
exact hTlast'
have hS' : S = List.replicate ell 0 ++ C := by simpa [S] using hS
have hsig : S = A ++ List.replicate q 0 := by
rw [hS', hC]
simp [A, List.append_assoc]
have hIF : x.integerDigits ++ F = A ++ List.replicate q 0 := by
simpa [S] using hsig
cases hf : x.fractionDigits with
| none =>
have hif : x.integerDigits ++ [] = A ++ List.replicate q 0 := by
simpa [F, hf] using hIF
obtain ⟨prefx, suffix, hrender, hprefix, hsuffix, hcount⟩ :=
DecimalSeqRenderedSplit x.integerDigits [] A q hif hAne hAlast
rcases hprefix with ⟨pre, b, hpre, hb48, hb46⟩
have hsuffne : suffix ≠ [] := by
intro hsuff
subst suffix
have heq : x.integerDigits.map (fun n => 48 + n) ++ [46] = pre ++ [b] := by
simpa [hpre] using hrender
have hlast := congrArg List.getLast? heq
rw [List.getLast?_append_of_ne_nil _ (by simp)] at hlast
rw [List.getLast?_append_of_ne_nil _ (by simp)] at hlast
simp at hlast
exact hb46 hlast
have hsufflast : suffix.getLast? = some 46 := by
have hh := congrArg List.getLast? hrender
rw [List.getLast?_append_of_ne_nil _ hsuffne] at hh
rw [List.getLast?_append_of_ne_nil _ (by simp)] at hh
simpa using hh.symm
have hsufflastVal : suffix.getLast hsuffne = 46 := by
have hh := List.getLast?_eq_getLast hsuffne
rw [hsufflast] at hh
exact Option.some.inj hh.symm
have hdrop : suffix.dropLast ++ [46] = suffix := by
simpa [hsufflastVal] using List.dropLast_append_getLast hsuffne
let suffix' := suffix.dropLast
have hrenderNoDot : x.integerDigits.map (fun n => 48 + n) = prefx ++ suffix' := by
have hh : x.integerDigits.map (fun n => 48 + n) ++ [46] =
(prefx ++ suffix') ++ [46] := by
rw [← hdrop] at hrender
simpa [suffix', List.append_assoc] using hrender
exact List.append_cancel_right hh
have hsuffix' : ∀ b, b ∈ suffix' → b = 48 ∨ b = 46 := by
intro c hc
apply hsuffix c
rw [← hdrop]
exact List.mem_append_left _ hc
have hcount' : suffix'.count 48 = q := by
have hh := congrArg (List.count 48) hdrop
simp only [List.count_append, List.count_singleton] at hh
have hc : suffix'.count 48 = suffix.count 48 := by
simpa [suffix'] using hh
simpa [hcount] using hc
let E : List Nat := match x.exponent with
| none => []
| some (upper, sign, es) =>
(if upper then 69 else 101) ::
((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
es.map (fun n => 48 + n))
have hrest' : rest.val.map (fun b => b.val) = E := by
simpa [E] using hrest
have hbytesSig : s.val.map (fun b => b.val) =
x.integerDigits.map (fun n => 48 + n) ++ E := by
simpa [DecimalGoalVocabularyDefault, hsign, hf, E, List.append_assoc] using hbytes.symm
have hrenderE : (x.integerDigits.map (fun n => 48 + n)) ++ E =
(prefx ++ suffix') ++ E := congrArg (fun z : List Nat => z ++ E) hrenderNoDot
have hfull : s.val.map (fun b => b.val) = prefx ++ suffix' ++
rest.val.map (fun b => b.val) := by
calc
_ = x.integerDigits.map (fun n => 48 + n) ++ E := hbytesSig
_ = prefx ++ suffix' ++ rest.val.map (fun b => b.val) := by
rw [hrenderE, hrest']
have hlength := congrArg List.length hfull
simp only [List.length_map, List.length_append] at hlength
have hcursorExists := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := Slice.len s) (y := Slice.len rest) (by
change rest.val.length ≤ s.val.length
omega))
obtain ⟨consumed, hsub, hsubval⟩ := hcursorExists
rcases hsubval with ⟨hsubval, hsuble⟩
change consumed.val = (Slice.len s).val - (Slice.len rest).val at hsubval
have hcursor : consumed.val = (prefx ++ suffix').length := by
rw [Slice.len_val, Slice.len_val] at hsubval
change consumed.val = s.val.length - rest.val.length at hsubval
have hlen' : s.val.length = (prefx ++ suffix').length + rest.val.length := by
simpa [List.length_append, Nat.add_assoc] using hlength
rw [hlen'] at hsubval
omega
have hCS : C.length ≤ S.length := by
have hh := congrArg List.length hS'
simp only [List.length_append, List.length_replicate] at hh
omega
have hqSource : q ≤ s.val.length := by
have := hboundDigits
omega
have hwidth := Dec2Flt.P340_T1_selected_target_numBits
have hbound : (0#usize).val + suffix'.count 48 ≤ UScalar.max .Usize := by
rw [hcount']
have hs : s.val.length ≤ 9223372036854775807 := hsource
have hm : 9223372036854775807 ≤ UScalar.max .Usize := by
rw [UScalar.max_USize_eq]
have hp := Usize.max_succ_eq_pow
rw [hwidth] at hp
norm_num at hp
omega
have hz : (0#usize).val = 0 := rfl
rw [hz]
omega
have hprefix' : prefx = [] ∨ ∃ pre b, prefx = pre ++ [b] ∧
b ≠ 48 ∧ b ≠ 46 := Or.inr ⟨pre, b, hpre, hb48, hb46⟩
obtain ⟨zeros, hloop, hzeros⟩ := DecimalSeqTrailingZeroValues s prefx suffix'
(rest.val.map (fun b => b.val)) (0#usize) consumed hfull hprefix'
hsuffix' hcursor hbound
have hcall : num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed =
ok zeros := by
simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros, hloop]
have hqvalue : q = C.length - T.length := by simpa [C, T] using hq
have hqvalue' : q = (List.dropWhile (fun z => z == 0)
(x.integerDigits ++ [])).length - (List.dropWhile (fun z => z == 0)
(List.dropWhile (fun z => z == 0) (x.integerDigits ++ [])).reverse).reverse.length := by
simpa [F, hf] using hq
have hsiglen : (prefx ++ suffix').length = x.integerDigits.length := by
have hh := congrArg List.length hrenderNoDot
simpa only [List.length_append, List.length_map] using hh.symm
have hzq : zeros.val = q := by simpa [hcount'] using hzeros
refine ⟨consumed, zeros, hsub, hcall, ?_, ?_, ?_⟩
· simpa [F, hf] using hzq.trans hqvalue'
· have hq_lt' : q < (List.dropWhile (fun z => z == 0)
(x.integerDigits ++ [])).length := by simpa [F, hf, C, S] using hq_lt
simpa [hzq, F, hf] using hq_lt'
· rw [hcursor]
simp only [hf]
exact hsiglen
| some ds =>
have hif : x.integerDigits ++ ds = A ++ List.replicate q 0 := by
simpa [F, hf] using hIF
obtain ⟨prefx, suffix, hrender, hprefix, hsuffix, hcount⟩ :=
DecimalSeqRenderedSplit x.integerDigits ds A q hif hAne hAlast
let E : List Nat := match x.exponent with
| none => []
| some (upper, sign, es) =>
(if upper then 69 else 101) ::
((match sign with | none => [] | some neg => [if neg then 45 else 43]) ++
es.map (fun n => 48 + n))
have hrest' : rest.val.map (fun b => b.val) = E := by
simpa [E] using hrest
have hbytesSig :
s.val.map (fun b => b.val) =
x.integerDigits.map (fun n => 48 + n) ++
(46 :: ds.map (fun n => 48 + n)) ++ E := by
simpa [DecimalGoalVocabularyDefault, hsign, hf, E, List.append_assoc] using hbytes.symm
have hrenderE :
(x.integerDigits.map (fun n => 48 + n) ++
(46 :: ds.map (fun n => 48 + n))) ++ E =
(prefx ++ suffix) ++ E := congrArg (fun z : List Nat => z ++ E) hrender
have hfull : s.val.map (fun b => b.val) =
prefx ++ suffix ++ rest.val.map (fun b => b.val) := by
calc
_ = (x.integerDigits.map (fun n => 48 + n) ++
(46 :: ds.map (fun n => 48 + n))) ++ E := by
simpa [List.append_assoc] using hbytesSig
_ = prefx ++ suffix ++ rest.val.map (fun b => b.val) := by
rw [hrenderE, hrest']
have hlength := congrArg List.length hfull
simp only [List.length_map, List.length_append] at hlength
have hcursorExists := Aeneas.Std.WP.spec_imp_exists
(UScalar.sub_spec (x := Slice.len s) (y := Slice.len rest) (by
change rest.val.length ≤ s.val.length
omega))
obtain ⟨consumed, hsub, hsubval⟩ := hcursorExists
rcases hsubval with ⟨hsubval, hsuble⟩
change consumed.val = (Slice.len s).val - (Slice.len rest).val at hsubval
have hcursor : consumed.val = (prefx ++ suffix).length := by
rw [Slice.len_val, Slice.len_val] at hsubval
change consumed.val = s.val.length - rest.val.length at hsubval
have hlen' : s.val.length = (prefx ++ suffix).length + rest.val.length := by
simpa [List.length_append, Nat.add_assoc] using hlength
rw [hlen'] at hsubval
omega
have hCS : C.length ≤ S.length := by
have hh := congrArg List.length hS'
simp only [List.length_append, List.length_replicate] at hh
omega
have hqSource : q ≤ s.val.length := by
have := hboundDigits
omega
have hwidth := Dec2Flt.P340_T1_selected_target_numBits
have hbound : (0#usize).val + suffix.count 48 ≤ UScalar.max .Usize := by
rw [hcount]
have hs : s.val.length ≤ 9223372036854775807 := hsource
have hm : 9223372036854775807 ≤ UScalar.max .Usize := by
rw [UScalar.max_USize_eq]
have hp := Usize.max_succ_eq_pow
rw [hwidth] at hp
norm_num at hp
omega
have hz : (0#usize).val = 0 := rfl
rw [hz]
omega
obtain ⟨zeros, hloop, hzeros⟩ := DecimalSeqTrailingZeroValues s prefx suffix
(rest.val.map (fun b => b.val)) (0#usize) consumed hfull (Or.inr hprefix) hsuffix
hcursor hbound
have hcall : num.imp.dec2flt.decimal_seq.count_trailing_zeros s consumed =
ok zeros := by
simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros, hloop]
have hqvalue : q = C.length - T.length := by simpa [C, T] using hq
have hqvalue' : q =
(List.dropWhile (fun z => z == 0) (x.integerDigits ++ ds)).length -
(List.dropWhile (fun z => z == 0)
(List.dropWhile (fun z => z == 0) (x.integerDigits ++ ds)).reverse).reverse.length := by
simpa [F, hf] using hq
have hsiglen : (prefx ++ suffix).length =
x.integerDigits.length + (1 + ds.length) := by
have hh := congrArg List.length hrender
have hh' : (prefx ++ suffix).length =
x.integerDigits.length + (ds.length + 1) := by
simpa only [List.length_append, List.length_cons, List.length_map] using hh.symm
omega
have hzq : zeros.val = q := by simpa [hcount] using hzeros
have hsiglen' : (prefx ++ suffix).length =
x.integerDigits.length +
(match x.fractionDigits with | none => 0 | some ds => 1 + ds.length) := by
simpa [hf] using hsiglen
refine ⟨consumed, zeros, hsub, hcall, ?_, ?_, ?_⟩
· simpa [F, hf] using hzq.trans hqvalue'
· have hq_lt' : q < (List.dropWhile (fun z => z == 0)
(x.integerDigits ++ ds)).length := by simpa [F, hf, C, S] using hq_lt
simpa [hzq, F, hf] using hq_lt'
· rw [hcursor]
simp only [hf]
omega
end dec2flt_model
Proof structure
Follow the declared dependencies to read the supporting statements and proofs.
Uses
Used by
Inspect related source (47)
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
More program dependencies (14)
- 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
- DecimalSeq · dec2flt/decimal_seq.rs:22 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
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
Standard-library dependencies (30)
- clone · core/src/clone.rs:698 Rust library
- div · core/src/ops/arith.rs:515 Rust library
- mul · core/src/ops/arith.rs:350 Rust library
- neg · core/src/ops/arith.rs:719 Rust library
- bitand · core/src/ops/bit.rs:178 Rust library
- bitor · core/src/ops/bit.rs:282 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- shr · core/src/ops/bit.rs:612 Rust library
- EXPONENT_MASK · core/src/num/f64.rs:615 Rust library
- INFINITY · core/src/num/f64.rs:512 Rust library
- MANTISSA_DIGITS · core/src/num/f64.rs:412 Rust library
- MANTISSA_MASK · core/src/num/f64.rs:637 Rust library
- NAN · core/src/num/f64.rs:509 Rust library
- NEG_INFINITY · core/src/num/f64.rs:515 Rust library
- classify · core/src/num/f64.rs:782 Rust library
- from_bits · core/src/num/f64.rs:1269 Rust library
- to_bits · core/src/num/f64.rs:1223 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 Rust library
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- first · core/src/slice/mod.rs:155 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
Related source and models come from the extraction records and declared dependencies.
Proof assumptions and review
Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.
Transitive assumptions
- Classical.choice
- Dec2Flt.P340_T1_selected_target_numBits
- 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
- DecimalDigitTrimDecomposition
- DecimalGoalVocabulary
- DecimalGoalVocabularyDefault
- DecimalLexeme
- DecimalSeqRenderedSplit
- DecimalSeqTrailingZeroLoopExact
All 13 entries
- DecimalDigitTrimDecomposition
- DecimalGoalVocabulary
- DecimalGoalVocabularyDefault
- DecimalLexeme
- DecimalSeqRenderedSplit
- DecimalSeqTrailingZeroLoopExact
- DecimalSeqTrailingZeroValues
- F64
- TrailingCount
- SourceValidByteSlice
- num imp dec2flt common BiasedFp
- num imp dec2flt decimal Decimal
- num imp dec2flt decimal seq count trailing zeros
Technical checking records
Local closure summary
{
"node": "P495_T1_TrailingCount",
"closure_version": "patch_c_v1",
"active_decl_hash": "6b0ca48a7887c78ce3d707cd34be88072b5e6442ee29c8dea73277e5db0ca79c",
"active_statement_hash": "eff4e2c15e2afe4d3b71e61eb97e4d05a43322562f6336d59f9f40913e476171",
"kernel_axioms": [
"Classical.choice",
"Dec2Flt.P340_T1_selected_target_numBits",
"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-131",
"boundary_theorems": {},
"strict_theorem_deps": {
"DecimalDigitTrimDecomposition": "74d5424585de54f0",
"DecimalGoalVocabularyDefault": "94cb4c406f8efb78",
"DecimalSeqRenderedSplit": "8d4db985a4bb4317",
"DecimalSeqTrailingZeroValues": "9bf98eea90c6e7c3"
},
"strict_definition_deps": {
"DecimalGoalVocabulary": "c08dd4b4aea4abb4",
"DecimalGoalVocabularyDefault": "4634372c8f7a7f44",
"DecimalLexeme": "250be645520aec86",
"F64": "ec4e25fbaf4e11d4",
"SourceValidByteSlice": "3dc42716a6378d26",
"num_imp_dec2flt_common_BiasedFp": "5c88d5d7976c5e8f",
"num_imp_dec2flt_decimal_Decimal": "4c41a56bf44cb31e",
"num_imp_dec2flt_decimal_seq_count_trailing_zeros": "7a77bc9e01c1a09b"
}
}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:b7d09d6421b228b3c7c90f33897727c54828551b6e404c943c38bdf637b1ccbd",
"superseded_for_theorem_closure": false
}Review verdicts
{
"corr_status": "Pass",
"substantiveness_status": "Pass"
}