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
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 1 addition & 32 deletions Data/Hashable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ module Data.Hashable
, unhashed
) where

import Data.String (IsString(..))
import Data.Typeable (Typeable)
import Data.Foldable (Foldable(foldr))
import Data.Hashable.Class

#ifdef GENERICS
import Data.Hashable.Generic ()
#endif
Expand Down Expand Up @@ -212,32 +210,3 @@ import Data.Hashable.Generic ()
-- > hashWithSalt s (Months n) = s `hashWithSalt`
-- > (2::Int) `hashWithSalt` n

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

-- | Wrap a hashable value, caching the 'hash' function result.
hashed :: Hashable a => a -> Hashed a
hashed a = Hashed a (hash a)

-- | Unwrap hashed value.
unhashed :: Hashed a -> a
unhashed (Hashed a _) = a

-- | Uses precomputed hash to detect inequality faster
instance Eq a => Eq (Hashed a) where
Hashed a ha == Hashed b hb = ha == hb && a == b

instance Ord a => Ord (Hashed a) where
Hashed a _ `compare` Hashed b _ = a `compare` b

instance Hashable a => Hashable (Hashed a) where
hashWithSalt = defaultHashWithSalt
hash (Hashed _ h) = h

instance (IsString a, Hashable a) => IsString (Hashed a) where
fromString s = let r = fromString s in Hashed r (hash r)

instance Foldable Hashed where
foldr f acc (Hashed a _) = f a acc

Loading