DecimalDigitLexScale

Lean checked

Statement

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

Lemma · Decimal lexicographic comparison as a scaled integer comparison

Let x and y be finite lists of natural-number decimal digits. Assume every entry of both lists is less than ten and, when y is nonempty, its last entry is nonzero. If E(z) denotes the base-ten left fold of a list z, then
\operatorname{Lex}(\operatorname{take}_{|y|}(x),y) \quad\Longleftrightarrow\quad E(x)10^{|y|}<E(y)10^{|x|}.
Here \operatorname{Lex} uses the strict order on natural numbers.

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: DecimalDigitLexScale]
theorem DecimalDigitLexScale (xs ys : List Nat)
    (hxs : forall x, x ∈ xs -> x < 10)
    (hys : forall y, y ∈ ys -> y < 10)
    (hylast : forall hne : ys ≠ [], ys.getLast hne ≠ 0) :
    List.Lex (fun a b : Nat => a < b) (xs.take ys.length) ys ↔
      xs.foldl (fun a z => 10 * a + z) 0 * 10 ^ ys.length <
      ys.foldl (fun a z => 10 * a + z) 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 a list z and initial accumulator a, induction on z gives
\operatorname{fold}_{10}(z,a)=a10^{|z|}+E(z). \tag{1}
The same induction, using that each digit is below ten, gives E(z)<10^{|z|}. If z is nonempty and its last digit is nonzero, a second induction using (1) gives E(z)>0: for a singleton this is exactly the last-digit hypothesis, and after a nonempty tail it follows from the positive value of that tail.
We prove the displayed equivalence by induction on y. For y=[], both sides are false. If y=b::y' and x=[], the lexicographic relation holds by the empty-list rule and the numerical inequality holds because the preceding positivity result gives E(y)>0.
It remains to take x=a::x'. Formula (1) writes E(x)=a10^{|x'|}+E(x') and E(y)=b10^{|y'|}+E(y'), with each remainder strictly below its corresponding power of ten. If a<b, these strict remainder bounds show the scaled value of x is smaller, and the lexicographic relation holds at its first digit. If a=b, cancellation of the equal leading terms reduces the numerical inequality exactly to the induction hypothesis for x' and y'; the nonzero-last-digit hypothesis passes to y' whenever it is nonempty. If b<a, the same strict bounds make the scaled inequality impossible. A lexicographic proof in this last case also cannot start at the heads, and if it continues to the tails the induction hypothesis contradicts the inequality forced by b<a. These three cases exhaust the natural-number order and prove the equivalence.

Lean proof


  let eval := fun zs : List Nat => zs.foldl (fun a z => 10 * a + z) 0
  have hacc : forall (zs : List Nat) (a : Nat),
      zs.foldl (fun n z => 10 * n + z) a =
        a * 10 ^ zs.length + eval zs := by
    intro zs
    induction zs with
    | nil => simp [eval]
    | cons z zs ih =>
      intro a
      simp only [List.foldl_cons, List.length_cons]
      rw [ih (10 * a + z)]
      dsimp only [eval]
      simp only [List.foldl_cons]
      simp only [mul_zero, zero_add]
      rw [ih z]
      simp only [Nat.pow_succ]
      simp only [eval]
      ring
  have heval_cons : forall (a : Nat) (zs : List Nat),
      eval (a :: zs) = a * 10 ^ zs.length + eval zs := by
    intro a zs
    simp only [eval, List.foldl_cons]
    simpa using hacc zs a
  have hbound : forall (zs : List Nat),
      (forall z, z ∈ zs -> z < 10) -> eval zs < 10 ^ zs.length := by
    intro zs hz
    induction zs with
    | nil => simp [eval]
    | cons z zs ih =>
      rw [heval_cons]
      have hz0 := hz z (by simp)
      have hzt : forall w, w ∈ zs -> w < 10 := by
        intro w hw
        exact hz w (by simp [hw])
      have hit := ih hzt
      simp only [List.length_cons, Nat.pow_succ]
      nlinarith
  have hpos : forall (zs : List Nat),
      (forall z, z ∈ zs -> z < 10) ->
      (forall hne : zs ≠ [], zs.getLast hne ≠ 0) ->
      zs ≠ [] -> 0 < eval zs := by
    intro zs hz hlast hne
    induction zs with
    | nil => contradiction
    | cons z zs ih =>
      rw [heval_cons]
      by_cases ht : zs = []
      · subst zs
        simp only [List.length_nil, pow_zero, mul_one, eval,
          List.foldl_nil, add_zero]
        have hzlast := hlast (by simp)
        simpa using Nat.pos_of_ne_zero hzlast
      · have hzt : forall w, w ∈ zs -> w < 10 := by
          intro w hw
          exact hz w (by simp [hw])
        have hlastt : forall hne : zs ≠ [], zs.getLast hne ≠ 0 := by
          intro hne
          simpa [List.getLast_cons hne] using hlast (by simp)
        have := ih hzt hlastt ht
        omega
  change List.Lex (fun a b : Nat => a < b) (xs.take ys.length) ys ↔
    eval xs * 10 ^ ys.length < eval ys * 10 ^ xs.length
  induction ys generalizing xs with
  | nil => simp [eval]
  | cons b bs ih =>
    cases xs with
    | nil =>
      simp only [List.length_cons, List.take_nil, List.length_nil, pow_zero,
        mul_one]
      simp only [eval, List.foldl_nil, zero_mul]
      constructor
      · intro _
        exact hpos (b :: bs) hys hylast (by simp)
      · intro _
        exact List.Lex.nil
    | cons a xsTail =>
      have has : forall x, x ∈ xsTail -> x < 10 := by
        intro x hx
        exact hxs x (by simp [hx])
      have hbs : forall y, y ∈ bs -> y < 10 := by
        intro y hy
        exact hys y (by simp [hy])
      have habound := hbound xsTail has
      have hbbound := hbound bs hbs
      rw [heval_cons, heval_cons]
      simp only [List.length_cons, List.take_succ_cons, Nat.pow_succ]
      rcases lt_trichotomy a b with hab | hab | hab
      · constructor
        · intro _
          have hp : 0 < 10 ^ xsTail.length * 10 ^ bs.length * 10 := by positivity
          calc
            (a * 10 ^ xsTail.length + eval xsTail) * (10 ^ bs.length * 10)
                < (a + 1) * (10 ^ xsTail.length * 10 ^ bs.length * 10) := by
                    nlinarith
            _ ≤ b * (10 ^ xsTail.length * 10 ^ bs.length * 10) := by
                    exact Nat.mul_le_mul_right _ hab
            _ ≤ (b * 10 ^ bs.length + eval bs) *
                (10 ^ xsTail.length * 10) := by
                    nlinarith
        · intro _
          exact List.Lex.rel hab
      · subst b
        have hlastbs : forall hne : bs ≠ [], bs.getLast hne ≠ 0 := by
          intro hne
          simpa [List.getLast_cons hne] using hylast (by simp)
        rw [List.lex_cons_iff]
        have hi := ih xsTail has hbs hlastbs
        rw [hi]
        constructor <;> intro ht <;> nlinarith
      · constructor
        · intro hlex
          cases hlex with
          | rel h => omega
          | cons h =>
            have hlastbs : forall hne : bs ≠ [], bs.getLast hne ≠ 0 := by
              intro hne
              simpa [List.getLast_cons hne] using hylast (by simp)
            have htail := (ih xsTail has hbs hlastbs).mp h
            nlinarith
        · intro hnum
          have hp : 0 < 10 ^ xsTail.length * 10 ^ bs.length * 10 := by positivity
          exfalso
          nlinarith

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: DecimalDigitLexScale]
theorem DecimalDigitLexScale (xs ys : List Nat)
    (hxs : forall x, x ∈ xs -> x < 10)
    (hys : forall y, y ∈ ys -> y < 10)
    (hylast : forall hne : ys ≠ [], ys.getLast hne ≠ 0) :
    List.Lex (fun a b : Nat => a < b) (xs.take ys.length) ys ↔
      xs.foldl (fun a z => 10 * a + z) 0 * 10 ^ ys.length <
      ys.foldl (fun a z => 10 * a + z) 0 * 10 ^ xs.length := by
