P9_T1_ScientificVocabularyFold

Lean checked

Statement

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

Lemma

For every Aeneas byte-slice carrier s:\texttt{Slice U8}, optional exponent sign \sigma:\texttt{Option Bool}, and byte lists D,R:\texttt{List U8}, suppose that D\ne[], every b\in D satisfies 48\leq b.\mathit{val}\leq57, and R is empty or has a head b for which \neg(48\leq b.\mathit{val}\leq57). No condition is imposed on the bytes after that head. Suppose also that
s.\mathit{val}=\operatorname{signBytes}(\sigma)\mathbin{++}D\mathbin{++}R, \qquad \operatorname{signBytes}(\sigma)= \begin{cases} []&\sigma=\texttt{none},\\ [43]&\sigma=\texttt{some false}\quad\text{(plus)},\\ [45]&\sigma=\texttt{some true}\quad\text{(minus)}. \end{cases}
The displayed codes denote U8 bytes. Let T=2^{59}=576460752303423488, let \iota:\mathbb N\to\mathbb Z be the natural-to-integer embedding, and define
\Phi(a,d)= \begin{cases} 10a+d&a<T,\\ a&a\geq T, \end{cases} \qquad N(D)=\operatorname{foldl} (\Phi,0,\operatorname{map}(b\mapsto b.\mathit{val}-48,D)),
where both the subtraction and the fold are in \mathbb N, and the test uses the old accumulator a. Let \varepsilon(\sigma)=-1 for \sigma=\texttt{some true} and \varepsilon(\sigma)=1 otherwise. Then there exist an exponent e:\texttt{I64} and a residual slice r:\texttt{Slice U8} such that
\texttt{num.imp.dec2flt.parse.parse\_scientific}(s) =\texttt{ok}(\texttt{some}\ e,r), \qquad r.\mathit{val}=R, \qquad e.\mathit{val}=\varepsilon(\sigma)\,\iota(N(D)).
Thus the actual call returns normally with Some, consumes all of D even after accumulator updates stop, and leaves exactly R. An update may overshoot T; its result is retained, not clipped to T. The statement applies to every slice carrier satisfying the displayed premises, with no A or B length restriction, UTF-8 requirement, or extra exponent-digit restriction. It asserts neither complete numeric-literal acceptance nor behavior on empty or malformed exponent prefixes.

Lean statement

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

