P804_T1_DecimalLeadingInterval

Lean checked

Statement

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

Helper · Decimal value interval determined by the leading digit

Let x=z::z_s be a nonempty list of decimal digits, let I be its base-ten fold, and let p be any integer. Then
z10^{p-1}\le I10^{p-|x|}<(z+1)10^{p-1}.
The Lean statement retains the harmless positive-leading hypothesis used by its callers.

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_DecimalValueBounds

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_DecimalLeadingInterval]
theorem P804_T1_DecimalLeadingInterval
    (xs : List Nat) (z : Nat) (zs : List Nat)
    (hxs : xs = z :: zs) (hz : 1 <= z)
    (hd : forall q, q ∈ xs -> q < 10) (p : Int) :
    let I : Nat := xs.foldl (fun n digit => 10 * n + digit) 0
    (z : Real) * (10 : Real) ^ (p - 1) <=
        (I : Real) * (10 : Real) ^ (p - (xs.length : Int)) /\
      (I : Real) * (10 : Real) ^ (p - (xs.length : Int)) <
        (z + 1 : Nat) * (10 : Real) ^ (p - 1) := 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

Writing F for the base-ten fold, induction on a list y proves both
F(y,a)=a10^{|y|}+F(y,0),\qquad F(y,0)<10^{|y|}
when every member of y is below ten. Applying these identities to the tail z_s gives
I=z10^{|z_s|}+F(z_s,0),qquad 0\le F(z_s,0)<10^{|z_s|}.
Thus z10^{|z_s|}\le I<(z+1)10^{|z_s|}. Since |x|=|z_s|+1, multiply both inequalities by the positive factor 10^{p-|x|}. The integer-power addition law gives 10^{|z_s|}10^{p-|x|}=10^{p-1}, yielding exactly the displayed interval.

Lean proof


  dsimp only
  have hn : 1 <= xs.length := by simp [hxs]
  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 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 htailUpper : F zs < 10 ^ zs.length := by
    apply foldBound
    intro q hq
    exact hd q (by rw [hxs]; simp [hq])
  have hform : xs.foldl (fun n digit => 10 * n + digit) 0 =
      z * 10 ^ zs.length + F zs := by
    rw [hxs]
    simp only [List.foldl_cons]
    simpa [F] using foldlFrom zs z
  have hlen : xs.length = zs.length + 1 := by simp [hxs]
  have hlowerNat : z * 10 ^ zs.length <=
      xs.foldl (fun n digit => 10 * n + digit) 0 := by
    rw [hform]
    omega
  have hupperNat : xs.foldl (fun n digit => 10 * n + digit) 0 <
      (z + 1) * 10 ^ zs.length := by
    rw [hform]
    nlinarith
  have hlower : (z : Real) * (10 : Real) ^ zs.length <=
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) := by
    exact_mod_cast hlowerNat
  have hupper :
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) <
        (z + 1 : Nat) * (10 : Real) ^ zs.length := by
    exact_mod_cast hupperNat
  have hpowpos : 0 < (10 : Real) ^ (p - (xs.length : Int)) :=
    zpow_pos (by norm_num) _
  have hexp : p - 1 = (zs.length : Int) +
      (p - (xs.length : Int)) := by omega
  constructor
  · have hm := mul_le_mul_of_nonneg_right hlower (le_of_lt hpowpos)
    calc
      (z : Real) * (10 : Real) ^ (p - 1) =
          ((z : Real) * (10 : Real) ^ zs.length) *
            (10 : Real) ^ (p - (xs.length : Int)) := by
              rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
              norm_num only [zpow_natCast]
              ring
      _ <= ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) *
          (10 : Real) ^ (p - (xs.length : Int)) := hm
  · have hm := mul_lt_mul_of_pos_right hupper hpowpos
    calc
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) *
          (10 : Real) ^ (p - (xs.length : Int)) <
          ((z + 1 : Nat) * (10 : Real) ^ zs.length) *
            (10 : Real) ^ (p - (xs.length : Int)) := hm
      _ = (z + 1 : Nat) * (10 : Real) ^ (p - 1) := by
        rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
        norm_num only [zpow_natCast]
        ring

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P804_T1_DecimalValueBounds

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_DecimalLeadingInterval]
theorem P804_T1_DecimalLeadingInterval
    (xs : List Nat) (z : Nat) (zs : List Nat)
    (hxs : xs = z :: zs) (hz : 1 <= z)
    (hd : forall q, q ∈ xs -> q < 10) (p : Int) :
    let I : Nat := xs.foldl (fun n digit => 10 * n + digit) 0
    (z : Real) * (10 : Real) ^ (p - 1) <=
        (I : Real) * (10 : Real) ^ (p - (xs.length : Int)) /\
      (I : Real) * (10 : Real) ^ (p - (xs.length : Int)) <
        (z + 1 : Nat) * (10 : Real) ^ (p - 1) := by
