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;
56 | LocalTop OccName -- Top-level from the same module as
57 -- the enclosing IfaceDecl
59 | LocalTopSub -- Same as LocalTop, but for a class method or constr
60 OccName -- Class-meth/constr name
61 OccName -- Parent class/datatype name
62 -- LocalTopSub is written into iface files as LocalTop; the parent
63 -- info is only used when computing version information in MkIface
65 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
66 -- Local helper for wired-in names
70 %************************************************************************
72 Local (nested) binders
74 %************************************************************************
77 data IfaceBndr -- Local (non-top-level) binders
78 = IfaceIdBndr IfaceIdBndr
79 | IfaceTvBndr IfaceTvBndr
81 type IfaceIdBndr = (OccName, IfaceType) -- OccName, because always local
82 type IfaceTvBndr = (OccName, IfaceKind)
84 -------------------------------
85 type IfaceKind = Kind -- Re-use the Kind type, but no KindVars in it
88 = IfaceTyVar OccName -- Type variable only, not tycon
89 | IfaceAppTy IfaceType IfaceType
90 | IfaceForAllTy IfaceTvBndr IfaceType
91 | IfacePredTy IfacePredType
92 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
93 -- Includes newtypes, synonyms, tuples
94 | IfaceFunTy IfaceType IfaceType
96 data IfacePredType -- NewTypes are handled as ordinary TyConApps
97 = IfaceClassP IfaceExtName [IfaceType]
98 | IfaceIParam (IPName OccName) IfaceType
100 type IfaceContext = [IfacePredType]
102 data IfaceTyCon -- Abbreviations for common tycons with known names
103 = IfaceTc IfaceExtName -- The common case
104 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
105 | IfaceListTc | IfacePArrTc
106 | IfaceTupTc Boxity Arity
108 ifaceTyConName :: IfaceTyCon -> Name -- Works for all except IfaceTc
109 ifaceTyConName IfaceIntTc = intTyConName
110 ifaceTyConName IfaceBoolTc = boolTyConName
111 ifaceTyConName IfaceCharTc = charTyConName
112 ifaceTyConName IfaceListTc = listTyConName
113 ifaceTyConName IfacePArrTc = parrTyConName
114 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
115 ifaceTyConName (IfaceTc ext) = pprPanic "ifaceTyConName" (ppr ext)
119 %************************************************************************
121 Functions over IFaceTypes
123 %************************************************************************
127 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
128 -- Mainly for printing purposes
132 (tvs, rho) = split_foralls ty
133 (theta, tau) = split_rho rho
135 split_foralls (IfaceForAllTy tv ty)
136 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
137 split_foralls rho = ([], rho)
139 split_rho (IfaceFunTy (IfacePredTy st) ty)
140 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
141 split_rho tau = ([], tau)
144 %************************************************************************
148 %************************************************************************
152 @ppr_ty@ takes an @Int@ that is the precedence of the context.
153 The precedence levels are:
155 \item[tOP_PREC] No parens required.
156 \item[fUN_PREC] Left hand argument of a function arrow.
157 \item[tYCON_PREC] Argument of a type constructor.
161 tOP_PREC = (0 :: Int) -- type in ParseIface.y
162 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
163 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
165 noParens :: SDoc -> SDoc
168 maybeParen ctxt_prec inner_prec pretty
169 | ctxt_prec < inner_prec = pretty
170 | otherwise = parens pretty
174 ----------------------------- Printing binders ------------------------------------
177 -- These instances are used only when printing for the user, either when
178 -- debugging, or in GHCi when printing the results of a :info command
179 instance Outputable IfaceExtName where
180 ppr (ExtPkg mod occ) = pprExt mod occ
181 ppr (HomePkg mod occ vers) = pprExt mod occ <> braces (ppr vers)
182 ppr (LocalTop occ) = ppr occ -- Do we want to distinguish these
183 ppr (LocalTopSub occ _) = ppr occ -- from an ordinary occurrence?
185 pprExt :: Module -> OccName -> SDoc
187 = getPprStyle $ \ sty ->
188 if unqualStyle sty mod occ then
191 ppr mod <> dot <> ppr occ
193 instance Outputable IfaceBndr where
194 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
195 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
197 pprIfaceBndrs :: [IfaceBndr] -> SDoc
198 pprIfaceBndrs bs = sep (map ppr bs)
200 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
202 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
203 pprIfaceTvBndr (tv, LiftedTypeKind) = ppr tv
204 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
206 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
207 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
210 ----------------------------- Printing IfaceType ------------------------------------
213 ---------------------------------
214 instance Outputable IfaceType where
215 ppr ty = pprIfaceTypeForUser ty
217 pprIfaceTypeForUser ::IfaceType -> SDoc
218 -- Drop top-level for-alls; if that's not what you want, use pprIfaceType dire
219 pprIfaceTypeForUser ty
220 = pprIfaceForAllPart [] theta (pprIfaceType tau)
222 (_tvs, theta, tau) = splitIfaceSigmaTy ty
224 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
225 pprIfaceType = ppr_ty tOP_PREC
226 pprParendIfaceType = ppr_ty tYCON_PREC
229 ppr_ty :: Int -> IfaceType -> SDoc
230 ppr_ty ctxt_prec (IfaceTyVar tyvar) = ppr tyvar
231 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
232 ppr_ty ctxt_prec (IfacePredTy st) = ppr st
235 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
236 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
237 maybeParen ctxt_prec fUN_PREC $
238 sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
240 ppr_fun_tail (IfaceFunTy ty1 ty2)
241 = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
242 ppr_fun_tail other_ty
243 = [arrow <+> pprIfaceType other_ty]
245 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
246 = maybeParen ctxt_prec tYCON_PREC $
247 ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
249 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
250 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
252 (tvs, theta, tau) = splitIfaceSigmaTy ty
255 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
256 pprIfaceForAllPart tvs ctxt doc
257 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
259 ppr_tvs | null tvs = empty
260 | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
263 ppr_tc_app ctxt_prec tc [] = ppr tc
264 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets (pprIfaceType ty)
265 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
266 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
267 | arity == length tys
268 = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
269 ppr_tc_app ctxt_prec tc tys
270 = maybeParen ctxt_prec tYCON_PREC
271 (sep [ppr tc, nest 4 (sep (map pprParendIfaceType tys))])
274 instance Outputable IfacePredType where
275 -- Print without parens
276 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
277 ppr (IfaceClassP cls ts) = ppr cls <+> sep (map pprParendIfaceType ts)
279 instance Outputable IfaceTyCon where
280 ppr (IfaceTc ext) = ppr ext
281 ppr other_tc = ppr (ifaceTyConName other_tc)
284 pprIfaceContext :: IfaceContext -> SDoc
285 -- Prints "(C a, D b) =>", including the arrow
286 pprIfaceContext [] = empty
287 pprIfaceContext theta = parens (sep (punctuate comma (map ppr theta)))
290 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
293 %************************************************************************
295 Conversion from Type to IfaceType
297 %************************************************************************
301 toIfaceTvBndr tyvar = (getOccName tyvar, tyVarKind tyvar)
302 toIfaceIdBndr ext id = (getOccName id, toIfaceType ext (idType id))
303 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
306 | isId var = IfaceIdBndr (toIfaceIdBndr ext var)
307 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
309 ---------------------
310 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
311 toIfaceType ext (TyVarTy tv) = IfaceTyVar (getOccName tv)
312 toIfaceType ext (AppTy t1 t2) = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
313 toIfaceType ext (FunTy t1 t2) = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
314 toIfaceType ext (TyConApp tc tys) = IfaceTyConApp (mkIfaceTc ext tc) (toIfaceTypes ext tys)
315 toIfaceType ext (ForAllTy tv t) = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
316 toIfaceType ext (PredTy st) = IfacePredTy (toIfacePred ext st)
317 toIfaceType ext (NoteTy (SynNote tc_app) ty) = toIfaceType ext tc_app
318 toIfaceType ext (NoteTy other_note ty) = toIfaceType ext ty
321 mkIfaceTc :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
323 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
324 | nm == intTyConName = IfaceIntTc
325 | nm == boolTyConName = IfaceBoolTc
326 | nm == charTyConName = IfaceCharTc
327 | nm == listTyConName = IfaceListTc
328 | nm == parrTyConName = IfacePArrTc
329 | otherwise = IfaceTc (ext nm)
334 toIfaceTypes ext ts = map (toIfaceType ext) ts
337 toIfacePred ext (ClassP cls ts) = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
338 toIfacePred ext (IParam ip t) = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
341 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
342 toIfaceContext ext cs = map (toIfacePred ext) cs