[project @ 2000-11-07 15:21:38 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, boxedTypeKind )  -- friend
22 import Type             ( PredType(..), ThetaType,
23                           splitPredTy_maybe,
24                           splitForAllTys, splitSigmaTy, splitRhoTy,
25                           isDictTy, splitTyConApp_maybe, splitFunTy_maybe,
26                           predRepTy, isUTyVar
27                         )
28 import Var              ( TyVar, tyVarKind )
29 import TyCon            ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon, 
30                           maybeTyConSingleCon, isEnumerationTyCon, 
31                           tyConArity, tyConName
32                         )
33 import Class            ( Class )
34
35 -- others:
36 import CmdLineOpts      ( opt_PprStyle_RawTypes )
37 import Maybes           ( maybeToBool )
38 import Name             ( getOccString, getOccName )
39 import Outputable
40 import PprEnv
41 import Unique           ( Uniquable(..) )
42 import PrelNames                -- quite a few *Keys
43 \end{code}
44
45 %************************************************************************
46 %*                                                                      *
47 \subsection{The external interface}
48 %*                                                                      *
49 %************************************************************************
50
51 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
52 defined to use this.  @pprParendType@ is the same, except it puts
53 parens around the type, except for the atomic cases.  @pprParendType@
54 works just by setting the initial context precedence very high.
55
56 \begin{code}
57 pprType, pprParendType :: Type -> SDoc
58 pprType       ty = ppr_ty pprTyEnv tOP_PREC   ty
59 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
60
61 pprKind, pprParendKind :: Kind -> SDoc
62 pprKind       = pprType
63 pprParendKind = pprParendType
64
65 pprPred :: PredType -> SDoc
66 pprPred (Class clas tys) = pprConstraint clas tys
67 pprPred (IParam n ty)    = hsep [ptext SLIT("?") <> ppr n,
68                                  ptext SLIT("::"), ppr ty]
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 pprPred theta)))
75
76 instance Outputable Type where
77     ppr ty = pprType ty
78
79 instance Outputable PredType where
80     ppr = pprPred
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)  -- type   in ParseIface.y
103 fUN_PREC    = (1 :: Int)  -- btype  in ParseIface.y
104 tYCON_PREC  = (2 :: Int)  -- atype  in ParseIface.y
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 (getOccName bx)   -- Always unqualified
124         other          -> maybeParen ctxt_prec tYCON_PREC 
125                                      (sep [ppr tycon, nest 4 tys_w_spaces])
126
127         -- USAGE CASE
128   | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey) && n_tys == 0
129   =     -- For usages (! and .), always print bare OccName, without pkg/mod/uniq
130     ppr (getOccName (tyConName tycon))
131         
132         -- TUPLE CASE (boxed and unboxed)
133   |  isTupleTyCon tycon
134   && length tys == tyConArity tycon     -- no magic if partially applied
135   = parens tys_w_commas
136
137   |  isUnboxedTupleTyCon tycon
138   && length tys == tyConArity tycon     -- no magic if partially applied
139   = parens (char '#' <+> tys_w_commas <+> char '#')
140
141         -- LIST CASE
142   | tycon `hasKey` listTyConKey && n_tys == 1
143   = brackets (ppr_ty env tOP_PREC ty1)
144
145         -- DICTIONARY CASE, prints {C a}
146         -- This means that instance decls come out looking right in interfaces
147         -- and that in turn means they get "gated" correctly when being slurped in
148   | maybeToBool maybe_pred
149   = braces (ppr_pred env pred)
150
151         -- NO-ARGUMENT CASE (=> no parens)
152   | null tys
153   = ppr tycon
154
155         -- GENERAL CASE
156   | otherwise
157   = maybeParen ctxt_prec tYCON_PREC (sep [ppr tycon, nest 4 tys_w_spaces])
158
159   where
160     n_tys      = length tys
161     (ty1:_)    = tys
162     Just pred  = maybe_pred
163     maybe_pred = splitPredTy_maybe ty   -- Checks class and arity
164     tys_w_commas = sep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
165     tys_w_spaces = sep (map (ppr_ty env tYCON_PREC) tys)
166   
167
168
169 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
170   = getPprStyle $ \ sty -> 
171     maybeParen ctxt_prec fUN_PREC $
172     sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."), 
173           ppr_theta theta,
174           ppr_ty env tOP_PREC tau
175     ]
176  where          
177     (tyvars, rho) = splitForAllTys ty
178     (theta, tau)  = splitRhoTy rho
179     
180     pp_tyvars sty = hsep (map (pBndr env LambdaBind) some_tyvars)
181       where
182         some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
183                     = filter (not . isUTyVar) tyvars  -- hide uvars from user
184                     | otherwise
185                     = tyvars
186     
187     ppr_theta []        = empty
188     ppr_theta theta     = parens (hsep (punctuate comma (map (ppr_pred env) theta))) 
189                           <+> ptext SLIT("=>")
190
191
192 ppr_ty env ctxt_prec (FunTy ty1 ty2)
193   -- we don't want to lose usage annotations or synonyms,
194   -- so we mustn't use splitFunTys here.
195   = maybeParen ctxt_prec fUN_PREC $
196     sep [ ppr_ty env fUN_PREC ty1
197         , ptext SLIT("->") <+> ppr_ty env tOP_PREC ty2
198         ]
199
200 ppr_ty env ctxt_prec (AppTy ty1 ty2)
201   = maybeParen ctxt_prec tYCON_PREC $
202     ppr_ty env fUN_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
203
204 ppr_ty env ctxt_prec (UsageTy u ty)
205   = maybeParen ctxt_prec tYCON_PREC $
206     ptext SLIT("__u") <+> ppr_ty env tYCON_PREC u
207                       <+> ppr_ty env tYCON_PREC ty
208     -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
209
210 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
211   = ppr_ty env ctxt_prec ty
212 --  = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
213
214 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
215
216 ppr_ty env ctxt_prec (PredTy p) = braces (ppr_pred env p)
217
218 ppr_pred env (Class clas tys) = ppr clas <+>
219                                 hsep (map (ppr_ty env tYCON_PREC) tys)
220 ppr_pred env (IParam n ty)    = hsep [char '?' <> ppr n, text "::",
221                                       ppr_ty env tYCON_PREC ty]
222 \end{code}
223
224 \begin{code}
225 pprTyEnv = initPprEnv b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
226   where
227     b = panic "PprType:init_ppr_env"
228 \end{code}
229
230
231 %************************************************************************
232 %*                                                                      *
233 \subsection[TyVar]{@TyVar@}
234 %*                                                                      *
235 %************************************************************************
236
237 We print type-variable binders with their kinds in interface files,
238 and when in debug mode.
239
240 \begin{code}
241 pprTyVarBndr tyvar
242   = getPprStyle $ \ sty ->
243     if (ifaceStyle sty  && kind /= boxedTypeKind) || debugStyle sty then
244         hsep [ppr tyvar, dcolon, pprParendKind kind]
245                 -- See comments with ppDcolon in PprCore.lhs
246     else
247         ppr tyvar
248   where
249     kind = tyVarKind tyvar
250
251 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
252 \end{code}
253
254
255 %************************************************************************
256 %*                                                                      *
257 \subsection{Mumbo jumbo}
258 %*                                                                      *
259 %************************************************************************
260
261 Grab a name for the type. This is used to determine the type
262 description for profiling.
263
264 \begin{code}
265 getTyDescription :: Type -> String
266
267 getTyDescription ty
268   = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
269     case tau_ty of
270       TyVarTy _        -> "*"
271       AppTy fun _      -> getTyDescription fun
272       FunTy _ res      -> '-' : '>' : fun_result res
273       TyConApp tycon _ -> getOccString tycon
274       NoteTy (FTVNote _) ty  -> getTyDescription ty
275       NoteTy (SynNote ty1) _ -> getTyDescription ty1
276       PredTy p               -> getTyDescription (predRepTy p)
277       ForAllTy _ ty    -> getTyDescription ty
278     }
279   where
280     fun_result (FunTy _ res) = '>' : fun_result res
281     fun_result other         = getTyDescription other
282 \end{code}
283
284
285 \begin{code}
286 showTypeCategory :: Type -> Char
287   {-
288         {C,I,F,D}   char, int, float, double
289         T           tuple
290         S           other single-constructor type
291         {c,i,f,d}   unboxed ditto
292         t           *unpacked* tuple
293         s           *unpacked" single-cons...
294
295         v           void#
296         a           primitive array
297
298         E           enumeration type
299         +           dictionary, unless it's a ...
300         L           List
301         >           function
302         M           other (multi-constructor) data-con type
303         .           other type
304         -           reserved for others to mark as "uninteresting"
305     -}
306 showTypeCategory ty
307   = if isDictTy ty
308     then '+'
309     else
310       case splitTyConApp_maybe ty of
311         Nothing -> if maybeToBool (splitFunTy_maybe ty)
312                    then '>'
313                    else '.'
314
315         Just (tycon, _) ->
316           let utc = getUnique tycon in
317           if      utc == charDataConKey    then 'C'
318           else if utc == intDataConKey     then 'I'
319           else if utc == floatDataConKey   then 'F'
320           else if utc == doubleDataConKey  then 'D'
321           else if utc == smallIntegerDataConKey ||
322                   utc == largeIntegerDataConKey   then 'J'
323           else if utc == charPrimTyConKey  then 'c'
324           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
325                 || utc == addrPrimTyConKey)                then 'i'
326           else if utc  == floatPrimTyConKey                then 'f'
327           else if utc  == doublePrimTyConKey               then 'd'
328           else if isPrimTyCon tycon {- array, we hope -}   then 'A'
329           else if isEnumerationTyCon tycon                 then 'E'
330           else if isTupleTyCon tycon                       then 'T'
331           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
332           else if utc == listTyConKey                      then 'L'
333           else 'M' -- oh, well...
334 \end{code}