De-orphan the Eq/Ord [a] instances
[ghc-base.git] / GHC / Classes.hs
1
2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module      :  GHC.Classes
7 -- Copyright   :  (c) The University of Glasgow, 1992-2002
8 -- License     :  see libraries/base/LICENSE
9 --
10 -- Maintainer  :  cvs-ghc@haskell.org
11 -- Stability   :  internal
12 -- Portability :  non-portable (GHC extensions)
13 --
14 -- Basic classes.
15 --
16 -----------------------------------------------------------------------------
17
18 module GHC.Classes where
19
20 import GHC.Bool
21 -- GHC.Magic is used in some derived instances
22 import GHC.Magic ()
23 import GHC.Ordering
24 import GHC.Prim
25 import GHC.Types
26
27 infix  4  ==, /=, <, <=, >=, >
28 infixr 3  &&
29 infixr 2  ||
30
31 default ()              -- Double isn't available yet
32
33 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
34 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
35 -- and 'Eq' may be derived for any datatype whose constituents are also
36 -- instances of 'Eq'.
37 --
38 -- Minimal complete definition: either '==' or '/='.
39 --
40 class  Eq a  where
41     (==), (/=)           :: a -> a -> Bool
42
43     {-# INLINE (/=) #-}
44     {-# INLINE (==) #-}
45     x /= y               = not (x == y)
46     x == y               = not (x /= y)
47
48 instance (Eq a) => Eq [a] where
49     {-# SPECIALISE instance Eq [Char] #-}
50     []     == []     = True
51     (x:xs) == (y:ys) = x == y && xs == ys
52     _xs    == _ys    = False
53
54 -- XXX This doesn't work:
55 -- deriving instance Eq Bool
56 -- <wired into compiler>:
57 --     Illegal binding of built-in syntax: con2tag_Bool#
58 instance Eq Bool where
59     True  == True  = True
60     False == False = True
61     _     == _     = False
62
63 -- XXX This doesn't work:
64 -- deriving instance Eq Ordering
65 -- Illegal binding of built-in syntax: con2tag_Ordering#
66 instance Eq Ordering where
67     EQ == EQ = True
68     LT == LT = True
69     GT == GT = True
70     _  == _  = False
71
72 instance Eq Char where
73     (C# c1) == (C# c2) = c1 `eqChar#` c2
74     (C# c1) /= (C# c2) = c1 `neChar#` c2
75
76 -- | The 'Ord' class is used for totally ordered datatypes.
77 --
78 -- Instances of 'Ord' can be derived for any user-defined
79 -- datatype whose constituent types are in 'Ord'.  The declared order
80 -- of the constructors in the data declaration determines the ordering
81 -- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
82 -- comparison to determine the precise ordering of two objects.
83 --
84 -- Minimal complete definition: either 'compare' or '<='.
85 -- Using 'compare' can be more efficient for complex types.
86 --
87 class  (Eq a) => Ord a  where
88     compare              :: a -> a -> Ordering
89     (<), (<=), (>), (>=) :: a -> a -> Bool
90     max, min             :: a -> a -> a
91
92     compare x y = if x == y then EQ
93                   -- NB: must be '<=' not '<' to validate the
94                   -- above claim about the minimal things that
95                   -- can be defined for an instance of Ord:
96                   else if x <= y then LT
97                   else GT
98
99     x <  y = case compare x y of { LT -> True;  _ -> False }
100     x <= y = case compare x y of { GT -> False; _ -> True }
101     x >  y = case compare x y of { GT -> True;  _ -> False }
102     x >= y = case compare x y of { LT -> False; _ -> True }
103
104         -- These two default methods use '<=' rather than 'compare'
105         -- because the latter is often more expensive
106     max x y = if x <= y then y else x
107     min x y = if x <= y then x else y
108
109 instance (Ord a) => Ord [a] where
110     {-# SPECIALISE instance Ord [Char] #-}
111     compare []     []     = EQ
112     compare []     (_:_)  = LT
113     compare (_:_)  []     = GT
114     compare (x:xs) (y:ys) = case compare x y of
115                                 EQ    -> compare xs ys
116                                 other -> other
117
118 -- XXX This doesn't work:
119 -- deriving instance Ord Bool
120 -- <wired into compiler>:
121 --     Illegal binding of built-in syntax: con2tag_Bool#
122 instance Ord Bool where
123     compare False True  = LT
124     compare True  False = GT
125     compare _     _     = EQ
126
127 -- XXX This doesn't work:
128 -- deriving instance Ord Ordering
129 -- Illegal binding of built-in syntax: con2tag_Ordering#
130 instance Ord Ordering where
131     LT <= _  = True
132     _  <= LT = False
133     EQ <= _  = True
134     _  <= EQ = False
135     GT <= GT = True
136
137 -- We don't use deriving for Ord Char, because for Ord the derived
138 -- instance defines only compare, which takes two primops.  Then
139 -- '>' uses compare, and therefore takes two primops instead of one.
140 instance Ord Char where
141     (C# c1) >  (C# c2) = c1 `gtChar#` c2
142     (C# c1) >= (C# c2) = c1 `geChar#` c2
143     (C# c1) <= (C# c2) = c1 `leChar#` c2
144     (C# c1) <  (C# c2) = c1 `ltChar#` c2
145
146 -- OK, so they're technically not part of a class...:
147
148 -- Boolean functions
149
150 -- | Boolean \"and\"
151 (&&)                    :: Bool -> Bool -> Bool
152 True  && x              =  x
153 False && _              =  False
154
155 -- | Boolean \"or\"
156 (||)                    :: Bool -> Bool -> Bool
157 True  || _              =  True
158 False || x              =  x
159
160 -- | Boolean \"not\"
161 not                     :: Bool -> Bool
162 not True                =  False
163 not False               =  True
164