~ubuntu-branches/ubuntu/wily/agda/wily-proposed

« back to all changes in this revision

Viewing changes to src/transl/agda/Hash.hs

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-08-05 06:38:12 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140805063812-io8e77niomivhd49
Tags: 2.4.0.2-1
* [6e140ac] Imported Upstream version 2.4.0.2
* [2049fc8] Update Build-Depends to match control
* [93dc4d4] Install the new primitives
* [e48f40f] Fix typo dev→doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
{-| Provides 'hash' function for many data types
2
 
-}
3
 
module Hash(Hash, combineHash, emptyHash, hashToInt, hashToMax, Hashable(..)) where
4
 
--
5
 
--
6
 
import Data.Array
7
 
import Data.Complex
8
 
import Data.Ratio
9
 
 
10
 
 
11
 
 
12
 
newtype Hash = H Int deriving (Eq)
13
 
 
14
 
----instance Show Hash where
15
 
 
16
 
combineHash :: Hash -> Hash -> Hash
17
 
combineHash (H x) (H y) = H (x+y)
18
 
 
19
 
emptyHash :: Hash
20
 
emptyHash = H 0
21
 
 
22
 
class Hashable a where
23
 
    hash :: a -> Hash
24
 
 
25
 
instance Hashable Char where
26
 
    hash x = H $ fromEnum x
27
 
 
28
 
instance Hashable Int where
29
 
    hash x = H $ x
30
 
 
31
 
instance Hashable Integer where
32
 
    hash x = H $ fromInteger x
33
 
 
34
 
instance Hashable Float where
35
 
    hash x = H $ truncate x
36
 
 
37
 
instance Hashable Double where
38
 
    hash x = H $ truncate x
39
 
 
40
 
instance Hashable (IO a) where
41
 
    hash x = H 0
42
 
 
43
 
instance Hashable () where
44
 
    hash x = H 0
45
 
 
46
 
instance Hashable (a -> b) where
47
 
    hash x = H 0
48
 
 
49
 
instance (Hashable a) => Hashable (Maybe a) where
50
 
    hash Nothing = H 0
51
 
    hash (Just x) = hash x
52
 
 
53
 
instance (Hashable a, Hashable b) => Hashable (Either a b) where
54
 
    hash (Left x) = hash x
55
 
    hash (Right y) = hash y
56
 
 
57
 
 
58
 
--instance Hashable a => Hashable [a] where
59
 
 
60
 
 
61
 
instance (Hashable a,Enum a) => Hashable [a] where
62
 
    hash l = H $ f l 0
63
 
        where f :: Enum b => [b] -> Int -> Int
64
 
              f [] r = r
65
 
              f (c:cs) r = f cs (3*r + fromEnum c)
66
 
 
67
 
 
68
 
instance (Hashable a, Hashable b) => Hashable (a,b) where
69
 
    hash (a,b) = H $ (case hash ( a ) of H h -> h)  + 3 * (case hash ( b ) of H h -> h)
70
 
 
71
 
instance (Hashable a, Hashable b, Hashable c) => Hashable (a,b,c) where
72
 
    hash (a,b,c) = H $ (case hash ( a ) of H h -> h)  + 3 * (case hash ( b ) of H h -> h)  + 5 * (case hash ( c ) of H h -> h)
73
 
 
74
 
instance (Hashable a, Hashable b, Hashable c, Hashable d) => Hashable (a,b,c,d) where
75
 
    hash (a,b,c,d) = H $ (case hash ( a ) of H h -> h)  + 3 * (case hash ( b ) of H h -> h)  + 5 * (case hash ( c ) of H h -> h)  + 7 * (case hash ( d ) of H h -> h)
76
 
 
77
 
instance (Hashable a, Hashable b, Hashable c, Hashable d, Hashable e) => Hashable (a,b,c,d,e) where
78
 
    hash (a,b,c,d,e) = H $ (case hash ( a ) of H h -> h)  + 3 * (case hash ( b ) of H h -> h)  + 5 * (case hash ( c ) of H h -> h)  + 7 * (case hash ( d ) of H h -> h)  + 9 * (case hash ( e ) of H h -> h)
79
 
 
80
 
instance Hashable Bool where
81
 
    hash False = H 0
82
 
    hash True = H 1
83
 
 
84
 
instance (Integral a, Hashable a) => Hashable (Ratio a) where
85
 
    hash x = H $ (case hash ( denominator x ) of H h -> h)  + (case hash ( numerator x ) of H h -> h)
86
 
 
87
 
instance (RealFloat a, Hashable a) => Hashable (Complex a) where
88
 
    hash (x :+ y) = H $ (case hash ( x ) of H h -> h)  + (case hash ( y ) of H h -> h)
89
 
 
90
 
instance (Ix a) => Hashable (Array a b) where
91
 
    hash x = H $ 0 -- !!!
92
 
 
93
 
hashToInt :: Int -> Hash -> Int
94
 
hashToInt maxhash x =
95
 
    case x of
96
 
    H h ->
97
 
        if h < 0 then
98
 
            if -h < 0 then 0
99
 
            else (-h) `rem` maxhash
100
 
        else h `rem` maxhash
101
 
 
102
 
hashToMax maxhash x =
103
 
    case hash x of
104
 
    H h ->
105
 
        if h < 0 then
106
 
            if -h < 0 then 0
107
 
            else (-h) `rem` maxhash
108
 
        else h `rem` maxhash