Merge branch 'master' of darcs.haskell.org:/home/darcs/ghc
[ghc.git] / compiler / main / PprTyThing.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing TyThings
4 --
5 -- (c) The GHC Team 2005
6 --
7 -----------------------------------------------------------------------------
8
9 {-# OPTIONS -fno-warn-tabs #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and
12 -- detab the module (please do the detabbing in a separate patch). See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
14 -- for details
15
16 module PprTyThing (
17 PrintExplicitForalls,
18 pprTyThing,
19 pprTyThingInContext,
20 pprTyThingLoc,
21 pprTyThingInContextLoc,
22 pprTyThingHdr,
23 pprTypeForUser
24 ) where
25
26 import qualified GHC
27
28 import GHC ( TyThing(..) )
29 import DataCon
30 import Id
31 import TyCon
32 import Coercion( pprCoAxiom )
33 import HscTypes( tyThingParent_maybe )
34 import Type( tidyTopType, tidyOpenType )
35 import TcType
36 import Name
37 import VarEnv( emptyTidyEnv )
38 import StaticFlags( opt_PprStyle_Debug )
39 import Outputable
40 import FastString
41
42 -- -----------------------------------------------------------------------------
43 -- Pretty-printing entities that we get from the GHC API
44
45 -- This should be a good source of sample code for using the GHC API to
46 -- inspect source code entities.
47
48 type PrintExplicitForalls = Bool
49
50 type ShowSub = [Name]
51 -- [] <=> print all sub-components of the current thing
52 -- (n:ns) <=> print sub-component 'n' with ShowSub=ns
53 -- elide other sub-components to "..."
54 showAll :: ShowSub
55 showAll = []
56
57 showSub :: NamedThing n => ShowSub -> n -> Bool
58 showSub [] _ = True
59 showSub (n:_) thing = n == getName thing
60
61 showSub_maybe :: NamedThing n => ShowSub -> n -> Maybe ShowSub
62 showSub_maybe [] _ = Just []
63 showSub_maybe (n:ns) thing = if n == getName thing then Just ns
64 else Nothing
65
66 ----------------------------
67 -- | Pretty-prints a 'TyThing' with its defining location.
68 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
69 pprTyThingLoc pefas tyThing
70 = showWithLoc (pprDefinedAt (GHC.getName tyThing)) (pprTyThing pefas tyThing)
71
72 -- | Pretty-prints a 'TyThing'.
73 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
74 pprTyThing pefas thing = ppr_ty_thing pefas showAll thing
75
76 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
77 -- is a data constructor, record selector, or class method, then
78 -- the entity's parent declaration is pretty-printed with irrelevant
79 -- parts omitted.
80 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
81 pprTyThingInContext pefas thing
82 = go [] thing
83 where
84 go ss thing = case tyThingParent_maybe thing of
85 Just parent -> go (getName thing : ss) parent
86 Nothing -> ppr_ty_thing pefas ss thing
87
88 -- | Like 'pprTyThingInContext', but adds the defining location.
89 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
90 pprTyThingInContextLoc pefas tyThing
91 = showWithLoc (pprDefinedAt (GHC.getName tyThing))
92 (pprTyThingInContext pefas tyThing)
93
94 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
95 -- the function is equivalent to 'pprTyThing' but for type constructors
96 -- and classes it prints only the header part of the declaration.
97 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
98 pprTyThingHdr pefas (AnId id) = pprId pefas id
99 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
100 pprTyThingHdr pefas (ATyCon tyCon) = pprTyConHdr pefas tyCon
101 pprTyThingHdr _ (ACoAxiom ax) = pprCoAxiom ax
102
103 ------------------------
104 ppr_ty_thing :: PrintExplicitForalls -> ShowSub -> TyThing -> SDoc
105 ppr_ty_thing pefas _ (AnId id) = pprId pefas id
106 ppr_ty_thing pefas _ (ADataCon dataCon) = pprDataConSig pefas dataCon
107 ppr_ty_thing pefas ss (ATyCon tyCon) = pprTyCon pefas ss tyCon
108 ppr_ty_thing _ _ (ACoAxiom ax) = pprCoAxiom ax
109 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
110 pprTyConHdr pefas tyCon
111 | Just (fam_tc, tys) <- tyConFamInst_maybe tyCon
112 = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp fam_tc tys
113 | Just cls <- tyConClass_maybe tyCon
114 = pprClassHdr pefas cls
115 | otherwise
116 = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
117 where
118 vars | GHC.isPrimTyCon tyCon ||
119 GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
120 | otherwise = GHC.tyConTyVars tyCon
121
122 keyword | GHC.isSynTyCon tyCon = sLit "type"
123 | GHC.isNewTyCon tyCon = sLit "newtype"
124 | otherwise = sLit "data"
125
126 opt_family
127 | GHC.isFamilyTyCon tyCon = ptext (sLit "family")
128 | otherwise = empty
129
130 opt_stupid -- The "stupid theta" part of the declaration
131 | isAlgTyCon tyCon = GHC.pprThetaArrowTy (tyConStupidTheta tyCon)
132 | otherwise = empty -- Returns 'empty' if null theta
133
134 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
135 pprDataConSig pefas dataCon
136 = ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
137
138 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
139 pprClassHdr _ cls
140 = ptext (sLit "class") <+>
141 GHC.pprThetaArrowTy (GHC.classSCTheta cls) <+>
142 ppr_bndr cls <+>
143 hsep (map ppr tyVars) <+>
144 GHC.pprFundeps funDeps
145 where
146 (tyVars, funDeps) = GHC.classTvsFds cls
147
148 pprId :: PrintExplicitForalls -> Var -> SDoc
149 pprId pefas ident
150 = hang (ppr_bndr ident <+> dcolon)
151 2 (pprTypeForUser pefas (GHC.idType ident))
152
153 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
154 -- We do two things here.
155 -- a) We tidy the type, regardless
156 -- b) If PrintExplicitForAlls is True, we discard the foralls
157 -- but we do so `deeply'
158 -- Prime example: a class op might have type
159 -- forall a. C a => forall b. Ord b => stuff
160 -- Then we want to display
161 -- (C a, Ord b) => stuff
162 pprTypeForUser print_foralls ty
163 | print_foralls = ppr tidy_ty
164 | otherwise = ppr (mkPhiTy ctxt ty')
165 where
166 (_, ctxt, ty') = tcSplitSigmaTy tidy_ty
167 (_, tidy_ty) = tidyOpenType emptyTidyEnv ty
168 -- Often the types/kinds we print in ghci are fully generalised
169 -- and have no free variables, but it turns out that we sometimes
170 -- print un-generalised kinds (eg when doing :k T), so it's
171 -- better to use tidyOpenType here
172
173 pprTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
174 pprTyCon pefas ss tyCon
175 | Just syn_rhs <- GHC.synTyConRhs_maybe tyCon
176 = case syn_rhs of
177 SynFamilyTyCon {} -> pprTyConHdr pefas tyCon <+> dcolon <+>
178 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
179 SynonymTyCon rhs_ty -> hang (pprTyConHdr pefas tyCon <+> equals)
180 2 (pprTypeForUser pefas rhs_ty)
181
182 | Just cls <- GHC.tyConClass_maybe tyCon
183 = pprClass pefas ss cls
184 | otherwise
185 = pprAlgTyCon pefas ss tyCon
186
187 pprAlgTyCon :: PrintExplicitForalls -> ShowSub -> TyCon -> SDoc
188 pprAlgTyCon pefas ss tyCon
189 | gadt = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$
190 nest 2 (vcat (ppr_trim (map show_con datacons)))
191 | otherwise = hang (pprTyConHdr pefas tyCon)
192 2 (add_bars (ppr_trim (map show_con datacons)))
193 where
194 datacons = GHC.tyConDataCons tyCon
195 gadt = any (not . GHC.isVanillaDataCon) datacons
196
197 ok_con dc = showSub ss dc || any (showSub ss) (dataConFieldLabels dc)
198 show_con dc
199 | ok_con dc = Just (pprDataConDecl pefas ss gadt dc)
200 | otherwise = Nothing
201
202 pprDataConDecl :: PrintExplicitForalls -> ShowSub -> Bool -> GHC.DataCon -> SDoc
203 pprDataConDecl pefas ss gadt_style dataCon
204 | not gadt_style = ppr_fields tys_w_strs
205 | otherwise = ppr_bndr dataCon <+> dcolon <+>
206 sep [ pp_foralls, GHC.pprThetaArrowTy theta, pp_tau ]
207 -- Printing out the dataCon as a type signature, in GADT style
208 where
209 (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
210 (arg_tys, res_ty) = tcSplitFunTys tau
211 labels = GHC.dataConFieldLabels dataCon
212 stricts = GHC.dataConStrictMarks dataCon
213 tys_w_strs = zip (map user_ify stricts) arg_tys
214 pp_foralls | pefas = GHC.pprForAll forall_tvs
215 | otherwise = empty
216
217 pp_tau = foldr add (ppr res_ty) tys_w_strs
218 add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
219
220 pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
221 pprBangTy (bang,ty) = ppr bang <> ppr ty
222
223 -- See Note [Printing bangs on data constructors]
224 user_ify :: HsBang -> HsBang
225 user_ify bang | opt_PprStyle_Debug = bang
226 user_ify HsStrict = HsUserBang Nothing True
227 user_ify (HsUnpack {}) = HsUserBang (Just True) True
228 user_ify bang = bang
229
230 maybe_show_label (lbl,bty)
231 | showSub ss lbl = Just (ppr lbl <+> dcolon <+> pprBangTy bty)
232 | otherwise = Nothing
233
234 ppr_fields [ty1, ty2]
235 | GHC.dataConIsInfix dataCon && null labels
236 = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
237 ppr_fields fields
238 | null labels
239 = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
240 | otherwise
241 = ppr_bndr dataCon
242 <+> (braces $ sep $ punctuate comma $ ppr_trim $
243 map maybe_show_label (zip labels fields))
244
245 pprClass :: PrintExplicitForalls -> ShowSub -> GHC.Class -> SDoc
246 pprClass pefas ss cls
247 | null methods && null assoc_ts
248 = pprClassHdr pefas cls
249 | otherwise
250 = vcat [ pprClassHdr pefas cls <+> ptext (sLit "where")
251 , nest 2 (vcat $ ppr_trim $
252 map show_at assoc_ts ++ map show_meth methods)]
253 where
254 methods = GHC.classMethods cls
255 assoc_ts = GHC.classATs cls
256 show_meth id | showSub ss id = Just (pprClassMethod pefas id)
257 | otherwise = Nothing
258 show_at tc = case showSub_maybe ss tc of
259 Just ss' -> Just (pprTyCon pefas ss' tc)
260 Nothing -> Nothing
261
262 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
263 pprClassMethod pefas id
264 = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
265 where
266 -- Here's the magic incantation to strip off the dictionary
267 -- from the class op type. Stolen from IfaceSyn.tyThingToIfaceDecl.
268 --
269 -- It's important to tidy it *before* splitting it up, so that if
270 -- we have class C a b where
271 -- op :: forall a. a -> b
272 -- then the inner forall on op gets renamed to a1, and we print
273 -- (when dropping foralls)
274 -- class C a b where
275 -- op :: a1 -> b
276
277 tidy_sel_ty = tidyTopType (GHC.idType id)
278 (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
279 op_ty = GHC.funResultTy rho_ty
280
281 ppr_trim :: [Maybe SDoc] -> [SDoc]
282 -- Collapse a group of Nothings to a single "..."
283 ppr_trim xs
284 = snd (foldr go (False, []) xs)
285 where
286 go (Just doc) (_, so_far) = (False, doc : so_far)
287 go Nothing (True, so_far) = (True, so_far)
288 go Nothing (False, so_far) = (True, ptext (sLit "...") : so_far)
289
290 add_bars :: [SDoc] -> SDoc
291 add_bars [] = empty
292 add_bars [c] = equals <+> c
293 add_bars (c:cs) = sep ((equals <+> c) : map (char '|' <+>) cs)
294
295 -- Wrap operators in ()
296 ppr_bndr :: GHC.NamedThing a => a -> SDoc
297 ppr_bndr a = GHC.pprParenSymName a
298
299 showWithLoc :: SDoc -> SDoc -> SDoc
300 showWithLoc loc doc
301 = hang doc 2 (char '\t' <> comment <+> loc)
302 -- The tab tries to make them line up a bit
303 where
304 comment = ptext (sLit "--")
305
306 {-
307 Note [Printing bangs on data constructors]
308 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
309 For imported data constructors the dataConStrictMarks are the
310 representation choices (see Note [Bangs on data constructor arguments]
311 in DataCon.lhs). So we have to fiddle a little bit here to turn them
312 back into user-printable form.
313 -}