P866_T1_RightShiftPointNonincrease

Lean checked

Statement

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

Lemma · A nonempty right shift does not increase the decimal point

Let d have a positive base-ten fold in its recorded prefix and let every recorded cell be a decimal digit. If the extracted right shift by k\leq60 succeeds with a nonempty output d', then
d'.\mathit{decimal\_point}\leq d.\mathit{decimal\_point}.

Lean statement

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

import Tablet.DecimalRightShiftPrefixLoopSemantics

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P866_T1_RightShiftPointNonincrease]
theorem P866_T1_RightShiftPointNonincrease :
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      shift.val <= 60 ->
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      0 < digits.foldl (fun n digit => 10 * n + digit) 0 ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' ->
      d'.num_digits.val != 0 ->
      d'.decimal_point.val <= d.decimal_point.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 right-shift call and name the result of its prefix loop (d_0,r,a,z). Its successful return equation, the positive input fold, the decimal-cell hypothesis, and k\leq60 meet all hypotheses of DecimalRightShiftPrefixLoopSemantics. That theorem states d_0=d, z=\mathsf{false}, and 1\leq r. Its two alternatives also give either r\leq d.\mathit{num\_digits}\leq768 or r\leq d.\mathit{num\_digits}+k\leq828. Hence the extracted cast of r to a signed 32-bit integer is in range and has integer value r.
Because the whole right shift returns successfully, its checked subtraction of one from that cast and its subsequent checked point subtraction both succeed. The scalar subtraction specifications therefore identify the prospective point as
p'=d.\mathit{decimal\_point}-(r-1) =d.\mathit{decimal\_point}-r+1. \tag{1}
If p' is below the extracted point range, the definition returns the reset record, whose count is zero; this contradicts the assumed nonempty output. Thus the nonreset branch is taken.
Invert in turn the successful checked power, mask subtraction, main digit loop, and final digit loop. These operations can alter the digits, count, and sticky flag, but the record passed to the final extracted trim has point exactly p'. Unfolding that successful trim shows that its assertion and trim loop succeed and that its returned structure changes only \mathit{num\_digits}; consequently d'.\mathit{decimal\_point}=p'. Finally 1\leq r in (1) gives p'\leq d.\mathit{decimal\_point}, which is the claim.

