ParserAssembledPairNonNan

Lean checked

Statement

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

Helper

Let b be a biased binary64 pair and w its assembled unsigned word. If 0\le b.p\le2047, b.m\le2^{52}, exponent 2047 forces b.m=0, and w<2^{63}, then w is not a binary64 NaN.

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

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserAssembledPairNonNan]
theorem ParserAssembledPairNonNan
    (fp : num.imp.dec2flt.common.BiasedFp) (word : F64)
    (hp0 : 0 ≤ fp.p_biased.val) (hpmax : fp.p_biased.val ≤ 2047)
    (hm : fp.m.val ≤ 2 ^ 52)
    (hinf : fp.p_biased.val = 2047 -> fp.m.val = 0)
    (hassembled : DecimalGoalVocabularyDefault.assembledWord fp word)
    (hword : word.val < 2 ^ 63) :
    ¬ DecimalGoalVocabularyDefault.isNan word := 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

Unfold the predicates in DecimalGoalVocabularyDefault. Since 0\le b.p\le2047, reducing the signed exponent modulo 2^{64} and then converting it to a natural number preserves its value. Moreover b.p\,2^{52}<2^{64}, so the second reduction in assembledWord also preserves that product. Hence
w=\operatorname{lor}(b.m,b.p\,2^{52}).
The bound w<2^{63} also gives w\bmod2^{63}=w.
First suppose b.m<2^{52}. Every set bit of b.m is below position 52, whereas every set bit of b.p\,2^{52} is at or above position 52. The standard binary identity 2^{52}b.p+b.m=(2^{52}b.p)\mathbin{\mathtt{|}}b.m therefore gives w=b.p\,2^{52}+b.m. Euclidean division by 2^{52} has quotient b.p and remainder b.m. If the NaN exponent equation made this quotient 2047, the endpoint hypothesis would give b.m=0, contradicting the required nonzero NaN fraction.
It remains to consider the only other case allowed by the mantissa bound, b.m=2^{52}. For natural numbers a,c,k, bit extensionality and the formula for bits of a multiple of 2^k give
(a2^k)\mathbin{\mathtt{|}}(c2^k)=(a\mathbin{\mathtt{|}}c)2^k.
Apply this with a=1, c=b.p, and k=52. The assembled word is a multiple of 2^{52}, so its fraction remainder is zero, again contradicting the NaN predicate. The two mantissa cases are exhaustive.

Lean proof


  dsimp only [DecimalGoalVocabularyDefault] at hassembled ⊢
  intro hnan
  rcases hnan with ⟨hexponent, hfraction⟩
  have hsign : word.val % (2 ^ 63) = word.val := Nat.mod_eq_of_lt hword
  rw [hsign] at hexponent
  have hpLt : fp.p_biased.val < 2 ^ 64 := by omega
  have hpEmod : fp.p_biased.val.emod (2 ^ 64) = fp.p_biased.val :=
    Int.emod_eq_of_lt hp0 hpLt
  have hpNat : fp.p_biased.val.toNat ≤ 2047 := by
    rw [Int.toNat_le]
    omega
  have hexponentField :
      (((fp.p_biased.val.emod (2 ^ 64)).toNat * 2 ^ 52) % 2 ^ 64) =
        fp.p_biased.val.toNat * 2 ^ 52 := by
    rw [hpEmod, Nat.mod_eq_of_lt]
    omega
  rw [hexponentField] at hassembled
  by_cases hmLt : fp.m.val < 2 ^ 52
  · have hor := Nat.two_pow_add_eq_or_of_lt hmLt fp.p_biased.val.toNat
    have hwordExact : word.val = fp.p_biased.val.toNat * 2 ^ 52 + fp.m.val := by
      calc
        word.val = fp.m.val ||| (fp.p_biased.val.toNat * 2 ^ 52) := hassembled
        _ = (fp.p_biased.val.toNat * 2 ^ 52) ||| fp.m.val :=
          Nat.lor_comm _ _
        _ = (2 ^ 52 * fp.p_biased.val.toNat) ||| fp.m.val := by
          rw [Nat.mul_comm]
        _ = 2 ^ 52 * fp.p_biased.val.toNat + fp.m.val := hor.symm
        _ = fp.p_biased.val.toNat * 2 ^ 52 + fp.m.val := by omega
    have hpExact : fp.p_biased.val.toNat = 2047 := by
      rw [hwordExact, Nat.mul_comm fp.p_biased.val.toNat,
        Nat.mul_add_div (by positivity), Nat.div_eq_of_lt hmLt,
        Nat.add_zero] at hexponent
      exact hexponent
    have hpVal : fp.p_biased.val = 2047 := by
      have htoNat := Int.toNat_of_nonneg hp0
      omega
    have hmZero := hinf hpVal
    rw [hwordExact, hpExact, hmZero] at hfraction
    norm_num at hfraction
  · have hmExact : fp.m.val = 2 ^ 52 := by omega
    have lor_mul (a b k : Nat) :
        (a * 2 ^ k) ||| (b * 2 ^ k) = (a ||| b) * 2 ^ k := by
      apply Nat.eq_of_testBit_eq
      intro j
      simp only [Nat.testBit_lor, Nat.testBit_mul_two_pow]
      by_cases hkj : k ≤ j <;> simp [hkj]
    have hwordExact :
        word.val = (1 ||| fp.p_biased.val.toNat) * 2 ^ 52 := by
      rw [hassembled, hmExact]
      exact lor_mul 1 fp.p_biased.val.toNat 52
    rw [hwordExact] at hfraction
    simp at hfraction

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserAssembledPairNonNan]
theorem ParserAssembledPairNonNan
    (fp : num.imp.dec2flt.common.BiasedFp) (word : F64)
    (hp0 : 0 ≤ fp.p_biased.val) (hpmax : fp.p_biased.val ≤ 2047)
    (hm : fp.m.val ≤ 2 ^ 52)
    (hinf : fp.p_biased.val = 2047 -> fp.m.val = 0)
    (hassembled : DecimalGoalVocabularyDefault.assembledWord fp word)
    (hword : word.val < 2 ^ 63) :
    ¬ DecimalGoalVocabularyDefault.isNan word := by
