P616_T1_ExtremeRounding

Lean checked

Statement

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

Lemma · Independent extreme rounding certificates

Write T=2^{1024}-2^{970}. Every real y\geq0 with T\leq y rounds in DecimalGoalVocabularyDefault to the positive-infinity word 9218868437227405312. Every real y with 0\leq y\leq2^{-1075} rounds to the zero word. In particular the second assertion includes the tie at half the least positive subnormal.

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.Binary64FiniteMagnitudeStrictMono
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Positivity
import Mathlib.Tactic.FieldSimp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P616_T1_ExtremeRounding]
theorem P616_T1_ExtremeRounding :
    (forall y : Real, 0 <= y ->
      (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= y ->
      DecimalGoalVocabularyDefault.roundsMagnitude y (9218868437227405312#u64)) ∧
    (forall y : Real, 0 <= y -> y <= 1 / (2 : Real) ^ (1075 : Nat) ->
      DecimalGoalVocabularyDefault.roundsMagnitude y (0#u64)) := 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 DecimalGoalVocabularyDefault. Put F=2^{52}, I=2047F=9218868437227405312, and let \phi(n) be its finite decoder. The carrier F64 contains both displayed words since 0\leq I<2^{64}. The infinity word has value I, so its bound by I holds and its conditional rounding branch asks exactly for T\leq y. Together with the supplied nonnegativity this proves the first assertion.
For the zero word, \phi(0)=0 by the exponent-zero formula and its residue modulo two is zero. The threshold satisfies T>1: indeed T=2^{970}(2^{54}-1)>1. Also 2^{-1075}<1, so y<T. The target bound and finite-branch test hold because 0<I. It remains to compare zero with every finite competitor v\ne0. Word extensionality in F64 makes v.\mathit{val}\geq1. The exponent-zero formula gives \phi(1)=2^{-1074}, since 1<F. If v.\mathit{val}=1 its value is exactly that number; if it is greater than one, Binary64FiniteMagnitudeStrictMono applies to 1<v.\mathit{val}<I and gives the strict inequality. Thus in either case \phi(v.\mathit{val})\geq2^{-1074}=2\,2^{-1075}\geq2y. Consequently \phi(v.\mathit{val})-y\geq y\geq0, and
|y-\phi(0)|=y\leq\phi(v.\mathit{val})-y =|y-\phi(v.\mathit{val})|.
If equality occurs, the target's even-residue clause is already true: 0\bmod2=0. These comparisons discharge every finite competitor, including the half-subnormal tie. All clauses of the zero rounding predicate have now been checked, without any source-call premise.

Lean proof


  let F : Nat := 2 ^ 52
  let I : Nat := 2047 * F
  let phi : Nat → Real := fun n =>
    let e := n / F
    let m := n % F
    if e = 0 then (m : Real) / (2 : Real) ^ 1074
    else if 1075 <= e then ((F + m : Nat) : Real) * (2 : Real) ^ (e - 1075)
    else ((F + m : Nat) : Real) / (2 : Real) ^ (1075 - e)
  have hmono : ∀ p q : Nat, p < q → q < I → phi p < phi q :=
    Binary64FiniteMagnitudeStrictMono
  have hF : F = 4503599627370496 := by norm_num [F]
  have hI : I = 9218868437227405312 := by norm_num [I, F]
  have hphi0 : phi 0 = 0 := by norm_num [phi, hF]
  have hphi1 : phi 1 = 1 / (2 : Real) ^ 1074 := by norm_num [phi, hF]
  have hT : (1 : Real) < (2 : Real)^1024 - (2 : Real)^970 := by
    have hnat : (2 : Nat)^970 + 1 < 2^1024 := by decide +kernel
    have hreals : (2 : Real)^970 + 1 < 2^1024 := by exact_mod_cast hnat
    exact (lt_sub_iff_add_lt).2 (by simpa only [add_comm] using hreals)
  have hhalf : 1 / (2 : Real)^1075 < 1 := by
    have hpow : (1 : Real) < (2 : Real)^1075 := one_lt_pow₀ (by norm_num) (by omega)
    exact (div_lt_one (by positivity)).2 hpow
  constructor
  · intro y hy hTy
    change 0 <= y ∧ (9218868437227405312 : Nat) <= 2047 * 2^52 ∧ _
    refine ⟨hy, by norm_num, ?_⟩
    rw [if_pos (by norm_num)]
    exact hTy
  · intro y hy hsmall
    change 0 <= y ∧ (0 : Nat) <= 2047 * 2^52 ∧ _
    refine ⟨hy, by omega, ?_⟩
    rw [if_neg (by norm_num)]
    refine ⟨lt_trans (lt_of_le_of_lt hsmall hhalf) hT, ?_⟩
    intro v hv hne
    change abs (y - phi 0) <= abs (y - phi v.val) ∧ _
    have hv0 : 0 < v.val := by
      by_contra h
      apply hne
      apply UScalar.eq_of_val_eq
      change v.val = 0
      omega
    have hphiLower : 1 / (2 : Real)^1074 <= phi v.val := by
      rw [← hphi1]
      rcases eq_or_lt_of_le (show 1 <= v.val by omega) with heq | hlt
      · rw [heq]
      · exact le_of_lt (hmono 1 v.val hlt (by simpa [I, F] using hv))
    have hscale : 1 / (2 : Real)^1074 = 2 * (1 / (2 : Real)^1075) := by
      have hp : (2 : Real)^1075 = (2 : Real)^1074 * 2 := pow_succ _ _
      rw [hp]
      field_simp
      <;> ring
    have hdiff : y <= phi v.val - y := by
      rw [hscale] at hphiLower
      linarith
    constructor
    · rw [hphi0, sub_zero, abs_of_nonneg hy,
        abs_of_nonpos (show y - phi v.val <= 0 by linarith)]
      linarith
    · intro _
      change (0 : Nat) % 2 = 0
      rfl
end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.Binary64FiniteMagnitudeStrictMono
import Mathlib.Tactic.Linarith
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Positivity
import Mathlib.Tactic.FieldSimp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P616_T1_ExtremeRounding]
theorem P616_T1_ExtremeRounding :
    (forall y : Real, 0 <= y ->
      (2 : Real) ^ (1024 : Nat) - (2 : Real) ^ (970 : Nat) <= y ->
      DecimalGoalVocabularyDefault.roundsMagnitude y (9218868437227405312#u64)) ∧
    (forall y : Real, 0 <= y -> y <= 1 / (2 : Real) ^ (1075 : Nat) ->
      DecimalGoalVocabularyDefault.roundsMagnitude y (0#u64)) := by
-- BODY
  let F : Nat := 2 ^ 52
  let I : Nat := 2047 * F
  let phi : Nat → Real := fun n =>
    let e := n / F
    let m := n % F
    if e = 0 then (m : Real) / (2 : Real) ^ 1074
    else if 1075 <= e then ((F + m : Nat) : Real) * (2 : Real) ^ (e - 1075)
    else ((F + m : Nat) : Real) / (2 : Real) ^ (1075 - e)
  have hmono : ∀ p q : Nat, p < q → q < I → phi p < phi q :=
    Binary64FiniteMagnitudeStrictMono
  have hF : F = 4503599627370496 := by norm_num [F]
  have hI : I = 9218868437227405312 := by norm_num [I, F]
  have hphi0 : phi 0 = 0 := by norm_num [phi, hF]
  have hphi1 : phi 1 = 1 / (2 : Real) ^ 1074 := by norm_num [phi, hF]
  have hT : (1 : Real) < (2 : Real)^1024 - (2 : Real)^970 := by
    have hnat : (2 : Nat)^970 + 1 < 2^1024 := by decide +kernel
    have hreals : (2 : Real)^970 + 1 < 2^1024 := by exact_mod_cast hnat
    exact (lt_sub_iff_add_lt).2 (by simpa only [add_comm] using hreals)
  have hhalf : 1 / (2 : Real)^1075 < 1 := by
    have hpow : (1 : Real) < (2 : Real)^1075 := one_lt_pow₀ (by norm_num) (by omega)
    exact (div_lt_one (by positivity)).2 hpow
  constructor
  · intro y hy hTy
    change 0 <= y ∧ (9218868437227405312 : Nat) <= 2047 * 2^52 ∧ _
    refine ⟨hy, by norm_num, ?_⟩
    rw [if_pos (by norm_num)]
    exact hTy
  · intro y hy hsmall
    change 0 <= y ∧ (0 : Nat) <= 2047 * 2^52 ∧ _
    refine ⟨hy, by omega, ?_⟩
    rw [if_neg (by norm_num)]
    refine ⟨lt_trans (lt_of_le_of_lt hsmall hhalf) hT, ?_⟩
    intro v hv hne
    change abs (y - phi 0) <= abs (y - phi v.val) ∧ _
    have hv0 : 0 < v.val := by
      by_contra h
      apply hne
      apply UScalar.eq_of_val_eq
      change v.val = 0
      omega
    have hphiLower : 1 / (2 : Real)^1074 <= phi v.val := by
      rw [← hphi1]
      rcases eq_or_lt_of_le (show 1 <= v.val by omega) with heq | hlt
      · rw [heq]
      · exact le_of_lt (hmono 1 v.val hlt (by simpa [I, F] using hv))
    have hscale : 1 / (2 : Real)^1074 = 2 * (1 / (2 : Real)^1075) := by
      have hp : (2 : Real)^1075 = (2 : Real)^1074 * 2 := pow_succ _ _
      rw [hp]
      field_simp
      <;> ring
    have hdiff : y <= phi v.val - y := by
      rw [hscale] at hphiLower
      linarith
    constructor
    · rw [hphi0, sub_zero, abs_of_nonneg hy,
        abs_of_nonpos (show y - phi v.val <= 0 by linarith)]
      linarith
    · intro _
      change (0 : Nat) % 2 = 0
      rfl
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 8 entries
Technical checking records
Local closure summary
{
  "node": "P616_T1_ExtremeRounding",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "b43e3144ec7a151310e4bc1804c0902a1ff26026cce3a933966e925ecf1d0437",
  "active_statement_hash": "5e60022d6118e03d5465f64f02f05201712ede5762e40b65b2da112d3611669c",
  "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-138",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64FiniteMagnitudeStrictMono": "ea630fa702fb7b61",
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "F64": "ec4e25fbaf4e11d4"
  }
}
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:667739fea202d96a07b79ce83d808b2236ae45c20298d2417e11184a6a644127",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}