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