Skip to content

Add some hashed operations from classes that cannot be implemented #122

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 3 commits into from
Nov 9, 2016
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
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ env:
- CABALVER=1.18 GHCVER=7.6.3
# - CABALVER=1.18 GHCVER=7.8.1
# - CABALVER=1.18 GHCVER=7.8.2
- CABALVER=1.18 GHCVER=7.8.3
- CABALVER=1.22 GHCVER=7.10.1
- CABALVER=1.18 GHCVER=7.8.4
- CABALVER=1.22 GHCVER=7.10.3
- CABALVER=1.24 GHCVER=8.0.1
# - CABALVER=head GHCVER=head
# - HPVER=2013.2.0.0
Expand Down Expand Up @@ -115,8 +115,9 @@ script:
# Try to compile tests and benchmarks, run tests
# For some reason doesn't work with old cabal
#
# TODO: don't build tests with cabal 1.24 (ghc 8.0) yet
- if [ ! $CABALVER = "1.16" -a ! $CABALVER = "1.24" ]; then
# Disable tests on GHC 7.6.3 and 7.8.4, as there's something weird happening
# because of -inplace and globally installed hashable
- if [ ! $CABALVER = "1.16" -a ! $GHCVER = "7.6.3" -a ! $GHCVER = "7.8.4" ]; then
cabal install HUnit QuickCheck criterion random siphash test-framework test-framework-hunit test-framework-quickcheck2;
cabal configure -v2 --enable-tests --enable-benchmarks;
cabal test;
Expand Down
3 changes: 2 additions & 1 deletion Data/Hashable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ module Data.Hashable
, Hashed
, hashed
, unhashed
, mapHashed
, traverseHashed
) where

import Data.Hashable.Class
Expand Down Expand Up @@ -209,4 +211,3 @@ import Data.Hashable.Generic ()
-- > (1::Int) `hashWithSalt` n
-- > hashWithSalt s (Months n) = s `hashWithSalt`
-- > (2::Int) `hashWithSalt` n

28 changes: 23 additions & 5 deletions Data/Hashable/Class.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
ScopedTypeVariables, UnliftedFFITypes #-}
ScopedTypeVariables, UnliftedFFITypes, DeriveDataTypeable #-}
#ifdef GENERICS
{-# LANGUAGE DefaultSignatures, FlexibleContexts, GADTs,
MultiParamTypeClasses, EmptyDataDecls #-}
Expand Down Expand Up @@ -50,6 +50,8 @@ module Data.Hashable.Class
, Hashed
, hashed
, unhashed
, mapHashed
, traverseHashed
) where

import Control.Applicative (Const(..))
Expand All @@ -66,7 +68,7 @@ import qualified Data.Text as T
import qualified Data.Text.Array as TA
import qualified Data.Text.Internal as T
import qualified Data.Text.Lazy as TL
import Data.Typeable
import Data.Typeable (Typeable, TypeRep)
import Data.Version (Version(..))
import Data.Word (Word8, Word16, Word32, Word64)
import Foreign.C (CString)
Expand All @@ -80,7 +82,10 @@ import System.IO.Unsafe (unsafePerformIO)
import System.Mem.StableName
import Data.Unique (Unique, hashUnique)

#if !(MIN_VERSION_base(4,7,0))
-- As we use qualified F.Foldable, we don't get warnings with newer base
import qualified Data.Foldable as F

#if MIN_VERSION_base(4,7,0)
import Data.Proxy (Proxy)
#endif

Expand All @@ -97,10 +102,13 @@ import GHC.Generics
#endif

#if __GLASGOW_HASKELL__ >= 710
import Data.Typeable (typeRepFingerprint)
import GHC.Fingerprint.Type(Fingerprint(..))
#elif __GLASGOW_HASKELL__ >= 702
import Data.Typeable.Internal(TypeRep(..))
import Data.Typeable.Internal (TypeRep (..))
import GHC.Fingerprint.Type(Fingerprint(..))
#elif __GLASGOW_HASKELL__ >= 606
import Data.Typeable (typeRepKey)
#endif

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

#if MIN_VERSION_base(4,7,0)
instance Hashable (Proxy a) where
hash _ = 0
hashWithSalt s _ = s

instance Hashable1 Proxy where
liftHashWithSalt _ s _ = s
#endif

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

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

-- | 'Hashed' cannot be 'Functor'
mapHashed :: Hashable b => (a -> b) -> Hashed a -> Hashed b
mapHashed f (Hashed a _) = hashed (f a)

-- | 'Hashed' cannot be 'Traversable'
traverseHashed :: (Hashable b, Functor f) => (a -> f b) -> Hashed a -> f (Hashed b)
traverseHashed f (Hashed a _) = fmap hashed (f a)

-- instances for @Data.Functor.Classes@ higher rank typeclasses
-- in base-4.9 and onward.
#if MIN_VERSION_base(4,9,0)
Expand Down