Add kind equalities to GHC.
[ghc.git] / compiler / main / PprTyThing.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing TyThings
4 --
5 -- (c) The GHC Team 2005
6 --
7 -----------------------------------------------------------------------------
8
9 {-# LANGUAGE CPP #-}
10 module PprTyThing (
11 pprTyThing,
12 pprTyThingInContext,
13 pprTyThingLoc,
14 pprTyThingInContextLoc,
15 pprTyThingHdr,
16 pprTypeForUser,
17 pprFamInst
18 ) where
19
20 #include "HsVersions.h"
21
22 import Type ( TyThing(..) )
23 import CoAxiom ( coAxiomTyCon )
24 import HscTypes( tyThingParent_maybe )
25 import MkIface ( tyThingToIfaceDecl )
26 import Type ( tidyOpenType )
27 import IfaceSyn ( pprIfaceDecl, ShowSub(..), ShowHowMuch(..) )
28 import FamInstEnv( FamInst( .. ), FamFlavor(..) )
29 import TcType
30 import Name
31 import VarEnv( emptyTidyEnv )
32 import Outputable
33 import FastString
34
35 -- -----------------------------------------------------------------------------
36 -- Pretty-printing entities that we get from the GHC API
37
38 {- Note [Pretty-printing TyThings]
39 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40 We pretty-print a TyThing by converting it to an IfaceDecl,
41 and pretty-printing that (see ppr_ty_thing below).
42 Here is why:
43
44 * When pretty-printing (a type, say), the idiomatic solution is not to
45 "rename type variables on the fly", but rather to "tidy" the type
46 (which gives each variable a distinct print-name), and then
47 pretty-print it (without renaming). Separate the two
48 concerns. Functions like tidyType do this.
49
50 * Alas, for type constructors, TyCon, tidying does not work well,
51 because a TyCon includes DataCons which include Types, which mention
52 TyCons. And tidying can't tidy a mutually recursive data structure
53 graph, only trees.
54
55 * One alternative would be to ensure that TyCons get type variables
56 with distinct print-names. That's ok for type variables but less
57 easy for kind variables. Processing data type declarations is
58 already so complicated that I don't think it's sensible to add the
59 extra requirement that it generates only "pretty" types and kinds.
60
61 * One place the non-pretty names can show up is in GHCi. But another
62 is in interface files. Look at MkIface.tyThingToIfaceDecl which
63 converts a TyThing (i.e. TyCon, Class etc) to an IfaceDecl. And it
64 already does tidying as part of that conversion! Why? Because
65 interface files contains fast-strings, not uniques, so the names
66 must at least be distinct.
67
68 So if we convert to IfaceDecl, we get a nice tidy IfaceDecl, and can
69 print that. Of course, that means that pretty-printing IfaceDecls
70 must be careful to display nice user-friendly results, but that's ok.
71
72 See #7730, #8776 for details -}
73
74 --------------------
75 -- | Pretty-prints a 'FamInst' (type/data family instance) with its defining location.
76 pprFamInst :: FamInst -> SDoc
77 -- * For data instances we go via pprTyThing of the representational TyCon,
78 -- because there is already much cleverness associated with printing
79 -- data type declarations that I don't want to duplicate
80 -- * For type instances we print directly here; there is no TyCon
81 -- to give to pprTyThing
82 --
83 -- FamInstEnv.pprFamInst does a more quick-and-dirty job for internal purposes
84
85 pprFamInst (FamInst { fi_flavor = DataFamilyInst rep_tc })
86 = pprTyThingInContextLoc (ATyCon rep_tc)
87
88 pprFamInst (FamInst { fi_flavor = SynFamilyInst, fi_axiom = axiom
89 , fi_tys = lhs_tys, fi_rhs = rhs })
90 = showWithLoc (pprDefinedAt (getName axiom)) $
91 hang (ptext (sLit "type instance") <+> pprTypeApp (coAxiomTyCon axiom) lhs_tys)
92 2 (equals <+> ppr rhs)
93
94 ----------------------------
95 -- | Pretty-prints a 'TyThing' with its defining location.
96 pprTyThingLoc :: TyThing -> SDoc
97 pprTyThingLoc tyThing
98 = showWithLoc (pprDefinedAt (getName tyThing)) (pprTyThing tyThing)
99
100 -- | Pretty-prints a 'TyThing'.
101 pprTyThing :: TyThing -> SDoc
102 pprTyThing = ppr_ty_thing False []
103
104 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
105 -- the function is equivalent to 'pprTyThing' but for type constructors
106 -- and classes it prints only the header part of the declaration.
107 pprTyThingHdr :: TyThing -> SDoc
108 pprTyThingHdr = ppr_ty_thing True []
109
110 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
111 -- is a data constructor, record selector, or class method, then
112 -- the entity's parent declaration is pretty-printed with irrelevant
113 -- parts omitted.
114 pprTyThingInContext :: TyThing -> SDoc
115 pprTyThingInContext thing
116 = go [] thing
117 where
118 go ss thing = case tyThingParent_maybe thing of
119 Just parent -> go (getOccName thing : ss) parent
120 Nothing -> ppr_ty_thing False ss thing
121
122 -- | Like 'pprTyThingInContext', but adds the defining location.
123 pprTyThingInContextLoc :: TyThing -> SDoc
124 pprTyThingInContextLoc tyThing
125 = showWithLoc (pprDefinedAt (getName tyThing))
126 (pprTyThingInContext tyThing)
127
128 ------------------------
129 ppr_ty_thing :: Bool -> [OccName] -> TyThing -> SDoc
130 -- We pretty-print 'TyThing' via 'IfaceDecl'
131 -- See Note [Pretty-printing TyThings]
132 ppr_ty_thing hdr_only path ty_thing
133 = pprIfaceDecl ss (tyThingToIfaceDecl ty_thing)
134 where
135 ss = ShowSub { ss_how_much = how_much, ss_ppr_bndr = ppr_bndr }
136 how_much | hdr_only = ShowHeader
137 | otherwise = ShowSome path
138 name = getName ty_thing
139 ppr_bndr :: OccName -> SDoc
140 ppr_bndr | isBuiltInSyntax name
141 = ppr
142 | otherwise
143 = case nameModule_maybe name of
144 Just mod -> \ occ -> getPprStyle $ \sty ->
145 pprModulePrefix sty mod occ <> ppr occ
146 Nothing -> WARN( True, ppr name ) ppr
147 -- Nothing is unexpected here; TyThings have External names
148
149 pprTypeForUser :: Type -> SDoc
150 -- We do two things here.
151 -- a) We tidy the type, regardless
152 -- b) Swizzle the foralls to the top, so that without
153 -- -fprint-explicit-foralls we'll suppress all the foralls
154 -- Prime example: a class op might have type
155 -- forall a. C a => forall b. Ord b => stuff
156 -- Then we want to display
157 -- (C a, Ord b) => stuff
158 pprTypeForUser ty
159 = pprSigmaType (mkInvSigmaTy tvs ctxt tau)
160 where
161 (tvs, ctxt, tau) = tcSplitSigmaTy tidy_ty
162 (_, tidy_ty) = tidyOpenType emptyTidyEnv ty
163 -- Often the types/kinds we print in ghci are fully generalised
164 -- and have no free variables, but it turns out that we sometimes
165 -- print un-generalised kinds (eg when doing :k T), so it's
166 -- better to use tidyOpenType here
167
168 showWithLoc :: SDoc -> SDoc -> SDoc
169 showWithLoc loc doc
170 = hang doc 2 (char '\t' <> comment <+> loc)
171 -- The tab tries to make them line up a bit
172 where
173 comment = ptext (sLit "--")