Lean proof


  dsimp only
  intro d d' shift hshift hpositive hdigit hcall hnonzero
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift at hcall
  cases hloop0 :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
        d shift 0#usize 0#u64 false with
  | fail e => simp [hloop0] at hcall
  | div => simp [hloop0] at hcall
  | ok out0 =>
    rcases out0 with ⟨d0, read, acc, allZero⟩
    simp only [hloop0, bind_tc_ok] at hcall
    have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc
      allZero hshift hpositive hdigit hloop0
    dsimp only at hpref
    rcases hpref with ⟨hd0, hzero, hreadpos, _, _, _⟩
    subst d0
    subst allZero
    change (do
      let i ← lift (UScalar.hcast .I32 read)
      let i1 ← i - 1#i32
      let i2 ← d.decimal_point - i1
      let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
      if i2 < i3 then
        ok
          { d
            with
            num_digits := 0#usize, decimal_point := 0#i32,
            truncated := false }
      else
        let i4 ← 1#u64 <<< shift
        let mask ← i4 - 1#u64
        let (a, writeIndex, n1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
            d.num_digits d.digits shift read 0#usize acc mask
        let (b, a1, writeIndex1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
            d.truncated a shift writeIndex n1 mask
        num.imp.dec2flt.decimal_seq.DecimalSeq.trim
          { num_digits := writeIndex1, decimal_point := i2,
            truncated := b, digits := a1 }) = ok d' at hcall
    let readI : Std.I32 := UScalar.hcast .I32 read
    rw [show UScalar.hcast .I32 read = readI by rfl] at hcall
    simp only [lift, bind_tc_ok] at hcall
    have hreadIVal : readI.val = (read.val : Int) := by
      have hs := UScalar.hcast_inBounds_spec .I32 read (by
        rw [IScalar.max_I32_eq]
        omega)
      simp only [lift, WP.spec_ok] at hs
      exact hs
    cases hsub : readI - 1#i32 with
    | fail e => rw [hsub] at hcall; simp at hcall
    | div => rw [hsub] at hcall; simp at hcall
    | ok readMinus =>
      rw [hsub] at hcall
      simp only [bind_tc_ok] at hcall
      have hsubVal : readMinus.val = readI.val - 1 := by
        have hs := IScalar.sub_equiv readI 1#i32
        rw [hsub] at hs
        exact hs.2.1
      cases hpoint : d.decimal_point - readMinus with
      | fail e => simp [hpoint] at hcall
      | div => simp [hpoint] at hcall
      | ok point =>
        rw [hpoint] at hcall
        simp only [bind_tc_ok] at hcall
        have hpointVal : point.val =
            d.decimal_point.val - (read.val : Int) + 1 := by
          have hs := IScalar.sub_equiv d.decimal_point readMinus
          rw [hpoint] at hs
          rw [hsubVal, hreadIVal] at hs
          omega
        cases hnegRange :
            -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE with
        | fail e => simp [hnegRange] at hcall
        | div => simp [hnegRange] at hcall
        | ok negRange =>
          rw [hnegRange] at hcall
          simp only [bind_tc_ok] at hcall
          by_cases hreset : point < negRange
          · simp only [hreset, if_true] at hcall
            have hd' : d' =
                { d
                  with
                  num_digits := 0#usize, decimal_point := 0#i32,
                  truncated := false } := by
              simpa using hcall.symm
            subst d'
            simp at hnonzero
          · simp only [hreset, if_false] at hcall
            cases hpow : 1#u64 <<< shift with
            | fail e => simp [hpow] at hcall
            | div => simp [hpow] at hcall
            | ok pow =>
              rw [hpow] at hcall
              simp only [bind_tc_ok] at hcall
              cases hmask : pow - 1#u64 with
              | fail e => simp [hmask] at hcall
              | div => simp [hmask] at hcall
              | ok mask =>
                rw [hmask] at hcall
                simp only [bind_tc_ok] at hcall
                cases hloop1 :
                    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                      d.num_digits d.digits shift read 0#usize acc mask with
                | fail e => simp [hloop1] at hcall
                | div => simp [hloop1] at hcall
                | ok out1 =>
                  rcases out1 with ⟨a1, write1, acc1⟩
                  rw [hloop1] at hcall
                  simp only [bind_tc_ok] at hcall
                  cases hloop2 :
                      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                        d.truncated a1 shift write1 acc1 mask with
                  | fail e => simp [hloop2] at hcall
                  | div => simp [hloop2] at hcall
                  | ok out2 =>
                    rcases out2 with ⟨b2, a2, write2⟩
                    change (do
                      let (b, a, writeIndex) ←
                        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                          d.truncated a1 shift write1 acc1 mask
                      num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                        { num_digits := writeIndex, decimal_point := point,
                          truncated := b, digits := a }) = ok d' at hcall
                    rw [hloop2] at hcall
                    simp only [bind_tc_ok] at hcall
                    let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                      { num_digits := write2, decimal_point := point,
                        truncated := b2, digits := a2 }
                    have htrim :
                        num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                      simpa [pre] using hcall
                    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim at htrim
                    by_cases hle : pre.num_digits <=
                        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                    · simp only [hle, massert_True, bind_tc_ok] at htrim
                      cases htrimLoop :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
                            pre.num_digits pre.digits with
                      | fail e => simp [htrimLoop] at htrim
                      | div => simp [htrimLoop] at htrim
                      | ok j =>
                        simp only [htrimLoop, bind_tc_ok] at htrim
                        cases htrim
                        simp only [pre]
                        omega
                    · simp [hle] at htrim

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalRightShiftPrefixLoopSemantics

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P866_T1_RightShiftPointNonincrease]
theorem P866_T1_RightShiftPointNonincrease :
    forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize),
      shift.val <= 60 ->
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      0 < digits.foldl (fun n digit => 10 * n + digit) 0 ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' ->
      d'.num_digits.val != 0 ->
      d'.decimal_point.val <= d.decimal_point.val := by
