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
27 infix 4 ==, /=, <, <=, >=, >
31 default () -- Double isn't available yet
33 -- | The 'Eq' class defines equality ('==') and inequality ('/=').
34 -- All the basic datatypes exported by the "Prelude" are instances of 'Eq',
35 -- and 'Eq' may be derived for any datatype whose constituents are also
38 -- Minimal complete definition: either '==' or '/='.
41 (==), (/=) :: a -> a -> Bool
48 instance (Eq a) => Eq [a] where
49 {-# SPECIALISE instance Eq [Char] #-}
51 (x:xs) == (y:ys) = x == y && xs == ys
54 -- XXX This doesn't work:
55 -- deriving instance Eq Bool
56 -- <wired into compiler>:
57 -- Illegal binding of built-in syntax: con2tag_Bool#
58 instance Eq Bool where
63 -- XXX This doesn't work:
64 -- deriving instance Eq Ordering
65 -- Illegal binding of built-in syntax: con2tag_Ordering#
66 instance Eq Ordering where
72 instance Eq Char where
73 (C# c1) == (C# c2) = c1 `eqChar#` c2
74 (C# c1) /= (C# c2) = c1 `neChar#` c2
76 -- | The 'Ord' class is used for totally ordered datatypes.
78 -- Instances of 'Ord' can be derived for any user-defined
79 -- datatype whose constituent types are in 'Ord'. The declared order
80 -- of the constructors in the data declaration determines the ordering
81 -- in derived 'Ord' instances. The 'Ordering' datatype allows a single
82 -- comparison to determine the precise ordering of two objects.
84 -- Minimal complete definition: either 'compare' or '<='.
85 -- Using 'compare' can be more efficient for complex types.
87 class (Eq a) => Ord a where
88 compare :: a -> a -> Ordering
89 (<), (<=), (>), (>=) :: a -> a -> Bool
90 max, min :: a -> a -> a
92 compare x y = if x == y then EQ
93 -- NB: must be '<=' not '<' to validate the
94 -- above claim about the minimal things that
95 -- can be defined for an instance of Ord:
96 else if x <= y then LT
99 x < y = case compare x y of { LT -> True; _ -> False }
100 x <= y = case compare x y of { GT -> False; _ -> True }
101 x > y = case compare x y of { GT -> True; _ -> False }
102 x >= y = case compare x y of { LT -> False; _ -> True }
104 -- These two default methods use '<=' rather than 'compare'
105 -- because the latter is often more expensive
106 max x y = if x <= y then y else x
107 min x y = if x <= y then x else y
109 instance (Ord a) => Ord [a] where
110 {-# SPECIALISE instance Ord [Char] #-}
112 compare [] (_:_) = LT
113 compare (_:_) [] = GT
114 compare (x:xs) (y:ys) = case compare x y of
118 -- XXX This doesn't work:
119 -- deriving instance Ord Bool
120 -- <wired into compiler>:
121 -- Illegal binding of built-in syntax: con2tag_Bool#
122 instance Ord Bool where
123 compare False True = LT
124 compare True False = GT
127 -- XXX This doesn't work:
128 -- deriving instance Ord Ordering
129 -- Illegal binding of built-in syntax: con2tag_Ordering#
130 instance Ord Ordering where
137 -- We don't use deriving for Ord Char, because for Ord the derived
138 -- instance defines only compare, which takes two primops. Then
139 -- '>' uses compare, and therefore takes two primops instead of one.
140 instance Ord Char where
141 (C# c1) > (C# c2) = c1 `gtChar#` c2
142 (C# c1) >= (C# c2) = c1 `geChar#` c2
143 (C# c1) <= (C# c2) = c1 `leChar#` c2
144 (C# c1) < (C# c2) = c1 `ltChar#` c2
146 -- OK, so they're technically not part of a class...:
151 (&&) :: Bool -> Bool -> Bool
156 (||) :: Bool -> Bool -> Bool