summaryrefslogtreecommitdiff
path: root/ghc/lib/std/PrelEnum.lhs
blob: 2b0f5bd5af0e6cdce2a6b308a9bbade2a5affe9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
%
\section[PrelBounded]{Module @PrelBounded@}

Instances of Bounded for various datatypes.

\begin{code}
{-# OPTIONS -fno-implicit-prelude #-}

module PrelEnum(
	Bounded(..), Enum(..),
	enumFromBounded, enumFromThenBounded,

	-- Instances for Bounded and Eum: (), Char, Int

   ) where

import {-# SOURCE #-} PrelErr ( error )
import PrelBase
import PrelTup	()	-- To make sure we look for the .hi file

default ()		-- Double isn't available yet
\end{code}


%*********************************************************
%*							*
\subsection{Class declarations}
%*							*
%*********************************************************

\begin{code}
class  Bounded a  where
    minBound, maxBound :: a

class  Enum a	where
    succ, pred		:: a -> a
    toEnum              :: Int -> a
    fromEnum            :: a -> Int
    enumFrom		:: a -> [a]		-- [n..]
    enumFromThen	:: a -> a -> [a]	-- [n,n'..]
    enumFromTo		:: a -> a -> [a]	-- [n..m]
    enumFromThenTo	:: a -> a -> a -> [a]	-- [n,n'..m]

    succ		   = toEnum . (`plusInt` oneInt)  . fromEnum
    pred		   = toEnum . (`minusInt` oneInt) . fromEnum
    enumFrom x       	   = map toEnum [fromEnum x ..]
    enumFromThen x y 	   = map toEnum [fromEnum x, fromEnum y ..]
    enumFromTo x y         = map toEnum [fromEnum x .. fromEnum y]
    enumFromThenTo x1 x2 y = map toEnum [fromEnum x1, fromEnum x2 .. fromEnum y]

-- Default methods for bounded enumerations
enumFromBounded :: (Enum a, Bounded a) => a -> [a]
enumFromBounded n = map toEnum [fromEnum n .. fromEnum (maxBound `asTypeOf` n)]

enumFromThenBounded :: (Enum a, Bounded a) => a -> a -> [a]
enumFromThenBounded n1 n2 
  | i_n2 >= i_n1  = map toEnum [i_n1, i_n2 .. fromEnum (maxBound `asTypeOf` n1)]
  | otherwise     = map toEnum [i_n1, i_n2 .. fromEnum (minBound `asTypeOf` n1)]
  where
    i_n1 = fromEnum n1
    i_n2 = fromEnum n2
\end{code}


%*********************************************************
%*							*
\subsection{Tuples}
%*							*
%*********************************************************

\begin{code}
instance Bounded () where
    minBound = ()
    maxBound = ()

instance Enum () where
    succ _      = error "Prelude.Enum.().succ: bad argment"
    pred _      = error "Prelude.Enum.().pred: bad argument"

    toEnum x | x == zeroInt = ()
             | otherwise    = error "Prelude.Enum.().toEnum: bad argument"

    fromEnum () = zeroInt
    enumFrom () 	= [()]
    enumFromThen () () 	= [()]
    enumFromTo () () 	= [()]
    enumFromThenTo () () () = [()]
\end{code}

\begin{code}
instance (Bounded a, Bounded b) => Bounded (a,b) where
   minBound = (minBound, minBound)
   maxBound = (maxBound, maxBound)

instance (Bounded a, Bounded b, Bounded c) => Bounded (a,b,c) where
   minBound = (minBound, minBound, minBound)
   maxBound = (maxBound, maxBound, maxBound)

instance (Bounded a, Bounded b, Bounded c, Bounded d) => Bounded (a,b,c,d) where
   minBound = (minBound, minBound, minBound, minBound)
   maxBound = (maxBound, maxBound, maxBound, maxBound)
\end{code}


%*********************************************************
%*							*
\subsection{Type @Bool@}
%*							*
%*********************************************************

\begin{code}
instance Bounded Bool where
  minBound = False
  maxBound = True

instance Enum Bool where
  succ False = True
  succ True  = error "Prelude.Enum.Bool.succ: bad argment"

  pred True  = False
  pred False  = error "Prelude.Enum.Bool.pred: bad argment"

  toEnum n | n == zeroInt = False
	   | n == oneInt  = True
	   | otherwise    = error "Prelude.Enum.Bool.toEnum: bad argment"

  fromEnum False = zeroInt
  fromEnum True  = oneInt

  -- Use defaults for the rest
  enumFrom     = enumFromBounded
  enumFromThen = enumFromThenBounded
\end{code}

%*********************************************************
%*							*
\subsection{Type @Ordering@}
%*							*
%*********************************************************

\begin{code}
instance Bounded Ordering where
  minBound = LT
  maxBound = GT

instance Enum Ordering where
  succ LT = EQ
  succ EQ = GT
  succ GT = error "Prelude.Enum.Ordering.succ: bad argment"

  pred GT = EQ
  pred EQ = LT
  pred LT = error "Prelude.Enum.Ordering.pred: bad argment"

  toEnum n | n == zeroInt = LT
	   | n == oneInt  = EQ
	   | n == twoInt  = GT
  toEnum _ = error "Prelude.Enum.Ordering.toEnum: bad argment"

  fromEnum LT = zeroInt
  fromEnum EQ = oneInt
  fromEnum GT = twoInt

  -- Use defaults for the rest
  enumFrom     = enumFromBounded
  enumFromThen = enumFromThenBounded
\end{code}

%*********************************************************
%*							*
\subsection{Type @Char@}
%*							*
%*********************************************************

\begin{code}
instance  Bounded Char  where
    minBound =  '\0'
    maxBound =  '\255'

instance  Enum Char  where
    succ (C# c#)
       | not (ord# c# ==# 255#) = C# (chr# (ord# c# +# 1#))
       | otherwise	        = error ("Prelude.Enum.Char.succ: bad argument")
    pred (C# c#)
       | not (ord# c# ==# 0#)   = C# (chr# (ord# c# -# 1#))
       | otherwise	        = error ("Prelude.Enum.Char.pred: bad argument")

    toEnum   = chr
    fromEnum = ord

    {-# INLINE enumFrom #-}
    enumFrom (C# x) = build (\ c n -> eftCharFB c n (ord# x) 255#)
	-- Blarg: technically I guess enumFrom isn't strict!

    {-# INLINE enumFromTo #-}
    enumFromTo (C# x) (C# y) = build (\ c n -> eftCharFB c n (ord# x) (ord# y))

    {-# INLINE enumFromThen #-}
    enumFromThen (C# x1) (C# x2) = build (\ c n -> efdCharFB c n (ord# x1) (ord# x2))

    {-# INLINE enumFromThenTo #-}
    enumFromThenTo (C# x1) (C# x2) (C# y) = build (\ c n -> efdtCharFB c n (ord# x1) (ord# x2) (ord# y))

-- We can do better than for Ints because we don't
-- have hassles about arithmetic overflow at maxBound
{-# INLINE eftCharFB #-}
eftCharFB c n x y = go x
		 where
		    go x | x ># y    = n
			 | otherwise = C# (chr# x) `c` go (x +# 1#)

eftCharList x y | x ># y    = [] 
	        | otherwise = C# (chr# x) : eftCharList (x +# 1#) y


-- For enumFromThenTo we give up on inlining
efdCharFB c n x1 x2
  | delta >=# 0# = go_up_char_fb c n x1 delta 255#
  | otherwise    = go_dn_char_fb c n x1 delta 0#
  where
    delta = x2 -# x1

efdCharList x1 x2
  | delta >=# 0# = go_up_char_list x1 delta 255#
  | otherwise    = go_dn_char_list x1 delta 0#
  where
    delta = x2 -# x1

efdtCharFB c n x1 x2 lim
  | delta >=# 0# = go_up_char_fb c n x1 delta lim
  | otherwise    = go_dn_char_fb c n x1 delta lim
  where
    delta = x2 -# x1

efdtCharList x1 x2 lim
  | delta >=# 0# = go_up_char_list x1 delta lim
  | otherwise    = go_dn_char_list x1 delta lim
  where
    delta = x2 -# x1

go_up_char_fb c n x delta lim
  = go_up x
  where
    go_up x | x ># lim  = n
	    | otherwise	= C# (chr# x) `c` go_up (x +# delta)

go_dn_char_fb c n x delta lim
  = go_dn x
  where
    go_dn x | x <# lim  = n
	    | otherwise	= C# (chr# x) `c` go_dn (x +# delta)

go_up_char_list x delta lim
  = go_up x
  where
    go_up x | x ># lim  = []
	    | otherwise	= C# (chr# x) : go_up (x +# delta)

go_dn_char_list x delta lim
  = go_dn x
  where
    go_dn x | x <# lim  = []
	    | otherwise	= C# (chr# x) : go_dn (x +# delta)


{-# RULES
"eftCharList"	eftCharFB  (:) [] = eftCharList
"efdCharList"	efdCharFB  (:) [] = efdCharList
"efdtCharList"	efdtCharFB (:) [] = efdtCharList
 #-}
\end{code}


%*********************************************************
%*							*
\subsection{Type @Int@}
%*							*
%*********************************************************

Be careful about these instances.  
	(a) remember that you have to count down as well as up e.g. [13,12..0]
	(b) be careful of Int overflow
	(c) remember that Int is bounded, so [1..] terminates at maxInt

Also NB that the Num class isn't available in this module.
	
\begin{code}
instance  Bounded Int where
    minBound =  minInt
    maxBound =  maxInt

instance  Enum Int  where
    succ x  
       | x == maxBound  = error "Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
       | otherwise      = x `plusInt` oneInt
    pred x
       | x == minBound  = error "Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
       | otherwise      = x `minusInt` oneInt

    toEnum   x = x
    fromEnum x = x

    {-# INLINE enumFrom #-}
    enumFrom (I# x) = build (\ c n -> eftIntFB c n x 2147483647#)
	-- Blarg: technically I guess enumFrom isn't strict!

    {-# INLINE enumFromTo #-}
    enumFromTo (I# x) (I# y) = build (\ c n -> eftIntFB c n x y)

    {-# INLINE enumFromThen #-}
    enumFromThen (I# x1) (I# x2) = build (\ c n -> efdIntFB c n x1 x2)

    {-# INLINE enumFromThenTo #-}
    enumFromThenTo (I# x1) (I# x2) (I# y) = build (\ c n -> efdtIntFB c n x1 x2 y)

{-# INLINE eftIntFB #-}
eftIntFB c n x y | x ># y    = n	
		 | otherwise = go x
		 where
		   go x = I# x `c` if x ==# y then n else go (x +# 1#)
			-- Watch out for y=maxBound; hence ==, not >
	-- Be very careful not to have more than one "c"
	-- so that when eftInfFB is inlined we can inline
	-- whatver is bound to "c"

eftIntList x y | x ># y    = []
	       | otherwise = go x
	       where
		 go x = I# x : if x ==# y then [] else go (x +# 1#)


-- For enumFromThenTo we give up on inlining; so we don't worry
-- about duplicating occurrences of "c"
efdtIntFB c n x1 x2 y
  | delta >=# 0# = if x1 ># y then n else go_up_int_fb c n x1 delta lim
  | otherwise    = if x1 <# y then n else go_dn_int_fb c n x1 delta lim 
  where
    delta = x2 -# x1
    lim   = y -# delta

efdtIntList x1 x2 y
  | delta >=# 0# = if x1 ># y then [] else go_up_int_list x1 delta lim
  | otherwise    = if x1 <# y then [] else go_dn_int_list x1 delta lim
  where
    delta = x2 -# x1
    lim   = y -# delta

efdIntFB c n x1 x2
  | delta >=# 0# = go_up_int_fb c n x1 delta (  2147483647#  -# delta)
  | otherwise    = go_dn_int_fb c n x1 delta ((-2147483648#) -# delta)
  where
    delta = x2 -# x1

efdIntList x1 x2
  | delta >=# 0# = go_up_int_list x1 delta (  2147483647#  -# delta)
  | otherwise    = go_dn_int_list x1 delta ((-2147483648#) -# delta)
  where
    delta = x2 -# x1

-- In all of these, the (x +# delta) is guaranteed not to overflow

go_up_int_fb c n x delta lim
  = go_up x
  where
    go_up x | x ># lim  = I# x `c` n
	    | otherwise = I# x `c` go_up (x +# delta)

go_dn_int_fb c n x delta lim 
  = go_dn x
  where
    go_dn x | x <# lim  = I# x `c` n
	    | otherwise = I# x `c` go_dn (x +# delta)

go_up_int_list x delta lim
  = go_up x
  where
    go_up x | x ># lim  = [I# x]
	    | otherwise = I# x : go_up (x +# delta)

go_dn_int_list x delta lim 
  = go_dn x
  where
    go_dn x | x <# lim  = [I# x]
	    | otherwise = I# x : go_dn (x +# delta)


{-# RULES
"eftIntList"	eftIntFB  (:) [] = eftIntList
"efdIntList"	efdIntFB  (:) [] = efdIntList
"efdtIntList"	efdtIntFB (:) [] = efdtIntList
 #-}
\end{code}