-- BODY
  dsimp only [DecimalGoalVocabularyDefault] at hassembled ⊢
  intro hnan
  rcases hnan with ⟨hexponent, hfraction⟩
  have hsign : word.val % (2 ^ 63) = word.val := Nat.mod_eq_of_lt hword
  rw [hsign] at hexponent
  have hpLt : fp.p_biased.val < 2 ^ 64 := by omega
  have hpEmod : fp.p_biased.val.emod (2 ^ 64) = fp.p_biased.val :=
    Int.emod_eq_of_lt hp0 hpLt
  have hpNat : fp.p_biased.val.toNat ≤ 2047 := by
    rw [Int.toNat_le]
    omega
  have hexponentField :
      (((fp.p_biased.val.emod (2 ^ 64)).toNat * 2 ^ 52) % 2 ^ 64) =
        fp.p_biased.val.toNat * 2 ^ 52 := by
    rw [hpEmod, Nat.mod_eq_of_lt]
    omega
  rw [hexponentField] at hassembled
  by_cases hmLt : fp.m.val < 2 ^ 52
  · have hor := Nat.two_pow_add_eq_or_of_lt hmLt fp.p_biased.val.toNat
    have hwordExact : word.val = fp.p_biased.val.toNat * 2 ^ 52 + fp.m.val := by
      calc
        word.val = fp.m.val ||| (fp.p_biased.val.toNat * 2 ^ 52) := hassembled
        _ = (fp.p_biased.val.toNat * 2 ^ 52) ||| fp.m.val :=
          Nat.lor_comm _ _
        _ = (2 ^ 52 * fp.p_biased.val.toNat) ||| fp.m.val := by
          rw [Nat.mul_comm]
        _ = 2 ^ 52 * fp.p_biased.val.toNat + fp.m.val := hor.symm
        _ = fp.p_biased.val.toNat * 2 ^ 52 + fp.m.val := by omega
    have hpExact : fp.p_biased.val.toNat = 2047 := by
      rw [hwordExact, Nat.mul_comm fp.p_biased.val.toNat,
        Nat.mul_add_div (by positivity), Nat.div_eq_of_lt hmLt,
        Nat.add_zero] at hexponent
      exact hexponent
    have hpVal : fp.p_biased.val = 2047 := by
      have htoNat := Int.toNat_of_nonneg hp0
      omega
    have hmZero := hinf hpVal
    rw [hwordExact, hpExact, hmZero] at hfraction
    norm_num at hfraction
  · have hmExact : fp.m.val = 2 ^ 52 := by omega
    have lor_mul (a b k : Nat) :
        (a * 2 ^ k) ||| (b * 2 ^ k) = (a ||| b) * 2 ^ k := by
      apply Nat.eq_of_testBit_eq
      intro j
      simp only [Nat.testBit_lor, Nat.testBit_mul_two_pow]
      by_cases hkj : k ≤ j <;> simp [hkj]
    have hwordExact :
        word.val = (1 ||| fp.p_biased.val.toNat) * 2 ^ 52 := by
      rw [hassembled, hmExact]
      exact lor_mul 1 fp.p_biased.val.toNat 52
    rw [hwordExact] at hfraction
    simp at hfraction

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
All 7 entries
Technical checking records
Local closure summary
{
  "node": "ParserAssembledPairNonNan",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "1d1f11e4ef8d162b6e03fe347a9bb08d5cc207b74719bcc36829323eed7af737",
  "active_statement_hash": "fcdc6fe75dcf4a21edc3b44c2182b208b1aa200c00ec7cb348c45ded6f3a24c8",
  "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-136",
  "boundary_theorems": {},
  "strict_theorem_deps": {},
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "d8d2f621d2f6aa27",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4",
    "num_imp_dec2flt_common_BiasedFp": "31d81cf1cf22709f"
  }
}
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:985f066499a47246343a4ba80eb27162580d229d544ff32ccc7e0037db7f2321",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}