-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4aab50
commit fad5cfe
Showing
46 changed files
with
731 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
shelley/chain-and-ledger/executable-spec/src/Cardano/Ledger/Compactible.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
|
||
module Cardano.Ledger.Compactible | ||
( -- * Compactible | ||
Compactible (..), | ||
Compact (..), | ||
) | ||
where | ||
|
||
import Cardano.Binary (FromCBOR (..), ToCBOR (..)) | ||
import Data.Kind (Type) | ||
import Data.Typeable (Typeable) | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
-- * Compactible | ||
|
||
-- | ||
-- Certain types may have a "presentation" form and a more compact | ||
-- representation that allows for more efficient memory usage. In this case, | ||
-- one should make instances of the 'Compactible' class for them. | ||
-------------------------------------------------------------------------------- | ||
|
||
class Compactible a where | ||
data CompactForm a :: Type | ||
toCompact :: a -> CompactForm a | ||
fromCompact :: CompactForm a -> a | ||
|
||
newtype Compact a = Compact {unCompact :: a} | ||
|
||
instance | ||
(Typeable a, Compactible a, ToCBOR (CompactForm a)) => | ||
ToCBOR (Compact a) | ||
where | ||
toCBOR = toCBOR . toCompact . unCompact | ||
|
||
instance | ||
(Typeable a, Compactible a, FromCBOR (CompactForm a)) => | ||
FromCBOR (Compact a) | ||
where | ||
fromCBOR = Compact . fromCompact <$> fromCBOR | ||
|
||
-- TODO: consider if this is better the other way around | ||
instance (Eq a, Compactible a) => Eq (CompactForm a) where | ||
a == b = fromCompact a == fromCompact b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.