import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_round
import Tablet.DecimalGoalVocabularyDefault
import Tablet.DecimalSeqPrimitiveSemantics
import Tablet.NumberHelperDecimalPrefixInterval
import Tablet.DecimalSeqFractionalDigitInterval
import Tablet.DecimalSeqRoundLoopValue

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqRoundArbitraryEndpoint]
theorem DecimalSeqRoundArbitraryEndpoint :
    forall (d : num.imp.dec2flt.decimal_seq.DecimalSeq) (X B : Real),
      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))
      d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      ((d.num_digits.val = 0 /\ digits = [] /\ d.truncated = false /\
          X = 0) \/
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val /\
          digits.getLast? != some 0)) /\
      (exists eta u : Real,
        X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\
        (d.truncated = true <-> 0 < eta)) ->
      d.decimal_point.val < 19 ->
      0 <= X -> X < (2 : Real) ^ (64 : Nat) - (1 / 2 : Real) ->
      (forall n : Nat,
        (n : Real) + (1 / 2 : Real) <= value \/
          B <= (n : Real) + (1 / 2 : Real)) ->
      exists m : Std.U64,
        num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
        X <= (m.val : Real) + (1 / 2 : Real) /\
        (m.val : Real) <= X + (1 / 2 : Real) /\
        (X = (m.val : Real) + (1 / 2 : Real) \/
          X = (m.val : Real) - (1 / 2 : Real) -> m.val % 2 = 0) := by
