De-orphan the Eq/Ord Char 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 -- XXX This doesn't work:
49 -- deriving instance Eq Bool
50 -- <wired into compiler>:
51 --     Illegal binding of built-in syntax: con2tag_Bool#
52 instance Eq Bool where
53     True  == True  = True
54     False == False = True
55     _     == _     = False
56
57 -- XXX This doesn't work:
58 -- deriving instance Eq Ordering
59 -- Illegal binding of built-in syntax: con2tag_Ordering#
60 instance Eq Ordering where
61     EQ == EQ = True
62     LT == LT = True
63     GT == GT = True
64     _  == _  = False
65
66 instance Eq Char where
67     (C# c1) == (C# c2) = c1 `eqChar#` c2
68     (C# c1) /= (C# c2) = c1 `neChar#` c2
69
70 -- | The 'Ord' class is used for totally ordered datatypes.
71 --
72 -- Instances of 'Ord' can be derived for any user-defined
73 -- datatype whose constituent types are in 'Ord'.  The declared order
74 -- of the constructors in the data declaration determines the ordering
75 -- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
76 -- comparison to determine the precise ordering of two objects.
77 --
78 -- Minimal complete definition: either 'compare' or '<='.
79 -- Using 'compare' can be more efficient for complex types.
80 --
81 class  (Eq a) => Ord a  where
82     compare              :: a -> a -> Ordering
83     (<), (<=), (>), (>=) :: a -> a -> Bool
84     max, min             :: a -> a -> a
85
86     compare x y = if x == y then EQ
87                   -- NB: must be '<=' not '<' to validate the
88                   -- above claim about the minimal things that
89                   -- can be defined for an instance of Ord:
90                   else if x <= y then LT
91                   else GT
92
93     x <  y = case compare x y of { LT -> True;  _ -> False }
94     x <= y = case compare x y of { GT -> False; _ -> True }
95     x >  y = case compare x y of { GT -> True;  _ -> False }
96     x >= y = case compare x y of { LT -> False; _ -> True }
97
98         -- These two default methods use '<=' rather than 'compare'
99         -- because the latter is often more expensive
100     max x y = if x <= y then y else x
101     min x y = if x <= y then x else y
102
103 -- XXX This doesn't work:
104 -- deriving instance Ord Bool
105 -- <wired into compiler>:
106 --     Illegal binding of built-in syntax: con2tag_Bool#
107 instance Ord Bool where
108     compare False True  = LT
109     compare True  False = GT
110     compare _     _     = EQ
111
112 -- XXX This doesn't work:
113 -- deriving instance Ord Ordering
114 -- Illegal binding of built-in syntax: con2tag_Ordering#
115 instance Ord Ordering where
116     LT <= _  = True
117     _  <= LT = False
118     EQ <= _  = True
119     _  <= EQ = False
120     GT <= GT = True
121
122 -- We don't use deriving for Ord Char, because for Ord the derived
123 -- instance defines only compare, which takes two primops.  Then
124 -- '>' uses compare, and therefore takes two primops instead of one.
125 instance Ord Char where
126     (C# c1) >  (C# c2) = c1 `gtChar#` c2
127     (C# c1) >= (C# c2) = c1 `geChar#` c2
128     (C# c1) <= (C# c2) = c1 `leChar#` c2
129     (C# c1) <  (C# c2) = c1 `ltChar#` c2
130
131 -- OK, so they're technically not part of a class...:
132
133 -- Boolean functions
134
135 -- | Boolean \"and\"
136 (&&)                    :: Bool -> Bool -> Bool
137 True  && x              =  x
138 False && _              =  False
139
140 -- | Boolean \"or\"
141 (||)                    :: Bool -> Bool -> Bool
142 True  || _              =  True
143 False || x              =  x
144
145 -- | Boolean \"not\"
146 not                     :: Bool -> Bool
147 not True                =  False
148 not False               =  True
149