P472_T1_LexemeSignificantMagnitude

Lean checked

Statement

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

Lemma · Full magnitude after deleting leading zeros

For any decimal lexeme x, write S for its integer digits followed by its fractional digits (empty when absent), and let C be S with its leading zeros removed. Put P=|x.\mathit{integerDigits}|-(|S|-|C|). Then, with the complete natural base-ten fold and integer powers,
\operatorname{accumulatedMagnitude}(x) =\operatorname{fold}_{10}(C)\, 10^{P+\operatorname{accumulatedExplicitExponent}(x)-|C|}.
No validity, length, storage-cap, source-execution or exact-exponent premise is required; the explicit exponent is the accumulated one in DecimalGoalVocabularyDefault.

Lean statement

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

import Tablet.DecimalGoalVocabularyDefault
import Mathlib.Data.List.TakeWhile

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P472_T1_LexemeSignificantMagnitude]
theorem P472_T1_LexemeSignificantMagnitude (x : DecimalLexeme) :
    let S := x.integerDigits ++
      match x.fractionDigits with | none => [] | some ds => ds
    let C := S.dropWhile (fun digit => digit == 0)
    let P : Int := (x.integerDigits.length : Int) - (S.length - C.length : Nat)
    DecimalGoalVocabularyDefault.accumulatedMagnitude x =
      ((C.foldl (fun a digit => 10 * a + digit) 0 : Nat) : Real) *
        (10 : Real) ^ (P +
          DecimalGoalVocabularyDefault.accumulatedExplicitExponent x -
          (C.length : Int)) := 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 definition in DecimalGoalVocabularyDefault is \operatorname{fold}_{10}(S)10^{E-f}, where E is its accumulated explicit exponent and f the fractional digit count. Deleting leading zeros leaves the zero-start fold unchanged. To verify this, induct on the list S. The empty list is immediate. If its first digit is zero, the fold's first update is 10\cdot0+0=0 and the deletion continues on its tail, so the induction hypothesis applies. If the first digit is nonzero, leading-zero deletion stops without changing the list or its fold.
Deleting a prefix gives |C|\le|S|, and the significand concatenation gives |S|=|x.\mathit{integerDigits}|+f. Therefore the natural subtraction |S|-|C| is exact, and, after passing to integers,
P+E-|C| =|x.\mathit{integerDigits}|-(|S|-|C|)+E-|C|=E-f.
Substituting the unchanged fold and this exponent equality in the defining magnitude formula proves the identity, including the case C=[], when both folds are zero regardless of the exponent.

Lean proof


  dsimp only
  let S := x.integerDigits ++
    match x.fractionDigits with | none => [] | some ds => ds
  let C := S.dropWhile (fun digit => digit == 0)
  let f := match x.fractionDigits with | none => 0 | some ds => ds.length
  have hfold (digits : List Nat) :
      (digits.dropWhile (fun digit => digit == 0)).foldl
        (fun a digit => 10 * a + digit) 0 =
      digits.foldl (fun a digit => 10 * a + digit) 0 := by
    induction digits with
    | nil => rfl
    | cons digit digits ih =>
      by_cases hz : digit = 0
      · subst digit
        simpa using ih
      · have hb : (digit == 0) = false := beq_eq_false_iff_ne.mpr hz
        simp [List.dropWhile, hb]
  have hlength : S.length = x.integerDigits.length + f := by
    cases hf : x.fractionDigits <;> simp [S, f, hf]
  have hdrop : C.length <= S.length := List.length_dropWhile_le _ _
  have hexp : (x.integerDigits.length : Int) - (S.length - C.length : Nat) +
      DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (C.length : Int) =
      DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (f : Int) := by
    omega
  change DecimalGoalVocabularyDefault.accumulatedMagnitude x =
    ((C.foldl (fun a digit => 10 * a + digit) 0 : Nat) : Real) *
      (10 : Real) ^ ((x.integerDigits.length : Int) - (S.length - C.length : Nat) +
        DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (C.length : Int))
  rw [hexp, show C.foldl (fun a digit => 10 * a + digit) 0 =
    S.foldl (fun a digit => 10 * a + digit) 0 from hfold S]
  simp only [DecimalGoalVocabularyDefault, S, f]
  cases hfrac : x.fractionDigits <;> simp [hfrac]

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Mathlib.Data.List.TakeWhile

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P472_T1_LexemeSignificantMagnitude]
theorem P472_T1_LexemeSignificantMagnitude (x : DecimalLexeme) :
    let S := x.integerDigits ++
      match x.fractionDigits with | none => [] | some ds => ds
    let C := S.dropWhile (fun digit => digit == 0)
    let P : Int := (x.integerDigits.length : Int) - (S.length - C.length : Nat)
    DecimalGoalVocabularyDefault.accumulatedMagnitude x =
      ((C.foldl (fun a digit => 10 * a + digit) 0 : Nat) : Real) *
        (10 : Real) ^ (P +
          DecimalGoalVocabularyDefault.accumulatedExplicitExponent x -
          (C.length : Int)) := by