-- BODY
  dsimp only
  have hn : 1 <= xs.length := by simp [hxs]
  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 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 htailUpper : F zs < 10 ^ zs.length := by
    apply foldBound
    intro q hq
    exact hd q (by rw [hxs]; simp [hq])
  have hform : xs.foldl (fun n digit => 10 * n + digit) 0 =
      z * 10 ^ zs.length + F zs := by
    rw [hxs]
    simp only [List.foldl_cons]
    simpa [F] using foldlFrom zs z
  have hlen : xs.length = zs.length + 1 := by simp [hxs]
  have hlowerNat : z * 10 ^ zs.length <=
      xs.foldl (fun n digit => 10 * n + digit) 0 := by
    rw [hform]
    omega
  have hupperNat : xs.foldl (fun n digit => 10 * n + digit) 0 <
      (z + 1) * 10 ^ zs.length := by
    rw [hform]
    nlinarith
  have hlower : (z : Real) * (10 : Real) ^ zs.length <=
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) := by
    exact_mod_cast hlowerNat
  have hupper :
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) <
        (z + 1 : Nat) * (10 : Real) ^ zs.length := by
    exact_mod_cast hupperNat
  have hpowpos : 0 < (10 : Real) ^ (p - (xs.length : Int)) :=
    zpow_pos (by norm_num) _
  have hexp : p - 1 = (zs.length : Int) +
      (p - (xs.length : Int)) := by omega
  constructor
  · have hm := mul_le_mul_of_nonneg_right hlower (le_of_lt hpowpos)
    calc
      (z : Real) * (10 : Real) ^ (p - 1) =
          ((z : Real) * (10 : Real) ^ zs.length) *
            (10 : Real) ^ (p - (xs.length : Int)) := by
              rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
              norm_num only [zpow_natCast]
              ring
      _ <= ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) *
          (10 : Real) ^ (p - (xs.length : Int)) := hm
  · have hm := mul_lt_mul_of_pos_right hupper hpowpos
    calc
      ((xs.foldl (fun n digit => 10 * n + digit) 0 : Nat) : Real) *
          (10 : Real) ^ (p - (xs.length : Int)) <
          ((z + 1 : Nat) * (10 : Real) ^ zs.length) *
            (10 : Real) ^ (p - (xs.length : Int)) := hm
      _ = (z + 1 : Nat) * (10 : Real) ^ (p - 1) := by
        rw [hexp, zpow_add₀ (by norm_num : (10 : Real) ≠ 0)]
        norm_num only [zpow_natCast]
        ring

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
Technical checking records
Local closure summary
{
  "node": "P804_T1_DecimalLeadingInterval",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "c9ddaf9639ede43e345dca342ee120268a3864d2735e05096737715674956b8d",
  "active_statement_hash": "e5e344ccb2cf001b34a2139c32132317c9942aec5a4a7986c305d379c227f325",
  "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:47240a2343a5c7da172a71bb8912ecb3983367d8c8819c3f1432184be913e7fa",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}