2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[PprType]{Printing Types, TyVars, Classes, TyCons}
8 pprKind, pprParendKind,
9 pprType, pprParendType,
10 pprConstraint, pprPred, pprTheta,
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, boxedTypeKind ) -- friend
22 import Type ( PredType(..), ThetaType,
24 splitForAllTys, splitSigmaTy, splitRhoTy,
25 isDictTy, splitTyConApp_maybe, splitFunTy_maybe,
28 import Var ( TyVar, tyVarKind )
29 import TyCon ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon,
30 maybeTyConSingleCon, isEnumerationTyCon,
33 import Class ( Class )
36 import CmdLineOpts ( opt_PprStyle_RawTypes )
37 import Maybes ( maybeToBool )
38 import Name ( getOccString, getOccName )
41 import Unique ( Uniquable(..) )
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 pprTyEnv tOP_PREC ty
59 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
61 pprKind, pprParendKind :: Kind -> SDoc
63 pprParendKind = pprParendType
65 pprPred :: PredType -> SDoc
66 pprPred (Class clas tys) = pprConstraint clas tys
67 pprPred (IParam n ty) = hsep [ptext SLIT("?") <> ppr n,
68 ptext SLIT("::"), ppr ty]
70 pprConstraint :: Class -> [Type] -> SDoc
71 pprConstraint 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 :: PprEnv TyVar -> Int -> Type -> SDoc
113 ppr_ty env ctxt_prec (TyVarTy tyvar)
116 ppr_ty env 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 = parens tys_w_commas
137 | isUnboxedTupleTyCon tycon
138 && length tys == tyConArity tycon -- no magic if partially applied
139 = parens (char '#' <+> tys_w_commas <+> char '#')
142 | tycon `hasKey` listTyConKey && n_tys == 1
143 = brackets (ppr_ty env tOP_PREC ty1)
145 -- DICTIONARY CASE, prints {C a}
146 -- This means that instance decls come out looking right in interfaces
147 -- and that in turn means they get "gated" correctly when being slurped in
148 | maybeToBool maybe_pred
149 = braces (ppr_pred env pred)
151 -- NO-ARGUMENT CASE (=> no parens)
157 = maybeParen ctxt_prec tYCON_PREC (sep [ppr tycon, nest 4 tys_w_spaces])
162 Just pred = maybe_pred
163 maybe_pred = splitPredTy_maybe ty -- Checks class and arity
164 tys_w_commas = sep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
165 tys_w_spaces = sep (map (ppr_ty env tYCON_PREC) tys)
169 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
170 = getPprStyle $ \ sty ->
171 maybeParen ctxt_prec fUN_PREC $
172 sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."),
174 ppr_ty env tOP_PREC tau
177 (tyvars, rho) = splitForAllTys ty
178 (theta, tau) = splitRhoTy rho
180 pp_tyvars sty = hsep (map (pBndr env LambdaBind) some_tyvars)
182 some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
183 = filter (not . isUTyVar) tyvars -- hide uvars from user
188 ppr_theta theta = parens (hsep (punctuate comma (map (ppr_pred env) theta)))
192 ppr_ty env ctxt_prec (FunTy ty1 ty2)
193 -- we don't want to lose usage annotations or synonyms,
194 -- so we mustn't use splitFunTys here.
195 = maybeParen ctxt_prec fUN_PREC $
196 sep [ ppr_ty env fUN_PREC ty1
197 , ptext SLIT("->") <+> ppr_ty env tOP_PREC ty2
200 ppr_ty env ctxt_prec (AppTy ty1 ty2)
201 = maybeParen ctxt_prec tYCON_PREC $
202 ppr_ty env fUN_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
204 ppr_ty env ctxt_prec (UsageTy u ty)
205 = maybeParen ctxt_prec tYCON_PREC $
206 ptext SLIT("__u") <+> ppr_ty env tYCON_PREC u
207 <+> ppr_ty env tYCON_PREC ty
208 -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
210 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
211 = ppr_ty env ctxt_prec ty
212 -- = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
214 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
216 ppr_ty env ctxt_prec (PredTy p) = braces (ppr_pred env p)
218 ppr_pred env (Class clas tys) = ppr clas <+>
219 hsep (map (ppr_ty env tYCON_PREC) tys)
220 ppr_pred env (IParam n ty) = hsep [char '?' <> ppr n, text "::",
221 ppr_ty env tYCON_PREC ty]
225 pprTyEnv = initPprEnv b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
227 b = panic "PprType:init_ppr_env"
231 %************************************************************************
233 \subsection[TyVar]{@TyVar@}
235 %************************************************************************
237 We print type-variable binders with their kinds in interface files,
238 and when in debug mode.
242 = getPprStyle $ \ sty ->
243 if (ifaceStyle sty && kind /= boxedTypeKind) || debugStyle sty then
244 hsep [ppr tyvar, dcolon, pprParendKind kind]
245 -- See comments with ppDcolon in PprCore.lhs
249 kind = tyVarKind tyvar
251 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
255 %************************************************************************
257 \subsection{Mumbo jumbo}
259 %************************************************************************
261 Grab a name for the type. This is used to determine the type
262 description for profiling.
265 getTyDescription :: Type -> String
268 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
271 AppTy fun _ -> getTyDescription fun
272 FunTy _ res -> '-' : '>' : fun_result res
273 TyConApp tycon _ -> getOccString tycon
274 NoteTy (FTVNote _) ty -> getTyDescription ty
275 NoteTy (SynNote ty1) _ -> getTyDescription ty1
276 PredTy p -> getTyDescription (predRepTy p)
277 ForAllTy _ ty -> getTyDescription ty
280 fun_result (FunTy _ res) = '>' : fun_result res
281 fun_result other = getTyDescription other
286 showTypeCategory :: Type -> Char
288 {C,I,F,D} char, int, float, double
290 S other single-constructor type
291 {c,i,f,d} unboxed ditto
293 s *unpacked" single-cons...
299 + dictionary, unless it's a ...
302 M other (multi-constructor) data-con type
304 - reserved for others to mark as "uninteresting"
310 case splitTyConApp_maybe ty of
311 Nothing -> if maybeToBool (splitFunTy_maybe ty)
316 let utc = getUnique tycon in
317 if utc == charDataConKey then 'C'
318 else if utc == intDataConKey then 'I'
319 else if utc == floatDataConKey then 'F'
320 else if utc == doubleDataConKey then 'D'
321 else if utc == smallIntegerDataConKey ||
322 utc == largeIntegerDataConKey then 'J'
323 else if utc == charPrimTyConKey then 'c'
324 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
325 || utc == addrPrimTyConKey) then 'i'
326 else if utc == floatPrimTyConKey then 'f'
327 else if utc == doublePrimTyConKey then 'd'
328 else if isPrimTyCon tycon {- array, we hope -} then 'A'
329 else if isEnumerationTyCon tycon then 'E'
330 else if isTupleTyCon tycon then 'T'
331 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
332 else if utc == listTyConKey then 'L'
333 else 'M' -- oh, well...