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, IfaceCoercion,
12 IfaceExtName(..), mkIfaceExtName, isLocalIfaceExtName,
15 -- Conversion from Type -> IfaceType
16 toIfaceType, toIfacePred, toIfaceContext,
17 toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs,
18 toIfaceTyCon, toIfaceTyCon_name,
21 pprIfaceType, pprParendIfaceType, pprIfaceContext,
22 pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
23 tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
27 #include "HsVersions.h"
30 import Coercion ( Coercion )
31 import TypeRep ( TyThing(..), Type(..), PredType(..), ThetaType,
32 unliftedTypeKindTyConName, openTypeKindTyConName,
33 ubxTupleKindTyConName, argTypeKindTyConName,
34 liftedTypeKindTyConName, isLiftedTypeKind )
35 import TyCon ( TyCon, isTupleTyCon, tyConArity, tupleTyConBoxity, tyConName )
36 import Var ( isId, tyVarKind, idType )
37 import TysWiredIn ( listTyConName, parrTyConName, tupleTyCon, intTyConName, charTyConName, boolTyConName )
38 import OccName ( OccName, parenSymOcc, occNameFS )
39 import Name ( Name, getName, getOccName, nameModule, nameOccName,
40 wiredInNameTyThing_maybe )
41 import Module ( Module, ModuleName )
42 import BasicTypes ( IPName(..), Arity, Version, mapIPName, tupleParens, Boxity )
48 %************************************************************************
52 %************************************************************************
56 = ExtPkg Module OccName
57 -- From an external package; no version # Also used for
58 -- wired-in things regardless of whether they are home-pkg or
61 | HomePkg ModuleName OccName Version
62 -- From another module in home package; has version #; in all
63 -- other respects, HomePkg and ExtPkg are the same. Since this
64 -- is a home package name, we use ModuleName rather than Module
66 | LocalTop OccName -- Top-level from the same module as
67 -- the enclosing IfaceDecl
69 | LocalTopSub -- Same as LocalTop, but for a class method or constr
70 OccName -- Class-meth/constr name
71 OccName -- Parent class/datatype name
72 -- LocalTopSub is written into iface files as LocalTop; the parent
73 -- info is only used when computing version information in MkIface
75 isLocalIfaceExtName :: IfaceExtName -> Bool
76 isLocalIfaceExtName (LocalTop _) = True
77 isLocalIfaceExtName (LocalTopSub _ _) = True
78 isLocalIfaceExtName other = False
80 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
81 -- Local helper for wired-in names
83 ifaceExtOcc :: IfaceExtName -> OccName
84 ifaceExtOcc (ExtPkg _ occ) = occ
85 ifaceExtOcc (HomePkg _ occ _) = occ
86 ifaceExtOcc (LocalTop occ) = occ
87 ifaceExtOcc (LocalTopSub occ _) = occ
91 %************************************************************************
93 Local (nested) binders
95 %************************************************************************
98 data IfaceBndr -- Local (non-top-level) binders
99 = IfaceIdBndr {-# UNPACK #-} !IfaceIdBndr
100 | IfaceTvBndr {-# UNPACK #-} !IfaceTvBndr
102 type IfaceIdBndr = (FastString, IfaceType)
103 type IfaceTvBndr = (FastString, IfaceKind)
105 -------------------------------
106 type IfaceKind = IfaceType -- Re-use the Kind type, but no KindVars in it
108 type IfaceCoercion = IfaceType
111 = IfaceTyVar FastString -- Type variable only, not tycon
112 | IfaceAppTy IfaceType IfaceType
113 | IfaceForAllTy IfaceTvBndr IfaceType
114 | IfacePredTy IfacePredType
115 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
116 -- Includes newtypes, synonyms, tuples
117 | IfaceFunTy IfaceType IfaceType
119 data IfacePredType -- NewTypes are handled as ordinary TyConApps
120 = IfaceClassP IfaceExtName [IfaceType]
121 | IfaceIParam (IPName OccName) IfaceType
122 | IfaceEqPred IfaceType IfaceType
124 type IfaceContext = [IfacePredType]
126 data IfaceTyCon -- Abbreviations for common tycons with known names
127 = IfaceTc IfaceExtName -- The common case
128 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
129 | IfaceListTc | IfacePArrTc
130 | IfaceTupTc Boxity Arity
131 | IfaceLiftedTypeKindTc | IfaceOpenTypeKindTc | IfaceUnliftedTypeKindTc
132 | IfaceUbxTupleKindTc | IfaceArgTypeKindTc
134 ifaceTyConName :: IfaceTyCon -> Name -- Works for all except IfaceTc
135 ifaceTyConName IfaceIntTc = intTyConName
136 ifaceTyConName IfaceBoolTc = boolTyConName
137 ifaceTyConName IfaceCharTc = charTyConName
138 ifaceTyConName IfaceListTc = listTyConName
139 ifaceTyConName IfacePArrTc = parrTyConName
140 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
141 ifaceTyConName IfaceLiftedTypeKindTc = liftedTypeKindTyConName
142 ifaceTyConName IfaceOpenTypeKindTc = openTypeKindTyConName
143 ifaceTyConName IfaceUnliftedTypeKindTc = unliftedTypeKindTyConName
144 ifaceTyConName IfaceUbxTupleKindTc = ubxTupleKindTyConName
145 ifaceTyConName IfaceArgTypeKindTc = argTypeKindTyConName
146 ifaceTyConName (IfaceTc ext) = pprPanic "ifaceTyConName" (ppr ext)
152 %************************************************************************
154 Functions over IFaceTypes
156 %************************************************************************
160 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
161 -- Mainly for printing purposes
165 (tvs, rho) = split_foralls ty
166 (theta, tau) = split_rho rho
168 split_foralls (IfaceForAllTy tv ty)
169 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
170 split_foralls rho = ([], rho)
172 split_rho (IfaceFunTy (IfacePredTy st) ty)
173 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
174 split_rho tau = ([], tau)
177 %************************************************************************
181 %************************************************************************
185 @ppr_ty@ takes an @Int@ that is the precedence of the context.
186 The precedence levels are:
188 \item[tOP_PREC] No parens required.
189 \item[fUN_PREC] Left hand argument of a function arrow.
190 \item[tYCON_PREC] Argument of a type constructor.
194 tOP_PREC = (0 :: Int) -- type in ParseIface.y
195 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
196 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
198 noParens :: SDoc -> SDoc
201 maybeParen ctxt_prec inner_prec pretty
202 | ctxt_prec < inner_prec = pretty
203 | otherwise = parens pretty
207 ----------------------------- Printing binders ------------------------------------
210 -- These instances are used only when printing for the user, either when
211 -- debugging, or in GHCi when printing the results of a :info command
212 instance Outputable IfaceExtName where
213 ppr (ExtPkg mod occ) = ppr mod <> dot <> ppr occ
214 ppr (HomePkg mod occ vers) = ppr mod <> dot <> ppr occ <> braces (ppr vers)
215 ppr (LocalTop occ) = ppr occ -- Do we want to distinguish these
216 ppr (LocalTopSub occ _) = ppr occ -- from an ordinary occurrence?
217 -- No need to worry about printing unqualified becuase that was handled
218 -- in the transiation to IfaceSyn
220 instance Outputable IfaceBndr where
221 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
222 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
224 pprIfaceBndrs :: [IfaceBndr] -> SDoc
225 pprIfaceBndrs bs = sep (map ppr bs)
227 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
229 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
230 pprIfaceTvBndr (tv, IfaceTyConApp IfaceLiftedTypeKindTc [])
232 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
233 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
234 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
237 ----------------------------- Printing IfaceType ------------------------------------
240 ---------------------------------
241 instance Outputable IfaceType where
242 ppr ty = pprIfaceTypeForUser ty
244 pprIfaceTypeForUser ::IfaceType -> SDoc
245 -- Drop top-level for-alls; if that's not what you want, use pprIfaceType dire
246 pprIfaceTypeForUser ty
247 = pprIfaceForAllPart [] theta (pprIfaceType tau)
249 (_tvs, theta, tau) = splitIfaceSigmaTy ty
251 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
252 pprIfaceType = ppr_ty tOP_PREC
253 pprParendIfaceType = ppr_ty tYCON_PREC
256 ppr_ty :: Int -> IfaceType -> SDoc
257 ppr_ty ctxt_prec (IfaceTyVar tyvar) = ppr tyvar
258 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
259 ppr_ty ctxt_prec (IfacePredTy st) = ppr st
262 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
263 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
264 maybeParen ctxt_prec fUN_PREC $
265 sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
267 ppr_fun_tail (IfaceFunTy ty1 ty2)
268 = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
269 ppr_fun_tail other_ty
270 = [arrow <+> pprIfaceType other_ty]
272 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
273 = maybeParen ctxt_prec tYCON_PREC $
274 ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
276 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
277 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
279 (tvs, theta, tau) = splitIfaceSigmaTy ty
282 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
283 pprIfaceForAllPart tvs ctxt doc
284 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
286 ppr_tvs | null tvs = empty
287 | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
290 ppr_tc_app ctxt_prec tc [] = ppr_tc tc
291 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets (pprIfaceType ty)
292 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
293 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
294 | arity == length tys
295 = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
296 ppr_tc_app ctxt_prec tc tys
297 = maybeParen ctxt_prec tYCON_PREC
298 (sep [ppr_tc tc, nest 4 (sep (map pprParendIfaceType tys))])
300 ppr_tc :: IfaceTyCon -> SDoc
301 -- Wrap infix type constructors in parens
302 ppr_tc tc@(IfaceTc ext_nm) = parenSymOcc (ifaceExtOcc ext_nm) (ppr tc)
306 instance Outputable IfacePredType where
307 -- Print without parens
308 ppr (IfaceEqPred ty1 ty2)= hsep [ppr ty1, ptext SLIT(":=:"), ppr ty2]
309 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
310 ppr (IfaceClassP cls ts) = parenSymOcc (ifaceExtOcc cls) (ppr cls)
311 <+> sep (map pprParendIfaceType ts)
313 instance Outputable IfaceTyCon where
314 ppr (IfaceTc ext) = ppr ext
315 ppr other_tc = ppr (ifaceTyConName other_tc)
318 pprIfaceContext :: IfaceContext -> SDoc
319 -- Prints "(C a, D b) =>", including the arrow
320 pprIfaceContext [] = empty
321 pprIfaceContext theta = ppr_preds theta <+> ptext SLIT("=>")
323 ppr_preds [pred] = ppr pred -- No parens
324 ppr_preds preds = parens (sep (punctuate comma (map ppr preds)))
327 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
330 %************************************************************************
332 Conversion from Type to IfaceType
334 %************************************************************************
338 toIfaceTvBndr tyvar = (occNameFS (getOccName tyvar), toIfaceKind (tyVarKind tyvar))
339 toIfaceIdBndr ext id = (occNameFS (getOccName id), toIfaceType ext (idType id))
340 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
343 | isId var = IfaceIdBndr (toIfaceIdBndr ext var)
344 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
346 -- we had better not have to use ext for kinds
347 toIfaceKind = toIfaceType (\name -> pprPanic "toIfaceKind ext used on:" (ppr name))
349 ---------------------
350 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
351 -- Synonyms are retained in the interface type
352 toIfaceType ext (TyVarTy tv) = IfaceTyVar (occNameFS (getOccName tv))
353 toIfaceType ext (AppTy t1 t2) = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
354 toIfaceType ext (FunTy t1 t2) = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
355 toIfaceType ext (TyConApp tc tys) = IfaceTyConApp (toIfaceTyCon ext tc) (toIfaceTypes ext tys)
356 toIfaceType ext (ForAllTy tv t) = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
357 toIfaceType ext (PredTy st) = IfacePredTy (toIfacePred ext st)
358 toIfaceType ext (NoteTy other_note ty) = toIfaceType ext ty
361 -- A little bit of (perhaps optional) trickiness here. When
362 -- compiling Data.Tuple, the tycons are not TupleTyCons, although
363 -- they have a wired-in name. But we'd like to dump them into the Iface
364 -- as a tuple tycon, to save lookups when reading the interface
365 -- Hence a tuple tycon may 'miss' in toIfaceTyCon, but then
366 -- toIfaceTyCon_name will still catch it.
368 toIfaceTyCon :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
370 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
371 | otherwise = toIfaceTyCon_name ext (tyConName tc)
373 toIfaceTyCon_name :: (Name -> IfaceExtName) -> Name -> IfaceTyCon
374 toIfaceTyCon_name ext nm
375 | Just (ATyCon tc) <- wiredInNameTyThing_maybe nm
376 = toIfaceWiredInTyCon ext tc nm
380 toIfaceWiredInTyCon :: (Name -> IfaceExtName) -> TyCon -> Name -> IfaceTyCon
381 toIfaceWiredInTyCon ext tc nm
382 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
383 | nm == intTyConName = IfaceIntTc
384 | nm == boolTyConName = IfaceBoolTc
385 | nm == charTyConName = IfaceCharTc
386 | nm == listTyConName = IfaceListTc
387 | nm == parrTyConName = IfacePArrTc
388 | nm == liftedTypeKindTyConName = IfaceLiftedTypeKindTc
389 | nm == unliftedTypeKindTyConName = IfaceUnliftedTypeKindTc
390 | nm == openTypeKindTyConName = IfaceOpenTypeKindTc
391 | nm == argTypeKindTyConName = IfaceArgTypeKindTc
392 | nm == ubxTupleKindTyConName = IfaceUbxTupleKindTc
393 | otherwise = IfaceTc (ext nm)
396 toIfaceTypes ext ts = map (toIfaceType ext) ts
399 toIfacePred ext (ClassP cls ts) = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
400 toIfacePred ext (IParam ip t) = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
401 toIfacePred ext (EqPred ty1 ty2) = IfaceEqPred (toIfaceType ext ty1) (toIfaceType ext ty2)
404 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
405 toIfaceContext ext cs = map (toIfacePred ext) cs