import Tablet.DecimalGoalVocabularyDefault
import Tablet.F64_Insts_Dec2flt_modelNumImpDec2fltLemireU64
import Tablet.F64_Insts_CoreOpsArithDivF64F64
import Tablet.F64_Insts_CoreOpsArithMulF64F64
import Tablet.F64_Insts_CoreOpsArithNegF64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatExtU64
import Tablet.F64_Insts_Dec2flt_modelNumImpTraitsFloatU64
import Tablet.ModelSliceFirst
import Tablet.ModelStrAsBytes
import Tablet.NumberHelperRejectionSafeLength
import Tablet.SourceValidByteSlice
import Tablet.SpecialHelperNan
import Tablet.num_imp_dec2flt_Dec2FltTrace
import Tablet.num_imp_dec2flt_Lemire
import Tablet.num_imp_dec2flt_dec2flt
import Tablet.num_imp_dec2flt_dec2flt_with_trace
import Tablet.num_imp_dec2flt_parse_parse_number
import Tablet.num_imp_dec2flt_parse_parse_inf_nan
import Tablet.num_imp_dec2flt_decimal_Decimal_try_fast_path
import Tablet.num_imp_dec2flt_lemire_compute_float
import Tablet.num_imp_dec2flt_slow_parse_long_mantissa
import Tablet.num_imp_dec2flt_biased_fp_to_float
import Tablet.num_imp_traits_FloatExt

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: ParserNanAcceptance]
theorem ParserNanAcceptance :
    ∀ (s : Str) (sign : Option Bool), DecimalGoalVocabularyDefault.sourceString s ->
      DecimalGoalVocabularyDefault.specialLiteral (DecimalGoalVocabularyDefault.bytes s) sign true -> ∃ f : F64,
      num.imp.dec2flt.dec2flt F64.Insts.Dec2flt_modelNumImpDec2fltLemireU64
        F64.Insts.CoreOpsArithDivF64F64 F64.Insts.CoreOpsArithMulF64F64
        F64.Insts.CoreOpsArithNegF64 s = ok (core.result.Result.Ok f) ∧ DecimalGoalVocabularyDefault.isNan f := by
