Skip to content

Commit b6b9f6d

Browse files
committed
moved Hashed into Class module
1 parent a7f702b commit b6b9f6d

File tree

2 files changed

+59
-62
lines changed

2 files changed

+59
-62
lines changed

Data/Hashable.hs

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,8 @@ module Data.Hashable
7070
, unhashed
7171
) where
7272

73-
import Data.String (IsString(..))
74-
import Data.Typeable (Typeable)
7573
import Data.Hashable.Class
7674

77-
#if !(MIN_VERSION_base(4,8,0))
78-
import Data.Foldable (Foldable(foldr))
79-
#endif
80-
81-
#if MIN_VERSION_base(4,9,0)
82-
import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),showsUnaryWith)
83-
#endif
84-
8575
#ifdef GENERICS
8676
import Data.Hashable.Generic ()
8777
#endif
@@ -220,55 +210,3 @@ import Data.Hashable.Generic ()
220210
-- > hashWithSalt s (Months n) = s `hashWithSalt`
221211
-- > (2::Int) `hashWithSalt` n
222212

223-
-- | A hashable value along with the result of the 'hash' function.
224-
data Hashed a = Hashed a {-# UNPACK #-} !Int
225-
deriving (Typeable)
226-
227-
-- | Wrap a hashable value, caching the 'hash' function result.
228-
hashed :: Hashable a => a -> Hashed a
229-
hashed a = Hashed a (hash a)
230-
231-
-- | Unwrap hashed value.
232-
unhashed :: Hashed a -> a
233-
unhashed (Hashed a _) = a
234-
235-
-- | Uses precomputed hash to detect inequality faster
236-
instance Eq a => Eq (Hashed a) where
237-
Hashed a ha == Hashed b hb = ha == hb && a == b
238-
239-
instance Ord a => Ord (Hashed a) where
240-
Hashed a _ `compare` Hashed b _ = a `compare` b
241-
242-
instance Show a => Show (Hashed a) where
243-
showsPrec d (Hashed a _) = showParen (d > 10) $
244-
showString "hashed" . showChar ' ' . showsPrec 11 a
245-
246-
instance Hashable (Hashed a) where
247-
hashWithSalt = defaultHashWithSalt
248-
hash (Hashed _ h) = h
249-
250-
-- This instance is a little unsettling. It is unusal for
251-
-- 'liftHashWithSalt' to ignore its first argument when a
252-
-- value is actually available for it to work on.
253-
instance Hashable1 Hashed where
254-
liftHashWithSalt _ s (Hashed _ h) = defaultHashWithSalt s h
255-
256-
instance (IsString a, Hashable a) => IsString (Hashed a) where
257-
fromString s = let r = fromString s in Hashed r (hash r)
258-
259-
instance Foldable Hashed where
260-
foldr f acc (Hashed a _) = f a acc
261-
262-
-- instances for @Data.Functor.Classes@ higher rank typeclasses
263-
-- in base-4.9 and onward.
264-
#if MIN_VERSION_base(4,9,0)
265-
instance Eq1 Hashed where
266-
liftEq f (Hashed a ha) (Hashed b hb) = ha == hb && f a b
267-
268-
instance Ord1 Hashed where
269-
liftCompare f (Hashed a _) (Hashed b _) = f a b
270-
271-
instance Show1 Hashed where
272-
liftShowsPrec sp _ d (Hashed a _) = showsUnaryWith sp "hashed" d a
273-
#endif
274-

Data/Hashable/Class.hs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ module Data.Hashable.Class
4646
, hashWithSalt1
4747
, hashWithSalt2
4848
, defaultLiftHashWithSalt
49+
-- * Caching hashes
50+
, Hashed
51+
, hashed
52+
, unhashed
4953
) where
5054

5155
import Control.Applicative (Const(..))
@@ -148,12 +152,15 @@ import GHC.Exts (Word(..))
148152
#if MIN_VERSION_base(4,9,0)
149153
import qualified Data.List.NonEmpty as NE
150154
import Data.Semigroup
155+
import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),showsUnaryWith)
151156

152157
import Data.Functor.Compose (Compose(..))
153158
import qualified Data.Functor.Product as FP
154159
import qualified Data.Functor.Sum as FS
155160
#endif
156161

162+
import Data.String (IsString(..))
163+
157164
#include "MachDeps.h"
158165

159166
infixl 0 `hashWithSalt`
@@ -799,4 +806,56 @@ instance (Hashable1 f, Hashable1 g, Hashable a) => Hashable (FS.Sum f g a) where
799806
hashWithSalt = hashWithSalt1
800807
#endif
801808

809+
-- | A hashable value along with the result of the 'hash' function.
810+
data Hashed a = Hashed a {-# UNPACK #-} !Int
811+
deriving (Typeable)
812+
813+
-- | Wrap a hashable value, caching the 'hash' function result.
814+
hashed :: Hashable a => a -> Hashed a
815+
hashed a = Hashed a (hash a)
816+
817+
-- | Unwrap hashed value.
818+
unhashed :: Hashed a -> a
819+
unhashed (Hashed a _) = a
820+
821+
-- | Uses precomputed hash to detect inequality faster
822+
instance Eq a => Eq (Hashed a) where
823+
Hashed a ha == Hashed b hb = ha == hb && a == b
824+
825+
instance Ord a => Ord (Hashed a) where
826+
Hashed a _ `compare` Hashed b _ = a `compare` b
827+
828+
instance Show a => Show (Hashed a) where
829+
showsPrec d (Hashed a _) = showParen (d > 10) $
830+
showString "hashed" . showChar ' ' . showsPrec 11 a
831+
832+
instance Hashable (Hashed a) where
833+
hashWithSalt = defaultHashWithSalt
834+
hash (Hashed _ h) = h
835+
836+
-- This instance is a little unsettling. It is unusal for
837+
-- 'liftHashWithSalt' to ignore its first argument when a
838+
-- value is actually available for it to work on.
839+
instance Hashable1 Hashed where
840+
liftHashWithSalt _ s (Hashed _ h) = defaultHashWithSalt s h
841+
842+
instance (IsString a, Hashable a) => IsString (Hashed a) where
843+
fromString s = let r = fromString s in Hashed r (hash r)
844+
845+
instance Foldable Hashed where
846+
foldr f acc (Hashed a _) = f a acc
847+
848+
-- instances for @Data.Functor.Classes@ higher rank typeclasses
849+
-- in base-4.9 and onward.
850+
#if MIN_VERSION_base(4,9,0)
851+
instance Eq1 Hashed where
852+
liftEq f (Hashed a ha) (Hashed b hb) = ha == hb && f a b
853+
854+
instance Ord1 Hashed where
855+
liftCompare f (Hashed a _) (Hashed b _) = f a b
856+
857+
instance Show1 Hashed where
858+
liftShowsPrec sp _ d (Hashed a _) = showsUnaryWith sp "hashed" d a
859+
#endif
860+
802861

0 commit comments

Comments
 (0)