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, isLocalIfaceExtName,
13 ifaceTyConName, interactiveExtNameFun,
15 -- Conversion from Type -> IfaceType
16 toIfaceType, toIfacePred, toIfaceContext,
17 toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs,
20 pprIfaceType, pprParendIfaceType, pprIfaceContext,
21 pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
22 tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
26 #include "HsVersions.h"
28 import Kind ( Kind(..) )
29 import TypeRep ( Type(..), TyNote(..), PredType(..), ThetaType )
30 import TyCon ( TyCon, isTupleTyCon, tyConArity, tupleTyConBoxity )
31 import Var ( isId, tyVarKind, idType )
32 import TysWiredIn ( listTyConName, parrTyConName, tupleTyCon, intTyConName, charTyConName, boolTyConName )
33 import OccName ( OccName )
34 import Name ( Name, getName, getOccName, nameModule, nameOccName )
35 import Module ( Module )
36 import BasicTypes ( IPName(..), Arity, Version, mapIPName, tupleParens, Boxity )
42 %************************************************************************
46 %************************************************************************
50 = ExtPkg Module OccName -- From an external package; no version #
51 -- Also used for wired-in things regardless
52 -- of whether they are home-pkg or not
54 | HomePkg Module OccName Version -- From another module in home package;
55 -- has version #; in all other respects,
56 -- HomePkg and ExtPkg are the same
58 | LocalTop OccName -- Top-level from the same module as
59 -- the enclosing IfaceDecl
61 | LocalTopSub -- Same as LocalTop, but for a class method or constr
62 OccName -- Class-meth/constr name
63 OccName -- Parent class/datatype name
64 -- LocalTopSub is written into iface files as LocalTop; the parent
65 -- info is only used when computing version information in MkIface
67 isLocalIfaceExtName :: IfaceExtName -> Bool
68 isLocalIfaceExtName (LocalTop _) = True
69 isLocalIfaceExtName (LocalTopSub _ _) = True
70 isLocalIfaceExtName other = False
72 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
73 -- Local helper for wired-in names
75 interactiveExtNameFun :: PrintUnqualified -> Name-> IfaceExtName
76 interactiveExtNameFun print_unqual name
77 | print_unqual mod occ = LocalTop occ
78 | otherwise = ExtPkg mod occ
81 occ = nameOccName name
85 %************************************************************************
87 Local (nested) binders
89 %************************************************************************
92 data IfaceBndr -- Local (non-top-level) binders
93 = IfaceIdBndr IfaceIdBndr
94 | IfaceTvBndr IfaceTvBndr
96 type IfaceIdBndr = (OccName, IfaceType) -- OccName, because always local
97 type IfaceTvBndr = (OccName, IfaceKind)
99 -------------------------------
100 type IfaceKind = Kind -- Re-use the Kind type, but no KindVars in it
103 = IfaceTyVar OccName -- Type variable only, not tycon
104 | IfaceAppTy IfaceType IfaceType
105 | IfaceForAllTy IfaceTvBndr IfaceType
106 | IfacePredTy IfacePredType
107 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
108 -- Includes newtypes, synonyms, tuples
109 | IfaceFunTy IfaceType IfaceType
111 data IfacePredType -- NewTypes are handled as ordinary TyConApps
112 = IfaceClassP IfaceExtName [IfaceType]
113 | IfaceIParam (IPName OccName) IfaceType
115 type IfaceContext = [IfacePredType]
117 data IfaceTyCon -- Abbreviations for common tycons with known names
118 = IfaceTc IfaceExtName -- The common case
119 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
120 | IfaceListTc | IfacePArrTc
121 | IfaceTupTc Boxity Arity
123 ifaceTyConName :: IfaceTyCon -> Name -- Works for all except IfaceTc
124 ifaceTyConName IfaceIntTc = intTyConName
125 ifaceTyConName IfaceBoolTc = boolTyConName
126 ifaceTyConName IfaceCharTc = charTyConName
127 ifaceTyConName IfaceListTc = listTyConName
128 ifaceTyConName IfacePArrTc = parrTyConName
129 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
130 ifaceTyConName (IfaceTc ext) = pprPanic "ifaceTyConName" (ppr ext)
134 %************************************************************************
136 Functions over IFaceTypes
138 %************************************************************************
142 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
143 -- Mainly for printing purposes
147 (tvs, rho) = split_foralls ty
148 (theta, tau) = split_rho rho
150 split_foralls (IfaceForAllTy tv ty)
151 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
152 split_foralls rho = ([], rho)
154 split_rho (IfaceFunTy (IfacePredTy st) ty)
155 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
156 split_rho tau = ([], tau)
159 %************************************************************************
163 %************************************************************************
167 @ppr_ty@ takes an @Int@ that is the precedence of the context.
168 The precedence levels are:
170 \item[tOP_PREC] No parens required.
171 \item[fUN_PREC] Left hand argument of a function arrow.
172 \item[tYCON_PREC] Argument of a type constructor.
176 tOP_PREC = (0 :: Int) -- type in ParseIface.y
177 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
178 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
180 noParens :: SDoc -> SDoc
183 maybeParen ctxt_prec inner_prec pretty
184 | ctxt_prec < inner_prec = pretty
185 | otherwise = parens pretty
189 ----------------------------- Printing binders ------------------------------------
192 -- These instances are used only when printing for the user, either when
193 -- debugging, or in GHCi when printing the results of a :info command
194 instance Outputable IfaceExtName where
195 ppr (ExtPkg mod occ) = pprExt mod occ
196 ppr (HomePkg mod occ vers) = pprExt mod occ <> braces (ppr vers)
197 ppr (LocalTop occ) = ppr occ -- Do we want to distinguish these
198 ppr (LocalTopSub occ _) = ppr occ -- from an ordinary occurrence?
200 pprExt :: Module -> OccName -> SDoc
201 -- No need to worry about printing unqualified becuase that was handled
202 -- in the transiation to IfaceSyn
203 pprExt mod occ = ppr mod <> dot <> ppr occ
205 instance Outputable IfaceBndr where
206 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
207 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
209 pprIfaceBndrs :: [IfaceBndr] -> SDoc
210 pprIfaceBndrs bs = sep (map ppr bs)
212 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
214 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
215 pprIfaceTvBndr (tv, LiftedTypeKind) = ppr tv
216 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
218 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
219 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
222 ----------------------------- Printing IfaceType ------------------------------------
225 ---------------------------------
226 instance Outputable IfaceType where
227 ppr ty = pprIfaceTypeForUser ty
229 pprIfaceTypeForUser ::IfaceType -> SDoc
230 -- Drop top-level for-alls; if that's not what you want, use pprIfaceType dire
231 pprIfaceTypeForUser ty
232 = pprIfaceForAllPart [] theta (pprIfaceType tau)
234 (_tvs, theta, tau) = splitIfaceSigmaTy ty
236 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
237 pprIfaceType = ppr_ty tOP_PREC
238 pprParendIfaceType = ppr_ty tYCON_PREC
241 ppr_ty :: Int -> IfaceType -> SDoc
242 ppr_ty ctxt_prec (IfaceTyVar tyvar) = ppr tyvar
243 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
244 ppr_ty ctxt_prec (IfacePredTy st) = ppr st
247 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
248 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
249 maybeParen ctxt_prec fUN_PREC $
250 sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
252 ppr_fun_tail (IfaceFunTy ty1 ty2)
253 = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
254 ppr_fun_tail other_ty
255 = [arrow <+> pprIfaceType other_ty]
257 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
258 = maybeParen ctxt_prec tYCON_PREC $
259 ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
261 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
262 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
264 (tvs, theta, tau) = splitIfaceSigmaTy ty
267 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
268 pprIfaceForAllPart tvs ctxt doc
269 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
271 ppr_tvs | null tvs = empty
272 | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
275 ppr_tc_app ctxt_prec tc [] = ppr tc
276 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets (pprIfaceType ty)
277 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
278 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
279 | arity == length tys
280 = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
281 ppr_tc_app ctxt_prec tc tys
282 = maybeParen ctxt_prec tYCON_PREC
283 (sep [ppr tc, nest 4 (sep (map pprParendIfaceType tys))])
286 instance Outputable IfacePredType where
287 -- Print without parens
288 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
289 ppr (IfaceClassP cls ts) = ppr cls <+> sep (map pprParendIfaceType ts)
291 instance Outputable IfaceTyCon where
292 ppr (IfaceTc ext) = ppr ext
293 ppr other_tc = ppr (ifaceTyConName other_tc)
296 pprIfaceContext :: IfaceContext -> SDoc
297 -- Prints "(C a, D b) =>", including the arrow
298 pprIfaceContext [] = empty
299 pprIfaceContext theta = parens (sep (punctuate comma (map ppr theta)))
302 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
305 %************************************************************************
307 Conversion from Type to IfaceType
309 %************************************************************************
313 toIfaceTvBndr tyvar = (getOccName tyvar, tyVarKind tyvar)
314 toIfaceIdBndr ext id = (getOccName id, toIfaceType ext (idType id))
315 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
318 | isId var = IfaceIdBndr (toIfaceIdBndr ext var)
319 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
321 ---------------------
322 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
323 toIfaceType ext (TyVarTy tv) = IfaceTyVar (getOccName tv)
324 toIfaceType ext (AppTy t1 t2) = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
325 toIfaceType ext (FunTy t1 t2) = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
326 toIfaceType ext (TyConApp tc tys) = IfaceTyConApp (mkIfaceTc ext tc) (toIfaceTypes ext tys)
327 toIfaceType ext (ForAllTy tv t) = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
328 toIfaceType ext (PredTy st) = IfacePredTy (toIfacePred ext st)
329 toIfaceType ext (NoteTy (SynNote tc_app) ty) = toIfaceType ext tc_app
330 toIfaceType ext (NoteTy other_note ty) = toIfaceType ext ty
333 mkIfaceTc :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
335 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
336 | nm == intTyConName = IfaceIntTc
337 | nm == boolTyConName = IfaceBoolTc
338 | nm == charTyConName = IfaceCharTc
339 | nm == listTyConName = IfaceListTc
340 | nm == parrTyConName = IfacePArrTc
341 | otherwise = IfaceTc (ext nm)
346 toIfaceTypes ext ts = map (toIfaceType ext) ts
349 toIfacePred ext (ClassP cls ts) = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
350 toIfacePred ext (IParam ip t) = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
353 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
354 toIfaceContext ext cs = map (toIfacePred ext) cs