Skip to content

Commit a0c288e

Browse files
authored
Merge pull request #122 from phadej/hashed-operators
Add some hashed operations from classes that cannot be implemented
2 parents ed36538 + 76b01e9 commit a0c288e

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

.travis.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ env:
2121
- CABALVER=1.18 GHCVER=7.6.3
2222
# - CABALVER=1.18 GHCVER=7.8.1
2323
# - CABALVER=1.18 GHCVER=7.8.2
24-
- CABALVER=1.18 GHCVER=7.8.3
25-
- CABALVER=1.22 GHCVER=7.10.1
24+
- CABALVER=1.18 GHCVER=7.8.4
25+
- CABALVER=1.22 GHCVER=7.10.3
2626
- CABALVER=1.24 GHCVER=8.0.1
2727
# - CABALVER=head GHCVER=head
2828
# - HPVER=2013.2.0.0
@@ -115,8 +115,9 @@ script:
115115
# Try to compile tests and benchmarks, run tests
116116
# For some reason doesn't work with old cabal
117117
#
118-
# TODO: don't build tests with cabal 1.24 (ghc 8.0) yet
119-
- if [ ! $CABALVER = "1.16" -a ! $CABALVER = "1.24" ]; then
118+
# Disable tests on GHC 7.6.3 and 7.8.4, as there's something weird happening
119+
# because of -inplace and globally installed hashable
120+
- if [ ! $CABALVER = "1.16" -a ! $GHCVER = "7.6.3" -a ! $GHCVER = "7.8.4" ]; then
120121
cabal install HUnit QuickCheck criterion random siphash test-framework test-framework-hunit test-framework-quickcheck2;
121122
cabal configure -v2 --enable-tests --enable-benchmarks;
122123
cabal test;

Data/Hashable.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ module Data.Hashable
6868
, Hashed
6969
, hashed
7070
, unhashed
71+
, mapHashed
72+
, traverseHashed
7173
) where
7274

7375
import Data.Hashable.Class
@@ -209,4 +211,3 @@ import Data.Hashable.Generic ()
209211
-- > (1::Int) `hashWithSalt` n
210212
-- > hashWithSalt s (Months n) = s `hashWithSalt`
211213
-- > (2::Int) `hashWithSalt` n
212-

Data/Hashable/Class.hs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
2-
ScopedTypeVariables, UnliftedFFITypes #-}
2+
ScopedTypeVariables, UnliftedFFITypes, DeriveDataTypeable #-}
33
#ifdef GENERICS
44
{-# LANGUAGE DefaultSignatures, FlexibleContexts, GADTs,
55
MultiParamTypeClasses, EmptyDataDecls #-}
@@ -50,6 +50,8 @@ module Data.Hashable.Class
5050
, Hashed
5151
, hashed
5252
, unhashed
53+
, mapHashed
54+
, traverseHashed
5355
) where
5456

5557
import Control.Applicative (Const(..))
@@ -66,7 +68,7 @@ import qualified Data.Text as T
6668
import qualified Data.Text.Array as TA
6769
import qualified Data.Text.Internal as T
6870
import qualified Data.Text.Lazy as TL
69-
import Data.Typeable
71+
import Data.Typeable (Typeable, TypeRep)
7072
import Data.Version (Version(..))
7173
import Data.Word (Word8, Word16, Word32, Word64)
7274
import Foreign.C (CString)
@@ -80,7 +82,10 @@ import System.IO.Unsafe (unsafePerformIO)
8082
import System.Mem.StableName
8183
import Data.Unique (Unique, hashUnique)
8284

83-
#if !(MIN_VERSION_base(4,7,0))
85+
-- As we use qualified F.Foldable, we don't get warnings with newer base
86+
import qualified Data.Foldable as F
87+
88+
#if MIN_VERSION_base(4,7,0)
8489
import Data.Proxy (Proxy)
8590
#endif
8691

@@ -97,10 +102,13 @@ import GHC.Generics
97102
#endif
98103

99104
#if __GLASGOW_HASKELL__ >= 710
105+
import Data.Typeable (typeRepFingerprint)
100106
import GHC.Fingerprint.Type(Fingerprint(..))
101107
#elif __GLASGOW_HASKELL__ >= 702
102-
import Data.Typeable.Internal(TypeRep(..))
108+
import Data.Typeable.Internal (TypeRep (..))
103109
import GHC.Fingerprint.Type(Fingerprint(..))
110+
#elif __GLASGOW_HASKELL__ >= 606
111+
import Data.Typeable (typeRepKey)
104112
#endif
105113

106114
#if __GLASGOW_HASKELL__ >= 703
@@ -747,12 +755,14 @@ instance Hashable a => Hashable1 (Const a) where
747755
instance Hashable2 Const where
748756
liftHashWithSalt2 f _ salt (Const x) = f salt x
749757

758+
#if MIN_VERSION_base(4,7,0)
750759
instance Hashable (Proxy a) where
751760
hash _ = 0
752761
hashWithSalt s _ = s
753762

754763
instance Hashable1 Proxy where
755764
liftHashWithSalt _ s _ = s
765+
#endif
756766

757767
-- instances formerly provided by 'semigroups' package
758768
#if MIN_VERSION_base(4,9,0)
@@ -842,9 +852,17 @@ instance Hashable1 Hashed where
842852
instance (IsString a, Hashable a) => IsString (Hashed a) where
843853
fromString s = let r = fromString s in Hashed r (hash r)
844854

845-
instance Foldable Hashed where
855+
instance F.Foldable Hashed where
846856
foldr f acc (Hashed a _) = f a acc
847857

858+
-- | 'Hashed' cannot be 'Functor'
859+
mapHashed :: Hashable b => (a -> b) -> Hashed a -> Hashed b
860+
mapHashed f (Hashed a _) = hashed (f a)
861+
862+
-- | 'Hashed' cannot be 'Traversable'
863+
traverseHashed :: (Hashable b, Functor f) => (a -> f b) -> Hashed a -> f (Hashed b)
864+
traverseHashed f (Hashed a _) = fmap hashed (f a)
865+
848866
-- instances for @Data.Functor.Classes@ higher rank typeclasses
849867
-- in base-4.9 and onward.
850868
#if MIN_VERSION_base(4,9,0)

0 commit comments

Comments
 (0)