P804_T1_PointZeroGrowth

Lean checked

Statement

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

Lemma · Zero digit growth in the point-zero repair

Let a nonempty decimal record have leading digit 1\le z<5, at most 768 cells, and decimal cells throughout. Set k=2 when z=1 and k=1 otherwise. The extracted left-shift digit-growth routine returns normally and returns growth zero.

Lean statement

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

import Tablet.DecimalLeftShiftDigitGrowth

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_PointZeroGrowth]
theorem P804_T1_PointZeroGrowth
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (z : Nat) (zs : List Nat)
    (hcap : d.num_digits.val <= 768)
    (hdigit : forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10)
    (hshape :
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs)
    (hzLo : 1 <= z) (hzHi : z < 5) :
    let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
    exists growth : Std.Usize,
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d shift = ok growth /\ growth.val = 0 := 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

For either selected shift, 1<2^k<10. Thus DecimalLeftShiftDigitGrowth with h=1 says that the returned growth is one exactly if 10^n\le I2^k, and is zero otherwise, where n is the retained length and I its base-ten fold.
Write the retained word as z::z_s. The elementary fold recurrence gives
I=z10^{|z_s|}+F(z_s),\qquad0\le F(z_s)<10^{|z_s|}.
The second inequality follows by induction from the fact that every cell is below ten. Hence I<(z+1)10^{n-1}. If z=1, then k=2 and I2^k<2\cdot4\,10^{n-1}<10^n. If z=2,3, or 4, then k=1 and I2^k<(z+1)2\,10^{n-1}\le10^n. In every case the threshold test is false, so the cited digit-growth formula returns zero. The capacity hypothesis also makes the taken list have the stated retained length, completing the call and value claims.

