2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[PprType]{Printing Types, TyVars, Classes, ClassOps, TyCons}
7 #include "HsVersions.h"
12 GenType, pprType, pprParendType,
18 GenClassOp, pprClassOp
22 import IdLoop -- for paranoia checking
23 import TyLoop -- for paranoia checking
24 import NameLoop -- for paranoia checking
27 -- (PprType can see all the representations it's trying to print)
28 import Type ( GenType(..), maybeAppTyCon,
29 splitForAllTy, splitSigmaTy, splitRhoTy, splitAppTy )
30 import TyVar ( GenTyVar(..) )
31 import TyCon ( TyCon(..), ConsVisible, NewOrData )
32 import Class ( Class(..), GenClass(..),
33 ClassOp(..), GenClassOp(..) )
34 import Kind ( Kind(..) )
37 import CStrings ( identToC )
38 import CmdLineOpts ( opt_OmitInterfacePragmas )
39 import Maybes ( maybeToBool )
40 import NameTypes ( ShortName, FullName )
41 import Outputable ( ifPprShowAll, isAvarop, interpp'SP )
42 import PprStyle ( PprStyle(..), codeStyle )
44 import TysWiredIn ( listTyCon )
45 import Unique ( pprUnique10, pprUnique )
46 import Usage ( UVar(..), pprUVar )
51 instance (Eq tyvar, Outputable tyvar,
52 Eq uvar, Outputable uvar ) => Outputable (GenType tyvar uvar) where
53 ppr sty ty = pprType sty ty
55 instance Outputable TyCon where
56 ppr sty tycon = pprTyCon sty tycon
58 instance Outputable (GenClass tyvar uvar) where
59 -- we use pprIfaceClass for printing in interfaces
60 ppr sty (Class u n _ _ _ _ _ _ _ _) = ppr sty n
62 instance Outputable ty => Outputable (GenClassOp ty) where
63 ppr sty clsop = pprClassOp sty clsop
65 instance Outputable (GenTyVar flexi) where
66 ppr sty tv = pprTyVar sty tv
69 %************************************************************************
71 \subsection[Type]{@Type@}
73 %************************************************************************
75 @pprType@ is the std @Type@ printer; the overloaded @ppr@ function is
76 defined to use this. @pprParendType@ is the same, except it puts
77 parens around the type, except for the atomic cases. @pprParendType@
78 works just by setting the initial context precedence very high.
81 pprType, pprParendType :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
82 => PprStyle -> GenType tyvar uvar -> Pretty
84 pprType sty ty = ppr_ty sty (initial_ve sty) tOP_PREC ty
85 pprParendType sty ty = ppr_ty sty (initial_ve sty) tYCON_PREC ty
87 pprMaybeTy :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
88 => PprStyle -> Maybe (GenType tyvar uvar) -> Pretty
89 pprMaybeTy sty Nothing = ppChar '*'
90 pprMaybeTy sty (Just ty) = pprParendType sty ty
93 This somewhat sleazy interface is used when printing out Core syntax
96 pprType_Internal sty tvs ppr_tv uvs ppr_uv ty
97 = ppr_ty sty (VE tvs ppr_tv uvs ppr_uv) tOP_PREC ty
101 ppr_ty :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
102 => PprStyle -> VarEnv tyvar uvar -> Int
103 -> GenType tyvar uvar
106 ppr_ty sty env ctxt_prec (TyVarTy tyvar)
107 = ppr_tyvar env tyvar
109 ppr_ty sty env ctxt_prec (TyConTy tycon usage)
112 ppr_ty sty env ctxt_prec ty@(ForAllTy _ _)
113 | showUserishTypes sty = ppr_ty sty env' ctxt_prec body_ty
115 | otherwise = ppSep [ ppPStr SLIT("_forall_"),
116 ppIntersperse pp'SP pp_tyvars,
118 ppr_ty sty env' ctxt_prec body_ty
121 (tyvars, body_ty) = splitForAllTy ty
122 env' = foldl add_tyvar env tyvars
123 pp_tyvars = map (ppr_tyvar env') tyvars
125 ppr_ty sty env ctxt_prec (ForAllUsageTy uv uvs ty)
126 = panic "ppr_ty:ForAllUsageTy"
128 ppr_ty sty env ctxt_prec ty@(FunTy (DictTy _ _ _) _ _)
129 | showUserishTypes sty
130 -- Print a nice looking context (Eq a, Text b) => ...
131 = ppSep [ppBesides [ppLparen,
132 ppIntersperse pp'SP (map (ppr_dict sty env tOP_PREC) theta),
135 ppr_ty sty env ctxt_prec body_ty
138 (theta, body_ty) = splitRhoTy ty
140 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
141 -- We fiddle the precedences passed to left/right branches,
142 -- so that right associativity comes out nicely...
143 = maybeParen ctxt_prec fUN_PREC
144 (ppCat [ppr_ty sty env fUN_PREC ty1,
146 ppr_ty sty env tOP_PREC ty2])
148 ppr_ty sty env ctxt_prec ty@(AppTy _ _)
149 = ppr_corner sty env ctxt_prec fun_ty arg_tys
151 (fun_ty, arg_tys) = splitAppTy ty
153 ppr_ty PprInterface env ctxt_prec (SynTy tycon tys expansion)
154 -- always expand types in an interface
155 = ppr_ty PprInterface env ctxt_prec expansion
157 ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
159 (ppr_app sty env ctxt_prec (ppr sty tycon) tys)
160 (ifPprShowAll sty (ppCat [ppStr " {- expansion:",
161 ppr_ty sty env tOP_PREC expansion,
164 ppr_ty sty env ctxt_prec (DictTy clas ty usage)
165 = ppr_dict sty env ctxt_prec (clas, ty)
168 -- Some help functions
169 ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
170 = ASSERT(length arg_tys == 2)
171 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
173 (ty1:ty2:_) = arg_tys
175 ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon a) usage) arg_tys
176 = ASSERT(length arg_tys == a)
177 ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
179 arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty sty env tOP_PREC) arg_tys)
181 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
183 = ASSERT(length arg_tys == 1)
184 ppBesides [ppLbrack, ppr_ty sty env tOP_PREC ty1, ppRbrack]
188 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
189 = ppr_app sty env ctxt_prec (ppr sty tycon) arg_tys
191 ppr_corner sty env ctxt_prec (TyVarTy tyvar) arg_tys
192 = ppr_app sty env ctxt_prec (ppr_tyvar env tyvar) arg_tys
195 ppr_app sty env ctxt_prec pp_fun []
197 ppr_app sty env ctxt_prec pp_fun arg_tys
198 = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
200 arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty sty env tYCON_PREC) arg_tys)
203 ppr_dict sty env ctxt_prec (clas, ty)
204 = maybeParen ctxt_prec tYCON_PREC
205 (ppCat [ppr sty clas, ppr_ty sty env tYCON_PREC ty])
208 Nota Bene: we must assign print-names to the forall'd type variables
209 alphabetically, with the first forall'd variable having the alphabetically
210 first name. Reason: so anyone reading the type signature printed without
211 explicit forall's will be able to reconstruct them in the right order.
214 -- Entirely local to this module
215 data VarEnv tyvar uvar
216 = VE [Pretty] -- Tyvar pretty names
217 (tyvar -> Pretty) -- Tyvar lookup function
218 [Pretty] -- Uvar pretty names
219 (uvar -> Pretty) -- Uvar lookup function
221 initial_ve PprForC = VE [] (\tv -> ppChar '*')
222 [] (\tv -> ppChar '#')
224 initial_ve sty = VE tv_pretties (ppr sty)
225 uv_pretties (ppr sty)
227 tv_pretties = map (\ c -> ppChar c ) ['a' .. 'h']
229 map (\ n -> ppBeside (ppChar 'a') (ppInt n))
230 ([0 .. ] :: [Int]) -- a0 ... aN
232 uv_pretties = map (\ c -> ppChar c ) ['u' .. 'y']
234 map (\ n -> ppBeside (ppChar 'u') (ppInt n))
235 ([0 .. ] :: [Int]) -- u0 ... uN
238 ppr_tyvar (VE _ ppr _ _) tyvar = ppr tyvar
239 ppr_uvar (VE _ _ _ ppr) uvar = ppr uvar
241 add_tyvar ve@(VE [] _ _ _) tyvar = ve
242 add_tyvar (VE (tv_pp:tv_supply') tv_ppr uv_supply uv_ppr) tyvar
243 = VE tv_supply' tv_ppr' uv_supply uv_ppr
245 tv_ppr' tv | tv==tyvar = tv_pp
246 | otherwise = tv_ppr tv
248 add_uvar ve@(VE _ _ [] _) uvar = ve
249 add_uvar (VE tv_supply tv_ppr (uv_pp:uv_supply') uv_ppr) uvar
250 = VE tv_supply tv_ppr uv_supply' uv_ppr'
252 uv_ppr' uv | uv==uvar = uv_pp
253 | otherwise = uv_ppr uv
256 @ppr_ty@ takes an @Int@ that is the precedence of the context.
257 The precedence levels are:
259 \item[0:] What we start with.
260 \item[1:] Function application (@FunTys@).
261 \item[2:] Type constructors.
266 tOP_PREC = (0 :: Int)
267 fUN_PREC = (1 :: Int)
268 tYCON_PREC = (2 :: Int)
270 maybeParen ctxt_prec inner_prec pretty
271 | ctxt_prec < inner_prec = pretty
272 | otherwise = ppParens pretty
275 -- True means types like (Eq a, Text b) => a -> b
276 -- False means types like _forall_ a b => Eq a -> Text b -> a -> b
277 showUserishTypes PprForUser = True
278 showUserishTypes PprInterface = True
279 showUserishTypes other = False
284 %************************************************************************
286 \subsection[TyVar]{@TyVar@}
288 %************************************************************************
291 pprTyVar sty (TyVar uniq kind name usage)
292 = ppBesides [pp_name, pprUnique10 uniq]
294 pp_name = case name of
296 Nothing -> case kind of
297 TypeKind -> ppChar 'o'
298 BoxedTypeKind -> ppChar 't'
299 UnboxedTypeKind -> ppChar 'u'
300 ArrowKind _ _ -> ppChar 'a'
303 %************************************************************************
305 \subsection[TyCon]{@TyCon@}
307 %************************************************************************
309 ToDo; all this is suspiciously like getOccurrenceName!
312 showTyCon :: PprStyle -> TyCon -> String
313 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
315 pprTyCon :: PprStyle -> TyCon -> Pretty
317 pprTyCon sty FunTyCon = ppStr "(->)"
318 pprTyCon sty (TupleTyCon arity) = ppBeside (ppPStr SLIT("Tuple")) (ppInt arity)
319 pprTyCon sty (PrimTyCon uniq name kind) = ppr sty name
321 pprTyCon sty tycon@(DataTyCon uniq kind name tyvars ctxt cons derivings cv nd)
323 PprDebug -> pp_tycon_and_uniq
324 PprShowAll -> pp_tycon_and_uniq
327 pp_tycon_and_uniq = ppBesides [pp_tycon, ppChar '.', pprUnique uniq]
328 pp_tycon = ppr sty name
330 pprTyCon sty (SpecTyCon tc ty_maybes)
331 = ppBeside (pprTyCon sty tc)
333 then identToC tys_stuff
334 else ppPStr tys_stuff)
336 tys_stuff = specMaybeTysSuffix ty_maybes
338 pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
339 = ppBeside (ppr sty name)
341 (ppCat [ ppStr " {-",
343 interpp'SP sty tyvars,
344 pprParendType sty expansion,
349 %************************************************************************
351 \subsection[Class]{@Class@}
353 %************************************************************************
356 pprClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
358 pprClassOp sty op = ppr_class_op sty [] op
360 ppr_class_op sty tyvars (ClassOp op_name i ty)
363 PprForAsm _ _ -> pp_C
364 PprInterface -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
365 PprShowAll -> ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
368 pp_C = ppPStr op_name
369 pp_user = if isAvarop op_name
370 then ppBesides [ppLparen, pp_C, ppRparen]
375 %************************************************************************
377 \subsection[]{Mumbo jumbo}
379 %************************************************************************
382 -- Shallowly magical; converts a type into something
383 -- vaguely close to what can be used in C identifier.
384 -- Don't forget to include the module name!!!
385 getTypeString :: Type -> [FAST_STRING]
387 | is_prelude_ty = [string]
388 | otherwise = [mod, string]
390 string = _PK_ (tidy (ppShow 1000 ppr_t))
391 ppr_t = pprType PprForC ty
392 -- PprForC expands type synonyms as it goes
395 = case (maybeAppTyCon ty) of
396 Nothing -> true_bottom
398 if fromPreludeCore tycon
400 else (False, fst (getOrigName tycon))
402 true_bottom = (True, panic "getTypeString")
404 --------------------------------------------------
411 '-' : '>' : xs -> '-' : '>' : tidy (no_leading_sps xs)
412 other -> ' ' : tidy more
414 tidy (',' : more) = ',' : tidy (no_leading_sps more)
416 tidy (x : xs) = x : tidy xs -- catch all
418 no_leading_sps [] = []
419 no_leading_sps (' ':xs) = no_leading_sps xs
420 no_leading_sps other = other
422 typeMaybeString :: Maybe Type -> [FAST_STRING]
423 typeMaybeString Nothing = [SLIT("!")]
424 typeMaybeString (Just t) = getTypeString t
426 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
427 specMaybeTysSuffix ty_maybes
429 ty_strs = concat (map typeMaybeString ty_maybes)
430 dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
435 ========================================================
436 INTERFACE STUFF; move it out
440 pprTyCon sty@PprInterface (SynonymTyCon k n a vs exp unabstract) specs
441 = ASSERT (null specs)
443 lookup_fn = mk_lookup_tyvar_fn sty vs
444 pp_tyvars = map lookup_fn vs
446 ppCat [ppPStr SLIT("type"), ppr sty n, ppIntersperse ppSP pp_tyvars,
447 ppEquals, ppr_ty sty lookup_fn tOP_PREC exp]
449 pprTyCon sty@PprInterface this_tycon@(DataTyCon k n a vs ctxt cons derivings unabstract data_or_new) specs
450 = ppHang (ppCat [pp_data_or_new,
453 ppIntersperse ppSP (map lookup_fn vs)])
455 (ppCat [pp_unabstract_condecls,
457 -- NB: we do not print deriving info in interfaces
459 lookup_fn = mk_lookup_tyvar_fn sty vs
461 pp_data_or_new = case data_or_new of
462 DataType -> ppPStr SLIT("data")
463 NewType -> ppPStr SLIT("newtype")
465 yes_we_print_condecls
467 && not (null cons) -- we know what they are
468 && (case (getExportFlag n) of
472 yes_we_print_pragma_condecls
473 = not yes_we_print_condecls
474 && not opt_OmitInterfacePragmas
476 && not (maybeToBool (maybePurelyLocalTyCon this_tycon))
477 {- && not (any (dataConMentionsNonPreludeTyCon this_tycon) cons) -}
479 yes_we_print_pragma_specs
482 pp_unabstract_condecls
483 = if yes_we_print_condecls
484 then ppCat [ppSP, ppEquals, pp_condecls]
488 = if yes_we_print_pragma_condecls
493 = if yes_we_print_pragma_specs
498 = if (yes_we_print_pragma_condecls || yes_we_print_pragma_specs)
499 then ppCat [ppStr "\t{-# GHC_PRAGMA", pp_pragma_condecls, pp_pragma_specs, ppStr "#-}"]
506 ppCat ((ppr_con c) : (map ppr_next_con cs))
510 (_, _, con_arg_tys, _) = getDataConSig con
512 ppCat [pprNonOp PprForUser con, -- the data con's name...
513 ppIntersperse ppSP (map (ppr_ty sty lookup_fn tYCON_PREC) con_arg_tys)]
515 ppr_next_con con = ppCat [ppChar '|', ppr_con con]
518 = ppBesides [ppPStr SLIT("_SPECIALIZE_ "), pp_the_list [
519 ppCat [ppLbrack, ppInterleave ppComma (map pp_maybe ty_maybes), ppRbrack]
520 | ty_maybes <- specs ]]
523 pp_the_list (p:ps) = ppAbove (ppBeside p ppComma) (pp_the_list ps)
525 pp_maybe Nothing = pp_NONE
526 pp_maybe (Just ty) = pprParendType sty ty
528 pp_NONE = ppPStr SLIT("_N_")
530 pprTyCon PprInterface (TupleTyCon a) specs
531 = ASSERT (null specs)
532 ppCat [ ppStr "{- Tuple", ppInt a, ppStr "-}" ]
534 pprTyCon PprInterface (PrimTyCon k n a kind_fn) specs
535 = ASSERT (null specs)
536 ppCat [ ppStr "{- data", ppr PprForUser n, ppStr " *built-in* -}" ]
542 pprIfaceClass :: (Id -> Id) -> IdEnv UnfoldingDetails -> Class -> Pretty
544 pprIfaceClass better_id_fn inline_env
545 (Class k n tyvar super_classes sdsels ops sels defms insts links)
547 sdsel_infos = map (getIdInfo . better_id_fn) sdsels
549 ppAboves [ ppCat [ppPStr SLIT("class"), ppr_theta tyvar super_classes,
550 ppr sty n, lookup_fn tyvar,
552 || opt_OmitInterfacePragmas
553 || (any boringIdInfo sdsel_infos)
554 -- ToDo: really should be "all bor..."
555 -- but then parsing is more tedious,
556 -- and this is really as good in practice.
558 else pp_sdsel_pragmas (sdsels `zip` sdsel_infos),
561 else ppPStr SLIT("where")],
563 [ ppr_op op (better_id_fn sel) (better_id_fn defm)
564 | (op,sel,defm) <- zip3 ops sels defms]) ]
566 lookup_fn = mk_lookup_tyvar_fn sty [tyvar]
568 ppr_theta :: TyVar -> [Class] -> Pretty
569 ppr_theta tv [] = ppNil
570 ppr_theta tv super_classes
571 = ppBesides [ppLparen,
572 ppIntersperse pp'SP{-'-} (map ppr_assert super_classes),
575 ppr_assert (Class _ n _ _ _ _ _ _ _ _) = ppCat [ppr sty n, lookup_fn tv]
577 pp_sdsel_pragmas sdsels_and_infos
578 = ppCat [ppStr "{-# GHC_PRAGMA {-superdicts-}",
579 ppIntersperse pp'SP{-'-}
580 [ppIdInfo sty sdsel False{-NO specs-} better_id_fn inline_env info
581 | (sdsel, info) <- sdsels_and_infos ],
584 ppr_op op opsel_id defm_id
586 stuff = ppBeside (ppChar '\t') (ppr_class_op sty [tyvar] op)
588 if opt_OmitInterfacePragmas
591 (ppCat [ppStr "\t {-# GHC_PRAGMA", ppAbove pp_opsel pp_defm, ppStr "#-}"])
593 pp_opsel = ppCat [ppPStr SLIT("{-meth-}"), ppIdInfo sty opsel_id False{-no specs-} better_id_fn inline_env (getIdInfo opsel_id)]
594 pp_defm = ppCat [ppPStr SLIT("\t\t{-defm-}"), ppIdInfo sty defm_id False{-no specs-} better_id_fn inline_env (getIdInfo defm_id)]