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,
22 addTyVar{-ToDo:don't export-}, nmbrTyVar,
24 nmbrType, nmbrTyCon, nmbrClass
28 IMPORT_DELOOPER(IdLoop) -- for paranoia checking
29 IMPORT_DELOOPER(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, origName, moduleOf,
47 getLocalName, 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, listTyConKey )
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 = case sty of { PprInterface -> ppr_theta_2 ; _ -> ppr_theta_1 }
152 ppr_theta_1 [ct] = ppr_dict sty env tOP_PREC ct
153 ppr_theta_1 cts = ppParens (ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts))
155 ppr_theta_2 cts = ppBesides [ppStr "{{", ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts), ppStr "}}"]
157 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
158 -- We fiddle the precedences passed to left/right branches,
159 -- so that right associativity comes out nicely...
160 = maybeParen ctxt_prec fUN_PREC
161 (ppCat [ppr_ty sty env fUN_PREC ty1,
163 ppr_ty sty env tOP_PREC ty2])
165 ppr_ty sty env ctxt_prec ty@(AppTy _ _)
166 = ppr_corner sty env ctxt_prec fun_ty arg_tys
168 (fun_ty, arg_tys) = splitAppTy ty
171 ppr_ty PprInterface env ctxt_prec (SynTy tycon tys expansion)
172 -- always expand types in an interface
173 = ppr_ty PprInterface env ctxt_prec expansion
176 ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
178 (ppr_app sty env ctxt_prec (ppr sty tycon) tys)
179 (ifPprShowAll sty (ppCat [ppStr " {- expansion:",
180 ppr_ty sty env tOP_PREC expansion,
183 ppr_ty sty env ctxt_prec (DictTy clas ty usage)
184 = ppr_dict sty env ctxt_prec (clas, ty)
187 -- Some help functions
188 ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
189 | length arg_tys == 2
190 = ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
192 (ty1:ty2:_) = arg_tys
194 ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
195 = --ASSERT(length arg_tys == a)
196 (if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
197 ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
199 arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty sty env tOP_PREC) arg_tys)
201 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
203 = ASSERT(length arg_tys == 1)
204 ppBesides [ppLbrack, ppr_ty sty env tOP_PREC ty1, ppRbrack]
208 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
209 = ppr_app sty env ctxt_prec (ppr sty tycon) arg_tys
211 ppr_corner sty env ctxt_prec (TyVarTy tyvar) arg_tys
212 = ppr_app sty env ctxt_prec (ppr_tyvar env tyvar) arg_tys
215 ppr_app sty env ctxt_prec pp_fun []
217 ppr_app sty env ctxt_prec pp_fun arg_tys
218 = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
220 arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty sty env tYCON_PREC) arg_tys)
223 ppr_dict sty env ctxt_prec (clas, ty)
224 = maybeParen ctxt_prec tYCON_PREC
225 (ppCat [ppr sty clas, ppr_ty sty env tYCON_PREC ty])
228 This stuff is effectively stubbed out for the time being
232 = initPprEnv sty b b b b b b b b b b b
234 b = panic "PprType:init_ppr_env"
236 ppr_tyvar env tyvar = ppr (pStyle env) tyvar
237 ppr_uvar env uvar = ppr (pStyle env) uvar
239 add_tyvar env tyvar = env
240 add_uvar env uvar = env
243 @ppr_ty@ takes an @Int@ that is the precedence of the context.
244 The precedence levels are:
246 \item[0:] What we start with.
247 \item[1:] Function application (@FunTys@).
248 \item[2:] Type constructors.
253 tOP_PREC = (0 :: Int)
254 fUN_PREC = (1 :: Int)
255 tYCON_PREC = (2 :: Int)
257 maybeParen ctxt_prec inner_prec pretty
258 | ctxt_prec < inner_prec = pretty
259 | otherwise = ppParens pretty
262 %************************************************************************
264 \subsection[TyVar]{@TyVar@}
266 %************************************************************************
269 pprGenTyVar sty (TyVar uniq kind name usage)
272 _ -> ppBesides [pp_name, ppStr "{-", pp_u, ppStr "-}"]
274 pp_u = pprUnique uniq
275 pp_name = case name of
276 Just n -> ppPStr (getLocalName n)
277 Nothing -> case kind of
278 TypeKind -> ppChar 'o'
279 BoxedTypeKind -> ppChar 't'
280 UnboxedTypeKind -> ppChar 'u'
281 ArrowKind _ _ -> ppChar 'a'
284 %************************************************************************
286 \subsection[TyCon]{@TyCon@}
288 %************************************************************************
290 ToDo; all this is suspiciously like getOccName!
293 showTyCon :: PprStyle -> TyCon -> String
294 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
296 maybe_code sty = if codeStyle sty then identToC else ppPStr
298 pprTyCon :: PprStyle -> TyCon -> Pretty
300 pprTyCon sty (PrimTyCon _ name _ _) = ppr sty name
302 pprTyCon sty FunTyCon = maybe_code sty SLIT("(->)")
303 pprTyCon sty (TupleTyCon _ _ arity) = case arity of
304 0 -> maybe_code sty SLIT("()")
305 2 -> maybe_code sty SLIT("(,)")
306 3 -> maybe_code sty SLIT("(,,)")
307 4 -> maybe_code sty SLIT("(,,,)")
308 5 -> maybe_code sty SLIT("(,,,,)")
309 n -> maybe_code sty (_PK_ ( "(" ++ nOfThem (n-1) ',' ++ ")"))
311 pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
312 | uniq == listTyConKey
313 = maybe_code sty SLIT("[]")
317 pprTyCon sty (SpecTyCon tc ty_maybes)
318 = ppBeside (pprTyCon sty tc)
320 then identToC tys_stuff
321 else ppPStr tys_stuff)
323 tys_stuff = specMaybeTysSuffix ty_maybes
325 pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
326 = ppBeside (ppr sty name)
328 (ppCat [ ppStr " {-",
330 interpp'SP sty tyvars,
331 pprParendGenType sty expansion,
336 %************************************************************************
338 \subsection[Class]{@Class@}
340 %************************************************************************
343 pprGenClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
345 pprGenClassOp sty op = ppr_class_op sty [] op
347 ppr_class_op sty tyvars (ClassOp op_name i ty)
350 PprForAsm _ _ -> pp_C
351 PprInterface -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
352 PprShowAll -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
355 pp_C = ppPStr op_name
356 pp_user = if isLexVarSym op_name && not (isLexSpecialSym op_name)
362 %************************************************************************
364 \subsection{Mumbo jumbo}
366 %************************************************************************
369 -- Shallowly magical; converts a type into something
370 -- vaguely close to what can be used in C identifier.
371 -- Don't forget to include the module name!!!
372 getTypeString :: Type -> [FAST_STRING]
373 getTypeString ty = [mod, string]
375 string = _PK_ (tidy (ppShow 1000 ppr_t))
376 ppr_t = pprGenType PprForC ty
377 -- PprForC expands type synonyms as it goes
380 = case (maybeAppTyCon ty) of
381 Nothing -> panic "getTypeString"
382 Just (tycon,_) -> moduleOf (origName "getTypeString" tycon)
384 --------------------------------------------------
391 '-' : '>' : xs -> '-' : '>' : tidy (no_leading_sps xs)
392 other -> ' ' : tidy more
394 tidy (',' : more) = ',' : tidy (no_leading_sps more)
396 tidy (x : xs) = x : tidy xs -- catch all
398 no_leading_sps [] = []
399 no_leading_sps (' ':xs) = no_leading_sps xs
400 no_leading_sps other = other
402 typeMaybeString :: Maybe Type -> [FAST_STRING]
403 typeMaybeString Nothing = [SLIT("!")]
404 typeMaybeString (Just t) = getTypeString t
406 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
407 specMaybeTysSuffix ty_maybes
409 ty_strs = concat (map typeMaybeString ty_maybes)
410 dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
417 nmbrType :: Type -> NmbrM Type
419 nmbrType (TyVarTy tv)
420 = nmbrTyVar tv `thenNmbr` \ new_tv ->
421 returnNmbr (TyVarTy new_tv)
423 nmbrType (AppTy t1 t2)
424 = nmbrType t1 `thenNmbr` \ new_t1 ->
425 nmbrType t2 `thenNmbr` \ new_t2 ->
426 returnNmbr (AppTy new_t1 new_t2)
428 nmbrType (TyConTy tc use)
429 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
430 nmbrUsage use `thenNmbr` \ new_use ->
431 returnNmbr (TyConTy tc new_use)
433 nmbrType (SynTy tc args expand)
434 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
435 mapNmbr nmbrType args `thenNmbr` \ new_args ->
436 nmbrType expand `thenNmbr` \ new_expand ->
437 returnNmbr (SynTy tc new_args new_expand)
439 nmbrType (ForAllTy tv ty)
440 = addTyVar tv `thenNmbr` \ new_tv ->
441 nmbrType ty `thenNmbr` \ new_ty ->
442 returnNmbr (ForAllTy new_tv new_ty)
444 nmbrType (ForAllUsageTy u us ty)
445 = addUVar u `thenNmbr` \ new_u ->
446 mapNmbr nmbrUVar us `thenNmbr` \ new_us ->
447 nmbrType ty `thenNmbr` \ new_ty ->
448 returnNmbr (ForAllUsageTy new_u new_us new_ty)
450 nmbrType (FunTy t1 t2 use)
451 = nmbrType t1 `thenNmbr` \ new_t1 ->
452 nmbrType t2 `thenNmbr` \ new_t2 ->
453 nmbrUsage use `thenNmbr` \ new_use ->
454 returnNmbr (FunTy new_t1 new_t2 new_use)
456 nmbrType (DictTy c ty use)
457 = --nmbrClass c `thenNmbr` \ new_c ->
458 nmbrType ty `thenNmbr` \ new_ty ->
459 nmbrUsage use `thenNmbr` \ new_use ->
460 returnNmbr (DictTy c new_ty new_use)
464 addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
466 addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
467 = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
468 case (lookupUFM_Directly tvenv u) of
469 Just xx -> -- pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
470 -- (It gets triggered when we do a datatype: first we
471 -- "addTyVar" the tyvars for the datatype as a whole;
472 -- we will subsequently "addId" the data cons, including
473 -- the type for each of them -- each of which includes
474 -- _forall_ ...tvs..., which we will addTyVar.
475 -- Harmless, if that's all that happens....
479 nenv_plus_tv = NmbrEnv ui (incrUnique ut) uu
481 (addToUFM_Directly tvenv u new_tv)
484 (nenv2, new_use) = nmbrUsage use nenv_plus_tv
486 new_tv = TyVar ut k maybe_name new_use
490 nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
491 = case (lookupUFM_Directly tvenv u) of
492 Just xx -> (nenv, xx)
494 pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
498 nmbrTyCon : only called from ``top-level'', if you know what I mean.
500 nmbrTyCon tc@FunTyCon = returnNmbr tc
501 nmbrTyCon tc@(TupleTyCon _ _ _) = returnNmbr tc
502 nmbrTyCon tc@(PrimTyCon _ _ _ _) = returnNmbr tc
504 nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
505 = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
506 mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
507 mapNmbr nmbr_theta theta `thenNmbr` \ new_theta ->
508 mapNmbr nmbrId cons `thenNmbr` \ new_cons ->
509 returnNmbr (DataTyCon u n k new_tvs new_theta new_cons clss nod)
512 = --nmbrClass c `thenNmbr` \ new_c ->
513 nmbrType t `thenNmbr` \ new_t ->
514 returnNmbr (c, new_t)
516 nmbrTyCon (SynTyCon u n k a tvs expand)
517 = mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
518 nmbrType expand `thenNmbr` \ new_expand ->
519 returnNmbr (SynTyCon u n k a new_tvs new_expand)
521 nmbrTyCon (SpecTyCon tc specs)
522 = mapNmbr nmbrMaybeTy specs `thenNmbr` \ new_specs ->
523 returnNmbr (SpecTyCon tc new_specs)
526 nmbrMaybeTy Nothing = returnNmbr Nothing
527 nmbrMaybeTy (Just t) = nmbrType t `thenNmbr` \ new_t ->
528 returnNmbr (Just new_t)
532 nmbrClass (Class u n tv supers ssels ops osels odefms instenv isupers)
533 = addTyVar tv `thenNmbr` \ new_tv ->
534 mapNmbr nmbr_op ops `thenNmbr` \ new_ops ->
535 returnNmbr (Class u n new_tv supers ssels new_ops osels odefms instenv isupers)
537 nmbr_op (ClassOp n tag ty)
538 = nmbrType ty `thenNmbr` \ new_ty ->
539 returnNmbr (ClassOp n tag new_ty)
543 nmbrUsage :: Usage -> NmbrM Usage
545 nmbrUsage u = returnNmbr u
547 nmbrUsage u@UsageOne = returnNmbr u
548 nmbrUsage u@UsageOmega = returnNmbr u
549 nmbrUsage (UsageVar u)
550 = nmbrUVar u `thenNmbr` \ new_u ->
551 returnNmbr (UsageVar new_u)
556 addUVar, nmbrUVar :: UVar -> NmbrM UVar
558 addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
559 = case (lookupUFM_Directly uvenv u) of
560 Just xx -> _trace "addUVar: already in map!" $
564 nenv_plus_uv = NmbrEnv ui ut (incrUnique uu)
567 (addToUFM_Directly uvenv u new_uv)
570 (nenv_plus_uv, new_uv)
572 nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
573 = case (lookupUFM_Directly uvenv u) of
574 Just xx -> (nenv, xx)
576 _trace "nmbrUVar: lookup failed" $