DecimalSeqLeadingZeroLoopExact

Lean checked

Statement

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

Helper · Exact maximal leading-zero scan

Let a byte slice have byte-value list consisting of n copies of ASCII zero followed by a list T. If T is empty or its first value is not ASCII zero, then the first leading-zero loop of the extracted decimal-sequence parser returns normally, and the returned slice has byte-value list exactly T.

Lean statement

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

import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqLeadingZeroLoopExact]
theorem DecimalSeqLeadingZeroLoopExact (s : Slice Std.U8) (n : Nat)
    (tail : List Nat)
    (hs : s.val.map (fun b => b.val) = List.replicate n 48 ++ tail)
    (hstop : tail = [] \/ tail[0]? ≠ some 48) :
    exists out : Slice Std.U8,
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s = ok out /\
      out.val.map (fun b => b.val) = tail := 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 loop body and slice operations in num imp dec2flt decimal seq parse decimal seq. Maintain the exact invariant that the current slice maps to k copies of value 48 followed by T, and use the current slice length as the decreasing measure. For k>0, map equality says the slice is nonempty and its first byte is exactly 48. The model's checked index succeeds at zero, the equality test takes the continue branch, and the in-bounds range-from-one operation drops exactly that byte. The invariant is restored with k-1, and the slice length has strictly decreased.
At k=0, map equality identifies the whole current byte-value list with T. If the slice is empty, so is T, and the loop returns it. Otherwise the stop hypothesis says its first value is not 48; injectivity of the U8 value representation makes the model's first-byte equality test false, so the loop again returns the current slice. Thus every terminal state has value list T. The initial invariant is the stated decomposition with k=n, and well-founded loop induction supplies the claimed normal return.

Lean proof


  let Inv := fun u : Slice Std.U8 =>
    exists k : Nat,
      u.val.map (fun b => b.val) = List.replicate k 48 ++ tail
  let Post := fun u : Slice Std.U8 =>
    u.val.map (fun b => b.val) = tail
  have hloop : Aeneas.Std.WP.spec
      (num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s) Post := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0
    apply Aeneas.Std.loop.spec_decr_nat
      (fun u : Slice Std.U8 => u.val.length) Inv Post
      (fun u => num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body u) s
    · intro u hu
      rcases hu with ⟨k, hk⟩
      simp only [Inv] at hk
      simp only [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body]
      cases k with
      | zero =>
          have huval : u.val.map (fun b => b.val) = tail := by
            simpa using hk
          cases huv : u.val with
          | nil =>
              have ht : tail = [] := by simpa [huv] using huval.symm
              simp [core.slice.Slice.is_empty, huv, WP.spec_ok, Post, ht]
          | cons b bs =>
              have hbval : b.val ≠ 48 := by
                rcases hstop with hnil | hne
                · rw [hnil] at huval
                  simp [huv] at huval
                · intro heq
                  apply hne
                  rw [← huval]
                  simp [huv, heq]
              have hb : b ≠ 48#u8 := by
                intro heq
                apply hbval
                simpa [heq]
              simp [core.slice.Slice.is_empty, Slice.index_usize, huv, hb,
                WP.spec_ok]
              simpa [Post, huv] using huval
      | succ k =>
          cases huv : u.val with
          | nil => simp [huv, List.replicate_succ] at hk
          | cons b bs =>
              have hbval : b.val = 48 := by
                have := congrArg List.head? hk
                simpa [huv, List.replicate_succ] using this
              have hb : b = 48#u8 := UScalar.eq_of_val_eq (by simpa using hbval)
              have hbs : bs.map (fun b => b.val) =
                  List.replicate k 48 ++ tail := by
                simpa [huv, hbval, List.replicate_succ] using hk
              simp [core.slice.Slice.is_empty, Slice.index_usize, huv, hb,
                core.slice.index.SliceIndexRangeFromUsizeSlice.index,
                Slice.drop, Slice.length, WP.spec_ok]
              exact ⟨k, hbs⟩
    · exact ⟨n, hs⟩
  obtain ⟨out, hout, houtPost⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  exact ⟨out, hout, houtPost⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqLeadingZeroLoopExact]
theorem DecimalSeqLeadingZeroLoopExact (s : Slice Std.U8) (n : Nat)
    (tail : List Nat)
    (hs : s.val.map (fun b => b.val) = List.replicate n 48 ++ tail)
    (hstop : tail = [] \/ tail[0]? ≠ some 48) :
    exists out : Slice Std.U8,
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s = ok out /\
      out.val.map (fun b => b.val) = tail := by
-- BODY
  let Inv := fun u : Slice Std.U8 =>
    exists k : Nat,
      u.val.map (fun b => b.val) = List.replicate k 48 ++ tail
  let Post := fun u : Slice Std.U8 =>
    u.val.map (fun b => b.val) = tail
  have hloop : Aeneas.Std.WP.spec
      (num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s) Post := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0
    apply Aeneas.Std.loop.spec_decr_nat
      (fun u : Slice Std.U8 => u.val.length) Inv Post
      (fun u => num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body u) s
    · intro u hu
      rcases hu with ⟨k, hk⟩
      simp only [Inv] at hk
      simp only [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body]
      cases k with
      | zero =>
          have huval : u.val.map (fun b => b.val) = tail := by
            simpa using hk
          cases huv : u.val with
          | nil =>
              have ht : tail = [] := by simpa [huv] using huval.symm
              simp [core.slice.Slice.is_empty, huv, WP.spec_ok, Post, ht]
          | cons b bs =>
              have hbval : b.val ≠ 48 := by
                rcases hstop with hnil | hne
                · rw [hnil] at huval
                  simp [huv] at huval
                · intro heq
                  apply hne
                  rw [← huval]
                  simp [huv, heq]
              have hb : b ≠ 48#u8 := by
                intro heq
                apply hbval
                simpa [heq]
              simp [core.slice.Slice.is_empty, Slice.index_usize, huv, hb,
                WP.spec_ok]
              simpa [Post, huv] using huval
      | succ k =>
          cases huv : u.val with
          | nil => simp [huv, List.replicate_succ] at hk
          | cons b bs =>
              have hbval : b.val = 48 := by
                have := congrArg List.head? hk
                simpa [huv, List.replicate_succ] using this
              have hb : b = 48#u8 := UScalar.eq_of_val_eq (by simpa using hbval)
              have hbs : bs.map (fun b => b.val) =
                  List.replicate k 48 ++ tail := by
                simpa [huv, hbval, List.replicate_succ] using hk
              simp [core.slice.Slice.is_empty, Slice.index_usize, huv, hb,
                core.slice.index.SliceIndexRangeFromUsizeSlice.index,
                Slice.drop, Slice.length, WP.spec_ok]
              exact ⟨k, hbs⟩
    · exact ⟨n, hs⟩
  obtain ⟨out, hout, houtPost⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  exact ⟨out, hout, houtPost⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (77)
More program dependencies (44)
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": "DecimalSeqLeadingZeroLoopExact",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "c95de21601e7f117ab422062c488e972966255877f85c662f8f75c60d4f03b95",
  "active_statement_hash": "9d95f3a4bb49e249f61288becef93ce9c3e4185d34cfe7670019f6c0dbe3c24e",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-167",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "7a20708611001f01"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "15eb2db9d4385140"
  }
}
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:e804f45aa83e3d5a0cb4278b828258d7ccfc881a9e37a1a2beed1108d5eb5b63",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}