2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[PrelBounded]{Module @PrelBounded@}
6 Instances of Bounded for various datatypes.
9 {-# OPTIONS -fno-implicit-prelude #-}
12 Bounded(..), Enum(..),
13 enumFromBounded, enumFromThenBounded,
15 -- Instances for Bounded and Eum: (), Char, Int
19 import {-# SOURCE #-} PrelErr ( error )
21 import PrelTup () -- To make sure we look for the .hi file
25 %*********************************************************
27 \subsection{Class declarations}
29 %*********************************************************
33 minBound, maxBound :: a
39 enumFrom :: a -> [a] -- [n..]
40 enumFromThen :: a -> a -> [a] -- [n,n'..]
41 enumFromTo :: a -> a -> [a] -- [n..m]
42 enumFromThenTo :: a -> a -> a -> [a] -- [n,n'..m]
44 succ = toEnum . (`plusInt` oneInt) . fromEnum
45 pred = toEnum . (`minusInt` oneInt) . fromEnum
46 enumFrom x = map toEnum [fromEnum x ..]
47 enumFromThen x y = map toEnum [fromEnum x, fromEnum y ..]
48 enumFromTo x y = map toEnum [fromEnum x .. fromEnum y]
49 enumFromThenTo x1 x2 y = map toEnum [fromEnum x1, fromEnum x2 .. fromEnum y]
51 -- Default methods for bounded enumerations
52 enumFromBounded :: (Enum a, Bounded a) => a -> [a]
53 enumFromBounded n = map toEnum [fromEnum n .. fromEnum (maxBound `asTypeOf` n)]
55 enumFromThenBounded :: (Enum a, Bounded a) => a -> a -> [a]
56 enumFromThenBounded n1 n2
57 | i_n2 >= i_n1 = map toEnum [i_n1, i_n2 .. fromEnum (maxBound `asTypeOf` n1)]
58 | otherwise = map toEnum [i_n1, i_n2 .. fromEnum (minBound `asTypeOf` n1)]
65 %*********************************************************
69 %*********************************************************
72 instance Bounded () where
76 instance Enum () where
77 succ _ = error "Prelude.Enum.().succ: bad argment"
78 pred _ = error "Prelude.Enum.().pred: bad argument"
80 toEnum x | x == zeroInt = ()
81 | otherwise = error "Prelude.Enum.().toEnum: bad argument"
85 enumFromThen () () = [()]
86 enumFromTo () () = [()]
87 enumFromThenTo () () () = [()]
91 instance (Bounded a, Bounded b) => Bounded (a,b) where
92 minBound = (minBound, minBound)
93 maxBound = (maxBound, maxBound)
95 instance (Bounded a, Bounded b, Bounded c) => Bounded (a,b,c) where
96 minBound = (minBound, minBound, minBound)
97 maxBound = (maxBound, maxBound, maxBound)
99 instance (Bounded a, Bounded b, Bounded c, Bounded d) => Bounded (a,b,c,d) where
100 minBound = (minBound, minBound, minBound, minBound)
101 maxBound = (maxBound, maxBound, maxBound, maxBound)
105 %*********************************************************
107 \subsection{Type @Bool@}
109 %*********************************************************
112 instance Bounded Bool where
116 instance Enum Bool where
118 succ True = error "Prelude.Enum.Bool.succ: bad argment"
121 pred False = error "Prelude.Enum.Bool.pred: bad argment"
123 toEnum n | n == zeroInt = False
125 | otherwise = error "Prelude.Enum.Bool.toEnum: bad argment"
127 fromEnum False = zeroInt
128 fromEnum True = oneInt
130 -- Use defaults for the rest
131 enumFrom = enumFromBounded
132 enumFromThen = enumFromThenBounded
135 %*********************************************************
137 \subsection{Type @Ordering@}
139 %*********************************************************
142 instance Bounded Ordering where
146 instance Enum Ordering where
149 succ GT = error "Prelude.Enum.Ordering.succ: bad argment"
153 pred LT = error "Prelude.Enum.Ordering.pred: bad argment"
155 toEnum n | n == zeroInt = LT
158 toEnum _ = error "Prelude.Enum.Ordering.toEnum: bad argment"
160 fromEnum LT = zeroInt
164 -- Use defaults for the rest
165 enumFrom = enumFromBounded
166 enumFromThen = enumFromThenBounded
169 %*********************************************************
171 \subsection{Type @Char@}
173 %*********************************************************
176 instance Bounded Char where
180 instance Enum Char where
182 | not (ord# c# ==# 255#) = C# (chr# (ord# c# +# 1#))
183 | otherwise = error ("Prelude.Enum.Char.succ: bad argument")
185 | not (ord# c# ==# 0#) = C# (chr# (ord# c# -# 1#))
186 | otherwise = error ("Prelude.Enum.Char.pred: bad argument")
191 {-# INLINE enumFrom #-}
192 enumFrom (C# x) = build (\ c n -> eftCharFB c n (ord# x) 255#)
193 -- Blarg: technically I guess enumFrom isn't strict!
195 {-# INLINE enumFromTo #-}
196 enumFromTo (C# x) (C# y) = build (\ c n -> eftCharFB c n (ord# x) (ord# y))
198 {-# INLINE enumFromThen #-}
199 enumFromThen (C# x1) (C# x2) = build (\ c n -> efdCharFB c n (ord# x1) (ord# x2))
201 {-# INLINE enumFromThenTo #-}
202 enumFromThenTo (C# x1) (C# x2) (C# y) = build (\ c n -> efdtCharFB c n (ord# x1) (ord# x2) (ord# y))
204 -- We can do better than for Ints because we don't
205 -- have hassles about arithmetic overflow at maxBound
206 {-# INLINE eftCharFB #-}
207 eftCharFB c n x y = go x
210 | otherwise = C# (chr# x) `c` go (x +# 1#)
212 eftCharList x y | x ># y = []
213 | otherwise = C# (chr# x) : eftCharList (x +# 1#) y
216 -- For enumFromThenTo we give up on inlining
218 | delta >=# 0# = go_up_char_fb c n x1 delta 255#
219 | otherwise = go_dn_char_fb c n x1 delta 0#
224 | delta >=# 0# = go_up_char_list x1 delta 255#
225 | otherwise = go_dn_char_list x1 delta 0#
229 efdtCharFB c n x1 x2 lim
230 | delta >=# 0# = go_up_char_fb c n x1 delta lim
231 | otherwise = go_dn_char_fb c n x1 delta lim
235 efdtCharList x1 x2 lim
236 | delta >=# 0# = go_up_char_list x1 delta lim
237 | otherwise = go_dn_char_list x1 delta lim
241 go_up_char_fb c n x delta lim
244 go_up x | x ># lim = n
245 | otherwise = C# (chr# x) `c` go_up (x +# delta)
247 go_dn_char_fb c n x delta lim
250 go_dn x | x <# lim = n
251 | otherwise = C# (chr# x) `c` go_dn (x +# delta)
253 go_up_char_list x delta lim
256 go_up x | x ># lim = []
257 | otherwise = C# (chr# x) : go_up (x +# delta)
259 go_dn_char_list x delta lim
262 go_dn x | x <# lim = []
263 | otherwise = C# (chr# x) : go_dn (x +# delta)
267 "eftCharList" eftCharFB (:) [] = eftCharList
268 "efdCharList" efdCharFB (:) [] = efdCharList
269 "efdtCharList" efdtCharFB (:) [] = efdtCharList
274 %*********************************************************
276 \subsection{Type @Int@}
278 %*********************************************************
280 Be careful about these instances.
281 (a) remember that you have to count down as well as up e.g. [13,12..0]
282 (b) be careful of Int overflow
283 (c) remember that Int is bounded, so [1..] terminates at maxInt
285 Also NB that the Num class isn't available in this module.
288 instance Bounded Int where
292 instance Enum Int where
294 | x == maxBound = error "Prelude.Enum.succ{Int}: tried to take `succ' of maxBound"
295 | otherwise = x `plusInt` oneInt
297 | x == minBound = error "Prelude.Enum.pred{Int}: tried to take `pred' of minBound"
298 | otherwise = x `minusInt` oneInt
303 {-# INLINE enumFrom #-}
304 enumFrom (I# x) = build (\ c n -> eftIntFB c n x 2147483647#)
305 -- Blarg: technically I guess enumFrom isn't strict!
307 {-# INLINE enumFromTo #-}
308 enumFromTo (I# x) (I# y) = build (\ c n -> eftIntFB c n x y)
310 {-# INLINE enumFromThen #-}
311 enumFromThen (I# x1) (I# x2) = build (\ c n -> efdIntFB c n x1 x2)
313 {-# INLINE enumFromThenTo #-}
314 enumFromThenTo (I# x1) (I# x2) (I# y) = build (\ c n -> efdtIntFB c n x1 x2 y)
316 {-# INLINE eftIntFB #-}
317 eftIntFB c n x y | x ># y = n
320 go x = I# x `c` if x ==# y then n else go (x +# 1#)
321 -- Watch out for y=maxBound; hence ==, not >
322 -- Be very careful not to have more than one "c"
323 -- so that when eftInfFB is inlined we can inline
324 -- whatver is bound to "c"
326 eftIntList x y | x ># y = []
329 go x = I# x : if x ==# y then [] else go (x +# 1#)
332 -- For enumFromThenTo we give up on inlining; so we don't worry
333 -- about duplicating occurrences of "c"
334 efdtIntFB c n x1 x2 y
335 | delta >=# 0# = if x1 ># y then n else go_up_int_fb c n x1 delta lim
336 | otherwise = if x1 <# y then n else go_dn_int_fb c n x1 delta lim
342 | delta >=# 0# = if x1 ># y then [] else go_up_int_list x1 delta lim
343 | otherwise = if x1 <# y then [] else go_dn_int_list x1 delta lim
349 | delta >=# 0# = go_up_int_fb c n x1 delta ( 2147483647# -# delta)
350 | otherwise = go_dn_int_fb c n x1 delta ((-2147483648#) -# delta)
355 | delta >=# 0# = go_up_int_list x1 delta ( 2147483647# -# delta)
356 | otherwise = go_dn_int_list x1 delta ((-2147483648#) -# delta)
360 -- In all of these, the (x +# delta) is guaranteed not to overflow
362 go_up_int_fb c n x delta lim
365 go_up x | x ># lim = I# x `c` n
366 | otherwise = I# x `c` go_up (x +# delta)
368 go_dn_int_fb c n x delta lim
371 go_dn x | x <# lim = I# x `c` n
372 | otherwise = I# x `c` go_dn (x +# delta)
374 go_up_int_list x delta lim
377 go_up x | x ># lim = [I# x]
378 | otherwise = I# x : go_up (x +# delta)
380 go_dn_int_list x delta lim
383 go_dn x | x <# lim = [I# x]
384 | otherwise = I# x : go_dn (x +# delta)
388 "eftIntList" eftIntFB (:) [] = eftIntList
389 "efdIntList" efdIntFB (:) [] = efdIntList
390 "efdtIntList" efdtIntFB (:) [] = efdtIntList