Permalink
Browse files

Move Integer out into its own package

We now depend on the new integer package.
We also depend on a new ghc-prim package, which has GHC.Prim,
GHC.PrimopWrappers, and new modules GHC.Bool and GHC.Generics,
containing Bool and Unit/Inl/Inr respectively.
  • Loading branch information...
1 parent 12c6837 commit 72e4fe7801d2d8ab5b243cbb430272b45010f59d @igfoo igfoo committed Mar 23, 2008
Showing with 184 additions and 441 deletions.
  1. +6 −25 Data/Bits.hs
  2. +3 −5 Data/Unique.hs
  3. +23 −16 GHC/Base.lhs
  4. +2 −1 GHC/Exts.hs
  5. +9 −22 GHC/Float.lhs
  6. +10 −14 GHC/Int.hs
  7. +104 −325 GHC/Num.lhs
  8. +1 −4 GHC/Ptr.lhs
  9. +11 −6 GHC/Real.lhs
  10. +14 −19 GHC/Word.hs
  11. +1 −4 base.cabal
View
@@ -259,30 +259,11 @@ foreign import ccall nhc_primIntCompl :: Int -> Int
#endif /* __NHC__ */
instance Bits Integer where
-#ifdef __GLASGOW_HASKELL__
- (S# x) .&. (S# y) = S# (word2Int# (int2Word# x `and#` int2Word# y))
- x@(S# _) .&. y = toBig x .&. y
- x .&. y@(S# _) = x .&. toBig y
- (J# s1 d1) .&. (J# s2 d2) =
- case andInteger# s1 d1 s2 d2 of
- (# s, d #) -> J# s d
-
- (S# x) .|. (S# y) = S# (word2Int# (int2Word# x `or#` int2Word# y))
- x@(S# _) .|. y = toBig x .|. y
- x .|. y@(S# _) = x .|. toBig y
- (J# s1 d1) .|. (J# s2 d2) =
- case orInteger# s1 d1 s2 d2 of
- (# s, d #) -> J# s d
-
- (S# x) `xor` (S# y) = S# (word2Int# (int2Word# x `xor#` int2Word# y))
- x@(S# _) `xor` y = toBig x `xor` y
- x `xor` y@(S# _) = x `xor` toBig y
- (J# s1 d1) `xor` (J# s2 d2) =
- case xorInteger# s1 d1 s2 d2 of
- (# s, d #) -> J# s d
-
- complement (S# x) = S# (word2Int# (int2Word# x `xor#` int2Word# (0# -# 1#)))
- complement (J# s d) = case complementInteger# s d of (# s, d #) -> J# s d
+#if defined(__GLASGOW_HASKELL__)
+ (.&.) = andInteger
+ (.|.) = orInteger
+ xor = xorInteger
+ complement = complementInteger
#else
-- reduce bitwise binary operations to special cases we can handle
@@ -309,7 +290,7 @@ instance Bits Integer where
bitSize _ = error "Data.Bits.bitSize(Integer)"
isSigned _ = True
-#ifndef __GLASGOW_HASKELL__
+#if !defined(__GLASGOW_HASKELL__)
-- Crude implementation of bitwise operations on Integers: convert them
-- to finite lists of Ints (least significant first), zip and convert
-- back again.
View
@@ -26,7 +26,7 @@ import System.IO.Unsafe (unsafePerformIO)
#ifdef __GLASGOW_HASKELL__
import GHC.Base
-import GHC.Num ( Integer(..) )
+import GHC.Num
#endif
-- | An abstract unique object. Objects of type 'Unique' may be
@@ -52,10 +52,8 @@ newUnique = do
-- same value, although in practice this is unlikely. The 'Int'
-- returned makes a good hash key.
hashUnique :: Unique -> Int
-#ifdef __GLASGOW_HASKELL__
-hashUnique (Unique (S# i)) = I# i
-hashUnique (Unique (J# s d)) | s ==# 0# = 0
- | otherwise = I# (indexIntArray# d 0#)
+#if defined(__GLASGOW_HASKELL__)
+hashUnique (Unique i) = I# (hashInteger i)
#else
hashUnique (Unique u) = fromInteger (u `mod` (toInteger (maxBound :: Int) + 1))
#endif
View
@@ -84,11 +84,15 @@ Other Prelude modules are much easier with fewer complex dependencies.
module GHC.Base
(
module GHC.Base,
+ module GHC.Bool,
+ module GHC.Generics,
module GHC.Prim, -- Re-export GHC.Prim and GHC.Err, to avoid lots
module GHC.Err -- of people having to import it explicitly
)
where
+import GHC.Bool
+import GHC.Generics
import GHC.Prim
import {-# SOURCE #-} GHC.Err
@@ -468,8 +472,25 @@ mapFB c f x ys = c (f x) ys
-- first so that the corresponding 'Prelude.Enum' instance will give
-- 'Prelude.fromEnum' 'False' the value zero, and
-- 'Prelude.fromEnum' 'True' the value 1.
-data Bool = False | True deriving (Eq, Ord)
- -- Read in GHC.Read, Show in GHC.Show
+-- The actual definition is in the ghc-prim package.
+
+-- XXX These don't work:
+-- deriving instance Eq Bool
+-- deriving instance Ord Bool
+-- <wired into compiler>:
+-- Illegal binding of built-in syntax: con2tag_Bool#
+
+instance Eq Bool where
+ True == True = True
+ False == False = True
+ _ == _ = False
+
+instance Ord Bool where
+ compare False True = LT
+ compare True False = GT
+ compare _ _ = EQ
+
+-- Read is in GHC.Read, Show in GHC.Show
-- Boolean functions
@@ -773,20 +794,6 @@ asTypeOf = const
%*********************************************************
%* *
-\subsection{Generics}
-%* *
-%*********************************************************
-
-\begin{code}
-data Unit = Unit
-#ifndef __HADDOCK__
-data (:+:) a b = Inl a | Inr b
-data (:*:) a b = a :*: b
-#endif
-\end{code}
-
-%*********************************************************
-%* *
\subsection{@getTag@}
%* *
%*********************************************************
View
@@ -15,7 +15,8 @@
module GHC.Exts
(
-- * Representations of some basic types
- Int(..),Word(..),Float(..),Double(..),Integer(..),Char(..),
+ Int(..),Word(..),Float(..),Double(..),
+ Char(..),
Ptr(..), FunPtr(..),
-- * Primitive operations
View
@@ -190,13 +190,7 @@ instance Num Float where
| otherwise = negate 1
{-# INLINE fromInteger #-}
- fromInteger (S# i#) = case (int2Float# i#) of { d# -> F# d# }
- fromInteger (J# s# d#) = encodeFloat# s# d# 0
- -- previous code: fromInteger n = encodeFloat n 0
- -- doesn't work too well, because encodeFloat is defined in
- -- terms of ccalls which can never be simplified away. We
- -- want simple literals like (fromInteger 3 :: Float) to turn
- -- into (F# 3.0), hence the special case for S# here.
+ fromInteger i = F# (floatFromInteger i)
instance Real Float where
toRational x = (m%1)*(b%1)^^n
@@ -278,12 +272,10 @@ instance RealFloat Float where
floatDigits _ = FLT_MANT_DIG -- ditto
floatRange _ = (FLT_MIN_EXP, FLT_MAX_EXP) -- ditto
- decodeFloat (F# f#)
- = case decodeFloat# f# of
- (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
+ decodeFloat (F# f#) = case decodeFloatInteger f# of
+ (# i, e #) -> (i, I# e)
- encodeFloat (S# i) j = int_encodeFloat# i j
- encodeFloat (J# s# d#) e = encodeFloat# s# d# e
+ encodeFloat i (I# e) = F# (encodeFloatInteger i e)
exponent x = case decodeFloat x of
(m,n) -> if m == 0 then 0 else n + floatDigits x
@@ -336,9 +328,7 @@ instance Num Double where
| otherwise = negate 1
{-# INLINE fromInteger #-}
- -- See comments with Num Float
- fromInteger (S# i#) = case (int2Double# i#) of { d# -> D# d# }
- fromInteger (J# s# d#) = encodeDouble# s# d# 0
+ fromInteger i = D# (doubleFromInteger i)
instance Real Double where
@@ -422,11 +412,10 @@ instance RealFloat Double where
floatRange _ = (DBL_MIN_EXP, DBL_MAX_EXP) -- ditto
decodeFloat (D# x#)
- = case decodeDouble# x# of
- (# exp#, s#, d# #) -> (J# s# d#, I# exp#)
+ = case decodeDoubleInteger x# of
+ (# i, j #) -> (i, I# j)
- encodeFloat (S# i) j = int_encodeDouble# i j
- encodeFloat (J# s# d#) e = encodeDouble# s# d# e
+ encodeFloat i (I# j) = D# (encodeDoubleInteger i j)
exponent x = case decodeFloat x of
(m,n) -> if m == 0 then 0 else n + floatDigits x
@@ -645,7 +634,7 @@ floatToDigits base x =
(p - 1 + e0) * 3 `div` 10
else
ceiling ((log (fromInteger (f+1)) +
- fromInteger (int2Integer e) * log (fromInteger b)) /
+ fromIntegral e * log (fromInteger b)) /
log (fromInteger base))
--WAS: fromInt e * log (fromInteger b))
@@ -936,8 +925,6 @@ foreign import ccall unsafe "isFloatNegativeZero" isFloatNegativeZero :: Float -
foreign import ccall unsafe "__encodeDouble"
encodeDouble# :: Int# -> ByteArray# -> Int -> Double
-foreign import ccall unsafe "__int_encodeDouble"
- int_encodeDouble# :: Int# -> Int -> Double
foreign import ccall unsafe "isDoubleNaN" isDoubleNaN :: Double -> Int
foreign import ccall unsafe "isDoubleInfinite" isDoubleInfinite :: Double -> Int
View
@@ -56,8 +56,7 @@ instance Num Int8 where
signum x | x > 0 = 1
signum 0 = 0
signum _ = -1
- fromInteger (S# i#) = I8# (narrow8Int# i#)
- fromInteger (J# s# d#) = I8# (narrow8Int# (integer2Int# s# d#))
+ fromInteger i = I8# (narrow8Int# (toInt# i))
instance Real Int8 where
toRational x = toInteger x % 1
@@ -104,7 +103,7 @@ instance Integral Int8 where
| x == minBound && y == (-1) = overflowError
| otherwise = (I8# (narrow8Int# (x# `divInt#` y#)),
I8# (narrow8Int# (x# `modInt#` y#)))
- toInteger (I8# x#) = S# x#
+ toInteger (I8# x#) = smallInteger x#
instance Bounded Int8 where
minBound = -0x80
@@ -169,8 +168,7 @@ instance Num Int16 where
signum x | x > 0 = 1
signum 0 = 0
signum _ = -1
- fromInteger (S# i#) = I16# (narrow16Int# i#)
- fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#))
+ fromInteger i = I16# (narrow16Int# (toInt# i))
instance Real Int16 where
toRational x = toInteger x % 1
@@ -217,7 +215,7 @@ instance Integral Int16 where
| x == minBound && y == (-1) = overflowError
| otherwise = (I16# (narrow16Int# (x# `divInt#` y#)),
I16# (narrow16Int# (x# `modInt#` y#)))
- toInteger (I16# x#) = S# x#
+ toInteger (I16# x#) = smallInteger x#
instance Bounded Int16 where
minBound = -0x8000
@@ -342,7 +340,7 @@ instance Integral Int32 where
I32# (x# `modInt32#` y#))
toInteger x@(I32# x#)
| x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
- = S# (int32ToInt# x#)
+ = smallInteger (int32ToInt# x#)
| otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d
divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
@@ -445,8 +443,7 @@ instance Num Int32 where
signum x | x > 0 = 1
signum 0 = 0
signum _ = -1
- fromInteger (S# i#) = I32# (narrow32Int# i#)
- fromInteger (J# s# d#) = I32# (narrow32Int# (integer2Int# s# d#))
+ fromInteger i = I32# (narrow32Int# (toInt# i))
instance Enum Int32 where
succ x
@@ -494,7 +491,7 @@ instance Integral Int32 where
| x == minBound && y == (-1) = overflowError
| otherwise = (I32# (narrow32Int# (x# `divInt#` y#)),
I32# (narrow32Int# (x# `modInt#` y#)))
- toInteger (I32# x#) = S# x#
+ toInteger (I32# x#) = smallInteger x#
instance Read Int32 where
readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
@@ -627,7 +624,7 @@ instance Integral Int64 where
toInteger x@(I64# x#)
| x >= fromIntegral (minBound::Int) &&
x <= fromIntegral (maxBound::Int)
- = S# (int64ToInt# x#)
+ = smallInteger (int64ToInt# x#)
| otherwise = case int64ToInteger# x# of
(# s, d #) -> J# s d
@@ -749,8 +746,7 @@ instance Num Int64 where
signum x | x > 0 = 1
signum 0 = 0
signum _ = -1
- fromInteger (S# i#) = I64# i#
- fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
+ fromInteger i = I64# (toInt# i)
instance Enum Int64 where
succ x
@@ -789,7 +785,7 @@ instance Integral Int64 where
| y == 0 = divZeroError
| x == minBound && y == (-1) = overflowError
| otherwise = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
- toInteger (I64# x#) = S# x#
+ toInteger (I64# x#) = smallInteger x#
instance Read Int64 where
readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
Oops, something went wrong.

0 comments on commit 72e4fe7

Please sign in to comment.