De-orphan the Eq/Ord Bool 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
25 infix  4  ==, /=, <, <=, >=, >
26 infixr 3  &&
27 infixr 2  ||
28
29 default ()              -- Double isn't available yet
30
31 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
32 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
33 -- and 'Eq' may be derived for any datatype whose constituents are also
34 -- instances of 'Eq'.
35 --
36 -- Minimal complete definition: either '==' or '/='.
37 --
38 class  Eq a  where
39     (==), (/=)           :: a -> a -> Bool
40
41     {-# INLINE (/=) #-}
42     {-# INLINE (==) #-}
43     x /= y               = not (x == y)
44     x == y               = not (x /= y)
45
46 -- XXX This doesn't work:
47 -- deriving instance Eq Bool
48 -- <wired into compiler>:
49 --     Illegal binding of built-in syntax: con2tag_Bool#
50 instance Eq Bool where
51     True  == True  = True
52     False == False = True
53     _     == _     = False
54
55 -- XXX This doesn't work:
56 -- deriving instance Eq Ordering
57 -- Illegal binding of built-in syntax: con2tag_Ordering#
58 instance Eq Ordering where
59     EQ == EQ = True
60     LT == LT = True
61     GT == GT = True
62     _  == _  = False
63
64 -- | The 'Ord' class is used for totally ordered datatypes.
65 --
66 -- Instances of 'Ord' can be derived for any user-defined
67 -- datatype whose constituent types are in 'Ord'.  The declared order
68 -- of the constructors in the data declaration determines the ordering
69 -- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
70 -- comparison to determine the precise ordering of two objects.
71 --
72 -- Minimal complete definition: either 'compare' or '<='.
73 -- Using 'compare' can be more efficient for complex types.
74 --
75 class  (Eq a) => Ord a  where
76     compare              :: a -> a -> Ordering
77     (<), (<=), (>), (>=) :: a -> a -> Bool
78     max, min             :: a -> a -> a
79
80     compare x y = if x == y then EQ
81                   -- NB: must be '<=' not '<' to validate the
82                   -- above claim about the minimal things that
83                   -- can be defined for an instance of Ord:
84                   else if x <= y then LT
85                   else GT
86
87     x <  y = case compare x y of { LT -> True;  _ -> False }
88     x <= y = case compare x y of { GT -> False; _ -> True }
89     x >  y = case compare x y of { GT -> True;  _ -> False }
90     x >= y = case compare x y of { LT -> False; _ -> True }
91
92         -- These two default methods use '<=' rather than 'compare'
93         -- because the latter is often more expensive
94     max x y = if x <= y then y else x
95     min x y = if x <= y then x else y
96
97 -- XXX This doesn't work:
98 -- deriving instance Ord Bool
99 -- <wired into compiler>:
100 --     Illegal binding of built-in syntax: con2tag_Bool#
101 instance Ord Bool where
102     compare False True  = LT
103     compare True  False = GT
104     compare _     _     = EQ
105
106 -- XXX This doesn't work:
107 -- deriving instance Ord Ordering
108 -- Illegal binding of built-in syntax: con2tag_Ordering#
109 instance Ord Ordering where
110     LT <= _  = True
111     _  <= LT = False
112     EQ <= _  = True
113     _  <= EQ = False
114     GT <= GT = True
115
116 -- OK, so they're technically not part of a class...:
117
118 -- Boolean functions
119
120 -- | Boolean \"and\"
121 (&&)                    :: Bool -> Bool -> Bool
122 True  && x              =  x
123 False && _              =  False
124
125 -- | Boolean \"or\"
126 (||)                    :: Bool -> Bool -> Bool
127 True  || _              =  True
128 False || x              =  x
129
130 -- | Boolean \"not\"
131 not                     :: Bool -> Bool
132 not True                =  False
133 not False               =  True
134