2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[PprType]{Printing Types, TyVars, Classes, TyCons}
8 pprKind, pprParendKind,
9 pprType, pprParendType,
10 pprSourceType, pprPred, pprTheta, pprClassPred,
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(..),
22 Kind, liftedTypeKind ) -- friend
23 import Type ( SourceType(..), eqKind )
24 import TcType ( ThetaType, PredType,
25 tcSplitSigmaTy, isPredTy, isDictTy,
26 tcSplitTyConApp_maybe, tcSplitFunTy_maybe
28 import Var ( TyVar, tyVarKind )
29 import Class ( Class )
30 import TyCon ( TyCon, isPrimTyCon, isTupleTyCon, tupleTyConBoxity,
31 maybeTyConSingleCon, isEnumerationTyCon,
36 import Maybes ( maybeToBool )
37 import Name ( getOccString, getOccName )
38 import OccName ( occNameUserString )
40 import Unique ( Uniquable(..) )
41 import Util ( lengthIs )
42 import BasicTypes ( IPName(..), tupleParens, ipNameName )
43 import PrelNames -- quite a few *Keys
46 %************************************************************************
48 \subsection{The external interface}
50 %************************************************************************
52 @pprType@ is the standard @Type@ printer; the overloaded @ppr@ function is
53 defined to use this. @pprParendType@ is the same, except it puts
54 parens around the type, except for the atomic cases. @pprParendType@
55 works just by setting the initial context precedence very high.
58 pprType, pprParendType :: Type -> SDoc
59 pprType ty = ppr_ty tOP_PREC ty
60 pprParendType ty = ppr_ty tYCON_PREC ty
62 pprKind, pprParendKind :: Kind -> SDoc
64 pprParendKind = pprParendType
66 pprPred :: PredType -> SDoc
67 pprPred = pprSourceType
69 pprSourceType :: SourceType -> SDoc
70 pprSourceType (ClassP clas tys) = pprClassPred clas tys
71 pprSourceType (IParam n ty) = hsep [ppr n, dcolon, ppr ty]
72 pprSourceType (NType tc tys) = ppr tc <+> sep (map pprParendType tys)
74 pprClassPred :: Class -> [Type] -> SDoc
75 pprClassPred clas tys = ppr clas <+> sep (map pprParendType tys)
77 pprTheta :: ThetaType -> SDoc
78 pprTheta theta = parens (sep (punctuate comma (map pprPred theta)))
80 instance Outputable Type where
83 instance Outputable SourceType where
86 instance Outputable name => Outputable (IPName name) where
87 ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
88 ppr (Linear n) = char '%' <> ppr n -- Splittable implicit parameters
92 %************************************************************************
94 \subsection{Pretty printing}
96 %************************************************************************
100 @ppr_ty@ takes an @Int@ that is the precedence of the context.
101 The precedence levels are:
103 \item[tOP_PREC] No parens required.
104 \item[fUN_PREC] Left hand argument of a function arrow.
105 \item[tYCON_PREC] Argument of a type constructor.
110 tOP_PREC = (0 :: Int) -- type in ParseIface.y
111 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
112 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
114 maybeParen ctxt_prec inner_prec pretty
115 | ctxt_prec < inner_prec = pretty
116 | otherwise = parens pretty
120 ppr_ty :: Int -> Type -> SDoc
121 ppr_ty ctxt_prec (TyVarTy tyvar)
124 ppr_ty ctxt_prec ty@(TyConApp tycon tys)
125 -- KIND CASE; it's of the form (Type x)
126 | tycon `hasKey` typeConKey,
128 = -- For kinds, print (Type x) as just x if x is a
129 -- type constructor (must be Boxed, Unboxed, AnyBox)
130 -- Otherwise print as (Type x)
132 TyConApp bx [] -> ppr (getOccName bx) -- Always unqualified
133 other -> maybeParen ctxt_prec tYCON_PREC
134 (ppr tycon <+> ppr_ty tYCON_PREC ty)
137 | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey),
139 = -- For usages (! and .), always print bare OccName, without pkg/mod/uniq
140 ppr (getOccName (tyConName tycon))
142 -- TUPLE CASE (boxed and unboxed)
143 | isTupleTyCon tycon,
144 tys `lengthIs` tyConArity tycon -- No magic if partially applied
145 = tupleParens (tupleTyConBoxity tycon)
146 (sep (punctuate comma (map (ppr_ty tOP_PREC) tys)))
149 | tycon `hasKey` listTyConKey,
151 = brackets (ppr_ty tOP_PREC ty)
153 -- PARALLEL ARRAY CASE
154 | tycon `hasKey` parrTyConKey,
156 = pabrackets (ppr_ty tOP_PREC ty)
160 = ppr_tc_app ctxt_prec tycon tys
163 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
166 ppr_ty ctxt_prec ty@(ForAllTy _ _)
167 = getPprStyle $ \ sty ->
168 maybeParen ctxt_prec fUN_PREC $
169 sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."),
174 (tyvars, theta, tau) = tcSplitSigmaTy ty
175 pp_tyvars sty = sep (map pprTyVarBndr tyvars)
178 ppr_theta theta = pprTheta theta <+> ptext SLIT("=>")
181 ppr_ty ctxt_prec (FunTy ty1 ty2)
182 -- we don't want to lose usage annotations or synonyms,
183 -- so we mustn't use splitFunTys here.
184 = maybeParen ctxt_prec fUN_PREC $
185 sep [ ppr_ty fUN_PREC ty1
186 , ptext arrow <+> ppr_ty tOP_PREC ty2
188 where arrow | isPredTy ty1 = SLIT("=>")
189 | otherwise = SLIT("->")
191 ppr_ty ctxt_prec (AppTy ty1 ty2)
192 = maybeParen ctxt_prec tYCON_PREC $
193 ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
195 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
196 = ppr_ty ctxt_prec ty
197 -- = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
199 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
201 ppr_ty ctxt_prec (SourceTy (NType tc tys)) = ppr_tc_app ctxt_prec tc tys
202 ppr_ty ctxt_prec (SourceTy pred) = braces (pprPred pred)
204 ppr_tc_app ctxt_prec tc [] = ppr tc
205 ppr_tc_app ctxt_prec tc tys = maybeParen ctxt_prec tYCON_PREC
206 (sep [ppr tc, nest 4 (sep (map (ppr_ty tYCON_PREC) tys))])
210 %************************************************************************
212 \subsection[TyVar]{@TyVar@}
214 %************************************************************************
216 We print type-variable binders with their kinds in interface files,
217 and when in debug mode.
220 pprTyVarBndr :: TyVar -> SDoc
222 = getPprStyle $ \ sty ->
223 if debugStyle sty then
224 hsep [ppr tyvar, dcolon, pprParendKind kind]
225 -- See comments with ppDcolon in PprCore.lhs
229 kind = tyVarKind tyvar
231 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
235 %************************************************************************
237 \subsection{Mumbo jumbo}
239 %************************************************************************
241 Grab a name for the type. This is used to determine the type
242 description for profiling.
245 getTyDescription :: Type -> String
248 = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
251 AppTy fun _ -> getTyDescription fun
252 FunTy _ res -> '-' : '>' : fun_result res
253 TyConApp tycon _ -> occNameUserString (getOccName tycon)
254 NoteTy (FTVNote _) ty -> getTyDescription ty
255 NoteTy (SynNote ty1) _ -> getTyDescription ty1
256 SourceTy sty -> getSourceTyDescription sty
257 ForAllTy _ ty -> getTyDescription ty
260 fun_result (FunTy _ res) = '>' : fun_result res
261 fun_result other = getTyDescription other
263 getSourceTyDescription (ClassP cl tys) = getOccString cl
264 getSourceTyDescription (NType tc tys) = getOccString tc
265 getSourceTyDescription (IParam ip ty) = getOccString (ipNameName ip)
270 showTypeCategory :: Type -> Char
272 {C,I,F,D} char, int, float, double
274 S other single-constructor type
275 {c,i,f,d} unboxed ditto
277 s *unpacked" single-cons...
283 + dictionary, unless it's a ...
286 M other (multi-constructor) data-con type
288 - reserved for others to mark as "uninteresting"
294 case tcSplitTyConApp_maybe ty of
295 Nothing -> if maybeToBool (tcSplitFunTy_maybe ty)
300 let utc = getUnique tycon in
301 if utc == charDataConKey then 'C'
302 else if utc == intDataConKey then 'I'
303 else if utc == floatDataConKey then 'F'
304 else if utc == doubleDataConKey then 'D'
305 else if utc == smallIntegerDataConKey ||
306 utc == largeIntegerDataConKey then 'J'
307 else if utc == charPrimTyConKey then 'c'
308 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
309 || utc == addrPrimTyConKey) then 'i'
310 else if utc == floatPrimTyConKey then 'f'
311 else if utc == doublePrimTyConKey then 'd'
312 else if isPrimTyCon tycon {- array, we hope -} then 'A' -- Bogus
313 else if isEnumerationTyCon tycon then 'E'
314 else if isTupleTyCon tycon then 'T'
315 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
316 else if utc == listTyConKey then 'L'
317 else 'M' -- oh, well...