NumberHelperMaximalDigitScan

Lean checked

Statement

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

Helper · Maximal leading-one digit scan

On a slice consisting of 9223372036854775800=8q ASCII one bytes followed by e655360, the extracted ordinary digit scanner succeeds, leaves exactly the seven-byte suffix e655360, and returns some unsigned accumulator.

Lean statement

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

import Tablet.NumberHelperEightDigitWordAccepted
import Tablet.NumberHelperParseEightDigitsSafe
import Tablet.NumberHelperReadEightBytes
import Tablet.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperMaximalDigitScan]
theorem NumberHelperMaximalDigitScan (s : Slice Std.U8)
    (hs : s.val = List.replicate 9223372036854775800 49#u8 ++
      [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) :
    exists (rest : Slice Std.U8) (value : Std.U64),
      num.imp.dec2flt.parse.try_parse_digits s 0#u64 = ok (rest, value) /\
      rest.val = [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] := 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

Put q=1152921504606846975 and use the loop invariant that the current slice is 8m ASCII one bytes followed by e655360, for some m\leq q. When m>0, its length is at least eight. By NumberHelperReadEightBytes, the bulk read succeeds and every byte of the returned word agrees with one of the first eight slice bytes, hence is 49. The hypotheses of NumberHelperEightDigitWordAccepted are therefore satisfied, so the digit test returns true. Then NumberHelperParseEightDigitsSafe shows that the eight-digit helper also returns normally. The two accumulator operations in num imp dec2flt parse try parse digits are wrapping operations and cannot fail, and the in-bounds range operation drops exactly eight bytes. Thus m decreases by one without expanding the replicated list.
At m=0 the remaining list is the seven-byte suffix, so the bulk length guard is false. The scalar loop reads its initial byte 101; wrapping subtraction of 48 is 53, which is not below ten, so its count remains zero. The final drop by zero preserves the suffix. This proves the exact residual assertion; the wrapped accumulator is immaterial.

Lean proof


  let tail : List Std.U8 :=
    [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]
  have hs' : s.val = List.replicate (8 * 1152921504606846975) 49#u8 ++ tail := by
    have hmul : 8 * 1152921504606846975 = 9223372036854775800 := by
      norm_num
    rw [hmul]
    exact hs
  have fast : forall (t : Slice Std.U8) (a : Std.U64) (n : Nat),
      t.val = List.replicate (8 * n) 49#u8 ++ tail ->
      num.imp.dec2flt.parse.try_parse_digits_loop0 t a
        ⦃ p => p.1.val = tail ⦄ := by
    intro t a n htn
    unfold num.imp.dec2flt.parse.try_parse_digits_loop0
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Slice Std.U8 × Std.U64 => p.1.length)
      (fun p => exists m : Nat,
        p.1.val = List.replicate (8 * m) 49#u8 ++ tail)
      (fun p => p.1.val = tail)
      (fun p => num.imp.dec2flt.parse.try_parse_digits_loop0.body p.1 p.2)
      (t, a)
    · intro p hp
      rcases p with ⟨u, x⟩
      rcases hp with ⟨m, hum⟩
      change u.val = List.replicate (8 * m) 49#u8 ++ tail at hum
      simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body, Slice.length]
      by_cases hm : m = 0
      · subst m
        have hutail : u.val = tail := by simpa using hum
        simp [hutail, tail]
      · have h8 : 8 <= u.val.length := by
          rw [hum]
          simp
          omega
        simp only [h8, ↓reduceIte]
        obtain ⟨chunk, hread, hbyte⟩ := NumberHelperReadEightBytes u h8
        have haccept :
            num.imp.dec2flt.common.is_8digits chunk = ok true := by
          apply NumberHelperEightDigitWordAccepted
          intro i hi
          rw [hbyte i hi]
          have him : i < 8 * m := by omega
          simp [hum, him]
        obtain ⟨z, hparse⟩ := NumberHelperParseEightDigitsSafe chunk haccept
        simp [hread, haccept, hparse,
          core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, Slice.length, h8]
        constructor
        · refine ⟨m - 1, ?_⟩
          change List.drop 8 u.val =
            List.replicate (8 * (m - 1)) 49#u8 ++ tail
          rw [hum]
          have hdrop :
              (List.replicate (8 * m) 49#u8).drop 8 =
                List.replicate (8 * (m - 1)) 49#u8 := by
            rw [List.drop_replicate]
            congr 2
            omega
          rw [List.drop_append_of_le_length (by simp; omega), hdrop]
        · simp
          omega
    · exact ⟨n, htn⟩
  have hf := fast s 0#u64 1152921504606846975 hs'
  obtain ⟨p, hfast, hp⟩ := Aeneas.Std.WP.spec_imp_exists hf
  rcases p with ⟨s1, x1⟩
  change s1.val = tail at hp
  have hloop1 :
      num.imp.dec2flt.parse.try_parse_digits_loop1 s1 x1 0#usize =
        ok (x1, 0#usize) := by
    unfold num.imp.dec2flt.parse.try_parse_digits_loop1
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body,
      Slice.index_usize, hp, tail, core.num.U8.wrapping_sub,
      Aeneas.Std.lift, U8.size_eq]
  unfold num.imp.dec2flt.parse.try_parse_digits
  simp [hfast, hloop1,
    core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop,
    Slice.length]
  simpa [tail] using hp

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.NumberHelperEightDigitWordAccepted
import Tablet.NumberHelperParseEightDigitsSafe
import Tablet.NumberHelperReadEightBytes
import Tablet.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperMaximalDigitScan]
theorem NumberHelperMaximalDigitScan (s : Slice Std.U8)
    (hs : s.val = List.replicate 9223372036854775800 49#u8 ++
      [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) :
    exists (rest : Slice Std.U8) (value : Std.U64),
      num.imp.dec2flt.parse.try_parse_digits s 0#u64 = ok (rest, value) /\
      rest.val = [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] := by
-- BODY
  let tail : List Std.U8 :=
    [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]
  have hs' : s.val = List.replicate (8 * 1152921504606846975) 49#u8 ++ tail := by
    have hmul : 8 * 1152921504606846975 = 9223372036854775800 := by
      norm_num
    rw [hmul]
    exact hs
  have fast : forall (t : Slice Std.U8) (a : Std.U64) (n : Nat),
      t.val = List.replicate (8 * n) 49#u8 ++ tail ->
      num.imp.dec2flt.parse.try_parse_digits_loop0 t a
        ⦃ p => p.1.val = tail ⦄ := by
    intro t a n htn
    unfold num.imp.dec2flt.parse.try_parse_digits_loop0
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Slice Std.U8 × Std.U64 => p.1.length)
      (fun p => exists m : Nat,
        p.1.val = List.replicate (8 * m) 49#u8 ++ tail)
      (fun p => p.1.val = tail)
      (fun p => num.imp.dec2flt.parse.try_parse_digits_loop0.body p.1 p.2)
      (t, a)
    · intro p hp
      rcases p with ⟨u, x⟩
      rcases hp with ⟨m, hum⟩
      change u.val = List.replicate (8 * m) 49#u8 ++ tail at hum
      simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body, Slice.length]
      by_cases hm : m = 0
      · subst m
        have hutail : u.val = tail := by simpa using hum
        simp [hutail, tail]
      · have h8 : 8 <= u.val.length := by
          rw [hum]
          simp
          omega
        simp only [h8, ↓reduceIte]
        obtain ⟨chunk, hread, hbyte⟩ := NumberHelperReadEightBytes u h8
        have haccept :
            num.imp.dec2flt.common.is_8digits chunk = ok true := by
          apply NumberHelperEightDigitWordAccepted
          intro i hi
          rw [hbyte i hi]
          have him : i < 8 * m := by omega
          simp [hum, him]
        obtain ⟨z, hparse⟩ := NumberHelperParseEightDigitsSafe chunk haccept
        simp [hread, haccept, hparse,
          core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, Slice.length, h8]
        constructor
        · refine ⟨m - 1, ?_⟩
          change List.drop 8 u.val =
            List.replicate (8 * (m - 1)) 49#u8 ++ tail
          rw [hum]
          have hdrop :
              (List.replicate (8 * m) 49#u8).drop 8 =
                List.replicate (8 * (m - 1)) 49#u8 := by
            rw [List.drop_replicate]
            congr 2
            omega
          rw [List.drop_append_of_le_length (by simp; omega), hdrop]
        · simp
          omega
    · exact ⟨n, htn⟩
  have hf := fast s 0#u64 1152921504606846975 hs'
  obtain ⟨p, hfast, hp⟩ := Aeneas.Std.WP.spec_imp_exists hf
  rcases p with ⟨s1, x1⟩
  change s1.val = tail at hp
  have hloop1 :
      num.imp.dec2flt.parse.try_parse_digits_loop1 s1 x1 0#usize =
        ok (x1, 0#usize) := by
    unfold num.imp.dec2flt.parse.try_parse_digits_loop1
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body,
      Slice.index_usize, hp, tail, core.num.U8.wrapping_sub,
      Aeneas.Std.lift, U8.size_eq]
  unfold num.imp.dec2flt.parse.try_parse_digits
  simp [hfast, hloop1,
    core.slice.index.SliceIndexRangeFromUsizeSlice.index, Slice.drop,
    Slice.length]
  simpa [tail] using hp

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (55)
More program dependencies (22)
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 12 entries
Technical checking records
Local closure summary
{
  "node": "NumberHelperMaximalDigitScan",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "2aa24bec4282fa3243ddf0225487f150c55f5ff846572c8a51595544fbca798e",
  "active_statement_hash": "588d0886761ff0d93809f5ceeaadf97dbe2eb5211f5bf80b05b721542aa7cdc4",
  "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-115",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "NumberHelperEightDigitWordAccepted": "eb5cd4fd1f7849dc",
    "NumberHelperParseEightDigitsSafe": "9fe8ed9fecf3320d",
    "NumberHelperReadEightBytes": "8738e3b807b9b2d9",
    "num_imp_dec2flt_parse_try_parse_digits": "0c0fe376f06b63b0"
  },
  "strict_definition_deps": {
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64": "279b0c1cd8ec9e39",
    "num_imp_dec2flt_common_is_8digits": "53e9de5212d69bf5",
    "num_imp_dec2flt_parse_parse_8digits": "ae610d45d88851c6",
    "num_imp_dec2flt_parse_try_parse_digits": "2b12615e16e91640"
  }
}
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:c50a2a57d9b4592712f39f72f24274da3ff12f7d61db5f267f298a83a7930d79",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}