P435_T1_DigitDecoding

Lean checked

Statement

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

Lemma · Byte-digit decoding and fold transport

For every U8 list B the following hold, writing \delta(b)=b.\mathit{val}-48 with natural subtraction. If all its bytes have values in [48,57], all members of \operatorname{map}(\delta,B) are below ten, and \operatorname{map}(\mathit{val},B)= \operatorname{map}(d\mapsto48+d,\operatorname{map}(\delta,B)). Moreover, for any natural list D whose encoded value map equals \operatorname{map}(\mathit{val},B), its decoding is exactly D, |B|=|D|, and for every function H:\mathbb N\times\mathbb N\to\mathbb N and starting accumulator a,
\operatorname{foldl}((n,b)\mapsto H(n,\delta(b)),a,B) =\operatorname{foldl}(H,a,D).
No digit-range hypothesis on D is needed for this second assertion.

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: P435_T1_DigitDecoding]
theorem P435_T1_DigitDecoding (bytes : List Std.U8) :
    ((forall b, b ∈ bytes -> 48 <= b.val /\ b.val <= 57) ->
      (forall d, d ∈ bytes.map (fun b => b.val - 48) -> d < 10) /\
      bytes.map (fun b => b.val) =
        (bytes.map (fun b => b.val - 48)).map (fun d => 48 + d)) /\
    (forall digits : List Nat,
      bytes.map (fun b => b.val) = digits.map (fun d => 48 + d) ->
      bytes.map (fun b => b.val - 48) = digits /\
      bytes.length = digits.length /\
      (forall (step : Nat -> Nat -> Nat) (initial : Nat),
        bytes.foldl (fun a b => step a (b.val - 48)) initial =
          digits.foldl step initial)) := 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

If 48\le b.\mathit{val}\le57, subtraction gives \delta(b)<10 and 48+\delta(b)=b.\mathit{val}. Every decoded member comes from such a byte; mapping the latter equality proves the reencoding assertion. For the second assertion, map natural subtraction by 48 over the assumed value-map equality. On an encoded natural number d, (48+d)-48=d, without any bound on d. Composition of maps therefore gives \operatorname{map}(\delta,B)=D. Taking lengths yields |B|=|D|. To prove fold transport, induct on B and generalize the accumulator. The empty fold is a on both sides. For a head b, both folds first update to H(a,\delta(b)); induction at that accumulator equates the remaining folds. Replace the decoded list by D. The arbitrary function H includes both ordinary decimal accumulation and the pre-update 2^{59} cutoff fold, so this argument does not change when updates stop.

Lean proof


  constructor
  · intro hd
    constructor
    · intro d hmem
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hmem
      have := hd b hb
      omega
    · simp only [List.map_map]
      apply List.map_congr_left
      intro b hb
      have := hd b hb
      dsimp
      omega
  · intro digits hmap
    have hdecode : bytes.map (fun b => b.val - 48) = digits := by
      have h := congrArg (List.map (fun n : Nat => n - 48)) hmap
      simpa [List.map_map, Function.comp_def] using h
    refine ⟨hdecode, ?_, ?_⟩
    · have h := congrArg List.length hdecode
      simpa using h
    · intro step initial
      rw [← hdecode]
      exact (List.foldl_map (f := fun b : Std.U8 => b.val - 48)
        (g := step) (l := bytes) (init := initial)).symm

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: P435_T1_DigitDecoding]
theorem P435_T1_DigitDecoding (bytes : List Std.U8) :
    ((forall b, b ∈ bytes -> 48 <= b.val /\ b.val <= 57) ->
      (forall d, d ∈ bytes.map (fun b => b.val - 48) -> d < 10) /\
      bytes.map (fun b => b.val) =
        (bytes.map (fun b => b.val - 48)).map (fun d => 48 + d)) /\
    (forall digits : List Nat,
      bytes.map (fun b => b.val) = digits.map (fun d => 48 + d) ->
      bytes.map (fun b => b.val - 48) = digits /\
      bytes.length = digits.length /\
      (forall (step : Nat -> Nat -> Nat) (initial : Nat),
        bytes.foldl (fun a b => step a (b.val - 48)) initial =
          digits.foldl step initial)) := by
-- BODY
  constructor
  · intro hd
    constructor
    · intro d hmem
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hmem
      have := hd b hb
      omega
    · simp only [List.map_map]
      apply List.map_congr_left
      intro b hb
      have := hd b hb
      dsimp
      omega
  · intro digits hmap
    have hdecode : bytes.map (fun b => b.val - 48) = digits := by
      have h := congrArg (List.map (fun n : Nat => n - 48)) hmap
      simpa [List.map_map, Function.comp_def] using h
    refine ⟨hdecode, ?_, ?_⟩
    · have h := congrArg List.length hdecode
      simpa using h
    · intro step initial
      rw [← hdecode]
      exact (List.foldl_map (f := fun b : Std.U8 => b.val - 48)
        (g := step) (l := bytes) (init := initial)).symm

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": "P435_T1_DigitDecoding",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "1e61d2955ffed0b238f7fb268837b80deb37a4bc3e22233e9fa5277453ad2cc3",
  "active_statement_hash": "273b057d0dda5ef51d651c92d977f4ca92f99d61e9de8475f7e7bcdc68fc5dc6",
  "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-92",
  "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:128ce7f307121cd9e110d5fb431454b55f09fe43abee8c775f1154f4d8c66000",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}