[project @ 2005-01-28 17:44:55 by simonpj]
[ghc-hetmet.git] / ghc / compiler / iface / IfaceType.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4
5         This module defines interface types and binders
6
7 \begin{code}
8 module IfaceType (
9         IfaceType(..), IfaceKind, IfacePredType(..), IfaceTyCon(..),
10         IfaceContext, IfaceBndr(..), IfaceTvBndr, IfaceIdBndr,
11
12         IfaceExtName(..), mkIfaceExtName, isLocalIfaceExtName,
13         ifaceTyConName, interactiveExtNameFun,
14
15         -- Conversion from Type -> IfaceType
16         toIfaceType, toIfacePred, toIfaceContext, 
17         toIfaceBndr, toIfaceIdBndr, toIfaceTvBndrs, 
18
19         -- Printing
20         pprIfaceType, pprParendIfaceType, pprIfaceContext, 
21         pprIfaceIdBndr, pprIfaceTvBndr, pprIfaceTvBndrs, pprIfaceBndrs,
22         tOP_PREC, tYCON_PREC, noParens, maybeParen, pprIfaceForAllPart
23
24     ) where
25
26 #include "HsVersions.h"
27
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 )
37 import Outputable
38 import FastString
39 \end{code}
40
41         
42 %************************************************************************
43 %*                                                                      *
44                 IfaceExtName
45 %*                                                                      *
46 %************************************************************************
47
48 \begin{code}
49 data IfaceExtName
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
53
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
57
58   | LocalTop OccName                    -- Top-level from the same module as 
59                                         -- the enclosing IfaceDecl
60
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
66
67 isLocalIfaceExtName :: IfaceExtName -> Bool
68 isLocalIfaceExtName (LocalTop _)      = True
69 isLocalIfaceExtName (LocalTopSub _ _) = True
70 isLocalIfaceExtName other             = False
71
72 mkIfaceExtName name = ExtPkg (nameModule name) (nameOccName name)
73         -- Local helper for wired-in names
74
75 interactiveExtNameFun :: PrintUnqualified -> Name-> IfaceExtName
76 interactiveExtNameFun print_unqual name
77   | print_unqual mod occ = LocalTop occ
78   | otherwise            = ExtPkg mod occ
79   where
80     mod = nameModule name
81     occ = nameOccName name
82 \end{code}
83
84
85 %************************************************************************
86 %*                                                                      *
87                 Local (nested) binders
88 %*                                                                      *
89 %************************************************************************
90
91 \begin{code}
92 data IfaceBndr          -- Local (non-top-level) binders
93   = IfaceIdBndr IfaceIdBndr
94   | IfaceTvBndr IfaceTvBndr
95
96 type IfaceIdBndr  = (OccName, IfaceType)        -- OccName, because always local
97 type IfaceTvBndr  = (OccName, IfaceKind)
98
99 -------------------------------
100 type IfaceKind = Kind                   -- Re-use the Kind type, but no KindVars in it
101
102 data IfaceType
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
110
111 data IfacePredType      -- NewTypes are handled as ordinary TyConApps
112   = IfaceClassP IfaceExtName [IfaceType]
113   | IfaceIParam (IPName OccName) IfaceType
114
115 type IfaceContext = [IfacePredType]
116
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 
122
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)
131 \end{code}
132
133
134 %************************************************************************
135 %*                                                                      *
136                 Functions over IFaceTypes
137 %*                                                                      *
138 %************************************************************************
139
140
141 \begin{code}
142 splitIfaceSigmaTy :: IfaceType -> ([IfaceTvBndr], IfaceContext, IfaceType)
143 -- Mainly for printing purposes
144 splitIfaceSigmaTy ty
145   = (tvs,theta,tau)
146   where
147     (tvs, rho)   = split_foralls ty
148     (theta, tau) = split_rho rho
149
150     split_foralls (IfaceForAllTy tv ty) 
151         = case split_foralls ty of { (tvs, rho) -> (tv:tvs, rho) }
152     split_foralls rho = ([], rho)
153
154     split_rho (IfaceFunTy (IfacePredTy st) ty) 
155         = case split_rho ty of { (sts, tau) -> (st:sts, tau) }
156     split_rho tau = ([], tau)
157 \end{code}
158
159 %************************************************************************
160 %*                                                                      *
161                 Pretty-printing
162 %*                                                                      *
163 %************************************************************************
164
165 Precedence
166 ~~~~~~~~~~
167 @ppr_ty@ takes an @Int@ that is the precedence of the context.
168 The precedence levels are:
169 \begin{description}
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.
173 \end{description}
174
175 \begin{code}
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
179
180 noParens :: SDoc -> SDoc
181 noParens pp = pp
182
183 maybeParen ctxt_prec inner_prec pretty
184   | ctxt_prec < inner_prec = pretty
185   | otherwise              = parens pretty
186 \end{code}
187
188
189 ----------------------------- Printing binders ------------------------------------
190
191 \begin{code}
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?
199
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
204
205 instance Outputable IfaceBndr where
206     ppr (IfaceIdBndr bndr) = pprIfaceIdBndr bndr
207     ppr (IfaceTvBndr bndr) = char '@' <+> pprIfaceTvBndr bndr
208
209 pprIfaceBndrs :: [IfaceBndr] -> SDoc
210 pprIfaceBndrs bs = sep (map ppr bs)
211
212 pprIfaceIdBndr (name, ty) = hsep [ppr name, dcolon, ppr ty]
213
214 pprIfaceTvBndr :: IfaceTvBndr -> SDoc
215 pprIfaceTvBndr (tv, LiftedTypeKind) = ppr tv
216 pprIfaceTvBndr (tv, kind)           = parens (ppr tv <> dcolon <> ppr kind)
217
218 pprIfaceTvBndrs :: [IfaceTvBndr] -> SDoc
219 pprIfaceTvBndrs tyvars = hsep (map pprIfaceTvBndr tyvars)
220 \end{code}
221
222 ----------------------------- Printing IfaceType ------------------------------------
223
224 \begin{code}
225 ---------------------------------
226 instance Outputable IfaceType where
227   ppr ty = pprIfaceTypeForUser ty
228
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)
233  where          
234     (_tvs, theta, tau) = splitIfaceSigmaTy ty
235
236 pprIfaceType, pprParendIfaceType ::IfaceType -> SDoc
237 pprIfaceType       = ppr_ty tOP_PREC
238 pprParendIfaceType = ppr_ty tYCON_PREC
239
240
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
245
246         -- Function types
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)
251   where
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]
256
257 ppr_ty ctxt_prec (IfaceAppTy ty1 ty2)
258   = maybeParen ctxt_prec tYCON_PREC $
259     ppr_ty fUN_PREC ty1 <+> pprParendIfaceType ty2
260
261 ppr_ty ctxt_prec ty@(IfaceForAllTy _ _)
262   = maybeParen ctxt_prec fUN_PREC (pprIfaceForAllPart tvs theta (pprIfaceType tau))
263  where          
264     (tvs, theta, tau) = splitIfaceSigmaTy ty
265     
266 -------------------
267 pprIfaceForAllPart :: [IfaceTvBndr] -> IfaceContext -> SDoc -> SDoc
268 pprIfaceForAllPart tvs ctxt doc 
269   = sep [ppr_tvs, pprIfaceContext ctxt, doc]
270   where
271     ppr_tvs | null tvs  = empty
272             | otherwise = ptext SLIT("forall") <+> pprIfaceTvBndrs tvs <> dot
273
274 -------------------
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))])
284
285 -------------------
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)
290
291 instance Outputable IfaceTyCon where
292   ppr (IfaceTc ext) = ppr ext
293   ppr other_tc      = ppr (ifaceTyConName other_tc)
294
295 -------------------
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))) 
300                         <+> ptext SLIT("=>")
301   
302 pabrackets p = ptext SLIT("[:") <> p <> ptext SLIT(":]")
303 \end{code}
304
305 %************************************************************************
306 %*                                                                      *
307         Conversion from Type to IfaceType
308 %*                                                                      *
309 %************************************************************************
310
311 \begin{code}
312 ----------------
313 toIfaceTvBndr tyvar   = (getOccName tyvar, tyVarKind tyvar)
314 toIfaceIdBndr ext id  = (getOccName id,    toIfaceType ext (idType id))
315 toIfaceTvBndrs tyvars = map toIfaceTvBndr tyvars
316
317 toIfaceBndr ext var
318   | isId var  = IfaceIdBndr (toIfaceIdBndr ext var)
319   | otherwise = IfaceTvBndr (toIfaceTvBndr var)
320
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
331
332 ----------------
333 mkIfaceTc :: (Name -> IfaceExtName) -> TyCon -> IfaceTyCon
334 mkIfaceTc ext tc 
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)
342   where
343     nm = getName tc
344
345 ----------------
346 toIfaceTypes ext ts = map (toIfaceType ext) ts
347
348 ----------------
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)
351
352 ----------------
353 toIfaceContext :: (Name -> IfaceExtName) -> ThetaType -> IfaceContext
354 toIfaceContext ext cs = map (toIfacePred ext) cs
355 \end{code}
356