import Tablet.P804_T1_RightLoopStep

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: P826_T1_RightBodySelector]
theorem P826_T1_RightBodySelector
    (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
    (e e' : Std.I32)
    (hp : 0 < d.decimal_point.val)
    (hbody :
      num.imp.dec2flt.slow.parse_long_mantissa_loop0.body
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64
        { m := 0#u64, p_biased := 0#i32 }
        d.num_digits d.decimal_point d.truncated d.digits e none =
          ok (cont (d'.num_digits, d'.decimal_point, d'.truncated,
            d'.digits, e', none))) :
    exists (n shift : Std.Usize),
      (n.val : Int) = d.decimal_point.val /\
      num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n = ok shift /\
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' /\
      shift.val <= 60 /\
      e'.val = e.val + (shift.val : Int) := by
-- BODY
  unfold num.imp.dec2flt.slow.parse_long_mantissa_loop0.body at hbody
  have hpw : d.decimal_point > 0#i32 := by
    change d.decimal_point.val > 0
    omega
  simp only [hpw, if_true, core.option.Option.is_none, lift] at hbody
  let n : Std.Usize := IScalar.hcast .Usize d.decimal_point
  rw [show IScalar.hcast .Usize d.decimal_point = n by rfl] at hbody
  simp only [bind_tc_ok] at hbody
  cases hs :
      num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
        F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n with
  | fail err => simp [hs] at hbody
  | div => simp [hs] at hbody
  | ok shift =>
    rw [hs] at hbody
    simp only [bind_tc_ok] at hbody
    cases hr : num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift with
    | fail err => simp [hr] at hbody
    | div => simp [hr] at hbody
    | ok q =>
      rw [hr] at hbody
      simp only [bind_tc_ok] at hbody
      cases hneg : -. num.imp.dec2flt.decimal_seq.DecimalSeq.DECIMAL_POINT_RANGE with
      | fail err => simp [hneg] at hbody
      | div => simp [hneg] at hbody
      | ok negRange =>
        rw [hneg] at hbody
        simp only [bind_tc_ok] at hbody
        by_cases hreset : q.decimal_point < negRange
        · simp [hreset] at hbody
        · simp only [hreset, if_false] at hbody
          let shiftI : Std.I32 := UScalar.hcast .I32 shift
          rw [show UScalar.hcast .I32 shift = shiftI by rfl] at hbody
          cases ha : e + shiftI with
          | fail err => simp [ha] at hbody
          | div => simp [ha] at hbody
          | ok eout =>
            rw [ha] at hbody
            simp only [bind_tc_ok] at hbody
            have hstate := Result.ok.inj hbody
            simp only [ControlFlow.cont.injEq, Prod.mk.injEq] at hstate
            have hq : q = d' := by
              cases q
              cases d'
              simp only [num.imp.dec2flt.decimal_seq.DecimalSeq.mk.injEq]
              tauto
            have heq : eout = e' := by tauto
            subst q
            subst eout
            have hnval := IScalar.hcast_inBounds_spec .Usize d.decimal_point
              (by scalar_tac)
            simp only [lift, WP.spec_ok] at hnval
            have hcert := SlowShiftSelectorCertificate
              F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 n
            let k : Std.Usize := match n.val with
              | 0 => 0#usize | 1 => 3#usize | 2 => 6#usize
              | 3 => 9#usize | 4 => 13#usize | 5 => 16#usize
              | 6 => 19#usize | 7 => 23#usize | 8 => 26#usize
              | 9 => 29#usize | 10 => 33#usize | 11 => 36#usize
              | 12 => 39#usize | 13 => 43#usize | 14 => 46#usize
              | 15 => 49#usize | 16 => 53#usize | 17 => 56#usize
              | 18 => 59#usize | _ => 60#usize
            change
              num.imp.dec2flt.slow.parse_long_mantissa.closure.Insts.CoreOpsFunctionFnTupleUsizeUsize.call
                  F64.Insts.Dec2flt_modelNumImpTraitsFloatU64 () n = ok k /\
                k.val <= 60 /\ _ at hcert
            have hk : k = shift := by
              exact Result.ok.inj (hcert.1.symm.trans hs)
            rw [hk] at hcert
            have hshift60 := hcert.2.1
            have hcval := UScalar.hcast_inBounds_spec .I32 shift
              (by rw [IScalar.max_I32_eq]; omega)
            simp only [lift, WP.spec_ok] at hcval
            have haval := IScalar.add_equiv e shiftI
            rw [ha] at haval
            refine ⟨n, shift, ?_, hs, hr, hshift60, ?_⟩
            · simpa using hnval
            · rw [haval.2.1]
              simpa using hcval

end dec2flt_model
