import Tablet.P856_T1_ReachableCapacity
import Tablet.P856_T1_FinalLeftShiftExecution
import Tablet.P856_T1_LeftHistoryTransport
import Tablet.P856_T1_RightHistoryTransport
import Tablet.P856_T1_CoefficientGridAlignment
import Tablet.P798_T1_GridAlignment
import Tablet.P798_T1_GridBoundaryRetained
import Tablet.P798_T1_LeftBoundaryTransport
import Tablet.P798_T1_RightBoundaryTransport
import Tablet.P804_T1_DecimalValueBounds
import Tablet.ParserSlowReachableNormalizationBounds
import Mathlib.Tactic.FieldSimp

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P856_T1_SeparationPackage]
theorem P856_T1_SeparationPackage :
    forall (A B : Real)
      (d d0 d1 d2 : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (exp0 exp1 expStart exp2 : Std.I32),
      -324 <= d.decimal_point.val -> d.decimal_point.val < 310 ->
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun n digit => 10 * n + digit) 0
      let value : Real := integer * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      let cutoff : Real := (10 : Real) ^
        (d.decimal_point.val - (768 : Int))
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d.num_digits.val) /\
      B = value + cutoff /\
      (exists eta : Real,
        A = value + eta /\ 0 <= eta /\ eta < cutoff /\
        (d.truncated = true <-> 0 < eta)) ->
      num.imp.dec2flt.slow.parse_long_mantissa_loop0
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 0#i32 }
        d.num_digits d.decimal_point d.truncated d.digits 0#i32 none =
          ok (d0.num_digits, d0.decimal_point, d0.truncated, d0.digits,
            exp0, none) ->
      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) ->
      exp1 - 1#i32 = ok expStart ->
      num.imp.dec2flt.slow.parse_long_mantissa_loop2
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 d1 expStart =
          ok (d2, exp2) ->
      let digits2 :=
        (d2.digits.val.take d2.num_digits.val).map (fun z => z.val)
      let integer2 : Nat := digits2.foldl (fun n digit => 10 * n + digit) 0
      let value2 : Real := integer2 * (10 : Real) ^
        (d2.decimal_point.val - (d2.num_digits.val : Int))
      d2.num_digits.val <= 768 /\
      (forall z, z ∈ d2.digits.val.take d2.num_digits.val -> z.val < 10) /\
      (exists z zs, digits2 = z :: zs /\ 1 <= z /\ z < 10 /\
        digits2.length = d2.num_digits.val) /\
      (exists eta2 u2 : Real,
        A / (2 : Real) ^ (exp2.val + 1) = value2 + eta2 /\
        B / (2 : Real) ^ (exp2.val + 1) = value2 + u2 /\
        0 <= eta2 /\ eta2 < u2 /\
        (d2.truncated = true <-> 0 < eta2)) ->
      exists d3 : num.imp.dec2flt.decimal_seq.DecimalSeq,
        num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d2 53#usize = ok d3 /\
        let digits3 :=
          (d3.digits.val.take d3.num_digits.val).map (fun z => z.val)
        let integer3 : Nat := digits3.foldl (fun n digit => 10 * n + digit) 0
        let value3 : Real := integer3 * (10 : Real) ^
          (d3.decimal_point.val - (d3.num_digits.val : Int))
        d3.num_digits.val <= 768 /\
        (forall z, z ∈ d3.digits.val.take d3.num_digits.val -> z.val < 10) /\
        (exists z zs, digits3 = z :: zs /\ 1 <= z /\ z < 10 /\
          digits3.length = d3.num_digits.val /\ digits3.getLast? != some 0) /\
        (exists eta3 u3 : Real,
          A / (2 : Real) ^ (exp2.val - 52) = value3 + eta3 /\
          B / (2 : Real) ^ (exp2.val - 52) = value3 + u3 /\
          0 <= eta3 /\ eta3 < u3 /\
          (d3.truncated = true <-> 0 < eta3)) /\
        d3.decimal_point.val < 19 /\
        0 <= A / (2 : Real) ^ (exp2.val - 52) /\
        A / (2 : Real) ^ (exp2.val - 52) <
          (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) /\
        (forall n : Nat,
          (n : Real) + (1 / 2 : Real) <= value3 \/
            B / (2 : Real) ^ (exp2.val - 52) <=
              (n : Real) + (1 / 2 : Real)) /\
        (forall n : Nat,
          ((2 * n + 1 : Nat) : Real) <= value3 \/
            B / (2 : Real) ^ (exp2.val - 52) <=
              ((2 * n + 1 : Nat) : Real)) := by
