remove trailing whitespace
[ghc-base.git] / GHC / Real.lhs
index 8f39cf8..bc63aeb 100644 (file)
@@ -1,12 +1,12 @@
 \begin{code}
-{-# OPTIONS_GHC -fno-implicit-prelude #-}
+{-# OPTIONS_GHC -XNoImplicitPrelude #-}
 {-# OPTIONS_HADDOCK hide #-}
 -----------------------------------------------------------------------------
 -- |
 -- Module      :  GHC.Real
--- Copyright   :  (c) The FFI Task Force, 1994-2002
+-- Copyright   :  (c) The University of Glasgow, 1994-2002
 -- License     :  see libraries/base/LICENSE
--- 
+--
 -- Maintainer  :  cvs-ghc@haskell.org
 -- Stability   :  internal
 -- Portability :  non-portable (GHC Extensions)
@@ -24,12 +24,13 @@ import GHC.Num
 import GHC.List
 import GHC.Enum
 import GHC.Show
+import GHC.Err
 
 infixr 8  ^, ^^
 infixl 7  /, `quot`, `rem`, `div`, `mod`
 infixl 7  %
 
-default ()              -- Double isn't available yet, 
+default ()              -- Double isn't available yet,
                         -- and we shouldn't be using defaults anyway
 \end{code}
 
@@ -57,8 +58,8 @@ infinity, notANumber :: Rational
 infinity   = 1 :% 0
 notANumber = 0 :% 0
 
--- Use :%, not % for Inf/NaN; the latter would 
--- immediately lead to a runtime error, because it normalises. 
+-- Use :%, not % for Inf/NaN; the latter would
+-- immediately lead to a runtime error, because it normalises.
 \end{code}
 
 
@@ -132,10 +133,15 @@ class  (Real a, Enum a) => Integral a  where
     -- | conversion to 'Integer'
     toInteger           :: a -> Integer
 
+    {-# INLINE quot #-}
+    {-# INLINE rem #-}
+    {-# INLINE div #-}
+    {-# INLINE mod #-}
     n `quot` d          =  q  where (q,_) = quotRem n d
     n `rem` d           =  r  where (_,r) = quotRem n d
     n `div` d           =  q  where (q,_) = divMod n d
     n `mod` d           =  r  where (_,r) = divMod n d
+
     divMod n d          =  if signum r == negate (signum d) then (q-1, r+d) else qr
                            where qr@(q,r) = quotRem n d
 
@@ -153,6 +159,8 @@ class  (Num a) => Fractional a  where
     -- @('Fractional' a) => a@.
     fromRational        :: Rational -> a
 
+    {-# INLINE recip #-}
+    {-# INLINE (/) #-}
     recip x             =  1 / x
     x / y               = x * recip y
 
@@ -173,25 +181,28 @@ class  (Real a, Fractional a) => RealFrac a  where
     properFraction      :: (Integral b) => a -> (b,a)
     -- | @'truncate' x@ returns the integer nearest @x@ between zero and @x@
     truncate            :: (Integral b) => a -> b
-    -- | @'round' x@ returns the nearest integer to @x@
+    -- | @'round' x@ returns the nearest integer to @x@;
+    --   the even integer if @x@ is equidistant between two integers
     round               :: (Integral b) => a -> b
     -- | @'ceiling' x@ returns the least integer not less than @x@
     ceiling             :: (Integral b) => a -> b
     -- | @'floor' x@ returns the greatest integer not greater than @x@
     floor               :: (Integral b) => a -> b
 
+    {-# INLINE truncate #-}
     truncate x          =  m  where (m,_) = properFraction x
-    
+
     round x             =  let (n,r) = properFraction x
                                m     = if r < 0 then n - 1 else n + 1
                            in case signum (abs r - 0.5) of
                                 -1 -> n
                                 0  -> if even n then n else m
                                 1  -> m
-    
+                                _  -> error "round default defn: Bad value"
+
     ceiling x           =  if r > 0 then n + 1 else n
                            where (n,r) = properFraction x
-    
+
     floor x             =  if r < 0 then n - 1 else n
                            where (n,r) = properFraction x
 \end{code}
@@ -201,20 +212,21 @@ These 'numeric' enumerations come straight from the Report
 
 \begin{code}
 numericEnumFrom         :: (Fractional a) => a -> [a]
-numericEnumFrom         =  iterate (+1)
+numericEnumFrom n      =  n `seq` (n : numericEnumFrom (n + 1))
 
 numericEnumFromThen     :: (Fractional a) => a -> a -> [a]
-numericEnumFromThen n m =  iterate (+(m-n)) n
+numericEnumFromThen n m        = n `seq` m `seq` (n : numericEnumFromThen m (m+m-n))
 
 numericEnumFromTo       :: (Ord a, Fractional a) => a -> a -> [a]
 numericEnumFromTo n m   = takeWhile (<= m + 1/2) (numericEnumFrom n)
 
 numericEnumFromThenTo   :: (Ord a, Fractional a) => a -> a -> a -> [a]
-numericEnumFromThenTo e1 e2 e3 = takeWhile pred (numericEnumFromThen e1 e2)
+numericEnumFromThenTo e1 e2 e3
+    = takeWhile predicate (numericEnumFromThen e1 e2)
                                 where
                                  mid = (e2 - e1) / 2
-                                 pred | e2 >= e1  = (<= e3 + mid)
-                                      | otherwise = (>= e3 + mid)
+                                 predicate | e2 >= e1  = (<= e3 + mid)
+                                           | otherwise = (>= e3 + mid)
 \end{code}
 
 
@@ -276,17 +288,17 @@ instance  Real Integer  where
 instance  Integral Integer where
     toInteger n      = n
 
-    a `quot` 0 = divZeroError
+    _ `quot` 0 = divZeroError
     n `quot` d = n `quotInteger` d
 
-    a `rem` 0 = divZeroError
+    _ `rem` 0 = divZeroError
     n `rem`  d = n `remInteger`  d
 
-    a `divMod` 0 = divZeroError
+    _ `divMod` 0 = divZeroError
     a `divMod` b = case a `divModInteger` b of
                    (# x, y #) -> (x, y)
 
-    a `quotRem` 0 = divZeroError
+    _ `quotRem` 0 = divZeroError
     a `quotRem` b = case a `quotRemInteger` b of
                     (# q, r #) -> (q, r)
 
@@ -319,7 +331,10 @@ instance  (Integral a)  => Num (Ratio a)  where
 instance  (Integral a)  => Fractional (Ratio a)  where
     {-# SPECIALIZE instance Fractional Rational #-}
     (x:%y) / (x':%y')   =  (x*y') % (y*x')
-    recip (x:%y)        =  y % x
+    recip (0:%_)        = error "Ratio.%: zero denominator"
+    recip (x:%y)
+        | x < 0         = negate y :% negate x
+        | otherwise     = y :% x
     fromRational (x:%y) =  fromInteger x :% fromInteger y
 
 instance  (Integral a)  => Real (Ratio a)  where
@@ -334,9 +349,12 @@ instance  (Integral a)  => RealFrac (Ratio a)  where
 instance  (Integral a)  => Show (Ratio a)  where
     {-# SPECIALIZE instance Show Rational #-}
     showsPrec p (x:%y)  =  showParen (p > ratioPrec) $
-                           showsPrec ratioPrec1 x . 
-                           showString "%" .     -- H98 report has spaces round the %
-                                                -- but we removed them [May 04]
+                           showsPrec ratioPrec1 x .
+                           showString " % " .
+                           -- H98 report has spaces round the %
+                           -- but we removed them [May 04]
+                           -- and added them again for consistency with
+                           -- Haskell 98 [Sep 08, #1920]
                            showsPrec ratioPrec1 y
 
 instance  (Integral a)  => Enum (Ratio a)  where
@@ -391,7 +409,7 @@ showSigned :: (Real a)
   -> Int                -- ^ the precedence of the enclosing context
   -> a                  -- ^ the value to show
   -> ShowS
-showSigned showPos p x 
+showSigned showPos p x
    | x < 0     = showParen (p > 6) (showChar '-' . showPos (-x))
    | otherwise = showPos x
 
@@ -408,11 +426,15 @@ odd             =  not . even
 (^) :: (Num a, Integral b) => a -> b -> a
 x0 ^ y0 | y0 < 0    = error "Negative exponent"
         | y0 == 0   = 1
-        | otherwise = f x0 y0 1
-    where -- x0 ^ y0 = (x ^ y) * z
-          f x y z | even y = f (x * x) (y `quot` 2) z
+        | otherwise = f x0 y0
+    where -- f : x0 ^ y0 = x ^ y
+          f x y | even y    = f (x * x) (y `quot` 2)
+                | y == 1    = x
+                | otherwise = g (x * x) ((y - 1) `quot` 2) x
+          -- g : x0 ^ y0 = (x ^ y) * z
+          g x y z | even y = g (x * x) (y `quot` 2) z
                   | y == 1 = x * z
-                  | otherwise = f (x * x) ((y - 1) `quot` 2) (x * z)
+                  | otherwise = g (x * x) ((y - 1) `quot` 2) (x * z)
 
 -- | raise a number to an integral power
 {-# SPECIALISE (^^) ::
@@ -438,18 +460,21 @@ lcm _ 0         =  0
 lcm 0 _         =  0
 lcm x y         =  abs ((x `quot` (gcd x y)) * y)
 
+#ifdef OPTIMISE_INTEGER_GCD_LCM
 {-# RULES
 "gcd/Int->Int->Int"             gcd = gcdInt
+"gcd/Integer->Integer->Integer" gcd = gcdInteger'
+"lcm/Integer->Integer->Integer" lcm = lcmInteger
  #-}
 
--- XXX these optimisation rules are disabled for now to make it easier
---     to experiment with other Integer implementations
--- "gcd/Integer->Integer->Integer" gcd = gcdInteger'
--- "lcm/Integer->Integer->Integer" lcm = lcmInteger
---
--- gcdInteger' :: Integer -> Integer -> Integer
--- gcdInteger' 0 0 = error "GHC.Real.gcdInteger': gcd 0 0 is undefined"
--- gcdInteger' a b = gcdInteger a b
+gcdInteger' :: Integer -> Integer -> Integer
+gcdInteger' 0 0 = error "GHC.Real.gcdInteger': gcd 0 0 is undefined"
+gcdInteger' a b = gcdInteger a b
+
+gcdInt :: Int -> Int -> Int
+gcdInt 0 0 = error "GHC.Real.gcdInt: gcd 0 0 is undefined"
+gcdInt a b = fromIntegral (gcdInteger (fromIntegral a) (fromIntegral b))
+#endif
 
 integralEnumFrom :: (Integral a, Bounded a) => a -> [a]
 integralEnumFrom n = map fromInteger [toInteger n .. toInteger (maxBound `asTypeOf` n)]