{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
module Flat.Instances.Base () where
import Control.Monad (liftM2)
import Data.Bool
import Data.Char
import Data.Complex (Complex (..))
import Data.Fixed
import qualified Data.List.NonEmpty as B
#if ! MIN_VERSION_base(4,8,0)
import Control.Applicative
import Data.Monoid (mempty)
#endif
#if MIN_VERSION_base(4,9,0)
import qualified Data.Semigroup as Semigroup
#endif
import qualified Data.Monoid as Monoid
import Data.Ratio
import Flat.Instances.Util
import Prelude hiding (mempty)
#if MIN_VERSION_base(4,9,0)
import Data.Functor.Identity (Identity (..))
#endif
#if MIN_VERSION_base(4,8,0)
instance Flat Monoid.All where
encode :: All -> Encoding
encode (Monoid.All Bool
a) = forall a. Flat a => a -> Encoding
encode Bool
a
size :: All -> NumBits -> NumBits
size (Monoid.All Bool
a) = forall a. Flat a => a -> NumBits -> NumBits
size Bool
a
decode :: Get All
decode = Bool -> All
Monoid.All forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat (f a) => Flat (Monoid.Alt f a) where
encode :: Alt f a -> Encoding
encode (Monoid.Alt f a
a) = forall a. Flat a => a -> Encoding
encode f a
a
size :: Alt f a -> NumBits -> NumBits
size (Monoid.Alt f a
a) = forall a. Flat a => a -> NumBits -> NumBits
size f a
a
decode :: Get (Alt f a)
decode = forall {k} (f :: k -> *) (a :: k). f a -> Alt f a
Monoid.Alt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
#endif
#if MIN_VERSION_base(4,9,0)
instance Flat a => Flat (Identity a) where
encode :: Identity a -> Encoding
encode (Identity a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Identity a -> NumBits -> NumBits
size (Identity a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Identity a)
decode = forall a. a -> Identity a
Identity forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
#endif
instance Flat a => Flat (Monoid.Dual a) where
encode :: Dual a -> Encoding
encode (Monoid.Dual a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Dual a -> NumBits -> NumBits
size (Monoid.Dual a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Dual a)
decode = forall a. a -> Dual a
Monoid.Dual forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat Monoid.Any where
encode :: Any -> Encoding
encode (Monoid.Any Bool
a) = forall a. Flat a => a -> Encoding
encode Bool
a
size :: Any -> NumBits -> NumBits
size (Monoid.Any Bool
a) = forall a. Flat a => a -> NumBits -> NumBits
size Bool
a
decode :: Get Any
decode = Bool -> Any
Monoid.Any forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat a => Flat (Monoid.Sum a) where
encode :: Sum a -> Encoding
encode (Monoid.Sum a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Sum a -> NumBits -> NumBits
size (Monoid.Sum a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Sum a)
decode = forall a. a -> Sum a
Monoid.Sum forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat a => Flat (Monoid.Product a) where
encode :: Product a -> Encoding
encode (Monoid.Product a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Product a -> NumBits -> NumBits
size (Monoid.Product a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Product a)
decode = forall a. a -> Product a
Monoid.Product forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
#if MIN_VERSION_base(4,9,0)
instance Flat a => Flat (Semigroup.Min a) where
encode :: Min a -> Encoding
encode (Semigroup.Min a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Min a -> NumBits -> NumBits
size (Semigroup.Min a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Min a)
decode = forall a. a -> Min a
Semigroup.Min forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.Max a) where
encode :: Max a -> Encoding
encode (Semigroup.Max a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Max a -> NumBits -> NumBits
size (Semigroup.Max a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Max a)
decode = forall a. a -> Max a
Semigroup.Max forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.First a) where
encode :: First a -> Encoding
encode (Semigroup.First a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: First a -> NumBits -> NumBits
size (Semigroup.First a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (First a)
decode = forall a. a -> First a
Semigroup.First forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat a => Flat (Semigroup.Last a) where
encode :: Last a -> Encoding
encode (Semigroup.Last a
a) = forall a. Flat a => a -> Encoding
encode a
a
size :: Last a -> NumBits -> NumBits
size (Semigroup.Last a
a) = forall a. Flat a => a -> NumBits -> NumBits
size a
a
decode :: Get (Last a)
decode = forall a. a -> Last a
Semigroup.Last forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
#endif
instance Flat () where
encode :: () -> Encoding
encode ()
_ = forall a. Monoid a => a
mempty
size :: () -> NumBits -> NumBits
size ()
_ = forall a. a -> a
id
decode :: Get ()
decode = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
instance Flat Bool where
encode :: Bool -> Encoding
encode = Bool -> Encoding
eBool
size :: Bool -> NumBits -> NumBits
size = Bool -> NumBits -> NumBits
sBool
decode :: Get Bool
decode = Get Bool
dBool
instance Flat Char where
size :: Char -> NumBits -> NumBits
size = Char -> NumBits -> NumBits
sChar
encode :: Char -> Encoding
encode = Char -> Encoding
eChar
decode :: Get Char
decode = Get Char
dChar
instance Flat a => Flat (Maybe a)
instance (Flat a, Flat b) => Flat (Either a b)
instance Flat (Fixed a) where
encode :: Fixed a -> Encoding
encode (MkFixed Integer
n) = forall a. Flat a => a -> Encoding
encode Integer
n
size :: Fixed a -> NumBits -> NumBits
size (MkFixed Integer
n) = forall a. Flat a => a -> NumBits -> NumBits
size Integer
n
decode :: Get (Fixed a)
decode = forall k (a :: k). Integer -> Fixed a
MkFixed forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Flat a => Get a
decode
instance Flat Word8 where
encode :: Word8 -> Encoding
encode = Word8 -> Encoding
eWord8
decode :: Get Word8
decode = Get Word8
dWord8
size :: Word8 -> NumBits -> NumBits
size = Word8 -> NumBits -> NumBits
sWord8
instance Flat Word where
size :: Word -> NumBits -> NumBits
size = Word -> NumBits -> NumBits
sWord
encode :: Word -> Encoding
encode = Word -> Encoding
eWord
decode :: Get Word
decode = Get Word
dWord
instance Flat Natural where
size :: Nat -> NumBits -> NumBits
size = Nat -> NumBits -> NumBits
sNatural
encode :: Nat -> Encoding
encode = Nat -> Encoding
eNatural
decode :: Get Nat
decode = Get Nat
dNatural
instance Flat Word16 where
encode :: Word16 -> Encoding
encode = Word16 -> Encoding
eWord16
decode :: Get Word16
decode = Get Word16
dWord16
size :: Word16 -> NumBits -> NumBits
size = Word16 -> NumBits -> NumBits
sWord16
instance Flat Word32 where
encode :: Word32 -> Encoding
encode = Word32 -> Encoding
eWord32
decode :: Get Word32
decode = Get Word32
dWord32
size :: Word32 -> NumBits -> NumBits
size = Word32 -> NumBits -> NumBits
sWord32
instance Flat Word64 where
encode :: Word64 -> Encoding
encode = Word64 -> Encoding
eWord64
decode :: Get Word64
decode = Get Word64
dWord64
size :: Word64 -> NumBits -> NumBits
size = Word64 -> NumBits -> NumBits
sWord64
instance Flat Int where
size :: NumBits -> NumBits -> NumBits
size = NumBits -> NumBits -> NumBits
sInt
encode :: NumBits -> Encoding
encode = NumBits -> Encoding
eInt
decode :: Get NumBits
decode = Get NumBits
dInt
instance Flat Integer where
size :: Integer -> NumBits -> NumBits
size = Integer -> NumBits -> NumBits
sInteger
encode :: Integer -> Encoding
encode = Integer -> Encoding
eInteger
decode :: Get Integer
decode = Get Integer
dInteger
instance Flat Int8 where
encode :: Int8 -> Encoding
encode = Int8 -> Encoding
eInt8
decode :: Get Int8
decode = Get Int8
dInt8
size :: Int8 -> NumBits -> NumBits
size = Int8 -> NumBits -> NumBits
sInt8
instance Flat Int16 where
size :: Int16 -> NumBits -> NumBits
size = Int16 -> NumBits -> NumBits
sInt16
encode :: Int16 -> Encoding
encode = Int16 -> Encoding
eInt16
decode :: Get Int16
decode = Get Int16
dInt16
instance Flat Int32 where
size :: Int32 -> NumBits -> NumBits
size = Int32 -> NumBits -> NumBits
sInt32
encode :: Int32 -> Encoding
encode = Int32 -> Encoding
eInt32
decode :: Get Int32
decode = Get Int32
dInt32
instance Flat Int64 where
size :: Int64 -> NumBits -> NumBits
size = Int64 -> NumBits -> NumBits
sInt64
encode :: Int64 -> Encoding
encode = Int64 -> Encoding
eInt64
decode :: Get Int64
decode = Get Int64
dInt64
instance Flat Float where
size :: Float -> NumBits -> NumBits
size = Float -> NumBits -> NumBits
sFloat
encode :: Float -> Encoding
encode = Float -> Encoding
eFloat
decode :: Get Float
decode = Get Float
dFloat
instance Flat Double where
size :: Double -> NumBits -> NumBits
size = Double -> NumBits -> NumBits
sDouble
encode :: Double -> Encoding
encode = Double -> Encoding
eDouble
decode :: Get Double
decode = Get Double
dDouble
instance Flat a => Flat (Complex a)
instance (Integral a, Flat a) => Flat (Ratio a) where
size :: Ratio a -> NumBits -> NumBits
size Ratio a
a = forall a. Flat a => a -> NumBits -> NumBits
size (forall a. Ratio a -> a
numerator Ratio a
a, forall a. Ratio a -> a
denominator Ratio a
a)
encode :: Ratio a -> Encoding
encode Ratio a
a = forall a. Flat a => a -> Encoding
encode (forall a. Ratio a -> a
numerator Ratio a
a, forall a. Ratio a -> a
denominator Ratio a
a)
decode :: Get (Ratio a)
decode = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. Integral a => a -> a -> Ratio a
(%) forall a. Flat a => Get a
decode forall a. Flat a => Get a
decode
instance {-# OVERLAPPABLE #-} Flat a => Flat [a]
instance {-# OVERLAPPABLE #-} Flat a => Flat (B.NonEmpty a)
instance {-# OVERLAPPABLE #-} (Flat a, Flat b) => Flat (a, b)
instance {-# OVERLAPPABLE #-} (Flat a, Flat b, Flat c) => Flat (a, b, c)
instance
{-# OVERLAPPABLE #-}
(Flat a, Flat b, Flat c, Flat d) =>
Flat (a, b, c, d)
instance
{-# OVERLAPPABLE #-}
(Flat a, Flat b, Flat c, Flat d, Flat e) =>
Flat (a, b, c, d, e)
instance
{-# OVERLAPPABLE #-}
(Flat a, Flat b, Flat c, Flat d, Flat e, Flat f) =>
Flat (a, b, c, d, e, f)
instance
{-# OVERLAPPABLE #-}
( Flat a
, Flat b
, Flat c
, Flat d
, Flat e
, Flat f
, Flat g
) =>
Flat (a, b, c, d, e, f, g)