SlowAccumulatedRoundingDecimal

Lean checked

Statement

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

Lemma · Exact long decimal-sequence result

Let N=2147483648. If an Aeneas byte slice s satisfies s.\mathit{val}=\operatorname{replicate}(N,49\#\mathrm{u8}), then there is an array a of 768 unsigned bytes for which parse_decimal_seq(s) returns the record
\{\mathit{num\_digits}=768,\ \mathit{decimal\_point}=-2147483648,\ \mathit{truncated}=\mathrm{true},\ \mathit{digits}=a\}.

Lean statement

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

import Tablet.SlowAccumulatedRoundingCount
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: SlowAccumulatedRoundingDecimal]
theorem SlowAccumulatedRoundingDecimal (s : Slice Std.U8)
    (hs : s.val = List.replicate 2147483648 49#u8) :
    exists a : Array Std.U8 768#usize,
      num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok {
        num_digits := 768#usize
        decimal_point := (-2147483648)#i32
        truncated := true
        digits := a } := 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 decimal parser in num imp dec2flt decimal seq parse decimal seq. Its default record, from num imp dec2flt decimal seq DecimalSeq Insts CoreDefaultDefault default, has zero count, zero point, false truncation, and a 768-byte zero array. The input is nonempty. Its bounds-checked zeroth byte is 49, so the leading-zero loop sees a byte unequal to 48 and immediately returns the original slice.
Apply SlowAccumulatedRoundingCount to the following digit loop. It returns an empty residual slice and a sequence d_1 with exact count N, zero point, and false truncation. During those iterations the first 768 digits are the only array entries eligible for update; each scanned digit is 49-48=1, and later iterations leave the bounded array alone. Denote the resulting array by a=d_1.\mathit{digits}. The theorem only needs this array's fixed type, not an extensional expansion of its 768 entries.
The consumed length is N-0=N. The last bounds-checked input byte, at index N-1=2147483647, is 49, not ASCII zero; hence num imp dec2flt decimal seq count trailing zeros takes one loop step and returns zero trailing zeroes. Casting that zero to i32 gives zero. The parser's cast of the consumed count is the 32-bit two's-complement narrowing
\operatorname{hcast}_{\mathrm{i32}}(2147483648) = -2147483648.
This equality follows directly by comparing the 32-bit vectors, whose unsigned value on both sides is 2^{31}. Adding it to the old point zero does not overflow and gives i32::MIN.
Since N>768, the parser exposes \min(N,768)=768 as its final digit count and changes the truncation flag to true. The cleanup range starts at 768 and ends at the fixed MAX_DIGITS_WITHOUT_OVERFLOW value 19, so its comparison is false and it performs no update. Substitution into the cited parser definition gives exactly the displayed record.

Lean proof


  let d0 : num.imp.dec2flt.decimal_seq.DecimalSeq := {
    num_digits := 0#usize
    decimal_point := 0#i32
    truncated := false
    digits := Array.repeat 768#usize 0#u8 }
  have hdefault :
      num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default =
        ok d0 := by rfl
  have hsLen : s.val.length = 2147483648 := by
    rw [hs]
    exact List.length_replicate
  have hnonempty : 0 < s.val.length := by omega
  have hidx0 : Slice.index_usize s 0#usize = ok 49#u8 := by
    have hget : s.val[0]'(by omega) = 49#u8 := by
      simpa only [hs, List.getElem_replicate]
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (Slice.index_usize_spec s 0#usize (by
        simpa [Slice.length] using hnonempty))
    rw [hz]
    congr
    exact hzval.trans hget
  have hleading :
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s = ok s := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body,
      core.slice.Slice.is_empty, hnonempty, hidx0]
  obtain ⟨empty, d1, hscan, hempty, hdcount, hdpoint, hdtrunc⟩ :=
    SlowAccumulatedRoundingCount s d0 hs rfl rfl rfl
  have hemptyLen : empty.val.length = 0 := by rw [hempty]; rfl
  have hemptyTest : core.slice.Slice.is_empty empty = ok true := by
    simp [core.slice.Slice.is_empty, hempty]
  have hsUsize : Slice.len s = 2147483648#usize := by
    apply UScalar.eq_of_val_eq
    simpa [Slice.len_val] using hsLen
  have hemptyUsize : Slice.len empty = 0#usize := by
    apply UScalar.eq_of_val_eq
    simpa [Slice.len_val] using hemptyLen
  have hconsumed : (2147483648#usize : Std.Usize) - 0#usize =
      ok 2147483648#usize := by
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := 2147483648#usize) (y := 0#usize) (by norm_num))
    rw [hz]
    congr 2
    apply UScalar.eq_of_val_eq
    simpa using hzval
  have hpred : (2147483648#usize : Std.Usize) - 1#usize =
      ok 2147483647#usize := by
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := 2147483648#usize) (y := 1#usize) (by norm_num))
    rw [hz]
    congr 2
    apply UScalar.eq_of_val_eq
    simpa using hzval
  have hlast : Slice.index_usize s 2147483647#usize = ok 49#u8 := by
    have hget : s.val[2147483647]'(by omega) = 49#u8 := by
      simpa only [hs, List.getElem_replicate]
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (Slice.index_usize_spec s 2147483647#usize (by
        simp [Slice.length, hsLen]))
    rw [hz]
    congr
    exact hzval.trans hget
  have hzeros :
      num.imp.dec2flt.decimal_seq.count_trailing_zeros s
        2147483648#usize = ok 0#usize := by
    unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros
    unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop.body,
      hpred, hlast]
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop.body]
  have hcastZero : UScalar.hcast .I32 (0#usize : Std.Usize) = 0#i32 := by
    rw [IScalar.eq_equiv_bv_eq]
    apply BitVec.eq_of_toNat_eq
    simp only [UScalar.hcast, BitVec.zeroExtend_eq_setWidth,
      BitVec.toNat_setWidth, UScalar.bv_toNat, I32.ofInt_bv,
      BitVec.toNat_ofInt]
    norm_num
  have hcastN : UScalar.hcast .I32 (2147483648#usize : Std.Usize) =
      (-2147483648)#i32 := by
    rw [IScalar.eq_equiv_bv_eq]
    apply BitVec.eq_of_toNat_eq
    simp only [UScalar.hcast, BitVec.zeroExtend_eq_setWidth,
      BitVec.toNat_setWidth, UScalar.bv_toNat, I32.ofInt_bv,
      BitVec.toNat_ofInt]
    norm_num
    rfl
  have haddZero : ((0#i32 : Std.I32) + 0#i32) = ok 0#i32 := by rfl
  have hsubZero : ((2147483648#usize : Std.Usize) - 0#usize) =
      ok 2147483648#usize := hconsumed
  have haddMin : ((0#i32 : Std.I32) + (-2147483648)#i32) =
      ok (-2147483648)#i32 := by rfl
  have hlarge : (2147483648#usize : Std.Usize) >
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
    rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
    norm_num
  have hcleanup (a : Array Std.U8 768#usize) :
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := 768#usize,
          «end» := num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        a = ok a := by
    have hlt : core.iter.range.StepUsize.partialOrdInst.lt
        768#usize 19#usize = ok false := by rfl
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body,
      core.iter.range.IteratorRange.next,
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def,
      hlt]
  refine ⟨d1.digits, ?_⟩
  unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq
  simp [hdefault, hleading, hscan, hempty, hemptyTest, hdcount, hsUsize,
    hemptyUsize, hconsumed, hzeros, hcastZero, hdpoint, haddZero,
    hsubZero, hcastN, haddMin, hlarge, hcleanup, hdtrunc,
    Aeneas.Std.lift,
    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.SlowAccumulatedRoundingCount
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_seq

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: SlowAccumulatedRoundingDecimal]
theorem SlowAccumulatedRoundingDecimal (s : Slice Std.U8)
    (hs : s.val = List.replicate 2147483648 49#u8) :
    exists a : Array Std.U8 768#usize,
      num.imp.dec2flt.decimal_seq.parse_decimal_seq s = ok {
        num_digits := 768#usize
        decimal_point := (-2147483648)#i32
        truncated := true
        digits := a } := by
-- BODY
  let d0 : num.imp.dec2flt.decimal_seq.DecimalSeq := {
    num_digits := 0#usize
    decimal_point := 0#i32
    truncated := false
    digits := Array.repeat 768#usize 0#u8 }
  have hdefault :
      num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default =
        ok d0 := by rfl
  have hsLen : s.val.length = 2147483648 := by
    rw [hs]
    exact List.length_replicate
  have hnonempty : 0 < s.val.length := by omega
  have hidx0 : Slice.index_usize s 0#usize = ok 49#u8 := by
    have hget : s.val[0]'(by omega) = 49#u8 := by
      simpa only [hs, List.getElem_replicate]
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (Slice.index_usize_spec s 0#usize (by
        simpa [Slice.length] using hnonempty))
    rw [hz]
    congr
    exact hzval.trans hget
  have hleading :
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0 s = ok s := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop0.body,
      core.slice.Slice.is_empty, hnonempty, hidx0]
  obtain ⟨empty, d1, hscan, hempty, hdcount, hdpoint, hdtrunc⟩ :=
    SlowAccumulatedRoundingCount s d0 hs rfl rfl rfl
  have hemptyLen : empty.val.length = 0 := by rw [hempty]; rfl
  have hemptyTest : core.slice.Slice.is_empty empty = ok true := by
    simp [core.slice.Slice.is_empty, hempty]
  have hsUsize : Slice.len s = 2147483648#usize := by
    apply UScalar.eq_of_val_eq
    simpa [Slice.len_val] using hsLen
  have hemptyUsize : Slice.len empty = 0#usize := by
    apply UScalar.eq_of_val_eq
    simpa [Slice.len_val] using hemptyLen
  have hconsumed : (2147483648#usize : Std.Usize) - 0#usize =
      ok 2147483648#usize := by
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := 2147483648#usize) (y := 0#usize) (by norm_num))
    rw [hz]
    congr 2
    apply UScalar.eq_of_val_eq
    simpa using hzval
  have hpred : (2147483648#usize : Std.Usize) - 1#usize =
      ok 2147483647#usize := by
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.sub_spec (x := 2147483648#usize) (y := 1#usize) (by norm_num))
    rw [hz]
    congr 2
    apply UScalar.eq_of_val_eq
    simpa using hzval
  have hlast : Slice.index_usize s 2147483647#usize = ok 49#u8 := by
    have hget : s.val[2147483647]'(by omega) = 49#u8 := by
      simpa only [hs, List.getElem_replicate]
    obtain ⟨z, hz, hzval⟩ := Aeneas.Std.WP.spec_imp_exists
      (Slice.index_usize_spec s 2147483647#usize (by
        simp [Slice.length, hsLen]))
    rw [hz]
    congr
    exact hzval.trans hget
  have hzeros :
      num.imp.dec2flt.decimal_seq.count_trailing_zeros s
        2147483648#usize = ok 0#usize := by
    unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros
    unfold num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop.body,
      hpred, hlast]
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.count_trailing_zeros_loop.body]
  have hcastZero : UScalar.hcast .I32 (0#usize : Std.Usize) = 0#i32 := by
    rw [IScalar.eq_equiv_bv_eq]
    apply BitVec.eq_of_toNat_eq
    simp only [UScalar.hcast, BitVec.zeroExtend_eq_setWidth,
      BitVec.toNat_setWidth, UScalar.bv_toNat, I32.ofInt_bv,
      BitVec.toNat_ofInt]
    norm_num
  have hcastN : UScalar.hcast .I32 (2147483648#usize : Std.Usize) =
      (-2147483648)#i32 := by
    rw [IScalar.eq_equiv_bv_eq]
    apply BitVec.eq_of_toNat_eq
    simp only [UScalar.hcast, BitVec.zeroExtend_eq_setWidth,
      BitVec.toNat_setWidth, UScalar.bv_toNat, I32.ofInt_bv,
      BitVec.toNat_ofInt]
    norm_num
    rfl
  have haddZero : ((0#i32 : Std.I32) + 0#i32) = ok 0#i32 := by rfl
  have hsubZero : ((2147483648#usize : Std.Usize) - 0#usize) =
      ok 2147483648#usize := hconsumed
  have haddMin : ((0#i32 : Std.I32) + (-2147483648)#i32) =
      ok (-2147483648)#i32 := by rfl
  have hlarge : (2147483648#usize : Std.Usize) >
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
    rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
    norm_num
  have hcleanup (a : Array Std.U8 768#usize) :
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := 768#usize,
          «end» := num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        a = ok a := by
    have hlt : core.iter.range.StepUsize.partialOrdInst.lt
        768#usize 19#usize = ok false := by rfl
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
    rw [Aeneas.Std.loop.eq_1]
    simp [num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body,
      core.iter.range.IteratorRange.next,
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def,
      hlt]
  refine ⟨d1.digits, ?_⟩
  unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq
  simp [hdefault, hleading, hscan, hempty, hemptyTest, hdcount, hsUsize,
    hemptyUsize, hconsumed, hzeros, hcastZero, hdpoint, haddZero,
    hsubZero, hcastN, haddMin, hlarge, hcleanup, hdtrunc,
    Aeneas.Std.lift,
    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]

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 15 entries
Technical checking records
Local closure summary
{
  "node": "SlowAccumulatedRoundingDecimal",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "566776cc04d3c3c469d489b160516c23a6428013019ffebfbe15585fc19df38f",
  "active_statement_hash": "015ea407115573d805a2339854cbdcf9d498da7655c7e5a531e763394709151e",
  "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-129",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "ParserNumericDecimalScalarCertificate": "8ad9dcedc946e36d",
    "SlowAccumulatedRoundingCount": "6430f05edfc25124",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "6886acab8f15b99e",
    "num_imp_dec2flt_decimal_seq_count_trailing_zeros": "2ae98c902455cf97",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "39417be39e20ce5d"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "6799fe6525ddb761",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_Insts_CoreDefaultDefault_default": "7b9921904495bc2a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "65634ac95536dcca",
    "num_imp_dec2flt_decimal_seq_count_trailing_zeros": "2df37dbda4b3d3d6",
    "num_imp_dec2flt_decimal_seq_parse_decimal_exponent": "e22eec67a6a49ff8",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "6a45691a499d27c6",
    "num_imp_dec2flt_decimal_seq_parse_digits_into_seq": "8b2cc3a350f40ce1"
  }
}
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:97c16346207bbf2829473405962961309bded47cea3a014a44596f72dd5500fc",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}