finitary-derive-1.0.0.0: Easy and efficient Unbox, Storable, Binary and Hashable instances for Finitary types.
Copyright(C) Koz Ross 2019
LicenseGPL version 3.0 or later
Maintainerkoz.ross@retro-freedom.nz
StabilityExperimental
PortabilityGHC only
Safe HaskellTrustworthy
LanguageHaskell2010

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 

Fields

Instances

Instances details
Functor Pack Source # 
Instance details

Defined in Data.Finitary.Pack

Methods

fmap :: (a -> b) -> Pack a -> Pack b #

(<$) :: a -> Pack b -> Pack a #

(Finitary a, 1 <= Cardinality a) => Vector Vector (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Methods

basicUnsafeFreeze :: 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) -> Int #

basicUnsafeSlice :: 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 details

Defined in Data.Finitary.Pack

Methods

basicLength :: MVector s (Pack a) -> Int #

basicUnsafeSlice :: Int -> Int -> MVector s (Pack a) -> MVector s (Pack a) #

basicOverlaps :: MVector s (Pack a) -> MVector s (Pack a) -> Bool #

basicUnsafeNew :: 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 details

Defined in Data.Finitary.Pack

Methods

minBound :: Pack a #

maxBound :: Pack a #

Eq a => Eq (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Methods

(==) :: Pack a -> Pack a -> Bool #

(/=) :: Pack a -> Pack a -> Bool #

Data a => Data (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Methods

gfoldl :: (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 details

Defined in Data.Finitary.Pack

Methods

compare :: 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 details

Defined in Data.Finitary.Pack

Show a => Show (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Methods

showsPrec :: Int -> Pack a -> ShowS #

show :: Pack a -> String #

showList :: [Pack a] -> ShowS #

Generic (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Associated Types

type Rep (Pack a) :: Type -> Type #

Methods

from :: Pack a -> Rep (Pack a) x #

to :: Rep (Pack a) x -> Pack a #

Semigroup a => Semigroup (Pack a) Source # 
Instance details

Defined 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 details

Defined in Data.Finitary.Pack

Methods

mempty :: 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 details

Defined in Data.Finitary.Pack

Methods

sizeOf :: 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 details

Defined in Data.Finitary.Pack

Methods

put :: Pack a -> Put #

get :: Get (Pack a) #

putList :: [Pack a] -> Put #

NFData a => NFData (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Methods

rnf :: Pack a -> () #

Finitary a => Finitary (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

Associated Types

type Cardinality (Pack a) :: Nat #

Methods

fromFinite :: Finite (Cardinality (Pack a)) -> Pack a #

toFinite :: Pack a -> Finite (Cardinality (Pack a)) #

start :: Pack a #

end :: Pack a #

previous :: 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) => Hashable (Pack a) Source #

We can hash any Finitary by hashing its index.

Instance details

Defined in Data.Finitary.Pack

Methods

hashWithSalt :: Int -> Pack a -> Int #

hash :: Pack a -> Int #

(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 details

Defined in Data.Finitary.Pack

Generic1 Pack Source # 
Instance details

Defined in Data.Finitary.Pack

Associated Types

type Rep1 Pack :: k -> Type #

Methods

from1 :: forall (a :: k). Pack a -> Rep1 Pack a #

to1 :: forall (a :: k). Rep1 Pack a -> Pack a #

newtype MVector s (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

newtype MVector s (Pack a) = MV_Pack (MVector s Word8)
type Rep (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

type Rep (Pack a) = D1 ('MetaData "Pack" "Data.Finitary.Pack" "finitary-derive-1.0.0.0-inplace" 'True) (C1 ('MetaCons "Pack" 'PrefixI 'True) (S1 ('MetaSel ('Just "unPack") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a)))
newtype Vector (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

newtype Vector (Pack a) = V_Pack (Vector Word8)
type Cardinality (Pack a) Source # 
Instance details

Defined in Data.Finitary.Pack

type Cardinality (Pack a) = GCardinality (Rep (Pack a))
type Rep1 Pack Source # 
Instance details

Defined in Data.Finitary.Pack

type Rep1 Pack = D1 ('MetaData "Pack" "Data.Finitary.Pack" "finitary-derive-1.0.0.0-inplace" 'True) (C1 ('MetaCons "Pack" 'PrefixI 'True) (S1 ('MetaSel ('Just "unPack") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) Par1))