NumberHelperAdjustManySourceLength

Lean checked

Statement

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

Lemma · Safe bounded many-digit adjustment

Let s be an Aeneas byte slice of length at most 9223372036854775807, and let n be a nonnegative isize whose value is at most |s|. The extracted adjust_many_digit_count operation returns normally.

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: NumberHelperAdjustManySourceLength]
theorem NumberHelperAdjustManySourceLength
    (start : Slice Std.U8) (n : Std.Isize)
    (hlen : start.val.length <= 9223372036854775807)
    (hnonneg : 0 <= n.val) (hnle : n.val <= start.val.length) :
    exists adjusted,
      num.imp.dec2flt.parse.adjust_many_digit_count start n = ok adjusted := 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 loop in num imp dec2flt parse adjust many digit count. At a loop head with count c and scan index j, maintain
0\leq j\leq |s|,\qquad n-j\leq c\leq n.
Use the lexicographic natural measure encoded by 2(|s|-j)+1 while scanning and 2(|s|-j) after scanning stops.
Every byte access is guarded by j<|s|. A point byte advances the index and subtracts zero, because 48-1=47 and saturating subtraction of 47 from 46 is zero. A zero digit advances the index and subtracts one, because saturating subtraction of 47 from 48 is one. Both byte values cast exactly to isize. In the first branch the new count remains above n-(j+1); in the second it is exactly one less and again remains above that lower bound. A different byte changes the scanning flag from true to false without changing the index, which decreases the parity part of the measure; the next iteration returns. Reaching the end also returns.
Throughout the loop, -|s|\leq c\leq n\leq |s|. By Assumptions, the configured isize and usize widths are 64 bits, and the assumed length is at most 2^{63}-1. Consequently both possible signed subtractions and every guarded index increment succeed. The measure decreases on every continuing branch, so the loop terminates normally and supplies the claimed result.

