2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[PprType]{Printing Types, TyVars, Classes, ClassOps, TyCons}
7 #include "HsVersions.h"
10 GenTyVar, pprGenTyVar,
11 TyCon, pprTyCon, showTyCon,
13 pprGenType, pprParendGenType,
14 pprType, pprParendType,
20 GenClassOp, pprGenClassOp,
24 nmbrType, nmbrTyCon, nmbrClass
28 import IdLoop -- for paranoia checking
29 import TyLoop -- for paranoia checking
32 -- (PprType can see all the representations it's trying to print)
33 import Type ( GenType(..), maybeAppTyCon,
34 splitForAllTy, splitSigmaTy, splitRhoTy, splitAppTy )
35 import TyVar ( GenTyVar(..) )
36 import TyCon ( TyCon(..), NewOrData )
37 import Class ( Class(..), GenClass(..),
38 ClassOp(..), GenClassOp(..) )
39 import Kind ( Kind(..) )
40 import Usage ( GenUsage(..) )
43 import CStrings ( identToC )
44 import CmdLineOpts ( opt_OmitInterfacePragmas )
45 import Maybes ( maybeToBool )
46 import Name ( isLexVarSym, isLexSpecialSym, isPreludeDefined, origName, moduleOf,
47 nameOrigName, nameOf, Name{-instance Outputable-}
49 import Outputable ( ifPprShowAll, interpp'SP )
51 import PprStyle ( PprStyle(..), codeStyle, showUserishTypes )
53 import TysWiredIn ( listTyCon )
54 import UniqFM ( addToUFM_Directly, lookupUFM_Directly, ufmToList{-ToDo:rm-} )
55 import Unique ( pprUnique10, pprUnique, incrUnique )
56 import Usage ( UVar(..), pprUVar )
61 instance (Eq tyvar, Outputable tyvar,
62 Eq uvar, Outputable uvar ) => Outputable (GenType tyvar uvar) where
63 ppr sty ty = pprGenType sty ty
65 instance Outputable TyCon where
66 ppr sty tycon = pprTyCon sty tycon
68 instance Outputable (GenClass tyvar uvar) where
69 -- we use pprIfaceClass for printing in interfaces
70 ppr sty (Class u n _ _ _ _ _ _ _ _) = ppr sty n
72 instance Outputable ty => Outputable (GenClassOp ty) where
73 ppr sty clsop = pprGenClassOp sty clsop
75 instance Outputable (GenTyVar flexi) where
76 ppr sty tv = pprGenTyVar sty tv
78 -- and two SPECIALIZEd ones:
79 instance Outputable {-Type, i.e.:-}(GenType TyVar UVar) where
80 ppr sty ty = pprGenType sty ty
82 instance Outputable {-TyVar, i.e.:-}(GenTyVar Usage) where
83 ppr sty ty = pprGenTyVar sty ty
86 %************************************************************************
88 \subsection[Type]{@Type@}
90 %************************************************************************
92 @pprGenType@ is the std @Type@ printer; the overloaded @ppr@ function is
93 defined to use this. @pprParendGenType@ is the same, except it puts
94 parens around the type, except for the atomic cases. @pprParendGenType@
95 works just by setting the initial context precedence very high.
98 pprGenType, pprParendGenType :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
99 => PprStyle -> GenType tyvar uvar -> Pretty
101 pprGenType sty ty = ppr_ty sty (init_ppr_env sty) tOP_PREC ty
102 pprParendGenType sty ty = ppr_ty sty (init_ppr_env sty) tYCON_PREC ty
104 pprType sty ty = ppr_ty sty (init_ppr_env sty) tOP_PREC (ty :: Type)
105 pprParendType sty ty = ppr_ty sty (init_ppr_env sty) tYCON_PREC (ty :: Type)
107 pprMaybeTy :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
108 => PprStyle -> Maybe (GenType tyvar uvar) -> Pretty
109 pprMaybeTy sty Nothing = ppChar '*'
110 pprMaybeTy sty (Just ty) = pprParendGenType sty ty
114 ppr_ty :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
115 => PprStyle -> PprEnv tyvar uvar bndr occ -> Int
116 -> GenType tyvar uvar
119 ppr_ty sty env ctxt_prec (TyVarTy tyvar)
120 = ppr_tyvar env tyvar
122 ppr_ty sty env ctxt_prec (TyConTy tycon usage)
125 ppr_ty sty env ctxt_prec ty@(ForAllTy _ _)
126 | showUserishTypes sty = ppr_ty sty env' ctxt_prec body_ty
128 | otherwise = ppSep [ ppPStr SLIT("_forall_"),
129 ppIntersperse pp'SP pp_tyvars,
131 ppr_ty sty env' ctxt_prec body_ty
134 (tyvars, body_ty) = splitForAllTy ty
135 env' = foldl add_tyvar env tyvars
136 pp_tyvars = map (ppr_tyvar env') tyvars
138 ppr_ty sty env ctxt_prec (ForAllUsageTy uv uvs ty)
139 = panic "ppr_ty:ForAllUsageTy"
141 ppr_ty sty env ctxt_prec ty@(FunTy (DictTy _ _ _) _ _)
142 | showUserishTypes sty
143 -- Print a nice looking context (Eq a, Text b) => ...
144 = ppSep [ppBeside (ppr_theta theta) (ppPStr SLIT(" =>")),
145 ppr_ty sty env ctxt_prec body_ty
148 (theta, body_ty) = splitRhoTy ty
150 ppr_theta [ct] = ppr_dict sty env tOP_PREC ct
151 ppr_theta cts = ppParens (ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts))
153 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
154 -- We fiddle the precedences passed to left/right branches,
155 -- so that right associativity comes out nicely...
156 = maybeParen ctxt_prec fUN_PREC
157 (ppCat [ppr_ty sty env fUN_PREC ty1,
159 ppr_ty sty env tOP_PREC ty2])
161 ppr_ty sty env ctxt_prec ty@(AppTy _ _)
162 = ppr_corner sty env ctxt_prec fun_ty arg_tys
164 (fun_ty, arg_tys) = splitAppTy ty
166 ppr_ty PprInterface env ctxt_prec (SynTy tycon tys expansion)
167 -- always expand types in an interface
168 = ppr_ty PprInterface env ctxt_prec expansion
170 ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
172 (ppr_app sty env ctxt_prec (ppr sty tycon) tys)
173 (ifPprShowAll sty (ppCat [ppStr " {- expansion:",
174 ppr_ty sty env tOP_PREC expansion,
177 ppr_ty sty env ctxt_prec (DictTy clas ty usage)
178 = ppr_dict sty env ctxt_prec (clas, ty)
181 -- Some help functions
182 ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
183 | length arg_tys == 2
184 = ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
186 (ty1:ty2:_) = arg_tys
188 ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
189 = --ASSERT(length arg_tys == a)
190 (if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
191 ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
193 arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty sty env tOP_PREC) arg_tys)
195 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
197 = ASSERT(length arg_tys == 1)
198 ppBesides [ppLbrack, ppr_ty sty env tOP_PREC ty1, ppRbrack]
202 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
203 = ppr_app sty env ctxt_prec (ppr sty tycon) arg_tys
205 ppr_corner sty env ctxt_prec (TyVarTy tyvar) arg_tys
206 = ppr_app sty env ctxt_prec (ppr_tyvar env tyvar) arg_tys
209 ppr_app sty env ctxt_prec pp_fun []
211 ppr_app sty env ctxt_prec pp_fun arg_tys
212 = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
214 arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty sty env tYCON_PREC) arg_tys)
217 ppr_dict sty env ctxt_prec (clas, ty)
218 = maybeParen ctxt_prec tYCON_PREC
219 (ppCat [ppr sty clas, ppr_ty sty env tYCON_PREC ty])
222 This stuff is effectively stubbed out for the time being
226 = initPprEnv sty b b b b b b b b b b b
228 b = panic "PprType:init_ppr_env"
230 ppr_tyvar env tyvar = ppr (pStyle env) tyvar
231 ppr_uvar env uvar = ppr (pStyle env) uvar
233 add_tyvar env tyvar = env
234 add_uvar env uvar = env
237 @ppr_ty@ takes an @Int@ that is the precedence of the context.
238 The precedence levels are:
240 \item[0:] What we start with.
241 \item[1:] Function application (@FunTys@).
242 \item[2:] Type constructors.
247 tOP_PREC = (0 :: Int)
248 fUN_PREC = (1 :: Int)
249 tYCON_PREC = (2 :: Int)
251 maybeParen ctxt_prec inner_prec pretty
252 | ctxt_prec < inner_prec = pretty
253 | otherwise = ppParens pretty
256 %************************************************************************
258 \subsection[TyVar]{@TyVar@}
260 %************************************************************************
263 pprGenTyVar sty (TyVar uniq kind name usage)
266 _ -> ppBesides [pp_name, ppStr "{-", pp_u, ppStr "-}"]
268 pp_u = pprUnique uniq
269 pp_name = case name of
270 Just n -> ppPStr (nameOf (nameOrigName n))
271 Nothing -> case kind of
272 TypeKind -> ppChar 'o'
273 BoxedTypeKind -> ppChar 't'
274 UnboxedTypeKind -> ppChar 'u'
275 ArrowKind _ _ -> ppChar 'a'
278 %************************************************************************
280 \subsection[TyCon]{@TyCon@}
282 %************************************************************************
284 ToDo; all this is suspiciously like getOccName!
287 showTyCon :: PprStyle -> TyCon -> String
288 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
290 pprTyCon :: PprStyle -> TyCon -> Pretty
292 pprTyCon sty FunTyCon = ppStr "(->)"
293 pprTyCon sty (TupleTyCon _ name _) = ppr sty name
294 pprTyCon sty (PrimTyCon uniq name kind) = ppr sty name
296 pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
299 pprTyCon sty (SpecTyCon tc ty_maybes)
300 = ppBeside (pprTyCon sty tc)
302 then identToC tys_stuff
303 else ppPStr tys_stuff)
305 tys_stuff = specMaybeTysSuffix ty_maybes
307 pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
308 = ppBeside (ppr sty name)
310 (ppCat [ ppStr " {-",
312 interpp'SP sty tyvars,
313 pprParendGenType sty expansion,
318 %************************************************************************
320 \subsection[Class]{@Class@}
322 %************************************************************************
325 pprGenClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
327 pprGenClassOp sty op = ppr_class_op sty [] op
329 ppr_class_op sty tyvars (ClassOp op_name i ty)
332 PprForAsm _ _ -> pp_C
333 PprInterface -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
334 PprShowAll -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
337 pp_C = ppPStr op_name
338 pp_user = if isLexVarSym op_name && not (isLexSpecialSym op_name)
344 %************************************************************************
346 \subsection{Mumbo jumbo}
348 %************************************************************************
351 -- Shallowly magical; converts a type into something
352 -- vaguely close to what can be used in C identifier.
353 -- Don't forget to include the module name!!!
354 getTypeString :: Type -> [FAST_STRING]
356 | is_prelude_ty = [string]
357 | otherwise = [mod, string]
359 string = _PK_ (tidy (ppShow 1000 ppr_t))
360 ppr_t = pprGenType PprForC ty
361 -- PprForC expands type synonyms as it goes
364 = case (maybeAppTyCon ty) of
365 Nothing -> true_bottom
367 if isPreludeDefined tycon
369 else (False, moduleOf (origName tycon))
371 true_bottom = (True, panic "getTypeString")
373 --------------------------------------------------
380 '-' : '>' : xs -> '-' : '>' : tidy (no_leading_sps xs)
381 other -> ' ' : tidy more
383 tidy (',' : more) = ',' : tidy (no_leading_sps more)
385 tidy (x : xs) = x : tidy xs -- catch all
387 no_leading_sps [] = []
388 no_leading_sps (' ':xs) = no_leading_sps xs
389 no_leading_sps other = other
391 typeMaybeString :: Maybe Type -> [FAST_STRING]
392 typeMaybeString Nothing = [SLIT("!")]
393 typeMaybeString (Just t) = getTypeString t
395 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
396 specMaybeTysSuffix ty_maybes
398 ty_strs = concat (map typeMaybeString ty_maybes)
399 dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
406 nmbrType :: Type -> NmbrM Type
408 nmbrType (TyVarTy tv)
409 = nmbrTyVar tv `thenNmbr` \ new_tv ->
410 returnNmbr (TyVarTy new_tv)
412 nmbrType (AppTy t1 t2)
413 = nmbrType t1 `thenNmbr` \ new_t1 ->
414 nmbrType t2 `thenNmbr` \ new_t2 ->
415 returnNmbr (AppTy new_t1 new_t2)
417 nmbrType (TyConTy tc use)
418 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
419 nmbrUsage use `thenNmbr` \ new_use ->
420 returnNmbr (TyConTy tc new_use)
422 nmbrType (SynTy tc args expand)
423 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
424 mapNmbr nmbrType args `thenNmbr` \ new_args ->
425 nmbrType expand `thenNmbr` \ new_expand ->
426 returnNmbr (SynTy tc new_args new_expand)
428 nmbrType (ForAllTy tv ty)
429 = addTyVar tv `thenNmbr` \ new_tv ->
430 nmbrType ty `thenNmbr` \ new_ty ->
431 returnNmbr (ForAllTy new_tv new_ty)
433 nmbrType (ForAllUsageTy u us ty)
434 = addUVar u `thenNmbr` \ new_u ->
435 mapNmbr nmbrUVar us `thenNmbr` \ new_us ->
436 nmbrType ty `thenNmbr` \ new_ty ->
437 returnNmbr (ForAllUsageTy new_u new_us new_ty)
439 nmbrType (FunTy t1 t2 use)
440 = nmbrType t1 `thenNmbr` \ new_t1 ->
441 nmbrType t2 `thenNmbr` \ new_t2 ->
442 nmbrUsage use `thenNmbr` \ new_use ->
443 returnNmbr (FunTy new_t1 new_t2 new_use)
445 nmbrType (DictTy c ty use)
446 = --nmbrClass c `thenNmbr` \ new_c ->
447 nmbrType ty `thenNmbr` \ new_ty ->
448 nmbrUsage use `thenNmbr` \ new_use ->
449 returnNmbr (DictTy c new_ty new_use)
453 addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
455 addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
456 = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
457 case (lookupUFM_Directly tvenv u) of
458 Just xx -> pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
462 nenv_plus_tv = NmbrEnv ui (incrUnique ut) uu
464 (addToUFM_Directly tvenv u new_tv)
467 (nenv2, new_use) = nmbrUsage use nenv_plus_tv
469 new_tv = TyVar ut k maybe_name new_use
473 nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
474 = case (lookupUFM_Directly tvenv u) of
475 Just xx -> (nenv, xx)
477 pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
481 nmbrTyCon : only called from ``top-level'', if you know what I mean.
483 nmbrTyCon tc@FunTyCon = returnNmbr tc
484 nmbrTyCon tc@(TupleTyCon _ _ _) = returnNmbr tc
485 nmbrTyCon tc@(PrimTyCon _ _ _) = returnNmbr tc
487 nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
488 = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
489 mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
490 mapNmbr nmbr_theta theta `thenNmbr` \ new_theta ->
491 mapNmbr nmbrId cons `thenNmbr` \ new_cons ->
492 returnNmbr (DataTyCon u n k new_tvs new_theta new_cons clss nod)
495 = --nmbrClass c `thenNmbr` \ new_c ->
496 nmbrType t `thenNmbr` \ new_t ->
497 returnNmbr (c, new_t)
499 nmbrTyCon (SynTyCon u n k a tvs expand)
500 = mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
501 nmbrType expand `thenNmbr` \ new_expand ->
502 returnNmbr (SynTyCon u n k a new_tvs new_expand)
504 nmbrTyCon (SpecTyCon tc specs)
505 = mapNmbr nmbrMaybeTy specs `thenNmbr` \ new_specs ->
506 returnNmbr (SpecTyCon tc new_specs)
509 nmbrMaybeTy Nothing = returnNmbr Nothing
510 nmbrMaybeTy (Just t) = nmbrType t `thenNmbr` \ new_t ->
511 returnNmbr (Just new_t)
515 nmbrClass (Class u n tv supers ssels ops osels odefms instenv isupers)
516 = addTyVar tv `thenNmbr` \ new_tv ->
517 mapNmbr nmbr_op ops `thenNmbr` \ new_ops ->
518 returnNmbr (Class u n new_tv supers ssels new_ops osels odefms instenv isupers)
520 nmbr_op (ClassOp n tag ty)
521 = nmbrType ty `thenNmbr` \ new_ty ->
522 returnNmbr (ClassOp n tag new_ty)
526 nmbrUsage :: Usage -> NmbrM Usage
528 nmbrUsage u = returnNmbr u
530 nmbrUsage u@UsageOne = returnNmbr u
531 nmbrUsage u@UsageOmega = returnNmbr u
532 nmbrUsage (UsageVar u)
533 = nmbrUVar u `thenNmbr` \ new_u ->
534 returnNmbr (UsageVar new_u)
539 addUVar, nmbrUVar :: UVar -> NmbrM UVar
541 addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
542 = case (lookupUFM_Directly uvenv u) of
543 Just xx -> _trace "addUVar: already in map!" $
547 nenv_plus_uv = NmbrEnv ui ut (incrUnique uu)
550 (addToUFM_Directly uvenv u new_uv)
553 (nenv_plus_uv, new_uv)
555 nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
556 = case (lookupUFM_Directly uvenv u) of
557 Just xx -> (nenv, xx)
559 _trace "nmbrUVar: lookup failed" $