[project @ 1996-07-25 20:43:49 by partain]
[ghc-hetmet.git] / ghc / compiler / utils / Outputable.lhs
1 %
2 % (c) The GRASP Project, Glasgow University, 1992-1996
3 %
4 \section[Outputable]{Classes for pretty-printing}
5
6 Defines classes for pretty-printing and forcing, both forms of
7 ``output.''
8
9 \begin{code}
10 #include "HsVersions.h"
11
12 module Outputable (
13         Outputable(..),         -- class
14
15         interppSP, interpp'SP,
16         ifnotPprForUser,
17         ifPprDebug,
18         ifPprShowAll, ifnotPprShowAll,
19         ifPprInterface
20     ) where
21
22 IMP_Ubiq(){-uitous-}
23
24 import PprStyle         ( PprStyle(..) )
25 import Pretty
26 import Util             ( cmpPString )
27 \end{code}
28
29 %************************************************************************
30 %*                                                                      *
31 \subsection[Outputable-class]{The @Outputable@ class}
32 %*                                                                      *
33 %************************************************************************
34
35 \begin{code}
36 class Outputable a where
37         ppr :: PprStyle -> a -> Pretty
38 \end{code}
39
40 \begin{code}
41 -- the ppSep in the ppInterleave puts in the spaces
42 -- Death to ppSep! (WDP 94/11)
43
44 interppSP  :: Outputable a => PprStyle -> [a] -> Pretty
45 interppSP  sty xs = ppIntersperse ppSP (map (ppr sty) xs)
46
47 interpp'SP :: Outputable a => PprStyle -> [a] -> Pretty
48 interpp'SP sty xs
49   = ppIntersperse sep (map (ppr sty) xs)
50   where
51     sep = ppBeside ppComma ppSP
52 \end{code}
53
54 \begin{code}
55 ifPprDebug      sty p = case sty of PprDebug     -> p ; _ -> ppNil
56 ifPprShowAll    sty p = case sty of PprShowAll   -> p ; _ -> ppNil
57 ifPprInterface  sty p = case sty of PprInterface -> p ; _ -> ppNil
58
59 ifnotPprForUser   sty p = case sty of PprForUser -> ppNil ; _ -> p
60 ifnotPprShowAll   sty p = case sty of PprShowAll -> ppNil ; _ -> p
61 \end{code}
62
63 \begin{code}
64 instance Outputable Bool where
65     ppr sty True = ppPStr SLIT("True")
66     ppr sty False = ppPStr SLIT("False")
67
68 instance (Outputable a) => Outputable [a] where
69     ppr sty xs =
70       ppBesides [ ppLbrack, ppInterleave ppComma (map (ppr sty) xs), ppRbrack ]
71
72 instance (Outputable a, Outputable b) => Outputable (a, b) where
73     ppr sty (x,y) =
74       ppHang (ppBesides [ppLparen, ppr sty x, ppComma]) 4 (ppBeside (ppr sty y) ppRparen)
75
76 -- ToDo: may not be used
77 instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
78     ppr sty (x,y,z) =
79       ppSep [ ppBesides [ppLparen, ppr sty x, ppComma],
80               ppBeside (ppr sty y) ppComma,
81               ppBeside (ppr sty z) ppRparen ]
82 \end{code}