P495_T1_FillExact

Lean checked

Statement

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

Lemma · Exact nineteen-cell fill

For every Usize start r and 768-cell U8 array a, fill loop 1 from r to MAX_DIGITS_WITHOUT_OVERFLOW returns normally with a'. It preserves \operatorname{take}(r.\mathit{val},a.\mathit{val}). For every j<768, its cell is U8 zero when r.\mathit{val}\le j<19 and is the original cell otherwise. If all input cell values are below ten, all output cell values are below ten.

Lean statement

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

import Tablet.DecimalSeqDuplicateLoops

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P495_T1_FillExact]
theorem P495_T1_FillExact (r : Std.Usize) (a : Array Std.U8 768#usize) :
    exists out : Array Std.U8 768#usize,
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := r, «end» :=
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        a = ok out /\
      out.val.take r.val = a.val.take r.val /\
      (forall j, j < 768 -> out.val[j]? =
        if r.val <= j /\ j < 19 then some 0#u8 else a.val[j]?) /\
      ((forall b, b ∈ a.val -> b.val < 10) ->
        forall b, b ∈ out.val -> b.val < 10) := 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

The constant is 19 in num imp dec2flt decimal seq DecimalSeq MAX DIGITS WITHOUT OVERFLOW. Use the range iterator and fill body in num imp dec2flt decimal seq parse decimal seq. If r\ge19, the iterator immediately returns None and the input array; the write interval is empty, proving all assertions. Otherwise at iterator start t maintain r\le t\le19, zero cells on [r,t), and the original cells outside [r,t). The rank is 19-t. Initially the interval is empty. For t<19, the range iterator returns index t and advances to t+1. Its checked successor is safe since t+1\le19<2^{64}, using Assumptions. The array index is in bounds since t<19<768. The update writes precisely U8 zero at t, extending the zero interval to [r,t+1) and preserving all other cells. The rank decreases by one. At t=19 the iterator returns None, so well-founded induction gives the normal result and exact cell equation. Since no changed index is below r, the take-r equality follows by list extensionality. Each output cell is either an unchanged input cell or zero, proving the digit bound. The copied loops 4, 6, and 7 can use this same statement through the extensional equalities in DecimalSeqDuplicateLoops.

Lean proof


  let Inv := fun p : core.ops.range.Range Std.Usize ×
      Array Std.U8 768#usize =>
    (19 <= r.val /\ p.1.start.val = r.val /\ p.1.end.val = 19 /\
      p.2 = a) \/
    (r.val < 19 /\ r.val <= p.1.start.val /\ p.1.start.val <= 19 /\
      p.1.end.val = 19 /\
      p.2.val.take r.val = a.val.take r.val /\
      forall j, j < 768 -> p.2.val[j]? =
        if r.val <= j /\ j < p.1.start.val then some 0#u8 else a.val[j]?)
  let Post := fun out : Array Std.U8 768#usize =>
    out.val.take r.val = a.val.take r.val /\
    (forall j, j < 768 -> out.val[j]? =
      if r.val <= j /\ j < 19 then some 0#u8 else a.val[j]?) /\
    ((forall b, b ∈ a.val -> b.val < 10) ->
      forall b, b ∈ out.val -> b.val < 10)
  have hloop : Aeneas.Std.WP.spec
      (num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := r, «end» :=
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        a) Post := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : core.ops.range.Range Std.Usize × Array Std.U8 768#usize =>
        p.1.end.val - p.1.start.val)
      Inv Post
      (fun p => num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body
        p.1 p.2)
      ({ start := r, «end» :=
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }, a)
    · intro p hp
      rcases p with ⟨iter, arr⟩
      simp only [Inv] at hp
      unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body
      unfold core.iter.range.IteratorRange.next
      rcases hp with hlarge | hsmall
      · rcases hlarge with ⟨hr, hstart, hend, harr⟩
        have hnot : ¬ iter.start.val < iter.end.val := by omega
        simp [core.iter.range.StepUsize, core.iter.range.UScalarStep,
          core.cmp.PartialOrdUsize, core.cmp.impls.PartialOrdUsize.lt,
          liftFun2, hnot, WP.spec_ok, Post]
        refine ⟨?_, ?_, ?_⟩
        · simpa [harr]
        · intro j hj
          have hfalse : ¬ (r.val <= j /\ j < 19) := by omega
          simpa [hfalse, harr]
        · intro hdigits b hb
          have hb' : b ∈ a.val := by simpa [harr] using hb
          exact hdigits b hb'
      · rcases hsmall with ⟨hr, hrt, hte, hend, hprefix, hcells⟩
        simp [core.iter.range.UScalarStep, core.cmp.PartialOrdUsize,
          core.cmp.impls.PartialOrdUsize.lt, liftFun2]
        split
        · rename_i hlt
          have hmax : iter.start.val + 1 <= UScalar.max .Usize := by
            have hlt19 : iter.start.val < 19 := by omega
            calc
              iter.start.val + 1 <= 19 := by omega
              _ <= UScalar.max .Usize := by
                norm_num [UScalar.max, Usize.max, Usize.numBits,
                  Dec2Flt.P340_T1_selected_target_numBits]
          simp [core.iter.range.UScalarStep.forward_checked, hmax]
          have hidx : iter.start.val < 768 := by omega
          obtain ⟨a1, ha1, ha1post⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.update_spec arr iter.start 0#u8 (by
              simpa [Array.length] using hidx))
          rw [ha1]
          simp [WP.spec_ok]
          refine ⟨Or.inr ⟨hr, ?_, ?_, hend, ?_, ?_⟩, ?_⟩
          · change r.val <= iter.start.val + 1
            omega
          · change iter.start.val + 1 <= 19
            omega
          · rw [ha1post]
            simp [Array.set, List.take_set_of_le hrt, hprefix]
          · intro j hj
            rw [ha1post]
            by_cases hji : j = iter.start.val
            · subst j
              simp [Array.set, List.getElem?_set, hidx, hrt, hlt]
            · simp [Array.set, List.getElem?_set, hji]
              by_cases hbefore : j < iter.start.val
              · have hjle : j <= iter.start.val := Nat.le_of_lt hbefore
                simp [hcells j hj, hbefore, hjle]
              · have hafter : iter.start.val < j := by omega
                simp [hcells j hj, hbefore, hafter]
          · change iter.end.val - (iter.start.val + 1) <
              iter.end.val - iter.start.val
            omega
        · simp [WP.spec_ok, Post]
          refine ⟨?_, ?_, ?_⟩
          · exact hprefix
          · intro j hj
            have heq : iter.start.val = 19 := by omega
            simpa [heq] using (hcells j hj)
          · intro hdigits b hb
            rcases List.mem_iff_getElem.mp hb with ⟨j, hj, rfl⟩
            have hj768 : j < 768 := by simpa using hj
            have hget := hcells j hj768
            have hjArr : j < arr.val.length := by simpa using hj768
            have hjA : j < a.val.length := by simpa using hj768
            rw [List.getElem?_eq_getElem hjArr] at hget
            have heq : iter.start.val = 19 := by omega
            by_cases hz : r.val <= j /\ j < 19
            · have hzero : arr.val[j] = 0#u8 := by
                have hopt : some arr.val[j] = some 0#u8 := by
                  simpa [hz, heq] using hget
                exact Option.some.inj hopt
              simp [hzero]
            · have hval : arr.val[j] = a.val[j] := by
                rw [List.getElem?_eq_getElem hjA] at hget
                have hopt : some arr.val[j] = some a.val[j] := by
                  simpa [hz, heq] using hget
                exact Option.some.inj hopt
              rw [hval]
              exact hdigits _ (List.getElem_mem _)
    · simp [Inv,
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def]
      omega
  obtain ⟨out, hout, hpost⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  exact ⟨out, hout, hpost⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqDuplicateLoops

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P495_T1_FillExact]
theorem P495_T1_FillExact (r : Std.Usize) (a : Array Std.U8 768#usize) :
    exists out : Array Std.U8 768#usize,
      num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := r, «end» :=
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        a = ok out /\
      out.val.take r.val = a.val.take r.val /\
      (forall j, j < 768 -> out.val[j]? =
        if r.val <= j /\ j < 19 then some 0#u8 else a.val[j]?) /\
      ((forall b, b ∈ a.val -> b.val < 10) ->
        forall b, b ∈ out.val -> b.val < 10) := by
-- BODY
  let Inv := fun p : core.ops.range.Range Std.Usize ×
      Array Std.U8 768#usize =>
    (19 <= r.val /\ p.1.start.val = r.val /\ p.1.end.val = 19 /\
      p.2 = a) \/
    (r.val < 19 /\ r.val <= p.1.start.val /\ p.1.start.val <= 19 /\
      p.1.end.val = 19 /\
      p.2.val.take r.val = a.val.take r.val /\
      forall j, j < 768 -> p.2.val[j]? =
        if r.val <= j /\ j < p.1.start.val then some 0#u8 else a.val[j]?)
  let Post := fun out : Array Std.U8 768#usize =>
    out.val.take r.val = a.val.take r.val /\
    (forall j, j < 768 -> out.val[j]? =
      if r.val <= j /\ j < 19 then some 0#u8 else a.val[j]?) /\
    ((forall b, b ∈ a.val -> b.val < 10) ->
      forall b, b ∈ out.val -> b.val < 10)
  have hloop : Aeneas.Std.WP.spec
      (num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
        { start := r, «end» :=
          num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }
        a) Post := by
    unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1
    apply Aeneas.Std.loop.spec_decr_nat
      (fun p : core.ops.range.Range Std.Usize × Array Std.U8 768#usize =>
        p.1.end.val - p.1.start.val)
      Inv Post
      (fun p => num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body
        p.1 p.2)
      ({ start := r, «end» :=
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW }, a)
    · intro p hp
      rcases p with ⟨iter, arr⟩
      simp only [Inv] at hp
      unfold num.imp.dec2flt.decimal_seq.parse_decimal_seq_loop1.body
      unfold core.iter.range.IteratorRange.next
      rcases hp with hlarge | hsmall
      · rcases hlarge with ⟨hr, hstart, hend, harr⟩
        have hnot : ¬ iter.start.val < iter.end.val := by omega
        simp [core.iter.range.StepUsize, core.iter.range.UScalarStep,
          core.cmp.PartialOrdUsize, core.cmp.impls.PartialOrdUsize.lt,
          liftFun2, hnot, WP.spec_ok, Post]
        refine ⟨?_, ?_, ?_⟩
        · simpa [harr]
        · intro j hj
          have hfalse : ¬ (r.val <= j /\ j < 19) := by omega
          simpa [hfalse, harr]
        · intro hdigits b hb
          have hb' : b ∈ a.val := by simpa [harr] using hb
          exact hdigits b hb'
      · rcases hsmall with ⟨hr, hrt, hte, hend, hprefix, hcells⟩
        simp [core.iter.range.UScalarStep, core.cmp.PartialOrdUsize,
          core.cmp.impls.PartialOrdUsize.lt, liftFun2]
        split
        · rename_i hlt
          have hmax : iter.start.val + 1 <= UScalar.max .Usize := by
            have hlt19 : iter.start.val < 19 := by omega
            calc
              iter.start.val + 1 <= 19 := by omega
              _ <= UScalar.max .Usize := by
                norm_num [UScalar.max, Usize.max, Usize.numBits,
                  Dec2Flt.P340_T1_selected_target_numBits]
          simp [core.iter.range.UScalarStep.forward_checked, hmax]
          have hidx : iter.start.val < 768 := by omega
          obtain ⟨a1, ha1, ha1post⟩ := Aeneas.Std.WP.spec_imp_exists
            (Array.update_spec arr iter.start 0#u8 (by
              simpa [Array.length] using hidx))
          rw [ha1]
          simp [WP.spec_ok]
          refine ⟨Or.inr ⟨hr, ?_, ?_, hend, ?_, ?_⟩, ?_⟩
          · change r.val <= iter.start.val + 1
            omega
          · change iter.start.val + 1 <= 19
            omega
          · rw [ha1post]
            simp [Array.set, List.take_set_of_le hrt, hprefix]
          · intro j hj
            rw [ha1post]
            by_cases hji : j = iter.start.val
            · subst j
              simp [Array.set, List.getElem?_set, hidx, hrt, hlt]
            · simp [Array.set, List.getElem?_set, hji]
              by_cases hbefore : j < iter.start.val
              · have hjle : j <= iter.start.val := Nat.le_of_lt hbefore
                simp [hcells j hj, hbefore, hjle]
              · have hafter : iter.start.val < j := by omega
                simp [hcells j hj, hbefore, hafter]
          · change iter.end.val - (iter.start.val + 1) <
              iter.end.val - iter.start.val
            omega
        · simp [WP.spec_ok, Post]
          refine ⟨?_, ?_, ?_⟩
          · exact hprefix
          · intro j hj
            have heq : iter.start.val = 19 := by omega
            simpa [heq] using (hcells j hj)
          · intro hdigits b hb
            rcases List.mem_iff_getElem.mp hb with ⟨j, hj, rfl⟩
            have hj768 : j < 768 := by simpa using hj
            have hget := hcells j hj768
            have hjArr : j < arr.val.length := by simpa using hj768
            have hjA : j < a.val.length := by simpa using hj768
            rw [List.getElem?_eq_getElem hjArr] at hget
            have heq : iter.start.val = 19 := by omega
            by_cases hz : r.val <= j /\ j < 19
            · have hzero : arr.val[j] = 0#u8 := by
                have hopt : some arr.val[j] = some 0#u8 := by
                  simpa [hz, heq] using hget
                exact Option.some.inj hopt
              simp [hzero]
            · have hval : arr.val[j] = a.val[j] := by
                rw [List.getElem?_eq_getElem hjA] at hget
                have hopt : some arr.val[j] = some a.val[j] := by
                  simpa [hz, heq] using hget
                exact Option.some.inj hopt
              rw [hval]
              exact hdigits _ (List.getElem_mem _)
    · simp [Inv,
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.eq_def]
      omega
  obtain ⟨out, hout, hpost⟩ := Aeneas.Std.WP.spec_imp_exists hloop
  exact ⟨out, hout, hpost⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (79)
More program dependencies (46)
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 44 entries
Technical checking records
Local closure summary
{
  "node": "P495_T1_FillExact",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "b71498b73f02f946912efce0cc679106694a88c2ddbb30be151ada1c9d89831e",
  "active_statement_hash": "9d0f249a91fa59201b788b411becce5f1956c090733289ed17a3bb1ed250894b",
  "kernel_axioms": [
    "Classical.choice",
    "Dec2Flt.P340_T1_selected_target_numBits",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "6886acab8f15b99e",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "cb99c951cfc3a86f"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS_WITHOUT_OVERFLOW": "65634ac95536dcca",
    "num_imp_dec2flt_decimal_seq_parse_decimal_seq": "4a6288a8320b70c5"
  }
}
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:67c52bd5843ec4efc67a6316542992dff3b21c795354321bb461c43d39078816",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}