RoundsMagnitudeInterval

Lean checked

Statement

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

Lemma

If L\le A\le U and the same binary64 word m satisfies roundsMagnitude at both L and U, then it satisfies roundsMagnitude at A.

Lean statement

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

import Tablet.ParserSuccessfulTrace

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: RoundsMagnitudeInterval]
theorem RoundsMagnitudeInterval :
    ∀ (lower value upper : Real) (target : F64),
      lower ≤ value -> value ≤ upper ->
      DecimalGoalVocabularyDefault.roundsMagnitude lower target ->
      DecimalGoalVocabularyDefault.roundsMagnitude upper target ->
      DecimalGoalVocabularyDefault.roundsMagnitude value target := 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. The lower endpoint hypothesis gives 0\le L, hence 0\le A, and either endpoint gives the fixed word bound m.\mathit{val}\le 2047\cdot2^{52}.
If m is positive infinity, both endpoint hypotheses put L and U at or above the overflow threshold; L\le A puts A there as well. Suppose m is finite. Both endpoints are strictly below the overflow threshold, hence so is A\le U. Fix any competing finite word v\ne m. Squaring the two nonnegative absolute values shows that
|z-\operatorname{value}(m)|\le|z-\operatorname{value}(v)|
is equivalent to an affine weak inequality in z: after cancelling z^2, its left side is 2z(\operatorname{value}(v)-\operatorname{value}(m)) and its right side is \operatorname{value}(v)^2-\operatorname{value}(m)^2. Thus its solution set is a half-line (or all reals), and is convex. It contains both L and U, so it contains every A between them.
It remains to check the tie clause. If m.\mathit{val} is even, the clause is immediate. If it is odd, each endpoint hypothesis rules out equality with every competitor, because equality would require m even. The strict version of the same affine inequality defines an open half-line. An interval between two points of that open half-line remains inside it, so equality is also impossible at A. This proves the nearest and ties-to-even clauses for every v, and hence all fields of roundsMagnitude at A.

