6c663034c088d676e1f4dfae5daf7cc8f905bf22
[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             ( SourceType(..), isUTyVar, eqKind )
23 import TcType           ( ThetaType, PredType, tcSplitPredTy_maybe, 
24                           tcSplitSigmaTy, isPredTy, isDictTy,
25                           tcSplitTyConApp_maybe, tcSplitFunTy_maybe
26                         ) 
27 import Var              ( TyVar, tyVarKind )
28 import Class            ( Class )
29 import TyCon            ( TyCon, isPrimTyCon, isTupleTyCon, tupleTyConBoxity,
30                           maybeTyConSingleCon, isEnumerationTyCon, 
31                           tyConArity, tyConName
32                         )
33
34 -- others:
35 import CmdLineOpts      ( opt_PprStyle_RawTypes )
36 import Maybes           ( maybeToBool )
37 import Name             ( getOccString, getOccName )
38 import Outputable
39 import Unique           ( Uniquable(..) )
40 import BasicTypes       ( tupleParens )
41 import PrelNames                -- quite a few *Keys
42 \end{code}
43
44 %************************************************************************
45 %*                                                                      *
46 \subsection{The external interface}
47 %*                                                                      *
48 %************************************************************************
49
50 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
51 defined to use this.  @pprParendType@ is the same, except it puts
52 parens around the type, except for the atomic cases.  @pprParendType@
53 works just by setting the initial context precedence very high.
54
55 \begin{code}
56 pprType, pprParendType :: Type -> SDoc
57 pprType       ty = ppr_ty tOP_PREC   ty
58 pprParendType ty = ppr_ty tYCON_PREC ty
59
60 pprKind, pprParendKind :: Kind -> SDoc
61 pprKind       = pprType
62 pprParendKind = pprParendType
63
64 pprPred :: PredType -> SDoc
65 pprPred (ClassP clas tys) = pprClassPred clas tys
66 pprPred (IParam n ty)     = hsep [ptext SLIT("?") <> ppr n,
67                                   ptext SLIT("::"), ppr ty]
68
69 pprClassPred :: Class -> [Type] -> SDoc
70 pprClassPred clas tys = ppr clas <+> hsep (map pprParendType tys)
71
72 pprTheta :: ThetaType -> SDoc
73 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
74
75 instance Outputable Type where
76     ppr ty = pprType ty
77
78 instance Outputable PredType where
79     ppr = pprPred
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)  -- type   in ParseIface.y
102 fUN_PREC    = (1 :: Int)  -- btype  in ParseIface.y
103 tYCON_PREC  = (2 :: Int)  -- atype  in ParseIface.y
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 :: Int -> Type -> SDoc
112 ppr_ty ctxt_prec (TyVarTy tyvar)
113   = ppr tyvar
114
115 ppr_ty ctxt_prec ty@(TyConApp tycon tys)
116         -- KIND CASE; it's of the form (Type x)
117   | tycon `hasKey` typeConKey,
118     [ty] <- tys
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 ty of
123         TyConApp bx [] -> ppr (getOccName bx)   -- Always unqualified
124         other          -> maybeParen ctxt_prec tYCON_PREC 
125                                      (ppr tycon <+> ppr_ty tYCON_PREC ty)
126
127         -- USAGE CASE
128   | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey),
129     null tys
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)
137                 (sep (punctuate comma (map (ppr_ty tOP_PREC) tys)))
138
139         -- LIST CASE
140   | tycon `hasKey` listTyConKey,
141     [ty] <- tys
142   = brackets (ppr_ty tOP_PREC ty)
143
144         -- GENERAL CASE
145   | otherwise
146   = ppr_tc_app ctxt_prec tycon tys
147
148
149 ppr_ty ctxt_prec ty@(ForAllTy _ _)
150   = getPprStyle $ \ sty -> 
151     maybeParen ctxt_prec fUN_PREC $
152     sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."), 
153           ppr_theta theta,
154           ppr_ty tOP_PREC tau
155     ]
156  where          
157     (tyvars, theta, tau) = tcSplitSigmaTy ty
158     
159     pp_tyvars sty = sep (map pprTyVarBndr some_tyvars)
160       where
161         some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
162                     = filter (not . isUTyVar) tyvars  -- hide uvars from user
163                     | otherwise
164                     = tyvars
165     
166     ppr_theta []     = empty
167     ppr_theta theta  = pprTheta theta <+> ptext SLIT("=>")
168
169
170 ppr_ty ctxt_prec (FunTy ty1 ty2)
171   -- we don't want to lose usage annotations or synonyms,
172   -- so we mustn't use splitFunTys here.
173   = maybeParen ctxt_prec fUN_PREC $
174     sep [ ppr_ty fUN_PREC ty1
175         , ptext arrow <+> ppr_ty tOP_PREC ty2
176         ]
177   where arrow | isPredTy ty1 = SLIT("=>")
178               | otherwise    = SLIT("->")
179
180 ppr_ty ctxt_prec (AppTy ty1 ty2)
181   = maybeParen ctxt_prec tYCON_PREC $
182     ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
183
184 ppr_ty ctxt_prec (UsageTy u ty)
185   = maybeParen ctxt_prec tYCON_PREC $
186     ptext SLIT("__u") <+> ppr_ty tYCON_PREC u
187                       <+> ppr_ty tYCON_PREC ty
188     -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
189
190 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
191   = ppr_ty ctxt_prec ty
192 --  = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
193
194 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
195
196 ppr_ty ctxt_prec (SourceTy (NType tc tys))
197   =  ppr_tc_app ctxt_prec tc tys
198
199 ppr_ty ctxt_prec (SourceTy pred) = braces (pprPred pred)
200
201 ppr_tc_app ctxt_prec tc []  = ppr tc
202 ppr_tc_app ctxt_prec tc tys = maybeParen ctxt_prec tYCON_PREC 
203                                          (sep [ppr tc, nest 4 (sep (map (ppr_ty tYCON_PREC) tys))])
204 \end{code}
205
206
207 %************************************************************************
208 %*                                                                      *
209 \subsection[TyVar]{@TyVar@}
210 %*                                                                      *
211 %************************************************************************
212
213 We print type-variable binders with their kinds in interface files,
214 and when in debug mode.
215
216 \begin{code}
217 pprTyVarBndr :: TyVar -> SDoc
218 pprTyVarBndr tyvar
219   = getPprStyle $ \ sty ->
220     if (ifaceStyle sty  && not (kind `eqKind` liftedTypeKind)) || debugStyle sty then
221         hsep [ppr tyvar, dcolon, pprParendKind kind]
222                 -- See comments with ppDcolon in PprCore.lhs
223     else
224         ppr tyvar
225   where
226     kind = tyVarKind tyvar
227
228 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
229 \end{code}
230
231
232 %************************************************************************
233 %*                                                                      *
234 \subsection{Mumbo jumbo}
235 %*                                                                      *
236 %************************************************************************
237
238 Grab a name for the type. This is used to determine the type
239 description for profiling.
240
241 \begin{code}
242 getTyDescription :: Type -> String
243
244 getTyDescription ty
245   = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
246     case tau_ty of
247       TyVarTy _              -> "*"
248       AppTy fun _            -> getTyDescription fun
249       FunTy _ res            -> '-' : '>' : fun_result res
250       TyConApp tycon _       -> getOccString tycon
251       NoteTy (FTVNote _) ty  -> getTyDescription ty
252       NoteTy (SynNote ty1) _ -> getTyDescription ty1
253       SourceTy sty           -> getSourceTyDescription sty
254       ForAllTy _ ty          -> getTyDescription ty
255     }
256   where
257     fun_result (FunTy _ res) = '>' : fun_result res
258     fun_result other         = getTyDescription other
259
260 getSourceTyDescription (ClassP cl tys) = getOccString cl
261 getSourceTyDescription (NType  tc tys) = getOccString tc
262 getSourceTyDescription (IParam id ty)  = getOccString id
263 \end{code}
264
265
266 \begin{code}
267 showTypeCategory :: Type -> Char
268   {-
269         {C,I,F,D}   char, int, float, double
270         T           tuple
271         S           other single-constructor type
272         {c,i,f,d}   unboxed ditto
273         t           *unpacked* tuple
274         s           *unpacked" single-cons...
275
276         v           void#
277         a           primitive array
278
279         E           enumeration type
280         +           dictionary, unless it's a ...
281         L           List
282         >           function
283         M           other (multi-constructor) data-con type
284         .           other type
285         -           reserved for others to mark as "uninteresting"
286     -}
287 showTypeCategory ty
288   = if isDictTy ty
289     then '+'
290     else
291       case tcSplitTyConApp_maybe ty of
292         Nothing -> if maybeToBool (tcSplitFunTy_maybe ty)
293                    then '>'
294                    else '.'
295
296         Just (tycon, _) ->
297           let utc = getUnique tycon in
298           if      utc == charDataConKey    then 'C'
299           else if utc == intDataConKey     then 'I'
300           else if utc == floatDataConKey   then 'F'
301           else if utc == doubleDataConKey  then 'D'
302           else if utc == smallIntegerDataConKey ||
303                   utc == largeIntegerDataConKey   then 'J'
304           else if utc == charPrimTyConKey  then 'c'
305           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
306                 || utc == addrPrimTyConKey)                then 'i'
307           else if utc  == floatPrimTyConKey                then 'f'
308           else if utc  == doublePrimTyConKey               then 'd'
309           else if isPrimTyCon tycon {- array, we hope -}   then 'A'     -- Bogus
310           else if isEnumerationTyCon tycon                 then 'E'
311           else if isTupleTyCon tycon                       then 'T'
312           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
313           else if utc == listTyConKey                      then 'L'
314           else 'M' -- oh, well...
315 \end{code}