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