P14_NativeTotality

Lean checked

Statement

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

Lemma

For every Decimal record, with arbitrary values of all four fields, the native operation of ActualDispatchOperations returns normally with an Option of a binary64 word. There is no input length, reachability, lexical, or normal-return premise.

Lean statement

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

import Tablet.Binary64FastPathGuard
import Tablet.Binary64IntPow10Index
import Tablet.Binary64ApplySign
import Tablet.P14_NativeMultiplyRounding
import Tablet.P14_NativeDivideRounding
import Tablet.P9_T1_ActualDispatchOperations

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: P14_NativeTotality]
theorem P14_NativeTotality :
    forall d : num.imp.dec2flt.decimal.Decimal,
      exists r : Option F64, P9_T1_ActualDispatchOperations.native d = ok r := 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

Expand the selected native operation in ActualDispatchOperations through the frozen definition num imp dec2flt decimal Decimal try fast path. The precision-setting operation num imp dec2flt fpu fpu precision set precision returns unit. By Binary64FastPathGuard, the subsequent guard always returns normally. If it is false, the function returns None immediately. If true, write the exponent and mantissa as q,w; the lemma gives -22\le q\le37 and w\le2^{53}. The maximum ordinary exponent is 22 by F64 Insts Dec2flt modelNumImpDec2fltLemireU64 MAX EXPONENT FAST PATH.
For q\le22 and q<0, -q\in[1,22], so I64 negation is checked and successful and the ensuing Usize cast is exact on the 64-bit target. By P14 NativeDivideRounding, conversion of w, lookup at -q, and division all return normally, with a clear-sign magnitude. For 0\le q\le22, the Usize cast is exact and P14 NativeMultiplyRounding instead supplies successful conversion, lookup and multiplication, again with a clear-sign magnitude. In either case Binary64ApplySign supplies the successful final conditional negation and Some return. Its negation is the selected ModelF64Neg, so it is the actual final computation.
For q>22, the guard gives 23\le q\le37. Thus checked I64 subtraction of 22 succeeds with a value s\in[1,15], its Usize cast is exact, and Binary64IntPow10Index gives successful indexing of the integer power table. U64 checked multiplication is a total Option-valued operation, including when the mathematical product exceeds U64 maximum. If its value is None, ModelOptionBranch chooses the residual branch and ModelOptionFromResidual returns None normally. If it is Some of v, the next constant computation returns 2^{53}: the extracted selected Lemire implementation F64 Insts Dec2flt modelNumImpDec2fltLemireU64 uses num imp dec2flt Lemire MAX MANTISSA FAST PATH default, with significand width 53 from F64 Insts Dec2flt modelNumImpTraitsFloatU64 SIG TOTAL BITS. Shifting one by 53 is legal for U64 and returns that exact constant. The comparison is total. If v>2^{53} the branch returns None. Otherwise the cast of 22 to Usize is exact and P14 NativeMultiplyRounding supplies all remaining conversion, lookup and multiplication returns for v and index 22. Binary64ApplySign then supplies the final signed Some.
The native operation uses the modeled multiply, divide and negation fields in its selected extracted instances; thus every success just cited replaces the exact corresponding source bind. Every checked integer operation and table index preceding those binds has an explicit in-range bound above. The initial guard, ordinary exponent test, exponent sign test, checked-product Option and product-size test exhaust every control-flow branch, and each branch constructs a normal output. This establishes totality directly, without assuming it from an on-normal correctness contract.

