Binary64ApplySign

Lean checked

Statement

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

Lemma

For every binary64 magnitude word below 2^{63} and every Boolean sign, the modeled final sign branch returns a word satisfying signedMagnitude for that sign and magnitude.

Lean statement

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

import Tablet.Binary64ModelDefault
import Tablet.DecimalGoalVocabularyDefault
import Tablet.ModelF64Neg

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: Binary64ApplySign]
theorem Binary64ApplySign :
    ∀ (negative : Bool) (magnitude : F64), magnitude.val < 2 ^ 63 →
      ∃ f : F64,
        (if negative then
          do let t ← ModelF64Neg magnitude; ok (some t)
         else ok (some magnitude)) = ok (some f) ∧
        DecimalGoalVocabularyDefault.signedMagnitude negative magnitude f := 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

If the sign is false, the branch returns the magnitude unchanged, and the required equation adds zero. If it is true, unfold ModelF64Neg and Binary64ModelDefault: the model xor-toggles the bit 2^{63}. The strict raw bound says that bit is initially false. Bitwise evaluation of the 64-bit xor therefore agrees with 64-bit addition by 2^{63}; the sum is still strictly below 2^{64}, so no modular wrap occurs. Thus the returned raw word is exactly the magnitude raw word plus 2^{63}, which together with the original bound is precisely DecimalGoalVocabularyDefault's signedMagnitude relation.

Lean proof


  intro negative magnitude hmag
  cases negative with
  | false =>
      refine ⟨magnitude, rfl, ?_⟩
      dsimp [DecimalGoalVocabularyDefault]
      exact ⟨hmag, by simp⟩
  | true =>
      let f : F64 := ⟨magnitude.bv ^^^ BitVec.ofNat 64 (2 ^ 63)⟩
      have hmodel : ModelF64Neg magnitude = ok f := rfl
      refine ⟨f, ?_, ?_⟩
      · rw [if_pos rfl, hmodel]
        rfl
      · dsimp [DecimalGoalVocabularyDefault]
        refine ⟨hmag, ?_⟩
        dsimp [f]
        have hxor : magnitude.val ^^^ (2 ^ 63) =
            (2 ^ 63) ||| magnitude.val := by
          apply Nat.eq_of_testBit_eq
          intro k
          rw [Nat.testBit_xor, Nat.testBit_or, Nat.testBit_two_pow]
          by_cases hk : k = 63
          · subst k
            rw [Nat.testBit_lt_two_pow hmag]
            decide
          · have hk' : 63 ≠ k := Ne.symm hk
            simp [hk, hk']
        have hor : (2 ^ 63) ||| magnitude.val =
            magnitude.val + 2 ^ 63 := by
          have h := Nat.two_pow_add_eq_or_of_lt hmag 1
          simpa [Nat.add_comm] using h.symm
        change (magnitude.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat =
          magnitude.val + 2 ^ 63
        rw [BitVec.toNat_xor, BitVec.toNat_ofNat]
        norm_num
        exact hxor.trans hor

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Binary64ModelDefault
import Tablet.DecimalGoalVocabularyDefault
import Tablet.ModelF64Neg

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: Binary64ApplySign]
theorem Binary64ApplySign :
    ∀ (negative : Bool) (magnitude : F64), magnitude.val < 2 ^ 63 →
      ∃ f : F64,
        (if negative then
          do let t ← ModelF64Neg magnitude; ok (some t)
         else ok (some magnitude)) = ok (some f) ∧
        DecimalGoalVocabularyDefault.signedMagnitude negative magnitude f := by
-- BODY
  intro negative magnitude hmag
  cases negative with
  | false =>
      refine ⟨magnitude, rfl, ?_⟩
      dsimp [DecimalGoalVocabularyDefault]
      exact ⟨hmag, by simp⟩
  | true =>
      let f : F64 := ⟨magnitude.bv ^^^ BitVec.ofNat 64 (2 ^ 63)⟩
      have hmodel : ModelF64Neg magnitude = ok f := rfl
      refine ⟨f, ?_, ?_⟩
      · rw [if_pos rfl, hmodel]
        rfl
      · dsimp [DecimalGoalVocabularyDefault]
        refine ⟨hmag, ?_⟩
        dsimp [f]
        have hxor : magnitude.val ^^^ (2 ^ 63) =
            (2 ^ 63) ||| magnitude.val := by
          apply Nat.eq_of_testBit_eq
          intro k
          rw [Nat.testBit_xor, Nat.testBit_or, Nat.testBit_two_pow]
          by_cases hk : k = 63
          · subst k
            rw [Nat.testBit_lt_two_pow hmag]
            decide
          · have hk' : 63 ≠ k := Ne.symm hk
            simp [hk, hk']
        have hor : (2 ^ 63) ||| magnitude.val =
            magnitude.val + 2 ^ 63 := by
          have h := Nat.two_pow_add_eq_or_of_lt hmag 1
          simpa [Nat.add_comm] using h.symm
        change (magnitude.bv ^^^ BitVec.ofNat 64 (2 ^ 63)).toNat =
          magnitude.val + 2 ^ 63
        rw [BitVec.toNat_xor, BitVec.toNat_ofNat]
        norm_num
        exact hxor.trans hor

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 11 entries
Technical checking records
Local closure summary
{
  "node": "Binary64ApplySign",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "026850b435ae092d6f4628863ec466c497af5f553dba0730a59153cb8a0c32c2",
  "active_statement_hash": "6b4f21b58fda08171445faf198374ebe1572cd59a85d9b94cec5b2e71baca008",
  "kernel_axioms": [
    "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-131",
  "boundary_theorems": {},
  "strict_theorem_deps": {},
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "91e42e6d78c02c8e",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4",
    "ModelF64Neg": "78a021de99eb373b"
  }
}
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:c7b2db9130001e892e458d2121535f528caad108ea6eeb84d300d792a997dee0",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}