Eq and Ord have moved into GHC.Classes
[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 import GHC.Ordering
22
23 infix  4  ==, /=, <, <=, >=, >
24 infixr 3  &&
25 infixr 2  ||
26
27 default ()              -- Double isn't available yet
28
29 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
30 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
31 -- and 'Eq' may be derived for any datatype whose constituents are also
32 -- instances of 'Eq'.
33 --
34 -- Minimal complete definition: either '==' or '/='.
35 --
36 class  Eq a  where
37     (==), (/=)           :: a -> a -> Bool
38
39     x /= y               = not (x == y)
40     x == y               = not (x /= y)
41
42 -- | The 'Ord' class is used for totally ordered datatypes.
43 --
44 -- Instances of 'Ord' can be derived for any user-defined
45 -- datatype whose constituent types are in 'Ord'.  The declared order
46 -- of the constructors in the data declaration determines the ordering
47 -- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
48 -- comparison to determine the precise ordering of two objects.
49 --
50 -- Minimal complete definition: either 'compare' or '<='.
51 -- Using 'compare' can be more efficient for complex types.
52 --
53 class  (Eq a) => Ord a  where
54     compare              :: a -> a -> Ordering
55     (<), (<=), (>), (>=) :: a -> a -> Bool
56     max, min             :: a -> a -> a
57
58     compare x y = if x == y then EQ
59                   -- NB: must be '<=' not '<' to validate the
60                   -- above claim about the minimal things that
61                   -- can be defined for an instance of Ord:
62                   else if x <= y then LT
63                   else GT
64
65     x <  y = case compare x y of { LT -> True;  _ -> False }
66     x <= y = case compare x y of { GT -> False; _ -> True }
67     x >  y = case compare x y of { GT -> True;  _ -> False }
68     x >= y = case compare x y of { LT -> False; _ -> True }
69
70         -- These two default methods use '<=' rather than 'compare'
71         -- because the latter is often more expensive
72     max x y = if x <= y then y else x
73     min x y = if x <= y then x else y
74
75 -- OK, so they're technically not part of a class...:
76
77 -- Boolean functions
78
79 -- | Boolean \"and\"
80 (&&)                    :: Bool -> Bool -> Bool
81 True  && x              =  x
82 False && _              =  False
83
84 -- | Boolean \"or\"
85 (||)                    :: Bool -> Bool -> Bool
86 True  || _              =  True
87 False || x              =  x
88
89 -- | Boolean \"not\"
90 not                     :: Bool -> Bool
91 not True                =  False
92 not False               =  True
93