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, 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 Type ( Type(..), TyNote(..), Kind, ThetaType, UsageAnn(..),
23 splitForAllTys, splitSigmaTy, splitRhoTy,
24 isDictTy, splitTyConApp_maybe, splitFunTy_maybe,
27 import Var ( TyVar, tyVarKind,
28 tyVarName, setTyVarName
31 import TyCon ( TyCon, isPrimTyCon, isTupleTyCon, isUnboxedTupleTyCon,
32 maybeTyConSingleCon, isEnumerationTyCon,
33 tyConArity, tyConUnique
35 import Class ( Class )
38 import Maybes ( maybeToBool )
39 import Name ( getOccString, NamedThing(..) )
42 import Unique ( Uniquable(..) )
43 import Unique -- quite a few *Keys
47 %************************************************************************
49 \subsection{The external interface}
51 %************************************************************************
53 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
54 defined to use this. @pprParendType@ is the same, except it puts
55 parens around the type, except for the atomic cases. @pprParendType@
56 works just by setting the initial context precedence very high.
59 pprType, pprParendType :: Type -> SDoc
60 pprType ty = ppr_ty pprTyEnv tOP_PREC ty
61 pprParendType ty = ppr_ty pprTyEnv tYCON_PREC ty
63 pprKind, pprParendKind :: Kind -> SDoc
65 pprParendKind = pprParendType
67 pprConstraint :: Class -> [Type] -> SDoc
68 pprConstraint clas tys = ppr clas <+> hsep (map (pprParendType) tys)
70 pprTheta :: ThetaType -> SDoc
71 pprTheta theta = parens (hsep (punctuate comma (map ppr_dict theta)))
73 ppr_dict (c,tys) = pprConstraint c tys
75 instance Outputable Type where
80 %************************************************************************
82 \subsection{Pretty printing}
84 %************************************************************************
88 @ppr_ty@ takes an @Int@ that is the precedence of the context.
89 The precedence levels are:
91 \item[tOP_PREC] No parens required.
92 \item[fUN_PREC] Left hand argument of a function arrow.
93 \item[tYCON_PREC] Argument of a type constructor.
100 tYCON_PREC = (2 :: Int)
102 maybeParen ctxt_prec inner_prec pretty
103 | ctxt_prec < inner_prec = pretty
104 | otherwise = parens pretty
108 ppr_ty :: PprEnv TyVar -> Int -> Type -> SDoc
109 ppr_ty env ctxt_prec (TyVarTy tyvar)
112 ppr_ty env ctxt_prec ty@(TyConApp tycon tys)
113 -- KIND CASE; it's of the form (Type x)
114 | tycon_uniq == typeConKey && n_tys == 1
115 = -- For kinds, print (Type x) as just x if x is a
116 -- type constructor (must be Boxed, Unboxed, AnyBox)
117 -- Otherwise print as (Type x)
119 TyConApp bx [] -> ppr bx
120 other -> maybeParen ctxt_prec tYCON_PREC
121 (ppr tycon <+> tys_w_spaces)
124 -- TUPLE CASE (boxed and unboxed)
126 && length tys == tyConArity tycon -- no magic if partially applied
127 = parens tys_w_commas
129 | isUnboxedTupleTyCon tycon
130 && length tys == tyConArity tycon -- no magic if partially applied
131 = parens (char '#' <+> tys_w_commas <+> char '#')
134 | tycon_uniq == listTyConKey && n_tys == 1
135 = brackets (ppr_ty env tOP_PREC ty1)
137 -- DICTIONARY CASE, prints {C a}
138 -- This means that instance decls come out looking right in interfaces
139 -- and that in turn means they get "gated" correctly when being slurped in
140 | maybeToBool maybe_dict
141 = braces (ppr_dict env tYCON_PREC ctys)
143 -- NO-ARGUMENT CASE (=> no parens)
149 = maybeParen ctxt_prec tYCON_PREC (hsep [ppr tycon, tys_w_spaces])
152 tycon_uniq = tyConUnique tycon
155 Just ctys = maybe_dict
156 maybe_dict = splitDictTy_maybe ty -- Checks class and arity
157 tys_w_commas = sep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
158 tys_w_spaces = hsep (map (ppr_ty env tYCON_PREC) tys)
162 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
163 = getPprStyle $ \ sty ->
164 maybeParen ctxt_prec fUN_PREC $
165 if ifaceStyle sty then
166 sep [ ptext SLIT("__forall") <+> brackets pp_tyvars <+> ptext SLIT("=>"), pp_body ]
168 sep [ ptext SLIT("forall") <+> pp_tyvars <> ptext SLIT("."), pp_body ]
170 (tyvars, body_ty) = splitForAllTys ty -- don't treat theta specially any more (KSW 1999-04)
172 pp_tyvars = hsep (map (pBndr env LambdaBind) tyvars)
173 pp_body = ppr_ty env tOP_PREC body_ty
176 ppr_ty env ctxt_prec (FunTy ty1 ty2)
177 = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest ty2))
178 -- we don't want to lose usage annotations or synonyms,
179 -- so we mustn't use splitFunTys here.
181 pp_rest (FunTy ty1 ty2) = pp_codom ty1 : pp_rest ty2
182 pp_rest ty = [pp_codom ty]
183 pp_codom ty = ptext SLIT("->") <+> ppr_ty env fUN_PREC ty
185 ppr_ty env ctxt_prec (AppTy ty1 ty2)
186 = maybeParen ctxt_prec tYCON_PREC $
187 ppr_ty env tOP_PREC ty1 <+> ppr_ty env tYCON_PREC ty2
189 ppr_ty env ctxt_prec (NoteTy (SynNote ty) expansion)
190 = ppr_ty env ctxt_prec ty
191 -- = ppr_ty env ctxt_prec expansion -- if we don't want to see syntys
193 ppr_ty env ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty env ctxt_prec ty
195 ppr_ty env ctxt_prec (NoteTy (UsgNote u) ty)
196 = maybeParen ctxt_prec tYCON_PREC $
197 ppr u <+> ppr_ty env tYCON_PREC ty
199 ppr_theta env [] = empty
200 ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_dict env tOP_PREC) theta)))
202 ppr_dict env ctxt (clas, tys) = ppr clas <+>
203 hsep (map (ppr_ty env tYCON_PREC) tys)
207 pprTyEnv = initPprEnv b b (Just ppr) b (Just (\site -> pprTyVarBndr)) b
209 b = panic "PprType:init_ppr_env"
213 instance Outputable UsageAnn where
214 ppr UsOnce = ptext SLIT("__o")
215 ppr UsMany = ptext SLIT("__m")
216 ppr (UsVar uv) = ptext SLIT("__uv") <> ppr uv
219 %************************************************************************
221 \subsection[TyVar]{@TyVar@}
223 %************************************************************************
225 We print type-variable binders with their kinds in interface files,
226 and when in debug mode.
230 = getPprStyle $ \ sty ->
231 if (ifaceStyle sty || debugStyle sty) && kind /= boxedTypeKind then
232 hsep [ppr tyvar, dcolon, pprParendKind kind]
233 -- See comments with ppDcolon in PprCore.lhs
237 kind = tyVarKind tyvar
239 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
243 %************************************************************************
245 \subsection{Mumbo jumbo}
247 %************************************************************************
249 Grab a name for the type. This is used to determine the type
250 description for profiling.
253 getTyDescription :: Type -> String
256 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
259 AppTy fun _ -> getTyDescription fun
260 FunTy _ res -> '-' : '>' : fun_result res
261 TyConApp tycon _ -> getOccString tycon
262 NoteTy (FTVNote _) ty -> getTyDescription ty
263 NoteTy (SynNote ty1) _ -> getTyDescription ty1
264 NoteTy (UsgNote _) ty -> getTyDescription ty
265 ForAllTy _ ty -> getTyDescription ty
268 fun_result (FunTy _ res) = '>' : fun_result res
269 fun_result other = getTyDescription other
274 showTypeCategory :: Type -> Char
276 {C,I,F,D} char, int, float, double
278 S other single-constructor type
279 {c,i,f,d} unboxed ditto
281 s *unpacked" single-cons...
287 + dictionary, unless it's a ...
290 M other (multi-constructor) data-con type
292 - reserved for others to mark as "uninteresting"
298 case splitTyConApp_maybe ty of
299 Nothing -> if maybeToBool (splitFunTy_maybe ty)
304 let utc = getUnique tycon in
305 if utc == charDataConKey then 'C'
306 else if utc == intDataConKey then 'I'
307 else if utc == floatDataConKey then 'F'
308 else if utc == doubleDataConKey then 'D'
309 else if utc == smallIntegerDataConKey ||
310 utc == largeIntegerDataConKey then 'J'
311 else if utc == charPrimTyConKey then 'c'
312 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
313 || utc == addrPrimTyConKey) then 'i'
314 else if utc == floatPrimTyConKey then 'f'
315 else if utc == doublePrimTyConKey then 'd'
316 else if isPrimTyCon tycon {- array, we hope -} then 'A'
317 else if isEnumerationTyCon tycon then 'E'
318 else if isTupleTyCon tycon then 'T'
319 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
320 else if utc == listTyConKey then 'L'
321 else 'M' -- oh, well...