P9_T1_FinalPointExact

Lean checked

Statement

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

Lemma

For any machine counts n,f whose values are at most M=9223372036854775807, and every I64 exponent e, the actual extracted finalizer returns normally. Its I32 value is zero if n=0, and otherwise \max(-2047,\min(2047,n-f+e)), with arithmetic on integers.

Lean statement

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

import Tablet.P9_T1_FinalPointArithmetic
import Tablet.num_imp_dec2flt_decimal_seq_final_decimal_point
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P9_T1_FinalPointExact]
theorem P9_T1_FinalPointExact (n f : Std.Usize) (e : Std.I64)
    (hn : n.val <= 9223372036854775807) (hf : f.val <= 9223372036854775807) :
    exists point : Std.I32,
      num.imp.dec2flt.decimal_seq.final_decimal_point n f e = ok point /\
      point.val = if n.val = 0 then 0 else
        max (-2047) (min 2047 ((n.val : Int) - f.val + e.val)) := 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

The zero branch returns before any cast. In the nonzero branch both Usize casts are exact, and n-f lies in [-M,M], so checked subtraction is exact. Signed saturation produces \max(-M-1,\min(M,n-f+e)). The constants cast exactly to I64, their negation is safe, and ordered clamping returns exactly the final interval projection. By FinalPointArithmetic, the outer projection absorbs I64 saturation. The clamped value lies in [-2047,2047], so the I32 cast is exact. This statement is about the finalizer, not about bypassing the scientific call which precedes it in the parser.

