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
33 import TyCon ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon,
34 maybeTyConSingleCon, isEnumerationTyCon,
35 tyConArity, tyConUnique
37 import Class ( Class, className )
40 import Maybes ( maybeToBool )
41 import Name ( getOccString, NamedThing(..) )
44 import Unique ( Uniquable(..) )
45 import Unique -- quite a few *Keys
48 %************************************************************************
50 \subsection{The external interface}
52 %************************************************************************
54 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
55 defined to use this. @pprParendType@ is the same, except it puts
56 parens around the type, except for the atomic cases. @pprParendType@
57 works just by setting the initial context precedence very high.
60 pprType, pprParendType :: Type -> SDoc
61 pprType ty = ppr_ty pprTyEnv tOP_PREC ty
62 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
64 pprKind, pprParendKind :: Kind -> SDoc
66 pprParendKind = pprParendType
68 pprPred :: PredType -> SDoc
69 pprPred (Class clas tys) = pprConstraint clas tys
70 pprPred (IParam n ty) = hsep [ptext SLIT("?") <> ppr n,
71 ptext SLIT("::"), ppr ty]
73 pprConstraint :: Class -> [Type] -> SDoc
74 pprConstraint clas tys = ppr clas <+> hsep (map pprParendType tys)
76 pprTheta :: ThetaType -> SDoc
77 pprTheta theta = parens (hsep (punctuate comma (map pprPred theta)))
79 instance Outputable Type 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)
103 fUN_PREC = (1 :: Int)
104 tYCON_PREC = (2 :: Int)
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 bx
124 other -> maybeParen ctxt_prec tYCON_PREC
125 (sep [ppr tycon, nest 4 tys_w_spaces])
128 -- TUPLE CASE (boxed and unboxed)
130 && length tys == tyConArity tycon -- no magic if partially applied
131 = parens tys_w_commas
133 | isUnboxedTupleTyCon tycon
134 && length tys == tyConArity tycon -- no magic if partially applied
135 = parens (char '#' <+> tys_w_commas <+> char '#')
138 | tycon `hasKey` listTyConKey && n_tys == 1
139 = brackets (ppr_ty env 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 (ppr_pred env 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 env tOP_PREC) tys))
161 tys_w_spaces = sep (map (ppr_ty env tYCON_PREC) tys)
165 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
166 = getPprStyle $ \ sty ->
167 maybeParen ctxt_prec fUN_PREC $
168 sep [ ptext SLIT("forall") <+> pp_tyvars <> ptext SLIT("."),
170 ppr_ty env tOP_PREC tau
173 (tyvars, rho) = splitForAllTys ty -- don't treat theta specially any more (KSW 1999-04)
174 (theta, tau) = splitRhoTy rho
176 pp_tyvars = hsep (map (pBndr env LambdaBind) tyvars)
179 ppr_theta theta = parens (hsep (punctuate comma (map (ppr_pred env) theta)))
183 ppr_ty env ctxt_prec (FunTy ty1 ty2)
184 = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest ty2))
185 -- we don't want to lose usage annotations or synonyms,
186 -- so we mustn't use splitFunTys here.
188 pp_rest (FunTy ty1 ty2) = pp_codom ty1 : pp_rest ty2
189 pp_rest ty = [pp_codom ty]
190 pp_codom ty = ptext SLIT("->") <+> ppr_ty env fUN_PREC ty
192 ppr_ty env ctxt_prec (AppTy ty1 ty2)
193 = maybeParen ctxt_prec tYCON_PREC $
194 ppr_ty env tOP_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
196 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
197 = ppr_ty env ctxt_prec ty
198 -- = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
200 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
202 ppr_ty env ctxt_prec ty@(NoteTy (UsgForAll _) _)
203 = maybeParen ctxt_prec fUN_PREC $
204 sep [ ptext SLIT("__fuall") <+> brackets pp_uvars <+> ptext SLIT("=>"),
205 ppr_ty env tOP_PREC sigma
208 (uvars,sigma) = splitUsForAllTys ty
209 pp_uvars = hsep (map ppr uvars)
211 ppr_ty env ctxt_prec (NoteTy (UsgNote u) ty)
212 = maybeParen ctxt_prec tYCON_PREC $
213 ptext SLIT("__u") <+> ppr u <+> ppr_ty env tYCON_PREC ty
215 ppr_ty env ctxt_prec (NoteTy (IPNote nm) ty)
216 = braces (ppr_pred env (IParam nm ty))
218 ppr_theta env [] = empty
219 ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_pred env) theta)))
221 ppr_pred env (Class clas tys) = ppr clas <+>
222 hsep (map (ppr_ty env tYCON_PREC) tys)
223 ppr_pred env (IParam n ty) = hsep [char '?' <> ppr n, text "::",
224 ppr_ty env tYCON_PREC ty]
228 pprTyEnv = initPprEnv b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
230 b = panic "PprType:init_ppr_env"
234 instance Outputable UsageAnn where
235 ppr UsOnce = ptext SLIT("-")
236 ppr UsMany = ptext SLIT("!")
237 ppr (UsVar uv) = ppr uv
241 %************************************************************************
243 \subsection[TyVar]{@TyVar@}
245 %************************************************************************
247 We print type-variable binders with their kinds in interface files,
248 and when in debug mode.
252 = getPprStyle $ \ sty ->
253 if (ifaceStyle sty && kind /= boxedTypeKind) || debugStyle sty then
254 hsep [ppr tyvar, dcolon, pprParendKind kind]
255 -- See comments with ppDcolon in PprCore.lhs
259 kind = tyVarKind tyvar
261 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
265 %************************************************************************
267 \subsection{Mumbo jumbo}
269 %************************************************************************
271 Grab a name for the type. This is used to determine the type
272 description for profiling.
275 getTyDescription :: Type -> String
278 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
281 AppTy fun _ -> getTyDescription fun
282 FunTy _ res -> '-' : '>' : fun_result res
283 TyConApp tycon _ -> getOccString tycon
284 NoteTy (FTVNote _) ty -> getTyDescription ty
285 NoteTy (SynNote ty1) _ -> getTyDescription ty1
286 NoteTy (UsgNote _) ty -> getTyDescription ty
287 ForAllTy _ ty -> getTyDescription ty
290 fun_result (FunTy _ res) = '>' : fun_result res
291 fun_result other = getTyDescription other
296 showTypeCategory :: Type -> Char
298 {C,I,F,D} char, int, float, double
300 S other single-constructor type
301 {c,i,f,d} unboxed ditto
303 s *unpacked" single-cons...
309 + dictionary, unless it's a ...
312 M other (multi-constructor) data-con type
314 - reserved for others to mark as "uninteresting"
320 case splitTyConApp_maybe ty of
321 Nothing -> if maybeToBool (splitFunTy_maybe ty)
326 let utc = getUnique tycon in
327 if utc == charDataConKey then 'C'
328 else if utc == intDataConKey then 'I'
329 else if utc == floatDataConKey then 'F'
330 else if utc == doubleDataConKey then 'D'
331 else if utc == smallIntegerDataConKey ||
332 utc == largeIntegerDataConKey then 'J'
333 else if utc == charPrimTyConKey then 'c'
334 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
335 || utc == addrPrimTyConKey) then 'i'
336 else if utc == floatPrimTyConKey then 'f'
337 else if utc == doublePrimTyConKey then 'd'
338 else if isPrimTyCon tycon {- array, we hope -} then 'A'
339 else if isEnumerationTyCon tycon then 'E'
340 else if isTupleTyCon tycon then 'T'
341 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
342 else if utc == listTyConKey then 'L'
343 else 'M' -- oh, well...