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