byron-spec-ledger-1.0.1.0: Executable specification of Cardano ledger
Safe HaskellSafe-Inferred
LanguageHaskell2010

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 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 Word8 Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsWord8Seq TypeRep Source #

HasTypeReps Addr Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsAddrSeq TypeRep Source #

HasTypeReps BlockCount Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

HasTypeReps Epoch Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsEpochSeq TypeRep Source #

HasTypeReps Hash Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsHashSeq TypeRep Source #

HasTypeReps KeyPair Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

HasTypeReps Lovelace Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

HasTypeReps Owner Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsOwnerSeq TypeRep Source #

HasTypeReps SKey Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsSKeySeq TypeRep Source #

HasTypeReps Slot Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsSlotSeq TypeRep Source #

HasTypeReps SlotCount Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

HasTypeReps VKey Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsVKeySeq TypeRep Source #

HasTypeReps VKeyGenesis Source # 
Instance details

Defined in Byron.Spec.Ledger.Core

HasTypeReps DCert Source # 
Instance details

Defined in Byron.Spec.Ledger.Delegation

Methods

typeRepsDCertSeq TypeRep Source #

HasTypeReps Tx Source # 
Instance details

Defined in Byron.Spec.Ledger.UTxO

Methods

typeRepsTxSeq TypeRep Source #

HasTypeReps TxBody Source # 
Instance details

Defined in Byron.Spec.Ledger.UTxO

HasTypeReps TxId Source # 
Instance details

Defined in Byron.Spec.Ledger.UTxO

Methods

typeRepsTxIdSeq TypeRep Source #

HasTypeReps TxIn Source # 
Instance details

Defined in Byron.Spec.Ledger.UTxO

Methods

typeRepsTxInSeq TypeRep Source #

HasTypeReps TxOut Source # 
Instance details

Defined in Byron.Spec.Ledger.UTxO

Methods

typeRepsTxOutSeq TypeRep Source #

HasTypeReps Wit Source # 
Instance details

Defined in Byron.Spec.Ledger.UTxO

Methods

typeRepsWitSeq TypeRep Source #

HasTypeReps ApName Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps ApVer Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

Methods

typeRepsApVerSeq TypeRep Source #

HasTypeReps BkSgnCntT Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps FactorA Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps FactorB Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps Metadata Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps PParams Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps ProtVer Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps SwVer Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

Methods

typeRepsSwVerSeq TypeRep Source #

HasTypeReps UProp Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

Methods

typeRepsUPropSeq TypeRep Source #

HasTypeReps UpAdptThd Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

HasTypeReps UpId Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

Methods

typeRepsUpIdSeq TypeRep Source #

HasTypeReps Vote Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

Methods

typeRepsVoteSeq TypeRep Source #

HasTypeReps ShortHash 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 Bool Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsBoolSeq TypeRep Source #

HasTypeReps Char Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsCharSeq TypeRep Source #

HasTypeReps Double Source # 
Instance details

Defined in Data.AbstractSize

HasTypeReps Int Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsIntSeq TypeRep Source #

HasTypeReps Word Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsWordSeq TypeRep Source #

Typeable a ⇒ HasTypeReps (Sig a) Source #

We need a custom instance here that returns only the top level type. A generic instance would have recursed into type a and since we use typeReps to compute abstractSize, this would mean the size of 'Sig a' would include the size of a (e.g. Tx). This would create an artificial coupling between the size of a type and it's "signature".

Instance details

Defined in Byron.Spec.Ledger.Core

Methods

typeRepsSig a → Seq TypeRep Source #

HasTypeReps (VerKeyDSIGN MockDSIGN) Source # 
Instance details

Defined in Data.AbstractSize

(Typeable a, HasTypeReps a) ⇒ HasTypeReps (Set a) Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsSet a → Seq TypeRep Source #

HasTypeReps (Digest SHA256) Source # 
Instance details

Defined in Data.AbstractSize

(Typeable a, HasTypeReps a) ⇒ HasTypeReps (Maybe a) Source # 
Instance details

Defined in Data.AbstractSize

Methods

typeRepsMaybe a → Seq TypeRep Source #

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

Defined in Data.AbstractSize

Methods

typeReps ∷ [a] → Seq TypeRep Source #

HasTypeReps (SignedDSIGN MockDSIGN a) Source # 
Instance details

Defined in Data.AbstractSize

Typeable a ⇒ HasTypeReps (Hash ShortHash a) 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

Methods

typeReps ∷ (a, b) → Seq TypeRep Source #

HasTypeReps (ProtVer, PParams, SwVer, Set STag, Metadata) Source # 
Instance details

Defined in Byron.Spec.Ledger.Update

abstractSizeHasTypeReps 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
>>> 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

type Size = Int Source #