import Tablet.DecimalLeftShiftLoop1Execution
import Tablet.DecimalSeqLeftShiftCanonical
import Tablet.P804_T1_LeftLeadingProgress
import Tablet.P804_T1_LeftShiftPointGrowth
import Tablet.P804_T1_PointMinusOneGrowth
import Tablet.P804_T1_PointZeroGrowth
import Tablet.P804_T1_SelectedLeftGrowth
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_DECIMAL_POINT_RANGE
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P804_T1_LeftLoopStep]
theorem P804_T1_LeftLoopStep
    (X Y : Real) (d : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (exp : Std.I32)
    (hpointLo : -324 <= d.decimal_point.val)
    (hpointHi : d.decimal_point.val <= 0)
    (hexpLo : -1260 <= exp.val)
    (hinput :
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun q => q.val)
      d.num_digits.val <= 768 /\
      (forall q, q ∈ d.digits.val.take d.num_digits.val -> q.val < 10) /\
      (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
        digits.length = d.num_digits.val))
    (hend :
      let digits :=
        (d.digits.val.take d.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) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
      exists eta u : Real, X = V + eta /\ Y = V + u /\
        0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) :
    (num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (done (d.num_digits, d.decimal_point, d.truncated,
            d.digits, exp, none)) /\
      d.decimal_point.val = 0 /\
      5 <= d.digits.val[0].val) \/
    exists (d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift growth : Std.Usize) (exp' : Std.I32) (z z' : Nat)
      (zs zs' : List Nat),
      num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 2047#i32 }
        d.num_digits d.decimal_point d.truncated d.digits exp none =
          ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, exp', none)) /\
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' /\
      num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
        d shift = ok growth /\
      shift.val <= 60 /\
      exp'.val = exp.val - (shift.val : Int) /\
      (d.digits.val.take d.num_digits.val).map (fun q => q.val) = z :: zs /\
      (d'.digits.val.take d'.num_digits.val).map (fun q => q.val) = z' :: zs' /\
      1 <= z /\ z < 10 /\ 1 <= z' /\ z' < 10 /\
      (d.decimal_point.val <= -19 ->
        d'.decimal_point.val = d.decimal_point.val + 18 \/
        d'.decimal_point.val = d.decimal_point.val + 19) /\
      (-18 <= d.decimal_point.val -> d.decimal_point.val < 0 ->
        d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0) /\
      (d.decimal_point.val = -1 -> d'.decimal_point.val = -1 -> 7 <= z') /\
      (d.decimal_point.val = -1 -> 7 <= z ->
        d'.decimal_point.val = 0 /\ 5 <= z') /\
      (d.decimal_point.val = 0 -> z < 5) /\
      (d.decimal_point.val = 0 ->
        d'.decimal_point.val = 0 /\
        (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z')) /\
      (let digits' :=
        (d'.digits.val.take d'.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) ^
         (d'.decimal_point.val - (d'.num_digits.val : Int))
       d'.num_digits.val <= 768 /\
       (forall q, q ∈ d'.digits.val.take d'.num_digits.val -> q.val < 10) /\
       digits'.length = d'.num_digits.val /\
       (exists eta' u' : Real,
         (2 : Real) ^ shift.val * X = V' + eta' /\
         (2 : Real) ^ shift.val * Y = V' + u' /\
         0 <= eta' /\ eta' < u' /\
         (d'.truncated = true <-> 0 < eta'))) := by
