summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Peyton Jones <simonpj@microsoft.com>2014-05-12 10:54:30 +0100
committerSimon Peyton Jones <simonpj@microsoft.com>2014-08-28 11:14:10 +0100
commit343633307f5a24c741b80bbbc952919d9947f56c (patch)
treee0b7c5416a9d3bdc45229821d8664747870c2783
parent4c03791f986509c5d95adf50de555876ed05522e (diff)
downloadhaskell-343633307f5a24c741b80bbbc952919d9947f56c.tar.gz
Move the Enum Word instance into GHC.Enum
This just avoids an unnecessary orphan instance. All the other instances for "earlier" types are in GHC.Enum already.
-rw-r--r--libraries/base/GHC/Enum.lhs35
-rw-r--r--libraries/base/GHC/Real.lhs25
2 files changed, 36 insertions, 24 deletions
diff --git a/libraries/base/GHC/Enum.lhs b/libraries/base/GHC/Enum.lhs
index d94e2ec54b..a6dae7a2f5 100644
--- a/libraries/base/GHC/Enum.lhs
+++ b/libraries/base/GHC/Enum.lhs
@@ -650,6 +650,41 @@ instance Bounded Word where
#else
#error Unhandled value for WORD_SIZE_IN_BITS
#endif
+
+instance Enum Word where
+ succ x
+ | x /= maxBound = x + 1
+ | otherwise = succError "Word"
+ pred x
+ | x /= minBound = x - 1
+ | otherwise = predError "Word"
+ toEnum i@(I# i#)
+ | i >= 0 = W# (int2Word# i#)
+ | otherwise = toEnumError "Word" i (minBound::Word, maxBound::Word)
+ fromEnum x@(W# x#)
+ | x <= maxIntWord = I# (word2Int# x#)
+ | otherwise = fromEnumError "Word" x
+
+ enumFrom n = map integerToWordX [wordToIntegerX n .. wordToIntegerX (maxBound :: Word)]
+ enumFromTo n1 n2 = map integerToWordX [wordToIntegerX n1 .. wordToIntegerX n2]
+ enumFromThenTo n1 n2 m = map integerToWordX [wordToIntegerX n1, wordToIntegerX n2 .. wordToIntegerX m]
+ enumFromThen n1 n2 = map integerToWordX [wordToIntegerX n1, wordToIntegerX n2 .. wordToIntegerX limit]
+ where
+ limit :: Word
+ limit | n2 >= n1 = maxBound
+ | otherwise = minBound
+
+maxIntWord :: Word
+-- The biggest word representable as an Int
+maxIntWord = W# (case maxInt of I# i -> int2Word# i)
+
+-- For some reason integerToWord and wordToInteger (GHC.Integer.Type)
+-- work over Word#
+integerToWordX :: Integer -> Word
+integerToWordX i = W# (integerToWord i)
+
+wordToIntegerX :: Word -> Integer
+wordToIntegerX (W# x#) = wordToInteger x#
\end{code}
diff --git a/libraries/base/GHC/Real.lhs b/libraries/base/GHC/Real.lhs
index d70dd819d9..a54818f049 100644
--- a/libraries/base/GHC/Real.lhs
+++ b/libraries/base/GHC/Real.lhs
@@ -345,30 +345,7 @@ instance Integral Word where
divMod (W# x#) y@(W# y#)
| y /= 0 = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
| otherwise = divZeroError
- toInteger (W# x#)
- | isTrue# (i# >=# 0#) = smallInteger i#
- | otherwise = wordToInteger x#
- where
- !i# = word2Int# x#
-
-instance Enum Word where
- succ x
- | x /= maxBound = x + 1
- | otherwise = succError "Word"
- pred x
- | x /= minBound = x - 1
- | otherwise = predError "Word"
- toEnum i@(I# i#)
- | i >= 0 = W# (int2Word# i#)
- | otherwise = toEnumError "Word" i (minBound::Word, maxBound::Word)
- fromEnum x@(W# x#)
- | x <= fromIntegral (maxBound::Int)
- = I# (word2Int# x#)
- | otherwise = fromEnumError "Word" x
- enumFrom = integralEnumFrom
- enumFromThen = integralEnumFromThen
- enumFromTo = integralEnumFromTo
- enumFromThenTo = integralEnumFromThenTo
+ toInteger (W# x#) = wordToInteger x#
\end{code}