[project @ 2002-02-12 11:44:54 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelEnum.lhs
1 % -----------------------------------------------------------------------------
2 % $Id: PrelEnum.lhs,v 1.18 2002/01/29 09:58:19 simonpj Exp $
3 %
4 % (c) The University of Glasgow, 1992-2001
5 %
6
7 Instances of Bounded and Enum for various datatypes.
8
9 \begin{code}
10 {-# OPTIONS -fno-implicit-prelude #-}
11
12 module PrelEnum(
13         Bounded(..), Enum(..),
14         boundedEnumFrom, boundedEnumFromThen,
15
16         -- Instances for Bounded and Eum: (), Char, Int
17
18    ) where
19
20 import {-# SOURCE #-} PrelErr ( error )
21 import PrelBase
22 import PrelTup  ()      -- To make sure we look for the .hi file
23
24 default ()              -- Double isn't available yet
25 \end{code}
26
27
28 %*********************************************************
29 %*                                                      *
30 \subsection{Class declarations}
31 %*                                                      *
32 %*********************************************************
33
34 \begin{code}
35 class  Bounded a  where
36     minBound, maxBound :: a
37
38 class  Enum a   where
39     succ, pred          :: a -> a
40     toEnum              :: Int -> a
41     fromEnum            :: a -> Int
42     enumFrom            :: a -> [a]             -- [n..]
43     enumFromThen        :: a -> a -> [a]        -- [n,n'..]
44     enumFromTo          :: a -> a -> [a]        -- [n..m]
45     enumFromThenTo      :: a -> a -> a -> [a]   -- [n,n'..m]
46
47     succ                   = toEnum . (`plusInt` oneInt)  . fromEnum
48     pred                   = toEnum . (`minusInt` oneInt) . fromEnum
49     enumFrom x             = map toEnum [fromEnum x ..]
50     enumFromThen x y       = map toEnum [fromEnum x, fromEnum y ..]
51     enumFromTo x y         = map toEnum [fromEnum x .. fromEnum y]
52     enumFromThenTo x1 x2 y = map toEnum [fromEnum x1, fromEnum x2 .. fromEnum y]
53
54 -- Default methods for bounded enumerations
55 boundedEnumFrom :: (Enum a, Bounded a) => a -> [a]
56 boundedEnumFrom n = map toEnum [fromEnum n .. fromEnum (maxBound `asTypeOf` n)]
57
58 boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a]
59 boundedEnumFromThen n1 n2 
60   | i_n2 >= i_n1  = map toEnum [i_n1, i_n2 .. fromEnum (maxBound `asTypeOf` n1)]
61   | otherwise     = map toEnum [i_n1, i_n2 .. fromEnum (minBound `asTypeOf` n1)]
62   where
63     i_n1 = fromEnum n1
64     i_n2 = fromEnum n2
65 \end{code}
66
67
68 %*********************************************************
69 %*                                                      *
70 \subsection{Tuples}
71 %*                                                      *
72 %*********************************************************
73
74 \begin{code}
75 instance Bounded () where
76     minBound = ()
77     maxBound = ()
78
79 instance Enum () where
80     succ _      = error "Prelude.Enum.().succ: bad argment"
81     pred _      = error "Prelude.Enum.().pred: bad argument"
82
83     toEnum x | x == zeroInt = ()
84              | otherwise    = error "Prelude.Enum.().toEnum: bad argument"
85
86     fromEnum () = zeroInt
87     enumFrom ()         = [()]
88     enumFromThen () ()  = [()]
89     enumFromTo () ()    = [()]
90     enumFromThenTo () () () = [()]
91 \end{code}
92
93 \begin{code}
94 instance (Bounded a, Bounded b) => Bounded (a,b) where
95    minBound = (minBound, minBound)
96    maxBound = (maxBound, maxBound)
97
98 instance (Bounded a, Bounded b, Bounded c) => Bounded (a,b,c) where
99    minBound = (minBound, minBound, minBound)
100    maxBound = (maxBound, maxBound, maxBound)
101
102 instance (Bounded a, Bounded b, Bounded c, Bounded d) => Bounded (a,b,c,d) where
103    minBound = (minBound, minBound, minBound, minBound)
104    maxBound = (maxBound, maxBound, maxBound, maxBound)
105 \end{code}
106
107
108 %*********************************************************
109 %*                                                      *
110 \subsection{Type @Bool@}
111 %*                                                      *
112 %*********************************************************
113
114 \begin{code}
115 instance Bounded Bool where
116   minBound = False
117   maxBound = True
118
119 instance Enum Bool where
120   succ False = True
121   succ True  = error "Prelude.Enum.Bool.succ: bad argment"
122
123   pred True  = False
124   pred False  = error "Prelude.Enum.Bool.pred: bad argment"
125
126   toEnum n | n == zeroInt = False
127            | n == oneInt  = True
128            | otherwise    = error "Prelude.Enum.Bool.toEnum: bad argment"
129
130   fromEnum False = zeroInt
131   fromEnum True  = oneInt
132
133   -- Use defaults for the rest
134   enumFrom     = boundedEnumFrom
135   enumFromThen = boundedEnumFromThen
136 \end{code}
137
138 %*********************************************************
139 %*                                                      *
140 \subsection{Type @Ordering@}
141 %*                                                      *
142 %*********************************************************
143
144 \begin{code}
145 instance Bounded Ordering where
146   minBound = LT
147   maxBound = GT
148
149 instance Enum Ordering where
150   succ LT = EQ
151   succ EQ = GT
152   succ GT = error "Prelude.Enum.Ordering.succ: bad argment"
153
154   pred GT = EQ
155   pred EQ = LT
156   pred LT = error "Prelude.Enum.Ordering.pred: bad argment"
157
158   toEnum n | n == zeroInt = LT
159            | n == oneInt  = EQ
160            | n == twoInt  = GT
161   toEnum _ = error "Prelude.Enum.Ordering.toEnum: bad argment"
162
163   fromEnum LT = zeroInt
164   fromEnum EQ = oneInt
165   fromEnum GT = twoInt
166
167   -- Use defaults for the rest
168   enumFrom     = boundedEnumFrom
169   enumFromThen = boundedEnumFromThen
170 \end{code}
171
172 %*********************************************************
173 %*                                                      *
174 \subsection{Type @Char@}
175 %*                                                      *
176 %*********************************************************
177
178 \begin{code}
179 instance  Bounded Char  where
180     minBound =  '\0'
181     maxBound =  '\x10FFFF'
182
183 instance  Enum Char  where
184     succ (C# c#)
185        | not (ord# c# ==# 0x10FFFF#) = C# (chr# (ord# c# +# 1#))
186        | otherwise              = error ("Prelude.Enum.Char.succ: bad argument")
187     pred (C# c#)
188        | not (ord# c# ==# 0#)   = C# (chr# (ord# c# -# 1#))
189        | otherwise              = error ("Prelude.Enum.Char.pred: bad argument")
190
191     toEnum   = chr
192     fromEnum = ord
193
194     {-# INLINE enumFrom #-}
195     enumFrom (C# x) = eftChar (ord# x) 0x10FFFF#
196         -- Blarg: technically I guess enumFrom isn't strict!
197
198     {-# INLINE enumFromTo #-}
199     enumFromTo (C# x) (C# y) = eftChar (ord# x) (ord# y)
200     
201     {-# INLINE enumFromThen #-}
202     enumFromThen (C# x1) (C# x2) = efdChar (ord# x1) (ord# x2)
203     
204     {-# INLINE enumFromThenTo #-}
205     enumFromThenTo (C# x1) (C# x2) (C# y) = efdtChar (ord# x1) (ord# x2) (ord# y)
206
207 {-# RULES
208 "eftChar"       [~1] forall x y.        eftChar x y       = build (\c n -> eftCharFB c n x y)
209 "efdChar"       [~1] forall x1 x2.      efdChar x1 x2     = build (\ c n -> efdCharFB c n x1 x2)
210 "efdtChar"      [~1] forall x1 x2 l.    efdtChar x1 x2 l  = build (\ c n -> efdtCharFB c n x1 x2 l)
211 "eftCharList"   [1]  eftCharFB  (:) [] = eftChar
212 "efdCharList"   [1]  efdCharFB  (:) [] = efdChar
213 "efdtCharList"  [1]  efdtCharFB (:) [] = efdtChar
214  #-}
215
216
217 -- We can do better than for Ints because we don't
218 -- have hassles about arithmetic overflow at maxBound
219 {-# INLINE [0] eftCharFB #-}
220 eftCharFB c n x y = go x
221                  where
222                     go x | x ># y    = n
223                          | otherwise = C# (chr# x) `c` go (x +# 1#)
224
225 eftChar x y | x ># y    = [] 
226                 | otherwise = C# (chr# x) : eftChar (x +# 1#) y
227
228
229 -- For enumFromThenTo we give up on inlining
230 {-# NOINLINE [0] efdCharFB #-}
231 efdCharFB c n x1 x2
232   | delta >=# 0# = go_up_char_fb c n x1 delta 0x10FFFF#
233   | otherwise    = go_dn_char_fb c n x1 delta 0#
234   where
235     delta = x2 -# x1
236
237 efdChar x1 x2
238   | delta >=# 0# = go_up_char_list x1 delta 0x10FFFF#
239   | otherwise    = go_dn_char_list x1 delta 0#
240   where
241     delta = x2 -# x1
242
243 {-# NOINLINE [0] efdtCharFB #-}
244 efdtCharFB c n x1 x2 lim
245   | delta >=# 0# = go_up_char_fb c n x1 delta lim
246   | otherwise    = go_dn_char_fb c n x1 delta lim
247   where
248     delta = x2 -# x1
249
250 efdtChar x1 x2 lim
251   | delta >=# 0# = go_up_char_list x1 delta lim
252   | otherwise    = go_dn_char_list x1 delta lim
253   where
254     delta = x2 -# x1
255
256 go_up_char_fb c n x delta lim
257   = go_up x
258   where
259     go_up x | x ># lim  = n
260             | otherwise = C# (chr# x) `c` go_up (x +# delta)
261
262 go_dn_char_fb c n x delta lim
263   = go_dn x
264   where
265     go_dn x | x <# lim  = n
266             | otherwise = C# (chr# x) `c` go_dn (x +# delta)
267
268 go_up_char_list x delta lim
269   = go_up x
270   where
271     go_up x | x ># lim  = []
272             | otherwise = C# (chr# x) : go_up (x +# delta)
273
274 go_dn_char_list x delta lim
275   = go_dn x
276   where
277     go_dn x | x <# lim  = []
278             | otherwise = C# (chr# x) : go_dn (x +# delta)
279 \end{code}
280
281
282 %*********************************************************
283 %*                                                      *
284 \subsection{Type @Int@}
285 %*                                                      *
286 %*********************************************************
287
288 Be careful about these instances.  
289         (a) remember that you have to count down as well as up e.g. [13,12..0]
290         (b) be careful of Int overflow
291         (c) remember that Int is bounded, so [1..] terminates at maxInt
292
293 Also NB that the Num class isn't available in this module.
294         
295 \begin{code}
296 instance  Bounded Int where
297     minBound =  minInt
298     maxBound =  maxInt
299
300 instance  Enum Int  where
301     succ x  
302        | x == maxBound  = error "Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
303        | otherwise      = x `plusInt` oneInt
304     pred x
305        | x == minBound  = error "Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
306        | otherwise      = x `minusInt` oneInt
307
308     toEnum   x = x
309     fromEnum x = x
310
311     {-# INLINE enumFrom #-}
312     enumFrom (I# x) = eftInt x maxInt#
313         where I# maxInt# = maxInt
314         -- Blarg: technically I guess enumFrom isn't strict!
315
316     {-# INLINE enumFromTo #-}
317     enumFromTo (I# x) (I# y) = eftInt x y
318
319     {-# INLINE enumFromThen #-}
320     enumFromThen (I# x1) (I# x2) = efdInt x1 x2
321
322     {-# INLINE enumFromThenTo #-}
323     enumFromThenTo (I# x1) (I# x2) (I# y) = efdtInt x1 x2 y
324
325 {-# RULES
326 "eftInt"        [~1] forall x y.        eftInt x y       = build (\ c n -> eftIntFB c n x y)
327 "efdInt"        [~1] forall x1 x2.      efdInt x1 x2     = build (\ c n -> efdIntFB c n x1 x2)
328 "efdtInt"       [~1] forall x1 x2 l.    efdtInt x1 x2 l  = build (\ c n -> efdtIntFB c n x1 x2 l)
329
330 "eftIntList"    [1] eftIntFB  (:) [] = eftInt
331 "efdIntList"    [1] efdIntFB  (:) [] = efdInt
332 "efdtIntList"   [1] efdtIntFB (:) [] = efdtInt
333  #-}
334
335
336 {-# INLINE [0] eftIntFB #-}
337 eftIntFB c n x y | x ># y    = n        
338                  | otherwise = go x
339                  where
340                    go x = I# x `c` if x ==# y then n else go (x +# 1#)
341                         -- Watch out for y=maxBound; hence ==, not >
342         -- Be very careful not to have more than one "c"
343         -- so that when eftInfFB is inlined we can inline
344         -- whatver is bound to "c"
345
346 eftInt x y | x ># y    = []
347                | otherwise = go x
348                where
349                  go x = I# x : if x ==# y then [] else go (x +# 1#)
350
351
352 -- For enumFromThenTo we give up on inlining; so we don't worry
353 -- about duplicating occurrences of "c"
354 {-# NOINLINE [0] efdtIntFB #-}
355 efdtIntFB c n x1 x2 y
356   | delta >=# 0# = if x1 ># y then n else go_up_int_fb c n x1 delta lim
357   | otherwise    = if x1 <# y then n else go_dn_int_fb c n x1 delta lim 
358   where
359     delta = x2 -# x1
360     lim   = y -# delta
361
362 efdtInt x1 x2 y
363   | delta >=# 0# = if x1 ># y then [] else go_up_int_list x1 delta lim
364   | otherwise    = if x1 <# y then [] else go_dn_int_list x1 delta lim
365   where
366     delta = x2 -# x1
367     lim   = y -# delta
368
369 {-# NOINLINE [0] efdIntFB #-}
370 efdIntFB c n x1 x2
371   | delta >=# 0# = case maxInt of I# y -> go_up_int_fb c n x1 delta (y -# delta)
372   | otherwise    = case minInt of I# y -> go_dn_int_fb c n x1 delta (y -# delta)
373   where
374     delta = x2 -# x1
375
376 efdInt x1 x2
377   | delta >=# 0# = case maxInt of I# y -> go_up_int_list x1 delta (y -# delta)
378   | otherwise    = case minInt of I# y -> go_dn_int_list x1 delta (y -# delta)
379   where
380     delta = x2 -# x1
381
382 -- In all of these, the (x +# delta) is guaranteed not to overflow
383
384 go_up_int_fb c n x delta lim
385   = go_up x
386   where
387     go_up x | x ># lim  = I# x `c` n
388             | otherwise = I# x `c` go_up (x +# delta)
389
390 go_dn_int_fb c n x delta lim 
391   = go_dn x
392   where
393     go_dn x | x <# lim  = I# x `c` n
394             | otherwise = I# x `c` go_dn (x +# delta)
395
396 go_up_int_list x delta lim
397   = go_up x
398   where
399     go_up x | x ># lim  = [I# x]
400             | otherwise = I# x : go_up (x +# delta)
401
402 go_dn_int_list x delta lim 
403   = go_dn x
404   where
405     go_dn x | x <# lim  = [I# x]
406             | otherwise = I# x : go_dn (x +# delta)
407 \end{code}
408