Skip to content

Commit

Permalink
Update base
Browse files Browse the repository at this point in the history
  • Loading branch information
redxaxder committed Jul 14, 2020
1 parent 058452a commit 8a9d67b
Show file tree
Hide file tree
Showing 24 changed files with 250 additions and 179 deletions.
16 changes: 8 additions & 8 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ write-ghc-environment-files: always
source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: c454b6e791ee2fe84508b4d5ed2c4dedafb7dce4
--sha256: 01m5jq6gsym3j4v85lv01n6f8480lglrb0n2mv87aqm5ksh4di9y
tag: 7d795c3040ea7785812efa1c97864bbb41b15d3e
--sha256: 130i0yj4y9br1m2bhisi6wni3f40i31nfhg878hv0kwi17chl9sz
subdir: binary

source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: c454b6e791ee2fe84508b4d5ed2c4dedafb7dce4
--sha256: 01m5jq6gsym3j4v85lv01n6f8480lglrb0n2mv87aqm5ksh4di9y
tag: 7d795c3040ea7785812efa1c97864bbb41b15d3e
--sha256: 130i0yj4y9br1m2bhisi6wni3f40i31nfhg878hv0kwi17chl9sz
subdir: binary/test

source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: c454b6e791ee2fe84508b4d5ed2c4dedafb7dce4
--sha256: 01m5jq6gsym3j4v85lv01n6f8480lglrb0n2mv87aqm5ksh4di9y
tag: 7d795c3040ea7785812efa1c97864bbb41b15d3e
--sha256: 130i0yj4y9br1m2bhisi6wni3f40i31nfhg878hv0kwi17chl9sz
subdir: cardano-crypto-class

source-repository-package
type: git
location: https://github.com/input-output-hk/cardano-base
tag: c454b6e791ee2fe84508b4d5ed2c4dedafb7dce4
--sha256: 01m5jq6gsym3j4v85lv01n6f8480lglrb0n2mv87aqm5ksh4di9y
tag: 7d795c3040ea7785812efa1c97864bbb41b15d3e
--sha256: 130i0yj4y9br1m2bhisi6wni3f40i31nfhg878hv0kwi17chl9sz
subdir: slotting

