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, UsageAnn(..),
24 import Type ( PredType(..), ThetaType,
26 splitForAllTys, splitSigmaTy, splitRhoTy,
27 isDictTy, splitTyConApp_maybe, splitFunTy_maybe,
30 import Var ( TyVar, tyVarKind,
31 tyVarName, setTyVarName
34 import TyCon ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon,
35 maybeTyConSingleCon, isEnumerationTyCon,
36 tyConArity, tyConUnique
38 import Class ( Class, className )
41 import Maybes ( maybeToBool )
42 import Name ( getOccString, NamedThing(..) )
45 import Unique ( Uniquable(..) )
46 import Unique -- quite a few *Keys
50 %************************************************************************
52 \subsection{The external interface}
54 %************************************************************************
56 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
57 defined to use this. @pprParendType@ is the same, except it puts
58 parens around the type, except for the atomic cases. @pprParendType@
59 works just by setting the initial context precedence very high.
62 pprType, pprParendType :: Type -> SDoc
63 pprType ty = ppr_ty pprTyEnv tOP_PREC ty
64 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
66 pprKind, pprParendKind :: Kind -> SDoc
68 pprParendKind = pprParendType
70 pprPred :: PredType -> SDoc
71 pprPred (Class clas tys) = pprConstraint clas tys
72 pprPred (IParam n ty) = hsep [ptext SLIT("?") <> ppr n,
73 ptext SLIT("::"), ppr ty]
75 pprConstraint :: Class -> [Type] -> SDoc
76 pprConstraint clas tys = ppr clas <+> hsep (map pprParendType tys)
78 pprTheta :: ThetaType -> SDoc
79 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
81 instance Outputable Type where
86 %************************************************************************
88 \subsection{Pretty printing}
90 %************************************************************************
94 @ppr_ty@ takes an @Int@ that is the precedence of the context.
95 The precedence levels are:
97 \item[tOP_PREC] No parens required.
98 \item[fUN_PREC] Left hand argument of a function arrow.
99 \item[tYCON_PREC] Argument of a type constructor.
104 tOP_PREC = (0 :: Int)
105 fUN_PREC = (1 :: Int)
106 tYCON_PREC = (2 :: Int)
108 maybeParen ctxt_prec inner_prec pretty
109 | ctxt_prec < inner_prec = pretty
110 | otherwise = parens pretty
114 ppr_ty :: PprEnv TyVar -> Int -> Type -> SDoc
115 ppr_ty env ctxt_prec (TyVarTy tyvar)
118 ppr_ty env ctxt_prec ty@(TyConApp tycon tys)
119 -- KIND CASE; it's of the form (Type x)
120 | tycon `hasKey` typeConKey && n_tys == 1
121 = -- For kinds, print (Type x) as just x if x is a
122 -- type constructor (must be Boxed, Unboxed, AnyBox)
123 -- Otherwise print as (Type x)
125 TyConApp bx [] -> ppr bx
126 other -> maybeParen ctxt_prec tYCON_PREC
127 (sep [ppr tycon, nest 4 tys_w_spaces])
130 -- TUPLE CASE (boxed and unboxed)
132 && length tys == tyConArity tycon -- no magic if partially applied
133 = parens tys_w_commas
135 | isUnboxedTupleTyCon tycon
136 && length tys == tyConArity tycon -- no magic if partially applied
137 = parens (char '#' <+> tys_w_commas <+> char '#')
140 | tycon `hasKey` listTyConKey && n_tys == 1
141 = brackets (ppr_ty env tOP_PREC ty1)
143 -- DICTIONARY CASE, prints {C a}
144 -- This means that instance decls come out looking right in interfaces
145 -- and that in turn means they get "gated" correctly when being slurped in
146 | maybeToBool maybe_pred
147 = braces (ppr_pred env pred)
149 -- NO-ARGUMENT CASE (=> no parens)
155 = maybeParen ctxt_prec tYCON_PREC (sep [ppr tycon, nest 4 tys_w_spaces])
160 Just pred = maybe_pred
161 maybe_pred = splitPredTy_maybe ty -- Checks class and arity
162 tys_w_commas = sep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
163 tys_w_spaces = sep (map (ppr_ty env tYCON_PREC) tys)
167 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
168 = getPprStyle $ \ sty ->
169 maybeParen ctxt_prec fUN_PREC $
170 sep [ ptext SLIT("forall") <+> pp_tyvars <> ptext SLIT("."),
172 ppr_ty env tOP_PREC tau
175 (tyvars, rho) = splitForAllTys ty -- don't treat theta specially any more (KSW 1999-04)
176 (theta, tau) = splitRhoTy rho
178 pp_tyvars = hsep (map (pBndr env LambdaBind) tyvars)
181 ppr_theta theta = parens (hsep (punctuate comma (map (ppr_pred env) theta)))
185 ppr_ty env ctxt_prec (FunTy ty1 ty2)
186 = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest ty2))
187 -- we don't want to lose usage annotations or synonyms,
188 -- so we mustn't use splitFunTys here.
190 pp_rest (FunTy ty1 ty2) = pp_codom ty1 : pp_rest ty2
191 pp_rest ty = [pp_codom ty]
192 pp_codom ty = ptext SLIT("->") <+> ppr_ty env fUN_PREC ty
194 ppr_ty env ctxt_prec (AppTy ty1 ty2)
195 = maybeParen ctxt_prec tYCON_PREC $
196 ppr_ty env tOP_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
198 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
199 = ppr_ty env ctxt_prec ty
200 -- = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
202 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
204 ppr_ty env ctxt_prec ty@(NoteTy (UsgForAll _) _)
205 = maybeParen ctxt_prec fUN_PREC $
206 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
207 ppr_ty env tOP_PREC sigma
210 (uvars,sigma) = splitUsForAllTys ty
211 pp_uvars = hsep (map ppr uvars)
213 ppr_ty env ctxt_prec (NoteTy (UsgNote u) ty)
214 = maybeParen ctxt_prec tYCON_PREC $
215 ptext SLIT("__u") <+> ppr u <+> ppr_ty env tYCON_PREC ty
217 ppr_ty env ctxt_prec (NoteTy (IPNote nm) ty)
218 = braces (ppr_pred env (IParam nm ty))
220 ppr_theta env [] = empty
221 ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_pred env) theta)))
223 ppr_pred env (Class clas tys) = ppr clas <+>
224 hsep (map (ppr_ty env tYCON_PREC) tys)
225 ppr_pred env (IParam n ty) = hsep [char '?' <> ppr n, text "::",
226 ppr_ty env tYCON_PREC ty]
230 pprTyEnv = initPprEnv b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
232 b = panic "PprType:init_ppr_env"
236 instance Outputable UsageAnn where
237 ppr UsOnce = ptext SLIT("-")
238 ppr UsMany = ptext SLIT("!")
239 ppr (UsVar uv) = ppr uv
243 %************************************************************************
245 \subsection[TyVar]{@TyVar@}
247 %************************************************************************
249 We print type-variable binders with their kinds in interface files,
250 and when in debug mode.
254 = getPprStyle $ \ sty ->
255 if (ifaceStyle sty && kind /= boxedTypeKind) || debugStyle sty then
256 hsep [ppr tyvar, dcolon, pprParendKind kind]
257 -- See comments with ppDcolon in PprCore.lhs
261 kind = tyVarKind tyvar
263 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
267 %************************************************************************
269 \subsection{Mumbo jumbo}
271 %************************************************************************
273 Grab a name for the type. This is used to determine the type
274 description for profiling.
277 getTyDescription :: Type -> String
280 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
283 AppTy fun _ -> getTyDescription fun
284 FunTy _ res -> '-' : '>' : fun_result res
285 TyConApp tycon _ -> getOccString tycon
286 NoteTy (FTVNote _) ty -> getTyDescription ty
287 NoteTy (SynNote ty1) _ -> getTyDescription ty1
288 NoteTy (UsgNote _) ty -> getTyDescription ty
289 ForAllTy _ ty -> getTyDescription ty
292 fun_result (FunTy _ res) = '>' : fun_result res
293 fun_result other = getTyDescription other
298 showTypeCategory :: Type -> Char
300 {C,I,F,D} char, int, float, double
302 S other single-constructor type
303 {c,i,f,d} unboxed ditto
305 s *unpacked" single-cons...
311 + dictionary, unless it's a ...
314 M other (multi-constructor) data-con type
316 - reserved for others to mark as "uninteresting"
322 case splitTyConApp_maybe ty of
323 Nothing -> if maybeToBool (splitFunTy_maybe ty)
328 let utc = getUnique tycon in
329 if utc == charDataConKey then 'C'
330 else if utc == intDataConKey then 'I'
331 else if utc == floatDataConKey then 'F'
332 else if utc == doubleDataConKey then 'D'
333 else if utc == smallIntegerDataConKey ||
334 utc == largeIntegerDataConKey then 'J'
335 else if utc == charPrimTyConKey then 'c'
336 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
337 || utc == addrPrimTyConKey) then 'i'
338 else if utc == floatPrimTyConKey then 'f'
339 else if utc == doublePrimTyConKey then 'd'
340 else if isPrimTyCon tycon {- array, we hope -} then 'A'
341 else if isEnumerationTyCon tycon then 'E'
342 else if isTupleTyCon tycon then 'T'
343 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
344 else if utc == listTyConKey then 'L'
345 else 'M' -- oh, well...