[project @ 2001-11-26 09:20:25 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         pprSourceType, 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(..), IPName(..), 
22                           Kind, liftedTypeKind ) -- friend
23 import Type             ( SourceType(..), isUTyVar, eqKind )
24 import TcType           ( ThetaType, PredType, ipNameName,
25                           tcSplitSigmaTy, isPredTy, isDictTy,
26                           tcSplitTyConApp_maybe, tcSplitFunTy_maybe
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 Util             ( lengthIs )
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 tOP_PREC   ty
60 pprParendType ty = ppr_ty tYCON_PREC ty
61
62 pprKind, pprParendKind :: Kind -> SDoc
63 pprKind       = pprType
64 pprParendKind = pprParendType
65
66 pprPred :: PredType -> SDoc
67 pprPred = pprSourceType
68
69 pprSourceType :: SourceType -> SDoc
70 pprSourceType (ClassP clas tys) = pprClassPred clas tys
71 pprSourceType (IParam n ty)     = hsep [ppr n, dcolon, ppr ty]
72 pprSourceType (NType tc tys)    = ppr tc <+> hsep (map pprParendType tys)
73
74 pprClassPred :: Class -> [Type] -> SDoc
75 pprClassPred clas tys = ppr clas <+> hsep (map pprParendType tys)
76
77 pprTheta :: ThetaType -> SDoc
78 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
79
80 instance Outputable Type where
81     ppr ty = pprType ty
82
83 instance Outputable SourceType where
84     ppr = pprPred
85
86 instance Outputable name => Outputable (IPName name) where
87     ppr (Dupable n)   = char '?' <> ppr n -- Ordinary implicit parameters
88     ppr (MustSplit n) = char '%' <> ppr n -- Splittable implicit parameters
89 \end{code}
90
91
92 %************************************************************************
93 %*                                                                      *
94 \subsection{Pretty printing}
95 %*                                                                      *
96 %************************************************************************
97
98 Precedence
99 ~~~~~~~~~~
100 @ppr_ty@ takes an @Int@ that is the precedence of the context.
101 The precedence levels are:
102 \begin{description}
103 \item[tOP_PREC]   No parens required.
104 \item[fUN_PREC]   Left hand argument of a function arrow.
105 \item[tYCON_PREC] Argument of a type constructor.
106 \end{description}
107
108
109 \begin{code}
110 tOP_PREC    = (0 :: Int)  -- type   in ParseIface.y
111 fUN_PREC    = (1 :: Int)  -- btype  in ParseIface.y
112 tYCON_PREC  = (2 :: Int)  -- atype  in ParseIface.y
113
114 maybeParen ctxt_prec inner_prec pretty
115   | ctxt_prec < inner_prec = pretty
116   | otherwise              = parens pretty
117 \end{code}
118
119 \begin{code}
120 ppr_ty :: Int -> Type -> SDoc
121 ppr_ty ctxt_prec (TyVarTy tyvar)
122   = ppr tyvar
123
124 ppr_ty ctxt_prec ty@(TyConApp tycon tys)
125         -- KIND CASE; it's of the form (Type x)
126   | tycon `hasKey` typeConKey,
127     [ty] <- tys
128   =     -- For kinds, print (Type x) as just x if x is a 
129         --      type constructor (must be Boxed, Unboxed, AnyBox)
130         -- Otherwise print as (Type x)
131     case ty of
132         TyConApp bx [] -> ppr (getOccName bx)   -- Always unqualified
133         other          -> maybeParen ctxt_prec tYCON_PREC 
134                                      (ppr tycon <+> ppr_ty tYCON_PREC ty)
135
136         -- USAGE CASE
137   | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey),
138     null tys
139   =     -- For usages (! and .), always print bare OccName, without pkg/mod/uniq
140     ppr (getOccName (tyConName tycon))
141         
142         -- TUPLE CASE (boxed and unboxed)
143   |  isTupleTyCon tycon,
144       tys `lengthIs` tyConArity tycon   -- No magic if partially applied
145   = tupleParens (tupleTyConBoxity tycon)
146                 (sep (punctuate comma (map (ppr_ty tOP_PREC) tys)))
147
148         -- LIST CASE
149   | tycon `hasKey` listTyConKey,
150     [ty] <- tys
151   = brackets (ppr_ty tOP_PREC ty)
152
153         -- GENERAL CASE
154   | otherwise
155   = ppr_tc_app ctxt_prec tycon tys
156
157
158 ppr_ty ctxt_prec ty@(ForAllTy _ _)
159   = getPprStyle $ \ sty -> 
160     maybeParen ctxt_prec fUN_PREC $
161     sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."), 
162           ppr_theta theta,
163           ppr_ty tOP_PREC tau
164     ]
165  where          
166     (tyvars, theta, tau) = tcSplitSigmaTy ty
167     
168     pp_tyvars sty = sep (map pprTyVarBndr some_tyvars)
169       where
170         some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
171                     = filter (not . isUTyVar) tyvars  -- hide uvars from user
172                     | otherwise
173                     = tyvars
174     
175     ppr_theta []     = empty
176     ppr_theta theta  = pprTheta theta <+> ptext SLIT("=>")
177
178
179 ppr_ty ctxt_prec (FunTy ty1 ty2)
180   -- we don't want to lose usage annotations or synonyms,
181   -- so we mustn't use splitFunTys here.
182   = maybeParen ctxt_prec fUN_PREC $
183     sep [ ppr_ty fUN_PREC ty1
184         , ptext arrow <+> ppr_ty tOP_PREC ty2
185         ]
186   where arrow | isPredTy ty1 = SLIT("=>")
187               | otherwise    = SLIT("->")
188
189 ppr_ty ctxt_prec (AppTy ty1 ty2)
190   = maybeParen ctxt_prec tYCON_PREC $
191     ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
192
193 ppr_ty ctxt_prec (UsageTy u ty)
194   = maybeParen ctxt_prec tYCON_PREC $
195     ptext SLIT("__u") <+> ppr_ty tYCON_PREC u
196                       <+> ppr_ty tYCON_PREC ty
197     -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
198
199 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
200   = ppr_ty ctxt_prec ty
201 --  = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
202
203 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
204
205 ppr_ty ctxt_prec (SourceTy (NType tc tys)) = ppr_tc_app ctxt_prec tc tys
206 ppr_ty ctxt_prec (SourceTy pred)           = braces (pprPred pred)
207
208 ppr_tc_app ctxt_prec tc []  = ppr tc
209 ppr_tc_app ctxt_prec tc tys = maybeParen ctxt_prec tYCON_PREC 
210                                          (sep [ppr tc, nest 4 (sep (map (ppr_ty tYCON_PREC) tys))])
211 \end{code}
212
213
214 %************************************************************************
215 %*                                                                      *
216 \subsection[TyVar]{@TyVar@}
217 %*                                                                      *
218 %************************************************************************
219
220 We print type-variable binders with their kinds in interface files,
221 and when in debug mode.
222
223 \begin{code}
224 pprTyVarBndr :: TyVar -> SDoc
225 pprTyVarBndr tyvar
226   = getPprStyle $ \ sty ->
227     if (ifaceStyle sty  && not (kind `eqKind` liftedTypeKind)) || debugStyle sty then
228         hsep [ppr tyvar, dcolon, pprParendKind kind]
229                 -- See comments with ppDcolon in PprCore.lhs
230     else
231         ppr tyvar
232   where
233     kind = tyVarKind tyvar
234
235 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
236 \end{code}
237
238
239 %************************************************************************
240 %*                                                                      *
241 \subsection{Mumbo jumbo}
242 %*                                                                      *
243 %************************************************************************
244
245 Grab a name for the type. This is used to determine the type
246 description for profiling.
247
248 \begin{code}
249 getTyDescription :: Type -> String
250
251 getTyDescription ty
252   = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
253     case tau_ty of
254       TyVarTy _              -> "*"
255       AppTy fun _            -> getTyDescription fun
256       FunTy _ res            -> '-' : '>' : fun_result res
257       TyConApp tycon _       -> getOccString tycon
258       NoteTy (FTVNote _) ty  -> getTyDescription ty
259       NoteTy (SynNote ty1) _ -> getTyDescription ty1
260       SourceTy sty           -> getSourceTyDescription sty
261       ForAllTy _ ty          -> getTyDescription ty
262     }
263   where
264     fun_result (FunTy _ res) = '>' : fun_result res
265     fun_result other         = getTyDescription other
266
267 getSourceTyDescription (ClassP cl tys) = getOccString cl
268 getSourceTyDescription (NType  tc tys) = getOccString tc
269 getSourceTyDescription (IParam ip ty)  = getOccString (ipNameName ip)
270 \end{code}
271
272
273 \begin{code}
274 showTypeCategory :: Type -> Char
275   {-
276         {C,I,F,D}   char, int, float, double
277         T           tuple
278         S           other single-constructor type
279         {c,i,f,d}   unboxed ditto
280         t           *unpacked* tuple
281         s           *unpacked" single-cons...
282
283         v           void#
284         a           primitive array
285
286         E           enumeration type
287         +           dictionary, unless it's a ...
288         L           List
289         >           function
290         M           other (multi-constructor) data-con type
291         .           other type
292         -           reserved for others to mark as "uninteresting"
293     -}
294 showTypeCategory ty
295   = if isDictTy ty
296     then '+'
297     else
298       case tcSplitTyConApp_maybe ty of
299         Nothing -> if maybeToBool (tcSplitFunTy_maybe ty)
300                    then '>'
301                    else '.'
302
303         Just (tycon, _) ->
304           let utc = getUnique tycon in
305           if      utc == charDataConKey    then 'C'
306           else if utc == intDataConKey     then 'I'
307           else if utc == floatDataConKey   then 'F'
308           else if utc == doubleDataConKey  then 'D'
309           else if utc == smallIntegerDataConKey ||
310                   utc == largeIntegerDataConKey   then 'J'
311           else if utc == charPrimTyConKey  then 'c'
312           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
313                 || utc == addrPrimTyConKey)                then 'i'
314           else if utc  == floatPrimTyConKey                then 'f'
315           else if utc  == doublePrimTyConKey               then 'd'
316           else if isPrimTyCon tycon {- array, we hope -}   then 'A'     -- Bogus
317           else if isEnumerationTyCon tycon                 then 'E'
318           else if isTupleTyCon tycon                       then 'T'
319           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
320           else if utc == listTyConKey                      then 'L'
321           else 'M' -- oh, well...
322 \end{code}