2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 1992-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- The 'Show' class, and related operations.
15 -----------------------------------------------------------------------------
22 -- Instances for Show: (), [], Bool, Ordering, Int, Char
25 shows, showChar, showString, showParen, showList__, showSpace,
26 showLitChar, protectEsc,
27 intToDigit, showSignedInt,
30 -- Character operations
38 import GHC.List ( (!!), foldr1
39 #ifdef USE_REPORT_PRELUDE
47 %*********************************************************
49 \subsection{The @Show@ class}
51 %*********************************************************
54 -- | The @shows@ functions return a function that prepends the
55 -- output 'String' to an existing 'String'. This allows constant-time
56 -- concatenation of results using function composition.
57 type ShowS = String -> String
59 -- | Conversion of values to readable 'String's.
61 -- Minimal complete definition: 'showsPrec' or 'show'.
63 -- Derived instances of 'Show' have the following properties, which
64 -- are compatible with derived instances of 'Text.Read.Read':
66 -- * The result of 'show' is a syntactically correct Haskell
67 -- expression containing only constants, given the fixity
68 -- declarations in force at the point where the type is declared.
69 -- It contains only the constructor names defined in the data type,
70 -- parentheses, and spaces. When labelled constructor fields are
71 -- used, braces, commas, field names, and equal signs are also used.
73 -- * If the constructor is defined to be an infix operator, then
74 -- 'showsPrec' will produce infix applications of the constructor.
76 -- * the representation will be enclosed in parentheses if the
77 -- precedence of the top-level constructor in @x@ is less than @d@
78 -- (associativity is ignored). Thus, if @d@ is @0@ then the result
79 -- is never surrounded in parentheses; if @d@ is @11@ it is always
80 -- surrounded in parentheses, unless it is an atomic expression.
82 -- * If the constructor is defined using record syntax, then 'show'
83 -- will produce the record-syntax form, with the fields given in the
84 -- same order as the original declaration.
86 -- For example, given the declarations
89 -- > data Tree a = Leaf a | Tree a :^: Tree a
91 -- the derived instance of 'Show' is equivalent to
93 -- > instance (Show a) => Show (Tree a) where
95 -- > showsPrec d (Leaf m) = showParen (d > app_prec) $
96 -- > showString "Leaf " . showsPrec (app_prec+1) m
97 -- > where app_prec = 10
99 -- > showsPrec d (u :^: v) = showParen (d > up_prec) $
100 -- > showsPrec (up_prec+1) u .
101 -- > showString " :^: " .
102 -- > showsPrec (up_prec+1) v
103 -- > where up_prec = 5
105 -- Note that right-associativity of @:^:@ is ignored. For example,
107 -- * @'show' (Leaf 1 :^: Leaf 2 :^: Leaf 3)@ produces the string
108 -- @\"Leaf 1 :^: (Leaf 2 :^: Leaf 3)\"@.
111 -- | Convert a value to a readable 'String'.
113 -- 'showsPrec' should satisfy the law
115 -- > showsPrec d x r ++ s == showsPrec d x (r ++ s)
117 -- Derived instances of 'Text.Read.Read' and 'Show' satisfy the following:
119 -- * @(x,\"\")@ is an element of
120 -- @('Text.Read.readsPrec' d ('showsPrec' d x \"\"))@.
122 -- That is, 'Text.Read.readsPrec' parses the string produced by
123 -- 'showsPrec', and delivers the value that 'showsPrec' started with.
125 showsPrec :: Int -- ^ the operator precedence of the enclosing
126 -- context (a number from @0@ to @11@).
127 -- Function application has precedence @10@.
128 -> a -- ^ the value to be converted to a 'String'
131 -- | A specialised variant of 'showsPrec', using precedence context
132 -- zero, and returning an ordinary 'String'.
135 -- | The method 'showList' is provided to allow the programmer to
136 -- give a specialised way of showing lists of values.
137 -- For example, this is used by the predefined 'Show' instance of
138 -- the 'Char' type, where values of type 'String' should be shown
139 -- in double quotes, rather than between square brackets.
140 showList :: [a] -> ShowS
142 showsPrec _ x s = show x ++ s
144 showList ls s = showList__ shows ls s
146 showList__ :: (a -> ShowS) -> [a] -> ShowS
147 showList__ _ [] s = "[]" ++ s
148 showList__ showx (x:xs) s = '[' : showx x (showl xs)
151 showl (y:ys) = ',' : showx y (showl ys)
153 appPrec, appPrec1 :: Int
154 -- Use unboxed stuff because we don't have overloaded numerics yet
155 appPrec = I# 10# -- Precedence of application:
156 -- one more than the maximum operator precedence of 9
157 appPrec1 = I# 11# -- appPrec + 1
160 %*********************************************************
162 \subsection{Simple Instances}
164 %*********************************************************
168 instance Show () where
169 showsPrec _ () = showString "()"
171 instance Show a => Show [a] where
172 showsPrec _ = showList
174 instance Show Bool where
175 showsPrec _ True = showString "True"
176 showsPrec _ False = showString "False"
178 instance Show Ordering where
179 showsPrec _ LT = showString "LT"
180 showsPrec _ EQ = showString "EQ"
181 showsPrec _ GT = showString "GT"
183 instance Show Char where
184 showsPrec _ '\'' = showString "'\\''"
185 showsPrec _ c = showChar '\'' . showLitChar c . showChar '\''
187 showList cs = showChar '"' . showl cs
188 where showl "" s = showChar '"' s
189 showl ('"':xs) s = showString "\\\"" (showl xs s)
190 showl (x:xs) s = showLitChar x (showl xs s)
191 -- Making 's' an explicit parameter makes it clear to GHC
192 -- that showl has arity 2, which avoids it allocating an extra lambda
193 -- The sticking point is the recursive call to (showl xs), which
194 -- it can't figure out would be ok with arity 2.
196 instance Show Int where
197 showsPrec = showSignedInt
199 instance Show a => Show (Maybe a) where
200 showsPrec _p Nothing s = showString "Nothing" s
201 showsPrec p (Just x) s
202 = (showParen (p > appPrec) $
204 showsPrec appPrec1 x) s
206 instance (Show a, Show b) => Show (Either a b) where
208 (showParen (p > appPrec) $
210 Left a -> showString "Left " . showsPrec appPrec1 a
211 Right b -> showString "Right " . showsPrec appPrec1 b)
216 %*********************************************************
218 \subsection{Show instances for the first few tuples
220 %*********************************************************
223 -- The explicit 's' parameters are important
224 -- Otherwise GHC thinks that "shows x" might take a lot of work to compute
225 -- and generates defns like
226 -- showsPrec _ (x,y) = let sx = shows x; sy = shows y in
227 -- \s -> showChar '(' (sx (showChar ',' (sy (showChar ')' s))))
229 instance (Show a, Show b) => Show (a,b) where
230 showsPrec _ (a,b) s = show_tuple [shows a, shows b] s
232 instance (Show a, Show b, Show c) => Show (a, b, c) where
233 showsPrec _ (a,b,c) s = show_tuple [shows a, shows b, shows c] s
235 instance (Show a, Show b, Show c, Show d) => Show (a, b, c, d) where
236 showsPrec _ (a,b,c,d) s = show_tuple [shows a, shows b, shows c, shows d] s
238 instance (Show a, Show b, Show c, Show d, Show e) => Show (a, b, c, d, e) where
239 showsPrec _ (a,b,c,d,e) s = show_tuple [shows a, shows b, shows c, shows d, shows e] s
241 instance (Show a, Show b, Show c, Show d, Show e, Show f) => Show (a,b,c,d,e,f) where
242 showsPrec _ (a,b,c,d,e,f) s = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f] s
244 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g)
245 => Show (a,b,c,d,e,f,g) where
246 showsPrec _ (a,b,c,d,e,f,g) s
247 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g] s
249 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h)
250 => Show (a,b,c,d,e,f,g,h) where
251 showsPrec _ (a,b,c,d,e,f,g,h) s
252 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h] s
254 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i)
255 => Show (a,b,c,d,e,f,g,h,i) where
256 showsPrec _ (a,b,c,d,e,f,g,h,i) s
257 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
260 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j)
261 => Show (a,b,c,d,e,f,g,h,i,j) where
262 showsPrec _ (a,b,c,d,e,f,g,h,i,j) s
263 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
266 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k)
267 => Show (a,b,c,d,e,f,g,h,i,j,k) where
268 showsPrec _ (a,b,c,d,e,f,g,h,i,j,k) s
269 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
270 shows i, shows j, shows k] s
272 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k,
274 => Show (a,b,c,d,e,f,g,h,i,j,k,l) where
275 showsPrec _ (a,b,c,d,e,f,g,h,i,j,k,l) s
276 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
277 shows i, shows j, shows k, shows l] s
279 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k,
281 => Show (a,b,c,d,e,f,g,h,i,j,k,l,m) where
282 showsPrec _ (a,b,c,d,e,f,g,h,i,j,k,l,m) s
283 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
284 shows i, shows j, shows k, shows l, shows m] s
286 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k,
287 Show l, Show m, Show n)
288 => Show (a,b,c,d,e,f,g,h,i,j,k,l,m,n) where
289 showsPrec _ (a,b,c,d,e,f,g,h,i,j,k,l,m,n) s
290 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
291 shows i, shows j, shows k, shows l, shows m, shows n] s
293 instance (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, Show i, Show j, Show k,
294 Show l, Show m, Show n, Show o)
295 => Show (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) where
296 showsPrec _ (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o) s
297 = show_tuple [shows a, shows b, shows c, shows d, shows e, shows f, shows g, shows h,
298 shows i, shows j, shows k, shows l, shows m, shows n, shows o] s
300 show_tuple :: [ShowS] -> ShowS
301 show_tuple ss = showChar '('
302 . foldr1 (\s r -> s . showChar ',' . r) ss
307 %*********************************************************
309 \subsection{Support code for @Show@}
311 %*********************************************************
314 -- | equivalent to 'showsPrec' with a precedence of 0.
315 shows :: (Show a) => a -> ShowS
316 shows = showsPrec zeroInt
318 -- | utility function converting a 'Char' to a show function that
319 -- simply prepends the character unchanged.
320 showChar :: Char -> ShowS
323 -- | utility function converting a 'String' to a show function that
324 -- simply prepends the string unchanged.
325 showString :: String -> ShowS
328 -- | utility function that surrounds the inner show function with
329 -- parentheses when the 'Bool' parameter is 'True'.
330 showParen :: Bool -> ShowS -> ShowS
331 showParen b p = if b then showChar '(' . p . showChar ')' else p
334 showSpace = {-showChar ' '-} \ xs -> ' ' : xs
337 Code specific for characters
340 -- | Convert a character to a string using only printable characters,
341 -- using Haskell source-language escape conventions. For example:
343 -- > showLitChar '\n' s = "\\n" ++ s
345 showLitChar :: Char -> ShowS
346 showLitChar c s | c > '\DEL' = showChar '\\' (protectEsc isDec (shows (ord c)) s)
347 showLitChar '\DEL' s = showString "\\DEL" s
348 showLitChar '\\' s = showString "\\\\" s
349 showLitChar c s | c >= ' ' = showChar c s
350 showLitChar '\a' s = showString "\\a" s
351 showLitChar '\b' s = showString "\\b" s
352 showLitChar '\f' s = showString "\\f" s
353 showLitChar '\n' s = showString "\\n" s
354 showLitChar '\r' s = showString "\\r" s
355 showLitChar '\t' s = showString "\\t" s
356 showLitChar '\v' s = showString "\\v" s
357 showLitChar '\SO' s = protectEsc (== 'H') (showString "\\SO") s
358 showLitChar c s = showString ('\\' : asciiTab!!ord c) s
359 -- I've done manual eta-expansion here, becuase otherwise it's
360 -- impossible to stop (asciiTab!!ord) getting floated out as an MFE
362 isDec c = c >= '0' && c <= '9'
364 protectEsc :: (Char -> Bool) -> ShowS -> ShowS
365 protectEsc p f = f . cont
366 where cont s@(c:_) | p c = "\\&" ++ s
371 asciiTab = -- Using an array drags in the array module. listArray ('\NUL', ' ')
372 ["NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
373 "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
374 "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
375 "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
379 Code specific for Ints.
382 -- | Convert an 'Int' in the range @0@..@15@ to the corresponding single
383 -- digit 'Char'. This function fails on other inputs, and generates
384 -- lower-case hexadecimal digits.
385 intToDigit :: Int -> Char
387 | i >=# 0# && i <=# 9# = unsafeChr (ord '0' `plusInt` I# i)
388 | i >=# 10# && i <=# 15# = unsafeChr (ord 'a' `minusInt` ten `plusInt` I# i)
389 | otherwise = error ("Char.intToDigit: not a digit " ++ show (I# i))
393 showSignedInt :: Int -> Int -> ShowS
394 showSignedInt (I# p) (I# n) r
395 | n <# 0# && p ># 6# = '(' : itos n (')' : r)
396 | otherwise = itos n r
398 itos :: Int# -> String -> String
401 let I# minInt# = minInt in
403 -- negateInt# minInt overflows, so we can't do that:
404 then '-' : itos' (negateInt# (n# `quotInt#` 10#))
405 (itos' (negateInt# (n# `remInt#` 10#)) cs)
406 else '-' : itos' (negateInt# n#) cs
407 | otherwise = itos' n# cs
409 itos' :: Int# -> String -> String
411 | n# <# 10# = C# (chr# (ord# '0'# +# n#)) : cs
412 | otherwise = case chr# (ord# '0'# +# (n# `remInt#` 10#)) of { c# ->
413 itos' (n# `quotInt#` 10#) (C# c# : cs) }