{-# LANGUAGE GADTs #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
module Data.TypedEncoding.Internal.Types.Enc where
import Data.Proxy
import Data.TypedEncoding.Internal.Class.Util
data Enc enc conf str where
MkEnc :: Proxy enc -> conf -> str -> Enc enc conf str
deriving (Show, Eq)
instance (SymbolList xs, Show c, Displ str) => Displ ( Enc xs c str) where
displ (MkEnc p c s) =
"MkEnc '" ++ displ (Proxy :: Proxy xs) ++ " " ++ show c ++ " " ++ displ s
toEncoding :: conf -> str -> Enc '[] conf str
toEncoding = MkEnc Proxy
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' (const 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
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 = MkEnc Proxy
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)