cardano-data-0.1.0.0: Specialized data for Cardano project
Safe Haskell None
Language Haskell2010

Data.AbstractSize

Description

An approach to computing the abstract size of data using TypeRep .

Synopsis

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
>>> import GHC.Generics (Generic)
>>> 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

Instances details
HasTypeReps Bool Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Char Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Double Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Int Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Integer Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Natural Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Word Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Word8 Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Word16 Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Word32 Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps Word64 Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps ShortHash Source #
Instance details

Defined in Data.AbstractSize

( Typeable a, HasTypeReps a) => HasTypeReps [a] Source #
Instance details

Defined in Data.AbstractSize

( Typeable a, HasTypeReps a) => HasTypeReps ( Maybe a) Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps ( VerKeyDSIGN MockDSIGN ) Source #
Instance details

Defined in Data.AbstractSize

( Typeable a, HasTypeReps a) => HasTypeReps ( Set a) Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps ( Digest SHA256 ) Source #
Instance details

Defined in Data.AbstractSize

( Typeable a, Typeable b, HasTypeReps a, HasTypeReps b) => HasTypeReps (a, b) Source #
Instance details

Defined in Data.AbstractSize

HasTypeReps ( SignedDSIGN MockDSIGN a) Source #
Instance details

Defined in Data.AbstractSize

Typeable a => HasTypeReps ( Hash ShortHash a) Source #
Instance details

Defined in Data.AbstractSize

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 ( TypeRep s) inside a , we require it to be an instance of HasTypeReps .

Examples:

>>> :set -XOverloadedLists
>>> import Data.Typeable (typeOf)
>>> 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