module Ledger.Conway.Foreign.HSLedger.Epoch where

open import Ledger.Conway.Foreign.HSLedger.BaseTypes
open import Ledger.Conway.Foreign.HSLedger.Enact
open import Ledger.Conway.Foreign.HSLedger.Ledger
open import Ledger.Conway.Foreign.HSLedger.PParams
open import Ledger.Conway.Foreign.HSLedger.Ratify
open import Ledger.Conway.Foreign.HSLedger.Rewards

open import Ledger.Conway.Conformance.Equivalence.Convert
open import Ledger.Conway.Conformance.Epoch it it
open import Ledger.Conway.Conformance.Epoch.Properties it it

module EpochSpec where
  open import Ledger.Conway.Specification.Epoch it it public
  open import Ledger.Conway.Specification.Epoch.Properties.Computational it it public

import Data.String as S

instance
  Show-EPOCH :  {eps e eps'}  Show (_ EpochSpec.⊢ eps ⇀⦇ e ,EPOCH⦈ eps')
  Show-EPOCH .show (EpochSpec.EPOCH (s , r , pr)) =
    "EPOCH\n" S.++ "SNAP" S.++ " POOLREAP"

instance
  HsTy-EpochState = autoHsType EpochState  withConstructor "MkEpochState"
                                           fieldPrefix "es"
  Conv-EpochState = autoConvert EpochState

-- An implementation of EPOCH that connects the conformance state
-- with the specification rule.

epoch-step
  : HsType (  EpochState  Epoch  ComputationResult  (EpochState × String))
epoch-step _ epochSt e =
  let r = EpochSpec.Computational-EPOCH .computeProof _ (conv (from epochSt)) e
  in case r of λ where
    (success (s , p))  to (success (conv s , show p))

{-# COMPILE GHC epoch-step as epochStep #-}