P826_T1_LeftHistoryBoundary

Lean checked

Statement

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

Helper · Boundary order through a reachable loop-1 history

For a decimal record d, write d.p for d.\mathsf{decimal\_point}.\mathsf{val} and
D(d)=\operatorname{map}(q\mapsto q.\mathsf{val}, \operatorname{take}(d.\mathsf{num\_digits}.\mathsf{val}, d.\mathsf{digits}.\mathsf{val})), \quad I(d)=\operatorname{foldl}((n,q)\mapsto10n+q,0,D(d)), \quad V(d)=I(d)10^{d.p-d.\mathsf{num\_digits}.\mathsf{val}}.
Let A,B\in\mathbb R, let d_0,d_1 be decimal records, and let e_0,e_1 be signed machine exponents; in formulas, each exponent denotes its signed integer value. Assume -324\leq d_0.p\leq0 and
d_0.\mathsf{num\_digits}.\mathsf{val}\leq768, \qquad \forall q\in\operatorname{take}(d_0.\mathsf{num\_digits}.\mathsf{val}, d_0.\mathsf{digits}.\mathsf{val}),\quad q.\mathsf{val}<10,
and assume that there are z\in\mathbb N and a list z_s such that
D(d_0)=z::z_s,qquad 1\leq z<10,qquad |D(d_0)|=d_0.\mathsf{num\_digits}.\mathsf{val}. \tag{S}
Assume also that there are \eta,u\in\mathbb R satisfying
A/2^{e_0}=V(d_0)+\eta,qquad B/2^{e_0}=V(d_0)+u,qquad 0\leq\eta<u,qquad (d_0.\mathsf{truncated}=\mathsf{true}\Longleftrightarrow0<\eta). \tag{E}
If the exact extracted loop-1 call, with the maximal biased-float sentinel and no early result, from (d_0,e_0,\mathsf{none}) returns (d_1,e_1,\mathsf{none}), then
0\leq e_1-e_0+768-d_0.p, \tag{1}
and
2^{e_1-e_0}\leq V(d_0)\quad\Longrightarrow\quad1\leq V(d_1). \tag{2}

Lean statement

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

import Tablet.P826_T1_ResultLoopBackward
import Tablet.P826_T1_LeftDoneCertificate
import Tablet.P826_T1_LeftStepCertificate
import Tablet.P826_T1_LeftCapacityStep
import Tablet.P826_T1_PowerGridAlignment
import Tablet.P798_T1_LeftBoundaryTransport
import Tablet.P804_T1_DecimalValueBounds
import Mathlib.Tactic.FieldSimp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P826_T1_LeftHistoryBoundary]
theorem P826_T1_LeftHistoryBoundary
    (A B : Real)
    (d0 d1 : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (e0 e1 : Std.I32)
    (hpLo : -324 <= d0.decimal_point.val)
    (hpHi : d0.decimal_point.val <= 0)
    (hshape0 :
      let digits :=
        (d0.digits.val.take d0.num_digits.val).map (fun q => q.val)
      d0.num_digits.val <= 768 /\
      (forall q, q ∈ d0.digits.val.take d0.num_digits.val -> q.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d0.num_digits.val))
    (hend0 :
      let digits :=
        (d0.digits.val.take d0.num_digits.val).map (fun q => q.val)
      let I : Nat := digits.foldl (fun n q => 10 * n + q) 0
      let V : Real := I * (10 : Real) ^
        (d0.decimal_point.val - (d0.num_digits.val : Int))
      exists eta u : Real,
        A / (2 : Real) ^ e0.val = V + eta /\
        B / (2 : Real) ^ e0.val = V + u /\
        0 <= eta /\ eta < u /\ (d0.truncated = true <-> 0 < eta))
    (hloop :
      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 e0 none =
          ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits,
            e1, none)) :
    let Digits := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      (d.digits.val.take d.num_digits.val).map (fun q => q.val)
    let Value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((Digits d).foldl (fun n q => 10 * n + q) 0 : Nat) *
        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int))
    0 <= e1.val - e0.val + 768 - d0.decimal_point.val /\
      ((2 : Real) ^ (e1.val - e0.val) <= Value d0 ->
        1 <= Value d1) := 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

