DecimalSeqExponentLoopExact

Lean checked

Statement

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

Lemma · Exact decimal-sequence exponent loop

Let s be a source-valid byte slice whose byte-value list renders a list D of decimal digits, every member of D being below ten. Define
G(a,r)=\begin{cases}10a+r,&a<65536,\\a,&a\ge65536.\end{cases}
Then parse_decimal_exponent(s) returns normally with an I32 value whose mathematical value is \operatorname{foldl}(G,0,D).

Lean statement

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

import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_exponent

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqExponentLoopExact]
theorem DecimalSeqExponentLoopExact :
    forall (s : Slice Std.U8) (digits : List Nat),
      SourceValidByteSlice s ->
      DecimalGoalVocabularyDefault.bytes s =
        digits.map (fun digit => 48 + digit) ->
      (forall digit, digit ∈ digits -> digit < 10) ->
      exists e : Std.I32,
        num.imp.dec2flt.decimal_seq.parse_decimal_exponent s = ok e /\
        e.val = (digits.foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) := 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

Map equality and the digit bound imply that the jth byte of s, for every j<|D|, is exactly 48+D[j] and lies in 48,\ldots,57. We prove by induction on j the invariant for the loop in num imp dec2flt decimal seq parse decimal exponent: before its jth test, the cursor is the Usize value j and the I32 accumulator has value the fold of G over take j D.
The invariant holds initially because both cursor and accumulator are zero. If j<|D|, source-validity SourceValidByteSlice gives j<|D|\le2^{63}-1<2^{64}, so the slice read at j is in bounds and the checked cursor increment j+1 cannot overflow Usize. The byte is an ASCII digit, hence the digit test is true, wrapping subtraction by 48 is ordinary subtraction, and the U8-to-I32 cast returns exactly D[j]\in[0,9].
If the old accumulator is at least 65536 the extracted branch retains it, which is G(a,D[j]) by definition. Otherwise 0\le a\le65535; therefore
0\le 10a+D[j]\le10\cdot65535+9=655359<2^{31}.
Thus the checked I32 multiplication by ten and addition of the digit both return normally and yield exactly G(a,D[j]). This proves the invariant after the continuation. If j=|D|, the bounds test is false and the loop returns the accumulator. Induction therefore reaches the normal return at j=|D|, and the invariant there is the full fold asserted in the statement. The reducible wrapper merely invokes that loop with the two zero initial values, so choosing its returned I32 value proves the existential equation.