-- BODY
  dsimp only
  let S := x.integerDigits ++
    match x.fractionDigits with | none => [] | some ds => ds
  let C := S.dropWhile (fun digit => digit == 0)
  let f := match x.fractionDigits with | none => 0 | some ds => ds.length
  have hfold (digits : List Nat) :
      (digits.dropWhile (fun digit => digit == 0)).foldl
        (fun a digit => 10 * a + digit) 0 =
      digits.foldl (fun a digit => 10 * a + digit) 0 := by
    induction digits with
    | nil => rfl
    | cons digit digits ih =>
      by_cases hz : digit = 0
      · subst digit
        simpa using ih
      · have hb : (digit == 0) = false := beq_eq_false_iff_ne.mpr hz
        simp [List.dropWhile, hb]
  have hlength : S.length = x.integerDigits.length + f := by
    cases hf : x.fractionDigits <;> simp [S, f, hf]
  have hdrop : C.length <= S.length := List.length_dropWhile_le _ _
  have hexp : (x.integerDigits.length : Int) - (S.length - C.length : Nat) +
      DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (C.length : Int) =
      DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (f : Int) := by
    omega
  change DecimalGoalVocabularyDefault.accumulatedMagnitude x =
    ((C.foldl (fun a digit => 10 * a + digit) 0 : Nat) : Real) *
      (10 : Real) ^ ((x.integerDigits.length : Int) - (S.length - C.length : Nat) +
        DecimalGoalVocabularyDefault.accumulatedExplicitExponent x - (C.length : Int))
  rw [hexp, show C.foldl (fun a digit => 10 * a + digit) 0 =
    S.foldl (fun a digit => 10 * a + digit) 0 from hfold S]
  simp only [DecimalGoalVocabularyDefault, S, f]
  cases hfrac : x.fractionDigits <;> simp [hfrac]

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
All 7 entries
Technical checking records
Local closure summary
{
  "node": "P472_T1_LexemeSignificantMagnitude",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "704ce6acd444b0b6f06ce55668cdcdead880dbf45a95051dc4622e14c3d35510",
  "active_statement_hash": "3520d2d602af4b53e13e474ad454875f41798a078ac321b269183eb87c2242f2",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "576e6bf9480cfd32f53c309c7ac8fb115c1ba6e413f299ed5ff9fa7c647e6769",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-99",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "94cb4c406f8efb78"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "c43614ff2149ecf2",
    "DecimalGoalVocabularyDefault": "895e8e2ceadc7913",
    "DecimalLexeme": "303e59678842cea4",
    "F64": "ec4e25fbaf4e11d4",
    "num_imp_dec2flt_common_BiasedFp": "5c88d5d7976c5e8f",
    "num_imp_dec2flt_decimal_Decimal": "c43614ff2149ecf2"
  }
}
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:182e9efe0945ef3a2fcec47ed455df7fd24671774e2f9f2fb09172df6091a66d",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}