import Tablet.ParserExponentConstants
import Tablet.num_imp_dec2flt_lemire_compute_float

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option pp.universes false

namespace dec2flt_model

-- [TABLET NODE: ParserComputeFloatInfinityMantissa]
theorem ParserComputeFloatInfinityMantissa (q : Std.I64) (w : Std.U64)
    (b : num.imp.dec2flt.common.BiasedFp)
    (h : num.imp.dec2flt.lemire.compute_float
      F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 q w = ok b) :
    b.p_biased.val = 2047 -> b.m.val = 0 := by
-- BODY
  intro hp
  have bind_ok_inv : ∀ {α β : Type} (r : Result α) (f : α → Result β) y,
      Bind.bind r f = ok y → ∃ x, r = ok x ∧ f x = ok y := by
    intro α β r f y hr
    cases r <;> simp_all [Bind.bind, Aeneas.Std.bind]
  rcases ParserExponentConstants with
    ⟨hbits, htotal, hsig, hexpbits, hexpsat, hinf, hbias, hmin,
      hsmall, hlarge, hroundmin, hroundmax⟩
  have hzero : num.imp.dec2flt.common.BiasedFp.zero_pow2 0#i32 =
      ok { m := 0#u64, p_biased := 0#i32 } := by rfl
  have hinfFp : num.imp.dec2flt.common.BiasedFp.zero_pow2 2047#i32 =
      ok { m := 0#u64, p_biased := 2047#i32 } := by rfl
  have herr : num.imp.dec2flt.common.BiasedFp.zero_pow2 (-1)#i32 =
      ok { m := 0#u64, p_biased := (-1)#i32 } := by rfl
  have hcastSmall : lift (IScalar.cast .I64 (-342)#i32) =
      ok (-342)#i64 := by rfl
  have hcastLarge : lift (IScalar.cast .I64 308#i32) =
      ok 308#i64 := by rfl
  have hsigop : (53#u32 - 1#u32) = ok 52#u32 := by rfl
  unfold num.imp.dec2flt.lemire.compute_float at h
  rw [hzero, hinf] at h
  simp only [bind_tc_ok] at h
  rw [hinfFp, herr] at h
  simp only [bind_tc_ok] at h
  by_cases hw : w = 0#u64
  · simp only [hw, if_true] at h
    cases h
    norm_num at hp
  simp only [hw, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SMALLEST_POWER_OF_TEN.eq_def] at h
  rw [hcastSmall] at h
  simp only [bind_tc_ok] at h
  by_cases hqsmall : q < (-342)#i64
  · simp only [hqsmall, if_true] at h
    cases h
    norm_num at hp
  simp only [hqsmall, if_false] at h
  rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.LARGEST_POWER_OF_TEN.eq_def] at h
  rw [hcastLarge] at h
  simp only [bind_tc_ok] at h
  by_cases hqlarge : q > 308#i64
  · simp only [hqlarge, if_true] at h
    cases h
    rfl
  simp only [hqlarge, if_false] at h
  simp only [lift, bind_tc_ok] at h
  cases hwshift : (w <<< core.num.U64.leading_zeros w) with
  | fail e => simp only [hwshift, bind_tc_fail] at h; contradiction
  | div => simp only [hwshift, bind_tc_div] at h; contradiction
  | ok w1 =>
    simp only [hwshift, bind_tc_ok] at h
    rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.SIG_TOTAL_BITS.eq_def] at h
    simp only [core.f64.F64.MANTISSA_DIGITS, ModelF64MantissaDigits,
      Binary64ModelDefault] at h
    rw [F64.Insts.Dec2flt_modelNumImpTraitsFloatU64.BITS.eq_def] at h
    simp only [bind_tc_ok] at h
    rw [hsigop] at h
    simp only [bind_tc_ok] at h
    obtain ⟨i7, hi7, h⟩ := bind_ok_inv _ _ _ h
    obtain ⟨pair, hpair, h⟩ := bind_ok_inv _ _ _ h
    rcases pair with ⟨lo, hi⟩
    simp only [Aeneas.Std.uncurry_apply_pair] at h
    by_cases hlo : lo = 18446744073709551615#u64
    · simp only [hlo, if_true] at h
      by_cases hqneg : q ≥ (-27)#i64
      · simp only [hqneg, if_true, bind_tc_ok] at h
        by_cases hq55 : q ≤ 55#i64
        · have hbtrue : decide (q ≤ 55#i64) = true := by simpa using hq55
          simp only [hbtrue, if_true, bind_tc_ok] at h
          obtain ⟨i8, hi8, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i12, hi12, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i19, hi19, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h
          by_cases hp2 : power2 ≤ 0#i32
          · simp only [hp2, if_true] at h
            obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
            by_cases hz : i21 ≥ 64#i32
            · simp only [hz, if_true] at h
              cases h
              norm_num at hp
            · simp only [hz, if_false] at h
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
              cases h
              have hc := IScalar.cast_fromBool_bound_eq .I32
                (decide (mantissa3 ≥ i24))
              change (IScalar.cast_fromBool IScalarTy.I32
                (decide (mantissa3 ≥ i24))).val = 2047 at hp
              have hupper : (IScalar.cast_fromBool IScalarTy.I32
                  (decide (mantissa3 ≥ i24))).val ≤ 1 :=
                hc.2.trans (by norm_num)
              exfalso
              omega
          · simp only [hp2, if_false] at h
            obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
            obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
            by_cases hcarry : mantissa3 ≥ i21
            · simp only [hcarry, if_true] at h
              obtain ⟨pair2, hpair2, h⟩ := bind_ok_inv _ _ _ h
              rcases pair2 with ⟨mantissa4, power21⟩
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              obtain ⟨mantissa5, hm5, hpair2⟩ :=
                bind_ok_inv _ _ _ hpair2
              obtain ⟨power22, hp22, hpair2⟩ :=
                bind_ok_inv _ _ _ hpair2
              cases hpair2
              by_cases hsat : power21 ≥ 2047#i32
              · simp only [hsat, if_true] at h
                cases h
                rfl
              · simp only [hsat, if_false] at h
                cases h
                change power21.val = 2047 at hp
                change ¬ 2047 ≤ power21.val at hsat
                omega
            · simp only [hcarry, if_false, bind_tc_ok,
                Aeneas.Std.uncurry_apply_pair] at h
              obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
              by_cases hsat : power2 ≥ 2047#i32
              · simp only [hsat, if_true] at h
                cases h
                rfl
              · simp only [hsat, if_false] at h
                cases h
                change power2.val = 2047 at hp
                change ¬ 2047 ≤ power2.val at hsat
                omega
        · have hbfalse : decide (q ≤ 55#i64) = false := by simpa using hq55
          simp only [hbfalse, if_false, bind_tc_ok] at h
          cases h
          norm_num at hp
      · simp only [hqneg, if_false, bind_tc_ok] at h
        cases h
        norm_num at hp
    · simp only [hlo, if_false] at h
      obtain ⟨i8, hi8, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i9, hi9, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i11, hi11, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i12, hi12, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨mantissa, hmantissa, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i14, hi14, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i15, hi15, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i17, hi17, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i18, hi18, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨i19, hi19, h⟩ := bind_ok_inv _ _ _ h
      obtain ⟨power2, hpower2, h⟩ := bind_ok_inv _ _ _ h
      by_cases hp2 : power2 ≤ 0#i32
      · simp only [hp2, if_true] at h
        obtain ⟨i20, hi20, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
        by_cases hz : i21 ≥ 64#i32
        · simp only [hz, if_true] at h
          cases h
          norm_num at hp
        · simp only [hz, if_false] at h
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨i24, hi24, h⟩ := bind_ok_inv _ _ _ h
          cases h
          have hc := IScalar.cast_fromBool_bound_eq .I32
            (decide (mantissa3 ≥ i24))
          change (IScalar.cast_fromBool IScalarTy.I32
            (decide (mantissa3 ≥ i24))).val = 2047 at hp
          have hupper : (IScalar.cast_fromBool IScalarTy.I32
              (decide (mantissa3 ≥ i24))).val ≤ 1 :=
            hc.2.trans (by norm_num)
          exfalso
          omega
      · simp only [hp2, if_false] at h
        obtain ⟨mantissa1, hm1, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨mantissa2, hm2, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨mantissa3, hm3, h⟩ := bind_ok_inv _ _ _ h
        obtain ⟨i21, hi21, h⟩ := bind_ok_inv _ _ _ h
        by_cases hcarry : mantissa3 ≥ i21
        · simp only [hcarry, if_true] at h
          obtain ⟨pair2, hpair2, h⟩ := bind_ok_inv _ _ _ h
          rcases pair2 with ⟨mantissa4, power21⟩
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          obtain ⟨mantissa5, hm5, hpair2⟩ :=
            bind_ok_inv _ _ _ hpair2
          obtain ⟨power22, hp22, hpair2⟩ :=
            bind_ok_inv _ _ _ hpair2
          cases hpair2
          by_cases hsat : power21 ≥ 2047#i32
          · simp only [hsat, if_true] at h
            cases h
            rfl
          · simp only [hsat, if_false] at h
            cases h
            change power21.val = 2047 at hp
            change ¬ 2047 ≤ power21.val at hsat
            omega
        · simp only [hcarry, if_false, bind_tc_ok,
            Aeneas.Std.uncurry_apply_pair] at h
          obtain ⟨i22, hi22, h⟩ := bind_ok_inv _ _ _ h
          by_cases hsat : power2 ≥ 2047#i32
          · simp only [hsat, if_true] at h
            cases h
            rfl
          · simp only [hsat, if_false] at h
            cases h
            change power2.val = 2047 at hp
            change ¬ 2047 ≤ power2.val at hsat
            omega

end dec2flt_model

