[project @ 2002-09-03 09:40:51 by simonmar]
[haskell-directory.git] / GHC / Real.lhs
index 98edd2e..a656c5f 100644 (file)
@@ -1,25 +1,19 @@
-% ------------------------------------------------------------------------------
-% $Id: Real.lhs,v 1.3 2002/02/12 15:51:26 simonmar Exp $
-%
-% (c) The University of Glasgow, 1994-2000
-%
-
-\section[GHC.Real]{Module @GHC.Real@}
-
-The types
-
-       Ratio, Rational
-
-and the classes
-
-       Real
-       Integral
-       Fractional
-       RealFrac
-
-
 \begin{code}
 {-# OPTIONS -fno-implicit-prelude #-}
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  GHC.Real
+-- Copyright   :  (c) The FFI Task Force, 1994-2002
+-- License     :  see libraries/base/LICENSE
+-- 
+-- Maintainer  :  cvs-ghc@haskell.org
+-- Stability   :  internal
+-- Portability :  non-portable (GHC Extensions)
+--
+-- The types 'Ratio' and 'Rational', and the classes 'Real', 'Fractional',
+-- 'Integral', and 'RealFrac'.
+--
+-----------------------------------------------------------------------------
 
 module GHC.Real where
 
@@ -32,6 +26,7 @@ import GHC.Show
 
 infixr 8  ^, ^^
 infixl 7  /, `quot`, `rem`, `div`, `mod`
+infixl 7  %
 
 default ()             -- Double isn't available yet, 
                        -- and we shouldn't be using defaults anyway
@@ -46,7 +41,22 @@ default ()           -- Double isn't available yet,
 
 \begin{code}
 data  (Integral a)     => Ratio a = !a :% !a  deriving (Eq)
+
+-- | Arbitrary-precision rational numbers, represented as a ratio of
+-- two 'Integer' values.  A rational number may be constructed using
+-- the '%' operator.
 type  Rational         =  Ratio Integer
+
+ratioPrec, ratioPrec1 :: Int
+ratioPrec  = 7         -- Precedence of ':%' constructor
+ratioPrec1 = ratioPrec + 1
+
+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. 
 \end{code}
 
 
@@ -241,11 +251,10 @@ instance  (Integral a)    => RealFrac (Ratio a)  where
 
 instance  (Integral a)  => Show (Ratio a)  where
     {-# SPECIALIZE instance Show Rational #-}
-    showsPrec p (x:%y) =  showParen (p > ratio_prec)
-                              (shows x . showString " % " . shows y)
-
-ratio_prec :: Int
-ratio_prec = 7
+    showsPrec p (x:%y) =  showParen (p > ratioPrec) $
+                          showsPrec ratioPrec1 x . 
+                          showString " % " . 
+                          showsPrec ratioPrec1 y
 
 instance  (Integral a) => Enum (Ratio a)  where
     {-# SPECIALIZE instance Enum Rational #-}