X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FInt.hs;h=146bf66e48b75ee7643f831bb4c288b4d7805249;hb=e95046ebf2e4833b56c6ddaf5b3108390cee08a5;hp=ae49806e8683d8b499086c188a473692d61277f8;hpb=95087a4ef4877a0860331cac2eafab481158bd60;p=ghc-base.git diff --git a/GHC/Int.hs b/GHC/Int.hs index ae49806..146bf66 100644 --- a/GHC/Int.hs +++ b/GHC/Int.hs @@ -1,4 +1,5 @@ -{-# OPTIONS_GHC -XNoImplicitPrelude #-} +{-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash, + StandaloneDeriving #-} {-# OPTIONS_HADDOCK hide #-} ----------------------------------------------------------------------------- -- | @@ -40,6 +41,9 @@ import GHC.Arr import GHC.Err import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#) import GHC.Show +import GHC.Float () -- for RealFrac methods +-- For defining instances for the new generic deriving mechanism +--import GHC.Generics (Arity(..), Associativity(..), Fixity(..)) ------------------------------------------------------------------------ -- type Int8 @@ -87,28 +91,28 @@ instance Enum Int8 where instance Integral Int8 where quot x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I8# (narrow8Int# (x# `quotInt#` y#)) rem x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I8# (narrow8Int# (x# `remInt#` y#)) div x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I8# (narrow8Int# (x# `divInt#` y#)) mod x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I8# (narrow8Int# (x# `modInt#` y#)) quotRem x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I8# (narrow8Int# (x# `quotInt#` y#)), I8# (narrow8Int# (x# `remInt#` y#))) divMod x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I8# (narrow8Int# (x# `divInt#` y#)), I8# (narrow8Int# (x# `modInt#` y#))) toInteger (I8# x#) = smallInteger x# @@ -142,21 +146,47 @@ instance Bits Int8 where = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (8# -# i'#))))) where - x'# = narrow8Word# (int2Word# x#) - i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) + !x'# = narrow8Word# (int2Word# x#) + !i'# = word2Int# (int2Word# i# `and#` int2Word# 7#) bitSize _ = 8 isSigned _ = True - {-# INLINE shiftR #-} - -- same as the default definition, but we want it inlined (#2376) - x `shiftR` i = x `shift` (-i) - {-# RULES "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8 "fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#) "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#) #-} +{-# RULES +"properFraction/Float->(Int8,Float)" + forall x. properFraction (x :: Float) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int8) n, y) } +"truncate/Float->Int8" + forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int8) (truncate x) +"floor/Float->Int8" + forall x. floor (x :: Float) = (fromIntegral :: Int -> Int8) (floor x) +"ceiling/Float->Int8" + forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int8) (ceiling x) +"round/Float->Int8" + forall x. round (x :: Float) = (fromIntegral :: Int -> Int8) (round x) + #-} + +{-# RULES +"properFraction/Double->(Int8,Double)" + forall x. properFraction (x :: Double) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int8) n, y) } +"truncate/Double->Int8" + forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int8) (truncate x) +"floor/Double->Int8" + forall x. floor (x :: Double) = (fromIntegral :: Int -> Int8) (floor x) +"ceiling/Double->Int8" + forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int8) (ceiling x) +"round/Double->Int8" + forall x. round (x :: Double) = (fromIntegral :: Int -> Int8) (round x) + #-} + ------------------------------------------------------------------------ -- type Int16 ------------------------------------------------------------------------ @@ -203,28 +233,28 @@ instance Enum Int16 where instance Integral Int16 where quot x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I16# (narrow16Int# (x# `quotInt#` y#)) rem x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I16# (narrow16Int# (x# `remInt#` y#)) div x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I16# (narrow16Int# (x# `divInt#` y#)) mod x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I16# (narrow16Int# (x# `modInt#` y#)) quotRem x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I16# (narrow16Int# (x# `quotInt#` y#)), I16# (narrow16Int# (x# `remInt#` y#))) divMod x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I16# (narrow16Int# (x# `divInt#` y#)), I16# (narrow16Int# (x# `modInt#` y#))) toInteger (I16# x#) = smallInteger x# @@ -258,14 +288,11 @@ instance Bits Int16 where = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (16# -# i'#))))) where - x'# = narrow16Word# (int2Word# x#) - i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) + !x'# = narrow16Word# (int2Word# x#) + !i'# = word2Int# (int2Word# i# `and#` int2Word# 15#) bitSize _ = 16 isSigned _ = True - {-# INLINE shiftR #-} - -- same as the default definition, but we want it inlined (#2376) - x `shiftR` i = x `shift` (-i) {-# RULES "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#) @@ -275,6 +302,36 @@ instance Bits Int16 where "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#) #-} +{-# RULES +"properFraction/Float->(Int16,Float)" + forall x. properFraction (x :: Float) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int16) n, y) } +"truncate/Float->Int16" + forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int16) (truncate x) +"floor/Float->Int16" + forall x. floor (x :: Float) = (fromIntegral :: Int -> Int16) (floor x) +"ceiling/Float->Int16" + forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int16) (ceiling x) +"round/Float->Int16" + forall x. round (x :: Float) = (fromIntegral :: Int -> Int16) (round x) + #-} + +{-# RULES +"properFraction/Double->(Int16,Double)" + forall x. properFraction (x :: Double) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int16) n, y) } +"truncate/Double->Int16" + forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int16) (truncate x) +"floor/Double->Int16" + forall x. floor (x :: Double) = (fromIntegral :: Int -> Int16) (floor x) +"ceiling/Double->Int16" + forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int16) (ceiling x) +"round/Double->Int16" + forall x. round (x :: Double) = (fromIntegral :: Int -> Int16) (round x) + #-} + ------------------------------------------------------------------------ -- type Int32 ------------------------------------------------------------------------ @@ -330,28 +387,28 @@ instance Enum Int32 where instance Integral Int32 where quot x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (x# `quotInt32#` y#) rem x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (x# `remInt32#` y#) div x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (x# `divInt32#` y#) mod x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (x# `modInt32#` y#) quotRem x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I32# (x# `quotInt32#` y#), I32# (x# `remInt32#` y#)) divMod x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I32# (x# `divInt32#` y#), I32# (x# `modInt32#` y#)) toInteger x@(I32# x#) @@ -399,9 +456,6 @@ instance Bits Int32 where bitSize _ = 32 isSigned _ = True - {-# INLINE shiftR #-} - -- same as the default definition, but we want it inlined (#2376) - x `shiftR` i = x `shift` (-i) {-# RULES "fromIntegral/Int->Int32" fromIntegral = \(I# x#) -> I32# (intToInt32# x#) @@ -413,7 +467,8 @@ instance Bits Int32 where "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32 #-} -#else +-- No rules for RealFrac methods if Int32 is larger than Int +#else -- Int32 is represented in the same way as Int. #if WORD_SIZE_IN_BITS > 32 @@ -461,28 +516,28 @@ instance Enum Int32 where instance Integral Int32 where quot x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (narrow32Int# (x# `quotInt#` y#)) rem x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (narrow32Int# (x# `remInt#` y#)) div x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (narrow32Int# (x# `divInt#` y#)) mod x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I32# (narrow32Int# (x# `modInt#` y#)) quotRem x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I32# (narrow32Int# (x# `quotInt#` y#)), I32# (narrow32Int# (x# `remInt#` y#))) divMod x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I32# (narrow32Int# (x# `divInt#` y#)), I32# (narrow32Int# (x# `modInt#` y#))) toInteger (I32# x#) = smallInteger x# @@ -507,15 +562,11 @@ instance Bits Int32 where = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (32# -# i'#))))) where - x'# = narrow32Word# (int2Word# x#) - i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) + !x'# = narrow32Word# (int2Word# x#) + !i'# = word2Int# (int2Word# i# `and#` int2Word# 31#) bitSize _ = 32 isSigned _ = True - {-# INLINE shiftR #-} - -- same as the default definition, but we want it inlined (#2376) - x `shiftR` i = x `shift` (-i) - {-# RULES "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#) "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#) @@ -526,7 +577,37 @@ instance Bits Int32 where "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#) #-} -#endif +{-# RULES +"properFraction/Float->(Int32,Float)" + forall x. properFraction (x :: Float) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int32) n, y) } +"truncate/Float->Int32" + forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int32) (truncate x) +"floor/Float->Int32" + forall x. floor (x :: Float) = (fromIntegral :: Int -> Int32) (floor x) +"ceiling/Float->Int32" + forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int32) (ceiling x) +"round/Float->Int32" + forall x. round (x :: Float) = (fromIntegral :: Int -> Int32) (round x) + #-} + +{-# RULES +"properFraction/Double->(Int32,Double)" + forall x. properFraction (x :: Double) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int32) n, y) } +"truncate/Double->Int32" + forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int32) (truncate x) +"floor/Double->Int32" + forall x. floor (x :: Double) = (fromIntegral :: Int -> Int32) (floor x) +"ceiling/Double->Int32" + forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int32) (ceiling x) +"round/Double->Int32" + forall x. round (x :: Double) = (fromIntegral :: Int -> Int32) (round x) + #-} + +#endif instance Real Int32 where toRational x = toInteger x % 1 @@ -594,28 +675,28 @@ instance Enum Int64 where instance Integral Int64 where quot x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `quotInt64#` y#) rem x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `remInt64#` y#) div x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `divInt64#` y#) mod x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `modInt64#` y#) quotRem x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I64# (x# `quotInt64#` y#), I64# (x# `remInt64#` y#)) divMod x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#)) toInteger (I64# x) = int64ToInteger x @@ -634,7 +715,7 @@ x# `modInt64#` y# = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0# | otherwise = r# where - r# = x# `remInt64#` y# + !r# = x# `remInt64#` y# instance Read Int64 where readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s] @@ -656,16 +737,11 @@ instance Bits Int64 where = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#` (x'# `uncheckedShiftRL64#` (64# -# i'#)))) where - x'# = int64ToWord64# x# - i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) + !x'# = int64ToWord64# x# + !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) bitSize _ = 64 isSigned _ = True - {-# 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 @@ -691,7 +767,9 @@ a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#) "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64 #-} -#else +-- No RULES for RealFrac methods if Int is smaller than Int64, we can't +-- go through Int and whether going through Integer is faster is uncertain. +#else -- Int64 is represented in the same way as Int. -- Operations may assume and must ensure that it holds only values @@ -730,27 +808,27 @@ instance Enum Int64 where instance Integral Int64 where quot x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `quotInt#` y#) rem x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `remInt#` y#) div x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `divInt#` y#) mod x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = I64# (x# `modInt#` y#) quotRem x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#)) divMod x@(I64# x#) y@(I64# y#) | y == 0 = divZeroError - | x == minBound && y == (-1) = overflowError + | y == (-1) && x == minBound = overflowError -- Note [Order of tests] | otherwise = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#)) toInteger (I64# x#) = smallInteger x# @@ -774,20 +852,46 @@ instance Bits Int64 where = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#` (x'# `uncheckedShiftRL#` (64# -# i'#)))) where - x'# = int2Word# x# - i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) + !x'# = int2Word# x# + !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#) bitSize _ = 64 isSigned _ = True - {-# INLINE shiftR #-} - -- same as the default definition, but we want it inlined (#2376) - x `shiftR` i = x `shift` (-i) - {-# RULES "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x# "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#) #-} +{-# RULES +"properFraction/Float->(Int64,Float)" + forall x. properFraction (x :: Float) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int64) n, y) } +"truncate/Float->Int64" + forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int64) (truncate x) +"floor/Float->Int64" + forall x. floor (x :: Float) = (fromIntegral :: Int -> Int64) (floor x) +"ceiling/Float->Int64" + forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int64) (ceiling x) +"round/Float->Int64" + forall x. round (x :: Float) = (fromIntegral :: Int -> Int64) (round x) + #-} + +{-# RULES +"properFraction/Double->(Int64,Double)" + forall x. properFraction (x :: Double) = + case properFraction x of { + (n, y) -> ((fromIntegral :: Int -> Int64) n, y) } +"truncate/Double->Int64" + forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int64) (truncate x) +"floor/Double->Int64" + forall x. floor (x :: Double) = (fromIntegral :: Int -> Int64) (floor x) +"ceiling/Double->Int64" + forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int64) (ceiling x) +"round/Double->Int64" + forall x. round (x :: Double) = (fromIntegral :: Int -> Int64) (round x) + #-} + uncheckedIShiftL64# :: Int# -> Int# -> Int# uncheckedIShiftL64# = uncheckedIShiftL# @@ -806,3 +910,151 @@ instance Ix Int64 where range (m,n) = [m..n] unsafeIndex (m,_) i = fromIntegral i - fromIntegral m inRange (m,n) i = m <= i && i <= n + +------------------------------------------------------------------------ +-- Generic deriving +------------------------------------------------------------------------ + +-- We need instances for some basic datatypes, but some of those use Int, +-- so we have to put the instances here +{- +deriving instance Eq Arity +deriving instance Eq Associativity +deriving instance Eq Fixity + +deriving instance Ord Arity +deriving instance Ord Associativity +deriving instance Ord Fixity + +deriving instance Read Arity +deriving instance Read Associativity +deriving instance Read Fixity + +deriving instance Show Arity +deriving instance Show Associativity +deriving instance Show Fixity +-} + +{- +Note [Order of tests] + +Suppose we had a definition like: + + quot x y + | y == 0 = divZeroError + | x == minBound && y == (-1) = overflowError + | otherwise = x `primQuot` y + +Note in particular that the + x == minBound +test comes before the + y == (-1) +test. + +this expands to something like: + + case y of + 0 -> divZeroError + _ -> case x of + -9223372036854775808 -> + case y of + -1 -> overflowError + _ -> x `primQuot` y + _ -> x `primQuot` y + +Now if we have the call (x `quot` 2), and quot gets inlined, then we get: + + case 2 of + 0 -> divZeroError + _ -> case x of + -9223372036854775808 -> + case 2 of + -1 -> overflowError + _ -> x `primQuot` 2 + _ -> x `primQuot` 2 + +which simplifies to: + + case x of + -9223372036854775808 -> x `primQuot` 2 + _ -> x `primQuot` 2 + +Now we have a case with two identical branches, which would be +eliminated (assuming it doesn't affect strictness, which it doesn't in +this case), leaving the desired: + + x `primQuot` 2 + +except in the minBound branch we know what x is, and GHC cleverly does +the division at compile time, giving: + + case x of + -9223372036854775808 -> -4611686018427387904 + _ -> x `primQuot` 2 + +So instead we use a definition like: + + quot x y + | y == 0 = divZeroError + | y == (-1) && x == minBound = overflowError + | otherwise = x `primQuot` y + +which gives us: + + case y of + 0 -> divZeroError + -1 -> + case x of + -9223372036854775808 -> overflowError + _ -> x `primQuot` y + _ -> x `primQuot` y + +for which our call (x `quot` 2) expands to: + + case 2 of + 0 -> divZeroError + -1 -> + case x of + -9223372036854775808 -> overflowError + _ -> x `primQuot` 2 + _ -> x `primQuot` 2 + +which simplifies to: + + x `primQuot` 2 + +as required. + + + +But we now have the same problem with a constant numerator: the call +(2 `quot` y) expands to + + case y of + 0 -> divZeroError + -1 -> + case 2 of + -9223372036854775808 -> overflowError + _ -> 2 `primQuot` y + _ -> 2 `primQuot` y + +which simplifies to: + + case y of + 0 -> divZeroError + -1 -> 2 `primQuot` y + _ -> 2 `primQuot` y + +which simplifies to: + + case y of + 0 -> divZeroError + -1 -> -2 + _ -> 2 `primQuot` y + + +However, constant denominators are more common than constant numerators, +so the + y == (-1) && x == minBound +order gives us better code in the common case. +-}