[project @ 2001-05-18 16:54:04 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelNum.lhs
index 1ff4c98..42ec20b 100644 (file)
@@ -1,5 +1,7 @@
+% ------------------------------------------------------------------------------
+% $Id: PrelNum.lhs,v 1.39 2001/04/14 22:28:22 qrczak Exp $
 %
-% (c) The AQUA Project, Glasgow University, 1994-1996
+% (c) The University of Glasgow, 1994-2000
 %
 
 \section[PrelNum]{Module @PrelNum@}
@@ -43,24 +45,13 @@ class  (Eq a, Show a) => Num a  where
     negate             :: a -> a
     abs, signum                :: a -> a
     fromInteger                :: Integer -> a
-    fromInt            :: Int -> a -- partain: Glasgow extension
 
     x - y              = x + negate y
     negate x           = 0 - x
-    fromInt (I# i#)    = fromInteger (S# i#)
-                                       -- Go via the standard class-op if the
-                                       -- non-standard one ain't provided
-\end{code}
-
-A few small numeric functions
 
-\begin{code}
-subtract       :: (Num a) => a -> a -> a
 {-# INLINE subtract #-}
-subtract x y   =  y - x
-
-ord_0 :: Num a => a
-ord_0 = fromInt (ord '0')
+subtract :: (Num a) => a -> a -> a
+subtract x y = y - x
 \end{code}
 
 
@@ -72,18 +63,17 @@ ord_0 = fromInt (ord '0')
 
 \begin{code}
 instance  Num Int  where
-    (+)           x y =  plusInt x y
-    (-)           x y =  minusInt x y
-    negate x   =  negateInt x
-    (*)           x y =  timesInt x y
-    abs    n   = if n `geInt` 0 then n else (negateInt n)
+    (+)           = plusInt
+    (-)           = minusInt
+    negate = negateInt
+    (*)           = timesInt
+    abs n  = if n `geInt` 0 then n else negateInt n
 
     signum n | n `ltInt` 0 = negateInt 1
             | n `eqInt` 0 = 0
             | otherwise   = 1
 
-    fromInteger n = integer2Int n
-    fromInt n    = n
+    fromInteger = integer2Int
 \end{code}
 
 
@@ -127,10 +117,6 @@ integer2Int :: Integer -> Int
 integer2Int (S# i)   = I# i
 integer2Int (J# s d) = case (integer2Int# s d) of { n# -> I# n# }
 
-addr2Integer :: Addr# -> Integer
-{-# INLINE addr2Integer #-}
-addr2Integer x = case addr2Integer# x of (# s, d #) -> J# s d
-
 toBig (S# i)     = case int2Integer# i of { (# s, d #) -> J# s d }
 toBig i@(J# _ _) = i
 \end{code}
@@ -181,7 +167,7 @@ remInteger ia@(S# _) ib@(J# _ _) = remInteger (toBig ia) ib
 remInteger (J# sa a) (S# b)
   = case int2Integer# b of { (# sb, b #) ->
     case remInteger# sa a sb b of { (# sr, r #) ->
-    S# (sr *# (word2Int# (integer2Word# sr r))) }}
+    S# (integer2Int# sr r) }}
 remInteger (J# sa a) (J# sb b)
   = case remInteger# sa a sb b of (# sr, r #) -> J# sr r
 
@@ -208,17 +194,19 @@ quotInteger (J# sa a) (J# sb b)
 
 \begin{code}
 gcdInteger :: Integer -> Integer -> Integer
+-- SUP: Do we really need the first two cases?
 gcdInteger a@(S# (-2147483648#)) b = gcdInteger (toBig a) b
 gcdInteger a b@(S# (-2147483648#)) = gcdInteger a (toBig b)
-gcdInteger (S# a) (S# b) = S# (gcdInt# a b)
-gcdInteger ia@(S# a) ib@(J# sb b)
+gcdInteger (S# a) (S# b) = case gcdInt (I# a) (I# b) of { I# c -> S# c }
+gcdInteger ia@(S# 0#) ib@(J# 0# _) = error "PrelNum.gcdInteger: gcd 0 0 is undefined"
+gcdInteger ia@(S# a)  ib@(J# sb b)
   | a  ==# 0#  = abs ib
   | sb ==# 0#  = abs ia
-  | otherwise  = S# (gcdIntegerInt# sb b a)
-gcdInteger ia@(J# sa a) ib@(S# b)
-  | sa ==# 0#  = abs ib
-  | b ==# 0#   = abs ia
-  | otherwise  = S# (gcdIntegerInt# sa a b)
+  | otherwise  = S# (gcdIntegerInt# absSb b absA)
+       where absA  = if a  <# 0# then negateInt# a  else a
+             absSb = if sb <# 0# then negateInt# sb else sb
+gcdInteger ia@(J# _ _) ib@(S# _) = gcdInteger ib ia
+gcdInteger (J# 0# _) (J# 0# _) = error "PrelNum.gcdInteger: gcd 0 0 is undefined"
 gcdInteger (J# sa a) (J# sb b)
   = case gcdInteger# sa a sb b of (# sg, g #) -> J# sg g
 
@@ -236,7 +224,7 @@ divExact :: Integer -> Integer -> Integer
 divExact a@(S# (-2147483648#)) b = divExact (toBig a) b
 divExact (S# a) (S# b) = S# (quotInt# a b)
 divExact (S# a) (J# sb b)
-  = S# (quotInt# a (sb *# (word2Int# (integer2Word# sb b))))
+  = S# (quotInt# a (integer2Int# sb b))
 divExact (J# sa a) (S# b)
   = case int2Integer# b of
      (# sb, b #) -> case divExactInteger# sa a sb b of (# sd, d #) -> J# sd d
@@ -315,39 +303,13 @@ instance  Ord Integer  where
 
 \begin{code}
 instance  Num Integer  where
-    (+) i1@(S# i) i2@(S# j)
-       = case addIntC# i j of { (# r, c #) ->
-         if c ==# 0# then S# r
-         else toBig i1 + toBig i2 }
-    (+) i1@(J# _ _) i2@(S# _)  = i1 + toBig i2
-    (+) i1@(S# _) i2@(J# _ _)  = toBig i1 + i2
-    (+) (J# s1 d1) (J# s2 d2)
-      = case plusInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
-
-    (-) i1@(S# i) i2@(S# j)
-       = case subIntC# i j of { (# r, c #) ->
-         if c ==# 0# then S# r
-         else toBig i1 - toBig i2 }
-    (-) i1@(J# _ _) i2@(S# _)  = i1 - toBig i2
-    (-) i1@(S# _) i2@(J# _ _)  = toBig i1 - i2
-    (-) (J# s1 d1) (J# s2 d2)
-      = case minusInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
-
-    (*) i1@(S# i) i2@(S# j)
-       = case mulIntC# i j of { (# r, c #) ->
-         if c ==# 0# then S# r
-         else toBig i1 * toBig i2 }
-    (*) i1@(J# _ _) i2@(S# _)  = i1 * toBig i2
-    (*) i1@(S# _) i2@(J# _ _)  = toBig i1 * i2
-    (*) (J# s1 d1) (J# s2 d2)
-      = case timesInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
-
-    negate (S# (-2147483648#)) = 2147483648
-    negate (S# i) = S# (negateInt# i)
-    negate (J# s d) = J# (negateInt# s) d
+    (+) = plusInteger
+    (-) = minusInteger
+    (*) = timesInteger
+    negate        = negateInteger
+    fromInteger        x  =  x
 
     -- ORIG: abs n = if n >= 0 then n else -n
-
     abs (S# (-2147483648#)) = 2147483648
     abs (S# i) = case abs (I# i) of I# j -> S# j
     abs n@(J# s d) = if (s >=# 0#) then n else J# (negateInt# s) d
@@ -361,9 +323,30 @@ instance  Num Integer  where
        else if cmp ==# 0# then S# 0#
        else                    S# (negateInt# 1#)
 
-    fromInteger        x       =  x
-
-    fromInt (I# i)     =  S# i
+plusInteger i1@(S# i) i2@(S# j)  = case addIntC# i j of { (# r, c #) ->
+                                  if c ==# 0# then S# r
+                                  else toBig i1 + toBig i2 }
+plusInteger i1@(J# _ _) i2@(S# _) = i1 + toBig i2
+plusInteger i1@(S# _) i2@(J# _ _) = toBig i1 + i2
+plusInteger (J# s1 d1) (J# s2 d2) = case plusInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
+
+minusInteger i1@(S# i) i2@(S# j)   = case subIntC# i j of { (# r, c #) ->
+                                    if c ==# 0# then S# r
+                                    else toBig i1 - toBig i2 }
+minusInteger i1@(J# _ _) i2@(S# _) = i1 - toBig i2
+minusInteger i1@(S# _) i2@(J# _ _) = toBig i1 - i2
+minusInteger (J# s1 d1) (J# s2 d2) = case minusInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
+
+timesInteger i1@(S# i) i2@(S# j)   = case mulIntC# i j of { (# r, c #) ->
+                                    if c ==# 0# then S# r
+                                    else toBig i1 * toBig i2 }
+timesInteger i1@(J# _ _) i2@(S# _) = i1 * toBig i2
+timesInteger i1@(S# _) i2@(J# _ _) = toBig i1 * i2
+timesInteger (J# s1 d1) (J# s2 d2) = case timesInteger# s1 d1 s2 d2 of (# s, d #) -> J# s d
+
+negateInteger (S# (-2147483648#)) = 2147483648
+negateInteger (S# i)             = S# (negateInt# i)
+negateInteger (J# s d)           = J# (negateInt# s) d
 \end{code}
 
 
@@ -442,24 +425,23 @@ dn_list x delta lim = go (x::Integer)
 %*********************************************************
 
 \begin{code}
-instance  Show Integer  where
-    showsPrec   x = showSignedInteger x
-    showList = showList__ (showsPrec 0) 
-
-showSignedInteger :: Int -> Integer -> ShowS
-showSignedInteger p n r
-  | n < 0 && p > 6 = '(':jtos n (')':r)
-  | otherwise      = jtos n r
+instance Show Integer where
+    showsPrec p n r
+        | n < 0 && p > 6 = '(' : jtos n (')' : r)
+        | otherwise      = jtos n r
+    showList = showList__ (showsPrec 0)
 
 jtos :: Integer -> String -> String
-jtos i rs
- | i < 0     = '-' : jtos' (-i) rs
- | otherwise = jtos' i rs
- where
-  jtos' :: Integer -> String -> String
-  jtos' n cs
-   | n < 10    = chr (fromInteger n + (ord_0::Int)) : cs
-   | otherwise = jtos' q (chr (integer2Int r + (ord_0::Int)) : cs)
+jtos n cs
+    | n < 0     = '-' : jtos' (-n) cs
+    | otherwise = jtos' n cs
     where
-     (q,r) = n `quotRemInteger` 10
+    jtos' :: Integer -> String -> String
+    jtos' n' cs'
+        | n' < 10    = case unsafeChr (ord '0' + fromInteger n') of
+            c@(C# _) -> c:cs'
+        | otherwise = case unsafeChr (ord '0' + fromInteger r) of
+            c@(C# _) -> jtos' q (c:cs')
+        where
+        (q,r) = n' `quotRemInteger` 10
 \end{code}