P616_T1_SignStrippedClassification

Lean checked

Statement

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

Lemma · Exact whole-input sign removal

Let s be a source-valid Rust string, and let a byte slice b have exactly its bytes after removing one initial byte when that byte is - or +, and its unchanged bytes otherwise. Put \nu=\texttt{true} exactly when the initial byte is -. Then b is source-valid and |b|\leq|s|.
For every numeric classification x of the complete string, the lexeme y obtained by changing just its leading sign to absent is valid and renders exactly b. Both its accumulated and exact magnitudes equal those of x, and x's lexical-negative flag equals \nu. For every complete special classification (\sigma,\mathit{nan}), b is the unsigned special spelling with that same NaN flag, and \operatorname{decide}(\sigma=\mathrm{some\ true})=\nu. Conversely, existence of a valid unsigned numeric rendering of b implies existence of a complete numeric classification of s; existence of an unsigned special spelling of b implies existence of a complete special classification of s.

Lean statement

Trellis authored these statements and proofs. Lean checks the formal proofs; the assumptions used by each proof are listed below.

import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P616_T1_SignStrippedClassification]
theorem P616_T1_SignStrippedClassification (s : Str) (body : Slice Std.U8)
    (hs : DecimalGoalVocabularyDefault.sourceString s)
    (hbody : body.val =
      if s.val.head? = some (45#u8) ∨ s.val.head? = some (43#u8)
      then s.val.drop 1 else s.val) :
    let negative : Bool := decide (s.val.head? = some (45#u8))
    SourceValidByteSlice body ∧ body.val.length <= s.val.length ∧
    (forall x : DecimalLexeme,
      DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s)
        (some (Sum.inl x)) ->
      let y : DecimalLexeme := { x with leadingSign := none }
      DecimalGoalVocabularyDefault.validNumeric y ∧
      DecimalGoalVocabularyDefault.renderNumeric y = DecimalGoalVocabularyDefault.bytes body ∧
      DecimalGoalVocabularyDefault.accumulatedMagnitude y =
        DecimalGoalVocabularyDefault.accumulatedMagnitude x ∧
      DecimalGoalVocabularyDefault.exactMagnitude y = DecimalGoalVocabularyDefault.exactMagnitude x ∧
      DecimalGoalVocabularyDefault.lexicalNegative x = negative) ∧
    (forall sign nan,
      DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s)
        (some (Sum.inr (sign, nan))) ->
      DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body) none nan ∧
      decide (sign = some true) = negative) ∧
    ((exists x : DecimalLexeme, x.leadingSign = none ∧
      DecimalGoalVocabularyDefault.validNumeric x ∧
      DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes body) ->
      exists x : DecimalLexeme,
        DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s)
          (some (Sum.inl x))) ∧
    ((exists nan, DecimalGoalVocabularyDefault.specialLiteral
      (DecimalGoalVocabularyDefault.bytes body) none nan) ->
      exists sign nan, DecimalGoalVocabularyDefault.classifies
        (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inr (sign, nan)))) := 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

Use the definitions in DecimalGoalVocabularyDefault and SourceValidByteSlice. Source validity of s bounds its length by 2^{63}-1. Removing one byte, or leaving the list unchanged, cannot increase length, proving both slice-domain assertions without needing the slice to carry UTF-8 evidence.
A valid unsigned numeric rendering is nonempty: validity supplies at least one integer or fractional digit. Its first byte is either a digit in [48,57] or the dot 46 when the integer part is empty. In particular its first byte is neither sign byte 43 nor 45. Every unsigned special body is nonempty too, and begins with i,I,n, or N, because its case relation is to inf, infinity, or nan. These four first bytes also exclude both signs.
The definition of numeric rendering puts precisely the leading-sign prefix before the unsigned rendering, and the definition of special spelling puts precisely the same prefix before its keyword body. For a present negative sign that prefix is the one byte 45; for a present positive sign it is 43; for an absent sign it is empty. The non-sign first-byte facts just proved make these three cases unambiguous. Equality of natural byte maps implies equality of the original byte lists, since a U8 is determined by its value field. Thus the slice equation in the hypotheses removes exactly this prefix in both complete classifications. In the numeric case the remaining rendering is that of y, and its validity is unchanged because validity does not inspect the leading sign. Neither magnitude formula inspects the leading sign either: the full digit lists, fractional length and independent exponent sign are unchanged. This proves both value equalities. The definition of lexical negativity, or of the special sign test, gives \nu in each of the three prefix cases.
For the converses, inspect the initial byte of s. If it is 45, prepend the negative sign to the given unsigned lexeme or keyword; if it is 43, prepend the positive sign; otherwise use no leading sign. In the first two cases the hypothesis identifies b with the tail of that nonempty byte list, so the restored prefix renders exactly s. In the third case b=s. Numeric validity is unaffected by the restored sign, and special spelling is defined by that prefix and its case-related body. These constructions provide precisely the two existential complete classifications in the conclusion.

