IntegerPowerTableCertificate

Lean checked

Statement

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

Lemma

Let q be a signed 64-bit integer with -342\le q\le308. Casting q to signed 32 bits succeeds. If
e_q=\left\lfloor\frac{217706q}{65536}\right\rfloor-q, \qquad T_q=2^{127-e_q}5^q,
then the extracted fixed-point exponent routine succeeds and returns q+e_q+63. Moreover the checked difference q-(-342), its cast to a 64-bit usize, and the corresponding access to the extracted power-of-five table all succeed. Writing the accessed pair as (a_q,b_q) and D_q=a_q2^{64}+b_q, one has
2^{127}\le T_q<2^{128}. \tag{1}
If q<0, there are positive integers n,z_n with q=-n such that
2^{z_n-1}<5^n<2^{z_n},\qquad e_q=-z_n,\qquad T_q=\frac{2^{z_n+127}}{5^n}. \tag{2}
For the small nonnegative ranges needed below,
0\le q\le55\Longrightarrow D_q=T_q,qquad 0\le q\le23\Longrightarrow b_q=0. \tag{3}
Finally,
\begin{array}{ll} D_q\le T_q<D_q+1,&q<-27\text{ or }q\ge0,\\ D_q-1<T_q<D_q,&-27\le q<0. \end{array}

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 Tablet.IntegerPowerFiniteCertificate
import Tablet.IntegerPowerRealBridge
import Tablet.num_imp_dec2flt_lemire_power
import Tablet.num_imp_dec2flt_table_POWER_OF_FIVE_128

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerPowerTableCertificate]
theorem IntegerPowerTableCertificate :
    ∀ q : Std.I64, -342 ≤ q.val → q.val ≤ 308 →
      ∃ (q32 p : Std.I32) (delta : Std.I64) (index : Std.Usize)
          (a b : Std.U64),
        lift (IScalar.cast .I32 q) = ok q32 ∧
        num.imp.dec2flt.lemire.power q32 = ok p ∧
        p.val = 217706 * q.val / 65536 + 63 ∧
        q - (-342)#i64 = ok delta ∧
        lift (IScalar.hcast .Usize delta) = ok index ∧
        Array.index_usize num.imp.dec2flt.table.POWER_OF_FIVE_128 index =
          ok (a, b) ∧
        let e : Int := 217706 * q.val / 65536 - q.val
        let D : Nat := a.val * 2 ^ 64 + b.val
        let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val
        (((2 : Real) ^ (127 : Nat) ≤ T ∧
            T < (2 : Real) ^ (128 : Nat)) ∧
          (q.val < 0 →
            ∃ n z : Nat,
              q.val = -(n : Int) ∧
              1 ≤ n ∧
              1 ≤ z ∧
              (2 : Nat) ^ (z - 1) < 5 ^ n ∧
              5 ^ n < 2 ^ z ∧
              e = -(z : Int) ∧
              T = (2 : Real) ^ (z + 127) / (5 : Real) ^ n) ∧
          ((0 ≤ q.val ∧ q.val ≤ 55) → (D : Real) = T) ∧
          ((0 ≤ q.val ∧ q.val ≤ 23) → b.val = 0) ∧
          ((q.val < -27 ∨ 0 ≤ q.val) →
            (D : Real) ≤ T ∧ T < (D : Real) + 1) ∧
          ((-27 ≤ q.val ∧ q.val < 0) →
            (D : Real) - 1 < T ∧ T < D)) := 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

