P495_T1_RightPrefixLengthBounds

Lean checked

Statement

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

Lemma · Decimal length of the executed prefix accumulator

Let d have a retained digit list of length n=d.\mathit{num\_digits.val}, with n\le768, decimal entries, and a positive head. For k\le60, if its actual right-shift prefix loop returns normally with cursor r and accumulator c, then 10^{r.\mathit{val}-1}\le c.\mathit{val}<10^{r.\mathit{val}}.

Lean statement

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

import Tablet.P472_T1_DecimalRightShiftPrefixLoopSemantics
import Tablet.P804_T1_DecimalFoldBounds

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

-- [TABLET NODE: P495_T1_RightPrefixLengthBounds]
theorem P495_T1_RightPrefixLengthBounds
    (d d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift read : Std.Usize) (acc : Std.U64) (allZero : Bool)
    (hshift : shift.val <= 60) (hcap : d.num_digits.val <= 768)
    (hdigits : forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10)
    (head : Nat) (tail : List Nat)
    (hshape : (d.digits.val.take d.num_digits.val).map (fun b => b.val) =
      head :: tail)
    (hhead : 1 <= head) (hlen : (head :: tail).length = d.num_digits.val)
    (hloop : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
      d shift 0#usize 0#u64 false = ok (d0, read, acc, allZero)) :
    10 ^ (read.val - 1) <= acc.val /\ acc.val < 10 ^ read.val := 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 positive head makes the input digit fold positive. Apply DecimalRightShiftPrefixLoopSemantics to the given prefix-loop equation. It gives r\ge1 and two alternatives. If r\le n, the accumulator is the fold of the first r retained digits. This list has length r, the same positive head, and decimal entries. DecimalFoldBounds therefore gives its lower bound 10^{r-1} and strict upper bound 10^r. If r>n, that principal gives c=I10^{r-n}, where I is the whole input fold. The head and length hypotheses give n\ge1. Applying the same fold-bounds principal to the whole list gives 10^{n-1}\le I<10^n. Multiply by the positive integer 10^{r-n}. The exponent identities (n-1)+(r-n)=r-1 and n+(r-n)=r give the asserted bounds. These exhaust the prefix-loop alternatives. No fact about a cursor or accumulator that appears only in another theorem's proof is used.

Lean proof


  have hpositive : 0 <
      ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
        (fun n z => 10 * n + z) 0 := by
    rw [hshape]
    simp only [List.foldl_cons, Nat.mul_zero, Nat.zero_add]
    have foldPos : forall (ys : List Nat) (x : Nat), 0 < x ->
        0 < List.foldl (fun n z => 10 * n + z) x ys := by
      intro ys
      induction ys with
      | nil => intro x hx; simpa
      | cons z zs ih =>
          intro x hx
          simp only [List.foldl_cons]
          exact ih (10 * x + z) (by omega)
    exact foldPos tail head hhead
  have hs := P472_T1_DecimalRightShiftPrefixLoopSemantics
    d d0 shift read acc allZero hshift hpositive hdigits hloop
  dsimp only at hs
  rcases hs with ⟨_, _, hrpos, _, _, hcases⟩
  rcases hcases with hwithin | happended
  · rcases hwithin with ⟨hrn, _, hacc⟩
    let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    have hlen : xs.length = d.num_digits.val := by
      simp only [xs, List.length_map]
      rw [List.length_take]
      have hdlen : d.digits.val.length = 768 := by
        simpa using d.digits.property
      omega
    have htake : (d.digits.val.map (fun z => z.val)).take read.val =
        xs.take read.val := by
      simp [xs, List.map_take, List.take_take, Nat.min_eq_left hrn]
    have hdecimal : forall z, z ∈ xs.take read.val -> z < 10 := by
      intro z hz
      have hz' : z ∈ xs := List.mem_of_mem_take hz
      obtain ⟨b, hb, hbval⟩ := List.mem_map.mp hz'
      have : b.val = z := by simpa using hbval
      subst z
      exact hdigits b hb
    have hwhole : xs = head :: tail := by simpa [xs] using hshape
    have hshapePrefix : ∃ zs, xs.take read.val = head :: zs := by
      by_cases hz : read.val = 0
      · omega
      · obtain ⟨r, hr⟩ := Nat.exists_eq_succ_of_ne_zero hz
        refine ⟨tail.take r, ?_⟩
        rw [hwhole, hr]
        simp
    obtain ⟨zs, hxs⟩ := hshapePrefix
    have hbounds := P804_T1_DecimalFoldBounds (xs.take read.val)
      hdecimal head zs hxs (by omega)
    have hprefixlen : (xs.take read.val).length = read.val := by
      rw [List.length_take, hlen]
      omega
    constructor
    · rw [hacc, htake]
      simpa [hprefixlen] using hbounds.1
    · rw [hacc, htake]
      simpa [hprefixlen] using hbounds.2
  · rcases happended with ⟨hnr, _, _, hacc⟩
    have hnpos : 1 <= d.num_digits.val := by
      have hh := hlen
      simp at hh
      omega
    let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    have hwhole : xs = head :: tail := by simpa [xs] using hshape
    have hdecimal : forall z, z ∈ xs -> z < 10 := by
      intro z hz
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
      exact hdigits b hb
    have hnle : d.num_digits.val <= d.digits.val.length := by
      have hdlen : d.digits.val.length = 768 := by
        simp [d.digits.property]
      rw [hdlen]
      exact hcap
    have hxslen : xs.length = d.num_digits.val := by
      simp only [xs, List.length_map, List.length_take]
      rw [show d.digits.val.length = 768 by simp [d.digits.property]]
      exact Nat.min_eq_left hcap
    have hfold : List.foldl (fun n z => 10 * n + z) 0
        (List.take d.num_digits.val (d.digits.val.map (fun z => z.val))) =
        List.foldl (fun n z => 10 * n + z) 0 xs := by
      congr 1
      simp [xs, List.map_take, hnle]
    have hbounds := P804_T1_DecimalFoldBounds xs hdecimal head tail hwhole hhead
    have hpowpos : 0 < 10 ^ (read.val - d.num_digits.val) := by positivity
    have hmulLower := Nat.mul_le_mul_right
      (10 ^ (read.val - d.num_digits.val)) hbounds.1
    have hmulUpper := Nat.mul_lt_mul_of_pos_right hbounds.2 hpowpos
    have hpowLower : 10 ^ (d.num_digits.val - 1) *
        10 ^ (read.val - d.num_digits.val) = 10 ^ (read.val - 1) := by
      rw [← pow_add]
      congr 1
      omega
    have hpowUpper : 10 ^ d.num_digits.val *
        10 ^ (read.val - d.num_digits.val) = 10 ^ read.val := by
      rw [← pow_add]
      congr 1
      omega
    have hmulLower' : 10 ^ (d.num_digits.val - 1) *
        10 ^ (read.val - d.num_digits.val) <=
        List.foldl (fun n z => 10 * n + z) 0 xs *
          10 ^ (read.val - d.num_digits.val) := by
      simpa [hxslen] using hmulLower
    have hmulUpper' : List.foldl (fun n z => 10 * n + z) 0 xs *
        10 ^ (read.val - d.num_digits.val) <
        10 ^ d.num_digits.val * 10 ^ (read.val - d.num_digits.val) := by
      simpa [hxslen] using hmulUpper
    constructor
    · rw [hacc, hfold, ← hpowLower]
      exact hmulLower'
    · rw [hacc, hfold, ← hpowUpper]
      exact hmulUpper'

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P472_T1_DecimalRightShiftPrefixLoopSemantics
import Tablet.P804_T1_DecimalFoldBounds

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

-- [TABLET NODE: P495_T1_RightPrefixLengthBounds]
theorem P495_T1_RightPrefixLengthBounds
    (d d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift read : Std.Usize) (acc : Std.U64) (allZero : Bool)
    (hshift : shift.val <= 60) (hcap : d.num_digits.val <= 768)
    (hdigits : forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10)
    (head : Nat) (tail : List Nat)
    (hshape : (d.digits.val.take d.num_digits.val).map (fun b => b.val) =
      head :: tail)
    (hhead : 1 <= head) (hlen : (head :: tail).length = d.num_digits.val)
    (hloop : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
      d shift 0#usize 0#u64 false = ok (d0, read, acc, allZero)) :
    10 ^ (read.val - 1) <= acc.val /\ acc.val < 10 ^ read.val := by
-- BODY
  have hpositive : 0 <
      ((d.digits.val.take d.num_digits.val).map (fun z => z.val)).foldl
        (fun n z => 10 * n + z) 0 := by
    rw [hshape]
    simp only [List.foldl_cons, Nat.mul_zero, Nat.zero_add]
    have foldPos : forall (ys : List Nat) (x : Nat), 0 < x ->
        0 < List.foldl (fun n z => 10 * n + z) x ys := by
      intro ys
      induction ys with
      | nil => intro x hx; simpa
      | cons z zs ih =>
          intro x hx
          simp only [List.foldl_cons]
          exact ih (10 * x + z) (by omega)
    exact foldPos tail head hhead
  have hs := P472_T1_DecimalRightShiftPrefixLoopSemantics
    d d0 shift read acc allZero hshift hpositive hdigits hloop
  dsimp only at hs
  rcases hs with ⟨_, _, hrpos, _, _, hcases⟩
  rcases hcases with hwithin | happended
  · rcases hwithin with ⟨hrn, _, hacc⟩
    let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    have hlen : xs.length = d.num_digits.val := by
      simp only [xs, List.length_map]
      rw [List.length_take]
      have hdlen : d.digits.val.length = 768 := by
        simpa using d.digits.property
      omega
    have htake : (d.digits.val.map (fun z => z.val)).take read.val =
        xs.take read.val := by
      simp [xs, List.map_take, List.take_take, Nat.min_eq_left hrn]
    have hdecimal : forall z, z ∈ xs.take read.val -> z < 10 := by
      intro z hz
      have hz' : z ∈ xs := List.mem_of_mem_take hz
      obtain ⟨b, hb, hbval⟩ := List.mem_map.mp hz'
      have : b.val = z := by simpa using hbval
      subst z
      exact hdigits b hb
    have hwhole : xs = head :: tail := by simpa [xs] using hshape
    have hshapePrefix : ∃ zs, xs.take read.val = head :: zs := by
      by_cases hz : read.val = 0
      · omega
      · obtain ⟨r, hr⟩ := Nat.exists_eq_succ_of_ne_zero hz
        refine ⟨tail.take r, ?_⟩
        rw [hwhole, hr]
        simp
    obtain ⟨zs, hxs⟩ := hshapePrefix
    have hbounds := P804_T1_DecimalFoldBounds (xs.take read.val)
      hdecimal head zs hxs (by omega)
    have hprefixlen : (xs.take read.val).length = read.val := by
      rw [List.length_take, hlen]
      omega
    constructor
    · rw [hacc, htake]
      simpa [hprefixlen] using hbounds.1
    · rw [hacc, htake]
      simpa [hprefixlen] using hbounds.2
  · rcases happended with ⟨hnr, _, _, hacc⟩
    have hnpos : 1 <= d.num_digits.val := by
      have hh := hlen
      simp at hh
      omega
    let xs := (d.digits.val.take d.num_digits.val).map (fun z => z.val)
    have hwhole : xs = head :: tail := by simpa [xs] using hshape
    have hdecimal : forall z, z ∈ xs -> z < 10 := by
      intro z hz
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
      exact hdigits b hb
    have hnle : d.num_digits.val <= d.digits.val.length := by
      have hdlen : d.digits.val.length = 768 := by
        simp [d.digits.property]
      rw [hdlen]
      exact hcap
    have hxslen : xs.length = d.num_digits.val := by
      simp only [xs, List.length_map, List.length_take]
      rw [show d.digits.val.length = 768 by simp [d.digits.property]]
      exact Nat.min_eq_left hcap
    have hfold : List.foldl (fun n z => 10 * n + z) 0
        (List.take d.num_digits.val (d.digits.val.map (fun z => z.val))) =
        List.foldl (fun n z => 10 * n + z) 0 xs := by
      congr 1
      simp [xs, List.map_take, hnle]
    have hbounds := P804_T1_DecimalFoldBounds xs hdecimal head tail hwhole hhead
    have hpowpos : 0 < 10 ^ (read.val - d.num_digits.val) := by positivity
    have hmulLower := Nat.mul_le_mul_right
      (10 ^ (read.val - d.num_digits.val)) hbounds.1
    have hmulUpper := Nat.mul_lt_mul_of_pos_right hbounds.2 hpowpos
    have hpowLower : 10 ^ (d.num_digits.val - 1) *
        10 ^ (read.val - d.num_digits.val) = 10 ^ (read.val - 1) := by
      rw [← pow_add]
      congr 1
      omega
    have hpowUpper : 10 ^ d.num_digits.val *
        10 ^ (read.val - d.num_digits.val) = 10 ^ read.val := by
      rw [← pow_add]
      congr 1
      omega
    have hmulLower' : 10 ^ (d.num_digits.val - 1) *
        10 ^ (read.val - d.num_digits.val) <=
        List.foldl (fun n z => 10 * n + z) 0 xs *
          10 ^ (read.val - d.num_digits.val) := by
      simpa [hxslen] using hmulLower
    have hmulUpper' : List.foldl (fun n z => 10 * n + z) 0 xs *
        10 ^ (read.val - d.num_digits.val) <
        10 ^ d.num_digits.val * 10 ^ (read.val - d.num_digits.val) := by
      simpa [hxslen] using hmulUpper
    constructor
    · rw [hacc, hfold, ← hpowLower]
      exact hmulLower'
    · rw [hacc, hfold, ← hpowUpper]
      exact hmulUpper'

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (117)
More program dependencies (84)
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": "P495_T1_RightPrefixLengthBounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "d0008326b0af83efa77bb616b6d9a6b0f46d3668a9d9903c21332f1d1e4c9f3b",
  "active_statement_hash": "106d3a5179237a89478cef5347760169e6c6fb9a9bcfda38543373516a8ffc4d",
  "kernel_axioms": [
    "Classical.choice",
    "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": {
    "P472_T1_DecimalRightShiftPrefixLoopSemantics": "0d5cb25f18eedbfb",
    "P804_T1_DecimalFoldBounds": "9af7ca1186664411"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "13e3cec939d02f51",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "4db7c37f5da1c6e0"
  }
}
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:fa9922f496ea76050bfe64d61d6f2ca0fd62cdac5f619f5f0ddba2a5428fd219",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}