2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[PprType]{Printing Types, TyVars, Classes, TyCons}
8 pprTyVar, pprTyVarBndr, pprTyVarBndrs,
9 TyCon, pprTyCon, showTyCon,
10 pprType, pprParendType,
13 pprConstraint, pprTheta,
15 nmbrType, nmbrGlobalType
18 #include "HsVersions.h"
21 -- (PprType can see all the representations it's trying to print)
22 import Type ( GenType(..), Type, ThetaType, splitFunTys, splitDictTy_maybe,
23 splitForAllTys, splitSigmaTy, splitRhoTy, splitAppTys )
24 import TyVar ( GenTyVar(..), TyVar, cloneTyVar )
25 import TyCon ( TyCon, NewOrData, isFunTyCon, isTupleTyCon, tyConArity )
26 import Class ( Class )
27 import Kind ( GenKind(..), isBoxedTypeKind, pprParendKind )
30 import CmdLineOpts ( opt_PprUserLength )
31 import Maybes ( maybeToBool )
32 import Name ( nameString, pprOccName, getOccString, OccName, NamedThing(..) )
35 import BasicTypes ( Unused )
36 import UniqFM ( UniqFM, addToUFM, emptyUFM, lookupUFM )
37 import Unique ( Unique, Uniquable(..), pprUnique,
38 incrUnique, listTyConKey, initTyVarUnique
44 instance Outputable (GenType flexi) where
47 instance Outputable TyCon where
48 ppr tycon = pprTyCon tycon
50 instance Outputable Class where
51 -- we use pprIfaceClass for printing in interfaces
52 ppr clas = ppr (getName clas)
54 instance Outputable (GenTyVar flexi) where
58 %************************************************************************
60 \subsection[Type]{@Type@}
62 %************************************************************************
66 @ppr_ty@ takes an @Int@ that is the precedence of the context.
67 The precedence levels are:
69 \item[tOP_PREC] No parens required.
70 \item[fUN_PREC] Left hand argument of a function arrow.
71 \item[tYCON_PREC] Argument of a type constructor.
78 tYCON_PREC = (2 :: Int)
80 maybeParen ctxt_prec inner_prec pretty
81 | ctxt_prec < inner_prec = pretty
82 | otherwise = parens pretty
85 @pprType@ is the std @Type@ printer; the overloaded @ppr@ function is
86 defined to use this. @pprParendType@ is the same, except it puts
87 parens around the type, except for the atomic cases. @pprParendType@
88 works just by setting the initial context precedence very high.
91 pprType, pprParendType :: GenType flexi -> SDoc
93 pprType ty = ppr_ty init_ppr_env tOP_PREC ty
94 pprParendType ty = ppr_ty init_ppr_env tYCON_PREC ty
96 pprConstraint :: Class -> [GenType flexi] -> SDoc
97 pprConstraint clas tys = hsep [ppr clas, hsep (map (pprParendType) tys)]
99 pprTheta :: ThetaType -> SDoc
100 pprTheta theta = parens (hsep (punctuate comma (map ppr_dict theta)))
102 ppr_dict (c,tys) = pprConstraint c tys
104 pprMaybeTy :: Maybe (GenType flexi) -> SDoc
105 pprMaybeTy Nothing = char '*'
106 pprMaybeTy (Just ty) = pprParendType ty
110 ppr_ty :: PprEnv flexi bndr occ -> Int
114 ppr_ty env ctxt_prec (TyVarTy tyvar)
118 ppr_ty env ctxt_prec (TyConApp tycon tys)
120 && length tys == tyConArity tycon -- no magic if partially applied
121 = parens tys_w_commas
123 tys_w_commas = hsep (punctuate comma (map (ppr_ty env tOP_PREC) tys))
126 ppr_ty env ctxt_prec (TyConApp tycon [ty])
127 | uniqueOf tycon == listTyConKey
128 = brackets (ppr_ty env tOP_PREC ty)
130 -- DICTIONARY CASE, prints {C a}
131 -- This means that instance decls come out looking right in interfaces
132 -- and that in turn means they get "gated" correctly when being slurped in
133 ppr_ty env ctxt_prec ty@(TyConApp tycon tys)
134 | maybeToBool maybe_dict
135 = braces (ppr_dict env tYCON_PREC ctys)
137 Just ctys = maybe_dict
138 maybe_dict = splitDictTy_maybe ty
140 -- NO-ARGUMENT CASE (=> no parens)
141 ppr_ty env ctxt_prec (TyConApp tycon [])
142 = ppr_tycon env tycon
145 ppr_ty env ctxt_prec (TyConApp tycon tys)
146 = maybeParen ctxt_prec tYCON_PREC (hsep [ppr_tycon env tycon, tys_w_spaces])
148 tys_w_spaces = hsep (map (ppr_ty env tYCON_PREC) tys)
151 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
152 = getPprStyle $ \ sty ->
154 (tyvars, rho_ty) = splitForAllTys ty
155 (theta, body_ty) | show_context = splitRhoTy rho_ty
156 | otherwise = ([], rho_ty)
158 pp_tyvars = brackets (hsep (map (pTyVarB env) tyvars))
159 pp_body = ppr_ty env tOP_PREC body_ty
161 show_forall = not (userStyle sty)
162 show_context = ifaceStyle sty || userStyle sty
165 maybeParen ctxt_prec fUN_PREC $
166 sep [ ptext SLIT("_forall_"), pp_tyvars,
167 ppr_theta env theta, ptext SLIT("=>"), pp_body
170 else if null theta then
171 ppr_ty env ctxt_prec body_ty
174 maybeParen ctxt_prec fUN_PREC $
175 sep [ppr_theta env theta, ptext SLIT("=>"), pp_body]
177 ppr_ty env ctxt_prec (FunTy ty1 ty2)
178 -- We fiddle the precedences passed to left/right branches,
179 -- so that right associativity comes out nicely...
180 = maybeParen ctxt_prec fUN_PREC (sep (ppr_ty env fUN_PREC ty1 : pp_rest))
182 (arg_tys, result_ty) = splitFunTys ty2
183 pp_rest = [ ptext SLIT("-> ") <> ppr_ty env fUN_PREC ty | ty <- arg_tys ++ [result_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 (SynTy ty expansion)
190 = ppr_ty env ctxt_prec ty
192 ppr_theta env [] = empty
193 ppr_theta env theta = braces (hsep (punctuate comma (map (ppr_dict env tOP_PREC) theta)))
195 ppr_dict env ctxt (clas, tys) = ppr_class env clas <+>
196 hsep (map (ppr_ty env tYCON_PREC) tys)
201 = initPprEnv b b b b (Just pprTyVarBndr) (Just ppr) b b b
203 b = panic "PprType:init_ppr_env"
205 ppr_tycon env tycon = ppr tycon
206 ppr_class env clas = ppr clas
209 %************************************************************************
211 \subsection[TyVar]{@TyVar@}
213 %************************************************************************
216 pprTyVar (TyVar uniq kind maybe_name _)
218 -- If the tyvar has a name we can safely use just it, I think
219 Just n -> pprOccName (getOccName n) <> ifPprDebug pp_debug
220 Nothing -> pprUnique uniq
222 pp_debug = text "_" <> pp_kind <> pprUnique uniq
224 pp_kind = case kind of
226 BoxedTypeKind -> char 't'
227 UnboxedTypeKind -> char 'u'
228 ArrowKind _ _ -> char 'a'
231 We print type-variable binders with their kinds in interface files.
234 pprTyVarBndr tyvar@(TyVar uniq kind name _)
235 = getPprStyle $ \ sty ->
236 if ifaceStyle sty && not (isBoxedTypeKind kind) then
237 hcat [pprTyVar tyvar, text " :: ", pprParendKind kind]
238 -- See comments with ppDcolon in PprCore.lhs
242 pprTyVarBndrs tyvars = hsep (map pprTyVarBndr tyvars)
245 %************************************************************************
247 \subsection[TyCon]{@TyCon@}
249 %************************************************************************
251 ToDo; all this is suspiciously like getOccName!
254 showTyCon :: TyCon -> String
255 showTyCon tycon = showSDoc (pprTyCon tycon)
257 pprTyCon :: TyCon -> SDoc
258 pprTyCon tycon = ppr (getName tycon)
263 %************************************************************************
265 \subsection{Mumbo jumbo}
267 %************************************************************************
269 Grab a name for the type. This is used to determine the type
270 description for profiling.
272 getTyDescription :: Type -> String
275 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
278 AppTy fun _ -> getTyDescription fun
279 FunTy _ res -> '-' : '>' : fun_result res
280 TyConApp tycon _ -> getOccString tycon
281 SynTy ty1 _ -> getTyDescription ty1
282 ForAllTy _ ty -> getTyDescription ty
285 fun_result (FunTy _ res) = '>' : fun_result res
286 fun_result other = getTyDescription other
291 %************************************************************************
293 \subsection{Renumbering types}
295 %************************************************************************
297 We tend to {\em renumber} everything before printing, so that we get
298 consistent Uniques on everything from run to run.
302 nmbrGlobalType :: Type -> Type -- Renumber a top-level type
303 nmbrGlobalType ty = nmbrType (\tyvar -> tyvar) initTyVarUnique ty
305 nmbrType :: (TyVar -> TyVar) -- Mapping for free vars
310 nmbrType tyvar_env uniq ty
311 = initNmbr tyvar_env uniq (nmbrTy ty)
313 nmbrTy :: Type -> NmbrM Type
316 = lookupTyVar tv `thenNmbr` \ new_tv ->
317 returnNmbr (TyVarTy new_tv)
320 = nmbrTy t1 `thenNmbr` \ new_t1 ->
321 nmbrTy t2 `thenNmbr` \ new_t2 ->
322 returnNmbr (AppTy new_t1 new_t2)
324 nmbrTy (TyConApp tc tys)
325 = nmbrTys tys `thenNmbr` \ new_tys ->
326 returnNmbr (TyConApp tc new_tys)
328 nmbrTy (SynTy ty1 ty2)
329 = nmbrTy ty1 `thenNmbr` \ new_ty1 ->
330 nmbrTy ty2 `thenNmbr` \ new_ty2 ->
331 returnNmbr (SynTy new_ty1 new_ty2)
333 nmbrTy (ForAllTy tv ty)
334 = addTyVar tv $ \ new_tv ->
335 nmbrTy ty `thenNmbr` \ new_ty ->
336 returnNmbr (ForAllTy new_tv new_ty)
339 = nmbrTy t1 `thenNmbr` \ new_t1 ->
340 nmbrTy t2 `thenNmbr` \ new_t2 ->
341 returnNmbr (FunTy new_t1 new_t2)
344 nmbrTys tys = mapNmbr nmbrTy tys
346 lookupTyVar tyvar (NmbrEnv tv_fn tv_env) uniq
349 tyvar' = case lookupUFM tv_env tyvar of
350 Just tyvar' -> tyvar'
351 Nothing -> tv_fn tyvar
353 addTyVar tv m (NmbrEnv f_tv tv_ufm) u
356 nenv = NmbrEnv f_tv tv_ufm'
357 tv_ufm' = addToUFM tv_ufm tv tv'
358 tv' = cloneTyVar tv u
366 = NmbrEnv (TyVar -> TyVar) (UniqFM TyVar) -- Global and local map for tyvars
368 type NmbrM a = NmbrEnv -> Unique -> (Unique, a) -- Unique is name supply
370 initNmbr :: (TyVar -> TyVar) -> Unique -> NmbrM a -> a
371 initNmbr tyvar_env uniq m
373 init_nmbr_env = NmbrEnv tyvar_env emptyUFM
375 snd (m init_nmbr_env uniq)
377 returnNmbr x nenv u = (u, x)
386 mapNmbr f [] = returnNmbr []
388 = f x `thenNmbr` \ r ->
389 mapNmbr f xs `thenNmbr` \ rs ->