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 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
17 IfaceType(..), IfaceKind, IfacePredType(..), IfaceTyCon(..),
18 IfaceContext, IfaceBndr(..), IfaceTvBndr, IfaceIdBndr, IfaceCoercion,
21 -- Conversion from Type -> IfaceType
22 toIfaceType, toIfacePred, toIfaceContext,
23 toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs,
24 toIfaceTyCon, toIfaceTyCon_name,
27 pprIfaceType, pprParendIfaceType, pprIfaceContext,
28 pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
29 tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
43 %************************************************************************
45 Local (nested) binders
47 %************************************************************************
50 data IfaceBndr -- Local (non-top-level) binders
51 = IfaceIdBndr {-# UNPACK #-} !IfaceIdBndr
52 | IfaceTvBndr {-# UNPACK #-} !IfaceTvBndr
54 type IfaceIdBndr = (FastString, IfaceType)
55 type IfaceTvBndr = (FastString, IfaceKind)
57 -------------------------------
58 type IfaceKind = IfaceType
59 type IfaceCoercion = IfaceType
62 = IfaceTyVar FastString -- Type variable only, not tycon
63 | IfaceAppTy IfaceType IfaceType
64 | IfaceForAllTy IfaceTvBndr IfaceType
65 | IfacePredTy IfacePredType
66 | IfaceTyConApp IfaceTyCon [IfaceType] -- Not necessarily saturated
67 -- Includes newtypes, synonyms, tuples
68 | IfaceFunTy IfaceType IfaceType
70 data IfacePredType -- NewTypes are handled as ordinary TyConApps
71 = IfaceClassP Name [IfaceType]
72 | IfaceIParam (IPName OccName) IfaceType
73 | IfaceEqPred IfaceType IfaceType
75 type IfaceContext = [IfacePredType]
77 -- NB: If you add a data constructor, remember to add a case to
79 data IfaceTyCon -- Abbreviations for common tycons with known names
80 = IfaceTc Name -- The common case
81 | IfaceIntTc | IfaceBoolTc | IfaceCharTc
82 | IfaceListTc | IfacePArrTc
83 | IfaceTupTc Boxity Arity
84 | IfaceLiftedTypeKindTc | IfaceOpenTypeKindTc | IfaceUnliftedTypeKindTc
85 | IfaceUbxTupleKindTc | IfaceArgTypeKindTc
88 ifaceTyConName :: IfaceTyCon -> Name
89 ifaceTyConName IfaceIntTc = intTyConName
90 ifaceTyConName IfaceBoolTc = boolTyConName
91 ifaceTyConName IfaceCharTc = charTyConName
92 ifaceTyConName IfaceListTc = listTyConName
93 ifaceTyConName IfacePArrTc = parrTyConName
94 ifaceTyConName (IfaceTupTc bx ar) = getName (tupleTyCon bx ar)
95 ifaceTyConName IfaceLiftedTypeKindTc = liftedTypeKindTyConName
96 ifaceTyConName IfaceOpenTypeKindTc = openTypeKindTyConName
97 ifaceTyConName IfaceUnliftedTypeKindTc = unliftedTypeKindTyConName
98 ifaceTyConName IfaceUbxTupleKindTc = ubxTupleKindTyConName
99 ifaceTyConName IfaceArgTypeKindTc = argTypeKindTyConName
100 ifaceTyConName (IfaceTc ext) = ext
104 %************************************************************************
106 Functions over IFaceTypes
108 %************************************************************************
112 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
113 -- Mainly for printing purposes
117 (tvs, rho) = split_foralls ty
118 (theta, tau) = split_rho rho
120 split_foralls (IfaceForAllTy tv ty)
121 = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
122 split_foralls rho = ([], rho)
124 split_rho (IfaceFunTy (IfacePredTy st) ty)
125 = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
126 split_rho tau = ([], tau)
129 %************************************************************************
133 %************************************************************************
137 @ppr_ty@ takes an @Int@ that is the precedence of the context.
138 The precedence levels are:
140 \item[tOP_PREC] No parens required.
141 \item[fUN_PREC] Left hand argument of a function arrow.
142 \item[tYCON_PREC] Argument of a type constructor.
146 tOP_PREC = (0 :: Int) -- type in ParseIface.y
147 fUN_PREC = (1 :: Int) -- btype in ParseIface.y
148 tYCON_PREC = (2 :: Int) -- atype in ParseIface.y
150 noParens :: SDoc -> SDoc
153 maybeParen ctxt_prec inner_prec pretty
154 | ctxt_prec < inner_prec = pretty
155 | otherwise = parens pretty
159 ----------------------------- Printing binders ------------------------------------
162 instance Outputable IfaceBndr where
163 ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
164 ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
166 pprIfaceBndrs :: [IfaceBndr] -> SDoc
167 pprIfaceBndrs bs = sep (map ppr bs)
169 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
171 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
172 pprIfaceTvBndr (tv, IfaceTyConApp IfaceLiftedTypeKindTc [])
174 pprIfaceTvBndr (tv, kind) = parens (ppr tv <> dcolon <> ppr kind)
175 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
176 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
179 ----------------------------- Printing IfaceType ------------------------------------
182 ---------------------------------
183 instance Outputable IfaceType where
184 ppr ty = pprIfaceType ty
186 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
187 pprIfaceType = ppr_ty tOP_PREC
188 pprParendIfaceType = ppr_ty tYCON_PREC
191 ppr_ty :: Int -> IfaceType -> SDoc
192 ppr_ty ctxt_prec (IfaceTyVar tyvar) = ppr tyvar
193 ppr_ty ctxt_prec (IfaceTyConApp tc tys) = ppr_tc_app ctxt_prec tc tys
194 ppr_ty ctxt_prec (IfacePredTy st) = ppr st
197 ppr_ty ctxt_prec (IfaceFunTy ty1 ty2)
198 = -- We don't want to lose synonyms, so we mustn't use splitFunTys here.
199 maybeParen ctxt_prec fUN_PREC $
200 sep (ppr_ty fUN_PREC ty1 : ppr_fun_tail ty2)
202 ppr_fun_tail (IfaceFunTy ty1 ty2)
203 = (arrow <+> ppr_ty fUN_PREC ty1) : ppr_fun_tail ty2
204 ppr_fun_tail other_ty
205 = [arrow <+> pprIfaceType other_ty]
207 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
208 = maybeParen ctxt_prec tYCON_PREC $
209 ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
211 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
212 = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
214 (tvs, theta, tau) = splitIfaceSigmaTy ty
217 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
218 pprIfaceForAllPart tvs ctxt doc
219 = sep [ppr_tvs, pprIfaceContext ctxt, doc]
221 ppr_tvs | null tvs = empty
222 | otherwise = ptext (sLit "forall") <+> pprIfaceTvBndrs tvs <> dot
225 ppr_tc_app ctxt_prec tc [] = ppr_tc tc
226 ppr_tc_app ctxt_prec IfaceListTc [ty] = brackets (pprIfaceType ty)
227 ppr_tc_app ctxt_prec IfacePArrTc [ty] = pabrackets (pprIfaceType ty)
228 ppr_tc_app ctxt_prec (IfaceTupTc bx arity) tys
229 | arity == length tys
230 = tupleParens bx (sep (punctuate comma (map pprIfaceType tys)))
231 ppr_tc_app ctxt_prec tc tys
232 = maybeParen ctxt_prec tYCON_PREC
233 (sep [ppr_tc tc, nest 4 (sep (map pprParendIfaceType tys))])
235 ppr_tc :: IfaceTyCon -> SDoc
236 -- Wrap infix type constructors in parens
237 ppr_tc tc@(IfaceTc ext_nm) = parenSymOcc (getOccName ext_nm) (ppr tc)
241 instance Outputable IfacePredType where
242 -- Print without parens
243 ppr (IfaceEqPred ty1 ty2)= hsep [ppr ty1, ptext (sLit ":=:"), ppr ty2]
244 ppr (IfaceIParam ip ty) = hsep [ppr ip, dcolon, ppr ty]
245 ppr (IfaceClassP cls ts) = parenSymOcc (getOccName cls) (ppr cls)
246 <+> sep (map pprParendIfaceType ts)
248 instance Outputable IfaceTyCon where
249 ppr (IfaceTc ext) = ppr ext
250 ppr other_tc = ppr (ifaceTyConName other_tc)
253 pprIfaceContext :: IfaceContext -> SDoc
254 -- Prints "(C a, D b) =>", including the arrow
255 pprIfaceContext [] = empty
256 pprIfaceContext theta = ppr_preds theta <+> ptext (sLit "=>")
258 ppr_preds [pred] = ppr pred -- No parens
259 ppr_preds preds = parens (sep (punctuate comma (map ppr preds)))
262 pabrackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")
265 %************************************************************************
267 Conversion from Type to IfaceType
269 %************************************************************************
273 toIfaceTvBndr tyvar = (occNameFS (getOccName tyvar), toIfaceKind (tyVarKind tyvar))
274 toIfaceIdBndr id = (occNameFS (getOccName id), toIfaceType (idType id))
275 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
278 | isId var = IfaceIdBndr (toIfaceIdBndr var)
279 | otherwise = IfaceTvBndr (toIfaceTvBndr var)
281 toIfaceKind = toIfaceType
283 ---------------------
284 toIfaceType :: Type -> IfaceType
285 -- Synonyms are retained in the interface type
286 toIfaceType (TyVarTy tv) =
287 IfaceTyVar (occNameFS (getOccName tv))
288 toIfaceType (AppTy t1 t2) =
289 IfaceAppTy (toIfaceType t1) (toIfaceType t2)
290 toIfaceType (FunTy t1 t2) =
291 IfaceFunTy (toIfaceType t1) (toIfaceType t2)
292 toIfaceType (TyConApp tc tys) =
293 IfaceTyConApp (toIfaceTyCon tc) (toIfaceTypes tys)
294 toIfaceType (ForAllTy tv t) =
295 IfaceForAllTy (toIfaceTvBndr tv) (toIfaceType t)
296 toIfaceType (PredTy st) =
297 IfacePredTy (toIfacePred st)
300 -- A little bit of (perhaps optional) trickiness here. When
301 -- compiling Data.Tuple, the tycons are not TupleTyCons, although
302 -- they have a wired-in name. But we'd like to dump them into the Iface
303 -- as a tuple tycon, to save lookups when reading the interface
304 -- Hence a tuple tycon may 'miss' in toIfaceTyCon, but then
305 -- toIfaceTyCon_name will still catch it.
307 toIfaceTyCon :: TyCon -> IfaceTyCon
309 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
310 | otherwise = toIfaceTyCon_name (tyConName tc)
312 toIfaceTyCon_name :: Name -> IfaceTyCon
314 | Just (ATyCon tc) <- wiredInNameTyThing_maybe nm
315 = toIfaceWiredInTyCon tc nm
319 toIfaceWiredInTyCon :: TyCon -> Name -> IfaceTyCon
320 toIfaceWiredInTyCon tc nm
321 | isTupleTyCon tc = IfaceTupTc (tupleTyConBoxity tc) (tyConArity tc)
322 | nm == intTyConName = IfaceIntTc
323 | nm == boolTyConName = IfaceBoolTc
324 | nm == charTyConName = IfaceCharTc
325 | nm == listTyConName = IfaceListTc
326 | nm == parrTyConName = IfacePArrTc
327 | nm == liftedTypeKindTyConName = IfaceLiftedTypeKindTc
328 | nm == unliftedTypeKindTyConName = IfaceUnliftedTypeKindTc
329 | nm == openTypeKindTyConName = IfaceOpenTypeKindTc
330 | nm == argTypeKindTyConName = IfaceArgTypeKindTc
331 | nm == ubxTupleKindTyConName = IfaceUbxTupleKindTc
332 | otherwise = IfaceTc nm
335 toIfaceTypes ts = map toIfaceType ts
338 toIfacePred (ClassP cls ts) =
339 IfaceClassP (getName cls) (toIfaceTypes ts)
340 toIfacePred (IParam ip t) =
341 IfaceIParam (mapIPName getOccName ip) (toIfaceType t)
342 toIfacePred (EqPred ty1 ty2) =
343 IfaceEqPred (toIfaceType ty1) (toIfaceType ty2)
346 toIfaceContext :: ThetaType -> IfaceContext
347 toIfaceContext cs = map toIfacePred cs