{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableSuperClasses #-}
module Cardano.Ledger.Core.Translation (
PreviousEra,
TranslationContext,
TranslationError,
TranslateEra,
translateEra,
translateEraMaybe,
translateEra',
translateEraThroughCBOR,
)
where
import Cardano.Ledger.Binary
import Cardano.Ledger.Core.Era
import Cardano.Ledger.Genesis
import Control.Monad.Except (Except, runExcept)
import Data.Coerce (Coercible, coerce)
import Data.Kind (Type)
import Data.Text (Text)
import Data.Void (Void, absurd)
type family TranslationContext era :: Type
class (Era era, Era (PreviousEra era)) => TranslateEra era f where
type TranslationError era f :: Type
type TranslationError era f = Void
translateEra ::
TranslationContext era -> f (PreviousEra era) -> Except (TranslationError era f) (f era)
default translateEra ::
(Coercible (f (PreviousEra era)) (f era), TranslationContext era ~ NoGenesis era) =>
TranslationContext era ->
f (PreviousEra era) ->
Except (TranslationError era f) (f era)
translateEra NoGenesis era
TranslationContext era
NoGenesis = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. coerce :: forall a b. Coercible a b => a -> b
coerce
translateEra' ::
(TranslateEra era f, TranslationError era f ~ Void) =>
TranslationContext era ->
f (PreviousEra era) ->
f era
translateEra' :: forall era (f :: * -> *).
(TranslateEra era f, TranslationError era f ~ Void) =>
TranslationContext era -> f (PreviousEra era) -> f era
translateEra' TranslationContext era
ctxt = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. Void -> a
absurd forall a. a -> a
id forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Except e a -> Either e a
runExcept forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era (f :: * -> *).
TranslateEra era f =>
TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
translateEra TranslationContext era
ctxt
translateEraMaybe ::
(TranslateEra era f, TranslationError era f ~ ()) =>
TranslationContext era ->
f (PreviousEra era) ->
Maybe (f era)
translateEraMaybe :: forall era (f :: * -> *).
(TranslateEra era f, TranslationError era f ~ ()) =>
TranslationContext era -> f (PreviousEra era) -> Maybe (f era)
translateEraMaybe TranslationContext era
ctxt =
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e a. Except e a -> Either e a
runExcept forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era (f :: * -> *).
TranslateEra era f =>
TranslationContext era
-> f (PreviousEra era) -> Except (TranslationError era f) (f era)
translateEra TranslationContext era
ctxt
translateEraThroughCBOR ::
forall era ti to.
(Era era, ToCBOR (ti (PreviousEra era)), DecCBOR (Annotator (to era))) =>
Text ->
ti (PreviousEra era) ->
Except DecoderError (to era)
translateEraThroughCBOR :: forall era (ti :: * -> *) (to :: * -> *).
(Era era, ToCBOR (ti (PreviousEra era)),
DecCBOR (Annotator (to era))) =>
Text -> ti (PreviousEra era) -> Except DecoderError (to era)
translateEraThroughCBOR =
forall a b.
(ToCBOR a, DecCBOR (Annotator b)) =>
Version -> Text -> a -> Except DecoderError b
translateViaCBORAnnotator (forall era. Era era => Version
eraProtVerLow @era)