Lean proof


  set_option maxHeartbeats 800000 in
  intro s digits hsource hbytes hdigits
  change s.val.map (fun b : Std.U8 => b.val) =
    digits.map (fun digit => 48 + digit) at hbytes
  have hlen : s.val.length = digits.length := by
    have hm := congrArg List.length hbytes
    simpa using hm
  have hbyte : forall j (hj : j < digits.length),
      (s.val[j]'(by rw [hlen]; exact hj)).val = 48 + digits[j]'hj := by
    intro j hj
    have hm := congrArg (fun xs : List Nat => xs[j]?) hbytes
    simpa [List.getElem?_eq_getElem, hj, hlen] using hm
  have hloop :
      num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop s 0#i32 0#usize
        ⦃ p => p.val = (digits.foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) ⦄ := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.I32 × Std.Usize => digits.length - p.2.val)
      (fun p => p.2.val <= digits.length /\
        p.1.val = (((digits.take p.2.val).foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) : Int))
      (fun p => p.val = ((digits.foldl
        (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) : Int))
      (fun p => num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop.body
        s p.1 p.2)
      (0#i32, 0#usize)
    · intro p hp
      rcases p with ⟨acc, count⟩
      rcases hp with ⟨hcount, hacc⟩
      change count.val <= digits.length at hcount
      change acc.val = (((digits.take count.val).foldl
        (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) : Int) at hacc
      simp [num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop.body,
        WP.spec_ok, Slice.length]
      split
      · rename_i hins
        step
        have hi := hbyte count.val (by omega)
        have hiVal : i1.val = 48 + digits[count.val]'(by omega) := by
          rw [i1_post]
          exact hi
        step
        have hi2Val : i2.val = digits[count.val]'(by omega) := by
          rw [i2_post, core.num.U8.wrapping_sub_val_eq]
          simp only [UScalar.size_UScalarTyU8, U8.size_eq]
          norm_num
          rw [hiVal]
          have hd := hdigits (digits[count.val]'(by omega))
            (List.getElem_mem _)
          rw [show 48 + digits[count.val] + 208 =
              digits[count.val] + 256 by omega]
          simp [Nat.add_mod, Nat.mod_eq_of_lt (by omega :
            digits[count.val] < 256)]
        have hi2Small : i2.val < 10 := by
          rw [hi2Val]
          exact hdigits _ (List.getElem_mem _)
        simp [hi2Small]
        step
        have hdigitVal : digit.val = digits[count.val]'(by omega) := by
          have heq : digit = i2 := by rw [digit_post, i2_post]
          rw [heq, hi2Val]
        have hcountLt : count.val < digits.length := by
          rw [← hlen]
          exact hins
        by_cases hsmall : acc.val < 65536
        · simp only [if_pos hsmall]
          step
          step
          have hxcast : (UScalar.hcast .I32 digit).val = digit.val := by
            have hh := UScalar.hcast_inBounds_spec .I32 digit (by scalar_tac)
            simpa [WP.spec_ok] using hh
          have hmax : (10 * acc.val + digit.val) <= I32.max := by
            simp [I32.max_eq]
            rw [hdigitVal]
            have hd := hdigits (digits[count.val]'(by omega))
              (List.getElem_mem _)
            omega
          step with IScalar.add_spec
          step
          have hcountMax : count.val + 1 <= UScalar.max .Usize := by
            have hsprop := s.property
            have hbits := Dec2Flt.configured_x86_64_numBits
            simp [UScalar.max, Usize.max, Usize.numBits, hbits] at hsprop ⊢
            omega
          have hcount1 : count1.val = count.val + 1 := by
            simpa using count1_post
          have hnext : exp_num1.val = 10 * acc.val + digit.val := by
            rw [exp_num1_post, x_post, hxcast]
            omega
          have hcountLt : count.val < digits.length := by
            rw [← hlen]
            exact hins
          constructor
          · rw [hcount1]
            omega
          · constructor
            · rw [hcount1, List.take_succ_eq_append_getElem hcountLt,
                List.foldl_append, List.foldl_cons, List.foldl_nil]
              have hfoldsmall :
                  (digits.take count.val).foldl
                    (fun n digit => if n < 65536 then 10 * n + digit else n) 0 <
                    65536 := by
                rw [hacc] at hsmall
                exact_mod_cast hsmall
              simp only [if_pos hfoldsmall]
              rw [hnext, hacc, hdigitVal]
              norm_num
            · rw [hcount1]
              omega
        · simp only [if_neg hsmall]
          have hcountMax : count.val + 1 <= UScalar.max .Usize := by
            have hsprop := s.property
            have hbits := Dec2Flt.configured_x86_64_numBits
            simp [UScalar.max, Usize.max, Usize.numBits, hbits] at hsprop ⊢
            omega
          obtain ⟨count1, hcount1, hcount1Val⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (UScalar.add_spec (x := count) (y := 1#usize) hcountMax)
          rw [hcount1]
          norm_num at hcount1Val
          simp [WP.spec_ok]
          constructor
          · change count1.val <= digits.length
            rw [hcount1Val]
            omega
          · constructor
            · change acc.val = _
              rw [hacc]
              have hfoldge : Not
                  ((digits.take count.val).foldl
                    (fun n digit => if n < 65536 then 10 * n + digit else n) 0 <
                    65536) := by
                intro hh
                apply hsmall
                rw [hacc]
                exact_mod_cast hh
              rw [hcount1Val, List.take_succ_eq_append_getElem hcountLt,
                List.foldl_append, List.foldl_cons, List.foldl_nil]
              simp [hfoldge]
            · change digits.length - count1.val < digits.length - count.val
              rw [hcount1Val]
              omega
      · simp [WP.spec_ok]
        have hcountEq : count.val = digits.length := by
          rw [← hlen]
          omega
        rw [hacc, (List.take_eq_self_iff digits).2 (by omega)]
    · simp
  obtain ⟨e, he⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  refine ⟨e, ?_, he.2⟩
  unfold num.imp.dec2flt.decimal_seq.parse_decimal_exponent
  exact he.1
end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.SourceValidByteSlice
import Tablet.num_imp_dec2flt_decimal_seq_parse_decimal_exponent

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqExponentLoopExact]
theorem DecimalSeqExponentLoopExact :
    forall (s : Slice Std.U8) (digits : List Nat),
      SourceValidByteSlice s ->
      DecimalGoalVocabularyDefault.bytes s =
        digits.map (fun digit => 48 + digit) ->
      (forall digit, digit ∈ digits -> digit < 10) ->
      exists e : Std.I32,
        num.imp.dec2flt.decimal_seq.parse_decimal_exponent s = ok e /\
        e.val = (digits.foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) := by
-- BODY
  set_option maxHeartbeats 800000 in
  intro s digits hsource hbytes hdigits
  change s.val.map (fun b : Std.U8 => b.val) =
    digits.map (fun digit => 48 + digit) at hbytes
  have hlen : s.val.length = digits.length := by
    have hm := congrArg List.length hbytes
    simpa using hm
  have hbyte : forall j (hj : j < digits.length),
      (s.val[j]'(by rw [hlen]; exact hj)).val = 48 + digits[j]'hj := by
    intro j hj
    have hm := congrArg (fun xs : List Nat => xs[j]?) hbytes
    simpa [List.getElem?_eq_getElem, hj, hlen] using hm
  have hloop :
      num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop s 0#i32 0#usize
        ⦃ p => p.val = (digits.foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) ⦄ := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : Std.I32 × Std.Usize => digits.length - p.2.val)
      (fun p => p.2.val <= digits.length /\
        p.1.val = (((digits.take p.2.val).foldl
          (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) : Int))
      (fun p => p.val = ((digits.foldl
        (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) : Int))
      (fun p => num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop.body
        s p.1 p.2)
      (0#i32, 0#usize)
    · intro p hp
      rcases p with ⟨acc, count⟩
      rcases hp with ⟨hcount, hacc⟩
      change count.val <= digits.length at hcount
      change acc.val = (((digits.take count.val).foldl
        (fun n digit => if n < 65536 then 10 * n + digit else n) 0 : Nat) : Int) at hacc
      simp [num.imp.dec2flt.decimal_seq.parse_decimal_exponent_loop.body,
        WP.spec_ok, Slice.length]
      split
      · rename_i hins
        step
        have hi := hbyte count.val (by omega)
        have hiVal : i1.val = 48 + digits[count.val]'(by omega) := by
          rw [i1_post]
          exact hi
        step
        have hi2Val : i2.val = digits[count.val]'(by omega) := by
          rw [i2_post, core.num.U8.wrapping_sub_val_eq]
          simp only [UScalar.size_UScalarTyU8, U8.size_eq]
          norm_num
          rw [hiVal]
          have hd := hdigits (digits[count.val]'(by omega))
            (List.getElem_mem _)
          rw [show 48 + digits[count.val] + 208 =
              digits[count.val] + 256 by omega]
          simp [Nat.add_mod, Nat.mod_eq_of_lt (by omega :
            digits[count.val] < 256)]
        have hi2Small : i2.val < 10 := by
          rw [hi2Val]
          exact hdigits _ (List.getElem_mem _)
        simp [hi2Small]
        step
        have hdigitVal : digit.val = digits[count.val]'(by omega) := by
          have heq : digit = i2 := by rw [digit_post, i2_post]
          rw [heq, hi2Val]
        have hcountLt : count.val < digits.length := by
          rw [← hlen]
          exact hins
        by_cases hsmall : acc.val < 65536
        · simp only [if_pos hsmall]
          step
          step
          have hxcast : (UScalar.hcast .I32 digit).val = digit.val := by
            have hh := UScalar.hcast_inBounds_spec .I32 digit (by scalar_tac)
            simpa [WP.spec_ok] using hh
          have hmax : (10 * acc.val + digit.val) <= I32.max := by
            simp [I32.max_eq]
            rw [hdigitVal]
            have hd := hdigits (digits[count.val]'(by omega))
              (List.getElem_mem _)
            omega
          step with IScalar.add_spec
          step
          have hcountMax : count.val + 1 <= UScalar.max .Usize := by
            have hsprop := s.property
            have hbits := Dec2Flt.configured_x86_64_numBits
            simp [UScalar.max, Usize.max, Usize.numBits, hbits] at hsprop ⊢
            omega
          have hcount1 : count1.val = count.val + 1 := by
            simpa using count1_post
          have hnext : exp_num1.val = 10 * acc.val + digit.val := by
            rw [exp_num1_post, x_post, hxcast]
            omega
          have hcountLt : count.val < digits.length := by
            rw [← hlen]
            exact hins
          constructor
          · rw [hcount1]
            omega
          · constructor
            · rw [hcount1, List.take_succ_eq_append_getElem hcountLt,
                List.foldl_append, List.foldl_cons, List.foldl_nil]
              have hfoldsmall :
                  (digits.take count.val).foldl
                    (fun n digit => if n < 65536 then 10 * n + digit else n) 0 <
                    65536 := by
                rw [hacc] at hsmall
                exact_mod_cast hsmall
              simp only [if_pos hfoldsmall]
              rw [hnext, hacc, hdigitVal]
              norm_num
            · rw [hcount1]
              omega
        · simp only [if_neg hsmall]
          have hcountMax : count.val + 1 <= UScalar.max .Usize := by
            have hsprop := s.property
            have hbits := Dec2Flt.configured_x86_64_numBits
            simp [UScalar.max, Usize.max, Usize.numBits, hbits] at hsprop ⊢
            omega
          obtain ⟨count1, hcount1, hcount1Val⟩ :=
            Aeneas.Std.WP.spec_imp_exists
              (UScalar.add_spec (x := count) (y := 1#usize) hcountMax)
          rw [hcount1]
          norm_num at hcount1Val
          simp [WP.spec_ok]
          constructor
          · change count1.val <= digits.length
            rw [hcount1Val]
            omega
          · constructor
            · change acc.val = _
              rw [hacc]
              have hfoldge : Not
                  ((digits.take count.val).foldl
                    (fun n digit => if n < 65536 then 10 * n + digit else n) 0 <
                    65536) := by
                intro hh
                apply hsmall
                rw [hacc]
                exact_mod_cast hh
              rw [hcount1Val, List.take_succ_eq_append_getElem hcountLt,
                List.foldl_append, List.foldl_cons, List.foldl_nil]
              simp [hfoldge]
            · change digits.length - count1.val < digits.length - count.val
              rw [hcount1Val]
              omega
      · simp [WP.spec_ok]
        have hcountEq : count.val = digits.length := by
          rw [← hlen]
          omega
        rw [hacc, (List.take_eq_self_iff digits).2 (by omega)]
    · simp
  obtain ⟨e, he⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  refine ⟨e, ?_, he.2⟩
  unfold num.imp.dec2flt.decimal_seq.parse_decimal_exponent
  exact he.1
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
All 9 entries
Technical checking records
Local closure summary
{
  "node": "DecimalSeqExponentLoopExact",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "c72deb7bb21f4f058c2e0b952a25f3c1d6ad84c04f2c78918762498652d35ce3",
  "active_statement_hash": "ae89ed0f3ad04fbc10b802b73cc7e4465434226ba5c25c920e040e9c0ab6f67b",
  "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": "revalidate-cycle-163",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "num_imp_dec2flt_decimal_seq_parse_decimal_exponent": "faf0081b4c91301b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "65227461bf3cac06",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "SourceValidByteSlice": "3dc42716a6378d26",
    "num_imp_dec2flt_decimal_seq_parse_decimal_exponent": "498406f251d28850"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:daa8a632b7ca9e58947455017bcd855a6ccbebf64f068dab773e999b1bf139ed",
  "current_fingerprint": "sound-v2:daa8a632b7ca9e58947455017bcd855a6ccbebf64f068dab773e999b1bf139ed",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}