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(..), isUTyVar, 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 CmdLineOpts ( opt_PprStyle_RawTypes )
37 import Maybes ( maybeToBool )
38 import Name ( getOccString, getOccName )
39 import OccName ( occNameUserString )
41 import Unique ( Uniquable(..) )
42 import Util ( lengthIs )
43 import BasicTypes ( IPName(..), tupleParens, ipNameName )
44 import PrelNames -- 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 tOP_PREC ty
61 pprParendType ty = ppr_ty tYCON_PREC ty
63 pprKind, pprParendKind :: Kind -> SDoc
65 pprParendKind = pprParendType
67 pprPred :: PredType -> SDoc
68 pprPred = pprSourceType
70 pprSourceType :: SourceType -> SDoc
71 pprSourceType (ClassP clas tys) = pprClassPred clas tys
72 pprSourceType (IParam n ty) = hsep [ppr n, dcolon, ppr ty]
73 pprSourceType (NType tc tys) = ppr tc <+> hsep (map pprParendType tys)
75 pprClassPred :: Class -> [Type] -> SDoc
76 pprClassPred 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
84 instance Outputable SourceType where
87 instance Outputable name => Outputable (IPName name) where
88 ppr (Dupable n) = char '?' <> ppr n -- Ordinary implicit parameters
89 ppr (Linear n) = char '%' <> ppr n -- Splittable implicit parameters
93 %************************************************************************
95 \subsection{Pretty printing}
97 %************************************************************************
101 @ppr_ty@ takes an @Int@ that is the precedence of the context.
102 The precedence levels are:
104 \item[tOP_PREC] No parens required.
105 \item[fUN_PREC] Left hand argument of a function arrow.
106 \item[tYCON_PREC] Argument of a type constructor.
111 tOP_PREC = (0 :: Int) -- type in ParseIface.y
112 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
113 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
115 maybeParen ctxt_prec inner_prec pretty
116 | ctxt_prec < inner_prec = pretty
117 | otherwise = parens pretty
121 ppr_ty :: Int -> Type -> SDoc
122 ppr_ty ctxt_prec (TyVarTy tyvar)
125 ppr_ty ctxt_prec ty@(TyConApp tycon tys)
126 -- KIND CASE; it's of the form (Type x)
127 | tycon `hasKey` typeConKey,
129 = -- For kinds, print (Type x) as just x if x is a
130 -- type constructor (must be Boxed, Unboxed, AnyBox)
131 -- Otherwise print as (Type x)
133 TyConApp bx [] -> ppr (getOccName bx) -- Always unqualified
134 other -> maybeParen ctxt_prec tYCON_PREC
135 (ppr tycon <+> ppr_ty tYCON_PREC ty)
138 | (tycon `hasKey` usOnceTyConKey || tycon `hasKey` usManyTyConKey),
140 = -- For usages (! and .), always print bare OccName, without pkg/mod/uniq
141 ppr (getOccName (tyConName tycon))
143 -- TUPLE CASE (boxed and unboxed)
144 | isTupleTyCon tycon,
145 tys `lengthIs` tyConArity tycon -- No magic if partially applied
146 = tupleParens (tupleTyConBoxity tycon)
147 (sep (punctuate comma (map (ppr_ty tOP_PREC) tys)))
150 | tycon `hasKey` listTyConKey,
152 = brackets (ppr_ty tOP_PREC ty)
156 = ppr_tc_app ctxt_prec tycon tys
159 ppr_ty ctxt_prec ty@(ForAllTy _ _)
160 = getPprStyle $ \ sty ->
161 maybeParen ctxt_prec fUN_PREC $
162 sep [ ptext SLIT("forall") <+> pp_tyvars sty <> ptext SLIT("."),
167 (tyvars, theta, tau) = tcSplitSigmaTy ty
169 pp_tyvars sty = sep (map pprTyVarBndr some_tyvars)
171 some_tyvars | userStyle sty && not opt_PprStyle_RawTypes
172 = filter (not . isUTyVar) tyvars -- hide uvars from user
177 ppr_theta theta = pprTheta theta <+> ptext SLIT("=>")
180 ppr_ty ctxt_prec (FunTy ty1 ty2)
181 -- we don't want to lose usage annotations or synonyms,
182 -- so we mustn't use splitFunTys here.
183 = maybeParen ctxt_prec fUN_PREC $
184 sep [ ppr_ty fUN_PREC ty1
185 , ptext arrow <+> ppr_ty tOP_PREC ty2
187 where arrow | isPredTy ty1 = SLIT("=>")
188 | otherwise = SLIT("->")
190 ppr_ty ctxt_prec (AppTy ty1 ty2)
191 = maybeParen ctxt_prec tYCON_PREC $
192 ppr_ty fUN_PREC ty1 <+> ppr_ty tYCON_PREC ty2
194 ppr_ty ctxt_prec (UsageTy u ty)
195 = maybeParen ctxt_prec tYCON_PREC $
196 ptext SLIT("__u") <+> ppr_ty tYCON_PREC u
197 <+> ppr_ty tYCON_PREC ty
198 -- fUN_PREC would be logical for u, but it yields a reduce/reduce conflict with AppTy
200 ppr_ty ctxt_prec (NoteTy (SynNote ty) expansion)
201 = ppr_ty ctxt_prec ty
202 -- = ppr_ty ctxt_prec expansion -- if we don't want to see syntys
204 ppr_ty ctxt_prec (NoteTy (FTVNote _) ty) = ppr_ty ctxt_prec ty
206 ppr_ty ctxt_prec (SourceTy (NType tc tys)) = ppr_tc_app ctxt_prec tc tys
207 ppr_ty ctxt_prec (SourceTy pred) = braces (pprPred pred)
209 ppr_tc_app ctxt_prec tc [] = ppr tc
210 ppr_tc_app ctxt_prec tc tys = maybeParen ctxt_prec tYCON_PREC
211 (sep [ppr tc, nest 4 (sep (map (ppr_ty tYCON_PREC) tys))])
215 %************************************************************************
217 \subsection[TyVar]{@TyVar@}
219 %************************************************************************
221 We print type-variable binders with their kinds in interface files,
222 and when in debug mode.
225 pprTyVarBndr :: TyVar -> SDoc
227 = getPprStyle $ \ sty ->
228 if (ifaceStyle sty && not (kind `eqKind` liftedTypeKind)) || debugStyle sty then
229 hsep [ppr tyvar, dcolon, pprParendKind kind]
230 -- See comments with ppDcolon in PprCore.lhs
234 kind = tyVarKind tyvar
236 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
240 %************************************************************************
242 \subsection{Mumbo jumbo}
244 %************************************************************************
246 Grab a name for the type. This is used to determine the type
247 description for profiling.
250 getTyDescription :: Type -> String
253 = case (tcSplitSigmaTy ty) of { (_, _, tau_ty) ->
256 AppTy fun _ -> getTyDescription fun
257 FunTy _ res -> '-' : '>' : fun_result res
258 TyConApp tycon _ -> occNameUserString (getOccName tycon)
259 NoteTy (FTVNote _) ty -> getTyDescription ty
260 NoteTy (SynNote ty1) _ -> getTyDescription ty1
261 SourceTy sty -> getSourceTyDescription sty
262 ForAllTy _ ty -> getTyDescription ty
265 fun_result (FunTy _ res) = '>' : fun_result res
266 fun_result other = getTyDescription other
268 getSourceTyDescription (ClassP cl tys) = getOccString cl
269 getSourceTyDescription (NType tc tys) = getOccString tc
270 getSourceTyDescription (IParam ip ty) = getOccString (ipNameName ip)
275 showTypeCategory :: Type -> Char
277 {C,I,F,D} char, int, float, double
279 S other single-constructor type
280 {c,i,f,d} unboxed ditto
282 s *unpacked" single-cons...
288 + dictionary, unless it's a ...
291 M other (multi-constructor) data-con type
293 - reserved for others to mark as "uninteresting"
299 case tcSplitTyConApp_maybe ty of
300 Nothing -> if maybeToBool (tcSplitFunTy_maybe ty)
305 let utc = getUnique tycon in
306 if utc == charDataConKey then 'C'
307 else if utc == intDataConKey then 'I'
308 else if utc == floatDataConKey then 'F'
309 else if utc == doubleDataConKey then 'D'
310 else if utc == smallIntegerDataConKey ||
311 utc == largeIntegerDataConKey then 'J'
312 else if utc == charPrimTyConKey then 'c'
313 else if (utc == intPrimTyConKey || utc == wordPrimTyConKey
314 || utc == addrPrimTyConKey) then 'i'
315 else if utc == floatPrimTyConKey then 'f'
316 else if utc == doublePrimTyConKey then 'd'
317 else if isPrimTyCon tycon {- array, we hope -} then 'A' -- Bogus
318 else if isEnumerationTyCon tycon then 'E'
319 else if isTupleTyCon tycon then 'T'
320 else if maybeToBool (maybeTyConSingleCon tycon) then 'S'
321 else if utc == listTyConKey then 'L'
322 else 'M' -- oh, well...