type-cereal (empty) → 0.1
raw patch · 5 files changed
+135/−0 lines, 5 filesdep +basedep +bytestringdep +cerealsetup-changed
Dependencies added: base, bytestring, cereal, data-hash, template-haskell, type-digits, type-spine
Files
- LICENSE +0/−0
- Setup.hs +2/−0
- Type/Serialize.hs +38/−0
- Type/Serialize/Base.hs +69/−0
- type-cereal.cabal +26/−0
+ LICENSE view
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Type/Serialize.hs view
@@ -0,0 +1,38 @@+{-# LANGUAGE TemplateHaskell, TypeFamilies #-}++{- |++Module : Type.Serialize+Copyright : (c) The University of Kansas 2011+License : BSD3++Maintainer : nicolas.frisby@gmail.com+Stability : experimental+Portability : see LANGUAGE pragmas (... GHC)++Type-level serialization for the basic types (Bool, Char, Either, etc.) as well+as the @type-spine@ default case for type-applications.++-}+module Type.Serialize (module Type.Serialize.Base) where++import Type.Serialize.Base++import Type.Spine.Stage0 (kTypeG)+import Language.Haskell.TH+import Type.Digits (toType_, digit, radix)++++fmap concat $ mapM serializeTypeAsHash+ [''Bool, ''Char, ''Double, ''Float, ''Int, ''Integer, ''Ordering,+ ''(), ''(,), ''(,,), ''(,,,), ''(,,,,),+ ''IO, ''[], ''Maybe, ''(->), ''Either]+++sequence [+ let k2 = kTypeG $ ArrowK StarK StarK+ n = conT $ digit d+ v = return $ toType_ [digit d]+ in tySynInstD ''Serialize [k2 `appT` n] v+ | d <- [0..radix-1]]
+ Type/Serialize/Base.hs view
@@ -0,0 +1,69 @@+{-# LANGUAGE TemplateHaskell, TypeFamilies, ViewPatterns #-}++{- |++Module : Type.Serialize.Base+Copyright : (c) The University of Kansas 2011+License : BSD3++Maintainer : nicolas.frisby@gmail.com+Stability : experimental+Portability : see LANGUAGE pragmas (... GHC)++Type-level serialization (i.e. type -> @type-digit@ type-level numeral).++-}+module Type.Serialize.Base+ (Serialize, encode, serializeType, serializeTypeAsHash) where++import Type.Spine.TH (tyConSignature)+import Type.Spine.Stage0 (kTypeG)+import Type.Digits (toDigits, toDigits_, fixed, flexible, exactly)++import Language.Haskell.TH+import Language.Haskell.TH.Syntax++import qualified Data.Hash as H++import qualified Data.Serialize as V+import qualified Data.ByteString as BS+++++-- | @Serialize@ maps a type to its unique type-level serialization.+type family Serialize a++-- | Encode uses the @cereal@ package serializer to encode the value and then+-- uses @type-digits@ to reflect it as a type-level numeral.+encode :: V.Serialize a => a -> Type+encode = toDigits_ (BS.foldl' ((. fixed) . (++)) []) . V.encode++-- | Generates the @Serialize@ instance corresponding to the serialization of+-- the type constructor's globally unique name (i.e. TH's @NameG@).+serializeType :: Name -> Q [Dec]+serializeType n@(Name (occString -> occ)+ (NameG _ (pkgString -> pkg) (modString -> mod))) = do+ (ks, k) <- tyConSignature n++ let qk = kTypeG $ foldr ArrowK k ks+ uid = (occ, mod, pkg)+ base128 = return .+ toDigits (exactly 3 . take 3 . flexible) (H.asWord64 $ H.hash uid) .+ encode $ uid+ (:[]) `fmap` tySynInstD ''Serialize [qk `appT` conT n] base128++-- | @serializeType@ can result in very large types, so we prefer the+-- @data-hash@ hash of the @NameG@'s serialization.+serializeTypeAsHash :: Name -> Q [Dec]+serializeTypeAsHash n@(Name (occString -> occ)+ (NameG _ (pkgString -> pkg) (modString -> mod))) = do+ (ks, k) <- tyConSignature n++ let qk = kTypeG $ foldr ArrowK k ks+ uid = (occ, mod, pkg)+ base128 = return . toDigits_ flexible . H.asWord64 . H.hash $ uid+ (:[]) `fmap` tySynInstD ''Serialize [qk `appT` conT n] base128++tvb_kind (PlainTV _) = StarK+tvb_kind (KindedTV _ k) = k
+ type-cereal.cabal view
@@ -0,0 +1,26 @@+Name: type-cereal+Version: 0.1+License: BSD3+License-File: LICENSE+Author: Nicolas Frisby <nicolas.frisby@gmail.com>+Maintainer: Nicolas Frisby <nicolas.frisby@gmail.com>++Category: Type System++Synopsis: Type-level serialization of type constructors++Description: Any @type-spine@-enabled type constructor can be converted to a+ unique @type-digits@ type-level numeral.+++Cabal-Version: >= 1.6.0.1++Build-Type: Simple+++Library+ Build-Depends: base >= 4 && < 5, template-haskell+ Build-Depends: cereal >= 0.3 && < 0.4, bytestring >= 0.9 && < 0.10, data-hash < 0.2+ Build-Depends: type-spine < 0.2, type-digits < 0.2++ Exposed-Modules: Type.Serialize, Type.Serialize.Base