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