[project @ 2002-10-11 14:46:02 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(..), Kind  ) -- friend
22 import Type             ( SourceType(..), TyThing(..) )
23 import TcType           ( ThetaType, PredType,
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 Maybes           ( maybeToBool )
36 import Name             ( getOccString, getOccName )
37 import OccName          ( occNameUserString )
38 import Outputable
39 import Unique           ( Uniquable(..) )
40 import Util             ( lengthIs )
41 import BasicTypes       ( IPName(..), tupleParens, ipNameName )
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 = pprSourceType
67
68 pprSourceType :: SourceType -> SDoc
69 pprSourceType (ClassP clas tys) = pprClassPred clas tys
70 pprSourceType (IParam n ty)     = hsep [ppr n, dcolon, ppr ty]
71 pprSourceType (NType tc tys)    = ppr tc <+> sep (map pprParendType tys)
72
73 pprClassPred :: Class -> [Type] -> SDoc
74 pprClassPred clas tys = ppr clas <+> sep (map pprParendType tys)
75
76 pprTheta :: ThetaType -> SDoc
77 pprTheta theta = parens (sep (punctuate comma (map pprPred theta)))
78
79 instance Outputable Type where
80     ppr ty = pprType ty
81
82 instance Outputable SourceType where
83     ppr = pprPred
84
85 instance Outputable name => Outputable (IPName name) where
86     ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
87     ppr (Linear  n) = char '%' <> ppr n -- Splittable implicit parameters
88
89 instance Outputable name => OutputableBndr (IPName name) where
90     pprBndr _ n = ppr n -- Simple for now
91
92 instance Outputable TyThing where
93   ppr (AnId   id) = ptext SLIT("AnId")   <+> ppr id
94   ppr (ATyCon tc) = ptext SLIT("ATyCon") <+> ppr tc
95   ppr (AClass cl) = ptext SLIT("AClass") <+> ppr cl
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         -- USAGE CASE
144   | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey),
145     null tys
146   =     -- For usages (! and .), always print bare OccName, without pkg/mod/uniq
147     ppr (getOccName (tyConName tycon))
148         
149         -- TUPLE CASE (boxed and unboxed)
150   |  isTupleTyCon tycon,
151       tys `lengthIs` tyConArity tycon   -- No magic if partially applied
152   = tupleParens (tupleTyConBoxity tycon)
153                 (sep (punctuate comma (map (ppr_ty tOP_PREC) tys)))
154
155         -- LIST CASE
156   | tycon `hasKey` listTyConKey,
157     [ty] <- tys
158   = brackets (ppr_ty tOP_PREC ty)
159
160         -- PARALLEL ARRAY CASE
161   | tycon `hasKey` parrTyConKey,
162     [ty] <- tys
163   = pabrackets (ppr_ty tOP_PREC ty)
164
165         -- GENERAL CASE
166   | otherwise
167   = ppr_tc_app ctxt_prec tycon tys
168
169   where
170     pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
171
172
173 ppr_ty ctxt_prec ty@(ForAllTy _ _)
174   = getPprStyle $ \ sty -> 
175     maybeParen ctxt_prec fUN_PREC $
176     sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."), 
177           ppr_theta theta,
178           ppr_ty tOP_PREC tau
179     ]
180  where          
181     (tyvars, theta, tau) = tcSplitSigmaTy ty
182     pp_tyvars sty        = sep (map pprTyVarBndr tyvars)
183     
184     ppr_theta []     = empty
185     ppr_theta theta  = pprTheta theta <+> ptext SLIT("=>")
186
187
188 ppr_ty ctxt_prec (FunTy ty1 ty2)
189   -- we don't want to lose usage annotations or synonyms,
190   -- so we mustn't use splitFunTys here.
191   = maybeParen ctxt_prec fUN_PREC $
192     sep [ ppr_ty fUN_PREC ty1
193         , ptext arrow <+> ppr_ty tOP_PREC ty2
194         ]
195   where arrow | isPredTy ty1 = SLIT("=>")
196               | otherwise    = SLIT("->")
197
198 ppr_ty ctxt_prec (AppTy ty1 ty2)
199   = maybeParen ctxt_prec tYCON_PREC $
200     ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
201
202 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
203   = ppr_ty ctxt_prec ty
204 --  = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
205
206 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
207
208 ppr_ty ctxt_prec (SourceTy (NType tc tys)) = ppr_tc_app ctxt_prec tc tys
209 ppr_ty ctxt_prec (SourceTy pred)           = braces (pprPred pred)
210
211 ppr_tc_app ctxt_prec tc []  = ppr tc
212 ppr_tc_app ctxt_prec tc tys = maybeParen ctxt_prec tYCON_PREC 
213                                          (sep [ppr tc, nest 4 (sep (map (ppr_ty tYCON_PREC) tys))])
214 \end{code}
215
216
217 %************************************************************************
218 %*                                                                      *
219 \subsection[TyVar]{@TyVar@}
220 %*                                                                      *
221 %************************************************************************
222
223 We print type-variable binders with their kinds in interface files,
224 and when in debug mode.
225
226 \begin{code}
227 pprTyVarBndr :: TyVar -> SDoc
228 pprTyVarBndr tyvar
229   = getPprStyle $ \ sty ->
230     if debugStyle sty then
231         hsep [ppr tyvar, dcolon, pprParendKind kind]
232                 -- See comments with ppDcolon in PprCore.lhs
233     else
234         ppr tyvar
235   where
236     kind = tyVarKind tyvar
237
238 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
239 \end{code}
240
241
242 %************************************************************************
243 %*                                                                      *
244 \subsection{Mumbo jumbo}
245 %*                                                                      *
246 %************************************************************************
247
248 Grab a name for the type. This is used to determine the type
249 description for profiling.
250
251 \begin{code}
252 getTyDescription :: Type -> String
253
254 getTyDescription ty
255   = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
256     case tau_ty of
257       TyVarTy _              -> "*"
258       AppTy fun _            -> getTyDescription fun
259       FunTy _ res            -> '-' : '>' : fun_result res
260       TyConApp tycon _       -> occNameUserString (getOccName tycon)
261       NoteTy (FTVNote _) ty  -> getTyDescription ty
262       NoteTy (SynNote ty1) _ -> getTyDescription ty1
263       SourceTy sty           -> getSourceTyDescription sty
264       ForAllTy _ ty          -> getTyDescription ty
265     }
266   where
267     fun_result (FunTy _ res) = '>' : fun_result res
268     fun_result other         = getTyDescription other
269
270 getSourceTyDescription (ClassP cl tys) = getOccString cl
271 getSourceTyDescription (NType  tc tys) = getOccString tc
272 getSourceTyDescription (IParam ip ty)  = getOccString (ipNameName ip)
273 \end{code}
274
275
276 \begin{code}
277 showTypeCategory :: Type -> Char
278   {-
279         {C,I,F,D}   char, int, float, double
280         T           tuple
281         S           other single-constructor type
282         {c,i,f,d}   unboxed ditto
283         t           *unpacked* tuple
284         s           *unpacked" single-cons...
285
286         v           void#
287         a           primitive array
288
289         E           enumeration type
290         +           dictionary, unless it's a ...
291         L           List
292         >           function
293         M           other (multi-constructor) data-con type
294         .           other type
295         -           reserved for others to mark as "uninteresting"
296     -}
297 showTypeCategory ty
298   = if isDictTy ty
299     then '+'
300     else
301       case tcSplitTyConApp_maybe ty of
302         Nothing -> if maybeToBool (tcSplitFunTy_maybe ty)
303                    then '>'
304                    else '.'
305
306         Just (tycon, _) ->
307           let utc = getUnique tycon in
308           if      utc == charDataConKey    then 'C'
309           else if utc == intDataConKey     then 'I'
310           else if utc == floatDataConKey   then 'F'
311           else if utc == doubleDataConKey  then 'D'
312           else if utc == smallIntegerDataConKey ||
313                   utc == largeIntegerDataConKey   then 'J'
314           else if utc == charPrimTyConKey  then 'c'
315           else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
316                 || utc == addrPrimTyConKey)                then 'i'
317           else if utc  == floatPrimTyConKey                then 'f'
318           else if utc  == doublePrimTyConKey               then 'd'
319           else if isPrimTyCon tycon {- array, we hope -}   then 'A'     -- Bogus
320           else if isEnumerationTyCon tycon                 then 'E'
321           else if isTupleTyCon tycon                       then 'T'
322           else if maybeToBool (maybeTyConSingleCon tycon)  then 'S'
323           else if utc == listTyConKey                      then 'L'
324           else 'M' -- oh, well...
325 \end{code}