source-repository-package
Expand Down
86 changes: 48 additions & 38 deletions semantics/executable-spec/src/Data/AbstractSize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,39 @@
{-# LANGUAGE TypeOperators #-}

-- | An approach to computing the abstract size of data using 'TypeRep'.
--
module Data.AbstractSize
( HasTypeReps
, typeReps
, abstractSize
, AccountingMap
, Size
) where

( HasTypeReps,
typeReps,
abstractSize,
AccountingMap,
Size,
)
where

import Cardano.Crypto.DSIGN.Class (SignedDSIGN (SignedDSIGN), VerKeyDSIGN)
import Cardano.Crypto.DSIGN.Mock (MockDSIGN, SigDSIGN (SigMockDSIGN))
import Cardano.Crypto.Hash (Hash (..))
import Cardano.Crypto.Hash.Short (ShortHash)
import qualified Crypto.Hash as Crypto
import qualified Data.ByteString as BS
import Data.Map.Strict (Map)
import qualified Data.ByteString.Short as SBS
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Sequence (Seq, empty, (<|), (><))
import Data.Sequence (Seq, empty, (<|), (><))
import qualified Data.Sequence as Seq
import Data.Set (Set)
import Data.Typeable (TypeRep, Typeable, typeOf)
import Data.Word (Word16, Word32, Word64, Word8)
import GHC.Generics ((:*:) ((:*:)), (:+:) (L1, R1), Generic, K1 (K1), M1 (M1), Rep,
U1 (U1), from)
import GHC.Natural (Natural)

import Cardano.Crypto.DSIGN.Class (SignedDSIGN (SignedDSIGN), VerKeyDSIGN)
import Cardano.Crypto.DSIGN.Mock (MockDSIGN, SigDSIGN (SigMockDSIGN))
import Cardano.Crypto.Hash (Hash(..))
import Cardano.Crypto.Hash.Short (ShortHash)
import Data.Set (Set)
import Data.Typeable (TypeRep, Typeable, typeOf)
import Data.Word (Word16, Word32, Word64, Word8)
import GHC.Generics
( Generic,
K1 (K1),
M1 (M1),
Rep,
U1 (U1),
from,
(:*:) ((:*:)),
(:+:) (L1, R1),
)
import GHC.Natural (Natural)

-- | @abstractSize m a@ computes the abstract size of @a@, using the accounting
-- map @m@. The map @m@ determines the abstract size of each 'TypeRep'
Expand All @@ -55,14 +62,14 @@ import Cardano.Crypto.Hash.Short (ShortHash)
--
-- >>> abstractSize [(typeOf (undefined :: [Int]), 3), (typeOf (1 :: Int), -1)] ([0, 1, 2] :: [Int])
-- 0
--
abstractSize :: HasTypeReps a => AccountingMap -> a -> Size
abstractSize m a = sum $ fmap cost trs
where
trs = typeReps a
cost t = Map.findWithDefault 0 t m

type Size = Int

type AccountingMap = Map TypeRep Size

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -102,15 +109,15 @@ type AccountingMap = Map TypeRep Size
-- >>> instance HasTypeReps Foo
-- >>> typeReps $ Foo [1, 2] ('a', 'b')
-- fromList [Foo,[Int],Int,Int,(Char,Char),Char,Char]
--
class HasTypeReps a where
typeReps :: a -> Seq TypeRep

default typeReps
:: ( Generic a
, GHasTypeReps (Rep a)
, Typeable a
) => a -> Seq TypeRep
default typeReps ::
( Generic a,
GHasTypeReps (Rep a),
Typeable a
) =>
a ->
Seq TypeRep
typeReps a = typeOf a <| gTypeReps (from a)

class GHasTypeReps f where
Expand All @@ -135,11 +142,11 @@ instance (GHasTypeReps a, GHasTypeReps b) => GHasTypeReps (a :+: b) where

-- | We do need to do anything for the metadata.
instance (GHasTypeReps a) => GHasTypeReps (M1 i c a) where
gTypeReps (M1 x) = gTypeReps x
gTypeReps (M1 x) = gTypeReps x

-- | And the only interesting case, get the type of a type constructor
instance (HasTypeReps a) => GHasTypeReps (K1 i a) where
gTypeReps (K1 x) = typeReps x
gTypeReps (K1 x) = typeReps x

--------------------------------------------------------------------------------
-- HasTypeReps instances
Expand All @@ -154,11 +161,14 @@ instance (Typeable a, HasTypeReps a) => HasTypeReps [a] where
instance (Typeable a, HasTypeReps a) => HasTypeReps (Set a) where
typeReps xs = typeOf xs <| foldMap typeReps xs

instance ( Typeable a
, Typeable b
, HasTypeReps a
, HasTypeReps b
) => HasTypeReps (a, b) where
instance
( Typeable a,
Typeable b,
HasTypeReps a,
HasTypeReps b
) =>
HasTypeReps (a, b)
where
typeReps t@(a, b) = typeOf t <| (typeReps a >< typeReps b)

instance HasTypeReps Bool where
Expand Down Expand Up @@ -208,7 +218,7 @@ instance HasTypeReps (SignedDSIGN MockDSIGN a) where
-- and a 'Word64'. For the 'ByteString' representation we return one character
-- per byte.
typeReps (SignedDSIGN (SigMockDSIGN (UnsafeHash bs) i)) =
typeOf i <| Seq.replicate (BS.length bs) (typeOf (undefined :: Char))
typeOf i <| Seq.replicate (SBS.length bs) (typeOf (undefined :: Char))

instance HasTypeReps (VerKeyDSIGN MockDSIGN) where
-- A mock verification key is just an 'Int'.
Expand Down
Loading

0 comments on commit 8a9d67b

Please sign in to comment.