P804_T1_DecimalFoldBounds

Lean checked

Statement

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

Helper · Bounds for a positive-leading decimal fold

Let x_0,\ldots,x_{n-1} be a nonempty list of natural numbers, with every x_i<10 and 1\leq x_0. Its base-ten left fold I satisfies
10^{n-1}\leq I<10^n.

Lean statement

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

import Tablet.Preamble

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_DecimalFoldBounds]
theorem P804_T1_DecimalFoldBounds (xs : List Nat)
    (hd : forall z, z ∈ xs -> z < 10)
    (z : Nat) (zs : List Nat) (hxs : xs = z :: zs) (hz : 1 <= z) :
    10 ^ (xs.length - 1) <=
        xs.foldl (fun n digit => 10 * n + digit) 0 /\
      xs.foldl (fun n digit => 10 * n + digit) 0 < 10 ^ xs.length := 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 every digit list y and starting accumulator a, induction on y gives the positional identity
\operatorname{fold}_{10}(y,a) =a10^{|y|}+\operatorname{fold}_{10}(y,0). \tag{1}
The empty case is immediate. In the cons case, apply the induction hypothesis to the updated accumulator 10a+y_0 and distribute; this gives exactly (1).
We next prove the strict upper bound by induction on the list. The empty fold is 0<1. For y_0::y, (1) and the digit bound give
y_0 10^{|y|}+\operatorname{fold}_{10}(y,0) <9\,10^{|y|}+10^{|y|}=10^{|y|+1}.
For the lower bound, write the original list as x_0::x'. Equation (1), x_0\geq1, and nonnegativity of the tail fold give
\operatorname{fold}_{10}(x_0::x',0) =x_0 10^{|x'|}+\operatorname{fold}_{10}(x',0) \geq10^{|x'|}=10^{n-1}.
Together these are the two asserted inequalities.

Lean proof


  let F := fun ys : List Nat =>
    ys.foldl (fun n digit => 10 * n + digit) 0
  have foldlFrom (ys : List Nat) (a : Nat) :
      ys.foldl (fun n digit => 10 * n + digit) a =
        a * 10 ^ ys.length + F ys := by
    induction ys generalizing a with
    | nil => simp [F]
    | cons y ys ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * a + y) * 10 ^ ys.length + F ys =
        a * (10 ^ ys.length * 10) +
          (y :: ys).foldl (fun n digit => 10 * n + digit) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have upper (ys : List Nat) (hys : forall q, q ∈ ys -> q < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hy : y < 10 := hys y (by simp)
      have htail : forall q, q ∈ ys -> q < 10 := by
        intro q hq
        exact hys q (by simp [hq])
      have hi := ih htail
      rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
        simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
      have hm : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        y * 10 ^ ys.length + F ys <
            y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ ys.length + 10 ^ ys.length := Nat.add_le_add_right hm _
        _ = 10 ^ ys.length * 10 := by ring
  subst xs
  constructor
  · simp only [List.length_cons, Nat.add_sub_cancel, List.foldl_cons]
    change 10 ^ zs.length <= F (z :: zs)
    rw [show F (z :: zs) = z * 10 ^ zs.length + F zs by
      simpa [F] using foldlFrom zs z]
    have hp : 10 ^ zs.length <= z * 10 ^ zs.length := by
      simpa using Nat.mul_le_mul_right (10 ^ zs.length) hz
    omega
  · exact upper (z :: zs) hd

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Preamble

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_DecimalFoldBounds]
theorem P804_T1_DecimalFoldBounds (xs : List Nat)
    (hd : forall z, z ∈ xs -> z < 10)
    (z : Nat) (zs : List Nat) (hxs : xs = z :: zs) (hz : 1 <= z) :
    10 ^ (xs.length - 1) <=
        xs.foldl (fun n digit => 10 * n + digit) 0 /\
      xs.foldl (fun n digit => 10 * n + digit) 0 < 10 ^ xs.length := by
-- BODY
  let F := fun ys : List Nat =>
    ys.foldl (fun n digit => 10 * n + digit) 0
  have foldlFrom (ys : List Nat) (a : Nat) :
      ys.foldl (fun n digit => 10 * n + digit) a =
        a * 10 ^ ys.length + F ys := by
    induction ys generalizing a with
    | nil => simp [F]
    | cons y ys ih =>
      simp only [List.foldl_cons, List.length_cons]
      rw [ih, pow_succ]
      change (10 * a + y) * 10 ^ ys.length + F ys =
        a * (10 ^ ys.length * 10) +
          (y :: ys).foldl (fun n digit => 10 * n + digit) 0
      simp only [List.foldl_cons]
      rw [ih]
      ring
  have upper (ys : List Nat) (hys : forall q, q ∈ ys -> q < 10) :
      F ys < 10 ^ ys.length := by
    induction ys with
    | nil => simp [F]
    | cons y ys ih =>
      have hy : y < 10 := hys y (by simp)
      have htail : forall q, q ∈ ys -> q < 10 := by
        intro q hq
        exact hys q (by simp [hq])
      have hi := ih htail
      rw [show F (y :: ys) = y * 10 ^ ys.length + F ys by
        simpa [F] using foldlFrom ys y, List.length_cons, pow_succ]
      have hm : y * 10 ^ ys.length <= 9 * 10 ^ ys.length :=
        Nat.mul_le_mul_right _ (by omega)
      calc
        y * 10 ^ ys.length + F ys <
            y * 10 ^ ys.length + 10 ^ ys.length := Nat.add_lt_add_left hi _
        _ <= 9 * 10 ^ ys.length + 10 ^ ys.length := Nat.add_le_add_right hm _
        _ = 10 ^ ys.length * 10 := by ring
  subst xs
  constructor
  · simp only [List.length_cons, Nat.add_sub_cancel, List.foldl_cons]
    change 10 ^ zs.length <= F (z :: zs)
    rw [show F (z :: zs) = z * 10 ^ zs.length + F zs by
      simpa [F] using foldlFrom zs z]
    have hp : 10 ^ zs.length <= z * 10 ^ zs.length := by
      simpa using Nat.mul_le_mul_right (10 ^ zs.length) hz
    omega
  · exact upper (z :: zs) hd

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (44)
More program dependencies (11)
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
Technical checking records
Local closure summary
{
  "node": "P804_T1_DecimalFoldBounds",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "cc9bdf9af3452ccb316b0001bcf9a5b0cf40c200f61155a1c0abe73920db2b33",
  "active_statement_hash": "c9c0a1663c068a241e337440fff01ec90d1774f032c9c6e6f78641c6884ac797",
  "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": {},
  "strict_definition_deps": {}
}
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:5b32170e28791a06dd0409d8849d957a367d327ea70f2482c6c5d10119378eb5",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}