Move Eq/Ord Ordering instances to de-orphan them
[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 Ordering
48 -- Illegal binding of built-in syntax: con2tag_Ordering#
49 instance Eq Ordering where
50     EQ == EQ = True
51     LT == LT = True
52     GT == GT = True
53     _  == _  = False
54
55 -- | The 'Ord' class is used for totally ordered datatypes.
56 --
57 -- Instances of 'Ord' can be derived for any user-defined
58 -- datatype whose constituent types are in 'Ord'.  The declared order
59 -- of the constructors in the data declaration determines the ordering
60 -- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
61 -- comparison to determine the precise ordering of two objects.
62 --
63 -- Minimal complete definition: either 'compare' or '<='.
64 -- Using 'compare' can be more efficient for complex types.
65 --
66 class  (Eq a) => Ord a  where
67     compare              :: a -> a -> Ordering
68     (<), (<=), (>), (>=) :: a -> a -> Bool
69     max, min             :: a -> a -> a
70
71     compare x y = if x == y then EQ
72                   -- NB: must be '<=' not '<' to validate the
73                   -- above claim about the minimal things that
74                   -- can be defined for an instance of Ord:
75                   else if x <= y then LT
76                   else GT
77
78     x <  y = case compare x y of { LT -> True;  _ -> False }
79     x <= y = case compare x y of { GT -> False; _ -> True }
80     x >  y = case compare x y of { GT -> True;  _ -> False }
81     x >= y = case compare x y of { LT -> False; _ -> True }
82
83         -- These two default methods use '<=' rather than 'compare'
84         -- because the latter is often more expensive
85     max x y = if x <= y then y else x
86     min x y = if x <= y then x else y
87
88 -- XXX This doesn't work:
89 -- deriving instance Ord Ordering
90 -- Illegal binding of built-in syntax: con2tag_Ordering#
91 instance Ord Ordering where
92     LT <= _  = True
93     _  <= LT = False
94     EQ <= _  = True
95     _  <= EQ = False
96     GT <= GT = True
97
98 -- OK, so they're technically not part of a class...:
99
100 -- Boolean functions
101
102 -- | Boolean \"and\"
103 (&&)                    :: Bool -> Bool -> Bool
104 True  && x              =  x
105 False && _              =  False
106
107 -- | Boolean \"or\"
108 (||)                    :: Bool -> Bool -> Bool
109 True  || _              =  True
110 False || x              =  x
111
112 -- | Boolean \"not\"
113 not                     :: Bool -> Bool
114 not True                =  False
115 not False               =  True
116