DecimalSeqPrimitiveSemantics

Lean checked

Statement

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

Helper · Decimal-sequence default, capacities, and scalar append

The extracted default constructor returns a decimal sequence d_0 with digit count and decimal point zero, truncation flag false, and every cell of its 768-cell array equal to zero. The extracted constants MAX_DIGITS and MAX_DIGITS_WITHOUT_OVERFLOW have unsigned values 768 and 19.
Moreover, whenever try_add_digit(d, c) returns d', its digit count is the old count plus one, its decimal point and truncation flag are unchanged, and its array is as follows. If the old count is below 768, the array is the old array with exactly that indexed cell replaced by c; otherwise the entire array is unchanged. These are exactly the existential, constant equalities, and universally quantified implication in Lean.

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_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_Insts_CoreDefaultDefault_default
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_try_add_digit

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqPrimitiveSemantics]
theorem DecimalSeqPrimitiveSemantics :
    (exists d0 : num.imp.dec2flt.decimal_seq.DecimalSeq,
      num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default =
        ok d0 /\
      d0.num_digits.val = 0 /\ d0.decimal_point.val = 0 /\
      d0.truncated = false /\
      (forall z, z ∈ d0.digits.val -> z = 0#u8)) /\
    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.val = 768 /\
    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.val =
      19 /\
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (digit : Std.U8),
      num.imp.dec2flt.decimal_seq.DecimalSeq.try_add_digit d digit = ok d' ->
      d'.num_digits.val = d.num_digits.val + 1 /\
      d'.decimal_point = d.decimal_point /\
      d'.truncated = d.truncated /\
      (if d.num_digits.val < 768 then
        d'.digits.val = d.digits.val.set d.num_digits.val digit
       else d'.digits = d.digits) := 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 num imp dec2flt decimal seq DecimalSeq Insts CoreDefaultDefault default. Its first operation is Array.repeat 768 0. The Aeneas array semantics from the Preamble creates an array whose value list has length 768 and whose every member is the supplied byte zero. The following record literal assigns count 0, point 0, flag false, and that array, and the final ok has no failure branch. Choosing this record as d_0 proves the first five displayed fields.
Unfolding num imp dec2flt decimal seq DecimalSeq MAX DIGITS and num imp dec2flt decimal seq DecimalSeq MAX DIGITS WITHOUT OVERFLOW gives the literal machine values 768\#\mathrm{usize} and 19\#\mathrm{usize}. Both are within the 64-bit unsigned range, so their val observations are exactly 768 and 19.
Finally fix d,c,d' and assume the successful append equation. Unfold num imp dec2flt decimal seq DecimalSeq try add digit and replace MAX_DIGITS by 768 using the preceding equality. If the old count is below 768, the selected array index is strictly below the array's fixed length. The Aeneas Array.update rule therefore succeeds, changes exactly that list index to c, and leaves every other cell unchanged. If the old count is at least 768, the other branch returns the old array unchanged. In both branches the next checked operation adds one to the old usize count. Since the whole call was assumed to return normally, this addition cannot have overflowed; its observed value is consequently the old observed value plus one. The concluding record update changes only num_digits and digits. Its point and flag are therefore definitionally those of d, and the two array alternatives are exactly the conditional equality in the statement.

Lean proof


  constructor
  · 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 }
    refine ⟨d0, ?_⟩
    constructor
    · rfl
    simp only [d0, Array.repeat_val, List.mem_replicate, true_and]
    norm_num
  constructor
  · rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
    rfl
  constructor
  · rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def]
    rfl
  · intro d d' digit hresult
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.try_add_digit at hresult
    by_cases hstore : d.num_digits.val < 768
    · have hword : d.num_digits <
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
        simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
          UScalar.lt_equiv] using hstore
      simp [hword] at hresult
      obtain ⟨a, ha, haVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (Array.update_spec d.digits d.num_digits digit (by
          simpa [Array.length] using hstore))
      rw [ha] at hresult
      simp only [bind_tc_ok] at hresult
      have hadd : d.num_digits.val + 1 <= UScalar.max .Usize := by
        simp [UScalar.max, Usize.numBits,
          Dec2Flt.configured_x86_64_numBits]
        omega
      obtain ⟨next, hnext, hnextVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (UScalar.add_spec (x := d.num_digits) (y := 1#usize)
            (by simpa using hadd))
      rw [hnext] at hresult
      simp only [bind_tc_ok] at hresult
      cases hresult
      constructor
      · change next.val = d.num_digits.val + 1 at hnextVal
        exact hnextVal
      constructor
      · rfl
      constructor
      · rfl
      · simpa [hstore] using congrArg (fun q => q.val) haVal
    · have hword : ¬ d.num_digits <
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
        simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
          UScalar.lt_equiv] using hstore
      simp [hword] at hresult
      cases hcalc : d.num_digits + 1#usize with
      | ok next =>
        have hsum := UScalar.add_equiv d.num_digits (1#usize)
        simp [hcalc] at hsum
        rw [hcalc] at hresult
        simp only [bind_tc_ok] at hresult
        cases hresult
        constructor
        · exact hsum.2.1
        constructor
        · rfl
        constructor
        · rfl
        · simp [hstore]
      | fail err =>
        rw [hcalc] at hresult
        simp at hresult
      | div =>
        rw [hcalc] at hresult
        simp at hresult

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_Insts_CoreDefaultDefault_default
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_try_add_digit

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqPrimitiveSemantics]
theorem DecimalSeqPrimitiveSemantics :
    (exists d0 : num.imp.dec2flt.decimal_seq.DecimalSeq,
      num.imp.dec2flt.decimal_seq.DecimalSeq.Insts.CoreDefaultDefault.default =
        ok d0 /\
      d0.num_digits.val = 0 /\ d0.decimal_point.val = 0 /\
      d0.truncated = false /\
      (forall z, z ∈ d0.digits.val -> z = 0#u8)) /\
    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.val = 768 /\
    num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.val =
      19 /\
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq) (digit : Std.U8),
      num.imp.dec2flt.decimal_seq.DecimalSeq.try_add_digit d digit = ok d' ->
      d'.num_digits.val = d.num_digits.val + 1 /\
      d'.decimal_point = d.decimal_point /\
      d'.truncated = d.truncated /\
      (if d.num_digits.val < 768 then
        d'.digits.val = d.digits.val.set d.num_digits.val digit
       else d'.digits = d.digits) := by
-- BODY
  constructor
  · 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 }
    refine ⟨d0, ?_⟩
    constructor
    · rfl
    simp only [d0, Array.repeat_val, List.mem_replicate, true_and]
    norm_num
  constructor
  · rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def]
    rfl
  constructor
  · rw [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def]
    rfl
  · intro d d' digit hresult
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.try_add_digit at hresult
    by_cases hstore : d.num_digits.val < 768
    · have hword : d.num_digits <
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
        simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
          UScalar.lt_equiv] using hstore
      simp [hword] at hresult
      obtain ⟨a, ha, haVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (Array.update_spec d.digits d.num_digits digit (by
          simpa [Array.length] using hstore))
      rw [ha] at hresult
      simp only [bind_tc_ok] at hresult
      have hadd : d.num_digits.val + 1 <= UScalar.max .Usize := by
        simp [UScalar.max, Usize.numBits,
          Dec2Flt.configured_x86_64_numBits]
        omega
      obtain ⟨next, hnext, hnextVal⟩ :=
        Aeneas.Std.WP.spec_imp_exists
          (UScalar.add_spec (x := d.num_digits) (y := 1#usize)
            (by simpa using hadd))
      rw [hnext] at hresult
      simp only [bind_tc_ok] at hresult
      cases hresult
      constructor
      · change next.val = d.num_digits.val + 1 at hnextVal
        exact hnextVal
      constructor
      · rfl
      constructor
      · rfl
      · simpa [hstore] using congrArg (fun q => q.val) haVal
    · have hword : ¬ d.num_digits <
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS := by
        simpa [num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS.eq_def,
          UScalar.lt_equiv] using hstore
      simp [hword] at hresult
      cases hcalc : d.num_digits + 1#usize with
      | ok next =>
        have hsum := UScalar.add_equiv d.num_digits (1#usize)
        simp [hcalc] at hsum
        rw [hcalc] at hresult
        simp only [bind_tc_ok] at hresult
        cases hresult
        constructor
        · exact hsum.2.1
        constructor
        · rfl
        constructor
        · rfl
        · simp [hstore]
      | fail err =>
        rw [hcalc] at hresult
        simp at hresult
      | div =>
        rw [hcalc] at hresult
        simp at hresult

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (48)
More program dependencies (15)
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": "DecimalSeqPrimitiveSemantics",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "df1f8ccd73b5fad38e1eea82223afc745e09cd3f1cfb90554b8e92ee576c4587",
  "active_statement_hash": "6e0f6d2e96d8c7c0fa73f6ff2e9faab03480eac004066830fd3cd5fa6e00da1c",
  "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_DecimalSeq_MAX_DIGITS": "0eb6fb2627d0ff5c",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "6886acab8f15b99e",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_try_add_digit": "0eb6fb2627d0ff5c"
  },
  "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_DecimalSeq_try_add_digit": "69a4869a11307175"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:f272d26f4c2b7a9c3aeaf009f2be88670dd8c55193b124d057b90b28ac1a40a9",
  "current_fingerprint": "sound-v2:f272d26f4c2b7a9c3aeaf009f2be88670dd8c55193b124d057b90b28ac1a40a9",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}