Import GHC.Err so we see bottoming functions properly
[ghc-base.git] / GHC / Word.hs
index b60cc33..8d63e11 100644 (file)
@@ -41,6 +41,7 @@ import GHC.Real
 import GHC.Read
 import GHC.Arr
 import GHC.Show
+import GHC.Err
 
 ------------------------------------------------------------------------
 -- Helper functions
@@ -115,22 +116,22 @@ instance Enum Word where
     enumFromThenTo      = integralEnumFromThenTo
 
 instance Integral Word where
-    quot    x@(W# x#) y@(W# y#)
+    quot    (W# x#) y@(W# y#)
         | y /= 0                = W# (x# `quotWord#` y#)
         | otherwise             = divZeroError
-    rem     x@(W# x#) y@(W# y#)
+    rem     (W# x#) y@(W# y#)
         | y /= 0                = W# (x# `remWord#` y#)
         | otherwise             = divZeroError
-    div     x@(W# x#) y@(W# y#)
+    div     (W# x#) y@(W# y#)
         | y /= 0                = W# (x# `quotWord#` y#)
         | otherwise             = divZeroError
-    mod     x@(W# x#) y@(W# y#)
+    mod     (W# x#) y@(W# y#)
         | y /= 0                = W# (x# `remWord#` y#)
         | otherwise             = divZeroError
-    quotRem x@(W# x#) y@(W# y#)
+    quotRem (W# x#) y@(W# y#)
         | y /= 0                = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
         | otherwise             = divZeroError
-    divMod  x@(W# x#) y@(W# y#)
+    divMod  (W# x#) y@(W# y#)
         | y /= 0                = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
         | otherwise             = divZeroError
     toInteger (W# x#)
@@ -153,9 +154,9 @@ instance Bounded Word where
 #endif
 
 instance Ix Word where
-    range (m,n)              = [m..n]
-    unsafeIndex b@(m,_) i    = fromIntegral (i - m)
-    inRange (m,n) i          = m <= i && i <= n
+    range (m,n)         = [m..n]
+    unsafeIndex (m,_) i = fromIntegral (i - m)
+    inRange (m,n) i     = m <= i && i <= n
 
 instance Read Word where
     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
@@ -179,6 +180,10 @@ instance Bits Word where
     bitSize  _               = WORD_SIZE_IN_BITS
     isSigned _               = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 {-# RULES
 "fromIntegral/Int->Word"  fromIntegral = \(I# x#) -> W# (int2Word# x#)
 "fromIntegral/Word->Int"  fromIntegral = \(W# x#) -> I# (word2Int# x#)
@@ -227,22 +232,22 @@ instance Enum Word8 where
     enumFromThen        = boundedEnumFromThen
 
 instance Integral Word8 where
-    quot    x@(W8# x#) y@(W8# y#)
+    quot    (W8# x#) y@(W8# y#)
         | y /= 0                  = W8# (x# `quotWord#` y#)
         | otherwise               = divZeroError
-    rem     x@(W8# x#) y@(W8# y#)
+    rem     (W8# x#) y@(W8# y#)
         | y /= 0                  = W8# (x# `remWord#` y#)
         | otherwise               = divZeroError
-    div     x@(W8# x#) y@(W8# y#)
+    div     (W8# x#) y@(W8# y#)
         | y /= 0                  = W8# (x# `quotWord#` y#)
         | otherwise               = divZeroError
-    mod     x@(W8# x#) y@(W8# y#)
+    mod     (W8# x#) y@(W8# y#)
         | y /= 0                  = W8# (x# `remWord#` y#)
         | otherwise               = divZeroError
-    quotRem x@(W8# x#) y@(W8# y#)
+    quotRem (W8# x#) y@(W8# y#)
         | y /= 0                  = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
         | otherwise               = divZeroError
-    divMod  x@(W8# x#) y@(W8# y#)
+    divMod  (W8# x#) y@(W8# y#)
         | y /= 0                  = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
         | otherwise               = divZeroError
     toInteger (W8# x#)            = smallInteger (word2Int# x#)
@@ -252,9 +257,9 @@ instance Bounded Word8 where
     maxBound = 0xFF
 
 instance Ix Word8 where
-    range (m,n)              = [m..n]
-    unsafeIndex b@(m,_) i    = fromIntegral (i - m)
-    inRange (m,n) i          = m <= i && i <= n
+    range (m,n)         = [m..n]
+    unsafeIndex (m,_) i = fromIntegral (i - m)
+    inRange (m,n) i     = m <= i && i <= n
 
 instance Read Word8 where
     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
@@ -278,6 +283,10 @@ instance Bits Word8 where
     bitSize  _                = 8
     isSigned _                = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 {-# RULES
 "fromIntegral/Word8->Word8"   fromIntegral = id :: Word8 -> Word8
 "fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
@@ -327,22 +336,22 @@ instance Enum Word16 where
     enumFromThen        = boundedEnumFromThen
 
 instance Integral Word16 where
-    quot    x@(W16# x#) y@(W16# y#)
+    quot    (W16# x#) y@(W16# y#)
         | y /= 0                    = W16# (x# `quotWord#` y#)
         | otherwise                 = divZeroError
-    rem     x@(W16# x#) y@(W16# y#)
+    rem     (W16# x#) y@(W16# y#)
         | y /= 0                    = W16# (x# `remWord#` y#)
         | otherwise                 = divZeroError
-    div     x@(W16# x#) y@(W16# y#)
+    div     (W16# x#) y@(W16# y#)
         | y /= 0                    = W16# (x# `quotWord#` y#)
         | otherwise                 = divZeroError
-    mod     x@(W16# x#) y@(W16# y#)
+    mod     (W16# x#) y@(W16# y#)
         | y /= 0                    = W16# (x# `remWord#` y#)
         | otherwise                 = divZeroError
-    quotRem x@(W16# x#) y@(W16# y#)
+    quotRem (W16# x#) y@(W16# y#)
         | y /= 0                    = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
         | otherwise                 = divZeroError
-    divMod  x@(W16# x#) y@(W16# y#)
+    divMod  (W16# x#) y@(W16# y#)
         | y /= 0                    = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
         | otherwise                 = divZeroError
     toInteger (W16# x#)             = smallInteger (word2Int# x#)
@@ -352,9 +361,9 @@ instance Bounded Word16 where
     maxBound = 0xFFFF
 
 instance Ix Word16 where
-    range (m,n)              = [m..n]
-    unsafeIndex b@(m,_) i    = fromIntegral (i - m)
-    inRange (m,n) i          = m <= i && i <= n
+    range (m,n)         = [m..n]
+    unsafeIndex (m,_) i = fromIntegral (i - m)
+    inRange (m,n) i     = m <= i && i <= n
 
 instance Read Word16 where
     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
@@ -378,6 +387,10 @@ instance Bits Word16 where
     bitSize  _                = 16
     isSigned _                = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 {-# RULES
 "fromIntegral/Word8->Word16"   fromIntegral = \(W8# x#) -> W16# x#
 "fromIntegral/Word16->Word16"  fromIntegral = id :: Word16 -> Word16
@@ -477,6 +490,10 @@ instance Bits Word32 where
     bitSize  _                = 32
     isSigned _                = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 {-# RULES
 "fromIntegral/Int->Word32"    fromIntegral = \(I#   x#) -> W32# (int32ToWord32# (intToInt32# x#))
 "fromIntegral/Word->Word32"   fromIntegral = \(W#   x#) -> W32# (wordToWord32# x#)
@@ -536,22 +553,22 @@ instance Enum Word32 where
 #endif
 
 instance Integral Word32 where
-    quot    x@(W32# x#) y@(W32# y#)
+    quot    (W32# x#) y@(W32# y#)
         | y /= 0                    = W32# (x# `quotWord#` y#)
         | otherwise                 = divZeroError
-    rem     x@(W32# x#) y@(W32# y#)
+    rem     (W32# x#) y@(W32# y#)
         | y /= 0                    = W32# (x# `remWord#` y#)
         | otherwise                 = divZeroError
-    div     x@(W32# x#) y@(W32# y#)
+    div     (W32# x#) y@(W32# y#)
         | y /= 0                    = W32# (x# `quotWord#` y#)
         | otherwise                 = divZeroError
-    mod     x@(W32# x#) y@(W32# y#)
+    mod     (W32# x#) y@(W32# y#)
         | y /= 0                    = W32# (x# `remWord#` y#)
         | otherwise                 = divZeroError
-    quotRem x@(W32# x#) y@(W32# y#)
+    quotRem (W32# x#) y@(W32# y#)
         | y /= 0                    = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
         | otherwise                 = divZeroError
-    divMod  x@(W32# x#) y@(W32# y#)
+    divMod  (W32# x#) y@(W32# y#)
         | y /= 0                    = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
         | otherwise                 = divZeroError
     toInteger (W32# x#)
@@ -583,6 +600,10 @@ instance Bits Word32 where
     bitSize  _                = 32
     isSigned _                = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 {-# RULES
 "fromIntegral/Word8->Word32"   fromIntegral = \(W8# x#) -> W32# x#
 "fromIntegral/Word16->Word32"  fromIntegral = \(W16# x#) -> W32# x#
@@ -610,9 +631,9 @@ instance Bounded Word32 where
     maxBound = 0xFFFFFFFF
 
 instance Ix Word32 where
-    range (m,n)              = [m..n]
-    unsafeIndex b@(m,_) i    = fromIntegral (i - m)
-    inRange (m,n) i          = m <= i && i <= n
+    range (m,n)         = [m..n]
+    unsafeIndex (m,_) i = fromIntegral (i - m)
+    inRange (m,n) i     = m <= i && i <= n
 
 instance Read Word32 where  
 #if WORD_SIZE_IN_BITS < 33
@@ -670,22 +691,22 @@ instance Enum Word64 where
     enumFromThenTo      = integralEnumFromThenTo
 
 instance Integral Word64 where
-    quot    x@(W64# x#) y@(W64# y#)
+    quot    (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `quotWord64#` y#)
         | otherwise                 = divZeroError
-    rem     x@(W64# x#) y@(W64# y#)
+    rem     (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `remWord64#` y#)
         | otherwise                 = divZeroError
-    div     x@(W64# x#) y@(W64# y#)
+    div     (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `quotWord64#` y#)
         | otherwise                 = divZeroError
-    mod     x@(W64# x#) y@(W64# y#)
+    mod     (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `remWord64#` y#)
         | otherwise                 = divZeroError
-    quotRem x@(W64# x#) y@(W64# y#)
+    quotRem (W64# x#) y@(W64# y#)
         | y /= 0                    = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
         | otherwise                 = divZeroError
-    divMod  x@(W64# x#) y@(W64# y#)
+    divMod  (W64# x#) y@(W64# y#)
         | y /= 0                    = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
         | otherwise                 = divZeroError
     toInteger (W64# x#)             = word64ToInteger x#
@@ -709,6 +730,10 @@ instance Bits Word64 where
     bitSize  _                = 64
     isSigned _                = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 -- give the 64-bit shift operations the same treatment as the 32-bit
 -- ones (see GHC.Base), namely we wrap them in tests to catch the
 -- cases when we're shifting more than 64 bits to avoid unspecified
@@ -769,22 +794,22 @@ instance Enum Word64 where
     enumFromThenTo      = integralEnumFromThenTo
 
 instance Integral Word64 where
-    quot    x@(W64# x#) y@(W64# y#)
+    quot    (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `quotWord#` y#)
         | otherwise                 = divZeroError
-    rem     x@(W64# x#) y@(W64# y#)
+    rem     (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `remWord#` y#)
         | otherwise                 = divZeroError
-    div     x@(W64# x#) y@(W64# y#)
+    div     (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `quotWord#` y#)
         | otherwise                 = divZeroError
-    mod     x@(W64# x#) y@(W64# y#)
+    mod     (W64# x#) y@(W64# y#)
         | y /= 0                    = W64# (x# `remWord#` y#)
         | otherwise                 = divZeroError
-    quotRem x@(W64# x#) y@(W64# y#)
+    quotRem (W64# x#) y@(W64# y#)
         | y /= 0                    = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
         | otherwise                 = divZeroError
-    divMod  x@(W64# x#) y@(W64# y#)
+    divMod  (W64# x#) y@(W64# y#)
         | y /= 0                    = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
         | otherwise                 = divZeroError
     toInteger (W64# x#)
@@ -812,12 +837,19 @@ instance Bits Word64 where
     bitSize  _                = 64
     isSigned _                = False
 
+    {-# INLINE shiftR #-}
+    -- same as the default definition, but we want it inlined (#2376)
+    x `shiftR`  i = x `shift`  (-i)
+
 {-# RULES
 "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#
 "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#)
   #-}
 
+uncheckedShiftL64# :: Word# -> Int# -> Word#
 uncheckedShiftL64#  = uncheckedShiftL#
+
+uncheckedShiftRL64# :: Word# -> Int# -> Word#
 uncheckedShiftRL64# = uncheckedShiftRL#
 
 #endif
@@ -833,9 +865,9 @@ instance Bounded Word64 where
     maxBound = 0xFFFFFFFFFFFFFFFF
 
 instance Ix Word64 where
-    range (m,n)              = [m..n]
-    unsafeIndex b@(m,_) i    = fromIntegral (i - m)
-    inRange (m,n) i          = m <= i && i <= n
+    range (m,n)         = [m..n]
+    unsafeIndex (m,_) i = fromIntegral (i - m)
+    inRange (m,n) i     = m <= i && i <= n
 
 instance Read Word64 where
     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]