2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
5 This module defines intereace types and binders
9 IfaceType(..), IfaceKind(..), IfacePredType(..), IfaceTyCon(..),
10 IfaceContext, IfaceBndr(..), IfaceTvBndr, IfaceIdBndr,
12 IfaceExtName(..), mkIfaceExtName, ifaceTyConName,
14 -- Conversion from Type -> IfaceType
15 toIfaceType, toIfaceKind, toIfacePred, toIfaceContext,
16 toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs,
19 pprIfaceKind, pprParendIfaceKind,
20 pprIfaceType, pprParendIfaceType, pprIfaceContext,
21 pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
23 tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
27 #include "HsVersions.h"
29 import Type ( openTypeKind, liftedTypeKind, unliftedTypeKind,
30 splitFunTy_maybe, eqKind, pprType )
31 import TypeRep ( Type(..), TyNote(..), PredType(..), Kind, ThetaType )
32 import TyCon ( TyCon, isTupleTyCon, tyConArity, tupleTyConBoxity )
33 import Var ( isId, tyVarKind, idType )
34 import TysWiredIn ( listTyConName, parrTyConName, tupleTyCon, intTyConName, charTyConName, boolTyConName )
35 import OccName ( OccName )
36 import Name ( Name, getName, getOccName, nameModuleName, nameOccName, isInternalName )
37 import Module ( ModuleName )
38 import BasicTypes ( IPName(..), Arity, Version, mapIPName, tupleParens, Boxity )
44 %************************************************************************
48 %************************************************************************
52 = ExtPkg ModuleName OccName -- From an external package; no version #
53 -- Also used for wired-in things regardless
54 -- of whether they are home-pkg or not
56 | HomePkg ModuleName OccName Version -- From another module in home package;
59 | LocalTop OccName -- Top-level from the same module as
60 -- the enclosing IfaceDecl
62 | LocalTopSub -- Same as LocalTop, but for a class method or constr
63 OccName -- Class-meth/constr name
64 OccName -- Parent class/datatype name
65 -- LocalTopSub is written into iface files as LocalTop; the parent
66 -- info is only used when computing version information in MkIface
68 mkIfaceExtName name = ExtPkg (nameModuleName name) (nameOccName name)
69 -- Local helper for wired-in names
73 %************************************************************************
75 Local (nested) binders
77 %************************************************************************
80 data IfaceBndr -- Local (non-top-level) binders
81 = IfaceIdBndr IfaceIdBndr
82 | IfaceTvBndr IfaceTvBndr
84 type IfaceIdBndr = (OccName, IfaceType) -- OccName, because always local
85 type IfaceTvBndr = (OccName, IfaceKind)
87 -------------------------------
91 | IfaceUnliftedTypeKind
92 | IfaceFunKind IfaceKind IfaceKind
95 -------------------------------
97 = IfaceTyVar OccName -- Type variable only, not tycon
98 | IfaceAppTy IfaceType IfaceType
99 | IfaceForAllTy IfaceTvBndr IfaceType
100 | IfacePredTy IfacePredType
101 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
102 -- Includes newtypes, synonyms, tuples
103 | IfaceFunTy IfaceType IfaceType
105 data IfacePredType -- NewTypes are handled as ordinary TyConApps
106 = IfaceClassP IfaceExtName [IfaceType]
107 | IfaceIParam (IPName OccName) IfaceType
109 type IfaceContext = [IfacePredType]
111 data IfaceTyCon -- Abbreviations for common tycons with known names
112 = IfaceTc IfaceExtName -- The common case
113 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
114 | IfaceListTc | IfacePArrTc
115 | IfaceTupTc Boxity Arity
117 ifaceTyConName :: IfaceTyCon -> Name -- Works for all except IfaceTc
118 ifaceTyConName IfaceIntTc = intTyConName
119 ifaceTyConName IfaceBoolTc = boolTyConName
120 ifaceTyConName IfaceCharTc = charTyConName
121 ifaceTyConName IfaceListTc = listTyConName
122 ifaceTyConName IfacePArrTc = parrTyConName
123 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
124 ifaceTyConName (IfaceTc ext) = pprPanic "ifaceTyConName" (ppr ext)
128 %************************************************************************
130 Functions over IFaceTypes
132 %************************************************************************
136 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
137 -- Mainly for printing purposes
141 (tvs, rho) = split_foralls ty
142 (theta, tau) = split_rho rho
144 split_foralls (IfaceForAllTy tv ty)
145 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
146 split_foralls rho = ([], rho)
148 split_rho (IfaceFunTy (IfacePredTy st) ty)
149 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
150 split_rho tau = ([], tau)
153 %************************************************************************
157 %************************************************************************
161 @ppr_ty@ takes an @Int@ that is the precedence of the context.
162 The precedence levels are:
164 \item[tOP_PREC] No parens required.
165 \item[fUN_PREC] Left hand argument of a function arrow.
166 \item[tYCON_PREC] Argument of a type constructor.
170 tOP_PREC = (0 :: Int) -- type in ParseIface.y
171 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
172 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
174 noParens :: SDoc -> SDoc
177 maybeParen ctxt_prec inner_prec pretty
178 | ctxt_prec < inner_prec = pretty
179 | otherwise = parens pretty
183 ----------------------------- Printing binders ------------------------------------
186 instance Outputable IfaceExtName where
187 ppr (ExtPkg mod occ) = ppr mod <> dot <> ppr occ
188 ppr (HomePkg mod occ vers) = ppr mod <> dot <> ppr occ <> braces (ppr vers)
189 ppr (LocalTop occ) = ppr occ -- Do we want to distinguish these
190 ppr (LocalTopSub occ _) = ppr occ -- from an ordinary occurrence?
192 getIfaceExt :: ((Name -> IfaceExtName) -> SDoc) -> SDoc
193 -- Uses the print-unqual info from the SDoc to make an 'ext'
194 -- which in turn tells toIfaceType when to make a qualified name
195 -- This is only used when making Iface stuff to print out for the user;
196 -- e.g. we use this in pprType
197 getIfaceExt thing_inside
198 = getPprStyle $ \ sty ->
200 ext nm | unqualStyle sty nm = LocalTop (nameOccName nm)
201 | isInternalName nm = LocalTop (nameOccName nm)
202 -- This only happens for Kind constructors, which
203 -- don't come from any particular module and are unqualified
204 -- This hack will go away when kinds are separated from types
205 | otherwise = ExtPkg (nameModuleName nm) (nameOccName nm)
209 instance Outputable IfaceBndr where
210 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
211 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
213 pprIfaceBndrs :: [IfaceBndr] -> SDoc
214 pprIfaceBndrs bs = sep (map ppr bs)
216 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
218 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
219 pprIfaceTvBndr (tv, IfaceLiftedTypeKind) = ppr tv
220 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
222 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
223 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
226 ----------------------------- Printing IfaceType ------------------------------------
229 ---------------------------------
230 instance Outputable IfaceKind where
231 ppr k = pprIfaceKind tOP_PREC k
233 pprParendIfaceKind :: IfaceKind -> SDoc
234 pprParendIfaceKind k = pprIfaceKind tYCON_PREC k
236 pprIfaceKind prec IfaceLiftedTypeKind = ptext SLIT("*")
237 pprIfaceKind prec IfaceUnliftedTypeKind = ptext SLIT("#")
238 pprIfaceKind prec IfaceOpenTypeKind = ptext SLIT("?")
239 pprIfaceKind prec (IfaceFunKind k1 k2) = maybeParen prec fUN_PREC $
240 sep [ pprIfaceKind fUN_PREC k1, arrow <+> ppr k2]
242 ---------------------------------
243 instance Outputable IfaceType where
246 ppr_ty = pprIfaceType tOP_PREC
247 pprParendIfaceType = pprIfaceType tYCON_PREC
249 pprIfaceType :: Int -> IfaceType -> SDoc
253 pprIfaceType ctxt_prec (IfaceTyVar tyvar) = ppr tyvar
254 pprIfaceType ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
255 pprIfaceType ctxt_prec (IfacePredTy st) = braces (ppr st)
258 pprIfaceType ctxt_prec (IfaceFunTy ty1 ty2)
259 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
260 maybeParen ctxt_prec fUN_PREC $
261 sep (pprIfaceType fUN_PREC ty1 : ppr_fun_tail ty2)
263 ppr_fun_tail (IfaceFunTy ty1 ty2)
264 = (arrow <+> pprIfaceType fUN_PREC ty1) : ppr_fun_tail ty2
265 ppr_fun_tail other_ty
266 = [arrow <+> ppr_ty other_ty]
268 pprIfaceType ctxt_prec (IfaceAppTy ty1 ty2)
269 = maybeParen ctxt_prec tYCON_PREC $
270 pprIfaceType fUN_PREC ty1 <+> pprParendIfaceType ty2
272 pprIfaceType ctxt_prec ty@(IfaceForAllTy _ _)
273 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (ppr_ty tau))
275 (tvs, theta, tau) = splitIfaceSigmaTy ty
278 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
279 pprIfaceForAllPart tvs ctxt doc
280 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
282 ppr_tvs | null tvs = empty
283 | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
286 ppr_tc_app ctxt_prec tc [] = ppr tc
287 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets (ppr_ty ty)
288 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (ppr_ty ty)
289 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
290 | arity == length tys
291 = tupleParens bx (sep (punctuate comma (map ppr_ty tys)))
292 ppr_tc_app ctxt_prec tc tys
293 = maybeParen ctxt_prec tYCON_PREC
294 (sep [ppr tc, nest 4 (sep (map pprParendIfaceType tys))])
297 instance Outputable IfacePredType where
298 -- Print without parens
299 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
300 ppr (IfaceClassP cls ts) = ppr cls <+> sep (map pprParendIfaceType ts)
302 instance Outputable IfaceTyCon where
303 ppr (IfaceTc ext) = ppr ext
304 ppr other_tc = ppr (ifaceTyConName other_tc)
307 pprIfaceContext :: IfaceContext -> SDoc
308 -- Prints "(C a, D b) =>", including the arrow
309 pprIfaceContext [] = empty
310 pprIfaceContext theta = parens (sep (punctuate comma (map ppr theta)))
313 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
316 %************************************************************************
318 Conversion from Type to IfaceType
320 %************************************************************************
324 toIfaceTvBndr tyvar = (getOccName tyvar, toIfaceKind (tyVarKind tyvar))
325 toIfaceIdBndr ext id = (getOccName id, toIfaceType ext (idType id))
326 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
329 | isId var = IfaceIdBndr (toIfaceIdBndr ext var)
330 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
332 ---------------------
333 toIfaceKind :: Kind -> IfaceKind
335 | k `eqKind` openTypeKind = IfaceOpenTypeKind
336 | k `eqKind` liftedTypeKind = IfaceLiftedTypeKind
337 | k `eqKind` unliftedTypeKind = IfaceUnliftedTypeKind
338 | Just (arg,res) <- splitFunTy_maybe k
339 = IfaceFunKind (toIfaceKind arg) (toIfaceKind res)
341 | otherwise = pprTrace "toIfaceKind" (pprType k) IfaceOpenTypeKind
344 ---------------------
345 toIfaceType :: (Name -> IfaceExtName) -> Type -> IfaceType
346 toIfaceType ext (TyVarTy tv) = IfaceTyVar (getOccName tv)
347 toIfaceType ext (AppTy t1 t2) = IfaceAppTy (toIfaceType ext t1) (toIfaceType ext t2)
348 toIfaceType ext (FunTy t1 t2) = IfaceFunTy (toIfaceType ext t1) (toIfaceType ext t2)
349 toIfaceType ext (NewTcApp tc tys) = IfaceTyConApp (mkIfaceTc ext tc) (toIfaceTypes ext tys)
350 toIfaceType ext (TyConApp tc tys) = IfaceTyConApp (mkIfaceTc ext tc) (toIfaceTypes ext tys)
351 toIfaceType ext (ForAllTy tv t) = IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType ext t)
352 toIfaceType ext (PredTy st) = IfacePredTy (toIfacePred ext st)
353 toIfaceType ext (NoteTy (SynNote tc_app) ty) = toIfaceType ext tc_app
354 toIfaceType ext (NoteTy other_note ty) = toIfaceType ext ty
357 mkIfaceTc :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
359 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
360 | nm == intTyConName = IfaceIntTc
361 | nm == boolTyConName = IfaceBoolTc
362 | nm == charTyConName = IfaceCharTc
363 | nm == listTyConName = IfaceListTc
364 | nm == parrTyConName = IfacePArrTc
365 | otherwise = IfaceTc (ext nm)
370 toIfaceTypes ext ts = map (toIfaceType ext) ts
373 toIfacePred ext (ClassP cls ts) = IfaceClassP (ext (getName cls)) (toIfaceTypes ext ts)
374 toIfacePred ext (IParam ip t) = IfaceIParam (mapIPName getOccName ip) (toIfaceType ext t)
377 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
378 toIfaceContext ext cs = map (toIfacePred ext) cs