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