Lean proof


  intro d
  obtain ⟨b, hb, hguard⟩ := Binary64FastPathGuard d
  change exists r : Option F64,
    num.imp.dec2flt.decimal.Decimal.try_fast_path
      F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
      F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
      F64.Insts.CoreOpsArithNegF64 d = ok r
  unfold num.imp.dec2flt.decimal.Decimal.try_fast_path
  simp only [num.imp.dec2flt.fpu.fpu_precision.set_precision, bind_tc_ok]
  rw [hb]
  cases b with
  | false => exact ⟨none, rfl⟩
  | true =>
    obtain ⟨hqMin, hqMax, hv, _⟩ := hguard.mp rfl
    simp only [if_true, bind_tc_ok]
    rw [F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.MAX_EXPONENT_FAST_PATH.eq_def]
    have finish : forall magnitude : F64, magnitude.val < 2 ^ 63 ->
        exists r : Option F64,
          (if d.negative then
            do let f <- ModelF64Neg magnitude; ok (some f)
           else ok (some magnitude)) = ok r := by
      intro magnitude hclear
      obtain ⟨f, hf, _⟩ := Binary64ApplySign d.negative magnitude hclear
      exact ⟨some f, hf⟩
    by_cases ordinary : d.exponent.val <= 22
    · have hordinary : d.exponent <= 22#i64 := ordinary
      simp only [hordinary, if_true]
      by_cases negativeExponent : d.exponent.val < 0
      · have hnegativeExponent : d.exponent < 0#i64 := negativeExponent
        simp only [hnegativeExponent, if_true]
        obtain ⟨q, hq, hqVal⟩ := WP.spec_imp_exists
          (IScalar.neg_step d.exponent (by scalar_tac))
        change (-. d.exponent) = ok q at hq
        have qVal : q.val = -d.exponent.val := hqVal
        obtain ⟨i, hi, hiVal⟩ := WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize q (by scalar_tac))
        obtain ⟨a, p, m, ha, hp, hm, hclear, _⟩ :=
          P14_NativeDivideRounding d.mantissa i hv (by omega)
        change F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.from_u64 d.mantissa = ok a at ha
        rw [ha]
        simp only [bind_tc_ok]
        rw [hq]
        simp only [bind_tc_ok]
        rw [hi]
        simp only [bind_tc_ok]
        rw [hp]
        simp only [bind_tc_ok]
        change F64.Insts.CoreOpsArithDivF64F64.div a p = ok m at hm
        rw [hm]
        simp only [bind_tc_ok]
        exact finish m hclear
      · have hnegativeExponent : ¬ d.exponent < 0#i64 := negativeExponent
        simp only [hnegativeExponent, if_false]
        obtain ⟨i, hi, hiVal⟩ := WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize d.exponent (by scalar_tac))
        obtain ⟨a, p, m, ha, hp, hm, hclear, _⟩ :=
          P14_NativeMultiplyRounding d.mantissa i hv (by omega)
        change F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.from_u64 d.mantissa = ok a at ha
        rw [ha]
        simp only [bind_tc_ok]
        rw [hi]
        simp only [bind_tc_ok]
        rw [hp]
        simp only [bind_tc_ok]
        change F64.Insts.CoreOpsArithMulF64F64.mul a p = ok m at hm
        rw [hm]
        simp only [bind_tc_ok]
        exact finish m hclear
    · have hordinary : ¬ d.exponent <= 22#i64 := ordinary
      simp only [hordinary, if_false]
      obtain ⟨shift, hshift, hshiftVal⟩ := WP.spec_imp_exists
        (IScalar.sub_spec (x := d.exponent) (y := 22#i64) (by scalar_tac) (by scalar_tac))
      have shiftVal : shift.val = d.exponent.val - 22 := hshiftVal
      rw [hshift]
      simp only [bind_tc_ok]
      obtain ⟨index, hindex, hindexVal⟩ := WP.spec_imp_exists
        (IScalar.hcast_inBounds_spec .Usize shift (by scalar_tac))
      rw [hindex]
      simp only [bind_tc_ok]
      obtain ⟨power, hpower, _⟩ := Binary64IntPow10Index index (by omega)
      rw [hpower]
      simp only [bind_tc_ok, lift]
      cases hproduct : U64.checked_mul d.mantissa power with
      | none =>
        exact ⟨none, rfl⟩
      | some v =>
        rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def]
        change exists r : Option F64,
          (if v > 9007199254740992#u64 then ok none else _) = ok r
        by_cases tooLarge : v.val > 2 ^ 53
        · have htooLarge : v > 9007199254740992#u64 := tooLarge
          simp only [htooLarge, if_true]
          exact ⟨none, rfl⟩
        · have htooLarge : ¬ v > 9007199254740992#u64 := tooLarge
          simp only [htooLarge, if_false]
          obtain ⟨a, p, m, ha, hp, hm, hclear, _⟩ :=
            P14_NativeMultiplyRounding v 22#usize (by omega) (by norm_num)
          change F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.from_u64 v = ok a at ha
          rw [ha]
          simp only [bind_tc_ok]
          have cast22 : IScalar.hcast .Usize 22#i64 = 22#usize := by
            have spec := IScalar.hcast_inBounds_spec .Usize 22#i64 (by scalar_tac)
            simp only [lift, WP.spec_ok] at spec
            apply UScalar.eq_of_val_eq
            change (IScalar.hcast .Usize 22#i64).val = 22
            change ((IScalar.hcast .Usize 22#i64).val : Int) = 22 at spec
            omega
          rw [cast22]
          rw [hp]
          simp only [bind_tc_ok]
          change F64.Insts.CoreOpsArithMulF64F64.mul a p = ok m at hm
          rw [hm]
          simp only [bind_tc_ok]
          exact finish m hclear

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.Binary64FastPathGuard
import Tablet.Binary64IntPow10Index
import Tablet.Binary64ApplySign
import Tablet.P14_NativeMultiplyRounding
import Tablet.P14_NativeDivideRounding
import Tablet.P9_T1_ActualDispatchOperations

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 1000000
set_option maxHeartbeats 2000000

namespace dec2flt_model

-- [TABLET NODE: P14_NativeTotality]
theorem P14_NativeTotality :
    forall d : num.imp.dec2flt.decimal.Decimal,
      exists r : Option F64, P9_T1_ActualDispatchOperations.native d = ok r := by
-- BODY
  intro d
  obtain ⟨b, hb, hguard⟩ := Binary64FastPathGuard d
  change exists r : Option F64,
    num.imp.dec2flt.decimal.Decimal.try_fast_path
      F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
      F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
      F64.Insts.CoreOpsArithNegF64 d = ok r
  unfold num.imp.dec2flt.decimal.Decimal.try_fast_path
  simp only [num.imp.dec2flt.fpu.fpu_precision.set_precision, bind_tc_ok]
  rw [hb]
  cases b with
  | false => exact ⟨none, rfl⟩
  | true =>
    obtain ⟨hqMin, hqMax, hv, _⟩ := hguard.mp rfl
    simp only [if_true, bind_tc_ok]
    rw [F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.MAX_EXPONENT_FAST_PATH.eq_def]
    have finish : forall magnitude : F64, magnitude.val < 2 ^ 63 ->
        exists r : Option F64,
          (if d.negative then
            do let f <- ModelF64Neg magnitude; ok (some f)
           else ok (some magnitude)) = ok r := by
      intro magnitude hclear
      obtain ⟨f, hf, _⟩ := Binary64ApplySign d.negative magnitude hclear
      exact ⟨some f, hf⟩
    by_cases ordinary : d.exponent.val <= 22
    · have hordinary : d.exponent <= 22#i64 := ordinary
      simp only [hordinary, if_true]
      by_cases negativeExponent : d.exponent.val < 0
      · have hnegativeExponent : d.exponent < 0#i64 := negativeExponent
        simp only [hnegativeExponent, if_true]
        obtain ⟨q, hq, hqVal⟩ := WP.spec_imp_exists
          (IScalar.neg_step d.exponent (by scalar_tac))
        change (-. d.exponent) = ok q at hq
        have qVal : q.val = -d.exponent.val := hqVal
        obtain ⟨i, hi, hiVal⟩ := WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize q (by scalar_tac))
        obtain ⟨a, p, m, ha, hp, hm, hclear, _⟩ :=
          P14_NativeDivideRounding d.mantissa i hv (by omega)
        change F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.from_u64 d.mantissa = ok a at ha
        rw [ha]
        simp only [bind_tc_ok]
        rw [hq]
        simp only [bind_tc_ok]
        rw [hi]
        simp only [bind_tc_ok]
        rw [hp]
        simp only [bind_tc_ok]
        change F64.Insts.CoreOpsArithDivF64F64.div a p = ok m at hm
        rw [hm]
        simp only [bind_tc_ok]
        exact finish m hclear
      · have hnegativeExponent : ¬ d.exponent < 0#i64 := negativeExponent
        simp only [hnegativeExponent, if_false]
        obtain ⟨i, hi, hiVal⟩ := WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize d.exponent (by scalar_tac))
        obtain ⟨a, p, m, ha, hp, hm, hclear, _⟩ :=
          P14_NativeMultiplyRounding d.mantissa i hv (by omega)
        change F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.from_u64 d.mantissa = ok a at ha
        rw [ha]
        simp only [bind_tc_ok]
        rw [hi]
        simp only [bind_tc_ok]
        rw [hp]
        simp only [bind_tc_ok]
        change F64.Insts.CoreOpsArithMulF64F64.mul a p = ok m at hm
        rw [hm]
        simp only [bind_tc_ok]
        exact finish m hclear
    · have hordinary : ¬ d.exponent <= 22#i64 := ordinary
      simp only [hordinary, if_false]
      obtain ⟨shift, hshift, hshiftVal⟩ := WP.spec_imp_exists
        (IScalar.sub_spec (x := d.exponent) (y := 22#i64) (by scalar_tac) (by scalar_tac))
      have shiftVal : shift.val = d.exponent.val - 22 := hshiftVal
      rw [hshift]
      simp only [bind_tc_ok]
      obtain ⟨index, hindex, hindexVal⟩ := WP.spec_imp_exists
        (IScalar.hcast_inBounds_spec .Usize shift (by scalar_tac))
      rw [hindex]
      simp only [bind_tc_ok]
      obtain ⟨power, hpower, _⟩ := Binary64IntPow10Index index (by omega)
      rw [hpower]
      simp only [bind_tc_ok, lift]
      cases hproduct : U64.checked_mul d.mantissa power with
      | none =>
        exact ⟨none, rfl⟩
      | some v =>
        rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def]
        change exists r : Option F64,
          (if v > 9007199254740992#u64 then ok none else _) = ok r
        by_cases tooLarge : v.val > 2 ^ 53
        · have htooLarge : v > 9007199254740992#u64 := tooLarge
          simp only [htooLarge, if_true]
          exact ⟨none, rfl⟩
        · have htooLarge : ¬ v > 9007199254740992#u64 := tooLarge
          simp only [htooLarge, if_false]
          obtain ⟨a, p, m, ha, hp, hm, hclear, _⟩ :=
            P14_NativeMultiplyRounding v 22#usize (by omega) (by norm_num)
          change F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64.from_u64 v = ok a at ha
          rw [ha]
          simp only [bind_tc_ok]
          have cast22 : IScalar.hcast .Usize 22#i64 = 22#usize := by
            have spec := IScalar.hcast_inBounds_spec .Usize 22#i64 (by scalar_tac)
            simp only [lift, WP.spec_ok] at spec
            apply UScalar.eq_of_val_eq
            change (IScalar.hcast .Usize 22#i64).val = 22
            change ((IScalar.hcast .Usize 22#i64).val : Int) = 22 at spec
            omega
          rw [cast22]
          rw [hp]
          simp only [bind_tc_ok]
          change F64.Insts.CoreOpsArithMulF64F64.mul a p = ok m at hm
          rw [hm]
          simp only [bind_tc_ok]
          exact finish m hclear

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (198)
More program dependencies (156)
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 160 entries
Technical checking records
Local closure summary
{
  "node": "P14_NativeTotality",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "48851031bbafdef4a3d8c89a77f763aa18f6ab89aee27e97f50c4f1817c278b3",
  "active_statement_hash": "ca494f74434d70a256498eceea53cdb09c4d8d2b98ba7cf1d944258e14d16e6d",
  "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-199",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "Binary64ApplySign": "d55a8c73d5261f86",
    "Binary64FastPathGuard": "6aedeb16425b5403",
    "Binary64IntPow10Index": "335765edc9734a2e",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_MAX_EXPONENT_FAST_PATH": "6bf5ea9f18f5e8dc",
    "P14_NativeDivideRounding": "1b9eb29cb2f0528e",
    "P14_NativeMultiplyRounding": "6ba584cabf2f30bd",
    "num_imp_dec2flt_Lemire_MAX_MANTISSA_FAST_PATH_default": "32d9110750ebf062",
    "num_imp_dec2flt_decimal_Decimal_try_fast_path": "ba81300a2601d639"
  },
  "strict_definition_deps": {
    "DecimalGoalVocabulary": "f76c8174a4d58eb5",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_CoreOpsArithDivF64F64": "dc44e13a99fc6bed",
    "F64_Insts_CoreOpsArithMulF64F64": "1ae1a9021a654abd",
    "F64_Insts_CoreOpsArithNegF64": "2d9be310c546ea2e",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64": "30963b4ef656cc79",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_MAX_EXPONENT_FAST_PATH": "c8b07a02533b83c6",
    "F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64_pow10_fast_path": "c0ce6fdd5caeca22",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_SIG_TOTAL_BITS": "887b52d84a3fc9c4",
    "ModelF64Div": "f8da2e585a4f4183",
    "ModelF64FromU64": "97a26f5bc593ff7a",
    "ModelF64Mul": "ba97971db93c2ae2",
    "ModelF64Neg": "78a021de99eb373b",
    "P9_T1_ActualDispatchOperations": "d8bf51ebbbf153ac",
    "P9_T1_DispatchOperations": "064b44803eb69a33",
    "Preamble": "ebc6971dd5f6042c",
    "core_ops_arith_Div": "2704fb3c3065e0f5",
    "core_ops_arith_Mul": "91e8d5ef661a3d2a",
    "core_ops_arith_Neg": "0ff07135a60bcbf9",
    "num_imp_dec2flt_Lemire": "dee8bafa6a4122e2",
    "num_imp_dec2flt_decimal_Decimal": "fe99f3f976739f2d",
    "num_imp_dec2flt_decimal_Decimal_can_use_fast_path": "11145d187f88b02e",
    "num_imp_dec2flt_decimal_Decimal_try_fast_path": "2e4888f65a06b49f",
    "num_imp_dec2flt_decimal_INT_POW10": "48c78c3dca210847",
    "num_imp_dec2flt_fpu_fpu_precision_set_precision": "73042c31057f9ab9"
  }
}
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:071e9bef827298ee47f1bab63c2fa61641e90cc292066b0700fd1f8a6a372e43",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}