P844_T1_LeftHistoryInvariant

Lean checked

Statement

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

Lemma · Bounded forward invariant for loop one

Let d_0 have count at most 768, decimal digits in [0,10), and an exact-length nonempty significant prefix whose first digit is in [1,10). Assume -324\le p_0\le0, e_0\ge0, and fixed endpoints X/2^{e_0}=V(d_0)+\eta_0, Y/2^{e_0}=V(d_0)+u_0 with 0\le\eta_0<u_0 and sticky iff \eta_0>0. Here V(d) is the significant-prefix integer times 10^{p-N}. For a loop state s=(N,p,t,A,e,r) let d(s)=(N,p,t,A). There exists a predicate I on these states containing (d_0,e_0,\mathsf{none}) and preserved by every normal continuing transition of the specialized loop-one body with sentinel (0,2047). Every state in I has r=\mathsf{none}, -324\le p\le0, e\ge-1260, the same positive exact-length decimal shape and count bound, and endpoints X/2^e=V(d(s))+\eta, Y/2^e=V(d(s))+u with 0\le\eta<u and sticky iff \eta>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.P529_T1_LeftLoopStep
import Tablet.P857_T1_LeftLoopBudgetTransition
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P844_T1_LeftHistoryInvariant]
theorem P844_T1_LeftHistoryInvariant (X Y : Real)
    (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 : Std.I32)
    (hpointLo : -324 <= d0.decimal_point.val)
    (hpointHi : d0.decimal_point.val <= 0)
    (hexpLo : 0 <= exp0.val)
    (hinput : d0.num_digits.val <= 768 /\
      (forall b, b ∈ d0.digits.val.take d0.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d0.digits.val.take d0.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d0.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 eta0 u0 : Real,
      X / (2 : Real) ^ exp0.val = V d0 + eta0 /\
      Y / (2 : Real) ^ exp0.val = V d0 + u0 /\
      0 <= eta0 /\ eta0 < u0 /\ (d0.truncated = true <-> 0 < eta0)) ->
    let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
      Std.I32 × Option num.imp.dec2flt.common.BiasedFp
    let recOf := fun (s : State) =>
      ({ num_digits := s.1, decimal_point := s.2.1,
         truncated := s.2.2.1, digits := s.2.2.2.1 } :
        num.imp.dec2flt.decimal_seq.DecimalSeq)
    exists Inv : State -> Prop,
      Inv (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none) /\
      (forall s, Inv s ->
        s.2.2.2.2.2 = none /\
        -324 <= (recOf s).decimal_point.val /\ (recOf s).decimal_point.val <= 0 /\
        -1260 <= s.2.2.2.2.1.val /\
        (recOf s).num_digits.val <= 768 /\
        (forall b, b ∈ (recOf s).digits.val.take (recOf s).num_digits.val -> b.val < 10) /\
        (exists h tail,
          ((recOf s).digits.val.take (recOf s).num_digits.val).map (fun b => b.val) = h :: tail /\
          1 <= h /\ h < 10 /\ (h :: tail).length = (recOf s).num_digits.val) /\
        (exists eta u : Real,
          X / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + eta /\
          Y / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + u /\
          0 <= eta /\ eta < u /\ ((recOf s).truncated = true <-> 0 < eta))) /\
      (forall s s', Inv s ->
        num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
          { m := 0#u64, p_biased := 2047#i32 }
          s.1 s.2.1 s.2.2.1 s.2.2.2.1 s.2.2.2.2.1 s.2.2.2.2.2 = ok (cont s') ->
        Inv s') := 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

We construct a stronger invariant. Write z for the prefix head and let j\ge0 be an existential transition count. Require all the asserted shape, point, option, and endpoint clauses, together with e_0-60j\le e\le e_0 and these seven budget implications:
\begin{array}{c|c} \text{state regime}&\text{budget}\\ p\le-19&18j-p\le324\\ -18\le p\le-2&j\le17\\ p=-1,\ z<7&j\le18\\ p=-1,\ z\ge7&j\le19\\ p=0,\ z\le2&j\le19\\ p=0,\ 3\le z<5&j\le20\\ p=0,\ z\ge5&j\le21. \end{array}
Take j=0 initially. The first budget is -p_0\le324, and every other budget has a nonnegative right-hand side. All other clauses are the hypotheses. In every invariant state these regimes exhaust -324\le p\le0. The first regime implies 18j\le324+p\le305, hence j\le16; all other regimes imply j\le21 directly. Consequently e\ge e_0-60j\ge-1260. This derives the exponent floor before invoking any step theorem, with no premise about a terminal state.
Apply LeftLoopStep to the current record and exponent, using endpoints X/2^e,Y/2^e. Its done alternative cannot equal a given normal continuation because the control-flow constructors are distinct. In its continuation alternative it supplies the actual successor record, a shift 0\le k\le60, e'=e-k, the preserved shape, option None and point interval, and all the following correlated transitions. If p\le-19, then p'=p+18 or p+19. If -18\le p<0, then p'=-1 or 0. A stall at p=p'=-1 gives z'\ge7; a head z\ge7 at p=-1 gives p'=0,z'\ge5. At p=0 a continuation has z<5,p'=0, with z\le2 implying z'\ge3 and z\ge3 implying z'\ge5. Injectivity of the normal result and continuation constructors identifies this successor with the one in the given transition.
Use j'=j+1. Apply LeftLoopBudgetTransition to the current counter, points, heads, point bounds, seven budgets, and these correlated transition clauses, omitting the unneeded shift selection equality. It gives the seven-way source-regime disjunction and every successor budget implication. Use the latter to reconstruct the stronger invariant together with the local shape and endpoint facts. The exponent inequalities persist because e'=e-k, k\le60 and k\ge0. For the endpoints, the step theorem supplies 2^k(X/2^e)=V(d')+\eta' and the corresponding Y equation, including 0\le\eta'<u' and the sticky equivalence. The identity Q/2^{e-k}=2^k(Q/2^e) follows from integer-power subtraction and 2\ne0. Thus these are exactly the required fixed-endpoint equations at exponent e'. This proves preservation of the stronger predicate and hence all properties of I in the statement.

Lean proof


  classical
  dsimp only
  intro hend0
  rcases hend0 with ⟨eta0, u0, hX0, hY0, heta0, hlt0, hsticky0⟩
  rcases hinput with ⟨hcap0, hdigit0, hshape0⟩
  rcases hshape0 with ⟨h0, tail0, hmap0, hhead0, hheadHi0, hlen0⟩
  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))
  let recOf := fun (s : Std.Usize × Std.I32 × Bool ×
      Array Std.U8 768#usize × Std.I32 ×
      Option num.imp.dec2flt.common.BiasedFp) =>
    ({ num_digits := s.1, decimal_point := s.2.1,
       truncated := s.2.2.1, digits := s.2.2.2.1 } :
      num.imp.dec2flt.decimal_seq.DecimalSeq)
  let headOf := fun (s : Std.Usize × Std.I32 × Bool ×
      Array Std.U8 768#usize × Std.I32 ×
      Option num.imp.dec2flt.common.BiasedFp) =>
    ((((recOf s).digits.val.take (recOf s).num_digits.val).map
      (fun b => b.val)).headD 0)
  let Inv := fun (s : Std.Usize × Std.I32 × Bool ×
      Array Std.U8 768#usize × Std.I32 ×
      Option num.imp.dec2flt.common.BiasedFp) =>
    ∃ j : Nat,
      s.2.2.2.2.2 = none ∧
      -324 ≤ (recOf s).decimal_point.val ∧
      (recOf s).decimal_point.val ≤ 0 ∧
      exp0.val - 60 * (j : Int) ≤ s.2.2.2.2.1.val ∧
      s.2.2.2.2.1.val ≤ exp0.val ∧
      (recOf s).num_digits.val ≤ 768 ∧
      (∀ b, b ∈ (recOf s).digits.val.take (recOf s).num_digits.val -> b.val < 10) ∧
      (∃ h tail,
        ((recOf s).digits.val.take (recOf s).num_digits.val).map
          (fun b => b.val) = h :: tail ∧ 1 ≤ h ∧ h < 10 ∧
        (h :: tail).length = (recOf s).num_digits.val) ∧
      (∃ eta u : Real,
        X / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + eta ∧
        Y / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + u ∧
        0 ≤ eta ∧ eta < u ∧
        ((recOf s).truncated = true ↔ 0 < eta)) ∧
      ((recOf s).decimal_point.val ≤ -19 -> 18 * (j : Int) -
          (recOf s).decimal_point.val ≤ 324) ∧
      (-18 ≤ (recOf s).decimal_point.val ->
          (recOf s).decimal_point.val ≤ -2 -> j ≤ 17) ∧
      ((recOf s).decimal_point.val = -1 -> headOf s < 7 -> j ≤ 18) ∧
      ((recOf s).decimal_point.val = -1 -> 7 ≤ headOf s -> j ≤ 19) ∧
      ((recOf s).decimal_point.val = 0 -> headOf s ≤ 2 -> j ≤ 19) ∧
      ((recOf s).decimal_point.val = 0 -> 3 ≤ headOf s ->
          headOf s < 5 -> j ≤ 20) ∧
      ((recOf s).decimal_point.val = 0 -> 5 ≤ headOf s -> j ≤ 21)
  let s0 := (d0.num_digits, d0.decimal_point, d0.truncated,
    d0.digits, exp0, (none : Option num.imp.dec2flt.common.BiasedFp))
  have hstart : Inv s0 := by
    simp only [Inv, recOf, headOf, s0]
    refine ⟨0, by trivial, hpointLo, hpointHi, ?_, le_rfl, hcap0,
      hdigit0, ⟨h0, tail0, hmap0, hhead0, hheadHi0, hlen0⟩,
      ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
    · omega
    · exact ⟨eta0, u0, hX0, hY0, heta0, hlt0, hsticky0⟩
    all_goals simp [hmap0] <;> intros <;> omega
  refine ⟨Inv, hstart, ?_, ?_⟩
  · intro s hs
    rcases s with ⟨i, p, b, a, e, res⟩
    dsimp only [Inv, recOf, headOf] at hs
    rcases hs with ⟨j, hnone, hpLo, hpHi, heLo, heHi, hcap,
      hdigit, hshape, hend, hb1, hb2, hb3, hb4, hb5, hb6, hb7⟩
    let d : num.imp.dec2flt.decimal_seq.DecimalSeq :=
      { num_digits := i, decimal_point := p, truncated := b, digits := a }
    rcases hshape with ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩
    have hhead :
        (((d.digits.val.take d.num_digits.val).map (fun q => q.val)).headD 0) = z := by
      dsimp [d]
      rw [hzmap]
      rfl
    have hzHead :
        (List.map (fun q : Std.U8 => q.val) (a.val.take i.val)).headD 0 = z := by
      simpa [d] using hhead
    have hjBound : j ≤ 21 := by
      by_cases hp : p.val ≤ -19
      · have := hb1 hp
        omega
      · by_cases hp18 : -18 ≤ p.val
        · by_cases hpneg : p.val ≤ -2
          · exact le_trans (hb2 hp18 hpneg) (by omega)
          · by_cases hpm : p.val = -1
            · by_cases hz : z < 7
              · exact le_trans (hb3 hpm (by rw [hzHead]; exact hz)) (by omega)
              · have h := hb4 hpm (by rw [hzHead]; omega)
                omega
            · have hp0 : p.val = 0 := by omega
              by_cases hz2 : z ≤ 2
              · exact le_trans (hb5 hp0 (by rw [hzHead]; exact hz2)) (by omega)
              · by_cases hz5 : 5 ≤ z
                · exact le_trans (hb7 hp0 (by rw [hzHead]; exact hz5)) (by omega)
                · have hlt5 : z < 5 := by omega
                  have hz3 : 3 ≤ z := by omega
                  have h := hb6 hp0 (by rw [hzHead]; exact hz3)
                    (by rw [hzHead]; exact hlt5)
                  omega
        · omega
    have hexpMin : -1260 ≤ e.val := by
      have hbound : (j : Int) ≤ 21 := by exact_mod_cast hjBound
      omega

    exact ⟨hnone, hpLo, hpHi, hexpMin, hcap, hdigit,
      ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩, hend⟩
  · intro s s' hs hcall
    rcases s with ⟨i, p, b, a, e, res⟩
    dsimp only [Inv, recOf, headOf] at hs
    rcases hs with ⟨j, hnone, hpLo, hpHi, heLo, heHi, hcap,
      hdigit, hshape, hend, hb1, hb2, hb3, hb4, hb5, hb6, hb7⟩
    let d : num.imp.dec2flt.decimal_seq.DecimalSeq :=
      { num_digits := i, decimal_point := p, truncated := b, digits := a }
    rcases hshape with ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩
    have hnumPos : 0 < i.val := by
      have hpos : 0 < (z :: zs).length := by simp
      rw [← hzlen]
      exact hpos
    have htakeBound : i.val ≤ a.val.length := by
      have haLen : a.val.length = 768 := by simpa using a.property
      have hi : i.val ≤ 768 := by exact_mod_cast hcap
      rw [haLen]
      exact hi
    have hhead :
        (((d.digits.val.take d.num_digits.val).map (fun q => q.val)).headD 0) = z := by
      dsimp [d]
      rw [hzmap]
      rfl
    have hzHead :
        (List.map (fun q : Std.U8 => q.val) (a.val.take i.val)).headD 0 = z := by
      simpa [d] using hhead
    have hzHead' :
        (List.take i.val (List.map (fun q : Std.U8 => q.val) a.val)).headD 0 = z := by
      simpa [List.map_take] using hzHead
    have hstateHead : headOf (i, p, b, a, e, none) = z := by
      dsimp [headOf, recOf]
      rw [hzmap]
      rfl
    have hheadVal : a.val[0].val = z := by
      have hread : (a.val[0]?).map (fun q => q.val) = some z := by
        have hmap := congrArg (fun l : List Nat => l[0]?) hzmap
        simp only [List.getElem?_cons_zero, List.getElem?_map] at hmap
        rw [List.getElem?_take_of_lt hnumPos] at hmap
        exact hmap
      have hidx : 0 < a.val.length := by
        exact lt_of_lt_of_le (by omega : 0 < i.val) htakeBound
      have := hread
      rw [List.getElem?_eq_getElem hidx, Option.map_some] at this
      exact Option.some.inj this
    have hjBound : j ≤ 21 := by
      by_cases hp : p.val ≤ -19
      · have := hb1 hp
        omega
      · by_cases hp18 : -18 ≤ p.val
        · by_cases hpneg : p.val ≤ -2
          · exact le_trans (hb2 hp18 hpneg) (by omega)
          · by_cases hpm : p.val = -1
            · by_cases hz : z < 7
              · exact le_trans (hb3 hpm (by rw [hzHead]; exact hz)) (by omega)
              · have h := hb4 hpm (by rw [hzHead]; omega)
                omega
            · have hp0 : p.val = 0 := by omega
              by_cases hz2 : z ≤ 2
              · exact le_trans (hb5 hp0 (by rw [hzHead]; exact hz2)) (by omega)
              · by_cases hz5 : 5 ≤ z
                · exact le_trans (hb7 hp0 (by rw [hzHead]; exact hz5)) (by omega)
                · have hlt5 : z < 5 := by omega
                  have hz3 : 3 ≤ z := by omega
                  have h := hb6 hp0 (by rw [hzHead]; exact hz3)
                    (by rw [hzHead]; exact hlt5)
                  omega
        · omega
    have hexpMin : -1260 ≤ e.val := by
      have hbound : (j : Int) ≤ 21 := by exact_mod_cast hjBound
      omega
    have hdinput : d.num_digits.val ≤ 768 /\
        (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
        (exists h tail,
          (d.digits.val.take d.num_digits.val).map (fun q => q.val) = h :: tail /\
          1 ≤ h /\ h < 10 /\ (h :: tail).length = d.num_digits.val) := by
      exact ⟨hcap, hdigit, ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩⟩
    have hprincipal := P529_T1_LeftLoopStep
      (X / (2 : Real) ^ e.val) (Y / (2 : Real) ^ e.val) d e
      hpLo hpHi hexpMin hdinput hend

    subst res
    rcases hprincipal with hdone | hcont
    · rcases hdone with ⟨hb, _, _⟩
      have heq := hb.symm.trans hcall
      simp only [d] at heq
      cases heq
    · rcases hcont with ⟨d', shift, growth, e', z, z', zs, zs', hbodyEq,
        hleft, hgrowth, hshift, hgrowthBound, heq, hpEq, hshape, hshape',
        hzlo, hzhi, hz'lo, hz'hi, hsel, hlarge, hsmall, hminus, hminusLarge,
        hzero, hpLo', hpHi', hcap', hdigit', hlen', hlast', hend'⟩
      rcases hend' with ⟨eta', u', hXscaled, hYscaled, heta', hlt', hsticky'⟩
      have hscale (Q : Real) : Q / (2 : Real) ^ e'.val =
          (2 : Real) ^ shift.val * (Q / (2 : Real) ^ e.val) := by
        rw [heq, zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
        rw [div_div_eq_mul_div]
        simp only [div_eq_mul_inv]
        ac_rfl
      have hXnew : X / (2 : Real) ^ e'.val = V d' + eta' := by
        rw [hscale]
        exact hXscaled
      have hYnew : Y / (2 : Real) ^ e'.val = V d' + u' := by
        rw [hscale]
        exact hYscaled
      have hhead' :
          (List.take d'.num_digits.val
            (List.map (fun q : Std.U8 => q.val) d'.digits.val)).headD 0 = z' := by
        simpa [List.map_take] using congrArg
          (fun l : List Nat => l.headD 0) hshape'
      have hheadState' : headOf
          (d'.num_digits, d'.decimal_point, d'.truncated, d'.digits, e', none) = z' := by
        dsimp [headOf, recOf]
        rw [hshape']
        rfl
      have hstateHead' : headOf
          (d'.num_digits, d'.decimal_point, d'.truncated, d'.digits, e', none) = z' :=
        hheadState'
      have hstateHeadCurrent : headOf (i, p, b, a, e, none) = z := by
        dsimp [headOf, recOf]
        rw [hshape]
        rfl
      have hzHeadCurrent :
          (List.map (fun q : Std.U8 => q.val) (a.val.take i.val)).headD 0 = z := by
        rw [hshape]
        rfl
      obtain ⟨hsource, hbudgets'⟩ :=
        P857_T1_LeftLoopBudgetTransition j p.val d'.decimal_point.val z z'
          hpLo hpHi hb1 hb2
          (by simpa only [hzHeadCurrent] using hb3)
          (by simpa only [hzHeadCurrent] using hb4)
          (by simpa only [hzHeadCurrent] using hb5)
          (by simpa only [hzHeadCurrent] using hb6)
          (by simpa only [hzHeadCurrent] using hb7)
          hlarge hsmall hminus hminusLarge
          (fun hp => ⟨(hzero hp).1, (hzero hp).2.2⟩)
      have hInv' : Inv (d'.num_digits, d'.decimal_point,
          d'.truncated, d'.digits, e', none) := by
        dsimp only [Inv, recOf]
        rcases hbudgets' with ⟨hb1', hb2', hb3', hb4', hb5', hb6', hb7'⟩
        refine ⟨j + 1, rfl, hpLo', hpHi', ?_, ?_, hcap', hdigit',
          ⟨z', zs', hshape', hz'lo, hz'hi, hlen'⟩,
          ⟨eta', u', ?_, ?_, heta', hlt', hsticky'⟩,
          (by simpa [hhead'] using hb1'),
          (by simpa [hhead'] using hb2'),
          (by simpa [hstateHead'] using hb3'),
          (by simpa [hstateHead'] using hb4'),
          (by simpa [hstateHead'] using hb5'),
          (by simpa [hstateHead'] using hb6'),
          (by simpa [hstateHead'] using hb7')⟩
        · rw [heq]
          have hk : (shift.val : Int) ≤ 60 := by exact_mod_cast hshift
          omega
        · rw [heq]
          have hk : 0 ≤ (shift.val : Int) := by omega
          omega
        · simpa [V] using hXnew
        · simpa [V] using hYnew

      have heq := Result.ok.inj (hbodyEq.symm.trans hcall)
      cases heq
      exact hInv'

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P529_T1_LeftLoopStep
import Tablet.P857_T1_LeftLoopBudgetTransition
open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
namespace dec2flt_model

-- [TABLET NODE: P844_T1_LeftHistoryInvariant]
theorem P844_T1_LeftHistoryInvariant (X Y : Real)
    (d0 : num.imp.dec2flt.decimal_seq.DecimalSeq) (exp0 : Std.I32)
    (hpointLo : -324 <= d0.decimal_point.val)
    (hpointHi : d0.decimal_point.val <= 0)
    (hexpLo : 0 <= exp0.val)
    (hinput : d0.num_digits.val <= 768 /\
      (forall b, b ∈ d0.digits.val.take d0.num_digits.val -> b.val < 10) /\
      (exists h tail,
        (d0.digits.val.take d0.num_digits.val).map (fun b => b.val) = h :: tail /\
        1 <= h /\ h < 10 /\ (h :: tail).length = d0.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 eta0 u0 : Real,
      X / (2 : Real) ^ exp0.val = V d0 + eta0 /\
      Y / (2 : Real) ^ exp0.val = V d0 + u0 /\
      0 <= eta0 /\ eta0 < u0 /\ (d0.truncated = true <-> 0 < eta0)) ->
    let State := Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
      Std.I32 × Option num.imp.dec2flt.common.BiasedFp
    let recOf := fun (s : State) =>
      ({ num_digits := s.1, decimal_point := s.2.1,
         truncated := s.2.2.1, digits := s.2.2.2.1 } :
        num.imp.dec2flt.decimal_seq.DecimalSeq)
    exists Inv : State -> Prop,
      Inv (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, exp0, none) /\
      (forall s, Inv s ->
        s.2.2.2.2.2 = none /\
        -324 <= (recOf s).decimal_point.val /\ (recOf s).decimal_point.val <= 0 /\
        -1260 <= s.2.2.2.2.1.val /\
        (recOf s).num_digits.val <= 768 /\
        (forall b, b ∈ (recOf s).digits.val.take (recOf s).num_digits.val -> b.val < 10) /\
        (exists h tail,
          ((recOf s).digits.val.take (recOf s).num_digits.val).map (fun b => b.val) = h :: tail /\
          1 <= h /\ h < 10 /\ (h :: tail).length = (recOf s).num_digits.val) /\
        (exists eta u : Real,
          X / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + eta /\
          Y / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + u /\
          0 <= eta /\ eta < u /\ ((recOf s).truncated = true <-> 0 < eta))) /\
      (forall s s', Inv s ->
        num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
          F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
          { m := 0#u64, p_biased := 2047#i32 }
          s.1 s.2.1 s.2.2.1 s.2.2.2.1 s.2.2.2.2.1 s.2.2.2.2.2 = ok (cont s') ->
        Inv s') := by
-- BODY
  classical
  dsimp only
  intro hend0
  rcases hend0 with ⟨eta0, u0, hX0, hY0, heta0, hlt0, hsticky0⟩
  rcases hinput with ⟨hcap0, hdigit0, hshape0⟩
  rcases hshape0 with ⟨h0, tail0, hmap0, hhead0, hheadHi0, hlen0⟩
  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))
  let recOf := fun (s : Std.Usize × Std.I32 × Bool ×
      Array Std.U8 768#usize × Std.I32 ×
      Option num.imp.dec2flt.common.BiasedFp) =>
    ({ num_digits := s.1, decimal_point := s.2.1,
       truncated := s.2.2.1, digits := s.2.2.2.1 } :
      num.imp.dec2flt.decimal_seq.DecimalSeq)
  let headOf := fun (s : Std.Usize × Std.I32 × Bool ×
      Array Std.U8 768#usize × Std.I32 ×
      Option num.imp.dec2flt.common.BiasedFp) =>
    ((((recOf s).digits.val.take (recOf s).num_digits.val).map
      (fun b => b.val)).headD 0)
  let Inv := fun (s : Std.Usize × Std.I32 × Bool ×
      Array Std.U8 768#usize × Std.I32 ×
      Option num.imp.dec2flt.common.BiasedFp) =>
    ∃ j : Nat,
      s.2.2.2.2.2 = none ∧
      -324 ≤ (recOf s).decimal_point.val ∧
      (recOf s).decimal_point.val ≤ 0 ∧
      exp0.val - 60 * (j : Int) ≤ s.2.2.2.2.1.val ∧
      s.2.2.2.2.1.val ≤ exp0.val ∧
      (recOf s).num_digits.val ≤ 768 ∧
      (∀ b, b ∈ (recOf s).digits.val.take (recOf s).num_digits.val -> b.val < 10) ∧
      (∃ h tail,
        ((recOf s).digits.val.take (recOf s).num_digits.val).map
          (fun b => b.val) = h :: tail ∧ 1 ≤ h ∧ h < 10 ∧
        (h :: tail).length = (recOf s).num_digits.val) ∧
      (∃ eta u : Real,
        X / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + eta ∧
        Y / (2 : Real) ^ s.2.2.2.2.1.val = V (recOf s) + u ∧
        0 ≤ eta ∧ eta < u ∧
        ((recOf s).truncated = true ↔ 0 < eta)) ∧
      ((recOf s).decimal_point.val ≤ -19 -> 18 * (j : Int) -
          (recOf s).decimal_point.val ≤ 324) ∧
      (-18 ≤ (recOf s).decimal_point.val ->
          (recOf s).decimal_point.val ≤ -2 -> j ≤ 17) ∧
      ((recOf s).decimal_point.val = -1 -> headOf s < 7 -> j ≤ 18) ∧
      ((recOf s).decimal_point.val = -1 -> 7 ≤ headOf s -> j ≤ 19) ∧
      ((recOf s).decimal_point.val = 0 -> headOf s ≤ 2 -> j ≤ 19) ∧
      ((recOf s).decimal_point.val = 0 -> 3 ≤ headOf s ->
          headOf s < 5 -> j ≤ 20) ∧
      ((recOf s).decimal_point.val = 0 -> 5 ≤ headOf s -> j ≤ 21)
  let s0 := (d0.num_digits, d0.decimal_point, d0.truncated,
    d0.digits, exp0, (none : Option num.imp.dec2flt.common.BiasedFp))
  have hstart : Inv s0 := by
    simp only [Inv, recOf, headOf, s0]
    refine ⟨0, by trivial, hpointLo, hpointHi, ?_, le_rfl, hcap0,
      hdigit0, ⟨h0, tail0, hmap0, hhead0, hheadHi0, hlen0⟩,
      ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩
    · omega
    · exact ⟨eta0, u0, hX0, hY0, heta0, hlt0, hsticky0⟩
    all_goals simp [hmap0] <;> intros <;> omega
  refine ⟨Inv, hstart, ?_, ?_⟩
  · intro s hs
    rcases s with ⟨i, p, b, a, e, res⟩
    dsimp only [Inv, recOf, headOf] at hs
    rcases hs with ⟨j, hnone, hpLo, hpHi, heLo, heHi, hcap,
      hdigit, hshape, hend, hb1, hb2, hb3, hb4, hb5, hb6, hb7⟩
    let d : num.imp.dec2flt.decimal_seq.DecimalSeq :=
      { num_digits := i, decimal_point := p, truncated := b, digits := a }
    rcases hshape with ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩
    have hhead :
        (((d.digits.val.take d.num_digits.val).map (fun q => q.val)).headD 0) = z := by
      dsimp [d]
      rw [hzmap]
      rfl
    have hzHead :
        (List.map (fun q : Std.U8 => q.val) (a.val.take i.val)).headD 0 = z := by
      simpa [d] using hhead
    have hjBound : j ≤ 21 := by
      by_cases hp : p.val ≤ -19
      · have := hb1 hp
        omega
      · by_cases hp18 : -18 ≤ p.val
        · by_cases hpneg : p.val ≤ -2
          · exact le_trans (hb2 hp18 hpneg) (by omega)
          · by_cases hpm : p.val = -1
            · by_cases hz : z < 7
              · exact le_trans (hb3 hpm (by rw [hzHead]; exact hz)) (by omega)
              · have h := hb4 hpm (by rw [hzHead]; omega)
                omega
            · have hp0 : p.val = 0 := by omega
              by_cases hz2 : z ≤ 2
              · exact le_trans (hb5 hp0 (by rw [hzHead]; exact hz2)) (by omega)
              · by_cases hz5 : 5 ≤ z
                · exact le_trans (hb7 hp0 (by rw [hzHead]; exact hz5)) (by omega)
                · have hlt5 : z < 5 := by omega
                  have hz3 : 3 ≤ z := by omega
                  have h := hb6 hp0 (by rw [hzHead]; exact hz3)
                    (by rw [hzHead]; exact hlt5)
                  omega
        · omega
    have hexpMin : -1260 ≤ e.val := by
      have hbound : (j : Int) ≤ 21 := by exact_mod_cast hjBound
      omega

    exact ⟨hnone, hpLo, hpHi, hexpMin, hcap, hdigit,
      ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩, hend⟩
  · intro s s' hs hcall
    rcases s with ⟨i, p, b, a, e, res⟩
    dsimp only [Inv, recOf, headOf] at hs
    rcases hs with ⟨j, hnone, hpLo, hpHi, heLo, heHi, hcap,
      hdigit, hshape, hend, hb1, hb2, hb3, hb4, hb5, hb6, hb7⟩
    let d : num.imp.dec2flt.decimal_seq.DecimalSeq :=
      { num_digits := i, decimal_point := p, truncated := b, digits := a }
    rcases hshape with ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩
    have hnumPos : 0 < i.val := by
      have hpos : 0 < (z :: zs).length := by simp
      rw [← hzlen]
      exact hpos
    have htakeBound : i.val ≤ a.val.length := by
      have haLen : a.val.length = 768 := by simpa using a.property
      have hi : i.val ≤ 768 := by exact_mod_cast hcap
      rw [haLen]
      exact hi
    have hhead :
        (((d.digits.val.take d.num_digits.val).map (fun q => q.val)).headD 0) = z := by
      dsimp [d]
      rw [hzmap]
      rfl
    have hzHead :
        (List.map (fun q : Std.U8 => q.val) (a.val.take i.val)).headD 0 = z := by
      simpa [d] using hhead
    have hzHead' :
        (List.take i.val (List.map (fun q : Std.U8 => q.val) a.val)).headD 0 = z := by
      simpa [List.map_take] using hzHead
    have hstateHead : headOf (i, p, b, a, e, none) = z := by
      dsimp [headOf, recOf]
      rw [hzmap]
      rfl
    have hheadVal : a.val[0].val = z := by
      have hread : (a.val[0]?).map (fun q => q.val) = some z := by
        have hmap := congrArg (fun l : List Nat => l[0]?) hzmap
        simp only [List.getElem?_cons_zero, List.getElem?_map] at hmap
        rw [List.getElem?_take_of_lt hnumPos] at hmap
        exact hmap
      have hidx : 0 < a.val.length := by
        exact lt_of_lt_of_le (by omega : 0 < i.val) htakeBound
      have := hread
      rw [List.getElem?_eq_getElem hidx, Option.map_some] at this
      exact Option.some.inj this
    have hjBound : j ≤ 21 := by
      by_cases hp : p.val ≤ -19
      · have := hb1 hp
        omega
      · by_cases hp18 : -18 ≤ p.val
        · by_cases hpneg : p.val ≤ -2
          · exact le_trans (hb2 hp18 hpneg) (by omega)
          · by_cases hpm : p.val = -1
            · by_cases hz : z < 7
              · exact le_trans (hb3 hpm (by rw [hzHead]; exact hz)) (by omega)
              · have h := hb4 hpm (by rw [hzHead]; omega)
                omega
            · have hp0 : p.val = 0 := by omega
              by_cases hz2 : z ≤ 2
              · exact le_trans (hb5 hp0 (by rw [hzHead]; exact hz2)) (by omega)
              · by_cases hz5 : 5 ≤ z
                · exact le_trans (hb7 hp0 (by rw [hzHead]; exact hz5)) (by omega)
                · have hlt5 : z < 5 := by omega
                  have hz3 : 3 ≤ z := by omega
                  have h := hb6 hp0 (by rw [hzHead]; exact hz3)
                    (by rw [hzHead]; exact hlt5)
                  omega
        · omega
    have hexpMin : -1260 ≤ e.val := by
      have hbound : (j : Int) ≤ 21 := by exact_mod_cast hjBound
      omega
    have hdinput : d.num_digits.val ≤ 768 /\
        (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
        (exists h tail,
          (d.digits.val.take d.num_digits.val).map (fun q => q.val) = h :: tail /\
          1 ≤ h /\ h < 10 /\ (h :: tail).length = d.num_digits.val) := by
      exact ⟨hcap, hdigit, ⟨z, zs, hzmap, hzlo, hzhi, hzlen⟩⟩
    have hprincipal := P529_T1_LeftLoopStep
      (X / (2 : Real) ^ e.val) (Y / (2 : Real) ^ e.val) d e
      hpLo hpHi hexpMin hdinput hend

    subst res
    rcases hprincipal with hdone | hcont
    · rcases hdone with ⟨hb, _, _⟩
      have heq := hb.symm.trans hcall
      simp only [d] at heq
      cases heq
    · rcases hcont with ⟨d', shift, growth, e', z, z', zs, zs', hbodyEq,
        hleft, hgrowth, hshift, hgrowthBound, heq, hpEq, hshape, hshape',
        hzlo, hzhi, hz'lo, hz'hi, hsel, hlarge, hsmall, hminus, hminusLarge,
        hzero, hpLo', hpHi', hcap', hdigit', hlen', hlast', hend'⟩
      rcases hend' with ⟨eta', u', hXscaled, hYscaled, heta', hlt', hsticky'⟩
      have hscale (Q : Real) : Q / (2 : Real) ^ e'.val =
          (2 : Real) ^ shift.val * (Q / (2 : Real) ^ e.val) := by
        rw [heq, zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
        rw [div_div_eq_mul_div]
        simp only [div_eq_mul_inv]
        ac_rfl
      have hXnew : X / (2 : Real) ^ e'.val = V d' + eta' := by
        rw [hscale]
        exact hXscaled
      have hYnew : Y / (2 : Real) ^ e'.val = V d' + u' := by
        rw [hscale]
        exact hYscaled
      have hhead' :
          (List.take d'.num_digits.val
            (List.map (fun q : Std.U8 => q.val) d'.digits.val)).headD 0 = z' := by
        simpa [List.map_take] using congrArg
          (fun l : List Nat => l.headD 0) hshape'
      have hheadState' : headOf
          (d'.num_digits, d'.decimal_point, d'.truncated, d'.digits, e', none) = z' := by
        dsimp [headOf, recOf]
        rw [hshape']
        rfl
      have hstateHead' : headOf
          (d'.num_digits, d'.decimal_point, d'.truncated, d'.digits, e', none) = z' :=
        hheadState'
      have hstateHeadCurrent : headOf (i, p, b, a, e, none) = z := by
        dsimp [headOf, recOf]
        rw [hshape]
        rfl
      have hzHeadCurrent :
          (List.map (fun q : Std.U8 => q.val) (a.val.take i.val)).headD 0 = z := by
        rw [hshape]
        rfl
      obtain ⟨hsource, hbudgets'⟩ :=
        P857_T1_LeftLoopBudgetTransition j p.val d'.decimal_point.val z z'
          hpLo hpHi hb1 hb2
          (by simpa only [hzHeadCurrent] using hb3)
          (by simpa only [hzHeadCurrent] using hb4)
          (by simpa only [hzHeadCurrent] using hb5)
          (by simpa only [hzHeadCurrent] using hb6)
          (by simpa only [hzHeadCurrent] using hb7)
          hlarge hsmall hminus hminusLarge
          (fun hp => ⟨(hzero hp).1, (hzero hp).2.2⟩)
      have hInv' : Inv (d'.num_digits, d'.decimal_point,
          d'.truncated, d'.digits, e', none) := by
        dsimp only [Inv, recOf]
        rcases hbudgets' with ⟨hb1', hb2', hb3', hb4', hb5', hb6', hb7'⟩
        refine ⟨j + 1, rfl, hpLo', hpHi', ?_, ?_, hcap', hdigit',
          ⟨z', zs', hshape', hz'lo, hz'hi, hlen'⟩,
          ⟨eta', u', ?_, ?_, heta', hlt', hsticky'⟩,
          (by simpa [hhead'] using hb1'),
          (by simpa [hhead'] using hb2'),
          (by simpa [hstateHead'] using hb3'),
          (by simpa [hstateHead'] using hb4'),
          (by simpa [hstateHead'] using hb5'),
          (by simpa [hstateHead'] using hb6'),
          (by simpa [hstateHead'] using hb7')⟩
        · rw [heq]
          have hk : (shift.val : Int) ≤ 60 := by exact_mod_cast hshift
          omega
        · rw [heq]
          have hk : 0 ≤ (shift.val : Int) := by omega
          omega
        · simpa [V] using hXnew
        · simpa [V] using hYnew

      have heq := Result.ok.inj (hbodyEq.symm.trans hcall)
      cases heq
      exact hInv'

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 129 entries
Technical checking records
Local closure summary
{
  "node": "P844_T1_LeftHistoryInvariant",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "2b1d1332268b3257031969e680518d4431e151634a80063a8319e77ec8efb03b",
  "active_statement_hash": "5fc2e7129e08775f8f5023a79cbd679ee200f123605d621ce879d03ecfec84c0",
  "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-200",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P529_T1_LeftLoopStep": "24fd9fefa4fce9ac",
    "P857_T1_LeftLoopBudgetTransition": "9f695f89ae209a63"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "num_imp_dec2flt_common_BiasedFp": "c43614ff2149ecf2",
    "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": "62216a51d634059e",
    "num_imp_dec2flt_slow_parse_long_mantissa_closure_Insts_CoreOpsFunctionFnTupleUsizeUsize_call": "7df8a2c8e3717790"
  }
}
Prose assessment
{
  "label": "Lean proof closed; predictive prose review no longer required",
  "assessment_status": null,
  "fingerprint_relation": "unknown",
  "assessment_fingerprint": null,
  "current_fingerprint": "sound-v2:58d989563677fa41ed53fa029e6842f64f27277970379ebf6ef923fe0a33dbb5",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}