Lean proof


  intro lower value upper target lower_le upper_le lower_round upper_round
  dsimp [DecimalGoalVocabularyDefault] at lower_round upper_round ⊢
  let top : Nat := 2047 * 2 ^ 52
  let threshold : Real := (2 : Real) ^ 1024 - (2 : Real) ^ 970
  let magnitude : Nat → Real := fun bits =>
    let exponent := bits / 4503599627370496
    let fraction := bits % 4503599627370496
    if exponent = 0 then (fraction : Real) / (2 : Real) ^ 1074
    else if 1075 ≤ exponent then
      ((4503599627370496 + fraction : Nat) : Real) *
        (2 : Real) ^ (exponent - 1075)
    else
      ((4503599627370496 + fraction : Nat) : Real) /
        (2 : Real) ^ (1075 - exponent)
  change 0 ≤ lower ∧ target.val ≤ top ∧
    (if target.val = top then threshold ≤ lower else
      lower < threshold ∧ ∀ competitor : F64, competitor.val < top →
        competitor ≠ target →
          abs (lower - magnitude target.val) ≤ abs (lower - magnitude competitor.val) ∧
          (abs (lower - magnitude target.val) = abs (lower - magnitude competitor.val) →
            target.val % 2 = 0)) at lower_round
  change 0 ≤ upper ∧ target.val ≤ top ∧
    (if target.val = top then threshold ≤ upper else
      upper < threshold ∧ ∀ competitor : F64, competitor.val < top →
        competitor ≠ target →
          abs (upper - magnitude target.val) ≤ abs (upper - magnitude competitor.val) ∧
          (abs (upper - magnitude target.val) = abs (upper - magnitude competitor.val) →
            target.val % 2 = 0)) at upper_round
  change 0 ≤ value ∧ target.val ≤ top ∧
    (if target.val = top then threshold ≤ value else
      value < threshold ∧ ∀ competitor : F64, competitor.val < top →
        competitor ≠ target →
          abs (value - magnitude target.val) ≤ abs (value - magnitude competitor.val) ∧
          (abs (value - magnitude target.val) = abs (value - magnitude competitor.val) →
            target.val % 2 = 0))
  have value_nonneg : 0 ≤ value := lower_round.1.trans lower_le
  have word_bound : target.val ≤ top := lower_round.2.1
  by_cases top_case : target.val = top
  · have lower_threshold : threshold ≤ lower := by
      rw [top_case] at lower_round
      exact lower_round.2.2
    refine ⟨value_nonneg, word_bound, ?_⟩
    rw [if_pos top_case]
    exact lower_threshold.trans lower_le
  · rw [if_neg top_case] at lower_round upper_round ⊢
    rcases lower_round with ⟨lower_nonneg, bound_lower, lower_limit, lower_competitor⟩
    rcases upper_round with ⟨upper_nonneg, bound_upper, upper_limit, upper_competitor⟩
    have value_limit : value < threshold := lt_of_le_of_lt upper_le upper_limit
    refine ⟨value_nonneg, word_bound, value_limit, ?_⟩
    intro competitor competitor_bound competitor_ne
    let target_value : Real := magnitude target.val
    let competitor_value : Real := magnitude competitor.val
    have target_value_nonneg : 0 ≤ target_value := by
      dsimp [target_value, magnitude]
      split
      · positivity
      · split <;> positivity
    have competitor_value_nonneg : 0 ≤ competitor_value := by
      dsimp [competitor_value, magnitude]
      split
      · positivity
      · split <;> positivity
    have lower_distance := (lower_competitor competitor competitor_bound competitor_ne).1
    have upper_distance := (upper_competitor competitor competitor_bound competitor_ne).1
    have lower_distance' : abs (lower - target_value) ≤
        abs (lower - competitor_value) := by
      simpa [target_value, competitor_value] using lower_distance
    have upper_distance' : abs (upper - target_value) ≤
        abs (upper - competitor_value) := by
      simpa [target_value, competitor_value] using upper_distance
    have lower_square : (lower - target_value) ^ 2 ≤
        (lower - competitor_value) ^ 2 := by
      exact (sq_le_sq).2 lower_distance'
    have upper_square : (upper - target_value) ^ 2 ≤
        (upper - competitor_value) ^ 2 := by
      exact (sq_le_sq).2 upper_distance'
    have lower_affine : 2 * lower * (competitor_value - target_value) ≤
        competitor_value ^ 2 - target_value ^ 2 := by
      nlinarith [lower_square]
    have upper_affine : 2 * upper * (competitor_value - target_value) ≤
        competitor_value ^ 2 - target_value ^ 2 := by
      nlinarith [upper_square]
    have value_affine : 2 * value * (competitor_value - target_value) ≤
        competitor_value ^ 2 - target_value ^ 2 := by
      by_cases coefficient : 0 ≤ competitor_value - target_value
      · have scaled : 2 * value * (competitor_value - target_value) ≤
            2 * upper * (competitor_value - target_value) := by
          have doubled : 2 * value ≤ 2 * upper :=
            mul_le_mul_of_nonneg_left upper_le (by norm_num)
          exact mul_le_mul_of_nonneg_right doubled coefficient
        exact scaled.trans upper_affine
      · have coefficient' : competitor_value - target_value ≤ 0 := le_of_not_ge coefficient
        have scaled : 2 * value * (competitor_value - target_value) ≤
            2 * lower * (competitor_value - target_value) := by
          have doubled : 2 * lower ≤ 2 * value :=
            mul_le_mul_of_nonneg_left lower_le (by norm_num)
          exact mul_le_mul_of_nonpos_right doubled coefficient'
        exact scaled.trans lower_affine
    have value_square : (value - target_value) ^ 2 ≤
        (value - competitor_value) ^ 2 := by
      nlinarith [value_affine]
    have value_distance : abs (value - target_value) ≤
        abs (value - competitor_value) := by
      exact (sq_le_sq).1 value_square
    refine ⟨?_, ?_⟩
    · simpa [target_value, competitor_value]
        using value_distance
    intro equality
    by_cases even : target.val % 2 = 0
    · exact even
    · exfalso
      have lower_not_equal : abs (lower - target_value) ≠
          abs (lower - competitor_value) := by
        intro equality_lower
        exact even ((lower_competitor competitor competitor_bound competitor_ne).2
          (by simpa [target_value, competitor_value] using equality_lower))
      have upper_not_equal : abs (upper - target_value) ≠
          abs (upper - competitor_value) := by
        intro equality_upper
        exact even ((upper_competitor competitor competitor_bound competitor_ne).2
          (by simpa [target_value, competitor_value] using equality_upper))
      have lower_strict : (lower - target_value) ^ 2 <
          (lower - competitor_value) ^ 2 := by
        have habs : abs (lower - target_value) <
            abs (lower - competitor_value) := lt_of_le_of_ne lower_distance' lower_not_equal
        exact (sq_lt_sq).2 habs
      have upper_strict : (upper - target_value) ^ 2 <
          (upper - competitor_value) ^ 2 := by
        have habs : abs (upper - target_value) <
            abs (upper - competitor_value) := lt_of_le_of_ne upper_distance' upper_not_equal
        exact (sq_lt_sq).2 habs
      have lower_affine_strict : 2 * lower * (competitor_value - target_value) <
          competitor_value ^ 2 - target_value ^ 2 := by
        nlinarith [lower_strict]
      have upper_affine_strict : 2 * upper * (competitor_value - target_value) <
          competitor_value ^ 2 - target_value ^ 2 := by
        nlinarith [upper_strict]
      have value_affine_strict : 2 * value * (competitor_value - target_value) <
          competitor_value ^ 2 - target_value ^ 2 := by
        by_cases coefficient : 0 ≤ competitor_value - target_value
        · have scaled : 2 * value * (competitor_value - target_value) ≤
              2 * upper * (competitor_value - target_value) := by
            have doubled : 2 * value ≤ 2 * upper :=
              mul_le_mul_of_nonneg_left upper_le (by norm_num)
            exact mul_le_mul_of_nonneg_right doubled coefficient
          exact lt_of_le_of_lt scaled upper_affine_strict
        · have coefficient' : competitor_value - target_value ≤ 0 := le_of_not_ge coefficient
          have scaled : 2 * value * (competitor_value - target_value) ≤
              2 * lower * (competitor_value - target_value) := by
            have doubled : 2 * lower ≤ 2 * value :=
              mul_le_mul_of_nonneg_left lower_le (by norm_num)
            exact mul_le_mul_of_nonpos_right doubled coefficient'
          exact lt_of_le_of_lt scaled lower_affine_strict
      have value_strict : (value - target_value) ^ 2 <
          (value - competitor_value) ^ 2 := by
        nlinarith [value_affine_strict]
      have strict_distance : abs (value - target_value) <
          abs (value - competitor_value) := (sq_lt_sq).1 value_strict
      have equality' : abs (value - target_value) =
          abs (value - competitor_value) := by
        simpa [target_value, competitor_value] using equality
      exact (ne_of_lt strict_distance) equality'

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.ParserSuccessfulTrace

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: RoundsMagnitudeInterval]
theorem RoundsMagnitudeInterval :
    ∀ (lower value upper : Real) (target : F64),
      lower ≤ value -> value ≤ upper ->
      DecimalGoalVocabularyDefault.roundsMagnitude lower target ->
      DecimalGoalVocabularyDefault.roundsMagnitude upper target ->
      DecimalGoalVocabularyDefault.roundsMagnitude value target := by