Lean proof


  have hloop : num.imp.dec2flt.parse.adjust_many_digit_count_loop
      start n 0#usize true ⦃ _ => True ⦄ := 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.1.val <= start.val.length /\
        n.val - p.2.1.val <= p.1.val /\ p.1.val <= n.val)
      (fun _ => True)
      (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 ⟨hindex, hcountlo, hcounthi⟩
      simp only [Prod.fst, Prod.snd] at hindex hcountlo hcounthi
      simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
        WP.spec_ok, Slice.len_val]
      split
      · rename_i hinside
        split
        · rename_i hscanning
          have hget : exists c, Slice.index_usize start index = ok c := by
            simp [Slice.index_usize, hinside]
          obtain ⟨c, hc⟩ := hget
          rw [hc]
          by_cases hpoint : c = 46#u8
          · subst c
            simp
            have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl
            rw [hi1]
            simp
            have hcast0 : UScalar.hcast .Isize (0#u8) = 0#isize := by
              apply IScalar.eq_of_val_eq
              have h := UScalar.hcast_inBounds_spec .Isize (0#u8) (by scalar_tac)
              simpa [Aeneas.Std.lift, WP.spec_ok] using h
            have hsaturate0 :
                core.num.U8.saturating_sub 46#u8 47#u8 = 0#u8 := by
              rfl
            rw [hsaturate0]
            simp [Aeneas.Std.lift]
            rw [hcast0]
            have hsublo : IScalar.min .Isize <= count.val - (0#isize).val := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.min, IScalarTy.numBits, hbits]
              omega
            have hsubhi : count.val - (0#isize).val <= IScalar.max .Isize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.max, IScalarTy.numBits, hbits]
              omega
            have hsub := IScalar.sub_spec hsublo hsubhi
            obtain ⟨count1, hcount1, hcount1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists hsub
            rw [hcount1]
            have haddbound :
                index.val + (1#usize).val <= UScalar.max .Usize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [UScalar.max, hbits]
              omega
            have hadd := UScalar.add_spec haddbound
            obtain ⟨index1, hindex1, hindex1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists hadd
            rw [hindex1]
            simp [WP.spec_ok, Slice.length]
            simp at hcount1val hindex1val
            omega
          · simp [hpoint]
            by_cases hzero : c = 48#u8
            · subst c
              simp
              have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl
              rw [hi1]
              simp
              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
              rw [hsaturate1]
              simp [Aeneas.Std.lift]
              rw [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]
                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]
                omega
              have hsub := IScalar.sub_spec hsublo hsubhi
              obtain ⟨count1, hcount1, hcount1val⟩ :=
                Aeneas.Std.WP.spec_imp_exists hsub
              rw [hcount1]
              have haddbound :
                  index.val + (1#usize).val <= UScalar.max .Usize := by
                have hbits := Dec2Flt.configured_x86_64_numBits
                simp [UScalar.max, hbits]
                omega
              have hadd := UScalar.add_spec haddbound
              obtain ⟨index1, hindex1, hindex1val⟩ :=
                Aeneas.Std.WP.spec_imp_exists hadd
              rw [hindex1]
              simp [WP.spec_ok, Slice.length]
              simp at hcount1val hindex1val
              omega
            · simp [hzero, WP.spec_ok]
              omega
        · simp [WP.spec_ok]
      · simp [WP.spec_ok]
    · simp [hnonneg, hnle]
  obtain ⟨adjusted, hadjusted, _⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  exact ⟨adjusted, 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: NumberHelperAdjustManySourceLength]
theorem NumberHelperAdjustManySourceLength
    (start : Slice Std.U8) (n : Std.Isize)
    (hlen : start.val.length <= 9223372036854775807)
    (hnonneg : 0 <= n.val) (hnle : n.val <= start.val.length) :
    exists adjusted,
      num.imp.dec2flt.parse.adjust_many_digit_count start n = ok adjusted := by
-- BODY
  have hloop : num.imp.dec2flt.parse.adjust_many_digit_count_loop
      start n 0#usize true ⦃ _ => True ⦄ := 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.1.val <= start.val.length /\
        n.val - p.2.1.val <= p.1.val /\ p.1.val <= n.val)
      (fun _ => True)
      (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 ⟨hindex, hcountlo, hcounthi⟩
      simp only [Prod.fst, Prod.snd] at hindex hcountlo hcounthi
      simp [num.imp.dec2flt.parse.adjust_many_digit_count_loop.body,
        WP.spec_ok, Slice.len_val]
      split
      · rename_i hinside
        split
        · rename_i hscanning
          have hget : exists c, Slice.index_usize start index = ok c := by
            simp [Slice.index_usize, hinside]
          obtain ⟨c, hc⟩ := hget
          rw [hc]
          by_cases hpoint : c = 46#u8
          · subst c
            simp
            have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl
            rw [hi1]
            simp
            have hcast0 : UScalar.hcast .Isize (0#u8) = 0#isize := by
              apply IScalar.eq_of_val_eq
              have h := UScalar.hcast_inBounds_spec .Isize (0#u8) (by scalar_tac)
              simpa [Aeneas.Std.lift, WP.spec_ok] using h
            have hsaturate0 :
                core.num.U8.saturating_sub 46#u8 47#u8 = 0#u8 := by
              rfl
            rw [hsaturate0]
            simp [Aeneas.Std.lift]
            rw [hcast0]
            have hsublo : IScalar.min .Isize <= count.val - (0#isize).val := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.min, IScalarTy.numBits, hbits]
              omega
            have hsubhi : count.val - (0#isize).val <= IScalar.max .Isize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [IScalar.max, IScalarTy.numBits, hbits]
              omega
            have hsub := IScalar.sub_spec hsublo hsubhi
            obtain ⟨count1, hcount1, hcount1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists hsub
            rw [hcount1]
            have haddbound :
                index.val + (1#usize).val <= UScalar.max .Usize := by
              have hbits := Dec2Flt.configured_x86_64_numBits
              simp [UScalar.max, hbits]
              omega
            have hadd := UScalar.add_spec haddbound
            obtain ⟨index1, hindex1, hindex1val⟩ :=
              Aeneas.Std.WP.spec_imp_exists hadd
            rw [hindex1]
            simp [WP.spec_ok, Slice.length]
            simp at hcount1val hindex1val
            omega
          · simp [hpoint]
            by_cases hzero : c = 48#u8
            · subst c
              simp
              have hi1 : (48#u8 - 1#u8) = ok 47#u8 := by rfl
              rw [hi1]
              simp
              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
              rw [hsaturate1]
              simp [Aeneas.Std.lift]
              rw [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]
                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]
                omega
              have hsub := IScalar.sub_spec hsublo hsubhi
              obtain ⟨count1, hcount1, hcount1val⟩ :=
                Aeneas.Std.WP.spec_imp_exists hsub
              rw [hcount1]
              have haddbound :
                  index.val + (1#usize).val <= UScalar.max .Usize := by
                have hbits := Dec2Flt.configured_x86_64_numBits
                simp [UScalar.max, hbits]
                omega
              have hadd := UScalar.add_spec haddbound
              obtain ⟨index1, hindex1, hindex1val⟩ :=
                Aeneas.Std.WP.spec_imp_exists hadd
              rw [hindex1]
              simp [WP.spec_ok, Slice.length]
              simp at hcount1val hindex1val
              omega
            · simp [hzero, WP.spec_ok]
              omega
        · simp [WP.spec_ok]
      · simp [WP.spec_ok]
    · simp [hnonneg, hnle]
  obtain ⟨adjusted, hadjusted, _⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  exact ⟨adjusted, 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": "NumberHelperAdjustManySourceLength",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "18d6142ef36a8214253a601c843b0b3b9a250b99e64a222c1d5696655c967400",
  "active_statement_hash": "5b69ae9e44571f54cc556c64ad7af6781f93b499372b3280899525adf457b012",
  "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:08296c86ad91345b6199aac17ff5fac40da4d14b621c41c32d3e593b0dbddf88",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}