import Tablet.NumberHelperReadEightBytes
import Tablet.NumberHelperEightDigitWordAccepted
import Tablet.NumberHelperParseEightDigitsSafe
import Tablet.num_imp_dec2flt_parse_try_parse_digits

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxHeartbeats 2000000
set_option maxRecDepth 1000000

namespace dec2flt_model

-- [TABLET NODE: ParserExactRoundingDigitScans]
theorem ParserExactRoundingDigitScans :
    (forall s : Slice Std.U8,
      s.val = List.replicate 65535 48#u8 ++
        [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] ->
      exists rest : Slice Std.U8,
        num.imp.dec2flt.parse.try_parse_digits s 0#u64 =
          ok (rest, 1#u64) /\
        rest.val = [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) /\
    (forall s : Slice Std.U8,
      s.val = [48#u8, 46#u8] ++ List.replicate 65535 48#u8 ++
        [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] ->
      exists point : Slice Std.U8,
        num.imp.dec2flt.parse.try_parse_digits s 0#u64 =
          ok (point, 0#u64) /\
        point.val = 46#u8 :: List.replicate 65535 48#u8 ++
          [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) := by
-- BODY
  constructor
  · intro s hs
    let tail : List Std.U8 :=
      [101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]
    have readZeros (u : Slice Std.U8) (r : List Std.U8)
        (hu : u.val = List.replicate 8 48#u8 ++ r) :
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.read_u64 u =
          ok 3472328296227680304#u64 := by
      have hlen : 8 <= u.val.length := by simp [hu]
      obtain ⟨w, hw, hb⟩ := NumberHelperReadEightBytes u hlen
      have h0 := hb 0 (by omega)
      have h1 := hb 1 (by omega)
      have h2 := hb 2 (by omega)
      have h3 := hb 3 (by omega)
      have h4 := hb 4 (by omega)
      have h5 := hb 5 (by omega)
      have h6 := hb 6 (by omega)
      have h7 := hb 7 (by omega)
      simp [hu, List.replicate_succ] at h0 h1 h2 h3 h4 h5 h6 h7
      have hweq : w = 3472328296227680304#u64 := by
        apply UScalar.eq_of_val_eq
        change w.val = 3472328296227680304
        have hwbound := UScalar.hrBounds w
        simp [UScalar.rMax_eq_max, U64.max_eq] at hwbound
        omega
      simpa [hweq] using hw
    have readTerminal (u : Slice Std.U8) (r : List Std.U8)
        (hu : u.val = List.replicate 7 48#u8 ++ 49#u8 :: r) :
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.read_u64 u =
          ok 3544385890265608240#u64 := by
      have hlen : 8 <= u.val.length := by simp [hu]
      obtain ⟨w, hw, hb⟩ := NumberHelperReadEightBytes u hlen
      have h0 := hb 0 (by omega)
      have h1 := hb 1 (by omega)
      have h2 := hb 2 (by omega)
      have h3 := hb 3 (by omega)
      have h4 := hb 4 (by omega)
      have h5 := hb 5 (by omega)
      have h6 := hb 6 (by omega)
      have h7 := hb 7 (by omega)
      simp [hu, List.replicate_succ] at h0 h1 h2 h3 h4 h5 h6 h7
      have hweq : w = 3544385890265608240#u64 := by
        apply UScalar.eq_of_val_eq
        change w.val = 3544385890265608240
        have hwbound := UScalar.hrBounds w
        simp [UScalar.rMax_eq_max, U64.max_eq] at hwbound
        omega
      simpa [hweq] using hw
    have hisZero :
        num.imp.dec2flt.common.is_8digits 3472328296227680304#u64 =
          ok true := by
      apply NumberHelperEightDigitWordAccepted
      intro i hi
      interval_cases i <;> norm_num
    have hisTerminal :
        num.imp.dec2flt.common.is_8digits 3544385890265608240#u64 =
          ok true := by
      apply NumberHelperEightDigitWordAccepted
      intro i hi
      interval_cases i <;> norm_num
    have hparseZero :
        num.imp.dec2flt.parse.parse_8digits 3472328296227680304#u64 =
          ok 0#u64 := by
      unfold num.imp.dec2flt.parse.parse_8digits
      rw [num.imp.dec2flt.parse.parse_8digits.MASK.eq_def,
        num.imp.dec2flt.parse.parse_8digits.MUL1.eq_def,
        num.imp.dec2flt.parse.parse_8digits.MUL2.eq_def]
      rfl
    have hparseTerminal :
        num.imp.dec2flt.parse.parse_8digits 3544385890265608240#u64 =
          ok 1#u64 := by
      unfold num.imp.dec2flt.parse.parse_8digits
      rw [num.imp.dec2flt.parse.parse_8digits.MASK.eq_def,
        num.imp.dec2flt.parse.parse_8digits.MUL1.eq_def,
        num.imp.dec2flt.parse.parse_8digits.MUL2.eq_def]
      rfl
    have hfast : num.imp.dec2flt.parse.try_parse_digits_loop0 s 0#u64
        ⦃ p => p.1.val = tail /\ p.2 = 1#u64 ⦄ := by
      unfold num.imp.dec2flt.parse.try_parse_digits_loop0
      apply Aeneas.Std.loop.spec_decr_nat
        (fun p : Slice Std.U8 × Std.U64 => p.1.val.length)
        (fun p =>
          (exists j : Nat, j <= 8191 /\
            p.1.val = List.replicate (8*j + 7) 48#u8 ++ 49#u8 :: tail /\
            p.2 = 0#u64) \/
          (p.1.val = tail /\ p.2 = 1#u64))
        (fun p => p.1.val = tail /\ p.2 = 1#u64)
        (fun p => num.imp.dec2flt.parse.try_parse_digits_loop0.body p.1 p.2)
        (s, 0#u64)
      · intro p hp
        rcases p with ⟨u, a⟩
        rcases hp with ⟨j, hj, hu, ha⟩ | ⟨hu, ha⟩
        · simp only [Prod.fst, Prod.snd] at hu ha ⊢
          by_cases hj0 : j = 0
          · subst j
            have hu' : u.val = List.replicate 7 48#u8 ++
                49#u8 :: tail := by simpa using hu
            have hread := readTerminal u tail hu'
            simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body,
              Slice.length, ha, hu', hread, hisTerminal, hparseTerminal,
              core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop, WP.spec_ok, Aeneas.Std.lift,
              core.num.U64.wrapping_mul, core.num.U64.wrapping_add]
            constructor
            · right
              rfl
            · omega
          · obtain ⟨k, rfl⟩ := Nat.exists_eq_succ_of_ne_zero hj0
            have hu' : u.val = List.replicate 8 48#u8 ++
                (List.replicate (8*k + 7) 48#u8 ++ 49#u8 :: tail) := by
              rw [hu, show 8 * k.succ + 7 = 8 + (8*k + 7) by omega,
                List.replicate_add, List.append_assoc]
            have hread := readZeros u
              (List.replicate (8*k + 7) 48#u8 ++ 49#u8 :: tail) hu'
            simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body,
              Slice.length, ha, hu', hread, hisZero, hparseZero,
              core.slice.index.SliceIndexRangeFromUsizeSlice.index,
              Slice.drop, WP.spec_ok]
            constructor
            · exact Or.inl ⟨k, by omega, rfl, rfl⟩
            · simp [Slice.length, List.length_append, List.length_replicate]
              omega
        · simp only [Prod.fst, Prod.snd] at hu ha ⊢
          simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body,
            Slice.length, ha, hu, tail, WP.spec_ok]
      · left
        refine ⟨8191, by omega, ?_, rfl⟩
        rw [hs]
    obtain ⟨p, hp, hpost⟩ := Aeneas.Std.WP.spec_imp_exists hfast
    rcases p with ⟨afterFast, fastValue⟩
    rcases hpost with ⟨hafterFast, hfastValue⟩
    simp only [Prod.fst, Prod.snd] at hafterFast hfastValue
    subst fastValue
    have hscalar :
        num.imp.dec2flt.parse.try_parse_digits_loop1 afterFast 1#u64 0#usize =
          ok (1#u64, 0#usize) := by
      unfold num.imp.dec2flt.parse.try_parse_digits_loop1
      rw [Aeneas.Std.loop.eq_1]
      simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body,
        Slice.length, hafterFast, tail, Slice.index_usize,
        core.num.U8.wrapping_sub, Aeneas.Std.lift, U8.size_eq]
    have hdrop : core.slice.index.SliceIndexRangeFromUsizeSlice.index
        { start := 0#usize } afterFast = ok afterFast := by
      simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
        Slice.drop, Slice.length]
    refine ⟨afterFast, ?_, ?_⟩
    · unfold num.imp.dec2flt.parse.try_parse_digits
      simp [hp, hscalar, hdrop]
    · simpa [tail] using hafterFast
  · intro s hs
    let afterPoint : List Std.U8 := List.replicate 65535 48#u8 ++
      [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]
    have hsHead : s.val = 48#u8 :: 46#u8 :: afterPoint := by
      dsimp only [afterPoint]
      rw [hs]
      rfl
    have hafterPrefix : afterPoint = List.replicate 6 48#u8 ++
        (List.replicate 65529 48#u8 ++
          [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) := by
      dsimp only [afterPoint]
      rw [show 65535 = 6 + 65529 by omega, List.replicate_add,
        List.append_assoc]
    have hsPrefix : s.val = 48#u8 :: 46#u8 :: 48#u8 :: 48#u8 ::
        48#u8 :: 48#u8 :: 48#u8 :: 48#u8 ::
        (List.replicate 65529 48#u8 ++
          [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8]) := by
      rw [hsHead, hafterPrefix]
      rfl
    have hread :
        SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.read_u64 s =
          ok 3472328296227679792#u64 := by
      have hlen : 8 <= s.val.length := by
        rw [hsPrefix]
        simp only [List.length_cons]
        omega
      obtain ⟨w, hw, hb⟩ := NumberHelperReadEightBytes s hlen
      have h0 := hb 0 (by omega)
      have h1 := hb 1 (by omega)
      have h2 := hb 2 (by omega)
      have h3 := hb 3 (by omega)
      have h4 := hb 4 (by omega)
      have h5 := hb 5 (by omega)
      have h6 := hb 6 (by omega)
      have h7 := hb 7 (by omega)
      simp only [hsPrefix, List.getElem_cons_zero,
        List.getElem_cons_succ] at h0 h1 h2 h3 h4 h5 h6 h7
      norm_num at h0 h1 h2 h3 h4 h5 h6 h7
      have hweq : w = 3472328296227679792#u64 := by
        apply UScalar.eq_of_val_eq
        change w.val = 3472328296227679792
        have hwbound := UScalar.hrBounds w
        simp [UScalar.rMax_eq_max, U64.max_eq] at hwbound
        omega
      simpa [hweq] using hw
    have hnotDigits :
        num.imp.dec2flt.common.is_8digits 3472328296227679792#u64 =
          ok false := by
      unfold num.imp.dec2flt.common.is_8digits
      rfl
    have hfast :
        num.imp.dec2flt.parse.try_parse_digits_loop0 s 0#u64 =
          ok (s, 0#u64) := by
      have hlen : 8 <= s.val.length := by
        rw [hsPrefix]
        simp only [List.length_cons]
        omega
      unfold num.imp.dec2flt.parse.try_parse_digits_loop0
      rw [Aeneas.Std.loop.eq_1]
      simp [num.imp.dec2flt.parse.try_parse_digits_loop0.body,
        Slice.length, hlen, hread, hnotDigits]
    have hscalar :
        num.imp.dec2flt.parse.try_parse_digits_loop1 s 0#u64 0#usize =
          ok (0#u64, 1#usize) := by
      unfold num.imp.dec2flt.parse.try_parse_digits_loop1
      have hlen : 2 <= s.val.length := by rw [hsHead]; simp
      have hpos : 0 < s.val.length := by omega
      have hidx0 : Slice.index_usize s 0#usize = ok 48#u8 := by
        simp [Slice.index_usize, hsHead]
      have hidx1 : Slice.index_usize s 1#usize = ok 46#u8 := by
        simp [Slice.index_usize, hsHead]
      have hbody0 :
          num.imp.dec2flt.parse.try_parse_digits_loop1.body
            s 0#u64 0#usize = ok (cont (0#u64, 1#usize)) := by
        simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body,
          Slice.length, hpos, hidx0, core.num.U8.wrapping_sub,
          Aeneas.Std.lift, U8.size_eq]
        rw [show 0#usize + 1#usize = ok 1#usize by
          change UScalar.add 0#usize 1#usize = ok 1#usize
          unfold UScalar.add UScalar.tryMk UScalar.tryMkOpt
          rcases System.Platform.numBits_eq with hbits | hbits <;>
            simp [Result.ofOption, UScalar.check_bounds, hbits]]
        rfl
      rw [Aeneas.Std.loop.eq_1]
      simp only [Prod.fst, Prod.snd]
      rw [hbody0]
      simp
      rw [Aeneas.Std.loop.eq_1]
      simp [num.imp.dec2flt.parse.try_parse_digits_loop1.body,
        Slice.length, hlen, hidx1, core.num.U8.wrapping_sub,
        Aeneas.Std.lift, U8.size_eq]
    have hdrop : exists point : Slice Std.U8,
        core.slice.index.SliceIndexRangeFromUsizeSlice.index
          { start := 1#usize } s = ok point /\
          point.val = 46#u8 :: List.replicate 65535 48#u8 ++
            [49#u8, 101#u8, 54#u8, 53#u8, 53#u8, 51#u8, 54#u8, 48#u8] := by
      let point : Slice Std.U8 := ⟨46#u8 :: afterPoint, by
        have hp := s.property
        rw [hsHead] at hp
        exact Nat.le_trans (by simp) hp⟩
      refine ⟨point, ?_, ?_⟩
      · have hdropEq : s.drop 1#usize = point := by
          apply (Slice.eq_iff _ _).2
          simp [Slice.drop, hsHead, point]
        have hone : 1 <= s.val.length := by rw [hsHead]; simp
        simp [core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.length, hdropEq, hone]
      · rfl
    obtain ⟨point, hdrop, hpoint⟩ := hdrop
    refine ⟨point, ?_, hpoint⟩
    unfold num.imp.dec2flt.parse.try_parse_digits
    simp [hfast, hscalar, hdrop]

end dec2flt_model
