X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Flib%2Fstd%2FPrelEnum.lhs;h=0104e46f9b1887fbfd98785b23e288398280b7f9;hb=8bd82b8898218c6285c4b23cb37c13a220cc5d2b;hp=2ace283077a85ba6ae82175b147e29e8fcd2cdc5;hpb=835564a79b091f7e17d794425655ec2aa7ca6ca7;p=ghc-hetmet.git diff --git a/ghc/lib/std/PrelEnum.lhs b/ghc/lib/std/PrelEnum.lhs index 2ace283..0104e46 100644 --- a/ghc/lib/std/PrelEnum.lhs +++ b/ghc/lib/std/PrelEnum.lhs @@ -1,6 +1,9 @@ +% ----------------------------------------------------------------------------- +% $Id: PrelEnum.lhs,v 1.13 2001/02/18 14:45:15 qrczak Exp $ % -% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996 +% (c) The University of Glasgow, 1992-2000 % + \section[PrelBounded]{Module @PrelBounded@} Instances of Bounded for various datatypes. @@ -10,7 +13,7 @@ Instances of Bounded for various datatypes. module PrelEnum( Bounded(..), Enum(..), - enumFromBounded, enumFromThenBounded, + boundedEnumFrom, boundedEnumFromThen, -- Instances for Bounded and Eum: (), Char, Int @@ -19,6 +22,8 @@ module PrelEnum( 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} @@ -49,11 +54,11 @@ class Enum a where 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)] +boundedEnumFrom :: (Enum a, Bounded a) => a -> [a] +boundedEnumFrom n = map toEnum [fromEnum n .. fromEnum (maxBound `asTypeOf` n)] -enumFromThenBounded :: (Enum a, Bounded a) => a -> a -> [a] -enumFromThenBounded n1 n2 +boundedEnumFromThen :: (Enum a, Bounded a) => a -> a -> [a] +boundedEnumFromThen 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 @@ -128,8 +133,8 @@ instance Enum Bool where fromEnum True = oneInt -- Use defaults for the rest - enumFrom = enumFromBounded - enumFromThen = enumFromThenBounded + enumFrom = boundedEnumFrom + enumFromThen = boundedEnumFromThen \end{code} %********************************************************* @@ -162,8 +167,8 @@ instance Enum Ordering where fromEnum GT = twoInt -- Use defaults for the rest - enumFrom = enumFromBounded - enumFromThen = enumFromThenBounded + enumFrom = boundedEnumFrom + enumFromThen = boundedEnumFromThen \end{code} %********************************************************* @@ -175,11 +180,11 @@ instance Enum Ordering where \begin{code} instance Bounded Char where minBound = '\0' - maxBound = '\255' + maxBound = '\x10FFFF' instance Enum Char where succ (C# c#) - | not (ord# c# ==# 255#) = C# (chr# (ord# c# +# 1#)) + | not (ord# c# ==# 0x10FFFF#) = 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#)) @@ -189,17 +194,32 @@ instance Enum Char where fromEnum = ord {-# INLINE enumFrom #-} - enumFrom (C# x) = build (\ c n -> eftCharFB c n (ord# x) 255#) + enumFrom (C# x) = eftChar (ord# x) 0x10FFFF# -- 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)) - + enumFromTo (C# x) (C# y) = eftChar (ord# x) (ord# y) + {-# INLINE enumFromThen #-} - enumFromThen (C# x1) (C# x2) = build (\ c n -> efdCharFB c n (ord# x1) (ord# x2)) - + enumFromThen (C# x1) (C# x2) = efdChar (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)) + enumFromThenTo (C# x1) (C# x2) (C# y) = efdtChar (ord# x1) (ord# x2) (ord# y) + +eftChar = eftCharList +efdChar = efdCharList +efdtChar = efdtCharList + + +{-# RULES +"eftChar" forall x y. eftChar x y = build (\c n -> eftCharFB c n x y) +"efdChar" forall x1 x2. efdChar x1 x2 = build (\ c n -> efdCharFB c n x1 x2) +"efdtChar" forall x1 x2 l. efdtChar x1 x2 l = build (\ c n -> efdtCharFB c n x1 x2 l) +"eftCharList" eftCharFB (:) [] = eftCharList +"efdCharList" efdCharFB (:) [] = efdCharList +"efdtCharList" efdtCharFB (:) [] = efdtCharList + #-} + -- We can do better than for Ints because we don't -- have hassles about arithmetic overflow at maxBound @@ -215,13 +235,13 @@ eftCharList x y | x ># y = [] -- For enumFromThenTo we give up on inlining efdCharFB c n x1 x2 - | delta >=# 0# = go_up_char_fb c n x1 delta 255# + | delta >=# 0# = go_up_char_fb c n x1 delta 0x10FFFF# | 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# + | delta >=# 0# = go_up_char_list x1 delta 0x10FFFF# | otherwise = go_dn_char_list x1 delta 0# where delta = x2 -# x1 @@ -261,13 +281,6 @@ go_dn_char_list x delta lim 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} @@ -301,17 +314,32 @@ instance Enum Int where fromEnum x = x {-# INLINE enumFrom #-} - enumFrom (I# x) = build (\ c n -> eftIntFB c n x 2147483647#) + enumFrom (I# x) = eftInt 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) + enumFromTo (I# x) (I# y) = eftInt x y {-# INLINE enumFromThen #-} - enumFromThen (I# x1) (I# x2) = build (\ c n -> efdIntFB c n x1 x2) + enumFromThen (I# x1) (I# x2) = efdInt x1 x2 {-# INLINE enumFromThenTo #-} - enumFromThenTo (I# x1) (I# x2) (I# y) = build (\ c n -> efdtIntFB c n x1 x2 y) + enumFromThenTo (I# x1) (I# x2) (I# y) = efdtInt x1 x2 y + +eftInt = eftIntList +efdInt = efdIntList +efdtInt = efdtIntList + +{-# RULES +"eftInt" forall x y. eftInt x y = build (\ c n -> eftIntFB c n x y) +"efdInt" forall x1 x2. efdInt x1 x2 = build (\ c n -> efdIntFB c n x1 x2) +"efdtInt" forall x1 x2 l. efdtInt x1 x2 l = build (\ c n -> efdtIntFB c n x1 x2 l) + +"eftIntList" eftIntFB (:) [] = eftIntList +"efdIntList" efdIntFB (:) [] = efdIntList +"efdtIntList" efdtIntFB (:) [] = efdtIntList + #-} + {-# INLINE eftIntFB #-} eftIntFB c n x y | x ># y = n @@ -382,12 +410,5 @@ go_dn_int_list x delta lim 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}