Put P_0=1 and P_{n+1}=5P_n, so induction gives P_n=5^n. For n>0, let z_n be the unique positive integer for which
2^{z_n-1}<P_n<2^{z_n}. \tag{4}
Both inequalities are strict because the odd integer 5^n>1 is not a power of two. The following recurrence is a finite certificate for the 651 table entries. Starting from P_0, multiply by five successively to obtain P_1,\ldots,P_{342} for the negative indices and P_1,\ldots,P_{308} for the nonnegative indices. At every multiplication, compare with consecutive powers of two to obtain the unique z_n in (4). Define
D_q=\begin{cases} \left\lfloor \dfrac{\left\lfloor2^{2z_{-q}+128}/P_{-q}\right\rfloor+1} {2^{z_{-q}+1}} \right\rfloor,&-342\le q<-27,\\[5pt] \left\lfloor2^{z_{-q}+127}/P_{-q}\right\rfloor+1, &-27\le q<0,\\[5pt] P_q2^{127-(P_q.\mathit{bitLength}-1)}, &q\ge0\text{ and }P_q<2^{128},\\[3pt] \left\lfloor P_q/2^{(P_q.\mathit{bitLength}-1)-127}\right\rfloor, &q\ge0\text{ and }P_q\ge2^{128}. \end{cases} \tag{5}
All operations in this certificate are multiplication by five, comparison, shift, and Euclidean division of nonnegative integers. Dividing each D_q by B=2^{64} gives a quotient and remainder. Direct recurrence evaluation gives exactly the pair at index q+342 of num imp dec2flt table POWER OF FIVE 128. The endpoints and origin, which also check the alignment, are
\begin{array}{c|c} q&(a_q,b_q)\\ \hline -342&(17218479456385750618,1242899115359157055)\\ 0&(9223372036854775808,0)\\ 308&(10261342003245940623,6273243709394548296). \end{array}
There are exactly 342+1+308=651 indices, so the two stated recurrence passes check every literal and establish D_q=a_qB+b_q throughout the specified interval.
We next prove the directed bounds, rather than merely trusting the table generation rule. For q=-n<-27, set K=2^{z_n+1} and divide N=2^{z_n+127} as N=kP_n+r. Oddness gives 1\le r<P_n. The numerator in the first line of (5) is
Kk+\left\lfloor Kr/P_n\right\rfloor+1.
By (4), P_n<K/2, hence K/P_n>2. Thus 0\le\lfloor Kr/P_n\rfloor and
\lfloor Kr/P_n\rfloor+1 \le Kr/P_n+1 \le K-K/P_n+1<K.
The outer quotient by K is therefore k. Since 0<r<P_n, this says D_q=\lfloor N/P_n\rfloor<T_q=N/P_n<D_q+1. For -27\le q=-n<0, the same nondivisibility gives D_q=\lfloor N/P_n\rfloor+1=\lceil T_q\rceil, whence D_q-1<T_q<D_q. For q\ge0, the last two lines of (5) are respectively the exact normalized integer and its floor, giving D_q\le T_q<D_q+1.
The same 651-step recurrence checks
65536(q+e_q)\le217706q<65536(q+e_q+1) \tag{6}
at every index. For q=-n<0 the candidate used in this check is e_q=-z_n, with z_n from (4). For q\ge0 it is the unique integer e_q satisfying
2^{e_q}\le P_q<2^{e_q+1}. \tag{7}
At each of the 651 indices, direct integer multiplication verifies the left comparison in (6) for the recorded q+e_q and the right comparison for its successor q+e_q+1. Thus Euclidean division by the positive number 65536 gives e_q=\lfloor217706q/65536\rfloor-q, including the asserted equality e_q=-z_n at negative indices.
These same inequalities give all of the additional exported conclusions. If q=-n<0, (4), e_q=-z_n, and the definition of T_q give
T_q=2^{z_n+127}/5^n,\qquad 2^{127}<T_q<2^{128}.
If q\ge0, (7) and T_q=2^{127-e_q}P_q instead give 2^{127}\le T_q<2^{128}. This proves (1) and (2). For 0\le q\le55, direct integer comparison gives
P_q\le5^{55}=277555756156289135105907917022705078125 <340282366920938463463374607431768211456=2^{128},
so the third line of (5) applies and is exactly D_q=P_q2^{127-e_q}=T_q. For 0\le q\le23, one has P_q\le5^{23}=11920928955078125<18014398509481984=2^{54}; (7) therefore gives e_q\le53. Hence D_q=P_q2^{127-e_q} is divisible by 2^{74} and in particular by 2^{64}. Since D_q=a_q2^{64}+b_q with 0\le b_q<2^{64}, uniqueness of Euclidean remainder gives b_q=0. This proves (3).
Finally unfold num imp dec2flt lemire power. The checked constant addition is 152170+65536=217706. On [-342,308] its product with q is between -74455452 and 67053448, well inside signed 32-bit range, so the wrapping multiplication has the ordinary integer value. Arithmetic right shift by 16 is division with floor by 65536, and the final addition of 63 is also in range. Thus it returns q+e_q+63 normally. The cast of q is in range. Also 0\le q+342\le650, so the checked subtraction, the 64-bit usize cast, and access to the length-651 table all succeed. This establishes every success equation, the normalization and negative-index representation, both small-range exactness clauses, and both directed intervals in the statement.