import Tablet.P9_T1_ScientificScannerFold
import Tablet.DecimalGoalVocabularyDefault
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P9_T1_ScientificVocabularyFold]
theorem P9_T1_ScientificVocabularyFold (s : Slice Std.U8) (sign : Option Bool)
    (digits tail : List Std.U8) (hne : digits != [])
    (hdigits : forall b, b ∈ digits -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57))
    (hs : s.val = (match sign with
      | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++
      digits ++ tail) :
    exists (exponent : Std.I64) (rest : Slice Std.U8),
      num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
      rest.val = tail /\
      exponent.val =
        (if sign = some true then -1 else 1) *
        ((digits.map (fun b => b.val - 48)).foldl
          (fun a d => if a < 576460752303423488 then 10 * a + d else a) 0 : Nat) := 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

The nonemptiness and digit-range premises are exactly those required by ScientificScannerFold. If R=b::R', the suffix premise gives \neg(48\leq b.\mathit{val}\leq57), discharging its nondigit-head premise. The three cases of \operatorname{signBytes} agree with the scanner's optional-sign prefix, so the displayed decomposition discharges its slice equality premise as well. Apply that theorem to obtain actual witnesses e,r with normal result \texttt{ok}(\texttt{some}\ e,r), residual contents R, and
e.\mathit{val}=\varepsilon(\sigma) \operatorname{foldl}(\Psi,0,D), \qquad \Psi(z,b)= \begin{cases} 10z+\iota(b.\mathit{val})-48&z<T,\\ z&z\geq T, \end{cases}
where \Psi is an integer byte operation.
For every byte b with 48\leq b.\mathit{val}, natural subtraction is exact:
\iota(b.\mathit{val}-48)=\iota(b.\mathit{val})-48.
Indeed b.\mathit{val}=48+(b.\mathit{val}-48) in \mathbb N, and embedding this equality into \mathbb Z gives the identity. For every a\in\mathbb N, \iota(a)<T is equivalent to a<T. The embedding preserves multiplication by 10 and addition, so the two threshold branches give
\Psi(\iota(a),b)=\iota(\Phi(a,b.\mathit{val}-48)).
Now prove, by induction on a byte list L whose members have value at least 48, the identity generalized to every starting a\in\mathbb N:
\operatorname{foldl}(\Psi,\iota(a),L) =\iota\!\left(\operatorname{foldl} (\Phi,a,\operatorname{map}(b\mapsto b.\mathit{val}-48,L))\right).
For the empty list both sides are \iota(a). For L=b::L', unfold the first fold step on each side and use the step identity. The induction hypothesis for L' applies at the new natural accumulator \Phi(a,b.\mathit{val}-48), whether the old accumulator was below the threshold or not, and proves the desired equality.
Apply this identity to L=D and a=0; the digit-range premise supplies all its lower bounds. Substitution into the scanner's exponent equation gives e.\mathit{val}=\varepsilon(\sigma)\,\iota(N(D)). The same witnesses retain the actual normal Some result and exact residual slice contents. The scanner theorem concerns an integer fold on byte values; this identity connects it to the natural digit-value fold defining the accumulated explicit exponent in DecimalGoalVocabularyDefault. It is the bridge used in that definition's preservation argument and in the Preamble preservation argument, without asserting any enclosing parser's correctness.

Lean proof


  have stepEq (a : Nat) (b : Std.U8) (hb : 48 <= b.val) :
      (if (a : Int) < 576460752303423488 then
        10 * (a : Int) + (b.val : Int) - 48 else a) =
      ((if a < 576460752303423488 then 10 * a + (b.val - 48) else a : Nat) : Int) := by
    by_cases ha : a < 576460752303423488
    · have hi : (a : Int) < 576460752303423488 := by exact_mod_cast ha
      simp only [if_pos ha, if_pos hi]
      omega
    · have hi : ¬ (a : Int) < 576460752303423488 := by exact_mod_cast ha
      simp only [if_neg ha, if_neg hi]
  have bridge (ds : List Std.U8) (hd : forall b, b ∈ ds -> 48 <= b.val) :
      forall a : Nat,
      ds.foldl (fun acc b => if acc < 576460752303423488 then
        10 * acc + (b.val : Int) - 48 else acc) (a : Int) =
      (((ds.map (fun b => b.val - 48)).foldl
        (fun acc d => if acc < 576460752303423488 then 10 * acc + d else acc) a : Nat) : Int) := by
    induction ds with
    | nil => intro a; rfl
    | cons b bs ih =>
      intro a
      simp only [List.foldl_cons, List.map_cons]
      rw [stepEq a b (hd b (by simp))]
      exact ih (fun d h => hd d (by simp [h])) _
  obtain ⟨exponent, rest, hex, hrest, hval⟩ :=
    P9_T1_ScientificScannerFold s sign digits tail hne hdigits htail hs
  refine ⟨exponent, rest, hex, hrest, ?_⟩
  have hb := bridge digits (fun b h => (hdigits b h).1) 0
  simp only [Nat.cast_zero] at hb
  rw [hval, hb]
end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P9_T1_ScientificScannerFold
import Tablet.DecimalGoalVocabularyDefault
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P9_T1_ScientificVocabularyFold]
theorem P9_T1_ScientificVocabularyFold (s : Slice Std.U8) (sign : Option Bool)
    (digits tail : List Std.U8) (hne : digits != [])
    (hdigits : forall b, b ∈ digits -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest -> ¬ (48 <= b.val /\ b.val <= 57))
    (hs : s.val = (match sign with
      | none => [] | some negative => [if negative then 45#u8 else 43#u8]) ++
      digits ++ tail) :
    exists (exponent : Std.I64) (rest : Slice Std.U8),
      num.imp.dec2flt.parse.parse_scientific s = ok (some exponent, rest) /\
      rest.val = tail /\
      exponent.val =
        (if sign = some true then -1 else 1) *
        ((digits.map (fun b => b.val - 48)).foldl
          (fun a d => if a < 576460752303423488 then 10 * a + d else a) 0 : Nat) := by
-- BODY
  have stepEq (a : Nat) (b : Std.U8) (hb : 48 <= b.val) :
      (if (a : Int) < 576460752303423488 then
        10 * (a : Int) + (b.val : Int) - 48 else a) =
      ((if a < 576460752303423488 then 10 * a + (b.val - 48) else a : Nat) : Int) := by
    by_cases ha : a < 576460752303423488
    · have hi : (a : Int) < 576460752303423488 := by exact_mod_cast ha
      simp only [if_pos ha, if_pos hi]
      omega
    · have hi : ¬ (a : Int) < 576460752303423488 := by exact_mod_cast ha
      simp only [if_neg ha, if_neg hi]
  have bridge (ds : List Std.U8) (hd : forall b, b ∈ ds -> 48 <= b.val) :
      forall a : Nat,
      ds.foldl (fun acc b => if acc < 576460752303423488 then
        10 * acc + (b.val : Int) - 48 else acc) (a : Int) =
      (((ds.map (fun b => b.val - 48)).foldl
        (fun acc d => if acc < 576460752303423488 then 10 * acc + d else acc) a : Nat) : Int) := by
    induction ds with
    | nil => intro a; rfl
    | cons b bs ih =>
      intro a
      simp only [List.foldl_cons, List.map_cons]
      rw [stepEq a b (hd b (by simp))]
      exact ih (fun d h => hd d (by simp [h])) _
  obtain ⟨exponent, rest, hex, hrest, hval⟩ :=
    P9_T1_ScientificScannerFold s sign digits tail hne hdigits htail hs
  refine ⟨exponent, rest, hex, hrest, ?_⟩
  have hb := bridge digits (fun b h => (hdigits b h).1) 0
  simp only [Nat.cast_zero] at hb
  rw [hval, hb]
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 33 entries
Technical checking records
Local closure summary
{
  "node": "P9_T1_ScientificVocabularyFold",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "de164ddff77a61916e44213eef5751be36d840e6f416c588ce922a881adc5332",
  "active_statement_hash": "c30932b2862ecb878986d3ecabeaf675ff945285f98a0956f6d89ac2da524e01",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P9_T1_ScientificScannerFold": "39aaf25e7fcfb501"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_parse_parse_scientific": "447f722d5841d2c5"
  }
}
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:3ff402e34348a1b142d84a857f962326551f0d9ed606095fe46cf339cb479b47",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}