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