P804_T1_RightPrefixLengthBounds

Lean checked

Statement

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

Helper · Decimal length of the right-shift prefix accumulator

Let d be a positive-leading, nonempty decimal record with at most 768 retained cells, all below ten. Suppose the extracted prefix loop for a shift k\leq60 returns cursor r and accumulator C. Then
10^{r-1}\leq C<10^r.

Lean statement

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

import Tablet.DecimalRightShiftPrefixLoopSemantics
import Tablet.P804_T1_DecimalFoldBounds

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_RightPrefixLengthBounds]
theorem P804_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)
    (hdigit : forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10)
    (z : Nat) (zs : List Nat)
    (hshape :
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs)
    (hz : 1 <= z) (hlen : (z :: zs).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

Apply DecimalRightShiftPrefixLoopSemantics. Its two alternatives are exhaustive. If the returned cursor r is within the retained count, the accumulator is the base-ten fold of the first r stored digits. The prefix-loop statement also gives r\geq1. Hence this prefix is nonempty, has the same positive leading digit as the input, has length r, and all its digits are below ten. Applying DecimalFoldBounds to that prefix gives the displayed inequalities.
In the zero-extension alternative, write n for the retained count and I for the fold of all retained digits. The prefix-loop statement gives r>n and
C=I10^{r-n}. \tag{1}
The input shape makes n\geq1, and DecimalFoldBounds gives 10^{n-1}\leq I<10^n. Multiplying those inequalities by the positive integer 10^{r-n} and using (n-1)+(r-n)=r-1 and n+(r-n)=r yields 10^{r-1}\leq C<10^r. This proves the result in both alternatives.

Lean proof


  let xs := d.digits.val.map (fun q => q.val)
  let digits := xs.take d.num_digits.val
  have hdigits : digits = z :: zs := by
    simpa [digits, xs, List.map_take] using hshape
  have hdigitsDef : digits =
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) := by
    simp [digits, xs, List.map_take]
  have hdigitsBound : forall q, q ∈ digits -> q < 10 := by
    intro q hq
    rw [hdigitsDef] at hq
    simp only [List.mem_map] at hq
    rcases hq with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have hdigitsLen : digits.length = d.num_digits.val := by
    rw [hdigits, hlen]
  have hIpos : 0 < digits.foldl (fun n q => 10 * n + q) 0 := by
    have hb := P804_T1_DecimalFoldBounds digits hdigitsBound z zs hdigits hz
    have hlen : 0 < digits.length := by simp [hdigits]
    have hp : 0 < 10 ^ (digits.length - 1) := by positivity
    omega
  have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc
    allZero hshift (by simpa [digits, xs, List.map_take] using hIpos) hdigit hloop
  dsimp only at hpref
  rcases hpref with ⟨_, _, hreadPos, _, _, hphase⟩
  rcases hphase with hwithin | hextend
  · rcases hwithin with ⟨hreadN, _, hacc⟩
    have htakes : xs.take read.val = digits.take read.val := by
      simp only [digits, List.take_take]
      rw [Nat.min_eq_left hreadN]
    have hreadLen : read.val <= digits.length := by
      rw [hdigitsLen]
      exact hreadN
    have htakeShape : exists tail, digits.take read.val = z :: tail := by
      cases hr : read.val with
      | zero => omega
      | succ r =>
        refine ⟨zs.take r, ?_⟩
        simp [hdigits]
    have htakeDigits : forall q, q ∈ digits.take read.val -> q < 10 := by
      intro q hq
      exact hdigitsBound q (List.mem_of_mem_take hq)
    rcases htakeShape with ⟨tail, htakeShape⟩
    have hb := P804_T1_DecimalFoldBounds (digits.take read.val)
      htakeDigits z tail htakeShape hz
    have htakeLen : (digits.take read.val).length = read.val := by
      simp [hreadLen]
    rw [hacc, htakes]
    simpa [htakeLen] using hb
  · rcases hextend with ⟨hnread, _, hreadBound, hacc⟩
    have hnpos : 1 <= d.num_digits.val := by
      have hlen : digits.length = d.num_digits.val := by
        simp [digits, xs, d.digits.property, hcap]
      rw [← hlen, hdigits]
      simp
    have hb := P804_T1_DecimalFoldBounds digits hdigitsBound z zs hdigits hz
    rw [hdigitsLen] at hb
    have hdigitsFold :
        (xs.take d.num_digits.val).foldl (fun n q => 10 * n + q) 0 =
          digits.foldl (fun n q => 10 * n + q) 0 := by rfl
    rw [hacc, hdigitsFold]
    have hpowEqLower :
        10 ^ (read.val - 1) =
          10 ^ (d.num_digits.val - 1) *
            10 ^ (read.val - d.num_digits.val) := by
      rw [← Nat.pow_add]
      congr 1
      omega
    have hpowEqUpper :
        10 ^ read.val = 10 ^ d.num_digits.val *
            10 ^ (read.val - d.num_digits.val) := by
      rw [← Nat.pow_add]
      congr 1
      omega
    constructor
    · rw [hpowEqLower]
      exact Nat.mul_le_mul_right _ hb.1
    · rw [hpowEqUpper]
      exact Nat.mul_lt_mul_right (by positivity) |>.mpr hb.2

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalRightShiftPrefixLoopSemantics
import Tablet.P804_T1_DecimalFoldBounds

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_RightPrefixLengthBounds]
theorem P804_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)
    (hdigit : forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10)
    (z : Nat) (zs : List Nat)
    (hshape :
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs)
    (hz : 1 <= z) (hlen : (z :: zs).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
  let xs := d.digits.val.map (fun q => q.val)
  let digits := xs.take d.num_digits.val
  have hdigits : digits = z :: zs := by
    simpa [digits, xs, List.map_take] using hshape
  have hdigitsDef : digits =
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) := by
    simp [digits, xs, List.map_take]
  have hdigitsBound : forall q, q ∈ digits -> q < 10 := by
    intro q hq
    rw [hdigitsDef] at hq
    simp only [List.mem_map] at hq
    rcases hq with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have hdigitsLen : digits.length = d.num_digits.val := by
    rw [hdigits, hlen]
  have hIpos : 0 < digits.foldl (fun n q => 10 * n + q) 0 := by
    have hb := P804_T1_DecimalFoldBounds digits hdigitsBound z zs hdigits hz
    have hlen : 0 < digits.length := by simp [hdigits]
    have hp : 0 < 10 ^ (digits.length - 1) := by positivity
    omega
  have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc
    allZero hshift (by simpa [digits, xs, List.map_take] using hIpos) hdigit hloop
  dsimp only at hpref
  rcases hpref with ⟨_, _, hreadPos, _, _, hphase⟩
  rcases hphase with hwithin | hextend
  · rcases hwithin with ⟨hreadN, _, hacc⟩
    have htakes : xs.take read.val = digits.take read.val := by
      simp only [digits, List.take_take]
      rw [Nat.min_eq_left hreadN]
    have hreadLen : read.val <= digits.length := by
      rw [hdigitsLen]
      exact hreadN
    have htakeShape : exists tail, digits.take read.val = z :: tail := by
      cases hr : read.val with
      | zero => omega
      | succ r =>
        refine ⟨zs.take r, ?_⟩
        simp [hdigits]
    have htakeDigits : forall q, q ∈ digits.take read.val -> q < 10 := by
      intro q hq
      exact hdigitsBound q (List.mem_of_mem_take hq)
    rcases htakeShape with ⟨tail, htakeShape⟩
    have hb := P804_T1_DecimalFoldBounds (digits.take read.val)
      htakeDigits z tail htakeShape hz
    have htakeLen : (digits.take read.val).length = read.val := by
      simp [hreadLen]
    rw [hacc, htakes]
    simpa [htakeLen] using hb
  · rcases hextend with ⟨hnread, _, hreadBound, hacc⟩
    have hnpos : 1 <= d.num_digits.val := by
      have hlen : digits.length = d.num_digits.val := by
        simp [digits, xs, d.digits.property, hcap]
      rw [← hlen, hdigits]
      simp
    have hb := P804_T1_DecimalFoldBounds digits hdigitsBound z zs hdigits hz
    rw [hdigitsLen] at hb
    have hdigitsFold :
        (xs.take d.num_digits.val).foldl (fun n q => 10 * n + q) 0 =
          digits.foldl (fun n q => 10 * n + q) 0 := by rfl
    rw [hacc, hdigitsFold]
    have hpowEqLower :
        10 ^ (read.val - 1) =
          10 ^ (d.num_digits.val - 1) *
            10 ^ (read.val - d.num_digits.val) := by
      rw [← Nat.pow_add]
      congr 1
      omega
    have hpowEqUpper :
        10 ^ read.val = 10 ^ d.num_digits.val *
            10 ^ (read.val - d.num_digits.val) := by
      rw [← Nat.pow_add]
      congr 1
      omega
    constructor
    · rw [hpowEqLower]
      exact Nat.mul_le_mul_right _ hb.1
    · rw [hpowEqUpper]
      exact Nat.mul_lt_mul_right (by positivity) |>.mpr hb.2

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (116)
More program dependencies (83)
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": "P804_T1_RightPrefixLengthBounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "47ea03c237208234df26495fff49753c97352ded416da9ff6b1a5f7cd9162194",
  "active_statement_hash": "f9256984a838b9755d6917500c526bf7341393521fffc21f62a92209fdc10a55",
  "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": "revalidate-cycle-189",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "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:808d61df7f4f0cb8d9369701f55de0382d65152b3122c81067852715d9aa062b",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}