Lean proof


  dsimp only
  let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
  have hk : shift.val <= 60 := by
    by_cases hz1 : z = 1 <;> simp [shift, hz1]
  have hlo : 10 ^ (1 - 1) < 2 ^ shift.val := by
    by_cases hz1 : z = 1 <;> simp [shift, hz1]
  have hhi : 2 ^ shift.val < 10 ^ 1 := by
    by_cases hz1 : z = 1 <;> simp [shift, hz1]
  obtain ⟨growth, hgrowth, hgrowthVal⟩ :=
    DecimalLeftShiftDigitGrowth d shift 1 hk hlo hhi hcap hdigit
  let xs := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
  let F := fun ys : List Nat =>
    ys.foldl (fun a q => 10 * a + q) 0
  have foldlFrom (ys : List Nat) (a : Nat) :
      ys.foldl (fun b q => 10 * b + q) a =
        a * 10 ^ ys.length + F ys := by
    induction ys generalizing a with
    | nil => simp [F]
    | cons q qs ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * a + q) * 10 ^ qs.length + F qs =
        a * (10 ^ qs.length * 10) +
          (q :: qs).foldl (fun b r => 10 * b + r) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have htailDigit : forall q, q ∈ zs -> q < 10 := by
    intro q hq
    have : q ∈ xs := by simp [xs, hshape, hq]
    simp only [xs, List.mem_map] at this
    rcases this with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have foldBound (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons q qs ih =>
      have hq := hy q (by simp)
      have hqs : forall r, r ∈ qs -> r < 10 := by
        intro r hr
        exact hy r (by simp [hr])
      have hi := ih hqs
      rw [show F (q :: qs) = q * 10 ^ qs.length + F qs by
        simpa [F] using foldlFrom qs q, List.length_cons, pow_succ]
      nlinarith [Nat.mul_le_mul_right (10 ^ qs.length) (show q <= 9 by omega)]
  have htail := foldBound zs htailDigit
  have hxs : xs = z :: zs := by simpa [xs] using hshape
  have hI : F xs = z * 10 ^ zs.length + F zs := by
    rw [hxs]
    change (z :: zs).foldl (fun a q => 10 * a + q) 0 = _
    simp only [List.foldl_cons]
    simpa [F] using foldlFrom zs z
  have hthreshold : F xs * 2 ^ shift.val < 10 ^ xs.length := by
    rw [hI, hxs, List.length_cons, pow_succ]
    by_cases hz1 : z = 1
    · simp [shift, hz1]
      nlinarith
    · have hzCases : z = 2 \/ z = 3 \/ z = 4 := by omega
      rcases hzCases with rfl | rfl | rfl <;> simp [shift] <;> nlinarith
  have htest : ¬ 10 ^ (d.num_digits.val + 1 - 1) <=
      ((d.digits.val.take d.num_digits.val).map (fun q => q.val)).foldl
        (fun a q => 10 * a + q) 0 * 2 ^ shift.val := by
    have hlen : xs.length = d.num_digits.val := by
      have h := congrArg List.length hshape
      simp only [List.length_map, List.length_take, d.digits.property,
        List.length_cons] at h
      norm_num at h
      rw [Nat.min_eq_left hcap] at h
      rw [hxs]
      simpa using h.symm
    change ¬ 10 ^ (d.num_digits.val + 1 - 1) <=
      F xs * 2 ^ shift.val
    rw [Nat.add_sub_cancel, ← hlen]
    omega
  refine ⟨growth, hgrowth, ?_⟩
  rw [hgrowthVal]
  have htest' : ¬ 10 ^ (d.num_digits.val + 1 - 1) <=
      ((d.digits.val.map (fun q => q.val)).take d.num_digits.val).foldl
        (fun a q => 10 * a + q) 0 * 2 ^ shift.val := by
    simpa only [List.map_take] using htest
  split
  · exact False.elim (htest (by assumption))
  · rfl

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalLeftShiftDigitGrowth

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_PointZeroGrowth]
theorem P804_T1_PointZeroGrowth
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (z : Nat) (zs : List Nat)
    (hcap : d.num_digits.val <= 768)
    (hdigit : forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10)
    (hshape :
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs)
    (hzLo : 1 <= z) (hzHi : z < 5) :
    let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
    exists growth : Std.Usize,
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d shift = ok growth /\ growth.val = 0 := by
-- BODY
  dsimp only
  let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
  have hk : shift.val <= 60 := by
    by_cases hz1 : z = 1 <;> simp [shift, hz1]
  have hlo : 10 ^ (1 - 1) < 2 ^ shift.val := by
    by_cases hz1 : z = 1 <;> simp [shift, hz1]
  have hhi : 2 ^ shift.val < 10 ^ 1 := by
    by_cases hz1 : z = 1 <;> simp [shift, hz1]
  obtain ⟨growth, hgrowth, hgrowthVal⟩ :=
    DecimalLeftShiftDigitGrowth d shift 1 hk hlo hhi hcap hdigit
  let xs := (d.digits.val.take d.num_digits.val).map (fun q => q.val)
  let F := fun ys : List Nat =>
    ys.foldl (fun a q => 10 * a + q) 0
  have foldlFrom (ys : List Nat) (a : Nat) :
      ys.foldl (fun b q => 10 * b + q) a =
        a * 10 ^ ys.length + F ys := by
    induction ys generalizing a with
    | nil => simp [F]
    | cons q qs ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * a + q) * 10 ^ qs.length + F qs =
        a * (10 ^ qs.length * 10) +
          (q :: qs).foldl (fun b r => 10 * b + r) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have htailDigit : forall q, q ∈ zs -> q < 10 := by
    intro q hq
    have : q ∈ xs := by simp [xs, hshape, hq]
    simp only [xs, List.mem_map] at this
    rcases this with ⟨u, hu, rfl⟩
    exact hdigit u hu
  have foldBound (ys : List Nat) (hy : forall q, q ∈ ys -> q < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons q qs ih =>
      have hq := hy q (by simp)
      have hqs : forall r, r ∈ qs -> r < 10 := by
        intro r hr
        exact hy r (by simp [hr])
      have hi := ih hqs
      rw [show F (q :: qs) = q * 10 ^ qs.length + F qs by
        simpa [F] using foldlFrom qs q, List.length_cons, pow_succ]
      nlinarith [Nat.mul_le_mul_right (10 ^ qs.length) (show q <= 9 by omega)]
  have htail := foldBound zs htailDigit
  have hxs : xs = z :: zs := by simpa [xs] using hshape
  have hI : F xs = z * 10 ^ zs.length + F zs := by
    rw [hxs]
    change (z :: zs).foldl (fun a q => 10 * a + q) 0 = _
    simp only [List.foldl_cons]
    simpa [F] using foldlFrom zs z
  have hthreshold : F xs * 2 ^ shift.val < 10 ^ xs.length := by
    rw [hI, hxs, List.length_cons, pow_succ]
    by_cases hz1 : z = 1
    · simp [shift, hz1]
      nlinarith
    · have hzCases : z = 2 \/ z = 3 \/ z = 4 := by omega
      rcases hzCases with rfl | rfl | rfl <;> simp [shift] <;> nlinarith
  have htest : ¬ 10 ^ (d.num_digits.val + 1 - 1) <=
      ((d.digits.val.take d.num_digits.val).map (fun q => q.val)).foldl
        (fun a q => 10 * a + q) 0 * 2 ^ shift.val := by
    have hlen : xs.length = d.num_digits.val := by
      have h := congrArg List.length hshape
      simp only [List.length_map, List.length_take, d.digits.property,
        List.length_cons] at h
      norm_num at h
      rw [Nat.min_eq_left hcap] at h
      rw [hxs]
      simpa using h.symm
    change ¬ 10 ^ (d.num_digits.val + 1 - 1) <=
      F xs * 2 ^ shift.val
    rw [Nat.add_sub_cancel, ← hlen]
    omega
  refine ⟨growth, hgrowth, ?_⟩
  rw [hgrowthVal]
  have htest' : ¬ 10 ^ (d.num_digits.val + 1 - 1) <=
      ((d.digits.val.map (fun q => q.val)).take d.num_digits.val).foldl
        (fun a q => 10 * a + q) 0 * 2 ^ shift.val := by
    simpa only [List.map_take] using htest
  split
  · exact False.elim (htest (by assumption))
  · rfl

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (49)
More program dependencies (16)
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_PointZeroGrowth",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "b15d5d58ca4b3f3d4d3a765e3a8830219d59ef1d1208f82480a66e457d0d0cd9",
  "active_statement_hash": "9cf0e1ff933a642da67cfa69353075d4c35584eb7ddf0f1903d9632eab8943fb",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "d5c04ffa333d9b33b238a527f5beaa2be82e021d3e9c0bbe01b1310280f7f55a",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-68",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalLeftShiftDigitGrowth": "6dcf8858df4713eb"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "13e3cec939d02f51",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061"
  }
}
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:e914d135be4d5dad117a43272afe5fc5f5c15c70ebe007690c802f0fcc085984",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}