2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
5 This module defines interface types and binders
9 IfaceType(..), IfaceKind, IfacePredType(..), IfaceTyCon(..),
10 IfaceContext, IfaceBndr(..), IfaceTvBndr, IfaceIdBndr,
12 IfaceExtName(..), mkIfaceExtName, ifaceTyConName,
14 -- Conversion from Type -> IfaceType
15 toIfaceType, toIfacePred, toIfaceContext,
16 toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs,
19 pprIfaceType, pprParendIfaceType, pprIfaceContext,
20 pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
21 tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
25 #include "HsVersions.h"
27 import Kind ( Kind(..) )
28 import TypeRep ( Type(..), TyNote(..), PredType(..), Kind, ThetaType )
29 import TyCon ( TyCon, isTupleTyCon, tyConArity, tupleTyConBoxity )
30 import Var ( isId, tyVarKind, idType )
31 import TysWiredIn ( listTyConName, parrTyConName, tupleTyCon, intTyConName, charTyConName, boolTyConName )
32 import OccName ( OccName )
33 import Name ( Name, getName, getOccName, nameModule, nameOccName )
34 import Module ( Module )
35 import BasicTypes ( IPName(..), Arity, Version, mapIPName, tupleParens, Boxity )
41 %************************************************************************
45 %************************************************************************
49 = ExtPkg Module OccName -- From an external package; no version #
50 -- Also used for wired-in things regardless
51 -- of whether they are home-pkg or not
53 | HomePkg Module OccName Version -- From another module in home package;
54 -- has version #; in all other respects,
55 -- HomePkg and ExtPkg are the same
57 | LocalTop OccName -- Top-level from the same module as
58 -- the enclosing IfaceDecl
60 | LocalTopSub -- Same as LocalTop, but for a class method or constr
61 OccName -- Class-meth/constr name
62 OccName -- Parent class/datatype name
63 -- LocalTopSub is written into iface files as LocalTop; the parent
64 -- info is only used when computing version information in MkIface
66 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
67 -- Local helper for wired-in names
71 %************************************************************************
73 Local (nested) binders
75 %************************************************************************
78 data IfaceBndr -- Local (non-top-level) binders
79 = IfaceIdBndr IfaceIdBndr
80 | IfaceTvBndr IfaceTvBndr
82 type IfaceIdBndr = (OccName, IfaceType) -- OccName, because always local
83 type IfaceTvBndr = (OccName, IfaceKind)
85 -------------------------------
86 type IfaceKind = Kind -- Re-use the Kind type, but no KindVars in it
89 = IfaceTyVar OccName -- Type variable only, not tycon
90 | IfaceAppTy IfaceType IfaceType
91 | IfaceForAllTy IfaceTvBndr IfaceType
92 | IfacePredTy IfacePredType
93 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
94 -- Includes newtypes, synonyms, tuples
95 | IfaceFunTy IfaceType IfaceType
97 data IfacePredType -- NewTypes are handled as ordinary TyConApps
98 = IfaceClassP IfaceExtName [IfaceType]
99 | IfaceIParam (IPName OccName) IfaceType
101 type IfaceContext = [IfacePredType]
103 data IfaceTyCon -- Abbreviations for common tycons with known names
104 = IfaceTc IfaceExtName -- The common case
105 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
106 | IfaceListTc | IfacePArrTc
107 | IfaceTupTc Boxity Arity
109 ifaceTyConName :: IfaceTyCon -> Name -- Works for all except IfaceTc
110 ifaceTyConName IfaceIntTc = intTyConName
111 ifaceTyConName IfaceBoolTc = boolTyConName
112 ifaceTyConName IfaceCharTc = charTyConName
113 ifaceTyConName IfaceListTc = listTyConName
114 ifaceTyConName IfacePArrTc = parrTyConName
115 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
116 ifaceTyConName (IfaceTc ext) = pprPanic "ifaceTyConName" (ppr ext)
120 %************************************************************************
122 Functions over IFaceTypes
124 %************************************************************************
128 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
129 -- Mainly for printing purposes
133 (tvs, rho) = split_foralls ty
134 (theta, tau) = split_rho rho
136 split_foralls (IfaceForAllTy tv ty)
137 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
138 split_foralls rho = ([], rho)
140 split_rho (IfaceFunTy (IfacePredTy st) ty)
141 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
142 split_rho tau = ([], tau)
145 %************************************************************************
149 %************************************************************************
153 @ppr_ty@ takes an @Int@ that is the precedence of the context.
154 The precedence levels are:
156 \item[tOP_PREC] No parens required.
157 \item[fUN_PREC] Left hand argument of a function arrow.
158 \item[tYCON_PREC] Argument of a type constructor.
162 tOP_PREC = (0 :: Int) -- type in ParseIface.y
163 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
164 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
166 noParens :: SDoc -> SDoc
169 maybeParen ctxt_prec inner_prec pretty
170 | ctxt_prec < inner_prec = pretty
171 | otherwise = parens pretty
175 ----------------------------- Printing binders ------------------------------------
178 -- These instances are used only when printing for the user, either when
179 -- debugging, or in GHCi when printing the results of a :info command
180 instance Outputable IfaceExtName where
181 ppr (ExtPkg mod occ) = pprExt mod occ
182 ppr (HomePkg mod occ vers) = pprExt mod occ <> braces (ppr vers)
183 ppr (LocalTop occ) = ppr occ -- Do we want to distinguish these
184 ppr (LocalTopSub occ _) = ppr occ -- from an ordinary occurrence?
186 pprExt :: Module -> OccName -> SDoc
188 = getPprStyle $ \ sty ->
189 if unqualStyle sty mod occ then
192 ppr mod <> dot <> ppr occ
194 instance Outputable IfaceBndr where
195 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
196 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
198 pprIfaceBndrs :: [IfaceBndr] -> SDoc
199 pprIfaceBndrs bs = sep (map ppr bs)
201 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
203 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
204 pprIfaceTvBndr (tv, LiftedTypeKind) = ppr tv
205 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
207 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
208 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
211 ----------------------------- Printing IfaceType ------------------------------------
214 ---------------------------------
215 instance Outputable IfaceType where
216 ppr ty = pprIfaceTypeForUser ty
218 pprIfaceTypeForUser ::IfaceType -> SDoc
219 -- Drop top-level for-alls; if that's not what you want, use pprIfaceType dire
220 pprIfaceTypeForUser ty
221 = pprIfaceForAllPart [] theta (pprIfaceType tau)
223 (_tvs, theta, tau) = splitIfaceSigmaTy ty
225 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
226 pprIfaceType = ppr_ty tOP_PREC
227 pprParendIfaceType = ppr_ty tYCON_PREC
230 ppr_ty :: Int -> IfaceType -> SDoc
231 ppr_ty ctxt_prec (IfaceTyVar tyvar) = ppr tyvar
232 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
233 ppr_ty ctxt_prec (IfacePredTy st) = ppr st
236 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
237 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
238 maybeParen ctxt_prec fUN_PREC $
239 sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
241 ppr_fun_tail (IfaceFunTy ty1 ty2)
242 = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
243 ppr_fun_tail other_ty
244 = [arrow <+> pprIfaceType other_ty]
246 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
247 = maybeParen ctxt_prec tYCON_PREC $
248 ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
250 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
251 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
253 (tvs, theta, tau) = splitIfaceSigmaTy ty
256 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
257 pprIfaceForAllPart tvs ctxt doc
258 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
260 ppr_tvs | null tvs = empty
261 | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
264 ppr_tc_app ctxt_prec tc [] = ppr tc
265 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets (pprIfaceType ty)
266 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
267 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
268 | arity == length tys
269 = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
270 ppr_tc_app ctxt_prec tc tys
271 = maybeParen ctxt_prec tYCON_PREC
272 (sep [ppr tc, nest 4 (sep (map pprParendIfaceType tys))])
275 instance Outputable IfacePredType where
276 -- Print without parens
277 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
278 ppr (IfaceClassP cls ts) = ppr cls <+> sep (map pprParendIfaceType ts)
280 instance Outputable IfaceTyCon where
281 ppr (IfaceTc ext) = ppr ext
282 ppr other_tc = ppr (ifaceTyConName other_tc)
285 pprIfaceContext :: IfaceContext -> SDoc
286 -- Prints "(C a, D b) =>", including the arrow
287 pprIfaceContext [] = empty
288 pprIfaceContext theta = parens (sep (punctuate comma (map ppr theta)))
291 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
294 %************************************************************************
296 Conversion from Type to IfaceType
298 %************************************************************************
302 toIfaceTvBndr tyvar = (getOccName tyvar, tyVarKind tyvar)
303 toIfaceIdBndr ext id = (getOccName id, toIfaceType ext (idType id))
304 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
307 | isId var = IfaceIdBndr (toIfaceIdBndr ext var)
308 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
310 ---------------------
311 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
312 toIfaceType ext (TyVarTy tv) = IfaceTyVar (getOccName tv)
313 toIfaceType ext (AppTy t1 t2) = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
314 toIfaceType ext (FunTy t1 t2) = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
315 toIfaceType ext (TyConApp tc tys) = IfaceTyConApp (mkIfaceTc ext tc) (toIfaceTypes ext tys)
316 toIfaceType ext (ForAllTy tv t) = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
317 toIfaceType ext (PredTy st) = IfacePredTy (toIfacePred ext st)
318 toIfaceType ext (NoteTy (SynNote tc_app) ty) = toIfaceType ext tc_app
319 toIfaceType ext (NoteTy other_note ty) = toIfaceType ext ty
322 mkIfaceTc :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
324 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
325 | nm == intTyConName = IfaceIntTc
326 | nm == boolTyConName = IfaceBoolTc
327 | nm == charTyConName = IfaceCharTc
328 | nm == listTyConName = IfaceListTc
329 | nm == parrTyConName = IfacePArrTc
330 | otherwise = IfaceTc (ext nm)
335 toIfaceTypes ext ts = map (toIfaceType ext) ts
338 toIfacePred ext (ClassP cls ts) = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
339 toIfacePred ext (IParam ip t) = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
342 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
343 toIfaceContext ext cs = map (toIfacePred ext) cs