Lean proof


  have satval (a b : Std.I64) :
      (core.num.I64.saturating_add a b).val =
        max (-9223372036854775808) (min 9223372036854775807 (a.val + b.val)) := by
    simp only [core.num.I64.saturating_add, IScalar.saturating_add, IScalar.val,
      BitVec.toInt_ofInt]
    rw [bmod_pow_numBits_eq_of_lt .I64]
    · norm_num [IScalar.min, IScalar.max, IScalarTy.numBits, I64.min, I64.max, I64.numBits]
    · norm_num [IScalar.min, IScalar.max, IScalarTy.numBits, I64.min, I64.max, I64.numBits]
    · norm_num [IScalar.min, IScalar.max, IScalarTy.numBits, I64.min, I64.max, I64.numBits]
  have clampSpec (a : Std.I64) :
      core.cmp.impls.OrdI64.clamp a (-2047)#i64 2047#i64
        ⦃ p => p.val = max (-2047) (min 2047 a.val) ⦄ := by
    simp [core.cmp.impls.OrdI64.clamp, IScalar.clamp]
    split_ifs <;> simp_all [WP.spec_ok] <;> omega
  by_cases hz : n = 0#usize
  · subst n
    exact ⟨0#i32, by simp [num.imp.dec2flt.decimal_seq.final_decimal_point], by simp⟩
  · have hnz : n.val ≠ 0 := by scalar_tac
    let ni := UScalar.hcast .I64 n
    let fi := UScalar.hcast .I64 f
    have hni : ni.val = n.val := by
      simpa [WP.spec_ok, ni] using UScalar.hcast_inBounds_spec .I64 n (by scalar_tac)
    have hfi : fi.val = f.val := by
      simpa [WP.spec_ok, fi] using UScalar.hcast_inBounds_spec .I64 f (by scalar_tac)
    obtain ⟨base, hbase, hbaseval⟩ := WP.spec_imp_exists
      (IScalar.sub_spec (x := ni) (y := fi) (by scalar_tac) (by scalar_tac))
    let summed := core.num.I64.saturating_add base e
    have hsum : summed.val = max (-9223372036854775808)
        (min 9223372036854775807 ((n.val : Int) - f.val + e.val)) := by
      rw [satval, hbaseval, hni, hfi]
    obtain ⟨clipped, hclamp, hclipval⟩ := WP.spec_imp_exists (clampSpec summed)
    have hclipbounds : -2047 <= clipped.val /\ clipped.val <= 2047 := by
      rw [hclipval]
      omega
    have hcast : (IScalar.cast .I32 clipped).val = clipped.val := by
      simpa [WP.spec_ok] using IScalar.cast_inBounds_spec .I32 clipped (by scalar_tac)
    have castRange : IScalar.cast .I64 2047#i32 = 2047#i64 := by rfl
    have negRange : -. (2047#i64) = ok ((-2047)#i64) := by rfl
    refine ⟨IScalar.cast .I32 clipped, ?_, ?_⟩
    · simp only [ni, fi] at hbase
      simp only [summed] at hclamp
      simp only [num.imp.dec2flt.decimal_seq.final_decimal_point, if_neg hz,
        lift, bind_tc_ok, hbase,
        num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE.eq_def,
        castRange, negRange, hclamp]
    · rw [hcast, hclipval, hsum, P9_T1_FinalPointArithmetic.1, if_neg hnz]
end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P9_T1_FinalPointArithmetic
import Tablet.num_imp_dec2flt_decimal_seq_final_decimal_point
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model
-- [TABLET NODE: P9_T1_FinalPointExact]
theorem P9_T1_FinalPointExact (n f : Std.Usize) (e : Std.I64)
    (hn : n.val <= 9223372036854775807) (hf : f.val <= 9223372036854775807) :
    exists point : Std.I32,
      num.imp.dec2flt.decimal_seq.final_decimal_point n f e = ok point /\
      point.val = if n.val = 0 then 0 else
        max (-2047) (min 2047 ((n.val : Int) - f.val + e.val)) := by
-- BODY
  have satval (a b : Std.I64) :
      (core.num.I64.saturating_add a b).val =
        max (-9223372036854775808) (min 9223372036854775807 (a.val + b.val)) := by
    simp only [core.num.I64.saturating_add, IScalar.saturating_add, IScalar.val,
      BitVec.toInt_ofInt]
    rw [bmod_pow_numBits_eq_of_lt .I64]
    · norm_num [IScalar.min, IScalar.max, IScalarTy.numBits, I64.min, I64.max, I64.numBits]
    · norm_num [IScalar.min, IScalar.max, IScalarTy.numBits, I64.min, I64.max, I64.numBits]
    · norm_num [IScalar.min, IScalar.max, IScalarTy.numBits, I64.min, I64.max, I64.numBits]
  have clampSpec (a : Std.I64) :
      core.cmp.impls.OrdI64.clamp a (-2047)#i64 2047#i64
        ⦃ p => p.val = max (-2047) (min 2047 a.val) ⦄ := by
    simp [core.cmp.impls.OrdI64.clamp, IScalar.clamp]
    split_ifs <;> simp_all [WP.spec_ok] <;> omega
  by_cases hz : n = 0#usize
  · subst n
    exact ⟨0#i32, by simp [num.imp.dec2flt.decimal_seq.final_decimal_point], by simp⟩
  · have hnz : n.val ≠ 0 := by scalar_tac
    let ni := UScalar.hcast .I64 n
    let fi := UScalar.hcast .I64 f
    have hni : ni.val = n.val := by
      simpa [WP.spec_ok, ni] using UScalar.hcast_inBounds_spec .I64 n (by scalar_tac)
    have hfi : fi.val = f.val := by
      simpa [WP.spec_ok, fi] using UScalar.hcast_inBounds_spec .I64 f (by scalar_tac)
    obtain ⟨base, hbase, hbaseval⟩ := WP.spec_imp_exists
      (IScalar.sub_spec (x := ni) (y := fi) (by scalar_tac) (by scalar_tac))
    let summed := core.num.I64.saturating_add base e
    have hsum : summed.val = max (-9223372036854775808)
        (min 9223372036854775807 ((n.val : Int) - f.val + e.val)) := by
      rw [satval, hbaseval, hni, hfi]
    obtain ⟨clipped, hclamp, hclipval⟩ := WP.spec_imp_exists (clampSpec summed)
    have hclipbounds : -2047 <= clipped.val /\ clipped.val <= 2047 := by
      rw [hclipval]
      omega
    have hcast : (IScalar.cast .I32 clipped).val = clipped.val := by
      simpa [WP.spec_ok] using IScalar.cast_inBounds_spec .I32 clipped (by scalar_tac)
    have castRange : IScalar.cast .I64 2047#i32 = 2047#i64 := by rfl
    have negRange : -. (2047#i64) = ok ((-2047)#i64) := by rfl
    refine ⟨IScalar.cast .I32 clipped, ?_, ?_⟩
    · simp only [ni, fi] at hbase
      simp only [summed] at hclamp
      simp only [num.imp.dec2flt.decimal_seq.final_decimal_point, if_neg hz,
        lift, bind_tc_ok, hbase,
        num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE.eq_def,
        castRange, negRange, hclamp]
    · rw [hcast, hclipval, hsum, P9_T1_FinalPointArithmetic.1, if_neg hnz]
end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (46)
More program dependencies (13)
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": "P9_T1_FinalPointExact",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "fe4f0e9d3dcb48f4c0593533b6c0674ddae93276379dc9e491fb8d5d7b02e8d3",
  "active_statement_hash": "dd5981c9c5f040f5fbc145add19bb16e4c5787f09b15b877f02869f0bfa362c8",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "d5c04ffa333d9b33b238a527f5beaa2be82e021d3e9c0bbe01b1310280f7f55a",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-68",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P9_T1_FinalPointArithmetic": "31f28ca2bc131e6b",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "01b84a7a35e5bcf4",
    "num_imp_dec2flt_decimal_seq_final_decimal_point": "d2b4145772dccc04"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_final_decimal_point": "4f8088dec06d629a"
  }
}
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:66c9a0a73c2c3bd431dca1d28e19da93464a7e298772e79221183cb83d361efb",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}