ParserSlowLoop1

Lean checked

Statement

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

Lemma · Slow normalization loop one with fixed endpoints

Suppose -324\leq p_0\leq0, e_0\geq0, and d_0 has decimal cells and positive-leading shape. Fix A<B through witnesses
A/2^{e_0}=V(d_0)+\eta_0,\qquad B/2^{e_0}=V(d_0)+u_0,\qquad0\leq\eta_0<u_0,\qquad d_0.\mathit{truncated}\iff0<\eta_0.
The second extracted loop terminates after at most 21 left shifts, returns `none', and has e_1\leq e_0, point zero, nonzero count, and first digit in [5,9]. Its retained count is at most 768, every retained digit is below 10, and its retained digits have the form z::zs, of length equal to the retained count, with 5\leq z<10. It preserves the two fixed endpoints:
A/2^{e_1}=V(d_1)+\eta_1,\qquad B/2^{e_1}=V(d_1)+u_1,\qquad0\leq\eta_1<u_1,\qquad d_1.\mathit{truncated}\iff0<\eta_1.
No lower bound on e_1 is claimed by this generic loop contract.

Lean statement

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

import Tablet.DecimalSeqLeftShiftCanonical
import Tablet.DecimalLeftShiftLoop1Execution
import Tablet.ParserSlowLoop0
import Tablet.P804_T1_LeftLoopStep
import Tablet.SlowShiftSelectorCertificate
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserSlowLoop1]
theorem ParserSlowLoop1 :
    forall (A B : Real) (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (exp0 : Std.I32),
      -324 <= d0.decimal_point.val -> d0.decimal_point.val <= 0 ->
      0 <= exp0.val ->
      let digits0 :=
        (d0.digits.val.take d0.num_digits.val).map (fun z => z.val)
      let integer0 : Nat := digits0.foldl (fun n digit => 10 * n + digit) 0
      let value0 : Real := integer0 * (10 : Real) ^
        (d0.decimal_point.val - (d0.num_digits.val : Int))
      d0.num_digits.val <= 768 /\
      (forall z, z ∈ d0.digits.val.take d0.num_digits.val -> z.val < 10) /\
      (exists z zs, digits0 = z :: zs /\ 1 <= z /\ z < 10 /\
        digits0.length = d0.num_digits.val) /\
      (exists eta0 u0 : Real,
        A / (2 : Real) ^ exp0.val = value0 + eta0 /\
        B / (2 : Real) ^ exp0.val = value0 + u0 /\
        0 <= eta0 /\ eta0 < u0 /\
        (d0.truncated = true <-> 0 < eta0)) ->
      exists (d1 : num.imp.dec2flt.decimal_seq.DecimalSeq)
        (exp1 : Std.I32) (steps : Nat),
        num.imp.dec2flt.slow.parse_long_mantissa_loop1
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
          { m := 0#u64, p_biased := 2047#i32 }
          d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none =
            ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits,
              exp1, none) /\
        steps <= 21 /\ exp1.val <= exp0.val /\
        d1.decimal_point.val = 0 /\ d1.num_digits.val != 0 /\
        5 <= d1.digits.val[0].val /\ d1.digits.val[0].val < 10 /\
        let digits1 :=
          (d1.digits.val.take d1.num_digits.val).map (fun z => z.val)
        let integer1 : Nat := digits1.foldl (fun n digit => 10 * n + digit) 0
        let value1 : Real := integer1 * (10 : Real) ^
          (d1.decimal_point.val - (d1.num_digits.val : Int))
        d1.num_digits.val <= 768 /\
        (forall z, z ∈ d1.digits.val.take d1.num_digits.val -> z.val < 10) /\
        (exists z zs, digits1 = z :: zs /\ 5 <= z /\ z < 10 /\
          digits1.length = d1.num_digits.val) /\
        exists eta1 u1 : Real,
          A / (2 : Real) ^ exp1.val = value1 + eta1 /\
          B / (2 : Real) ^ exp1.val = value1 + u1 /\
          0 <= eta1 /\ eta1 < u1 /\
          (d1.truncated = true <-> 0 < eta1) := 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.

Earlier prose review

An earlier review questioned the prose argument. Lean subsequently checked the formal proof. Review of the statement’s intended meaning is a separate judgment.

Proof

Use the loop-one definition in num imp dec2flt slow parse long mantissa. After s left shifts, write q,r,e,z for the current record, point, exponent, and leading digit. Maintain
\begin{gathered} -324\le r\le0,\qquad e_0-60s\le e\le e_0,\\ q\text{ has at most 768 decimal cells and retained word }z::z_s, \quad1\le z<10,\\ A/2^e=V(q)+\eta_s,quad B/2^e=V(q)+u_s,quad 0\le\eta_s<u_s,quad q.\mathit{truncated}\iff0<\eta_s. \tag{1} \end{gathered}
The simultaneous step budget is
\begin{array}{ll} r\le-19&\Longrightarrow 18s-r\le324,\\ -18\le r\le-2&\Longrightarrow s\le17,\\ r=-1, z<7&\Longrightarrow s\le18,\\ r=-1, z\ge7&\Longrightarrow s\le19,\\ r=0, z\le2&\Longrightarrow s\le19,\\ r=0, 3\le z<5&\Longrightarrow s\le20,\\ r=0, z\ge5&\Longrightarrow s\le21. \end{array} \tag{2}
For s=0, (1) is exactly the hypothesis. If r\le-19, its lower bound r\ge-324 gives 18s-r=-r\le324; every other applicable clause of (2) is immediate.
The seven clauses of (2), partitioned by r and z, first give s\le21. Together with e_0\ge0 and (1), this proves the pre-update lower bound e\ge-1260. Apply LeftLoopStep with the current endpoints A/2^e and B/2^e. Its done alternative is possible precisely at r=0,z\ge5 and supplies the unchanged extracted return. Its continuation alternative supplies the exact body and shift calls, k\le60, a successor q' with leading digit z', and
e'=e-k. \tag{3}
The subtraction is safe where it occurs: the helper uses e\ge-1260 to obtain e-k\ge-1320>-2^{31}, while k\ge0 and the intrinsic I32 upper bound on e give e-k<2^{31}. Thus (3) is the mathematical value of the checked subtraction, not an equation inferred only after termination. Moreover, (1) and k\le60 give simultaneously
e_0-60(s+1)\le e-k=e'\le e_0. \tag{4}
The helper transports shape, sticky equivalence, and both endpoint errors to 2^k(A/2^e) and 2^k(B/2^e). The integer-power law and (3) identify these quantities with A/2^{e'} and B/2^{e'}, proving all endpoint clauses of (1) for the successor. Its point alternatives also keep -324\le r'\le0: from r\le-19 one adds 18 or 19, from -18\le r<0 one obtains -1 or zero, and from point zero one remains at zero. In particular r' is never positive. The extracted point-range constant is 2047 by num imp dec2flt decimal seq DecimalSeq DECIMAL POINT RANGE; hence the comparison that would replace the record by infinity is false at every step. This explicitly accounts for the reset branch in the extracted body.
We next prove preservation of (2), retaining the correlations supplied by LeftLoopStep. If r\le-19, then r'=r+18 or r'=r+19. While r'\le-19,
18(s+1)-r'\le18s-r\le324.
If the step enters a later regime, 18s-r\le324 and r\le-19 imply s\le16, so s+1\le17, stronger than all relevant bounds. If -18\le r\le-2, the old s\le17 and r'\in\{-1,0\} give s+1\le18, again sufficient for each possible successor clause.
At r=-1,z<7, the old bound is s\le18. A stall has r'=-1 and, crucially, z'\ge7, so s+1\le19 proves the large-leading point-minus-one clause. A nonstall reaches point zero and s+1\le19, sufficient regardless of its new leading category. At r=-1,z\ge7, the helper's correlated recovery gives both r'=0 and z'\ge5; the old s\le19 therefore yields s+1\le20\le21. Finally, a continuing point-zero state has z<5. If z\le2, then s\le19 and z'\ge3; thus s+1\le20, sufficient whether z'<5 or z'\ge5. If 3\le z<5, then s\le20 and the helper gives z'\ge5, so s+1\le21. This proves every successor clause of (2). It is also the promised three-step tail analysis: a possible (-1,z<7) stall moves to (-1,z\ge7), that state moves to (0,z\ge5), while a low point-zero digit moves first to the middle point-zero class and then to the exit class.
For well-foundedness use
R(r,z)= \begin{cases} 4(-r)+0,&r<0, z\ge7,\\ 4(-r)+1,&r<0, z<7,\\ 0,&r=0, z\ge5,\\ 2,&r=0, z\le2,\\ 1,&r=0, 3\le z<5. \end{cases}
Adding 18 or 19 to a point at most -19 strictly lowers the first component; from points -18 through -2, reaching -1 or zero also strictly lowers the displayed rank. At point -1, a stall changes the final flag from one to zero, and any move to point zero has new rank at most two, below the old rank of at least four. At point zero, the two correlated progress clauses change rank 2 to at most 1, or rank 1 to 0. Every continuation therefore strictly decreases R, so the extracted loop rule terminates.
On termination (2)'s last clause gives s\le21. The postcondition is r=0,z\ge5; (1) also gives z<10, nonempty exact-length shape, all digit bounds, the exact loop call, and the two endpoint witnesses. Inequality (4) gives the claimed e_1\le e_0. No lower bound on e_1 beyond the generic inductive one was used or asserted, completing precisely the stated existential.

Lean proof


  dsimp only
  intro A B d0 exp0 hpLower hpUpper he0Lower hcanonical
  classical
  let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
    Std.I32 × Option num.imp.dec2flt.common.BiasedFp
  let recordOf : State -> num.imp.dec2flt.decimal_seq.DecimalSeq := fun x =>
    { num_digits := x.1, decimal_point := x.2.1, truncated := x.2.2.1,
      digits := x.2.2.2.1 }
  let exponentOf : State -> Std.I32 := fun x => x.2.2.2.2.1
  let resultOf : State -> Option num.imp.dec2flt.common.BiasedFp := fun x =>
    x.2.2.2.2.2
  let Digits := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    (q.digits.val.take q.num_digits.val).map (fun z => z.val)
  let Value := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    ((Digits q).foldl (fun n digit => 10 * n + digit) 0 : Nat) *
      (10 : Real) ^ (q.decimal_point.val - (q.num_digits.val : Int))
  let Shape := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    q.num_digits.val <= 768 /\
    (forall z, z ∈ q.digits.val.take q.num_digits.val -> z.val < 10) /\
    (exists z zs, Digits q = z :: zs /\ 1 <= z /\ z < 10 /\
      (Digits q).length = q.num_digits.val)
  let Endpoints := fun (q : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (e : Std.I32) =>
    exists eta u : Real,
      A / (2 : Real) ^ e.val = Value q + eta /\
      B / (2 : Real) ^ e.val = Value q + u /\
      0 <= eta /\ eta < u /\
      (q.truncated = true <-> 0 < eta)
  let Lead := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    q.digits.val[0].val
  let Budget := fun (q : num.imp.dec2flt.decimal_seq.DecimalSeq) (s : Nat) =>
    (q.decimal_point.val <= -19 ->
      (18 : Int) * s - q.decimal_point.val <= 324) /\
    (-18 <= q.decimal_point.val -> q.decimal_point.val <= -2 -> s <= 17) /\
    (q.decimal_point.val = -1 -> Lead q < 7 -> s <= 18) /\
    (q.decimal_point.val = -1 -> 7 <= Lead q -> s <= 19) /\
    (q.decimal_point.val = 0 -> Lead q <= 2 -> s <= 19) /\
    (q.decimal_point.val = 0 -> 3 <= Lead q -> Lead q < 5 -> s <= 20) /\
    (q.decimal_point.val = 0 -> 5 <= Lead q -> s <= 21)
  let Inv := fun x : State =>
    resultOf x = none /\
    -324 <= (recordOf x).decimal_point.val /\
    (recordOf x).decimal_point.val <= 0 /\
    exists s : Nat,
      exp0.val - (60 : Int) * s <= (exponentOf x).val /\
      (exponentOf x).val <= exp0.val /\
      Budget (recordOf x) s /\
      Shape (recordOf x) /\ Endpoints (recordOf x) (exponentOf x)
  let Post := fun x : State => Inv x /\
    (recordOf x).decimal_point.val = 0 /\ 5 <= Lead (recordOf x)
  let Measure := fun x : State =>
    if (recordOf x).decimal_point.val < 0 then
      4 * (- (recordOf x).decimal_point.val).toNat +
        (if 7 <= Lead (recordOf x) then 0 else 1)
    else if (recordOf x).decimal_point.val = 0 then
      if 5 <= Lead (recordOf x) then 0
      else if Lead (recordOf x) <= 2 then 2 else 1
    else 0
  let Body := fun x : State =>
    num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
      { m := 0#u64, p_biased := 2047#i32 }
      x.1 x.2.1 x.2.2.1 x.2.2.2.1 x.2.2.2.2.1 x.2.2.2.2.2
  have head_eq (q : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (z : Nat) (zs : List Nat)
      (hshape : Digits q = z :: zs)
      (hlen : (Digits q).length = q.num_digits.val) : Lead q = z := by
    simp only [Digits] at hshape hlen
    have hnumPos : 0 < q.num_digits.val := by
      rw [← hlen, hshape]
      simp
    have hindexBound : (0 : Nat) < q.digits.val.length := by
      rw [q.digits.property]
      norm_num
    have hget : q.digits.val[0]? = some q.digits.val[0] :=
      getElem?_eq_some_iff.mpr ⟨hindexBound, rfl⟩
    have hh := congrArg List.head? hshape
    rw [List.head?_eq_getElem?] at hh
    have ht : (q.digits.val.take q.num_digits.val)[0]? =
        some q.digits.val[0] := by
      rw [List.getElem?_take, if_pos hnumPos]
      exact hget
    simp only [List.getElem?_map, ht] at hh
    simpa [Lead] using Option.some.inj hh
  have hinitShape : Shape d0 := by
    exact ⟨hcanonical.1, hcanonical.2.1, hcanonical.2.2.1⟩
  have hinitEndpoints : Endpoints d0 exp0 := by
    simpa [Endpoints, Value, Digits] using hcanonical.2.2.2
  have hinitBudget : Budget d0 0 := by
    simp only [Budget]
    constructor
    · intro _
      omega
    constructor
    · omega
    constructor
    · omega
    constructor
    · omega
    constructor
    · omega
    constructor <;> omega
  have hinit : Inv
      (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits,
        exp0, none) := by
    simp only [Inv, recordOf, exponentOf, resultOf]
    exact ⟨by simp, hpLower, hpUpper, 0, by norm_num, by omega,
      hinitBudget, hinitShape, hinitEndpoints⟩
  have hspec :
      num.imp.dec2flt.slow.parse_long_mantissa_loop1
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none
        ⦃ Post ⦄ := by
    unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1
    apply Aeneas.Std.loop.spec_decr_nat Measure Inv Post Body
      (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none)
    · intro x hx
      rcases x with ⟨i, p, t, a, e, lr⟩
      simp only [Inv, recordOf, exponentOf, resultOf] at hx
      rcases hx with ⟨hlr, hpLo, hpHi, s, heLo, heHi, hbudget,
        hshape, hend⟩
      simp only [Budget] at hbudget
      simp only [Shape, Digits] at hshape
      simp only [Endpoints, Value, Digits] at hend
      simp only [Body, Prod.fst, Prod.snd]
      rw [hlr]
      rcases hshape.2.2 with ⟨z0, zs0, hzshape, hzLo, hzHi, hzlen⟩
      have hlead : Lead
          { num_digits := i, decimal_point := p, truncated := t, digits := a } =
          z0 := head_eq _ z0 zs0 hzshape hzlen
      have hs21 : s <= 21 := by
        by_cases hp19 : p.val <= -19
        · have hb := hbudget.1 hp19
          omega
        · by_cases hp2 : p.val <= -2
          · exact (hbudget.2.1 (by omega) hp2).trans (by omega)
          · by_cases hm1 : p.val = -1
            · by_cases hz7 : Lead
                { num_digits := i, decimal_point := p,
                  truncated := t, digits := a } < 7
              · exact (hbudget.2.2.1 hm1 hz7).trans (by omega)
              · exact (hbudget.2.2.2.1 hm1 (by omega)).trans (by omega)
            · have hp0 : p.val = 0 := by omega
              by_cases hz2 : Lead
                  { num_digits := i, decimal_point := p,
                    truncated := t, digits := a } <= 2
              · exact (hbudget.2.2.2.2.1 hp0 hz2).trans (by omega)
              · by_cases hz5 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 5
                · exact (hbudget.2.2.2.2.2.1 hp0 (by omega) hz5).trans
                    (by omega)
                · exact (hbudget.2.2.2.2.2.2 hp0 (by omega)).trans
                    (by omega)
      have heStepLo : -1260 <= e.val := by
        have he0 : 0 <= exp0.val := he0Lower
        nlinarith
      have hstep := P804_T1_LeftLoopStep
        (A / (2 : Real) ^ e.val) (B / (2 : Real) ^ e.val)
        { num_digits := i, decimal_point := p, truncated := t, digits := a }
        e hpLo hpHi heStepLo hshape hend
      rcases hstep with hdone | hcont
      · rcases hdone with ⟨hbody, hp0, hfive⟩
        rw [hbody]
        simp only [WP.spec_ok]
        simp only [Post, Inv, recordOf, exponentOf, resultOf, Lead]
        exact ⟨⟨by simp, hpLo, hpHi, s, heLo, heHi, hbudget,
          ⟨hshape.1, hshape.2.1, z0, zs0, hzshape, hzLo, hzHi, hzlen⟩,
          hend⟩, hp0, hfive⟩
      · rcases hcont with ⟨d', shift, growth, e', z, z', zs, zs',
          hbody, hleft, hgrowth, hk, he', hzshapeIn, hzshapeOut,
          hzInLo, hzInHi, hzOutLo, hzOutHi, hlarge, hsmall, hstall,
          hrecover, hzeroInput, hzeroProgress, hshapeEnd⟩
        change (p.val <= -19 ->
          d'.decimal_point.val = p.val + 18 \/
          d'.decimal_point.val = p.val + 19) at hlarge
        change (-18 <= p.val -> p.val < 0 ->
          d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0) at hsmall
        change (p.val = -1 -> d'.decimal_point.val = -1 -> 7 <= z') at hstall
        change (p.val = -1 -> 7 <= z ->
          d'.decimal_point.val = 0 /\ 5 <= z') at hrecover
        change (p.val = 0 -> z < 5) at hzeroInput
        change (p.val = 0 -> d'.decimal_point.val = 0 /\
          (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) at hzeroProgress
        rcases hshapeEnd with ⟨hcap', hdigit', hlen', hend'⟩
        have hleadIn : Lead
            { num_digits := i, decimal_point := p, truncated := t, digits := a } =
            z := head_eq _ z zs hzshapeIn (by simpa [Digits] using hzlen)
        have hleadOut : Lead d' = z' :=
          head_eq d' z' zs' hzshapeOut hlen'
        have hshape' : Shape d' := by
          exact ⟨hcap', hdigit', z', zs', hzshapeOut, hzOutLo, hzOutHi, hlen'⟩
        have hs20 : s <= 20 := by
          by_cases hp19 : p.val <= -19
          · have hb := hbudget.1 hp19
            omega
          · by_cases hp2 : p.val <= -2
            · exact (hbudget.2.1 (by omega) hp2).trans (by omega)
            · by_cases hm1 : p.val = -1
              · by_cases hz7 : Lead
                  { num_digits := i, decimal_point := p,
                    truncated := t, digits := a } < 7
                · exact (hbudget.2.2.1 hm1 hz7).trans (by omega)
                · exact (hbudget.2.2.2.1 hm1 (by omega)).trans (by omega)
              · have hp0 : p.val = 0 := by omega
                have hz5 : z < 5 := hzeroInput hp0
                by_cases hz2 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } <= 2
                · exact (hbudget.2.2.2.2.1 hp0 hz2).trans (by omega)
                · exact (hbudget.2.2.2.2.2.1 hp0 (by omega)
                    (by rw [hleadIn]; exact hz5)).trans (by omega)
        have hp'Lo : -324 <= d'.decimal_point.val := by
          by_cases hp19 : p.val <= -19
          · rcases hlarge hp19 with h | h <;> omega
          · by_cases hpneg : p.val < 0
            · rcases hsmall (by omega) hpneg with h | h <;> omega
            · have hz := hzeroProgress (by omega)
              omega
        have hp'Hi : d'.decimal_point.val <= 0 := by
          by_cases hp19 : p.val <= -19
          · rcases hlarge hp19 with h | h <;> omega
          · by_cases hpneg : p.val < 0
            · rcases hsmall (by omega) hpneg with h | h <;> omega
            · have hz := hzeroProgress (by omega)
              omega
        have heNextLo : exp0.val - (60 : Int) * (s + 1) <= e'.val := by
          rw [he']
          norm_num only [Nat.cast_add, Nat.cast_one]
          nlinarith
        have heNextHi : e'.val <= exp0.val := by
          rw [he']
          have hknonneg : (0 : Int) <= shift.val := by omega
          omega
        have hscaleA :
            (2 : Real) ^ shift.val * (A / (2 : Real) ^ e.val) =
              A / (2 : Real) ^ e'.val := by
          rw [he', zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
          norm_num only [zpow_natCast]
          field_simp
        have hscaleB :
            (2 : Real) ^ shift.val * (B / (2 : Real) ^ e.val) =
              B / (2 : Real) ^ e'.val := by
          rw [he', zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
          norm_num only [zpow_natCast]
          field_simp
        have hendNext : Endpoints d' e' := by
          rcases hend' with ⟨eta, u, hA, hB, heta, hetau, hsticky⟩
          refine ⟨eta, u, ?_, ?_, heta, hetau, hsticky⟩
          · rw [← hscaleA]
            exact hA
          · rw [← hscaleB]
            exact hB
        have hbudgetNext : Budget d' (s + 1) := by
          simp only [Budget]
          constructor
          · intro hp'19
            have hp19 : p.val <= -19 := by
              by_contra h
              by_cases hpneg : p.val < 0
              · rcases hsmall (by omega) hpneg with h | h <;> omega
              · have hz := hzeroProgress (by omega)
                omega
            have hb := hbudget.1 hp19
            rcases hlarge hp19 with h | h <;> omega
          constructor
          · intro hp'18 hp'2
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                rcases hsmall (by omega) (by omega) with h | h <;> omega
              · by_cases hm1 : p.val = -1
                · rcases hsmall (by omega) (by omega) with h | h <;> omega
                · have hz := hzeroProgress (by omega)
                  omega
          constructor
          · intro hp'm1 hz'7
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    have hs := hstall hm1 hp'm1
                    rw [hleadOut] at hz'7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    omega
                · have hz := hzeroProgress (by omega)
                  omega
          constructor
          · intro hp'm1 hz'7
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    omega
                · have hz := hzeroProgress (by omega)
                  omega
          constructor
          · intro hp'0 hz'2
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    rw [hleadOut] at hz'2
                    omega
                · have hz := hzeroProgress (by omega)
                  rw [hleadOut] at hz'2
                  by_cases hz2 : z <= 2
                  · have := hz.2.1 hz2
                    omega
                  · have := hz.2.2 (by omega)
                    omega
          constructor
          · intro hp'0 hz'3 hz'5
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    rw [hleadOut] at hz'5
                    omega
                · have hz := hzeroProgress (by omega)
                  rw [hleadOut] at hz'3 hz'5
                  by_cases hz2 : z <= 2
                  · have hb := hbudget.2.2.2.2.1 (by omega)
                        (by omega)
                    omega
                  · have := hz.2.2 (by omega)
                    omega
          · intro hp'0 hz'5
            omega
        rw [hbody]
        simp only [WP.spec_ok]
        constructor
        · simp only [Inv, recordOf, exponentOf, resultOf]
          exact ⟨by simp, hp'Lo, hp'Hi, s + 1, heNextLo, heNextHi,
            hbudgetNext, hshape', hendNext⟩
        · simp only [Measure, recordOf]
          have hd'eta :
              ({ 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
          have hleadOutEta : Lead
              { num_digits := d'.num_digits,
                decimal_point := d'.decimal_point,
                truncated := d'.truncated,
                digits := d'.digits } = z' := by
            rw [hd'eta]
            exact hleadOut
          rw [hleadIn, hleadOutEta]
          by_cases hp19 : p.val <= -19
          · rcases hlarge hp19 with h | h
            · have hpneg : p.val < 0 := by omega
              rw [h]
              by_cases hp'neg : p.val + 18 < 0
              · simp only [hpneg, hp'neg, if_true]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                have hp'Nat : ((-(p.val + 18)).toNat : Int) =
                    -(p.val + 18) := Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
              · have hp'0 : p.val + 18 = 0 := by omega
                simp only [hpneg, hp'neg, hp'0, if_true, if_false]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
            · have hpneg : p.val < 0 := by omega
              rw [h]
              by_cases hp'neg : p.val + 19 < 0
              · simp only [hpneg, hp'neg, if_true]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                have hp'Nat : ((-(p.val + 19)).toNat : Int) =
                    -(p.val + 19) := Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
              · have hp'0 : p.val + 19 = 0 := by omega
                simp only [hpneg, hp'neg, hp'0, if_true, if_false]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                by_cases hz'5 : 5 <= z'
                · simp only [hz'5, if_true]
                  norm_num at ⊢
                  omega
                · have hz'5' : ¬ 5 <= z' := hz'5
                  simp only [hz'5', if_false]
                  by_cases hz'2 : z' <= 2
                  · simp only [hz'2, if_true]
                    norm_num at ⊢
                    omega
                  · simp only [hz'2, if_false]
                    norm_num at ⊢
                    omega
          · by_cases hp2 : p.val <= -2
            · rcases hsmall (by omega) (by omega) with h | h
              · have hpneg : p.val < 0 := by omega
                rw [h]
                simp only [hpneg, if_true]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
              · have hpneg : p.val < 0 := by omega
                rw [h]
                simp only [hpneg, if_true, if_false]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                by_cases hz'5 : 5 <= z'
                · simp only [hz'5, if_true]
                  norm_num at ⊢
                  omega
                · have hz'5' : ¬ 5 <= z' := hz'5
                  simp only [hz'5', if_false]
                  by_cases hz'2 : z' <= 2
                  · simp only [hz'2, if_true]
                    norm_num at ⊢
                    omega
                  · simp only [hz'2, if_false]
                    norm_num at ⊢
                    omega
            · by_cases hm1 : p.val = -1
              · by_cases hp'm1 : d'.decimal_point.val = -1
                · have hs := hstall hm1 hp'm1
                  have hnold : ¬ 7 <= z := by
                    intro hz7
                    have hr := hrecover hm1 hz7
                    omega
                  have hpNat : (-p.val).toNat = 1 := by omega
                  simp [hm1, hp'm1, hpNat, hnold, hs]
                · have hp'0 : d'.decimal_point.val = 0 := by
                    rcases hsmall (by omega) (by omega) with h | h <;> omega
                  have hpNat : (-p.val).toNat = 1 := by omega
                  simp only [hm1, hp'0, hpNat, if_true, if_false]
                  by_cases hz'5 : 5 <= z'
                  · simp only [hz'5, if_true]
                    norm_num at ⊢
                  · simp only [hz'5, if_false]
                    by_cases hz'2 : z' <= 2
                    · simp only [hz'2, if_true]
                      norm_num at ⊢
                      by_cases hz7 : 7 <= z <;> simp [hz7]
                    · simp only [hz'2, if_false]
                      norm_num at ⊢
                      by_cases hz7 : 7 <= z <;> simp [hz7]
              · have hp0 : p.val = 0 := by omega
                have hz0 := hzeroProgress hp0
                have hz5 := hzeroInput hp0
                rw [hz0.1]
                simp only [hp0, if_true, if_false]
                by_cases hz2 : z <= 2
                · have hz'3 := hz0.2.1 hz2
                  have hz5' : ¬ 5 <= z := by omega
                  simp only [hz5', hz2, if_false, if_true]
                  by_cases hz'5 : 5 <= z'
                  · simp only [hz'5, if_true]
                    norm_num
                  · have hz'2 : ¬ z' <= 2 := by omega
                    simp only [hz'5, hz'2, if_false]
                    norm_num
                · have hz'5 := hz0.2.2 (by omega)
                  have hz5' : ¬ 5 <= z := by omega
                  simp only [hz5', hz2, hz'5, if_false, if_true]
                  norm_num
    · exact hinit
  obtain ⟨out, hcall, hout⟩ := Aeneas.Std.WP.spec_imp_exists hspec
  rcases out with ⟨i1, p1, t1, a1, e1, lr1⟩
  simp only [Post, Inv, recordOf, exponentOf, resultOf] at hout
  rcases hout with
    ⟨⟨hlr1, hp1Lo, hp1Hi, steps, he1Lo, he1Hi, hbudget1,
      hshape1, hend1⟩, hp1, hlead1⟩
  have hsteps : steps <= 21 := by
    exact hbudget1.2.2.2.2.2.2 hp1 hlead1
  let d1 : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := i1, decimal_point := p1, truncated := t1, digits := a1 }
  rcases hshape1.2.2 with ⟨z1, zs1, hzshape1, hz1Lo, hz1Hi, hzlen1⟩
  have hleadEq1 : Lead d1 = z1 := head_eq d1 z1 zs1
    (by simpa [Digits, d1] using hzshape1) (by simpa [Digits, d1] using hzlen1)
  have hz1Five : 5 <= z1 := by
    rw [← hleadEq1]
    simpa [Lead, d1] using hlead1
  refine ⟨d1, e1, steps, ?_, hsteps, he1Hi, hp1, ?_, ?_, ?_, ?_⟩
  · simpa [d1, hlr1] using hcall
  · have : 0 < z1 := by omega
    have hlenPos : 0 < (Digits d1).length := by rw [hzshape1]; simp
    have hcountPos : 0 < d1.num_digits.val := by rw [← hzlen1]; exact hlenPos
    simpa [d1, bne_iff_ne] using hcountPos.ne'
  · simpa [Lead, d1] using hlead1
  · have hz : Lead d1 < 10 := by rw [hleadEq1]; exact hz1Hi
    simpa [Lead] using hz
  · dsimp only
    refine ⟨?_, ?_, ?_, ?_⟩
    · simpa [d1] using hshape1.1
    · simpa [d1] using hshape1.2.1
    · refine ⟨z1, zs1, ?_, hz1Five, hz1Hi, ?_⟩
      · simpa [Digits, d1] using hzshape1
      · simpa [Digits, d1] using hzlen1
    · simpa [Endpoints, Value, Digits, d1] using hend1

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.DecimalSeqLeftShiftCanonical
import Tablet.DecimalLeftShiftLoop1Execution
import Tablet.ParserSlowLoop0
import Tablet.P804_T1_LeftLoopStep
import Tablet.SlowShiftSelectorCertificate
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserSlowLoop1]
theorem ParserSlowLoop1 :
    forall (A B : Real) (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (exp0 : Std.I32),
      -324 <= d0.decimal_point.val -> d0.decimal_point.val <= 0 ->
      0 <= exp0.val ->
      let digits0 :=
        (d0.digits.val.take d0.num_digits.val).map (fun z => z.val)
      let integer0 : Nat := digits0.foldl (fun n digit => 10 * n + digit) 0
      let value0 : Real := integer0 * (10 : Real) ^
        (d0.decimal_point.val - (d0.num_digits.val : Int))
      d0.num_digits.val <= 768 /\
      (forall z, z ∈ d0.digits.val.take d0.num_digits.val -> z.val < 10) /\
      (exists z zs, digits0 = z :: zs /\ 1 <= z /\ z < 10 /\
        digits0.length = d0.num_digits.val) /\
      (exists eta0 u0 : Real,
        A / (2 : Real) ^ exp0.val = value0 + eta0 /\
        B / (2 : Real) ^ exp0.val = value0 + u0 /\
        0 <= eta0 /\ eta0 < u0 /\
        (d0.truncated = true <-> 0 < eta0)) ->
      exists (d1 : num.imp.dec2flt.decimal_seq.DecimalSeq)
        (exp1 : Std.I32) (steps : Nat),
        num.imp.dec2flt.slow.parse_long_mantissa_loop1
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
          { m := 0#u64, p_biased := 2047#i32 }
          d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none =
            ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits,
              exp1, none) /\
        steps <= 21 /\ exp1.val <= exp0.val /\
        d1.decimal_point.val = 0 /\ d1.num_digits.val != 0 /\
        5 <= d1.digits.val[0].val /\ d1.digits.val[0].val < 10 /\
        let digits1 :=
          (d1.digits.val.take d1.num_digits.val).map (fun z => z.val)
        let integer1 : Nat := digits1.foldl (fun n digit => 10 * n + digit) 0
        let value1 : Real := integer1 * (10 : Real) ^
          (d1.decimal_point.val - (d1.num_digits.val : Int))
        d1.num_digits.val <= 768 /\
        (forall z, z ∈ d1.digits.val.take d1.num_digits.val -> z.val < 10) /\
        (exists z zs, digits1 = z :: zs /\ 5 <= z /\ z < 10 /\
          digits1.length = d1.num_digits.val) /\
        exists eta1 u1 : Real,
          A / (2 : Real) ^ exp1.val = value1 + eta1 /\
          B / (2 : Real) ^ exp1.val = value1 + u1 /\
          0 <= eta1 /\ eta1 < u1 /\
          (d1.truncated = true <-> 0 < eta1) := by
-- BODY
  dsimp only
  intro A B d0 exp0 hpLower hpUpper he0Lower hcanonical
  classical
  let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
    Std.I32 × Option num.imp.dec2flt.common.BiasedFp
  let recordOf : State -> num.imp.dec2flt.decimal_seq.DecimalSeq := fun x =>
    { num_digits := x.1, decimal_point := x.2.1, truncated := x.2.2.1,
      digits := x.2.2.2.1 }
  let exponentOf : State -> Std.I32 := fun x => x.2.2.2.2.1
  let resultOf : State -> Option num.imp.dec2flt.common.BiasedFp := fun x =>
    x.2.2.2.2.2
  let Digits := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    (q.digits.val.take q.num_digits.val).map (fun z => z.val)
  let Value := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    ((Digits q).foldl (fun n digit => 10 * n + digit) 0 : Nat) *
      (10 : Real) ^ (q.decimal_point.val - (q.num_digits.val : Int))
  let Shape := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    q.num_digits.val <= 768 /\
    (forall z, z ∈ q.digits.val.take q.num_digits.val -> z.val < 10) /\
    (exists z zs, Digits q = z :: zs /\ 1 <= z /\ z < 10 /\
      (Digits q).length = q.num_digits.val)
  let Endpoints := fun (q : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (e : Std.I32) =>
    exists eta u : Real,
      A / (2 : Real) ^ e.val = Value q + eta /\
      B / (2 : Real) ^ e.val = Value q + u /\
      0 <= eta /\ eta < u /\
      (q.truncated = true <-> 0 < eta)
  let Lead := fun q : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    q.digits.val[0].val
  let Budget := fun (q : num.imp.dec2flt.decimal_seq.DecimalSeq) (s : Nat) =>
    (q.decimal_point.val <= -19 ->
      (18 : Int) * s - q.decimal_point.val <= 324) /\
    (-18 <= q.decimal_point.val -> q.decimal_point.val <= -2 -> s <= 17) /\
    (q.decimal_point.val = -1 -> Lead q < 7 -> s <= 18) /\
    (q.decimal_point.val = -1 -> 7 <= Lead q -> s <= 19) /\
    (q.decimal_point.val = 0 -> Lead q <= 2 -> s <= 19) /\
    (q.decimal_point.val = 0 -> 3 <= Lead q -> Lead q < 5 -> s <= 20) /\
    (q.decimal_point.val = 0 -> 5 <= Lead q -> s <= 21)
  let Inv := fun x : State =>
    resultOf x = none /\
    -324 <= (recordOf x).decimal_point.val /\
    (recordOf x).decimal_point.val <= 0 /\
    exists s : Nat,
      exp0.val - (60 : Int) * s <= (exponentOf x).val /\
      (exponentOf x).val <= exp0.val /\
      Budget (recordOf x) s /\
      Shape (recordOf x) /\ Endpoints (recordOf x) (exponentOf x)
  let Post := fun x : State => Inv x /\
    (recordOf x).decimal_point.val = 0 /\ 5 <= Lead (recordOf x)
  let Measure := fun x : State =>
    if (recordOf x).decimal_point.val < 0 then
      4 * (- (recordOf x).decimal_point.val).toNat +
        (if 7 <= Lead (recordOf x) then 0 else 1)
    else if (recordOf x).decimal_point.val = 0 then
      if 5 <= Lead (recordOf x) then 0
      else if Lead (recordOf x) <= 2 then 2 else 1
    else 0
  let Body := fun x : State =>
    num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
      { m := 0#u64, p_biased := 2047#i32 }
      x.1 x.2.1 x.2.2.1 x.2.2.2.1 x.2.2.2.2.1 x.2.2.2.2.2
  have head_eq (q : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (z : Nat) (zs : List Nat)
      (hshape : Digits q = z :: zs)
      (hlen : (Digits q).length = q.num_digits.val) : Lead q = z := by
    simp only [Digits] at hshape hlen
    have hnumPos : 0 < q.num_digits.val := by
      rw [← hlen, hshape]
      simp
    have hindexBound : (0 : Nat) < q.digits.val.length := by
      rw [q.digits.property]
      norm_num
    have hget : q.digits.val[0]? = some q.digits.val[0] :=
      getElem?_eq_some_iff.mpr ⟨hindexBound, rfl⟩
    have hh := congrArg List.head? hshape
    rw [List.head?_eq_getElem?] at hh
    have ht : (q.digits.val.take q.num_digits.val)[0]? =
        some q.digits.val[0] := by
      rw [List.getElem?_take, if_pos hnumPos]
      exact hget
    simp only [List.getElem?_map, ht] at hh
    simpa [Lead] using Option.some.inj hh
  have hinitShape : Shape d0 := by
    exact ⟨hcanonical.1, hcanonical.2.1, hcanonical.2.2.1⟩
  have hinitEndpoints : Endpoints d0 exp0 := by
    simpa [Endpoints, Value, Digits] using hcanonical.2.2.2
  have hinitBudget : Budget d0 0 := by
    simp only [Budget]
    constructor
    · intro _
      omega
    constructor
    · omega
    constructor
    · omega
    constructor
    · omega
    constructor
    · omega
    constructor <;> omega
  have hinit : Inv
      (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits,
        exp0, none) := by
    simp only [Inv, recordOf, exponentOf, resultOf]
    exact ⟨by simp, hpLower, hpUpper, 0, by norm_num, by omega,
      hinitBudget, hinitShape, hinitEndpoints⟩
  have hspec :
      num.imp.dec2flt.slow.parse_long_mantissa_loop1
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d0.num_digits d0.decimal_point d0.truncated d0.digits exp0 none
        ⦃ Post ⦄ := by
    unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1
    apply Aeneas.Std.loop.spec_decr_nat Measure Inv Post Body
      (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none)
    · intro x hx
      rcases x with ⟨i, p, t, a, e, lr⟩
      simp only [Inv, recordOf, exponentOf, resultOf] at hx
      rcases hx with ⟨hlr, hpLo, hpHi, s, heLo, heHi, hbudget,
        hshape, hend⟩
      simp only [Budget] at hbudget
      simp only [Shape, Digits] at hshape
      simp only [Endpoints, Value, Digits] at hend
      simp only [Body, Prod.fst, Prod.snd]
      rw [hlr]
      rcases hshape.2.2 with ⟨z0, zs0, hzshape, hzLo, hzHi, hzlen⟩
      have hlead : Lead
          { num_digits := i, decimal_point := p, truncated := t, digits := a } =
          z0 := head_eq _ z0 zs0 hzshape hzlen
      have hs21 : s <= 21 := by
        by_cases hp19 : p.val <= -19
        · have hb := hbudget.1 hp19
          omega
        · by_cases hp2 : p.val <= -2
          · exact (hbudget.2.1 (by omega) hp2).trans (by omega)
          · by_cases hm1 : p.val = -1
            · by_cases hz7 : Lead
                { num_digits := i, decimal_point := p,
                  truncated := t, digits := a } < 7
              · exact (hbudget.2.2.1 hm1 hz7).trans (by omega)
              · exact (hbudget.2.2.2.1 hm1 (by omega)).trans (by omega)
            · have hp0 : p.val = 0 := by omega
              by_cases hz2 : Lead
                  { num_digits := i, decimal_point := p,
                    truncated := t, digits := a } <= 2
              · exact (hbudget.2.2.2.2.1 hp0 hz2).trans (by omega)
              · by_cases hz5 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 5
                · exact (hbudget.2.2.2.2.2.1 hp0 (by omega) hz5).trans
                    (by omega)
                · exact (hbudget.2.2.2.2.2.2 hp0 (by omega)).trans
                    (by omega)
      have heStepLo : -1260 <= e.val := by
        have he0 : 0 <= exp0.val := he0Lower
        nlinarith
      have hstep := P804_T1_LeftLoopStep
        (A / (2 : Real) ^ e.val) (B / (2 : Real) ^ e.val)
        { num_digits := i, decimal_point := p, truncated := t, digits := a }
        e hpLo hpHi heStepLo hshape hend
      rcases hstep with hdone | hcont
      · rcases hdone with ⟨hbody, hp0, hfive⟩
        rw [hbody]
        simp only [WP.spec_ok]
        simp only [Post, Inv, recordOf, exponentOf, resultOf, Lead]
        exact ⟨⟨by simp, hpLo, hpHi, s, heLo, heHi, hbudget,
          ⟨hshape.1, hshape.2.1, z0, zs0, hzshape, hzLo, hzHi, hzlen⟩,
          hend⟩, hp0, hfive⟩
      · rcases hcont with ⟨d', shift, growth, e', z, z', zs, zs',
          hbody, hleft, hgrowth, hk, he', hzshapeIn, hzshapeOut,
          hzInLo, hzInHi, hzOutLo, hzOutHi, hlarge, hsmall, hstall,
          hrecover, hzeroInput, hzeroProgress, hshapeEnd⟩
        change (p.val <= -19 ->
          d'.decimal_point.val = p.val + 18 \/
          d'.decimal_point.val = p.val + 19) at hlarge
        change (-18 <= p.val -> p.val < 0 ->
          d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0) at hsmall
        change (p.val = -1 -> d'.decimal_point.val = -1 -> 7 <= z') at hstall
        change (p.val = -1 -> 7 <= z ->
          d'.decimal_point.val = 0 /\ 5 <= z') at hrecover
        change (p.val = 0 -> z < 5) at hzeroInput
        change (p.val = 0 -> d'.decimal_point.val = 0 /\
          (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) at hzeroProgress
        rcases hshapeEnd with ⟨hcap', hdigit', hlen', hend'⟩
        have hleadIn : Lead
            { num_digits := i, decimal_point := p, truncated := t, digits := a } =
            z := head_eq _ z zs hzshapeIn (by simpa [Digits] using hzlen)
        have hleadOut : Lead d' = z' :=
          head_eq d' z' zs' hzshapeOut hlen'
        have hshape' : Shape d' := by
          exact ⟨hcap', hdigit', z', zs', hzshapeOut, hzOutLo, hzOutHi, hlen'⟩
        have hs20 : s <= 20 := by
          by_cases hp19 : p.val <= -19
          · have hb := hbudget.1 hp19
            omega
          · by_cases hp2 : p.val <= -2
            · exact (hbudget.2.1 (by omega) hp2).trans (by omega)
            · by_cases hm1 : p.val = -1
              · by_cases hz7 : Lead
                  { num_digits := i, decimal_point := p,
                    truncated := t, digits := a } < 7
                · exact (hbudget.2.2.1 hm1 hz7).trans (by omega)
                · exact (hbudget.2.2.2.1 hm1 (by omega)).trans (by omega)
              · have hp0 : p.val = 0 := by omega
                have hz5 : z < 5 := hzeroInput hp0
                by_cases hz2 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } <= 2
                · exact (hbudget.2.2.2.2.1 hp0 hz2).trans (by omega)
                · exact (hbudget.2.2.2.2.2.1 hp0 (by omega)
                    (by rw [hleadIn]; exact hz5)).trans (by omega)
        have hp'Lo : -324 <= d'.decimal_point.val := by
          by_cases hp19 : p.val <= -19
          · rcases hlarge hp19 with h | h <;> omega
          · by_cases hpneg : p.val < 0
            · rcases hsmall (by omega) hpneg with h | h <;> omega
            · have hz := hzeroProgress (by omega)
              omega
        have hp'Hi : d'.decimal_point.val <= 0 := by
          by_cases hp19 : p.val <= -19
          · rcases hlarge hp19 with h | h <;> omega
          · by_cases hpneg : p.val < 0
            · rcases hsmall (by omega) hpneg with h | h <;> omega
            · have hz := hzeroProgress (by omega)
              omega
        have heNextLo : exp0.val - (60 : Int) * (s + 1) <= e'.val := by
          rw [he']
          norm_num only [Nat.cast_add, Nat.cast_one]
          nlinarith
        have heNextHi : e'.val <= exp0.val := by
          rw [he']
          have hknonneg : (0 : Int) <= shift.val := by omega
          omega
        have hscaleA :
            (2 : Real) ^ shift.val * (A / (2 : Real) ^ e.val) =
              A / (2 : Real) ^ e'.val := by
          rw [he', zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
          norm_num only [zpow_natCast]
          field_simp
        have hscaleB :
            (2 : Real) ^ shift.val * (B / (2 : Real) ^ e.val) =
              B / (2 : Real) ^ e'.val := by
          rw [he', zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
          norm_num only [zpow_natCast]
          field_simp
        have hendNext : Endpoints d' e' := by
          rcases hend' with ⟨eta, u, hA, hB, heta, hetau, hsticky⟩
          refine ⟨eta, u, ?_, ?_, heta, hetau, hsticky⟩
          · rw [← hscaleA]
            exact hA
          · rw [← hscaleB]
            exact hB
        have hbudgetNext : Budget d' (s + 1) := by
          simp only [Budget]
          constructor
          · intro hp'19
            have hp19 : p.val <= -19 := by
              by_contra h
              by_cases hpneg : p.val < 0
              · rcases hsmall (by omega) hpneg with h | h <;> omega
              · have hz := hzeroProgress (by omega)
                omega
            have hb := hbudget.1 hp19
            rcases hlarge hp19 with h | h <;> omega
          constructor
          · intro hp'18 hp'2
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                rcases hsmall (by omega) (by omega) with h | h <;> omega
              · by_cases hm1 : p.val = -1
                · rcases hsmall (by omega) (by omega) with h | h <;> omega
                · have hz := hzeroProgress (by omega)
                  omega
          constructor
          · intro hp'm1 hz'7
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    have hs := hstall hm1 hp'm1
                    rw [hleadOut] at hz'7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    omega
                · have hz := hzeroProgress (by omega)
                  omega
          constructor
          · intro hp'm1 hz'7
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    omega
                · have hz := hzeroProgress (by omega)
                  omega
          constructor
          · intro hp'0 hz'2
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    rw [hleadOut] at hz'2
                    omega
                · have hz := hzeroProgress (by omega)
                  rw [hleadOut] at hz'2
                  by_cases hz2 : z <= 2
                  · have := hz.2.1 hz2
                    omega
                  · have := hz.2.2 (by omega)
                    omega
          constructor
          · intro hp'0 hz'3 hz'5
            by_cases hp19 : p.val <= -19
            · have hb := hbudget.1 hp19
              rcases hlarge hp19 with h | h <;> omega
            · by_cases hp2 : p.val <= -2
              · have hb := hbudget.2.1 (by omega) hp2
                omega
              · by_cases hm1 : p.val = -1
                · by_cases hz7 : Lead
                    { num_digits := i, decimal_point := p,
                      truncated := t, digits := a } < 7
                  · have hb := hbudget.2.2.1 hm1 hz7
                    omega
                  · have hr := hrecover hm1 (by omega)
                    rw [hleadOut] at hz'5
                    omega
                · have hz := hzeroProgress (by omega)
                  rw [hleadOut] at hz'3 hz'5
                  by_cases hz2 : z <= 2
                  · have hb := hbudget.2.2.2.2.1 (by omega)
                        (by omega)
                    omega
                  · have := hz.2.2 (by omega)
                    omega
          · intro hp'0 hz'5
            omega
        rw [hbody]
        simp only [WP.spec_ok]
        constructor
        · simp only [Inv, recordOf, exponentOf, resultOf]
          exact ⟨by simp, hp'Lo, hp'Hi, s + 1, heNextLo, heNextHi,
            hbudgetNext, hshape', hendNext⟩
        · simp only [Measure, recordOf]
          have hd'eta :
              ({ 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
          have hleadOutEta : Lead
              { num_digits := d'.num_digits,
                decimal_point := d'.decimal_point,
                truncated := d'.truncated,
                digits := d'.digits } = z' := by
            rw [hd'eta]
            exact hleadOut
          rw [hleadIn, hleadOutEta]
          by_cases hp19 : p.val <= -19
          · rcases hlarge hp19 with h | h
            · have hpneg : p.val < 0 := by omega
              rw [h]
              by_cases hp'neg : p.val + 18 < 0
              · simp only [hpneg, hp'neg, if_true]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                have hp'Nat : ((-(p.val + 18)).toNat : Int) =
                    -(p.val + 18) := Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
              · have hp'0 : p.val + 18 = 0 := by omega
                simp only [hpneg, hp'neg, hp'0, if_true, if_false]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
            · have hpneg : p.val < 0 := by omega
              rw [h]
              by_cases hp'neg : p.val + 19 < 0
              · simp only [hpneg, hp'neg, if_true]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                have hp'Nat : ((-(p.val + 19)).toNat : Int) =
                    -(p.val + 19) := Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
              · have hp'0 : p.val + 19 = 0 := by omega
                simp only [hpneg, hp'neg, hp'0, if_true, if_false]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                by_cases hz'5 : 5 <= z'
                · simp only [hz'5, if_true]
                  norm_num at ⊢
                  omega
                · have hz'5' : ¬ 5 <= z' := hz'5
                  simp only [hz'5', if_false]
                  by_cases hz'2 : z' <= 2
                  · simp only [hz'2, if_true]
                    norm_num at ⊢
                    omega
                  · simp only [hz'2, if_false]
                    norm_num at ⊢
                    omega
          · by_cases hp2 : p.val <= -2
            · rcases hsmall (by omega) (by omega) with h | h
              · have hpneg : p.val < 0 := by omega
                rw [h]
                simp only [hpneg, if_true]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                split <;> split <;> omega
              · have hpneg : p.val < 0 := by omega
                rw [h]
                simp only [hpneg, if_true, if_false]
                have hpNat : ((-p.val).toNat : Int) = -p.val :=
                  Int.toNat_of_nonneg (by omega)
                by_cases hz'5 : 5 <= z'
                · simp only [hz'5, if_true]
                  norm_num at ⊢
                  omega
                · have hz'5' : ¬ 5 <= z' := hz'5
                  simp only [hz'5', if_false]
                  by_cases hz'2 : z' <= 2
                  · simp only [hz'2, if_true]
                    norm_num at ⊢
                    omega
                  · simp only [hz'2, if_false]
                    norm_num at ⊢
                    omega
            · by_cases hm1 : p.val = -1
              · by_cases hp'm1 : d'.decimal_point.val = -1
                · have hs := hstall hm1 hp'm1
                  have hnold : ¬ 7 <= z := by
                    intro hz7
                    have hr := hrecover hm1 hz7
                    omega
                  have hpNat : (-p.val).toNat = 1 := by omega
                  simp [hm1, hp'm1, hpNat, hnold, hs]
                · have hp'0 : d'.decimal_point.val = 0 := by
                    rcases hsmall (by omega) (by omega) with h | h <;> omega
                  have hpNat : (-p.val).toNat = 1 := by omega
                  simp only [hm1, hp'0, hpNat, if_true, if_false]
                  by_cases hz'5 : 5 <= z'
                  · simp only [hz'5, if_true]
                    norm_num at ⊢
                  · simp only [hz'5, if_false]
                    by_cases hz'2 : z' <= 2
                    · simp only [hz'2, if_true]
                      norm_num at ⊢
                      by_cases hz7 : 7 <= z <;> simp [hz7]
                    · simp only [hz'2, if_false]
                      norm_num at ⊢
                      by_cases hz7 : 7 <= z <;> simp [hz7]
              · have hp0 : p.val = 0 := by omega
                have hz0 := hzeroProgress hp0
                have hz5 := hzeroInput hp0
                rw [hz0.1]
                simp only [hp0, if_true, if_false]
                by_cases hz2 : z <= 2
                · have hz'3 := hz0.2.1 hz2
                  have hz5' : ¬ 5 <= z := by omega
                  simp only [hz5', hz2, if_false, if_true]
                  by_cases hz'5 : 5 <= z'
                  · simp only [hz'5, if_true]
                    norm_num
                  · have hz'2 : ¬ z' <= 2 := by omega
                    simp only [hz'5, hz'2, if_false]
                    norm_num
                · have hz'5 := hz0.2.2 (by omega)
                  have hz5' : ¬ 5 <= z := by omega
                  simp only [hz5', hz2, hz'5, if_false, if_true]
                  norm_num
    · exact hinit
  obtain ⟨out, hcall, hout⟩ := Aeneas.Std.WP.spec_imp_exists hspec
  rcases out with ⟨i1, p1, t1, a1, e1, lr1⟩
  simp only [Post, Inv, recordOf, exponentOf, resultOf] at hout
  rcases hout with
    ⟨⟨hlr1, hp1Lo, hp1Hi, steps, he1Lo, he1Hi, hbudget1,
      hshape1, hend1⟩, hp1, hlead1⟩
  have hsteps : steps <= 21 := by
    exact hbudget1.2.2.2.2.2.2 hp1 hlead1
  let d1 : num.imp.dec2flt.decimal_seq.DecimalSeq :=
    { num_digits := i1, decimal_point := p1, truncated := t1, digits := a1 }
  rcases hshape1.2.2 with ⟨z1, zs1, hzshape1, hz1Lo, hz1Hi, hzlen1⟩
  have hleadEq1 : Lead d1 = z1 := head_eq d1 z1 zs1
    (by simpa [Digits, d1] using hzshape1) (by simpa [Digits, d1] using hzlen1)
  have hz1Five : 5 <= z1 := by
    rw [← hleadEq1]
    simpa [Lead, d1] using hlead1
  refine ⟨d1, e1, steps, ?_, hsteps, he1Hi, hp1, ?_, ?_, ?_, ?_⟩
  · simpa [d1, hlr1] using hcall
  · have : 0 < z1 := by omega
    have hlenPos : 0 < (Digits d1).length := by rw [hzshape1]; simp
    have hcountPos : 0 < d1.num_digits.val := by rw [← hzlen1]; exact hlenPos
    simpa [d1, bne_iff_ne] using hcountPos.ne'
  · simpa [Lead, d1] using hlead1
  · have hz : Lead d1 < 10 := by rw [hleadEq1]; exact hz1Hi
    simpa [Lead] using hz
  · dsimp only
    refine ⟨?_, ?_, ?_, ?_⟩
    · simpa [d1] using hshape1.1
    · simpa [d1] using hshape1.2.1
    · refine ⟨z1, zs1, ?_, hz1Five, hz1Hi, ?_⟩
      · simpa [Digits, d1] using hzshape1
      · simpa [Digits, d1] using hzlen1
    · simpa [Endpoints, Value, Digits, d1] using hend1

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (201)
More program dependencies (159)
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 127 entries
Technical checking records
Local closure summary
{
  "node": "ParserSlowLoop1",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "a8080fe08b5f915527b0a5b0b628cfa5474819a7e644a88a55cec91a6319b06a",
  "active_statement_hash": "61320ccf5d49f11dfb928e21541f4d9b45b8b45e8bf439451533fa3dc85b68aa",
  "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-192",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P804_T1_LeftLoopStep": "9c33b506a357178d"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift": "b3efbd0e6b3fe061",
    "num_imp_dec2flt_slow_parse_long_mantissa": "a3ed147b34a97b11"
  }
}
Prose assessment
{
  "label": "Sound flag superseded by recorded Lean closure",
  "assessment_status": "VerifierFail",
  "fingerprint_relation": "differs from current fingerprint",
  "assessment_fingerprint": "sound-v2:49b651b477a2cce09fcba33e07d0d93c4e5afd6acb95e202f1e523b79c104b87",
  "current_fingerprint": "sound-v2:d110e864776ddda0863771eabd5d42e4aaf8642924d1e65ac869fc3336496ebb",
  "superseded_for_theorem_closure": true
}
Review verdicts
{
  "corr_status": "Pass",
  "sound_status": "Fail",
  "substantiveness_status": "Pass"
}