Apply backward induction to the exact extracted result loop by ResultLoopBackward. At a reachable state (d,e) and final state (d_f,e_f), put T=2^{e_f-e} and C=e_f-e+768-d.p. For leading digit z use the bonus H(d.p,z) from LeftCapacityStep. The simultaneous backward invariant is
7(d.p+324)+H(d.p,z)\leq3C, \tag{3}
together with T\leq V(d)\Rightarrow1\leq V(d_f). It also records that a final result of \mathsf{none} forces the current result to be \mathsf{none}; unfolding the extracted body proves this because a stored result exits unchanged.
For a done body, LeftDoneCertificate says that the returned record and exponent are unchanged, the point is zero, and the leading digit is at least five. Thus H=36, and both sides of (3) are 7\cdot324+36=3\cdot768=2304. Here T=2^0=1, so the order implication is the assumed inequality itself.
For a continuing body, use LeftStepCertificate with the current scaled endpoints. It exposes the exact successful left shift k, the update e'=e-k, the complete point/leading-digit transition clauses, the output digit shape, and the scaled endpoint equations. Those clauses keep -324\leq d'.p\leq0. Multiplication by 2^k and e'=e-k turn the output endpoint equations into the required equations at e'. Applying the induction hypothesis at (d',e') gives (3) there. The one-step arithmetic theorem LeftCapacityStep states
H(d.p,z)+3k\leq H(d'.p,z')+10(d'.p-d.p).
Substitution of e'=e-k combines this inequality with output (3) and gives input (3) exactly. Since d'.p\geq-324 and its bonus is nonnegative, output (3) implies C'=e_f-e'+768-d'.p\geq0.
It remains to pull order through the step. By PowerGridAlignment, C'\geq0 and d'.p\leq768 give a natural b such that
2^{e_f-e'}=b10^{d'.p-768}. \tag{4}
The decimal fold lower bound in DecimalValueBounds and the positive leading digit give V(d)>0. The exponent update gives
2^{e_f-e'}=2^k2^{e_f-e}.
Hence 2^{e_f-e}\leq V(d) says that the boundary in (4) is at most 2^kV(d). Apply LeftBoundaryTransport to the exact left shift. Its proof uses the aligned output grid GridAlignment and strict boundary retention GridBoundaryRetained, so it yields 2^{e_f-e'}\leq V(d') without accumulating a truncation error. The induction hypothesis now yields 1\leq V(d_f).
At the initial state, (3), nonnegativity of H, and d_0.p\geq-324 imply (1); its order component is (2). Every step used is the one exposed by the supplied exact loop equality, so the conclusion is specific to the reachable history.

Lean proof


  dsimp only at hshape0 hend0 ⊢
  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 d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    (d.digits.val.take d.num_digits.val).map (fun q => q.val)
  let Value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    ((Digits d).foldl (fun n q => 10 * n + q) 0 : Nat) *
      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int))
  let Shape := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    d.num_digits.val <= 768 /\
    (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
    (exists z zs, Digits d = z :: zs /\ 1 <= z /\ z < 10 /\
      (Digits d).length = d.num_digits.val)
  let Endpoints := fun (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (e : Std.I32) =>
    exists eta u : Real,
      A / (2 : Real) ^ e.val = Value d + eta /\
      B / (2 : Real) ^ e.val = Value d + u /\
      0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)
  let Lead := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    d.digits.val[0].val
  let Bonus : Int -> Nat -> Int := fun (p : Int) (z : Nat) =>
    if p = -1 then (if 7 <= z then 36 else 27)
    else if p = 0 then
      (if 5 <= z then 36 else if 3 <= z then 33 else 27)
    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
  let Rel := fun (x y : State) =>
    resultOf y = none ->
    resultOf x = none /\
    (-324 <= (recordOf x).decimal_point.val ->
      (recordOf x).decimal_point.val <= 0 ->
      Shape (recordOf x) -> Endpoints (recordOf x) (exponentOf x) ->
      7 * ((recordOf x).decimal_point.val + 324) +
          Bonus (recordOf x).decimal_point.val (Lead (recordOf x)) <=
        3 * ((exponentOf y).val - (exponentOf x).val + 768 -
          (recordOf x).decimal_point.val) /\
      ((2 : Real) ^ ((exponentOf y).val - (exponentOf x).val) <=
          Value (recordOf x) ->
        1 <= Value (recordOf y)))
  have head_eq (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (z : Nat) (zs : List Nat)
      (hshape : Digits d = z :: zs)
      (hlen : (Digits d).length = d.num_digits.val) : Lead d = z := by
    simp only [Digits] at hshape hlen
    have hnumPos : 0 < d.num_digits.val := by
      rw [← hlen, hshape]
      simp
    have hindexBound : (0 : Nat) < d.digits.val.length := by
      rw [d.digits.property]
      norm_num
    have hget : d.digits.val[0]? = some d.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 : (d.digits.val.take d.num_digits.val)[0]? =
        some d.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 hrel : Rel
      (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, e0, none)
      (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits, e1, none) := by
    apply P826_T1_ResultLoopBackward Rel Body
    · intro x y hbody
      rcases x with ⟨i, p, t, a, e, lr⟩
      rcases y with ⟨iy, py, ty, ay, ey, lry⟩
      simp only [Rel, resultOf, recordOf, exponentOf]
      intro hy
      rw [hy] at hbody
      have hx : lr = none := by
        cases lr with
        | none => rfl
        | some v =>
          simp only [Body, Prod.fst, Prod.snd] at hbody
          unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body at hbody
          by_cases hp : p <= 0#i32 <;> simp [hp] at hbody
      subst lr
      refine ⟨rfl, ?_⟩
      intro hpLoX hpHiX hshapeX hendX
      have hdone := P826_T1_LeftDoneCertificate
        { num_digits := i, decimal_point := p, truncated := t, digits := a }
        { num_digits := iy, decimal_point := py, truncated := ty, digits := ay }
        e ey hpHiX (by simpa [Body] using hbody)
      rcases hdone with ⟨hdy, hey, hp0, hlead5⟩
      have hlead5' : 5 <= Lead
          { num_digits := i, decimal_point := p, truncated := t,
            digits := a } := by
        simpa [Lead] using hlead5
      subst ey
      constructor
      · simp only [Bonus]
        simp [hp0, hlead5']
      · intro horder
        rw [hdy]
        simpa using horder
    · intro x x' y hbody ih
      rcases x with ⟨i, p, t, a, e, lr⟩
      rcases x' with ⟨i', p', t', a', e', lr'⟩
      rcases y with ⟨iy, py, ty, ay, ey, lry⟩
      simp only [Rel, resultOf, recordOf, exponentOf] at ih ⊢
      intro hy
      have hi := ih hy
      rcases hi with ⟨hx'none, hi⟩
      have hxnone : lr = none := by
        cases lr with
        | none => rfl
        | some v =>
          simp only [Body, Prod.fst, Prod.snd] at hbody
          unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body at hbody
          by_cases hp : p <= 0#i32 <;> simp [hp] at hbody
      subst lr
      subst lr'
      refine ⟨rfl, ?_⟩
      intro hpLoX hpHiX hshapeX hendX
      simp only [Shape, Digits] at hshapeX
      simp only [Endpoints, Value, Digits] at hendX
      obtain ⟨z0, zs0, hshape0X, hz0Lo, hz0Hi, hlen0X⟩ := hshapeX.2.2
      obtain ⟨shift, z, z', zs, zs', hleft, hk, heq, hsel,
          hshapeIn, hshapeOut, hzLo, hzHi, hz'Lo, hz'Hi, hlarge,
          hsmall, hstall, hrecover, hzeroInput, hzeroProgress, hout⟩ :=
        P826_T1_LeftStepCertificate
          (A / (2 : Real) ^ e.val) (B / (2 : Real) ^ e.val)
          { num_digits := i, decimal_point := p, truncated := t, digits := a }
          { num_digits := i', decimal_point := p', truncated := t', digits := a' }
          e e' hpLoX hpHiX hshapeX hendX (by simpa [Body] using hbody)
      change (p.val <= -19 -> p'.val = p.val + 18 \/
        p'.val = p.val + 19) at hlarge
      change (-18 <= p.val -> p.val < 0 ->
        p'.val = -1 \/ p'.val = 0) at hsmall
      change (p.val = -1 -> p'.val = -1 -> 7 <= z') at hstall
      change (p.val = -1 -> 7 <= z ->
        p'.val = 0 /\ 5 <= z') at hrecover
      change (p.val = 0 -> z < 5) at hzeroInput
      change (p.val = 0 -> p'.val = 0 /\
        (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) at hzeroProgress
      dsimp only at hout
      rcases hout with ⟨hcap', hdigit', hlen', hend'⟩
      have hp'Lo : -324 <= p'.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 h := hzeroProgress (by omega)
            omega
      have hp'Hi : p'.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 h := hzeroProgress (by omega)
            omega
      have hshape' : Shape
          { num_digits := i', decimal_point := p', truncated := t',
            digits := a' } := by
        simp only [Shape, Digits]
        exact ⟨hcap', hdigit', z', zs', hshapeOut, hz'Lo, hz'Hi, hlen'⟩
      have hscaleA :
          (2 : Real) ^ shift.val * (A / (2 : Real) ^ e.val) =
            A / (2 : Real) ^ e'.val := by
        rw [heq, 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 [heq, zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
        norm_num only [zpow_natCast]
        field_simp
      have hendNext : Endpoints
          { num_digits := i', decimal_point := p', truncated := t',
            digits := a' } e' := by
        simp only [Endpoints, Value, Digits]
        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 hnext := hi hp'Lo hp'Hi hshape' hendNext
      rcases hnext with ⟨hcapNext, horderNext⟩
      have hleadIn : Lead
          { num_digits := i, decimal_point := p, truncated := t,
            digits := a } = z := by
        exact head_eq _ z zs hshapeIn (by simpa [Digits] using hlen0X)
      have hleadOut : Lead
          { num_digits := i', decimal_point := p', truncated := t',
            digits := a' } = z' := head_eq _ z' zs' hshapeOut hlen'
      have hcapStep := P826_T1_LeftCapacityStep p.val p'.val shift z z'
        hpHiX hsel hlarge hsmall hstall hrecover hzeroProgress
      dsimp only at hcapStep
      have hcapStep' : Bonus p.val z + 3 * (shift.val : Int) <=
          Bonus p'.val z' + 10 * (p'.val - p.val) := by
        simpa only [Bonus] using hcapStep
      constructor
      · rw [hleadOut] at hcapNext
        rw [hleadIn]
        omega
      · intro horder
        have hbonusNonneg : 0 <= Bonus p'.val (Lead
            { num_digits := i', decimal_point := p', truncated := t',
              digits := a' }) := by
          simp only [Bonus]
          split_ifs <;> omega
        have hcapacity : 0 <= ey.val - e'.val + 768 - p'.val := by
          omega
        obtain ⟨b, hb⟩ := P826_T1_PowerGridAlignment
          (ey.val - e'.val) p'.val (by omega) hcapacity
        let dx : num.imp.dec2flt.decimal_seq.DecimalSeq :=
          { num_digits := i, decimal_point := p, truncated := t, digits := a }
        have hvaluePos : 0 < Value
            { num_digits := i, decimal_point := p, truncated := t,
              digits := a } := by
          have hv := P804_T1_DecimalValueBounds
            (Digits dx) z0 zs0 (by simpa [dx, Digits] using hshape0X)
              hz0Lo (by
                intro q hq
                simp only [dx, Digits, List.mem_map] at hq
                rcases hq with ⟨q8, hq8, rfl⟩
                exact hshapeX.2.1 q8 hq8) p.val
          have hvlow := hv.1
          have hpow : 0 < (z0 : Real) * (10 : Real) ^ (p.val - 1) :=
            mul_pos (by exact_mod_cast hz0Lo) (by
              exact zpow_pos (by norm_num) _)
          change 0 < Value dx
          simp only [Value, dx, Digits]
          rw [hlen0X] at hvlow
          have hpow10 : 0 < (10 : Real) ^ (p.val - 1) :=
            zpow_pos (by norm_num) _
          nlinarith
        have hpowScale :
            (2 : Real) ^ shift.val *
                (2 : Real) ^ (ey.val - e.val) =
              (2 : Real) ^ (ey.val - e'.val) := by
          rw [heq]
          rw [← zpow_natCast]
          rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
          congr 1
          omega
        have hboundary :
            (b : Real) * (10 : Real) ^ (p'.val - 768) <=
              (2 : Real) ^ shift.val * Value
                { num_digits := i, decimal_point := p, truncated := t,
                  digits := a } := by
          rw [← hb, ← hpowScale]
          exact mul_le_mul_of_nonneg_left horder
            (le_of_lt (pow_pos (by norm_num : (0 : Real) < 2) _))
        have htransport := P798_T1_LeftBoundaryTransport
          { num_digits := i, decimal_point := p, truncated := t, digits := a }
          { num_digits := i', decimal_point := p', truncated := t', digits := a' }
          shift b hk hvaluePos hshapeX.2.1 hleft hcap' hboundary
        apply horderNext
        rw [hb]
        exact htransport
    · unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1 at hloop
      simpa [Body] using hloop
  simp only [Rel, resultOf, recordOf, exponentOf] at hrel
  have h := hrel trivial
  rcases h with ⟨_, hmain⟩
  have hmain' := hmain hpLo hpHi (by simpa [Shape, Digits] using hshape0)
    (by simpa [Endpoints, Value, Digits] using hend0)
  rcases hmain' with ⟨hcap, horder⟩
  have hcap' :
      7 * (d0.decimal_point.val + 324) +
          Bonus d0.decimal_point.val (Lead d0) <=
        3 * (e1.val - e0.val + 768 - d0.decimal_point.val) := by
    simpa using hcap
  have hbonusNonneg : 0 <= Bonus d0.decimal_point.val (Lead d0) := by
    simp only [Bonus]
    split_ifs <;> omega
  constructor
  · omega
  · simpa [Value, Digits] using horder

end dec2flt_model

Download Lean source · Download TeX source

Complete Lean module
import Tablet.P826_T1_ResultLoopBackward
import Tablet.P826_T1_LeftDoneCertificate
import Tablet.P826_T1_LeftStepCertificate
import Tablet.P826_T1_LeftCapacityStep
import Tablet.P826_T1_PowerGridAlignment
import Tablet.P798_T1_LeftBoundaryTransport
import Tablet.P804_T1_DecimalValueBounds
import Mathlib.Tactic.FieldSimp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P826_T1_LeftHistoryBoundary]
theorem P826_T1_LeftHistoryBoundary
    (A B : Real)
    (d0 d1 : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (e0 e1 : Std.I32)
    (hpLo : -324 <= d0.decimal_point.val)
    (hpHi : d0.decimal_point.val <= 0)
    (hshape0 :
      let digits :=
        (d0.digits.val.take d0.num_digits.val).map (fun q => q.val)
      d0.num_digits.val <= 768 /\
      (forall q, q ∈ d0.digits.val.take d0.num_digits.val -> q.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d0.num_digits.val))
    (hend0 :
      let digits :=
        (d0.digits.val.take d0.num_digits.val).map (fun q => q.val)
      let I : Nat := digits.foldl (fun n q => 10 * n + q) 0
      let V : Real := I * (10 : Real) ^
        (d0.decimal_point.val - (d0.num_digits.val : Int))
      exists eta u : Real,
        A / (2 : Real) ^ e0.val = V + eta /\
        B / (2 : Real) ^ e0.val = V + u /\
        0 <= eta /\ eta < u /\ (d0.truncated = true <-> 0 < eta))
    (hloop :
      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 e0 none =
          ok (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits,
            e1, none)) :
    let Digits := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      (d.digits.val.take d.num_digits.val).map (fun q => q.val)
    let Value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      ((Digits d).foldl (fun n q => 10 * n + q) 0 : Nat) *
        (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int))
    0 <= e1.val - e0.val + 768 - d0.decimal_point.val /\
      ((2 : Real) ^ (e1.val - e0.val) <= Value d0 ->
        1 <= Value d1) := by
-- BODY
  dsimp only at hshape0 hend0 ⊢
  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 d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    (d.digits.val.take d.num_digits.val).map (fun q => q.val)
  let Value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    ((Digits d).foldl (fun n q => 10 * n + q) 0 : Nat) *
      (10 : Real) ^ (d.decimal_point.val - (d.num_digits.val : Int))
  let Shape := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    d.num_digits.val <= 768 /\
    (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
    (exists z zs, Digits d = z :: zs /\ 1 <= z /\ z < 10 /\
      (Digits d).length = d.num_digits.val)
  let Endpoints := fun (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (e : Std.I32) =>
    exists eta u : Real,
      A / (2 : Real) ^ e.val = Value d + eta /\
      B / (2 : Real) ^ e.val = Value d + u /\
      0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)
  let Lead := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
    d.digits.val[0].val
  let Bonus : Int -> Nat -> Int := fun (p : Int) (z : Nat) =>
    if p = -1 then (if 7 <= z then 36 else 27)
    else if p = 0 then
      (if 5 <= z then 36 else if 3 <= z then 33 else 27)
    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
  let Rel := fun (x y : State) =>
    resultOf y = none ->
    resultOf x = none /\
    (-324 <= (recordOf x).decimal_point.val ->
      (recordOf x).decimal_point.val <= 0 ->
      Shape (recordOf x) -> Endpoints (recordOf x) (exponentOf x) ->
      7 * ((recordOf x).decimal_point.val + 324) +
          Bonus (recordOf x).decimal_point.val (Lead (recordOf x)) <=
        3 * ((exponentOf y).val - (exponentOf x).val + 768 -
          (recordOf x).decimal_point.val) /\
      ((2 : Real) ^ ((exponentOf y).val - (exponentOf x).val) <=
          Value (recordOf x) ->
        1 <= Value (recordOf y)))
  have head_eq (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (z : Nat) (zs : List Nat)
      (hshape : Digits d = z :: zs)
      (hlen : (Digits d).length = d.num_digits.val) : Lead d = z := by
    simp only [Digits] at hshape hlen
    have hnumPos : 0 < d.num_digits.val := by
      rw [← hlen, hshape]
      simp
    have hindexBound : (0 : Nat) < d.digits.val.length := by
      rw [d.digits.property]
      norm_num
    have hget : d.digits.val[0]? = some d.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 : (d.digits.val.take d.num_digits.val)[0]? =
        some d.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 hrel : Rel
      (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits, e0, none)
      (d1.num_digits, d1.decimal_point, d1.truncated, d1.digits, e1, none) := by
    apply P826_T1_ResultLoopBackward Rel Body
    · intro x y hbody
      rcases x with ⟨i, p, t, a, e, lr⟩
      rcases y with ⟨iy, py, ty, ay, ey, lry⟩
      simp only [Rel, resultOf, recordOf, exponentOf]
      intro hy
      rw [hy] at hbody
      have hx : lr = none := by
        cases lr with
        | none => rfl
        | some v =>
          simp only [Body, Prod.fst, Prod.snd] at hbody
          unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body at hbody
          by_cases hp : p <= 0#i32 <;> simp [hp] at hbody
      subst lr
      refine ⟨rfl, ?_⟩
      intro hpLoX hpHiX hshapeX hendX
      have hdone := P826_T1_LeftDoneCertificate
        { num_digits := i, decimal_point := p, truncated := t, digits := a }
        { num_digits := iy, decimal_point := py, truncated := ty, digits := ay }
        e ey hpHiX (by simpa [Body] using hbody)
      rcases hdone with ⟨hdy, hey, hp0, hlead5⟩
      have hlead5' : 5 <= Lead
          { num_digits := i, decimal_point := p, truncated := t,
            digits := a } := by
        simpa [Lead] using hlead5
      subst ey
      constructor
      · simp only [Bonus]
        simp [hp0, hlead5']
      · intro horder
        rw [hdy]
        simpa using horder
    · intro x x' y hbody ih
      rcases x with ⟨i, p, t, a, e, lr⟩
      rcases x' with ⟨i', p', t', a', e', lr'⟩
      rcases y with ⟨iy, py, ty, ay, ey, lry⟩
      simp only [Rel, resultOf, recordOf, exponentOf] at ih ⊢
      intro hy
      have hi := ih hy
      rcases hi with ⟨hx'none, hi⟩
      have hxnone : lr = none := by
        cases lr with
        | none => rfl
        | some v =>
          simp only [Body, Prod.fst, Prod.snd] at hbody
          unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body at hbody
          by_cases hp : p <= 0#i32 <;> simp [hp] at hbody
      subst lr
      subst lr'
      refine ⟨rfl, ?_⟩
      intro hpLoX hpHiX hshapeX hendX
      simp only [Shape, Digits] at hshapeX
      simp only [Endpoints, Value, Digits] at hendX
      obtain ⟨z0, zs0, hshape0X, hz0Lo, hz0Hi, hlen0X⟩ := hshapeX.2.2
      obtain ⟨shift, z, z', zs, zs', hleft, hk, heq, hsel,
          hshapeIn, hshapeOut, hzLo, hzHi, hz'Lo, hz'Hi, hlarge,
          hsmall, hstall, hrecover, hzeroInput, hzeroProgress, hout⟩ :=
        P826_T1_LeftStepCertificate
          (A / (2 : Real) ^ e.val) (B / (2 : Real) ^ e.val)
          { num_digits := i, decimal_point := p, truncated := t, digits := a }
          { num_digits := i', decimal_point := p', truncated := t', digits := a' }
          e e' hpLoX hpHiX hshapeX hendX (by simpa [Body] using hbody)
      change (p.val <= -19 -> p'.val = p.val + 18 \/
        p'.val = p.val + 19) at hlarge
      change (-18 <= p.val -> p.val < 0 ->
        p'.val = -1 \/ p'.val = 0) at hsmall
      change (p.val = -1 -> p'.val = -1 -> 7 <= z') at hstall
      change (p.val = -1 -> 7 <= z ->
        p'.val = 0 /\ 5 <= z') at hrecover
      change (p.val = 0 -> z < 5) at hzeroInput
      change (p.val = 0 -> p'.val = 0 /\
        (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) at hzeroProgress
      dsimp only at hout
      rcases hout with ⟨hcap', hdigit', hlen', hend'⟩
      have hp'Lo : -324 <= p'.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 h := hzeroProgress (by omega)
            omega
      have hp'Hi : p'.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 h := hzeroProgress (by omega)
            omega
      have hshape' : Shape
          { num_digits := i', decimal_point := p', truncated := t',
            digits := a' } := by
        simp only [Shape, Digits]
        exact ⟨hcap', hdigit', z', zs', hshapeOut, hz'Lo, hz'Hi, hlen'⟩
      have hscaleA :
          (2 : Real) ^ shift.val * (A / (2 : Real) ^ e.val) =
            A / (2 : Real) ^ e'.val := by
        rw [heq, 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 [heq, zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
        norm_num only [zpow_natCast]
        field_simp
      have hendNext : Endpoints
          { num_digits := i', decimal_point := p', truncated := t',
            digits := a' } e' := by
        simp only [Endpoints, Value, Digits]
        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 hnext := hi hp'Lo hp'Hi hshape' hendNext
      rcases hnext with ⟨hcapNext, horderNext⟩
      have hleadIn : Lead
          { num_digits := i, decimal_point := p, truncated := t,
            digits := a } = z := by
        exact head_eq _ z zs hshapeIn (by simpa [Digits] using hlen0X)
      have hleadOut : Lead
          { num_digits := i', decimal_point := p', truncated := t',
            digits := a' } = z' := head_eq _ z' zs' hshapeOut hlen'
      have hcapStep := P826_T1_LeftCapacityStep p.val p'.val shift z z'
        hpHiX hsel hlarge hsmall hstall hrecover hzeroProgress
      dsimp only at hcapStep
      have hcapStep' : Bonus p.val z + 3 * (shift.val : Int) <=
          Bonus p'.val z' + 10 * (p'.val - p.val) := by
        simpa only [Bonus] using hcapStep
      constructor
      · rw [hleadOut] at hcapNext
        rw [hleadIn]
        omega
      · intro horder
        have hbonusNonneg : 0 <= Bonus p'.val (Lead
            { num_digits := i', decimal_point := p', truncated := t',
              digits := a' }) := by
          simp only [Bonus]
          split_ifs <;> omega
        have hcapacity : 0 <= ey.val - e'.val + 768 - p'.val := by
          omega
        obtain ⟨b, hb⟩ := P826_T1_PowerGridAlignment
          (ey.val - e'.val) p'.val (by omega) hcapacity
        let dx : num.imp.dec2flt.decimal_seq.DecimalSeq :=
          { num_digits := i, decimal_point := p, truncated := t, digits := a }
        have hvaluePos : 0 < Value
            { num_digits := i, decimal_point := p, truncated := t,
              digits := a } := by
          have hv := P804_T1_DecimalValueBounds
            (Digits dx) z0 zs0 (by simpa [dx, Digits] using hshape0X)
              hz0Lo (by
                intro q hq
                simp only [dx, Digits, List.mem_map] at hq
                rcases hq with ⟨q8, hq8, rfl⟩
                exact hshapeX.2.1 q8 hq8) p.val
          have hvlow := hv.1
          have hpow : 0 < (z0 : Real) * (10 : Real) ^ (p.val - 1) :=
            mul_pos (by exact_mod_cast hz0Lo) (by
              exact zpow_pos (by norm_num) _)
          change 0 < Value dx
          simp only [Value, dx, Digits]
          rw [hlen0X] at hvlow
          have hpow10 : 0 < (10 : Real) ^ (p.val - 1) :=
            zpow_pos (by norm_num) _
          nlinarith
        have hpowScale :
            (2 : Real) ^ shift.val *
                (2 : Real) ^ (ey.val - e.val) =
              (2 : Real) ^ (ey.val - e'.val) := by
          rw [heq]
          rw [← zpow_natCast]
          rw [← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
          congr 1
          omega
        have hboundary :
            (b : Real) * (10 : Real) ^ (p'.val - 768) <=
              (2 : Real) ^ shift.val * Value
                { num_digits := i, decimal_point := p, truncated := t,
                  digits := a } := by
          rw [← hb, ← hpowScale]
          exact mul_le_mul_of_nonneg_left horder
            (le_of_lt (pow_pos (by norm_num : (0 : Real) < 2) _))
        have htransport := P798_T1_LeftBoundaryTransport
          { num_digits := i, decimal_point := p, truncated := t, digits := a }
          { num_digits := i', decimal_point := p', truncated := t', digits := a' }
          shift b hk hvaluePos hshapeX.2.1 hleft hcap' hboundary
        apply horderNext
        rw [hb]
        exact htransport
    · unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1 at hloop
      simpa [Body] using hloop
  simp only [Rel, resultOf, recordOf, exponentOf] at hrel
  have h := hrel trivial
  rcases h with ⟨_, hmain⟩
  have hmain' := hmain hpLo hpHi (by simpa [Shape, Digits] using hshape0)
    (by simpa [Endpoints, Value, Digits] using hend0)
  rcases hmain' with ⟨hcap, horder⟩
  have hcap' :
      7 * (d0.decimal_point.val + 324) +
          Bonus d0.decimal_point.val (Lead d0) <=
        3 * (e1.val - e0.val + 768 - d0.decimal_point.val) := by
    simpa using hcap
  have hbonusNonneg : 0 <= Bonus d0.decimal_point.val (Lead d0) := by
    simp only [Bonus]
    split_ifs <;> omega
  constructor
  · omega
  · simpa [Value, Digits] using horder

end dec2flt_model

Proof structure

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

Explore this dependency tree

Inspect related source (151)
More program dependencies (109)
Standard-library dependencies (39)

Related source and models come from the extraction records and declared dependencies.

Proof assumptions and review

Lean checks the formal proof. Trellis also reviews how the statement corresponds to the intended requirement.

Transitive assumptions

This list combines the assumptions recorded for this declaration and its dependencies. It may include assumptions used by auxiliary declarations in those modules.

Helper theorems included
All 138 entries
Technical checking records
Local closure summary
{
  "node": "P826_T1_LeftHistoryBoundary",
  "closure_version": "patch_c_v1",
  "active_decl_hash": "cffb21f338022a9583ee712860833b7195998bfedafd3a66d71cd025dd69f905",
  "active_statement_hash": "9ef2239b936119839fa183742d029a5d63a5acfc59b1f854fb0a0a132dca2673",
  "kernel_axioms": [
    "Classical.choice",
    "Quot.sound",
    "propext"
  ],
  "toolchain_hash": "ce4c4e3d87434b9663f46de25ce34b48a0cf0d392e0a320a0787b4674a2d7b61",
  "lean_executable_hash": "3e0d0d3d801675359f2d4cf9815bfdb417b20b92fdd9d48b3b14c95bbae28bbf",
  "checker_script_hash": "c8e4a03273e23b9a8f2e141020652a4e0b7b0a8ab0883366ea9d031ca3f37aa1",
  "approved_axioms_hash": "ab64d13f256386ee0e494a5abcc6a5db49512cf3d90bbdfcabd6327793a31f95",
  "axcheck_status": "Agreed",
  "accepted_at_snapshot_id": "worker-result-local-closure-issuance-cycle-196",
  "boundary_theorems": {},
  "strict_theorem_deps": {
    "P798_T1_LeftBoundaryTransport": "8382fe6b25ea536f",
    "P804_T1_DecimalValueBounds": "4b5c924b5d52bc9d",
    "P826_T1_LeftCapacityStep": "c0dcbedfb2ed83e3",
    "P826_T1_LeftDoneCertificate": "cea79000f1fdd2b1",
    "P826_T1_LeftStepCertificate": "97cc58b153450e0e",
    "P826_T1_PowerGridAlignment": "0cee3aaeb64f7353",
    "P826_T1_ResultLoopBackward": "46222c89d1819e7b",
    "num_imp_dec2flt_slow_parse_long_mantissa": "0eb6fb2627d0ff5c"
  },
  "strict_definition_deps": {
    "F64": "ec4e25fbaf4e11d4",
    "F64_Insts_Dec2flt_modelNumImpTraitsFloatU64": "94095c9bf79da164",
    "num_imp_dec2flt_common_BiasedFp": "63baac539d218a81",
    "num_imp_dec2flt_decimal_seq_DecimalSeq": "b0286c42454b158d",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE": "0cc5155758bc976a",
    "num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift": "fd298e37d5ae4f86",
    "num_imp_dec2flt_slow_parse_long_mantissa": "a3ed147b34a97b11",
    "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:abdd11b5d55bfb83976043adb33dcc99d48e6ed1f0f83ab63aaabe203d9fccde",
  "superseded_for_theorem_closure": false
}
Review verdicts
{
  "corr_status": "Pass",
  "substantiveness_status": "Pass"
}