{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.TypedEncoding.Internal.Types where
import Data.Proxy
import Data.Functor.Identity
import GHC.TypeLits
data Enc enc conf str where
MkEnc :: Proxy enc -> conf -> str -> Enc enc conf str
deriving (Show, Eq)
toEncoding :: conf -> str -> Enc '[] conf str
toEncoding conf str = MkEnc Proxy conf str
fromEncoding :: Enc '[] conf str -> str
fromEncoding = getPayload
implTranF :: Functor f => (str -> f str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implTranF f = implTranF' (\c -> f)
implEncodeF :: (Show err, KnownSymbol x) => Proxy x -> (str -> Either err str) -> Enc enc1 conf str -> Either EncodeEx (Enc enc2 conf str)
implEncodeF p f = implTranF (either (Left . EncodeEx p) Right . f)
implDecodeF :: Functor f => (str -> f str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implDecodeF = implTranF
implCheckPrevF :: Functor f => (str -> f str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implCheckPrevF = implTranF
implTranF' :: Functor f => (conf -> str -> f str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implTranF' f (MkEnc _ conf str) = (MkEnc Proxy conf) <$> f conf str
implEncodeF' :: (Show err, KnownSymbol x) => Proxy x -> (conf -> str -> Either err str) -> Enc enc1 conf str -> Either EncodeEx (Enc enc2 conf str)
implEncodeF' p f = implTranF' (\c -> either (Left . EncodeEx p) Right . f c)
implDecodeF' :: Functor f => (conf -> str -> f str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implDecodeF' = implTranF'
implTranP :: Applicative f => (str -> str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implTranP f = implTranF' (\c -> pure . f)
implEncodeP :: Applicative f => (str -> str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implEncodeP = implTranP
implTranP' :: Applicative f => (conf -> str -> str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implTranP' f = implTranF' (\c -> pure . f c)
implEncodeP' :: Applicative f => (conf -> str -> str) -> Enc enc1 conf str -> f (Enc enc2 conf str)
implEncodeP' = implTranP'
getPayload :: Enc enc conf str -> str
getPayload (MkEnc _ _ str) = str
unsafeSetPayload :: conf -> str -> Enc enc conf str
unsafeSetPayload c str = MkEnc Proxy c str
withUnsafeCoerce :: (s1 -> s2) -> Enc e1 c s1 -> Enc e2 c s2
withUnsafeCoerce f (MkEnc _ conf str) = (MkEnc Proxy conf (f str))
unsafeChangePayload :: (s1 -> s2) -> Enc e c s1 -> Enc e c s2
unsafeChangePayload f (MkEnc p conf str) = (MkEnc p conf (f str))
data RecreateEx where
RecreateEx:: (Show e, KnownSymbol x) => Proxy x -> e -> RecreateEx
instance Show RecreateEx where
show (RecreateEx prxy a) = "(RecreateEx \"" ++ symbolVal prxy ++ "\" (" ++ show a ++ "))"
data EncodeEx where
EncodeEx:: (Show a, KnownSymbol x) => Proxy x -> a -> EncodeEx
instance Show EncodeEx where
show (EncodeEx prxy a) = "(EncodeEx \"" ++ symbolVal prxy ++ "\" (" ++ show a ++ "))"
data UnexpectedDecodeEx where
UnexpectedDecodeEx :: (Show a, KnownSymbol x) => Proxy x -> a -> UnexpectedDecodeEx
instance Show UnexpectedDecodeEx where
show (UnexpectedDecodeEx prxy a) = "(UnexpectedDecodeEx \"" ++ symbolVal prxy ++ "\" (" ++ show a ++ "))"