import Tablet.DecimalLeftShiftDigitGrowth
import Tablet.DecimalSeqLeftShiftRecordSemantics
import Tablet.DecimalSeqRightShiftRecordSemantics
import Tablet.DecimalSeqTrimSemantics
import Tablet.DecimalGoalVocabularyDefault
import Tablet.PositiveScaleErrorTransport
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_Insts_CoreDefaultDefault_default
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_left_shift
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_right_shift
import Tablet.num_imp_dec2flt_decimal_seq_DecimalSeq_trim

open Aeneas Aeneas.Std Result ControlFlow Error
set_option autoImplicit false

namespace dec2flt_model

-- [TABLET NODE: DecimalSeqDyadicShiftSemantics]
theorem DecimalSeqDyadicShiftSemantics :
    let value := fun d : num.imp.dec2flt.decimal_seq.DecimalSeq =>
      let digits :=
        (d.digits.val.take d.num_digits.val).map (fun z => z.val)
      let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
      (integer : Real) * (10 : Real) ^
        (d.decimal_point.val - (d.num_digits.val : Int))
    (forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (X eta u : Real),
      shift.val <= 60 ->
      0 < value d ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.left_shift d shift = ok d' ->
      X = value d + eta -> 0 <= eta -> eta < u ->
      (d.truncated = true <-> 0 < eta) ->
      exists eta' u' : Real,
        (2 : Real) ^ shift.val * X = value d' + eta' /\
        0 <= eta' /\ eta' < u' /\
        (d'.truncated = true <-> 0 < eta') /\
        value d' + u' = (2 : Real) ^ shift.val * (value d + u) /\
        (forall H : Real,
          (X < H <-> (2 : Real) ^ shift.val * X <
            (2 : Real) ^ shift.val * H) /\
          (X = H <-> (2 : Real) ^ shift.val * X =
            (2 : Real) ^ shift.val * H) /\
          (H < X <-> (2 : Real) ^ shift.val * H <
            (2 : Real) ^ shift.val * X))) /\
    (forall (d d' : num.imp.dec2flt.decimal_seq.DecimalSeq)
      (shift : Std.Usize) (X eta u : Real),
      shift.val <= 60 ->
      0 < value d ->
      (forall z, z ∈ d.digits.val.take d.num_digits.val -> z.val < 10) ->
      num.imp.dec2flt.decimal_seq.DecimalSeq.right_shift d shift = ok d' ->
      X = value d + eta -> 0 <= eta -> eta < u ->
      (d.truncated = true <-> 0 < eta) ->
      (d'.num_digits.val = 0 /\ d'.decimal_point.val = 0 /\
        d'.truncated = false) \/
      exists eta' u' : Real,
        X / (2 : Real) ^ shift.val = value d' + eta' /\
        0 <= eta' /\ eta' < u' /\
        (d'.truncated = true <-> 0 < eta') /\
        value d' + u' = (value d + u) / (2 : Real) ^ shift.val /\
        (forall H : Real,
          (X < H <-> X / (2 : Real) ^ shift.val <
            H / (2 : Real) ^ shift.val) /\
          (X = H <-> X / (2 : Real) ^ shift.val =
            H / (2 : Real) ^ shift.val) /\
          (H < X <-> H / (2 : Real) ^ shift.val <
            X / (2 : Real) ^ shift.val))) := by
-- BODY
  dsimp only
  constructor
  · intro d d' shift X eta u hshift hvalue hdigit hcall hX heta0 hetau
      htruncated
    obtain ⟨rho, hrho0, _, hrecord, hsticky⟩ :=
      DecimalSeqLeftShiftRecordSemantics d d' shift hshift hvalue hdigit hcall
    let eta' := rho + (2 : Real) ^ shift.val * eta
    let u' := rho + (2 : Real) ^ shift.val * u
    have hscale : 0 < (2 : Real) ^ shift.val := pow_pos (by norm_num) _
    have htransport := PositiveScaleErrorTransport
      ((2 : Real) ^ shift.val) X _ _ eta u rho hscale hX hrecord
        heta0 hetau hrho0
    dsimp only at htransport
    rcases htransport with
      ⟨hscaled, heta'0, heta'u', hendpoint, heta'pos, hcomparison⟩
    refine ⟨eta', u', ?_, ?_, ?_, ?_, ?_, ?_⟩
    · simpa [eta'] using hscaled
    · simpa [eta'] using heta'0
    · simpa [eta', u'] using heta'u'
    · calc
        d'.truncated = true ↔ d.truncated = true ∨ 0 < rho := hsticky
        _ ↔ 0 < eta ∨ 0 < rho := by rw [htruncated]
        _ ↔ 0 < eta' := by simpa [eta', or_comm] using heta'pos.symm
    · simpa [u'] using hendpoint
    · exact hcomparison
  · intro d d' shift X eta u hshift hvalue hdigit hcall hX heta0 hetau
      htruncated
    rcases DecimalSeqRightShiftRecordSemantics d d' shift hshift hvalue
        hdigit hcall with hreset | ⟨rho, hrho0, _, hrecord, hsticky⟩
    · exact Or.inl hreset
    · right
      let Vfun := fun e : num.imp.dec2flt.decimal_seq.DecimalSeq =>
        let digits :=
          (e.digits.val.take e.num_digits.val).map (fun z => z.val)
        let integer : Nat := digits.foldl (fun a z => 10 * a + z) 0
        (integer : Real) * (10 : Real) ^
          (e.decimal_point.val - (e.num_digits.val : Int))
      let s : Real := 1 / (2 : Real) ^ shift.val
      let eta' := rho + s * eta
      let u' := rho + s * u
      have hpow : 0 < (2 : Real) ^ shift.val := pow_pos (by norm_num) _
      have hscale : 0 < s := by
        dsimp only [s]
        positivity
      have hX' : X = Vfun d + eta := by simpa [Vfun] using hX
      have hrecord' : Vfun d' = s * Vfun d - rho := by
        simpa [Vfun, s, div_eq_mul_inv, mul_comm] using hrecord
      have htransport := PositiveScaleErrorTransport
        s X (Vfun d) (Vfun d') eta u rho hscale hX' hrecord' heta0 hetau
          hrho0
      dsimp only at htransport
      rcases htransport with
        ⟨hscaled, heta'0, heta'u', hendpoint, heta'pos, hcomparison⟩
      refine ⟨eta', u', ?_, ?_, ?_, ?_, ?_, ?_⟩
      · simpa [Vfun, eta', s, div_eq_mul_inv, mul_comm] using hscaled
      · simpa [eta'] using heta'0
      · simpa [eta', u'] using heta'u'
      · calc
          d'.truncated = true ↔ d.truncated = true ∨ 0 < rho := hsticky
          _ ↔ 0 < eta ∨ 0 < rho := by rw [htruncated]
          _ ↔ 0 < eta' := by simpa [eta', or_comm] using heta'pos.symm
      · simpa [Vfun, u', s, div_eq_mul_inv, mul_comm] using hendpoint
      · simpa [s, div_eq_mul_inv, mul_comm] using hcomparison

end dec2flt_model
