P495_T1_TrailingCount

Lean checked

Statement

This explanation was written alongside the formalization to describe the statement and its reasoning.

Lemma · Reverse count on the actual consumed significand

Let s render a source-valid unsigned valid lexeme x, and let u render exactly its optional e/E exponent suffix. Let S be the full significand digit list, C its leading-zero deletion, and T its terminal-zero trim. If C\ne[], there are actual normal operands c,z:\texttt{Usize} with
\texttt{Slice.len}(s)-\texttt{Slice.len}(u)=\texttt{ok}\ c, \qquad \texttt{count\_trailing\_zeros}(s,c)=\texttt{ok}\ z.
Here z.\mathit{val}=|C|-|T|<|C|, and c.\mathit{val} is the byte length of the rendered significand, including its point if present: |I| without a fraction and |I|+1+|F| with a fraction.

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

Expand the two render equations using DecimalGoalVocabularyDefault. They give |s|=H+|u|, where H is the displayed significand byte length. Thus checked length subtraction is safe and yields c with value H. By DecimalDigitTrimDecomposition, write S=0^\ell\mathbin{++}C and C=T\mathbin{++}0^q, with q=|C|-|T|, and C nonzero-headed. In this nonempty case T cannot be empty: otherwise C=0^q would be a nonempty zero-headed list. Consequently q<|C| and T ends in a nonzero digit. Digit validity makes that last digit a value between one and nine.
Split the rendered significand immediately after its LAST nonzero digit. Call the prefix A and the suffix Z. The prefix ends in byte value 48+d with 1\le d<10, so its last value is neither 48 nor 46. Every digit in Z is zero: deleting the point leaves exactly the q terminal zeros of S, also the terminal zeros of C. There is at most one point, and it belongs to Z exactly when it follows the last nonzero digit. Thus every member of Z is 48 or 46 and its count of 48 is exactly q. This also treats an empty fractional list: a terminal point contributes a 46 to Z but contributes no zero. The source value list is A\mathbin{++}Z\mathbin{++}R, where R is the rendered exponent suffix, and |A\mathbin{++}Z|=H=c.\mathit{val}.
The count bound needed by DecimalSeqTrailingZeroValues is 0+q\le|C|\le|S|\le|s|\le2^{63}-1<2^{64}, using SourceValidByteSlice and the width in Assumptions. Apply that principal with initial count Usize zero and cursor c. It returns z with value q, skipping a point on either side of any zeros without counting it. Unfolding the wrapper in num imp dec2flt decimal seq count trailing zeros identifies this loop equation with the actual call in the conclusion. The cursor starts before R, so no exponent byte is visited. Substituting the decomposition's q gives all assertions, including the strict count bound.

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.

Explore this dependency tree

Inspect related source (47)
More program dependencies (14)
Standard-library dependencies (30)

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

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
All 13 entries
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"
}