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