Lean proof


  obtain ⟨hopcert, hcert, hsmallE⟩ := IntegerPowerFiniteCertificate
  intro q hlow hhigh
  have hc := IScalar.cast_inBounds_spec .I32 q (by scalar_tac)
  obtain ⟨q32, hq32, hq32val⟩ := Aeneas.Std.WP.spec_imp_exists hc

  have hdwp := IScalar.sub_spec (x := q) (y := (-342)#i64)
      (by scalar_tac) (by scalar_tac)
  obtain ⟨delta, hdelta, hdval⟩ := Aeneas.Std.WP.spec_imp_exists hdwp
  have hdval' : delta.val = q.val + 342 := by simpa using hdval
  have hiwp := IScalar.hcast_inBounds_spec .Usize delta (by
    rw [hdval']
    constructor <;> scalar_tac)
  obtain ⟨index, hindex, hival⟩ := Aeneas.Std.WP.spec_imp_exists hiwp

  let i : Fin 651 := ⟨(q.val + 342).toNat, by omega⟩
  let ab := num.imp.dec2flt.table.POWER_OF_FIVE_128.val.get
    ⟨i.val, by simpa using i.isLt⟩
  have hqeq : (i.val : Int) - 342 = q.val := by
    change (((q.val + 342).toNat : Nat) : Int) - 342 = q.val
    rw [Int.toNat_of_nonneg (by omega)]
    omega

  have hop := hopcert i
  dsimp only at hop
  let q32c : Std.I32 := IScalar.ofInt ((i.val : Int) - 342) (by
    change -(2 : Int) ^ 31 ≤ (i.val : Int) - 342 ∧
      (i.val : Int) - 342 ≤ (2 : Int) ^ 31 - 1
    omega)
  have hq32cval : q32c.val = q.val := by
    rw [← hqeq]
    simpa [q32c, IScalarTy.numBits] using
      (IScalar.ofInt_val_eq (ty := .I32) (x := (i.val : Int) - 342) (by omega))
  have hq32ceq : q32c = q32 := by
    apply IScalar.eq_of_val_eq
    omega
  change (match num.imp.dec2flt.lemire.power q32c with
    | ok y => decide (y.val = 217706 * ((i.val : Int) - 342) / 65536 + 63)
    | fail _ => false
    | div => false) = true at hop
  rw [hqeq, hq32ceq] at hop
  have hpExists : ∃ p : Std.I32,
      num.imp.dec2flt.lemire.power q32 = ok p ∧
      p.val = 217706 * q.val / 65536 + 63 := by
    generalize hpdef : num.imp.dec2flt.lemire.power q32 = pres at hop
    cases pres with
    | fail err => simp at hop
    | div => simp at hop
    | ok p =>
      simp at hop
      exact ⟨p, rfl, hop⟩
  obtain ⟨p, hp, hpval⟩ := hpExists

  have hiNat : index.val = i.val := by
    change index.val = (q.val + 342).toNat
    rw [← hdval', ← hival]
    simp
  have htable : Array.index_usize
      num.imp.dec2flt.table.POWER_OF_FIVE_128 index = ok ab := by
    simp only [Array.index_usize]
    simp [ab, hiNat]

  have hrow := hcert i
  dsimp only at hrow
  rw [hqeq] at hrow
  have hsmallERow := hsmallE i
  dsimp only at hsmallERow
  rw [hqeq] at hsmallERow

  refine ⟨q32, p, delta, index, ab.1, ab.2, hq32, hp, hpval,
    hdelta, hindex, htable, ?_⟩
  exact IntegerPowerRealBridge q.val
    (217706 * q.val / 65536 - q.val)
    (ab.1.val * 2 ^ 64 + ab.2.val) ab.2.val
    hrow.1 hrow.2 hsmallERow

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.IntegerPowerFiniteCertificate
import Tablet.IntegerPowerRealBridge
import Tablet.num_imp_dec2flt_lemire_power
import Tablet.num_imp_dec2flt_table_POWER_OF_FIVE_128

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: IntegerPowerTableCertificate]
theorem IntegerPowerTableCertificate :
    ∀ q : Std.I64, -342 ≤ q.val → q.val ≤ 308 →
      ∃ (q32 p : Std.I32) (delta : Std.I64) (index : Std.Usize)
          (a b : Std.U64),
        lift (IScalar.cast .I32 q) = ok q32 ∧
        num.imp.dec2flt.lemire.power q32 = ok p ∧
        p.val = 217706 * q.val / 65536 + 63 ∧
        q - (-342)#i64 = ok delta ∧
        lift (IScalar.hcast .Usize delta) = ok index ∧
        Array.index_usize num.imp.dec2flt.table.POWER_OF_FIVE_128 index =
          ok (a, b) ∧
        let e : Int := 217706 * q.val / 65536 - q.val
        let D : Nat := a.val * 2 ^ 64 + b.val
        let T : Real := (2 : Real) ^ (127 - e) * (5 : Real) ^ q.val
        (((2 : Real) ^ (127 : Nat) ≤ T ∧
            T < (2 : Real) ^ (128 : Nat)) ∧
          (q.val < 0 →
            ∃ n z : Nat,
              q.val = -(n : Int) ∧
              1 ≤ n ∧
              1 ≤ z ∧
              (2 : Nat) ^ (z - 1) < 5 ^ n ∧
              5 ^ n < 2 ^ z ∧
              e = -(z : Int) ∧
              T = (2 : Real) ^ (z + 127) / (5 : Real) ^ n) ∧
          ((0 ≤ q.val ∧ q.val ≤ 55) → (D : Real) = T) ∧
          ((0 ≤ q.val ∧ q.val ≤ 23) → b.val = 0) ∧
          ((q.val < -27 ∨ 0 ≤ q.val) →
            (D : Real) ≤ T ∧ T < (D : Real) + 1) ∧
          ((-27 ≤ q.val ∧ q.val < 0) →
            (D : Real) - 1 < T ∧ T < D)) := by
-- BODY
  obtain ⟨hopcert, hcert, hsmallE⟩ := IntegerPowerFiniteCertificate
  intro q hlow hhigh
  have hc := IScalar.cast_inBounds_spec .I32 q (by scalar_tac)
  obtain ⟨q32, hq32, hq32val⟩ := Aeneas.Std.WP.spec_imp_exists hc

  have hdwp := IScalar.sub_spec (x := q) (y := (-342)#i64)
      (by scalar_tac) (by scalar_tac)
  obtain ⟨delta, hdelta, hdval⟩ := Aeneas.Std.WP.spec_imp_exists hdwp
  have hdval' : delta.val = q.val + 342 := by simpa using hdval
  have hiwp := IScalar.hcast_inBounds_spec .Usize delta (by
    rw [hdval']
    constructor <;> scalar_tac)
  obtain ⟨index, hindex, hival⟩ := Aeneas.Std.WP.spec_imp_exists hiwp

  let i : Fin 651 := ⟨(q.val + 342).toNat, by omega⟩
  let ab := num.imp.dec2flt.table.POWER_OF_FIVE_128.val.get
    ⟨i.val, by simpa using i.isLt⟩
  have hqeq : (i.val : Int) - 342 = q.val := by
    change (((q.val + 342).toNat : Nat) : Int) - 342 = q.val
    rw [Int.toNat_of_nonneg (by omega)]
    omega

  have hop := hopcert i
  dsimp only at hop
  let q32c : Std.I32 := IScalar.ofInt ((i.val : Int) - 342) (by
    change -(2 : Int) ^ 31 ≤ (i.val : Int) - 342 ∧
      (i.val : Int) - 342 ≤ (2 : Int) ^ 31 - 1
    omega)
  have hq32cval : q32c.val = q.val := by
    rw [← hqeq]
    simpa [q32c, IScalarTy.numBits] using
      (IScalar.ofInt_val_eq (ty := .I32) (x := (i.val : Int) - 342) (by omega))
  have hq32ceq : q32c = q32 := by
    apply IScalar.eq_of_val_eq
    omega
  change (match num.imp.dec2flt.lemire.power q32c with
    | ok y => decide (y.val = 217706 * ((i.val : Int) - 342) / 65536 + 63)
    | fail _ => false
    | div => false) = true at hop
  rw [hqeq, hq32ceq] at hop
  have hpExists : ∃ p : Std.I32,
      num.imp.dec2flt.lemire.power q32 = ok p ∧
      p.val = 217706 * q.val / 65536 + 63 := by
    generalize hpdef : num.imp.dec2flt.lemire.power q32 = pres at hop
    cases pres with
    | fail err => simp at hop
    | div => simp at hop
    | ok p =>
      simp at hop
      exact ⟨p, rfl, hop⟩
  obtain ⟨p, hp, hpval⟩ := hpExists

  have hiNat : index.val = i.val := by
    change index.val = (q.val + 342).toNat
    rw [← hdval', ← hival]
    simp
  have htable : Array.index_usize
      num.imp.dec2flt.table.POWER_OF_FIVE_128 index = ok ab := by
    simp only [Array.index_usize]
    simp [ab, hiNat]

  have hrow := hcert i
  dsimp only at hrow
  rw [hqeq] at hrow
  have hsmallERow := hsmallE i
  dsimp only at hsmallERow
  rw [hqeq] at hsmallERow

  refine ⟨q32, p, delta, index, ab.1, ab.2, hq32, hp, hpval,
    hdelta, hindex, htable, ?_⟩
  exact IntegerPowerRealBridge q.val
    (217706 * q.val / 65536 - q.val)
    (ab.1.val * 2 ^ 64 + ab.2.val) ab.2.val
    hrow.1 hrow.2 hsmallERow

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (46)
More program dependencies (13)
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": "IntegerPowerTableCertificate",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "92c4254586c5bcc8b7ed62cb52b5badaee3ceaa51000a32fff02a5dee727a82c",
  "active_statement_hash": "9c8b76a7277e08cc8130b24b893616acee55990c411f0a653386e372acd20694",
  "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-145",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "IntegerPowerFiniteCertificate": "51241f81a8dc22e8",
    "IntegerPowerRealBridge": "1e9cbf5ae71e7110"
  },
  "strict_definition_deps": {
    "IntegerPowerFiniteCertificate": "7b88d77bb302cd1b",
    "num_imp_dec2flt_lemire_power": "56b7c3bd69ca3c6a",
    "num_imp_dec2flt_table_POWER_OF_FIVE_128": "bdd28e83b60fa362"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:89c2d4b18b657103cfc3f06dacad11697252116d4ff2a06f230f57a6bdfb0c41",
  "current_fingerprint": "sound-v2:89c2d4b18b657103cfc3f06dacad11697252116d4ff2a06f230f57a6bdfb0c41",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}