[project @ 2001-08-04 06:11:24 by ken]
[ghc-hetmet.git] / ghc / lib / std / PrelWord.lhs
index 4cac193..0a8bc1d 100644 (file)
@@ -4,6 +4,8 @@
 \section[PrelWord]{Module @PrelWord@}
 
 \begin{code}
+{-# OPTIONS -fno-implicit-prelude #-}
+
 #include "MachDeps.h"
 
 module PrelWord (
@@ -18,6 +20,7 @@ import PrelReal
 import PrelRead
 import PrelArr
 import PrelBits
+import PrelShow
 
 ------------------------------------------------------------------------
 -- Helper functions
@@ -98,6 +101,8 @@ instance Enum Word where
         | otherwise     = fromEnumError "Word" x
     enumFrom            = integralEnumFrom
     enumFromThen        = integralEnumFromThen
+    enumFromTo          = integralEnumFromTo
+    enumFromThenTo      = integralEnumFromThenTo
 
 instance Integral Word where
     quot    x@(W# x#) y@(W# y#)
@@ -166,7 +171,7 @@ instance Bits Word where
 "fromIntegral/Int->Word"  fromIntegral = \(I# x#) -> W# (int2Word# x#)
 "fromIntegral/Word->Int"  fromIntegral = \(W# x#) -> I# (word2Int# x#)
 "fromIntegral/Word->Word" fromIntegral = id :: Word -> Word
-    #-}
+  #-}
 
 ------------------------------------------------------------------------
 -- type Word8
@@ -263,9 +268,11 @@ instance Bits Word8 where
     isSigned _                = False
 
 {-# RULES
-"fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (wordToWord8# x#)
-"fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#)
-    #-}
+"fromIntegral/Word8->Word8"   fromIntegral = id :: Word8 -> Word8
+"fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
+"fromIntegral/a->Word8"       fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (wordToWord8# x#)
+"fromIntegral/Word8->a"       fromIntegral = \(W8# x#) -> fromIntegral (W# x#)
+  #-}
 
 ------------------------------------------------------------------------
 -- type Word16
@@ -362,9 +369,12 @@ instance Bits Word16 where
     isSigned _                = False
 
 {-# RULES
-"fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (wordToWord16# x#)
-"fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#)
-    #-}
+"fromIntegral/Word8->Word16"   fromIntegral = \(W8# x#) -> W16# x#
+"fromIntegral/Word16->Word16"  fromIntegral = id :: Word16 -> Word16
+"fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer
+"fromIntegral/a->Word16"       fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (wordToWord16# x#)
+"fromIntegral/Word16->a"       fromIntegral = \(W16# x#) -> fromIntegral (W# x#)
+  #-}
 
 ------------------------------------------------------------------------
 -- type Word32
@@ -378,10 +388,6 @@ instance Bits Word16 where
 
 data Word32 = W32# Word# deriving (Eq, Ord)
 
-#if WORD_SIZE_IN_BYTES == 4
-{-# RULES "wordToWord32#" forall x#. wordToWord32# x# = x# #-}
-#endif
-
 instance CCallable Word32
 instance CReturnable Word32
 
@@ -421,14 +427,16 @@ instance Enum Word32 where
                         = W32# (int2Word# i#)
         | otherwise     = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
 #if WORD_SIZE_IN_BYTES == 4
-    fromEnum (W32# x#)  = I# (word2Int# x#)
-    enumFrom            = integralEnumFrom
-    enumFromThen        = integralEnumFromThen
-#else
     fromEnum x@(W32# x#)
         | x <= fromIntegral (maxBound::Int)
                         = I# (word2Int# x#)
         | otherwise     = fromEnumError "Word32" x
+    enumFrom            = integralEnumFrom
+    enumFromThen        = integralEnumFromThen
+    enumFromTo          = integralEnumFromTo
+    enumFromThenTo      = integralEnumFromThenTo
+#else
+    fromEnum (W32# x#)  = I# (word2Int# x#)
     enumFrom            = boundedEnumFrom
     enumFromThen        = boundedEnumFromThen
 #endif
@@ -496,9 +504,13 @@ instance Bits Word32 where
     isSigned _                = False
 
 {-# RULES
-"fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (wordToWord32# x#)
-"fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
-    #-}
+"fromIntegral/Word8->Word32"   fromIntegral = \(W8# x#) -> W32# x#
+"fromIntegral/Word16->Word32"  fromIntegral = \(W16# x#) -> W32# x#
+"fromIntegral/Word32->Word32"  fromIntegral = id :: Word32 -> Word32
+"fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer
+"fromIntegral/a->Word32"       fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (wordToWord32# x#)
+"fromIntegral/Word32->a"       fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
+  #-}
 
 ------------------------------------------------------------------------
 -- type Word64
@@ -545,6 +557,8 @@ instance Enum Word64 where
         | otherwise     = fromEnumError "Word64" x
     enumFrom            = integralEnumFrom
     enumFromThen        = integralEnumFromThen
+    enumFromTo          = integralEnumFromTo
+    enumFromThenTo      = integralEnumFromThenTo
 
 instance Integral Word64 where
     quot    x@(W64# x#) y@(W64# y#)
@@ -614,11 +628,11 @@ foreign import "stg_shiftRL64"     unsafe shiftRL64#     :: Word64# -> Int# -> W
 "fromIntegral/Word64->Int"    fromIntegral = \(W64# x#) -> I#   (word2Int# (word64ToWord# x#))
 "fromIntegral/Word64->Word"   fromIntegral = \(W64# x#) -> W#   (word64ToWord# x#)
 "fromIntegral/Word64->Word64" fromIntegral = id :: Word64 -> Word64
-    #-}
+  #-}
 
 #else
 
-data Word32 = W64# Word# deriving (Eq, Ord)
+data Word64 = W64# Word# deriving (Eq, Ord)
 
 instance Num Word64 where
     (W64# x#) + (W64# y#)  = W64# (x# `plusWord#` y#)
@@ -647,6 +661,8 @@ instance Enum Word64 where
         | otherwise     = fromEnumError "Word64" x
     enumFrom            = integralEnumFrom
     enumFromThen        = integralEnumFromThen
+    enumFromTo          = integralEnumFromTo
+    enumFromThenTo      = integralEnumFromThenTo
 
 instance Integral Word64 where
     quot    x@(W64# x#) y@(W64# y#)
@@ -691,7 +707,7 @@ instance Bits Word64 where
 {-# RULES
 "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#
 "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#)
-    #-}
+  #-}
 
 #endif