| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Data.AbstractSize
Description
An approach to computing the abstract size of data using TypeRep.
Synopsis
- class HasTypeReps a
- typeReps :: HasTypeReps a => a -> Seq TypeRep
- abstractSize :: HasTypeReps a => AccountingMap -> a -> Size
- type AccountingMap = Map TypeRep Size
- type Size = Int
Documentation
class HasTypeReps a Source #
The typeReps function retrieves all the type representations found while
traversing the data given as parameter.
CAUTION: for newtypes, do not use 'deriving newtype (HasTypeReps)' to derive
instances, rather use 'deriving anyclass (HasTypeReps)'.
This is because we use these instances in abstractSize, and for that
we prefer to have the newtype wrapper type available for "costing".
The difference between 'newtype' and anyclass instances is as follows:
newtype Hash = Hash { unHash :: Int } deriving newtype (..., HasTypeReps) > typeReps someHash = Seq.fromList [Int] vs newtype Hash = Hash { unHash :: Int } deriving stock (...,Generics); deriving anyclass (HasTypeReps) > typeReps someHash = Seq.fromList [Hash, Int]
Examples:
>>>typeReps "a"fromList [[Char],Char]
>>>typeReps "ab"fromList [[Char],Char,Char]
>>>typeReps ([] :: [Int])fromList [[Int]]
>>>:set -XDeriveGeneric>>>data Foo = Foo [Int] (Char, Char) deriving (Generic)>>>instance HasTypeReps Foo>>>typeReps $ Foo [1, 2] ('a', 'b')fromList [Foo,[Int],Int,Int,(Char,Char),Char,Char]
Instances
| HasTypeReps Bool Source # | |
| HasTypeReps Char Source # | |
| HasTypeReps Double Source # | |
| HasTypeReps Int Source # | |
| HasTypeReps Integer Source # | |
| HasTypeReps Natural Source # | |
| HasTypeReps Word Source # | |
| HasTypeReps Word8 Source # | |
| HasTypeReps Word16 Source # | |
| HasTypeReps Word32 Source # | |
| HasTypeReps Word64 Source # | |
| HasTypeReps ShortHash Source # | |
| (Typeable a, HasTypeReps a) => HasTypeReps [a] Source # | |
| (Typeable a, HasTypeReps a) => HasTypeReps (Maybe a) Source # | |
| HasTypeReps (VerKeyDSIGN MockDSIGN) Source # | |
Defined in Data.AbstractSize | |
| (Typeable a, HasTypeReps a) => HasTypeReps (Set a) Source # | |
| HasTypeReps (Digest SHA256) Source # | |
| (Typeable a, Typeable b, HasTypeReps a, HasTypeReps b) => HasTypeReps (a, b) Source # | |
| HasTypeReps (SignedDSIGN MockDSIGN a) Source # | |
Defined in Data.AbstractSize | |
| Typeable a => HasTypeReps (Hash ShortHash a) Source # | |
abstractSize :: HasTypeReps a => AccountingMap -> a -> Size Source #
abstractSize m a computes the abstract size of a, using the accounting
map m. The map m determines the abstract size of each TypeRep
contained in a, and this function simply adds all the individual abstract
sizes. To be able to extract the type representations (TypeReps) inside
a, we require it to be an instance of HasTypeReps.
Examples:
>>>:set -XOverloadedLists>>>abstractSize [(typeOf (undefined:: Char), 10)] 'a'10
>>>abstractSize [(typeOf 'x', 10)] "hello"50
>>>abstractSize [(typeOf 'x', 10), (typeOf True, 100)] ("hello", False)150
>>>abstractSize [(typeOf (undefined :: [Int]), 6), (typeOf (1 :: Int), 1)] ([0, 1, 2, 3] :: [Int])10
>>>abstractSize [(typeOf (undefined :: [Int]), 3), (typeOf (1 :: Int), -1)] ([0, 1, 2] :: [Int])0