finitary-derive-1.0.0.1: Easy and efficient Unbox, Storable, Binary and Hashable instances for Finitary types.
Copyright (C) Koz Ross 2019 GPL version 3.0 or later koz.ross@retro-freedom.nz Experimental GHC only Trustworthy Haskell2010

Data.Finitary.Pack

Description

Defines a newtype for easy derivation of Unbox, Storable, Binary and Hashable instances for any type with a Finitary instance. The easiest way to use this is with the DerivingVia extension:

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}

import Data.Finitary
import Data.Finitary.Pack
import Data.Word
import Data.Hashable

data Foo = Bar | Baz (Word8, Word8) | Quux Word16
deriving (Eq, Generic, Finitary)
deriving (Storable, Binary, Hashable) via (Pack Foo)

Alternatively, you can just use Pack a instead of a wherever appropriate. Unfortunately (due to role restrictions on unboxed vectors), you must use Pack a if you want a Vector full of as - DerivingVia is of no help here.

Synopsis

# Documentation

newtype Pack a Source #

Essentially Identity a, but with different instances. So named due to the 'packing' of the type's indices densely into arrays, memory or bits.

Constructors

 Pack FieldsunPack :: a

#### Instances

Instances details
 Source # Instance detailsDefined in Data.Finitary.Pack Methodsfmap :: (a -> b) -> Pack a -> Pack b #(<\$) :: a -> Pack b -> Pack a # (Finitary a, 1 <= Cardinality a) => Vector Vector (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack MethodsbasicUnsafeFreeze :: PrimMonad m => Mutable Vector (PrimState m) (Pack a) -> m (Vector (Pack a))basicUnsafeThaw :: PrimMonad m => Vector (Pack a) -> m (Mutable Vector (PrimState m) (Pack a))basicLength :: Vector (Pack a) -> IntbasicUnsafeSlice :: Int -> Int -> Vector (Pack a) -> Vector (Pack a)basicUnsafeIndexM :: Monad m => Vector (Pack a) -> Int -> m (Pack a)basicUnsafeCopy :: PrimMonad m => Mutable Vector (PrimState m) (Pack a) -> Vector (Pack a) -> m ()elemseq :: Vector (Pack a) -> Pack a -> b -> b (Finitary a, 1 <= Cardinality a) => MVector MVector (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack MethodsbasicLength :: MVector s (Pack a) -> IntbasicUnsafeSlice :: Int -> Int -> MVector s (Pack a) -> MVector s (Pack a)basicOverlaps :: MVector s (Pack a) -> MVector s (Pack a) -> BoolbasicUnsafeNew :: PrimMonad m => Int -> m (MVector (PrimState m) (Pack a))basicInitialize :: PrimMonad m => MVector (PrimState m) (Pack a) -> m ()basicUnsafeReplicate :: PrimMonad m => Int -> Pack a -> m (MVector (PrimState m) (Pack a))basicUnsafeRead :: PrimMonad m => MVector (PrimState m) (Pack a) -> Int -> m (Pack a)basicUnsafeWrite :: PrimMonad m => MVector (PrimState m) (Pack a) -> Int -> Pack a -> m ()basicClear :: PrimMonad m => MVector (PrimState m) (Pack a) -> m ()basicSet :: PrimMonad m => MVector (PrimState m) (Pack a) -> Pack a -> m ()basicUnsafeCopy :: PrimMonad m => MVector (PrimState m) (Pack a) -> MVector (PrimState m) (Pack a) -> m ()basicUnsafeMove :: PrimMonad m => MVector (PrimState m) (Pack a) -> MVector (PrimState m) (Pack a) -> m ()basicUnsafeGrow :: PrimMonad m => MVector (PrimState m) (Pack a) -> Int -> m (MVector (PrimState m) (Pack a)) Bounded a => Bounded (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methods Eq a => Eq (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methods(==) :: Pack a -> Pack a -> Bool #(/=) :: Pack a -> Pack a -> Bool # Data a => Data (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Pack a -> c (Pack a) #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Pack a) #toConstr :: Pack a -> Constr #dataTypeOf :: Pack a -> DataType #dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Pack a)) #dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Pack a)) #gmapT :: (forall b. Data b => b -> b) -> Pack a -> Pack a #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Pack a -> r #gmapQr :: forall r r'. (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Pack a -> r #gmapQ :: (forall d. Data d => d -> u) -> Pack a -> [u] #gmapQi :: Int -> (forall d. Data d => d -> u) -> Pack a -> u #gmapM :: Monad m => (forall d. Data d => d -> m d) -> Pack a -> m (Pack a) #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Pack a -> m (Pack a) #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Pack a -> m (Pack a) # Ord a => Ord (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methodscompare :: Pack a -> Pack a -> Ordering #(<) :: Pack a -> Pack a -> Bool #(<=) :: Pack a -> Pack a -> Bool #(>) :: Pack a -> Pack a -> Bool #(>=) :: Pack a -> Pack a -> Bool #max :: Pack a -> Pack a -> Pack a #min :: Pack a -> Pack a -> Pack a # Read a => Read (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack MethodsreadsPrec :: Int -> ReadS (Pack a) #readList :: ReadS [Pack a] # Show a => Show (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack MethodsshowsPrec :: Int -> Pack a -> ShowS #show :: Pack a -> String #showList :: [Pack a] -> ShowS # Generic (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Associated Typestype Rep (Pack a) :: Type -> Type # Methodsfrom :: Pack a -> Rep (Pack a) x #to :: Rep (Pack a) x -> Pack a # Semigroup a => Semigroup (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methods(<>) :: Pack a -> Pack a -> Pack a #sconcat :: NonEmpty (Pack a) -> Pack a #stimes :: Integral b => b -> Pack a -> Pack a # Monoid a => Monoid (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methodsmempty :: Pack a #mappend :: Pack a -> Pack a -> Pack a #mconcat :: [Pack a] -> Pack a # (Finitary a, 1 <= Cardinality a) => Storable (Pack a) Source # As Finitary instances have known limits on their indices, they can be stored as their indices. Instance detailsDefined in Data.Finitary.Pack MethodssizeOf :: Pack a -> Int #alignment :: Pack a -> Int #peekElemOff :: Ptr (Pack a) -> Int -> IO (Pack a) #pokeElemOff :: Ptr (Pack a) -> Int -> Pack a -> IO () #peekByteOff :: Ptr b -> Int -> IO (Pack a) #pokeByteOff :: Ptr b -> Int -> Pack a -> IO () #peek :: Ptr (Pack a) -> IO (Pack a) #poke :: Ptr (Pack a) -> Pack a -> IO () # (Finitary a, 1 <= Cardinality a) => Binary (Pack a) Source # We can serialize any Finitary by serializing its index. Instance detailsDefined in Data.Finitary.Pack Methodsput :: Pack a -> Put #get :: Get (Pack a) #putList :: [Pack a] -> Put # NFData a => NFData (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Methodsrnf :: Pack a -> () # Finitary a => Finitary (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack Associated Typestype Cardinality (Pack a) :: Nat MethodsfromFinite :: Finite (Cardinality (Pack a)) -> Pack atoFinite :: Pack a -> Finite (Cardinality (Pack a))start :: Pack aend :: Pack aprevious :: Alternative f => Pack a -> f (Pack a)previousSkipping :: Alternative f => Finite (Cardinality (Pack a)) -> Pack a -> f (Pack a)next :: Alternative f => Pack a -> f (Pack a)nextSkipping :: Alternative f => Finite (Cardinality (Pack a)) -> Pack a -> f (Pack a)enumerateFrom :: Pack a -> [Pack a]enumerateFromThen :: Pack a -> Pack a -> [Pack a]enumerateFromTo :: Pack a -> Pack a -> [Pack a]enumerateFromThenTo :: Pack a -> Pack a -> Pack a -> [Pack a] (Finitary a, 1 <= Cardinality a) => Unbox (Pack a) Source # We can rely on the fact that indexes of any Finitary type have a fixed maximum size to 'unravel' them into a block of Word8s, which we can easily unbox. Instance detailsDefined in Data.Finitary.Pack (Finitary a, 1 <= Cardinality a) => Hashable (Pack a) Source # We can hash any Finitary by hashing its index. Instance detailsDefined in Data.Finitary.Pack MethodshashWithSalt :: Int -> Pack a -> Inthash :: Pack a -> Int Source # Instance detailsDefined in Data.Finitary.Pack Associated Typestype Rep1 Pack :: k -> Type # Methodsfrom1 :: forall (a :: k). Pack a -> Rep1 Pack a #to1 :: forall (a :: k). Rep1 Pack a -> Pack a # newtype MVector s (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack newtype MVector s (Pack a) = MV_Pack (MVector s Word8) type Rep (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack type Rep (Pack a) = D1 ('MetaData "Pack" "Data.Finitary.Pack" "finitary-derive-1.0.0.1-inplace" 'True) (C1 ('MetaCons "Pack" 'PrefixI 'True) (S1 ('MetaSel ('Just "unPack") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a))) type Cardinality (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack type Cardinality (Pack a) = GCardinality (Rep (Pack a)) newtype Vector (Pack a) Source # Instance detailsDefined in Data.Finitary.Pack newtype Vector (Pack a) = V_Pack (Vector Word8) type Rep1 Pack Source # Instance detailsDefined in Data.Finitary.Pack type Rep1 Pack = D1 ('MetaData "Pack" "Data.Finitary.Pack" "finitary-derive-1.0.0.1-inplace" 'True) (C1 ('MetaCons "Pack" 'PrefixI 'True) (S1 ('MetaSel ('Just "unPack") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))