[project @ 1996-04-08 16:15:43 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 import 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   = ppInterleave sep (map (ppr sty) xs)
50   where
51     sep = ppBeside ppComma ppSP
52
53 #ifdef USE_ATTACK_PRAGMAS
54 {-# SPECIALIZE interppSP :: PprStyle -> [Id] -> Pretty #-}
55 {-# SPECIALIZE interppSP :: PprStyle -> [TyVar] -> Pretty #-}
56
57 {-# SPECIALIZE interpp'SP :: PprStyle -> [(Id, Id)] -> Pretty #-}
58 {-# SPECIALIZE interpp'SP :: PprStyle -> [Id] -> Pretty #-}
59 {-# SPECIALIZE interpp'SP :: PprStyle -> [TyVarTemplate] -> Pretty #-}
60 {-# SPECIALIZE interpp'SP :: PprStyle -> [TyVar] -> Pretty #-}
61 {-# SPECIALIZE interpp'SP :: PprStyle -> [Type] -> Pretty #-}
62 #endif
63 \end{code}
64
65 \begin{code}
66 ifPprDebug      sty p = case sty of PprDebug     -> p ; _ -> ppNil
67 ifPprShowAll    sty p = case sty of PprShowAll   -> p ; _ -> ppNil
68 ifPprInterface  sty p = case sty of PprInterface -> p ; _ -> ppNil
69
70 ifnotPprForUser   sty p = case sty of PprForUser -> ppNil ; _ -> p
71 ifnotPprShowAll   sty p = case sty of PprShowAll -> ppNil ; _ -> p
72 \end{code}
73
74 \begin{code}
75 instance Outputable Bool where
76     ppr sty True = ppPStr SLIT("True")
77     ppr sty False = ppPStr SLIT("False")
78
79 instance (Outputable a) => Outputable [a] where
80     ppr sty xs =
81       ppBesides [ ppLbrack, ppInterleave ppComma (map (ppr sty) xs), ppRbrack ]
82
83 instance (Outputable a, Outputable b) => Outputable (a, b) where
84     ppr sty (x,y) =
85       ppHang (ppBesides [ppLparen, ppr sty x, ppComma]) 4 (ppBeside (ppr sty y) ppRparen)
86
87 -- ToDo: may not be used
88 instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
89     ppr sty (x,y,z) =
90       ppSep [ ppBesides [ppLparen, ppr sty x, ppComma],
91               ppBeside (ppr sty y) ppComma,
92               ppBeside (ppr sty z) ppRparen ]
93 \end{code}