Inline more default methods
[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     {-# INLINE (/=) #-}
40     {-# INLINE (==) #-}
41     x /= y               = not (x == y)
42     x == y               = not (x /= y)
43
44 -- | The 'Ord' class is used for totally ordered datatypes.
45 --
46 -- Instances of 'Ord' can be derived for any user-defined
47 -- datatype whose constituent types are in 'Ord'.  The declared order
48 -- of the constructors in the data declaration determines the ordering
49 -- in derived 'Ord' instances.  The 'Ordering' datatype allows a single
50 -- comparison to determine the precise ordering of two objects.
51 --
52 -- Minimal complete definition: either 'compare' or '<='.
53 -- Using 'compare' can be more efficient for complex types.
54 --
55 class  (Eq a) => Ord a  where
56     compare              :: a -> a -> Ordering
57     (<), (<=), (>), (>=) :: a -> a -> Bool
58     max, min             :: a -> a -> a
59
60     compare x y = if x == y then EQ
61                   -- NB: must be '<=' not '<' to validate the
62                   -- above claim about the minimal things that
63                   -- can be defined for an instance of Ord:
64                   else if x <= y then LT
65                   else GT
66
67     x <  y = case compare x y of { LT -> True;  _ -> False }
68     x <= y = case compare x y of { GT -> False; _ -> True }
69     x >  y = case compare x y of { GT -> True;  _ -> False }
70     x >= y = case compare x y of { LT -> False; _ -> True }
71
72         -- These two default methods use '<=' rather than 'compare'
73         -- because the latter is often more expensive
74     max x y = if x <= y then y else x
75     min x y = if x <= y then x else y
76
77 -- OK, so they're technically not part of a class...:
78
79 -- Boolean functions
80
81 -- | Boolean \"and\"
82 (&&)                    :: Bool -> Bool -> Bool
83 True  && x              =  x
84 False && _              =  False
85
86 -- | Boolean \"or\"
87 (||)                    :: Bool -> Bool -> Bool
88 True  || _              =  True
89 False || x              =  x
90
91 -- | Boolean \"not\"
92 not                     :: Bool -> Bool
93 not True                =  False
94 not False               =  True
95