P822_T1_NormalRoundingExecution

Lean checked

Statement

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

Lemma · Normal rounding suffix executes

Let p be a modeled I32 and M a modeled U64 with 0<p.\mathrm{val}\le2110 and M.\mathrm{val}<2^{55}. The following source-ordered Result computation returns normally: lift c=M\mathbin{\&}1; checked addition M_2=M+c; checked right shift M_3=M_2\gg1; checked left shift T=2\ll52; if M_3\ge T, compute K=1\ll52 and the checked I32 addition p_2=p+1 and take (M_4,p_1)=(K,p_2), otherwise take (M_4,p_1)=(M_3,p); then compute B=1\ll52, lift C=\mathord{\sim}B, and lift M_5=M_4\mathbin{\&}C. Return (0,2047) if p_1\ge2047, and (M_5,p_1) otherwise. The literals 1 in right shifts and exponent addition have I32 type; 52 has U32 type; mantissa literals have U64 type. Normality means \mathrm{WP.spec} with the identically true postcondition, equivalently existence of an \mathrm{ok} biased pair, with no failure or divergence.

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: P822_T1_NormalRoundingExecution]
theorem P822_T1_NormalRoundingExecution (power2 : Std.I32) (m1 : Std.U64)
    (hm : m1.val < 2 ^ 55) (hpPos : 0 < power2.val)
    (hpHi : power2.val ≤ 2110) :
    WP.spec (do
      let i20 ← lift (m1 &&& 1#u64)
      let mantissa2 ← m1 + i20
      let mantissa3 ← mantissa2 >>> 1#i32
      let i21 ← 2#u64 <<< 52#u32
      let (mantissa4, power21) ←
        if mantissa3 >= i21 then do
          let mantissa5 ← 1#u64 <<< 52#u32
          let power22 ← power2 + 1#i32
          ok (mantissa5, power22)
        else ok (mantissa3, power2)
      let i22 ← 1#u64 <<< 52#u32
      let i23 ← lift (~~~ i22)
      let mantissa5 ← lift (mantissa4 &&& i23)
      if power21 >= 2047#i32 then
        ok { m := 0#u64, p_biased := 2047#i32 }
      else ok { m := mantissa5, p_biased := power21 }
    
      : Result num.imp.dec2flt.common.BiasedFp) (fun _ => True) := 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

All U64 bitwise AND and complement operations are total with their usual 64-bit semantics. In particular the first lifted AND returns c with 0\le c.\mathrm{val}\le1. Thus M.\mathrm{val}+c.\mathrm{val}\le2^{55}<2^{64}, so the checked U64 addition returns M_2 normally. Its right shift has count 1, which is nonnegative and below 64, so it returns M_3 normally. The left shift of 2 has count 52 below 64 and returns T normally. Such shifts use bitvector semantics and require only the shift-count bound.
Split on the total comparison M_3\ge T. In its true arm, 1\ll52 succeeds by the same count bound. The hypotheses give 2\le p.\mathrm{val}+1\le2111, strictly within the signed 32-bit range [-2^{31},2^{31}-1], so the checked addition returns p_2. The pair is then returned normally. In the false arm the pair (M_3,p) is returned without additional arithmetic. In either arm the subsequent 1\ll52 succeeds, and its lifted complement and the lifted AND with M_4 are total. Finally the comparison of p_1 with 2047 is total; each branch is an immediate normal return of a biased pair. Substituting each normal result into the next monadic bind proves the claimed normality of the entire computation, including the carry and non-carry arms. No correctness-on-normal assumption is used.

Lean proof


  have hpLo : -114 ≤ power2.val := by omega
  have handSpec := UScalar.and_spec m1 1#u64
  obtain ⟨i20, hand, handval⟩ := WP.spec_imp_exists handSpec
  rw [hand]
  simp only [bind_tc_ok]
  have hlow : i20.val ≤ 1 := by
    rw [handval.1, UScalar.val_and]
    exact Nat.and_le_right
  have haddBound : m1.val + i20.val ≤ UScalar.max .U64 := by
    norm_num [UScalar.max, U64.max_eq, UScalarTy.numBits]
    omega
  have haddSpec := UScalar.add_spec (x := m1) (y := i20) haddBound
  obtain ⟨m2, hadd, _⟩ := WP.spec_imp_exists haddSpec
  rw [hadd]
  simp only [bind_tc_ok]
  have hshiftM3 := UScalar.ShiftRight_IScalar_spec m2 (1#i32)
    (by norm_num) (by change (1#i32).val < 64; norm_num)
  obtain ⟨m3, hm3, _⟩ := WP.spec_imp_exists hshiftM3
  rw [hm3]
  simp only [bind_tc_ok]
  have hshift21 := UScalar.ShiftLeft_spec (2#u64) (52#u32)
    (2 ^ 64) (by norm_num [UScalarTy.numBits]) (by norm_num [U64.size_eq])
  obtain ⟨i21, hi21, _⟩ := WP.spec_imp_exists hshift21
  rw [hi21]
  simp only [bind_tc_ok]
  by_cases hcarry : m3 >= i21
  · simp only [hcarry, if_pos]
    have hshiftOne := UScalar.ShiftLeft_spec (1#u64) (52#u32)
      (2 ^ 64) (by norm_num [UScalarTy.numBits]) (by norm_num [U64.size_eq])
    obtain ⟨m5, hm5, _⟩ := WP.spec_imp_exists hshiftOne
    rw [hm5]
    simp only [bind_tc_ok]
    have hp22lo : IScalar.min .I32 ≤ power2.val + 1 := by
      rw [IScalar.min_I32_eq]
      omega
    have hp22hi : power2.val + 1 ≤ IScalar.max .I32 := by
      rw [IScalar.max_I32_eq]
      omega
    have hp22Spec := IScalar.add_spec (x := power2) (y := 1#i32)
      hp22lo hp22hi
    obtain ⟨p22, hp22, _⟩ := WP.spec_imp_exists hp22Spec
    rw [hp22]
    simp only [bind_tc_ok]
    change WP.spec (do
      let i23 ← lift (~~~ m5)
      let mantissa5 ← lift (m5 &&& i23)
      if p22 >= 2047#i32 then
        (ok { m := 0#u64, p_biased := 2047#i32 } :
          Result num.imp.dec2flt.common.BiasedFp)
      else (ok { m := mantissa5, p_biased := p22 } :
        Result num.imp.dec2flt.common.BiasedFp)) (fun _ => True)
    have hnotSpec := UScalar.not_spec m5
    obtain ⟨i23, hnot, _⟩ := WP.spec_imp_exists hnotSpec
    rw [hnot]
    simp only [bind_tc_ok]
    have handFinal := UScalar.and_spec m5 i23
    obtain ⟨mFinal, hand, _⟩ := WP.spec_imp_exists handFinal
    rw [hand]
    simp only [bind_tc_ok]
    by_cases hbig : p22 >= 2047#i32
    · simp only [hbig, if_pos]
      simp [WP.spec_ok]
    · simp only [hbig, if_neg]
      simp [WP.spec_ok]
  · simp only [hcarry, if_neg]
    simp only [if_false]
    simp only [bind_tc_ok]
    step by scalar_tac
    step by scalar_tac
    step by scalar_tac
    by_cases hbig : power2 >= 2047#i32
    · simp only [hbig, if_pos]
      simp [WP.spec_ok]
    · simp only [hbig, if_neg]
      simp [WP.spec_ok]

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: P822_T1_NormalRoundingExecution]
theorem P822_T1_NormalRoundingExecution (power2 : Std.I32) (m1 : Std.U64)
    (hm : m1.val < 2 ^ 55) (hpPos : 0 < power2.val)
    (hpHi : power2.val ≤ 2110) :
    WP.spec (do
      let i20 ← lift (m1 &&& 1#u64)
      let mantissa2 ← m1 + i20
      let mantissa3 ← mantissa2 >>> 1#i32
      let i21 ← 2#u64 <<< 52#u32
      let (mantissa4, power21) ←
        if mantissa3 >= i21 then do
          let mantissa5 ← 1#u64 <<< 52#u32
          let power22 ← power2 + 1#i32
          ok (mantissa5, power22)
        else ok (mantissa3, power2)
      let i22 ← 1#u64 <<< 52#u32
      let i23 ← lift (~~~ i22)
      let mantissa5 ← lift (mantissa4 &&& i23)
      if power21 >= 2047#i32 then
        ok { m := 0#u64, p_biased := 2047#i32 }
      else ok { m := mantissa5, p_biased := power21 }
    
      : Result num.imp.dec2flt.common.BiasedFp) (fun _ => True) := by
-- BODY
  have hpLo : -114 ≤ power2.val := by omega
  have handSpec := UScalar.and_spec m1 1#u64
  obtain ⟨i20, hand, handval⟩ := WP.spec_imp_exists handSpec
  rw [hand]
  simp only [bind_tc_ok]
  have hlow : i20.val ≤ 1 := by
    rw [handval.1, UScalar.val_and]
    exact Nat.and_le_right
  have haddBound : m1.val + i20.val ≤ UScalar.max .U64 := by
    norm_num [UScalar.max, U64.max_eq, UScalarTy.numBits]
    omega
  have haddSpec := UScalar.add_spec (x := m1) (y := i20) haddBound
  obtain ⟨m2, hadd, _⟩ := WP.spec_imp_exists haddSpec
  rw [hadd]
  simp only [bind_tc_ok]
  have hshiftM3 := UScalar.ShiftRight_IScalar_spec m2 (1#i32)
    (by norm_num) (by change (1#i32).val < 64; norm_num)
  obtain ⟨m3, hm3, _⟩ := WP.spec_imp_exists hshiftM3
  rw [hm3]
  simp only [bind_tc_ok]
  have hshift21 := UScalar.ShiftLeft_spec (2#u64) (52#u32)
    (2 ^ 64) (by norm_num [UScalarTy.numBits]) (by norm_num [U64.size_eq])
  obtain ⟨i21, hi21, _⟩ := WP.spec_imp_exists hshift21
  rw [hi21]
  simp only [bind_tc_ok]
  by_cases hcarry : m3 >= i21
  · simp only [hcarry, if_pos]
    have hshiftOne := UScalar.ShiftLeft_spec (1#u64) (52#u32)
      (2 ^ 64) (by norm_num [UScalarTy.numBits]) (by norm_num [U64.size_eq])
    obtain ⟨m5, hm5, _⟩ := WP.spec_imp_exists hshiftOne
    rw [hm5]
    simp only [bind_tc_ok]
    have hp22lo : IScalar.min .I32 ≤ power2.val + 1 := by
      rw [IScalar.min_I32_eq]
      omega
    have hp22hi : power2.val + 1 ≤ IScalar.max .I32 := by
      rw [IScalar.max_I32_eq]
      omega
    have hp22Spec := IScalar.add_spec (x := power2) (y := 1#i32)
      hp22lo hp22hi
    obtain ⟨p22, hp22, _⟩ := WP.spec_imp_exists hp22Spec
    rw [hp22]
    simp only [bind_tc_ok]
    change WP.spec (do
      let i23 ← lift (~~~ m5)
      let mantissa5 ← lift (m5 &&& i23)
      if p22 >= 2047#i32 then
        (ok { m := 0#u64, p_biased := 2047#i32 } :
          Result num.imp.dec2flt.common.BiasedFp)
      else (ok { m := mantissa5, p_biased := p22 } :
        Result num.imp.dec2flt.common.BiasedFp)) (fun _ => True)
    have hnotSpec := UScalar.not_spec m5
    obtain ⟨i23, hnot, _⟩ := WP.spec_imp_exists hnotSpec
    rw [hnot]
    simp only [bind_tc_ok]
    have handFinal := UScalar.and_spec m5 i23
    obtain ⟨mFinal, hand, _⟩ := WP.spec_imp_exists handFinal
    rw [hand]
    simp only [bind_tc_ok]
    by_cases hbig : p22 >= 2047#i32
    · simp only [hbig, if_pos]
      simp [WP.spec_ok]
    · simp only [hbig, if_neg]
      simp [WP.spec_ok]
  · simp only [hcarry, if_neg]
    simp only [if_false]
    simp only [bind_tc_ok]
    step by scalar_tac
    step by scalar_tac
    step by scalar_tac
    by_cases hbig : power2 >= 2047#i32
    · simp only [hbig, if_pos]
      simp [WP.spec_ok]
    · simp only [hbig, if_neg]
      simp [WP.spec_ok]

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": "P822_T1_NormalRoundingExecution",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "787473e4c4daa9d00192c2f59a88daf2bdcb5f21c684e3eb0049467a18657d57",
  "active_statement_hash": "1fa45d92938624bcd572169f177eb04f8c3d9528c06f39095e21c4a8037a69a6",
  "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-192",
  "boundary_theorems": {},
  "strict_theorem_deps": {},
  "strict_definition_deps": {
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2"
  }
}
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:cee42297dd5b44a739424e6d383643412e694e99173ffbba5b4f89a54c9ba20d",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}