-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This better reflects what it is currently about.
- Loading branch information
Showing
8 changed files
with
41 additions
and
41 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
34 changes: 17 additions & 17 deletions
34
...crypto-class/src/Cardano/Crypto/MEqOrd.hs → ...o-crypto-class/src/Cardano/Crypto/EqST.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 |
---|---|---|
@@ -1,58 +1,58 @@ | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
module Cardano.Crypto.MEqOrd where | ||
module Cardano.Crypto.EqST where | ||
|
||
import Control.Monad.Class.MonadST (MonadST) | ||
|
||
-- | Monadic flavor of 'Eq', for things that can only be compared in a monadic | ||
-- context. | ||
-- context that satisfies 'MonadST'. | ||
-- This is needed because we cannot have a sound 'Eq' instance on mlocked | ||
-- memory types. | ||
class MEq a where | ||
-- memory types, but we do need to compare them for equality in tests. | ||
class EqST a where | ||
equalsM :: MonadST m => a -> a -> m Bool | ||
|
||
nequalsM :: (MonadST m, MEq a) => a -> a -> m Bool | ||
nequalsM :: (MonadST m, EqST a) => a -> a -> m Bool | ||
nequalsM a b = not <$> equalsM a b | ||
|
||
-- | Infix version of 'equalsM' | ||
(==!) :: (MonadST m, MEq a) => a -> a -> m Bool | ||
(==!) :: (MonadST m, EqST a) => a -> a -> m Bool | ||
(==!) = equalsM | ||
infix 4 ==! | ||
|
||
-- | Infix version of 'nequalsM' | ||
(!=!) :: (MonadST m, MEq a) => a -> a -> m Bool | ||
(!=!) :: (MonadST m, EqST a) => a -> a -> m Bool | ||
(!=!) = nequalsM | ||
infix 4 !=! | ||
|
||
instance MEq a => MEq (Maybe a) where | ||
instance EqST a => EqST (Maybe a) where | ||
equalsM Nothing Nothing = pure True | ||
equalsM (Just a) (Just b) = equalsM a b | ||
equalsM _ _ = pure False | ||
|
||
instance (MEq a, MEq b) => MEq (Either a b) where | ||
instance (EqST a, EqST b) => EqST (Either a b) where | ||
equalsM (Left x) (Left y) = equalsM x y | ||
equalsM (Right x) (Right y) = equalsM x y | ||
equalsM _ _ = pure False | ||
|
||
instance (MEq a, MEq b) => MEq (a, b) where | ||
instance (EqST a, EqST b) => EqST (a, b) where | ||
equalsM (a, b) (a', b') = (&&) <$> equalsM a a' <*> equalsM b b' | ||
|
||
instance (MEq a, MEq b, MEq c) => MEq (a, b, c) where | ||
instance (EqST a, EqST b, EqST c) => EqST (a, b, c) where | ||
equalsM (a, b, c) (a', b', c') = equalsM ((a, b), c) ((a', b'), c') | ||
|
||
instance (MEq a, MEq b, MEq c, MEq d) => MEq (a, b, c, d) where | ||
instance (EqST a, EqST b, EqST c, EqST d) => EqST (a, b, c, d) where | ||
equalsM (a, b, c, d) (a', b', c', d') = equalsM ((a, b, c), d) ((a', b', c'), d') | ||
|
||
-- TODO: If anyone needs larger tuples, add more instances here... | ||
|
||
-- | Helper newtype, useful for defining 'MEq' in terms of 'Eq' for types that | ||
-- | Helper newtype, useful for defining 'EqST' in terms of 'Eq' for types that | ||
-- have sound 'Eq' instances, using @DerivingVia@. An 'Applicative' context | ||
-- must be provided for such instances to work, so this will generally require | ||
-- @StandaloneDeriving@ as well. | ||
-- | ||
-- Ex.: @deriving via PureEq Int instance Applicative m => MEq m Int@ | ||
newtype PureMEq a = PureMEq a | ||
-- Ex.: @deriving via PureEq Int instance Applicative m => EqST m Int@ | ||
newtype PureEqST a = PureEqST a | ||
|
||
instance Eq a => MEq (PureMEq a) where | ||
equalsM (PureMEq a) (PureMEq b) = pure (a == b) | ||
instance Eq a => EqST (PureEqST a) where | ||
equalsM (PureEqST a) (PureEqST b) = pure (a == 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