[project @ 2000-10-31 12:07:43 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 import TyCon            ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon, 
32                           maybeTyConSingleCon, isEnumerationTyCon, 
33                           tyConArity
34                         )
35 import Class            ( Class )
36
37 -- others:
38 import Maybes           ( maybeToBool )
39 import Name             ( getOccString, getOccName )
40 import Outputable
41 import PprEnv
42 import Unique           ( Uniquable(..) )
43 import PrelNames                -- quite a few *Keys
44 \end{code}
45
46 %************************************************************************
47 %*                                                                      *
48 \subsection{The external interface}
49 %*                                                                      *
50 %************************************************************************
51
52 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
53 defined to use this.  @pprParendType@ is the same, except it puts
54 parens around the type, except for the atomic cases.  @pprParendType@
55 works just by setting the initial context precedence very high.
56
57 \begin{code}
58 pprType, pprParendType :: Type -> SDoc
59 pprType       ty = ppr_ty pprTyEnv tOP_PREC   ty
60 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
61
62 pprKind, pprParendKind :: Kind -> SDoc
63 pprKind       = pprType
64 pprParendKind = pprParendType
65
66 pprPred :: PredType -> SDoc
67 pprPred (Class clas tys) = pprConstraint clas tys
68 pprPred (IParam n ty)    = hsep [ptext SLIT("?") <> ppr n,
69                                  ptext SLIT("::"), ppr ty]
70
71 pprConstraint :: Class -> [Type] -> SDoc
72 pprConstraint clas tys = ppr clas <+> hsep (map pprParendType tys)
73
74 pprTheta :: ThetaType -> SDoc
75 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
76
77 instance Outputable Type where
78     ppr ty = pprType ty
79
80 instance Outputable PredType where
81     ppr = pprPred
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 `hasKey` 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 (getOccName bx)   -- Always unqualified
125         other          -> maybeParen ctxt_prec tYCON_PREC 
126                                      (sep [ppr tycon, nest 4 tys_w_spaces])
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 (PredTy p) = braces (ppr_pred env p)
216
217 ppr_pred env (Class clas tys) = ppr clas <+>
218                                 hsep (map (ppr_ty env tYCON_PREC) tys)
219 ppr_pred env (IParam n ty)    = hsep [char '?' <> ppr n, text "::",
220                                       ppr_ty env tYCON_PREC ty]
221 \end{code}
222
223 \begin{code}
224 pprTyEnv = initPprEnv b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
225   where
226     b = panic "PprType:init_ppr_env"
227 \end{code}
228
229 \begin{code}
230 instance Outputable UsageAnn where
231   ppr UsOnce     = ptext SLIT("-")
232   ppr UsMany     = ptext SLIT("!")
233   ppr (UsVar uv) = ppr uv
234 \end{code}
235
236
237 %************************************************************************
238 %*                                                                      *
239 \subsection[TyVar]{@TyVar@}
240 %*                                                                      *
241 %************************************************************************
242
243 We print type-variable binders with their kinds in interface files,
244 and when in debug mode.
245
246 \begin{code}
247 pprTyVarBndr tyvar
248   = getPprStyle $ \ sty ->
249     if (ifaceStyle sty  && kind /= boxedTypeKind) || debugStyle sty then
250         hsep [ppr tyvar, dcolon, pprParendKind kind]
251                 -- See comments with ppDcolon in PprCore.lhs
252     else
253         ppr tyvar
254   where
255     kind = tyVarKind tyvar
256
257 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
258 \end{code}
259
260
261 %************************************************************************
262 %*                                                                      *
263 \subsection{Mumbo jumbo}
264 %*                                                                      *
265 %************************************************************************
266
267 Grab a name for the type. This is used to determine the type
268 description for profiling.
269
270 \begin{code}
271 getTyDescription :: Type -> String
272
273 getTyDescription ty
274   = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
275     case tau_ty of
276       TyVarTy _        -> "*"
277       AppTy fun _      -> getTyDescription fun
278       FunTy _ res      -> '-' : '>' : fun_result res
279       TyConApp tycon _ -> getOccString tycon
280       NoteTy (FTVNote _) ty  -> getTyDescription ty
281       NoteTy (SynNote ty1) _ -> getTyDescription ty1
282       NoteTy (UsgNote _) ty  -> getTyDescription ty
283       PredTy p               -> getTyDescription (predRepTy p)
284       ForAllTy _ ty    -> getTyDescription ty
285     }
286   where
287     fun_result (FunTy _ res) = '>' : fun_result res
288     fun_result other         = getTyDescription other
289 \end{code}
290
291
292 \begin{code}
293 showTypeCategory :: Type -> Char
294   {-
295         {C,I,F,D}   char, int, float, double
296         T           tuple
297         S           other single-constructor type
298         {c,i,f,d}   unboxed ditto
299         t           *unpacked* tuple
300         s           *unpacked" single-cons...
301
302         v           void#
303         a           primitive array
304
305         E           enumeration type
306         +           dictionary, unless it's a ...
307         L           List
308         >           function
309         M           other (multi-constructor) data-con type
310         .           other type
311         -           reserved for others to mark as "uninteresting"
312     -}
313 showTypeCategory ty
314   = if isDictTy ty
315     then '+'
316     else
317       case splitTyConApp_maybe ty of
318         Nothing -> if maybeToBool (splitFunTy_maybe ty)
319                    then '>'
320                    else '.'
321
322         Just (tycon, _) ->
323           let utc = getUnique tycon in
324           if      utc == charDataConKey    then 'C'
325           else if utc == intDataConKey     then 'I'
326           else if utc == floatDataConKey   then 'F'
327           else if utc == doubleDataConKey  then 'D'
328           else if utc == smallIntegerDataConKey ||
329                   utc == largeIntegerDataConKey   then 'J'
330           else if utc == charPrimTyConKey  then 'c'
331           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
332                 || utc == addrPrimTyConKey)                then 'i'
333           else if utc  == floatPrimTyConKey                then 'f'
334           else if utc  == doublePrimTyConKey               then 'd'
335           else if isPrimTyCon tycon {- array, we hope -}   then 'A'
336           else if isEnumerationTyCon tycon                 then 'E'
337           else if isTupleTyCon tycon                       then 'T'
338           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
339           else if utc == listTyConKey                      then 'L'
340           else 'M' -- oh, well...
341 \end{code}