import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE
import Tablet.num_imp_dec2flt_decimal_seq_number_of_digits_decimal_left_shift_TABLE_POW5

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false
set_option maxRecDepth 65536

namespace dec2flt_model

-- [TABLET NODE: DecimalLeftShiftTableCertificate]
theorem DecimalLeftShiftTableCertificate :
    forall k : Fin 61,
      let table :=
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE
      let pow5 :=
        num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5
      let x := table.val.get ⟨k.val, by
        have hk := k.isLt
        have hlen : table.val.length = 65 := by
          simp
        omega⟩
      let y := table.val.get ⟨k.val + 1, by
        have hk := k.isLt
        have hlen : table.val.length = 65 := by
          simp
        omega⟩
      let hi := x.val / 2048
      let lo := x.val % 2048
      let hiNext := y.val % 2048
      let ds := (pow5.val.extract lo hiNext).map (fun d => d.val)
      lo <= hiNext /\ hiNext <= 1308 /\
      ds.length = hiNext - lo /\
      (forall d, d ∈ ds -> d < 10) /\
      (k.val = 0 -> hi = 0 /\ ds = []) /\
      (0 < k.val ->
        0 < ds.length /\ 0 < ds.headD 0 /\
        ds.foldl (fun n d => 10 * n + d) 0 = 5 ^ k.val /\
        hi + ds.length = k.val + 1 /\
        10 ^ (hi - 1) < 2 ^ k.val /\ 2 ^ k.val < 10 ^ hi) := by
-- BODY
  simp only [
    num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE.eq_def,
    num.imp.dec2flt.decimal_seq.number_of_digits_decimal_left_shift.TABLE_POW5.eq_def]
  decide +kernel

end dec2flt_model
