{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE RankNTypes #-}
module Data.TypedEncoding.Common.Types.CheckedEnc where
import Data.TypedEncoding.Common.Types.Enc
import Data.TypedEncoding.Common.Types.Common
import Data.TypedEncoding.Common.Class.Util
import Data.Proxy
data CheckedEnc conf str = UnsafeMkCheckedEnc [EncAnn] conf str
deriving (Show, Eq)
unsafeCheckedEnc:: [EncAnn] -> c -> s -> CheckedEnc c s
unsafeCheckedEnc = UnsafeMkCheckedEnc
getCheckedPayload :: CheckedEnc conf str -> str
getCheckedPayload = snd . getCheckedEncPayload
getCheckedEncPayload :: CheckedEnc conf str -> ([EncAnn], str)
getCheckedEncPayload (UnsafeMkCheckedEnc t _ s) = (t,s)
toCheckedEnc :: forall xs c str . (SymbolList xs) => Enc xs c str -> CheckedEnc c str
toCheckedEnc (UnsafeMkEnc p c s) =
UnsafeMkCheckedEnc (symbolVals @ xs) c s
fromCheckedEnc :: forall xs c str . SymbolList xs => CheckedEnc c str -> Maybe (Enc xs c str)
fromCheckedEnc (UnsafeMkCheckedEnc xs c s) =
let p = Proxy :: Proxy xs
in if symbolVals @ xs == xs
then Just $ UnsafeMkEnc p c s
else Nothing
proc_toCheckedEncFromCheckedEnc :: forall xs c str . (SymbolList xs, Eq c, Eq str) => CheckedEnc c str -> Bool
proc_toCheckedEncFromCheckedEnc x = (== Just x) . fmap (toCheckedEnc @ xs) . fromCheckedEnc $ x
proc_fromCheckedEncToCheckedEnc :: forall xs c str . (SymbolList xs, Eq c, Eq str) => Enc xs c str -> Bool
proc_fromCheckedEncToCheckedEnc x = (== Just x) . fromCheckedEnc . toCheckedEnc $ x
instance (Show c, Displ str) => Displ (CheckedEnc c str) where
displ (UnsafeMkCheckedEnc xs c s) =
"UnsafeMkCheckedEnc " ++ displ xs ++ " " ++ show c ++ " " ++ displ s