{-# LANGUAGE GADTs #-} import Data.Char data Type t where TInt :: Type Int TChar :: Type Char TList :: Type t -> Type [t] tString :: Type String tString = TList TChar data Bit = F | T deriving(Show, Eq) encode :: Type t -> t -> [Bit] encode TInt i = encodeInt i encode TChar c = encodeChar c encode (TList _) [] = F : [] encode (TList t) (x : xs) = T : (encode t x) ++ encode (TList t) xs encodeInt :: Int -> [Bit] encodeInt 0 = [F] encodeInt n = reverse $ helper n where helper 0 = [] helper n = let (q,r) = n `divMod` 2 in (mkBit r) : helper q mkBit i = if i == 1 then T else F encodeChar :: Char -> [Bit] encodeChar c = encodeInt $ ord c data Dynamic where Dyn :: Type t -> t -> Dynamic encode' :: Dynamic -> [Bit] encode' (Dyn t v) = encode t v