DecimalRightShiftU64DyadicBridge

Lean checked

Statement

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

Helper · U64 bridge for decimal dyadic right shift

Let 0\le k\le60, let x be a U64 word, and put B=2^k. Suppose the modeled word operations successfully compute
p=1\mathbin{\mathtt{<<}}k,\qquad m=p-1,\qquad q=x\mathbin{\mathtt{>>}}k,\qquad e=x\mathbin{\mathtt{\&}}m.
Then
p=B,\quad m=B-1,\quad q=\lfloor x/B\rfloor,\quad e=x\bmod B,
and
x=qB+e,\qquad 0\le e<B,qquad B\le2^{60}.
Moreover, for every natural a<10,
10e+a<2^{64}. \tag{1}

Lean statement

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

import Tablet.ModelU64BitAnd
import Tablet.ModelU64Shl
import Tablet.ModelU64Shr

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalRightShiftU64DyadicBridge]
theorem DecimalRightShiftU64DyadicBridge
    (k : Std.Usize) (x pow mask quot rem : Std.U64)
    (hk : k.val <= 60)
    (hpow : 1#u64 <<< k = ok pow)
    (hmask : pow - 1#u64 = ok mask)
    (hquot : x >>> k = ok quot)
    (hrem : lift (x &&& mask) = ok rem) :
    let B := 2 ^ k.val
    pow.val = B /\
    mask.val = B - 1 /\
    quot.val = x.val / B /\
    rem.val = x.val % B /\
    x.val = quot.val * B + rem.val /\
    rem.val < B /\
    B <= 2 ^ 60 /\
    (forall a : Nat, a < 10 -> 10 * rem.val + a < 2 ^ 64) := 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

Because k\le60<64, the exact bounded left-shift operation represented by ModelU64Shl shifts the word one without discarding any set bit. Its natural value is therefore 2^k=B, which is strictly below 2^{64}. The checked subtraction by one is consequently defined and has natural value B-1.
The exact bounded right-shift operation represented by ModelU64Shr shifts a U64 natural value right by k<64 bits, hence returns q=\lfloor x/2^k\rfloor=\lfloor x/B\rfloor. The bounded bitwise conjunction represented by ModelU64BitAnd is exact on every pair of U64 words. The low-k-bit mask identity
x\mathbin{\mathtt{\&}}(2^k-1)=x\bmod2^k
therefore gives e=x\bmod B. Euclidean division by the positive number B now gives x=qB+e and 0\le e<B.
Monotonicity of powers gives B=2^k\le2^{60}. If a<10, then
10e+a<10B+10\le10\cdot2^{60}+10<2^{64},
where the last inequality is direct integer arithmetic. This proves (1), uniformly in the U64 input x and the next decimal digit a.

Lean proof


  dsimp only
  let B := 2 ^ k.val
  have hk64 : k.val < 64 := by omega
  have hBpos : 0 < B := by simp [B]
  have hBle : B <= 2 ^ 60 := Nat.pow_le_pow_right (by omega) hk
  have hpSpec := UScalar.ShiftLeft_spec (1#u64) k (2 ^ 64)
    (by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq])
  rw [hpow] at hpSpec
  have hpLt : 2 ^ k.val < 2 ^ 64 := Nat.pow_lt_pow_right (by omega) hk64
  simp only [WP.spec_ok] at hpSpec
  norm_num [Nat.shiftLeft_eq] at hpSpec
  have h64 : 18446744073709551616 = 2 ^ 64 := by norm_num
  rw [h64, Nat.mod_eq_of_lt hpLt] at hpSpec
  have hpVal : pow.val = B := by simpa [B] using hpSpec.1
  have hOneLe : (1#u64).val <= pow.val := by rw [hpVal]; simp [B]
  have hmSpec := UScalar.sub_spec (x := pow) (y := 1#u64) hOneLe
  rw [hmask] at hmSpec
  have hmVal : mask.val = B - 1 := by
    simpa [WP.spec_ok, hpVal] using hmSpec.1
  have hqSpec := UScalar.ShiftRight_spec x k (by
    norm_num [U64.numBits]
    exact hk64)
  rw [hquot] at hqSpec
  have hqVal : quot.val = x.val / B := by
    simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqSpec.1
  simp only [lift] at hrem
  cases hrem
  have hrVal : (x &&& mask).val = x.val % B := by
    rw [UScalar.val_and, hmVal]
    exact Nat.and_two_pow_sub_one_eq_mod x.val k.val
  have heuc : x.val = quot.val * B + (x &&& mask).val := by
    rw [hqVal, hrVal]
    simpa [Nat.mul_comm] using (Nat.div_add_mod x.val B).symm
  have hrLt : (x &&& mask).val < B := by
    rw [hrVal]
    exact Nat.mod_lt _ hBpos
  refine ⟨hpVal, hmVal, hqVal, hrVal, heuc, hrLt, hBle, ?_⟩
  intro a ha
  calc
    10 * (x &&& mask).val + a < 10 * B + 10 := by omega
    _ <= 10 * 2 ^ 60 + 10 := by omega
    _ < 2 ^ 64 := by norm_num

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ModelU64BitAnd
import Tablet.ModelU64Shl
import Tablet.ModelU64Shr

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalRightShiftU64DyadicBridge]
theorem DecimalRightShiftU64DyadicBridge
    (k : Std.Usize) (x pow mask quot rem : Std.U64)
    (hk : k.val <= 60)
    (hpow : 1#u64 <<< k = ok pow)
    (hmask : pow - 1#u64 = ok mask)
    (hquot : x >>> k = ok quot)
    (hrem : lift (x &&& mask) = ok rem) :
    let B := 2 ^ k.val
    pow.val = B /\
    mask.val = B - 1 /\
    quot.val = x.val / B /\
    rem.val = x.val % B /\
    x.val = quot.val * B + rem.val /\
    rem.val < B /\
    B <= 2 ^ 60 /\
    (forall a : Nat, a < 10 -> 10 * rem.val + a < 2 ^ 64) := by
-- BODY
  dsimp only
  let B := 2 ^ k.val
  have hk64 : k.val < 64 := by omega
  have hBpos : 0 < B := by simp [B]
  have hBle : B <= 2 ^ 60 := Nat.pow_le_pow_right (by omega) hk
  have hpSpec := UScalar.ShiftLeft_spec (1#u64) k (2 ^ 64)
    (by norm_num [U64.numBits]; omega) (by norm_num [U64.size_eq])
  rw [hpow] at hpSpec
  have hpLt : 2 ^ k.val < 2 ^ 64 := Nat.pow_lt_pow_right (by omega) hk64
  simp only [WP.spec_ok] at hpSpec
  norm_num [Nat.shiftLeft_eq] at hpSpec
  have h64 : 18446744073709551616 = 2 ^ 64 := by norm_num
  rw [h64, Nat.mod_eq_of_lt hpLt] at hpSpec
  have hpVal : pow.val = B := by simpa [B] using hpSpec.1
  have hOneLe : (1#u64).val <= pow.val := by rw [hpVal]; simp [B]
  have hmSpec := UScalar.sub_spec (x := pow) (y := 1#u64) hOneLe
  rw [hmask] at hmSpec
  have hmVal : mask.val = B - 1 := by
    simpa [WP.spec_ok, hpVal] using hmSpec.1
  have hqSpec := UScalar.ShiftRight_spec x k (by
    norm_num [U64.numBits]
    exact hk64)
  rw [hquot] at hqSpec
  have hqVal : quot.val = x.val / B := by
    simpa [WP.spec_ok, Nat.shiftRight_eq_div_pow, B] using hqSpec.1
  simp only [lift] at hrem
  cases hrem
  have hrVal : (x &&& mask).val = x.val % B := by
    rw [UScalar.val_and, hmVal]
    exact Nat.and_two_pow_sub_one_eq_mod x.val k.val
  have heuc : x.val = quot.val * B + (x &&& mask).val := by
    rw [hqVal, hrVal]
    simpa [Nat.mul_comm] using (Nat.div_add_mod x.val B).symm
  have hrLt : (x &&& mask).val < B := by
    rw [hrVal]
    exact Nat.mod_lt _ hBpos
  refine ⟨hpVal, hmVal, hqVal, hrVal, heuc, hrLt, hBle, ?_⟩
  intro a ha
  calc
    10 * (x &&& mask).val + a < 10 * B + 10 := by omega
    _ <= 10 * 2 ^ 60 + 10 := by omega
    _ < 2 ^ 64 := by norm_num

end dec2flt_model

Proof structure

Follow the declared dependencies to read the supporting statements and proofs.

Explore this dependency tree

Inspect related source (3)

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": "DecimalRightShiftU64DyadicBridge",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "9ebfefa0c7be15761e734029cb1a7f9092866ce177ecd1b77778595f24db3d26",
  "active_statement_hash": "71c2905e521b62549dccd661c95b5173bf59a9ada47b6f3090954058cc1167ad",
  "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-159",
  "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:241f67c006b7d6505d85e92920d187e947ac57cc955cfc4291bdfdb1cd69725a",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}