Statement
This explanation was written alongside the formalization to describe the statement and its reasoning.
Lemma · Decimal lexicographic comparison as a scaled integer comparison
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
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.
Uses
Used by
Inspect related source (44)
- from_u64 · dec2flt/mod.rs:263 Program Rust
- FloatErrorKind · num/mod.rs:18 Program Rust
- ParseFloatError · num/mod.rs:13 Program Rust
More program dependencies (11)
- Dec2FltAssemblyTrace · dec2flt/mod.rs:329 Program Rust
- Dec2FltTrace · dec2flt/mod.rs:295 Program Rust
- Lemire · dec2flt/mod.rs:115 Program Rust
- BiasedFp · dec2flt/common.rs:52 Program Rust
- ByteSlice · dec2flt/common.rs:4 Program Rust
- Decimal · dec2flt/decimal.rs:32 Program Rust
- DecimalSeq · dec2flt/decimal_seq.rs:21 Program Rust
- CastInto · traits.rs:18 Program Rust
- Float · traits.rs:74 Program Rust
- FloatExt · traits.rs:170 Program Rust
- Int · traits.rs:27 Program Rust
Standard-library dependencies (30)
- clone · core/src/clone.rs:698 Rust library
- div · core/src/ops/arith.rs:515 Rust library
- mul · core/src/ops/arith.rs:350 Rust library
- neg · core/src/ops/arith.rs:719 Rust library
- bitand · core/src/ops/bit.rs:178 Rust library
- bitor · core/src/ops/bit.rs:282 Rust library
- shl · core/src/ops/bit.rs:489 Rust library
- shr · core/src/ops/bit.rs:612 Rust library
- EXPONENT_MASK · core/src/num/f64.rs:615 Rust library
- INFINITY · core/src/num/f64.rs:512 Rust library
- MANTISSA_DIGITS · core/src/num/f64.rs:412 Rust library
- MANTISSA_MASK · core/src/num/f64.rs:637 Rust library
- NAN · core/src/num/f64.rs:509 Rust library
- NEG_INFINITY · core/src/num/f64.rs:515 Rust library
- classify · core/src/num/f64.rs:782 Rust library
- from_bits · core/src/num/f64.rs:1269 Rust library
- to_bits · core/src/num/f64.rs:1223 Rust library
- is_ascii_digit · core/src/num/mod.rs:958 Rust library
- from_residual · core/src/option.rs:2793 Rust library
- branch · core/src/option.rs:2779 Rust library
- first · core/src/slice/mod.rs:155 Rust library
- split_first · core/src/slice/mod.rs:198 Rust library
- as_bytes · core/src/str/mod.rs:498 Rust library
- FpCategory · core/src/num/mod.rs:1522 Rust library
- Div · core/src/ops/arith.rs:460 Rust library
- Mul · core/src/ops/arith.rs:322 Rust library
- Neg · core/src/ops/arith.rs:690 Rust library
- BitOr · core/src/ops/bit.rs:254 Rust library
- Shl · core/src/ops/bit.rs:461 Rust library
- Shr · core/src/ops/bit.rs:584 Rust library
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
- Classical.choice
- Quot.sound
- propext
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"
}