-- BODY
  let eval := fun zs : List Nat => zs.foldl (fun a z => 10 * a + z) 0
  have hacc : forall (zs : List Nat) (a : Nat),
      zs.foldl (fun n z => 10 * n + z) a =
        a * 10 ^ zs.length + eval zs := by
    intro zs
    induction zs with
    | nil => simp [eval]
    | cons z zs ih =>
      intro a
      simp only [List.foldl_cons, List.length_cons]
      rw [ih (10 * a + z)]
      dsimp only [eval]
      simp only [List.foldl_cons]
      simp only [mul_zero, zero_add]
      rw [ih z]
      simp only [Nat.pow_succ]
      simp only [eval]
      ring
  have heval_cons : forall (a : Nat) (zs : List Nat),
      eval (a :: zs) = a * 10 ^ zs.length + eval zs := by
    intro a zs
    simp only [eval, List.foldl_cons]
    simpa using hacc zs a
  have hbound : forall (zs : List Nat),
      (forall z, z ∈ zs -> z < 10) -> eval zs < 10 ^ zs.length := by
    intro zs hz
    induction zs with
    | nil => simp [eval]
    | cons z zs ih =>
      rw [heval_cons]
      have hz0 := hz z (by simp)
      have hzt : forall w, w ∈ zs -> w < 10 := by
        intro w hw
        exact hz w (by simp [hw])
      have hit := ih hzt
      simp only [List.length_cons, Nat.pow_succ]
      nlinarith
  have hpos : forall (zs : List Nat),
      (forall z, z ∈ zs -> z < 10) ->
      (forall hne : zs ≠ [], zs.getLast hne ≠ 0) ->
      zs ≠ [] -> 0 < eval zs := by
    intro zs hz hlast hne
    induction zs with
    | nil => contradiction
    | cons z zs ih =>
      rw [heval_cons]
      by_cases ht : zs = []
      · subst zs
        simp only [List.length_nil, pow_zero, mul_one, eval,
          List.foldl_nil, add_zero]
        have hzlast := hlast (by simp)
        simpa using Nat.pos_of_ne_zero hzlast
      · have hzt : forall w, w ∈ zs -> w < 10 := by
          intro w hw
          exact hz w (by simp [hw])
        have hlastt : forall hne : zs ≠ [], zs.getLast hne ≠ 0 := by
          intro hne
          simpa [List.getLast_cons hne] using hlast (by simp)
        have := ih hzt hlastt ht
        omega
  change List.Lex (fun a b : Nat => a < b) (xs.take ys.length) ys ↔
    eval xs * 10 ^ ys.length < eval ys * 10 ^ xs.length
  induction ys generalizing xs with
  | nil => simp [eval]
  | cons b bs ih =>
    cases xs with
    | nil =>
      simp only [List.length_cons, List.take_nil, List.length_nil, pow_zero,
        mul_one]
      simp only [eval, List.foldl_nil, zero_mul]
      constructor
      · intro _
        exact hpos (b :: bs) hys hylast (by simp)
      · intro _
        exact List.Lex.nil
    | cons a xsTail =>
      have has : forall x, x ∈ xsTail -> x < 10 := by
        intro x hx
        exact hxs x (by simp [hx])
      have hbs : forall y, y ∈ bs -> y < 10 := by
        intro y hy
        exact hys y (by simp [hy])
      have habound := hbound xsTail has
      have hbbound := hbound bs hbs
      rw [heval_cons, heval_cons]
      simp only [List.length_cons, List.take_succ_cons, Nat.pow_succ]
      rcases lt_trichotomy a b with hab | hab | hab
      · constructor
        · intro _
          have hp : 0 < 10 ^ xsTail.length * 10 ^ bs.length * 10 := by positivity
          calc
            (a * 10 ^ xsTail.length + eval xsTail) * (10 ^ bs.length * 10)
                < (a + 1) * (10 ^ xsTail.length * 10 ^ bs.length * 10) := by
                    nlinarith
            _ ≤ b * (10 ^ xsTail.length * 10 ^ bs.length * 10) := by
                    exact Nat.mul_le_mul_right _ hab
            _ ≤ (b * 10 ^ bs.length + eval bs) *
                (10 ^ xsTail.length * 10) := by
                    nlinarith
        · intro _
          exact List.Lex.rel hab
      · subst b
        have hlastbs : forall hne : bs ≠ [], bs.getLast hne ≠ 0 := by
          intro hne
          simpa [List.getLast_cons hne] using hylast (by simp)
        rw [List.lex_cons_iff]
        have hi := ih xsTail has hbs hlastbs
        rw [hi]
        constructor <;> intro ht <;> nlinarith
      · constructor
        · intro hlex
          cases hlex with
          | rel h => omega
          | cons h =>
            have hlastbs : forall hne : bs ≠ [], bs.getLast hne ≠ 0 := by
              intro hne
              simpa [List.getLast_cons hne] using hylast (by simp)
            have htail := (ih xsTail has hbs hlastbs).mp h
            nlinarith
        · intro hnum
          have hp : 0 < 10 ^ xsTail.length * 10 ^ bs.length * 10 := by positivity
          exfalso
          nlinarith

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": "DecimalDigitLexScale",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "173a6fd674bc0386793023b51c64e5ec9a0ecd901df8e097961f74f05b17140a",
  "active_statement_hash": "461f16b435e0379ef8c6491b2e5d8c573bd103d7ff70d2123ae7a9148c8a07ad",
  "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": "worker-result-local-closure-issuance-cycle-154",
  "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:374dcfcc3ab1506225e4e15f7e88cb6be545f19d021634fd28d43394d10e5237",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}