2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[PprType]{Printing Types, TyVars, Classes, TyCons}
8 pprKind, pprParendKind,
9 pprType, pprParendType,
10 pprPred, pprTheta, pprClassPred,
11 pprTyVarBndr, pprTyVarBndrs,
14 getTyDescription, showTypeCategory
17 #include "HsVersions.h"
20 -- (PprType can see all the representations it's trying to print)
21 import TypeRep ( Type(..), TyNote(..), Kind, liftedTypeKind ) -- friend
22 import Type ( PredType(..), ThetaType,
24 splitForAllTys, splitSigmaTy, splitRhoTy,
25 isDictTy, splitTyConApp_maybe, splitFunTy_maybe,
28 import Var ( TyVar, tyVarKind )
29 import Class ( Class )
30 import TyCon ( TyCon, isPrimTyCon, isTupleTyCon, tupleTyConBoxity,
31 maybeTyConSingleCon, isEnumerationTyCon,
36 import CmdLineOpts ( opt_PprStyle_RawTypes )
37 import Maybes ( maybeToBool )
38 import Name ( getOccString, getOccName )
40 import Unique ( Uniquable(..) )
41 import BasicTypes ( tupleParens )
42 import PrelNames -- quite a few *Keys
45 %************************************************************************
47 \subsection{The external interface}
49 %************************************************************************
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.
57 pprType, pprParendType :: Type -> SDoc
58 pprType ty = ppr_ty tOP_PREC ty
59 pprParendType ty = ppr_ty tYCON_PREC ty
61 pprKind, pprParendKind :: Kind -> SDoc
63 pprParendKind = pprParendType
65 pprPred :: PredType -> SDoc
66 pprPred (Class clas tys) = pprClassPred clas tys
67 pprPred (IParam n ty) = hsep [ptext SLIT("?") <> ppr n,
68 ptext SLIT("::"), ppr ty]
70 pprClassPred :: Class -> [Type] -> SDoc
71 pprClassPred clas tys = ppr clas <+> hsep (map pprParendType tys)
73 pprTheta :: ThetaType -> SDoc
74 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
76 instance Outputable Type where
79 instance Outputable PredType where
84 %************************************************************************
86 \subsection{Pretty printing}
88 %************************************************************************
92 @ppr_ty@ takes an @Int@ that is the precedence of the context.
93 The precedence levels are:
95 \item[tOP_PREC] No parens required.
96 \item[fUN_PREC] Left hand argument of a function arrow.
97 \item[tYCON_PREC] Argument of a type constructor.
102 tOP_PREC = (0 :: Int) -- type in ParseIface.y
103 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
104 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
106 maybeParen ctxt_prec inner_prec pretty
107 | ctxt_prec < inner_prec = pretty
108 | otherwise = parens pretty
112 ppr_ty :: Int -> Type -> SDoc
113 ppr_ty ctxt_prec (TyVarTy tyvar)
116 ppr_ty ctxt_prec ty@(TyConApp tycon tys)
117 -- KIND CASE; it's of the form (Type x)
118 | tycon `hasKey` typeConKey && n_tys == 1
119 = -- For kinds, print (Type x) as just x if x is a
120 -- type constructor (must be Boxed, Unboxed, AnyBox)
121 -- Otherwise print as (Type x)
123 TyConApp bx [] -> ppr (getOccName bx) -- Always unqualified
124 other -> maybeParen ctxt_prec tYCON_PREC
125 (sep [ppr tycon, nest 4 tys_w_spaces])
128 | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey) && n_tys == 0
129 = -- For usages (! and .), always print bare OccName, without pkg/mod/uniq
130 ppr (getOccName (tyConName tycon))
132 -- TUPLE CASE (boxed and unboxed)
134 && length tys == tyConArity tycon -- no magic if partially applied
135 = tupleParens (tupleTyConBoxity tycon) tys_w_commas
138 | tycon `hasKey` listTyConKey && n_tys == 1
139 = brackets (ppr_ty tOP_PREC ty1)
141 -- DICTIONARY CASE, prints {C a}
142 -- This means that instance decls come out looking right in interfaces
143 -- and that in turn means they get "gated" correctly when being slurped in
144 | maybeToBool maybe_pred
145 = braces (pprPred pred)
147 -- NO-ARGUMENT CASE (=> no parens)
153 = maybeParen ctxt_prec tYCON_PREC (sep [ppr tycon, nest 4 tys_w_spaces])
158 Just pred = maybe_pred
159 maybe_pred = splitPredTy_maybe ty -- Checks class and arity
160 tys_w_commas = sep (punctuate comma (map (ppr_ty tOP_PREC) tys))
161 tys_w_spaces = sep (map (ppr_ty tYCON_PREC) tys)
165 ppr_ty ctxt_prec ty@(ForAllTy _ _)
166 = getPprStyle $ \ sty ->
167 maybeParen ctxt_prec fUN_PREC $
168 sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."),
173 (tyvars, rho) = splitForAllTys ty
174 (theta, tau) = splitRhoTy rho
176 pp_tyvars sty = hsep (map pprTyVarBndr some_tyvars)
178 some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
179 = filter (not . isUTyVar) tyvars -- hide uvars from user
184 ppr_theta theta = pprTheta theta <+> ptext SLIT("=>")
187 ppr_ty ctxt_prec (FunTy ty1 ty2)
188 -- we don't want to lose usage annotations or synonyms,
189 -- so we mustn't use splitFunTys here.
190 = maybeParen ctxt_prec fUN_PREC $
191 sep [ ppr_ty fUN_PREC ty1
192 , ptext SLIT("->") <+> ppr_ty tOP_PREC ty2
195 ppr_ty ctxt_prec (AppTy ty1 ty2)
196 = maybeParen ctxt_prec tYCON_PREC $
197 ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
199 ppr_ty ctxt_prec (UsageTy u ty)
200 = maybeParen ctxt_prec tYCON_PREC $
201 ptext SLIT("__u") <+> ppr_ty tYCON_PREC u
202 <+> ppr_ty tYCON_PREC ty
203 -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
205 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
206 = ppr_ty ctxt_prec ty
207 -- = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
209 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
211 ppr_ty ctxt_prec (PredTy p) = braces (pprPred p)
215 %************************************************************************
217 \subsection[TyVar]{@TyVar@}
219 %************************************************************************
221 We print type-variable binders with their kinds in interface files,
222 and when in debug mode.
225 pprTyVarBndr :: TyVar -> SDoc
227 = getPprStyle $ \ sty ->
228 if (ifaceStyle sty && kind /= liftedTypeKind) || debugStyle sty then
229 hsep [ppr tyvar, dcolon, pprParendKind kind]
230 -- See comments with ppDcolon in PprCore.lhs
234 kind = tyVarKind tyvar
236 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
240 %************************************************************************
242 \subsection{Mumbo jumbo}
244 %************************************************************************
246 Grab a name for the type. This is used to determine the type
247 description for profiling.
250 getTyDescription :: Type -> String
253 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
256 AppTy fun _ -> getTyDescription fun
257 FunTy _ res -> '-' : '>' : fun_result res
258 TyConApp tycon _ -> getOccString tycon
259 NoteTy (FTVNote _) ty -> getTyDescription ty
260 NoteTy (SynNote ty1) _ -> getTyDescription ty1
261 PredTy p -> getTyDescription (predRepTy p)
262 ForAllTy _ ty -> getTyDescription ty
265 fun_result (FunTy _ res) = '>' : fun_result res
266 fun_result other = getTyDescription other
271 showTypeCategory :: Type -> Char
273 {C,I,F,D} char, int, float, double
275 S other single-constructor type
276 {c,i,f,d} unboxed ditto
278 s *unpacked" single-cons...
284 + dictionary, unless it's a ...
287 M other (multi-constructor) data-con type
289 - reserved for others to mark as "uninteresting"
295 case splitTyConApp_maybe ty of
296 Nothing -> if maybeToBool (splitFunTy_maybe ty)
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'
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...