-- BODY
  dsimp only
  intro d d' shift hshift hpositive hdigit hcall hnonzero
  unfold num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift at hcall
  cases hloop0 :
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop0
        d shift 0#usize 0#u64 false with
  | fail e => simp [hloop0] at hcall
  | div => simp [hloop0] at hcall
  | ok out0 =>
    rcases out0 with ⟨d0, read, acc, allZero⟩
    simp only [hloop0, bind_tc_ok] at hcall
    have hpref := DecimalRightShiftPrefixLoopSemantics d d0 shift read acc
      allZero hshift hpositive hdigit hloop0
    dsimp only at hpref
    rcases hpref with ⟨hd0, hzero, hreadpos, _, _, _⟩
    subst d0
    subst allZero
    change (do
      let i ← lift (UScalar.hcast .I32 read)
      let i1 ← i - 1#i32
      let i2 ← d.decimal_point - i1
      let i3 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
      if i2 < i3 then
        ok
          { d
            with
            num_digits := 0#usize, decimal_point := 0#i32,
            truncated := false }
      else
        let i4 ← 1#u64 <<< shift
        let mask ← i4 - 1#u64
        let (a, writeIndex, n1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
            d.num_digits d.digits shift read 0#usize acc mask
        let (b, a1, writeIndex1) ←
          num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
            d.truncated a shift writeIndex n1 mask
        num.imp.dec2flt.decimal_seq.DecimalSeq.trim
          { num_digits := writeIndex1, decimal_point := i2,
            truncated := b, digits := a1 }) = ok d' at hcall
    let readI : Std.I32 := UScalar.hcast .I32 read
    rw [show UScalar.hcast .I32 read = readI by rfl] at hcall
    simp only [lift, bind_tc_ok] at hcall
    have hreadIVal : readI.val = (read.val : Int) := by
      have hs := UScalar.hcast_inBounds_spec .I32 read (by
        rw [IScalar.max_I32_eq]
        omega)
      simp only [lift, WP.spec_ok] at hs
      exact hs
    cases hsub : readI - 1#i32 with
    | fail e => rw [hsub] at hcall; simp at hcall
    | div => rw [hsub] at hcall; simp at hcall
    | ok readMinus =>
      rw [hsub] at hcall
      simp only [bind_tc_ok] at hcall
      have hsubVal : readMinus.val = readI.val - 1 := by
        have hs := IScalar.sub_equiv readI 1#i32
        rw [hsub] at hs
        exact hs.2.1
      cases hpoint : d.decimal_point - readMinus with
      | fail e => simp [hpoint] at hcall
      | div => simp [hpoint] at hcall
      | ok point =>
        rw [hpoint] at hcall
        simp only [bind_tc_ok] at hcall
        have hpointVal : point.val =
            d.decimal_point.val - (read.val : Int) + 1 := by
          have hs := IScalar.sub_equiv d.decimal_point readMinus
          rw [hpoint] at hs
          rw [hsubVal, hreadIVal] at hs
          omega
        cases hnegRange :
            -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE with
        | fail e => simp [hnegRange] at hcall
        | div => simp [hnegRange] at hcall
        | ok negRange =>
          rw [hnegRange] at hcall
          simp only [bind_tc_ok] at hcall
          by_cases hreset : point < negRange
          · simp only [hreset, if_true] at hcall
            have hd' : d' =
                { d
                  with
                  num_digits := 0#usize, decimal_point := 0#i32,
                  truncated := false } := by
              simpa using hcall.symm
            subst d'
            simp at hnonzero
          · simp only [hreset, if_false] at hcall
            cases hpow : 1#u64 <<< shift with
            | fail e => simp [hpow] at hcall
            | div => simp [hpow] at hcall
            | ok pow =>
              rw [hpow] at hcall
              simp only [bind_tc_ok] at hcall
              cases hmask : pow - 1#u64 with
              | fail e => simp [hmask] at hcall
              | div => simp [hmask] at hcall
              | ok mask =>
                rw [hmask] at hcall
                simp only [bind_tc_ok] at hcall
                cases hloop1 :
                    num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop1
                      d.num_digits d.digits shift read 0#usize acc mask with
                | fail e => simp [hloop1] at hcall
                | div => simp [hloop1] at hcall
                | ok out1 =>
                  rcases out1 with ⟨a1, write1, acc1⟩
                  rw [hloop1] at hcall
                  simp only [bind_tc_ok] at hcall
                  cases hloop2 :
                      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                        d.truncated a1 shift write1 acc1 mask with
                  | fail e => simp [hloop2] at hcall
                  | div => simp [hloop2] at hcall
                  | ok out2 =>
                    rcases out2 with ⟨b2, a2, write2⟩
                    change (do
                      let (b, a, writeIndex) ←
                        num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift_loop2
                          d.truncated a1 shift write1 acc1 mask
                      num.imp.dec2flt.decimal_seq.DecimalSeq.trim
                        { num_digits := writeIndex, decimal_point := point,
                          truncated := b, digits := a }) = ok d' at hcall
                    rw [hloop2] at hcall
                    simp only [bind_tc_ok] at hcall
                    let pre : num.imp.dec2flt.decimal_seq.DecimalSeq :=
                      { num_digits := write2, decimal_point := point,
                        truncated := b2, digits := a2 }
                    have htrim :
                        num.imp.dec2flt.decimal_seq.DecimalSeq.trim pre = ok d' := by
                      simpa [pre] using hcall
                    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.trim at htrim
                    by_cases hle : pre.num_digits <=
                        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS
                    · simp only [hle, massert_True, bind_tc_ok] at htrim
                      cases htrimLoop :
                          num.imp.dec2flt.decimal_seq.DecimalSeq.trim_loop
                            pre.num_digits pre.digits with
                      | fail e => simp [htrimLoop] at htrim
                      | div => simp [htrimLoop] at htrim
                      | ok j =>
                        simp only [htrimLoop, bind_tc_ok] at htrim
                        cases htrim
                        simp only [pre]
                        omega
                    · simp [hle] at htrim

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (116)
More program dependencies (83)
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
All 8 entries
Technical checking records
Local closure summary
{
  "node": "P866_T1_RightShiftPointNonincrease",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "59ec27621fe4f66089e974afdbdbd2cf7e5fa721505942b72780b4983d8ce0b4",
  "active_statement_hash": "efcfafb5752765f21e187474654b9e640074a0ace165721bad00e6e3a1fc80b8",
  "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": "revalidate-cycle-201",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "DecimalRightShiftPrefixLoopSemantics": "0d5cb25f18eedbfb",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "59bf1ff9fe88fa8d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_MAX_DIGITS": "39f6cb231ef80388",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1aaf1bb11c0b6a49",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_trim": "faa9fd92b8c29c14"
  }
}
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:019e9c491d0a1fb746b65f7b2f2cf6569aefe7635e43b835212584b5074dc816",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}