2 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
6 -- Module : GHC.Classes
7 -- Copyright : (c) The University of Glasgow, 1992-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC extensions)
16 -----------------------------------------------------------------------------
18 module GHC.Classes where
21 -- GHC.Magic is used in some derived instances
25 infix 4 ==, /=, <, <=, >=, >
29 default () -- Double isn't available yet
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
36 -- Minimal complete definition: either '==' or '/='.
39 (==), (/=) :: a -> a -> Bool
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
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
64 -- | The 'Ord' class is used for totally ordered datatypes.
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.
72 -- Minimal complete definition: either 'compare' or '<='.
73 -- Using 'compare' can be more efficient for complex types.
75 class (Eq a) => Ord a where
76 compare :: a -> a -> Ordering
77 (<), (<=), (>), (>=) :: a -> a -> Bool
78 max, min :: a -> a -> a
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
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 }
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
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
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
116 -- OK, so they're technically not part of a class...:
121 (&&) :: Bool -> Bool -> Bool
126 (||) :: Bool -> Bool -> Bool