-- BODY
  dsimp only
  intro A B d d0 d1 d2 exp0 exp1 expStart exp2 hpLo hpHi hinitial
    hloop0 hloop1 hsub hloop2 hstate2
  classical
  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))
  rcases hinitial with ⟨hcap, hdigit, hpositive, hB, hcanonicalA⟩
  rcases hpositive with ⟨z, zs, hzshape, hzLo, hzHi, hzlen⟩
  have hdne : d.num_digits.val != 0 := by
    have hpos : 0 < d.num_digits.val := by
      rw [← hzlen, hzshape]
      simp
    simpa [bne_iff_ne] using hpos.ne'
  have hcanonicalPack :
      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) /\
      (exists eta : Real,
        A = Value d + eta /\ 0 <= eta /\
        eta < (10 : Real) ^ (d.decimal_point.val - (768 : Int)) /\
        (d.truncated = true <-> 0 < eta)) :=
    ⟨hcap, hdigit, ⟨z, zs, hzshape, hzLo, hzHi, hzlen⟩, hcanonicalA⟩
  have hspec0 := ParserSlowLoop0 A B d hdne hpLo hpHi hB
    (by simpa [Digits, Value] using hcanonicalPack)
  dsimp only at hspec0
  rcases hspec0 with ⟨q0, e0, steps0, hcall0, hsteps0, he0Lo,
    he0Hi, hALower, hp0Lo, hp0Hi, hselector0, hshape0⟩
  have htuple0 := Result.ok.inj (hcall0.symm.trans hloop0)
  have hq0 : q0 = d0 := by
    cases q0
    cases d0
    simp only [Prod.mk.injEq] at htuple0
    simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq]
    tauto
  have he0 : e0 = exp0 := congrArg
    (fun x : Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
      Std.I32 × Option num.imp.dec2flt.common.BiasedFp => x.2.2.2.2.1)
    htuple0
  subst q0
  subst e0
  rcases hshape0 with ⟨hcap0, hdigit0, hpositive0, hend0⟩
  have hshape0Pack :
      d0.num_digits.val <= 768 /\
      (forall q, q ∈ d0.digits.val.take d0.num_digits.val -> q.val < 10) /\
      (exists z zs, Digits d0 = z :: zs /\ 1 <= z /\ z < 10 /\
        (Digits d0).length = d0.num_digits.val) :=
    ⟨hcap0, hdigit0, by simpa [Digits] using hpositive0⟩
  have hend0Pack :
      exists eta u : Real,
        A / (2 : Real) ^ exp0.val = Value d0 + eta /\
        B / (2 : Real) ^ exp0.val = Value d0 + u /\
        0 <= eta /\ eta < u /\ (d0.truncated = true <-> 0 < eta) := by
    simpa [Value, Digits] using hend0
  have hspec1 := ParserSlowLoop1 A B d0 exp0 hp0Lo hp0Hi he0Lo
    ⟨hcap0, hdigit0, hpositive0, hend0⟩
  dsimp only at hspec1
  rcases hspec1 with ⟨q1, e1, steps1, hcall1, hsteps1, he1Hi,
    hp1, hnum1, hlead1, hlead1Hi, hshape1⟩
  have htuple1 := Result.ok.inj (hcall1.symm.trans hloop1)
  have hq1 : q1 = d1 := by
    cases q1
    cases d1
    simp only [Prod.mk.injEq] at htuple1
    simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq]
    tauto
  have he1 : e1 = exp1 := congrArg
    (fun x : Std.Usize × Std.I32 × Bool × Array Std.U8 768#usize ×
      Std.I32 × Option num.imp.dec2flt.common.BiasedFp => x.2.2.2.2.1)
    htuple1
  subst q1
  subst e1
  rcases hshape1 with ⟨hcap1, hdigit1, hpositive1, hend1⟩
  have hpositive1One : exists z zs,
      Digits d1 = z :: zs /\ 1 <= z /\ z < 10 /\
        (Digits d1).length = d1.num_digits.val := by
    rcases hpositive1 with ⟨z1, zs1, hzshape1, hz1Lo, hz1Hi, hzlen1⟩
    exact ⟨z1, zs1, hzshape1, by omega, hz1Hi, hzlen1⟩
  have hnorm := ParserSlowReachableNormalizationBounds A B d d0 d1 exp0 exp1
    hpLo hpHi hB (by simpa [Digits, Value] using hcanonicalPack) hALower
    hloop0 hloop1 hp1 hnum1 hlead1 (by simpa [Digits, Value] using hend1)
  rcases hstate2 with ⟨hcap2, hdigit2, hpositive2, hend2⟩
  have hcapacity := P856_T1_ReachableCapacity A B d d0 d1 d2 exp0 exp1
    expStart exp2 hpLo hpHi hB (by simpa [Digits, Value] using hcanonicalPack)
    hloop0 hloop1 hsub hloop2
  have hfinal := P856_T1_FinalLeftShiftExecution A B d1 d2 exp1 expStart exp2
    hnorm.2 hp1 hnum1 hlead1
    (by exact ⟨hcap1, hdigit1, hpositive1One, hend1⟩) hsub hloop2
  rcases hfinal with ⟨d3, hp2Lo, hp2Hi, hloop2Step, hleft53, hstate3⟩
  dsimp only at hstate3
  rcases hstate3 with ⟨hcap3, hdigit3, hpositive3, hend3, hp3Hi⟩
  rcases hpositive0 with ⟨z0, zs0, hzshape0, hz0Lo, hz0Hi, hzlen0⟩
  rcases hpositive1 with ⟨z1, zs1, hzshape1, hz1Lo, hz1Hi, hzlen1⟩
  rcases hpositive2 with ⟨z2, zs2, hzshape2, hz2Lo, hz2Hi, hzlen2⟩
  rcases hpositive3 with
    ⟨z3, zs3, hzshape3, hz3Lo, hz3Hi, hzlen3, hlast3⟩
  have valuePos (q : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (zq : Nat) (zsq : List Nat)
      (hshapeq : Digits q = zq :: zsq) (hzq : 1 <= zq)
      (hlenq : (Digits q).length = q.num_digits.val)
      (hdigitq : forall w, w ∈ q.digits.val.take q.num_digits.val ->
        w.val < 10) : 0 < Value q := by
    have hv := P804_T1_DecimalValueBounds (Digits q) zq zsq hshapeq hzq
      (by
        intro w hw
        simp only [Digits, List.mem_map] at hw
        rcases hw with ⟨w8, hw8, rfl⟩
        exact hdigitq w8 hw8) q.decimal_point.val
    dsimp only at hv
    rw [hlenq] at hv
    have hp : 0 < (zq : Real) * (10 : Real) ^
        (q.decimal_point.val - 1) :=
      mul_pos (by exact_mod_cast hzq) (zpow_pos (by norm_num) _)
    change 0 < Value q
    simpa [Value, Digits] using lt_of_lt_of_le hp hv.1
  have hvalue0Pos := valuePos d0 z0 zs0 (by simpa [Digits] using hzshape0)
    hz0Lo hzlen0 hdigit0
  have hvalue1Pos := valuePos d1 z1 zs1 (by simpa [Digits] using hzshape1)
    (by omega) hzlen1 hdigit1
  have hvalue2Pos := valuePos d2 z2 zs2 (by simpa [Digits] using hzshape2)
    hz2Lo hzlen2 hdigit2
  have separate (c : Nat) (R : Int)
      (hcap0R : 0 <= R - exp0.val + 768 - d0.decimal_point.val)
      (hcap2R : 0 <= R - (exp2.val + 1) + 768 - d2.decimal_point.val)
      (hcap3R : 0 <= R - (exp2.val - 52) + 768 - d3.decimal_point.val)
      (hlower : Value d3 <
        (c : Real) * (2 : Real) ^ (R - (exp2.val - 52)))
      (hupper : (c : Real) * (2 : Real) ^ (R - (exp2.val - 52)) <
        B / (2 : Real) ^ (exp2.val - 52)) : False := by
    have hstrict2 : Value d2 <
        (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) := by
      by_contra hnot
      have hbound2 :
          (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) <= Value d2 :=
        le_of_not_gt hnot
      obtain ⟨b3, hb3⟩ := P856_T1_CoefficientGridAlignment c
        (R - (exp2.val - 52)) d3.decimal_point.val (by omega) hcap3R
      have hscale :
          (c : Real) * (2 : Real) ^ (R - (exp2.val - 52)) =
            (2 : Real) ^ (53 : Nat) *
              ((c : Real) * (2 : Real) ^ (R - (exp2.val + 1))) := by
        rw [show R - (exp2.val - 52) =
          (R - (exp2.val + 1)) + 53 by omega]
        rw [zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
        norm_num only [zpow_natCast]
        ring
      have hbound :
          (b3 : Real) * (10 : Real) ^ (d3.decimal_point.val - 768) <=
            (2 : Real) ^ (53 : Nat) * Value d2 := by
        rw [← hb3, hscale]
        exact mul_le_mul_of_nonneg_left hbound2 (by positivity)
      have ht := P798_T1_LeftBoundaryTransport d2 d3 53#usize b3
        (by norm_num) hvalue2Pos hdigit2 hleft53 hcap3 hbound
      have ht' :
          (b3 : Real) * (10 : Real) ^ (d3.decimal_point.val - 768) <=
            Value d3 := by simpa [Value, Digits] using ht
      rw [hb3] at hlower
      exact (not_lt_of_ge ht') hlower
    have hstrict1 : Value d1 <
        (c : Real) * (2 : Real) ^ (R - exp1.val) := by
      rcases hloop2Step with hid | hshift
      · rcases hid with ⟨hd, he⟩
        rw [hd] at hstrict2
        simpa [he] using hstrict2
      · rcases hshift with ⟨shift, hshift58, hright, he⟩
        by_contra hnot
        have hbound1 :
            (c : Real) * (2 : Real) ^ (R - exp1.val) <= Value d1 :=
          le_of_not_gt hnot
        obtain ⟨b2, hb2⟩ := P856_T1_CoefficientGridAlignment c
          (R - (exp2.val + 1)) d2.decimal_point.val (by omega) hcap2R
        have hscale :
            (c : Real) * (2 : Real) ^ (R - (exp2.val + 1)) =
              ((c : Real) * (2 : Real) ^ (R - exp1.val)) /
                (2 : Real) ^ shift.val := by
          rw [he]
          rw [show R - (exp1.val + (shift.val : Int)) =
            (R - exp1.val) - (shift.val : Int) by omega]
          rw [zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)]
          norm_num only [zpow_natCast]
          ring
        have hbound :
            (b2 : Real) * (10 : Real) ^ (d2.decimal_point.val - 768) <=
              Value d1 / (2 : Real) ^ shift.val := by
          rw [← hb2, hscale]
          exact div_le_div_of_nonneg_right hbound1 (by positivity)
        have hnonreset : ¬ (d2.num_digits.val = 0 /\
            d2.decimal_point.val = 0 /\ d2.truncated = false) := by
          intro hr
          have hlenPos := hzlen2
          rw [hzshape2] at hlenPos
          simp only [List.length_cons] at hlenPos
          omega
        have ht := P798_T1_RightBoundaryTransport d1 d2 shift b2
          (by omega) hvalue1Pos hdigit1 hright hnonreset hcap2 hbound
        have ht' :
            (b2 : Real) * (10 : Real) ^ (d2.decimal_point.val - 768) <=
              Value d2 := by simpa [Value, Digits] using ht
        rw [hb2] at hstrict2
        exact (not_lt_of_ge ht') hstrict2
    have hleftHistory := P856_T1_LeftHistoryTransport A B d0 d1 exp0 exp1
      c R hp0Lo hp0Hi (by simpa [Digits] using hshape0Pack)
      (by simpa [Value, Digits] using hend0Pack) hloop1 hcap0R
    have hstrict0 := hleftHistory.2 (by simpa [Value, Digits] using hstrict1)
    have hrightHistory := P856_T1_RightHistoryTransport A B d d0 0#i32 exp0
      c R hpLo hpHi
      (by exact ⟨hcap, hdigit, ⟨z, zs, hzshape, hzLo, hzHi, hzlen⟩⟩)
      (by
        rcases hcanonicalA with ⟨eta, hAeq, heta0, hetaCut, hsticky⟩
        refine ⟨eta, (10 : Real) ^ (d.decimal_point.val - 768), ?_, ?_,
          heta0, hetaCut, hsticky⟩
        · simpa [Value, Digits] using hAeq
        · simpa [Value, Digits] using hB)
      hloop0 hcap0R
    have hstrictInit := hrightHistory.2
      (by simpa [Value, Digits] using hstrict0)
    have hpowFinalPos : 0 < (2 : Real) ^ (exp2.val - 52) :=
      zpow_pos (by norm_num) _
    have hupperMul := (lt_div_iff₀ hpowFinalPos).mp hupper
    have hboundaryScale :
        ((c : Real) * (2 : Real) ^ (R - (exp2.val - 52))) *
            (2 : Real) ^ (exp2.val - 52) =
          (c : Real) * (2 : Real) ^ R := by
      rw [mul_assoc, ← zpow_add₀ (by norm_num : (2 : Real) ≠ 0)]
      congr 2
      omega
    have hupperInit : (c : Real) * (2 : Real) ^ R < B := by
      rw [← hboundaryScale]
      exact hupperMul
    obtain ⟨a, ha⟩ := P798_T1_GridAlignment d hcap
    obtain ⟨b, hb⟩ := P856_T1_CoefficientGridAlignment c R
      d.decimal_point.val (by omega) (by simpa using hrightHistory.1)
    let q : Real := (10 : Real) ^ (d.decimal_point.val - 768)
    let rho : Real := (c : Real) * (2 : Real) ^ R - Value d
    have hq : 0 < q := zpow_pos (by norm_num) _
    have hrho : rho < q := by
      dsimp only [rho, q]
      have hB' : B = Value d + (10 : Real) ^ (d.decimal_point.val - 768) := by
        simpa [Value, Digits] using hB
      linarith
    have hboundary : (b : Real) * q <= (a : Real) * q + rho := by
      have ha' : Value d = (a : Real) * q := by
        simpa [Value, Digits, q, List.map_take] using ha
      have hb' : (c : Real) * (2 : Real) ^ R = (b : Real) * q := by
        simpa [q] using hb
      rw [← hb', ← ha']
      simp [rho]
    have hretained := P798_T1_GridBoundaryRetained a b q rho hq hrho hboundary
    have ha' : Value d = (a : Real) * q := by
      simpa [Value, Digits, q, List.map_take] using ha
    have hb' : (c : Real) * (2 : Real) ^ R = (b : Real) * q := by
      simpa [q] using hb
    rw [← hb', ← ha'] at hretained
    exact (not_lt_of_ge hretained) (by simpa [Value, Digits] using hstrictInit)
  rcases hend3 with ⟨eta3, u3, hA3, hB3, heta3, heta3u3, hsticky3⟩
  have hA_nonneg : 0 <= A / (2 : Real) ^ (exp2.val - 52) := by
    have hvalue3Pos := valuePos d3 z3 zs3 (by simpa [Digits] using hzshape3)
      hz3Lo hzlen3 hdigit3
    rw [hA3]
    linarith
  have hAB : A < B := by
    rcases hcanonicalA with ⟨eta, hAeq, hetaLo, hetaHi, hsticky⟩
    have hcutPos : 0 < (10 : Real) ^ (d.decimal_point.val - 768) :=
      zpow_pos (by norm_num) _
    rw [hAeq, hB]
    linarith
  have hpow1Pos : 0 < (2 : Real) ^ exp1.val := zpow_pos (by norm_num) _
  have hBpow : B <= (2 : Real) ^ exp1.val := by
    have hm := (div_le_iff₀ hpow1Pos).mp hnorm.1
    simpa using hm
  have heRel : exp1.val - 1 <= exp2.val := by
    rcases hloop2Step with hid | hshift
    · omega
    · rcases hshift with ⟨shift, hshift58, hright, he⟩
      omega
  have hA_upper53 : A / (2 : Real) ^ (exp2.val - 52) <
      (2 : Real) ^ (53 : Nat) := by
    have hpowFinalPos : 0 < (2 : Real) ^ (exp2.val - 52) :=
      zpow_pos (by norm_num) _
    calc
      A / (2 : Real) ^ (exp2.val - 52) <
          B / (2 : Real) ^ (exp2.val - 52) :=
        (div_lt_div_iff_of_pos_right hpowFinalPos).2 hAB
      _ <= (2 : Real) ^ exp1.val / (2 : Real) ^ (exp2.val - 52) :=
        div_le_div_of_nonneg_right hBpow hpowFinalPos.le
      _ = (2 : Real) ^ (exp1.val - exp2.val + 52) := by
        rw [show exp1.val - exp2.val + 52 =
          exp1.val - (exp2.val - 52) by omega]
        exact (zpow_sub₀ (by norm_num : (2 : Real) ≠ 0)
          exp1.val (exp2.val - 52)).symm
      _ <= (2 : Real) ^ (53 : Nat) := by
        rw [← zpow_natCast]
        exact zpow_le_zpow_right₀ (by norm_num) (by omega)
  have hA_u64 : A / (2 : Real) ^ (exp2.val - 52) <
      (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) := by
    have hconst : (2 : Real) ^ (53 : Nat) <
        (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) := by norm_num
    exact hA_upper53.trans hconst
  have hhalf : forall n : Nat,
      (n : Real) + (1 / 2 : Real) <= Value d3 \/
        B / (2 : Real) ^ (exp2.val - 52) <=
          (n : Real) + (1 / 2 : Real) := by
    intro n
    let c : Nat := 2 * n + 1
    let R : Int := exp2.val - 53
    have hboundary : (c : Real) * (2 : Real) ^
        (R - (exp2.val - 52)) = (n : Real) + (1 / 2 : Real) := by
      simp only [c, R]
      rw [show exp2.val - 53 - (exp2.val - 52) = -1 by omega]
      norm_num [zpow_neg]
      push_cast
      ring
    by_cases hlower : (n : Real) + (1 / 2 : Real) <= Value d3
    · exact Or.inl hlower
    · right
      by_contra hupperNot
      have hlower' : Value d3 < (c : Real) * (2 : Real) ^
          (R - (exp2.val - 52)) := by
        rw [hboundary]
        exact lt_of_not_ge hlower
      have hupper' : (c : Real) * (2 : Real) ^
          (R - (exp2.val - 52)) <
          B / (2 : Real) ^ (exp2.val - 52) := by
        rw [hboundary]
        exact lt_of_not_ge hupperNot
      exact (separate c R (by dsimp only [R]; omega)
        (by dsimp only [R]; omega) (by dsimp only [R]; omega)
        hlower' hupper').elim
  have hodd : forall n : Nat,
      ((2 * n + 1 : Nat) : Real) <= Value d3 \/
        B / (2 : Real) ^ (exp2.val - 52) <=
          ((2 * n + 1 : Nat) : Real) := by
    intro n
    let c : Nat := 2 * n + 1
    let R : Int := exp2.val - 52
    have hboundary : (c : Real) * (2 : Real) ^
        (R - (exp2.val - 52)) = ((2 * n + 1 : Nat) : Real) := by
      simp [c, R]
    by_cases hlower : ((2 * n + 1 : Nat) : Real) <= Value d3
    · exact Or.inl hlower
    · right
      by_contra hupperNot
      have hlower' : Value d3 < (c : Real) * (2 : Real) ^
          (R - (exp2.val - 52)) := by
        rw [hboundary]
        exact lt_of_not_ge hlower
      have hupper' : (c : Real) * (2 : Real) ^
          (R - (exp2.val - 52)) <
          B / (2 : Real) ^ (exp2.val - 52) := by
        rw [hboundary]
        exact lt_of_not_ge hupperNot
      exact (separate c R (by dsimp only [R]; omega)
        (by dsimp only [R]; omega) (by dsimp only [R]; omega)
        hlower' hupper').elim
  refine ⟨d3, hleft53, ?_⟩
  dsimp only
  exact ⟨hcap3, hdigit3,
    ⟨z3, zs3, hzshape3, hz3Lo, hz3Hi, hzlen3, hlast3⟩,
    ⟨eta3, u3, hA3, hB3, heta3, heta3u3, hsticky3⟩,
    hp3Hi, hA_nonneg, hA_u64, hhalf, hodd⟩

end dec2flt_model
