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