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 [ppr n, ptext SLIT("::"), ppr ty]
74 pprConstraint :: Class -> [Type] -> SDoc
75 pprConstraint clas tys = ppr clas <+> hsep (map pprParendType tys)
77 pprTheta :: ThetaType -> SDoc
78 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
80 instance Outputable Type where
85 %************************************************************************
87 \subsection{Pretty printing}
89 %************************************************************************
93 @ppr_ty@ takes an @Int@ that is the precedence of the context.
94 The precedence levels are:
96 \item[tOP_PREC] No parens required.
97 \item[fUN_PREC] Left hand argument of a function arrow.
98 \item[tYCON_PREC] Argument of a type constructor.
103 tOP_PREC = (0 :: Int)
104 fUN_PREC = (1 :: Int)
105 tYCON_PREC = (2 :: Int)
107 maybeParen ctxt_prec inner_prec pretty
108 | ctxt_prec < inner_prec = pretty
109 | otherwise = parens pretty
113 ppr_ty :: PprEnv TyVar -> Int -> Type -> SDoc
114 ppr_ty env ctxt_prec (TyVarTy tyvar)
117 ppr_ty env ctxt_prec ty@(TyConApp tycon tys)
118 -- KIND CASE; it's of the form (Type x)
119 | tycon `hasKey` typeConKey && n_tys == 1
120 = -- For kinds, print (Type x) as just x if x is a
121 -- type constructor (must be Boxed, Unboxed, AnyBox)
122 -- Otherwise print as (Type x)
124 TyConApp bx [] -> ppr bx
125 other -> maybeParen ctxt_prec tYCON_PREC
126 (sep [ppr tycon, nest 4 tys_w_spaces])
129 -- TUPLE CASE (boxed and unboxed)
131 && length tys == tyConArity tycon -- no magic if partially applied
132 = parens tys_w_commas
134 | isUnboxedTupleTyCon tycon
135 && length tys == tyConArity tycon -- no magic if partially applied
136 = parens (char '#' <+> tys_w_commas <+> char '#')
139 | tycon `hasKey` listTyConKey && n_tys == 1
140 = brackets (ppr_ty env tOP_PREC ty1)
142 -- DICTIONARY CASE, prints {C a}
143 -- This means that instance decls come out looking right in interfaces
144 -- and that in turn means they get "gated" correctly when being slurped in
145 | maybeToBool maybe_pred
146 = braces (ppr_pred env pred)
148 -- NO-ARGUMENT CASE (=> no parens)
154 = maybeParen ctxt_prec tYCON_PREC (sep [ppr tycon, nest 4 tys_w_spaces])
159 Just pred = maybe_pred
160 maybe_pred = splitPredTy_maybe ty -- Checks class and arity
161 tys_w_commas = sep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
162 tys_w_spaces = sep (map (ppr_ty env tYCON_PREC) tys)
166 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
167 = getPprStyle $ \ sty ->
168 maybeParen ctxt_prec fUN_PREC $
169 sep [ ptext SLIT("forall") <+> pp_tyvars <> ptext SLIT("."),
171 ppr_ty env tOP_PREC tau
174 (tyvars, rho) = splitForAllTys ty -- don't treat theta specially any more (KSW 1999-04)
175 (theta, tau) = splitRhoTy rho
177 pp_tyvars = hsep (map (pBndr env LambdaBind) tyvars)
180 ppr_theta theta = parens (hsep (punctuate comma (map ppr_pred theta)))
183 ppr_pred (Class clas tys) = ppr clas <+> hsep (map (ppr_ty env tYCON_PREC) tys)
184 ppr_pred (IParam n ty) = hsep [{- char '?' <> -} ppr n, text "::",
185 ppr_ty env tYCON_PREC ty]
187 ppr_ty env ctxt_prec (FunTy ty1 ty2)
188 = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest ty2))
189 -- we don't want to lose usage annotations or synonyms,
190 -- so we mustn't use splitFunTys here.
192 pp_rest (FunTy ty1 ty2) = pp_codom ty1 : pp_rest ty2
193 pp_rest ty = [pp_codom ty]
194 pp_codom ty = ptext SLIT("->") <+> ppr_ty env fUN_PREC ty
196 ppr_ty env ctxt_prec (AppTy ty1 ty2)
197 = maybeParen ctxt_prec tYCON_PREC $
198 ppr_ty env tOP_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
200 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
201 = ppr_ty env ctxt_prec ty
202 -- = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
204 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
206 ppr_ty env ctxt_prec ty@(NoteTy (UsgForAll _) _)
207 = maybeParen ctxt_prec fUN_PREC $
208 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
209 ppr_ty env tOP_PREC sigma
212 (uvars,sigma) = splitUsForAllTys ty
213 pp_uvars = hsep (map ppr uvars)
215 ppr_ty env ctxt_prec (NoteTy (UsgNote u) ty)
216 = maybeParen ctxt_prec tYCON_PREC $
217 ptext SLIT("__u") <+> ppr u <+> ppr_ty env tYCON_PREC ty
219 ppr_ty env ctxt_prec (NoteTy (IPNote nm) ty)
220 = braces (ppr_pred env (IParam nm ty))
222 ppr_theta env [] = empty
223 ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_pred env) theta)))
225 ppr_pred env (Class clas tys) = ppr clas <+>
226 hsep (map (ppr_ty env tYCON_PREC) tys)
227 ppr_pred env (IParam n ty) = hsep [char '?' <> ppr n, text "::",
228 ppr_ty env tYCON_PREC ty]
232 pprTyEnv = initPprEnv b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
234 b = panic "PprType:init_ppr_env"
238 instance Outputable UsageAnn where
239 ppr UsOnce = ptext SLIT("-")
240 ppr UsMany = ptext SLIT("!")
241 ppr (UsVar uv) = ppr uv
245 %************************************************************************
247 \subsection[TyVar]{@TyVar@}
249 %************************************************************************
251 We print type-variable binders with their kinds in interface files,
252 and when in debug mode.
256 = getPprStyle $ \ sty ->
257 if (ifaceStyle sty && kind /= boxedTypeKind) || debugStyle sty then
258 hsep [ppr tyvar, dcolon, pprParendKind kind]
259 -- See comments with ppDcolon in PprCore.lhs
263 kind = tyVarKind tyvar
265 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
269 %************************************************************************
271 \subsection{Mumbo jumbo}
273 %************************************************************************
275 Grab a name for the type. This is used to determine the type
276 description for profiling.
279 getTyDescription :: Type -> String
282 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
285 AppTy fun _ -> getTyDescription fun
286 FunTy _ res -> '-' : '>' : fun_result res
287 TyConApp tycon _ -> getOccString tycon
288 NoteTy (FTVNote _) ty -> getTyDescription ty
289 NoteTy (SynNote ty1) _ -> getTyDescription ty1
290 NoteTy (UsgNote _) ty -> getTyDescription ty
291 ForAllTy _ ty -> getTyDescription ty
294 fun_result (FunTy _ res) = '>' : fun_result res
295 fun_result other = getTyDescription other
300 showTypeCategory :: Type -> Char
302 {C,I,F,D} char, int, float, double
304 S other single-constructor type
305 {c,i,f,d} unboxed ditto
307 s *unpacked" single-cons...
313 + dictionary, unless it's a ...
316 M other (multi-constructor) data-con type
318 - reserved for others to mark as "uninteresting"
324 case splitTyConApp_maybe ty of
325 Nothing -> if maybeToBool (splitFunTy_maybe ty)
330 let utc = getUnique tycon in
331 if utc == charDataConKey then 'C'
332 else if utc == intDataConKey then 'I'
333 else if utc == floatDataConKey then 'F'
334 else if utc == doubleDataConKey then 'D'
335 else if utc == smallIntegerDataConKey ||
336 utc == largeIntegerDataConKey then 'J'
337 else if utc == charPrimTyConKey then 'c'
338 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
339 || utc == addrPrimTyConKey) then 'i'
340 else if utc == floatPrimTyConKey then 'f'
341 else if utc == doublePrimTyConKey then 'd'
342 else if isPrimTyCon tycon {- array, we hope -} then 'A'
343 else if isEnumerationTyCon tycon then 'E'
344 else if isTupleTyCon tycon then 'T'
345 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
346 else if utc == listTyConKey then 'L'
347 else 'M' -- oh, well...