2 % (c) The GRASP Project, Glasgow University, 1992-1996
4 \section[Outputable]{Classes for pretty-printing}
6 Defines classes for pretty-printing and forcing, both forms of
10 #include "HsVersions.h"
13 Outputable(..), -- class
17 interppSP, interpp'SP,
20 ifPprShowAll, ifnotPprShowAll,
26 #if __GLASGOW_HASKELL__ <= 200
34 #if __GLASGOW_HASKELL__ >= 202
38 import PprStyle ( PprStyle(..) )
40 import Util ( cmpPString )
43 %************************************************************************
45 \subsection[Outputable-class]{The @Outputable@ class}
47 %************************************************************************
50 class Outputable a where
51 ppr :: PprStyle -> a -> Doc
55 printDoc :: Mode -> Handle -> Doc -> IO ()
57 = fullRender mode 100 1.5 put done doc
59 put (Chr c) next = hPutChar hdl c >> next
60 put (Str s) next = hPutStr hdl s >> next
61 put (PStr s) next = hPutFS hdl s >> next
63 done = hPutChar hdl '\n'
68 interppSP :: Outputable a => PprStyle -> [a] -> Doc
69 interppSP sty xs = hsep (map (ppr sty) xs)
71 interpp'SP :: Outputable a => PprStyle -> [a] -> Doc
73 = hsep (punctuate comma (map (ppr sty) xs))
77 ifPprDebug sty p = case sty of PprDebug -> p ; _ -> empty
78 ifPprShowAll sty p = case sty of PprShowAll -> p ; _ -> empty
79 ifPprInterface sty p = case sty of PprInterface -> p ; _ -> empty
81 ifnotPprForUser sty p = case sty of { PprForUser -> empty ; PprQuote -> empty; _ -> p }
82 ifnotPprShowAll sty p = case sty of { PprShowAll -> empty ; _ -> p }
86 pprQuote :: PprStyle -> (PprStyle -> Doc) -> Doc
87 pprQuote PprQuote fn = quotes (fn PprForUser)
88 pprQuote sty fn = fn sty
93 instance Outputable Bool where
94 ppr sty True = ptext SLIT("True")
95 ppr sty False = ptext SLIT("False")
97 instance (Outputable a) => Outputable [a] where
98 ppr sty xs = brackets (fsep (punctuate comma (map (ppr sty) xs)))
100 instance (Outputable a, Outputable b) => Outputable (a, b) where
102 hang (hcat [lparen, ppr sty x, comma]) 4 ((<>) (ppr sty y) rparen)
104 -- ToDo: may not be used
105 instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
107 parens (sep [ (<>) (ppr sty x) comma,
108 (<>) (ppr sty y) comma,
114 @speakNth@ converts an integer to a verbal index; eg 1 maps to
118 speakNth :: Int -> Doc
120 speakNth 1 = ptext SLIT("first")
121 speakNth 2 = ptext SLIT("second")
122 speakNth 3 = ptext SLIT("third")
123 speakNth 4 = ptext SLIT("fourth")
124 speakNth 5 = ptext SLIT("fifth")
125 speakNth 6 = ptext SLIT("sixth")
126 speakNth n = hcat [ int n, text st_nd_rd_th ]
128 st_nd_rd_th | n_rem_10 == 1 = "st"
129 | n_rem_10 == 2 = "nd"
130 | n_rem_10 == 3 = "rd"
133 n_rem_10 = n `rem` 10