P495_T1_RightLoopStep

Lean checked

Statement

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

Lemma · Quantitative normal continuation of slow loop zero

Let d have a decimal retained prefix, positive head, exact prefix length equal to its count at most 768, and point 1\le p<310. Let the I32 exponent satisfy 0\le e\le1260. Define V by retained decimal value, and suppose X=V(d)+\eta, Y=V(d)+u, 0\le\eta<u, and sticky true iff \eta>0. One actual specialized slow-loop-zero body step returns normally with no early result, a right-shift result d' by its selected k\le60, and an I32 exponent e'=e+k. The right-shift call itself returns normally. The output has count at most 768, decimal retained prefix of exact length, positive head, and nonzero last retained cell. Its transported endpoints are X/2^k=V(d')+\eta' and Y/2^k=V(d')+u' with 0\le\eta'<u' and exact sticky equivalence. If p\ge19, then p'=p-18 or p-19; if p\le18, then p'=0 or 1. A stall p=p'=1 leaves leading output digit exactly one. A point-one input already headed by one instead has p'=0.

Lean statement

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

import Tablet.P495_T1_RightShiftCanonical
import Tablet.P495_T1_RightPrefixLengthBounds
import Tablet.P472_T1_LoopZeroRightShiftTransport
import Tablet.P472_T1_DecimalValueBounds
import Tablet.SlowShiftSelectorCertificate
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.P472_T1_DecimalRightShiftPrefixLoopSemantics
import Tablet.P472_T1_DecimalSeqRightShiftRecordSemantics

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P495_T1_RightLoopStep]
theorem P495_T1_RightLoopStep (X Y : Real)
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp : Std.I32)
    (hpointLo : 1 <= d.decimal_point.val) (hpointHi : d.decimal_point.val < 310)
    (hexpLo : 0 <= exp.val) (hexpHi : exp.val <= 1260)
    (hinput : d.num_digits.val <= 768 /\
      (forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val)) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    (exists eta u : Real, X = V d + eta /\ Y = V d + u /\
      0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) ->
    exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (exp' : Std.I32),
      num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 0#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, exp', none)) /\
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' /\
      shift.val <= 60 /\ exp'.val = exp.val + (shift.val : Int) /\
      (19 <= d.decimal_point.val ->
        d'.decimal_point.val = d.decimal_point.val - 18 \/
          d'.decimal_point.val = d.decimal_point.val - 19) /\
      (d.decimal_point.val <= 18 ->
        d'.decimal_point.val = 0 \/ d'.decimal_point.val = 1) /\
      d'.num_digits.val <= 768 /\
      (forall b, b ∈ d'.digits.val.take d'.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d'.digits.val.take d'.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d'.num_digits.val) /\
      d'.digits.val[d'.num_digits.val - 1]? ≠ some 0#u8 /\
      (exists eta' u' : Real,
        X / (2 : Real) ^ shift.val = V d' + eta' /\
        Y / (2 : Real) ^ shift.val = V d' + u' /\
        0 <= eta' /\ eta' < u' /\ (d'.truncated = true <-> 0 < eta')) /\
      (d.decimal_point.val = 1 -> d'.decimal_point.val = 1 ->
        exists tail, (d'.digits.val.take d'.num_digits.val).map (fun b => b.val) =
          1 :: tail) /\
      (d.decimal_point.val = 1 ->
        (exists tail, (d.digits.val.take d.num_digits.val).map (fun b => b.val) =
          1 :: tail) -> d'.decimal_point.val = 0) := 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

Use the actual body in num imp dec2flt slow parse long mantissa. Its positive-point and None guards hold. Casting p to Usize is exact since 1\le p\le309, using the width in Assumptions. SlowShiftSelectorCertificate supplies the actual normal selector result k\le60 and its two power brackets. Apply DecimalRightShiftLoop0Execution to this k and d. Its point, count, digit, and shape premises are the given ones. It constructs the NORMAL complete right-shift call and actual prefix results r,c with B=2^k\le c<10B, nonzero output count, prospective point p'=p-r+1\ge-2047, and unchanged input prefix record. The positive head also implies V(d)>0 by positivity of its digit fold. Thus LoopZeroRightShiftTransport applies to the given \eta,u and constructs the error and sticky transport for this same call. Normal-result injectivity identifies its returned record with d'. RightShiftCanonical supplies output shape and terminal trimming using the same normal call and nonzero count, and transports the fixed Y endpoint. All its input and endpoint hypotheses are exactly the ones stated here.
Apply RightPrefixLengthBounds to the actual prefix equation exposed by the execution principal. It gives 10^{r-1}\le c<10^r. If p\ge19, the selector gives k=60 and 10^{18}<B<10^{19}. Combining B\le c<10B with the decimal-length bounds gives 19\le r\le20: r\le18 would give c<10^{18}<B, and r\ge21 would give c\ge10^{20}>10B. Therefore p'=p-18 or p-19. For 1\le p\le18, the selector gives B\le10^p<2B, hence 10^{p-1}<B. If r<p, then c<10^r\le10^{p-1}<B. If r\ge p+2, then c\ge10^{r-1}\ge10^{p+1}\ge10B. Both are impossible, so r=p or p+1, giving p'=1 or zero.
For a point-one stall, the selector has k=3, so division is by eight. DecimalSeqRightShiftRecordSemantics on this actual call has no reset alternative because the output count is nonzero. It gives V(d')=V(d)/8-\rho, \rho\ge0. By DecimalValueBounds, V(d)<10 and, at p'=1, the positive output head h' satisfies h'\le V(d'). Consequently 1\le h'\le V(d')<10/8<2; integrality makes h'=1. Conversely, if the input head is one and p=p'=1, then p'=p-r+1 forces r=1. The input count is at least one, so the within-prefix alternative of DecimalRightShiftPrefixLoopSemantics makes c the one-digit fold, equal to one. This contradicts c\ge2^3. The exhaustive point alternatives leave p'=0.
The reset test in the slow body compares to -2047, by num imp dec2flt decimal seq DecimalSeq DECIMAL POINT RANGE. The execution bound p'\ge-2047 makes this test false. The cast of k\le60 to I32 is exact. The checked update is safe since 0\le e+k\le1320<2^{31} and returns the mathematical value e+k. Substituting the selector, normal shift, false reset, and exact addition results into the body gives the required normal None continuation. All endpoint equations and shape clauses already proved refer to that same d'. Thus each asserted conclusion holds.

Lean proof


  set_option maxHeartbeats 1600000 in
  dsimp only
  intro happrox
  rcases happrox with ⟨eta, u, hX, hY, heta, hetau, hsticky⟩
  rcases hinput with ⟨hcap, hdigit, hshape⟩
  rcases hshape with ⟨head, tail, hshape, hhead, hhead10, hlen⟩
  obtain ⟨n, hncast, hnval⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.hcast_inBounds_spec .Usize d.decimal_point (by scalar_tac))
  let traits := F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
  let shift : Std.Usize := match n.val with
    | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
    | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
    | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
    | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
    | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize
  have hselectorAll := SlowShiftSelectorCertificate traits n
  dsimp only at hselectorAll
  rcases hselectorAll with ⟨hselector, hshift, hsmallzero, hsmall, hlarge⟩
  have hshiftCall :
      num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
        traits () n = ok shift := hselector
  have hinputExec : d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs, (d.digits.val.take d.num_digits.val).map (fun a => a.val) = z :: zs /\
        1 <= z /\ z < 10 /\ (z :: zs).length = d.num_digits.val) :=
    ⟨hcap, hdigit, ⟨head, tail, hshape, hhead, hhead10, hlen⟩⟩
  obtain ⟨read, acc, d', hloop0, hreadlo, hreadhi, haccLo, haccHi,
      hprospective⟩ :=
    P472_T1_DecimalRightShiftLoop0Execution d shift hshift
      (by omega) (by omega) hinputExec
  rcases hprospective with ⟨hprosLo, hprosHi, hcall, hpoint', hnz⟩
  have hprefixLen := P495_T1_RightPrefixLengthBounds d d shift read acc false
    hshift hcap hdigit head tail hshape hhead hlen hloop0
  have hnpoint : n.val = d.decimal_point.val := by omega
  have hlargePoint : 19 <= d.decimal_point.val ->
      d'.decimal_point.val = d.decimal_point.val - 18 \/
        d'.decimal_point.val = d.decimal_point.val - 19 := by
    intro hp
    have hb := hlarge (by omega)
    rcases hb with ⟨hsk, hpowlo, hpowhi⟩
    have hshift60 : shift.val = 60 := by simpa using hsk
    have hpows : 10 ^ 18 < 2 ^ shift.val /\ 2 ^ shift.val < 10 ^ 19 := by
      rw [hshift60]
      exact ⟨hpowlo, hpowhi⟩
    have hreadLo : 19 <= read.val := by
      by_contra hr
      have hr' : read.val <= 18 := by omega
      have hp10 : 10 ^ read.val <= 10 ^ 18 := Nat.pow_le_pow_right (by omega) hr'
      have haccLo' : 2 ^ shift.val <= acc.val := haccLo
      have haccHi' : acc.val < 10 ^ read.val := hprefixLen.2
      omega
    have hreadHi : read.val <= 20 := by
      by_contra hr
      have hr' : 21 <= read.val := by omega
      have hp10 : 10 ^ 20 <= 10 ^ (read.val - 1) :=
        Nat.pow_le_pow_right (by omega) (by omega)
      have hBlt : 10 * 2 ^ shift.val < 10 ^ 20 := by
        rw [hshift60]
        norm_num
      have haccLo' : 10 ^ (read.val - 1) <= acc.val := hprefixLen.1
      have haccHi' : acc.val < 10 * 2 ^ shift.val := haccHi
      omega
    have hrCases : read.val = 19 \/ read.val = 20 := by omega
    rw [hpoint']
    rcases hrCases with hr | hr
    · left
      rw [hr]
      omega
    · right
      rw [hr]
      omega
  have hsmallPoint : d.decimal_point.val <= 18 ->
      d'.decimal_point.val = 0 \/ d'.decimal_point.val = 1 := by
    intro hp
    have hnlo : 1 <= n.val := by omega
    have hnhi : n.val <= 18 := by omega
    rcases hsmall hnlo hnhi with ⟨hBle, hBlt⟩
    have hpowlo' : 2 ^ shift.val <= 10 ^ n.val := hBle
    have hpowhi' : 10 ^ n.val < 2 ^ (shift.val + 1) := hBlt
    have hreadLo : n.val <= read.val := by
      by_contra hr
      have hr' : read.val <= n.val - 1 := by omega
      have hp10 : 10 ^ read.val <= 10 ^ (n.val - 1) :=
        Nat.pow_le_pow_right (by omega) hr'
      have he : n.val - 1 + 1 = n.val := by omega
      have hpEq : 10 ^ n.val = 10 * 10 ^ (n.val - 1) := by
        calc
          10 ^ n.val = 10 ^ (n.val - 1 + 1) := by rw [he]
          _ = 10 ^ (n.val - 1) * 10 := by rw [pow_succ]
          _ = 10 * 10 ^ (n.val - 1) := by omega
      have hhi : 10 * 10 ^ (n.val - 1) < 2 * 2 ^ shift.val := by
        calc
          10 * 10 ^ (n.val - 1) = 10 ^ n.val := hpEq.symm
          _ < 2 ^ (shift.val + 1) := hpowhi'
          _ = 2 * 2 ^ shift.val := by rw [pow_succ]; ring
      have hBlt : 10 ^ (n.val - 1) < 2 ^ shift.val := by omega
      have haccBelow : acc.val < 10 ^ (n.val - 1) := by omega
      omega
    have hreadHi : read.val <= n.val + 1 := by
      by_contra hr
      have hr' : n.val + 2 <= read.val := by omega
      have hp10 : 10 ^ (n.val + 1) <= 10 ^ (read.val - 1) :=
        Nat.pow_le_pow_right (by omega) (by omega)
      have hpEq : 10 ^ (n.val + 1) = 10 * 10 ^ n.val := by
        rw [show n.val + 1 = Nat.succ n.val by omega, pow_succ]
        omega
      have haccBelow : 10 ^ (read.val - 1) <= acc.val := hprefixLen.1
      have haccAbove : acc.val < 10 * 2 ^ shift.val := haccHi
      omega
    have hread : read.val = n.val \/ read.val = n.val + 1 := by omega
    rw [hpoint']
    rcases hread with hr | hr
    · right
      rw [hr]
      omega
    · left
      rw [hr]
      omega
  have htransport := P472_T1_LoopZeroRightShiftTransport d shift X eta u
    hshift (by omega) (by omega) hcap hdigit
    ⟨head, tail, hshape, hhead, hhead10, hlen⟩
    (by
      dsimp only
      have hv := P472_T1_DecimalValueBounds
        ((d.digits.val.take d.num_digits.val).map (fun a => a.val)) head tail
        hshape hhead (by
          intro z hz
          obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
          exact hdigit b hb) d.decimal_point.val
      have hpow : 0 < (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int)) := zpow_pos (by norm_num) _
      have hfoldPos (ys : List Nat) (a : Nat) (ha : 0 < a) :
          0 < ys.foldl (fun n z => 10 * n + z) a := by
        induction ys generalizing a with
        | nil => simpa
        | cons z zs ih =>
            simp only [List.foldl_cons]
            apply ih
            omega
      have hfold : 0 <
          ((d.digits.val.take d.num_digits.val).map (fun a => a.val)).foldl
            (fun n z => 10 * n + z) 0 := by
        rw [hshape]
        simp only [List.foldl_cons, Nat.mul_zero, Nat.zero_add]
        exact hfoldPos tail head hhead
      exact mul_pos (by exact_mod_cast hfold) hpow)
    hX heta hetau hsticky
  rcases htransport with ⟨d'', eta', u', hcall', hpl, hph, hnz', hX', heta', hetau', hsticky', hY', hcompare⟩
  have hdEq : d' = d'' := by
    exact Result.ok.inj (hcall.symm.trans hcall')
  subst d''
  have hnz'' : d'.num_digits.val ≠ 0 := by simpa using hnz
  have hcanonical := P495_T1_RightShiftCanonical X Y d d' shift hshift hcap hdigit
    ⟨head, tail, hshape, hhead, hhead10, hlen⟩ hcall hnz''
    ⟨eta, u, hX, hY, heta, hetau, hsticky⟩
  rcases hcanonical with ⟨hcanonLo, hcap', hdigit', hhead', htrim, hcanon⟩
  rcases hcanon with ⟨eta'', u'', hX'', hY'', heta'', hetau'', hsticky''⟩
  have hetaEq : eta' = eta'' := by linarith
  have huEq : u' = u'' := by
    rw [hY] at hY''
    linarith
  subst eta''
  subst u''
  let val := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
      (fun n z => 10 * n + z) 0 : Nat) : Real) *
      (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
  have hvalueBounds := P472_T1_DecimalValueBounds
    ((d.digits.val.take d.num_digits.val).map (fun b => b.val)) head tail
    hshape hhead (by
      intro z hz
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
      exact hdigit b hb) d.decimal_point.val
  have hinputPrefixLength :
      ((d.digits.val.take d.num_digits.val).map (fun b => b.val)).length =
        d.num_digits.val := by rw [hshape]; exact hlen
  have hvalueBounds' : (head : Real) * (10 : Real) ^
      (d.decimal_point.val - 1) <= val d /\ val d <
      (10 : Real) ^ d.decimal_point.val := by
    have hdlen : d.digits.val.length = 768 := by simpa using d.digits.property
    have hh := hvalueBounds
    simp only [List.length_map, List.length_take, hdlen,
      Nat.min_eq_left hcap] at hh
    simpa [val, List.map_take] using hh
  have hstallHead : d.decimal_point.val = 1 -> d'.decimal_point.val = 1 ->
      exists tail, (d'.digits.val.take d'.num_digits.val).map
        (fun b => b.val) = 1 :: tail := by
    intro hp hp'
    have hn1 : n.val = 1 := by omega
    have hshift3 : shift.val = 3 := by simpa [shift, hn1]
    have hread1 : read.val = 1 := by rw [hpoint'] at hp'; omega
    rcases hhead' with ⟨outHead, outTail, houtshape, houtlo, hout10, houtlen⟩
    have houtBounds := P472_T1_DecimalValueBounds
      ((d'.digits.val.take d'.num_digits.val).map (fun b => b.val))
      outHead outTail houtshape houtlo (by
        intro z hz
        obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
        exact hdigit' b hb) d'.decimal_point.val
    have houtPrefixLength :
        ((d'.digits.val.take d'.num_digits.val).map (fun b => b.val)).length =
          d'.num_digits.val := by rw [houtshape]; exact houtlen
    have houtBounds' : (outHead : Real) * (10 : Real) ^
        (d'.decimal_point.val - 1) <= val d' /\ val d' <
        (10 : Real) ^ d'.decimal_point.val := by
      have hdlen' : d'.digits.val.length = 768 := by simpa using d'.digits.property
      have hh := houtBounds
      simp only [List.length_map, List.length_take, hdlen',
        Nat.min_eq_left hcap'] at hh
      simpa [val, List.map_take] using hh
    have hVin : 0 < val d := by
      have hh := hvalueBounds'.1
      rw [hp] at hh
      norm_num at hh
      have hhhead : (1 : Real) <= (head : Real) := by exact_mod_cast hhead
      linarith
    have hrecord := P472_T1_DecimalSeqRightShiftRecordSemantics
      d d' shift (by omega) hVin hdigit hcall
    rcases hrecord with hreset | ⟨rho, hrho, _, hrecord, _⟩
    · exact False.elim (by simp at hnz''; exact hnz'' hreset.1)
    have houtValLower : (outHead : Real) <= val d' := by
      have hh := houtBounds'.1
      rw [hp'] at hh
      norm_num at hh
      exact hh
    have hVinUpper : val d < 10 := by
      have hh := hvalueBounds'.2
      rw [hp] at hh
      norm_num at hh
      exact hh
    have hVout : val d' < 2 := by
      have hrecordVal : val d' = val d / (2 : Real) ^ shift.val - rho := by
        simpa [val, List.map_take, List.length_map, List.length_take,
          d.digits.property, d'.digits.property,
          Nat.min_eq_left hcap, Nat.min_eq_left hcap'] using hrecord
      rw [hrecordVal]
      have hpow : (2 : Real) ^ shift.val = 8 := by rw [hshift3]; norm_num
      rw [hpow]
      nlinarith
    have houtHeadLt : (outHead : Real) < 2 :=
      lt_of_le_of_lt houtValLower hVout
    have houtHeadLtNat : outHead < 2 := by exact_mod_cast houtHeadLt
    have houtHeadOne : outHead = 1 := by omega
    exact ⟨outTail, by rw [houtshape, houtHeadOne]⟩
  have hheadOneRecovery : d.decimal_point.val = 1 ->
      (exists tail, (d.digits.val.take d.num_digits.val).map
        (fun b => b.val) = 1 :: tail) -> d'.decimal_point.val = 0 := by
    intro hp hin
    have hn1 : n.val = 1 := by omega
    have hshift3 : shift.val = 3 := by simpa [shift, hn1]
    rcases hsmallPoint (by rw [hp]; norm_num) with hzero | hone
    · exact hzero
    · have hread1 : read.val = 1 := by rw [hpoint'] at hone; omega
      exfalso
      have hnumPos : 1 <= d.num_digits.val := by
        have hh := hlen
        simp at hh
        omega
      have hIpos : 0 <
          ((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
            (fun n z => 10 * n + z) 0 := by
        rw [hshape]
        simp only [List.foldl_cons, Nat.mul_zero, Nat.zero_add]
        have positive (ys : List Nat) (a : Nat) (ha : 0 < a) :
            0 < ys.foldl (fun n z => 10 * n + z) a := by
          induction ys generalizing a with
          | nil => simpa
          | cons z zs ih => simp only [List.foldl_cons]; apply ih; omega
        exact positive tail head hhead
      have hpref := P472_T1_DecimalRightShiftPrefixLoopSemantics d d shift read acc
        false hshift hIpos hdigit hloop0
      dsimp only at hpref
      rcases hpref with ⟨_, _, _, _, _, hphase⟩
      rcases hphase with ⟨hreadCount, _, hacc⟩ | hmore
      · have haccOne : acc.val = 1 := by
          rcases hin with ⟨intail, hin⟩
          have hfirst : (d.digits.val.take d.num_digits.val).map
              (fun b => b.val) = 1 :: intail := hin
          have hfirst' : List.take d.num_digits.val
              (d.digits.val.map (fun b => b.val)) = 1 :: intail := by
            simpa [List.map_take] using hfirst
          have htakeOne : List.take 1
              (d.digits.val.map (fun b => b.val)) = [1] := by
            have hh := congrArg (List.take 1) hfirst'
            simpa [List.take_take, Nat.min_eq_left hnumPos] using hh
          rw [hread1] at hacc
          rw [htakeOne] at hacc
          simpa using hacc
        have hpow : 2 ^ shift.val = 8 := by rw [hshift3]; norm_num
        rw [haccOne, hpow] at haccLo
        omega
      · rcases hmore with ⟨hcountlt, _, _, _⟩
        omega
  let shiftI : Std.I32 := UScalar.hcast .I32 shift
  have hshiftInt : (shift.val : Int) <= 60 := by exact_mod_cast hshift
  have hshiftCastBounds : (shift.val : Int) <= IScalar.max .I32 := by
    rw [IScalar.max_I32_eq]
    omega
  have hshiftIVal : shiftI.val = (shift.val : Int) := by
    have hs := UScalar.hcast_inBounds_spec .I32 shift hshiftCastBounds
    simp only [lift, WP.spec_ok] at hs
    exact hs
  have hcastShift : lift (UScalar.hcast .I32 shift) = ok shiftI := by
    obtain ⟨v, hv, hvval⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.hcast_inBounds_spec .I32 shift hshiftCastBounds)
    rw [hv]
    congr 1
    apply IScalar.eq_of_val_eq
    have hvval' : v.val = (shift.val : Int) := by
      simpa [WP.spec_ok] using hvval
    change v.val = shiftI.val
    rw [hshiftIVal]
    exact hvval'
  have hsumBounds : IScalar.min .I32 <= exp.val + shiftI.val /\
      exp.val + shiftI.val <= IScalar.max .I32 := by
    rw [IScalar.min_I32_eq, IScalar.max_I32_eq, hshiftIVal]
    constructor
    · omega
    · norm_num [IScalarTy.numBits, I32.numBits]
      omega
  obtain ⟨exp', hadd, haddVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.add_spec hsumBounds.1 hsumBounds.2)
  have hexpVal : exp'.val = exp.val + (shift.val : Int) := by
    rw [haddVal, hshiftIVal]
  have hnegRange :
      -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE =
        ok (-2047)#i32 := by
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
    change IScalar.neg (2047#i32 : Std.I32) = _
    unfold IScalar.neg IScalar.tryMk IScalar.tryMkOpt
    norm_num [IScalar.check_bounds, IScalarTy.numBits]
  have hnotReset : ¬ d'.decimal_point.val < -2047 := by
    rw [hpoint']
    omega
  have hnotResetCmp : ¬ d'.decimal_point < (-2047)#i32 := by
    simpa only [IScalar.lt_equiv] using hnotReset
  have houter : 0 < d.decimal_point.val := by omega
  have houterCmp : d.decimal_point > 0#i32 := by
    simpa only [gt_iff_lt, IScalar.lt_equiv] using houter
  have hrecordEta :
      ({ num_digits := d.num_digits, decimal_point := d.decimal_point,
         truncated := d.truncated, digits := d.digits } :
          num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
    cases d
    rfl
  let dIn : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := d.num_digits, decimal_point := d.decimal_point,
      truncated := d.truncated, digits := d.digits }
  have hdIn : dIn = d := hrecordEta
  have hcallIn : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift dIn
      (match n.val with
        | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
        | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
        | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
        | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
        | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize) =
        ok d' := by
    rw [hdIn]
    change num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d'
    exact hcall
  have hcallActual : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
      { num_digits := d.num_digits, decimal_point := d.decimal_point,
        truncated := d.truncated, digits := d.digits }
      (match n.val with
        | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
        | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
        | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
        | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
        | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize) =
        ok d' := by
    change num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d'
    exact hcall
  have hcastActual : lift (UScalar.hcast .I32
      (match n.val with
        | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
        | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
        | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
        | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
        | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize)) =
        ok shiftI := by simpa [shift] using hcastShift
  have hbody : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
      { m := 0#u64, p_biased := 0#i32 }
      d.num_digits d.decimal_point d.truncated d.digits exp none =
        ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
          d'.digits, exp', none)) := by
    cases d with
    | mk numDigits point truncated digits =>
      have hcallMatch : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
          { num_digits := numDigits, decimal_point := point,
            truncated := truncated, digits := digits }
          (match n.val with
            | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
            | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
            | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
            | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
            | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize) =
          ok d' := by
        simpa only [shift] using hcall
      unfold num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
      simp only [if_pos houterCmp, core.option.Option.is_none]
      have hnone : (none : Option num.imp.dec2flt.common.BiasedFp).isNone = true := rfl
      simp only [hnone, if_true]
      rw [hncast]
      simp only [bind_tc_ok]
      rw [hselector]
      simp only [bind_tc_ok]
      change Aeneas.Std.bind
        (num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
          ({ num_digits := numDigits, decimal_point := point,
             truncated := truncated, digits := digits } :
            num.imp.dec2flt.decimal_seq.DecimalSeq)
          (match n.val with
            | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
            | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
            | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
            | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
            | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize))
        (fun (d : num.imp.dec2flt.decimal_seq.DecimalSeq) => do
          let i2 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
          if d.decimal_point < i2 then
            ok (cont (d.num_digits, d.decimal_point, d.truncated, d.digits,
              exp, some ({ m := 0#u64, p_biased := 0#i32 } :
                num.imp.dec2flt.common.BiasedFp)))
          else do
            let i3 ← lift (UScalar.hcast .I32
              (match n.val with
                | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
                | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
                | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
                | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
                | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize))
            let exp21 ← exp + i3
            ok (cont (d.num_digits, d.decimal_point, d.truncated, d.digits,
              exp21, none))) = _
      rw [hcallMatch]
      simp only [Aeneas.Std.bind, bind_tc_ok]
      rw [hnegRange]
      simp only [bind_tc_ok, hnotResetCmp, if_false]
      rw [hcastActual]
      simp only [bind_tc_ok]
      rw [hadd]
      rfl
  exact ⟨d', shift, exp', hbody, hcall, hshift, hexpVal,
    hlargePoint, hsmallPoint, hcap', hdigit', hhead', htrim,
    ⟨eta', u', hX', hY'', heta', hetau'', hsticky'⟩,
    hstallHead, hheadOneRecovery⟩

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P495_T1_RightShiftCanonical
import Tablet.P495_T1_RightPrefixLengthBounds
import Tablet.P472_T1_LoopZeroRightShiftTransport
import Tablet.P472_T1_DecimalValueBounds
import Tablet.SlowShiftSelectorCertificate
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.P472_T1_DecimalRightShiftPrefixLoopSemantics
import Tablet.P472_T1_DecimalSeqRightShiftRecordSemantics

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P495_T1_RightLoopStep]
theorem P495_T1_RightLoopStep (X Y : Real)
    (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp : Std.I32)
    (hpointLo : 1 <= d.decimal_point.val) (hpointHi : d.decimal_point.val < 310)
    (hexpLo : 0 <= exp.val) (hexpHi : exp.val <= 1260)
    (hinput : d.num_digits.val <= 768 /\
      (forall b, b ∈ d.digits.val.take d.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d.digits.val.take d.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d.num_digits.val)) :
    let V := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
        (fun n z => 10 * n + z) 0 : Nat) : Real) *
        (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
    (exists eta u : Real, X = V d + eta /\ Y = V d + u /\
      0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) ->
    exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (exp' : Std.I32),
      num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 0#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, exp', none)) /\
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' /\
      shift.val <= 60 /\ exp'.val = exp.val + (shift.val : Int) /\
      (19 <= d.decimal_point.val ->
        d'.decimal_point.val = d.decimal_point.val - 18 \/
          d'.decimal_point.val = d.decimal_point.val - 19) /\
      (d.decimal_point.val <= 18 ->
        d'.decimal_point.val = 0 \/ d'.decimal_point.val = 1) /\
      d'.num_digits.val <= 768 /\
      (forall b, b ∈ d'.digits.val.take d'.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d'.digits.val.take d'.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d'.num_digits.val) /\
      d'.digits.val[d'.num_digits.val - 1]? ≠ some 0#u8 /\
      (exists eta' u' : Real,
        X / (2 : Real) ^ shift.val = V d' + eta' /\
        Y / (2 : Real) ^ shift.val = V d' + u' /\
        0 <= eta' /\ eta' < u' /\ (d'.truncated = true <-> 0 < eta')) /\
      (d.decimal_point.val = 1 -> d'.decimal_point.val = 1 ->
        exists tail, (d'.digits.val.take d'.num_digits.val).map (fun b => b.val) =
          1 :: tail) /\
      (d.decimal_point.val = 1 ->
        (exists tail, (d.digits.val.take d.num_digits.val).map (fun b => b.val) =
          1 :: tail) -> d'.decimal_point.val = 0) := by
-- BODY
  set_option maxHeartbeats 1600000 in
  dsimp only
  intro happrox
  rcases happrox with ⟨eta, u, hX, hY, heta, hetau, hsticky⟩
  rcases hinput with ⟨hcap, hdigit, hshape⟩
  rcases hshape with ⟨head, tail, hshape, hhead, hhead10, hlen⟩
  obtain ⟨n, hncast, hnval⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.hcast_inBounds_spec .Usize d.decimal_point (by scalar_tac))
  let traits := F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
  let shift : Std.Usize := match n.val with
    | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
    | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
    | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
    | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
    | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize
  have hselectorAll := SlowShiftSelectorCertificate traits n
  dsimp only at hselectorAll
  rcases hselectorAll with ⟨hselector, hshift, hsmallzero, hsmall, hlarge⟩
  have hshiftCall :
      num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
        traits () n = ok shift := hselector
  have hinputExec : d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs, (d.digits.val.take d.num_digits.val).map (fun a => a.val) = z :: zs /\
        1 <= z /\ z < 10 /\ (z :: zs).length = d.num_digits.val) :=
    ⟨hcap, hdigit, ⟨head, tail, hshape, hhead, hhead10, hlen⟩⟩
  obtain ⟨read, acc, d', hloop0, hreadlo, hreadhi, haccLo, haccHi,
      hprospective⟩ :=
    P472_T1_DecimalRightShiftLoop0Execution d shift hshift
      (by omega) (by omega) hinputExec
  rcases hprospective with ⟨hprosLo, hprosHi, hcall, hpoint', hnz⟩
  have hprefixLen := P495_T1_RightPrefixLengthBounds d d shift read acc false
    hshift hcap hdigit head tail hshape hhead hlen hloop0
  have hnpoint : n.val = d.decimal_point.val := by omega
  have hlargePoint : 19 <= d.decimal_point.val ->
      d'.decimal_point.val = d.decimal_point.val - 18 \/
        d'.decimal_point.val = d.decimal_point.val - 19 := by
    intro hp
    have hb := hlarge (by omega)
    rcases hb with ⟨hsk, hpowlo, hpowhi⟩
    have hshift60 : shift.val = 60 := by simpa using hsk
    have hpows : 10 ^ 18 < 2 ^ shift.val /\ 2 ^ shift.val < 10 ^ 19 := by
      rw [hshift60]
      exact ⟨hpowlo, hpowhi⟩
    have hreadLo : 19 <= read.val := by
      by_contra hr
      have hr' : read.val <= 18 := by omega
      have hp10 : 10 ^ read.val <= 10 ^ 18 := Nat.pow_le_pow_right (by omega) hr'
      have haccLo' : 2 ^ shift.val <= acc.val := haccLo
      have haccHi' : acc.val < 10 ^ read.val := hprefixLen.2
      omega
    have hreadHi : read.val <= 20 := by
      by_contra hr
      have hr' : 21 <= read.val := by omega
      have hp10 : 10 ^ 20 <= 10 ^ (read.val - 1) :=
        Nat.pow_le_pow_right (by omega) (by omega)
      have hBlt : 10 * 2 ^ shift.val < 10 ^ 20 := by
        rw [hshift60]
        norm_num
      have haccLo' : 10 ^ (read.val - 1) <= acc.val := hprefixLen.1
      have haccHi' : acc.val < 10 * 2 ^ shift.val := haccHi
      omega
    have hrCases : read.val = 19 \/ read.val = 20 := by omega
    rw [hpoint']
    rcases hrCases with hr | hr
    · left
      rw [hr]
      omega
    · right
      rw [hr]
      omega
  have hsmallPoint : d.decimal_point.val <= 18 ->
      d'.decimal_point.val = 0 \/ d'.decimal_point.val = 1 := by
    intro hp
    have hnlo : 1 <= n.val := by omega
    have hnhi : n.val <= 18 := by omega
    rcases hsmall hnlo hnhi with ⟨hBle, hBlt⟩
    have hpowlo' : 2 ^ shift.val <= 10 ^ n.val := hBle
    have hpowhi' : 10 ^ n.val < 2 ^ (shift.val + 1) := hBlt
    have hreadLo : n.val <= read.val := by
      by_contra hr
      have hr' : read.val <= n.val - 1 := by omega
      have hp10 : 10 ^ read.val <= 10 ^ (n.val - 1) :=
        Nat.pow_le_pow_right (by omega) hr'
      have he : n.val - 1 + 1 = n.val := by omega
      have hpEq : 10 ^ n.val = 10 * 10 ^ (n.val - 1) := by
        calc
          10 ^ n.val = 10 ^ (n.val - 1 + 1) := by rw [he]
          _ = 10 ^ (n.val - 1) * 10 := by rw [pow_succ]
          _ = 10 * 10 ^ (n.val - 1) := by omega
      have hhi : 10 * 10 ^ (n.val - 1) < 2 * 2 ^ shift.val := by
        calc
          10 * 10 ^ (n.val - 1) = 10 ^ n.val := hpEq.symm
          _ < 2 ^ (shift.val + 1) := hpowhi'
          _ = 2 * 2 ^ shift.val := by rw [pow_succ]; ring
      have hBlt : 10 ^ (n.val - 1) < 2 ^ shift.val := by omega
      have haccBelow : acc.val < 10 ^ (n.val - 1) := by omega
      omega
    have hreadHi : read.val <= n.val + 1 := by
      by_contra hr
      have hr' : n.val + 2 <= read.val := by omega
      have hp10 : 10 ^ (n.val + 1) <= 10 ^ (read.val - 1) :=
        Nat.pow_le_pow_right (by omega) (by omega)
      have hpEq : 10 ^ (n.val + 1) = 10 * 10 ^ n.val := by
        rw [show n.val + 1 = Nat.succ n.val by omega, pow_succ]
        omega
      have haccBelow : 10 ^ (read.val - 1) <= acc.val := hprefixLen.1
      have haccAbove : acc.val < 10 * 2 ^ shift.val := haccHi
      omega
    have hread : read.val = n.val \/ read.val = n.val + 1 := by omega
    rw [hpoint']
    rcases hread with hr | hr
    · right
      rw [hr]
      omega
    · left
      rw [hr]
      omega
  have htransport := P472_T1_LoopZeroRightShiftTransport d shift X eta u
    hshift (by omega) (by omega) hcap hdigit
    ⟨head, tail, hshape, hhead, hhead10, hlen⟩
    (by
      dsimp only
      have hv := P472_T1_DecimalValueBounds
        ((d.digits.val.take d.num_digits.val).map (fun a => a.val)) head tail
        hshape hhead (by
          intro z hz
          obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
          exact hdigit b hb) d.decimal_point.val
      have hpow : 0 < (10 : Real) ^
          (d.decimal_point.val - (d.num_digits.val : Int)) := zpow_pos (by norm_num) _
      have hfoldPos (ys : List Nat) (a : Nat) (ha : 0 < a) :
          0 < ys.foldl (fun n z => 10 * n + z) a := by
        induction ys generalizing a with
        | nil => simpa
        | cons z zs ih =>
            simp only [List.foldl_cons]
            apply ih
            omega
      have hfold : 0 <
          ((d.digits.val.take d.num_digits.val).map (fun a => a.val)).foldl
            (fun n z => 10 * n + z) 0 := by
        rw [hshape]
        simp only [List.foldl_cons, Nat.mul_zero, Nat.zero_add]
        exact hfoldPos tail head hhead
      exact mul_pos (by exact_mod_cast hfold) hpow)
    hX heta hetau hsticky
  rcases htransport with ⟨d'', eta', u', hcall', hpl, hph, hnz', hX', heta', hetau', hsticky', hY', hcompare⟩
  have hdEq : d' = d'' := by
    exact Result.ok.inj (hcall.symm.trans hcall')
  subst d''
  have hnz'' : d'.num_digits.val ≠ 0 := by simpa using hnz
  have hcanonical := P495_T1_RightShiftCanonical X Y d d' shift hshift hcap hdigit
    ⟨head, tail, hshape, hhead, hhead10, hlen⟩ hcall hnz''
    ⟨eta, u, hX, hY, heta, hetau, hsticky⟩
  rcases hcanonical with ⟨hcanonLo, hcap', hdigit', hhead', htrim, hcanon⟩
  rcases hcanon with ⟨eta'', u'', hX'', hY'', heta'', hetau'', hsticky''⟩
  have hetaEq : eta' = eta'' := by linarith
  have huEq : u' = u'' := by
    rw [hY] at hY''
    linarith
  subst eta''
  subst u''
  let val := fun a : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    ((((a.digits.val.take a.num_digits.val).map (fun b => b.val)).foldl
      (fun n z => 10 * n + z) 0 : Nat) : Real) *
      (10 : Real) ^ (a.decimal_point.val - (a.num_digits.val : Int))
  have hvalueBounds := P472_T1_DecimalValueBounds
    ((d.digits.val.take d.num_digits.val).map (fun b => b.val)) head tail
    hshape hhead (by
      intro z hz
      obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
      exact hdigit b hb) d.decimal_point.val
  have hinputPrefixLength :
      ((d.digits.val.take d.num_digits.val).map (fun b => b.val)).length =
        d.num_digits.val := by rw [hshape]; exact hlen
  have hvalueBounds' : (head : Real) * (10 : Real) ^
      (d.decimal_point.val - 1) <= val d /\ val d <
      (10 : Real) ^ d.decimal_point.val := by
    have hdlen : d.digits.val.length = 768 := by simpa using d.digits.property
    have hh := hvalueBounds
    simp only [List.length_map, List.length_take, hdlen,
      Nat.min_eq_left hcap] at hh
    simpa [val, List.map_take] using hh
  have hstallHead : d.decimal_point.val = 1 -> d'.decimal_point.val = 1 ->
      exists tail, (d'.digits.val.take d'.num_digits.val).map
        (fun b => b.val) = 1 :: tail := by
    intro hp hp'
    have hn1 : n.val = 1 := by omega
    have hshift3 : shift.val = 3 := by simpa [shift, hn1]
    have hread1 : read.val = 1 := by rw [hpoint'] at hp'; omega
    rcases hhead' with ⟨outHead, outTail, houtshape, houtlo, hout10, houtlen⟩
    have houtBounds := P472_T1_DecimalValueBounds
      ((d'.digits.val.take d'.num_digits.val).map (fun b => b.val))
      outHead outTail houtshape houtlo (by
        intro z hz
        obtain ⟨b, hb, rfl⟩ := List.mem_map.mp hz
        exact hdigit' b hb) d'.decimal_point.val
    have houtPrefixLength :
        ((d'.digits.val.take d'.num_digits.val).map (fun b => b.val)).length =
          d'.num_digits.val := by rw [houtshape]; exact houtlen
    have houtBounds' : (outHead : Real) * (10 : Real) ^
        (d'.decimal_point.val - 1) <= val d' /\ val d' <
        (10 : Real) ^ d'.decimal_point.val := by
      have hdlen' : d'.digits.val.length = 768 := by simpa using d'.digits.property
      have hh := houtBounds
      simp only [List.length_map, List.length_take, hdlen',
        Nat.min_eq_left hcap'] at hh
      simpa [val, List.map_take] using hh
    have hVin : 0 < val d := by
      have hh := hvalueBounds'.1
      rw [hp] at hh
      norm_num at hh
      have hhhead : (1 : Real) <= (head : Real) := by exact_mod_cast hhead
      linarith
    have hrecord := P472_T1_DecimalSeqRightShiftRecordSemantics
      d d' shift (by omega) hVin hdigit hcall
    rcases hrecord with hreset | ⟨rho, hrho, _, hrecord, _⟩
    · exact False.elim (by simp at hnz''; exact hnz'' hreset.1)
    have houtValLower : (outHead : Real) <= val d' := by
      have hh := houtBounds'.1
      rw [hp'] at hh
      norm_num at hh
      exact hh
    have hVinUpper : val d < 10 := by
      have hh := hvalueBounds'.2
      rw [hp] at hh
      norm_num at hh
      exact hh
    have hVout : val d' < 2 := by
      have hrecordVal : val d' = val d / (2 : Real) ^ shift.val - rho := by
        simpa [val, List.map_take, List.length_map, List.length_take,
          d.digits.property, d'.digits.property,
          Nat.min_eq_left hcap, Nat.min_eq_left hcap'] using hrecord
      rw [hrecordVal]
      have hpow : (2 : Real) ^ shift.val = 8 := by rw [hshift3]; norm_num
      rw [hpow]
      nlinarith
    have houtHeadLt : (outHead : Real) < 2 :=
      lt_of_le_of_lt houtValLower hVout
    have houtHeadLtNat : outHead < 2 := by exact_mod_cast houtHeadLt
    have houtHeadOne : outHead = 1 := by omega
    exact ⟨outTail, by rw [houtshape, houtHeadOne]⟩
  have hheadOneRecovery : d.decimal_point.val = 1 ->
      (exists tail, (d.digits.val.take d.num_digits.val).map
        (fun b => b.val) = 1 :: tail) -> d'.decimal_point.val = 0 := by
    intro hp hin
    have hn1 : n.val = 1 := by omega
    have hshift3 : shift.val = 3 := by simpa [shift, hn1]
    rcases hsmallPoint (by rw [hp]; norm_num) with hzero | hone
    · exact hzero
    · have hread1 : read.val = 1 := by rw [hpoint'] at hone; omega
      exfalso
      have hnumPos : 1 <= d.num_digits.val := by
        have hh := hlen
        simp at hh
        omega
      have hIpos : 0 <
          ((d.digits.val.take d.num_digits.val).map (fun b => b.val)).foldl
            (fun n z => 10 * n + z) 0 := by
        rw [hshape]
        simp only [List.foldl_cons, Nat.mul_zero, Nat.zero_add]
        have positive (ys : List Nat) (a : Nat) (ha : 0 < a) :
            0 < ys.foldl (fun n z => 10 * n + z) a := by
          induction ys generalizing a with
          | nil => simpa
          | cons z zs ih => simp only [List.foldl_cons]; apply ih; omega
        exact positive tail head hhead
      have hpref := P472_T1_DecimalRightShiftPrefixLoopSemantics d d shift read acc
        false hshift hIpos hdigit hloop0
      dsimp only at hpref
      rcases hpref with ⟨_, _, _, _, _, hphase⟩
      rcases hphase with ⟨hreadCount, _, hacc⟩ | hmore
      · have haccOne : acc.val = 1 := by
          rcases hin with ⟨intail, hin⟩
          have hfirst : (d.digits.val.take d.num_digits.val).map
              (fun b => b.val) = 1 :: intail := hin
          have hfirst' : List.take d.num_digits.val
              (d.digits.val.map (fun b => b.val)) = 1 :: intail := by
            simpa [List.map_take] using hfirst
          have htakeOne : List.take 1
              (d.digits.val.map (fun b => b.val)) = [1] := by
            have hh := congrArg (List.take 1) hfirst'
            simpa [List.take_take, Nat.min_eq_left hnumPos] using hh
          rw [hread1] at hacc
          rw [htakeOne] at hacc
          simpa using hacc
        have hpow : 2 ^ shift.val = 8 := by rw [hshift3]; norm_num
        rw [haccOne, hpow] at haccLo
        omega
      · rcases hmore with ⟨hcountlt, _, _, _⟩
        omega
  let shiftI : Std.I32 := UScalar.hcast .I32 shift
  have hshiftInt : (shift.val : Int) <= 60 := by exact_mod_cast hshift
  have hshiftCastBounds : (shift.val : Int) <= IScalar.max .I32 := by
    rw [IScalar.max_I32_eq]
    omega
  have hshiftIVal : shiftI.val = (shift.val : Int) := by
    have hs := UScalar.hcast_inBounds_spec .I32 shift hshiftCastBounds
    simp only [lift, WP.spec_ok] at hs
    exact hs
  have hcastShift : lift (UScalar.hcast .I32 shift) = ok shiftI := by
    obtain ⟨v, hv, hvval⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.hcast_inBounds_spec .I32 shift hshiftCastBounds)
    rw [hv]
    congr 1
    apply IScalar.eq_of_val_eq
    have hvval' : v.val = (shift.val : Int) := by
      simpa [WP.spec_ok] using hvval
    change v.val = shiftI.val
    rw [hshiftIVal]
    exact hvval'
  have hsumBounds : IScalar.min .I32 <= exp.val + shiftI.val /\
      exp.val + shiftI.val <= IScalar.max .I32 := by
    rw [IScalar.min_I32_eq, IScalar.max_I32_eq, hshiftIVal]
    constructor
    · omega
    · norm_num [IScalarTy.numBits, I32.numBits]
      omega
  obtain ⟨exp', hadd, haddVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.add_spec hsumBounds.1 hsumBounds.2)
  have hexpVal : exp'.val = exp.val + (shift.val : Int) := by
    rw [haddVal, hshiftIVal]
  have hnegRange :
      -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE =
        ok (-2047)#i32 := by
    unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
    change IScalar.neg (2047#i32 : Std.I32) = _
    unfold IScalar.neg IScalar.tryMk IScalar.tryMkOpt
    norm_num [IScalar.check_bounds, IScalarTy.numBits]
  have hnotReset : ¬ d'.decimal_point.val < -2047 := by
    rw [hpoint']
    omega
  have hnotResetCmp : ¬ d'.decimal_point < (-2047)#i32 := by
    simpa only [IScalar.lt_equiv] using hnotReset
  have houter : 0 < d.decimal_point.val := by omega
  have houterCmp : d.decimal_point > 0#i32 := by
    simpa only [gt_iff_lt, IScalar.lt_equiv] using houter
  have hrecordEta :
      ({ num_digits := d.num_digits, decimal_point := d.decimal_point,
         truncated := d.truncated, digits := d.digits } :
          num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
    cases d
    rfl
  let dIn : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := d.num_digits, decimal_point := d.decimal_point,
      truncated := d.truncated, digits := d.digits }
  have hdIn : dIn = d := hrecordEta
  have hcallIn : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift dIn
      (match n.val with
        | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
        | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
        | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
        | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
        | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize) =
        ok d' := by
    rw [hdIn]
    change num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d'
    exact hcall
  have hcallActual : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
      { num_digits := d.num_digits, decimal_point := d.decimal_point,
        truncated := d.truncated, digits := d.digits }
      (match n.val with
        | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
        | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
        | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
        | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
        | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize) =
        ok d' := by
    change num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d'
    exact hcall
  have hcastActual : lift (UScalar.hcast .I32
      (match n.val with
        | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
        | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
        | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
        | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
        | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize)) =
        ok shiftI := by simpa [shift] using hcastShift
  have hbody : num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
      { m := 0#u64, p_biased := 0#i32 }
      d.num_digits d.decimal_point d.truncated d.digits exp none =
        ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
          d'.digits, exp', none)) := by
    cases d with
    | mk numDigits point truncated digits =>
      have hcallMatch : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
          { num_digits := numDigits, decimal_point := point,
            truncated := truncated, digits := digits }
          (match n.val with
            | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
            | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
            | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
            | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
            | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize) =
          ok d' := by
        simpa only [shift] using hcall
      unfold num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
      simp only [if_pos houterCmp, core.option.Option.is_none]
      have hnone : (none : Option num.imp.dec2flt.common.BiasedFp).isNone = true := rfl
      simp only [hnone, if_true]
      rw [hncast]
      simp only [bind_tc_ok]
      rw [hselector]
      simp only [bind_tc_ok]
      change Aeneas.Std.bind
        (num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift
          ({ num_digits := numDigits, decimal_point := point,
             truncated := truncated, digits := digits } :
            num.imp.dec2flt.decimal_seq.DecimalSeq)
          (match n.val with
            | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
            | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
            | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
            | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
            | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize))
        (fun (d : num.imp.dec2flt.decimal_seq.DecimalSeq) => do
          let i2 ← -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
          if d.decimal_point < i2 then
            ok (cont (d.num_digits, d.decimal_point, d.truncated, d.digits,
              exp, some ({ m := 0#u64, p_biased := 0#i32 } :
                num.imp.dec2flt.common.BiasedFp)))
          else do
            let i3 ← lift (UScalar.hcast .I32
              (match n.val with
                | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize | 3 => 9#usize
                | 4 => 13#usize | 5 => 16#usize | 6 => 19#usize | 7 => 23#usize
                | 8 => 26#usize | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
                | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize | 15 => 49#usize
                | 16 => 53#usize | 17 => 56#usize | 18 => 59#usize | _ => 60#usize))
            let exp21 ← exp + i3
            ok (cont (d.num_digits, d.decimal_point, d.truncated, d.digits,
              exp21, none))) = _
      rw [hcallMatch]
      simp only [Aeneas.Std.bind, bind_tc_ok]
      rw [hnegRange]
      simp only [bind_tc_ok, hnotResetCmp, if_false]
      rw [hcastActual]
      simp only [bind_tc_ok]
      rw [hadd]
      rfl
  exact ⟨d', shift, exp', hbody, hcall, hshift, hexpVal,
    hlargePoint, hsmallPoint, hcap', hdigit', hhead', htrim,
    ⟨eta', u', hX', hY'', heta', hetau'', hsticky'⟩,
    hstallHead, hheadOneRecovery⟩

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (152)
More program dependencies (110)
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 125 entries
Technical checking records
Local closure summary
{
  "node": "P495_T1_RightLoopStep",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "f6f8ffdad01fcbfa0aff58ca2181daf845e825c6ff4525c31056d5363611eba3",
  "active_statement_hash": "75b711df51baaf5e8e889ff175905fcced0d1b70c7981f34a7e7999521dcc544",
  "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": {
    "P472_T1_DecimalRightShiftLoop0Execution": "e27252660de78d95",
    "P472_T1_DecimalRightShiftPrefixLoopSemantics": "0d5cb25f18eedbfb",
    "P472_T1_DecimalSeqRightShiftRecordSemantics": "81ed2135baca7c94",
    "P472_T1_DecimalValueBounds": "4b5c924b5d52bc9d",
    "P472_T1_LoopZeroRightShiftTransport": "b962e62f1cd86a4a",
    "P495_T1_RightPrefixLengthBounds": "1ef34b1c9650b938",
    "P495_T1_RightShiftCanonical": "fca19eef55f03341",
    "SlowShiftSelectorCertificate": "963b7a2e670856e8",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "01b84a7a35e5bcf4",
    "num_imp_dec2flt_slow_parse_long_mantissa": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "SlowShiftSelectorCertificate": "7ebc2b39d69850c4",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "c43614ff2149ecf2",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift": "1aaf1bb11c0b6a49",
    "num_imp_dec2flt_slow_parse_long_mantissa": "0ce7bdcf55d68c29",
    "num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call": "7df8a2c8e3717790",
    "num_imp_traits_Float": "94ab5f784cdf5db8"
  }
}
Prose assessment
{
  "label": "Recorded Sound: VerifierPass",
  "assessment_status": "VerifierPass",
  "fingerprint_relation": "matches current fingerprint",
  "assessment_fingerprint": "sound-v2:61ce01f9394b527e8c7b51817cd717febbe9e8f9174bb2d3af538c0715514d1c",
  "current_fingerprint": "sound-v2:61ce01f9394b527e8c7b51817cd717febbe9e8f9174bb2d3af538c0715514d1c",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Pass",
  "substantiveness_status": "Pass"
}