-- BODY
  dsimp only at hinput hend
  rcases hinput with ⟨hcap, hdigit, z, zs, hshape, hzLo, hzHi, hlen⟩
  have hnumPos : 0 < d.num_digits.val := by
    rw [← hlen]
    rw [hshape]
    simp
  have hnumNe : d.num_digits.val != 0 := by
    simp only [bne_iff_ne]
    omega
  have hlenCons : (z :: zs).length = d.num_digits.val := by
    rw [← hshape]
    exact hlen
  have hindexBound : (0 : Nat) < d.digits.val.length := by
    rw [d.digits.property]
    norm_num
  obtain ⟨head, hread, hreadVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (Array.index_usize_spec d.digits 0#usize (by
      simpa [Array.length] using hindexBound))
  have hgetHead : d.digits.val[0]? = some head :=
    getElem?_eq_some_iff.mpr ⟨hindexBound, hreadVal.symm⟩
  have hheadVal : head.val = z := by
    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 head := by
      rw [List.getElem?_take, if_pos hnumPos]
      exact hgetHead
    simp only [List.getElem?_map, ht] at hh
    simpa using Option.some.inj hh
  have harrayHead : d.digits.val[0].val = z := by
    have hv : d.digits.val[0] = head := by
      exact getElem?_eq_some_iff.mp hgetHead |>.2
    rw [hv, hheadVal]
  let chooseShift (u : Std.U8) : Result Std.Usize :=
    match u with
    | 0#uscalar => ok 2#usize
    | 1#uscalar => ok 2#usize
    | _ => ok 1#usize
  have chooseShift_eq (u : Std.U8) :
      (match u with
        | 0#uscalar => ok 2#usize
        | 1#uscalar => ok 2#usize
        | _ => ok 1#usize) = chooseShift u := by
    rfl
  have shiftRule (u : Std.U8) (w : Nat)
      (hwLo : 1 <= w) (hwHi : w < 5) (hw : u.val = w) :
      chooseShift u = ok (if w = 1 then 2#usize else 1#usize) := by
    have hwCases : w = 1 \/ w = 2 \/ w = 3 \/ w = 4 := by omega
    rcases hwCases with rfl | rfl | rfl | rfl
    · have hu : u = 1#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 2#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 3#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
    · have hu : u = 4#u8 := UScalar.eq_of_val_eq (by simpa using hw)
      subst u
      rfl
  by_cases hexit : d.decimal_point.val = 0 /\ 5 <= z
  · left
    rcases hexit with ⟨hp0, hz5⟩
    refine ⟨?_, hp0, by simpa [harrayHead] using hz5⟩
    unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
    have hple : d.decimal_point <= 0#i32 := by
      change d.decimal_point.val <= 0
      omega
    have hpeq : d.decimal_point = 0#i32 :=
      IScalar.eq_of_val_eq (by simpa using hp0)
    have hge : head >= 5#u8 := by
      change head.val >= 5
      omega
    have hnotlt : ¬ head.val < 5 := by omega
    simp [hpeq, hread, hnotlt]
  · right
    have hcontinue : d.decimal_point.val < 0 \/
        (d.decimal_point.val = 0 /\ z < 5) := by omega
    obtain ⟨shift, growth, hk, hgrowth, hcontrol, hlargeGrowth,
        hsmallGrowth, hzeroGrowth⟩ :
        exists (shift growth : Std.Usize),
          shift.val <= 60 /\
          num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift
            d shift = ok growth /\
          ((d.decimal_point.val < 0 /\
              exists (negp : Std.I32) (n : Std.Usize),
                (-. d.decimal_point) = ok negp /\
                lift (IScalar.hcast .Usize negp) = ok n /\
                n.val = -d.decimal_point.val /\
                num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
                  F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n = ok shift) \/
            (d.decimal_point.val = 0 /\ z < 5 /\
              shift = if z = 1 then 2#usize else 1#usize)) /\
          (d.decimal_point.val <= -19 -> growth.val = 18 \/ growth.val = 19) /\
          (-18 <= d.decimal_point.val -> d.decimal_point.val < 0 ->
            growth.val = (-d.decimal_point.val).toNat - 1 \/
            growth.val = (-d.decimal_point.val).toNat) /\
          (d.decimal_point.val = 0 -> growth.val = 0) := by
      rcases hcontinue with hpneg | ⟨hp0, hz5⟩
      · obtain ⟨negp, hneg, hnegVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.sub_spec (x := (0#i32 : Std.I32))
            (y := d.decimal_point) (by
              rw [IScalar.min_I32_eq]
              norm_num
              omega)
            (by
              rw [IScalar.max_I32_eq]
              norm_num
              omega))
        have hnegCall : (-. d.decimal_point) = ok negp := by
          change IScalar.neg d.decimal_point = ok negp
          unfold IScalar.neg
          change IScalar.sub (0#i32 : Std.I32) d.decimal_point = ok negp at hneg
          unfold IScalar.sub at hneg
          norm_num at hneg ⊢
          exact hneg
        have hnegVal' : negp.val = -d.decimal_point.val := by
          simpa using hnegVal
        obtain ⟨n, hnCall, hnVal⟩ := Aeneas.Std.WP.spec_imp_exists
          (IScalar.hcast_inBounds_spec .Usize negp (by
            rw [hnegVal']
            constructor <;> scalar_tac))
        have hnVal' : n.val = (-d.decimal_point.val).toNat := by
          have hnonneg : 0 <= -d.decimal_point.val := by omega
          have hnInt : (n.val : Int) = -d.decimal_point.val := by
            rw [hnegVal'] at hnVal
            simpa using hnVal
          have ht := congrArg Int.toNat hnInt
          simpa [Int.toNat_of_nonneg hnonneg] using ht
        obtain ⟨shift, growth, hselector, hk, hgrowth, hlarge, hsmall⟩ :=
          P804_T1_SelectedLeftGrowth d n (by rw [hnVal']; omega)
            (by rw [hnVal']; omega) ⟨hcap, hdigit, z, zs, hshape,
              hzLo, hzHi, hlen⟩
        refine ⟨shift, growth, hk, hgrowth, Or.inl ⟨hpneg, negp, n,
          hnegCall, hnCall, ?_, hselector⟩, ?_, ?_, ?_⟩
        · have hnInt : (n.val : Int) = -d.decimal_point.val := by
            rw [hnegVal'] at hnVal
            simpa using hnVal
          exact hnInt
        · intro hp19
          exact hlarge (by rw [hnVal']; omega)
        · intro hp18 hpneg'
          simpa [hnVal'] using hsmall (by rw [hnVal']; omega)
        · omega
      · obtain ⟨growth, hgrowth, hgrowth0⟩ :=
          P804_T1_PointZeroGrowth d z zs hcap hdigit hshape hzLo hz5
        let shift : Std.Usize := if z = 1 then 2#usize else 1#usize
        refine ⟨shift, growth, ?_, hgrowth, Or.inr ⟨hp0, hz5, rfl⟩,
          ?_, ?_, ?_⟩
        · by_cases hz1 : z = 1 <;> simp [shift, hz1]
        · omega
        · omega
        · exact fun _ => hgrowth0
    obtain ⟨d', q, hleft, hq, hqPoint, hp'LoWide, hp'HiWide, hnonzero⟩ :=
      DecimalLeftShiftLoop1Execution d shift hk hpointLo hpointHi
        ⟨hcap, hdigit, z, zs, hshape, hzLo, hzHi, hlenCons⟩
    have hpoint := P804_T1_LeftShiftPointGrowth d d' shift growth hnumNe
      hgrowth (by
        rcases hcontrol with ⟨hp, negp, n, hneg, hn, hnval, hsel⟩ |
          ⟨hp, hz5, hshift⟩
        · by_cases hn19 : 19 <= n.val
          · rcases hlargeGrowth (by omega) with h | h <;> omega
          · rcases hsmallGrowth (by omega) hp with h | h <;> omega
        · rw [hzeroGrowth hp]
          omega) hleft
    have hlargePoint : d.decimal_point.val <= -19 ->
        d'.decimal_point.val = d.decimal_point.val + 18 \/
        d'.decimal_point.val = d.decimal_point.val + 19 := by
      intro hp
      rcases hlargeGrowth hp with h | h <;> rw [hpoint, h] <;> omega
    have hsmallPoint : -18 <= d.decimal_point.val ->
        d.decimal_point.val < 0 ->
        d'.decimal_point.val = -1 \/ d'.decimal_point.val = 0 := by
      intro hp18 hpneg
      rcases hsmallGrowth hp18 hpneg with h | h
      · left
        rw [hpoint, h]
        have : ((-d.decimal_point.val).toNat : Int) =
            -d.decimal_point.val := Int.toNat_of_nonneg (by omega)
        omega
      · right
        rw [hpoint, h]
        have : ((-d.decimal_point.val).toNat : Int) =
            -d.decimal_point.val := Int.toNat_of_nonneg (by omega)
        omega
    have hzeroPoint : d.decimal_point.val = 0 ->
        d'.decimal_point.val = 0 := by
      intro hp0
      rw [hpoint, hzeroGrowth hp0]
      omega
    have hp'Hi : d'.decimal_point.val <= 0 := by
      by_cases hp19 : d.decimal_point.val <= -19
      · rcases hlargePoint hp19 with h | h <;> omega
      · by_cases hpneg : d.decimal_point.val < 0
        · rcases hsmallPoint (by omega) hpneg with h | h <;> omega
        · have hz := hzeroPoint (by omega)
          omega
    have hcanon := DecimalSeqLeftShiftCanonical d d' shift X Y
      ⟨hcap, hdigit, Or.inr ⟨z, zs, hshape, hzLo, hzHi, hlen⟩⟩
      hend hk hleft
    dsimp only at hcanon
    rcases hcanon with ⟨hcap', hdigit', hshapeOut, hend', q', hq', hpq'⟩
    rcases hshapeOut with hzero | hpositive
    · rcases hzero with ⟨hn0, hd0, ht0, hX0⟩
      simp [hn0] at hnonzero
    · rcases hpositive with
        ⟨z', zs', hshape', hz'Lo, hz'Hi, hlen', hlast'⟩
      have hlenCons' : (z' :: zs').length = d'.num_digits.val := by
        rw [← hshape']
        exact hlen'
      have hlead := P804_T1_LeftLeadingProgress d d' shift z z' zs zs'
        hk hshape hzLo hdigit hlenCons hshape' hz'Lo hdigit' hlenCons' hleft
      have hshiftMinusOne : d.decimal_point.val = -1 -> shift.val = 3 := by
        intro hp
        rcases hcontrol with ⟨hpneg, negp, n, hneg, hn, hnval, hsel⟩ |
            ⟨hp0, hz5, hshift⟩
        · have hn1 : n.val = 1 := by omega
          have hs := SlowShiftSelectorCertificate
            F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 n
          dsimp only at hs
          rcases hs with ⟨hsel', hk', hz0', hsmall', hlarge'⟩
          rw [hsel] at hsel'
          injection hsel' with heq
          have heq' : shift = 3#usize := by simpa [hn1] using heq
          simpa [heq']
        · omega
      have hstallLead : d.decimal_point.val = -1 ->
          d'.decimal_point.val = -1 -> 7 <= z' := by
        intro hp hp'
        exact hlead.1 hp hp' (hshiftMinusOne hp)
      have hminusRecovery : d.decimal_point.val = -1 -> 7 <= z ->
          d'.decimal_point.val = 0 /\ 5 <= z' := by
        intro hp hz7
        have hshift3 : shift = 3#usize := by
          apply UScalar.eq_of_val_eq
          simpa using hshiftMinusOne hp
        obtain ⟨growth1, hgrowth1, hgrowth1Val⟩ :=
          P804_T1_PointMinusOneGrowth d z zs hcap hdigit hshape hz7
        rw [hshift3] at hgrowth
        rw [hgrowth] at hgrowth1
        cases hgrowth1
        have hg1 : growth.val = 1 := hgrowth1Val
        have hp'0 : d'.decimal_point.val = 0 := by rw [hpoint, hp, hg1]; omega
        exact ⟨hp'0, hlead.2.2.2 hp hp'0 hz7 (by simpa [hshift3])⟩
      have hzeroProgress : d.decimal_point.val = 0 ->
          d'.decimal_point.val = 0 /\
          (z <= 2 -> 3 <= z') /\ (3 <= z -> 5 <= z') := by
        intro hp0
        have hp'0 := hzeroPoint hp0
        have hshiftCases : (z = 1 /\ shift.val = 2) \/
            (z = 2 /\ shift.val = 1) \/
            (3 <= z /\ shift.val = 1) := by
          rcases hcontrol with hneg | ⟨_, hz5, hshift⟩
          · omega
          · by_cases hz1 : z = 1
            · left
              exact ⟨hz1, by simp [hshift, hz1]⟩
            · by_cases hz2 : z = 2
              · right; left
                exact ⟨hz2, by simp [hshift, hz1]⟩
              · right; right
                exact ⟨by omega, by simp [hshift, hz1]⟩
        refine ⟨hp'0, ?_, ?_⟩
        · intro hz2
          rcases hshiftCases with h | h | h
          · exact hlead.2.1 hp0 hp'0 (Or.inl h)
          · exact hlead.2.1 hp0 hp'0 (Or.inr h)
          · omega
        · intro hz3
          rcases hshiftCases with h | h | h
          · omega
          · omega
          · exact hlead.2.2.1 hp0 hp'0 h.1 h.2
      have hshiftCast : (UScalar.hcast .I32 shift).val =
          (shift.val : Int) := by
        have hs := UScalar.hcast_inBounds_spec .I32 shift (by
          rw [IScalar.max_I32_eq]
          omega)
        simpa [lift, WP.spec_ok] using hs
      let shiftI : Std.I32 := UScalar.hcast .I32 shift
      have hshiftIVal : shiftI.val = (shift.val : Int) := by
        simpa [shiftI] using hshiftCast
      obtain ⟨exp', hexpSub, hexpVal⟩ := Aeneas.Std.WP.spec_imp_exists
        (IScalar.sub_spec (x := exp) (y := shiftI) (by
          rw [hshiftIVal, IScalar.min_I32_eq]
          omega) (by
          rw [hshiftIVal, IScalar.max_I32_eq]
          have := exp.bv.isLt
          scalar_tac))
      have hexpVal' : exp'.val = exp.val - (shift.val : Int) := by
        rw [hexpVal, hshiftIVal]
      have hnotReset : ¬ d'.decimal_point >
          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE := by
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
        change ¬ d'.decimal_point.val > 2047
        omega
      have hnotResetVal : ¬
          num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE.val <
            d'.decimal_point.val := by
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE
        change ¬ (2047 : Int) < d'.decimal_point.val
        omega
      have hzeroInput : d.decimal_point.val = 0 -> z < 5 := by
        intro hp0
        rcases hcontinue with hpneg | hzero
        · omega
        · exact hzero.2
      refine ⟨d', shift, growth, exp', z, z', zs, zs', ?_, hleft,
        hgrowth, hk, hexpVal', hshape, hshape', hzLo, hzHi, hz'Lo,
        hz'Hi, hlargePoint, hsmallPoint, hstallLead, hminusRecovery,
        hzeroInput, hzeroProgress, ?_⟩
      · unfold num.imp.dec2flt.slow.parse_long_mantissa_loop1.body
        have hple : d.decimal_point <= 0#i32 := by
          change d.decimal_point.val <= 0
          omega
        simp only [hple, if_true, core.option.Option.is_none]
        rcases hcontrol with ⟨hpneg, negp, n, hneg, hn, hnval, hsel⟩ |
            ⟨hp0, hz5, hshift⟩
        · have hpne : ¬ d.decimal_point = 0#i32 := by
            intro h
            have := congrArg IScalar.val h
            simp at this
            omega
          simp only [hpne, if_false, hneg, bind_tc_ok,
            Bool.true_eq, if_true]
          rw [hn]
          simp only [bind_tc_ok]
          rw [hsel]
          simp only [bind_tc_ok]
          have hdEta :
              ({ num_digits := d.num_digits,
                 decimal_point := d.decimal_point,
                 truncated := d.truncated,
                 digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
            cases d
            rfl
          rw [hdEta, hleft]
          simp only [bind_tc_ok, hnotReset, if_false, lift]
          rw [show UScalar.hcast .I32 shift = shiftI by rfl]
          simp only [hexpSub, bind_tc_ok]
          simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
            true by rfl, if_true]
        · have hpeq : d.decimal_point = 0#i32 :=
            IScalar.eq_of_val_eq (by simpa using hp0)
          have hlt : ¬ head >= 5#u8 := by
            change ¬ head.val >= 5
            omega
          have hshiftMatch := shiftRule head z hzLo hz5 hheadVal
          rw [← hshift] at hshiftMatch
          have hselector := (chooseShift_eq head).trans hshiftMatch
          simp only [hpeq, if_true, hread, hlt, if_false, bind_tc_ok]
          simp only [show (none : Option num.imp.dec2flt.common.BiasedFp).isNone =
            true by rfl, if_true]
          have hleadVal : d.digits.val[0].val = z := harrayHead
          split
          · change 0 = z at hheadVal
            have hz0 : z = 0 := hheadVal.symm
            omega
          · change 1 = z at hheadVal
            have hz1 : z = 1 := hheadVal.symm
            simp only [hz1, if_true] at hshift hleft
            subst shift
            have hdZeroEta :
                ({ num_digits := d.num_digits,
                   decimal_point := 0#i32,
                   truncated := d.truncated,
                   digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
              rw [← hpeq]
            simp only [bind_tc_ok]
            rw [hdZeroEta, hleft]
            simp only [bind_tc_ok, hnotReset, if_false, lift]
            rw [show UScalar.hcast .I32 2#usize = shiftI by rfl]
            simp only [hexpSub, bind_tc_ok]
          · have hz1 : z ≠ 1 := by
              intro hz
              subst z
              have hu : head = 1#u8 := by
                apply UScalar.eq_of_val_eq
                scalar_tac
              subst head
              simp_all
            simp only [hz1, if_false] at hshift
            subst shift
            have hdZeroEta :
                ({ num_digits := d.num_digits,
                   decimal_point := 0#i32,
                   truncated := d.truncated,
                   digits := d.digits } :
                num.imp.dec2flt.decimal_seq.DecimalSeq) = d := by
              rw [← hpeq]
            simp only [bind_tc_ok]
            rw [hdZeroEta, hleft]
            simp only [bind_tc_ok, hnotReset, if_false, lift]
            rw [show UScalar.hcast .I32 1#usize = shiftI by rfl]
            simp only [hexpSub, bind_tc_ok]
      · exact ⟨hcap', hdigit', hlen', hend'⟩

end dec2flt_model
