[project @ 2002-07-23 14:52:46 by simonpj]
authorsimonpj <unknown>
Tue, 23 Jul 2002 14:52:47 +0000 (14:52 +0000)
committersimonpj <unknown>
Tue, 23 Jul 2002 14:52:47 +0000 (14:52 +0000)
Various precedence errors in the code
for read and show.  A couple (the show instances
for Ratio and Array) were actually errors in
the Library Report.   A couple more were to
do with whether the precedence of application is
9 (wrong) or 10 (right).

GHC/Arr.lhs
GHC/Read.lhs
GHC/Real.lhs
GHC/Show.lhs

index dd8218c..a024b6f 100644 (file)
@@ -476,11 +476,12 @@ instance (Ix i, Ord e) => Ord (Array i e) where
 
 instance (Ix a, Show a, Show b) => Show (Array a b) where
     showsPrec p a =
-        showParen (p > 9) $
+        showParen (p > appPrec) $
         showString "array " .
-        shows (bounds a) .
+        showsPrec appPrec1 (bounds a) .
         showChar ' ' .
-        shows (assocs a)
+        showsPrec appPrec1 (assocs a)
+       -- Precedence of 'array' is the precedence of application
 
 -- The Read instance is in GHC.Read
 \end{code}
index 8296da5..68d0521 100644 (file)
@@ -73,10 +73,8 @@ import GHC.List
 import GHC.Show                -- isAlpha etc
 import GHC.Base
 import GHC.Arr
-
-ratioPrec = 7  -- Precedence of ':%' constructor
-appPrec   = 10 -- Precedence of applictaion
 \end{code}
+
 -------------------------------------------------------
        TEMPORARY UNTIL I DO DERIVED READ
 
index c2e73c0..e1696cc 100644 (file)
@@ -46,6 +46,10 @@ data  (Integral a)   => Ratio a = !a :% !a  deriving (Eq)
 -- 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
@@ -246,11 +250,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 #-}
index dd931b8..1401241 100644 (file)
@@ -24,6 +24,7 @@ module GHC.Show
        shows, showChar, showString, showParen, showList__, showSpace,
        showLitChar, protectEsc, 
        intToDigit, digitToInt, showSignedInt,
+       appPrec, appPrec1,
 
        -- Character operations
        isAscii, isLatin1, isControl, isPrint, isSpace, isUpper,
@@ -74,6 +75,12 @@ showList__ showx (x:xs) s = '[' : showx x (showl xs)
   where
     showl []     = ']' : s
     showl (y:ys) = ',' : showx y (showl ys)
+
+appPrec, appPrec1 :: Int
+       -- Use unboxed stuff because we don't have overloaded numerics yet
+appPrec = I# 10#       -- Precedence of application:
+                       --   one more than the maximum operator precedence of 9
+appPrec1 = I# 11#      -- appPrec + 1
 \end{code}
 
 %*********************************************************
@@ -117,19 +124,18 @@ instance Show Int where
 
 instance Show a => Show (Maybe a) where
     showsPrec _p Nothing s = showString "Nothing" s
-    showsPrec (I# p#) (Just x) s
-                          = (showParen (p# >=# 10#) $ 
+    showsPrec p (Just x) s
+                          = (showParen (p > appPrec) $ 
                             showString "Just " . 
-                            showsPrec (I# 10#) x) s
+                            showsPrec appPrec1 x) s
 
 instance (Show a, Show b) => Show (Either a b) where
-    showsPrec (I# p#) e s =
-       (showParen (p# >=# 10#) $
+    showsPrec p e s =
+       (showParen (p > appPrec) $
         case e of
-         Left  a -> showString "Left "  . showsPrec (I# 10#) a
-        Right b -> showString "Right " . showsPrec (I# 10#) b)
+         Left  a -> showString "Left "  . showsPrec appPrec1 a
+        Right b -> showString "Right " . showsPrec appPrec1 b)
        s
-
 \end{code}