SpecialHelperNan

Lean checked

Statement

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

Theorem

For every Rust byte slice spelling unsigned nan ASCII case-insensitively, and either separate sign argument, the specialized parse_inf_nan returns normally with Some of a binary64 NaN.

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.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.ModelF64Nan
import Tablet.ModelF64Neg
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: SpecialHelperNan]
theorem SpecialHelperNan :
    ∀ (s : Slice Std.U8) (negative : Bool), DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes s) none true ->
      ∃ f : F64, num.imp.dec2flt.parse.parse_inf_nan
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s negative = ok (some f) ∧ DecimalGoalVocabularyDefault.isNan 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

Fix a slice s, a Boolean negative, and the stated special-literal hypothesis. Unfolding DecimalGoalVocabularyDefault, the sign-byte list for sign none is empty and the kind Boolean true selects the nan alternative. Thus the existential body is the whole list of unsigned byte values of s, and it is related by List.Forall2 to [110,97,110]. In particular it has exactly three entries. If their values in order are a,b,c, the three component disjunctions are
a=110\mathrel\lor a+32=110,\qquad b=97\mathrel\lor b+32=97,\qquad c=110\mathrel\lor c+32=110.
Hence a,c\in\{110,78\} and b\in\{97,65\}: the bytes are respectively ASCII n/N, a/A, and n/N. Mapping a slice to its unsigned values preserves its list length, so the underlying slice also has length exactly three.
Now unfold num imp dec2flt parse parse inf nan at the supplied binary64 trait instance. Its first length comparison, with eight, is false; its following comparison, with three, is true. The indices 0, 1, and 2 are all strictly below the established length 3. Therefore the three Slice.index_usize binds succeed and return precisely the bytes with values a,b,c. Each is a u8, so its value is between 0 and 255; the checked casts to u64 are consequently in range, succeed, and preserve those three values exactly.
The next two extracted operations are specifically c <<< 16#i32 and b <<< 8#i32. For an unsigned left operand and signed count, Aeneas's UScalar-by-IScalar shift first checks that the signed count is nonnegative, converts it to its natural value, and checks that value against the operand width. The literal counts 16 and 8 are nonnegative, retain those natural values, and are both strictly below the u64 width 64, so neither shift can fail. Nor is either exact value truncated: from b,c\leq255,
c2^{16}\leq255\,2^{16}<2^{24}<2^{64},\qquad b2^8\leq255\,2^8<2^{16}<2^{64}.
The shifted results are therefore exactly c2^{16} and b2^8. Their possibly occupied bit ranges, 16--23 and 8--15, are disjoint, and the widened a occupies only bits 0--7. Both extracted OR binds succeed by ModelU64BitOr and assemble, without carries or truncation, the little-endian word
r=(c2^{16})\mathbin{\mathtt{|}}(b2^8)\mathbin{\mathtt{|}}a =a+b2^8+c2^{16}<2^{24}.
The following extracted AND succeeds by ModelU64BitAnd. Its mask 16131858542891098079=\mathtt{0xdfdfdfdfdfdfdfdf} has byte 0xdf in every position; 0xdf has every relevant ASCII bit set except the case bit 0x20. Accordingly each of the low three bytes of r has exactly that bit cleared, while the five high zero bytes stay zero. For a,c\in\{\mathtt{0x6e},\mathtt{0x4e}\} the masked byte is 0x4e, and for b\in\{\mathtt{0x61},\mathtt{0x41}\} it is 0x41. The masked word is thus
\mathtt{0x4e}+\mathtt{0x41}\,2^8+\mathtt{0x4e}\,2^{16} =\mathtt{0x4e414e}=5128526.
This is exactly the third displayed three-byte match in the extracted definition, so execution selects its NAN branch rather than any infinity, rejection, or default branch.
Unfolding the cited binary64 trait record F64 Insts Dec2flt modelNumImpTraitsFloatU64, that branch's NAN lookup is the constant operation whose direct paired semantics is ModelF64Nan. It succeeds with the raw word
q=\mathtt{0x7ff8000000000000} =2047\,2^{52}+2^{51}.
If negative is false, the extracted conditional returns ok (some q) directly. Because q<2^{63}, reducing it modulo 2^{63} leaves q; hence its exponent observation is q/2^{52}=2047 and its fraction observation is q\bmod 2^{52}=2^{51}\ne0. By the isNan field of DecimalGoalVocabularyDefault, q is therefore a NaN.
If negative is true, the record's coreopsarithNegInst selects the extracted binary64 negation instance, whose direct paired operation ModelF64Neg succeeds by toggling exactly raw bit 63. That bit is zero in q, so the returned word is
q'=q+2^{63}=\mathtt{0xfff8000000000000}.
The branch consequently returns ok (some q'). Moreover q'\bmod2^{63}=q, since 0\le q<2^{63}. Its sign-insensitive exponent and fraction observations are again respectively 2047 and the nonzero value 2^{51}, so the same isNan definition proves that q' is a NaN. Taking f=q in the false branch and f=q' in the true branch supplies the required witness for both exhaustive values of negative.

Lean proof


  intro s negative h
  dsimp [DecimalGoalVocabularyDefault] at h
  rcases h with ⟨body, hbody, hfor⟩
  have hlen : s.val.length = 3 := by
    rw [← List.length_map (fun b : Std.U8 => b.val), hbody]
    exact List.Forall₂.length_eq hfor
  obtain ⟨a, b, c, hvals⟩ := (List.length_eq_three).mp hlen
  rcases s with ⟨vals, bound⟩
  dsimp at hvals
  subst vals
  rw [← hbody] at hfor
  simp at hfor
  have value_identity (x : Std.U8) : x = @UScalar.ofNatCore .U8 x.val x.bv.isLt := by
    apply UScalar.eq_of_val_eq
    simp
  have cast110 : UScalar.cast .U64 (110#u8) = 110#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have cast78 : UScalar.cast .U64 (78#u8) = 78#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have cast97 : UScalar.cast .U64 (97#u8) = 97#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have cast65 : UScalar.cast .U64 (65#u8) = 65#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have shift110 : (110#u64 <<< 16#i32) = ok (7208960#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have shift78 : (78#u64 <<< 16#i32) = ok (5111808#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have shift97 : (97#u64 <<< 8#i32) = ok (24832#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have shift65 : (65#u64 <<< 8#i32) = ok (16640#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have maskA : ((7208960#u64 ||| 24832#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskB : ((7208960#u64 ||| 24832#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskC : ((7208960#u64 ||| 16640#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskD : ((7208960#u64 ||| 16640#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskE : ((5111808#u64 ||| 24832#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskF : ((5111808#u64 ||| 24832#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskG : ((5111808#u64 ||| 16640#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskH : ((5111808#u64 ||| 16640#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have tagTarget : (5128526#u64 : Std.U64) = 5128526#uscalar := by
    apply UScalar.eq_of_val_eq
    rfl
  have length_value : Usize.ofNatCore 3 (by scalar_tac) = 3#usize := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.ofNatCore_val_eq]
  have nan_value : F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.NAN =
      ok (9221120237041090560#u64) := by
    simp only [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.NAN,
      core.f64.F64.NAN, ModelF64Nan]
    rfl
  have neg_nan_value : F64.Insts.CoreOpsArithNegF64.neg
      (9221120237041090560#u64) = ok (18444492273895866368#u64) := by
    simp only [F64.Insts.CoreOpsArithNegF64.neg, ModelF64Neg]
    rfl
  rcases hfor with ⟨ha | ha, hb | hb, hc | hc⟩
  all_goals
    rw [value_identity a, value_identity b, value_identity c]
    simp [ha, hb, hc]
    cases negative
    · refine ⟨9221120237041090560#u64, ?_, ?_⟩
      · simp [num.imp.dec2flt.parse.parse_inf_nan, Slice.len, Slice.index_usize,
          lift, cast110, cast78, cast97, cast65, shift110, shift78, shift97,
          shift65, maskA, maskB, maskC, maskD, maskE, maskF, maskG, maskH,
          tagTarget, length_value, nan_value, neg_nan_value]
      · dsimp [DecimalGoalVocabularyDefault]
        norm_num
    · refine ⟨18444492273895866368#u64, ?_, ?_⟩
      · simp [num.imp.dec2flt.parse.parse_inf_nan, Slice.len, Slice.index_usize,
          lift, cast110, cast78, cast97, cast65, shift110, shift78, shift97,
          shift65, maskA, maskB, maskC, maskD, maskE, maskF, maskG, maskH,
          tagTarget, length_value, nan_value, neg_nan_value]
      · dsimp [DecimalGoalVocabularyDefault]
        norm_num

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalGoalVocabularyDefault
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.ModelF64Nan
import Tablet.ModelF64Neg
import Tablet.ModelU64BitAnd
import Tablet.ModelU64BitOr
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: SpecialHelperNan]
theorem SpecialHelperNan :
    ∀ (s : Slice Std.U8) (negative : Bool), DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes s) none true ->
      ∃ f : F64, num.imp.dec2flt.parse.parse_inf_nan
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 s negative = ok (some f) ∧ DecimalGoalVocabularyDefault.isNan f := by
-- BODY
  intro s negative h
  dsimp [DecimalGoalVocabularyDefault] at h
  rcases h with ⟨body, hbody, hfor⟩
  have hlen : s.val.length = 3 := by
    rw [← List.length_map (fun b : Std.U8 => b.val), hbody]
    exact List.Forall₂.length_eq hfor
  obtain ⟨a, b, c, hvals⟩ := (List.length_eq_three).mp hlen
  rcases s with ⟨vals, bound⟩
  dsimp at hvals
  subst vals
  rw [← hbody] at hfor
  simp at hfor
  have value_identity (x : Std.U8) : x = @UScalar.ofNatCore .U8 x.val x.bv.isLt := by
    apply UScalar.eq_of_val_eq
    simp
  have cast110 : UScalar.cast .U64 (110#u8) = 110#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have cast78 : UScalar.cast .U64 (78#u8) = 78#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have cast97 : UScalar.cast .U64 (97#u8) = 97#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have cast65 : UScalar.cast .U64 (65#u8) = 65#u64 := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.cast_val_eq]
  have shift110 : (110#u64 <<< 16#i32) = ok (7208960#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have shift78 : (78#u64 <<< 16#i32) = ok (5111808#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have shift97 : (97#u64 <<< 8#i32) = ok (24832#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have shift65 : (65#u64 <<< 8#i32) = ok (16640#u64) := by
    simp only [HShiftLeft.hShiftLeft, UScalar.shiftLeft_IScalar, UScalar.shiftLeft]
    rw [if_pos (by norm_num), if_pos (by norm_num)]
    congr 1
  have maskA : ((7208960#u64 ||| 24832#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskB : ((7208960#u64 ||| 24832#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskC : ((7208960#u64 ||| 16640#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskD : ((7208960#u64 ||| 16640#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskE : ((5111808#u64 ||| 24832#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskF : ((5111808#u64 ||| 24832#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskG : ((5111808#u64 ||| 16640#u64 ||| 110#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have maskH : ((5111808#u64 ||| 16640#u64 ||| 78#u64) &&&
      16131858542891098079#u64) = 5128526#u64 := by rfl
  have tagTarget : (5128526#u64 : Std.U64) = 5128526#uscalar := by
    apply UScalar.eq_of_val_eq
    rfl
  have length_value : Usize.ofNatCore 3 (by scalar_tac) = 3#usize := by
    apply UScalar.eq_of_val_eq
    simp [UScalar.ofNatCore_val_eq]
  have nan_value : F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.NAN =
      ok (9221120237041090560#u64) := by
    simp only [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.NAN,
      core.f64.F64.NAN, ModelF64Nan]
    rfl
  have neg_nan_value : F64.Insts.CoreOpsArithNegF64.neg
      (9221120237041090560#u64) = ok (18444492273895866368#u64) := by
    simp only [F64.Insts.CoreOpsArithNegF64.neg, ModelF64Neg]
    rfl
  rcases hfor with ⟨ha | ha, hb | hb, hc | hc⟩
  all_goals
    rw [value_identity a, value_identity b, value_identity c]
    simp [ha, hb, hc]
    cases negative
    · refine ⟨9221120237041090560#u64, ?_, ?_⟩
      · simp [num.imp.dec2flt.parse.parse_inf_nan, Slice.len, Slice.index_usize,
          lift, cast110, cast78, cast97, cast65, shift110, shift78, shift97,
          shift65, maskA, maskB, maskC, maskD, maskE, maskF, maskG, maskH,
          tagTarget, length_value, nan_value, neg_nan_value]
      · dsimp [DecimalGoalVocabularyDefault]
        norm_num
    · refine ⟨18444492273895866368#u64, ?_, ?_⟩
      · simp [num.imp.dec2flt.parse.parse_inf_nan, Slice.len, Slice.index_usize,
          lift, cast110, cast78, cast97, cast65, shift110, shift78, shift97,
          shift65, maskA, maskB, maskC, maskD, maskE, maskF, maskG, maskH,
          tagTarget, length_value, nan_value, neg_nan_value]
      · dsimp [DecimalGoalVocabularyDefault]
        norm_num

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (199)
More program dependencies (157)
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 80 entries
Technical checking records
Local closure summary
{
  "node": "SpecialHelperNan",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "6b715dd6c9bcb70ca50589cd44a2fc47414a5f6e09afe59fc4424675f19ebbf9",
  "active_statement_hash": "5c9ab0130a1dc6a0871c25eab4320612cf5ce1168da1957a5dcee85705d7e6fb",
  "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-181",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_NAN": "037dda431b30bc2b",
    "num_imp_dec2flt_parse_parse_inf_nan": "8b27e8518e48262f"
  },
  "strict_definition_deps": {
    "Binary64Model": "5786328d6a028d83",
    "Binary64ModelDefault": "47013ebe5c83894c",
    "DecimalGoalVocabulary": "daa08120138b32b6",
    "DecimalGoalVocabularyDefault": "71a18a6c119a3cdc",
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_INFINITY": "618a328783684238",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64_NAN": "faf51f3aa8d94806",
    "ModelF64Nan": "a89b59f88db8f8ee",
    "ModelF64Neg": "78a021de99eb373b",
    "Preamble": "5575719dcb1443eb",
    "SliceU8_Insts_Dec2flt_modelNumImpDec2fltCommonByteSlice_read_u64": "279b0c1cd8ec9e39",
    "core_ops_arith_Neg": "0ff07135a60bcbf9",
    "num_imp_dec2flt_parse_parse_inf_nan": "f0fbcb51738b1e4e",
    "num_imp_traits_Float": "062e1d14cad682f2"
  }
}
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:1520d21dc040cba72841bfeca4ce6f56ae26f0344f8cb9590305efa5f14b3033",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}