From 343633307f5a24c741b80bbbc952919d9947f56c Mon Sep 17 00:00:00 2001 From: Simon Peyton Jones Date: Mon, 12 May 2014 10:54:30 +0100 Subject: 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. --- libraries/base/GHC/Enum.lhs | 35 +++++++++++++++++++++++++++++++++++ libraries/base/GHC/Real.lhs | 25 +------------------------ 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} -- cgit v1.2.1