[project @ 2001-01-25 17:54:24 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         pprPred, pprTheta, pprClassPred,
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, liftedTypeKind ) -- 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 Class            ( Class )
30 import TyCon            ( TyCon, isPrimTyCon, isTupleTyCon, tupleTyConBoxity,
31                           maybeTyConSingleCon, isEnumerationTyCon, 
32                           tyConArity, tyConName
33                         )
34
35 -- others:
36 import CmdLineOpts      ( opt_PprStyle_RawTypes )
37 import Maybes           ( maybeToBool )
38 import Name             ( getOccString, getOccName )
39 import Outputable
40 import Unique           ( Uniquable(..) )
41 import BasicTypes       ( tupleParens )
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 tOP_PREC   ty
59 pprParendType ty = ppr_ty 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) = pprClassPred clas tys
67 pprPred (IParam n ty)    = hsep [ptext SLIT("?") <> ppr n,
68                                  ptext SLIT("::"), ppr ty]
69
70 pprClassPred :: Class -> [Type] -> SDoc
71 pprClassPred 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 :: Int -> Type -> SDoc
113 ppr_ty ctxt_prec (TyVarTy tyvar)
114   = ppr tyvar
115
116 ppr_ty 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   = tupleParens (tupleTyConBoxity tycon) tys_w_commas
136
137         -- LIST CASE
138   | tycon `hasKey` listTyConKey && n_tys == 1
139   = brackets (ppr_ty 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 (pprPred 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 tOP_PREC) tys))
161     tys_w_spaces = sep (map (ppr_ty tYCON_PREC) tys)
162   
163
164
165 ppr_ty ctxt_prec ty@(ForAllTy _ _)
166   = getPprStyle $ \ sty -> 
167     maybeParen ctxt_prec fUN_PREC $
168     sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."), 
169           ppr_theta theta,
170           ppr_ty tOP_PREC tau
171     ]
172  where          
173     (tyvars, rho) = splitForAllTys ty
174     (theta, tau)  = splitRhoTy rho
175     
176     pp_tyvars sty = hsep (map pprTyVarBndr some_tyvars)
177       where
178         some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
179                     = filter (not . isUTyVar) tyvars  -- hide uvars from user
180                     | otherwise
181                     = tyvars
182     
183     ppr_theta []     = empty
184     ppr_theta theta  = pprTheta theta <+> ptext SLIT("=>")
185
186
187 ppr_ty ctxt_prec (FunTy ty1 ty2)
188   -- we don't want to lose usage annotations or synonyms,
189   -- so we mustn't use splitFunTys here.
190   = maybeParen ctxt_prec fUN_PREC $
191     sep [ ppr_ty fUN_PREC ty1
192         , ptext SLIT("->") <+> ppr_ty tOP_PREC ty2
193         ]
194
195 ppr_ty ctxt_prec (AppTy ty1 ty2)
196   = maybeParen ctxt_prec tYCON_PREC $
197     ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
198
199 ppr_ty ctxt_prec (UsageTy u ty)
200   = maybeParen ctxt_prec tYCON_PREC $
201     ptext SLIT("__u") <+> ppr_ty tYCON_PREC u
202                       <+> ppr_ty tYCON_PREC ty
203     -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
204
205 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
206   = ppr_ty ctxt_prec ty
207 --  = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
208
209 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
210
211 ppr_ty ctxt_prec (PredTy p) = braces (pprPred p)
212 \end{code}
213
214
215 %************************************************************************
216 %*                                                                      *
217 \subsection[TyVar]{@TyVar@}
218 %*                                                                      *
219 %************************************************************************
220
221 We print type-variable binders with their kinds in interface files,
222 and when in debug mode.
223
224 \begin{code}
225 pprTyVarBndr :: TyVar -> SDoc
226 pprTyVarBndr tyvar
227   = getPprStyle $ \ sty ->
228     if (ifaceStyle sty  && kind /= liftedTypeKind) || debugStyle sty then
229         hsep [ppr tyvar, dcolon, pprParendKind kind]
230                 -- See comments with ppDcolon in PprCore.lhs
231     else
232         ppr tyvar
233   where
234     kind = tyVarKind tyvar
235
236 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
237 \end{code}
238
239
240 %************************************************************************
241 %*                                                                      *
242 \subsection{Mumbo jumbo}
243 %*                                                                      *
244 %************************************************************************
245
246 Grab a name for the type. This is used to determine the type
247 description for profiling.
248
249 \begin{code}
250 getTyDescription :: Type -> String
251
252 getTyDescription ty
253   = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
254     case tau_ty of
255       TyVarTy _        -> "*"
256       AppTy fun _      -> getTyDescription fun
257       FunTy _ res      -> '-' : '>' : fun_result res
258       TyConApp tycon _ -> getOccString tycon
259       NoteTy (FTVNote _) ty  -> getTyDescription ty
260       NoteTy (SynNote ty1) _ -> getTyDescription ty1
261       PredTy p               -> getTyDescription (predRepTy p)
262       ForAllTy _ ty    -> getTyDescription ty
263     }
264   where
265     fun_result (FunTy _ res) = '>' : fun_result res
266     fun_result other         = getTyDescription other
267 \end{code}
268
269
270 \begin{code}
271 showTypeCategory :: Type -> Char
272   {-
273         {C,I,F,D}   char, int, float, double
274         T           tuple
275         S           other single-constructor type
276         {c,i,f,d}   unboxed ditto
277         t           *unpacked* tuple
278         s           *unpacked" single-cons...
279
280         v           void#
281         a           primitive array
282
283         E           enumeration type
284         +           dictionary, unless it's a ...
285         L           List
286         >           function
287         M           other (multi-constructor) data-con type
288         .           other type
289         -           reserved for others to mark as "uninteresting"
290     -}
291 showTypeCategory ty
292   = if isDictTy ty
293     then '+'
294     else
295       case splitTyConApp_maybe ty of
296         Nothing -> if maybeToBool (splitFunTy_maybe ty)
297                    then '>'
298                    else '.'
299
300         Just (tycon, _) ->
301           let utc = getUnique tycon in
302           if      utc == charDataConKey    then 'C'
303           else if utc == intDataConKey     then 'I'
304           else if utc == floatDataConKey   then 'F'
305           else if utc == doubleDataConKey  then 'D'
306           else if utc == smallIntegerDataConKey ||
307                   utc == largeIntegerDataConKey   then 'J'
308           else if utc == charPrimTyConKey  then 'c'
309           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
310                 || utc == addrPrimTyConKey)                then 'i'
311           else if utc  == floatPrimTyConKey                then 'f'
312           else if utc  == doublePrimTyConKey               then 'd'
313           else if isPrimTyCon tycon {- array, we hope -}   then 'A'
314           else if isEnumerationTyCon tycon                 then 'E'
315           else if isTupleTyCon tycon                       then 'T'
316           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
317           else if utc == listTyConKey                      then 'L'
318           else 'M' -- oh, well...
319 \end{code}