P856_T1_RightPrefixReadBound

Lean checked

Statement

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

Helper · Sharp prefix bound for the loop-2 right shift

Let a nonempty decimal record have at most 768 digits, all below ten, and leading digit z\geq5. If the extracted right-shift prefix loop is called with a shift k\leq58 and returns cursor r and accumulator C, then r\leq18.

Lean statement

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

import Tablet.P804_T1_DecimalFoldBounds
import Tablet.DecimalRightShiftPrefixLoopSemantics

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P856_T1_RightPrefixReadBound]
theorem P856_T1_RightPrefixReadBound
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift read : Std.Usize) (acc : Std.U64)
    (hshift : shift.val <= 58)
    (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 : 5 <= 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 (d, read, acc, false)) :
    read.val <= 18 := 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 prefix-loop semantics in DecimalRightShiftPrefixLoopSemantics gives r\geq1, C<10\,2^k, and two exhaustive accumulator forms. If the cursor remains inside the retained digits, C is the fold of their first r digits. If the cursor has passed their end, C is the full fold followed by exactly r-N zero digits. In either case, induction on the tail of the relevant prefix gives
z10^{r-1}\leq C.
The induction step replaces an accumulator a by 10a+q\geq10a; its ordinary positive-fold base case is also supplied by DecimalFoldBounds. Were r\geq19, monotonicity of natural powers, z\geq5, and k\leq58 would give
5\,10^{18}\leq z10^{r-1}\leq C <10\,2^k\leq10\,2^{58},
contradicting the kernel-normalized integer inequality 10\,2^{58}<5\,10^{18}. Hence r\leq18.

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
      (by omega)
    have hpow : 0 < 10 ^ (digits.length - 1) := by positivity
    omega
  have hpref := DecimalRightShiftPrefixLoopSemantics d d shift read acc false
    (by omega) (by simpa [digits, xs, List.map_take] using hIpos) hdigit hloop
  dsimp only at hpref
  rcases hpref with ⟨_, _, hreadPos, _, haccUpper, hphase⟩
  have leadingLower (ys : List Nat) (w : Nat) (ws : List Nat)
      (hys : ys = w :: ws) :
      w * 10 ^ (ys.length - 1) <=
        ys.foldl (fun n q => 10 * n + q) 0 := by
    subst ys
    simp only [List.length_cons, Nat.add_sub_cancel, List.foldl_cons]
    have aux (tail : List Nat) (a : Nat) :
        a * 10 ^ tail.length <= tail.foldl (fun n q => 10 * n + q) a := by
      induction tail generalizing a with
      | nil => simp
      | cons b bs ih =>
          simp only [List.length_cons, pow_succ, List.foldl_cons]
          have h := ih (10 * a + b)
          nlinarith [show 0 <= b * 10 ^ bs.length by positivity]
    simpa using aux ws w
  have hlower : z * 10 ^ (read.val - 1) <= acc.val := by
    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 := leadingLower (digits.take read.val) z tail htakeShape
      have htakeLen : (digits.take read.val).length = read.val := by
        simp [hreadLen]
      rw [hacc, htakes]
      simpa [htakeLen] using hb
    · rcases hextend with ⟨hnread, _, _, hacc⟩
      have hnpos : 1 <= d.num_digits.val := by
        rw [← hdigitsLen, hdigits]
        simp
      have hb := leadingLower digits z zs hdigits
      rw [hdigitsLen] at hb
      rw [hacc]
      have hpowEq :
          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
      rw [hpowEq]
      calc
        z * (10 ^ (d.num_digits.val - 1) *
            10 ^ (read.val - d.num_digits.val)) =
            (z * 10 ^ (d.num_digits.val - 1)) *
              10 ^ (read.val - d.num_digits.val) := by ring
        _ <= digits.foldl (fun n q => 10 * n + q) 0 *
              10 ^ (read.val - d.num_digits.val) :=
            Nat.mul_le_mul_right _ hb
        _ = (xs.take d.num_digits.val).foldl
              (fun n q => 10 * n + q) 0 *
              10 ^ (read.val - d.num_digits.val) := by
            simp [digits]
  by_contra hnot
  have hread19 : 19 <= read.val := by omega
  have hpow : 10 ^ 18 <= 10 ^ (read.val - 1) :=
    Nat.pow_le_pow_right (by omega) (by omega)
  have hconst : 10 * 2 ^ 58 < 5 * 10 ^ 18 := by norm_num
  have hz5 : 5 * 10 ^ (read.val - 1) <= z * 10 ^ (read.val - 1) := by
    exact Nat.mul_le_mul_right _ hz
  have hshiftPow : 2 ^ shift.val <= 2 ^ 58 :=
    Nat.pow_le_pow_right (by omega) hshift
  omega

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P804_T1_DecimalFoldBounds
import Tablet.DecimalRightShiftPrefixLoopSemantics

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P856_T1_RightPrefixReadBound]
theorem P856_T1_RightPrefixReadBound
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (shift read : Std.Usize) (acc : Std.U64)
    (hshift : shift.val <= 58)
    (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 : 5 <= 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 (d, read, acc, false)) :
    read.val <= 18 := 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
      (by omega)
    have hpow : 0 < 10 ^ (digits.length - 1) := by positivity
    omega
  have hpref := DecimalRightShiftPrefixLoopSemantics d d shift read acc false
    (by omega) (by simpa [digits, xs, List.map_take] using hIpos) hdigit hloop
  dsimp only at hpref
  rcases hpref with ⟨_, _, hreadPos, _, haccUpper, hphase⟩
  have leadingLower (ys : List Nat) (w : Nat) (ws : List Nat)
      (hys : ys = w :: ws) :
      w * 10 ^ (ys.length - 1) <=
        ys.foldl (fun n q => 10 * n + q) 0 := by
    subst ys
    simp only [List.length_cons, Nat.add_sub_cancel, List.foldl_cons]
    have aux (tail : List Nat) (a : Nat) :
        a * 10 ^ tail.length <= tail.foldl (fun n q => 10 * n + q) a := by
      induction tail generalizing a with
      | nil => simp
      | cons b bs ih =>
          simp only [List.length_cons, pow_succ, List.foldl_cons]
          have h := ih (10 * a + b)
          nlinarith [show 0 <= b * 10 ^ bs.length by positivity]
    simpa using aux ws w
  have hlower : z * 10 ^ (read.val - 1) <= acc.val := by
    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 := leadingLower (digits.take read.val) z tail htakeShape
      have htakeLen : (digits.take read.val).length = read.val := by
        simp [hreadLen]
      rw [hacc, htakes]
      simpa [htakeLen] using hb
    · rcases hextend with ⟨hnread, _, _, hacc⟩
      have hnpos : 1 <= d.num_digits.val := by
        rw [← hdigitsLen, hdigits]
        simp
      have hb := leadingLower digits z zs hdigits
      rw [hdigitsLen] at hb
      rw [hacc]
      have hpowEq :
          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
      rw [hpowEq]
      calc
        z * (10 ^ (d.num_digits.val - 1) *
            10 ^ (read.val - d.num_digits.val)) =
            (z * 10 ^ (d.num_digits.val - 1)) *
              10 ^ (read.val - d.num_digits.val) := by ring
        _ <= digits.foldl (fun n q => 10 * n + q) 0 *
              10 ^ (read.val - d.num_digits.val) :=
            Nat.mul_le_mul_right _ hb
        _ = (xs.take d.num_digits.val).foldl
              (fun n q => 10 * n + q) 0 *
              10 ^ (read.val - d.num_digits.val) := by
            simp [digits]
  by_contra hnot
  have hread19 : 19 <= read.val := by omega
  have hpow : 10 ^ 18 <= 10 ^ (read.val - 1) :=
    Nat.pow_le_pow_right (by omega) (by omega)
  have hconst : 10 * 2 ^ 58 < 5 * 10 ^ 18 := by norm_num
  have hz5 : 5 * 10 ^ (read.val - 1) <= z * 10 ^ (read.val - 1) := by
    exact Nat.mul_le_mul_right _ hz
  have hshiftPow : 2 ^ shift.val <= 2 ^ 58 :=
    Nat.pow_le_pow_right (by omega) hshift
  omega

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": "P856_T1_RightPrefixReadBound",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "eac6f3b5356238a45ec642e7116bd5c5a775e06a83f4ab511e85d4465dba92b4",
  "active_statement_hash": "d2b51f50ffefa5456632b9e85162d2c5834413a7b4cddb8dd93a6077ad9e7a86",
  "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-199",
  "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:12360e8d1a9a59992008bce6cb1e3b16059ea1224a31dbf36ec0ea9ffead0b2d",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}