NumberHelperIntegralAdjustmentSourceLength

Lean checked

Statement

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

Lemma · Integral leading-zero adjustment

Let a starting byte slice of length at most 9223372036854775807 consist of a list D of decimal digit bytes followed by a tail whose first byte, if any, is neither a decimal digit nor a decimal point. Let an isize n have value |D|-19. The extracted adjust_many_digit_count call returns normally. Moreover, if its result a is positive, then
D=Z\mathbin{++}[b]\mathbin{++}R, \qquad Z\text{ consists only of zero bytes}, \qquad 49\leq b\leq57, \qquad a=|D|-19-|Z|.

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_parse_adjust_many_digit_count

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperIntegralAdjustmentSourceLength]
theorem NumberHelperIntegralAdjustmentSourceLength
    (start : Slice Std.U8) (n : Std.Isize) (digits tail : List Std.U8)
    (hstart : start.val = digits ++ tail)
    (hdigits : forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest ->
      Not (48 <= b.val /\ b.val <= 57))
    (htailpoint : forall b rest, tail = b :: rest -> b ≠ 46#u8)
    (hlen : start.val.length <= 9223372036854775807)
    (hn : n.val = (digits.length : Int) - 19) :
    exists (adjusted : Std.Isize),
      num.imp.dec2flt.parse.adjust_many_digit_count start n = ok adjusted /\
      (adjusted.val > 0 -> exists (zeros : List Std.U8) (b : Std.U8)
          (remaining : List Std.U8),
        digits = zeros ++ b :: remaining /\
        (forall c, List.Mem c zeros -> c = 48#u8) /\
        49 <= b.val /\ b.val <= 57 /\
        adjusted.val = (digits.length : Int) - 19 - zeros.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

Unfold the adjustment loop in num imp dec2flt parse adjust many digit count. Use the natural measure 2(|s|-j)+1 while the loop is scanning and 2(|s|-j) after scanning stops, where j is the scan index. While scanning within the digit list, maintain a decomposition D=Z\mathbin{++}R, where Z contains only byte 48, together with
j=|Z| \quad\hbox{and}\quad c=|D|-19-|Z|,
for the current count c. If scanning has stopped, maintain either c\leq0, or a decomposition D=Z\mathbin{++}[b]\mathbin{++}R with Z all zero bytes, 49\leq b\leq57, and the same count equation.
At a remaining zero digit, the extracted saturating subtraction computes 48-(48-1)=1. Its cast to isize is exact, so both the index and zero-prefix length increase by one and the count decreases by one. The invariant is preserved and the measure strictly decreases. At a nonzero digit, decimality and inequality with byte 48 give the bounds 49\leq b\leq57. The loop changes the scanning flag without changing the count or index; this supplies the second invariant alternative and strictly decreases the parity part of the measure.
If the digit remainder is empty, the index is |D|. An empty tail ends the loop with count -19. A nonempty tail begins with neither a digit nor a point by hypothesis, so it stops scanning without changing the count, again at -19. Thus neither case can yield a positive result. All byte accesses are guarded by the slice length. The assumed bound \lvert\mathtt{start}\rvert\leq9223372036854775807 and the 64-bit scalar widths from Assumptions put every count subtraction and index increment within range. Well-founded iteration therefore returns normally, and a positive returned count excludes the nonpositive alternative, leaving exactly the displayed significant-prefix decomposition.

Lean proof


  have hloop : num.imp.dec2flt.parse.adjust_many_digit_count_loop
      start n 0#usize true ⦃ adjusted => adjusted.val > 0 ->
        exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8),
          digits = zeros ++ b :: remaining /\
          (forall c, List.Mem c zeros -> c = 48#u8) /\
          49 <= b.val /\ b.val <= 57 /\
          adjusted.val = (digits.length : Int) - 19 - zeros.length ⦄ := by
    unfold num.imp.dec2flt.parse.adjust_many_digit_count_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.Isize × Std.Usize × Bool =>
        2 * (start.val.length - p.2.1.val) + if p.2.2 then 1 else 0)
      (fun p =>
        (p.2.2 = true /\ exists (zeros remaining : List Std.U8),
          digits = zeros ++ remaining /\
          (forall c, List.Mem c zeros -> c = 48#u8) /\
          p.2.1.val = zeros.length /\
          p.1.val = (digits.length : Int) - 19 - zeros.length) \/
        (p.2.2 = false /\
          (p.1.val <= 0 \/ exists (zeros : List Std.U8) (b : Std.U8)
              (remaining : List Std.U8),
            digits = zeros ++ b :: remaining /\
            (forall c, List.Mem c zeros -> c = 48#u8) /\
            49 <= b.val /\ b.val <= 57 /\
            p.1.val = (digits.length : Int) - 19 - zeros.length)))
      (fun adjusted => adjusted.val > 0 ->
        exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8),
        digits = zeros ++ b :: remaining /\
        (forall c, List.Mem c zeros -> c = 48#u8) /\
        49 <= b.val /\ b.val <= 57 /\
        adjusted.val = (digits.length : Int) - 19 - zeros.length)
      (fun p => num.imp.dec2flt.parse.adjust_many_digit_count_loop.body
        start p.1 p.2.1 p.2.2)
      (n, 0#usize, true)
    · intro p hp
      rcases p with ⟨count, index, scanning⟩
      rcases hp with hscan | hstop
      · rcases hscan with ⟨hscanning, zeros, remaining, hpartition,
          hzeros, hindex, hcount⟩
        simp only [Prod.fst, Prod.snd] at hscanning hindex hcount ⊢
        cases scanning <;> simp at hscanning
        cases remaining with
        | nil =>
          have hdigitsEq : digits = zeros := by simpa using hpartition
          cases htailEq : tail with
          | nil =>
            have hindexNot : Not (index.val < start.val.length) := by
              simp [hstart, hdigitsEq, htailEq, hindex]
            simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexNot]
            intro hpositive
            rw [hcount, hdigitsEq] at hpositive
            omega
          | cons c cs =>
            have hcnotpoint := htailpoint c cs htailEq
            have hcnotdigit := htail c cs htailEq
            have hcnotzero : c ≠ 48#u8 := by
              intro hc
              subst c
              exact hcnotdigit (by norm_num)
            have hindexInside : index.val < start.val.length := by
              simp [hstart, hdigitsEq, htailEq, hindex]
            have hget : Slice.index_usize start index = ok c := by
              simp [Slice.index_usize, hstart, hdigitsEq, htailEq, hindex]
            simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexInside, hget, hcnotpoint, hcnotzero]
            exact Or.inl (by rw [hcount, hdigitsEq]; omega)
        | cons b bs =>
          have hb := hdigits b (by
            rw [hpartition]
            exact List.mem_append_right _ List.mem_cons_self)
          have hbnotpoint : b ≠ 46#u8 := by scalar_tac
          have hindexInside : index.val < start.val.length := by
            simp [hstart, hpartition, hindex]
          have hget : Slice.index_usize start index = ok b := by
            simp [Slice.index_usize, hstart, hpartition, hindex]
          by_cases hbzero : b = 48#u8
          · subst b
            have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl
            have hcast1 : UScalar.hcast .Isize (1#u8) = 1#isize := by
              apply IScalar.eq_of_val_eq
              have h := UScalar.hcast_inBounds_spec .Isize (1#u8) (by scalar_tac)
              simpa [Aeneas.Std.lift, WP.spec_ok] using h
            have hsaturate1 :
                core.num.U8.saturating_sub 48#u8 47#u8 = 1#u8 := by rfl
            simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexInside, hget, hi1, hsaturate1,
              Aeneas.Std.lift, hcast1]
            have hsublo :
                IScalar.min .Isize <= count.val - (1#isize).val := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.min, IScalarTy.numBits, hbits]
              have hzeroslen : zeros.length <= digits.length := by
                rw [hpartition]
                simp
              rw [hcount]
              omega
            have hsubhi :
                count.val - (1#isize).val <= IScalar.max .Isize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.max, IScalarTy.numBits, hbits]
              have hdigitlen : digits.length <= start.val.length := by
                rw [hstart]
                simp
              rw [hcount]
              omega
            obtain ⟨count1, hcount1, hcount1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (IScalar.sub_spec (x := count) (y := 1#isize)
                  hsublo hsubhi)
            rw [hcount1]
            have haddbound : index.val + 1 <= UScalar.max .Usize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [UScalar.max, hbits]
              omega
            obtain ⟨index1, hindex1, hindex1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (UScalar.add_spec (x := index) (y := 1#usize) (by
                  simpa using haddbound))
            rw [hindex1]
            simp [WP.spec_ok]
            refine ⟨⟨zeros ++ [48#u8], ⟨bs, ?_⟩, ?_, ?_, ?_⟩, ?_⟩
            · simpa [List.append_assoc] using hpartition
            · intro c hc
              rcases List.mem_append.mp hc with hc | hc
              · exact hzeros c hc
              · simpa using hc
            · norm_num at hindex1val
              simp
              omega
            · norm_num at hcount1val
              simp
              omega
            · norm_num at hindex1val
              omega
          · simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexInside, hget, hbnotpoint, hbzero]
            refine Or.inr ⟨zeros, b, ⟨bs, hpartition⟩,
              hzeros, ?_, hb.2, hcount⟩
            have : b.val ≠ 48 := by
              intro h
              apply hbzero
              apply UScalar.eq_of_val_eq
              norm_num at h ⊢
              exact h
            omega
      · rcases hstop with ⟨hscanning, hstop⟩
        simp only [Prod.fst, Prod.snd] at hscanning hstop ⊢
        cases scanning <;> simp at hscanning
        simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
          WP.spec_ok]
        rcases hstop with hnonpos | hwitness
        · intro hpositive
          omega
        · intro _
          rcases hwitness with ⟨zeros, b, remaining, hpartition,
            hzeros, hb1, hb2, hcount⟩
          exact ⟨zeros, b, ⟨remaining, hpartition⟩,
            hzeros, hb1, hb2, hcount⟩
    · refine Or.inl ⟨rfl, [], digits, by simp, ?_, by simp, ?_⟩
      · intro c hc
        cases hc
      · simpa using hn
  obtain ⟨adjusted, hadjusted, hpost⟩ :=
    Aeneas.Std.WP.spec_imp_exists hloop
  refine ⟨adjusted, ?_, hpost⟩
  simpa [num.imp.dec2flt.parse.adjust_many_digit_count]
    using hadjusted

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.num_imp_dec2flt_parse_adjust_many_digit_count

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: NumberHelperIntegralAdjustmentSourceLength]
theorem NumberHelperIntegralAdjustmentSourceLength
    (start : Slice Std.U8) (n : Std.Isize) (digits tail : List Std.U8)
    (hstart : start.val = digits ++ tail)
    (hdigits : forall b, List.Mem b digits -> 48 <= b.val /\ b.val <= 57)
    (htail : forall b rest, tail = b :: rest ->
      Not (48 <= b.val /\ b.val <= 57))
    (htailpoint : forall b rest, tail = b :: rest -> b ≠ 46#u8)
    (hlen : start.val.length <= 9223372036854775807)
    (hn : n.val = (digits.length : Int) - 19) :
    exists (adjusted : Std.Isize),
      num.imp.dec2flt.parse.adjust_many_digit_count start n = ok adjusted /\
      (adjusted.val > 0 -> exists (zeros : List Std.U8) (b : Std.U8)
          (remaining : List Std.U8),
        digits = zeros ++ b :: remaining /\
        (forall c, List.Mem c zeros -> c = 48#u8) /\
        49 <= b.val /\ b.val <= 57 /\
        adjusted.val = (digits.length : Int) - 19 - zeros.length) := by
-- BODY
  have hloop : num.imp.dec2flt.parse.adjust_many_digit_count_loop
      start n 0#usize true ⦃ adjusted => adjusted.val > 0 ->
        exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8),
          digits = zeros ++ b :: remaining /\
          (forall c, List.Mem c zeros -> c = 48#u8) /\
          49 <= b.val /\ b.val <= 57 /\
          adjusted.val = (digits.length : Int) - 19 - zeros.length ⦄ := by
    unfold num.imp.dec2flt.parse.adjust_many_digit_count_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.Isize × Std.Usize × Bool =>
        2 * (start.val.length - p.2.1.val) + if p.2.2 then 1 else 0)
      (fun p =>
        (p.2.2 = true /\ exists (zeros remaining : List Std.U8),
          digits = zeros ++ remaining /\
          (forall c, List.Mem c zeros -> c = 48#u8) /\
          p.2.1.val = zeros.length /\
          p.1.val = (digits.length : Int) - 19 - zeros.length) \/
        (p.2.2 = false /\
          (p.1.val <= 0 \/ exists (zeros : List Std.U8) (b : Std.U8)
              (remaining : List Std.U8),
            digits = zeros ++ b :: remaining /\
            (forall c, List.Mem c zeros -> c = 48#u8) /\
            49 <= b.val /\ b.val <= 57 /\
            p.1.val = (digits.length : Int) - 19 - zeros.length)))
      (fun adjusted => adjusted.val > 0 ->
        exists (zeros : List Std.U8) (b : Std.U8) (remaining : List Std.U8),
        digits = zeros ++ b :: remaining /\
        (forall c, List.Mem c zeros -> c = 48#u8) /\
        49 <= b.val /\ b.val <= 57 /\
        adjusted.val = (digits.length : Int) - 19 - zeros.length)
      (fun p => num.imp.dec2flt.parse.adjust_many_digit_count_loop.body
        start p.1 p.2.1 p.2.2)
      (n, 0#usize, true)
    · intro p hp
      rcases p with ⟨count, index, scanning⟩
      rcases hp with hscan | hstop
      · rcases hscan with ⟨hscanning, zeros, remaining, hpartition,
          hzeros, hindex, hcount⟩
        simp only [Prod.fst, Prod.snd] at hscanning hindex hcount ⊢
        cases scanning <;> simp at hscanning
        cases remaining with
        | nil =>
          have hdigitsEq : digits = zeros := by simpa using hpartition
          cases htailEq : tail with
          | nil =>
            have hindexNot : Not (index.val < start.val.length) := by
              simp [hstart, hdigitsEq, htailEq, hindex]
            simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexNot]
            intro hpositive
            rw [hcount, hdigitsEq] at hpositive
            omega
          | cons c cs =>
            have hcnotpoint := htailpoint c cs htailEq
            have hcnotdigit := htail c cs htailEq
            have hcnotzero : c ≠ 48#u8 := by
              intro hc
              subst c
              exact hcnotdigit (by norm_num)
            have hindexInside : index.val < start.val.length := by
              simp [hstart, hdigitsEq, htailEq, hindex]
            have hget : Slice.index_usize start index = ok c := by
              simp [Slice.index_usize, hstart, hdigitsEq, htailEq, hindex]
            simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexInside, hget, hcnotpoint, hcnotzero]
            exact Or.inl (by rw [hcount, hdigitsEq]; omega)
        | cons b bs =>
          have hb := hdigits b (by
            rw [hpartition]
            exact List.mem_append_right _ List.mem_cons_self)
          have hbnotpoint : b ≠ 46#u8 := by scalar_tac
          have hindexInside : index.val < start.val.length := by
            simp [hstart, hpartition, hindex]
          have hget : Slice.index_usize start index = ok b := by
            simp [Slice.index_usize, hstart, hpartition, hindex]
          by_cases hbzero : b = 48#u8
          · subst b
            have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl
            have hcast1 : UScalar.hcast .Isize (1#u8) = 1#isize := by
              apply IScalar.eq_of_val_eq
              have h := UScalar.hcast_inBounds_spec .Isize (1#u8) (by scalar_tac)
              simpa [Aeneas.Std.lift, WP.spec_ok] using h
            have hsaturate1 :
                core.num.U8.saturating_sub 48#u8 47#u8 = 1#u8 := by rfl
            simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexInside, hget, hi1, hsaturate1,
              Aeneas.Std.lift, hcast1]
            have hsublo :
                IScalar.min .Isize <= count.val - (1#isize).val := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.min, IScalarTy.numBits, hbits]
              have hzeroslen : zeros.length <= digits.length := by
                rw [hpartition]
                simp
              rw [hcount]
              omega
            have hsubhi :
                count.val - (1#isize).val <= IScalar.max .Isize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.max, IScalarTy.numBits, hbits]
              have hdigitlen : digits.length <= start.val.length := by
                rw [hstart]
                simp
              rw [hcount]
              omega
            obtain ⟨count1, hcount1, hcount1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (IScalar.sub_spec (x := count) (y := 1#isize)
                  hsublo hsubhi)
            rw [hcount1]
            have haddbound : index.val + 1 <= UScalar.max .Usize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [UScalar.max, hbits]
              omega
            obtain ⟨index1, hindex1, hindex1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (UScalar.add_spec (x := index) (y := 1#usize) (by
                  simpa using haddbound))
            rw [hindex1]
            simp [WP.spec_ok]
            refine ⟨⟨zeros ++ [48#u8], ⟨bs, ?_⟩, ?_, ?_, ?_⟩, ?_⟩
            · simpa [List.append_assoc] using hpartition
            · intro c hc
              rcases List.mem_append.mp hc with hc | hc
              · exact hzeros c hc
              · simpa using hc
            · norm_num at hindex1val
              simp
              omega
            · norm_num at hcount1val
              simp
              omega
            · norm_num at hindex1val
              omega
          · simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
              WP.spec_ok, hindexInside, hget, hbnotpoint, hbzero]
            refine Or.inr ⟨zeros, b, ⟨bs, hpartition⟩,
              hzeros, ?_, hb.2, hcount⟩
            have : b.val ≠ 48 := by
              intro h
              apply hbzero
              apply UScalar.eq_of_val_eq
              norm_num at h ⊢
              exact h
            omega
      · rcases hstop with ⟨hscanning, hstop⟩
        simp only [Prod.fst, Prod.snd] at hscanning hstop ⊢
        cases scanning <;> simp at hscanning
        simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
          WP.spec_ok]
        rcases hstop with hnonpos | hwitness
        · intro hpositive
          omega
        · intro _
          rcases hwitness with ⟨zeros, b, remaining, hpartition,
            hzeros, hb1, hb2, hcount⟩
          exact ⟨zeros, b, ⟨remaining, hpartition⟩,
            hzeros, hb1, hb2, hcount⟩
    · refine Or.inl ⟨rfl, [], digits, by simp, ?_, by simp, ?_⟩
      · intro c hc
        cases hc
      · simpa using hn
  obtain ⟨adjusted, hadjusted, hpost⟩ :=
    Aeneas.Std.WP.spec_imp_exists hloop
  refine ⟨adjusted, ?_, hpost⟩
  simpa [num.imp.dec2flt.parse.adjust_many_digit_count]
    using hadjusted

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
Technical checking records
Local closure summary
{
  "node": "NumberHelperIntegralAdjustmentSourceLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "eb2449d2da72d9bad964897152bbfba5addd6d30765ab379b221a42af73ffbf7",
  "active_statement_hash": "a47a69b30acd711e661c4ffbba89798dd965177ec5bc2daf03723409f636f194",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.configured_x86_64_numBits",
    "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-116",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "num_imp_dec2flt_parse_adjust_many_digit_count": "7a20708611001f01"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_parse_adjust_many_digit_count": "1bc88a1b016164f3"
  }
}
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:2a783c4ea8c3beb3f9819bd682f5864aeea19a0b93086c3d23d5e5c384660c29",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}