-- BODY
  intro lower value upper target lower_le upper_le lower_round upper_round
  dsimp [DecimalGoalVocabularyDefault] at lower_round upper_round ⊢
  let top : Nat := 2047 * 2 ^ 52
  let threshold : Real := (2 : Real) ^ 1024 - (2 : Real) ^ 970
  let magnitude : Nat → Real := fun bits =>
    let exponent := bits / 4503599627370496
    let fraction := bits % 4503599627370496
    if exponent = 0 then (fraction : Real) / (2 : Real) ^ 1074
    else if 1075 ≤ exponent then
      ((4503599627370496 + fraction : Nat) : Real) *
        (2 : Real) ^ (exponent - 1075)
    else
      ((4503599627370496 + fraction : Nat) : Real) /
        (2 : Real) ^ (1075 - exponent)
  change 0 ≤ lower ∧ target.val ≤ top ∧
    (if target.val = top then threshold ≤ lower else
      lower < threshold ∧ ∀ competitor : F64, competitor.val < top →
        competitor ≠ target →
          abs (lower - magnitude target.val) ≤ abs (lower - magnitude competitor.val) ∧
          (abs (lower - magnitude target.val) = abs (lower - magnitude competitor.val) →
            target.val % 2 = 0)) at lower_round
  change 0 ≤ upper ∧ target.val ≤ top ∧
    (if target.val = top then threshold ≤ upper else
      upper < threshold ∧ ∀ competitor : F64, competitor.val < top →
        competitor ≠ target →
          abs (upper - magnitude target.val) ≤ abs (upper - magnitude competitor.val) ∧
          (abs (upper - magnitude target.val) = abs (upper - magnitude competitor.val) →
            target.val % 2 = 0)) at upper_round
  change 0 ≤ value ∧ target.val ≤ top ∧
    (if target.val = top then threshold ≤ value else
      value < threshold ∧ ∀ competitor : F64, competitor.val < top →
        competitor ≠ target →
          abs (value - magnitude target.val) ≤ abs (value - magnitude competitor.val) ∧
          (abs (value - magnitude target.val) = abs (value - magnitude competitor.val) →
            target.val % 2 = 0))
  have value_nonneg : 0 ≤ value := lower_round.1.trans lower_le
  have word_bound : target.val ≤ top := lower_round.2.1
  by_cases top_case : target.val = top
  · have lower_threshold : threshold ≤ lower := by
      rw [top_case] at lower_round
      exact lower_round.2.2
    refine ⟨value_nonneg, word_bound, ?_⟩
    rw [if_pos top_case]
    exact lower_threshold.trans lower_le
  · rw [if_neg top_case] at lower_round upper_round ⊢
    rcases lower_round with ⟨lower_nonneg, bound_lower, lower_limit, lower_competitor⟩
    rcases upper_round with ⟨upper_nonneg, bound_upper, upper_limit, upper_competitor⟩
    have value_limit : value < threshold := lt_of_le_of_lt upper_le upper_limit
    refine ⟨value_nonneg, word_bound, value_limit, ?_⟩
    intro competitor competitor_bound competitor_ne
    let target_value : Real := magnitude target.val
    let competitor_value : Real := magnitude competitor.val
    have target_value_nonneg : 0 ≤ target_value := by
      dsimp [target_value, magnitude]
      split
      · positivity
      · split <;> positivity
    have competitor_value_nonneg : 0 ≤ competitor_value := by
      dsimp [competitor_value, magnitude]
      split
      · positivity
      · split <;> positivity
    have lower_distance := (lower_competitor competitor competitor_bound competitor_ne).1
    have upper_distance := (upper_competitor competitor competitor_bound competitor_ne).1
    have lower_distance' : abs (lower - target_value) ≤
        abs (lower - competitor_value) := by
      simpa [target_value, competitor_value] using lower_distance
    have upper_distance' : abs (upper - target_value) ≤
        abs (upper - competitor_value) := by
      simpa [target_value, competitor_value] using upper_distance
    have lower_square : (lower - target_value) ^ 2 ≤
        (lower - competitor_value) ^ 2 := by
      exact (sq_le_sq).2 lower_distance'
    have upper_square : (upper - target_value) ^ 2 ≤
        (upper - competitor_value) ^ 2 := by
      exact (sq_le_sq).2 upper_distance'
    have lower_affine : 2 * lower * (competitor_value - target_value) ≤
        competitor_value ^ 2 - target_value ^ 2 := by
      nlinarith [lower_square]
    have upper_affine : 2 * upper * (competitor_value - target_value) ≤
        competitor_value ^ 2 - target_value ^ 2 := by
      nlinarith [upper_square]
    have value_affine : 2 * value * (competitor_value - target_value) ≤
        competitor_value ^ 2 - target_value ^ 2 := by
      by_cases coefficient : 0 ≤ competitor_value - target_value
      · have scaled : 2 * value * (competitor_value - target_value) ≤
            2 * upper * (competitor_value - target_value) := by
          have doubled : 2 * value ≤ 2 * upper :=
            mul_le_mul_of_nonneg_left upper_le (by norm_num)
          exact mul_le_mul_of_nonneg_right doubled coefficient
        exact scaled.trans upper_affine
      · have coefficient' : competitor_value - target_value ≤ 0 := le_of_not_ge coefficient
        have scaled : 2 * value * (competitor_value - target_value) ≤
            2 * lower * (competitor_value - target_value) := by
          have doubled : 2 * lower ≤ 2 * value :=
            mul_le_mul_of_nonneg_left lower_le (by norm_num)
          exact mul_le_mul_of_nonpos_right doubled coefficient'
        exact scaled.trans lower_affine
    have value_square : (value - target_value) ^ 2 ≤
        (value - competitor_value) ^ 2 := by
      nlinarith [value_affine]
    have value_distance : abs (value - target_value) ≤
        abs (value - competitor_value) := by
      exact (sq_le_sq).1 value_square
    refine ⟨?_, ?_⟩
    · simpa [target_value, competitor_value]
        using value_distance
    intro equality
    by_cases even : target.val % 2 = 0
    · exact even
    · exfalso
      have lower_not_equal : abs (lower - target_value) ≠
          abs (lower - competitor_value) := by
        intro equality_lower
        exact even ((lower_competitor competitor competitor_bound competitor_ne).2
          (by simpa [target_value, competitor_value] using equality_lower))
      have upper_not_equal : abs (upper - target_value) ≠
          abs (upper - competitor_value) := by
        intro equality_upper
        exact even ((upper_competitor competitor competitor_bound competitor_ne).2
          (by simpa [target_value, competitor_value] using equality_upper))
      have lower_strict : (lower - target_value) ^ 2 <
          (lower - competitor_value) ^ 2 := by
        have habs : abs (lower - target_value) <
            abs (lower - competitor_value) := lt_of_le_of_ne lower_distance' lower_not_equal
        exact (sq_lt_sq).2 habs
      have upper_strict : (upper - target_value) ^ 2 <
          (upper - competitor_value) ^ 2 := by
        have habs : abs (upper - target_value) <
            abs (upper - competitor_value) := lt_of_le_of_ne upper_distance' upper_not_equal
        exact (sq_lt_sq).2 habs
      have lower_affine_strict : 2 * lower * (competitor_value - target_value) <
          competitor_value ^ 2 - target_value ^ 2 := by
        nlinarith [lower_strict]
      have upper_affine_strict : 2 * upper * (competitor_value - target_value) <
          competitor_value ^ 2 - target_value ^ 2 := by
        nlinarith [upper_strict]
      have value_affine_strict : 2 * value * (competitor_value - target_value) <
          competitor_value ^ 2 - target_value ^ 2 := by
        by_cases coefficient : 0 ≤ competitor_value - target_value
        · have scaled : 2 * value * (competitor_value - target_value) ≤
              2 * upper * (competitor_value - target_value) := by
            have doubled : 2 * value ≤ 2 * upper :=
              mul_le_mul_of_nonneg_left upper_le (by norm_num)
            exact mul_le_mul_of_nonneg_right doubled coefficient
          exact lt_of_le_of_lt scaled upper_affine_strict
        · have coefficient' : competitor_value - target_value ≤ 0 := le_of_not_ge coefficient
          have scaled : 2 * value * (competitor_value - target_value) ≤
              2 * lower * (competitor_value - target_value) := by
            have doubled : 2 * lower ≤ 2 * value :=
              mul_le_mul_of_nonneg_left lower_le (by norm_num)
            exact mul_le_mul_of_nonpos_right doubled coefficient'
          exact lt_of_le_of_lt scaled lower_affine_strict
      have value_strict : (value - target_value) ^ 2 <
          (value - competitor_value) ^ 2 := by
        nlinarith [value_affine_strict]
      have strict_distance : abs (value - target_value) <
          abs (value - competitor_value) := (sq_lt_sq).1 value_strict
      have equality' : abs (value - target_value) =
          abs (value - competitor_value) := by
        simpa [target_value, competitor_value] using equality
      exact (ne_of_lt strict_distance) equality'

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (201)
More program dependencies (159)
Standard-library dependencies (39)

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": "RoundsMagnitudeInterval",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "38707d8604010b3bd2ca9851f015b98b9a8fcc0510da0e28741056292f1c4f34",
  "active_statement_hash": "517a6556615284dadbb1d52c2e4748a7ca8c1805c13fc31cac5651f2500b7f53",
  "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": "sidecar-sc-20261004-081038-bac9cb-RoundsMagnitudeInterval-cycle-161",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalGoalVocabularyDefault": "2bbf2be48b5a4a4b"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "512f8d0aea894250",
    "F64": "ec4e25fbaf4e11d4"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:8bd5bb0caa0cc1fe28de3a70918dc513d9a68f848f2448cca1db2ba35b8c30ec",
  "current_fingerprint": "sound-v2:8bd5bb0caa0cc1fe28de3a70918dc513d9a68f848f2448cca1db2ba35b8c30ec",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}