4a04bffb5528f5923dabfa47ed0fc0edf17d7071
[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(..), Kind  ) -- friend
22 import Type             ( SourceType(..) ) 
23 import TcType           ( ThetaType, PredType, TyThing(..),
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, tyConArity
31                         )
32
33 -- others:
34 import Maybes           ( maybeToBool )
35 import Name             ( getOccString, getOccName )
36 import OccName          ( occNameUserString )
37 import Outputable
38 import Unique           ( Uniquable(..) )
39 import Util             ( lengthIs )
40 import BasicTypes       ( IPName(..), tupleParens, ipNameName )
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 = pprSourceType
66
67 pprSourceType :: SourceType -> SDoc
68 pprSourceType (ClassP clas tys) = pprClassPred clas tys
69 pprSourceType (IParam n ty)     = hsep [ppr n, dcolon, ppr ty]
70 pprSourceType (NType tc tys)    = ppr tc <+> sep (map pprParendType tys)
71
72 pprClassPred :: Class -> [Type] -> SDoc
73 pprClassPred clas tys = ppr clas <+> sep (map pprParendType tys)
74
75 pprTheta :: ThetaType -> SDoc
76 pprTheta theta = parens (sep (punctuate comma (map pprPred theta)))
77
78 instance Outputable Type where
79     ppr ty = pprType ty
80
81 instance Outputable SourceType where
82     ppr = pprPred
83
84 instance Outputable name => Outputable (IPName name) where
85     ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
86     ppr (Linear  n) = char '%' <> ppr n -- Splittable implicit parameters
87
88 instance Outputable name => OutputableBndr (IPName name) where
89     pprBndr _ n = ppr n -- Simple for now
90
91 instance Outputable TyThing where
92   ppr (AnId   id)   = ptext SLIT("AnId")     <+> ppr id
93   ppr (ATyCon tc)   = ptext SLIT("ATyCon")   <+> ppr tc
94   ppr (AClass cl)   = ptext SLIT("AClass")   <+> ppr cl
95   ppr (ADataCon dc) = ptext SLIT("ADataCon") <+> ppr dc
96 \end{code}
97
98
99 %************************************************************************
100 %*                                                                      *
101 \subsection{Pretty printing}
102 %*                                                                      *
103 %************************************************************************
104
105 Precedence
106 ~~~~~~~~~~
107 @ppr_ty@ takes an @Int@ that is the precedence of the context.
108 The precedence levels are:
109 \begin{description}
110 \item[tOP_PREC]   No parens required.
111 \item[fUN_PREC]   Left hand argument of a function arrow.
112 \item[tYCON_PREC] Argument of a type constructor.
113 \end{description}
114
115
116 \begin{code}
117 tOP_PREC    = (0 :: Int)  -- type   in ParseIface.y
118 fUN_PREC    = (1 :: Int)  -- btype  in ParseIface.y
119 tYCON_PREC  = (2 :: Int)  -- atype  in ParseIface.y
120
121 maybeParen ctxt_prec inner_prec pretty
122   | ctxt_prec < inner_prec = pretty
123   | otherwise              = parens pretty
124 \end{code}
125
126 \begin{code}
127 ppr_ty :: Int -> Type -> SDoc
128 ppr_ty ctxt_prec (TyVarTy tyvar)
129   = ppr tyvar
130
131 ppr_ty ctxt_prec ty@(TyConApp tycon tys)
132         -- KIND CASE; it's of the form (Type x)
133   | tycon `hasKey` typeConKey,
134     [ty] <- tys
135   =     -- For kinds, print (Type x) as just x if x is a 
136         --      type constructor (must be Boxed, Unboxed, AnyBox)
137         -- Otherwise print as (Type x)
138     case ty of
139         TyConApp bx [] -> ppr (getOccName bx)   -- Always unqualified
140         other          -> maybeParen ctxt_prec tYCON_PREC 
141                                      (ppr tycon <+> ppr_ty tYCON_PREC ty)
142
143         -- TUPLE CASE (boxed and unboxed)
144   |  isTupleTyCon tycon,
145       tys `lengthIs` tyConArity tycon   -- No magic if partially applied
146   = tupleParens (tupleTyConBoxity tycon)
147                 (sep (punctuate comma (map (ppr_ty tOP_PREC) tys)))
148
149         -- LIST CASE
150   | tycon `hasKey` listTyConKey,
151     [ty] <- tys
152   = brackets (ppr_ty tOP_PREC ty)
153
154         -- PARALLEL ARRAY CASE
155   | tycon `hasKey` parrTyConKey,
156     [ty] <- tys
157   = pabrackets (ppr_ty tOP_PREC ty)
158
159         -- GENERAL CASE
160   | otherwise
161   = ppr_tc_app ctxt_prec tycon tys
162
163   where
164     pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
165
166
167 ppr_ty ctxt_prec ty@(ForAllTy _ _)
168   = getPprStyle $ \ sty -> 
169     maybeParen ctxt_prec fUN_PREC $
170     sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."), 
171           ppr_theta theta,
172           ppr_ty tOP_PREC tau
173     ]
174  where          
175     (tyvars, theta, tau) = tcSplitSigmaTy ty
176     pp_tyvars sty        = sep (map pprTyVarBndr tyvars)
177     
178     ppr_theta []     = empty
179     ppr_theta theta  = pprTheta theta <+> ptext SLIT("=>")
180
181
182 ppr_ty ctxt_prec (FunTy ty1 ty2)
183   -- we don't want to lose usage annotations or synonyms,
184   -- so we mustn't use splitFunTys here.
185   = maybeParen ctxt_prec fUN_PREC $
186     sep [ ppr_ty fUN_PREC ty1
187         , ptext arrow <+> ppr_ty tOP_PREC ty2
188         ]
189   where arrow | isPredTy ty1 = SLIT("=>")
190               | otherwise    = SLIT("->")
191
192 ppr_ty ctxt_prec (AppTy ty1 ty2)
193   = maybeParen ctxt_prec tYCON_PREC $
194     ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
195
196 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
197   = ppr_ty ctxt_prec ty
198 --  = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
199
200 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
201
202 ppr_ty ctxt_prec (SourceTy (NType tc tys)) = ppr_tc_app ctxt_prec tc tys
203 ppr_ty ctxt_prec (SourceTy pred)           = braces (pprPred pred)
204
205 ppr_tc_app ctxt_prec tc []  = ppr tc
206 ppr_tc_app ctxt_prec tc tys = maybeParen ctxt_prec tYCON_PREC 
207                                          (sep [ppr tc, nest 4 (sep (map (ppr_ty tYCON_PREC) tys))])
208 \end{code}
209
210
211 %************************************************************************
212 %*                                                                      *
213 \subsection[TyVar]{@TyVar@}
214 %*                                                                      *
215 %************************************************************************
216
217 We print type-variable binders with their kinds in interface files,
218 and when in debug mode.
219
220 \begin{code}
221 pprTyVarBndr :: TyVar -> SDoc
222 pprTyVarBndr tyvar
223   = getPprStyle $ \ sty ->
224     if debugStyle sty then
225         hsep [ppr tyvar, dcolon, pprParendKind kind]
226                 -- See comments with ppDcolon in PprCore.lhs
227     else
228         ppr tyvar
229   where
230     kind = tyVarKind tyvar
231
232 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
233 \end{code}
234
235
236 %************************************************************************
237 %*                                                                      *
238 \subsection{Mumbo jumbo}
239 %*                                                                      *
240 %************************************************************************
241
242 Grab a name for the type. This is used to determine the type
243 description for profiling.
244
245 \begin{code}
246 getTyDescription :: Type -> String
247
248 getTyDescription ty
249   = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
250     case tau_ty of
251       TyVarTy _              -> "*"
252       AppTy fun _            -> getTyDescription fun
253       FunTy _ res            -> '-' : '>' : fun_result res
254       TyConApp tycon _       -> occNameUserString (getOccName tycon)
255       NoteTy (FTVNote _) ty  -> getTyDescription ty
256       NoteTy (SynNote ty1) _ -> getTyDescription ty1
257       SourceTy sty           -> getSourceTyDescription sty
258       ForAllTy _ ty          -> getTyDescription ty
259     }
260   where
261     fun_result (FunTy _ res) = '>' : fun_result res
262     fun_result other         = getTyDescription other
263
264 getSourceTyDescription (ClassP cl tys) = getOccString cl
265 getSourceTyDescription (NType  tc tys) = getOccString tc
266 getSourceTyDescription (IParam ip ty)  = getOccString (ipNameName ip)
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 tcSplitTyConApp_maybe ty of
296         Nothing -> if maybeToBool (tcSplitFunTy_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'     -- Bogus
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}