#if __GLASGOW_HASKELL__ >= 706
#endif
#if MIN_VERSION_base(4,8,0)
#define HAS_NATURAL
#define HAS_VOID
#endif
#if MIN_VERSION_base(4,7,0)
#define HAS_FIXED_CONSTRUCTOR
#endif
module Data.Binary.Class (
Binary(..)
, GBinaryGet(..)
, GBinaryPut(..)
) where
import Data.Word
import Data.Bits
import Data.Int
import Data.Complex (Complex(..))
#ifdef HAS_VOID
import Data.Void
#endif
import Data.Binary.Put
import Data.Binary.Get
#if ! MIN_VERSION_base(4,8,0)
import Control.Applicative
import Data.Monoid (mempty)
#endif
import qualified Data.Monoid as Monoid
#if !MIN_VERSION_base(4,11,0)
import Data.Monoid ((<>))
#endif
#if MIN_VERSION_base(4,8,0)
import Data.Functor.Identity (Identity (..))
#endif
#if MIN_VERSION_base(4,9,0)
import qualified Data.List.NonEmpty as NE
import qualified Data.Semigroup as Semigroup
#endif
import Control.Monad
import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Builder.Prim as Prim
import Data.List (unfoldr, foldl')
#if MIN_VERSION_base(4,10,0)
import Type.Reflection
import Type.Reflection.Unsafe
import Data.Kind (Type)
import GHC.Exts (RuntimeRep(..), VecCount, VecElem)
#endif
import qualified Data.ByteString as B
#if MIN_VERSION_bytestring(0,10,4)
import qualified Data.ByteString.Short as BS
#endif
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Ratio as R
import qualified Data.Tree as T
import Data.Array.Unboxed
import GHC.Generics
#ifdef HAS_NATURAL
import Numeric.Natural
#endif
import qualified Data.Fixed as Fixed
import qualified Data.Sequence as Seq
import qualified Data.Foldable as Fold
import GHC.Fingerprint
import Data.Version (Version(..))
class GBinaryPut f where
gput :: f t -> Put
class GBinaryGet f where
gget :: Get (f t)
class Binary t where
put :: t -> Put
get :: Get t
putList :: [t] -> Put
putList = defaultPutList
default put :: (Generic t, GBinaryPut (Rep t)) => t -> Put
put = gput . from
default get :: (Generic t, GBinaryGet (Rep t)) => Get t
get = to `fmap` gget
defaultPutList :: Binary a => [a] -> Put
defaultPutList xs = put (length xs) <> mapM_ put xs
#ifdef HAS_VOID
instance Binary Void where
put = absurd
get = mzero
#endif
instance Binary () where
put () = mempty
get = return ()
instance Binary Bool where
put = putWord8 . fromIntegral . fromEnum
get = getWord8 >>= toBool
where
toBool 0 = return False
toBool 1 = return True
toBool c = fail ("Could not map value " ++ show c ++ " to Bool")
instance Binary Ordering where
put = putWord8 . fromIntegral . fromEnum
get = getWord8 >>= toOrd
where
toOrd 0 = return LT
toOrd 1 = return EQ
toOrd 2 = return GT
toOrd c = fail ("Could not map value " ++ show c ++ " to Ordering")
instance Binary Word8 where
put = putWord8
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.word8 xs)
get = getWord8
instance Binary Word16 where
put = putWord16be
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.word16BE xs)
get = getWord16be
instance Binary Word32 where
put = putWord32be
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.word32BE xs)
get = getWord32be
instance Binary Word64 where
put = putWord64be
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.word64BE xs)
get = getWord64be
instance Binary Int8 where
put = putInt8
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.int8 xs)
get = getInt8
instance Binary Int16 where
put = putInt16be
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.int16BE xs)
get = getInt16be
instance Binary Int32 where
put = putInt32be
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.int32BE xs)
get = getInt32be
instance Binary Int64 where
put = putInt64be
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.int64BE xs)
get = getInt64be
instance Binary Word where
put = putWord64be . fromIntegral
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.word64BE (map fromIntegral xs))
get = liftM fromIntegral getWord64be
instance Binary Int where
put = putInt64be . fromIntegral
putList xs =
put (length xs)
<> putBuilder (Prim.primMapListFixed Prim.int64BE (map fromIntegral xs))
get = liftM fromIntegral getInt64be
type SmallInt = Int32
instance Binary Integer where
put n | n >= lo && n <= hi =
putBuilder (Prim.primFixed (Prim.word8 Prim.>*< Prim.int32BE) (0, fromIntegral n))
where
lo = fromIntegral (minBound :: SmallInt) :: Integer
hi = fromIntegral (maxBound :: SmallInt) :: Integer
put n =
putWord8 1
<> put sign
<> put (unroll (abs n))
where
sign = fromIntegral (signum n) :: Word8
get = do
tag <- get :: Get Word8
case tag of
0 -> liftM fromIntegral (get :: Get SmallInt)
_ -> do sign <- get
bytes <- get
let v = roll bytes
return $! if sign == (1 :: Word8) then v else v
#ifdef HAS_FIXED_CONSTRUCTOR
instance Binary (Fixed.Fixed a) where
put (Fixed.MkFixed a) = put a
get = Fixed.MkFixed `liftM` get
#else
instance forall a. Fixed.HasResolution a => Binary (Fixed.Fixed a) where
put x = put (truncate (x * fromInteger (Fixed.resolution (undefined :: Maybe a))) :: Integer)
get = (\x -> fromInteger x / fromInteger (Fixed.resolution (undefined :: Maybe a))) `liftM` get
#endif
unroll :: (Integral a, Bits a) => a -> [Word8]
unroll = unfoldr step
where
step 0 = Nothing
step i = Just (fromIntegral i, i `shiftR` 8)
roll :: (Integral a, Bits a) => [Word8] -> a
roll = foldl' unstep 0 . reverse
where
unstep a b = a `shiftL` 8 .|. fromIntegral b
#ifdef HAS_NATURAL
type NaturalWord = Word64
instance Binary Natural where
put n | n <= hi =
putWord8 0
<> put (fromIntegral n :: NaturalWord)
where
hi = fromIntegral (maxBound :: NaturalWord) :: Natural
put n =
putWord8 1
<> put (unroll (abs n))
get = do
tag <- get :: Get Word8
case tag of
0 -> liftM fromIntegral (get :: Get NaturalWord)
_ -> do bytes <- get
return $! roll bytes
#endif
instance (Binary a,Integral a) => Binary (R.Ratio a) where
put r = put (R.numerator r) <> put (R.denominator r)
get = liftM2 (R.%) get get
instance Binary a => Binary (Complex a) where
put (r :+ i) = put (r, i)
get = (\(r,i) -> r :+ i) <$> get
instance Binary Char where
put = putCharUtf8
putList str = put (length str) <> putStringUtf8 str
get = do
let getByte = liftM (fromIntegral :: Word8 -> Int) get
shiftL6 = flip shiftL 6 :: Int -> Int
w <- getByte
r <- case () of
_ | w < 0x80 -> return w
| w < 0xe0 -> do
x <- liftM (xor 0x80) getByte
return (x .|. shiftL6 (xor 0xc0 w))
| w < 0xf0 -> do
x <- liftM (xor 0x80) getByte
y <- liftM (xor 0x80) getByte
return (y .|. shiftL6 (x .|. shiftL6
(xor 0xe0 w)))
| otherwise -> do
x <- liftM (xor 0x80) getByte
y <- liftM (xor 0x80) getByte
z <- liftM (xor 0x80) getByte
return (z .|. shiftL6 (y .|. shiftL6
(x .|. shiftL6 (xor 0xf0 w))))
getChr r
where
getChr w
| w <= 0x10ffff = return $! toEnum $ fromEnum w
| otherwise = fail "Not a valid Unicode code point!"
instance (Binary a, Binary b) => Binary (a,b) where
put (a,b) = put a <> put b
get = liftM2 (,) get get
instance (Binary a, Binary b, Binary c) => Binary (a,b,c) where
put (a,b,c) = put a <> put b <> put c
get = liftM3 (,,) get get get
instance (Binary a, Binary b, Binary c, Binary d) => Binary (a,b,c,d) where
put (a,b,c,d) = put a <> put b <> put c <> put d
get = liftM4 (,,,) get get get get
instance (Binary a, Binary b, Binary c, Binary d, Binary e) => Binary (a,b,c,d,e) where
put (a,b,c,d,e) = put a <> put b <> put c <> put d <> put e
get = liftM5 (,,,,) get get get get get
instance (Binary a, Binary b, Binary c, Binary d, Binary e, Binary f)
=> Binary (a,b,c,d,e,f) where
put (a,b,c,d,e,f) = put (a,(b,c,d,e,f))
get = do (a,(b,c,d,e,f)) <- get ; return (a,b,c,d,e,f)
instance (Binary a, Binary b, Binary c, Binary d, Binary e, Binary f, Binary g)
=> Binary (a,b,c,d,e,f,g) where
put (a,b,c,d,e,f,g) = put (a,(b,c,d,e,f,g))
get = do (a,(b,c,d,e,f,g)) <- get ; return (a,b,c,d,e,f,g)
instance (Binary a, Binary b, Binary c, Binary d, Binary e,
Binary f, Binary g, Binary h)
=> Binary (a,b,c,d,e,f,g,h) where
put (a,b,c,d,e,f,g,h) = put (a,(b,c,d,e,f,g,h))
get = do (a,(b,c,d,e,f,g,h)) <- get ; return (a,b,c,d,e,f,g,h)
instance (Binary a, Binary b, Binary c, Binary d, Binary e,
Binary f, Binary g, Binary h, Binary i)
=> Binary (a,b,c,d,e,f,g,h,i) where
put (a,b,c,d,e,f,g,h,i) = put (a,(b,c,d,e,f,g,h,i))
get = do (a,(b,c,d,e,f,g,h,i)) <- get ; return (a,b,c,d,e,f,g,h,i)
instance (Binary a, Binary b, Binary c, Binary d, Binary e,
Binary f, Binary g, Binary h, Binary i, Binary j)
=> Binary (a,b,c,d,e,f,g,h,i,j) where
put (a,b,c,d,e,f,g,h,i,j) = put (a,(b,c,d,e,f,g,h,i,j))
get = do (a,(b,c,d,e,f,g,h,i,j)) <- get ; return (a,b,c,d,e,f,g,h,i,j)
#if MIN_VERSION_base(4,8,0)
instance Binary a => Binary (Identity a) where
put (Identity x) = put x
get = Identity <$> get
#endif
instance Binary a => Binary [a] where
put = putList
get = do n <- get :: Get Int
getMany n
getMany :: Binary a => Int -> Get [a]
getMany n = go [] n
where
go xs 0 = return $! reverse xs
go xs i = do x <- get
x `seq` go (x:xs) (i1)
instance (Binary a) => Binary (Maybe a) where
put Nothing = putWord8 0
put (Just x) = putWord8 1 <> put x
get = do
w <- getWord8
case w of
0 -> return Nothing
_ -> liftM Just get
instance (Binary a, Binary b) => Binary (Either a b) where
put (Left a) = putWord8 0 <> put a
put (Right b) = putWord8 1 <> put b
get = do
w <- getWord8
case w of
0 -> liftM Left get
_ -> liftM Right get
instance Binary B.ByteString where
put bs = put (B.length bs)
<> putByteString bs
get = get >>= getByteString
instance Binary ByteString where
put bs = put (fromIntegral (L.length bs) :: Int)
<> putLazyByteString bs
get = get >>= getLazyByteString
#if MIN_VERSION_bytestring(0,10,4)
instance Binary BS.ShortByteString where
put bs = put (BS.length bs)
<> putShortByteString bs
get = get >>= fmap BS.toShort . getByteString
#endif
instance (Binary a) => Binary (Set.Set a) where
put s = put (Set.size s) <> mapM_ put (Set.toAscList s)
get = liftM Set.fromDistinctAscList get
instance (Binary k, Binary e) => Binary (Map.Map k e) where
put m = put (Map.size m) <> mapM_ put (Map.toAscList m)
get = liftM Map.fromDistinctAscList get
instance Binary IntSet.IntSet where
put s = put (IntSet.size s) <> mapM_ put (IntSet.toAscList s)
get = liftM IntSet.fromDistinctAscList get
instance (Binary e) => Binary (IntMap.IntMap e) where
put m = put (IntMap.size m) <> mapM_ put (IntMap.toAscList m)
get = liftM IntMap.fromDistinctAscList get
instance (Binary e) => Binary (Seq.Seq e) where
put s = put (Seq.length s) <> Fold.mapM_ put s
get = do n <- get :: Get Int
rep Seq.empty n get
where rep xs 0 _ = return $! xs
rep xs n g = xs `seq` n `seq` do
x <- g
rep (xs Seq.|> x) (n1) g
instance Binary Double where
put d = put (decodeFloat d)
get = do
x <- get
y <- get
return $! encodeFloat x y
instance Binary Float where
put f = put (decodeFloat f)
get = do
x <- get
y <- get
return $! encodeFloat x y
instance (Binary e) => Binary (T.Tree e) where
put (T.Node r s) = put r <> put s
get = liftM2 T.Node get get
instance (Binary i, Ix i, Binary e) => Binary (Array i e) where
put a =
put (bounds a)
<> put (rangeSize $ bounds a)
<> mapM_ put (elems a)
get = do
bs <- get
n <- get
xs <- getMany n
return (listArray bs xs)
instance (Binary i, Ix i, Binary e, IArray UArray e) => Binary (UArray i e) where
put a =
put (bounds a)
<> put (rangeSize $ bounds a)
<> mapM_ put (elems a)
get = do
bs <- get
n <- get
xs <- getMany n
return (listArray bs xs)
instance Binary Fingerprint where
put (Fingerprint x1 x2) = put x1 <> put x2
get = do
x1 <- get
x2 <- get
return $! Fingerprint x1 x2
instance Binary Version where
put (Version br tags) = put br <> put tags
get = Version <$> get <*> get
instance Binary a => Binary (Monoid.Dual a) where
get = fmap Monoid.Dual get
put = put . Monoid.getDual
instance Binary Monoid.All where
get = fmap Monoid.All get
put = put . Monoid.getAll
instance Binary Monoid.Any where
get = fmap Monoid.Any get
put = put . Monoid.getAny
instance Binary a => Binary (Monoid.Sum a) where
get = fmap Monoid.Sum get
put = put . Monoid.getSum
instance Binary a => Binary (Monoid.Product a) where
get = fmap Monoid.Product get
put = put . Monoid.getProduct
instance Binary a => Binary (Monoid.First a) where
get = fmap Monoid.First get
put = put . Monoid.getFirst
instance Binary a => Binary (Monoid.Last a) where
get = fmap Monoid.Last get
put = put . Monoid.getLast
#if MIN_VERSION_base(4,8,0)
instance Binary (f a) => Binary (Monoid.Alt f a) where
get = fmap Monoid.Alt get
put = put . Monoid.getAlt
#endif
#if MIN_VERSION_base(4,9,0)
instance Binary a => Binary (Semigroup.Min a) where
get = fmap Semigroup.Min get
put = put . Semigroup.getMin
instance Binary a => Binary (Semigroup.Max a) where
get = fmap Semigroup.Max get
put = put . Semigroup.getMax
instance Binary a => Binary (Semigroup.First a) where
get = fmap Semigroup.First get
put = put . Semigroup.getFirst
instance Binary a => Binary (Semigroup.Last a) where
get = fmap Semigroup.Last get
put = put . Semigroup.getLast
instance Binary a => Binary (Semigroup.Option a) where
get = fmap Semigroup.Option get
put = put . Semigroup.getOption
instance Binary m => Binary (Semigroup.WrappedMonoid m) where
get = fmap Semigroup.WrapMonoid get
put = put . Semigroup.unwrapMonoid
instance (Binary a, Binary b) => Binary (Semigroup.Arg a b) where
get = liftM2 Semigroup.Arg get get
put (Semigroup.Arg a b) = put a <> put b
instance Binary a => Binary (NE.NonEmpty a) where
get = fmap NE.fromList get
put = put . NE.toList
#endif
#if MIN_VERSION_base(4,10,0)
instance Binary VecCount where
put = putWord8 . fromIntegral . fromEnum
get = toEnum . fromIntegral <$> getWord8
instance Binary VecElem where
put = putWord8 . fromIntegral . fromEnum
get = toEnum . fromIntegral <$> getWord8
instance Binary RuntimeRep where
put (VecRep a b) = putWord8 0 >> put a >> put b
put (TupleRep reps) = putWord8 1 >> put reps
put (SumRep reps) = putWord8 2 >> put reps
put LiftedRep = putWord8 3
put UnliftedRep = putWord8 4
put IntRep = putWord8 5
put WordRep = putWord8 6
put Int64Rep = putWord8 7
put Word64Rep = putWord8 8
put AddrRep = putWord8 9
put FloatRep = putWord8 10
put DoubleRep = putWord8 11
#if __GLASGOW_HASKELL__ >= 807
put Int8Rep = putWord8 12
put Word8Rep = putWord8 13
put Int16Rep = putWord8 14
put Word16Rep = putWord8 15
#endif
get = do
tag <- getWord8
case tag of
0 -> VecRep <$> get <*> get
1 -> TupleRep <$> get
2 -> SumRep <$> get
3 -> pure LiftedRep
4 -> pure UnliftedRep
5 -> pure IntRep
6 -> pure WordRep
7 -> pure Int64Rep
8 -> pure Word64Rep
9 -> pure AddrRep
10 -> pure FloatRep
11 -> pure DoubleRep
#if __GLASGOW_HASKELL__ >= 807
12 -> pure Int8Rep
13 -> pure Word8Rep
14 -> pure Int16Rep
15 -> pure Word16Rep
#endif
_ -> fail "GHCi.TH.Binary.putRuntimeRep: invalid tag"
instance Binary TyCon where
put tc = do
put (tyConPackage tc)
put (tyConModule tc)
put (tyConName tc)
put (tyConKindArgs tc)
put (tyConKindRep tc)
get = mkTyCon <$> get <*> get <*> get <*> get <*> get
instance Binary KindRep where
put (KindRepTyConApp tc k) = putWord8 0 >> put tc >> put k
put (KindRepVar bndr) = putWord8 1 >> put bndr
put (KindRepApp a b) = putWord8 2 >> put a >> put b
put (KindRepFun a b) = putWord8 3 >> put a >> put b
put (KindRepTYPE r) = putWord8 4 >> put r
put (KindRepTypeLit sort r) = putWord8 5 >> put sort >> put r
get = do
tag <- getWord8
case tag of
0 -> KindRepTyConApp <$> get <*> get
1 -> KindRepVar <$> get
2 -> KindRepApp <$> get <*> get
3 -> KindRepFun <$> get <*> get
4 -> KindRepTYPE <$> get
5 -> KindRepTypeLit <$> get <*> get
_ -> fail "GHCi.TH.Binary.putKindRep: invalid tag"
instance Binary TypeLitSort where
put TypeLitSymbol = putWord8 0
put TypeLitNat = putWord8 1
get = do
tag <- getWord8
case tag of
0 -> pure TypeLitSymbol
1 -> pure TypeLitNat
_ -> fail "GHCi.TH.Binary.putTypeLitSort: invalid tag"
putTypeRep :: TypeRep a -> Put
putTypeRep rep
| Just HRefl <- rep `eqTypeRep` (typeRep :: TypeRep Type)
= put (0 :: Word8)
putTypeRep (Con' con ks) = do
put (1 :: Word8)
put con
put ks
putTypeRep (App f x) = do
put (2 :: Word8)
putTypeRep f
putTypeRep x
putTypeRep (Fun arg res) = do
put (3 :: Word8)
putTypeRep arg
putTypeRep res
putTypeRep _ = error "GHCi.TH.Binary.putTypeRep: Impossible"
getSomeTypeRep :: Get SomeTypeRep
getSomeTypeRep = do
tag <- get :: Get Word8
case tag of
0 -> return $ SomeTypeRep (typeRep :: TypeRep Type)
1 -> do con <- get :: Get TyCon
ks <- get :: Get [SomeTypeRep]
return $ SomeTypeRep $ mkTrCon con ks
2 -> do SomeTypeRep f <- getSomeTypeRep
SomeTypeRep x <- getSomeTypeRep
case typeRepKind f of
Fun arg res ->
case arg `eqTypeRep` typeRepKind x of
Just HRefl -> do
case typeRepKind res `eqTypeRep` (typeRep :: TypeRep Type) of
Just HRefl -> return $ SomeTypeRep $ mkTrApp f x
_ -> failure "Kind mismatch" []
_ -> failure "Kind mismatch"
[ "Found argument of kind: " ++ show (typeRepKind x)
, "Where the constructor: " ++ show f
, "Expects an argument of kind: " ++ show arg
]
_ -> failure "Applied non-arrow type"
[ "Applied type: " ++ show f
, "To argument: " ++ show x
]
3 -> do SomeTypeRep arg <- getSomeTypeRep
SomeTypeRep res <- getSomeTypeRep
case typeRepKind arg `eqTypeRep` (typeRep :: TypeRep Type) of
Just HRefl ->
case typeRepKind res `eqTypeRep` (typeRep :: TypeRep Type) of
Just HRefl -> return $ SomeTypeRep $ Fun arg res
Nothing -> failure "Kind mismatch" []
Nothing -> failure "Kind mismatch" []
_ -> failure "Invalid SomeTypeRep" []
where
failure description info =
fail $ unlines $ [ "GHCi.TH.Binary.getSomeTypeRep: "++description ]
++ map (" "++) info
instance Typeable a => Binary (TypeRep (a :: k)) where
put = putTypeRep
get = do
SomeTypeRep rep <- getSomeTypeRep
case rep `eqTypeRep` expected of
Just HRefl -> pure rep
Nothing -> fail $ unlines
[ "GHCi.TH.Binary: Type mismatch"
, " Deserialized type: " ++ show rep
, " Expected type: " ++ show expected
]
where expected = typeRep :: TypeRep a
instance Binary SomeTypeRep where
put (SomeTypeRep rep) = putTypeRep rep
get = getSomeTypeRep
#endif