Improve printing of TyThings; fixes Trac #4087
[ghc-hetmet.git] / compiler / main / PprTyThing.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing TyThings
4 --
5 -- (c) The GHC Team 2005
6 --
7 -----------------------------------------------------------------------------
8
9 module PprTyThing (
10         PrintExplicitForalls,
11         pprTyThing,
12         pprTyThingInContext,
13         pprTyThingLoc,
14         pprTyThingInContextLoc,
15         pprTyThingHdr,
16         pprTypeForUser
17   ) where
18
19 import qualified GHC
20
21 import GHC ( TyThing(..) )
22 import TyCon
23 import TcType
24 import Var
25 import Name
26 import Outputable
27 import FastString
28
29 -- -----------------------------------------------------------------------------
30 -- Pretty-printing entities that we get from the GHC API
31
32 -- This should be a good source of sample code for using the GHC API to
33 -- inspect source code entities.
34
35 type PrintExplicitForalls = Bool
36
37 -- | Pretty-prints a 'TyThing' with its defining location.
38 pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
39 pprTyThingLoc pefas tyThing 
40   = showWithLoc loc (pprTyThing pefas tyThing)
41   where loc = pprNameLoc (GHC.getName tyThing)
42
43 -- | Pretty-prints a 'TyThing'.
44 pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
45 pprTyThing pefas (AnId id)          = pprId         pefas id
46 pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
47 pprTyThing pefas (ATyCon tyCon)     = pprTyCon      pefas tyCon
48 pprTyThing pefas (AClass cls)       = pprClass      pefas cls
49
50 -- | Like 'pprTyThingInContext', but adds the defining location.
51 pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
52 pprTyThingInContextLoc pefas tyThing
53   = showWithLoc loc (pprTyThingInContext pefas tyThing)
54   where loc = pprNameLoc (GHC.getName tyThing)
55
56 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
57 -- is a data constructor, record selector, or class method, then 
58 -- the entity's parent declaration is pretty-printed with irrelevant
59 -- parts omitted.
60 pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
61 pprTyThingInContext pefas (AnId id)          = pprIdInContext pefas id
62 pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
63 pprTyThingInContext pefas (ATyCon tyCon)     = pprTyCon   pefas tyCon
64 pprTyThingInContext pefas (AClass cls)       = pprClass   pefas cls
65
66 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
67 -- the function is equivalent to 'pprTyThing' but for type constructors
68 -- and classes it prints only the header part of the declaration.
69 pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
70 pprTyThingHdr pefas (AnId id)          = pprId         pefas id
71 pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
72 pprTyThingHdr pefas (ATyCon tyCon)     = pprTyConHdr   pefas tyCon
73 pprTyThingHdr pefas (AClass cls)       = pprClassHdr   pefas cls
74
75 pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
76 pprTyConHdr _ tyCon
77   | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
78   = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
79   | otherwise
80   = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
81   where
82     vars | GHC.isPrimTyCon tyCon || 
83            GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
84          | otherwise = GHC.tyConTyVars tyCon
85
86     keyword | GHC.isSynTyCon tyCon = sLit "type"
87             | GHC.isNewTyCon tyCon = sLit "newtype"
88             | otherwise            = sLit "data"
89
90     opt_family
91       | GHC.isOpenTyCon tyCon = ptext (sLit "family")
92       | otherwise             = empty
93
94     opt_stupid  -- The "stupid theta" part of the declaration
95         | isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
96         | otherwise        = empty      -- Returns 'empty' if null theta
97
98 pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
99 pprDataConSig pefas dataCon =
100   ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
101
102 pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
103 pprClassHdr _ cls =
104   let (tyVars, funDeps) = GHC.classTvsFds cls
105   in ptext (sLit "class") <+> 
106      GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
107      ppr_bndr cls <+>
108      hsep (map ppr tyVars) <+>
109      GHC.pprFundeps funDeps
110
111 pprIdInContext :: PrintExplicitForalls -> Var -> SDoc
112 pprIdInContext pefas id
113   | GHC.isRecordSelector id               = pprRecordSelector pefas id
114   | Just cls <- GHC.isClassOpId_maybe id  = pprClassOneMethod pefas cls id
115   | otherwise                             = pprId pefas id
116
117 pprRecordSelector :: PrintExplicitForalls -> Id -> SDoc
118 pprRecordSelector pefas id
119   = pprAlgTyCon pefas tyCon show_con show_label
120   where
121         (tyCon,label) = GHC.recordSelectorFieldLabel id
122         show_con dataCon  = label `elem` GHC.dataConFieldLabels dataCon
123         show_label label' = label == label'
124
125 pprId :: PrintExplicitForalls -> Var -> SDoc
126 pprId pefas ident
127   = hang (ppr_bndr ident <+> dcolon)
128          2 (pprTypeForUser pefas (GHC.idType ident))
129
130 pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
131 -- We do two things here.
132 -- a) We tidy the type, regardless
133 -- b) If PrintExplicitForAlls is True, we discard the foralls
134 --      but we do so `deeply'
135 -- Prime example: a class op might have type
136 --      forall a. C a => forall b. Ord b => stuff
137 -- Then we want to display
138 --      (C a, Ord b) => stuff
139 pprTypeForUser print_foralls ty 
140   | print_foralls = ppr tidy_ty
141   | otherwise     = ppr (mkPhiTy [p | (_tvs, ps) <- ctxt, p <- ps] ty')
142   where
143     tidy_ty     = tidyTopType ty
144     (ctxt, ty') = tcMultiSplitSigmaTy tidy_ty
145
146 pprTyCon :: PrintExplicitForalls -> TyCon -> SDoc
147 pprTyCon pefas tyCon
148   | GHC.isSynTyCon tyCon
149   = if GHC.isOpenTyCon tyCon
150     then pprTyConHdr pefas tyCon <+> dcolon <+> 
151          pprTypeForUser pefas (GHC.synTyConResKind tyCon)
152     else 
153       let rhs_type = GHC.synTyConType tyCon
154       in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
155   | otherwise
156   = pprAlgTyCon pefas tyCon (const True) (const True)
157
158 pprAlgTyCon :: PrintExplicitForalls -> TyCon -> (GHC.DataCon -> Bool)
159             -> (FieldLabel -> Bool) -> SDoc
160 pprAlgTyCon pefas tyCon ok_con ok_label
161   | gadt      = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$ 
162                    nest 2 (vcat (ppr_trim show_con datacons))
163   | otherwise = hang (pprTyConHdr pefas tyCon)
164                    2 (add_bars (ppr_trim show_con datacons))
165   where
166     datacons = GHC.tyConDataCons tyCon
167     gadt = any (not . GHC.isVanillaDataCon) datacons
168
169     show_con dataCon
170       | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
171       | otherwise      = Nothing
172
173 pprDataCon :: PrintExplicitForalls -> GHC.DataCon -> SDoc
174 pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
175   where tyCon = GHC.dataConTyCon dataCon
176
177 pprDataConDecl :: PrintExplicitForalls -> Bool -> (FieldLabel -> Bool)
178                -> GHC.DataCon -> SDoc
179 pprDataConDecl _ gadt_style show_label dataCon
180   | not gadt_style = ppr_fields tys_w_strs
181   | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
182                         sep [ GHC.pprForAll forall_tvs, GHC.pprThetaArrow theta, pp_tau ]
183         -- Printing out the dataCon as a type signature, in GADT style
184   where
185     (forall_tvs, theta, tau) = tcSplitSigmaTy (GHC.dataConUserType dataCon)
186     (arg_tys, res_ty)        = tcSplitFunTys tau
187     labels     = GHC.dataConFieldLabels dataCon
188     stricts    = GHC.dataConStrictMarks dataCon
189     tys_w_strs = zip stricts arg_tys
190
191     pp_tau = foldr add (ppr res_ty) tys_w_strs
192     add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
193
194     pprParendBangTy (bang,ty) = ppr bang <> GHC.pprParendType ty
195
196     pprBangTy bang ty = ppr bang <> ppr ty
197
198     maybe_show_label (lbl,(strict,tp))
199         | show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
200         | otherwise      = Nothing
201
202     ppr_fields [ty1, ty2]
203         | GHC.dataConIsInfix dataCon && null labels
204         = sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
205     ppr_fields fields
206         | null labels
207         = ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
208         | otherwise
209         = ppr_bndr dataCon <+> 
210                 braces (sep (punctuate comma (ppr_trim maybe_show_label 
211                                         (zip labels fields))))
212
213 pprClass :: PrintExplicitForalls -> GHC.Class -> SDoc
214 pprClass pefas cls
215   | null methods = 
216         pprClassHdr pefas cls
217   | otherwise = 
218         hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
219             2 (vcat (map (pprClassMethod pefas) methods))
220   where
221         methods = GHC.classMethods cls
222
223 pprClassOneMethod :: PrintExplicitForalls -> GHC.Class -> Id -> SDoc
224 pprClassOneMethod pefas cls this_one
225   = hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
226          2 (vcat (ppr_trim show_meth methods))
227   where
228         methods = GHC.classMethods cls
229         show_meth id | id == this_one = Just (pprClassMethod pefas id)
230                      | otherwise      = Nothing
231
232 pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
233 pprClassMethod pefas id
234   = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
235   where
236   -- Here's the magic incantation to strip off the dictionary
237   -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
238   --
239   -- It's important to tidy it *before* splitting it up, so that if 
240   -- we have    class C a b where
241   --              op :: forall a. a -> b
242   -- then the inner forall on op gets renamed to a1, and we print
243   -- (when dropping foralls)
244   --            class C a b where
245   --              op :: a1 -> b
246
247   tidy_sel_ty = tidyTopType (GHC.idType id)
248   (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
249   op_ty = GHC.funResultTy rho_ty
250
251 ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
252 ppr_trim show xs
253   = snd (foldr go (False, []) xs)
254   where
255     go x (eliding, so_far)
256         | Just doc <- show x = (False, doc : so_far)
257         | otherwise = if eliding then (True, so_far)
258                                  else (True, ptext (sLit "...") : so_far)
259
260 add_bars :: [SDoc] -> SDoc
261 add_bars []      = empty
262 add_bars [c]     = equals <+> c
263 add_bars (c:cs)  = sep ((equals <+> c) : map (char '|' <+>) cs)
264
265 -- Wrap operators in ()
266 ppr_bndr :: GHC.NamedThing a => a -> SDoc
267 ppr_bndr a = GHC.pprParenSymName a
268
269 showWithLoc :: SDoc -> SDoc -> SDoc
270 showWithLoc loc doc 
271     = hang doc 2 (char '\t' <> comment <+> loc)
272                 -- The tab tries to make them line up a bit
273   where
274     comment = ptext (sLit "--")
275