-- BODY
  dsimp
  intro d X B hshape hp19 hX0 hXbound hsep
  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))
  change d.num_digits.val <= 768 /\
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) /\
      ((d.num_digits.val = 0 /\ digits = [] /\ d.truncated = false /\ X = 0) \/
        (exists z zs, digits = z :: zs /\ 1 <= z /\ z < 10 /\
          digits.length = d.num_digits.val /\ digits.getLast? != some 0)) /\
      (exists eta u : Real, X = value + eta /\ B = value + u /\
        0 <= eta /\ eta < u /\ (d.truncated = true <-> 0 < eta)) at hshape
  change forall n : Nat, (n : Real) + 1 / 2 <= value \/
    B <= (n : Real) + 1 / 2 at hsep
  rcases hshape with ⟨hnum, hdigit, hcanon,
    eta, u, hX, hB, heta0, hetau, hsticky⟩
  simp only [bne_iff_ne] at hcanon
  have hXB : X < B := by rw [hX, hB]; linarith
  have hdigits : forall z, z ∈ digits -> z < 10 := by
    intro z hz
    simp only [digits, List.mem_map] at hz
    rcases hz with ⟨w, hw, rfl⟩
    exact hdigit w hw
  by_cases hzero : d.num_digits.val = 0
  · have hword : d.num_digits = 0#usize :=
      UScalar.eq_of_val_eq (by simpa using hzero)
    have hXeq : X = 0 := by
      rcases hcanon with hz | hp
      · exact hz.2.2.2
      · rcases hp with ⟨z, zs, hds, hz1, hz10, hlen, hlast⟩
        rw [hzero] at hlen
        simp [hds] at hlen
    refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword]
    · norm_num [hXeq]
    · norm_num [hXeq]
    · norm_num [hXeq]
  have hword : d.num_digits ≠ 0#usize := by
    intro he
    apply hzero
    exact congrArg UScalar.val he
  rcases hcanon with hz | hp
  · exact (hzero hz.1).elim
  rcases hp with ⟨z, zs, hdigitsCons, hz1, hz10, hdigitsLen, hlast⟩
  by_cases hpneg : d.decimal_point.val < 0
  · have hpword : d.decimal_point < 0#i32 := by
      simpa [IScalar.lt_equiv] using hpneg
    have hsuffix := NumberHelperDecimalPrefixInterval [] digits hdigits
      (d.decimal_point.val - (digits.length : Int))
    dsimp only at hsuffix
    have hexp : d.decimal_point.val - (digits.length : Int) +
        (digits.length : Int) = d.decimal_point.val := by omega
    have hvaluePow : value < (10 : Real) ^ d.decimal_point.val := by
      rw [hexp] at hsuffix
      rw [hdigitsLen] at hsuffix
      simpa [value, integer] using hsuffix.2
    have hpLe : d.decimal_point.val <= (-1 : Int) := by omega
    have hpowLe : (10 : Real) ^ d.decimal_point.val <= 1 / 10 := by
      calc
        (10 : Real) ^ d.decimal_point.val <= (10 : Real) ^ (-1 : Int) :=
          zpow_le_zpow_right₀ (by norm_num) hpLe
        _ = 1 / 10 := by norm_num [zpow_neg]
    have hvalueHalf : value < 1 / 2 := by linarith
    have hBhalf : B <= 1 / 2 := by
      rcases hsep 0 with h | h
      · norm_num at h
        linarith
      · simpa using h
    have hXhalf : X < 1 / 2 := lt_of_lt_of_le hXB hBhalf
    refine ⟨0#u64, ?_, ?_, ?_, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpword]
    · norm_num
      exact hXhalf.le
    · norm_num
      linarith
    · norm_num
  have hp0 : 0 <= d.decimal_point.val := by omega
  have hpnotword : ¬ d.decimal_point < 0#i32 := by
    simpa [IScalar.lt_equiv] using (not_lt.mpr hp0)
  have hcap : lift (UScalar.hcast .I32
      num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW) =
      ok 19#i32 := by
    have hmaxVal := DecimalSeqPrimitiveSemantics.2.2.1
    have hbound :
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW.val <=
          IScalar.max .I32 := by
      rw [hmaxVal]
      simp [I32.max_eq]
    obtain ⟨i, hi, hival⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.hcast_inBounds_spec .I32
        num.imp.dec2flt.decimal_seq.DecimalSeq.MAX_DIGITS_WITHOUT_OVERFLOW hbound)
    have hieq : i = 19#i32 := by
      apply IScalar.eq_of_val_eq
      rw [hival, hmaxVal]
      norm_num
    subst i
    exact hi
  have hguard : ¬ d.decimal_point >= 19#i32 := by
    simpa [IScalar.le_equiv] using (not_le.mpr hp19)
  have hguardVal : ¬ (19 : Int) <= d.decimal_point.val := not_le.mpr hp19
  have hdpBounds : 0 <= d.decimal_point.val /\
      d.decimal_point.val <= UScalar.max .Usize := by
    constructor
    · exact hp0
    · rcases System.Platform.numBits_eq with hbits | hbits <;>
        simp [UScalar.max, hbits] <;> omega
  obtain ⟨dp, hdpCast0, hdpVal⟩ := Aeneas.Std.WP.spec_imp_exists
    (IScalar.hcast_inBounds_spec .Usize d.decimal_point hdpBounds)
  have hdpCast : lift (IScalar.hcast .Usize d.decimal_point) = ok dp := by
    simpa [lift] using hdpCast0
  have hpdp : d.decimal_point.val = (dp.val : Int) := hdpVal.symm
  have hdp18 : dp.val <= 18 := by omega
  obtain ⟨n, hloop, hnPrefix, hnAfter⟩ :=
    DecimalSeqRoundLoopValue d dp hdp18 hnum hdigit
  by_cases hfrac : dp.val < d.num_digits.val
  · have hnval := hnPrefix hfrac
    have hmath := DecimalSeqFractionalDigitInterval digits dp.val
      (by simpa [hdigitsLen] using hfrac) hdigits
      (by simpa [bne_iff_ne] using hlast)
    dsimp only at hmath
    have hN :
        (digits.take dp.val).foldl (fun a z => 10 * a + z) 0 = n.val :=
      hnval.symm
    have hV : (integer : Real) *
        (10 : Real) ^ ((dp.val : Int) - (digits.length : Int)) = value := by
      simp only [value]
      rw [hpdp, hdigitsLen]
    rw [hN, hV] at hmath
    have hvalueLower : (n.val : Real) <= value := hmath.1
    have hvalueUpper : value < (n.val : Real) + 1 := hmath.2.1
    have hXgeN : (n.val : Real) <= X := by rw [hX]; linarith
    have hnextB : B <= (n.val : Real) + 3 / 2 := by
      rcases hsep (n.val + 1) with h | h
      · norm_num at h
        push_cast at h
        linarith
      · push_cast at h
        norm_num at h ⊢
        linarith
    have hXnext : X < (n.val : Real) + 3 / 2 := lt_of_lt_of_le hXB hnextB
    have hdpWord : dp < d.num_digits := by
      simpa [UScalar.lt_equiv] using hfrac
    have hidx : dp.val < d.digits.val.length := by
      have ha : d.digits.val.length = 768 := by simpa using d.digits.property
      omega
    obtain ⟨digit, hdigitRead, hdigitReadVal⟩ :=
      Aeneas.Std.WP.spec_imp_exists
        (Array.index_usize_spec d.digits dp
          (by simpa [Array.length] using hidx))
    have hdigitVal : digit.val = digits[dp.val] := by
      have hraw : digit = d.digits.val[dp.val]'hidx := hdigitReadVal
      have htake :
          (d.digits.val.take d.num_digits.val)[dp.val]'(by
            simp
            omega) = d.digits.val[dp.val]'hidx := by
        apply List.getElem_take
      simp only [digits, List.getElem_map]
      rw [htake, ← hraw]
    have hdigitLt : digit.val < 10 := by
      rw [hdigitVal]
      exact hdigits _ (List.getElem_mem _)
    have haddBound : dp.val + 1 <= UScalar.max .Usize := by
      rcases System.Platform.numBits_eq with hbits | hbits <;>
        simp [UScalar.max, hbits] <;> omega
    obtain ⟨dp1, hdpAdd, hdp1Val⟩ := Aeneas.Std.WP.spec_imp_exists
      (UScalar.add_spec (x := dp) (y := 1#usize) (by simpa using haddBound))
    have finishDown (hmid : X < (n.val : Real) + 1 / 2)
        (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok n) :
        exists m : Std.U64,
          num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
          X <= (m.val : Real) + 1 / 2 /\
          (m.val : Real) <= X + 1 / 2 /\
          (X = (m.val : Real) + 1 / 2 \/
            X = (m.val : Real) - 1 / 2 -> m.val % 2 = 0) := by
      refine ⟨n, hround, hmid.le, by linarith, ?_⟩
      intro htie
      exfalso
      rcases htie with h | h <;> linarith
    have increment (hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat)) :
        exists n1 : Std.U64, n + 1#u64 = ok n1 /\ n1.val = n.val + 1 := by
      have hnat : n.val + 1 < 2 ^ 64 := by exact_mod_cast hsafe
      have hbound : n.val + 1 <= U64.max := by
        rw [U64.max_eq]
        omega
      obtain ⟨n1, hn1, hn1val⟩ := Aeneas.Std.WP.spec_imp_exists
        (U64.add_spec (x := n) (y := 1#u64) (by simpa using hbound))
      exact ⟨n1, hn1, by simpa using hn1val⟩
    have finishUp (habove : (n.val : Real) + 1 / 2 < X)
        (hcode : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = n + 1#u64) :
        exists m : Std.U64,
          num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
          X <= (m.val : Real) + 1 / 2 /\
          (m.val : Real) <= X + 1 / 2 /\
          (X = (m.val : Real) + 1 / 2 \/
            X = (m.val : Real) - 1 / 2 -> m.val % 2 = 0) := by
      have hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat) := by
        linarith
      obtain ⟨n1, hn1, hn1val⟩ := increment hsafe
      rw [hn1] at hcode
      refine ⟨n1, hcode, ?_, ?_, ?_⟩
      · rw [hn1val]
        push_cast
        linarith
      · rw [hn1val]
        push_cast
        linarith
      · intro htie
        exfalso
        rw [hn1val] at htie
        push_cast at htie
        rcases htie with h | h <;> linarith
    have finishTieDown (htie : X = (n.val : Real) + 1 / 2)
        (heven : n.val % 2 = 0)
        (hround : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok n) :
        exists m : Std.U64,
          num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
          X <= (m.val : Real) + 1 / 2 /\
          (m.val : Real) <= X + 1 / 2 /\
          (X = (m.val : Real) + 1 / 2 \/
            X = (m.val : Real) - 1 / 2 -> m.val % 2 = 0) := by
      refine ⟨n, hround, htie.le, by rw [htie]; linarith, ?_⟩
      exact fun _ => heven
    have finishTieUp (htie : X = (n.val : Real) + 1 / 2)
        (hodd : n.val % 2 = 1)
        (hcode : num.imp.dec2flt.decimal_seq.DecimalSeq.round d = n + 1#u64) :
        exists m : Std.U64,
          num.imp.dec2flt.decimal_seq.DecimalSeq.round d = ok m /\
          X <= (m.val : Real) + 1 / 2 /\
          (m.val : Real) <= X + 1 / 2 /\
          (X = (m.val : Real) + 1 / 2 \/
            X = (m.val : Real) - 1 / 2 -> m.val % 2 = 0) := by
      have hsafe : (n.val : Real) + 1 < (2 : Real) ^ (64 : Nat) := by
        linarith
      obtain ⟨n1, hn1, hn1val⟩ := increment hsafe
      rw [hn1] at hcode
      refine ⟨n1, hcode, ?_, ?_, ?_⟩
      · rw [hn1val, htie]
        push_cast
        linarith
      · rw [hn1val, htie]
        push_cast
        linarith
      · intro _
        rw [hn1val]
        omega
    by_cases hbelow : digit.val < 5
    · have hvalueMid : value < (n.val : Real) + 1 / 2 :=
        hmath.2.2.1 (by simpa [hdigitVal] using hbelow)
      have hBmid : B <= (n.val : Real) + 1 / 2 := by
        rcases hsep n.val with h | h
        · linarith
        · exact h
      have hXmid : X < (n.val : Real) + 1 / 2 := lt_of_lt_of_le hXB hBmid
      apply finishDown hXmid
      have hdigitNe : digit ≠ 5#u8 := by
        intro he
        have := congrArg UScalar.val he
        norm_num at this
        omega
      have hnotge : ¬ digit >= 5#u8 := by
        simpa [UScalar.le_equiv] using (not_le.mpr hbelow)
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
        hfrac, hdigitRead, hdigitNe, hnotge]
      omega
    by_cases habove : 5 < digit.val
    · have hvalueMid : (n.val : Real) + 1 / 2 < value :=
        hmath.2.2.2.1 (by simpa [hdigitVal] using habove)
      have hXmid : (n.val : Real) + 1 / 2 < X := by
        rw [hX]
        linarith
      apply finishUp hXmid
      have hdigitNe : digit ≠ 5#u8 := by
        intro he
        have := congrArg UScalar.val he
        norm_num at this
        omega
      have hge : digit >= 5#u8 := by
        simpa [UScalar.le_equiv] using (by omega : 5 <= digit.val)
      unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
        hfrac, hdigitRead, hdigitNe, hge]
      omega
    · have hdigitVal5 : digit.val = 5 := by omega
      have hdigitEq : digit = 5#u8 :=
        UScalar.eq_of_val_eq (by simpa using hdigitVal5)
      have hfirst5 : digits[dp.val] = 5 := by
        rw [← hdigitVal]
        exact hdigitVal5
      have htieMath := hmath.2.2.2.2 hfirst5
      have hdp1Nat : dp1.val = dp.val + 1 := by
        simpa using hdp1Val
      by_cases hterminal : dp1 = d.num_digits
      · have hterminalNat : dp.val + 1 = digits.length := by
          rw [hdigitsLen, ← hdp1Nat]
          exact congrArg UScalar.val hterminal
        have hvalueTie : value = (n.val : Real) + 1 / 2 :=
          htieMath.1 hterminalNat
        by_cases htrunc : d.truncated = true
        · have hetaPos : 0 < eta := hsticky.mp htrunc
          have hXmid : (n.val : Real) + 1 / 2 < X := by
            rw [hX, hvalueTie]
            linarith
          apply finishUp hXmid
          unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
          simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
            hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc]
        · have hetaNotPos : ¬ 0 < eta := by
            intro he
            exact htrunc (hsticky.mpr he)
          have hetaEq : eta = 0 := by linarith
          have hXtie : X = (n.val : Real) + 1 / 2 := by
            rw [hX, hvalueTie, hetaEq]
            ring
          by_cases hdpZero : dp.val = 0
          · have hdpEq : dp = 0#usize :=
              UScalar.eq_of_val_eq (by simpa using hdpZero)
            subst dp
            apply finishTieDown hXtie
            · rw [hnval]
              simp
            · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hzero, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc]
          · have hdpPos : 0 < dp.val := by omega
            obtain ⟨dpm1, hdpSub, hdpm1Val⟩ := Aeneas.Std.WP.spec_imp_exists
              (UScalar.sub_spec (x := dp) (y := 1#usize)
                (by simpa using hdpPos))
            norm_num at hdpm1Val
            have hdpm1Eq : dpm1.val = dp.val - 1 := hdpm1Val.1
            have hprevIdx : dpm1.val < d.digits.val.length := by
              have ha : d.digits.val.length = 768 := by
                simpa using d.digits.property
              omega
            obtain ⟨prev, hprevRead, hprevReadVal⟩ :=
              Aeneas.Std.WP.spec_imp_exists
                (Array.index_usize_spec d.digits dpm1
                  (by simpa [Array.length] using hprevIdx))
            have hprevVal : prev.val = digits[dp.val - 1] := by
              have hrawVal := congrArg UScalar.val hprevReadVal
              rw [hrawVal]
              simp [digits, List.getElem_map, List.getElem_take, hdpm1Eq]
            let pref := (digits.take (dp.val - 1)).foldl
              (fun a z => 10 * a + z) 0
            have htakeLast : digits.take dp.val =
                digits.take (dp.val - 1) ++ [digits[dp.val - 1]] := by
              conv_lhs => rw [show dp.val = (dp.val - 1) + 1 by omega]
              exact List.take_succ_eq_append_getElem
                (by omega : dp.val - 1 < digits.length)
            have hnLast : n.val = 10 * pref + digits[dp.val - 1] := by
              rw [hnval]
              rw [htakeLast, List.foldl_append]
              simp [pref]
            let bit : Std.U8 := 1#u8 &&& prev
            have hbitLift : lift (1#u8 &&& prev) = ok bit := by
              rfl
            have hbitVal : bit.val = prev.val % 2 := by
              change (BitVec.ofNat 8 1 &&& prev.bv).toNat =
                prev.bv.toNat % 2
              rw [BitVec.toNat_and]
              norm_num [Nat.one_and_eq_mod_two]
            have hnParity : n.val % 2 = prev.val % 2 := by
              rw [hnLast, hprevVal]
              omega
            have hdpNe : dp != 0#usize := by
              simp only [bne_iff_ne]
              intro he
              apply hdpZero
              exact congrArg UScalar.val he
            by_cases heven : n.val % 2 = 0
            · have hbitZeroVal : bit.val = 0 := by
                rw [hbitVal, ← hnParity, heven]
              have hbitZero : bit = 0#u8 :=
                UScalar.eq_of_val_eq (by simpa using hbitZeroVal)
              apply finishTieDown hXtie heven
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc,
                hdpNe, hdpZero, hdpSub, hprevRead, hbitLift, hbitZero]
            · have hodd : n.val % 2 = 1 := by
                have := Nat.mod_lt n.val (by omega : 0 < 2)
                omega
              have hbitOneVal : bit.val = 1 := by
                rw [hbitVal, ← hnParity, hodd]
              have hbitNe : bit != 0#u8 := by
                simp only [bne_iff_ne]
                intro he
                have := congrArg UScalar.val he
                norm_num [hbitOneVal] at this
              apply finishTieUp hXtie hodd
              unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
              simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
                hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal, htrunc,
                hdpNe, hdpZero, hdpSub, hprevRead, hbitLift, hbitNe]
      · have hnotTerminalNat : dp.val + 1 ≠ digits.length := by
          intro he
          apply hterminal
          apply UScalar.eq_of_val_eq
          rw [hdp1Nat]
          simpa [hdigitsLen] using he
        have hnonterminalNat : dp.val + 1 < digits.length := by omega
        have hvalueMid : (n.val : Real) + 1 / 2 < value :=
          htieMath.2 hnonterminalNat
        have hXmid : (n.val : Real) + 1 / 2 < X := by
          rw [hX]
          linarith
        apply finishUp hXmid
        unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
        simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop,
          hfrac, hdigitRead, hdigitEq, hdpAdd, hterminal]
  · have hle : d.num_digits.val <= dp.val := by omega
    have hnval := hnAfter hle
    have hvalueN : value = (n.val : Real) := by
      dsimp only [value, integer]
      rw [hnval, hpdp, ← Nat.cast_sub hle, zpow_natCast]
      push_cast
      ring
    have hXge : (n.val : Real) <= X := by
      rw [hX, hvalueN]
      linarith
    have hBmid : B <= (n.val : Real) + 1 / 2 := by
      rcases hsep n.val with h | h
      · rw [hvalueN] at h
        linarith
      · exact h
    have hXmid : X < (n.val : Real) + 1 / 2 := lt_of_lt_of_le hXB hBmid
    have hdpWord : ¬ dp < d.num_digits := by
      simpa [UScalar.lt_equiv] using hfrac
    refine ⟨n, ?_, hXmid.le, by linarith, ?_⟩
    · unfold num.imp.dec2flt.decimal_seq.DecimalSeq.round
      simp [hword, hpnotword, hcap, hguardVal, hdpCast, hloop, hfrac]
    · intro htie
      exfalso
      rcases htie with h | h <;> linarith

end dec2flt_model