-- BODY
  intro s sign source_ok special_ok
  dsimp [DecimalGoalVocabularyDefault] at source_ok special_ok
  rcases special_ok with ⟨body, bytes_shape, body_case⟩
  have body_length : body.length = 3 := by
    exact List.Forall₂.length_eq body_case
  cases sign with
  | none =>
    have source_shape : s.val.map (fun b : Std.U8 => b.val) = body := by
      simpa using bytes_shape
    rcases s with ⟨vals, bound⟩
    obtain ⟨a, b, c, vals_shape⟩ := (List.length_eq_three.mp (by
      rw [← List.length_map (fun b : Std.U8 => b.val), source_shape]
      exact body_length))
    dsimp at vals_shape
    subst vals
    have body_case' : List.Forall₂ (fun actual lower => actual = lower ∨ actual + 32 = lower)
        [a.val, b.val, c.val] [110, 97, 110] := by
      have copied := body_case
      rw [← source_shape] at copied
      simpa using copied
    have value_identity (x : Std.U8) : x = @UScalar.ofNatCore .U8 x.val x.bv.isLt := by
      apply UScalar.eq_of_val_eq
      simp
    cases body_case' with
    | cons ha rest =>
      have first_cases : a.val = 110 ∨ a.val = 78 := by
        rcases ha with ha | ha
        · exact Or.inl ha
        · right
          omega
      have a_not45 : a ≠ 45#u8 := by
        intro equality
        have value_equality := congrArg (fun x : Std.U8 => x.val) equality
        simp at value_equality
        omega
      have a_not43 : a ≠ 43#u8 := by
        intro equality
        have value_equality := congrArg (fun x : Std.U8 => x.val) equality
        simp at value_equality
        omega
      have slice_valid : SourceValidByteSlice (⟨[a, b, c], bound⟩ : Slice Std.U8) := by
        simp [SourceValidByteSlice] at source_ok ⊢
      have no_numeric : Not (∃ x : DecimalLexeme,
          x.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric x ∧
          DecimalGoalVocabularyDefault.bytes (⟨[a, b, c], bound⟩ : Slice Std.U8) =
            DecimalGoalVocabularyDefault.renderNumeric x) := by
        intro witness
        rcases witness with ⟨x, sign_shape, valid_shape, render_shape⟩
        have direct_shape : [a.val, b.val, c.val] =
            (DecimalGoalVocabularyDefault.renderNumeric x) := by
          dsimp [DecimalGoalVocabularyDefault] at render_shape ⊢
          simpa using render_shape
        dsimp [DecimalGoalVocabularyDefault] at valid_shape
        cases digits : x.integerDigits with
        | nil =>
          have nonempty_fraction := valid_shape.2.2.1
          simp [digits] at nonempty_fraction
          cases fraction : x.fractionDigits with
          | none => simp [fraction] at nonempty_fraction
          | some ds =>
            have head_shape := congrArg List.head? direct_shape
            simp [DecimalGoalVocabularyDefault, sign_shape, digits, fraction] at head_shape
            omega
        | cons d ds =>
          have digit_bound := valid_shape.1 d (by simp [digits])
          have head_shape := congrArg List.head? direct_shape
          simp [DecimalGoalVocabularyDefault, sign_shape, digits] at head_shape
          omega
      have number_result := NumberHelperRejectionSafeLength
        (⟨[a, b, c], bound⟩ : Slice Std.U8) slice_valid (by simp) (by norm_num) no_numeric
      have special_body : DecimalGoalVocabularyDefault.specialLiteral
          (DecimalGoalVocabularyDefault.bytes (⟨[a, b, c], bound⟩ : Slice Std.U8)) none true := by
        dsimp [DecimalGoalVocabularyDefault]
        refine ⟨body, ?_, body_case⟩
        simpa using source_shape
      obtain ⟨f, special_result, nan_result⟩ := SpecialHelperNan
        (⟨[a, b, c], bound⟩ : Slice Std.U8) false special_body
      refine ⟨f, ?_, nan_result⟩
      simp [num.imp.dec2flt.dec2flt, num.imp.dec2flt.dec2flt_with_trace,
        core.str.Str.as_bytes, core.slice.Slice.first, ModelStrAsBytes,
        ModelSliceFirst, number_result, special_result,
        a_not45, a_not43]
  | some negative =>
    cases negative with
    | false =>
      have source_length : s.val.length = 4 := by
        rw [← List.length_map (fun b : Std.U8 => b.val), bytes_shape]
        simp [body_length]
      rcases s with ⟨vals, bound⟩
      obtain ⟨p, a, b, c, vals_shape⟩ :=
        (List.length_eq_four.mp (by simpa using source_length))
      subst vals
      have source_shape : [p.val, a.val, b.val, c.val] = 43 :: body := by
        simpa using bytes_shape
      have tail_shape : [a.val, b.val, c.val] = body := by
        have copied := congrArg List.tail source_shape
        simpa using copied
      have prefix_value : p.val = 43 := by
        have copied := congrArg List.head? source_shape
        simpa using copied
      have prefix_identity : p = 43#u8 := by
        apply UScalar.eq_of_val_eq
        simpa using prefix_value
      have body_case' : List.Forall₂ (fun actual lower => actual = lower ∨ actual + 32 = lower)
          [a.val, b.val, c.val] [110, 97, 110] := by
        have copied := body_case
        rw [← tail_shape] at copied
        simpa using copied
      cases body_case' with
      | cons ha rest =>
        have first_cases : a.val = 110 ∨ a.val = 78 := by
          rcases ha with ha | ha
          · exact Or.inl ha
          · right
            omega
        have a_not45 : a ≠ 45#u8 := by
          intro equality
          have value_equality := congrArg (fun x : Std.U8 => x.val) equality
          simp at value_equality
          omega
        have a_not43 : a ≠ 43#u8 := by
          intro equality
          have value_equality := congrArg (fun x : Std.U8 => x.val) equality
          simp at value_equality
          omega
        have tail_bound : [a, b, c].length ≤ Usize.max := by scalar_tac
        have slice_valid : SourceValidByteSlice (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) := by
          simp [SourceValidByteSlice]
        have no_numeric : Not (∃ x : DecimalLexeme,
            x.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric x ∧
            DecimalGoalVocabularyDefault.bytes (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) =
              DecimalGoalVocabularyDefault.renderNumeric x) := by
          intro witness
          rcases witness with ⟨x, sign_shape, valid_shape, render_shape⟩
          have direct_shape : [a.val, b.val, c.val] =
              (DecimalGoalVocabularyDefault.renderNumeric x) := by
            dsimp [DecimalGoalVocabularyDefault] at render_shape ⊢
            simpa using render_shape
          dsimp [DecimalGoalVocabularyDefault] at valid_shape
          cases digits : x.integerDigits with
          | nil =>
            have nonempty_fraction := valid_shape.2.2.1
            simp [digits] at nonempty_fraction
            cases fraction : x.fractionDigits with
            | none => simp [fraction] at nonempty_fraction
            | some ds =>
              have head_shape := congrArg List.head? direct_shape
              simp [DecimalGoalVocabularyDefault, sign_shape, digits, fraction] at head_shape
              omega
          | cons d ds =>
            have digit_bound := valid_shape.1 d (by simp [digits])
            have head_shape := congrArg List.head? direct_shape
            simp [DecimalGoalVocabularyDefault, sign_shape, digits] at head_shape
            omega
        have number_result := NumberHelperRejectionSafeLength
          (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) slice_valid (by simp) (by norm_num) no_numeric
        have special_body : DecimalGoalVocabularyDefault.specialLiteral
            (DecimalGoalVocabularyDefault.bytes (⟨[a, b, c], tail_bound⟩ : Slice Std.U8)) none true := by
          dsimp [DecimalGoalVocabularyDefault]
          refine ⟨body, ?_, body_case⟩
          simpa using tail_shape
        obtain ⟨f, special_result, nan_result⟩ := SpecialHelperNan
          (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) false special_body
        refine ⟨f, ?_, nan_result⟩
        simp [num.imp.dec2flt.dec2flt, num.imp.dec2flt.dec2flt_with_trace,
          core.str.Str.as_bytes, core.slice.Slice.first, ModelStrAsBytes,
          ModelSliceFirst, core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, number_result, special_result, a_not45, a_not43,
          prefix_identity]
    | true =>
      have source_length : s.val.length = 4 := by
        rw [← List.length_map (fun b : Std.U8 => b.val), bytes_shape]
        simp [body_length]
      rcases s with ⟨vals, bound⟩
      obtain ⟨p, a, b, c, vals_shape⟩ :=
        (List.length_eq_four.mp (by simpa using source_length))
      subst vals
      have source_shape : [p.val, a.val, b.val, c.val] = 45 :: body := by
        simpa using bytes_shape
      have tail_shape : [a.val, b.val, c.val] = body := by
        have copied := congrArg List.tail source_shape
        simpa using copied
      have prefix_value : p.val = 45 := by
        have copied := congrArg List.head? source_shape
        simpa using copied
      have prefix_identity : p = 45#u8 := by
        apply UScalar.eq_of_val_eq
        simpa using prefix_value
      have body_case' : List.Forall₂ (fun actual lower => actual = lower ∨ actual + 32 = lower)
          [a.val, b.val, c.val] [110, 97, 110] := by
        have copied := body_case
        rw [← tail_shape] at copied
        simpa using copied
      cases body_case' with
      | cons ha rest =>
        have first_cases : a.val = 110 ∨ a.val = 78 := by
          rcases ha with ha | ha
          · exact Or.inl ha
          · right
            omega
        have a_not45 : a ≠ 45#u8 := by
          intro equality
          have value_equality := congrArg (fun x : Std.U8 => x.val) equality
          simp at value_equality
          omega
        have a_not43 : a ≠ 43#u8 := by
          intro equality
          have value_equality := congrArg (fun x : Std.U8 => x.val) equality
          simp at value_equality
          omega
        have tail_bound : [a, b, c].length ≤ Usize.max := by scalar_tac
        have slice_valid : SourceValidByteSlice (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) := by
          simp [SourceValidByteSlice]
        have no_numeric : Not (∃ x : DecimalLexeme,
            x.leadingSign = none ∧ DecimalGoalVocabularyDefault.validNumeric x ∧
            DecimalGoalVocabularyDefault.bytes (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) =
              DecimalGoalVocabularyDefault.renderNumeric x) := by
          intro witness
          rcases witness with ⟨x, sign_shape, valid_shape, render_shape⟩
          have direct_shape : [a.val, b.val, c.val] =
              (DecimalGoalVocabularyDefault.renderNumeric x) := by
            dsimp [DecimalGoalVocabularyDefault] at render_shape ⊢
            simpa using render_shape
          dsimp [DecimalGoalVocabularyDefault] at valid_shape
          cases digits : x.integerDigits with
          | nil =>
            have nonempty_fraction := valid_shape.2.2.1
            simp [digits] at nonempty_fraction
            cases fraction : x.fractionDigits with
            | none => simp [fraction] at nonempty_fraction
            | some ds =>
              have head_shape := congrArg List.head? direct_shape
              simp [DecimalGoalVocabularyDefault, sign_shape, digits, fraction] at head_shape
              omega
          | cons d ds =>
            have digit_bound := valid_shape.1 d (by simp [digits])
            have head_shape := congrArg List.head? direct_shape
            simp [DecimalGoalVocabularyDefault, sign_shape, digits] at head_shape
            omega
        have number_result := NumberHelperRejectionSafeLength
          (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) slice_valid (by simp) (by norm_num) no_numeric
        have special_body : DecimalGoalVocabularyDefault.specialLiteral
            (DecimalGoalVocabularyDefault.bytes (⟨[a, b, c], tail_bound⟩ : Slice Std.U8)) none true := by
          dsimp [DecimalGoalVocabularyDefault]
          refine ⟨body, ?_, body_case⟩
          simpa using tail_shape
        obtain ⟨f, special_result, nan_result⟩ := SpecialHelperNan
          (⟨[a, b, c], tail_bound⟩ : Slice Std.U8) true special_body
        refine ⟨f, ?_, nan_result⟩
        simp [num.imp.dec2flt.dec2flt, num.imp.dec2flt.dec2flt_with_trace,
          core.str.Str.as_bytes, core.slice.Slice.first, ModelStrAsBytes,
          ModelSliceFirst, core.slice.index.SliceIndexRangeFromUsizeSlice.index,
          Slice.drop, number_result, special_result, a_not45, a_not43,
          prefix_identity]

end dec2flt_model
