-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathClass.hs
More file actions
1053 lines (867 loc) · 34.9 KB
/
Copy pathClass.hs
File metadata and controls
1053 lines (867 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE BangPatterns, CPP, MagicHash,
ScopedTypeVariables, UnliftedFFITypes, DeriveDataTypeable,
DefaultSignatures, FlexibleContexts, TypeFamilies,
MultiParamTypeClasses, CApiFFI #-}
{-# LANGUAGE Trustworthy #-}
#if __GLASGOW_HASKELL__ >= 801
{-# LANGUAGE PolyKinds #-} -- For TypeRep instances
#endif
{-# OPTIONS_GHC -fno-warn-deprecations #-}
------------------------------------------------------------------------
-- |
-- Module : Data.Hashable.Class
-- Copyright : (c) Milan Straka 2010
-- (c) Johan Tibell 2011
-- (c) Bryan O'Sullivan 2011, 2012
-- SPDX-License-Identifier : BSD-3-Clause
-- Maintainer : johan.tibell@gmail.com
-- Stability : provisional
-- Portability : portable
--
-- This module defines a class, 'Hashable', for types that can be
-- converted to a hash value. This class exists for the benefit of
-- hashing-based data structures. The module provides instances for
-- most standard types.
module Data.Hashable.Class
(
-- * Computing hash values
Hashable(..)
, Hashable1(..)
, Hashable2(..)
-- ** Support for generics
, genericHashWithSalt
, genericLiftHashWithSalt
, GHashable(..)
, HashArgs(..)
, Zero
, One
-- * Creating new instances
, hashUsing
, hashPtr
, hashPtrWithSalt
, hashByteArray
, hashByteArrayWithSalt
, defaultHashWithSalt
-- * Higher Rank Functions
, hashWithSalt1
, hashWithSalt2
, defaultLiftHashWithSalt
-- * Caching hashes
, Hashed
, hashed
, unhashed
, mapHashed
, traverseHashed
) where
import Control.Applicative (Const(..), (<|>))
import Control.Exception (assert)
import Control.DeepSeq (NFData(rnf))
import Data.Bits (shiftL, shiftR, xor)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Unsafe as B
import Data.Complex (Complex(..))
import Data.Int (Int8, Int16, Int32, Int64)
import Data.List (foldl')
import Data.Ratio (Ratio, denominator, numerator)
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.Version (Version(..))
import Data.Word (Word8, Word16, Word32, Word64)
import Foreign.C (CString)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (Ptr, FunPtr, IntPtr, WordPtr, castPtr, castFunPtrToPtr, ptrToIntPtr)
import Foreign.Storable (alignment, peek, sizeOf)
import GHC.Base (ByteArray#)
import GHC.Conc (ThreadId(..))
import GHC.Prim (ThreadId#)
import System.Environment (lookupEnv)
import System.IO.Unsafe (unsafeDupablePerformIO, unsafePerformIO)
import System.Mem.StableName
import Data.Unique (Unique, hashUnique)
import Text.Read (readMaybe)
-- 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
#if MIN_VERSION_base(4,7,0)
import Data.Fixed (Fixed(..))
#else
import Data.Fixed (Fixed)
import Unsafe.Coerce (unsafeCoerce)
#endif
#if MIN_VERSION_base(4,8,0)
import Data.Functor.Identity (Identity(..))
#endif
import GHC.Generics
#if MIN_VERSION_base(4,10,0)
import Type.Reflection (Typeable, TypeRep, SomeTypeRep(..))
import Type.Reflection.Unsafe (typeRepFingerprint)
import GHC.Fingerprint.Type(Fingerprint(..))
#elif MIN_VERSION_base(4,8,0)
import Data.Typeable (typeRepFingerprint, Typeable, TypeRep)
import GHC.Fingerprint.Type(Fingerprint(..))
#else
import Data.Typeable.Internal (Typeable, TypeRep (..))
import GHC.Fingerprint.Type(Fingerprint(..))
#endif
import Foreign.C.Types (CInt(..))
#if !(MIN_VERSION_base(4,8,0))
import Data.Word (Word)
#endif
#if MIN_VERSION_base(4,7,0)
import Data.Bits (finiteBitSize)
#else
import Data.Bits (bitSize)
#endif
#if !(MIN_VERSION_bytestring(0,10,0))
import qualified Data.ByteString.Lazy.Internal as BL -- foldlChunks
#endif
#if MIN_VERSION_bytestring(0,10,4)
import qualified Data.ByteString.Short.Internal as BSI
#endif
#ifdef VERSION_ghc_bignum
import GHC.Num.BigNat (BigNat (..))
import GHC.Num.Integer (Integer (..))
import GHC.Num.Natural (Natural (..))
import GHC.Exts (Int (..), sizeofByteArray#)
#endif
#ifdef VERSION_integer_gmp
# if MIN_VERSION_integer_gmp(1,0,0)
# define MIN_VERSION_integer_gmp_1_0_0
# endif
import GHC.Exts (Int(..))
import GHC.Integer.GMP.Internals (Integer(..))
# if defined(MIN_VERSION_integer_gmp_1_0_0)
import GHC.Exts (sizeofByteArray#)
import GHC.Integer.GMP.Internals (BigNat(BN#))
# endif
#endif
#if MIN_VERSION_base(4,8,0)
import Data.Void (Void, absurd)
import GHC.Exts (Word(..))
#ifndef VERSION_ghc_bignum
import GHC.Natural (Natural(..))
#endif
#endif
#if MIN_VERSION_base(4,9,0)
import qualified Data.List.NonEmpty as NE
import Data.Semigroup
import Data.Functor.Classes (Eq1(..),Ord1(..),Show1(..),showsUnaryWith)
import Data.Functor.Compose (Compose(..))
import qualified Data.Functor.Product as FP
import qualified Data.Functor.Sum as FS
#endif
import Data.String (IsString(..))
#if MIN_VERSION_base(4,9,0)
import Data.Kind (Type)
#else
#define Type *
#endif
#include "MachDeps.h"
------------------------------------------------------------------------
-- * Default salt
getInitialSeed :: IO (Maybe Word64)
getInitialSeed = do
initialSeedFromEnv' <- lookupEnv "HASHABLE_INITIAL_SEED"
let initialSeedFromEnv = initialSeedFromEnv' >>= readMaybe
initialSeedFromRandom <- getRandomInitialSeed
return $ initialSeedFromEnv <|> initialSeedFromRandom
where
#ifdef HASHABLE_RANDOM_SEED
getRandomInitialSeed = Just <$> initialSeedC
foreign import capi "HsHashable.h hs_hashable_init" initialSeedC :: IO Word64
#else
getRandomInitialSeed = pure Nothing
#endif
getDefaultSalt :: IO Int
getDefaultSalt = maybe defaultSalt' (hashWithSalt defaultSalt') <$> getInitialSeed
where
defaultSalt' :: Int
#if WORD_SIZE_IN_BITS == 64
defaultSalt' = -3750763034362895579 -- 14695981039346656037 :: Int64
#else
defaultSalt' = -2128831035 -- 2166136261 :: Int32
#endif
-- | A default salt used in the implementation of 'hash'.
defaultSalt :: Int
defaultSalt = unsafePerformIO getDefaultSalt
{-# NOINLINE defaultSalt #-}
------------------------------------------------------------------------
-- * Computing hash values
infixl 0 `hashWithSalt`
-- | The class of types that can be converted to a hash value.
--
-- Minimal implementation: 'hashWithSalt'.
--
-- /Note:/ the hash is not guaranteed to be stable across
-- library versions, operating systems or architectures.
-- For stable hashing use named hashes: SHA256, CRC32 etc.
--
-- If you are looking for 'Hashable' instance in @time@ package,
-- check [time-compat](https://hackage.haskell.org/package/time-compat)
--
class Hashable a where
-- | Return a hash value for the argument, using the given salt.
--
-- The general contract of 'hashWithSalt' is:
--
-- * If two values are equal according to the '==' method, then
-- applying the 'hashWithSalt' method on each of the two values
-- /must/ produce the same integer result if the same salt is
-- used in each case.
--
-- * It is /not/ required that if two values are unequal
-- according to the '==' method, then applying the
-- 'hashWithSalt' method on each of the two values must produce
-- distinct integer results. However, the programmer should be
-- aware that producing distinct integer results for unequal
-- values may improve the performance of hashing-based data
-- structures.
--
-- * This method can be used to compute different hash values for
-- the same input by providing a different salt in each
-- application of the method. This implies that any instance
-- that defines 'hashWithSalt' /must/ make use of the salt in
-- its implementation.
--
-- * 'hashWithSalt' may return negative 'Int' values.
--
hashWithSalt :: Int -> a -> Int
-- | Like 'hashWithSalt', but no salt is used. The default
-- implementation uses 'hashWithSalt' with some default salt.
-- Instances might want to implement this method to provide a more
-- efficient implementation than the default implementation.
hash :: a -> Int
hash = hashWithSalt defaultSalt
default hashWithSalt :: (Generic a, GHashable Zero (Rep a)) => Int -> a -> Int
hashWithSalt = genericHashWithSalt
{-# INLINE hashWithSalt #-}
-- | Generic 'hashWithSalt'.
--
-- @since 1.3.0.0
genericHashWithSalt :: (Generic a, GHashable Zero (Rep a)) => Int -> a -> Int
genericHashWithSalt = \salt -> ghashWithSalt HashArgs0 salt . from
{-# INLINE genericHashWithSalt #-}
data Zero
data One
data family HashArgs arity a :: Type
data instance HashArgs Zero a = HashArgs0
newtype instance HashArgs One a = HashArgs1 (Int -> a -> Int)
-- | The class of types that can be generically hashed.
class GHashable arity f where
ghashWithSalt :: HashArgs arity a -> Int -> f a -> Int
class Hashable1 t where
-- | Lift a hashing function through the type constructor.
liftHashWithSalt :: (Int -> a -> Int) -> Int -> t a -> Int
default liftHashWithSalt :: (Generic1 t, GHashable One (Rep1 t)) => (Int -> a -> Int) -> Int -> t a -> Int
liftHashWithSalt = genericLiftHashWithSalt
{-# INLINE liftHashWithSalt #-}
-- | Generic 'liftHashWithSalt'.
--
-- @since 1.3.0.0
genericLiftHashWithSalt :: (Generic1 t, GHashable One (Rep1 t)) => (Int -> a -> Int) -> Int -> t a -> Int
genericLiftHashWithSalt = \h salt -> ghashWithSalt (HashArgs1 h) salt . from1
{-# INLINE genericLiftHashWithSalt #-}
class Hashable2 t where
-- | Lift a hashing function through the binary type constructor.
liftHashWithSalt2 :: (Int -> a -> Int) -> (Int -> b -> Int) -> Int -> t a b -> Int
-- | Lift the 'hashWithSalt' function through the type constructor.
--
-- > hashWithSalt1 = liftHashWithSalt hashWithSalt
hashWithSalt1 :: (Hashable1 f, Hashable a) => Int -> f a -> Int
hashWithSalt1 = liftHashWithSalt hashWithSalt
-- | Lift the 'hashWithSalt' function through the type constructor.
--
-- > hashWithSalt2 = liftHashWithSalt2 hashWithSalt hashWithSalt
hashWithSalt2 :: (Hashable2 f, Hashable a, Hashable b) => Int -> f a b -> Int
hashWithSalt2 = liftHashWithSalt2 hashWithSalt hashWithSalt
-- | Lift the 'hashWithSalt' function halfway through the type constructor.
-- This function makes a suitable default implementation of 'liftHashWithSalt',
-- given that the type constructor @t@ in question can unify with @f a@.
defaultLiftHashWithSalt :: (Hashable2 f, Hashable a) => (Int -> b -> Int) -> Int -> f a b -> Int
defaultLiftHashWithSalt h = liftHashWithSalt2 hashWithSalt h
-- | Since we support a generic implementation of 'hashWithSalt' we
-- cannot also provide a default implementation for that method for
-- the non-generic instance use case. Instead we provide
-- 'defaultHashWith'.
defaultHashWithSalt :: Hashable a => Int -> a -> Int
defaultHashWithSalt salt x = salt `combine` hash x
-- | Transform a value into a 'Hashable' value, then hash the
-- transformed value using the given salt.
--
-- This is a useful shorthand in cases where a type can easily be
-- mapped to another type that is already an instance of 'Hashable'.
-- Example:
--
-- > data Foo = Foo | Bar
-- > deriving (Enum)
-- >
-- > instance Hashable Foo where
-- > hashWithSalt = hashUsing fromEnum
--
-- @since 1.2.0.0
hashUsing :: (Hashable b) =>
(a -> b) -- ^ Transformation function.
-> Int -- ^ Salt.
-> a -- ^ Value to transform.
-> Int
hashUsing f salt x = hashWithSalt salt (f x)
{-# INLINE hashUsing #-}
instance Hashable Int where
hash = id
hashWithSalt = defaultHashWithSalt
instance Hashable Int8 where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Int16 where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Int32 where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Int64 where
hash n
#if MIN_VERSION_base(4,7,0)
| finiteBitSize (undefined :: Int) == 64 = fromIntegral n
#else
| bitSize (undefined :: Int) == 64 = fromIntegral n
#endif
| otherwise = fromIntegral (fromIntegral n `xor`
(fromIntegral n `shiftR` 32 :: Word64))
hashWithSalt = defaultHashWithSalt
instance Hashable Word where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Word8 where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Word16 where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Word32 where
hash = fromIntegral
hashWithSalt = defaultHashWithSalt
instance Hashable Word64 where
hash n
#if MIN_VERSION_base(4,7,0)
| finiteBitSize (undefined :: Int) == 64 = fromIntegral n
#else
| bitSize (undefined :: Int) == 64 = fromIntegral n
#endif
| otherwise = fromIntegral (n `xor` (n `shiftR` 32))
hashWithSalt = defaultHashWithSalt
instance Hashable () where
hash = fromEnum
hashWithSalt = defaultHashWithSalt
instance Hashable Bool where
hash = fromEnum
hashWithSalt = defaultHashWithSalt
instance Hashable Ordering where
hash = fromEnum
hashWithSalt = defaultHashWithSalt
instance Hashable Char where
hash = fromEnum
hashWithSalt = defaultHashWithSalt
#if defined(MIN_VERSION_integer_gmp_1_0_0) || defined(VERSION_ghc_bignum)
instance Hashable BigNat where
hashWithSalt salt (BN# ba) = hashByteArrayWithSalt ba 0 numBytes salt
`hashWithSalt` size
where
size = numBytes `quot` SIZEOF_HSWORD
numBytes = I# (sizeofByteArray# ba)
#endif
#if MIN_VERSION_base(4,8,0)
instance Hashable Natural where
# if defined(VERSION_ghc_bignum)
hash (NS n) = hash (W# n)
hash (NB bn) = hash (BN# bn)
hashWithSalt salt (NS n) = hashWithSalt salt (W# n)
hashWithSalt salt (NB bn) = hashWithSalt salt (BN# bn)
# else
# if defined(MIN_VERSION_integer_gmp_1_0_0)
hash (NatS# n) = hash (W# n)
hash (NatJ# bn) = hash bn
hashWithSalt salt (NatS# n) = hashWithSalt salt (W# n)
hashWithSalt salt (NatJ# bn) = hashWithSalt salt bn
# else
hash (Natural n) = hash n
hashWithSalt salt (Natural n) = hashWithSalt salt n
# endif
# endif
#endif
instance Hashable Integer where
#if defined(VERSION_ghc_bignum)
hash (IS n) = I# n
hash (IP bn) = hash (BN# bn)
hash (IN bn) = negate (hash (BN# bn))
hashWithSalt salt (IS n) = hashWithSalt salt (I# n)
hashWithSalt salt (IP bn) = hashWithSalt salt (BN# bn)
hashWithSalt salt (IN bn) = negate (hashWithSalt salt (BN# bn))
#else
#if defined(VERSION_integer_gmp)
# if defined(MIN_VERSION_integer_gmp_1_0_0)
hash (S# n) = (I# n)
hash (Jp# bn) = hash bn
hash (Jn# bn) = negate (hash bn)
hashWithSalt salt (S# n) = hashWithSalt salt (I# n)
hashWithSalt salt (Jp# bn) = hashWithSalt salt bn
hashWithSalt salt (Jn# bn) = negate (hashWithSalt salt bn)
# else
hash (S# int) = I# int
hash n@(J# size# byteArray)
| n >= minInt && n <= maxInt = fromInteger n :: Int
| otherwise = let size = I# size#
numBytes = SIZEOF_HSWORD * abs size
in hashByteArrayWithSalt byteArray 0 numBytes defaultSalt
`hashWithSalt` size
where minInt = fromIntegral (minBound :: Int)
maxInt = fromIntegral (maxBound :: Int)
hashWithSalt salt (S# n) = hashWithSalt salt (I# n)
hashWithSalt salt n@(J# size# byteArray)
| n >= minInt && n <= maxInt = hashWithSalt salt (fromInteger n :: Int)
| otherwise = let size = I# size#
numBytes = SIZEOF_HSWORD * abs size
in hashByteArrayWithSalt byteArray 0 numBytes salt
`hashWithSalt` size
where minInt = fromIntegral (minBound :: Int)
maxInt = fromIntegral (maxBound :: Int)
# endif
#else
hashWithSalt salt = foldl' hashWithSalt salt . go
where
go n | inBounds n = [fromIntegral n :: Int]
| otherwise = fromIntegral n : go (n `shiftR` WORD_SIZE_IN_BITS)
maxInt = fromIntegral (maxBound :: Int)
inBounds x = x >= fromIntegral (minBound :: Int) && x <= maxInt
#endif
#endif
instance Hashable a => Hashable (Complex a) where
{-# SPECIALIZE instance Hashable (Complex Double) #-}
{-# SPECIALIZE instance Hashable (Complex Float) #-}
hash (r :+ i) = hash r `hashWithSalt` i
hashWithSalt = hashWithSalt1
instance Hashable1 Complex where
liftHashWithSalt h s (r :+ i) = s `h` r `h` i
#if MIN_VERSION_base(4,9,0)
-- Starting with base-4.9, numerator/denominator don't need 'Integral' anymore
instance Hashable a => Hashable (Ratio a) where
#else
instance (Integral a, Hashable a) => Hashable (Ratio a) where
#endif
{-# SPECIALIZE instance Hashable (Ratio Integer) #-}
hash a = hash (numerator a) `hashWithSalt` denominator a
hashWithSalt s a = s `hashWithSalt` numerator a `hashWithSalt` denominator a
-- | __Note__: prior to @hashable-1.3.0.0@, @hash 0.0 /= hash (-0.0)@
--
-- The 'hash' of NaN is not well defined.
--
-- @since 1.3.0.0
instance Hashable Float where
hash x
| x == -0.0 || x == 0.0 = 0 -- see note in 'Hashable Double'
| isIEEE x =
assert (sizeOf x >= sizeOf (0::Word32) &&
alignment x >= alignment (0::Word32)) $
hash ((unsafeDupablePerformIO $ with x $ peek . castPtr) :: Word32)
| otherwise = hash (show x)
hashWithSalt = defaultHashWithSalt
-- | __Note__: prior to @hashable-1.3.0.0@, @hash 0.0 /= hash (-0.0)@
--
-- The 'hash' of NaN is not well defined.
--
-- @since 1.3.0.0
instance Hashable Double where
hash x
| x == -0.0 || x == 0.0 = 0 -- s.t. @hash -0.0 == hash 0.0@ ; see #173
| isIEEE x =
assert (sizeOf x >= sizeOf (0::Word64) &&
alignment x >= alignment (0::Word64)) $
hash ((unsafeDupablePerformIO $ with x $ peek . castPtr) :: Word64)
| otherwise = hash (show x)
hashWithSalt = defaultHashWithSalt
-- | A value with bit pattern (01)* (or 5* in hexa), for any size of Int.
-- It is used as data constructor distinguisher. GHC computes its value during
-- compilation.
distinguisher :: Int
distinguisher = fromIntegral $ (maxBound :: Word) `quot` 3
{-# INLINE distinguisher #-}
instance Hashable a => Hashable (Maybe a) where
hash Nothing = 0
hash (Just a) = distinguisher `hashWithSalt` a
hashWithSalt = hashWithSalt1
instance Hashable1 Maybe where
liftHashWithSalt _ s Nothing = s `combine` 0
liftHashWithSalt h s (Just a) = s `combine` distinguisher `h` a
instance (Hashable a, Hashable b) => Hashable (Either a b) where
hash (Left a) = 0 `hashWithSalt` a
hash (Right b) = distinguisher `hashWithSalt` b
hashWithSalt = hashWithSalt1
instance Hashable a => Hashable1 (Either a) where
liftHashWithSalt = defaultLiftHashWithSalt
instance Hashable2 Either where
liftHashWithSalt2 h _ s (Left a) = s `combine` 0 `h` a
liftHashWithSalt2 _ h s (Right b) = s `combine` distinguisher `h` b
instance (Hashable a1, Hashable a2) => Hashable (a1, a2) where
hash (a1, a2) = hash a1 `hashWithSalt` a2
hashWithSalt = hashWithSalt1
instance Hashable a1 => Hashable1 ((,) a1) where
liftHashWithSalt = defaultLiftHashWithSalt
instance Hashable2 (,) where
liftHashWithSalt2 h1 h2 s (a1, a2) = s `h1` a1 `h2` a2
instance (Hashable a1, Hashable a2, Hashable a3) => Hashable (a1, a2, a3) where
hash (a1, a2, a3) = hash a1 `hashWithSalt` a2 `hashWithSalt` a3
hashWithSalt = hashWithSalt1
instance (Hashable a1, Hashable a2) => Hashable1 ((,,) a1 a2) where
liftHashWithSalt = defaultLiftHashWithSalt
instance Hashable a1 => Hashable2 ((,,) a1) where
liftHashWithSalt2 h1 h2 s (a1, a2, a3) =
(s `hashWithSalt` a1) `h1` a2 `h2` a3
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4) =>
Hashable (a1, a2, a3, a4) where
hash (a1, a2, a3, a4) = hash a1 `hashWithSalt` a2
`hashWithSalt` a3 `hashWithSalt` a4
hashWithSalt = hashWithSalt1
instance (Hashable a1, Hashable a2, Hashable a3) => Hashable1 ((,,,) a1 a2 a3) where
liftHashWithSalt = defaultLiftHashWithSalt
instance (Hashable a1, Hashable a2) => Hashable2 ((,,,) a1 a2) where
liftHashWithSalt2 h1 h2 s (a1, a2, a3, a4) =
(s `hashWithSalt` a1 `hashWithSalt` a2) `h1` a3 `h2` a4
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5)
=> Hashable (a1, a2, a3, a4, a5) where
hash (a1, a2, a3, a4, a5) =
hash a1 `hashWithSalt` a2 `hashWithSalt` a3
`hashWithSalt` a4 `hashWithSalt` a5
hashWithSalt = hashWithSalt1
instance (Hashable a1, Hashable a2, Hashable a3,
Hashable a4) => Hashable1 ((,,,,) a1 a2 a3 a4) where
liftHashWithSalt = defaultLiftHashWithSalt
instance (Hashable a1, Hashable a2, Hashable a3)
=> Hashable2 ((,,,,) a1 a2 a3) where
liftHashWithSalt2 h1 h2 s (a1, a2, a3, a4, a5) =
(s `hashWithSalt` a1 `hashWithSalt` a2
`hashWithSalt` a3) `h1` a4 `h2` a5
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5,
Hashable a6) => Hashable (a1, a2, a3, a4, a5, a6) where
hash (a1, a2, a3, a4, a5, a6) =
hash a1 `hashWithSalt` a2 `hashWithSalt` a3
`hashWithSalt` a4 `hashWithSalt` a5 `hashWithSalt` a6
hashWithSalt = hashWithSalt1
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4,
Hashable a5) => Hashable1 ((,,,,,) a1 a2 a3 a4 a5) where
liftHashWithSalt = defaultLiftHashWithSalt
instance (Hashable a1, Hashable a2, Hashable a3,
Hashable a4) => Hashable2 ((,,,,,) a1 a2 a3 a4) where
liftHashWithSalt2 h1 h2 s (a1, a2, a3, a4, a5, a6) =
(s `hashWithSalt` a1 `hashWithSalt` a2 `hashWithSalt` a3
`hashWithSalt` a4) `h1` a5 `h2` a6
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5,
Hashable a6, Hashable a7) =>
Hashable (a1, a2, a3, a4, a5, a6, a7) where
hash (a1, a2, a3, a4, a5, a6, a7) =
hash a1 `hashWithSalt` a2 `hashWithSalt` a3
`hashWithSalt` a4 `hashWithSalt` a5 `hashWithSalt` a6 `hashWithSalt` a7
hashWithSalt s (a1, a2, a3, a4, a5, a6, a7) =
s `hashWithSalt` a1 `hashWithSalt` a2 `hashWithSalt` a3
`hashWithSalt` a4 `hashWithSalt` a5 `hashWithSalt` a6 `hashWithSalt` a7
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4, Hashable a5, Hashable a6) => Hashable1 ((,,,,,,) a1 a2 a3 a4 a5 a6) where
liftHashWithSalt = defaultLiftHashWithSalt
instance (Hashable a1, Hashable a2, Hashable a3, Hashable a4,
Hashable a5) => Hashable2 ((,,,,,,) a1 a2 a3 a4 a5) where
liftHashWithSalt2 h1 h2 s (a1, a2, a3, a4, a5, a6, a7) =
(s `hashWithSalt` a1 `hashWithSalt` a2 `hashWithSalt` a3
`hashWithSalt` a4 `hashWithSalt` a5) `h1` a6 `h2` a7
instance Hashable (StableName a) where
hash = hashStableName
hashWithSalt = defaultHashWithSalt
-- Auxillary type for Hashable [a] definition
data SPInt = SP !Int !Int
instance Hashable a => Hashable [a] where
{-# SPECIALIZE instance Hashable [Char] #-}
hashWithSalt = hashWithSalt1
instance Hashable1 [] where
liftHashWithSalt h salt arr = finalise (foldl' step (SP salt 0) arr)
where
finalise (SP s l) = hashWithSalt s l
step (SP s l) x = SP (h s x) (l + 1)
instance Hashable B.ByteString where
hashWithSalt salt bs = unsafeDupablePerformIO $
B.unsafeUseAsCStringLen bs $ \(p, len) ->
hashPtrWithSalt p (fromIntegral len) salt
instance Hashable BL.ByteString where
hashWithSalt = BL.foldlChunks hashWithSalt
#if MIN_VERSION_bytestring(0,10,4)
instance Hashable BSI.ShortByteString where
hashWithSalt salt sbs@(BSI.SBS ba) =
hashByteArrayWithSalt ba 0 (BSI.length sbs) salt
#endif
instance Hashable T.Text where
hashWithSalt salt (T.Text arr off len) =
hashByteArrayWithSalt (TA.aBA arr) (off `shiftL` 1) (len `shiftL` 1)
salt
instance Hashable TL.Text where
hashWithSalt = TL.foldlChunks hashWithSalt
-- | Compute the hash of a ThreadId.
hashThreadId :: ThreadId -> Int
hashThreadId (ThreadId t) = hash (fromIntegral (getThreadId t) :: Int)
-- this cannot be capi, as GHC panics.
foreign import ccall unsafe "rts_getThreadId" getThreadId
:: ThreadId# -> CInt
instance Hashable ThreadId where
hash = hashThreadId
hashWithSalt = defaultHashWithSalt
instance Hashable (Ptr a) where
hashWithSalt salt p = hashWithSalt salt $ ptrToIntPtr p
instance Hashable (FunPtr a) where
hashWithSalt salt p = hashWithSalt salt $ castFunPtrToPtr p
instance Hashable IntPtr where
hash n = fromIntegral n
hashWithSalt = defaultHashWithSalt
instance Hashable WordPtr where
hash n = fromIntegral n
hashWithSalt = defaultHashWithSalt
----------------------------------------------------------------------------
-- Fingerprint & TypeRep instances
-- | @since 1.3.0.0
instance Hashable Fingerprint where
hash (Fingerprint x _) = fromIntegral x
hashWithSalt = defaultHashWithSalt
{-# INLINE hash #-}
#if MIN_VERSION_base(4,10,0)
hashTypeRep :: Type.Reflection.TypeRep a -> Int
hashTypeRep tr =
let Fingerprint x _ = typeRepFingerprint tr in fromIntegral x
instance Hashable Type.Reflection.SomeTypeRep where
hash (Type.Reflection.SomeTypeRep r) = hashTypeRep r
hashWithSalt = defaultHashWithSalt
{-# INLINE hash #-}
instance Hashable (Type.Reflection.TypeRep a) where
hash = hashTypeRep
hashWithSalt = defaultHashWithSalt
{-# INLINE hash #-}
#else
-- | Compute the hash of a TypeRep, in various GHC versions we can do this quickly.
hashTypeRep :: TypeRep -> Int
{-# INLINE hashTypeRep #-}
#if MIN_VERSION_base(4,8,0)
-- Fingerprint is just the MD5, so taking any Int from it is fine
hashTypeRep tr = let Fingerprint x _ = typeRepFingerprint tr in fromIntegral x
#else
-- Fingerprint is just the MD5, so taking any Int from it is fine
hashTypeRep (TypeRep (Fingerprint x _) _ _) = fromIntegral x
#endif
instance Hashable TypeRep where
hash = hashTypeRep
hashWithSalt = defaultHashWithSalt
{-# INLINE hash #-}
#endif
----------------------------------------------------------------------------
#if MIN_VERSION_base(4,8,0)
instance Hashable Void where
hashWithSalt _ = absurd
#endif
-- | Compute a hash value for the content of this pointer.
hashPtr :: Ptr a -- ^ pointer to the data to hash
-> Int -- ^ length, in bytes
-> IO Int -- ^ hash value
hashPtr p len = hashPtrWithSalt p len defaultSalt
-- | Compute a hash value for the content of this pointer, using an
-- initial salt.
--
-- This function can for example be used to hash non-contiguous
-- segments of memory as if they were one contiguous segment, by using
-- the output of one hash as the salt for the next.
hashPtrWithSalt :: Ptr a -- ^ pointer to the data to hash
-> Int -- ^ length, in bytes
-> Int -- ^ salt
-> IO Int -- ^ hash value
hashPtrWithSalt p len salt =
fromIntegral `fmap` c_hashCString (castPtr p) (fromIntegral len)
(fromIntegral salt)
foreign import capi unsafe "HsHashable.h hashable_fnv_hash" c_hashCString
#if WORD_SIZE_IN_BITS == 64
:: CString -> Int64 -> Int64 -> IO Word64
#else
:: CString -> Int32 -> Int32 -> IO Word32
#endif
-- | Compute a hash value for the content of this 'ByteArray#',
-- beginning at the specified offset, using specified number of bytes.
hashByteArray :: ByteArray# -- ^ data to hash
-> Int -- ^ offset, in bytes
-> Int -- ^ length, in bytes
-> Int -- ^ hash value
hashByteArray ba0 off len = hashByteArrayWithSalt ba0 off len defaultSalt
{-# INLINE hashByteArray #-}
-- | Compute a hash value for the content of this 'ByteArray#', using
-- an initial salt.
--
-- This function can for example be used to hash non-contiguous
-- segments of memory as if they were one contiguous segment, by using
-- the output of one hash as the salt for the next.
hashByteArrayWithSalt
:: ByteArray# -- ^ data to hash
-> Int -- ^ offset, in bytes
-> Int -- ^ length, in bytes
-> Int -- ^ salt
-> Int -- ^ hash value
hashByteArrayWithSalt ba !off !len !h =
fromIntegral $ c_hashByteArray ba (fromIntegral off) (fromIntegral len)
(fromIntegral h)
#if __GLASGOW_HASKELL__ >= 802
foreign import capi unsafe "HsHashable.h hashable_fnv_hash_offset" c_hashByteArray
#else
foreign import ccall unsafe "hashable_fnv_hash_offset" c_hashByteArray
#endif
#if WORD_SIZE_IN_BITS == 64
:: ByteArray# -> Int64 -> Int64 -> Int64 -> Word64
#else
:: ByteArray# -> Int32 -> Int32 -> Int32 -> Word32
#endif
-- | Combine two given hash values. 'combine' has zero as a left
-- identity.
combine :: Int -> Int -> Int
combine h1 h2 = (h1 * 16777619) `xor` h2
instance Hashable Unique where
hash = hashUnique
hashWithSalt = defaultHashWithSalt
instance Hashable Version where
hashWithSalt salt (Version branch tags) =
salt `hashWithSalt` branch `hashWithSalt` tags
#if MIN_VERSION_base(4,7,0)
instance Hashable (Fixed a) where
hashWithSalt salt (MkFixed i) = hashWithSalt salt i
-- Using hashWithSalt1 would cause needless constraint
instance Hashable1 Fixed where
liftHashWithSalt _ salt (MkFixed i) = hashWithSalt salt i
#else
instance Hashable (Fixed a) where
hashWithSalt salt x = hashWithSalt salt (unsafeCoerce x :: Integer)
#endif
#if MIN_VERSION_base(4,8,0)
instance Hashable a => Hashable (Identity a) where
hashWithSalt = hashWithSalt1
instance Hashable1 Identity where
liftHashWithSalt h salt (Identity x) = h salt x
#endif
-- Using hashWithSalt1 would cause needless constraint
instance Hashable a => Hashable (Const a b) where
hashWithSalt salt (Const x) = hashWithSalt salt x
instance Hashable a => Hashable1 (Const a) where
liftHashWithSalt = defaultLiftHashWithSalt
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)
instance Hashable a => Hashable (NE.NonEmpty a) where
hashWithSalt p (a NE.:| as) = p `hashWithSalt` a `hashWithSalt` as
-- | @since 1.3.1.0
instance Hashable1 NE.NonEmpty where
liftHashWithSalt h salt (a NE.:| as) = liftHashWithSalt h (h salt a) as
instance Hashable a => Hashable (Min a) where
hashWithSalt p (Min a) = hashWithSalt p a
-- | @since 1.3.1.0
instance Hashable1 Min where liftHashWithSalt h salt (Min a) = h salt a
instance Hashable a => Hashable (Max a) where
hashWithSalt p (Max a) = hashWithSalt p a
-- | @since 1.3.1.0
instance Hashable1 Max where liftHashWithSalt h salt (Max a) = h salt a
-- | __Note__: Prior to @hashable-1.3.0.0@ the hash computation included the second argument of 'Arg' which wasn't consistent with its 'Eq' instance.
--
-- @since 1.3.0.0
instance Hashable a => Hashable (Arg a b) where
hashWithSalt p (Arg a _) = hashWithSalt p a
instance Hashable a => Hashable (First a) where
hashWithSalt p (First a) = hashWithSalt p a
-- | @since 1.3.1.0
instance Hashable1 First where liftHashWithSalt h salt (First a) = h salt a
instance Hashable a => Hashable (Last a) where
hashWithSalt p (Last a) = hashWithSalt p a
-- | @since 1.3.1.0
instance Hashable1 Last where liftHashWithSalt h salt (Last a) = h salt a
instance Hashable a => Hashable (WrappedMonoid a) where
hashWithSalt p (WrapMonoid a) = hashWithSalt p a
-- | @since 1.3.1.0
instance Hashable1 WrappedMonoid where liftHashWithSalt h salt (WrapMonoid a) = h salt a
#if !MIN_VERSION_base(4,16,0)
instance Hashable a => Hashable (Option a) where
hashWithSalt p (Option a) = hashWithSalt p a
-- | @since 1.3.1.0
instance Hashable1 Option where liftHashWithSalt h salt (Option a) = liftHashWithSalt h salt a
#endif
#endif
-- instances for @Data.Functor.{Product,Sum,Compose}@, present
-- in base-4.9 and onward.
#if MIN_VERSION_base(4,9,0)
-- | In general, @hash (Compose x) ≠ hash x@. However, @hashWithSalt@ satisfies
-- its variant of this equivalence.
instance (Hashable1 f, Hashable1 g, Hashable a) => Hashable (Compose f g a) where
hashWithSalt = hashWithSalt1
instance (Hashable1 f, Hashable1 g) => Hashable1 (Compose f g) where
liftHashWithSalt h s = liftHashWithSalt (liftHashWithSalt h) s . getCompose
instance (Hashable1 f, Hashable1 g) => Hashable1 (FP.Product f g) where
liftHashWithSalt h s (FP.Pair a b) = liftHashWithSalt h (liftHashWithSalt h s a) b
instance (Hashable1 f, Hashable1 g, Hashable a) => Hashable (FP.Product f g a) where
hashWithSalt = hashWithSalt1
instance (Hashable1 f, Hashable1 g) => Hashable1 (FS.Sum f g) where
liftHashWithSalt h s (FS.InL a) = liftHashWithSalt h (s `combine` 0) a
liftHashWithSalt h s (FS.InR a) = liftHashWithSalt h (s `combine` distinguisher) a
instance (Hashable1 f, Hashable1 g, Hashable a) => Hashable (FS.Sum f g a) where
hashWithSalt = hashWithSalt1
#endif
-- | A hashable value along with the result of the 'hash' function.
data Hashed a = Hashed a {-# UNPACK #-} !Int
deriving (Typeable)
-- | Wrap a hashable value, caching the 'hash' function result.
hashed :: Hashable a => a -> Hashed a
hashed a = Hashed a (hash a)
-- | Unwrap hashed value.