PprTyThing.hs 10.1 KB
Newer Older
1 2 3 4 5 6 7
-----------------------------------------------------------------------------
--
-- Pretty-printing TyThings
--
-- (c) The GHC Team 2005
--
-----------------------------------------------------------------------------
8

9
module PprTyThing (
10
	PrintExplicitForalls,
11 12 13 14
	pprTyThing,
	pprTyThingInContext,
	pprTyThingLoc,
	pprTyThingInContextLoc,
15 16
	pprTyThingHdr,
  	pprTypeForUser
17 18 19
  ) where

import qualified GHC
20

Ian Lynagh's avatar
Ian Lynagh committed
21 22 23 24
import GHC ( TyThing(..) )
import TyCon
import Type ( TyThing(..), tidyTopType, pprTypeApp )
import TcType
25
import Var
26
import Name
27
import Outputable
28
import FastString
29 30 31 32 33 34 35

-- -----------------------------------------------------------------------------
-- 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.

36 37
type PrintExplicitForalls = Bool

38
-- | Pretty-prints a 'TyThing' with its defining location.
39 40 41
pprTyThingLoc :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThingLoc pefas tyThing 
  = showWithLoc loc (pprTyThing pefas tyThing)
42
  where loc = pprNameLoc (GHC.getName tyThing)
43 44

-- | Pretty-prints a 'TyThing'.
45 46 47 48 49 50
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

51
-- | Like 'pprTyThingInContext', but adds the defining location.
52 53 54
pprTyThingInContextLoc :: PrintExplicitForalls -> TyThing -> SDoc
pprTyThingInContextLoc pefas tyThing
  = showWithLoc loc (pprTyThingInContext pefas tyThing)
55
  where loc = pprNameLoc (GHC.getName tyThing)
56 57 58 59 60

-- | 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.
61 62 63 64 65
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
66 67 68 69

-- | 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.
70 71 72 73 74
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
Ian Lynagh's avatar
Ian Lynagh committed
75

Ian Lynagh's avatar
Ian Lynagh committed
76
pprTyConHdr :: PrintExplicitForalls -> TyCon -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
77 78
pprTyConHdr _ tyCon
  | Just (_fam_tc, tys) <- tyConFamInst_maybe tyCon
79
  = ptext keyword <+> ptext (sLit "instance") <+> pprTypeApp tyCon tys
80
  | otherwise
81
  = ptext keyword <+> opt_family <+> opt_stupid <+> ppr_bndr tyCon <+> hsep (map ppr vars)
82
  where
83 84
    vars | GHC.isPrimTyCon tyCon || 
	   GHC.isFunTyCon tyCon = take (GHC.tyConArity tyCon) GHC.alphaTyVars
85 86
	 | otherwise = GHC.tyConTyVars tyCon

Ian Lynagh's avatar
Ian Lynagh committed
87 88 89
    keyword | GHC.isSynTyCon tyCon = sLit "type"
            | GHC.isNewTyCon tyCon = sLit "newtype"
            | otherwise            = sLit "data"
90

91
    opt_family
Ian Lynagh's avatar
Ian Lynagh committed
92
      | GHC.isOpenTyCon tyCon = ptext (sLit "family")
93
      | otherwise             = empty
94

95 96 97 98
    opt_stupid 	-- The "stupid theta" part of the declaration
	| isAlgTyCon tyCon = GHC.pprThetaArrow (tyConStupidTheta tyCon)
	| otherwise	   = empty	-- Returns 'empty' if null theta

Ian Lynagh's avatar
Ian Lynagh committed
99
pprDataConSig :: PrintExplicitForalls -> GHC.DataCon -> SDoc
100
pprDataConSig pefas dataCon =
101
  ppr_bndr dataCon <+> dcolon <+> pprTypeForUser pefas (GHC.dataConType dataCon)
102

Ian Lynagh's avatar
Ian Lynagh committed
103
pprClassHdr :: PrintExplicitForalls -> GHC.Class -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
104
pprClassHdr _ cls =
105
  let (tyVars, funDeps) = GHC.classTvsFds cls
Ian Lynagh's avatar
Ian Lynagh committed
106
  in ptext (sLit "class") <+> 
107 108 109 110 111
     GHC.pprThetaArrow (GHC.classSCTheta cls) <+>
     ppr_bndr cls <+>
     hsep (map ppr tyVars) <+>
     GHC.pprFundeps funDeps

Ian Lynagh's avatar
Ian Lynagh committed
112
pprIdInContext :: PrintExplicitForalls -> Var -> SDoc
113 114 115 116
pprIdInContext pefas id
  | GHC.isRecordSelector id  		  = pprRecordSelector pefas id
  | Just cls <- GHC.isClassOpId_maybe id  = pprClassOneMethod pefas cls id
  | otherwise				  = pprId pefas id
117

Ian Lynagh's avatar
Ian Lynagh committed
118
pprRecordSelector :: PrintExplicitForalls -> Id -> SDoc
119 120
pprRecordSelector pefas id
  = pprAlgTyCon pefas tyCon show_con show_label
121 122 123 124 125
  where
	(tyCon,label) = GHC.recordSelectorFieldLabel id
	show_con dataCon  = label `elem` GHC.dataConFieldLabels dataCon
	show_label label' = label == label'

126 127
pprId :: PrintExplicitForalls -> Var -> SDoc
pprId pefas ident
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  = 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
146

