PprTyThing.hs 9.72 KB
Newer Older
1
2
3
4
5
6
7
{-# OPTIONS -w #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and fix
-- any warnings in the module. See
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
-- for details

8
9
10
11
12
13
14
-----------------------------------------------------------------------------
--
-- Pretty-printing TyThings
--
-- (c) The GHC Team 2005
--
-----------------------------------------------------------------------------
15

16
module PprTyThing (
17
	PrintExplicitForalls,
18
19
20
21
	pprTyThing,
	pprTyThingInContext,
	pprTyThingLoc,
	pprTyThingInContextLoc,
22
23
	pprTyThingHdr,
  	pprTypeForUser
24
25
26
27
28
  ) where

#include "HsVersions.h"

import qualified GHC
29

30
import GHC	( TyThing(..) )
31
import TyCon	( tyConFamInst_maybe, isAlgTyCon, tyConStupidTheta )
32
33
34
import Type	( TyThing(..), tidyTopType, pprTypeApp )
import TcType	( tcMultiSplitSigmaTy, mkPhiTy )
import SrcLoc	( SrcSpan )
35
import Var
36
import Name
37
38
39
40
41
42
43
44
import Outputable

-- -----------------------------------------------------------------------------
-- Pretty-printing entities that we get from the GHC API

-- This should be a good source of sample code for using the GHC API to
-- inspect source code entities.

45
46
type PrintExplicitForalls = Bool

47
-- | Pretty-prints a 'TyThing' with its defining location.
48
49
50
pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThingLoc pefas tyThing 
  = showWithLoc loc (pprTyThing pefas tyThing)
51
  where loc = pprNameLoc (GHC.getName tyThing)
52
53

-- | Pretty-prints a 'TyThing'.
54
55
56
57
58
59
pprTyThing :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThing pefas (AnId id)          = pprId         pefas id
pprTyThing pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
pprTyThing pefas (ATyCon tyCon)     = pprTyCon      pefas tyCon
pprTyThing pefas (AClass cls)       = pprClass      pefas cls

60
-- | Like 'pprTyThingInContext', but adds the defining location.
61
62
63
pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThingInContextLoc pefas tyThing
  = showWithLoc loc (pprTyThingInContext pefas tyThing)
64
  where loc = pprNameLoc (GHC.getName tyThing)
65
66
67
68
69

-- | Pretty-prints a 'TyThing' in context: that is, if the entity
-- is a data constructor, record selector, or class method, then 
-- the entity's parent declaration is pretty-printed with irrelevant
-- parts omitted.
70
71
72
73
74
pprTyThingInContext :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThingInContext pefas (AnId id)          = pprIdInContext pefas id
pprTyThingInContext pefas (ADataCon dataCon) = pprDataCon pefas dataCon
pprTyThingInContext pefas (ATyCon tyCon)     = pprTyCon   pefas tyCon
pprTyThingInContext pefas (AClass cls)       = pprClass   pefas cls
75
76
77
78

-- | Pretty-prints the 'TyThing' header. For functions and data constructors
-- the function is equivalent to 'pprTyThing' but for type constructors
-- and classes it prints only the header part of the declaration.
79
80
81
82
83
pprTyThingHdr :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThingHdr pefas (AnId id)          = pprId         pefas id
pprTyThingHdr pefas (ADataCon dataCon) = pprDataConSig pefas dataCon
pprTyThingHdr pefas (ATyCon tyCon)     = pprTyConHdr   pefas tyCon
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 <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
90
  where
91
92
    vars | GHC.isPrimTyCon tyCon || 
	   GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
93
94
	 | otherwise = GHC.tyConTyVars tyCon

95
96
97
98
    keyword | GHC.isSynTyCon tyCon = SLIT("type")
            | GHC.isNewTyCon tyCon = SLIT("newtype")
            | otherwise            = SLIT("data")

99
100
101
    opt_family
      | GHC.isOpenTyCon tyCon = ptext SLIT("family")
      | otherwise             = empty
102

103
104
105
106
    opt_stupid 	-- The "stupid theta" part of the declaration
	| isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
	| otherwise	   = empty	-- Returns 'empty' if null theta

107
pprDataConSig pefas dataCon =
108
  ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
109

110
pprClassHdr pefas cls =
111
112
113
114
115
116
117
  let (tyVars, funDeps) = GHC.classTvsFds cls
  in ptext SLIT("class") <+> 
     GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
     ppr_bndr cls <+>
     hsep (map ppr tyVars) <+>
     GHC.pprFundeps funDeps

118
119
120
121
pprIdInContext pefas id
  | GHC.isRecordSelector id  		  = pprRecordSelector pefas id
  | Just cls <- GHC.isClassOpId_maybe id  = pprClassOneMethod pefas cls id
  | otherwise				  = pprId pefas id
122

123
124
pprRecordSelector pefas id
  = pprAlgTyCon pefas tyCon show_con show_label
125
126
127
128
129
  where
	(tyCon,label) = GHC.recordSelectorFieldLabel id
	show_con dataCon  = label `elem` GHC.dataConFieldLabels dataCon
	show_label label' = label == label'

130
131
pprId :: PrintExplicitForalls -> Var -> SDoc
pprId pefas ident
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  = hang (ppr_bndr ident <+> dcolon)
	 2 (pprTypeForUser pefas (GHC.idType ident))

pprTypeForUser :: PrintExplicitForalls -> GHC.Type -> SDoc
-- We do two things here.
-- a) We tidy the type, regardless
-- b) If PrintExplicitForAlls is True, we discard the foralls
-- 	but we do so `deeply'
-- Prime example: a class op might have type
--	forall a. C a => forall b. Ord b => stuff
-- Then we want to display
--	(C a, Ord b) => stuff
pprTypeForUser print_foralls ty 
  | print_foralls = ppr tidy_ty
  | otherwise     = ppr (mkPhiTy [p | (_tvs, ps) <- ctxt, p <- ps] ty')
  where
    tidy_ty     = tidyTopType ty
    (ctxt, ty') = tcMultiSplitSigmaTy tidy_ty
150

151
pprTyCon pefas tyCon
152
  | GHC.isSynTyCon tyCon
153
  = if GHC.isOpenTyCon tyCon
154
    then pprTyConHdr pefas tyCon <+> dcolon <+> 
155
	 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
156
157
    else 
      let rhs_type = GHC.synTyConType tyCon
158
      in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
159
  | otherwise
160
  = pprAlgTyCon pefas tyCon (const True) (const True)
161

162
163
pprAlgTyCon pefas tyCon ok_con ok_label
  | gadt      = pprTyConHdr pefas tyCon <+> ptext SLIT("where") $$ 
164
		   nest 2 (vcat (ppr_trim show_con datacons))
165
  | otherwise = hang (pprTyConHdr pefas tyCon)
166
167
168
169
170
171
    		   2 (add_bars (ppr_trim show_con datacons))
  where
    datacons = GHC.tyConDataCons tyCon
    gadt = any (not . GHC.isVanillaDataCon) datacons

    show_con dataCon
172
      | ok_con dataCon = Just (pprDataConDecl pefas gadt ok_label dataCon)
173
174
      | otherwise      = Nothing

175
pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
176
177
  where tyCon = GHC.dataConTyCon dataCon

178
pprDataConDecl pefas gadt_style show_label dataCon
179
180
181
182
  | not gadt_style = ppr_fields tys_w_strs
  | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
			sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
  where
183
    (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
184
    tyCon = GHC.dataConTyCon dataCon
185
186
187
188
189
190
191
192
193
194
195
    labels = GHC.dataConFieldLabels dataCon
    qualVars = filter (flip notElem (GHC.tyConTyVars tyCon)) tyvars
    stricts = GHC.dataConStrictMarks dataCon
    tys_w_strs = zip stricts argTypes

    ppr_tvs 
	| null qualVars = empty
	| otherwise     = ptext SLIT("forall") <+> 
				hsep (map ppr qualVars) <> dot

	-- printing out the dataCon as a type signature, in GADT style
196
    pp_tau = foldr add (ppr res_ty) tys_w_strs
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
197
    add str_ty pp_ty = pprParendBangTy str_ty <+> arrow <+> pp_ty
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

    pprParendBangTy (strict,ty)
	| GHC.isMarkedStrict strict = char '!' <> GHC.pprParendType ty
	| otherwise		    = GHC.pprParendType ty

    pprBangTy strict ty
	| GHC.isMarkedStrict strict = char '!' <> ppr ty
	| otherwise		    = ppr ty

    maybe_show_label (lbl,(strict,tp))
	| show_label lbl = Just (ppr lbl <+> dcolon <+> pprBangTy strict tp)
	| otherwise      = Nothing

    ppr_fields [ty1, ty2]
	| GHC.dataConIsInfix dataCon && null labels
	= sep [pprParendBangTy ty1, ppr dataCon, pprParendBangTy ty2]
    ppr_fields fields
	| null labels
	= ppr_bndr dataCon <+> sep (map pprParendBangTy fields)
	| otherwise
	= ppr_bndr dataCon <+> 
		braces (sep (punctuate comma (ppr_trim maybe_show_label 
					(zip labels fields))))
221

222
pprClass pefas cls
223
  | null methods = 
224
	pprClassHdr pefas cls
225
  | otherwise = 
226
227
	hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
	    2 (vcat (map (pprClassMethod pefas) methods))
228
229
230
  where
	methods = GHC.classMethods cls

231
232
233
pprClassOneMethod pefas cls this_one
  = hang (pprClassHdr pefas cls <+> ptext SLIT("where"))
	 2 (vcat (ppr_trim show_meth methods))
234
235
  where
	methods = GHC.classMethods cls
236
	show_meth id | id == this_one = Just (pprClassMethod pefas id)
237
238
		     | otherwise      = Nothing

239
240
pprClassMethod pefas id
  = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
241
242
243
  where
  -- Here's the magic incantation to strip off the dictionary
  -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
244
245
246
247
248
249
250
251
252
253
254
255
  --
  -- It's important to tidy it *before* splitting it up, so that if 
  -- we have	class C a b where
  --	          op :: forall a. a -> b
  -- then the inner forall on op gets renamed to a1, and we print
  -- (when dropping foralls)
  --		class C a b where
  --		  op :: a1 -> b

  tidy_sel_ty = tidyTopType (GHC.idType id)
  (_sel_tyvars, rho_ty) = GHC.splitForAllTys tidy_sel_ty
  op_ty = GHC.funResultTy rho_ty
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273

ppr_trim :: (a -> Maybe SDoc) -> [a] -> [SDoc]
ppr_trim show xs
  = snd (foldr go (False, []) xs)
  where
    go x (eliding, so_far)
	| Just doc <- show x = (False, doc : so_far)
	| otherwise = if eliding then (True, so_far)
		                 else (True, ptext SLIT("...") : so_far)

add_bars []      = empty
add_bars [c]     = equals <+> c
add_bars (c:cs)  = sep ((equals <+> c) : map (char '|' <+>) cs)

-- Wrap operators in ()
ppr_bndr :: GHC.NamedThing a => a -> SDoc
ppr_bndr a = GHC.pprParenSymName a

274
showWithLoc :: SDoc -> SDoc -> SDoc
275
showWithLoc loc doc 
276
    = hang doc 2 (char '\t' <> comment <+> loc)
277
278
279
280
		-- The tab tries to make them line up a bit
  where
    comment = ptext SLIT("--")