P826_T1_LeftDoneCertificate

Lean checked

Statement

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

Helper · Exact successful exit of the left-normalization body

Let d,d' be decimal records and e,e' signed machine exponents. Assume d has nonpositive decimal point and that the extracted loop-1 body, started from (d,e,\mathsf{none}), successfully returns \mathsf{done}(d',e',\mathsf{none}). Then d'=d, e'=e, the decimal point of d is zero, and the first stored digit of d is at least five.

Lean statement

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

import Tablet.P826_T1_LeftStepCertificate

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P826_T1_LeftDoneCertificate]
theorem P826_T1_LeftDoneCertificate
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (e e' : Std.I32)
    (hp : d.decimal_point.val <= 0)
    (hbody :
      num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits e none =
          ok (done (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, e', none))) :
    d' = d /\ e' = e /\ d.decimal_point.val = 0 /\
      5 <= d.digits.val[0].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

Unfold the extracted loop-1 body num imp dec2flt slow parse long mantissa. Its outer test accepts the assumed nonpositive point and its option test accepts \mathsf{none}. If the point is nonzero, it is negative; the body then evaluates negation, the selector, and a left shift, and every successful terminal constructor on that branch is \mathsf{cont}, never \mathsf{done}. Splitting each possible failure, divergence, overflow, and exponent-subtraction result therefore contradicts the displayed successful \mathsf{done} equation.
The point is consequently zero. Split the checked array read at index zero. Failure or divergence contradicts success. On a successful read, if the digit is below five the body chooses shift two for digits zero and one and shift one otherwise. In all three subcases, splitting the left-shift, overflow test, and exponent subtraction again shows that every successful result is \mathsf{cont}, a contradiction. Hence the read digit is at least five. The checked-index specification identifies it with the first stored digit. In this branch the extracted body immediately returns its unchanged record and exponent, so injectivity of the result, control-flow, and product constructors gives d'=d and e'=e.

Lean proof


  unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body at hbody
  have hple : d.decimal_point <= 0#i32 := by
    change d.decimal_point.val <= 0
    omega
  simp only [hple, if_true, core.option.Option.is_none] at hbody
  simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
    true by rfl, if_true] at hbody
  by_cases hp0 : d.decimal_point.val = 0
  · have hpeq : d.decimal_point = 0#i32 :=
      IScalar.eq_of_val_eq (by simpa using hp0)
    rw [hpeq] at hbody
    simp only [if_true] at hbody
    cases hread : Array.index_usize d.digits 0#usize with
    | fail err => simp [hread] at hbody
    | div => simp [hread] at hbody
    | ok head =>
      rw [hread] at hbody
      simp only [bind_tc_ok] at hbody
      have hdZeroEta :
          ({ num_digits := d.num_digits,
             decimal_point := 0#i32,
             truncated := d.truncated,
             digits := d.digits } :
            num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
        rw [← hpeq]
      rw [hdZeroEta] at hbody
      by_cases hge : head >= 5#u8
      · simp only [hge, if_true] at hbody
        have ht := Result.ok.inj hbody
        have hs := ControlFlow.done.inj ht
        simp only [Prod.mk.injEq] at hs
        have hd : d' = d := by
          cases d
          cases d'
          simp_all only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq]
        have he : e' = e := by tauto
        have hindexBound : (0 : Nat) < d.digits.val.length := by
          rw [d.digits.property]
          norm_num
        have hspec := Array.index_usize_spec d.digits 0#usize (by
          simp [Array.length])
        rw [hread] at hspec
        simp only [WP.spec_ok] at hspec
        have hhead : d.digits.val[0] = head := by
          simpa using hspec.symm
        refine ⟨hd, he, hp0, ?_⟩
        rw [hhead]
        exact hge
      · simp only [hge, if_false] at hbody
        let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
          Std.I32 × Option num.imp.dec2flt.common.BiasedFp
        have noDone (shift : Std.Usize)
            (hh :
              (do
                let q <-
                  num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift
                if q.decimal_point >
                    num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
                then
                  ok (cont (q.num_digits, q.decimal_point, q.truncated,
                    q.digits, e,
                    some ({ m := 0#u64, p_biased := 2047#i32 } :
                      num.imp.dec2flt.common.BiasedFp)))
                else do
                  let shiftI <- lift (UScalar.hcast .I32 shift)
                  let eout <- e - shiftI
                  ok (cont (q.num_digits, q.decimal_point, q.truncated,
                    q.digits, eout, none))) =
                ok (done (d'.num_digits, d'.decimal_point, d'.truncated,
                  d'.digits, e', none) : ControlFlow State State)) : False := by
          cases hleft :
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift with
          | fail err => simp [hleft] at hh
          | div => simp [hleft] at hh
          | ok q =>
            rw [hleft] at hh
            simp only [bind_tc_ok] at hh
            by_cases hover : q.decimal_point >
                num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
            · simp [hover] at hh
            · simp only [hover, if_false, lift] at hh
              cases hsub : e - UScalar.hcast .I32 shift with
              | fail err => simp [hsub] at hh
              | div => simp [hsub] at hh
              | ok eout => simp [hsub] at hh
        split at hbody
        · exact False.elim (noDone 2#usize (by simpa using hbody))
        · exact False.elim (noDone 2#usize (by simpa using hbody))
        · exact False.elim (noDone 1#usize (by simpa using hbody))
  · have hpne : ¬ d.decimal_point = 0#i32 := by
      intro h
      have := congrArg IScalar.val h
      simp at this
      omega
    simp only [hpne, if_false] at hbody
    cases hneg : -. d.decimal_point with
    | fail err => simp [hneg] at hbody
    | div => simp [hneg] at hbody
    | ok negp =>
      rw [hneg] at hbody
      simp only [bind_tc_ok, lift] at hbody
      cases hsel :
          num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 ()
              (IScalar.hcast .Usize negp) with
      | fail err => simp [hsel] at hbody
      | div => simp [hsel] at hbody
      | ok shift =>
        rw [hsel] at hbody
        simp only [bind_tc_ok] at hbody
        cases hleft :
            num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift with
        | fail err => simp [hleft] at hbody
        | div => simp [hleft] at hbody
        | ok q =>
          rw [hleft] at hbody
          simp only [bind_tc_ok] at hbody
          by_cases hover : q.decimal_point >
              num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
          · simp [hover] at hbody
          · simp only [hover, if_false, lift] at hbody
            cases hsub : e - UScalar.hcast .I32 shift with
            | fail err => simp [hsub] at hbody
            | div => simp [hsub] at hbody
            | ok eout => simp [hsub] at hbody

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P826_T1_LeftStepCertificate

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P826_T1_LeftDoneCertificate]
theorem P826_T1_LeftDoneCertificate
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (e e' : Std.I32)
    (hp : d.decimal_point.val <= 0)
    (hbody :
      num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits e none =
          ok (done (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, e', none))) :
    d' = d /\ e' = e /\ d.decimal_point.val = 0 /\
      5 <= d.digits.val[0].val := by
-- BODY
  unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body at hbody
  have hple : d.decimal_point <= 0#i32 := by
    change d.decimal_point.val <= 0
    omega
  simp only [hple, if_true, core.option.Option.is_none] at hbody
  simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
    true by rfl, if_true] at hbody
  by_cases hp0 : d.decimal_point.val = 0
  · have hpeq : d.decimal_point = 0#i32 :=
      IScalar.eq_of_val_eq (by simpa using hp0)
    rw [hpeq] at hbody
    simp only [if_true] at hbody
    cases hread : Array.index_usize d.digits 0#usize with
    | fail err => simp [hread] at hbody
    | div => simp [hread] at hbody
    | ok head =>
      rw [hread] at hbody
      simp only [bind_tc_ok] at hbody
      have hdZeroEta :
          ({ num_digits := d.num_digits,
             decimal_point := 0#i32,
             truncated := d.truncated,
             digits := d.digits } :
            num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
        rw [← hpeq]
      rw [hdZeroEta] at hbody
      by_cases hge : head >= 5#u8
      · simp only [hge, if_true] at hbody
        have ht := Result.ok.inj hbody
        have hs := ControlFlow.done.inj ht
        simp only [Prod.mk.injEq] at hs
        have hd : d' = d := by
          cases d
          cases d'
          simp_all only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq]
        have he : e' = e := by tauto
        have hindexBound : (0 : Nat) < d.digits.val.length := by
          rw [d.digits.property]
          norm_num
        have hspec := Array.index_usize_spec d.digits 0#usize (by
          simp [Array.length])
        rw [hread] at hspec
        simp only [WP.spec_ok] at hspec
        have hhead : d.digits.val[0] = head := by
          simpa using hspec.symm
        refine ⟨hd, he, hp0, ?_⟩
        rw [hhead]
        exact hge
      · simp only [hge, if_false] at hbody
        let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
          Std.I32 × Option num.imp.dec2flt.common.BiasedFp
        have noDone (shift : Std.Usize)
            (hh :
              (do
                let q <-
                  num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift
                if q.decimal_point >
                    num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
                then
                  ok (cont (q.num_digits, q.decimal_point, q.truncated,
                    q.digits, e,
                    some ({ m := 0#u64, p_biased := 2047#i32 } :
                      num.imp.dec2flt.common.BiasedFp)))
                else do
                  let shiftI <- lift (UScalar.hcast .I32 shift)
                  let eout <- e - shiftI
                  ok (cont (q.num_digits, q.decimal_point, q.truncated,
                    q.digits, eout, none))) =
                ok (done (d'.num_digits, d'.decimal_point, d'.truncated,
                  d'.digits, e', none) : ControlFlow State State)) : False := by
          cases hleft :
              num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift with
          | fail err => simp [hleft] at hh
          | div => simp [hleft] at hh
          | ok q =>
            rw [hleft] at hh
            simp only [bind_tc_ok] at hh
            by_cases hover : q.decimal_point >
                num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
            · simp [hover] at hh
            · simp only [hover, if_false, lift] at hh
              cases hsub : e - UScalar.hcast .I32 shift with
              | fail err => simp [hsub] at hh
              | div => simp [hsub] at hh
              | ok eout => simp [hsub] at hh
        split at hbody
        · exact False.elim (noDone 2#usize (by simpa using hbody))
        · exact False.elim (noDone 2#usize (by simpa using hbody))
        · exact False.elim (noDone 1#usize (by simpa using hbody))
  · have hpne : ¬ d.decimal_point = 0#i32 := by
      intro h
      have := congrArg IScalar.val h
      simp at this
      omega
    simp only [hpne, if_false] at hbody
    cases hneg : -. d.decimal_point with
    | fail err => simp [hneg] at hbody
    | div => simp [hneg] at hbody
    | ok negp =>
      rw [hneg] at hbody
      simp only [bind_tc_ok, lift] at hbody
      cases hsel :
          num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 ()
              (IScalar.hcast .Usize negp) with
      | fail err => simp [hsel] at hbody
      | div => simp [hsel] at hbody
      | ok shift =>
        rw [hsel] at hbody
        simp only [bind_tc_ok] at hbody
        cases hleft :
            num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift with
        | fail err => simp [hleft] at hbody
        | div => simp [hleft] at hbody
        | ok q =>
          rw [hleft] at hbody
          simp only [bind_tc_ok] at hbody
          by_cases hover : q.decimal_point >
              num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
          · simp [hover] at hbody
          · simp only [hover, if_false, lift] at hbody
            cases hsub : e - UScalar.hcast .I32 shift with
            | fail err => simp [hsub] at hbody
            | div => simp [hsub] at hbody
            | ok eout => simp [hsub] at hbody

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (151)
More program dependencies (109)
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 101 entries
Technical checking records
Local closure summary
{
  "node": "P826_T1_LeftDoneCertificate",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "4b7b3457ecca020834813428e2c82bd6c1ef5ebc6f28632eee1ae352c6194b86",
  "active_statement_hash": "26be0300c3c0579bd68db3e9653a2b9e2401c1c17bb2757eb1cb45cbe53ddfe8",
  "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": "worker-result-local-closure-issuance-cycle-196",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "num_imp_dec2flt_slow_parse_long_mantissa": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "92d5f91a5a106668",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_slow_parse_long_mantissa": "62216a51d634059e",
    "num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call": "7df8a2c8e3717790"
  }
}
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:94f18d5d6c9d969ccf600e65be6426813f0db9858eb610477df36d613efc46f8",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}