Ian Lynagh's avatar
Ian Lynagh committed
147
pprTyCon :: PrintExplicitForalls -> TyCon -> SDoc
148
pprTyCon pefas tyCon
149
  | GHC.isSynTyCon tyCon
150
  = if GHC.isOpenTyCon tyCon
151
    then pprTyConHdr pefas tyCon <+> dcolon <+> 
152
	 pprTypeForUser pefas (GHC.synTyConResKind tyCon)
153 154
    else 
      let rhs_type = GHC.synTyConType tyCon
155
      in hang (pprTyConHdr pefas tyCon <+> equals) 2 (pprTypeForUser pefas rhs_type)
156
  | otherwise
157
  = pprAlgTyCon pefas tyCon (const True) (const True)
158

Ian Lynagh's avatar
Ian Lynagh committed
159
pprAlgTyCon :: PrintExplicitForalls -> TyCon -> (GHC.DataCon -> Bool)
Ian Lynagh's avatar
Ian Lynagh committed
160
            -> (FieldLabel -> Bool) -> SDoc
161
pprAlgTyCon pefas tyCon ok_con ok_label
Ian Lynagh's avatar
Ian Lynagh committed
162
  | gadt      = pprTyConHdr pefas tyCon <+> ptext (sLit "where") $$ 
163
		   nest 2 (vcat (ppr_trim show_con datacons))
164
  | otherwise = hang (pprTyConHdr pefas tyCon)
165 166 167 168 169 170
    		   2 (add_bars (ppr_trim show_con datacons))
  where
    datacons = GHC.tyConDataCons tyCon
    gadt = any (not . GHC.isVanillaDataCon) datacons

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

Ian Lynagh's avatar
Ian Lynagh committed
174
pprDataCon :: PrintExplicitForalls -> GHC.DataCon -> SDoc
175
pprDataCon pefas dataCon = pprAlgTyCon pefas tyCon (== dataCon) (const True)
176 177
  where tyCon = GHC.dataConTyCon dataCon

Ian Lynagh's avatar
Ian Lynagh committed
178
pprDataConDecl :: PrintExplicitForalls -> Bool -> (FieldLabel -> Bool)
Ian Lynagh's avatar
Ian Lynagh committed
179
               -> GHC.DataCon -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
180
pprDataConDecl _ gadt_style show_label dataCon
181 182 183 184
  | not gadt_style = ppr_fields tys_w_strs
  | otherwise      = ppr_bndr dataCon <+> dcolon <+> 
			sep [ ppr_tvs, GHC.pprThetaArrow theta, pp_tau ]
  where
185
    (tyvars, theta, argTypes, res_ty) = GHC.dataConSig dataCon
186
    tyCon = GHC.dataConTyCon dataCon
187 188 189 190 191 192 193
    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
Ian Lynagh's avatar
Ian Lynagh committed
194
	| otherwise     = ptext (sLit "forall") <+> 
195 196 197
				hsep (map ppr qualVars) <> dot

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

    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
215
	= sep [pprParendBangTy ty1, pprInfixName dataCon, pprParendBangTy ty2]
216 217 218 219 220 221 222
    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))))
223

Ian Lynagh's avatar
Ian Lynagh committed
224
pprClass :: PrintExplicitForalls -> GHC.Class -> SDoc
225
pprClass pefas cls
226
  | null methods = 
227
	pprClassHdr pefas cls
228
  | otherwise = 
Ian Lynagh's avatar
Ian Lynagh committed
229
	hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
230
	    2 (vcat (map (pprClassMethod pefas) methods))
231 232 233
  where
	methods = GHC.classMethods cls

Ian Lynagh's avatar
Ian Lynagh committed
234
pprClassOneMethod :: PrintExplicitForalls -> GHC.Class -> Id -> SDoc
235
pprClassOneMethod pefas cls this_one
Ian Lynagh's avatar
Ian Lynagh committed
236
  = hang (pprClassHdr pefas cls <+> ptext (sLit "where"))
237
	 2 (vcat (ppr_trim show_meth methods))
238 239
  where
	methods = GHC.classMethods cls
240
	show_meth id | id == this_one = Just (pprClassMethod pefas id)
241 242
		     | otherwise      = Nothing

Ian Lynagh's avatar
Ian Lynagh committed
243
pprClassMethod :: PrintExplicitForalls -> Id -> SDoc
244 245
pprClassMethod pefas id
  = hang (ppr_bndr id <+> dcolon) 2 (pprTypeForUser pefas op_ty)
246 247 248
  where
  -- Here's the magic incantation to strip off the dictionary
  -- from the class op type.  Stolen from IfaceSyn.tyThingToIfaceDecl.
249 250 251 252 253 254 255 256 257 258 259 260
  --
  -- 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
261 262 263 264 265 266 267 268

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)
Ian Lynagh's avatar
Ian Lynagh committed
269
		                 else (True, ptext (sLit "...") : so_far)
270

Ian Lynagh's avatar
Ian Lynagh committed
271
add_bars :: [SDoc] -> SDoc
272 273 274 275 276 277 278 279
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

280
showWithLoc :: SDoc -> SDoc -> SDoc
281
showWithLoc loc doc 
282
    = hang doc 2 (char '\t' <> comment <+> loc)
283 284
		-- The tab tries to make them line up a bit
  where
Ian Lynagh's avatar
Ian Lynagh committed
285
    comment = ptext (sLit "--")
286