6b1b9051289355b9e0c52ed70800a16105856033
[ghc-hetmet.git] / ghc / compiler / types / PprType.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[PprType]{Printing Types, TyVars, Classes, TyCons}
5
6 \begin{code}
7 module PprType(
8         pprKind, pprParendKind,
9         pprType, pprParendType,
10         pprConstraint, pprTheta,
11         pprTyVarBndr, pprTyVarBndrs,
12
13         -- Junk
14         getTyDescription, showTypeCategory
15  ) where
16
17 #include "HsVersions.h"
18
19 -- friends:
20 -- (PprType can see all the representations it's trying to print)
21 import Type             ( Type(..), TyNote(..), Kind, ThetaType, UsageAnn(..),
22                           splitDictTy_maybe,
23                           splitForAllTys, splitSigmaTy, splitRhoTy,
24                           isDictTy, splitTyConApp_maybe, splitFunTy_maybe,
25                           boxedTypeKind
26                         )
27 import Var              ( TyVar, tyVarKind,
28                           tyVarName, setTyVarName
29                         )
30 import VarEnv
31 import TyCon            ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon, 
32                           maybeTyConSingleCon, isEnumerationTyCon, 
33                           tyConArity, tyConUnique
34                         )
35 import Class            ( Class )
36
37 -- others:
38 import Maybes           ( maybeToBool )
39 import Name             ( getOccString, NamedThing(..) )
40 import Outputable
41 import PprEnv
42 import Unique           ( Uniquable(..) )
43 import Unique           -- quite a few *Keys
44 import Util
45 \end{code}
46
47 %************************************************************************
48 %*                                                                      *
49 \subsection{The external interface}
50 %*                                                                      *
51 %************************************************************************
52
53 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
54 defined to use this.  @pprParendType@ is the same, except it puts
55 parens around the type, except for the atomic cases.  @pprParendType@
56 works just by setting the initial context precedence very high.
57
58 \begin{code}
59 pprType, pprParendType :: Type -> SDoc
60 pprType       ty = ppr_ty pprTyEnv tOP_PREC   ty
61 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
62
63 pprKind, pprParendKind :: Kind -> SDoc
64 pprKind       = pprType
65 pprParendKind = pprParendType
66
67 pprConstraint :: Class -> [Type] -> SDoc
68 pprConstraint clas tys = ppr clas <+> hsep (map (pprParendType) tys)
69
70 pprTheta :: ThetaType -> SDoc
71 pprTheta theta = parens (hsep (punctuate comma (map ppr_dict theta)))
72                where
73                  ppr_dict (c,tys) = pprConstraint c tys
74
75 instance Outputable Type where
76     ppr ty = pprType ty
77 \end{code}
78
79
80 %************************************************************************
81 %*                                                                      *
82 \subsection{Pretty printing}
83 %*                                                                      *
84 %************************************************************************
85
86 Precedence
87 ~~~~~~~~~~
88 @ppr_ty@ takes an @Int@ that is the precedence of the context.
89 The precedence levels are:
90 \begin{description}
91 \item[tOP_PREC]   No parens required.
92 \item[fUN_PREC]   Left hand argument of a function arrow.
93 \item[tYCON_PREC] Argument of a type constructor.
94 \end{description}
95
96
97 \begin{code}
98 tOP_PREC    = (0 :: Int)
99 fUN_PREC    = (1 :: Int)
100 tYCON_PREC  = (2 :: Int)
101
102 maybeParen ctxt_prec inner_prec pretty
103   | ctxt_prec < inner_prec = pretty
104   | otherwise              = parens pretty
105 \end{code}
106
107 \begin{code}
108 ppr_ty :: PprEnv TyVar -> Int -> Type -> SDoc
109 ppr_ty env ctxt_prec (TyVarTy tyvar)
110   = pTyVarO env tyvar
111
112 ppr_ty env ctxt_prec ty@(TyConApp tycon tys)
113         -- KIND CASE; it's of the form (Type x)
114   | tycon_uniq == typeConKey && n_tys == 1
115   =     -- For kinds, print (Type x) as just x if x is a 
116         --      type constructor (must be Boxed, Unboxed, AnyBox)
117         -- Otherwise print as (Type x)
118     case ty1 of
119         TyConApp bx [] -> ppr bx
120         other          -> maybeParen ctxt_prec tYCON_PREC 
121                                      (sep [ppr tycon, nest 4 tys_w_spaces])
122                        
123         
124         -- TUPLE CASE (boxed and unboxed)
125   |  isTupleTyCon tycon
126   && length tys == tyConArity tycon     -- no magic if partially applied
127   = parens tys_w_commas
128
129   |  isUnboxedTupleTyCon tycon
130   && length tys == tyConArity tycon     -- no magic if partially applied
131   = parens (char '#' <+> tys_w_commas <+> char '#')
132
133         -- LIST CASE
134   | tycon_uniq == listTyConKey && n_tys == 1
135   = brackets (ppr_ty env tOP_PREC ty1)
136
137         -- DICTIONARY CASE, prints {C a}
138         -- This means that instance decls come out looking right in interfaces
139         -- and that in turn means they get "gated" correctly when being slurped in
140   | maybeToBool maybe_dict
141   = braces (ppr_dict env tYCON_PREC ctys)
142
143         -- NO-ARGUMENT CASE (=> no parens)
144   | null tys
145   = ppr tycon
146
147         -- GENERAL CASE
148   | otherwise
149   = maybeParen ctxt_prec tYCON_PREC (sep [ppr tycon, nest 4 tys_w_spaces])
150
151   where
152     tycon_uniq = tyConUnique tycon
153     n_tys      = length tys
154     (ty1:_)    = tys
155     Just ctys  = maybe_dict
156     maybe_dict = splitDictTy_maybe ty   -- Checks class and arity
157     tys_w_commas = sep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
158     tys_w_spaces = sep (map (ppr_ty env tYCON_PREC) tys)
159   
160
161
162 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
163   = getPprStyle $ \ sty -> 
164     maybeParen ctxt_prec fUN_PREC $
165     if ifaceStyle sty then
166        sep [ ptext SLIT("__forall") <+> brackets pp_tyvars <+> ptext SLIT("=>"), 
167              ppr_ty env tOP_PREC rho
168            ]
169     else
170         -- The type checker occasionally prints a type in an error message,
171         -- and it had better come out looking like a user type
172        sep [ ptext SLIT("forall") <+> pp_tyvars <> ptext SLIT("."), 
173              ppr_theta theta,
174              ppr_ty env tOP_PREC tau
175            ]
176   where         
177     (tyvars, rho) = splitForAllTys ty  -- don't treat theta specially any more (KSW 1999-04)
178     (theta, tau)  = splitRhoTy rho
179     
180     pp_tyvars = hsep (map (pBndr env LambdaBind) tyvars)
181     
182     ppr_theta []        = empty
183     ppr_theta theta     = parens (hsep (punctuate comma (map ppr_dict theta))) 
184                           <+> ptext SLIT("=>")
185
186     ppr_dict (clas,tys) = ppr clas <+> hsep (map (ppr_ty env tYCON_PREC) tys)
187
188
189 ppr_ty env ctxt_prec (FunTy ty1 ty2)
190   = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest ty2))
191   -- we don't want to lose usage annotations or synonyms,
192   -- so we mustn't use splitFunTys here.
193   where
194     pp_rest (FunTy ty1 ty2) = pp_codom ty1 : pp_rest ty2
195     pp_rest ty              = [pp_codom ty]
196     pp_codom ty             = ptext SLIT("->") <+> ppr_ty env fUN_PREC ty
197
198 ppr_ty env ctxt_prec (AppTy ty1 ty2)
199   = maybeParen ctxt_prec tYCON_PREC $
200     ppr_ty env tOP_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
201
202 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
203   = ppr_ty env ctxt_prec ty
204 --  = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
205
206 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
207
208 ppr_ty env ctxt_prec (NoteTy (UsgNote u) ty)
209   = maybeParen ctxt_prec tYCON_PREC $
210     ppr u <+> ppr_ty env tYCON_PREC ty
211
212 ppr_theta env []    = empty
213 ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_dict env tOP_PREC) theta)))
214
215 ppr_dict env ctxt (clas, tys) = ppr clas <+> 
216                                 hsep (map (ppr_ty env tYCON_PREC) tys)
217 \end{code}
218
219 \begin{code}
220 pprTyEnv = initPprEnv b b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
221   where
222     b = panic "PprType:init_ppr_env"
223 \end{code}
224
225 \begin{code}
226 instance Outputable UsageAnn where
227   ppr UsOnce     = ptext SLIT("__o")
228   ppr UsMany     = ptext SLIT("__m")
229   ppr (UsVar uv) = ptext SLIT("__uv") <> ppr uv
230 \end{code}
231
232 %************************************************************************
233 %*                                                                      *
234 \subsection[TyVar]{@TyVar@}
235 %*                                                                      *
236 %************************************************************************
237
238 We print type-variable binders with their kinds in interface files,
239 and when in debug mode.
240
241 \begin{code}
242 pprTyVarBndr tyvar
243   = getPprStyle $ \ sty ->
244     if (ifaceStyle sty || debugStyle sty) && kind /= boxedTypeKind then
245         hsep [ppr tyvar, dcolon, pprParendKind kind]
246                 -- See comments with ppDcolon in PprCore.lhs
247     else
248         ppr tyvar
249   where
250     kind = tyVarKind tyvar
251
252 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
253 \end{code}
254
255
256 %************************************************************************
257 %*                                                                      *
258 \subsection{Mumbo jumbo}
259 %*                                                                      *
260 %************************************************************************
261
262 Grab a name for the type. This is used to determine the type
263 description for profiling.
264
265 \begin{code}
266 getTyDescription :: Type -> String
267
268 getTyDescription ty
269   = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
270     case tau_ty of
271       TyVarTy _        -> "*"
272       AppTy fun _      -> getTyDescription fun
273       FunTy _ res      -> '-' : '>' : fun_result res
274       TyConApp tycon _ -> getOccString tycon
275       NoteTy (FTVNote _) ty  -> getTyDescription ty
276       NoteTy (SynNote ty1) _ -> getTyDescription ty1
277       NoteTy (UsgNote _) ty  -> getTyDescription ty
278       ForAllTy _ ty    -> getTyDescription ty
279     }
280   where
281     fun_result (FunTy _ res) = '>' : fun_result res
282     fun_result other         = getTyDescription other
283 \end{code}
284
285
286 \begin{code}
287 showTypeCategory :: Type -> Char
288   {-
289         {C,I,F,D}   char, int, float, double
290         T           tuple
291         S           other single-constructor type
292         {c,i,f,d}   unboxed ditto
293         t           *unpacked* tuple
294         s           *unpacked" single-cons...
295
296         v           void#
297         a           primitive array
298
299         E           enumeration type
300         +           dictionary, unless it's a ...
301         L           List
302         >           function
303         M           other (multi-constructor) data-con type
304         .           other type
305         -           reserved for others to mark as "uninteresting"
306     -}
307 showTypeCategory ty
308   = if isDictTy ty
309     then '+'
310     else
311       case splitTyConApp_maybe ty of
312         Nothing -> if maybeToBool (splitFunTy_maybe ty)
313                    then '>'
314                    else '.'
315
316         Just (tycon, _) ->
317           let utc = getUnique tycon in
318           if      utc == charDataConKey    then 'C'
319           else if utc == intDataConKey     then 'I'
320           else if utc == floatDataConKey   then 'F'
321           else if utc == doubleDataConKey  then 'D'
322           else if utc == smallIntegerDataConKey ||
323                   utc == largeIntegerDataConKey   then 'J'
324           else if utc == charPrimTyConKey  then 'c'
325           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
326                 || utc == addrPrimTyConKey)                then 'i'
327           else if utc  == floatPrimTyConKey                then 'f'
328           else if utc  == doublePrimTyConKey               then 'd'
329           else if isPrimTyCon tycon {- array, we hope -}   then 'A'
330           else if isEnumerationTyCon tycon                 then 'E'
331           else if isTupleTyCon tycon                       then 'T'
332           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
333           else if utc == listTyConKey                      then 'L'
334           else 'M' -- oh, well...
335 \end{code}