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