2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
6 This module defines interface types and binders
10 IfaceType(..), IfaceKind, IfacePredType(..), IfaceTyCon(..),
11 IfaceContext, IfaceBndr(..), IfaceTvBndr, IfaceIdBndr, IfaceCoercion,
14 -- Conversion from Type -> IfaceType
15 toIfaceType, toIfacePred, toIfaceContext,
16 toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs,
17 toIfaceTyCon, toIfaceTyCon_name,
20 pprIfaceType, pprParendIfaceType, pprIfaceContext,
21 pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
22 tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
37 %************************************************************************
39 Local (nested) binders
41 %************************************************************************
44 data IfaceBndr -- Local (non-top-level) binders
45 = IfaceIdBndr {-# UNPACK #-} !IfaceIdBndr
46 | IfaceTvBndr {-# UNPACK #-} !IfaceTvBndr
48 type IfaceIdBndr = (FastString, IfaceType)
49 type IfaceTvBndr = (FastString, IfaceKind)
51 -------------------------------
52 type IfaceKind = IfaceType
53 type IfaceCoercion = IfaceType
56 = IfaceTyVar FastString -- Type variable only, not tycon
57 | IfaceAppTy IfaceType IfaceType
58 | IfaceForAllTy IfaceTvBndr IfaceType
59 | IfacePredTy IfacePredType
60 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
61 -- Includes newtypes, synonyms, tuples
62 | IfaceFunTy IfaceType IfaceType
64 data IfacePredType -- NewTypes are handled as ordinary TyConApps
65 = IfaceClassP Name [IfaceType]
66 | IfaceIParam (IPName OccName) IfaceType
67 | IfaceEqPred IfaceType IfaceType
69 type IfaceContext = [IfacePredType]
71 -- NB: If you add a data constructor, remember to add a case to
73 data IfaceTyCon -- Abbreviations for common tycons with known names
74 = IfaceTc Name -- The common case
75 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
76 | IfaceListTc | IfacePArrTc
77 | IfaceTupTc Boxity Arity
78 | IfaceLiftedTypeKindTc | IfaceOpenTypeKindTc | IfaceUnliftedTypeKindTc
79 | IfaceUbxTupleKindTc | IfaceArgTypeKindTc
82 ifaceTyConName :: IfaceTyCon -> Name
83 ifaceTyConName IfaceIntTc = intTyConName
84 ifaceTyConName IfaceBoolTc = boolTyConName
85 ifaceTyConName IfaceCharTc = charTyConName
86 ifaceTyConName IfaceListTc = listTyConName
87 ifaceTyConName IfacePArrTc = parrTyConName
88 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
89 ifaceTyConName IfaceLiftedTypeKindTc = liftedTypeKindTyConName
90 ifaceTyConName IfaceOpenTypeKindTc = openTypeKindTyConName
91 ifaceTyConName IfaceUnliftedTypeKindTc = unliftedTypeKindTyConName
92 ifaceTyConName IfaceUbxTupleKindTc = ubxTupleKindTyConName
93 ifaceTyConName IfaceArgTypeKindTc = argTypeKindTyConName
94 ifaceTyConName (IfaceTc ext) = ext
98 %************************************************************************
100 Functions over IFaceTypes
102 %************************************************************************
106 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
107 -- Mainly for printing purposes
111 (tvs, rho) = split_foralls ty
112 (theta, tau) = split_rho rho
114 split_foralls (IfaceForAllTy tv ty)
115 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
116 split_foralls rho = ([], rho)
118 split_rho (IfaceFunTy (IfacePredTy st) ty)
119 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
120 split_rho tau = ([], tau)
123 %************************************************************************
127 %************************************************************************
131 @ppr_ty@ takes an @Int@ that is the precedence of the context.
132 The precedence levels are:
134 \item[tOP_PREC] No parens required.
135 \item[fUN_PREC] Left hand argument of a function arrow.
136 \item[tYCON_PREC] Argument of a type constructor.
140 tOP_PREC, fUN_PREC, tYCON_PREC :: Int
141 tOP_PREC = 0 -- type in ParseIface.y
142 fUN_PREC = 1 -- btype in ParseIface.y
143 tYCON_PREC = 2 -- atype in ParseIface.y
145 noParens :: SDoc -> SDoc
148 maybeParen :: Int -> Int -> SDoc -> SDoc
149 maybeParen ctxt_prec inner_prec pretty
150 | ctxt_prec < inner_prec = pretty
151 | otherwise = parens pretty
155 ----------------------------- Printing binders ------------------------------------
158 instance Outputable IfaceBndr where
159 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
160 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
162 pprIfaceBndrs :: [IfaceBndr] -> SDoc
163 pprIfaceBndrs bs = sep (map ppr bs)
165 pprIfaceIdBndr :: (FastString, IfaceType) -> SDoc
166 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
168 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
169 pprIfaceTvBndr (tv, IfaceTyConApp IfaceLiftedTypeKindTc [])
171 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
172 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
173 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
176 ----------------------------- Printing IfaceType ------------------------------------
179 ---------------------------------
180 instance Outputable IfaceType where
181 ppr ty = pprIfaceType ty
183 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
184 pprIfaceType = ppr_ty tOP_PREC
185 pprParendIfaceType = ppr_ty tYCON_PREC
188 ppr_ty :: Int -> IfaceType -> SDoc
189 ppr_ty _ (IfaceTyVar tyvar) = ppr tyvar
190 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
191 ppr_ty _ (IfacePredTy st) = ppr st
194 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
195 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
196 maybeParen ctxt_prec fUN_PREC $
197 sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
199 ppr_fun_tail (IfaceFunTy ty1 ty2)
200 = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
201 ppr_fun_tail other_ty
202 = [arrow <+> pprIfaceType other_ty]
204 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
205 = maybeParen ctxt_prec tYCON_PREC $
206 ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
208 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
209 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
211 (tvs, theta, tau) = splitIfaceSigmaTy ty
214 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
215 pprIfaceForAllPart tvs ctxt doc
216 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
218 ppr_tvs | null tvs = empty
219 | otherwise = ptext (sLit "forall") <+> pprIfaceTvBndrs tvs <> dot
222 ppr_tc_app :: Int -> IfaceTyCon -> [IfaceType] -> SDoc
223 ppr_tc_app _ tc [] = ppr_tc tc
224 ppr_tc_app _ IfaceListTc [ty] = brackets (pprIfaceType ty)
225 ppr_tc_app _ IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
226 ppr_tc_app _ (IfaceTupTc bx arity) tys
227 | arity == length tys
228 = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
229 ppr_tc_app ctxt_prec tc tys
230 = maybeParen ctxt_prec tYCON_PREC
231 (sep [ppr_tc tc, nest 4 (sep (map pprParendIfaceType tys))])
233 ppr_tc :: IfaceTyCon -> SDoc
234 -- Wrap infix type constructors in parens
235 ppr_tc tc@(IfaceTc ext_nm) = parenSymOcc (getOccName ext_nm) (ppr tc)
239 instance Outputable IfacePredType where
240 -- Print without parens
241 ppr (IfaceEqPred ty1 ty2)= hsep [ppr ty1, ptext (sLit ":=:"), ppr ty2]
242 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
243 ppr (IfaceClassP cls ts) = parenSymOcc (getOccName cls) (ppr cls)
244 <+> sep (map pprParendIfaceType ts)
246 instance Outputable IfaceTyCon where
247 ppr (IfaceTc ext) = ppr ext
248 ppr other_tc = ppr (ifaceTyConName other_tc)
251 pprIfaceContext :: IfaceContext -> SDoc
252 -- Prints "(C a, D b) =>", including the arrow
253 pprIfaceContext [] = empty
254 pprIfaceContext theta = ppr_preds theta <+> ptext (sLit "=>")
256 ppr_preds :: [IfacePredType] -> SDoc
257 ppr_preds [pred] = ppr pred -- No parens
258 ppr_preds preds = parens (sep (punctuate comma (map ppr preds)))
261 pabrackets :: SDoc -> SDoc
262 pabrackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")
265 %************************************************************************
267 Conversion from Type to IfaceType
269 %************************************************************************
273 toIfaceTvBndr :: TyVar -> (FastString, IfaceType)
274 toIfaceTvBndr tyvar = (occNameFS (getOccName tyvar), toIfaceKind (tyVarKind tyvar))
275 toIfaceIdBndr :: Id -> (FastString, IfaceType)
276 toIfaceIdBndr id = (occNameFS (getOccName id), toIfaceType (idType id))
277 toIfaceTvBndrs :: [TyVar] -> [(FastString, IfaceType)]
278 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
280 toIfaceBndr :: Var -> IfaceBndr
282 | isId var = IfaceIdBndr (toIfaceIdBndr var)
283 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
285 toIfaceKind :: Type -> IfaceType
286 toIfaceKind = toIfaceType
288 ---------------------
289 toIfaceType :: Type -> IfaceType
290 -- Synonyms are retained in the interface type
291 toIfaceType (TyVarTy tv) =
292 IfaceTyVar (occNameFS (getOccName tv))
293 toIfaceType (AppTy t1 t2) =
294 IfaceAppTy (toIfaceType t1) (toIfaceType t2)
295 toIfaceType (FunTy t1 t2) =
296 IfaceFunTy (toIfaceType t1) (toIfaceType t2)
297 toIfaceType (TyConApp tc tys) =
298 IfaceTyConApp (toIfaceTyCon tc) (toIfaceTypes tys)
299 toIfaceType (ForAllTy tv t) =
300 IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType t)
301 toIfaceType (PredTy st) =
302 IfacePredTy (toIfacePred st)
305 -- A little bit of (perhaps optional) trickiness here. When
306 -- compiling Data.Tuple, the tycons are not TupleTyCons, although
307 -- they have a wired-in name. But we'd like to dump them into the Iface
308 -- as a tuple tycon, to save lookups when reading the interface
309 -- Hence a tuple tycon may 'miss' in toIfaceTyCon, but then
310 -- toIfaceTyCon_name will still catch it.
312 toIfaceTyCon :: TyCon -> IfaceTyCon
314 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
315 | otherwise = toIfaceTyCon_name (tyConName tc)
317 toIfaceTyCon_name :: Name -> IfaceTyCon
319 | Just (ATyCon tc) <- wiredInNameTyThing_maybe nm
320 = toIfaceWiredInTyCon tc nm
324 toIfaceWiredInTyCon :: TyCon -> Name -> IfaceTyCon
325 toIfaceWiredInTyCon tc nm
326 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
327 | nm == intTyConName = IfaceIntTc
328 | nm == boolTyConName = IfaceBoolTc
329 | nm == charTyConName = IfaceCharTc
330 | nm == listTyConName = IfaceListTc
331 | nm == parrTyConName = IfacePArrTc
332 | nm == liftedTypeKindTyConName = IfaceLiftedTypeKindTc
333 | nm == unliftedTypeKindTyConName = IfaceUnliftedTypeKindTc
334 | nm == openTypeKindTyConName = IfaceOpenTypeKindTc
335 | nm == argTypeKindTyConName = IfaceArgTypeKindTc
336 | nm == ubxTupleKindTyConName = IfaceUbxTupleKindTc
337 | otherwise = IfaceTc nm
340 toIfaceTypes :: [Type] -> [IfaceType]
341 toIfaceTypes ts = map toIfaceType ts
344 toIfacePred :: PredType -> IfacePredType
345 toIfacePred (ClassP cls ts) =
346 IfaceClassP (getName cls) (toIfaceTypes ts)
347 toIfacePred (IParam ip t) =
348 IfaceIParam (mapIPName getOccName ip) (toIfaceType t)
349 toIfacePred (EqPred ty1 ty2) =
350 IfaceEqPred (toIfaceType ty1) (toIfaceType ty2)
353 toIfaceContext :: ThetaType -> IfaceContext
354 toIfaceContext cs = map toIfacePred cs