Lean proof


  rcases s with ⟨sv, hsbound⟩
  rcases body with ⟨bv, hbv⟩
  change sv.length ≤ 9223372036854775807 ∧
    ∃ t : String, sv.map (fun z => z.val) = t.toUTF8.toList.map UInt8.toNat at hs
  have hlen : bv.length ≤ sv.length := by
    change bv = (if sv.head? = some (45#u8) ∨ sv.head? = some (43#u8)
      then sv.drop 1 else sv) at hbody
    cases sv with
    | nil => simp at hbody; simp [hbody]
    | cons a t =>
      by_cases hm : a = 45#u8
      · simp [hm] at hbody
        subst bv
        simp
      · by_cases hp : a = 43#u8
        · simp [hm, hp] at hbody
          subst bv
          simp
        · simp [hm, hp] at hbody
          subst bv
          simp
  have hvalidbody : SourceValidByteSlice (⟨bv, hbv⟩ : Slice Std.U8) := by
    unfold SourceValidByteSlice
    exact Nat.le_trans hlen hs.1
  have hmapinj : ∀ (u v : List Std.U8),
      u.map (fun z => z.val) = v.map (fun z => z.val) → u = v := by
    intro u v h
    induction u generalizing v with
    | nil => cases v <;> simp_all
    | cons a t iht =>
      cases v with
      | nil => simp at h
      | cons b r =>
        simp only [List.map_cons, List.cons.injEq] at h
        have hab : a = b := UScalar.eq_of_val_eq h.1
        have htr : t = r := iht r h.2
        simp [hab, htr]
  let signBytes : Option Bool → List Nat := fun sign =>
    match sign with | none => [] | some n => [if n then 45 else 43]
  have hsourceSplit : ∃ sg : Option Bool,
      sv.map (fun z => z.val) = signBytes sg ++ bv.map (fun z => z.val) ∧
      decide (sg = some true) = decide (sv.head? = some (45#u8)) := by
    cases sv with
    | nil =>
      have hb : bv = [] := by
        change bv = (if ([] : List Std.U8).head? = some (45#u8) ∨
          ([] : List Std.U8).head? = some (43#u8) then ([] : List Std.U8).drop 1 else []) at hbody
        simpa using hbody
      subst bv
      refine ⟨none, ?_, ?_⟩ <;> simp [signBytes]
    | cons a t =>
      change bv = (if (a :: t).head? = some (45#u8) ∨
        (a :: t).head? = some (43#u8) then (a :: t).drop 1 else a :: t) at hbody
      by_cases hm : a = 45#u8
      · have hb : bv = t := by simpa [hm] using hbody
        subst bv
        refine ⟨some true, ?_, ?_⟩ <;> simp [signBytes, hm]
      · by_cases hp : a = 43#u8
        · have hb : bv = t := by simpa [hm, hp] using hbody
          subst bv
          refine ⟨some false, ?_, ?_⟩ <;> simp [signBytes, hm, hp]
        · have hb : bv = a :: t := by simpa [hm, hp] using hbody
          subst bv
          refine ⟨none, ?_, ?_⟩ <;> simp [signBytes, hm, hp]
  refine ⟨hvalidbody, ?_, ?_, ?_, ?_, ?_⟩
  · omega
  · intro x hx
    change DecimalGoalVocabularyDefault.validNumeric x ∧
      DecimalGoalVocabularyDefault.renderNumeric x = sv.map (fun z => z.val) at hx
    rcases hx with ⟨hv, hr⟩
    let unsigned : DecimalLexeme := { x with leadingSign := none }
    let r : List Nat := x.integerDigits.map (fun d => 48 + d) ++
      (match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d)) ++
      (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 d => 48 + d))
    have hr' : (match x.leadingSign with
        | none => [] | some n => [if n then 45 else 43]) ++ r = sv.map (fun z => z.val) := by
      change (match x.leadingSign with
        | none => [] | some n => [if n then 45 else 43]) ++
          x.integerDigits.map (fun d => 48 + d) ++
          (match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d)) ++
          (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 d => 48 + d)) = sv.map (fun z => z.val) at hr
      simpa [r, List.append_assoc] using hr
    have hrUnsigned : DecimalGoalVocabularyDefault.renderNumeric unsigned = r := by
      change (match unsigned.leadingSign with
        | none => [] | some n => [if n then 45 else 43]) ++
          unsigned.integerDigits.map (fun d => 48 + d) ++
          (match unsigned.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d)) ++
          (match unsigned.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 d => 48 + d)) = r
      simp [unsigned, r, List.append_assoc]
    have hRne : r ≠ [] := by
      dsimp [r]
      rcases hv with ⟨hints, hfrac, hhas, hexp⟩
      cases hi : x.integerDigits with
      | nil =>
        cases hf : x.fractionDigits with
        | none => simp [hi, hf] at hhas
        | some ds =>
          have hds : ds ≠ [] := by simpa [hi, hf] using hhas
          cases ds <;> simp_all [hi, hf]
      | cons d ds => simp [hi]
    have hRhead : ∀ a t, r = a :: t → a ≠ 43 ∧ a ≠ 45 := by
      intro a t har
      dsimp [r] at har
      rcases hv with ⟨hints, hfrac, hhas, hexp⟩
      cases hi : x.integerDigits with
      | nil =>
        cases hf : x.fractionDigits with
        | none => simp [hi, hf] at hhas
        | some ds =>
          have hds : ds ≠ [] := by simpa [hi, hf] using hhas
          cases ds with
          | nil => contradiction
          | cons d ds => simp [hi, hf] at har; omega
      | cons d ds =>
        have hd : d < 10 := hints d (by simp [hi])
        simp [hi] at har
        omega
    refine ⟨?_, ?_, ?_, ?_, ?_⟩
    · change DecimalGoalVocabularyDefault.validNumeric x
      exact hv
    · rw [hrUnsigned]
      change r = bv.map (fun z => z.val)
      have hrbody : r = bv.map (fun z => z.val) := by
        cases sv with
        | nil =>
          cases x.leadingSign <;> simp at hr' <;> exact (hRne hr'.2).elim
        | cons a t =>
          have hb : bv = (if a = 45#u8 ∨ a = 43#u8 then t else a :: t) := by
            change bv = (if (a :: t).head? = some (45#u8) ∨
              (a :: t).head? = some (43#u8) then (a :: t).drop 1 else a :: t) at hbody
            simpa using hbody
          by_cases hm : a = 45#u8
          · simp [hm] at hb
            subst bv
            subst a
            cases hsx : x.leadingSign with
            | none =>
              simp [hsx] at hr'
              have hnot := hRhead 45 (List.map (fun z => z.val) t) hr'
              norm_num at hnot
            | some neg =>
              cases neg with
              | false => simp [hsx] at hr'
              | true =>
                simp [hsx] at hr'
                exact hr'
          · by_cases hp : a = 43#u8
            · simp [hm, hp] at hb
              subst bv
              subst a
              cases hsx : x.leadingSign with
              | none =>
                simp [hsx] at hr'
                have hnot := hRhead 43 (List.map (fun z => z.val) t) hr'
                norm_num at hnot
              | some neg =>
                cases neg with
                | false => simpa [hsx] using hr'
                | true => simp [hsx] at hr'
            · simp [hm, hp] at hb
              subst bv
              cases hsx : x.leadingSign with
              | none => simpa [hsx] using hr'
              | some neg =>
                cases neg <;> simp [hsx] at hr'
                · exact (hp (UScalar.eq_of_val_eq hr'.1)).elim
                · exact (hm (UScalar.eq_of_val_eq hr'.1)).elim
      exact hrbody
    · rfl
    · rfl
    · change decide (x.leadingSign = some true) = decide (sv.head? = some (45#u8))
      cases sv with
      | nil =>
        cases x.leadingSign <;> simp at hr' <;> exact (hRne hr'.2).elim
      | cons a t =>
        by_cases hm : a = 45#u8
        · subst a
          cases hxsign : x.leadingSign with
          | none =>
            simp [hxsign] at hr'
            have hnot := hRhead 45 (List.map (fun z => z.val) t) hr'
            norm_num at hnot
          | some n =>
            cases n with
            | false => simp [hxsign] at hr'
            | true => simp [hxsign]
        · by_cases hp : a = 43#u8
          · subst a
            cases hxsign : x.leadingSign with
            | none => simp [hxsign]
            | some n =>
              cases n with
              | false => simp [hxsign]
              | true => simp [hxsign] at hr'
          · cases hxsign : x.leadingSign with
            | none => simp [hxsign, hm]
            | some n =>
              cases n with
              | false =>
                simp [hxsign] at hr'
                exact (hp (UScalar.eq_of_val_eq hr'.1)).elim
              | true =>
                simp [hxsign] at hr'
                exact (hm (UScalar.eq_of_val_eq hr'.1)).elim
  · intro sign nan hx
    change DecimalGoalVocabularyDefault.specialLiteral
      (sv.map (fun z => z.val)) sign nan at hx
    dsimp [DecimalGoalVocabularyDefault] at hx
    rcases hx with ⟨word, hword, hcase⟩
    have hfirstWord : ∃ a rest, word = a :: rest ∧ a ≠ 43 ∧ a ≠ 45 := by
      have hpos : 0 < word.length := by
        cases nan with
        | false =>
          rcases hcase with hcase | hcase
          · have hlen' := List.Forall₂.length_eq hcase
            simp at hlen'
            omega
          · have hlen' := List.Forall₂.length_eq hcase
            simp at hlen'
            omega
        | true =>
          have hlen' := List.Forall₂.length_eq hcase
          simp at hlen'
          omega
      obtain ⟨a, rest, hcons⟩ := List.exists_cons_of_length_pos hpos
      rw [hcons] at hcase
      refine ⟨a, rest, hcons, ?_, ?_⟩
      · cases nan with
        | false =>
          rcases hcase with hcase | hcase
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
        | true =>
          cases hcase with
          | cons ha _ => change a = 110 ∨ a + 32 = 110 at ha; omega
      · cases nan with
        | false =>
          rcases hcase with hcase | hcase
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
        | true =>
          cases hcase with
          | cons ha _ => change a = 110 ∨ a + 32 = 110 at ha; omega
    obtain ⟨a, rest, hwordcons, haPlus, haMinus⟩ := hfirstWord
    have hsrc : signBytes sign ++ word = sv.map (fun z => z.val) := hword.symm
    refine ⟨?_, ?_⟩
    · change DecimalGoalVocabularyDefault.specialLiteral
        (bv.map (fun z => z.val)) none nan
      dsimp [DecimalGoalVocabularyDefault]
      refine ⟨word, ?_, hcase⟩
      have ⟨sg, hsplit, _⟩ := hsourceSplit
      cases sv with
      | nil =>
        cases sign <;> simp [signBytes, hwordcons] at hsrc
      | cons z t =>
        by_cases hm : z = 45#u8
        · subst z
          have hb : bv = t := by
            change bv = (if (45#u8 :: t).head? = some (45#u8) ∨
              (45#u8 :: t).head? = some (43#u8) then (45#u8 :: t).drop 1 else 45#u8 :: t) at hbody
            simpa using hbody
          subst bv
          cases sign with
          | none =>
            simp [signBytes] at hsrc
            have hh := congrArg List.head? hsrc
            simp [hwordcons] at hh
            exact (haMinus hh).elim
          | some neg =>
            cases neg with
            | false => simp [signBytes] at hsrc
            | true =>
              simp [signBytes] at hsrc
              exact hsrc.symm
        · by_cases hp : z = 43#u8
          · subst z
            have hb : bv = t := by
              change bv = (if (43#u8 :: t).head? = some (45#u8) ∨
                (43#u8 :: t).head? = some (43#u8) then (43#u8 :: t).drop 1 else 43#u8 :: t) at hbody
              simpa using hbody
            subst bv
            cases sign with
            | none =>
              simp [signBytes] at hsrc
              have hh := congrArg List.head? hsrc
              simp [hwordcons] at hh
              exact (haPlus hh).elim
            | some neg =>
              cases neg with
              | false =>
                simp [signBytes] at hsrc
                exact hsrc.symm
              | true => simp [signBytes] at hsrc
          · have hb : bv = z :: t := by
              change bv = (if (z :: t).head? = some (45#u8) ∨
                (z :: t).head? = some (43#u8) then (z :: t).drop 1 else z :: t) at hbody
              simpa [hm, hp] using hbody
            subst bv
            cases sign with
            | none => simpa [signBytes] using hsrc.symm
            | some neg =>
              cases neg <;> simp [signBytes] at hsrc
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hp (UScalar.eq_of_val_eq hfirst)).elim
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hm (UScalar.eq_of_val_eq hfirst)).elim
    · change decide (sign = some true) = decide (sv.head? = some (45#u8))
      cases sv with
      | nil =>
        cases sign <;> simp [signBytes, hwordcons] at hsrc
      | cons z t =>
        by_cases hm : z = 45#u8
        · subst z
          cases sign with
          | none =>
            simp [signBytes] at hsrc
            have hh := congrArg List.head? hsrc
            simp [hwordcons] at hh
            exact (haMinus hh).elim
          | some neg =>
            cases neg with
            | false => simp [signBytes] at hsrc
            | true => simp [signBytes]
        · by_cases hp : z = 43#u8
          · subst z
            cases sign with
            | none => simp [signBytes]
            | some neg =>
              cases neg with
              | false => simp [signBytes]
              | true => simp [signBytes] at hsrc
          · cases sign with
            | none => simp [signBytes, hm]
            | some neg =>
              cases neg <;> simp [signBytes] at hsrc
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hp (UScalar.eq_of_val_eq hfirst)).elim
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hm (UScalar.eq_of_val_eq hfirst)).elim
  · intro h
    rcases h with ⟨x, hnone, hv, hr⟩
    change DecimalGoalVocabularyDefault.renderNumeric x = bv.map (fun z => z.val) at hr
    obtain ⟨sg, hsplit, _⟩ := hsourceSplit
    let y : DecimalLexeme := { x with leadingSign := sg }
    refine ⟨y, ?_⟩
    change DecimalGoalVocabularyDefault.validNumeric y ∧
      DecimalGoalVocabularyDefault.renderNumeric y = sv.map (fun z => z.val)
    constructor
    · change DecimalGoalVocabularyDefault.validNumeric x
      exact hv
    · calc
        DecimalGoalVocabularyDefault.renderNumeric y =
            signBytes sg ++ DecimalGoalVocabularyDefault.renderNumeric x := by
              simp [y, signBytes, hnone, DecimalGoalVocabularyDefault, List.append_assoc]
              rfl
        _ = signBytes sg ++ bv.map (fun z => z.val) := by rw [hr]
        _ = sv.map (fun z => z.val) := hsplit.symm
  · intro h
    rcases h with ⟨nan, hsp⟩
    change DecimalGoalVocabularyDefault.specialLiteral
      (bv.map (fun z => z.val)) none nan at hsp
    dsimp [DecimalGoalVocabularyDefault] at hsp
    rcases hsp with ⟨word, hword, hcase⟩
    obtain ⟨sg, hsplit, _⟩ := hsourceSplit
    refine ⟨sg, nan, ?_⟩
    change DecimalGoalVocabularyDefault.specialLiteral
      (sv.map (fun z => z.val)) sg nan
    dsimp [DecimalGoalVocabularyDefault]
    refine ⟨word, ?_, hcase⟩
    calc
      sv.map (fun z => z.val) = signBytes sg ++ bv.map (fun z => z.val) := hsplit
      _ = signBytes sg ++ word := by rw [hword]
end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P616_T1_SignStrippedClassification]
theorem P616_T1_SignStrippedClassification (s : Str) (body : Slice Std.U8)
    (hs : DecimalGoalVocabularyDefault.sourceString s)
    (hbody : body.val =
      if s.val.head? = some (45#u8) ∨ s.val.head? = some (43#u8)
      then s.val.drop 1 else s.val) :
    let negative : Bool := decide (s.val.head? = some (45#u8))
    SourceValidByteSlice body ∧ body.val.length <= s.val.length ∧
    (forall x : DecimalLexeme,
      DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s)
        (some (Sum.inl x)) ->
      let y : DecimalLexeme := { x with leadingSign := none }
      DecimalGoalVocabularyDefault.validNumeric y ∧
      DecimalGoalVocabularyDefault.renderNumeric y = DecimalGoalVocabularyDefault.bytes body ∧
      DecimalGoalVocabularyDefault.accumulatedMagnitude y =
        DecimalGoalVocabularyDefault.accumulatedMagnitude x ∧
      DecimalGoalVocabularyDefault.exactMagnitude y = DecimalGoalVocabularyDefault.exactMagnitude x ∧
      DecimalGoalVocabularyDefault.lexicalNegative x = negative) ∧
    (forall sign nan,
      DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s)
        (some (Sum.inr (sign, nan))) ->
      DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes body) none nan ∧
      decide (sign = some true) = negative) ∧
    ((exists x : DecimalLexeme, x.leadingSign = none ∧
      DecimalGoalVocabularyDefault.validNumeric x ∧
      DecimalGoalVocabularyDefault.renderNumeric x = DecimalGoalVocabularyDefault.bytes body) ->
      exists x : DecimalLexeme,
        DecimalGoalVocabularyDefault.classifies (DecimalGoalVocabularyDefault.bytes s)
          (some (Sum.inl x))) ∧
    ((exists nan, DecimalGoalVocabularyDefault.specialLiteral
      (DecimalGoalVocabularyDefault.bytes body) none nan) ->
      exists sign nan, DecimalGoalVocabularyDefault.classifies
        (DecimalGoalVocabularyDefault.bytes s) (some (Sum.inr (sign, nan)))) := by
-- BODY
  rcases s with ⟨sv, hsbound⟩
  rcases body with ⟨bv, hbv⟩
  change sv.length ≤ 9223372036854775807 ∧
    ∃ t : String, sv.map (fun z => z.val) = t.toUTF8.toList.map UInt8.toNat at hs
  have hlen : bv.length ≤ sv.length := by
    change bv = (if sv.head? = some (45#u8) ∨ sv.head? = some (43#u8)
      then sv.drop 1 else sv) at hbody
    cases sv with
    | nil => simp at hbody; simp [hbody]
    | cons a t =>
      by_cases hm : a = 45#u8
      · simp [hm] at hbody
        subst bv
        simp
      · by_cases hp : a = 43#u8
        · simp [hm, hp] at hbody
          subst bv
          simp
        · simp [hm, hp] at hbody
          subst bv
          simp
  have hvalidbody : SourceValidByteSlice (⟨bv, hbv⟩ : Slice Std.U8) := by
    unfold SourceValidByteSlice
    exact Nat.le_trans hlen hs.1
  have hmapinj : ∀ (u v : List Std.U8),
      u.map (fun z => z.val) = v.map (fun z => z.val) → u = v := by
    intro u v h
    induction u generalizing v with
    | nil => cases v <;> simp_all
    | cons a t iht =>
      cases v with
      | nil => simp at h
      | cons b r =>
        simp only [List.map_cons, List.cons.injEq] at h
        have hab : a = b := UScalar.eq_of_val_eq h.1
        have htr : t = r := iht r h.2
        simp [hab, htr]
  let signBytes : Option Bool → List Nat := fun sign =>
    match sign with | none => [] | some n => [if n then 45 else 43]
  have hsourceSplit : ∃ sg : Option Bool,
      sv.map (fun z => z.val) = signBytes sg ++ bv.map (fun z => z.val) ∧
      decide (sg = some true) = decide (sv.head? = some (45#u8)) := by
    cases sv with
    | nil =>
      have hb : bv = [] := by
        change bv = (if ([] : List Std.U8).head? = some (45#u8) ∨
          ([] : List Std.U8).head? = some (43#u8) then ([] : List Std.U8).drop 1 else []) at hbody
        simpa using hbody
      subst bv
      refine ⟨none, ?_, ?_⟩ <;> simp [signBytes]
    | cons a t =>
      change bv = (if (a :: t).head? = some (45#u8) ∨
        (a :: t).head? = some (43#u8) then (a :: t).drop 1 else a :: t) at hbody
      by_cases hm : a = 45#u8
      · have hb : bv = t := by simpa [hm] using hbody
        subst bv
        refine ⟨some true, ?_, ?_⟩ <;> simp [signBytes, hm]
      · by_cases hp : a = 43#u8
        · have hb : bv = t := by simpa [hm, hp] using hbody
          subst bv
          refine ⟨some false, ?_, ?_⟩ <;> simp [signBytes, hm, hp]
        · have hb : bv = a :: t := by simpa [hm, hp] using hbody
          subst bv
          refine ⟨none, ?_, ?_⟩ <;> simp [signBytes, hm, hp]
  refine ⟨hvalidbody, ?_, ?_, ?_, ?_, ?_⟩
  · omega
  · intro x hx
    change DecimalGoalVocabularyDefault.validNumeric x ∧
      DecimalGoalVocabularyDefault.renderNumeric x = sv.map (fun z => z.val) at hx
    rcases hx with ⟨hv, hr⟩
    let unsigned : DecimalLexeme := { x with leadingSign := none }
    let r : List Nat := x.integerDigits.map (fun d => 48 + d) ++
      (match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d)) ++
      (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 d => 48 + d))
    have hr' : (match x.leadingSign with
        | none => [] | some n => [if n then 45 else 43]) ++ r = sv.map (fun z => z.val) := by
      change (match x.leadingSign with
        | none => [] | some n => [if n then 45 else 43]) ++
          x.integerDigits.map (fun d => 48 + d) ++
          (match x.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d)) ++
          (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 d => 48 + d)) = sv.map (fun z => z.val) at hr
      simpa [r, List.append_assoc] using hr
    have hrUnsigned : DecimalGoalVocabularyDefault.renderNumeric unsigned = r := by
      change (match unsigned.leadingSign with
        | none => [] | some n => [if n then 45 else 43]) ++
          unsigned.integerDigits.map (fun d => 48 + d) ++
          (match unsigned.fractionDigits with | none => [] | some ds => 46 :: ds.map (fun d => 48 + d)) ++
          (match unsigned.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 d => 48 + d)) = r
      simp [unsigned, r, List.append_assoc]
    have hRne : r ≠ [] := by
      dsimp [r]
      rcases hv with ⟨hints, hfrac, hhas, hexp⟩
      cases hi : x.integerDigits with
      | nil =>
        cases hf : x.fractionDigits with
        | none => simp [hi, hf] at hhas
        | some ds =>
          have hds : ds ≠ [] := by simpa [hi, hf] using hhas
          cases ds <;> simp_all [hi, hf]
      | cons d ds => simp [hi]
    have hRhead : ∀ a t, r = a :: t → a ≠ 43 ∧ a ≠ 45 := by
      intro a t har
      dsimp [r] at har
      rcases hv with ⟨hints, hfrac, hhas, hexp⟩
      cases hi : x.integerDigits with
      | nil =>
        cases hf : x.fractionDigits with
        | none => simp [hi, hf] at hhas
        | some ds =>
          have hds : ds ≠ [] := by simpa [hi, hf] using hhas
          cases ds with
          | nil => contradiction
          | cons d ds => simp [hi, hf] at har; omega
      | cons d ds =>
        have hd : d < 10 := hints d (by simp [hi])
        simp [hi] at har
        omega
    refine ⟨?_, ?_, ?_, ?_, ?_⟩
    · change DecimalGoalVocabularyDefault.validNumeric x
      exact hv
    · rw [hrUnsigned]
      change r = bv.map (fun z => z.val)
      have hrbody : r = bv.map (fun z => z.val) := by
        cases sv with
        | nil =>
          cases x.leadingSign <;> simp at hr' <;> exact (hRne hr'.2).elim
        | cons a t =>
          have hb : bv = (if a = 45#u8 ∨ a = 43#u8 then t else a :: t) := by
            change bv = (if (a :: t).head? = some (45#u8) ∨
              (a :: t).head? = some (43#u8) then (a :: t).drop 1 else a :: t) at hbody
            simpa using hbody
          by_cases hm : a = 45#u8
          · simp [hm] at hb
            subst bv
            subst a
            cases hsx : x.leadingSign with
            | none =>
              simp [hsx] at hr'
              have hnot := hRhead 45 (List.map (fun z => z.val) t) hr'
              norm_num at hnot
            | some neg =>
              cases neg with
              | false => simp [hsx] at hr'
              | true =>
                simp [hsx] at hr'
                exact hr'
          · by_cases hp : a = 43#u8
            · simp [hm, hp] at hb
              subst bv
              subst a
              cases hsx : x.leadingSign with
              | none =>
                simp [hsx] at hr'
                have hnot := hRhead 43 (List.map (fun z => z.val) t) hr'
                norm_num at hnot
              | some neg =>
                cases neg with
                | false => simpa [hsx] using hr'
                | true => simp [hsx] at hr'
            · simp [hm, hp] at hb
              subst bv
              cases hsx : x.leadingSign with
              | none => simpa [hsx] using hr'
              | some neg =>
                cases neg <;> simp [hsx] at hr'
                · exact (hp (UScalar.eq_of_val_eq hr'.1)).elim
                · exact (hm (UScalar.eq_of_val_eq hr'.1)).elim
      exact hrbody
    · rfl
    · rfl
    · change decide (x.leadingSign = some true) = decide (sv.head? = some (45#u8))
      cases sv with
      | nil =>
        cases x.leadingSign <;> simp at hr' <;> exact (hRne hr'.2).elim
      | cons a t =>
        by_cases hm : a = 45#u8
        · subst a
          cases hxsign : x.leadingSign with
          | none =>
            simp [hxsign] at hr'
            have hnot := hRhead 45 (List.map (fun z => z.val) t) hr'
            norm_num at hnot
          | some n =>
            cases n with
            | false => simp [hxsign] at hr'
            | true => simp [hxsign]
        · by_cases hp : a = 43#u8
          · subst a
            cases hxsign : x.leadingSign with
            | none => simp [hxsign]
            | some n =>
              cases n with
              | false => simp [hxsign]
              | true => simp [hxsign] at hr'
          · cases hxsign : x.leadingSign with
            | none => simp [hxsign, hm]
            | some n =>
              cases n with
              | false =>
                simp [hxsign] at hr'
                exact (hp (UScalar.eq_of_val_eq hr'.1)).elim
              | true =>
                simp [hxsign] at hr'
                exact (hm (UScalar.eq_of_val_eq hr'.1)).elim
  · intro sign nan hx
    change DecimalGoalVocabularyDefault.specialLiteral
      (sv.map (fun z => z.val)) sign nan at hx
    dsimp [DecimalGoalVocabularyDefault] at hx
    rcases hx with ⟨word, hword, hcase⟩
    have hfirstWord : ∃ a rest, word = a :: rest ∧ a ≠ 43 ∧ a ≠ 45 := by
      have hpos : 0 < word.length := by
        cases nan with
        | false =>
          rcases hcase with hcase | hcase
          · have hlen' := List.Forall₂.length_eq hcase
            simp at hlen'
            omega
          · have hlen' := List.Forall₂.length_eq hcase
            simp at hlen'
            omega
        | true =>
          have hlen' := List.Forall₂.length_eq hcase
          simp at hlen'
          omega
      obtain ⟨a, rest, hcons⟩ := List.exists_cons_of_length_pos hpos
      rw [hcons] at hcase
      refine ⟨a, rest, hcons, ?_, ?_⟩
      · cases nan with
        | false =>
          rcases hcase with hcase | hcase
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
        | true =>
          cases hcase with
          | cons ha _ => change a = 110 ∨ a + 32 = 110 at ha; omega
      · cases nan with
        | false =>
          rcases hcase with hcase | hcase
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
          · cases hcase with
            | cons ha _ => change a = 105 ∨ a + 32 = 105 at ha; omega
        | true =>
          cases hcase with
          | cons ha _ => change a = 110 ∨ a + 32 = 110 at ha; omega
    obtain ⟨a, rest, hwordcons, haPlus, haMinus⟩ := hfirstWord
    have hsrc : signBytes sign ++ word = sv.map (fun z => z.val) := hword.symm
    refine ⟨?_, ?_⟩
    · change DecimalGoalVocabularyDefault.specialLiteral
        (bv.map (fun z => z.val)) none nan
      dsimp [DecimalGoalVocabularyDefault]
      refine ⟨word, ?_, hcase⟩
      have ⟨sg, hsplit, _⟩ := hsourceSplit
      cases sv with
      | nil =>
        cases sign <;> simp [signBytes, hwordcons] at hsrc
      | cons z t =>
        by_cases hm : z = 45#u8
        · subst z
          have hb : bv = t := by
            change bv = (if (45#u8 :: t).head? = some (45#u8) ∨
              (45#u8 :: t).head? = some (43#u8) then (45#u8 :: t).drop 1 else 45#u8 :: t) at hbody
            simpa using hbody
          subst bv
          cases sign with
          | none =>
            simp [signBytes] at hsrc
            have hh := congrArg List.head? hsrc
            simp [hwordcons] at hh
            exact (haMinus hh).elim
          | some neg =>
            cases neg with
            | false => simp [signBytes] at hsrc
            | true =>
              simp [signBytes] at hsrc
              exact hsrc.symm
        · by_cases hp : z = 43#u8
          · subst z
            have hb : bv = t := by
              change bv = (if (43#u8 :: t).head? = some (45#u8) ∨
                (43#u8 :: t).head? = some (43#u8) then (43#u8 :: t).drop 1 else 43#u8 :: t) at hbody
              simpa using hbody
            subst bv
            cases sign with
            | none =>
              simp [signBytes] at hsrc
              have hh := congrArg List.head? hsrc
              simp [hwordcons] at hh
              exact (haPlus hh).elim
            | some neg =>
              cases neg with
              | false =>
                simp [signBytes] at hsrc
                exact hsrc.symm
              | true => simp [signBytes] at hsrc
          · have hb : bv = z :: t := by
              change bv = (if (z :: t).head? = some (45#u8) ∨
                (z :: t).head? = some (43#u8) then (z :: t).drop 1 else z :: t) at hbody
              simpa [hm, hp] using hbody
            subst bv
            cases sign with
            | none => simpa [signBytes] using hsrc.symm
            | some neg =>
              cases neg <;> simp [signBytes] at hsrc
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hp (UScalar.eq_of_val_eq hfirst)).elim
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hm (UScalar.eq_of_val_eq hfirst)).elim
    · change decide (sign = some true) = decide (sv.head? = some (45#u8))
      cases sv with
      | nil =>
        cases sign <;> simp [signBytes, hwordcons] at hsrc
      | cons z t =>
        by_cases hm : z = 45#u8
        · subst z
          cases sign with
          | none =>
            simp [signBytes] at hsrc
            have hh := congrArg List.head? hsrc
            simp [hwordcons] at hh
            exact (haMinus hh).elim
          | some neg =>
            cases neg with
            | false => simp [signBytes] at hsrc
            | true => simp [signBytes]
        · by_cases hp : z = 43#u8
          · subst z
            cases sign with
            | none => simp [signBytes]
            | some neg =>
              cases neg with
              | false => simp [signBytes]
              | true => simp [signBytes] at hsrc
          · cases sign with
            | none => simp [signBytes, hm]
            | some neg =>
              cases neg <;> simp [signBytes] at hsrc
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hp (UScalar.eq_of_val_eq hfirst)).elim
              · rcases hsrc with ⟨hfirst, _⟩
                exact (hm (UScalar.eq_of_val_eq hfirst)).elim
  · intro h
    rcases h with ⟨x, hnone, hv, hr⟩
    change DecimalGoalVocabularyDefault.renderNumeric x = bv.map (fun z => z.val) at hr
    obtain ⟨sg, hsplit, _⟩ := hsourceSplit
    let y : DecimalLexeme := { x with leadingSign := sg }
    refine ⟨y, ?_⟩
    change DecimalGoalVocabularyDefault.validNumeric y ∧
      DecimalGoalVocabularyDefault.renderNumeric y = sv.map (fun z => z.val)
    constructor
    · change DecimalGoalVocabularyDefault.validNumeric x
      exact hv
    · calc
        DecimalGoalVocabularyDefault.renderNumeric y =
            signBytes sg ++ DecimalGoalVocabularyDefault.renderNumeric x := by
              simp [y, signBytes, hnone, DecimalGoalVocabularyDefault, List.append_assoc]
              rfl
        _ = signBytes sg ++ bv.map (fun z => z.val) := by rw [hr]
        _ = sv.map (fun z => z.val) := hsplit.symm
  · intro h
    rcases h with ⟨nan, hsp⟩
    change DecimalGoalVocabularyDefault.specialLiteral
      (bv.map (fun z => z.val)) none nan at hsp
    dsimp [DecimalGoalVocabularyDefault] at hsp
    rcases hsp with ⟨word, hword, hcase⟩
    obtain ⟨sg, hsplit, _⟩ := hsourceSplit
    refine ⟨sg, nan, ?_⟩
    change DecimalGoalVocabularyDefault.specialLiteral
      (sv.map (fun z => z.val)) sg nan
    dsimp [DecimalGoalVocabularyDefault]
    refine ⟨word, ?_, hcase⟩
    calc
      sv.map (fun z => z.val) = signBytes sg ++ bv.map (fun z => z.val) := hsplit
      _ = signBytes sg ++ word := by rw [hword]
end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
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 8 entries
Technical checking records
Local closure summary
{
  "node": "P616_T1_SignStrippedClassification",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "3c135dd8b969d3e26eb81bddff591d333c86d57fa76bc1dc7e919677b915038e",
  "active_statement_hash": "1130fe50dfc9caf730a2a84a7f9a6f0a61d762900306108adb9c7d84f1f78e7d",
  "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-156",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c08dd4b4aea4abb4",
    "DecimalGoalVocabularyDefault": "28ca0a8ac503ee01",
    "DecimalLexeme": "250be645520aec86",
    "F64": "ec4e25fbaf4e11d4",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_decimal_Decimal": "63fd2f021dd0ef41"
  }
}
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:7d894d723001471fcfaa67aea4a41c41bdf966f525852809b7442d5dd225c6b4",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}