2 {-# LANGUAGE BangPatterns #-}
3 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
4 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
8 -- Copyright : (c) The University of Glasgow, 1992-2002
9 -- License : see libraries/base/LICENSE
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC extensions)
15 -- The 'Enum' and 'Bounded' classes.
17 -----------------------------------------------------------------------------
21 Bounded(..), Enum(..),
22 boundedEnumFrom, boundedEnumFromThen,
24 -- Instances for Bounded and Enum: (), Char, Int
29 import Data.Tuple () -- for dependencies
30 default () -- Double isn't available yet
34 %*********************************************************
36 \subsection{Class declarations}
38 %*********************************************************
41 -- | The 'Bounded' class is used to name the upper and lower limits of a
42 -- type. 'Ord' is not a superclass of 'Bounded' since types that are not
43 -- totally ordered may also have upper and lower bounds.
45 -- The 'Bounded' class may be derived for any enumeration type;
46 -- 'minBound' is the first constructor listed in the @data@ declaration
47 -- and 'maxBound' is the last.
48 -- 'Bounded' may also be derived for single-constructor datatypes whose
49 -- constituent types are in 'Bounded'.
52 minBound, maxBound :: a
54 -- | Class 'Enum' defines operations on sequentially ordered types.
56 -- The @enumFrom@... methods are used in Haskell's translation of
57 -- arithmetic sequences.
59 -- Instances of 'Enum' may be derived for any enumeration type (types
60 -- whose constructors have no fields). The nullary constructors are
61 -- assumed to be numbered left-to-right by 'fromEnum' from @0@ through @n-1@.
62 -- See Chapter 10 of the /Haskell Report/ for more details.
64 -- For any type that is an instance of class 'Bounded' as well as 'Enum',
65 -- the following should hold:
67 -- * The calls @'succ' 'maxBound'@ and @'pred' 'minBound'@ should result in
70 -- * 'fromEnum' and 'toEnum' should give a runtime error if the
71 -- result value is not representable in the result type.
72 -- For example, @'toEnum' 7 :: 'Bool'@ is an error.
74 -- * 'enumFrom' and 'enumFromThen' should be defined with an implicit bound,
77 -- > enumFrom x = enumFromTo x maxBound
78 -- > enumFromThen x y = enumFromThenTo x y bound
80 -- > bound | fromEnum y >= fromEnum x = maxBound
81 -- > | otherwise = minBound
84 -- | the successor of a value. For numeric types, 'succ' adds 1.
86 -- | the predecessor of a value. For numeric types, 'pred' subtracts 1.
88 -- | Convert from an 'Int'.
90 -- | Convert to an 'Int'.
91 -- It is implementation-dependent what 'fromEnum' returns when
92 -- applied to a value that is too large to fit in an 'Int'.
95 -- | Used in Haskell's translation of @[n..]@.
97 -- | Used in Haskell's translation of @[n,n'..]@.
98 enumFromThen :: a -> a -> [a]
99 -- | Used in Haskell's translation of @[n..m]@.
100 enumFromTo :: a -> a -> [a]
101 -- | Used in Haskell's translation of @[n,n'..m]@.
102 enumFromThenTo :: a -> a -> a -> [a]
104 succ = toEnum . (`plusInt` oneInt) . fromEnum
105 pred = toEnum . (`minusInt` oneInt) . fromEnum
106 enumFrom x = map toEnum [fromEnum x ..]
107 enumFromThen x y = map toEnum [fromEnum x, fromEnum y ..]
108 enumFromTo x y = map toEnum [fromEnum x .. fromEnum y]
109 enumFromThenTo x1 x2 y = map toEnum [fromEnum x1, fromEnum x2 .. fromEnum y]
111 -- Default methods for bounded enumerations
112 boundedEnumFrom :: (Enum a, Bounded a) => a -> [a]
113 boundedEnumFrom n = map toEnum [fromEnum n .. fromEnum (maxBound `asTypeOf` n)]
115 boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a]
116 boundedEnumFromThen n1 n2
117 | i_n2 >= i_n1 = map toEnum [i_n1, i_n2 .. fromEnum (maxBound `asTypeOf` n1)]
118 | otherwise = map toEnum [i_n1, i_n2 .. fromEnum (minBound `asTypeOf` n1)]
125 %*********************************************************
129 %*********************************************************
132 instance Bounded () where
136 instance Enum () where
137 succ _ = error "Prelude.Enum.().succ: bad argument"
138 pred _ = error "Prelude.Enum.().pred: bad argument"
140 toEnum x | x == zeroInt = ()
141 | otherwise = error "Prelude.Enum.().toEnum: bad argument"
143 fromEnum () = zeroInt
145 enumFromThen () () = let many = ():many in many
146 enumFromTo () () = [()]
147 enumFromThenTo () () () = let many = ():many in many
151 -- Report requires instances up to 15
152 instance (Bounded a, Bounded b) => Bounded (a,b) where
153 minBound = (minBound, minBound)
154 maxBound = (maxBound, maxBound)
156 instance (Bounded a, Bounded b, Bounded c) => Bounded (a,b,c) where
157 minBound = (minBound, minBound, minBound)
158 maxBound = (maxBound, maxBound, maxBound)
160 instance (Bounded a, Bounded b, Bounded c, Bounded d) => Bounded (a,b,c,d) where
161 minBound = (minBound, minBound, minBound, minBound)
162 maxBound = (maxBound, maxBound, maxBound, maxBound)
164 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e) => Bounded (a,b,c,d,e) where
165 minBound = (minBound, minBound, minBound, minBound, minBound)
166 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound)
168 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f)
169 => Bounded (a,b,c,d,e,f) where
170 minBound = (minBound, minBound, minBound, minBound, minBound, minBound)
171 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound)
173 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g)
174 => Bounded (a,b,c,d,e,f,g) where
175 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound)
176 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound)
178 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
180 => Bounded (a,b,c,d,e,f,g,h) where
181 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound)
182 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound)
184 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
185 Bounded h, Bounded i)
186 => Bounded (a,b,c,d,e,f,g,h,i) where
187 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
189 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
192 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
193 Bounded h, Bounded i, Bounded j)
194 => Bounded (a,b,c,d,e,f,g,h,i,j) where
195 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
197 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
200 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
201 Bounded h, Bounded i, Bounded j, Bounded k)
202 => Bounded (a,b,c,d,e,f,g,h,i,j,k) where
203 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
204 minBound, minBound, minBound)
205 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
206 maxBound, maxBound, maxBound)
208 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
209 Bounded h, Bounded i, Bounded j, Bounded k, Bounded l)
210 => Bounded (a,b,c,d,e,f,g,h,i,j,k,l) where
211 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
212 minBound, minBound, minBound, minBound)
213 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
214 maxBound, maxBound, maxBound, maxBound)
216 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
217 Bounded h, Bounded i, Bounded j, Bounded k, Bounded l, Bounded m)
218 => Bounded (a,b,c,d,e,f,g,h,i,j,k,l,m) where
219 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
220 minBound, minBound, minBound, minBound, minBound)
221 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
222 maxBound, maxBound, maxBound, maxBound, maxBound)
224 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
225 Bounded h, Bounded i, Bounded j, Bounded k, Bounded l, Bounded m, Bounded n)
226 => Bounded (a,b,c,d,e,f,g,h,i,j,k,l,m,n) where
227 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
228 minBound, minBound, minBound, minBound, minBound, minBound)
229 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
230 maxBound, maxBound, maxBound, maxBound, maxBound, maxBound)
232 instance (Bounded a, Bounded b, Bounded c, Bounded d, Bounded e, Bounded f, Bounded g,
233 Bounded h, Bounded i, Bounded j, Bounded k, Bounded l, Bounded m, Bounded n, Bounded o)
234 => Bounded (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) where
235 minBound = (minBound, minBound, minBound, minBound, minBound, minBound, minBound, minBound,
236 minBound, minBound, minBound, minBound, minBound, minBound, minBound)
237 maxBound = (maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound,
238 maxBound, maxBound, maxBound, maxBound, maxBound, maxBound, maxBound)
242 %*********************************************************
244 \subsection{Type @Bool@}
246 %*********************************************************
249 instance Bounded Bool where
253 instance Enum Bool where
255 succ True = error "Prelude.Enum.Bool.succ: bad argument"
258 pred False = error "Prelude.Enum.Bool.pred: bad argument"
260 toEnum n | n == zeroInt = False
262 | otherwise = error "Prelude.Enum.Bool.toEnum: bad argument"
264 fromEnum False = zeroInt
265 fromEnum True = oneInt
267 -- Use defaults for the rest
268 enumFrom = boundedEnumFrom
269 enumFromThen = boundedEnumFromThen
272 %*********************************************************
274 \subsection{Type @Ordering@}
276 %*********************************************************
279 instance Bounded Ordering where
283 instance Enum Ordering where
286 succ GT = error "Prelude.Enum.Ordering.succ: bad argument"
290 pred LT = error "Prelude.Enum.Ordering.pred: bad argument"
292 toEnum n | n == zeroInt = LT
295 toEnum _ = error "Prelude.Enum.Ordering.toEnum: bad argument"
297 fromEnum LT = zeroInt
301 -- Use defaults for the rest
302 enumFrom = boundedEnumFrom
303 enumFromThen = boundedEnumFromThen
306 %*********************************************************
308 \subsection{Type @Char@}
310 %*********************************************************
313 instance Bounded Char where
315 maxBound = '\x10FFFF'
317 instance Enum Char where
319 | not (ord# c# ==# 0x10FFFF#) = C# (chr# (ord# c# +# 1#))
320 | otherwise = error ("Prelude.Enum.Char.succ: bad argument")
322 | not (ord# c# ==# 0#) = C# (chr# (ord# c# -# 1#))
323 | otherwise = error ("Prelude.Enum.Char.pred: bad argument")
328 {-# INLINE enumFrom #-}
329 enumFrom (C# x) = eftChar (ord# x) 0x10FFFF#
330 -- Blarg: technically I guess enumFrom isn't strict!
332 {-# INLINE enumFromTo #-}
333 enumFromTo (C# x) (C# y) = eftChar (ord# x) (ord# y)
335 {-# INLINE enumFromThen #-}
336 enumFromThen (C# x1) (C# x2) = efdChar (ord# x1) (ord# x2)
338 {-# INLINE enumFromThenTo #-}
339 enumFromThenTo (C# x1) (C# x2) (C# y) = efdtChar (ord# x1) (ord# x2) (ord# y)
342 "eftChar" [~1] forall x y. eftChar x y = build (\c n -> eftCharFB c n x y)
343 "efdChar" [~1] forall x1 x2. efdChar x1 x2 = build (\ c n -> efdCharFB c n x1 x2)
344 "efdtChar" [~1] forall x1 x2 l. efdtChar x1 x2 l = build (\ c n -> efdtCharFB c n x1 x2 l)
345 "eftCharList" [1] eftCharFB (:) [] = eftChar
346 "efdCharList" [1] efdCharFB (:) [] = efdChar
347 "efdtCharList" [1] efdtCharFB (:) [] = efdtChar
351 -- We can do better than for Ints because we don't
352 -- have hassles about arithmetic overflow at maxBound
353 {-# INLINE [0] eftCharFB #-}
354 eftCharFB :: (Char -> a -> a) -> a -> Int# -> Int# -> a
355 eftCharFB c n x0 y = go x0
358 | otherwise = C# (chr# x) `c` go (x +# 1#)
360 eftChar :: Int# -> Int# -> String
361 eftChar x y | x ># y = []
362 | otherwise = C# (chr# x) : eftChar (x +# 1#) y
365 -- For enumFromThenTo we give up on inlining
366 {-# NOINLINE [0] efdCharFB #-}
367 efdCharFB :: (Char -> a -> a) -> a -> Int# -> Int# -> a
369 | delta >=# 0# = go_up_char_fb c n x1 delta 0x10FFFF#
370 | otherwise = go_dn_char_fb c n x1 delta 0#
374 efdChar :: Int# -> Int# -> String
376 | delta >=# 0# = go_up_char_list x1 delta 0x10FFFF#
377 | otherwise = go_dn_char_list x1 delta 0#
381 {-# NOINLINE [0] efdtCharFB #-}
382 efdtCharFB :: (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
383 efdtCharFB c n x1 x2 lim
384 | delta >=# 0# = go_up_char_fb c n x1 delta lim
385 | otherwise = go_dn_char_fb c n x1 delta lim
389 efdtChar :: Int# -> Int# -> Int# -> String
391 | delta >=# 0# = go_up_char_list x1 delta lim
392 | otherwise = go_dn_char_list x1 delta lim
396 go_up_char_fb :: (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
397 go_up_char_fb c n x0 delta lim
400 go_up x | x ># lim = n
401 | otherwise = C# (chr# x) `c` go_up (x +# delta)
403 go_dn_char_fb :: (Char -> a -> a) -> a -> Int# -> Int# -> Int# -> a
404 go_dn_char_fb c n x0 delta lim
407 go_dn x | x <# lim = n
408 | otherwise = C# (chr# x) `c` go_dn (x +# delta)
410 go_up_char_list :: Int# -> Int# -> Int# -> String
411 go_up_char_list x0 delta lim
414 go_up x | x ># lim = []
415 | otherwise = C# (chr# x) : go_up (x +# delta)
417 go_dn_char_list :: Int# -> Int# -> Int# -> String
418 go_dn_char_list x0 delta lim
421 go_dn x | x <# lim = []
422 | otherwise = C# (chr# x) : go_dn (x +# delta)
426 %*********************************************************
428 \subsection{Type @Int@}
430 %*********************************************************
432 Be careful about these instances.
433 (a) remember that you have to count down as well as up e.g. [13,12..0]
434 (b) be careful of Int overflow
435 (c) remember that Int is bounded, so [1..] terminates at maxInt
437 Also NB that the Num class isn't available in this module.
440 instance Bounded Int where
444 instance Enum Int where
446 | x == maxBound = error "Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
447 | otherwise = x `plusInt` oneInt
449 | x == minBound = error "Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
450 | otherwise = x `minusInt` oneInt
455 {-# INLINE enumFrom #-}
456 enumFrom (I# x) = eftInt x maxInt#
457 where !(I# maxInt#) = maxInt
458 -- Blarg: technically I guess enumFrom isn't strict!
460 {-# INLINE enumFromTo #-}
461 enumFromTo (I# x) (I# y) = eftInt x y
463 {-# INLINE enumFromThen #-}
464 enumFromThen (I# x1) (I# x2) = efdInt x1 x2
466 {-# INLINE enumFromThenTo #-}
467 enumFromThenTo (I# x1) (I# x2) (I# y) = efdtInt x1 x2 y
470 -----------------------------------------------------
471 -- eftInt and eftIntFB deal with [a..b], which is the
472 -- most common form, so we take a lot of care
473 -- In particular, we have rules for deforestation
476 "eftInt" [~1] forall x y. eftInt x y = build (\ c n -> eftIntFB c n x y)
477 "eftIntList" [1] eftIntFB (:) [] = eftInt
480 eftInt :: Int# -> Int# -> [Int]
482 eftInt x0 y | x0 ># y = []
485 go x = I# x : if x ==# y then [] else go (x +# 1#)
487 {-# INLINE [0] eftIntFB #-}
488 eftIntFB :: (Int -> r -> r) -> r -> Int# -> Int# -> r
489 eftIntFB c n x0 y | x0 ># y = n
492 go x = I# x `c` if x ==# y then n else go (x +# 1#)
493 -- Watch out for y=maxBound; hence ==, not >
494 -- Be very careful not to have more than one "c"
495 -- so that when eftInfFB is inlined we can inline
496 -- whatever is bound to "c"
499 -----------------------------------------------------
500 -- efdInt and efdtInt deal with [a,b..] and [a,b..c].
501 -- The code is more complicated because of worries about Int overflow.
504 "efdtInt" [~1] forall x1 x2 y.
505 efdtInt x1 x2 y = build (\ c n -> efdtIntFB c n x1 x2 y)
506 "efdtIntUpList" [1] efdtIntFB (:) [] = efdtInt
509 efdInt :: Int# -> Int# -> [Int]
512 | x2 >=# x1 = case maxInt of I# y -> efdtIntUp x1 x2 y
513 | otherwise = case minInt of I# y -> efdtIntDn x1 x2 y
515 efdtInt :: Int# -> Int# -> Int# -> [Int]
518 | x2 >=# x1 = efdtIntUp x1 x2 y
519 | otherwise = efdtIntDn x1 x2 y
521 {-# INLINE [0] efdtIntFB #-}
522 efdtIntFB :: (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
523 efdtIntFB c n x1 x2 y
524 | x2 >=# x1 = efdtIntUpFB c n x1 x2 y
525 | otherwise = efdtIntDnFB c n x1 x2 y
528 efdtIntUp :: Int# -> Int# -> Int# -> [Int]
529 efdtIntUp x1 x2 y -- Be careful about overflow!
530 | y <# x2 = if y <# x1 then [] else [I# x1]
531 | otherwise = -- Common case: x1 <= x2 <= y
532 let !delta = x2 -# x1 -- >= 0
533 !y' = y -# delta -- x1 <= y' <= y; hence y' is representable
536 -- Note that: z <= y' => z + delta won't overflow
537 -- so we are guaranteed not to overflow if/when we recurse
538 go_up x | x ># y' = [I# x]
539 | otherwise = I# x : go_up (x +# delta)
543 efdtIntUpFB :: (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
544 efdtIntUpFB c n x1 x2 y -- Be careful about overflow!
545 | y <# x2 = if y <# x1 then n else I# x1 `c` n
546 | otherwise = -- Common case: x1 <= x2 <= y
547 let !delta = x2 -# x1 -- >= 0
548 !y' = y -# delta -- x1 <= y' <= y; hence y' is representable
551 -- Note that: z <= y' => z + delta won't overflow
552 -- so we are guaranteed not to overflow if/when we recurse
553 go_up x | x ># y' = I# x `c` n
554 | otherwise = I# x `c` go_up (x +# delta)
555 in I# x1 `c` go_up x2
558 efdtIntDn :: Int# -> Int# -> Int# -> [Int]
559 efdtIntDn x1 x2 y -- Be careful about underflow!
560 | y ># x2 = if y ># x1 then [] else [I# x1]
561 | otherwise = -- Common case: x1 >= x2 >= y
562 let !delta = x2 -# x1 -- <= 0
563 !y' = y -# delta -- y <= y' <= x1; hence y' is representable
566 -- Note that: z >= y' => z + delta won't underflow
567 -- so we are guaranteed not to underflow if/when we recurse
568 go_dn x | x <# y' = [I# x]
569 | otherwise = I# x : go_dn (x +# delta)
573 efdtIntDnFB :: (Int -> r -> r) -> r -> Int# -> Int# -> Int# -> r
574 efdtIntDnFB c n x1 x2 y -- Be careful about underflow!
575 | y ># x2 = if y ># x1 then n else I# x1 `c` n
576 | otherwise = -- Common case: x1 >= x2 >= y
577 let !delta = x2 -# x1 -- <= 0
578 !y' = y -# delta -- y <= y' <= x1; hence y' is representable
581 -- Note that: z >= y' => z + delta won't underflow
582 -- so we are guaranteed not to underflow if/when we recurse
583 go_dn x | x <# y' = I# x `c` n
584 | otherwise = I# x `c` go_dn (x +# delta)
585 in I# x1 `c` go_dn x2