Skip to content

Higher kinded classes #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion Data/Hashable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ import Data.Hashable.Class
import Data.Foldable (Foldable(foldr))
#endif

#if MIN_VERSION_base(4,9,0)
import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..))
#endif

#ifdef GENERICS
import Data.Hashable.Generic ()
#endif
Expand Down Expand Up @@ -218,7 +222,7 @@ import Data.Hashable.Generic ()

-- | A hashable value along with the result of the 'hash' function.
data Hashed a = Hashed a {-# UNPACK #-} !Int
deriving (Typeable,Show)
deriving (Typeable)

-- | Wrap a hashable value, caching the 'hash' function result.
hashed :: Hashable a => a -> Hashed a
Expand All @@ -235,6 +239,9 @@ instance Eq a => Eq (Hashed a) where
instance Ord a => Ord (Hashed a) where
Hashed a _ `compare` Hashed b _ = a `compare` b

instance Show a => Show (Hashed a) where
showsPrec d (Hashed a _) = showsUnaryWith showsPrec "hashed" d a

instance Hashable a => Hashable (Hashed a) where
hashWithSalt = defaultHashWithSalt
hash (Hashed _ h) = h
Expand All @@ -251,3 +258,23 @@ instance (IsString a, Hashable a) => IsString (Hashed a) where
instance Foldable Hashed where
foldr f acc (Hashed a _) = f a acc

-- instances for @Data.Functor.Classes@ higher rank typeclasses
-- in base-4.9 and onward.
#if MIN_VERSION_base(4,9,0)
instance Eq1 Hashed where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly for these instances. Shouldn't they be in Lifted? We might need to move Hashed to an internal module to avoid the instances being orphaned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that putting it into Data.Hashable.Class would be alright? Or would you prefer a new internal module?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has now been moved.

liftEq f (Hashed a ha) (Hashed b hb) = ha == hb && f a b

instance Ord1 Hashed where
liftCompare f (Hashed a _) (Hashed b _) = f a b

instance Show1 Hashed where
liftShowsPrec sp _ d (Hashed a _) = showsUnaryWith sp "hashed" d a
#endif

-- This function is copied from Data.Functor.Classes, which does
-- not export it.
showsUnaryWith :: (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is copied from Data.Functor.Classes, which does not export it.

Doesn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops. Thanks for pointing that out.

showsUnaryWith sp name d x = showParen (d > 10) $
showString name . showChar ' ' . sp 11 x